mips.cc 440 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702
  1. // mips.cc -- mips target support for gold.
  2. // Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. // Written by Sasa Stankovic <sasa.stankovic@imgtec.com>
  4. // and Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>.
  5. // This file contains borrowed and adapted code from bfd/elfxx-mips.c.
  6. // This file is part of gold.
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 3 of the License, or
  10. // (at your option) any later version.
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. // MA 02110-1301, USA.
  19. #include "gold.h"
  20. #include <algorithm>
  21. #include <set>
  22. #include <sstream>
  23. #include "demangle.h"
  24. #include "elfcpp.h"
  25. #include "parameters.h"
  26. #include "reloc.h"
  27. #include "mips.h"
  28. #include "object.h"
  29. #include "symtab.h"
  30. #include "layout.h"
  31. #include "output.h"
  32. #include "copy-relocs.h"
  33. #include "target.h"
  34. #include "target-reloc.h"
  35. #include "target-select.h"
  36. #include "tls.h"
  37. #include "errors.h"
  38. #include "gc.h"
  39. #include "attributes.h"
  40. #include "nacl.h"
  41. namespace
  42. {
  43. using namespace gold;
  44. template<int size, bool big_endian>
  45. class Mips_output_data_plt;
  46. template<int size, bool big_endian>
  47. class Mips_output_data_got;
  48. template<int size, bool big_endian>
  49. class Target_mips;
  50. template<int size, bool big_endian>
  51. class Mips_output_section_reginfo;
  52. template<int size, bool big_endian>
  53. class Mips_output_section_options;
  54. template<int size, bool big_endian>
  55. class Mips_output_data_la25_stub;
  56. template<int size, bool big_endian>
  57. class Mips_output_data_mips_stubs;
  58. template<int size>
  59. class Mips_symbol;
  60. template<int size, bool big_endian>
  61. class Mips_got_info;
  62. template<int size, bool big_endian>
  63. class Mips_relobj;
  64. class Mips16_stub_section_base;
  65. template<int size, bool big_endian>
  66. class Mips16_stub_section;
  67. // The ABI says that every symbol used by dynamic relocations must have
  68. // a global GOT entry. Among other things, this provides the dynamic
  69. // linker with a free, directly-indexed cache. The GOT can therefore
  70. // contain symbols that are not referenced by GOT relocations themselves
  71. // (in other words, it may have symbols that are not referenced by things
  72. // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
  73. // GOT relocations are less likely to overflow if we put the associated
  74. // GOT entries towards the beginning. We therefore divide the global
  75. // GOT entries into two areas: "normal" and "reloc-only". Entries in
  76. // the first area can be used for both dynamic relocations and GP-relative
  77. // accesses, while those in the "reloc-only" area are for dynamic
  78. // relocations only.
  79. // These GGA_* ("Global GOT Area") values are organised so that lower
  80. // values are more general than higher values. Also, non-GGA_NONE
  81. // values are ordered by the position of the area in the GOT.
  82. enum Global_got_area
  83. {
  84. GGA_NORMAL = 0,
  85. GGA_RELOC_ONLY = 1,
  86. GGA_NONE = 2
  87. };
  88. // The types of GOT entries needed for this platform.
  89. // These values are exposed to the ABI in an incremental link.
  90. // Do not renumber existing values without changing the version
  91. // number of the .gnu_incremental_inputs section.
  92. enum Got_type
  93. {
  94. GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
  95. GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
  96. GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
  97. // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
  98. GOT_TYPE_STANDARD_MULTIGOT = 3,
  99. GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
  100. GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
  101. };
  102. // TLS type of GOT entry.
  103. enum Got_tls_type
  104. {
  105. GOT_TLS_NONE = 0,
  106. GOT_TLS_GD = 1,
  107. GOT_TLS_LDM = 2,
  108. GOT_TLS_IE = 4
  109. };
  110. // Values found in the r_ssym field of a relocation entry.
  111. enum Special_relocation_symbol
  112. {
  113. RSS_UNDEF = 0, // None - value is zero.
  114. RSS_GP = 1, // Value of GP.
  115. RSS_GP0 = 2, // Value of GP in object being relocated.
  116. RSS_LOC = 3 // Address of location being relocated.
  117. };
  118. // Whether the section is readonly.
  119. static inline bool
  120. is_readonly_section(Output_section* output_section)
  121. {
  122. elfcpp::Elf_Xword section_flags = output_section->flags();
  123. elfcpp::Elf_Word section_type = output_section->type();
  124. if (section_type == elfcpp::SHT_NOBITS)
  125. return false;
  126. if (section_flags & elfcpp::SHF_WRITE)
  127. return false;
  128. return true;
  129. }
  130. // Return TRUE if a relocation of type R_TYPE from OBJECT might
  131. // require an la25 stub. See also local_pic_function, which determines
  132. // whether the destination function ever requires a stub.
  133. template<int size, bool big_endian>
  134. static inline bool
  135. relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
  136. unsigned int r_type, bool target_is_16_bit_code)
  137. {
  138. // We specifically ignore branches and jumps from EF_PIC objects,
  139. // where the onus is on the compiler or programmer to perform any
  140. // necessary initialization of $25. Sometimes such initialization
  141. // is unnecessary; for example, -mno-shared functions do not use
  142. // the incoming value of $25, and may therefore be called directly.
  143. if (object->is_pic())
  144. return false;
  145. switch (r_type)
  146. {
  147. case elfcpp::R_MIPS_26:
  148. case elfcpp::R_MIPS_PC16:
  149. case elfcpp::R_MIPS_PC21_S2:
  150. case elfcpp::R_MIPS_PC26_S2:
  151. case elfcpp::R_MICROMIPS_26_S1:
  152. case elfcpp::R_MICROMIPS_PC7_S1:
  153. case elfcpp::R_MICROMIPS_PC10_S1:
  154. case elfcpp::R_MICROMIPS_PC16_S1:
  155. case elfcpp::R_MICROMIPS_PC23_S2:
  156. return true;
  157. case elfcpp::R_MIPS16_26:
  158. return !target_is_16_bit_code;
  159. default:
  160. return false;
  161. }
  162. }
  163. // Return true if SYM is a locally-defined PIC function, in the sense
  164. // that it or its fn_stub might need $25 to be valid on entry.
  165. // Note that MIPS16 functions set up $gp using PC-relative instructions,
  166. // so they themselves never need $25 to be valid. Only non-MIPS16
  167. // entry points are of interest here.
  168. template<int size, bool big_endian>
  169. static inline bool
  170. local_pic_function(Mips_symbol<size>* sym)
  171. {
  172. bool def_regular = (sym->source() == Symbol::FROM_OBJECT
  173. && !sym->object()->is_dynamic()
  174. && !sym->is_undefined());
  175. if (sym->is_defined() && def_regular)
  176. {
  177. Mips_relobj<size, big_endian>* object =
  178. static_cast<Mips_relobj<size, big_endian>*>(sym->object());
  179. if ((object->is_pic() || sym->is_pic())
  180. && (!sym->is_mips16()
  181. || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
  182. return true;
  183. }
  184. return false;
  185. }
  186. static inline bool
  187. hi16_reloc(int r_type)
  188. {
  189. return (r_type == elfcpp::R_MIPS_HI16
  190. || r_type == elfcpp::R_MIPS16_HI16
  191. || r_type == elfcpp::R_MICROMIPS_HI16
  192. || r_type == elfcpp::R_MIPS_PCHI16);
  193. }
  194. static inline bool
  195. lo16_reloc(int r_type)
  196. {
  197. return (r_type == elfcpp::R_MIPS_LO16
  198. || r_type == elfcpp::R_MIPS16_LO16
  199. || r_type == elfcpp::R_MICROMIPS_LO16
  200. || r_type == elfcpp::R_MIPS_PCLO16);
  201. }
  202. static inline bool
  203. got16_reloc(unsigned int r_type)
  204. {
  205. return (r_type == elfcpp::R_MIPS_GOT16
  206. || r_type == elfcpp::R_MIPS16_GOT16
  207. || r_type == elfcpp::R_MICROMIPS_GOT16);
  208. }
  209. static inline bool
  210. call_lo16_reloc(unsigned int r_type)
  211. {
  212. return (r_type == elfcpp::R_MIPS_CALL_LO16
  213. || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
  214. }
  215. static inline bool
  216. got_lo16_reloc(unsigned int r_type)
  217. {
  218. return (r_type == elfcpp::R_MIPS_GOT_LO16
  219. || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
  220. }
  221. static inline bool
  222. eh_reloc(unsigned int r_type)
  223. {
  224. return (r_type == elfcpp::R_MIPS_EH);
  225. }
  226. static inline bool
  227. got_disp_reloc(unsigned int r_type)
  228. {
  229. return (r_type == elfcpp::R_MIPS_GOT_DISP
  230. || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
  231. }
  232. static inline bool
  233. got_page_reloc(unsigned int r_type)
  234. {
  235. return (r_type == elfcpp::R_MIPS_GOT_PAGE
  236. || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
  237. }
  238. static inline bool
  239. tls_gd_reloc(unsigned int r_type)
  240. {
  241. return (r_type == elfcpp::R_MIPS_TLS_GD
  242. || r_type == elfcpp::R_MIPS16_TLS_GD
  243. || r_type == elfcpp::R_MICROMIPS_TLS_GD);
  244. }
  245. static inline bool
  246. tls_gottprel_reloc(unsigned int r_type)
  247. {
  248. return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
  249. || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
  250. || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
  251. }
  252. static inline bool
  253. tls_ldm_reloc(unsigned int r_type)
  254. {
  255. return (r_type == elfcpp::R_MIPS_TLS_LDM
  256. || r_type == elfcpp::R_MIPS16_TLS_LDM
  257. || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
  258. }
  259. static inline bool
  260. mips16_call_reloc(unsigned int r_type)
  261. {
  262. return (r_type == elfcpp::R_MIPS16_26
  263. || r_type == elfcpp::R_MIPS16_CALL16);
  264. }
  265. static inline bool
  266. jal_reloc(unsigned int r_type)
  267. {
  268. return (r_type == elfcpp::R_MIPS_26
  269. || r_type == elfcpp::R_MIPS16_26
  270. || r_type == elfcpp::R_MICROMIPS_26_S1);
  271. }
  272. static inline bool
  273. micromips_branch_reloc(unsigned int r_type)
  274. {
  275. return (r_type == elfcpp::R_MICROMIPS_26_S1
  276. || r_type == elfcpp::R_MICROMIPS_PC16_S1
  277. || r_type == elfcpp::R_MICROMIPS_PC10_S1
  278. || r_type == elfcpp::R_MICROMIPS_PC7_S1);
  279. }
  280. // Check if R_TYPE is a MIPS16 reloc.
  281. static inline bool
  282. mips16_reloc(unsigned int r_type)
  283. {
  284. switch (r_type)
  285. {
  286. case elfcpp::R_MIPS16_26:
  287. case elfcpp::R_MIPS16_GPREL:
  288. case elfcpp::R_MIPS16_GOT16:
  289. case elfcpp::R_MIPS16_CALL16:
  290. case elfcpp::R_MIPS16_HI16:
  291. case elfcpp::R_MIPS16_LO16:
  292. case elfcpp::R_MIPS16_TLS_GD:
  293. case elfcpp::R_MIPS16_TLS_LDM:
  294. case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
  295. case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
  296. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  297. case elfcpp::R_MIPS16_TLS_TPREL_HI16:
  298. case elfcpp::R_MIPS16_TLS_TPREL_LO16:
  299. return true;
  300. default:
  301. return false;
  302. }
  303. }
  304. // Check if R_TYPE is a microMIPS reloc.
  305. static inline bool
  306. micromips_reloc(unsigned int r_type)
  307. {
  308. switch (r_type)
  309. {
  310. case elfcpp::R_MICROMIPS_26_S1:
  311. case elfcpp::R_MICROMIPS_HI16:
  312. case elfcpp::R_MICROMIPS_LO16:
  313. case elfcpp::R_MICROMIPS_GPREL16:
  314. case elfcpp::R_MICROMIPS_LITERAL:
  315. case elfcpp::R_MICROMIPS_GOT16:
  316. case elfcpp::R_MICROMIPS_PC7_S1:
  317. case elfcpp::R_MICROMIPS_PC10_S1:
  318. case elfcpp::R_MICROMIPS_PC16_S1:
  319. case elfcpp::R_MICROMIPS_CALL16:
  320. case elfcpp::R_MICROMIPS_GOT_DISP:
  321. case elfcpp::R_MICROMIPS_GOT_PAGE:
  322. case elfcpp::R_MICROMIPS_GOT_OFST:
  323. case elfcpp::R_MICROMIPS_GOT_HI16:
  324. case elfcpp::R_MICROMIPS_GOT_LO16:
  325. case elfcpp::R_MICROMIPS_SUB:
  326. case elfcpp::R_MICROMIPS_HIGHER:
  327. case elfcpp::R_MICROMIPS_HIGHEST:
  328. case elfcpp::R_MICROMIPS_CALL_HI16:
  329. case elfcpp::R_MICROMIPS_CALL_LO16:
  330. case elfcpp::R_MICROMIPS_SCN_DISP:
  331. case elfcpp::R_MICROMIPS_JALR:
  332. case elfcpp::R_MICROMIPS_HI0_LO16:
  333. case elfcpp::R_MICROMIPS_TLS_GD:
  334. case elfcpp::R_MICROMIPS_TLS_LDM:
  335. case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
  336. case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
  337. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  338. case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
  339. case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
  340. case elfcpp::R_MICROMIPS_GPREL7_S2:
  341. case elfcpp::R_MICROMIPS_PC23_S2:
  342. return true;
  343. default:
  344. return false;
  345. }
  346. }
  347. static inline bool
  348. is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
  349. {
  350. switch (high_reloc)
  351. {
  352. case elfcpp::R_MIPS_HI16:
  353. case elfcpp::R_MIPS_GOT16:
  354. return lo16_reloc == elfcpp::R_MIPS_LO16;
  355. case elfcpp::R_MIPS_PCHI16:
  356. return lo16_reloc == elfcpp::R_MIPS_PCLO16;
  357. case elfcpp::R_MIPS16_HI16:
  358. case elfcpp::R_MIPS16_GOT16:
  359. return lo16_reloc == elfcpp::R_MIPS16_LO16;
  360. case elfcpp::R_MICROMIPS_HI16:
  361. case elfcpp::R_MICROMIPS_GOT16:
  362. return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
  363. default:
  364. return false;
  365. }
  366. }
  367. // This class is used to hold information about one GOT entry.
  368. // There are three types of entry:
  369. //
  370. // (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
  371. // (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
  372. // (2) a SYMBOL address, where SYMBOL is not local to an input object
  373. // (sym != NULL, symndx == -1)
  374. // (3) a TLS LDM slot (there's only one of these per GOT.)
  375. // (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
  376. template<int size, bool big_endian>
  377. class Mips_got_entry
  378. {
  379. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  380. public:
  381. Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
  382. Mips_address addend, unsigned char tls_type,
  383. unsigned int shndx, bool is_section_symbol)
  384. : addend_(addend), symndx_(symndx), tls_type_(tls_type),
  385. is_section_symbol_(is_section_symbol), shndx_(shndx)
  386. { this->d.object = object; }
  387. Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
  388. : addend_(0), symndx_(-1U), tls_type_(tls_type),
  389. is_section_symbol_(false), shndx_(-1U)
  390. { this->d.sym = sym; }
  391. // Return whether this entry is for a local symbol.
  392. bool
  393. is_for_local_symbol() const
  394. { return this->symndx_ != -1U; }
  395. // Return whether this entry is for a global symbol.
  396. bool
  397. is_for_global_symbol() const
  398. { return this->symndx_ == -1U; }
  399. // Return the hash of this entry.
  400. size_t
  401. hash() const
  402. {
  403. if (this->tls_type_ == GOT_TLS_LDM)
  404. return this->symndx_ + (1 << 18);
  405. size_t name_hash_value = gold::string_hash<char>(
  406. (this->symndx_ != -1U)
  407. ? this->d.object->name().c_str()
  408. : this->d.sym->name());
  409. size_t addend = this->addend_;
  410. return name_hash_value ^ this->symndx_ ^ (addend << 16);
  411. }
  412. // Return whether this entry is equal to OTHER.
  413. bool
  414. equals(Mips_got_entry<size, big_endian>* other) const
  415. {
  416. if (this->symndx_ != other->symndx_
  417. || this->tls_type_ != other->tls_type_)
  418. return false;
  419. if (this->tls_type_ == GOT_TLS_LDM)
  420. return true;
  421. return (((this->symndx_ != -1U)
  422. ? (this->d.object == other->d.object)
  423. : (this->d.sym == other->d.sym))
  424. && (this->addend_ == other->addend_));
  425. }
  426. // Return input object that needs this GOT entry.
  427. Mips_relobj<size, big_endian>*
  428. object() const
  429. {
  430. gold_assert(this->symndx_ != -1U);
  431. return this->d.object;
  432. }
  433. // Return local symbol index for local GOT entries.
  434. unsigned int
  435. symndx() const
  436. {
  437. gold_assert(this->symndx_ != -1U);
  438. return this->symndx_;
  439. }
  440. // Return the relocation addend for local GOT entries.
  441. Mips_address
  442. addend() const
  443. { return this->addend_; }
  444. // Return global symbol for global GOT entries.
  445. Mips_symbol<size>*
  446. sym() const
  447. {
  448. gold_assert(this->symndx_ == -1U);
  449. return this->d.sym;
  450. }
  451. // Return whether this is a TLS GOT entry.
  452. bool
  453. is_tls_entry() const
  454. { return this->tls_type_ != GOT_TLS_NONE; }
  455. // Return TLS type of this GOT entry.
  456. unsigned char
  457. tls_type() const
  458. { return this->tls_type_; }
  459. // Return section index of the local symbol for local GOT entries.
  460. unsigned int
  461. shndx() const
  462. { return this->shndx_; }
  463. // Return whether this is a STT_SECTION symbol.
  464. bool
  465. is_section_symbol() const
  466. { return this->is_section_symbol_; }
  467. private:
  468. // The addend.
  469. Mips_address addend_;
  470. // The index of the symbol if we have a local symbol; -1 otherwise.
  471. unsigned int symndx_;
  472. union
  473. {
  474. // The input object for local symbols that needs the GOT entry.
  475. Mips_relobj<size, big_endian>* object;
  476. // If symndx == -1, the global symbol corresponding to this GOT entry. The
  477. // symbol's entry is in the local area if mips_sym->global_got_area is
  478. // GGA_NONE, otherwise it is in the global area.
  479. Mips_symbol<size>* sym;
  480. } d;
  481. // The TLS type of this GOT entry. An LDM GOT entry will be a local
  482. // symbol entry with r_symndx == 0.
  483. unsigned char tls_type_;
  484. // Whether this is a STT_SECTION symbol.
  485. bool is_section_symbol_;
  486. // For local GOT entries, section index of the local symbol.
  487. unsigned int shndx_;
  488. };
  489. // Hash for Mips_got_entry.
  490. template<int size, bool big_endian>
  491. class Mips_got_entry_hash
  492. {
  493. public:
  494. size_t
  495. operator()(Mips_got_entry<size, big_endian>* entry) const
  496. { return entry->hash(); }
  497. };
  498. // Equality for Mips_got_entry.
  499. template<int size, bool big_endian>
  500. class Mips_got_entry_eq
  501. {
  502. public:
  503. bool
  504. operator()(Mips_got_entry<size, big_endian>* e1,
  505. Mips_got_entry<size, big_endian>* e2) const
  506. { return e1->equals(e2); }
  507. };
  508. // Hash for Mips_symbol.
  509. template<int size>
  510. class Mips_symbol_hash
  511. {
  512. public:
  513. size_t
  514. operator()(Mips_symbol<size>* sym) const
  515. { return sym->hash(); }
  516. };
  517. // Got_page_range. This class describes a range of addends: [MIN_ADDEND,
  518. // MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
  519. // increasing MIN_ADDEND.
  520. struct Got_page_range
  521. {
  522. Got_page_range()
  523. : next(NULL), min_addend(0), max_addend(0)
  524. { }
  525. Got_page_range* next;
  526. int min_addend;
  527. int max_addend;
  528. // Return the maximum number of GOT page entries required.
  529. int
  530. get_max_pages()
  531. { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
  532. };
  533. // Got_page_entry. This class describes the range of addends that are applied
  534. // to page relocations against a given symbol.
  535. struct Got_page_entry
  536. {
  537. Got_page_entry()
  538. : object(NULL), symndx(-1U), ranges(NULL)
  539. { }
  540. Got_page_entry(Object* object_, unsigned int symndx_)
  541. : object(object_), symndx(symndx_), ranges(NULL)
  542. { }
  543. // The input object that needs the GOT page entry.
  544. Object* object;
  545. // The index of the symbol, as stored in the relocation r_info.
  546. unsigned int symndx;
  547. // The ranges for this page entry.
  548. Got_page_range* ranges;
  549. };
  550. // Hash for Got_page_entry.
  551. struct Got_page_entry_hash
  552. {
  553. size_t
  554. operator()(Got_page_entry* entry) const
  555. { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
  556. };
  557. // Equality for Got_page_entry.
  558. struct Got_page_entry_eq
  559. {
  560. bool
  561. operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
  562. {
  563. return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
  564. }
  565. };
  566. // This class is used to hold .got information when linking.
  567. template<int size, bool big_endian>
  568. class Mips_got_info
  569. {
  570. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  571. typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
  572. Reloc_section;
  573. typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
  574. // Unordered set of GOT entries.
  575. typedef Unordered_set<Mips_got_entry<size, big_endian>*,
  576. Mips_got_entry_hash<size, big_endian>,
  577. Mips_got_entry_eq<size, big_endian> > Got_entry_set;
  578. // Unordered set of GOT page entries.
  579. typedef Unordered_set<Got_page_entry*,
  580. Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
  581. // Unordered set of global GOT entries.
  582. typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
  583. Global_got_entry_set;
  584. public:
  585. Mips_got_info()
  586. : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
  587. tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
  588. got_entries_(), got_page_entries_(), got_page_offset_start_(0),
  589. got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
  590. offset_(0)
  591. { }
  592. // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
  593. // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
  594. void
  595. record_local_got_symbol(Mips_relobj<size, big_endian>* object,
  596. unsigned int symndx, Mips_address addend,
  597. unsigned int r_type, unsigned int shndx,
  598. bool is_section_symbol);
  599. // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
  600. // in OBJECT. FOR_CALL is true if the caller is only interested in
  601. // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
  602. // relocation.
  603. void
  604. record_global_got_symbol(Mips_symbol<size>* mips_sym,
  605. Mips_relobj<size, big_endian>* object,
  606. unsigned int r_type, bool dyn_reloc, bool for_call);
  607. // Add ENTRY to master GOT and to OBJECT's GOT.
  608. void
  609. record_got_entry(Mips_got_entry<size, big_endian>* entry,
  610. Mips_relobj<size, big_endian>* object);
  611. // Record that OBJECT has a page relocation against symbol SYMNDX and
  612. // that ADDEND is the addend for that relocation.
  613. void
  614. record_got_page_entry(Mips_relobj<size, big_endian>* object,
  615. unsigned int symndx, int addend);
  616. // Create all entries that should be in the local part of the GOT.
  617. void
  618. add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
  619. // Create GOT page entries.
  620. void
  621. add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
  622. // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
  623. void
  624. add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
  625. unsigned int non_reloc_only_global_gotno);
  626. // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
  627. void
  628. add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
  629. // Create TLS GOT entries.
  630. void
  631. add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
  632. // Decide whether the symbol needs an entry in the global part of the primary
  633. // GOT, setting global_got_area accordingly. Count the number of global
  634. // symbols that are in the primary GOT only because they have dynamic
  635. // relocations R_MIPS_REL32 against them (reloc_only_gotno).
  636. void
  637. count_got_symbols(Symbol_table* symtab);
  638. // Return the offset of GOT page entry for VALUE.
  639. unsigned int
  640. get_got_page_offset(Mips_address value,
  641. Mips_output_data_got<size, big_endian>* got);
  642. // Count the number of GOT entries required.
  643. void
  644. count_got_entries();
  645. // Count the number of GOT entries required by ENTRY. Accumulate the result.
  646. void
  647. count_got_entry(Mips_got_entry<size, big_endian>* entry);
  648. // Add FROM's GOT entries.
  649. void
  650. add_got_entries(Mips_got_info<size, big_endian>* from);
  651. // Add FROM's GOT page entries.
  652. void
  653. add_got_page_count(Mips_got_info<size, big_endian>* from);
  654. // Return GOT size.
  655. unsigned int
  656. got_size() const
  657. { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
  658. + this->tls_gotno_) * size/8);
  659. }
  660. // Return the number of local GOT entries.
  661. unsigned int
  662. local_gotno() const
  663. { return this->local_gotno_; }
  664. // Return the maximum number of page GOT entries needed.
  665. unsigned int
  666. page_gotno() const
  667. { return this->page_gotno_; }
  668. // Return the number of global GOT entries.
  669. unsigned int
  670. global_gotno() const
  671. { return this->global_gotno_; }
  672. // Set the number of global GOT entries.
  673. void
  674. set_global_gotno(unsigned int global_gotno)
  675. { this->global_gotno_ = global_gotno; }
  676. // Return the number of GGA_RELOC_ONLY global GOT entries.
  677. unsigned int
  678. reloc_only_gotno() const
  679. { return this->reloc_only_gotno_; }
  680. // Return the number of TLS GOT entries.
  681. unsigned int
  682. tls_gotno() const
  683. { return this->tls_gotno_; }
  684. // Return the GOT type for this GOT. Used for multi-GOT links only.
  685. unsigned int
  686. multigot_got_type(unsigned int got_type) const
  687. {
  688. switch (got_type)
  689. {
  690. case GOT_TYPE_STANDARD:
  691. return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
  692. case GOT_TYPE_TLS_OFFSET:
  693. return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
  694. case GOT_TYPE_TLS_PAIR:
  695. return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
  696. default:
  697. gold_unreachable();
  698. }
  699. }
  700. // Remove lazy-binding stubs for global symbols in this GOT.
  701. void
  702. remove_lazy_stubs(Target_mips<size, big_endian>* target);
  703. // Return offset of this GOT from the start of .got section.
  704. unsigned int
  705. offset() const
  706. { return this->offset_; }
  707. // Set offset of this GOT from the start of .got section.
  708. void
  709. set_offset(unsigned int offset)
  710. { this->offset_ = offset; }
  711. // Set index of this GOT in multi-GOT links.
  712. void
  713. set_index(unsigned int index)
  714. { this->index_ = index; }
  715. // Return next GOT in multi-GOT links.
  716. Mips_got_info<size, big_endian>*
  717. next() const
  718. { return this->next_; }
  719. // Set next GOT in multi-GOT links.
  720. void
  721. set_next(Mips_got_info<size, big_endian>* next)
  722. { this->next_ = next; }
  723. // Return the offset of TLS LDM entry for this GOT.
  724. unsigned int
  725. tls_ldm_offset() const
  726. { return this->tls_ldm_offset_; }
  727. // Set the offset of TLS LDM entry for this GOT.
  728. void
  729. set_tls_ldm_offset(unsigned int tls_ldm_offset)
  730. { this->tls_ldm_offset_ = tls_ldm_offset; }
  731. Global_got_entry_set&
  732. global_got_symbols()
  733. { return this->global_got_symbols_; }
  734. // Return the GOT_TLS_* type required by relocation type R_TYPE.
  735. static int
  736. mips_elf_reloc_tls_type(unsigned int r_type)
  737. {
  738. if (tls_gd_reloc(r_type))
  739. return GOT_TLS_GD;
  740. if (tls_ldm_reloc(r_type))
  741. return GOT_TLS_LDM;
  742. if (tls_gottprel_reloc(r_type))
  743. return GOT_TLS_IE;
  744. return GOT_TLS_NONE;
  745. }
  746. // Return the number of GOT slots needed for GOT TLS type TYPE.
  747. static int
  748. mips_tls_got_entries(unsigned int type)
  749. {
  750. switch (type)
  751. {
  752. case GOT_TLS_GD:
  753. case GOT_TLS_LDM:
  754. return 2;
  755. case GOT_TLS_IE:
  756. return 1;
  757. case GOT_TLS_NONE:
  758. return 0;
  759. default:
  760. gold_unreachable();
  761. }
  762. }
  763. private:
  764. // The number of local GOT entries.
  765. unsigned int local_gotno_;
  766. // The maximum number of page GOT entries needed.
  767. unsigned int page_gotno_;
  768. // The number of global GOT entries.
  769. unsigned int global_gotno_;
  770. // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
  771. unsigned int reloc_only_gotno_;
  772. // The number of TLS GOT entries.
  773. unsigned int tls_gotno_;
  774. // The offset of TLS LDM entry for this GOT.
  775. unsigned int tls_ldm_offset_;
  776. // All symbols that have global GOT entry.
  777. Global_got_entry_set global_got_symbols_;
  778. // A hash table holding GOT entries.
  779. Got_entry_set got_entries_;
  780. // A hash table of GOT page entries (only used in master GOT).
  781. Got_page_entry_set got_page_entries_;
  782. // The offset of first GOT page entry for this GOT.
  783. unsigned int got_page_offset_start_;
  784. // The offset of next available GOT page entry for this GOT.
  785. unsigned int got_page_offset_next_;
  786. // A hash table that maps GOT page entry value to the GOT offset where
  787. // the entry is located.
  788. Got_page_offsets got_page_offsets_;
  789. // In multi-GOT links, a pointer to the next GOT.
  790. Mips_got_info<size, big_endian>* next_;
  791. // Index of this GOT in multi-GOT links.
  792. unsigned int index_;
  793. // The offset of this GOT in multi-GOT links.
  794. unsigned int offset_;
  795. };
  796. // This is a helper class used during relocation scan. It records GOT16 addend.
  797. template<int size, bool big_endian>
  798. struct got16_addend
  799. {
  800. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  801. got16_addend(const Sized_relobj_file<size, big_endian>* _object,
  802. unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
  803. Mips_address _addend)
  804. : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
  805. addend(_addend)
  806. { }
  807. const Sized_relobj_file<size, big_endian>* object;
  808. unsigned int shndx;
  809. unsigned int r_type;
  810. unsigned int r_sym;
  811. Mips_address addend;
  812. };
  813. // .MIPS.abiflags section content
  814. template<bool big_endian>
  815. struct Mips_abiflags
  816. {
  817. typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
  818. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
  819. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
  820. Mips_abiflags()
  821. : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
  822. cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
  823. { }
  824. // Version of flags structure.
  825. Valtype16 version;
  826. // The level of the ISA: 1-5, 32, 64.
  827. Valtype8 isa_level;
  828. // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
  829. Valtype8 isa_rev;
  830. // The size of general purpose registers.
  831. Valtype8 gpr_size;
  832. // The size of co-processor 1 registers.
  833. Valtype8 cpr1_size;
  834. // The size of co-processor 2 registers.
  835. Valtype8 cpr2_size;
  836. // The floating-point ABI.
  837. Valtype8 fp_abi;
  838. // Processor-specific extension.
  839. Valtype32 isa_ext;
  840. // Mask of ASEs used.
  841. Valtype32 ases;
  842. // Mask of general flags.
  843. Valtype32 flags1;
  844. Valtype32 flags2;
  845. };
  846. // Mips_symbol class. Holds additional symbol information needed for Mips.
  847. template<int size>
  848. class Mips_symbol : public Sized_symbol<size>
  849. {
  850. public:
  851. Mips_symbol()
  852. : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
  853. has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
  854. pointer_equality_needed_(false), global_got_area_(GGA_NONE),
  855. global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
  856. needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
  857. comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
  858. mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
  859. { }
  860. // Return whether this is a MIPS16 symbol.
  861. bool
  862. is_mips16() const
  863. {
  864. // (st_other & STO_MIPS16) == STO_MIPS16
  865. return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
  866. == elfcpp::STO_MIPS16 >> 2);
  867. }
  868. // Return whether this is a microMIPS symbol.
  869. bool
  870. is_micromips() const
  871. {
  872. // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
  873. return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
  874. == elfcpp::STO_MICROMIPS >> 2);
  875. }
  876. // Return whether the symbol needs MIPS16 fn_stub.
  877. bool
  878. need_fn_stub() const
  879. { return this->need_fn_stub_; }
  880. // Set that the symbol needs MIPS16 fn_stub.
  881. void
  882. set_need_fn_stub()
  883. { this->need_fn_stub_ = true; }
  884. // Return whether this symbol is referenced by branch relocations from
  885. // any non-PIC input file.
  886. bool
  887. has_nonpic_branches() const
  888. { return this->has_nonpic_branches_; }
  889. // Set that this symbol is referenced by branch relocations from
  890. // any non-PIC input file.
  891. void
  892. set_has_nonpic_branches()
  893. { this->has_nonpic_branches_ = true; }
  894. // Return the offset of the la25 stub for this symbol from the start of the
  895. // la25 stub section.
  896. unsigned int
  897. la25_stub_offset() const
  898. { return this->la25_stub_offset_; }
  899. // Set the offset of the la25 stub for this symbol from the start of the
  900. // la25 stub section.
  901. void
  902. set_la25_stub_offset(unsigned int offset)
  903. { this->la25_stub_offset_ = offset; }
  904. // Return whether the symbol has la25 stub. This is true if this symbol is
  905. // for a PIC function, and there are non-PIC branches and jumps to it.
  906. bool
  907. has_la25_stub() const
  908. { return this->la25_stub_offset_ != -1U; }
  909. // Return whether there is a relocation against this symbol that must be
  910. // resolved by the static linker (that is, the relocation cannot possibly
  911. // be made dynamic).
  912. bool
  913. has_static_relocs() const
  914. { return this->has_static_relocs_; }
  915. // Set that there is a relocation against this symbol that must be resolved
  916. // by the static linker (that is, the relocation cannot possibly be made
  917. // dynamic).
  918. void
  919. set_has_static_relocs()
  920. { this->has_static_relocs_ = true; }
  921. // Return whether we must not create a lazy-binding stub for this symbol.
  922. bool
  923. no_lazy_stub() const
  924. { return this->no_lazy_stub_; }
  925. // Set that we must not create a lazy-binding stub for this symbol.
  926. void
  927. set_no_lazy_stub()
  928. { this->no_lazy_stub_ = true; }
  929. // Return the offset of the lazy-binding stub for this symbol from the start
  930. // of .MIPS.stubs section.
  931. unsigned int
  932. lazy_stub_offset() const
  933. { return this->lazy_stub_offset_; }
  934. // Set the offset of the lazy-binding stub for this symbol from the start
  935. // of .MIPS.stubs section.
  936. void
  937. set_lazy_stub_offset(unsigned int offset)
  938. { this->lazy_stub_offset_ = offset; }
  939. // Return whether there are any relocations for this symbol where
  940. // pointer equality matters.
  941. bool
  942. pointer_equality_needed() const
  943. { return this->pointer_equality_needed_; }
  944. // Set that there are relocations for this symbol where pointer equality
  945. // matters.
  946. void
  947. set_pointer_equality_needed()
  948. { this->pointer_equality_needed_ = true; }
  949. // Return global GOT area where this symbol in located.
  950. Global_got_area
  951. global_got_area() const
  952. { return this->global_got_area_; }
  953. // Set global GOT area where this symbol in located.
  954. void
  955. set_global_got_area(Global_got_area global_got_area)
  956. { this->global_got_area_ = global_got_area; }
  957. // Return the global GOT offset for this symbol. For multi-GOT links, this
  958. // returns the offset from the start of .got section to the first GOT entry
  959. // for the symbol. Note that in multi-GOT links the symbol can have entry
  960. // in more than one GOT.
  961. unsigned int
  962. global_gotoffset() const
  963. { return this->global_gotoffset_; }
  964. // Set the global GOT offset for this symbol. Note that in multi-GOT links
  965. // the symbol can have entry in more than one GOT. This method will set
  966. // the offset only if it is less than current offset.
  967. void
  968. set_global_gotoffset(unsigned int offset)
  969. {
  970. if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
  971. this->global_gotoffset_ = offset;
  972. }
  973. // Return whether all GOT relocations for this symbol are for calls.
  974. bool
  975. got_only_for_calls() const
  976. { return this->got_only_for_calls_; }
  977. // Set that there is a GOT relocation for this symbol that is not for call.
  978. void
  979. set_got_not_only_for_calls()
  980. { this->got_only_for_calls_ = false; }
  981. // Return whether this is a PIC symbol.
  982. bool
  983. is_pic() const
  984. {
  985. // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
  986. return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
  987. == (elfcpp::STO_MIPS_PIC >> 2));
  988. }
  989. // Set the flag in st_other field that marks this symbol as PIC.
  990. void
  991. set_pic()
  992. {
  993. if (this->is_mips16())
  994. // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
  995. this->set_nonvis((this->nonvis()
  996. & ~((elfcpp::STO_MIPS16 >> 2)
  997. | (elfcpp::STO_MIPS_FLAGS >> 2)))
  998. | (elfcpp::STO_MIPS_PIC >> 2));
  999. else
  1000. // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
  1001. this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
  1002. | (elfcpp::STO_MIPS_PIC >> 2));
  1003. }
  1004. // Set the flag in st_other field that marks this symbol as PLT.
  1005. void
  1006. set_mips_plt()
  1007. {
  1008. if (this->is_mips16())
  1009. // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
  1010. this->set_nonvis((this->nonvis()
  1011. & ((elfcpp::STO_MIPS16 >> 2)
  1012. | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
  1013. | (elfcpp::STO_MIPS_PLT >> 2));
  1014. else
  1015. // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
  1016. this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
  1017. | (elfcpp::STO_MIPS_PLT >> 2));
  1018. }
  1019. // Downcast a base pointer to a Mips_symbol pointer.
  1020. static Mips_symbol<size>*
  1021. as_mips_sym(Symbol* sym)
  1022. { return static_cast<Mips_symbol<size>*>(sym); }
  1023. // Downcast a base pointer to a Mips_symbol pointer.
  1024. static const Mips_symbol<size>*
  1025. as_mips_sym(const Symbol* sym)
  1026. { return static_cast<const Mips_symbol<size>*>(sym); }
  1027. // Return whether the symbol has lazy-binding stub.
  1028. bool
  1029. has_lazy_stub() const
  1030. { return this->has_lazy_stub_; }
  1031. // Set whether the symbol has lazy-binding stub.
  1032. void
  1033. set_has_lazy_stub(bool has_lazy_stub)
  1034. { this->has_lazy_stub_ = has_lazy_stub; }
  1035. // Return whether the symbol needs a standard PLT entry.
  1036. bool
  1037. needs_mips_plt() const
  1038. { return this->needs_mips_plt_; }
  1039. // Set whether the symbol needs a standard PLT entry.
  1040. void
  1041. set_needs_mips_plt(bool needs_mips_plt)
  1042. { this->needs_mips_plt_ = needs_mips_plt; }
  1043. // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
  1044. // entry.
  1045. bool
  1046. needs_comp_plt() const
  1047. { return this->needs_comp_plt_; }
  1048. // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
  1049. void
  1050. set_needs_comp_plt(bool needs_comp_plt)
  1051. { this->needs_comp_plt_ = needs_comp_plt; }
  1052. // Return standard PLT entry offset, or -1 if none.
  1053. unsigned int
  1054. mips_plt_offset() const
  1055. { return this->mips_plt_offset_; }
  1056. // Set standard PLT entry offset.
  1057. void
  1058. set_mips_plt_offset(unsigned int mips_plt_offset)
  1059. { this->mips_plt_offset_ = mips_plt_offset; }
  1060. // Return whether the symbol has standard PLT entry.
  1061. bool
  1062. has_mips_plt_offset() const
  1063. { return this->mips_plt_offset_ != -1U; }
  1064. // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
  1065. unsigned int
  1066. comp_plt_offset() const
  1067. { return this->comp_plt_offset_; }
  1068. // Set compressed (MIPS16 or microMIPS) PLT entry offset.
  1069. void
  1070. set_comp_plt_offset(unsigned int comp_plt_offset)
  1071. { this->comp_plt_offset_ = comp_plt_offset; }
  1072. // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
  1073. bool
  1074. has_comp_plt_offset() const
  1075. { return this->comp_plt_offset_ != -1U; }
  1076. // Return MIPS16 fn stub for a symbol.
  1077. template<bool big_endian>
  1078. Mips16_stub_section<size, big_endian>*
  1079. get_mips16_fn_stub() const
  1080. {
  1081. return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
  1082. }
  1083. // Set MIPS16 fn stub for a symbol.
  1084. void
  1085. set_mips16_fn_stub(Mips16_stub_section_base* stub)
  1086. { this->mips16_fn_stub_ = stub; }
  1087. // Return whether symbol has MIPS16 fn stub.
  1088. bool
  1089. has_mips16_fn_stub() const
  1090. { return this->mips16_fn_stub_ != NULL; }
  1091. // Return MIPS16 call stub for a symbol.
  1092. template<bool big_endian>
  1093. Mips16_stub_section<size, big_endian>*
  1094. get_mips16_call_stub() const
  1095. {
  1096. return static_cast<Mips16_stub_section<size, big_endian>*>(
  1097. mips16_call_stub_);
  1098. }
  1099. // Set MIPS16 call stub for a symbol.
  1100. void
  1101. set_mips16_call_stub(Mips16_stub_section_base* stub)
  1102. { this->mips16_call_stub_ = stub; }
  1103. // Return whether symbol has MIPS16 call stub.
  1104. bool
  1105. has_mips16_call_stub() const
  1106. { return this->mips16_call_stub_ != NULL; }
  1107. // Return MIPS16 call_fp stub for a symbol.
  1108. template<bool big_endian>
  1109. Mips16_stub_section<size, big_endian>*
  1110. get_mips16_call_fp_stub() const
  1111. {
  1112. return static_cast<Mips16_stub_section<size, big_endian>*>(
  1113. mips16_call_fp_stub_);
  1114. }
  1115. // Set MIPS16 call_fp stub for a symbol.
  1116. void
  1117. set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
  1118. { this->mips16_call_fp_stub_ = stub; }
  1119. // Return whether symbol has MIPS16 call_fp stub.
  1120. bool
  1121. has_mips16_call_fp_stub() const
  1122. { return this->mips16_call_fp_stub_ != NULL; }
  1123. bool
  1124. get_applied_secondary_got_fixup() const
  1125. { return applied_secondary_got_fixup_; }
  1126. void
  1127. set_applied_secondary_got_fixup()
  1128. { this->applied_secondary_got_fixup_ = true; }
  1129. // Return the hash of this symbol.
  1130. size_t
  1131. hash() const
  1132. {
  1133. return gold::string_hash<char>(this->name());
  1134. }
  1135. private:
  1136. // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
  1137. // appears in any relocs other than a 16 bit call.
  1138. bool need_fn_stub_;
  1139. // True if this symbol is referenced by branch relocations from
  1140. // any non-PIC input file. This is used to determine whether an
  1141. // la25 stub is required.
  1142. bool has_nonpic_branches_;
  1143. // The offset of the la25 stub for this symbol from the start of the
  1144. // la25 stub section.
  1145. unsigned int la25_stub_offset_;
  1146. // True if there is a relocation against this symbol that must be
  1147. // resolved by the static linker (that is, the relocation cannot
  1148. // possibly be made dynamic).
  1149. bool has_static_relocs_;
  1150. // Whether we must not create a lazy-binding stub for this symbol.
  1151. // This is true if the symbol has relocations related to taking the
  1152. // function's address.
  1153. bool no_lazy_stub_;
  1154. // The offset of the lazy-binding stub for this symbol from the start of
  1155. // .MIPS.stubs section.
  1156. unsigned int lazy_stub_offset_;
  1157. // True if there are any relocations for this symbol where pointer equality
  1158. // matters.
  1159. bool pointer_equality_needed_;
  1160. // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
  1161. // in the global part of the GOT.
  1162. Global_got_area global_got_area_;
  1163. // The global GOT offset for this symbol. For multi-GOT links, this is offset
  1164. // from the start of .got section to the first GOT entry for the symbol.
  1165. // Note that in multi-GOT links the symbol can have entry in more than one GOT.
  1166. unsigned int global_gotoffset_;
  1167. // Whether all GOT relocations for this symbol are for calls.
  1168. bool got_only_for_calls_;
  1169. // Whether the symbol has lazy-binding stub.
  1170. bool has_lazy_stub_;
  1171. // Whether the symbol needs a standard PLT entry.
  1172. bool needs_mips_plt_;
  1173. // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
  1174. bool needs_comp_plt_;
  1175. // Standard PLT entry offset, or -1 if none.
  1176. unsigned int mips_plt_offset_;
  1177. // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
  1178. unsigned int comp_plt_offset_;
  1179. // MIPS16 fn stub for a symbol.
  1180. Mips16_stub_section_base* mips16_fn_stub_;
  1181. // MIPS16 call stub for a symbol.
  1182. Mips16_stub_section_base* mips16_call_stub_;
  1183. // MIPS16 call_fp stub for a symbol.
  1184. Mips16_stub_section_base* mips16_call_fp_stub_;
  1185. bool applied_secondary_got_fixup_;
  1186. };
  1187. // Mips16_stub_section class.
  1188. // The mips16 compiler uses a couple of special sections to handle
  1189. // floating point arguments.
  1190. // Section names that look like .mips16.fn.FNNAME contain stubs that
  1191. // copy floating point arguments from the fp regs to the gp regs and
  1192. // then jump to FNNAME. If any 32 bit function calls FNNAME, the
  1193. // call should be redirected to the stub instead. If no 32 bit
  1194. // function calls FNNAME, the stub should be discarded. We need to
  1195. // consider any reference to the function, not just a call, because
  1196. // if the address of the function is taken we will need the stub,
  1197. // since the address might be passed to a 32 bit function.
  1198. // Section names that look like .mips16.call.FNNAME contain stubs
  1199. // that copy floating point arguments from the gp regs to the fp
  1200. // regs and then jump to FNNAME. If FNNAME is a 32 bit function,
  1201. // then any 16 bit function that calls FNNAME should be redirected
  1202. // to the stub instead. If FNNAME is not a 32 bit function, the
  1203. // stub should be discarded.
  1204. // .mips16.call.fp.FNNAME sections are similar, but contain stubs
  1205. // which call FNNAME and then copy the return value from the fp regs
  1206. // to the gp regs. These stubs store the return address in $18 while
  1207. // calling FNNAME; any function which might call one of these stubs
  1208. // must arrange to save $18 around the call. (This case is not
  1209. // needed for 32 bit functions that call 16 bit functions, because
  1210. // 16 bit functions always return floating point values in both
  1211. // $f0/$f1 and $2/$3.)
  1212. // Note that in all cases FNNAME might be defined statically.
  1213. // Therefore, FNNAME is not used literally. Instead, the relocation
  1214. // information will indicate which symbol the section is for.
  1215. // We record any stubs that we find in the symbol table.
  1216. // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
  1217. class Mips16_stub_section_base { };
  1218. template<int size, bool big_endian>
  1219. class Mips16_stub_section : public Mips16_stub_section_base
  1220. {
  1221. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1222. public:
  1223. Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
  1224. : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
  1225. found_r_mips_none_(false)
  1226. {
  1227. gold_assert(object->is_mips16_fn_stub_section(shndx)
  1228. || object->is_mips16_call_stub_section(shndx)
  1229. || object->is_mips16_call_fp_stub_section(shndx));
  1230. }
  1231. // Return the object of this stub section.
  1232. Mips_relobj<size, big_endian>*
  1233. object() const
  1234. { return this->object_; }
  1235. // Return the size of a section.
  1236. uint64_t
  1237. section_size() const
  1238. { return this->object_->section_size(this->shndx_); }
  1239. // Return section index of this stub section.
  1240. unsigned int
  1241. shndx() const
  1242. { return this->shndx_; }
  1243. // Return symbol index, if stub is for a local function.
  1244. unsigned int
  1245. r_sym() const
  1246. { return this->r_sym_; }
  1247. // Return symbol, if stub is for a global function.
  1248. Mips_symbol<size>*
  1249. gsym() const
  1250. { return this->gsym_; }
  1251. // Return whether stub is for a local function.
  1252. bool
  1253. is_for_local_function() const
  1254. { return this->gsym_ == NULL; }
  1255. // This method is called when a new relocation R_TYPE for local symbol R_SYM
  1256. // is found in the stub section. Try to find stub target.
  1257. void
  1258. new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
  1259. {
  1260. // To find target symbol for this stub, trust the first R_MIPS_NONE
  1261. // relocation, if any. Otherwise trust the first relocation, whatever
  1262. // its kind.
  1263. if (this->found_r_mips_none_)
  1264. return;
  1265. if (r_type == elfcpp::R_MIPS_NONE)
  1266. {
  1267. this->r_sym_ = r_sym;
  1268. this->gsym_ = NULL;
  1269. this->found_r_mips_none_ = true;
  1270. }
  1271. else if (!is_target_found())
  1272. this->r_sym_ = r_sym;
  1273. }
  1274. // This method is called when a new relocation R_TYPE for global symbol GSYM
  1275. // is found in the stub section. Try to find stub target.
  1276. void
  1277. new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
  1278. {
  1279. // To find target symbol for this stub, trust the first R_MIPS_NONE
  1280. // relocation, if any. Otherwise trust the first relocation, whatever
  1281. // its kind.
  1282. if (this->found_r_mips_none_)
  1283. return;
  1284. if (r_type == elfcpp::R_MIPS_NONE)
  1285. {
  1286. this->gsym_ = gsym;
  1287. this->r_sym_ = 0;
  1288. this->found_r_mips_none_ = true;
  1289. }
  1290. else if (!is_target_found())
  1291. this->gsym_ = gsym;
  1292. }
  1293. // Return whether we found the stub target.
  1294. bool
  1295. is_target_found() const
  1296. { return this->r_sym_ != 0 || this->gsym_ != NULL; }
  1297. // Return whether this is a fn stub.
  1298. bool
  1299. is_fn_stub() const
  1300. { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
  1301. // Return whether this is a call stub.
  1302. bool
  1303. is_call_stub() const
  1304. { return this->object_->is_mips16_call_stub_section(this->shndx_); }
  1305. // Return whether this is a call_fp stub.
  1306. bool
  1307. is_call_fp_stub() const
  1308. { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
  1309. // Return the output address.
  1310. Mips_address
  1311. output_address() const
  1312. {
  1313. return (this->object_->output_section(this->shndx_)->address()
  1314. + this->object_->output_section_offset(this->shndx_));
  1315. }
  1316. private:
  1317. // The object of this stub section.
  1318. Mips_relobj<size, big_endian>* object_;
  1319. // The section index of this stub section.
  1320. unsigned int shndx_;
  1321. // The symbol index, if stub is for a local function.
  1322. unsigned int r_sym_;
  1323. // The symbol, if stub is for a global function.
  1324. Mips_symbol<size>* gsym_;
  1325. // True if we found R_MIPS_NONE relocation in this stub.
  1326. bool found_r_mips_none_;
  1327. };
  1328. // Mips_relobj class.
  1329. template<int size, bool big_endian>
  1330. class Mips_relobj : public Sized_relobj_file<size, big_endian>
  1331. {
  1332. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1333. typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
  1334. Mips16_stubs_int_map;
  1335. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  1336. public:
  1337. Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
  1338. const typename elfcpp::Ehdr<size, big_endian>& ehdr)
  1339. : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
  1340. processor_specific_flags_(0), local_symbol_is_mips16_(),
  1341. local_symbol_is_micromips_(), mips16_stub_sections_(),
  1342. local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
  1343. local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
  1344. merge_processor_specific_data_(true), got_info_(NULL),
  1345. section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
  1346. section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U),
  1347. attributes_section_data_(NULL), abiflags_(NULL), gprmask_(0),
  1348. cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
  1349. {
  1350. this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
  1351. this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
  1352. }
  1353. ~Mips_relobj()
  1354. { delete this->attributes_section_data_; }
  1355. // Downcast a base pointer to a Mips_relobj pointer. This is
  1356. // not type-safe but we only use Mips_relobj not the base class.
  1357. static Mips_relobj<size, big_endian>*
  1358. as_mips_relobj(Relobj* relobj)
  1359. { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
  1360. // Downcast a base pointer to a Mips_relobj pointer. This is
  1361. // not type-safe but we only use Mips_relobj not the base class.
  1362. static const Mips_relobj<size, big_endian>*
  1363. as_mips_relobj(const Relobj* relobj)
  1364. { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
  1365. // Processor-specific flags in ELF file header. This is valid only after
  1366. // reading symbols.
  1367. elfcpp::Elf_Word
  1368. processor_specific_flags() const
  1369. { return this->processor_specific_flags_; }
  1370. // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
  1371. // index. This is only valid after do_count_local_symbol is called.
  1372. bool
  1373. local_symbol_is_mips16(unsigned int r_sym) const
  1374. {
  1375. gold_assert(r_sym < this->local_symbol_is_mips16_.size());
  1376. return this->local_symbol_is_mips16_[r_sym];
  1377. }
  1378. // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
  1379. // index. This is only valid after do_count_local_symbol is called.
  1380. bool
  1381. local_symbol_is_micromips(unsigned int r_sym) const
  1382. {
  1383. gold_assert(r_sym < this->local_symbol_is_micromips_.size());
  1384. return this->local_symbol_is_micromips_[r_sym];
  1385. }
  1386. // Get or create MIPS16 stub section.
  1387. Mips16_stub_section<size, big_endian>*
  1388. get_mips16_stub_section(unsigned int shndx)
  1389. {
  1390. typename Mips16_stubs_int_map::const_iterator it =
  1391. this->mips16_stub_sections_.find(shndx);
  1392. if (it != this->mips16_stub_sections_.end())
  1393. return (*it).second;
  1394. Mips16_stub_section<size, big_endian>* stub_section =
  1395. new Mips16_stub_section<size, big_endian>(this, shndx);
  1396. this->mips16_stub_sections_.insert(
  1397. std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
  1398. stub_section->shndx(), stub_section));
  1399. return stub_section;
  1400. }
  1401. // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
  1402. // object doesn't have fn stub for R_SYM.
  1403. Mips16_stub_section<size, big_endian>*
  1404. get_local_mips16_fn_stub(unsigned int r_sym) const
  1405. {
  1406. typename Mips16_stubs_int_map::const_iterator it =
  1407. this->local_mips16_fn_stubs_.find(r_sym);
  1408. if (it != this->local_mips16_fn_stubs_.end())
  1409. return (*it).second;
  1410. return NULL;
  1411. }
  1412. // Record that this object has MIPS16 fn stub for local symbol. This method
  1413. // is only called if we decided not to discard the stub.
  1414. void
  1415. add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
  1416. {
  1417. gold_assert(stub->is_for_local_function());
  1418. unsigned int r_sym = stub->r_sym();
  1419. this->local_mips16_fn_stubs_.insert(
  1420. std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
  1421. r_sym, stub));
  1422. }
  1423. // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
  1424. // object doesn't have call stub for R_SYM.
  1425. Mips16_stub_section<size, big_endian>*
  1426. get_local_mips16_call_stub(unsigned int r_sym) const
  1427. {
  1428. typename Mips16_stubs_int_map::const_iterator it =
  1429. this->local_mips16_call_stubs_.find(r_sym);
  1430. if (it != this->local_mips16_call_stubs_.end())
  1431. return (*it).second;
  1432. return NULL;
  1433. }
  1434. // Record that this object has MIPS16 call stub for local symbol. This method
  1435. // is only called if we decided not to discard the stub.
  1436. void
  1437. add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
  1438. {
  1439. gold_assert(stub->is_for_local_function());
  1440. unsigned int r_sym = stub->r_sym();
  1441. this->local_mips16_call_stubs_.insert(
  1442. std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
  1443. r_sym, stub));
  1444. }
  1445. // Record that we found "non 16-bit" call relocation against local symbol
  1446. // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
  1447. // is one.
  1448. void
  1449. add_local_non_16bit_call(unsigned int symndx)
  1450. { this->local_non_16bit_calls_.insert(symndx); }
  1451. // Return true if there is any "non 16-bit" call relocation against local
  1452. // symbol SYMNDX in this object.
  1453. bool
  1454. has_local_non_16bit_call_relocs(unsigned int symndx)
  1455. {
  1456. return (this->local_non_16bit_calls_.find(symndx)
  1457. != this->local_non_16bit_calls_.end());
  1458. }
  1459. // Record that we found 16-bit call relocation R_MIPS16_26 against local
  1460. // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
  1461. // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
  1462. void
  1463. add_local_16bit_call(unsigned int symndx)
  1464. { this->local_16bit_calls_.insert(symndx); }
  1465. // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
  1466. // symbol SYMNDX in this object.
  1467. bool
  1468. has_local_16bit_call_relocs(unsigned int symndx)
  1469. {
  1470. return (this->local_16bit_calls_.find(symndx)
  1471. != this->local_16bit_calls_.end());
  1472. }
  1473. // Get gp value that was used to create this object.
  1474. Mips_address
  1475. gp_value() const
  1476. { return this->gp_; }
  1477. // Return whether the object is a PIC object.
  1478. bool
  1479. is_pic() const
  1480. { return this->is_pic_; }
  1481. // Return whether the object uses N32 ABI.
  1482. bool
  1483. is_n32() const
  1484. { return this->is_n32_; }
  1485. // Return whether the object uses N64 ABI.
  1486. bool
  1487. is_n64() const
  1488. { return size == 64; }
  1489. // Return whether the object uses NewABI conventions.
  1490. bool
  1491. is_newabi() const
  1492. { return this->is_n32() || this->is_n64(); }
  1493. // Return Mips_got_info for this object.
  1494. Mips_got_info<size, big_endian>*
  1495. get_got_info() const
  1496. { return this->got_info_; }
  1497. // Return Mips_got_info for this object. Create new info if it doesn't exist.
  1498. Mips_got_info<size, big_endian>*
  1499. get_or_create_got_info()
  1500. {
  1501. if (!this->got_info_)
  1502. this->got_info_ = new Mips_got_info<size, big_endian>();
  1503. return this->got_info_;
  1504. }
  1505. // Set Mips_got_info for this object.
  1506. void
  1507. set_got_info(Mips_got_info<size, big_endian>* got_info)
  1508. { this->got_info_ = got_info; }
  1509. // Whether a section SHDNX is a MIPS16 stub section. This is only valid
  1510. // after do_read_symbols is called.
  1511. bool
  1512. is_mips16_stub_section(unsigned int shndx)
  1513. {
  1514. return (is_mips16_fn_stub_section(shndx)
  1515. || is_mips16_call_stub_section(shndx)
  1516. || is_mips16_call_fp_stub_section(shndx));
  1517. }
  1518. // Return TRUE if relocations in section SHNDX can refer directly to a
  1519. // MIPS16 function rather than to a hard-float stub. This is only valid
  1520. // after do_read_symbols is called.
  1521. bool
  1522. section_allows_mips16_refs(unsigned int shndx)
  1523. {
  1524. return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
  1525. }
  1526. // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
  1527. // after do_read_symbols is called.
  1528. bool
  1529. is_mips16_fn_stub_section(unsigned int shndx)
  1530. {
  1531. gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
  1532. return this->section_is_mips16_fn_stub_[shndx];
  1533. }
  1534. // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
  1535. // after do_read_symbols is called.
  1536. bool
  1537. is_mips16_call_stub_section(unsigned int shndx)
  1538. {
  1539. gold_assert(shndx < this->section_is_mips16_call_stub_.size());
  1540. return this->section_is_mips16_call_stub_[shndx];
  1541. }
  1542. // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
  1543. // valid after do_read_symbols is called.
  1544. bool
  1545. is_mips16_call_fp_stub_section(unsigned int shndx)
  1546. {
  1547. gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
  1548. return this->section_is_mips16_call_fp_stub_[shndx];
  1549. }
  1550. // Discard MIPS16 stub secions that are not needed.
  1551. void
  1552. discard_mips16_stub_sections(Symbol_table* symtab);
  1553. // Return whether there is a .reginfo section.
  1554. bool
  1555. has_reginfo_section() const
  1556. { return this->has_reginfo_section_; }
  1557. // Return whether we want to merge processor-specific data.
  1558. bool
  1559. merge_processor_specific_data() const
  1560. { return this->merge_processor_specific_data_; }
  1561. // Return gprmask from the .reginfo section of this object.
  1562. Valtype
  1563. gprmask() const
  1564. { return this->gprmask_; }
  1565. // Return cprmask1 from the .reginfo section of this object.
  1566. Valtype
  1567. cprmask1() const
  1568. { return this->cprmask1_; }
  1569. // Return cprmask2 from the .reginfo section of this object.
  1570. Valtype
  1571. cprmask2() const
  1572. { return this->cprmask2_; }
  1573. // Return cprmask3 from the .reginfo section of this object.
  1574. Valtype
  1575. cprmask3() const
  1576. { return this->cprmask3_; }
  1577. // Return cprmask4 from the .reginfo section of this object.
  1578. Valtype
  1579. cprmask4() const
  1580. { return this->cprmask4_; }
  1581. // This is the contents of the .MIPS.abiflags section if there is one.
  1582. Mips_abiflags<big_endian>*
  1583. abiflags()
  1584. { return this->abiflags_; }
  1585. // This is the contents of the .gnu.attribute section if there is one.
  1586. const Attributes_section_data*
  1587. attributes_section_data() const
  1588. { return this->attributes_section_data_; }
  1589. protected:
  1590. // Count the local symbols.
  1591. void
  1592. do_count_local_symbols(Stringpool_template<char>*,
  1593. Stringpool_template<char>*);
  1594. // Read the symbol information.
  1595. void
  1596. do_read_symbols(Read_symbols_data* sd);
  1597. private:
  1598. // The name of the options section.
  1599. const char* mips_elf_options_section_name()
  1600. { return this->is_newabi() ? ".MIPS.options" : ".options"; }
  1601. // processor-specific flags in ELF file header.
  1602. elfcpp::Elf_Word processor_specific_flags_;
  1603. // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
  1604. // This is only valid after do_count_local_symbol is called.
  1605. std::vector<bool> local_symbol_is_mips16_;
  1606. // Bit vector to tell if a local symbol is a microMIPS symbol or not.
  1607. // This is only valid after do_count_local_symbol is called.
  1608. std::vector<bool> local_symbol_is_micromips_;
  1609. // Map from section index to the MIPS16 stub for that section. This contains
  1610. // all stubs found in this object.
  1611. Mips16_stubs_int_map mips16_stub_sections_;
  1612. // Local symbols that have "non 16-bit" call relocation. This relocation
  1613. // would need to refer to a MIPS16 fn stub, if there is one.
  1614. std::set<unsigned int> local_non_16bit_calls_;
  1615. // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
  1616. // call or call_fp stubs will only be needed if there is some R_MIPS16_26
  1617. // relocation that refers to the stub symbol.
  1618. std::set<unsigned int> local_16bit_calls_;
  1619. // Map from local symbol index to the MIPS16 fn stub for that symbol.
  1620. // This contains only the stubs that we decided not to discard.
  1621. Mips16_stubs_int_map local_mips16_fn_stubs_;
  1622. // Map from local symbol index to the MIPS16 call stub for that symbol.
  1623. // This contains only the stubs that we decided not to discard.
  1624. Mips16_stubs_int_map local_mips16_call_stubs_;
  1625. // gp value that was used to create this object.
  1626. Mips_address gp_;
  1627. // Whether the object is a PIC object.
  1628. bool is_pic_ : 1;
  1629. // Whether the object uses N32 ABI.
  1630. bool is_n32_ : 1;
  1631. // Whether the object contains a .reginfo section.
  1632. bool has_reginfo_section_ : 1;
  1633. // Whether we merge processor-specific data of this object to output.
  1634. bool merge_processor_specific_data_ : 1;
  1635. // The Mips_got_info for this object.
  1636. Mips_got_info<size, big_endian>* got_info_;
  1637. // Bit vector to tell if a section is a MIPS16 fn stub section or not.
  1638. // This is only valid after do_read_symbols is called.
  1639. std::vector<bool> section_is_mips16_fn_stub_;
  1640. // Bit vector to tell if a section is a MIPS16 call stub section or not.
  1641. // This is only valid after do_read_symbols is called.
  1642. std::vector<bool> section_is_mips16_call_stub_;
  1643. // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
  1644. // This is only valid after do_read_symbols is called.
  1645. std::vector<bool> section_is_mips16_call_fp_stub_;
  1646. // .pdr section index.
  1647. unsigned int pdr_shndx_;
  1648. // Object attributes if there is a .gnu.attributes section or NULL.
  1649. Attributes_section_data* attributes_section_data_;
  1650. // Object abiflags if there is a .MIPS.abiflags section or NULL.
  1651. Mips_abiflags<big_endian>* abiflags_;
  1652. // gprmask from the .reginfo section of this object.
  1653. Valtype gprmask_;
  1654. // cprmask1 from the .reginfo section of this object.
  1655. Valtype cprmask1_;
  1656. // cprmask2 from the .reginfo section of this object.
  1657. Valtype cprmask2_;
  1658. // cprmask3 from the .reginfo section of this object.
  1659. Valtype cprmask3_;
  1660. // cprmask4 from the .reginfo section of this object.
  1661. Valtype cprmask4_;
  1662. };
  1663. // Mips_output_data_got class.
  1664. template<int size, bool big_endian>
  1665. class Mips_output_data_got : public Output_data_got<size, big_endian>
  1666. {
  1667. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1668. typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
  1669. Reloc_section;
  1670. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  1671. public:
  1672. Mips_output_data_got(Target_mips<size, big_endian>* target,
  1673. Symbol_table* symtab, Layout* layout)
  1674. : Output_data_got<size, big_endian>(), target_(target),
  1675. symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
  1676. first_global_got_dynsym_index_(-1U), primary_got_(NULL),
  1677. secondary_got_relocs_()
  1678. {
  1679. this->master_got_info_ = new Mips_got_info<size, big_endian>();
  1680. this->set_addralign(16);
  1681. }
  1682. // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
  1683. // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
  1684. void
  1685. record_local_got_symbol(Mips_relobj<size, big_endian>* object,
  1686. unsigned int symndx, Mips_address addend,
  1687. unsigned int r_type, unsigned int shndx,
  1688. bool is_section_symbol)
  1689. {
  1690. this->master_got_info_->record_local_got_symbol(object, symndx, addend,
  1691. r_type, shndx,
  1692. is_section_symbol);
  1693. }
  1694. // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
  1695. // in OBJECT. FOR_CALL is true if the caller is only interested in
  1696. // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
  1697. // relocation.
  1698. void
  1699. record_global_got_symbol(Mips_symbol<size>* mips_sym,
  1700. Mips_relobj<size, big_endian>* object,
  1701. unsigned int r_type, bool dyn_reloc, bool for_call)
  1702. {
  1703. this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
  1704. dyn_reloc, for_call);
  1705. }
  1706. // Record that OBJECT has a page relocation against symbol SYMNDX and
  1707. // that ADDEND is the addend for that relocation.
  1708. void
  1709. record_got_page_entry(Mips_relobj<size, big_endian>* object,
  1710. unsigned int symndx, int addend)
  1711. { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
  1712. // Add a static entry for the GOT entry at OFFSET. GSYM is a global
  1713. // symbol and R_TYPE is the code of a dynamic relocation that needs to be
  1714. // applied in a static link.
  1715. void
  1716. add_static_reloc(unsigned int got_offset, unsigned int r_type,
  1717. Mips_symbol<size>* gsym)
  1718. { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
  1719. // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
  1720. // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
  1721. // relocation that needs to be applied in a static link.
  1722. void
  1723. add_static_reloc(unsigned int got_offset, unsigned int r_type,
  1724. Sized_relobj_file<size, big_endian>* relobj,
  1725. unsigned int index)
  1726. {
  1727. this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
  1728. index));
  1729. }
  1730. // Record that global symbol GSYM has R_TYPE dynamic relocation in the
  1731. // secondary GOT at OFFSET.
  1732. void
  1733. add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
  1734. Mips_symbol<size>* gsym)
  1735. {
  1736. this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
  1737. r_type, gsym));
  1738. }
  1739. // Update GOT entry at OFFSET with VALUE.
  1740. void
  1741. update_got_entry(unsigned int offset, Mips_address value)
  1742. {
  1743. elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
  1744. }
  1745. // Return the number of entries in local part of the GOT. This includes
  1746. // local entries, page entries and 2 reserved entries.
  1747. unsigned int
  1748. get_local_gotno() const
  1749. {
  1750. if (!this->multi_got())
  1751. {
  1752. return (2 + this->master_got_info_->local_gotno()
  1753. + this->master_got_info_->page_gotno());
  1754. }
  1755. else
  1756. return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
  1757. }
  1758. // Return dynamic symbol table index of the first symbol with global GOT
  1759. // entry.
  1760. unsigned int
  1761. first_global_got_dynsym_index() const
  1762. { return this->first_global_got_dynsym_index_; }
  1763. // Set dynamic symbol table index of the first symbol with global GOT entry.
  1764. void
  1765. set_first_global_got_dynsym_index(unsigned int index)
  1766. { this->first_global_got_dynsym_index_ = index; }
  1767. // Lay out the GOT. Add local, global and TLS entries. If GOT is
  1768. // larger than 64K, create multi-GOT.
  1769. void
  1770. lay_out_got(Layout* layout, Symbol_table* symtab,
  1771. const Input_objects* input_objects);
  1772. // Create multi-GOT. For every GOT, add local, global and TLS entries.
  1773. void
  1774. lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
  1775. // Attempt to merge GOTs of different input objects.
  1776. void
  1777. merge_gots(const Input_objects* input_objects);
  1778. // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
  1779. // this would lead to overflow, true if they were merged successfully.
  1780. bool
  1781. merge_got_with(Mips_got_info<size, big_endian>* from,
  1782. Mips_relobj<size, big_endian>* object,
  1783. Mips_got_info<size, big_endian>* to);
  1784. // Return the offset of GOT page entry for VALUE. For multi-GOT links,
  1785. // use OBJECT's GOT.
  1786. unsigned int
  1787. get_got_page_offset(Mips_address value,
  1788. const Mips_relobj<size, big_endian>* object)
  1789. {
  1790. Mips_got_info<size, big_endian>* g = (!this->multi_got()
  1791. ? this->master_got_info_
  1792. : object->get_got_info());
  1793. gold_assert(g != NULL);
  1794. return g->get_got_page_offset(value, this);
  1795. }
  1796. // Return the GOT offset of type GOT_TYPE of the global symbol
  1797. // GSYM. For multi-GOT links, use OBJECT's GOT.
  1798. unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
  1799. Mips_relobj<size, big_endian>* object) const
  1800. {
  1801. if (!this->multi_got())
  1802. return gsym->got_offset(got_type);
  1803. else
  1804. {
  1805. Mips_got_info<size, big_endian>* g = object->get_got_info();
  1806. gold_assert(g != NULL);
  1807. return gsym->got_offset(g->multigot_got_type(got_type));
  1808. }
  1809. }
  1810. // Return the GOT offset of type GOT_TYPE of the local symbol
  1811. // SYMNDX.
  1812. unsigned int
  1813. got_offset(unsigned int symndx, unsigned int got_type,
  1814. Sized_relobj_file<size, big_endian>* object,
  1815. uint64_t addend) const
  1816. { return object->local_got_offset(symndx, got_type, addend); }
  1817. // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
  1818. unsigned int
  1819. tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
  1820. {
  1821. Mips_got_info<size, big_endian>* g = (!this->multi_got()
  1822. ? this->master_got_info_
  1823. : object->get_got_info());
  1824. gold_assert(g != NULL);
  1825. return g->tls_ldm_offset();
  1826. }
  1827. // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
  1828. void
  1829. set_tls_ldm_offset(unsigned int tls_ldm_offset,
  1830. Mips_relobj<size, big_endian>* object)
  1831. {
  1832. Mips_got_info<size, big_endian>* g = (!this->multi_got()
  1833. ? this->master_got_info_
  1834. : object->get_got_info());
  1835. gold_assert(g != NULL);
  1836. g->set_tls_ldm_offset(tls_ldm_offset);
  1837. }
  1838. // Return true for multi-GOT links.
  1839. bool
  1840. multi_got() const
  1841. { return this->primary_got_ != NULL; }
  1842. // Return the offset of OBJECT's GOT from the start of .got section.
  1843. unsigned int
  1844. get_got_offset(const Mips_relobj<size, big_endian>* object)
  1845. {
  1846. if (!this->multi_got())
  1847. return 0;
  1848. else
  1849. {
  1850. Mips_got_info<size, big_endian>* g = object->get_got_info();
  1851. return g != NULL ? g->offset() : 0;
  1852. }
  1853. }
  1854. // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
  1855. void
  1856. add_reloc_only_entries()
  1857. { this->master_got_info_->add_reloc_only_entries(this); }
  1858. // Return offset of the primary GOT's entry for global symbol.
  1859. unsigned int
  1860. get_primary_got_offset(const Mips_symbol<size>* sym) const
  1861. {
  1862. gold_assert(sym->global_got_area() != GGA_NONE);
  1863. return (this->get_local_gotno() + sym->dynsym_index()
  1864. - this->first_global_got_dynsym_index()) * size/8;
  1865. }
  1866. // For the entry at offset GOT_OFFSET, return its offset from the gp.
  1867. // Input argument GOT_OFFSET is always global offset from the start of
  1868. // .got section, for both single and multi-GOT links.
  1869. // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
  1870. // links, the return value is object_got_offset - 0x7FF0, where
  1871. // object_got_offset is offset in the OBJECT's GOT.
  1872. int
  1873. gp_offset(unsigned int got_offset,
  1874. const Mips_relobj<size, big_endian>* object) const
  1875. {
  1876. return (this->address() + got_offset
  1877. - this->target_->adjusted_gp_value(object));
  1878. }
  1879. protected:
  1880. // Write out the GOT table.
  1881. void
  1882. do_write(Output_file*);
  1883. private:
  1884. // This class represent dynamic relocations that need to be applied by
  1885. // gold because we are using TLS relocations in a static link.
  1886. class Static_reloc
  1887. {
  1888. public:
  1889. Static_reloc(unsigned int got_offset, unsigned int r_type,
  1890. Mips_symbol<size>* gsym)
  1891. : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
  1892. { this->u_.global.symbol = gsym; }
  1893. Static_reloc(unsigned int got_offset, unsigned int r_type,
  1894. Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
  1895. : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
  1896. {
  1897. this->u_.local.relobj = relobj;
  1898. this->u_.local.index = index;
  1899. }
  1900. // Return the GOT offset.
  1901. unsigned int
  1902. got_offset() const
  1903. { return this->got_offset_; }
  1904. // Relocation type.
  1905. unsigned int
  1906. r_type() const
  1907. { return this->r_type_; }
  1908. // Whether the symbol is global or not.
  1909. bool
  1910. symbol_is_global() const
  1911. { return this->symbol_is_global_; }
  1912. // For a relocation against a global symbol, the global symbol.
  1913. Mips_symbol<size>*
  1914. symbol() const
  1915. {
  1916. gold_assert(this->symbol_is_global_);
  1917. return this->u_.global.symbol;
  1918. }
  1919. // For a relocation against a local symbol, the defining object.
  1920. Sized_relobj_file<size, big_endian>*
  1921. relobj() const
  1922. {
  1923. gold_assert(!this->symbol_is_global_);
  1924. return this->u_.local.relobj;
  1925. }
  1926. // For a relocation against a local symbol, the local symbol index.
  1927. unsigned int
  1928. index() const
  1929. {
  1930. gold_assert(!this->symbol_is_global_);
  1931. return this->u_.local.index;
  1932. }
  1933. private:
  1934. // GOT offset of the entry to which this relocation is applied.
  1935. unsigned int got_offset_;
  1936. // Type of relocation.
  1937. unsigned int r_type_;
  1938. // Whether this relocation is against a global symbol.
  1939. bool symbol_is_global_;
  1940. // A global or local symbol.
  1941. union
  1942. {
  1943. struct
  1944. {
  1945. // For a global symbol, the symbol itself.
  1946. Mips_symbol<size>* symbol;
  1947. } global;
  1948. struct
  1949. {
  1950. // For a local symbol, the object defining object.
  1951. Sized_relobj_file<size, big_endian>* relobj;
  1952. // For a local symbol, the symbol index.
  1953. unsigned int index;
  1954. } local;
  1955. } u_;
  1956. };
  1957. // The target.
  1958. Target_mips<size, big_endian>* target_;
  1959. // The symbol table.
  1960. Symbol_table* symbol_table_;
  1961. // The layout.
  1962. Layout* layout_;
  1963. // Static relocs to be applied to the GOT.
  1964. std::vector<Static_reloc> static_relocs_;
  1965. // .got section view.
  1966. unsigned char* got_view_;
  1967. // The dynamic symbol table index of the first symbol with global GOT entry.
  1968. unsigned int first_global_got_dynsym_index_;
  1969. // The master GOT information.
  1970. Mips_got_info<size, big_endian>* master_got_info_;
  1971. // The primary GOT information.
  1972. Mips_got_info<size, big_endian>* primary_got_;
  1973. // Secondary GOT fixups.
  1974. std::vector<Static_reloc> secondary_got_relocs_;
  1975. };
  1976. // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
  1977. // two ways of creating these interfaces. The first is to add:
  1978. //
  1979. // lui $25,%hi(func)
  1980. // j func
  1981. // addiu $25,$25,%lo(func)
  1982. //
  1983. // to a separate trampoline section. The second is to add:
  1984. //
  1985. // lui $25,%hi(func)
  1986. // addiu $25,$25,%lo(func)
  1987. //
  1988. // immediately before a PIC function "func", but only if a function is at the
  1989. // beginning of the section, and the section is not too heavily aligned (i.e we
  1990. // would need to add no more than 2 nops before the stub.)
  1991. //
  1992. // We only create stubs of the first type.
  1993. template<int size, bool big_endian>
  1994. class Mips_output_data_la25_stub : public Output_section_data
  1995. {
  1996. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1997. public:
  1998. Mips_output_data_la25_stub()
  1999. : Output_section_data(size == 32 ? 4 : 8), symbols_()
  2000. { }
  2001. // Create LA25 stub for a symbol.
  2002. void
  2003. create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
  2004. Mips_symbol<size>* gsym);
  2005. // Return output address of a stub.
  2006. Mips_address
  2007. stub_address(const Mips_symbol<size>* sym) const
  2008. {
  2009. gold_assert(sym->has_la25_stub());
  2010. return this->address() + sym->la25_stub_offset();
  2011. }
  2012. protected:
  2013. void
  2014. do_adjust_output_section(Output_section* os)
  2015. { os->set_entsize(0); }
  2016. private:
  2017. // Template for standard LA25 stub.
  2018. static const uint32_t la25_stub_entry[];
  2019. // Template for microMIPS LA25 stub.
  2020. static const uint32_t la25_stub_micromips_entry[];
  2021. // Set the final size.
  2022. void
  2023. set_final_data_size()
  2024. { this->set_data_size(this->symbols_.size() * 16); }
  2025. // Create a symbol for SYM stub's value and size, to help make the
  2026. // disassembly easier to read.
  2027. void
  2028. create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
  2029. Target_mips<size, big_endian>* target, uint64_t symsize);
  2030. // Write to a map file.
  2031. void
  2032. do_print_to_mapfile(Mapfile* mapfile) const
  2033. { mapfile->print_output_data(this, _(".LA25.stubs")); }
  2034. // Write out the LA25 stub section.
  2035. void
  2036. do_write(Output_file*);
  2037. // Symbols that have LA25 stubs.
  2038. std::vector<Mips_symbol<size>*> symbols_;
  2039. };
  2040. // MIPS-specific relocation writer.
  2041. template<int sh_type, bool dynamic, int size, bool big_endian>
  2042. struct Mips_output_reloc_writer;
  2043. template<int sh_type, bool dynamic, bool big_endian>
  2044. struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
  2045. {
  2046. typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
  2047. typedef std::vector<Output_reloc_type> Relocs;
  2048. static void
  2049. write(typename Relocs::const_iterator p, unsigned char* pov)
  2050. { p->write(pov); }
  2051. };
  2052. template<int sh_type, bool dynamic, bool big_endian>
  2053. struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
  2054. {
  2055. typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
  2056. typedef std::vector<Output_reloc_type> Relocs;
  2057. static void
  2058. write(typename Relocs::const_iterator p, unsigned char* pov)
  2059. {
  2060. elfcpp::Mips64_rel_write<big_endian> orel(pov);
  2061. orel.put_r_offset(p->get_address());
  2062. orel.put_r_sym(p->get_symbol_index());
  2063. orel.put_r_ssym(RSS_UNDEF);
  2064. orel.put_r_type(p->type());
  2065. if (p->type() == elfcpp::R_MIPS_REL32)
  2066. orel.put_r_type2(elfcpp::R_MIPS_64);
  2067. else
  2068. orel.put_r_type2(elfcpp::R_MIPS_NONE);
  2069. orel.put_r_type3(elfcpp::R_MIPS_NONE);
  2070. }
  2071. };
  2072. template<int sh_type, bool dynamic, int size, bool big_endian>
  2073. class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
  2074. size, big_endian>
  2075. {
  2076. public:
  2077. Mips_output_data_reloc(bool sort_relocs)
  2078. : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
  2079. { }
  2080. protected:
  2081. // Write out the data.
  2082. void
  2083. do_write(Output_file* of)
  2084. {
  2085. typedef Mips_output_reloc_writer<sh_type, dynamic, size,
  2086. big_endian> Writer;
  2087. this->template do_write_generic<Writer>(of);
  2088. }
  2089. };
  2090. // A class to handle the PLT data.
  2091. template<int size, bool big_endian>
  2092. class Mips_output_data_plt : public Output_section_data
  2093. {
  2094. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  2095. typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
  2096. size, big_endian> Reloc_section;
  2097. public:
  2098. // Create the PLT section. The ordinary .got section is an argument,
  2099. // since we need to refer to the start.
  2100. Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
  2101. Target_mips<size, big_endian>* target)
  2102. : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
  2103. plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
  2104. target_(target)
  2105. {
  2106. this->rel_ = new Reloc_section(false);
  2107. layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
  2108. elfcpp::SHF_ALLOC, this->rel_,
  2109. ORDER_DYNAMIC_PLT_RELOCS, false);
  2110. }
  2111. // Add an entry to the PLT for a symbol referenced by r_type relocation.
  2112. void
  2113. add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
  2114. // Return the .rel.plt section data.
  2115. Reloc_section*
  2116. rel_plt() const
  2117. { return this->rel_; }
  2118. // Return the number of PLT entries.
  2119. unsigned int
  2120. entry_count() const
  2121. { return this->symbols_.size(); }
  2122. // Return the offset of the first non-reserved PLT entry.
  2123. unsigned int
  2124. first_plt_entry_offset() const
  2125. { return sizeof(plt0_entry_o32); }
  2126. // Return the size of a PLT entry.
  2127. unsigned int
  2128. plt_entry_size() const
  2129. { return sizeof(plt_entry); }
  2130. // Set final PLT offsets. For each symbol, determine whether standard or
  2131. // compressed (MIPS16 or microMIPS) PLT entry is used.
  2132. void
  2133. set_plt_offsets();
  2134. // Return the offset of the first standard PLT entry.
  2135. unsigned int
  2136. first_mips_plt_offset() const
  2137. { return this->plt_header_size_; }
  2138. // Return the offset of the first compressed PLT entry.
  2139. unsigned int
  2140. first_comp_plt_offset() const
  2141. { return this->plt_header_size_ + this->plt_mips_offset_; }
  2142. // Return whether there are any standard PLT entries.
  2143. bool
  2144. has_standard_entries() const
  2145. { return this->plt_mips_offset_ > 0; }
  2146. // Return the output address of standard PLT entry.
  2147. Mips_address
  2148. mips_entry_address(const Mips_symbol<size>* sym) const
  2149. {
  2150. gold_assert (sym->has_mips_plt_offset());
  2151. return (this->address() + this->first_mips_plt_offset()
  2152. + sym->mips_plt_offset());
  2153. }
  2154. // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
  2155. Mips_address
  2156. comp_entry_address(const Mips_symbol<size>* sym) const
  2157. {
  2158. gold_assert (sym->has_comp_plt_offset());
  2159. return (this->address() + this->first_comp_plt_offset()
  2160. + sym->comp_plt_offset());
  2161. }
  2162. protected:
  2163. void
  2164. do_adjust_output_section(Output_section* os)
  2165. { os->set_entsize(0); }
  2166. // Write to a map file.
  2167. void
  2168. do_print_to_mapfile(Mapfile* mapfile) const
  2169. { mapfile->print_output_data(this, _(".plt")); }
  2170. private:
  2171. // Template for the first PLT entry.
  2172. static const uint32_t plt0_entry_o32[];
  2173. static const uint32_t plt0_entry_n32[];
  2174. static const uint32_t plt0_entry_n64[];
  2175. static const uint32_t plt0_entry_micromips_o32[];
  2176. static const uint32_t plt0_entry_micromips32_o32[];
  2177. // Template for subsequent PLT entries.
  2178. static const uint32_t plt_entry[];
  2179. static const uint32_t plt_entry_r6[];
  2180. static const uint32_t plt_entry_mips16_o32[];
  2181. static const uint32_t plt_entry_micromips_o32[];
  2182. static const uint32_t plt_entry_micromips32_o32[];
  2183. // Set the final size.
  2184. void
  2185. set_final_data_size()
  2186. {
  2187. this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
  2188. + this->plt_comp_offset_);
  2189. }
  2190. // Write out the PLT data.
  2191. void
  2192. do_write(Output_file*);
  2193. // Return whether the plt header contains microMIPS code. For the sake of
  2194. // cache alignment always use a standard header whenever any standard entries
  2195. // are present even if microMIPS entries are present as well. This also lets
  2196. // the microMIPS header rely on the value of $v0 only set by microMIPS
  2197. // entries, for a small size reduction.
  2198. bool
  2199. is_plt_header_compressed() const
  2200. {
  2201. gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
  2202. return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
  2203. }
  2204. // Return the size of the PLT header.
  2205. unsigned int
  2206. get_plt_header_size() const
  2207. {
  2208. if (this->target_->is_output_n64())
  2209. return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
  2210. else if (this->target_->is_output_n32())
  2211. return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
  2212. else if (!this->is_plt_header_compressed())
  2213. return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
  2214. else if (this->target_->use_32bit_micromips_instructions())
  2215. return (2 * sizeof(plt0_entry_micromips32_o32)
  2216. / sizeof(plt0_entry_micromips32_o32[0]));
  2217. else
  2218. return (2 * sizeof(plt0_entry_micromips_o32)
  2219. / sizeof(plt0_entry_micromips_o32[0]));
  2220. }
  2221. // Return the PLT header entry.
  2222. const uint32_t*
  2223. get_plt_header_entry() const
  2224. {
  2225. if (this->target_->is_output_n64())
  2226. return plt0_entry_n64;
  2227. else if (this->target_->is_output_n32())
  2228. return plt0_entry_n32;
  2229. else if (!this->is_plt_header_compressed())
  2230. return plt0_entry_o32;
  2231. else if (this->target_->use_32bit_micromips_instructions())
  2232. return plt0_entry_micromips32_o32;
  2233. else
  2234. return plt0_entry_micromips_o32;
  2235. }
  2236. // Return the size of the standard PLT entry.
  2237. unsigned int
  2238. standard_plt_entry_size() const
  2239. { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
  2240. // Return the size of the compressed PLT entry.
  2241. unsigned int
  2242. compressed_plt_entry_size() const
  2243. {
  2244. gold_assert(!this->target_->is_output_newabi());
  2245. if (!this->target_->is_output_micromips())
  2246. return (2 * sizeof(plt_entry_mips16_o32)
  2247. / sizeof(plt_entry_mips16_o32[0]));
  2248. else if (this->target_->use_32bit_micromips_instructions())
  2249. return (2 * sizeof(plt_entry_micromips32_o32)
  2250. / sizeof(plt_entry_micromips32_o32[0]));
  2251. else
  2252. return (2 * sizeof(plt_entry_micromips_o32)
  2253. / sizeof(plt_entry_micromips_o32[0]));
  2254. }
  2255. // The reloc section.
  2256. Reloc_section* rel_;
  2257. // The .got.plt section.
  2258. Output_data_space* got_plt_;
  2259. // Symbols that have PLT entry.
  2260. std::vector<Mips_symbol<size>*> symbols_;
  2261. // The offset of the next standard PLT entry to create.
  2262. unsigned int plt_mips_offset_;
  2263. // The offset of the next compressed PLT entry to create.
  2264. unsigned int plt_comp_offset_;
  2265. // The size of the PLT header in bytes.
  2266. unsigned int plt_header_size_;
  2267. // The target.
  2268. Target_mips<size, big_endian>* target_;
  2269. };
  2270. // A class to handle the .MIPS.stubs data.
  2271. template<int size, bool big_endian>
  2272. class Mips_output_data_mips_stubs : public Output_section_data
  2273. {
  2274. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  2275. // Unordered set of .MIPS.stubs entries.
  2276. typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
  2277. Mips_stubs_entry_set;
  2278. public:
  2279. Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
  2280. : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
  2281. stub_offsets_are_set_(false), target_(target)
  2282. { }
  2283. // Create entry for a symbol.
  2284. void
  2285. make_entry(Mips_symbol<size>*);
  2286. // Remove entry for a symbol.
  2287. void
  2288. remove_entry(Mips_symbol<size>* gsym);
  2289. // Set stub offsets for symbols. This method expects that the number of
  2290. // entries in dynamic symbol table is set.
  2291. void
  2292. set_lazy_stub_offsets();
  2293. void
  2294. set_needs_dynsym_value();
  2295. // Set the number of entries in dynamic symbol table.
  2296. void
  2297. set_dynsym_count(unsigned int dynsym_count)
  2298. { this->dynsym_count_ = dynsym_count; }
  2299. // Return maximum size of the stub, ie. the stub size if the dynamic symbol
  2300. // count is greater than 0x10000. If the dynamic symbol count is less than
  2301. // 0x10000, the stub will be 4 bytes smaller.
  2302. // There's no disadvantage from using microMIPS code here, so for the sake of
  2303. // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
  2304. // output produced at all. This has a benefit of stubs being shorter by
  2305. // 4 bytes each too, unless in the insn32 mode.
  2306. unsigned int
  2307. stub_max_size() const
  2308. {
  2309. if (!this->target_->is_output_micromips()
  2310. || this->target_->use_32bit_micromips_instructions())
  2311. return 20;
  2312. else
  2313. return 16;
  2314. }
  2315. // Return the size of the stub. This method expects that the final dynsym
  2316. // count is set.
  2317. unsigned int
  2318. stub_size() const
  2319. {
  2320. gold_assert(this->dynsym_count_ != -1U);
  2321. if (this->dynsym_count_ > 0x10000)
  2322. return this->stub_max_size();
  2323. else
  2324. return this->stub_max_size() - 4;
  2325. }
  2326. // Return output address of a stub.
  2327. Mips_address
  2328. stub_address(const Mips_symbol<size>* sym) const
  2329. {
  2330. gold_assert(sym->has_lazy_stub());
  2331. return this->address() + sym->lazy_stub_offset();
  2332. }
  2333. protected:
  2334. void
  2335. do_adjust_output_section(Output_section* os)
  2336. { os->set_entsize(0); }
  2337. // Write to a map file.
  2338. void
  2339. do_print_to_mapfile(Mapfile* mapfile) const
  2340. { mapfile->print_output_data(this, _(".MIPS.stubs")); }
  2341. private:
  2342. static const uint32_t lazy_stub_normal_1[];
  2343. static const uint32_t lazy_stub_normal_1_n64[];
  2344. static const uint32_t lazy_stub_normal_2[];
  2345. static const uint32_t lazy_stub_normal_2_n64[];
  2346. static const uint32_t lazy_stub_big[];
  2347. static const uint32_t lazy_stub_big_n64[];
  2348. static const uint32_t lazy_stub_micromips_normal_1[];
  2349. static const uint32_t lazy_stub_micromips_normal_1_n64[];
  2350. static const uint32_t lazy_stub_micromips_normal_2[];
  2351. static const uint32_t lazy_stub_micromips_normal_2_n64[];
  2352. static const uint32_t lazy_stub_micromips_big[];
  2353. static const uint32_t lazy_stub_micromips_big_n64[];
  2354. static const uint32_t lazy_stub_micromips32_normal_1[];
  2355. static const uint32_t lazy_stub_micromips32_normal_1_n64[];
  2356. static const uint32_t lazy_stub_micromips32_normal_2[];
  2357. static const uint32_t lazy_stub_micromips32_normal_2_n64[];
  2358. static const uint32_t lazy_stub_micromips32_big[];
  2359. static const uint32_t lazy_stub_micromips32_big_n64[];
  2360. // Set the final size.
  2361. void
  2362. set_final_data_size()
  2363. { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
  2364. // Write out the .MIPS.stubs data.
  2365. void
  2366. do_write(Output_file*);
  2367. // .MIPS.stubs symbols
  2368. Mips_stubs_entry_set symbols_;
  2369. // Number of entries in dynamic symbol table.
  2370. unsigned int dynsym_count_;
  2371. // Whether the stub offsets are set.
  2372. bool stub_offsets_are_set_;
  2373. // The target.
  2374. Target_mips<size, big_endian>* target_;
  2375. };
  2376. // This class handles Mips .reginfo output section.
  2377. template<int size, bool big_endian>
  2378. class Mips_output_section_reginfo : public Output_section_data
  2379. {
  2380. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  2381. public:
  2382. Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
  2383. Valtype gprmask, Valtype cprmask1,
  2384. Valtype cprmask2, Valtype cprmask3,
  2385. Valtype cprmask4)
  2386. : Output_section_data(24, 4, true), target_(target),
  2387. gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
  2388. cprmask3_(cprmask3), cprmask4_(cprmask4)
  2389. { }
  2390. protected:
  2391. // Write to a map file.
  2392. void
  2393. do_print_to_mapfile(Mapfile* mapfile) const
  2394. { mapfile->print_output_data(this, _(".reginfo")); }
  2395. // Write out reginfo section.
  2396. void
  2397. do_write(Output_file* of);
  2398. private:
  2399. Target_mips<size, big_endian>* target_;
  2400. // gprmask of the output .reginfo section.
  2401. Valtype gprmask_;
  2402. // cprmask1 of the output .reginfo section.
  2403. Valtype cprmask1_;
  2404. // cprmask2 of the output .reginfo section.
  2405. Valtype cprmask2_;
  2406. // cprmask3 of the output .reginfo section.
  2407. Valtype cprmask3_;
  2408. // cprmask4 of the output .reginfo section.
  2409. Valtype cprmask4_;
  2410. };
  2411. // This class handles .MIPS.options output section.
  2412. template<int size, bool big_endian>
  2413. class Mips_output_section_options : public Output_section
  2414. {
  2415. public:
  2416. Mips_output_section_options(const char* name, elfcpp::Elf_Word type,
  2417. elfcpp::Elf_Xword flags,
  2418. Target_mips<size, big_endian>* target)
  2419. : Output_section(name, type, flags), target_(target)
  2420. {
  2421. // After the input sections are written, we only need to update
  2422. // ri_gp_value field of ODK_REGINFO entries.
  2423. this->set_after_input_sections();
  2424. }
  2425. protected:
  2426. // Write out option section.
  2427. void
  2428. do_write(Output_file* of);
  2429. private:
  2430. Target_mips<size, big_endian>* target_;
  2431. };
  2432. // This class handles .MIPS.abiflags output section.
  2433. template<int size, bool big_endian>
  2434. class Mips_output_section_abiflags : public Output_section_data
  2435. {
  2436. public:
  2437. Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
  2438. : Output_section_data(24, 8, true), abiflags_(abiflags)
  2439. { }
  2440. protected:
  2441. // Write to a map file.
  2442. void
  2443. do_print_to_mapfile(Mapfile* mapfile) const
  2444. { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
  2445. void
  2446. do_write(Output_file* of);
  2447. private:
  2448. const Mips_abiflags<big_endian>& abiflags_;
  2449. };
  2450. // The MIPS target has relocation types which default handling of relocatable
  2451. // relocation cannot process. So we have to extend the default code.
  2452. template<bool big_endian, typename Classify_reloc>
  2453. class Mips_scan_relocatable_relocs :
  2454. public Default_scan_relocatable_relocs<Classify_reloc>
  2455. {
  2456. public:
  2457. // Return the strategy to use for a local symbol which is a section
  2458. // symbol, given the relocation type.
  2459. inline Relocatable_relocs::Reloc_strategy
  2460. local_section_strategy(unsigned int r_type, Relobj* object)
  2461. {
  2462. if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
  2463. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  2464. else
  2465. {
  2466. switch (r_type)
  2467. {
  2468. case elfcpp::R_MIPS_26:
  2469. return Relocatable_relocs::RELOC_SPECIAL;
  2470. default:
  2471. return Default_scan_relocatable_relocs<Classify_reloc>::
  2472. local_section_strategy(r_type, object);
  2473. }
  2474. }
  2475. }
  2476. };
  2477. // Mips_copy_relocs class. The only difference from the base class is the
  2478. // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
  2479. // Mips cannot convert all relocation types to dynamic relocs. If a reloc
  2480. // cannot be made dynamic, a COPY reloc is emitted.
  2481. template<int sh_type, int size, bool big_endian>
  2482. class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
  2483. {
  2484. public:
  2485. Mips_copy_relocs()
  2486. : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
  2487. { }
  2488. // Emit any saved relocations which turn out to be needed. This is
  2489. // called after all the relocs have been scanned.
  2490. void
  2491. emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
  2492. Symbol_table*, Layout*, Target_mips<size, big_endian>*);
  2493. private:
  2494. typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
  2495. Copy_reloc_entry;
  2496. // Emit this reloc if appropriate. This is called after we have
  2497. // scanned all the relocations, so we know whether we emitted a
  2498. // COPY relocation for SYM_.
  2499. void
  2500. emit_entry(Copy_reloc_entry& entry,
  2501. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
  2502. Symbol_table* symtab, Layout* layout,
  2503. Target_mips<size, big_endian>* target);
  2504. };
  2505. // Return true if the symbol SYM should be considered to resolve local
  2506. // to the current module, and false otherwise. The logic is taken from
  2507. // GNU ld's method _bfd_elf_symbol_refs_local_p.
  2508. static bool
  2509. symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
  2510. bool local_protected)
  2511. {
  2512. // If it's a local sym, of course we resolve locally.
  2513. if (sym == NULL)
  2514. return true;
  2515. // STV_HIDDEN or STV_INTERNAL ones must be local.
  2516. if (sym->visibility() == elfcpp::STV_HIDDEN
  2517. || sym->visibility() == elfcpp::STV_INTERNAL)
  2518. return true;
  2519. // If we don't have a definition in a regular file, then we can't
  2520. // resolve locally. The sym is either undefined or dynamic.
  2521. if (sym->is_from_dynobj() || sym->is_undefined())
  2522. return false;
  2523. // Forced local symbols resolve locally.
  2524. if (sym->is_forced_local())
  2525. return true;
  2526. // As do non-dynamic symbols.
  2527. if (!has_dynsym_entry)
  2528. return true;
  2529. // At this point, we know the symbol is defined and dynamic. In an
  2530. // executable it must resolve locally, likewise when building symbolic
  2531. // shared libraries.
  2532. if (parameters->options().output_is_executable()
  2533. || parameters->options().Bsymbolic())
  2534. return true;
  2535. // Now deal with defined dynamic symbols in shared libraries. Ones
  2536. // with default visibility might not resolve locally.
  2537. if (sym->visibility() == elfcpp::STV_DEFAULT)
  2538. return false;
  2539. // STV_PROTECTED non-function symbols are local.
  2540. if (sym->type() != elfcpp::STT_FUNC)
  2541. return true;
  2542. // Function pointer equality tests may require that STV_PROTECTED
  2543. // symbols be treated as dynamic symbols. If the address of a
  2544. // function not defined in an executable is set to that function's
  2545. // plt entry in the executable, then the address of the function in
  2546. // a shared library must also be the plt entry in the executable.
  2547. return local_protected;
  2548. }
  2549. // Return TRUE if references to this symbol always reference the symbol in this
  2550. // object.
  2551. static bool
  2552. symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
  2553. {
  2554. return symbol_refs_local(sym, has_dynsym_entry, false);
  2555. }
  2556. // Return TRUE if calls to this symbol always call the version in this object.
  2557. static bool
  2558. symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
  2559. {
  2560. return symbol_refs_local(sym, has_dynsym_entry, true);
  2561. }
  2562. // Compare GOT offsets of two symbols.
  2563. template<int size, bool big_endian>
  2564. static bool
  2565. got_offset_compare(Symbol* sym1, Symbol* sym2)
  2566. {
  2567. Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
  2568. Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
  2569. unsigned int area1 = mips_sym1->global_got_area();
  2570. unsigned int area2 = mips_sym2->global_got_area();
  2571. gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
  2572. // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
  2573. if (area1 != area2)
  2574. return area1 < area2;
  2575. return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
  2576. }
  2577. // This method divides dynamic symbols into symbols that have GOT entry, and
  2578. // symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
  2579. // Mips ABI requires that symbols with the GOT entry must be at the end of
  2580. // dynamic symbol table, and the order in dynamic symbol table must match the
  2581. // order in GOT.
  2582. template<int size, bool big_endian>
  2583. static void
  2584. reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
  2585. std::vector<Symbol*>* non_got_symbols,
  2586. std::vector<Symbol*>* got_symbols)
  2587. {
  2588. for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
  2589. p != dyn_symbols->end();
  2590. ++p)
  2591. {
  2592. Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
  2593. if (mips_sym->global_got_area() == GGA_NORMAL
  2594. || mips_sym->global_got_area() == GGA_RELOC_ONLY)
  2595. got_symbols->push_back(mips_sym);
  2596. else
  2597. non_got_symbols->push_back(mips_sym);
  2598. }
  2599. std::sort(got_symbols->begin(), got_symbols->end(),
  2600. got_offset_compare<size, big_endian>);
  2601. }
  2602. // Functor class for processing the global symbol table.
  2603. template<int size, bool big_endian>
  2604. class Symbol_visitor_check_symbols
  2605. {
  2606. public:
  2607. Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
  2608. Layout* layout, Symbol_table* symtab)
  2609. : target_(target), layout_(layout), symtab_(symtab)
  2610. { }
  2611. void
  2612. operator()(Sized_symbol<size>* sym)
  2613. {
  2614. Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
  2615. if (local_pic_function<size, big_endian>(mips_sym))
  2616. {
  2617. // SYM is a function that might need $25 to be valid on entry.
  2618. // If we're creating a non-PIC relocatable object, mark SYM as
  2619. // being PIC. If we're creating a non-relocatable object with
  2620. // non-PIC branches and jumps to SYM, make sure that SYM has an la25
  2621. // stub.
  2622. if (parameters->options().relocatable())
  2623. {
  2624. if (!parameters->options().output_is_position_independent())
  2625. mips_sym->set_pic();
  2626. }
  2627. else if (mips_sym->has_nonpic_branches())
  2628. {
  2629. this->target_->la25_stub_section(layout_)
  2630. ->create_la25_stub(this->symtab_, this->target_, mips_sym);
  2631. }
  2632. }
  2633. }
  2634. private:
  2635. Target_mips<size, big_endian>* target_;
  2636. Layout* layout_;
  2637. Symbol_table* symtab_;
  2638. };
  2639. // Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
  2640. // and endianness. The relocation format for MIPS-64 is non-standard.
  2641. template<int sh_type, int size, bool big_endian>
  2642. struct Mips_reloc_types;
  2643. template<bool big_endian>
  2644. struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
  2645. {
  2646. typedef typename elfcpp::Rel<32, big_endian> Reloc;
  2647. typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
  2648. static typename elfcpp::Elf_types<32>::Elf_Swxword
  2649. get_r_addend(const Reloc*)
  2650. { return 0; }
  2651. static inline void
  2652. set_reloc_addend(Reloc_write*,
  2653. typename elfcpp::Elf_types<32>::Elf_Swxword)
  2654. { gold_unreachable(); }
  2655. };
  2656. template<bool big_endian>
  2657. struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
  2658. {
  2659. typedef typename elfcpp::Rela<32, big_endian> Reloc;
  2660. typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
  2661. static typename elfcpp::Elf_types<32>::Elf_Swxword
  2662. get_r_addend(const Reloc* reloc)
  2663. { return reloc->get_r_addend(); }
  2664. static inline void
  2665. set_reloc_addend(Reloc_write* p,
  2666. typename elfcpp::Elf_types<32>::Elf_Swxword val)
  2667. { p->put_r_addend(val); }
  2668. };
  2669. template<bool big_endian>
  2670. struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
  2671. {
  2672. typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
  2673. typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
  2674. static typename elfcpp::Elf_types<64>::Elf_Swxword
  2675. get_r_addend(const Reloc*)
  2676. { return 0; }
  2677. static inline void
  2678. set_reloc_addend(Reloc_write*,
  2679. typename elfcpp::Elf_types<64>::Elf_Swxword)
  2680. { gold_unreachable(); }
  2681. };
  2682. template<bool big_endian>
  2683. struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
  2684. {
  2685. typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
  2686. typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
  2687. static typename elfcpp::Elf_types<64>::Elf_Swxword
  2688. get_r_addend(const Reloc* reloc)
  2689. { return reloc->get_r_addend(); }
  2690. static inline void
  2691. set_reloc_addend(Reloc_write* p,
  2692. typename elfcpp::Elf_types<64>::Elf_Swxword val)
  2693. { p->put_r_addend(val); }
  2694. };
  2695. // Forward declaration.
  2696. static unsigned int
  2697. mips_get_size_for_reloc(unsigned int, Relobj*);
  2698. // A class for inquiring about properties of a relocation,
  2699. // used while scanning relocs during a relocatable link and
  2700. // garbage collection.
  2701. template<int sh_type_, int size, bool big_endian>
  2702. class Mips_classify_reloc;
  2703. template<int sh_type_, bool big_endian>
  2704. class Mips_classify_reloc<sh_type_, 32, big_endian> :
  2705. public gold::Default_classify_reloc<sh_type_, 32, big_endian>
  2706. {
  2707. public:
  2708. typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
  2709. Reltype;
  2710. typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
  2711. Reltype_write;
  2712. // Return the symbol referred to by the relocation.
  2713. static inline unsigned int
  2714. get_r_sym(const Reltype* reloc)
  2715. { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
  2716. // Return the type of the relocation.
  2717. static inline unsigned int
  2718. get_r_type(const Reltype* reloc)
  2719. { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
  2720. static inline unsigned int
  2721. get_r_type2(const Reltype*)
  2722. { return 0; }
  2723. static inline unsigned int
  2724. get_r_type3(const Reltype*)
  2725. { return 0; }
  2726. static inline unsigned int
  2727. get_r_ssym(const Reltype*)
  2728. { return 0; }
  2729. // Return the explicit addend of the relocation (return 0 for SHT_REL).
  2730. static inline unsigned int
  2731. get_r_addend(const Reltype* reloc)
  2732. {
  2733. if (sh_type_ == elfcpp::SHT_REL)
  2734. return 0;
  2735. return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
  2736. }
  2737. // Write the r_info field to a new reloc, using the r_info field from
  2738. // the original reloc, replacing the r_sym field with R_SYM.
  2739. static inline void
  2740. put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
  2741. {
  2742. unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
  2743. new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
  2744. }
  2745. // Write the r_addend field to a new reloc.
  2746. static inline void
  2747. put_r_addend(Reltype_write* to,
  2748. typename elfcpp::Elf_types<32>::Elf_Swxword addend)
  2749. { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
  2750. // Return the size of the addend of the relocation (only used for SHT_REL).
  2751. static unsigned int
  2752. get_size_for_reloc(unsigned int r_type, Relobj* obj)
  2753. { return mips_get_size_for_reloc(r_type, obj); }
  2754. };
  2755. template<int sh_type_, bool big_endian>
  2756. class Mips_classify_reloc<sh_type_, 64, big_endian> :
  2757. public gold::Default_classify_reloc<sh_type_, 64, big_endian>
  2758. {
  2759. public:
  2760. typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
  2761. Reltype;
  2762. typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
  2763. Reltype_write;
  2764. // Return the symbol referred to by the relocation.
  2765. static inline unsigned int
  2766. get_r_sym(const Reltype* reloc)
  2767. { return reloc->get_r_sym(); }
  2768. // Return the r_type of the relocation.
  2769. static inline unsigned int
  2770. get_r_type(const Reltype* reloc)
  2771. { return reloc->get_r_type(); }
  2772. // Return the r_type2 of the relocation.
  2773. static inline unsigned int
  2774. get_r_type2(const Reltype* reloc)
  2775. { return reloc->get_r_type2(); }
  2776. // Return the r_type3 of the relocation.
  2777. static inline unsigned int
  2778. get_r_type3(const Reltype* reloc)
  2779. { return reloc->get_r_type3(); }
  2780. // Return the special symbol of the relocation.
  2781. static inline unsigned int
  2782. get_r_ssym(const Reltype* reloc)
  2783. { return reloc->get_r_ssym(); }
  2784. // Return the explicit addend of the relocation (return 0 for SHT_REL).
  2785. static inline typename elfcpp::Elf_types<64>::Elf_Swxword
  2786. get_r_addend(const Reltype* reloc)
  2787. {
  2788. if (sh_type_ == elfcpp::SHT_REL)
  2789. return 0;
  2790. return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
  2791. }
  2792. // Write the r_info field to a new reloc, using the r_info field from
  2793. // the original reloc, replacing the r_sym field with R_SYM.
  2794. static inline void
  2795. put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
  2796. {
  2797. new_reloc->put_r_sym(r_sym);
  2798. new_reloc->put_r_ssym(reloc->get_r_ssym());
  2799. new_reloc->put_r_type3(reloc->get_r_type3());
  2800. new_reloc->put_r_type2(reloc->get_r_type2());
  2801. new_reloc->put_r_type(reloc->get_r_type());
  2802. }
  2803. // Write the r_addend field to a new reloc.
  2804. static inline void
  2805. put_r_addend(Reltype_write* to,
  2806. typename elfcpp::Elf_types<64>::Elf_Swxword addend)
  2807. { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
  2808. // Return the size of the addend of the relocation (only used for SHT_REL).
  2809. static unsigned int
  2810. get_size_for_reloc(unsigned int r_type, Relobj* obj)
  2811. { return mips_get_size_for_reloc(r_type, obj); }
  2812. };
  2813. template<int size, bool big_endian>
  2814. class Target_mips : public Sized_target<size, big_endian>
  2815. {
  2816. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  2817. typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
  2818. Reloc_section;
  2819. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
  2820. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  2821. typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
  2822. Reltype;
  2823. typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
  2824. Relatype;
  2825. public:
  2826. Target_mips(const Target::Target_info* info = &mips_info)
  2827. : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
  2828. got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
  2829. dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
  2830. mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
  2831. mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
  2832. entry_symbol_is_compressed_(false), insn32_(false)
  2833. {
  2834. this->add_machine_extensions();
  2835. }
  2836. // The offset of $gp from the beginning of the .got section.
  2837. static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
  2838. // The maximum size of the GOT for it to be addressable using 16-bit
  2839. // offsets from $gp.
  2840. static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
  2841. // Make a new symbol table entry for the Mips target.
  2842. Sized_symbol<size>*
  2843. make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
  2844. { return new Mips_symbol<size>(); }
  2845. // Process the relocations to determine unreferenced sections for
  2846. // garbage collection.
  2847. void
  2848. gc_process_relocs(Symbol_table* symtab,
  2849. Layout* layout,
  2850. Sized_relobj_file<size, big_endian>* object,
  2851. unsigned int data_shndx,
  2852. unsigned int sh_type,
  2853. const unsigned char* prelocs,
  2854. size_t reloc_count,
  2855. Output_section* output_section,
  2856. bool needs_special_offset_handling,
  2857. size_t local_symbol_count,
  2858. const unsigned char* plocal_symbols);
  2859. // Scan the relocations to look for symbol adjustments.
  2860. void
  2861. scan_relocs(Symbol_table* symtab,
  2862. Layout* layout,
  2863. Sized_relobj_file<size, big_endian>* object,
  2864. unsigned int data_shndx,
  2865. unsigned int sh_type,
  2866. const unsigned char* prelocs,
  2867. size_t reloc_count,
  2868. Output_section* output_section,
  2869. bool needs_special_offset_handling,
  2870. size_t local_symbol_count,
  2871. const unsigned char* plocal_symbols);
  2872. // Finalize the sections.
  2873. void
  2874. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  2875. // Relocate a section.
  2876. void
  2877. relocate_section(const Relocate_info<size, big_endian>*,
  2878. unsigned int sh_type,
  2879. const unsigned char* prelocs,
  2880. size_t reloc_count,
  2881. Output_section* output_section,
  2882. bool needs_special_offset_handling,
  2883. unsigned char* view,
  2884. Mips_address view_address,
  2885. section_size_type view_size,
  2886. const Reloc_symbol_changes*);
  2887. // Scan the relocs during a relocatable link.
  2888. void
  2889. scan_relocatable_relocs(Symbol_table* symtab,
  2890. Layout* layout,
  2891. Sized_relobj_file<size, big_endian>* object,
  2892. unsigned int data_shndx,
  2893. unsigned int sh_type,
  2894. const unsigned char* prelocs,
  2895. size_t reloc_count,
  2896. Output_section* output_section,
  2897. bool needs_special_offset_handling,
  2898. size_t local_symbol_count,
  2899. const unsigned char* plocal_symbols,
  2900. Relocatable_relocs*);
  2901. // Scan the relocs for --emit-relocs.
  2902. void
  2903. emit_relocs_scan(Symbol_table* symtab,
  2904. Layout* layout,
  2905. Sized_relobj_file<size, big_endian>* object,
  2906. unsigned int data_shndx,
  2907. unsigned int sh_type,
  2908. const unsigned char* prelocs,
  2909. size_t reloc_count,
  2910. Output_section* output_section,
  2911. bool needs_special_offset_handling,
  2912. size_t local_symbol_count,
  2913. const unsigned char* plocal_syms,
  2914. Relocatable_relocs* rr);
  2915. // Emit relocations for a section.
  2916. void
  2917. relocate_relocs(const Relocate_info<size, big_endian>*,
  2918. unsigned int sh_type,
  2919. const unsigned char* prelocs,
  2920. size_t reloc_count,
  2921. Output_section* output_section,
  2922. typename elfcpp::Elf_types<size>::Elf_Off
  2923. offset_in_output_section,
  2924. unsigned char* view,
  2925. Mips_address view_address,
  2926. section_size_type view_size,
  2927. unsigned char* reloc_view,
  2928. section_size_type reloc_view_size);
  2929. // Perform target-specific processing in a relocatable link. This is
  2930. // only used if we use the relocation strategy RELOC_SPECIAL.
  2931. void
  2932. relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
  2933. unsigned int sh_type,
  2934. const unsigned char* preloc_in,
  2935. size_t relnum,
  2936. Output_section* output_section,
  2937. typename elfcpp::Elf_types<size>::Elf_Off
  2938. offset_in_output_section,
  2939. unsigned char* view,
  2940. Mips_address view_address,
  2941. section_size_type view_size,
  2942. unsigned char* preloc_out);
  2943. // Return whether SYM is defined by the ABI.
  2944. bool
  2945. do_is_defined_by_abi(const Symbol* sym) const
  2946. {
  2947. return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
  2948. || (strcmp(sym->name(), "_gp_disp") == 0)
  2949. || (strcmp(sym->name(), "___tls_get_addr") == 0));
  2950. }
  2951. // Return the number of entries in the GOT.
  2952. unsigned int
  2953. got_entry_count() const
  2954. {
  2955. if (!this->has_got_section())
  2956. return 0;
  2957. return this->got_size() / (size/8);
  2958. }
  2959. // Return the number of entries in the PLT.
  2960. unsigned int
  2961. plt_entry_count() const
  2962. {
  2963. if (this->plt_ == NULL)
  2964. return 0;
  2965. return this->plt_->entry_count();
  2966. }
  2967. // Return the offset of the first non-reserved PLT entry.
  2968. unsigned int
  2969. first_plt_entry_offset() const
  2970. { return this->plt_->first_plt_entry_offset(); }
  2971. // Return the size of each PLT entry.
  2972. unsigned int
  2973. plt_entry_size() const
  2974. { return this->plt_->plt_entry_size(); }
  2975. // Get the GOT section, creating it if necessary.
  2976. Mips_output_data_got<size, big_endian>*
  2977. got_section(Symbol_table*, Layout*);
  2978. // Get the GOT section.
  2979. Mips_output_data_got<size, big_endian>*
  2980. got_section() const
  2981. {
  2982. gold_assert(this->got_ != NULL);
  2983. return this->got_;
  2984. }
  2985. // Get the .MIPS.stubs section, creating it if necessary.
  2986. Mips_output_data_mips_stubs<size, big_endian>*
  2987. mips_stubs_section(Layout* layout);
  2988. // Get the .MIPS.stubs section.
  2989. Mips_output_data_mips_stubs<size, big_endian>*
  2990. mips_stubs_section() const
  2991. {
  2992. gold_assert(this->mips_stubs_ != NULL);
  2993. return this->mips_stubs_;
  2994. }
  2995. // Get the LA25 stub section, creating it if necessary.
  2996. Mips_output_data_la25_stub<size, big_endian>*
  2997. la25_stub_section(Layout*);
  2998. // Get the LA25 stub section.
  2999. Mips_output_data_la25_stub<size, big_endian>*
  3000. la25_stub_section()
  3001. {
  3002. gold_assert(this->la25_stub_ != NULL);
  3003. return this->la25_stub_;
  3004. }
  3005. // Get gp value. It has the value of .got + 0x7FF0.
  3006. Mips_address
  3007. gp_value() const
  3008. {
  3009. if (this->gp_ != NULL)
  3010. return this->gp_->value();
  3011. return 0;
  3012. }
  3013. // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
  3014. // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
  3015. Mips_address
  3016. adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
  3017. {
  3018. if (this->gp_ == NULL)
  3019. return 0;
  3020. bool multi_got = false;
  3021. if (this->has_got_section())
  3022. multi_got = this->got_section()->multi_got();
  3023. if (!multi_got)
  3024. return this->gp_->value();
  3025. else
  3026. return this->gp_->value() + this->got_section()->get_got_offset(object);
  3027. }
  3028. // Get the dynamic reloc section, creating it if necessary.
  3029. Reloc_section*
  3030. rel_dyn_section(Layout*);
  3031. bool
  3032. do_has_custom_set_dynsym_indexes() const
  3033. { return true; }
  3034. // Don't emit input .reginfo/.MIPS.abiflags sections to
  3035. // output .reginfo/.MIPS.abiflags.
  3036. bool
  3037. do_should_include_section(elfcpp::Elf_Word sh_type) const
  3038. {
  3039. return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
  3040. && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
  3041. }
  3042. // Set the dynamic symbol indexes. INDEX is the index of the first
  3043. // global dynamic symbol. Pointers to the symbols are stored into the
  3044. // vector SYMS. The names are added to DYNPOOL. This returns an
  3045. // updated dynamic symbol index.
  3046. unsigned int
  3047. do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
  3048. std::vector<Symbol*>* syms, Stringpool* dynpool,
  3049. Versions* versions, Symbol_table* symtab) const;
  3050. // Remove .MIPS.stubs entry for a symbol.
  3051. void
  3052. remove_lazy_stub_entry(Mips_symbol<size>* sym)
  3053. {
  3054. if (this->mips_stubs_ != NULL)
  3055. this->mips_stubs_->remove_entry(sym);
  3056. }
  3057. // The value to write into got[1] for SVR4 targets, to identify it is
  3058. // a GNU object. The dynamic linker can then use got[1] to store the
  3059. // module pointer.
  3060. uint64_t
  3061. mips_elf_gnu_got1_mask()
  3062. {
  3063. if (this->is_output_n64())
  3064. return (uint64_t)1 << 63;
  3065. else
  3066. return 1 << 31;
  3067. }
  3068. // Whether the output has microMIPS code. This is valid only after
  3069. // merge_obj_e_flags() is called.
  3070. bool
  3071. is_output_micromips() const
  3072. {
  3073. gold_assert(this->are_processor_specific_flags_set());
  3074. return elfcpp::is_micromips(this->processor_specific_flags());
  3075. }
  3076. // Whether the output uses N32 ABI. This is valid only after
  3077. // merge_obj_e_flags() is called.
  3078. bool
  3079. is_output_n32() const
  3080. {
  3081. gold_assert(this->are_processor_specific_flags_set());
  3082. return elfcpp::abi_n32(this->processor_specific_flags());
  3083. }
  3084. // Whether the output uses R6 ISA. This is valid only after
  3085. // merge_obj_e_flags() is called.
  3086. bool
  3087. is_output_r6() const
  3088. {
  3089. gold_assert(this->are_processor_specific_flags_set());
  3090. return elfcpp::r6_isa(this->processor_specific_flags());
  3091. }
  3092. // Whether the output uses N64 ABI.
  3093. bool
  3094. is_output_n64() const
  3095. { return size == 64; }
  3096. // Whether the output uses NEWABI. This is valid only after
  3097. // merge_obj_e_flags() is called.
  3098. bool
  3099. is_output_newabi() const
  3100. { return this->is_output_n32() || this->is_output_n64(); }
  3101. // Whether we can only use 32-bit microMIPS instructions.
  3102. bool
  3103. use_32bit_micromips_instructions() const
  3104. { return this->insn32_; }
  3105. // Return the r_sym field from a relocation.
  3106. unsigned int
  3107. get_r_sym(const unsigned char* preloc) const
  3108. {
  3109. // Since REL and RELA relocs share the same structure through
  3110. // the r_info field, we can just use REL here.
  3111. Reltype rel(preloc);
  3112. return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
  3113. get_r_sym(&rel);
  3114. }
  3115. protected:
  3116. // Return the value to use for a dynamic symbol which requires special
  3117. // treatment. This is how we support equality comparisons of function
  3118. // pointers across shared library boundaries, as described in the
  3119. // processor specific ABI supplement.
  3120. uint64_t
  3121. do_dynsym_value(const Symbol* gsym) const;
  3122. // Make an ELF object.
  3123. Object*
  3124. do_make_elf_object(const std::string&, Input_file*, off_t,
  3125. const elfcpp::Ehdr<size, big_endian>& ehdr);
  3126. Object*
  3127. do_make_elf_object(const std::string&, Input_file*, off_t,
  3128. const elfcpp::Ehdr<size, !big_endian>&)
  3129. { gold_unreachable(); }
  3130. // Make an output section.
  3131. Output_section*
  3132. do_make_output_section(const char* name, elfcpp::Elf_Word type,
  3133. elfcpp::Elf_Xword flags)
  3134. {
  3135. if (type == elfcpp::SHT_MIPS_OPTIONS)
  3136. return new Mips_output_section_options<size, big_endian>(name, type,
  3137. flags, this);
  3138. else
  3139. return new Output_section(name, type, flags);
  3140. }
  3141. // Adjust ELF file header.
  3142. void
  3143. do_adjust_elf_header(unsigned char* view, int len);
  3144. // Get the custom dynamic tag value.
  3145. unsigned int
  3146. do_dynamic_tag_custom_value(elfcpp::DT) const;
  3147. // Adjust the value written to the dynamic symbol table.
  3148. virtual void
  3149. do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
  3150. {
  3151. elfcpp::Sym<size, big_endian> isym(view);
  3152. elfcpp::Sym_write<size, big_endian> osym(view);
  3153. const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
  3154. // Keep dynamic compressed symbols odd. This allows the dynamic linker
  3155. // to treat compressed symbols like any other.
  3156. Mips_address value = isym.get_st_value();
  3157. if (mips_sym->is_mips16() && value != 0)
  3158. {
  3159. if (!mips_sym->has_mips16_fn_stub())
  3160. value |= 1;
  3161. else
  3162. {
  3163. // If we have a MIPS16 function with a stub, the dynamic symbol
  3164. // must refer to the stub, since only the stub uses the standard
  3165. // calling conventions. Stub contains MIPS32 code, so don't add +1
  3166. // in this case.
  3167. // There is a code which does this in the method
  3168. // Target_mips::do_dynsym_value, but that code will only be
  3169. // executed if the symbol is from dynobj.
  3170. // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
  3171. // table.
  3172. Mips16_stub_section<size, big_endian>* fn_stub =
  3173. mips_sym->template get_mips16_fn_stub<big_endian>();
  3174. value = fn_stub->output_address();
  3175. osym.put_st_size(fn_stub->section_size());
  3176. }
  3177. osym.put_st_value(value);
  3178. osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
  3179. mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
  3180. }
  3181. else if ((mips_sym->is_micromips()
  3182. // Stubs are always microMIPS if there is any microMIPS code in
  3183. // the output.
  3184. || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
  3185. && value != 0)
  3186. {
  3187. osym.put_st_value(value | 1);
  3188. osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
  3189. mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
  3190. }
  3191. }
  3192. private:
  3193. // The class which scans relocations.
  3194. class Scan
  3195. {
  3196. public:
  3197. Scan()
  3198. { }
  3199. static inline int
  3200. get_reference_flags(unsigned int r_type);
  3201. inline void
  3202. local(Symbol_table* symtab, Layout* layout, Target_mips* target,
  3203. Sized_relobj_file<size, big_endian>* object,
  3204. unsigned int data_shndx,
  3205. Output_section* output_section,
  3206. const Reltype& reloc, unsigned int r_type,
  3207. const elfcpp::Sym<size, big_endian>& lsym,
  3208. bool is_discarded);
  3209. inline void
  3210. local(Symbol_table* symtab, Layout* layout, Target_mips* target,
  3211. Sized_relobj_file<size, big_endian>* object,
  3212. unsigned int data_shndx,
  3213. Output_section* output_section,
  3214. const Relatype& reloc, unsigned int r_type,
  3215. const elfcpp::Sym<size, big_endian>& lsym,
  3216. bool is_discarded);
  3217. inline void
  3218. local(Symbol_table* symtab, Layout* layout, Target_mips* target,
  3219. Sized_relobj_file<size, big_endian>* object,
  3220. unsigned int data_shndx,
  3221. Output_section* output_section,
  3222. const Relatype* rela,
  3223. const Reltype* rel,
  3224. unsigned int rel_type,
  3225. unsigned int r_type,
  3226. const elfcpp::Sym<size, big_endian>& lsym,
  3227. bool is_discarded);
  3228. inline void
  3229. global(Symbol_table* symtab, Layout* layout, Target_mips* target,
  3230. Sized_relobj_file<size, big_endian>* object,
  3231. unsigned int data_shndx,
  3232. Output_section* output_section,
  3233. const Reltype& reloc, unsigned int r_type,
  3234. Symbol* gsym);
  3235. inline void
  3236. global(Symbol_table* symtab, Layout* layout, Target_mips* target,
  3237. Sized_relobj_file<size, big_endian>* object,
  3238. unsigned int data_shndx,
  3239. Output_section* output_section,
  3240. const Relatype& reloc, unsigned int r_type,
  3241. Symbol* gsym);
  3242. inline void
  3243. global(Symbol_table* symtab, Layout* layout, Target_mips* target,
  3244. Sized_relobj_file<size, big_endian>* object,
  3245. unsigned int data_shndx,
  3246. Output_section* output_section,
  3247. const Relatype* rela,
  3248. const Reltype* rel,
  3249. unsigned int rel_type,
  3250. unsigned int r_type,
  3251. Symbol* gsym);
  3252. inline bool
  3253. local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
  3254. Target_mips*,
  3255. Sized_relobj_file<size, big_endian>*,
  3256. unsigned int,
  3257. Output_section*,
  3258. const Reltype&,
  3259. unsigned int,
  3260. const elfcpp::Sym<size, big_endian>&)
  3261. { return false; }
  3262. inline bool
  3263. global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
  3264. Target_mips*,
  3265. Sized_relobj_file<size, big_endian>*,
  3266. unsigned int,
  3267. Output_section*,
  3268. const Reltype&,
  3269. unsigned int, Symbol*)
  3270. { return false; }
  3271. inline bool
  3272. local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
  3273. Target_mips*,
  3274. Sized_relobj_file<size, big_endian>*,
  3275. unsigned int,
  3276. Output_section*,
  3277. const Relatype&,
  3278. unsigned int,
  3279. const elfcpp::Sym<size, big_endian>&)
  3280. { return false; }
  3281. inline bool
  3282. global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
  3283. Target_mips*,
  3284. Sized_relobj_file<size, big_endian>*,
  3285. unsigned int,
  3286. Output_section*,
  3287. const Relatype&,
  3288. unsigned int, Symbol*)
  3289. { return false; }
  3290. private:
  3291. static void
  3292. unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
  3293. unsigned int r_type);
  3294. static void
  3295. unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
  3296. unsigned int r_type, Symbol*);
  3297. };
  3298. // The class which implements relocation.
  3299. class Relocate
  3300. {
  3301. public:
  3302. Relocate()
  3303. : calculated_value_(0), calculate_only_(false)
  3304. { }
  3305. ~Relocate()
  3306. { }
  3307. // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
  3308. inline bool
  3309. should_apply_static_reloc(const Mips_symbol<size>* gsym,
  3310. unsigned int r_type,
  3311. Output_section* output_section,
  3312. Target_mips* target);
  3313. // Do a relocation. Return false if the caller should not issue
  3314. // any warnings about this relocation.
  3315. inline bool
  3316. relocate(const Relocate_info<size, big_endian>*, unsigned int,
  3317. Target_mips*, Output_section*, size_t, const unsigned char*,
  3318. const Sized_symbol<size>*, const Symbol_value<size>*,
  3319. unsigned char*, Mips_address, section_size_type);
  3320. private:
  3321. // Result of the relocation.
  3322. Valtype calculated_value_;
  3323. // Whether we have to calculate relocation instead of applying it.
  3324. bool calculate_only_;
  3325. };
  3326. // This POD class holds the dynamic relocations that should be emitted instead
  3327. // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
  3328. // relocations if it turns out that the symbol does not have static
  3329. // relocations.
  3330. class Dyn_reloc
  3331. {
  3332. public:
  3333. Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
  3334. Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
  3335. Output_section* output_section, Mips_address r_offset)
  3336. : sym_(sym), r_type_(r_type), relobj_(relobj),
  3337. shndx_(shndx), output_section_(output_section),
  3338. r_offset_(r_offset)
  3339. { }
  3340. // Emit this reloc if appropriate. This is called after we have
  3341. // scanned all the relocations, so we know whether the symbol has
  3342. // static relocations.
  3343. void
  3344. emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
  3345. Symbol_table* symtab)
  3346. {
  3347. if (!this->sym_->has_static_relocs())
  3348. {
  3349. got->record_global_got_symbol(this->sym_, this->relobj_,
  3350. this->r_type_, true, false);
  3351. if (!symbol_references_local(this->sym_,
  3352. this->sym_->should_add_dynsym_entry(symtab)))
  3353. rel_dyn->add_global(this->sym_, this->r_type_,
  3354. this->output_section_, this->relobj_,
  3355. this->shndx_, this->r_offset_);
  3356. else
  3357. rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
  3358. this->output_section_, this->relobj_,
  3359. this->shndx_, this->r_offset_);
  3360. }
  3361. }
  3362. private:
  3363. Mips_symbol<size>* sym_;
  3364. unsigned int r_type_;
  3365. Mips_relobj<size, big_endian>* relobj_;
  3366. unsigned int shndx_;
  3367. Output_section* output_section_;
  3368. Mips_address r_offset_;
  3369. };
  3370. // Adjust TLS relocation type based on the options and whether this
  3371. // is a local symbol.
  3372. static tls::Tls_optimization
  3373. optimize_tls_reloc(bool is_final, int r_type);
  3374. // Return whether there is a GOT section.
  3375. bool
  3376. has_got_section() const
  3377. { return this->got_ != NULL; }
  3378. // Check whether the given ELF header flags describe a 32-bit binary.
  3379. bool
  3380. mips_32bit_flags(elfcpp::Elf_Word);
  3381. enum Mips_mach {
  3382. mach_mips3000 = 3000,
  3383. mach_mips3900 = 3900,
  3384. mach_mips4000 = 4000,
  3385. mach_mips4010 = 4010,
  3386. mach_mips4100 = 4100,
  3387. mach_mips4111 = 4111,
  3388. mach_mips4120 = 4120,
  3389. mach_mips4300 = 4300,
  3390. mach_mips4400 = 4400,
  3391. mach_mips4600 = 4600,
  3392. mach_mips4650 = 4650,
  3393. mach_mips5000 = 5000,
  3394. mach_mips5400 = 5400,
  3395. mach_mips5500 = 5500,
  3396. mach_mips5900 = 5900,
  3397. mach_mips6000 = 6000,
  3398. mach_mips7000 = 7000,
  3399. mach_mips8000 = 8000,
  3400. mach_mips9000 = 9000,
  3401. mach_mips10000 = 10000,
  3402. mach_mips12000 = 12000,
  3403. mach_mips14000 = 14000,
  3404. mach_mips16000 = 16000,
  3405. mach_mips16 = 16,
  3406. mach_mips5 = 5,
  3407. mach_mips_loongson_2e = 3001,
  3408. mach_mips_loongson_2f = 3002,
  3409. mach_mips_gs464 = 3003,
  3410. mach_mips_gs464e = 3004,
  3411. mach_mips_gs264e = 3005,
  3412. mach_mips_sb1 = 12310201, // octal 'SB', 01
  3413. mach_mips_octeon = 6501,
  3414. mach_mips_octeonp = 6601,
  3415. mach_mips_octeon2 = 6502,
  3416. mach_mips_octeon3 = 6503,
  3417. mach_mips_xlr = 887682, // decimal 'XLR'
  3418. mach_mipsisa32 = 32,
  3419. mach_mipsisa32r2 = 33,
  3420. mach_mipsisa32r3 = 34,
  3421. mach_mipsisa32r5 = 36,
  3422. mach_mipsisa32r6 = 37,
  3423. mach_mipsisa64 = 64,
  3424. mach_mipsisa64r2 = 65,
  3425. mach_mipsisa64r3 = 66,
  3426. mach_mipsisa64r5 = 68,
  3427. mach_mipsisa64r6 = 69,
  3428. mach_mips_micromips = 96
  3429. };
  3430. // Return the MACH for a MIPS e_flags value.
  3431. unsigned int
  3432. elf_mips_mach(elfcpp::Elf_Word);
  3433. // Return the MACH for each .MIPS.abiflags ISA Extension.
  3434. unsigned int
  3435. mips_isa_ext_mach(unsigned int);
  3436. // Return the .MIPS.abiflags value representing each ISA Extension.
  3437. unsigned int
  3438. mips_isa_ext(unsigned int);
  3439. // Update the isa_level, isa_rev, isa_ext fields of abiflags.
  3440. void
  3441. update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
  3442. Mips_abiflags<big_endian>*);
  3443. // Infer the content of the ABI flags based on the elf header.
  3444. void
  3445. infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
  3446. // Create abiflags from elf header or from .MIPS.abiflags section.
  3447. void
  3448. create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
  3449. // Return the meaning of fp_abi, or "unknown" if not known.
  3450. const char*
  3451. fp_abi_string(int);
  3452. // Select fp_abi.
  3453. int
  3454. select_fp_abi(const std::string&, int, int);
  3455. // Merge attributes from input object.
  3456. void
  3457. merge_obj_attributes(const std::string&, const Attributes_section_data*);
  3458. // Merge abiflags from input object.
  3459. void
  3460. merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
  3461. // Check whether machine EXTENSION is an extension of machine BASE.
  3462. bool
  3463. mips_mach_extends(unsigned int, unsigned int);
  3464. // Merge file header flags from input object.
  3465. void
  3466. merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
  3467. // Encode ISA level and revision as a single value.
  3468. int
  3469. level_rev(unsigned char isa_level, unsigned char isa_rev) const
  3470. { return (isa_level << 3) | isa_rev; }
  3471. // True if we are linking for CPUs that are faster if JAL is converted to BAL.
  3472. static inline bool
  3473. jal_to_bal()
  3474. { return false; }
  3475. // True if we are linking for CPUs that are faster if JALR is converted to
  3476. // BAL. This should be safe for all architectures. We enable this predicate
  3477. // for all CPUs.
  3478. static inline bool
  3479. jalr_to_bal()
  3480. { return true; }
  3481. // True if we are linking for CPUs that are faster if JR is converted to B.
  3482. // This should be safe for all architectures. We enable this predicate for
  3483. // all CPUs.
  3484. static inline bool
  3485. jr_to_b()
  3486. { return true; }
  3487. // Return the size of the GOT section.
  3488. section_size_type
  3489. got_size() const
  3490. {
  3491. gold_assert(this->got_ != NULL);
  3492. return this->got_->data_size();
  3493. }
  3494. // Create a PLT entry for a global symbol referenced by r_type relocation.
  3495. void
  3496. make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
  3497. unsigned int r_type);
  3498. // Get the PLT section.
  3499. Mips_output_data_plt<size, big_endian>*
  3500. plt_section() const
  3501. {
  3502. gold_assert(this->plt_ != NULL);
  3503. return this->plt_;
  3504. }
  3505. // Get the GOT PLT section.
  3506. const Mips_output_data_plt<size, big_endian>*
  3507. got_plt_section() const
  3508. {
  3509. gold_assert(this->got_plt_ != NULL);
  3510. return this->got_plt_;
  3511. }
  3512. // Copy a relocation against a global symbol.
  3513. void
  3514. copy_reloc(Symbol_table* symtab, Layout* layout,
  3515. Sized_relobj_file<size, big_endian>* object,
  3516. unsigned int shndx, Output_section* output_section,
  3517. Symbol* sym, unsigned int r_type, Mips_address r_offset)
  3518. {
  3519. this->copy_relocs_.copy_reloc(symtab, layout,
  3520. symtab->get_sized_symbol<size>(sym),
  3521. object, shndx, output_section,
  3522. r_type, r_offset, 0,
  3523. this->rel_dyn_section(layout));
  3524. }
  3525. void
  3526. dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
  3527. Mips_relobj<size, big_endian>* relobj,
  3528. unsigned int shndx, Output_section* output_section,
  3529. Mips_address r_offset)
  3530. {
  3531. this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
  3532. output_section, r_offset));
  3533. }
  3534. // Calculate value of _gp symbol.
  3535. void
  3536. set_gp(Layout*, Symbol_table*);
  3537. const char*
  3538. elf_mips_abi_name(elfcpp::Elf_Word e_flags);
  3539. const char*
  3540. elf_mips_mach_name(elfcpp::Elf_Word e_flags);
  3541. // Adds entries that describe how machines relate to one another. The entries
  3542. // are ordered topologically with MIPS I extensions listed last. First
  3543. // element is extension, second element is base.
  3544. void
  3545. add_machine_extensions()
  3546. {
  3547. // MIPS64r2 extensions.
  3548. this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
  3549. this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
  3550. this->add_extension(mach_mips_octeonp, mach_mips_octeon);
  3551. this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
  3552. this->add_extension(mach_mips_gs264e, mach_mips_gs464e);
  3553. this->add_extension(mach_mips_gs464e, mach_mips_gs464);
  3554. this->add_extension(mach_mips_gs464, mach_mipsisa64r2);
  3555. // MIPS64 extensions.
  3556. this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
  3557. this->add_extension(mach_mips_sb1, mach_mipsisa64);
  3558. this->add_extension(mach_mips_xlr, mach_mipsisa64);
  3559. // MIPS V extensions.
  3560. this->add_extension(mach_mipsisa64, mach_mips5);
  3561. // R10000 extensions.
  3562. this->add_extension(mach_mips12000, mach_mips10000);
  3563. this->add_extension(mach_mips14000, mach_mips10000);
  3564. this->add_extension(mach_mips16000, mach_mips10000);
  3565. // R5000 extensions. Note: the vr5500 ISA is an extension of the core
  3566. // vr5400 ISA, but doesn't include the multimedia stuff. It seems
  3567. // better to allow vr5400 and vr5500 code to be merged anyway, since
  3568. // many libraries will just use the core ISA. Perhaps we could add
  3569. // some sort of ASE flag if this ever proves a problem.
  3570. this->add_extension(mach_mips5500, mach_mips5400);
  3571. this->add_extension(mach_mips5400, mach_mips5000);
  3572. // MIPS IV extensions.
  3573. this->add_extension(mach_mips5, mach_mips8000);
  3574. this->add_extension(mach_mips10000, mach_mips8000);
  3575. this->add_extension(mach_mips5000, mach_mips8000);
  3576. this->add_extension(mach_mips7000, mach_mips8000);
  3577. this->add_extension(mach_mips9000, mach_mips8000);
  3578. // VR4100 extensions.
  3579. this->add_extension(mach_mips4120, mach_mips4100);
  3580. this->add_extension(mach_mips4111, mach_mips4100);
  3581. // MIPS III extensions.
  3582. this->add_extension(mach_mips_loongson_2e, mach_mips4000);
  3583. this->add_extension(mach_mips_loongson_2f, mach_mips4000);
  3584. this->add_extension(mach_mips8000, mach_mips4000);
  3585. this->add_extension(mach_mips4650, mach_mips4000);
  3586. this->add_extension(mach_mips4600, mach_mips4000);
  3587. this->add_extension(mach_mips4400, mach_mips4000);
  3588. this->add_extension(mach_mips4300, mach_mips4000);
  3589. this->add_extension(mach_mips4100, mach_mips4000);
  3590. this->add_extension(mach_mips4010, mach_mips4000);
  3591. this->add_extension(mach_mips5900, mach_mips4000);
  3592. // MIPS32 extensions.
  3593. this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
  3594. // MIPS II extensions.
  3595. this->add_extension(mach_mips4000, mach_mips6000);
  3596. this->add_extension(mach_mipsisa32, mach_mips6000);
  3597. // MIPS I extensions.
  3598. this->add_extension(mach_mips6000, mach_mips3000);
  3599. this->add_extension(mach_mips3900, mach_mips3000);
  3600. }
  3601. // Add value to MIPS extenstions.
  3602. void
  3603. add_extension(unsigned int base, unsigned int extension)
  3604. {
  3605. std::pair<unsigned int, unsigned int> ext(base, extension);
  3606. this->mips_mach_extensions_.push_back(ext);
  3607. }
  3608. // Return the number of entries in the .dynsym section.
  3609. unsigned int get_dt_mips_symtabno() const
  3610. {
  3611. return ((unsigned int)(this->layout_->dynsym_section()->data_size()
  3612. / elfcpp::Elf_sizes<size>::sym_size));
  3613. // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
  3614. }
  3615. // Information about this specific target which we pass to the
  3616. // general Target structure.
  3617. static const Target::Target_info mips_info;
  3618. // The GOT section.
  3619. Mips_output_data_got<size, big_endian>* got_;
  3620. // gp symbol. It has the value of .got + 0x7FF0.
  3621. Sized_symbol<size>* gp_;
  3622. // The PLT section.
  3623. Mips_output_data_plt<size, big_endian>* plt_;
  3624. // The GOT PLT section.
  3625. Output_data_space* got_plt_;
  3626. // The dynamic reloc section.
  3627. Reloc_section* rel_dyn_;
  3628. // The .rld_map section.
  3629. Output_data_zero_fill* rld_map_;
  3630. // Relocs saved to avoid a COPY reloc.
  3631. Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
  3632. // A list of dyn relocs to be saved.
  3633. std::vector<Dyn_reloc> dyn_relocs_;
  3634. // The LA25 stub section.
  3635. Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
  3636. // Architecture extensions.
  3637. std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
  3638. // .MIPS.stubs
  3639. Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
  3640. // Attributes section data in output.
  3641. Attributes_section_data* attributes_section_data_;
  3642. // .MIPS.abiflags section data in output.
  3643. Mips_abiflags<big_endian>* abiflags_;
  3644. unsigned int mach_;
  3645. Layout* layout_;
  3646. typename std::list<got16_addend<size, big_endian> > got16_addends_;
  3647. // Whether there is an input .MIPS.abiflags section.
  3648. bool has_abiflags_section_;
  3649. // Whether the entry symbol is mips16 or micromips.
  3650. bool entry_symbol_is_compressed_;
  3651. // Whether we can use only 32-bit microMIPS instructions.
  3652. // TODO(sasa): This should be a linker option.
  3653. bool insn32_;
  3654. };
  3655. // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
  3656. // It records high part of the relocation pair.
  3657. template<int size, bool big_endian>
  3658. struct reloc_high
  3659. {
  3660. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  3661. reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
  3662. const Symbol_value<size>* _psymval, Mips_address _addend,
  3663. unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
  3664. Mips_address _address = 0, bool _gp_disp = false)
  3665. : view(_view), object(_object), psymval(_psymval), addend(_addend),
  3666. r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
  3667. address(_address), gp_disp(_gp_disp)
  3668. { }
  3669. unsigned char* view;
  3670. const Mips_relobj<size, big_endian>* object;
  3671. const Symbol_value<size>* psymval;
  3672. Mips_address addend;
  3673. unsigned int r_type;
  3674. unsigned int r_sym;
  3675. bool extract_addend;
  3676. Mips_address address;
  3677. bool gp_disp;
  3678. };
  3679. template<int size, bool big_endian>
  3680. class Mips_relocate_functions : public Relocate_functions<size, big_endian>
  3681. {
  3682. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  3683. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  3684. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
  3685. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
  3686. typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
  3687. public:
  3688. typedef enum
  3689. {
  3690. STATUS_OKAY, // No error during relocation.
  3691. STATUS_OVERFLOW, // Relocation overflow.
  3692. STATUS_BAD_RELOC, // Relocation cannot be applied.
  3693. STATUS_PCREL_UNALIGNED // Unaligned PC-relative relocation.
  3694. } Status;
  3695. private:
  3696. typedef Relocate_functions<size, big_endian> Base;
  3697. typedef Mips_relocate_functions<size, big_endian> This;
  3698. static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
  3699. static typename std::list<reloc_high<size, big_endian> > got16_relocs;
  3700. static typename std::list<reloc_high<size, big_endian> > pchi16_relocs;
  3701. template<int valsize>
  3702. static inline typename This::Status
  3703. check_overflow(Valtype value)
  3704. {
  3705. if (size == 32)
  3706. return (Bits<valsize>::has_overflow32(value)
  3707. ? This::STATUS_OVERFLOW
  3708. : This::STATUS_OKAY);
  3709. return (Bits<valsize>::has_overflow(value)
  3710. ? This::STATUS_OVERFLOW
  3711. : This::STATUS_OKAY);
  3712. }
  3713. static inline bool
  3714. should_shuffle_micromips_reloc(unsigned int r_type)
  3715. {
  3716. return (micromips_reloc(r_type)
  3717. && r_type != elfcpp::R_MICROMIPS_PC7_S1
  3718. && r_type != elfcpp::R_MICROMIPS_PC10_S1);
  3719. }
  3720. public:
  3721. // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
  3722. // Most mips16 instructions are 16 bits, but these instructions
  3723. // are 32 bits.
  3724. //
  3725. // The format of these instructions is:
  3726. //
  3727. // +--------------+--------------------------------+
  3728. // | JALX | X| Imm 20:16 | Imm 25:21 |
  3729. // +--------------+--------------------------------+
  3730. // | Immediate 15:0 |
  3731. // +-----------------------------------------------+
  3732. //
  3733. // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
  3734. // Note that the immediate value in the first word is swapped.
  3735. //
  3736. // When producing a relocatable object file, R_MIPS16_26 is
  3737. // handled mostly like R_MIPS_26. In particular, the addend is
  3738. // stored as a straight 26-bit value in a 32-bit instruction.
  3739. // (gas makes life simpler for itself by never adjusting a
  3740. // R_MIPS16_26 reloc to be against a section, so the addend is
  3741. // always zero). However, the 32 bit instruction is stored as 2
  3742. // 16-bit values, rather than a single 32-bit value. In a
  3743. // big-endian file, the result is the same; in a little-endian
  3744. // file, the two 16-bit halves of the 32 bit value are swapped.
  3745. // This is so that a disassembler can recognize the jal
  3746. // instruction.
  3747. //
  3748. // When doing a final link, R_MIPS16_26 is treated as a 32 bit
  3749. // instruction stored as two 16-bit values. The addend A is the
  3750. // contents of the targ26 field. The calculation is the same as
  3751. // R_MIPS_26. When storing the calculated value, reorder the
  3752. // immediate value as shown above, and don't forget to store the
  3753. // value as two 16-bit values.
  3754. //
  3755. // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
  3756. // defined as
  3757. //
  3758. // big-endian:
  3759. // +--------+----------------------+
  3760. // | | |
  3761. // | | targ26-16 |
  3762. // |31 26|25 0|
  3763. // +--------+----------------------+
  3764. //
  3765. // little-endian:
  3766. // +----------+------+-------------+
  3767. // | | | |
  3768. // | sub1 | | sub2 |
  3769. // |0 9|10 15|16 31|
  3770. // +----------+--------------------+
  3771. // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
  3772. // ((sub1 << 16) | sub2)).
  3773. //
  3774. // When producing a relocatable object file, the calculation is
  3775. // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
  3776. // When producing a fully linked file, the calculation is
  3777. // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
  3778. // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
  3779. //
  3780. // The table below lists the other MIPS16 instruction relocations.
  3781. // Each one is calculated in the same way as the non-MIPS16 relocation
  3782. // given on the right, but using the extended MIPS16 layout of 16-bit
  3783. // immediate fields:
  3784. //
  3785. // R_MIPS16_GPREL R_MIPS_GPREL16
  3786. // R_MIPS16_GOT16 R_MIPS_GOT16
  3787. // R_MIPS16_CALL16 R_MIPS_CALL16
  3788. // R_MIPS16_HI16 R_MIPS_HI16
  3789. // R_MIPS16_LO16 R_MIPS_LO16
  3790. //
  3791. // A typical instruction will have a format like this:
  3792. //
  3793. // +--------------+--------------------------------+
  3794. // | EXTEND | Imm 10:5 | Imm 15:11 |
  3795. // +--------------+--------------------------------+
  3796. // | Major | rx | ry | Imm 4:0 |
  3797. // +--------------+--------------------------------+
  3798. //
  3799. // EXTEND is the five bit value 11110. Major is the instruction
  3800. // opcode.
  3801. //
  3802. // All we need to do here is shuffle the bits appropriately.
  3803. // As above, the two 16-bit halves must be swapped on a
  3804. // little-endian system.
  3805. // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
  3806. // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
  3807. // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
  3808. static void
  3809. mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
  3810. bool jal_shuffle)
  3811. {
  3812. if (!mips16_reloc(r_type)
  3813. && !should_shuffle_micromips_reloc(r_type))
  3814. return;
  3815. // Pick up the first and second halfwords of the instruction.
  3816. Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
  3817. Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
  3818. Valtype32 val;
  3819. if (micromips_reloc(r_type)
  3820. || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
  3821. val = first << 16 | second;
  3822. else if (r_type != elfcpp::R_MIPS16_26)
  3823. val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
  3824. | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
  3825. else
  3826. val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
  3827. | ((first & 0x1f) << 21) | second);
  3828. elfcpp::Swap<32, big_endian>::writeval(view, val);
  3829. }
  3830. static void
  3831. mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
  3832. {
  3833. if (!mips16_reloc(r_type)
  3834. && !should_shuffle_micromips_reloc(r_type))
  3835. return;
  3836. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  3837. Valtype16 first, second;
  3838. if (micromips_reloc(r_type)
  3839. || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
  3840. {
  3841. second = val & 0xffff;
  3842. first = val >> 16;
  3843. }
  3844. else if (r_type != elfcpp::R_MIPS16_26)
  3845. {
  3846. second = ((val >> 11) & 0xffe0) | (val & 0x1f);
  3847. first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
  3848. }
  3849. else
  3850. {
  3851. second = val & 0xffff;
  3852. first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
  3853. | ((val >> 21) & 0x1f);
  3854. }
  3855. elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
  3856. elfcpp::Swap<16, big_endian>::writeval(view, first);
  3857. }
  3858. // R_MIPS_16: S + sign-extend(A)
  3859. static inline typename This::Status
  3860. rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3861. const Symbol_value<size>* psymval, Mips_address addend_a,
  3862. bool extract_addend, bool calculate_only, Valtype* calculated_value)
  3863. {
  3864. Valtype16* wv = reinterpret_cast<Valtype16*>(view);
  3865. Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
  3866. Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
  3867. : addend_a);
  3868. Valtype x = psymval->value(object, addend);
  3869. val = Bits<16>::bit_select32(val, x, 0xffffU);
  3870. if (calculate_only)
  3871. {
  3872. *calculated_value = x;
  3873. return This::STATUS_OKAY;
  3874. }
  3875. else
  3876. elfcpp::Swap<16, big_endian>::writeval(wv, val);
  3877. return check_overflow<16>(x);
  3878. }
  3879. // R_MIPS_32: S + A
  3880. static inline typename This::Status
  3881. rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3882. const Symbol_value<size>* psymval, Mips_address addend_a,
  3883. bool extract_addend, bool calculate_only, Valtype* calculated_value)
  3884. {
  3885. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3886. Valtype addend = (extract_addend
  3887. ? elfcpp::Swap<32, big_endian>::readval(wv)
  3888. : addend_a);
  3889. Valtype x = psymval->value(object, addend);
  3890. if (calculate_only)
  3891. *calculated_value = x;
  3892. else
  3893. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  3894. return This::STATUS_OKAY;
  3895. }
  3896. // R_MIPS_JALR, R_MICROMIPS_JALR
  3897. static inline typename This::Status
  3898. reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3899. const Symbol_value<size>* psymval, Mips_address address,
  3900. Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
  3901. unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
  3902. bool calculate_only, Valtype* calculated_value)
  3903. {
  3904. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3905. Valtype addend = extract_addend ? 0 : addend_a;
  3906. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3907. // Try converting J(AL)R to B(AL), if the target is in range.
  3908. if (r_type == elfcpp::R_MIPS_JALR
  3909. && !cross_mode_jump
  3910. && ((jalr_to_bal && val == 0x0320f809) // jalr t9
  3911. || (jr_to_b && val == 0x03200008))) // jr t9
  3912. {
  3913. int offset = psymval->value(object, addend) - (address + 4);
  3914. if (!Bits<18>::has_overflow32(offset))
  3915. {
  3916. if (val == 0x03200008) // jr t9
  3917. val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
  3918. else
  3919. val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
  3920. }
  3921. }
  3922. if (calculate_only)
  3923. *calculated_value = val;
  3924. else
  3925. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3926. return This::STATUS_OKAY;
  3927. }
  3928. // R_MIPS_PC32: S + A - P
  3929. static inline typename This::Status
  3930. relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3931. const Symbol_value<size>* psymval, Mips_address address,
  3932. Mips_address addend_a, bool extract_addend, bool calculate_only,
  3933. Valtype* calculated_value)
  3934. {
  3935. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3936. Valtype addend = (extract_addend
  3937. ? elfcpp::Swap<32, big_endian>::readval(wv)
  3938. : addend_a);
  3939. Valtype x = psymval->value(object, addend) - address;
  3940. if (calculate_only)
  3941. *calculated_value = x;
  3942. else
  3943. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  3944. return This::STATUS_OKAY;
  3945. }
  3946. // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
  3947. static inline typename This::Status
  3948. rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3949. const Symbol_value<size>* psymval, Mips_address address,
  3950. bool local, Mips_address addend_a, bool extract_addend,
  3951. const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
  3952. bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
  3953. {
  3954. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3955. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3956. Valtype addend;
  3957. if (extract_addend)
  3958. {
  3959. if (r_type == elfcpp::R_MICROMIPS_26_S1)
  3960. addend = (val & 0x03ffffff) << 1;
  3961. else
  3962. addend = (val & 0x03ffffff) << 2;
  3963. }
  3964. else
  3965. addend = addend_a;
  3966. // Make sure the target of JALX is word-aligned. Bit 0 must be
  3967. // the correct ISA mode selector and bit 1 must be 0.
  3968. if (!calculate_only && cross_mode_jump
  3969. && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
  3970. {
  3971. gold_warning(_("JALX to a non-word-aligned address"));
  3972. return This::STATUS_BAD_RELOC;
  3973. }
  3974. // Shift is 2, unusually, for microMIPS JALX.
  3975. unsigned int shift =
  3976. (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
  3977. Valtype x;
  3978. if (local)
  3979. x = addend | ((address + 4) & (0xfc000000 << shift));
  3980. else
  3981. {
  3982. if (shift == 1)
  3983. x = Bits<27>::sign_extend32(addend);
  3984. else
  3985. x = Bits<28>::sign_extend32(addend);
  3986. }
  3987. x = psymval->value(object, x) >> shift;
  3988. if (!calculate_only && !local && !gsym->is_weak_undefined()
  3989. && ((x >> 26) != ((address + 4) >> (26 + shift))))
  3990. return This::STATUS_OVERFLOW;
  3991. val = Bits<32>::bit_select32(val, x, 0x03ffffff);
  3992. // If required, turn JAL into JALX.
  3993. if (cross_mode_jump)
  3994. {
  3995. bool ok;
  3996. Valtype32 opcode = val >> 26;
  3997. Valtype32 jalx_opcode;
  3998. // Check to see if the opcode is already JAL or JALX.
  3999. if (r_type == elfcpp::R_MIPS16_26)
  4000. {
  4001. ok = (opcode == 0x6) || (opcode == 0x7);
  4002. jalx_opcode = 0x7;
  4003. }
  4004. else if (r_type == elfcpp::R_MICROMIPS_26_S1)
  4005. {
  4006. ok = (opcode == 0x3d) || (opcode == 0x3c);
  4007. jalx_opcode = 0x3c;
  4008. }
  4009. else
  4010. {
  4011. ok = (opcode == 0x3) || (opcode == 0x1d);
  4012. jalx_opcode = 0x1d;
  4013. }
  4014. // If the opcode is not JAL or JALX, there's a problem. We cannot
  4015. // convert J or JALS to JALX.
  4016. if (!calculate_only && !ok)
  4017. {
  4018. gold_error(_("Unsupported jump between ISA modes; consider "
  4019. "recompiling with interlinking enabled."));
  4020. return This::STATUS_BAD_RELOC;
  4021. }
  4022. // Make this the JALX opcode.
  4023. val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
  4024. }
  4025. // Try converting JAL to BAL, if the target is in range.
  4026. if (!parameters->options().relocatable()
  4027. && !cross_mode_jump
  4028. && ((jal_to_bal
  4029. && r_type == elfcpp::R_MIPS_26
  4030. && (val >> 26) == 0x3))) // jal addr
  4031. {
  4032. Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
  4033. int offset = dest - (address + 4);
  4034. if (!Bits<18>::has_overflow32(offset))
  4035. {
  4036. if (val == 0x03200008) // jr t9
  4037. val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
  4038. else
  4039. val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
  4040. }
  4041. }
  4042. if (calculate_only)
  4043. *calculated_value = val;
  4044. else
  4045. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4046. return This::STATUS_OKAY;
  4047. }
  4048. // R_MIPS_PC16
  4049. static inline typename This::Status
  4050. relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4051. const Symbol_value<size>* psymval, Mips_address address,
  4052. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4053. Valtype* calculated_value)
  4054. {
  4055. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4056. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4057. Valtype addend = (extract_addend
  4058. ? Bits<18>::sign_extend32((val & 0xffff) << 2)
  4059. : addend_a);
  4060. Valtype x = psymval->value(object, addend) - address;
  4061. val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
  4062. if (calculate_only)
  4063. {
  4064. *calculated_value = x >> 2;
  4065. return This::STATUS_OKAY;
  4066. }
  4067. else
  4068. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4069. if (psymval->value(object, addend) & 3)
  4070. return This::STATUS_PCREL_UNALIGNED;
  4071. return check_overflow<18>(x);
  4072. }
  4073. // R_MIPS_PC21_S2
  4074. static inline typename This::Status
  4075. relpc21(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4076. const Symbol_value<size>* psymval, Mips_address address,
  4077. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4078. Valtype* calculated_value)
  4079. {
  4080. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4081. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4082. Valtype addend = (extract_addend
  4083. ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
  4084. : addend_a);
  4085. Valtype x = psymval->value(object, addend) - address;
  4086. val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
  4087. if (calculate_only)
  4088. {
  4089. *calculated_value = x >> 2;
  4090. return This::STATUS_OKAY;
  4091. }
  4092. else
  4093. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4094. if (psymval->value(object, addend) & 3)
  4095. return This::STATUS_PCREL_UNALIGNED;
  4096. return check_overflow<23>(x);
  4097. }
  4098. // R_MIPS_PC26_S2
  4099. static inline typename This::Status
  4100. relpc26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4101. const Symbol_value<size>* psymval, Mips_address address,
  4102. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4103. Valtype* calculated_value)
  4104. {
  4105. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4106. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4107. Valtype addend = (extract_addend
  4108. ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
  4109. : addend_a);
  4110. Valtype x = psymval->value(object, addend) - address;
  4111. val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
  4112. if (calculate_only)
  4113. {
  4114. *calculated_value = x >> 2;
  4115. return This::STATUS_OKAY;
  4116. }
  4117. else
  4118. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4119. if (psymval->value(object, addend) & 3)
  4120. return This::STATUS_PCREL_UNALIGNED;
  4121. return check_overflow<28>(x);
  4122. }
  4123. // R_MIPS_PC18_S3
  4124. static inline typename This::Status
  4125. relpc18(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4126. const Symbol_value<size>* psymval, Mips_address address,
  4127. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4128. Valtype* calculated_value)
  4129. {
  4130. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4131. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4132. Valtype addend = (extract_addend
  4133. ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
  4134. : addend_a);
  4135. Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
  4136. val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
  4137. if (calculate_only)
  4138. {
  4139. *calculated_value = x >> 3;
  4140. return This::STATUS_OKAY;
  4141. }
  4142. else
  4143. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4144. if (psymval->value(object, addend) & 7)
  4145. return This::STATUS_PCREL_UNALIGNED;
  4146. return check_overflow<21>(x);
  4147. }
  4148. // R_MIPS_PC19_S2
  4149. static inline typename This::Status
  4150. relpc19(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4151. const Symbol_value<size>* psymval, Mips_address address,
  4152. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4153. Valtype* calculated_value)
  4154. {
  4155. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4156. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4157. Valtype addend = (extract_addend
  4158. ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
  4159. : addend_a);
  4160. Valtype x = psymval->value(object, addend) - address;
  4161. val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
  4162. if (calculate_only)
  4163. {
  4164. *calculated_value = x >> 2;
  4165. return This::STATUS_OKAY;
  4166. }
  4167. else
  4168. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4169. if (psymval->value(object, addend) & 3)
  4170. return This::STATUS_PCREL_UNALIGNED;
  4171. return check_overflow<21>(x);
  4172. }
  4173. // R_MIPS_PCHI16
  4174. static inline typename This::Status
  4175. relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4176. const Symbol_value<size>* psymval, Mips_address addend,
  4177. Mips_address address, unsigned int r_sym, bool extract_addend)
  4178. {
  4179. // Record the relocation. It will be resolved when we find pclo16 part.
  4180. pchi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
  4181. addend, 0, r_sym, extract_addend, address));
  4182. return This::STATUS_OKAY;
  4183. }
  4184. // R_MIPS_PCHI16
  4185. static inline typename This::Status
  4186. do_relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4187. const Symbol_value<size>* psymval, Mips_address addend_hi,
  4188. Mips_address address, bool extract_addend, Valtype32 addend_lo,
  4189. bool calculate_only, Valtype* calculated_value)
  4190. {
  4191. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4192. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4193. Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
  4194. : addend_hi);
  4195. Valtype value = psymval->value(object, addend) - address;
  4196. Valtype x = ((value + 0x8000) >> 16) & 0xffff;
  4197. val = Bits<32>::bit_select32(val, x, 0xffff);
  4198. if (calculate_only)
  4199. *calculated_value = x;
  4200. else
  4201. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4202. return This::STATUS_OKAY;
  4203. }
  4204. // R_MIPS_PCLO16
  4205. static inline typename This::Status
  4206. relpclo16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4207. const Symbol_value<size>* psymval, Mips_address addend_a,
  4208. bool extract_addend, Mips_address address, unsigned int r_sym,
  4209. unsigned int rel_type, bool calculate_only,
  4210. Valtype* calculated_value)
  4211. {
  4212. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4213. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4214. Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
  4215. : addend_a);
  4216. if (rel_type == elfcpp::SHT_REL)
  4217. {
  4218. // Resolve pending R_MIPS_PCHI16 relocations.
  4219. typename std::list<reloc_high<size, big_endian> >::iterator it =
  4220. pchi16_relocs.begin();
  4221. while (it != pchi16_relocs.end())
  4222. {
  4223. reloc_high<size, big_endian> pchi16 = *it;
  4224. if (pchi16.r_sym == r_sym)
  4225. {
  4226. do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
  4227. pchi16.addend, pchi16.address,
  4228. pchi16.extract_addend, addend, calculate_only,
  4229. calculated_value);
  4230. it = pchi16_relocs.erase(it);
  4231. }
  4232. else
  4233. ++it;
  4234. }
  4235. }
  4236. // Resolve R_MIPS_PCLO16 relocation.
  4237. Valtype x = psymval->value(object, addend) - address;
  4238. val = Bits<32>::bit_select32(val, x, 0xffff);
  4239. if (calculate_only)
  4240. *calculated_value = x;
  4241. else
  4242. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4243. return This::STATUS_OKAY;
  4244. }
  4245. // R_MICROMIPS_PC7_S1
  4246. static inline typename This::Status
  4247. relmicromips_pc7_s1(unsigned char* view,
  4248. const Mips_relobj<size, big_endian>* object,
  4249. const Symbol_value<size>* psymval, Mips_address address,
  4250. Mips_address addend_a, bool extract_addend,
  4251. bool calculate_only, Valtype* calculated_value)
  4252. {
  4253. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4254. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4255. Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
  4256. : addend_a;
  4257. Valtype x = psymval->value(object, addend) - address;
  4258. val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
  4259. if (calculate_only)
  4260. {
  4261. *calculated_value = x >> 1;
  4262. return This::STATUS_OKAY;
  4263. }
  4264. else
  4265. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4266. return check_overflow<8>(x);
  4267. }
  4268. // R_MICROMIPS_PC10_S1
  4269. static inline typename This::Status
  4270. relmicromips_pc10_s1(unsigned char* view,
  4271. const Mips_relobj<size, big_endian>* object,
  4272. const Symbol_value<size>* psymval, Mips_address address,
  4273. Mips_address addend_a, bool extract_addend,
  4274. bool calculate_only, Valtype* calculated_value)
  4275. {
  4276. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4277. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4278. Valtype addend = (extract_addend
  4279. ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
  4280. : addend_a);
  4281. Valtype x = psymval->value(object, addend) - address;
  4282. val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
  4283. if (calculate_only)
  4284. {
  4285. *calculated_value = x >> 1;
  4286. return This::STATUS_OKAY;
  4287. }
  4288. else
  4289. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4290. return check_overflow<11>(x);
  4291. }
  4292. // R_MICROMIPS_PC16_S1
  4293. static inline typename This::Status
  4294. relmicromips_pc16_s1(unsigned char* view,
  4295. const Mips_relobj<size, big_endian>* object,
  4296. const Symbol_value<size>* psymval, Mips_address address,
  4297. Mips_address addend_a, bool extract_addend,
  4298. bool calculate_only, Valtype* calculated_value)
  4299. {
  4300. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4301. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4302. Valtype addend = (extract_addend
  4303. ? Bits<17>::sign_extend32((val & 0xffff) << 1)
  4304. : addend_a);
  4305. Valtype x = psymval->value(object, addend) - address;
  4306. val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
  4307. if (calculate_only)
  4308. {
  4309. *calculated_value = x >> 1;
  4310. return This::STATUS_OKAY;
  4311. }
  4312. else
  4313. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4314. return check_overflow<17>(x);
  4315. }
  4316. // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
  4317. static inline typename This::Status
  4318. relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4319. const Symbol_value<size>* psymval, Mips_address addend,
  4320. Mips_address address, bool gp_disp, unsigned int r_type,
  4321. unsigned int r_sym, bool extract_addend)
  4322. {
  4323. // Record the relocation. It will be resolved when we find lo16 part.
  4324. hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
  4325. addend, r_type, r_sym, extract_addend, address,
  4326. gp_disp));
  4327. return This::STATUS_OKAY;
  4328. }
  4329. // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
  4330. static inline typename This::Status
  4331. do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4332. const Symbol_value<size>* psymval, Mips_address addend_hi,
  4333. Mips_address address, bool is_gp_disp, unsigned int r_type,
  4334. bool extract_addend, Valtype32 addend_lo,
  4335. Target_mips<size, big_endian>* target, bool calculate_only,
  4336. Valtype* calculated_value)
  4337. {
  4338. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4339. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4340. Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
  4341. : addend_hi);
  4342. Valtype32 value;
  4343. if (!is_gp_disp)
  4344. value = psymval->value(object, addend);
  4345. else
  4346. {
  4347. // For MIPS16 ABI code we generate this sequence
  4348. // 0: li $v0,%hi(_gp_disp)
  4349. // 4: addiupc $v1,%lo(_gp_disp)
  4350. // 8: sll $v0,16
  4351. // 12: addu $v0,$v1
  4352. // 14: move $gp,$v0
  4353. // So the offsets of hi and lo relocs are the same, but the
  4354. // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
  4355. // ADDIUPC clears the low two bits of the instruction address,
  4356. // so the base is ($t9 + 4) & ~3.
  4357. Valtype32 gp_disp;
  4358. if (r_type == elfcpp::R_MIPS16_HI16)
  4359. gp_disp = (target->adjusted_gp_value(object)
  4360. - ((address + 4) & ~0x3));
  4361. // The microMIPS .cpload sequence uses the same assembly
  4362. // instructions as the traditional psABI version, but the
  4363. // incoming $t9 has the low bit set.
  4364. else if (r_type == elfcpp::R_MICROMIPS_HI16)
  4365. gp_disp = target->adjusted_gp_value(object) - address - 1;
  4366. else
  4367. gp_disp = target->adjusted_gp_value(object) - address;
  4368. value = gp_disp + addend;
  4369. }
  4370. Valtype x = ((value + 0x8000) >> 16) & 0xffff;
  4371. val = Bits<32>::bit_select32(val, x, 0xffff);
  4372. if (calculate_only)
  4373. {
  4374. *calculated_value = x;
  4375. return This::STATUS_OKAY;
  4376. }
  4377. else
  4378. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4379. return (is_gp_disp ? check_overflow<16>(x)
  4380. : This::STATUS_OKAY);
  4381. }
  4382. // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
  4383. static inline typename This::Status
  4384. relgot16_local(unsigned char* view,
  4385. const Mips_relobj<size, big_endian>* object,
  4386. const Symbol_value<size>* psymval, Mips_address addend_a,
  4387. bool extract_addend, unsigned int r_type, unsigned int r_sym)
  4388. {
  4389. // Record the relocation. It will be resolved when we find lo16 part.
  4390. got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
  4391. addend_a, r_type, r_sym, extract_addend));
  4392. return This::STATUS_OKAY;
  4393. }
  4394. // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
  4395. static inline typename This::Status
  4396. do_relgot16_local(unsigned char* view,
  4397. const Mips_relobj<size, big_endian>* object,
  4398. const Symbol_value<size>* psymval, Mips_address addend_hi,
  4399. bool extract_addend, Valtype32 addend_lo,
  4400. Target_mips<size, big_endian>* target, bool calculate_only,
  4401. Valtype* calculated_value)
  4402. {
  4403. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4404. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4405. Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
  4406. : addend_hi);
  4407. // Find GOT page entry.
  4408. Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
  4409. & 0xffff;
  4410. value <<= 16;
  4411. unsigned int got_offset =
  4412. target->got_section()->get_got_page_offset(value, object);
  4413. // Resolve the relocation.
  4414. Valtype x = target->got_section()->gp_offset(got_offset, object);
  4415. val = Bits<32>::bit_select32(val, x, 0xffff);
  4416. if (calculate_only)
  4417. {
  4418. *calculated_value = x;
  4419. return This::STATUS_OKAY;
  4420. }
  4421. else
  4422. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4423. return check_overflow<16>(x);
  4424. }
  4425. // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
  4426. static inline typename This::Status
  4427. rello16(Target_mips<size, big_endian>* target, unsigned char* view,
  4428. const Mips_relobj<size, big_endian>* object,
  4429. const Symbol_value<size>* psymval, Mips_address addend_a,
  4430. bool extract_addend, Mips_address address, bool is_gp_disp,
  4431. unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
  4432. bool calculate_only, Valtype* calculated_value)
  4433. {
  4434. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4435. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4436. Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
  4437. : addend_a);
  4438. if (rel_type == elfcpp::SHT_REL)
  4439. {
  4440. typename This::Status reloc_status = This::STATUS_OKAY;
  4441. // Resolve pending R_MIPS_HI16 relocations.
  4442. typename std::list<reloc_high<size, big_endian> >::iterator it =
  4443. hi16_relocs.begin();
  4444. while (it != hi16_relocs.end())
  4445. {
  4446. reloc_high<size, big_endian> hi16 = *it;
  4447. if (hi16.r_sym == r_sym
  4448. && is_matching_lo16_reloc(hi16.r_type, r_type))
  4449. {
  4450. mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
  4451. reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
  4452. hi16.addend, hi16.address, hi16.gp_disp,
  4453. hi16.r_type, hi16.extract_addend, addend,
  4454. target, calculate_only, calculated_value);
  4455. mips_reloc_shuffle(hi16.view, hi16.r_type, false);
  4456. if (reloc_status == This::STATUS_OVERFLOW)
  4457. return This::STATUS_OVERFLOW;
  4458. it = hi16_relocs.erase(it);
  4459. }
  4460. else
  4461. ++it;
  4462. }
  4463. // Resolve pending local R_MIPS_GOT16 relocations.
  4464. typename std::list<reloc_high<size, big_endian> >::iterator it2 =
  4465. got16_relocs.begin();
  4466. while (it2 != got16_relocs.end())
  4467. {
  4468. reloc_high<size, big_endian> got16 = *it2;
  4469. if (got16.r_sym == r_sym
  4470. && is_matching_lo16_reloc(got16.r_type, r_type))
  4471. {
  4472. mips_reloc_unshuffle(got16.view, got16.r_type, false);
  4473. reloc_status = do_relgot16_local(got16.view, got16.object,
  4474. got16.psymval, got16.addend,
  4475. got16.extract_addend, addend, target,
  4476. calculate_only, calculated_value);
  4477. mips_reloc_shuffle(got16.view, got16.r_type, false);
  4478. if (reloc_status == This::STATUS_OVERFLOW)
  4479. return This::STATUS_OVERFLOW;
  4480. it2 = got16_relocs.erase(it2);
  4481. }
  4482. else
  4483. ++it2;
  4484. }
  4485. }
  4486. // Resolve R_MIPS_LO16 relocation.
  4487. Valtype x;
  4488. if (!is_gp_disp)
  4489. x = psymval->value(object, addend);
  4490. else
  4491. {
  4492. // See the comment for R_MIPS16_HI16 above for the reason
  4493. // for this conditional.
  4494. Valtype32 gp_disp;
  4495. if (r_type == elfcpp::R_MIPS16_LO16)
  4496. gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
  4497. else if (r_type == elfcpp::R_MICROMIPS_LO16
  4498. || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
  4499. gp_disp = target->adjusted_gp_value(object) - address + 3;
  4500. else
  4501. gp_disp = target->adjusted_gp_value(object) - address + 4;
  4502. // The MIPS ABI requires checking the R_MIPS_LO16 relocation
  4503. // for overflow. Relocations against _gp_disp are normally
  4504. // generated from the .cpload pseudo-op. It generates code
  4505. // that normally looks like this:
  4506. // lui $gp,%hi(_gp_disp)
  4507. // addiu $gp,$gp,%lo(_gp_disp)
  4508. // addu $gp,$gp,$t9
  4509. // Here $t9 holds the address of the function being called,
  4510. // as required by the MIPS ELF ABI. The R_MIPS_LO16
  4511. // relocation can easily overflow in this situation, but the
  4512. // R_MIPS_HI16 relocation will handle the overflow.
  4513. // Therefore, we consider this a bug in the MIPS ABI, and do
  4514. // not check for overflow here.
  4515. x = gp_disp + addend;
  4516. }
  4517. val = Bits<32>::bit_select32(val, x, 0xffff);
  4518. if (calculate_only)
  4519. *calculated_value = x;
  4520. else
  4521. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4522. return This::STATUS_OKAY;
  4523. }
  4524. // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
  4525. // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
  4526. // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
  4527. // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
  4528. // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
  4529. // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
  4530. static inline typename This::Status
  4531. relgot(unsigned char* view, int gp_offset, bool calculate_only,
  4532. Valtype* calculated_value)
  4533. {
  4534. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4535. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4536. Valtype x = gp_offset;
  4537. val = Bits<32>::bit_select32(val, x, 0xffff);
  4538. if (calculate_only)
  4539. {
  4540. *calculated_value = x;
  4541. return This::STATUS_OKAY;
  4542. }
  4543. else
  4544. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4545. return check_overflow<16>(x);
  4546. }
  4547. // R_MIPS_EH
  4548. static inline typename This::Status
  4549. releh(unsigned char* view, int gp_offset, bool calculate_only,
  4550. Valtype* calculated_value)
  4551. {
  4552. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4553. Valtype x = gp_offset;
  4554. if (calculate_only)
  4555. {
  4556. *calculated_value = x;
  4557. return This::STATUS_OKAY;
  4558. }
  4559. else
  4560. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  4561. return check_overflow<32>(x);
  4562. }
  4563. // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
  4564. static inline typename This::Status
  4565. relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
  4566. const Mips_relobj<size, big_endian>* object,
  4567. const Symbol_value<size>* psymval, Mips_address addend_a,
  4568. bool extract_addend, bool calculate_only,
  4569. Valtype* calculated_value)
  4570. {
  4571. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4572. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  4573. Valtype addend = extract_addend ? val & 0xffff : addend_a;
  4574. // Find a GOT page entry that points to within 32KB of symbol + addend.
  4575. Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
  4576. unsigned int got_offset =
  4577. target->got_section()->get_got_page_offset(value, object);
  4578. Valtype x = target->got_section()->gp_offset(got_offset, object);
  4579. val = Bits<32>::bit_select32(val, x, 0xffff);
  4580. if (calculate_only)
  4581. {
  4582. *calculated_value = x;
  4583. return This::STATUS_OKAY;
  4584. }
  4585. else
  4586. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4587. return check_overflow<16>(x);
  4588. }
  4589. // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
  4590. static inline typename This::Status
  4591. relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
  4592. const Mips_relobj<size, big_endian>* object,
  4593. const Symbol_value<size>* psymval, Mips_address addend_a,
  4594. bool extract_addend, bool local, bool calculate_only,
  4595. Valtype* calculated_value)
  4596. {
  4597. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4598. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  4599. Valtype addend = extract_addend ? val & 0xffff : addend_a;
  4600. // For a local symbol, find a GOT page entry that points to within 32KB of
  4601. // symbol + addend. Relocation value is the offset of the GOT page entry's
  4602. // value from symbol + addend.
  4603. // For a global symbol, relocation value is addend.
  4604. Valtype x;
  4605. if (local)
  4606. {
  4607. // Find GOT page entry.
  4608. Mips_address value = ((psymval->value(object, addend) + 0x8000)
  4609. & ~0xffff);
  4610. target->got_section()->get_got_page_offset(value, object);
  4611. x = psymval->value(object, addend) - value;
  4612. }
  4613. else
  4614. x = addend;
  4615. val = Bits<32>::bit_select32(val, x, 0xffff);
  4616. if (calculate_only)
  4617. {
  4618. *calculated_value = x;
  4619. return This::STATUS_OKAY;
  4620. }
  4621. else
  4622. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4623. return check_overflow<16>(x);
  4624. }
  4625. // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
  4626. // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
  4627. static inline typename This::Status
  4628. relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
  4629. Valtype* calculated_value)
  4630. {
  4631. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4632. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4633. Valtype x = gp_offset;
  4634. x = ((x + 0x8000) >> 16) & 0xffff;
  4635. val = Bits<32>::bit_select32(val, x, 0xffff);
  4636. if (calculate_only)
  4637. *calculated_value = x;
  4638. else
  4639. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4640. return This::STATUS_OKAY;
  4641. }
  4642. // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
  4643. // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
  4644. static inline typename This::Status
  4645. relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
  4646. Valtype* calculated_value)
  4647. {
  4648. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4649. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4650. Valtype x = gp_offset;
  4651. val = Bits<32>::bit_select32(val, x, 0xffff);
  4652. if (calculate_only)
  4653. *calculated_value = x;
  4654. else
  4655. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4656. return This::STATUS_OKAY;
  4657. }
  4658. // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
  4659. // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
  4660. static inline typename This::Status
  4661. relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4662. const Symbol_value<size>* psymval, Mips_address gp,
  4663. Mips_address addend_a, bool extract_addend, bool local,
  4664. unsigned int r_type, bool calculate_only,
  4665. Valtype* calculated_value)
  4666. {
  4667. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4668. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4669. Valtype addend;
  4670. if (extract_addend)
  4671. {
  4672. if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
  4673. addend = (val & 0x7f) << 2;
  4674. else
  4675. addend = val & 0xffff;
  4676. // Only sign-extend the addend if it was extracted from the
  4677. // instruction. If the addend was separate, leave it alone,
  4678. // otherwise we may lose significant bits.
  4679. addend = Bits<16>::sign_extend32(addend);
  4680. }
  4681. else
  4682. addend = addend_a;
  4683. Valtype x = psymval->value(object, addend) - gp;
  4684. // If the symbol was local, any earlier relocatable links will
  4685. // have adjusted its addend with the gp offset, so compensate
  4686. // for that now. Don't do it for symbols forced local in this
  4687. // link, though, since they won't have had the gp offset applied
  4688. // to them before.
  4689. if (local)
  4690. x += object->gp_value();
  4691. if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
  4692. val = Bits<32>::bit_select32(val, x, 0x7f);
  4693. else
  4694. val = Bits<32>::bit_select32(val, x, 0xffff);
  4695. if (calculate_only)
  4696. {
  4697. *calculated_value = x;
  4698. return This::STATUS_OKAY;
  4699. }
  4700. else
  4701. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4702. if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
  4703. {
  4704. gold_error(_("small-data section exceeds 64KB; lower small-data size "
  4705. "limit (see option -G)"));
  4706. return This::STATUS_OVERFLOW;
  4707. }
  4708. return This::STATUS_OKAY;
  4709. }
  4710. // R_MIPS_GPREL32
  4711. static inline typename This::Status
  4712. relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4713. const Symbol_value<size>* psymval, Mips_address gp,
  4714. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4715. Valtype* calculated_value)
  4716. {
  4717. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4718. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4719. Valtype addend = extract_addend ? val : addend_a;
  4720. // R_MIPS_GPREL32 relocations are defined for local symbols only.
  4721. Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
  4722. if (calculate_only)
  4723. *calculated_value = x;
  4724. else
  4725. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  4726. return This::STATUS_OKAY;
  4727. }
  4728. // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
  4729. // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
  4730. // R_MICROMIPS_TLS_DTPREL_HI16
  4731. static inline typename This::Status
  4732. tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4733. const Symbol_value<size>* psymval, Valtype32 tp_offset,
  4734. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4735. Valtype* calculated_value)
  4736. {
  4737. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4738. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4739. Valtype addend = extract_addend ? val & 0xffff : addend_a;
  4740. // tls symbol values are relative to tls_segment()->vaddr()
  4741. Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
  4742. val = Bits<32>::bit_select32(val, x, 0xffff);
  4743. if (calculate_only)
  4744. *calculated_value = x;
  4745. else
  4746. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4747. return This::STATUS_OKAY;
  4748. }
  4749. // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
  4750. // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
  4751. // R_MICROMIPS_TLS_DTPREL_LO16,
  4752. static inline typename This::Status
  4753. tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4754. const Symbol_value<size>* psymval, Valtype32 tp_offset,
  4755. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4756. Valtype* calculated_value)
  4757. {
  4758. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4759. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4760. Valtype addend = extract_addend ? val & 0xffff : addend_a;
  4761. // tls symbol values are relative to tls_segment()->vaddr()
  4762. Valtype x = psymval->value(object, addend) - tp_offset;
  4763. val = Bits<32>::bit_select32(val, x, 0xffff);
  4764. if (calculate_only)
  4765. *calculated_value = x;
  4766. else
  4767. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4768. return This::STATUS_OKAY;
  4769. }
  4770. // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
  4771. // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
  4772. static inline typename This::Status
  4773. tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4774. const Symbol_value<size>* psymval, Valtype32 tp_offset,
  4775. Mips_address addend_a, bool extract_addend, bool calculate_only,
  4776. Valtype* calculated_value)
  4777. {
  4778. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4779. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4780. Valtype addend = extract_addend ? val : addend_a;
  4781. // tls symbol values are relative to tls_segment()->vaddr()
  4782. Valtype x = psymval->value(object, addend) - tp_offset;
  4783. if (calculate_only)
  4784. *calculated_value = x;
  4785. else
  4786. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  4787. return This::STATUS_OKAY;
  4788. }
  4789. // R_MIPS_SUB, R_MICROMIPS_SUB
  4790. static inline typename This::Status
  4791. relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4792. const Symbol_value<size>* psymval, Mips_address addend_a,
  4793. bool extract_addend, bool calculate_only, Valtype* calculated_value)
  4794. {
  4795. Valtype64* wv = reinterpret_cast<Valtype64*>(view);
  4796. Valtype64 addend = (extract_addend
  4797. ? elfcpp::Swap<64, big_endian>::readval(wv)
  4798. : addend_a);
  4799. Valtype64 x = psymval->value(object, -addend);
  4800. if (calculate_only)
  4801. *calculated_value = x;
  4802. else
  4803. elfcpp::Swap<64, big_endian>::writeval(wv, x);
  4804. return This::STATUS_OKAY;
  4805. }
  4806. // R_MIPS_64: S + A
  4807. static inline typename This::Status
  4808. rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4809. const Symbol_value<size>* psymval, Mips_address addend_a,
  4810. bool extract_addend, bool calculate_only, Valtype* calculated_value,
  4811. bool apply_addend_only)
  4812. {
  4813. Valtype64* wv = reinterpret_cast<Valtype64*>(view);
  4814. Valtype64 addend = (extract_addend
  4815. ? elfcpp::Swap<64, big_endian>::readval(wv)
  4816. : addend_a);
  4817. Valtype64 x = psymval->value(object, addend);
  4818. if (calculate_only)
  4819. *calculated_value = x;
  4820. else
  4821. {
  4822. if (apply_addend_only)
  4823. x = addend;
  4824. elfcpp::Swap<64, big_endian>::writeval(wv, x);
  4825. }
  4826. return This::STATUS_OKAY;
  4827. }
  4828. // R_MIPS_HIGHER, R_MICROMIPS_HIGHER
  4829. static inline typename This::Status
  4830. relhigher(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4831. const Symbol_value<size>* psymval, Mips_address addend_a,
  4832. bool extract_addend, bool calculate_only, Valtype* calculated_value)
  4833. {
  4834. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4835. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4836. Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
  4837. : addend_a);
  4838. Valtype x = psymval->value(object, addend);
  4839. x = ((x + (uint64_t) 0x80008000) >> 32) & 0xffff;
  4840. val = Bits<32>::bit_select32(val, x, 0xffff);
  4841. if (calculate_only)
  4842. *calculated_value = x;
  4843. else
  4844. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4845. return This::STATUS_OKAY;
  4846. }
  4847. // R_MIPS_HIGHEST, R_MICROMIPS_HIGHEST
  4848. static inline typename This::Status
  4849. relhighest(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4850. const Symbol_value<size>* psymval, Mips_address addend_a,
  4851. bool extract_addend, bool calculate_only,
  4852. Valtype* calculated_value)
  4853. {
  4854. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4855. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4856. Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
  4857. : addend_a);
  4858. Valtype x = psymval->value(object, addend);
  4859. x = ((x + (uint64_t) 0x800080008000llu) >> 48) & 0xffff;
  4860. val = Bits<32>::bit_select32(val, x, 0xffff);
  4861. if (calculate_only)
  4862. *calculated_value = x;
  4863. else
  4864. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4865. return This::STATUS_OKAY;
  4866. }
  4867. };
  4868. template<int size, bool big_endian>
  4869. typename std::list<reloc_high<size, big_endian> >
  4870. Mips_relocate_functions<size, big_endian>::hi16_relocs;
  4871. template<int size, bool big_endian>
  4872. typename std::list<reloc_high<size, big_endian> >
  4873. Mips_relocate_functions<size, big_endian>::got16_relocs;
  4874. template<int size, bool big_endian>
  4875. typename std::list<reloc_high<size, big_endian> >
  4876. Mips_relocate_functions<size, big_endian>::pchi16_relocs;
  4877. // Mips_got_info methods.
  4878. // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
  4879. // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
  4880. template<int size, bool big_endian>
  4881. void
  4882. Mips_got_info<size, big_endian>::record_local_got_symbol(
  4883. Mips_relobj<size, big_endian>* object, unsigned int symndx,
  4884. Mips_address addend, unsigned int r_type, unsigned int shndx,
  4885. bool is_section_symbol)
  4886. {
  4887. Mips_got_entry<size, big_endian>* entry =
  4888. new Mips_got_entry<size, big_endian>(object, symndx, addend,
  4889. mips_elf_reloc_tls_type(r_type),
  4890. shndx, is_section_symbol);
  4891. this->record_got_entry(entry, object);
  4892. }
  4893. // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
  4894. // in OBJECT. FOR_CALL is true if the caller is only interested in
  4895. // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
  4896. // relocation.
  4897. template<int size, bool big_endian>
  4898. void
  4899. Mips_got_info<size, big_endian>::record_global_got_symbol(
  4900. Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
  4901. unsigned int r_type, bool dyn_reloc, bool for_call)
  4902. {
  4903. if (!for_call)
  4904. mips_sym->set_got_not_only_for_calls();
  4905. // A global symbol in the GOT must also be in the dynamic symbol table.
  4906. if (!mips_sym->needs_dynsym_entry() && !mips_sym->is_forced_local())
  4907. {
  4908. switch (mips_sym->visibility())
  4909. {
  4910. case elfcpp::STV_INTERNAL:
  4911. case elfcpp::STV_HIDDEN:
  4912. mips_sym->set_is_forced_local();
  4913. break;
  4914. default:
  4915. mips_sym->set_needs_dynsym_entry();
  4916. break;
  4917. }
  4918. }
  4919. unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
  4920. if (tls_type == GOT_TLS_NONE)
  4921. this->global_got_symbols_.insert(mips_sym);
  4922. if (dyn_reloc)
  4923. {
  4924. if (mips_sym->global_got_area() == GGA_NONE)
  4925. mips_sym->set_global_got_area(GGA_RELOC_ONLY);
  4926. return;
  4927. }
  4928. Mips_got_entry<size, big_endian>* entry =
  4929. new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
  4930. this->record_got_entry(entry, object);
  4931. }
  4932. // Add ENTRY to master GOT and to OBJECT's GOT.
  4933. template<int size, bool big_endian>
  4934. void
  4935. Mips_got_info<size, big_endian>::record_got_entry(
  4936. Mips_got_entry<size, big_endian>* entry,
  4937. Mips_relobj<size, big_endian>* object)
  4938. {
  4939. this->got_entries_.insert(entry);
  4940. // Create the GOT entry for the OBJECT's GOT.
  4941. Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
  4942. Mips_got_entry<size, big_endian>* entry2 =
  4943. new Mips_got_entry<size, big_endian>(*entry);
  4944. g->got_entries_.insert(entry2);
  4945. }
  4946. // Record that OBJECT has a page relocation against symbol SYMNDX and
  4947. // that ADDEND is the addend for that relocation.
  4948. // This function creates an upper bound on the number of GOT slots
  4949. // required; no attempt is made to combine references to non-overridable
  4950. // global symbols across multiple input files.
  4951. template<int size, bool big_endian>
  4952. void
  4953. Mips_got_info<size, big_endian>::record_got_page_entry(
  4954. Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
  4955. {
  4956. struct Got_page_range **range_ptr, *range;
  4957. int old_pages, new_pages;
  4958. // Find the Got_page_entry for this symbol.
  4959. Got_page_entry* entry = new Got_page_entry(object, symndx);
  4960. typename Got_page_entry_set::iterator it =
  4961. this->got_page_entries_.find(entry);
  4962. if (it != this->got_page_entries_.end())
  4963. entry = *it;
  4964. else
  4965. this->got_page_entries_.insert(entry);
  4966. // Get the object's GOT, but we don't need to insert an entry here.
  4967. Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
  4968. // Skip over ranges whose maximum extent cannot share a page entry
  4969. // with ADDEND.
  4970. range_ptr = &entry->ranges;
  4971. while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
  4972. range_ptr = &(*range_ptr)->next;
  4973. // If we scanned to the end of the list, or found a range whose
  4974. // minimum extent cannot share a page entry with ADDEND, create
  4975. // a new singleton range.
  4976. range = *range_ptr;
  4977. if (!range || addend < range->min_addend - 0xffff)
  4978. {
  4979. range = new Got_page_range();
  4980. range->next = *range_ptr;
  4981. range->min_addend = addend;
  4982. range->max_addend = addend;
  4983. *range_ptr = range;
  4984. ++this->page_gotno_;
  4985. ++g2->page_gotno_;
  4986. return;
  4987. }
  4988. // Remember how many pages the old range contributed.
  4989. old_pages = range->get_max_pages();
  4990. // Update the ranges.
  4991. if (addend < range->min_addend)
  4992. range->min_addend = addend;
  4993. else if (addend > range->max_addend)
  4994. {
  4995. if (range->next && addend >= range->next->min_addend - 0xffff)
  4996. {
  4997. old_pages += range->next->get_max_pages();
  4998. range->max_addend = range->next->max_addend;
  4999. range->next = range->next->next;
  5000. }
  5001. else
  5002. range->max_addend = addend;
  5003. }
  5004. // Record any change in the total estimate.
  5005. new_pages = range->get_max_pages();
  5006. if (old_pages != new_pages)
  5007. {
  5008. this->page_gotno_ += new_pages - old_pages;
  5009. g2->page_gotno_ += new_pages - old_pages;
  5010. }
  5011. }
  5012. // Create all entries that should be in the local part of the GOT.
  5013. template<int size, bool big_endian>
  5014. void
  5015. Mips_got_info<size, big_endian>::add_local_entries(
  5016. Target_mips<size, big_endian>* target, Layout* layout)
  5017. {
  5018. Mips_output_data_got<size, big_endian>* got = target->got_section();
  5019. // First two GOT entries are reserved. The first entry will be filled at
  5020. // runtime. The second entry will be used by some runtime loaders.
  5021. got->add_constant(0);
  5022. got->add_constant(target->mips_elf_gnu_got1_mask());
  5023. for (typename Got_entry_set::iterator
  5024. p = this->got_entries_.begin();
  5025. p != this->got_entries_.end();
  5026. ++p)
  5027. {
  5028. Mips_got_entry<size, big_endian>* entry = *p;
  5029. if (entry->is_for_local_symbol() && !entry->is_tls_entry())
  5030. {
  5031. got->add_local(entry->object(), entry->symndx(),
  5032. GOT_TYPE_STANDARD, entry->addend());
  5033. unsigned int got_offset = entry->object()->local_got_offset(
  5034. entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
  5035. if (got->multi_got() && this->index_ > 0
  5036. && parameters->options().output_is_position_independent())
  5037. {
  5038. if (!entry->is_section_symbol())
  5039. target->rel_dyn_section(layout)->add_local(entry->object(),
  5040. entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
  5041. else
  5042. target->rel_dyn_section(layout)->add_symbolless_local_addend(
  5043. entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
  5044. got, got_offset);
  5045. }
  5046. }
  5047. }
  5048. this->add_page_entries(target, layout);
  5049. // Add global entries that should be in the local area.
  5050. for (typename Got_entry_set::iterator
  5051. p = this->got_entries_.begin();
  5052. p != this->got_entries_.end();
  5053. ++p)
  5054. {
  5055. Mips_got_entry<size, big_endian>* entry = *p;
  5056. if (!entry->is_for_global_symbol())
  5057. continue;
  5058. Mips_symbol<size>* mips_sym = entry->sym();
  5059. if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
  5060. {
  5061. unsigned int got_type;
  5062. if (!got->multi_got())
  5063. got_type = GOT_TYPE_STANDARD;
  5064. else
  5065. got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
  5066. if (got->add_global(mips_sym, got_type))
  5067. {
  5068. mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
  5069. if (got->multi_got() && this->index_ > 0
  5070. && parameters->options().output_is_position_independent())
  5071. target->rel_dyn_section(layout)->add_symbolless_global_addend(
  5072. mips_sym, elfcpp::R_MIPS_REL32, got,
  5073. mips_sym->got_offset(got_type));
  5074. }
  5075. }
  5076. }
  5077. }
  5078. // Create GOT page entries.
  5079. template<int size, bool big_endian>
  5080. void
  5081. Mips_got_info<size, big_endian>::add_page_entries(
  5082. Target_mips<size, big_endian>* target, Layout* layout)
  5083. {
  5084. if (this->page_gotno_ == 0)
  5085. return;
  5086. Mips_output_data_got<size, big_endian>* got = target->got_section();
  5087. this->got_page_offset_start_ = got->add_constant(0);
  5088. if (got->multi_got() && this->index_ > 0
  5089. && parameters->options().output_is_position_independent())
  5090. target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
  5091. this->got_page_offset_start_);
  5092. int num_entries = this->page_gotno_;
  5093. unsigned int prev_offset = this->got_page_offset_start_;
  5094. while (--num_entries > 0)
  5095. {
  5096. unsigned int next_offset = got->add_constant(0);
  5097. if (got->multi_got() && this->index_ > 0
  5098. && parameters->options().output_is_position_independent())
  5099. target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
  5100. next_offset);
  5101. gold_assert(next_offset == prev_offset + size/8);
  5102. prev_offset = next_offset;
  5103. }
  5104. this->got_page_offset_next_ = this->got_page_offset_start_;
  5105. }
  5106. // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
  5107. template<int size, bool big_endian>
  5108. void
  5109. Mips_got_info<size, big_endian>::add_global_entries(
  5110. Target_mips<size, big_endian>* target, Layout* layout,
  5111. unsigned int non_reloc_only_global_gotno)
  5112. {
  5113. Mips_output_data_got<size, big_endian>* got = target->got_section();
  5114. // Add GGA_NORMAL entries.
  5115. unsigned int count = 0;
  5116. for (typename Got_entry_set::iterator
  5117. p = this->got_entries_.begin();
  5118. p != this->got_entries_.end();
  5119. ++p)
  5120. {
  5121. Mips_got_entry<size, big_endian>* entry = *p;
  5122. if (!entry->is_for_global_symbol())
  5123. continue;
  5124. Mips_symbol<size>* mips_sym = entry->sym();
  5125. if (mips_sym->global_got_area() != GGA_NORMAL)
  5126. continue;
  5127. unsigned int got_type;
  5128. if (!got->multi_got())
  5129. got_type = GOT_TYPE_STANDARD;
  5130. else
  5131. // In multi-GOT links, global symbol can be in both primary and
  5132. // secondary GOT(s). By creating custom GOT type
  5133. // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
  5134. // is added to secondary GOT(s).
  5135. got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
  5136. if (!got->add_global(mips_sym, got_type))
  5137. continue;
  5138. mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
  5139. if (got->multi_got() && this->index_ == 0)
  5140. count++;
  5141. if (got->multi_got() && this->index_ > 0)
  5142. {
  5143. if (parameters->options().output_is_position_independent()
  5144. || (!parameters->doing_static_link()
  5145. && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
  5146. {
  5147. target->rel_dyn_section(layout)->add_global(
  5148. mips_sym, elfcpp::R_MIPS_REL32, got,
  5149. mips_sym->got_offset(got_type));
  5150. got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
  5151. elfcpp::R_MIPS_REL32, mips_sym);
  5152. }
  5153. }
  5154. }
  5155. if (!got->multi_got() || this->index_ == 0)
  5156. {
  5157. if (got->multi_got())
  5158. {
  5159. // We need to allocate space in the primary GOT for GGA_NORMAL entries
  5160. // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
  5161. // entries correspond to dynamic symbol indexes.
  5162. while (count < non_reloc_only_global_gotno)
  5163. {
  5164. got->add_constant(0);
  5165. ++count;
  5166. }
  5167. }
  5168. // Add GGA_RELOC_ONLY entries.
  5169. got->add_reloc_only_entries();
  5170. }
  5171. }
  5172. // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
  5173. template<int size, bool big_endian>
  5174. void
  5175. Mips_got_info<size, big_endian>::add_reloc_only_entries(
  5176. Mips_output_data_got<size, big_endian>* got)
  5177. {
  5178. for (typename Global_got_entry_set::iterator
  5179. p = this->global_got_symbols_.begin();
  5180. p != this->global_got_symbols_.end();
  5181. ++p)
  5182. {
  5183. Mips_symbol<size>* mips_sym = *p;
  5184. if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
  5185. {
  5186. unsigned int got_type;
  5187. if (!got->multi_got())
  5188. got_type = GOT_TYPE_STANDARD;
  5189. else
  5190. got_type = GOT_TYPE_STANDARD_MULTIGOT;
  5191. if (got->add_global(mips_sym, got_type))
  5192. mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
  5193. }
  5194. }
  5195. }
  5196. // Create TLS GOT entries.
  5197. template<int size, bool big_endian>
  5198. void
  5199. Mips_got_info<size, big_endian>::add_tls_entries(
  5200. Target_mips<size, big_endian>* target, Layout* layout)
  5201. {
  5202. Mips_output_data_got<size, big_endian>* got = target->got_section();
  5203. // Add local tls entries.
  5204. for (typename Got_entry_set::iterator
  5205. p = this->got_entries_.begin();
  5206. p != this->got_entries_.end();
  5207. ++p)
  5208. {
  5209. Mips_got_entry<size, big_endian>* entry = *p;
  5210. if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
  5211. continue;
  5212. if (entry->tls_type() == GOT_TLS_GD)
  5213. {
  5214. unsigned int got_type = GOT_TYPE_TLS_PAIR;
  5215. unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
  5216. : elfcpp::R_MIPS_TLS_DTPMOD64);
  5217. unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
  5218. : elfcpp::R_MIPS_TLS_DTPREL64);
  5219. if (!parameters->doing_static_link())
  5220. {
  5221. got->add_local_pair_with_rel(entry->object(), entry->symndx(),
  5222. entry->shndx(), got_type,
  5223. target->rel_dyn_section(layout),
  5224. r_type1, entry->addend());
  5225. unsigned int got_offset =
  5226. entry->object()->local_got_offset(entry->symndx(), got_type,
  5227. entry->addend());
  5228. got->add_static_reloc(got_offset + size/8, r_type2,
  5229. entry->object(), entry->symndx());
  5230. }
  5231. else
  5232. {
  5233. // We are doing a static link. Mark it as belong to module 1,
  5234. // the executable.
  5235. unsigned int got_offset = got->add_constant(1);
  5236. entry->object()->set_local_got_offset(entry->symndx(), got_type,
  5237. got_offset,
  5238. entry->addend());
  5239. got->add_constant(0);
  5240. got->add_static_reloc(got_offset + size/8, r_type2,
  5241. entry->object(), entry->symndx());
  5242. }
  5243. }
  5244. else if (entry->tls_type() == GOT_TLS_IE)
  5245. {
  5246. unsigned int got_type = GOT_TYPE_TLS_OFFSET;
  5247. unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
  5248. : elfcpp::R_MIPS_TLS_TPREL64);
  5249. if (!parameters->doing_static_link())
  5250. got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
  5251. target->rel_dyn_section(layout), r_type,
  5252. entry->addend());
  5253. else
  5254. {
  5255. got->add_local(entry->object(), entry->symndx(), got_type,
  5256. entry->addend());
  5257. unsigned int got_offset =
  5258. entry->object()->local_got_offset(entry->symndx(), got_type,
  5259. entry->addend());
  5260. got->add_static_reloc(got_offset, r_type, entry->object(),
  5261. entry->symndx());
  5262. }
  5263. }
  5264. else if (entry->tls_type() == GOT_TLS_LDM)
  5265. {
  5266. unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
  5267. : elfcpp::R_MIPS_TLS_DTPMOD64);
  5268. unsigned int got_offset;
  5269. if (!parameters->doing_static_link())
  5270. {
  5271. got_offset = got->add_constant(0);
  5272. target->rel_dyn_section(layout)->add_local(
  5273. entry->object(), 0, r_type, got, got_offset);
  5274. }
  5275. else
  5276. // We are doing a static link. Just mark it as belong to module 1,
  5277. // the executable.
  5278. got_offset = got->add_constant(1);
  5279. got->add_constant(0);
  5280. got->set_tls_ldm_offset(got_offset, entry->object());
  5281. }
  5282. else
  5283. gold_unreachable();
  5284. }
  5285. // Add global tls entries.
  5286. for (typename Got_entry_set::iterator
  5287. p = this->got_entries_.begin();
  5288. p != this->got_entries_.end();
  5289. ++p)
  5290. {
  5291. Mips_got_entry<size, big_endian>* entry = *p;
  5292. if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
  5293. continue;
  5294. Mips_symbol<size>* mips_sym = entry->sym();
  5295. if (entry->tls_type() == GOT_TLS_GD)
  5296. {
  5297. unsigned int got_type;
  5298. if (!got->multi_got())
  5299. got_type = GOT_TYPE_TLS_PAIR;
  5300. else
  5301. got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
  5302. unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
  5303. : elfcpp::R_MIPS_TLS_DTPMOD64);
  5304. unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
  5305. : elfcpp::R_MIPS_TLS_DTPREL64);
  5306. if (!parameters->doing_static_link())
  5307. got->add_global_pair_with_rel(mips_sym, got_type,
  5308. target->rel_dyn_section(layout), r_type1, r_type2);
  5309. else
  5310. {
  5311. // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
  5312. // GOT entries. The first one is initialized to be 1, which is the
  5313. // module index for the main executable and the second one 0. A
  5314. // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
  5315. // the second GOT entry and will be applied by gold.
  5316. unsigned int got_offset = got->add_constant(1);
  5317. mips_sym->set_got_offset(got_type, got_offset);
  5318. got->add_constant(0);
  5319. got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
  5320. }
  5321. }
  5322. else if (entry->tls_type() == GOT_TLS_IE)
  5323. {
  5324. unsigned int got_type;
  5325. if (!got->multi_got())
  5326. got_type = GOT_TYPE_TLS_OFFSET;
  5327. else
  5328. got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
  5329. unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
  5330. : elfcpp::R_MIPS_TLS_TPREL64);
  5331. if (!parameters->doing_static_link())
  5332. got->add_global_with_rel(mips_sym, got_type,
  5333. target->rel_dyn_section(layout), r_type);
  5334. else
  5335. {
  5336. got->add_global(mips_sym, got_type);
  5337. unsigned int got_offset = mips_sym->got_offset(got_type);
  5338. got->add_static_reloc(got_offset, r_type, mips_sym);
  5339. }
  5340. }
  5341. else
  5342. gold_unreachable();
  5343. }
  5344. }
  5345. // Decide whether the symbol needs an entry in the global part of the primary
  5346. // GOT, setting global_got_area accordingly. Count the number of global
  5347. // symbols that are in the primary GOT only because they have dynamic
  5348. // relocations R_MIPS_REL32 against them (reloc_only_gotno).
  5349. template<int size, bool big_endian>
  5350. void
  5351. Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
  5352. {
  5353. for (typename Global_got_entry_set::iterator
  5354. p = this->global_got_symbols_.begin();
  5355. p != this->global_got_symbols_.end();
  5356. ++p)
  5357. {
  5358. Mips_symbol<size>* sym = *p;
  5359. // Make a final decision about whether the symbol belongs in the
  5360. // local or global GOT. Symbols that bind locally can (and in the
  5361. // case of forced-local symbols, must) live in the local GOT.
  5362. // Those that are aren't in the dynamic symbol table must also
  5363. // live in the local GOT.
  5364. if (!sym->should_add_dynsym_entry(symtab)
  5365. || (sym->got_only_for_calls()
  5366. ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
  5367. : symbol_references_local(sym,
  5368. sym->should_add_dynsym_entry(symtab))))
  5369. // The symbol belongs in the local GOT. We no longer need this
  5370. // entry if it was only used for relocations; those relocations
  5371. // will be against the null or section symbol instead.
  5372. sym->set_global_got_area(GGA_NONE);
  5373. else if (sym->global_got_area() == GGA_RELOC_ONLY)
  5374. {
  5375. ++this->reloc_only_gotno_;
  5376. ++this->global_gotno_ ;
  5377. }
  5378. }
  5379. }
  5380. // Return the offset of GOT page entry for VALUE. Initialize the entry with
  5381. // VALUE if it is not initialized.
  5382. template<int size, bool big_endian>
  5383. unsigned int
  5384. Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
  5385. Mips_output_data_got<size, big_endian>* got)
  5386. {
  5387. typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
  5388. if (it != this->got_page_offsets_.end())
  5389. return it->second;
  5390. gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
  5391. + (size/8) * this->page_gotno_);
  5392. unsigned int got_offset = this->got_page_offset_next_;
  5393. this->got_page_offsets_[value] = got_offset;
  5394. this->got_page_offset_next_ += size/8;
  5395. got->update_got_entry(got_offset, value);
  5396. return got_offset;
  5397. }
  5398. // Remove lazy-binding stubs for global symbols in this GOT.
  5399. template<int size, bool big_endian>
  5400. void
  5401. Mips_got_info<size, big_endian>::remove_lazy_stubs(
  5402. Target_mips<size, big_endian>* target)
  5403. {
  5404. for (typename Got_entry_set::iterator
  5405. p = this->got_entries_.begin();
  5406. p != this->got_entries_.end();
  5407. ++p)
  5408. {
  5409. Mips_got_entry<size, big_endian>* entry = *p;
  5410. if (entry->is_for_global_symbol())
  5411. target->remove_lazy_stub_entry(entry->sym());
  5412. }
  5413. }
  5414. // Count the number of GOT entries required.
  5415. template<int size, bool big_endian>
  5416. void
  5417. Mips_got_info<size, big_endian>::count_got_entries()
  5418. {
  5419. for (typename Got_entry_set::iterator
  5420. p = this->got_entries_.begin();
  5421. p != this->got_entries_.end();
  5422. ++p)
  5423. {
  5424. this->count_got_entry(*p);
  5425. }
  5426. }
  5427. // Count the number of GOT entries required by ENTRY. Accumulate the result.
  5428. template<int size, bool big_endian>
  5429. void
  5430. Mips_got_info<size, big_endian>::count_got_entry(
  5431. Mips_got_entry<size, big_endian>* entry)
  5432. {
  5433. if (entry->is_tls_entry())
  5434. this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
  5435. else if (entry->is_for_local_symbol()
  5436. || entry->sym()->global_got_area() == GGA_NONE)
  5437. ++this->local_gotno_;
  5438. else
  5439. ++this->global_gotno_;
  5440. }
  5441. // Add FROM's GOT entries.
  5442. template<int size, bool big_endian>
  5443. void
  5444. Mips_got_info<size, big_endian>::add_got_entries(
  5445. Mips_got_info<size, big_endian>* from)
  5446. {
  5447. for (typename Got_entry_set::iterator
  5448. p = from->got_entries_.begin();
  5449. p != from->got_entries_.end();
  5450. ++p)
  5451. {
  5452. Mips_got_entry<size, big_endian>* entry = *p;
  5453. if (this->got_entries_.find(entry) == this->got_entries_.end())
  5454. {
  5455. Mips_got_entry<size, big_endian>* entry2 =
  5456. new Mips_got_entry<size, big_endian>(*entry);
  5457. this->got_entries_.insert(entry2);
  5458. this->count_got_entry(entry);
  5459. }
  5460. }
  5461. }
  5462. // Add FROM's GOT page entries.
  5463. template<int size, bool big_endian>
  5464. void
  5465. Mips_got_info<size, big_endian>::add_got_page_count(
  5466. Mips_got_info<size, big_endian>* from)
  5467. {
  5468. this->page_gotno_ += from->page_gotno_;
  5469. }
  5470. // Mips_output_data_got methods.
  5471. // Lay out the GOT. Add local, global and TLS entries. If GOT is
  5472. // larger than 64K, create multi-GOT.
  5473. template<int size, bool big_endian>
  5474. void
  5475. Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
  5476. Symbol_table* symtab, const Input_objects* input_objects)
  5477. {
  5478. // Decide which symbols need to go in the global part of the GOT and
  5479. // count the number of reloc-only GOT symbols.
  5480. this->master_got_info_->count_got_symbols(symtab);
  5481. // Count the number of GOT entries.
  5482. this->master_got_info_->count_got_entries();
  5483. unsigned int got_size = this->master_got_info_->got_size();
  5484. if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
  5485. this->lay_out_multi_got(layout, input_objects);
  5486. else
  5487. {
  5488. // Record that all objects use single GOT.
  5489. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  5490. p != input_objects->relobj_end();
  5491. ++p)
  5492. {
  5493. Mips_relobj<size, big_endian>* object =
  5494. Mips_relobj<size, big_endian>::as_mips_relobj(*p);
  5495. if (object->get_got_info() != NULL)
  5496. object->set_got_info(this->master_got_info_);
  5497. }
  5498. this->master_got_info_->add_local_entries(this->target_, layout);
  5499. this->master_got_info_->add_global_entries(this->target_, layout,
  5500. /*not used*/-1U);
  5501. this->master_got_info_->add_tls_entries(this->target_, layout);
  5502. }
  5503. }
  5504. // Create multi-GOT. For every GOT, add local, global and TLS entries.
  5505. template<int size, bool big_endian>
  5506. void
  5507. Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
  5508. const Input_objects* input_objects)
  5509. {
  5510. // Try to merge the GOTs of input objects together, as long as they
  5511. // don't seem to exceed the maximum GOT size, choosing one of them
  5512. // to be the primary GOT.
  5513. this->merge_gots(input_objects);
  5514. // Every symbol that is referenced in a dynamic relocation must be
  5515. // present in the primary GOT.
  5516. this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
  5517. // Add GOT entries.
  5518. unsigned int i = 0;
  5519. unsigned int offset = 0;
  5520. Mips_got_info<size, big_endian>* g = this->primary_got_;
  5521. do
  5522. {
  5523. g->set_index(i);
  5524. g->set_offset(offset);
  5525. g->add_local_entries(this->target_, layout);
  5526. if (i == 0)
  5527. g->add_global_entries(this->target_, layout,
  5528. (this->master_got_info_->global_gotno()
  5529. - this->master_got_info_->reloc_only_gotno()));
  5530. else
  5531. g->add_global_entries(this->target_, layout, /*not used*/-1U);
  5532. g->add_tls_entries(this->target_, layout);
  5533. // Forbid global symbols in every non-primary GOT from having
  5534. // lazy-binding stubs.
  5535. if (i > 0)
  5536. g->remove_lazy_stubs(this->target_);
  5537. ++i;
  5538. offset += g->got_size();
  5539. g = g->next();
  5540. }
  5541. while (g);
  5542. }
  5543. // Attempt to merge GOTs of different input objects. Try to use as much as
  5544. // possible of the primary GOT, since it doesn't require explicit dynamic
  5545. // relocations, but don't use objects that would reference global symbols
  5546. // out of the addressable range. Failing the primary GOT, attempt to merge
  5547. // with the current GOT, or finish the current GOT and then make make the new
  5548. // GOT current.
  5549. template<int size, bool big_endian>
  5550. void
  5551. Mips_output_data_got<size, big_endian>::merge_gots(
  5552. const Input_objects* input_objects)
  5553. {
  5554. gold_assert(this->primary_got_ == NULL);
  5555. Mips_got_info<size, big_endian>* current = NULL;
  5556. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  5557. p != input_objects->relobj_end();
  5558. ++p)
  5559. {
  5560. Mips_relobj<size, big_endian>* object =
  5561. Mips_relobj<size, big_endian>::as_mips_relobj(*p);
  5562. Mips_got_info<size, big_endian>* g = object->get_got_info();
  5563. if (g == NULL)
  5564. continue;
  5565. g->count_got_entries();
  5566. // Work out the number of page, local and TLS entries.
  5567. unsigned int estimate = this->master_got_info_->page_gotno();
  5568. if (estimate > g->page_gotno())
  5569. estimate = g->page_gotno();
  5570. estimate += g->local_gotno() + g->tls_gotno();
  5571. // We place TLS GOT entries after both locals and globals. The globals
  5572. // for the primary GOT may overflow the normal GOT size limit, so be
  5573. // sure not to merge a GOT which requires TLS with the primary GOT in that
  5574. // case. This doesn't affect non-primary GOTs.
  5575. estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
  5576. : g->global_gotno());
  5577. unsigned int max_count =
  5578. Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
  5579. if (estimate <= max_count)
  5580. {
  5581. // If we don't have a primary GOT, use it as
  5582. // a starting point for the primary GOT.
  5583. if (!this->primary_got_)
  5584. {
  5585. this->primary_got_ = g;
  5586. continue;
  5587. }
  5588. // Try merging with the primary GOT.
  5589. if (this->merge_got_with(g, object, this->primary_got_))
  5590. continue;
  5591. }
  5592. // If we can merge with the last-created GOT, do it.
  5593. if (current && this->merge_got_with(g, object, current))
  5594. continue;
  5595. // Well, we couldn't merge, so create a new GOT. Don't check if it
  5596. // fits; if it turns out that it doesn't, we'll get relocation
  5597. // overflows anyway.
  5598. g->set_next(current);
  5599. current = g;
  5600. }
  5601. // If we do not find any suitable primary GOT, create an empty one.
  5602. if (this->primary_got_ == NULL)
  5603. this->primary_got_ = new Mips_got_info<size, big_endian>();
  5604. // Link primary GOT with secondary GOTs.
  5605. this->primary_got_->set_next(current);
  5606. }
  5607. // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
  5608. // this would lead to overflow, true if they were merged successfully.
  5609. template<int size, bool big_endian>
  5610. bool
  5611. Mips_output_data_got<size, big_endian>::merge_got_with(
  5612. Mips_got_info<size, big_endian>* from,
  5613. Mips_relobj<size, big_endian>* object,
  5614. Mips_got_info<size, big_endian>* to)
  5615. {
  5616. // Work out how many page entries we would need for the combined GOT.
  5617. unsigned int estimate = this->master_got_info_->page_gotno();
  5618. if (estimate >= from->page_gotno() + to->page_gotno())
  5619. estimate = from->page_gotno() + to->page_gotno();
  5620. // Conservatively estimate how many local and TLS entries would be needed.
  5621. estimate += from->local_gotno() + to->local_gotno();
  5622. estimate += from->tls_gotno() + to->tls_gotno();
  5623. // If we're merging with the primary got, any TLS relocations will
  5624. // come after the full set of global entries. Otherwise estimate those
  5625. // conservatively as well.
  5626. if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
  5627. estimate += this->master_got_info_->global_gotno();
  5628. else
  5629. estimate += from->global_gotno() + to->global_gotno();
  5630. // Bail out if the combined GOT might be too big.
  5631. unsigned int max_count =
  5632. Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
  5633. if (estimate > max_count)
  5634. return false;
  5635. // Transfer the object's GOT information from FROM to TO.
  5636. to->add_got_entries(from);
  5637. to->add_got_page_count(from);
  5638. // Record that OBJECT should use output GOT TO.
  5639. object->set_got_info(to);
  5640. return true;
  5641. }
  5642. // Write out the GOT.
  5643. template<int size, bool big_endian>
  5644. void
  5645. Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
  5646. {
  5647. typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
  5648. Mips_stubs_entry_set;
  5649. // Call parent to write out GOT.
  5650. Output_data_got<size, big_endian>::do_write(of);
  5651. const off_t offset = this->offset();
  5652. const section_size_type oview_size =
  5653. convert_to_section_size_type(this->data_size());
  5654. unsigned char* const oview = of->get_output_view(offset, oview_size);
  5655. // Needed for fixing values of .got section.
  5656. this->got_view_ = oview;
  5657. // Write lazy stub addresses.
  5658. for (typename Mips_stubs_entry_set::iterator
  5659. p = this->master_got_info_->global_got_symbols().begin();
  5660. p != this->master_got_info_->global_got_symbols().end();
  5661. ++p)
  5662. {
  5663. Mips_symbol<size>* mips_sym = *p;
  5664. if (mips_sym->has_lazy_stub())
  5665. {
  5666. Valtype* wv = reinterpret_cast<Valtype*>(
  5667. oview + this->get_primary_got_offset(mips_sym));
  5668. Valtype value =
  5669. this->target_->mips_stubs_section()->stub_address(mips_sym);
  5670. elfcpp::Swap<size, big_endian>::writeval(wv, value);
  5671. }
  5672. }
  5673. // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
  5674. for (typename Mips_stubs_entry_set::iterator
  5675. p = this->master_got_info_->global_got_symbols().begin();
  5676. p != this->master_got_info_->global_got_symbols().end();
  5677. ++p)
  5678. {
  5679. Mips_symbol<size>* mips_sym = *p;
  5680. if (!this->multi_got()
  5681. && (mips_sym->is_mips16() || mips_sym->is_micromips())
  5682. && mips_sym->global_got_area() == GGA_NONE
  5683. && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
  5684. {
  5685. Valtype* wv = reinterpret_cast<Valtype*>(
  5686. oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
  5687. Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
  5688. if (value != 0)
  5689. {
  5690. value |= 1;
  5691. elfcpp::Swap<size, big_endian>::writeval(wv, value);
  5692. }
  5693. }
  5694. }
  5695. if (!this->secondary_got_relocs_.empty())
  5696. {
  5697. // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
  5698. // secondary GOT entries with non-zero initial value copy the value
  5699. // to the corresponding primary GOT entry, and set the secondary GOT
  5700. // entry to zero.
  5701. // TODO(sasa): This is workaround. It needs to be investigated further.
  5702. for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
  5703. {
  5704. Static_reloc& reloc(this->secondary_got_relocs_[i]);
  5705. if (reloc.symbol_is_global())
  5706. {
  5707. Mips_symbol<size>* gsym = reloc.symbol();
  5708. gold_assert(gsym != NULL);
  5709. unsigned got_offset = reloc.got_offset();
  5710. gold_assert(got_offset < oview_size);
  5711. // Find primary GOT entry.
  5712. Valtype* wv_prim = reinterpret_cast<Valtype*>(
  5713. oview + this->get_primary_got_offset(gsym));
  5714. // Find secondary GOT entry.
  5715. Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
  5716. Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
  5717. if (value != 0)
  5718. {
  5719. elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
  5720. elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
  5721. gsym->set_applied_secondary_got_fixup();
  5722. }
  5723. }
  5724. }
  5725. of->write_output_view(offset, oview_size, oview);
  5726. }
  5727. // We are done if there is no fix up.
  5728. if (this->static_relocs_.empty())
  5729. return;
  5730. Output_segment* tls_segment = this->layout_->tls_segment();
  5731. gold_assert(tls_segment != NULL);
  5732. for (size_t i = 0; i < this->static_relocs_.size(); ++i)
  5733. {
  5734. Static_reloc& reloc(this->static_relocs_[i]);
  5735. Mips_address value;
  5736. if (!reloc.symbol_is_global())
  5737. {
  5738. Sized_relobj_file<size, big_endian>* object = reloc.relobj();
  5739. const Symbol_value<size>* psymval =
  5740. object->local_symbol(reloc.index());
  5741. // We are doing static linking. Issue an error and skip this
  5742. // relocation if the symbol is undefined or in a discarded_section.
  5743. bool is_ordinary;
  5744. unsigned int shndx = psymval->input_shndx(&is_ordinary);
  5745. if ((shndx == elfcpp::SHN_UNDEF)
  5746. || (is_ordinary
  5747. && shndx != elfcpp::SHN_UNDEF
  5748. && !object->is_section_included(shndx)
  5749. && !this->symbol_table_->is_section_folded(object, shndx)))
  5750. {
  5751. gold_error(_("undefined or discarded local symbol %u from "
  5752. " object %s in GOT"),
  5753. reloc.index(), reloc.relobj()->name().c_str());
  5754. continue;
  5755. }
  5756. value = psymval->value(object, 0);
  5757. }
  5758. else
  5759. {
  5760. const Mips_symbol<size>* gsym = reloc.symbol();
  5761. gold_assert(gsym != NULL);
  5762. // We are doing static linking. Issue an error and skip this
  5763. // relocation if the symbol is undefined or in a discarded_section
  5764. // unless it is a weakly_undefined symbol.
  5765. if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
  5766. && !gsym->is_weak_undefined())
  5767. {
  5768. gold_error(_("undefined or discarded symbol %s in GOT"),
  5769. gsym->name());
  5770. continue;
  5771. }
  5772. if (!gsym->is_weak_undefined())
  5773. value = gsym->value();
  5774. else
  5775. value = 0;
  5776. }
  5777. unsigned got_offset = reloc.got_offset();
  5778. gold_assert(got_offset < oview_size);
  5779. Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
  5780. Valtype x;
  5781. switch (reloc.r_type())
  5782. {
  5783. case elfcpp::R_MIPS_TLS_DTPMOD32:
  5784. case elfcpp::R_MIPS_TLS_DTPMOD64:
  5785. x = value;
  5786. break;
  5787. case elfcpp::R_MIPS_TLS_DTPREL32:
  5788. case elfcpp::R_MIPS_TLS_DTPREL64:
  5789. x = value - elfcpp::DTP_OFFSET;
  5790. break;
  5791. case elfcpp::R_MIPS_TLS_TPREL32:
  5792. case elfcpp::R_MIPS_TLS_TPREL64:
  5793. x = value - elfcpp::TP_OFFSET;
  5794. break;
  5795. default:
  5796. gold_unreachable();
  5797. break;
  5798. }
  5799. elfcpp::Swap<size, big_endian>::writeval(wv, x);
  5800. }
  5801. of->write_output_view(offset, oview_size, oview);
  5802. }
  5803. // Mips_relobj methods.
  5804. // Count the local symbols. The Mips backend needs to know if a symbol
  5805. // is a MIPS16 or microMIPS function or not. For global symbols, it is easy
  5806. // because the Symbol object keeps the ELF symbol type and st_other field.
  5807. // For local symbol it is harder because we cannot access this information.
  5808. // So we override the do_count_local_symbol in parent and scan local symbols to
  5809. // mark MIPS16 and microMIPS functions. This is not the most efficient way but
  5810. // I do not want to slow down other ports by calling a per symbol target hook
  5811. // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
  5812. template<int size, bool big_endian>
  5813. void
  5814. Mips_relobj<size, big_endian>::do_count_local_symbols(
  5815. Stringpool_template<char>* pool,
  5816. Stringpool_template<char>* dynpool)
  5817. {
  5818. // Ask parent to count the local symbols.
  5819. Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
  5820. const unsigned int loccount = this->local_symbol_count();
  5821. if (loccount == 0)
  5822. return;
  5823. // Initialize the mips16 and micromips function bit-vector.
  5824. this->local_symbol_is_mips16_.resize(loccount, false);
  5825. this->local_symbol_is_micromips_.resize(loccount, false);
  5826. // Read the symbol table section header.
  5827. const unsigned int symtab_shndx = this->symtab_shndx();
  5828. elfcpp::Shdr<size, big_endian>
  5829. symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
  5830. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  5831. // Read the local symbols.
  5832. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  5833. gold_assert(loccount == symtabshdr.get_sh_info());
  5834. off_t locsize = loccount * sym_size;
  5835. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  5836. locsize, true, true);
  5837. // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
  5838. // Skip the first dummy symbol.
  5839. psyms += sym_size;
  5840. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  5841. {
  5842. elfcpp::Sym<size, big_endian> sym(psyms);
  5843. unsigned char st_other = sym.get_st_other();
  5844. this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
  5845. this->local_symbol_is_micromips_[i] =
  5846. elfcpp::elf_st_is_micromips(st_other);
  5847. }
  5848. }
  5849. // Read the symbol information.
  5850. template<int size, bool big_endian>
  5851. void
  5852. Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
  5853. {
  5854. // Call parent class to read symbol information.
  5855. this->base_read_symbols(sd);
  5856. // If this input file is a binary file, it has no processor
  5857. // specific data.
  5858. Input_file::Format format = this->input_file()->format();
  5859. if (format != Input_file::FORMAT_ELF)
  5860. {
  5861. gold_assert(format == Input_file::FORMAT_BINARY);
  5862. this->merge_processor_specific_data_ = false;
  5863. return;
  5864. }
  5865. // Read processor-specific flags in ELF file header.
  5866. const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
  5867. elfcpp::Elf_sizes<size>::ehdr_size,
  5868. true, false);
  5869. elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
  5870. this->processor_specific_flags_ = ehdr.get_e_flags();
  5871. // Get the section names.
  5872. const unsigned char* pnamesu = sd->section_names->data();
  5873. const char* pnames = reinterpret_cast<const char*>(pnamesu);
  5874. // Initialize the mips16 stub section bit-vectors.
  5875. this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
  5876. this->section_is_mips16_call_stub_.resize(this->shnum(), false);
  5877. this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
  5878. const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  5879. const unsigned char* pshdrs = sd->section_headers->data();
  5880. const unsigned char* ps = pshdrs + shdr_size;
  5881. bool must_merge_processor_specific_data = false;
  5882. for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
  5883. {
  5884. elfcpp::Shdr<size, big_endian> shdr(ps);
  5885. // Sometimes an object has no contents except the section name string
  5886. // table and an empty symbol table with the undefined symbol. We
  5887. // don't want to merge processor-specific data from such an object.
  5888. if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
  5889. {
  5890. // Symbol table is not empty.
  5891. const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
  5892. elfcpp::Elf_sizes<size>::sym_size;
  5893. if (shdr.get_sh_size() > sym_size)
  5894. must_merge_processor_specific_data = true;
  5895. }
  5896. else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
  5897. // If this is neither an empty symbol table nor a string table,
  5898. // be conservative.
  5899. must_merge_processor_specific_data = true;
  5900. if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
  5901. {
  5902. this->has_reginfo_section_ = true;
  5903. // Read the gp value that was used to create this object. We need the
  5904. // gp value while processing relocs. The .reginfo section is not used
  5905. // in the 64-bit MIPS ELF ABI.
  5906. section_offset_type section_offset = shdr.get_sh_offset();
  5907. section_size_type section_size =
  5908. convert_to_section_size_type(shdr.get_sh_size());
  5909. const unsigned char* view =
  5910. this->get_view(section_offset, section_size, true, false);
  5911. this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
  5912. // Read the rest of .reginfo.
  5913. this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
  5914. this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
  5915. this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
  5916. this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
  5917. this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
  5918. }
  5919. if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
  5920. {
  5921. gold_assert(this->attributes_section_data_ == NULL);
  5922. section_offset_type section_offset = shdr.get_sh_offset();
  5923. section_size_type section_size =
  5924. convert_to_section_size_type(shdr.get_sh_size());
  5925. const unsigned char* view =
  5926. this->get_view(section_offset, section_size, true, false);
  5927. this->attributes_section_data_ =
  5928. new Attributes_section_data(view, section_size);
  5929. }
  5930. if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
  5931. {
  5932. gold_assert(this->abiflags_ == NULL);
  5933. section_offset_type section_offset = shdr.get_sh_offset();
  5934. section_size_type section_size =
  5935. convert_to_section_size_type(shdr.get_sh_size());
  5936. const unsigned char* view =
  5937. this->get_view(section_offset, section_size, true, false);
  5938. this->abiflags_ = new Mips_abiflags<big_endian>();
  5939. this->abiflags_->version =
  5940. elfcpp::Swap<16, big_endian>::readval(view);
  5941. if (this->abiflags_->version != 0)
  5942. {
  5943. gold_error(_("%s: .MIPS.abiflags section has "
  5944. "unsupported version %u"),
  5945. this->name().c_str(),
  5946. this->abiflags_->version);
  5947. break;
  5948. }
  5949. this->abiflags_->isa_level =
  5950. elfcpp::Swap<8, big_endian>::readval(view + 2);
  5951. this->abiflags_->isa_rev =
  5952. elfcpp::Swap<8, big_endian>::readval(view + 3);
  5953. this->abiflags_->gpr_size =
  5954. elfcpp::Swap<8, big_endian>::readval(view + 4);
  5955. this->abiflags_->cpr1_size =
  5956. elfcpp::Swap<8, big_endian>::readval(view + 5);
  5957. this->abiflags_->cpr2_size =
  5958. elfcpp::Swap<8, big_endian>::readval(view + 6);
  5959. this->abiflags_->fp_abi =
  5960. elfcpp::Swap<8, big_endian>::readval(view + 7);
  5961. this->abiflags_->isa_ext =
  5962. elfcpp::Swap<32, big_endian>::readval(view + 8);
  5963. this->abiflags_->ases =
  5964. elfcpp::Swap<32, big_endian>::readval(view + 12);
  5965. this->abiflags_->flags1 =
  5966. elfcpp::Swap<32, big_endian>::readval(view + 16);
  5967. this->abiflags_->flags2 =
  5968. elfcpp::Swap<32, big_endian>::readval(view + 20);
  5969. }
  5970. // In the 64-bit ABI, .MIPS.options section holds register information.
  5971. // A SHT_MIPS_OPTIONS section contains a series of options, each of which
  5972. // starts with this header:
  5973. //
  5974. // typedef struct
  5975. // {
  5976. // // Type of option.
  5977. // unsigned char kind[1];
  5978. // // Size of option descriptor, including header.
  5979. // unsigned char size[1];
  5980. // // Section index of affected section, or 0 for global option.
  5981. // unsigned char section[2];
  5982. // // Information specific to this kind of option.
  5983. // unsigned char info[4];
  5984. // };
  5985. //
  5986. // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
  5987. // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
  5988. // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
  5989. if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
  5990. {
  5991. section_offset_type section_offset = shdr.get_sh_offset();
  5992. section_size_type section_size =
  5993. convert_to_section_size_type(shdr.get_sh_size());
  5994. const unsigned char* view =
  5995. this->get_view(section_offset, section_size, true, false);
  5996. const unsigned char* end = view + section_size;
  5997. while (view + 8 <= end)
  5998. {
  5999. unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
  6000. unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
  6001. if (sz < 8)
  6002. {
  6003. gold_error(_("%s: Warning: bad `%s' option size %u smaller "
  6004. "than its header"),
  6005. this->name().c_str(),
  6006. this->mips_elf_options_section_name(), sz);
  6007. break;
  6008. }
  6009. if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
  6010. {
  6011. // In the 64 bit ABI, an ODK_REGINFO option is the following
  6012. // structure. The info field of the options header is not
  6013. // used.
  6014. //
  6015. // typedef struct
  6016. // {
  6017. // // Mask of general purpose registers used.
  6018. // unsigned char ri_gprmask[4];
  6019. // // Padding.
  6020. // unsigned char ri_pad[4];
  6021. // // Mask of co-processor registers used.
  6022. // unsigned char ri_cprmask[4][4];
  6023. // // GP register value for this object file.
  6024. // unsigned char ri_gp_value[8];
  6025. // };
  6026. this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
  6027. + 32);
  6028. }
  6029. else if (kind == elfcpp::ODK_REGINFO)
  6030. {
  6031. // In the 32 bit ABI, an ODK_REGINFO option is the following
  6032. // structure. The info field of the options header is not
  6033. // used. The same structure is used in .reginfo section.
  6034. //
  6035. // typedef struct
  6036. // {
  6037. // unsigned char ri_gprmask[4];
  6038. // unsigned char ri_cprmask[4][4];
  6039. // unsigned char ri_gp_value[4];
  6040. // };
  6041. this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
  6042. + 28);
  6043. }
  6044. view += sz;
  6045. }
  6046. }
  6047. const char* name = pnames + shdr.get_sh_name();
  6048. this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
  6049. this->section_is_mips16_call_stub_[i] =
  6050. is_prefix_of(".mips16.call.", name);
  6051. this->section_is_mips16_call_fp_stub_[i] =
  6052. is_prefix_of(".mips16.call.fp.", name);
  6053. if (strcmp(name, ".pdr") == 0)
  6054. {
  6055. gold_assert(this->pdr_shndx_ == -1U);
  6056. this->pdr_shndx_ = i;
  6057. }
  6058. }
  6059. // This is rare.
  6060. if (!must_merge_processor_specific_data)
  6061. this->merge_processor_specific_data_ = false;
  6062. }
  6063. // Discard MIPS16 stub secions that are not needed.
  6064. template<int size, bool big_endian>
  6065. void
  6066. Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
  6067. {
  6068. for (typename Mips16_stubs_int_map::const_iterator
  6069. it = this->mips16_stub_sections_.begin();
  6070. it != this->mips16_stub_sections_.end(); ++it)
  6071. {
  6072. Mips16_stub_section<size, big_endian>* stub_section = it->second;
  6073. if (!stub_section->is_target_found())
  6074. {
  6075. gold_error(_("no relocation found in mips16 stub section '%s'"),
  6076. stub_section->object()
  6077. ->section_name(stub_section->shndx()).c_str());
  6078. }
  6079. bool discard = false;
  6080. if (stub_section->is_for_local_function())
  6081. {
  6082. if (stub_section->is_fn_stub())
  6083. {
  6084. // This stub is for a local symbol. This stub will only
  6085. // be needed if there is some relocation in this object,
  6086. // other than a 16 bit function call, which refers to this
  6087. // symbol.
  6088. if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
  6089. discard = true;
  6090. else
  6091. this->add_local_mips16_fn_stub(stub_section);
  6092. }
  6093. else
  6094. {
  6095. // This stub is for a local symbol. This stub will only
  6096. // be needed if there is some relocation (R_MIPS16_26) in
  6097. // this object that refers to this symbol.
  6098. gold_assert(stub_section->is_call_stub()
  6099. || stub_section->is_call_fp_stub());
  6100. if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
  6101. discard = true;
  6102. else
  6103. this->add_local_mips16_call_stub(stub_section);
  6104. }
  6105. }
  6106. else
  6107. {
  6108. Mips_symbol<size>* gsym = stub_section->gsym();
  6109. if (stub_section->is_fn_stub())
  6110. {
  6111. if (gsym->has_mips16_fn_stub())
  6112. // We already have a stub for this function.
  6113. discard = true;
  6114. else
  6115. {
  6116. gsym->set_mips16_fn_stub(stub_section);
  6117. if (gsym->should_add_dynsym_entry(symtab))
  6118. {
  6119. // If we have a MIPS16 function with a stub, the
  6120. // dynamic symbol must refer to the stub, since only
  6121. // the stub uses the standard calling conventions.
  6122. gsym->set_need_fn_stub();
  6123. if (gsym->is_from_dynobj())
  6124. gsym->set_needs_dynsym_value();
  6125. }
  6126. }
  6127. if (!gsym->need_fn_stub())
  6128. discard = true;
  6129. }
  6130. else if (stub_section->is_call_stub())
  6131. {
  6132. if (gsym->is_mips16())
  6133. // We don't need the call_stub; this is a 16 bit
  6134. // function, so calls from other 16 bit functions are
  6135. // OK.
  6136. discard = true;
  6137. else if (gsym->has_mips16_call_stub())
  6138. // We already have a stub for this function.
  6139. discard = true;
  6140. else
  6141. gsym->set_mips16_call_stub(stub_section);
  6142. }
  6143. else
  6144. {
  6145. gold_assert(stub_section->is_call_fp_stub());
  6146. if (gsym->is_mips16())
  6147. // We don't need the call_stub; this is a 16 bit
  6148. // function, so calls from other 16 bit functions are
  6149. // OK.
  6150. discard = true;
  6151. else if (gsym->has_mips16_call_fp_stub())
  6152. // We already have a stub for this function.
  6153. discard = true;
  6154. else
  6155. gsym->set_mips16_call_fp_stub(stub_section);
  6156. }
  6157. }
  6158. if (discard)
  6159. this->set_output_section(stub_section->shndx(), NULL);
  6160. }
  6161. }
  6162. // Mips_output_data_la25_stub methods.
  6163. // Template for standard LA25 stub.
  6164. template<int size, bool big_endian>
  6165. const uint32_t
  6166. Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
  6167. {
  6168. 0x3c190000, // lui $25,%hi(func)
  6169. 0x08000000, // j func
  6170. 0x27390000, // add $25,$25,%lo(func)
  6171. 0x00000000 // nop
  6172. };
  6173. // Template for microMIPS LA25 stub.
  6174. template<int size, bool big_endian>
  6175. const uint32_t
  6176. Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
  6177. {
  6178. 0x41b9, 0x0000, // lui t9,%hi(func)
  6179. 0xd400, 0x0000, // j func
  6180. 0x3339, 0x0000, // addiu t9,t9,%lo(func)
  6181. 0x0000, 0x0000 // nop
  6182. };
  6183. // Create la25 stub for a symbol.
  6184. template<int size, bool big_endian>
  6185. void
  6186. Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
  6187. Symbol_table* symtab, Target_mips<size, big_endian>* target,
  6188. Mips_symbol<size>* gsym)
  6189. {
  6190. if (!gsym->has_la25_stub())
  6191. {
  6192. gsym->set_la25_stub_offset(this->symbols_.size() * 16);
  6193. this->symbols_.push_back(gsym);
  6194. this->create_stub_symbol(gsym, symtab, target, 16);
  6195. }
  6196. }
  6197. // Create a symbol for SYM stub's value and size, to help make the disassembly
  6198. // easier to read.
  6199. template<int size, bool big_endian>
  6200. void
  6201. Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
  6202. Mips_symbol<size>* sym, Symbol_table* symtab,
  6203. Target_mips<size, big_endian>* target, uint64_t symsize)
  6204. {
  6205. std::string name(".pic.");
  6206. name += sym->name();
  6207. unsigned int offset = sym->la25_stub_offset();
  6208. if (sym->is_micromips())
  6209. offset |= 1;
  6210. // Make it a local function.
  6211. Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
  6212. Symbol_table::PREDEFINED,
  6213. target->la25_stub_section(),
  6214. offset, symsize, elfcpp::STT_FUNC,
  6215. elfcpp::STB_LOCAL,
  6216. elfcpp::STV_DEFAULT, 0,
  6217. false, false);
  6218. new_sym->set_is_forced_local();
  6219. }
  6220. // Write out la25 stubs. This uses the hand-coded instructions above,
  6221. // and adjusts them as needed.
  6222. template<int size, bool big_endian>
  6223. void
  6224. Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
  6225. {
  6226. const off_t offset = this->offset();
  6227. const section_size_type oview_size =
  6228. convert_to_section_size_type(this->data_size());
  6229. unsigned char* const oview = of->get_output_view(offset, oview_size);
  6230. for (typename std::vector<Mips_symbol<size>*>::iterator
  6231. p = this->symbols_.begin();
  6232. p != this->symbols_.end();
  6233. ++p)
  6234. {
  6235. Mips_symbol<size>* sym = *p;
  6236. unsigned char* pov = oview + sym->la25_stub_offset();
  6237. Mips_address target = sym->value();
  6238. if (!sym->is_micromips())
  6239. {
  6240. elfcpp::Swap<32, big_endian>::writeval(pov,
  6241. la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
  6242. elfcpp::Swap<32, big_endian>::writeval(pov + 4,
  6243. la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
  6244. elfcpp::Swap<32, big_endian>::writeval(pov + 8,
  6245. la25_stub_entry[2] | (target & 0xffff));
  6246. elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
  6247. }
  6248. else
  6249. {
  6250. target |= 1;
  6251. // First stub instruction. Paste high 16-bits of the target.
  6252. elfcpp::Swap<16, big_endian>::writeval(pov,
  6253. la25_stub_micromips_entry[0]);
  6254. elfcpp::Swap<16, big_endian>::writeval(pov + 2,
  6255. ((target + 0x8000) >> 16) & 0xffff);
  6256. // Second stub instruction. Paste low 26-bits of the target, shifted
  6257. // right by 1.
  6258. elfcpp::Swap<16, big_endian>::writeval(pov + 4,
  6259. la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
  6260. elfcpp::Swap<16, big_endian>::writeval(pov + 6,
  6261. la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
  6262. // Third stub instruction. Paste low 16-bits of the target.
  6263. elfcpp::Swap<16, big_endian>::writeval(pov + 8,
  6264. la25_stub_micromips_entry[4]);
  6265. elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
  6266. // Fourth stub instruction.
  6267. elfcpp::Swap<16, big_endian>::writeval(pov + 12,
  6268. la25_stub_micromips_entry[6]);
  6269. elfcpp::Swap<16, big_endian>::writeval(pov + 14,
  6270. la25_stub_micromips_entry[7]);
  6271. }
  6272. }
  6273. of->write_output_view(offset, oview_size, oview);
  6274. }
  6275. // Mips_output_data_plt methods.
  6276. // The format of the first PLT entry in an O32 executable.
  6277. template<int size, bool big_endian>
  6278. const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
  6279. {
  6280. 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
  6281. 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
  6282. 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
  6283. 0x031cc023, // subu $24, $24, $28
  6284. 0x03e07825, // or $15, $31, zero
  6285. 0x0018c082, // srl $24, $24, 2
  6286. 0x0320f809, // jalr $25
  6287. 0x2718fffe // subu $24, $24, 2
  6288. };
  6289. // The format of the first PLT entry in an N32 executable. Different
  6290. // because gp ($28) is not available; we use t2 ($14) instead.
  6291. template<int size, bool big_endian>
  6292. const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
  6293. {
  6294. 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
  6295. 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
  6296. 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
  6297. 0x030ec023, // subu $24, $24, $14
  6298. 0x03e07825, // or $15, $31, zero
  6299. 0x0018c082, // srl $24, $24, 2
  6300. 0x0320f809, // jalr $25
  6301. 0x2718fffe // subu $24, $24, 2
  6302. };
  6303. // The format of the first PLT entry in an N64 executable. Different
  6304. // from N32 because of the increased size of GOT entries.
  6305. template<int size, bool big_endian>
  6306. const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
  6307. {
  6308. 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
  6309. 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
  6310. 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
  6311. 0x030ec023, // subu $24, $24, $14
  6312. 0x03e07825, // or $15, $31, zero
  6313. 0x0018c0c2, // srl $24, $24, 3
  6314. 0x0320f809, // jalr $25
  6315. 0x2718fffe // subu $24, $24, 2
  6316. };
  6317. // The format of the microMIPS first PLT entry in an O32 executable.
  6318. // We rely on v0 ($2) rather than t8 ($24) to contain the address
  6319. // of the GOTPLT entry handled, so this stub may only be used when
  6320. // all the subsequent PLT entries are microMIPS code too.
  6321. //
  6322. // The trailing NOP is for alignment and correct disassembly only.
  6323. template<int size, bool big_endian>
  6324. const uint32_t Mips_output_data_plt<size, big_endian>::
  6325. plt0_entry_micromips_o32[] =
  6326. {
  6327. 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
  6328. 0xff23, 0x0000, // lw $25, 0($3)
  6329. 0x0535, // subu $2, $2, $3
  6330. 0x2525, // srl $2, $2, 2
  6331. 0x3302, 0xfffe, // subu $24, $2, 2
  6332. 0x0dff, // move $15, $31
  6333. 0x45f9, // jalrs $25
  6334. 0x0f83, // move $28, $3
  6335. 0x0c00 // nop
  6336. };
  6337. // The format of the microMIPS first PLT entry in an O32 executable
  6338. // in the insn32 mode.
  6339. template<int size, bool big_endian>
  6340. const uint32_t Mips_output_data_plt<size, big_endian>::
  6341. plt0_entry_micromips32_o32[] =
  6342. {
  6343. 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
  6344. 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
  6345. 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
  6346. 0x0398, 0xc1d0, // subu $24, $24, $28
  6347. 0x001f, 0x7a90, // or $15, $31, zero
  6348. 0x0318, 0x1040, // srl $24, $24, 2
  6349. 0x03f9, 0x0f3c, // jalr $25
  6350. 0x3318, 0xfffe // subu $24, $24, 2
  6351. };
  6352. // The format of subsequent standard entries in the PLT.
  6353. template<int size, bool big_endian>
  6354. const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
  6355. {
  6356. 0x3c0f0000, // lui $15, %hi(.got.plt entry)
  6357. 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
  6358. 0x03200008, // jr $25
  6359. 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
  6360. };
  6361. // The format of subsequent R6 PLT entries.
  6362. template<int size, bool big_endian>
  6363. const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
  6364. {
  6365. 0x3c0f0000, // lui $15, %hi(.got.plt entry)
  6366. 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
  6367. 0x03200009, // jr $25
  6368. 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
  6369. };
  6370. // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
  6371. // temporary because t8 ($24) and t9 ($25) are not directly addressable.
  6372. // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
  6373. // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
  6374. // target function address in register v0.
  6375. template<int size, bool big_endian>
  6376. const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
  6377. {
  6378. 0xb303, // lw $3, 12($pc)
  6379. 0x651b, // move $24, $3
  6380. 0x9b60, // lw $3, 0($3)
  6381. 0xeb00, // jr $3
  6382. 0x653b, // move $25, $3
  6383. 0x6500, // nop
  6384. 0x0000, 0x0000 // .word (.got.plt entry)
  6385. };
  6386. // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
  6387. // as a temporary because t8 ($24) is not addressable with ADDIUPC.
  6388. template<int size, bool big_endian>
  6389. const uint32_t Mips_output_data_plt<size, big_endian>::
  6390. plt_entry_micromips_o32[] =
  6391. {
  6392. 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
  6393. 0xff22, 0x0000, // lw $25, 0($2)
  6394. 0x4599, // jr $25
  6395. 0x0f02 // move $24, $2
  6396. };
  6397. // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
  6398. template<int size, bool big_endian>
  6399. const uint32_t Mips_output_data_plt<size, big_endian>::
  6400. plt_entry_micromips32_o32[] =
  6401. {
  6402. 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
  6403. 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
  6404. 0x0019, 0x0f3c, // jr $25
  6405. 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
  6406. };
  6407. // Add an entry to the PLT for a symbol referenced by r_type relocation.
  6408. template<int size, bool big_endian>
  6409. void
  6410. Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
  6411. unsigned int r_type)
  6412. {
  6413. gold_assert(!gsym->has_plt_offset());
  6414. // Final PLT offset for a symbol will be set in method set_plt_offsets().
  6415. gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
  6416. + sizeof(plt0_entry_o32));
  6417. this->symbols_.push_back(gsym);
  6418. // Record whether the relocation requires a standard MIPS
  6419. // or a compressed code entry.
  6420. if (jal_reloc(r_type))
  6421. {
  6422. if (r_type == elfcpp::R_MIPS_26)
  6423. gsym->set_needs_mips_plt(true);
  6424. else
  6425. gsym->set_needs_comp_plt(true);
  6426. }
  6427. section_offset_type got_offset = this->got_plt_->current_data_size();
  6428. // Every PLT entry needs a GOT entry which points back to the PLT
  6429. // entry (this will be changed by the dynamic linker, normally
  6430. // lazily when the function is called).
  6431. this->got_plt_->set_current_data_size(got_offset + size/8);
  6432. gsym->set_needs_dynsym_entry();
  6433. this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
  6434. got_offset);
  6435. }
  6436. // Set final PLT offsets. For each symbol, determine whether standard or
  6437. // compressed (MIPS16 or microMIPS) PLT entry is used.
  6438. template<int size, bool big_endian>
  6439. void
  6440. Mips_output_data_plt<size, big_endian>::set_plt_offsets()
  6441. {
  6442. // The sizes of individual PLT entries.
  6443. unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
  6444. unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
  6445. ? this->compressed_plt_entry_size() : 0);
  6446. for (typename std::vector<Mips_symbol<size>*>::const_iterator
  6447. p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
  6448. {
  6449. Mips_symbol<size>* mips_sym = *p;
  6450. // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
  6451. // so always use a standard entry there.
  6452. //
  6453. // If the symbol has a MIPS16 call stub and gets a PLT entry, then
  6454. // all MIPS16 calls will go via that stub, and there is no benefit
  6455. // to having a MIPS16 entry. And in the case of call_stub a
  6456. // standard entry actually has to be used as the stub ends with a J
  6457. // instruction.
  6458. if (this->target_->is_output_newabi()
  6459. || mips_sym->has_mips16_call_stub()
  6460. || mips_sym->has_mips16_call_fp_stub())
  6461. {
  6462. mips_sym->set_needs_mips_plt(true);
  6463. mips_sym->set_needs_comp_plt(false);
  6464. }
  6465. // Otherwise, if there are no direct calls to the function, we
  6466. // have a free choice of whether to use standard or compressed
  6467. // entries. Prefer microMIPS entries if the object is known to
  6468. // contain microMIPS code, so that it becomes possible to create
  6469. // pure microMIPS binaries. Prefer standard entries otherwise,
  6470. // because MIPS16 ones are no smaller and are usually slower.
  6471. if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
  6472. {
  6473. if (this->target_->is_output_micromips())
  6474. mips_sym->set_needs_comp_plt(true);
  6475. else
  6476. mips_sym->set_needs_mips_plt(true);
  6477. }
  6478. if (mips_sym->needs_mips_plt())
  6479. {
  6480. mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
  6481. this->plt_mips_offset_ += plt_mips_entry_size;
  6482. }
  6483. if (mips_sym->needs_comp_plt())
  6484. {
  6485. mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
  6486. this->plt_comp_offset_ += plt_comp_entry_size;
  6487. }
  6488. }
  6489. // Figure out the size of the PLT header if we know that we are using it.
  6490. if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
  6491. this->plt_header_size_ = this->get_plt_header_size();
  6492. }
  6493. // Write out the PLT. This uses the hand-coded instructions above,
  6494. // and adjusts them as needed.
  6495. template<int size, bool big_endian>
  6496. void
  6497. Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
  6498. {
  6499. const off_t offset = this->offset();
  6500. const section_size_type oview_size =
  6501. convert_to_section_size_type(this->data_size());
  6502. unsigned char* const oview = of->get_output_view(offset, oview_size);
  6503. const off_t gotplt_file_offset = this->got_plt_->offset();
  6504. const section_size_type gotplt_size =
  6505. convert_to_section_size_type(this->got_plt_->data_size());
  6506. unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
  6507. gotplt_size);
  6508. unsigned char* pov = oview;
  6509. Mips_address plt_address = this->address();
  6510. // Calculate the address of .got.plt.
  6511. Mips_address gotplt_addr = this->got_plt_->address();
  6512. Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
  6513. Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
  6514. // The PLT sequence is not safe for N64 if .got.plt's address can
  6515. // not be loaded in two instructions.
  6516. gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
  6517. || ~(gotplt_addr | 0x7fffffff) == 0);
  6518. // Write the PLT header.
  6519. const uint32_t* plt0_entry = this->get_plt_header_entry();
  6520. if (plt0_entry == plt0_entry_micromips_o32)
  6521. {
  6522. // Write microMIPS PLT header.
  6523. gold_assert(gotplt_addr % 4 == 0);
  6524. Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
  6525. // ADDIUPC has a span of +/-16MB, check we're in range.
  6526. if (gotpc_offset + 0x1000000 >= 0x2000000)
  6527. {
  6528. gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
  6529. "ADDIUPC"), (long)gotpc_offset);
  6530. return;
  6531. }
  6532. elfcpp::Swap<16, big_endian>::writeval(pov,
  6533. plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
  6534. elfcpp::Swap<16, big_endian>::writeval(pov + 2,
  6535. (gotpc_offset >> 2) & 0xffff);
  6536. pov += 4;
  6537. for (unsigned int i = 2;
  6538. i < (sizeof(plt0_entry_micromips_o32)
  6539. / sizeof(plt0_entry_micromips_o32[0]));
  6540. i++)
  6541. {
  6542. elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
  6543. pov += 2;
  6544. }
  6545. }
  6546. else if (plt0_entry == plt0_entry_micromips32_o32)
  6547. {
  6548. // Write microMIPS PLT header in insn32 mode.
  6549. elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
  6550. elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
  6551. elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
  6552. elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
  6553. elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
  6554. elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
  6555. pov += 12;
  6556. for (unsigned int i = 6;
  6557. i < (sizeof(plt0_entry_micromips32_o32)
  6558. / sizeof(plt0_entry_micromips32_o32[0]));
  6559. i++)
  6560. {
  6561. elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
  6562. pov += 2;
  6563. }
  6564. }
  6565. else
  6566. {
  6567. // Write standard PLT header.
  6568. elfcpp::Swap<32, big_endian>::writeval(pov,
  6569. plt0_entry[0] | gotplt_addr_high);
  6570. elfcpp::Swap<32, big_endian>::writeval(pov + 4,
  6571. plt0_entry[1] | gotplt_addr_low);
  6572. elfcpp::Swap<32, big_endian>::writeval(pov + 8,
  6573. plt0_entry[2] | gotplt_addr_low);
  6574. pov += 12;
  6575. for (int i = 3; i < 8; i++)
  6576. {
  6577. elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
  6578. pov += 4;
  6579. }
  6580. }
  6581. unsigned char* gotplt_pov = gotplt_view;
  6582. unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
  6583. // The first two entries in .got.plt are reserved.
  6584. elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
  6585. elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
  6586. unsigned int gotplt_offset = 2 * got_entry_size;
  6587. gotplt_pov += 2 * got_entry_size;
  6588. // Calculate the address of the PLT header.
  6589. Mips_address header_address = (plt_address
  6590. + (this->is_plt_header_compressed() ? 1 : 0));
  6591. // Initialize compressed PLT area view.
  6592. unsigned char* pov2 = pov + this->plt_mips_offset_;
  6593. // Write the PLT entries.
  6594. for (typename std::vector<Mips_symbol<size>*>::const_iterator
  6595. p = this->symbols_.begin();
  6596. p != this->symbols_.end();
  6597. ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
  6598. {
  6599. Mips_symbol<size>* mips_sym = *p;
  6600. // Calculate the address of the .got.plt entry.
  6601. uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
  6602. uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
  6603. & 0xffff);
  6604. uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
  6605. // Initially point the .got.plt entry at the PLT header.
  6606. if (this->target_->is_output_n64())
  6607. elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
  6608. else
  6609. elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
  6610. // Now handle the PLT itself. First the standard entry.
  6611. if (mips_sym->has_mips_plt_offset())
  6612. {
  6613. // Pick the load opcode (LW or LD).
  6614. uint64_t load = this->target_->is_output_n64() ? 0xdc000000
  6615. : 0x8c000000;
  6616. const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
  6617. : plt_entry;
  6618. // Fill in the PLT entry itself.
  6619. elfcpp::Swap<32, big_endian>::writeval(pov,
  6620. entry[0] | gotplt_entry_addr_hi);
  6621. elfcpp::Swap<32, big_endian>::writeval(pov + 4,
  6622. entry[1] | gotplt_entry_addr_lo | load);
  6623. elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
  6624. elfcpp::Swap<32, big_endian>::writeval(pov + 12,
  6625. entry[3] | gotplt_entry_addr_lo);
  6626. pov += 16;
  6627. }
  6628. // Now the compressed entry. They come after any standard ones.
  6629. if (mips_sym->has_comp_plt_offset())
  6630. {
  6631. if (!this->target_->is_output_micromips())
  6632. {
  6633. // Write MIPS16 PLT entry.
  6634. const uint32_t* plt_entry = plt_entry_mips16_o32;
  6635. elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
  6636. elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
  6637. elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
  6638. elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
  6639. elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
  6640. elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
  6641. elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
  6642. gotplt_entry_addr);
  6643. pov2 += 16;
  6644. }
  6645. else if (this->target_->use_32bit_micromips_instructions())
  6646. {
  6647. // Write microMIPS PLT entry in insn32 mode.
  6648. const uint32_t* plt_entry = plt_entry_micromips32_o32;
  6649. elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
  6650. elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
  6651. gotplt_entry_addr_hi);
  6652. elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
  6653. elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
  6654. gotplt_entry_addr_lo);
  6655. elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
  6656. elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
  6657. elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
  6658. elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
  6659. gotplt_entry_addr_lo);
  6660. pov2 += 16;
  6661. }
  6662. else
  6663. {
  6664. // Write microMIPS PLT entry.
  6665. const uint32_t* plt_entry = plt_entry_micromips_o32;
  6666. gold_assert(gotplt_entry_addr % 4 == 0);
  6667. Mips_address loc_address = plt_address + pov2 - oview;
  6668. int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
  6669. // ADDIUPC has a span of +/-16MB, check we're in range.
  6670. if (gotpc_offset + 0x1000000 >= 0x2000000)
  6671. {
  6672. gold_error(_(".got.plt offset of %ld from .plt beyond the "
  6673. "range of ADDIUPC"), (long)gotpc_offset);
  6674. return;
  6675. }
  6676. elfcpp::Swap<16, big_endian>::writeval(pov2,
  6677. plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
  6678. elfcpp::Swap<16, big_endian>::writeval(
  6679. pov2 + 2, (gotpc_offset >> 2) & 0xffff);
  6680. elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
  6681. elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
  6682. elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
  6683. elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
  6684. pov2 += 12;
  6685. }
  6686. }
  6687. }
  6688. // Check the number of bytes written for standard entries.
  6689. gold_assert(static_cast<section_size_type>(
  6690. pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
  6691. // Check the number of bytes written for compressed entries.
  6692. gold_assert((static_cast<section_size_type>(pov2 - pov)
  6693. == this->plt_comp_offset_));
  6694. // Check the total number of bytes written.
  6695. gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
  6696. gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
  6697. == gotplt_size);
  6698. of->write_output_view(offset, oview_size, oview);
  6699. of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
  6700. }
  6701. // Mips_output_data_mips_stubs methods.
  6702. // The format of the lazy binding stub when dynamic symbol count is less than
  6703. // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
  6704. template<int size, bool big_endian>
  6705. const uint32_t
  6706. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
  6707. {
  6708. 0x8f998010, // lw t9,0x8010(gp)
  6709. 0x03e07825, // or t7,ra,zero
  6710. 0x0320f809, // jalr t9,ra
  6711. 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
  6712. };
  6713. // The format of the lazy binding stub when dynamic symbol count is less than
  6714. // 64K, dynamic symbol index is less than 32K, and ABI is N64.
  6715. template<int size, bool big_endian>
  6716. const uint32_t
  6717. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
  6718. {
  6719. 0xdf998010, // ld t9,0x8010(gp)
  6720. 0x03e07825, // or t7,ra,zero
  6721. 0x0320f809, // jalr t9,ra
  6722. 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
  6723. };
  6724. // The format of the lazy binding stub when dynamic symbol count is less than
  6725. // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
  6726. template<int size, bool big_endian>
  6727. const uint32_t
  6728. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
  6729. {
  6730. 0x8f998010, // lw t9,0x8010(gp)
  6731. 0x03e07825, // or t7,ra,zero
  6732. 0x0320f809, // jalr t9,ra
  6733. 0x34180000 // ori t8,zero,DYN_INDEX unsigned
  6734. };
  6735. // The format of the lazy binding stub when dynamic symbol count is less than
  6736. // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
  6737. template<int size, bool big_endian>
  6738. const uint32_t
  6739. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
  6740. {
  6741. 0xdf998010, // ld t9,0x8010(gp)
  6742. 0x03e07825, // or t7,ra,zero
  6743. 0x0320f809, // jalr t9,ra
  6744. 0x34180000 // ori t8,zero,DYN_INDEX unsigned
  6745. };
  6746. // The format of the lazy binding stub when dynamic symbol count is greater than
  6747. // 64K, and ABI is not N64.
  6748. template<int size, bool big_endian>
  6749. const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
  6750. {
  6751. 0x8f998010, // lw t9,0x8010(gp)
  6752. 0x03e07825, // or t7,ra,zero
  6753. 0x3c180000, // lui t8,DYN_INDEX
  6754. 0x0320f809, // jalr t9,ra
  6755. 0x37180000 // ori t8,t8,DYN_INDEX
  6756. };
  6757. // The format of the lazy binding stub when dynamic symbol count is greater than
  6758. // 64K, and ABI is N64.
  6759. template<int size, bool big_endian>
  6760. const uint32_t
  6761. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
  6762. {
  6763. 0xdf998010, // ld t9,0x8010(gp)
  6764. 0x03e07825, // or t7,ra,zero
  6765. 0x3c180000, // lui t8,DYN_INDEX
  6766. 0x0320f809, // jalr t9,ra
  6767. 0x37180000 // ori t8,t8,DYN_INDEX
  6768. };
  6769. // microMIPS stubs.
  6770. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  6771. // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
  6772. template<int size, bool big_endian>
  6773. const uint32_t
  6774. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
  6775. {
  6776. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  6777. 0x0dff, // move t7,ra
  6778. 0x45d9, // jalr t9
  6779. 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
  6780. };
  6781. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  6782. // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
  6783. template<int size, bool big_endian>
  6784. const uint32_t
  6785. Mips_output_data_mips_stubs<size, big_endian>::
  6786. lazy_stub_micromips_normal_1_n64[] =
  6787. {
  6788. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  6789. 0x0dff, // move t7,ra
  6790. 0x45d9, // jalr t9
  6791. 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
  6792. };
  6793. // The format of the microMIPS lazy binding stub when dynamic symbol
  6794. // count is less than 64K, dynamic symbol index is between 32K and 64K,
  6795. // and ABI is not N64.
  6796. template<int size, bool big_endian>
  6797. const uint32_t
  6798. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
  6799. {
  6800. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  6801. 0x0dff, // move t7,ra
  6802. 0x45d9, // jalr t9
  6803. 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
  6804. };
  6805. // The format of the microMIPS lazy binding stub when dynamic symbol
  6806. // count is less than 64K, dynamic symbol index is between 32K and 64K,
  6807. // and ABI is N64.
  6808. template<int size, bool big_endian>
  6809. const uint32_t
  6810. Mips_output_data_mips_stubs<size, big_endian>::
  6811. lazy_stub_micromips_normal_2_n64[] =
  6812. {
  6813. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  6814. 0x0dff, // move t7,ra
  6815. 0x45d9, // jalr t9
  6816. 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
  6817. };
  6818. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  6819. // greater than 64K, and ABI is not N64.
  6820. template<int size, bool big_endian>
  6821. const uint32_t
  6822. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
  6823. {
  6824. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  6825. 0x0dff, // move t7,ra
  6826. 0x41b8, 0x0000, // lui t8,DYN_INDEX
  6827. 0x45d9, // jalr t9
  6828. 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
  6829. };
  6830. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  6831. // greater than 64K, and ABI is N64.
  6832. template<int size, bool big_endian>
  6833. const uint32_t
  6834. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
  6835. {
  6836. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  6837. 0x0dff, // move t7,ra
  6838. 0x41b8, 0x0000, // lui t8,DYN_INDEX
  6839. 0x45d9, // jalr t9
  6840. 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
  6841. };
  6842. // 32-bit microMIPS stubs.
  6843. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  6844. // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
  6845. // can use only 32-bit instructions.
  6846. template<int size, bool big_endian>
  6847. const uint32_t
  6848. Mips_output_data_mips_stubs<size, big_endian>::
  6849. lazy_stub_micromips32_normal_1[] =
  6850. {
  6851. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  6852. 0x001f, 0x7a90, // or t7,ra,zero
  6853. 0x03f9, 0x0f3c, // jalr ra,t9
  6854. 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
  6855. };
  6856. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  6857. // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
  6858. // use only 32-bit instructions.
  6859. template<int size, bool big_endian>
  6860. const uint32_t
  6861. Mips_output_data_mips_stubs<size, big_endian>::
  6862. lazy_stub_micromips32_normal_1_n64[] =
  6863. {
  6864. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  6865. 0x001f, 0x7a90, // or t7,ra,zero
  6866. 0x03f9, 0x0f3c, // jalr ra,t9
  6867. 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
  6868. };
  6869. // The format of the microMIPS lazy binding stub when dynamic symbol
  6870. // count is less than 64K, dynamic symbol index is between 32K and 64K,
  6871. // ABI is not N64, and we can use only 32-bit instructions.
  6872. template<int size, bool big_endian>
  6873. const uint32_t
  6874. Mips_output_data_mips_stubs<size, big_endian>::
  6875. lazy_stub_micromips32_normal_2[] =
  6876. {
  6877. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  6878. 0x001f, 0x7a90, // or t7,ra,zero
  6879. 0x03f9, 0x0f3c, // jalr ra,t9
  6880. 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
  6881. };
  6882. // The format of the microMIPS lazy binding stub when dynamic symbol
  6883. // count is less than 64K, dynamic symbol index is between 32K and 64K,
  6884. // ABI is N64, and we can use only 32-bit instructions.
  6885. template<int size, bool big_endian>
  6886. const uint32_t
  6887. Mips_output_data_mips_stubs<size, big_endian>::
  6888. lazy_stub_micromips32_normal_2_n64[] =
  6889. {
  6890. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  6891. 0x001f, 0x7a90, // or t7,ra,zero
  6892. 0x03f9, 0x0f3c, // jalr ra,t9
  6893. 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
  6894. };
  6895. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  6896. // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
  6897. template<int size, bool big_endian>
  6898. const uint32_t
  6899. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
  6900. {
  6901. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  6902. 0x001f, 0x7a90, // or t7,ra,zero
  6903. 0x41b8, 0x0000, // lui t8,DYN_INDEX
  6904. 0x03f9, 0x0f3c, // jalr ra,t9
  6905. 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
  6906. };
  6907. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  6908. // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
  6909. template<int size, bool big_endian>
  6910. const uint32_t
  6911. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
  6912. {
  6913. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  6914. 0x001f, 0x7a90, // or t7,ra,zero
  6915. 0x41b8, 0x0000, // lui t8,DYN_INDEX
  6916. 0x03f9, 0x0f3c, // jalr ra,t9
  6917. 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
  6918. };
  6919. // Create entry for a symbol.
  6920. template<int size, bool big_endian>
  6921. void
  6922. Mips_output_data_mips_stubs<size, big_endian>::make_entry(
  6923. Mips_symbol<size>* gsym)
  6924. {
  6925. if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
  6926. {
  6927. this->symbols_.insert(gsym);
  6928. gsym->set_has_lazy_stub(true);
  6929. }
  6930. }
  6931. // Remove entry for a symbol.
  6932. template<int size, bool big_endian>
  6933. void
  6934. Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
  6935. Mips_symbol<size>* gsym)
  6936. {
  6937. if (gsym->has_lazy_stub())
  6938. {
  6939. this->symbols_.erase(gsym);
  6940. gsym->set_has_lazy_stub(false);
  6941. }
  6942. }
  6943. // Set stub offsets for symbols. This method expects that the number of
  6944. // entries in dynamic symbol table is set.
  6945. template<int size, bool big_endian>
  6946. void
  6947. Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
  6948. {
  6949. gold_assert(this->dynsym_count_ != -1U);
  6950. if (this->stub_offsets_are_set_)
  6951. return;
  6952. unsigned int stub_size = this->stub_size();
  6953. unsigned int offset = 0;
  6954. for (typename Mips_stubs_entry_set::const_iterator
  6955. p = this->symbols_.begin();
  6956. p != this->symbols_.end();
  6957. ++p, offset += stub_size)
  6958. {
  6959. Mips_symbol<size>* mips_sym = *p;
  6960. mips_sym->set_lazy_stub_offset(offset);
  6961. }
  6962. this->stub_offsets_are_set_ = true;
  6963. }
  6964. template<int size, bool big_endian>
  6965. void
  6966. Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
  6967. {
  6968. for (typename Mips_stubs_entry_set::const_iterator
  6969. p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
  6970. {
  6971. Mips_symbol<size>* sym = *p;
  6972. if (sym->is_from_dynobj())
  6973. sym->set_needs_dynsym_value();
  6974. }
  6975. }
  6976. // Write out the .MIPS.stubs. This uses the hand-coded instructions and
  6977. // adjusts them as needed.
  6978. template<int size, bool big_endian>
  6979. void
  6980. Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
  6981. {
  6982. const off_t offset = this->offset();
  6983. const section_size_type oview_size =
  6984. convert_to_section_size_type(this->data_size());
  6985. unsigned char* const oview = of->get_output_view(offset, oview_size);
  6986. bool big_stub = this->dynsym_count_ > 0x10000;
  6987. unsigned char* pov = oview;
  6988. for (typename Mips_stubs_entry_set::const_iterator
  6989. p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
  6990. {
  6991. Mips_symbol<size>* sym = *p;
  6992. const uint32_t* lazy_stub;
  6993. bool n64 = this->target_->is_output_n64();
  6994. if (!this->target_->is_output_micromips())
  6995. {
  6996. // Write standard (non-microMIPS) stub.
  6997. if (!big_stub)
  6998. {
  6999. if (sym->dynsym_index() & ~0x7fff)
  7000. // Dynsym index is between 32K and 64K.
  7001. lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
  7002. else
  7003. // Dynsym index is less than 32K.
  7004. lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
  7005. }
  7006. else
  7007. lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
  7008. unsigned int i = 0;
  7009. elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
  7010. elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
  7011. pov += 8;
  7012. i += 2;
  7013. if (big_stub)
  7014. {
  7015. // LUI instruction of the big stub. Paste high 16 bits of the
  7016. // dynsym index.
  7017. elfcpp::Swap<32, big_endian>::writeval(pov,
  7018. lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
  7019. pov += 4;
  7020. i += 1;
  7021. }
  7022. elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
  7023. // Last stub instruction. Paste low 16 bits of the dynsym index.
  7024. elfcpp::Swap<32, big_endian>::writeval(pov + 4,
  7025. lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
  7026. pov += 8;
  7027. }
  7028. else if (this->target_->use_32bit_micromips_instructions())
  7029. {
  7030. // Write microMIPS stub in insn32 mode.
  7031. if (!big_stub)
  7032. {
  7033. if (sym->dynsym_index() & ~0x7fff)
  7034. // Dynsym index is between 32K and 64K.
  7035. lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
  7036. : lazy_stub_micromips32_normal_2;
  7037. else
  7038. // Dynsym index is less than 32K.
  7039. lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
  7040. : lazy_stub_micromips32_normal_1;
  7041. }
  7042. else
  7043. lazy_stub = n64 ? lazy_stub_micromips32_big_n64
  7044. : lazy_stub_micromips32_big;
  7045. unsigned int i = 0;
  7046. // First stub instruction. We emit 32-bit microMIPS instructions by
  7047. // emitting two 16-bit parts because on microMIPS the 16-bit part of
  7048. // the instruction where the opcode is must always come first, for
  7049. // both little and big endian.
  7050. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  7051. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
  7052. // Second stub instruction.
  7053. elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
  7054. elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
  7055. pov += 8;
  7056. i += 4;
  7057. if (big_stub)
  7058. {
  7059. // LUI instruction of the big stub. Paste high 16 bits of the
  7060. // dynsym index.
  7061. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  7062. elfcpp::Swap<16, big_endian>::writeval(pov + 2,
  7063. (sym->dynsym_index() >> 16) & 0x7fff);
  7064. pov += 4;
  7065. i += 2;
  7066. }
  7067. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  7068. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
  7069. // Last stub instruction. Paste low 16 bits of the dynsym index.
  7070. elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
  7071. elfcpp::Swap<16, big_endian>::writeval(pov + 6,
  7072. sym->dynsym_index() & 0xffff);
  7073. pov += 8;
  7074. }
  7075. else
  7076. {
  7077. // Write microMIPS stub.
  7078. if (!big_stub)
  7079. {
  7080. if (sym->dynsym_index() & ~0x7fff)
  7081. // Dynsym index is between 32K and 64K.
  7082. lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
  7083. : lazy_stub_micromips_normal_2;
  7084. else
  7085. // Dynsym index is less than 32K.
  7086. lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
  7087. : lazy_stub_micromips_normal_1;
  7088. }
  7089. else
  7090. lazy_stub = n64 ? lazy_stub_micromips_big_n64
  7091. : lazy_stub_micromips_big;
  7092. unsigned int i = 0;
  7093. // First stub instruction. We emit 32-bit microMIPS instructions by
  7094. // emitting two 16-bit parts because on microMIPS the 16-bit part of
  7095. // the instruction where the opcode is must always come first, for
  7096. // both little and big endian.
  7097. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  7098. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
  7099. // Second stub instruction.
  7100. elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
  7101. pov += 6;
  7102. i += 3;
  7103. if (big_stub)
  7104. {
  7105. // LUI instruction of the big stub. Paste high 16 bits of the
  7106. // dynsym index.
  7107. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  7108. elfcpp::Swap<16, big_endian>::writeval(pov + 2,
  7109. (sym->dynsym_index() >> 16) & 0x7fff);
  7110. pov += 4;
  7111. i += 2;
  7112. }
  7113. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  7114. // Last stub instruction. Paste low 16 bits of the dynsym index.
  7115. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
  7116. elfcpp::Swap<16, big_endian>::writeval(pov + 4,
  7117. sym->dynsym_index() & 0xffff);
  7118. pov += 6;
  7119. }
  7120. }
  7121. // We always allocate 20 bytes for every stub, because final dynsym count is
  7122. // not known in method do_finalize_sections. There are 4 unused bytes per
  7123. // stub if final dynsym count is less than 0x10000.
  7124. unsigned int used = pov - oview;
  7125. unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
  7126. gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
  7127. // Fill the unused space with zeroes.
  7128. // TODO(sasa): Can we strip unused bytes during the relaxation?
  7129. if (unused > 0)
  7130. memset(pov, 0, unused);
  7131. of->write_output_view(offset, oview_size, oview);
  7132. }
  7133. // Mips_output_section_reginfo methods.
  7134. template<int size, bool big_endian>
  7135. void
  7136. Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
  7137. {
  7138. off_t offset = this->offset();
  7139. off_t data_size = this->data_size();
  7140. unsigned char* view = of->get_output_view(offset, data_size);
  7141. elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
  7142. elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
  7143. elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
  7144. elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
  7145. elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
  7146. // Write the gp value.
  7147. elfcpp::Swap<size, big_endian>::writeval(view + 20,
  7148. this->target_->gp_value());
  7149. of->write_output_view(offset, data_size, view);
  7150. }
  7151. // Mips_output_section_options methods.
  7152. template<int size, bool big_endian>
  7153. void
  7154. Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
  7155. {
  7156. off_t offset = this->offset();
  7157. const section_size_type oview_size =
  7158. convert_to_section_size_type(this->data_size());
  7159. unsigned char* view = of->get_output_view(offset, oview_size);
  7160. const unsigned char* end = view + oview_size;
  7161. while (view + 8 <= end)
  7162. {
  7163. unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
  7164. unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
  7165. if (sz < 8)
  7166. {
  7167. gold_error(_("Warning: bad `%s' option size %u smaller "
  7168. "than its header in output section"),
  7169. this->name(), sz);
  7170. break;
  7171. }
  7172. // Only update ri_gp_value (GP register value) field of ODK_REGINFO entry.
  7173. if (this->target_->is_output_n64() && kind == elfcpp::ODK_REGINFO)
  7174. elfcpp::Swap<size, big_endian>::writeval(view + 32,
  7175. this->target_->gp_value());
  7176. else if (kind == elfcpp::ODK_REGINFO)
  7177. elfcpp::Swap<size, big_endian>::writeval(view + 28,
  7178. this->target_->gp_value());
  7179. view += sz;
  7180. }
  7181. of->write_output_view(offset, oview_size, view);
  7182. }
  7183. // Mips_output_section_abiflags methods.
  7184. template<int size, bool big_endian>
  7185. void
  7186. Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
  7187. {
  7188. off_t offset = this->offset();
  7189. off_t data_size = this->data_size();
  7190. unsigned char* view = of->get_output_view(offset, data_size);
  7191. elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
  7192. elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
  7193. elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
  7194. elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
  7195. elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
  7196. elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
  7197. elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
  7198. elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
  7199. elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
  7200. elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
  7201. elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
  7202. of->write_output_view(offset, data_size, view);
  7203. }
  7204. // Mips_copy_relocs methods.
  7205. // Emit any saved relocs.
  7206. template<int sh_type, int size, bool big_endian>
  7207. void
  7208. Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
  7209. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
  7210. Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
  7211. {
  7212. for (typename Copy_relocs<sh_type, size, big_endian>::
  7213. Copy_reloc_entries::iterator p = this->entries_.begin();
  7214. p != this->entries_.end();
  7215. ++p)
  7216. emit_entry(*p, reloc_section, symtab, layout, target);
  7217. // We no longer need the saved information.
  7218. this->entries_.clear();
  7219. }
  7220. // Emit the reloc if appropriate.
  7221. template<int sh_type, int size, bool big_endian>
  7222. void
  7223. Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
  7224. Copy_reloc_entry& entry,
  7225. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
  7226. Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
  7227. {
  7228. // If the symbol is no longer defined in a dynamic object, then we
  7229. // emitted a COPY relocation, and we do not want to emit this
  7230. // dynamic relocation.
  7231. if (!entry.sym_->is_from_dynobj())
  7232. return;
  7233. bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
  7234. || entry.reloc_type_ == elfcpp::R_MIPS_REL32
  7235. || entry.reloc_type_ == elfcpp::R_MIPS_64);
  7236. Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
  7237. if (can_make_dynamic && !sym->has_static_relocs())
  7238. {
  7239. Mips_relobj<size, big_endian>* object =
  7240. Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
  7241. target->got_section(symtab, layout)->record_global_got_symbol(
  7242. sym, object, entry.reloc_type_, true, false);
  7243. if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
  7244. target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
  7245. entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
  7246. else
  7247. target->rel_dyn_section(layout)->add_symbolless_global_addend(
  7248. sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
  7249. entry.shndx_, entry.address_);
  7250. }
  7251. else
  7252. this->make_copy_reloc(symtab, layout,
  7253. static_cast<Sized_symbol<size>*>(entry.sym_),
  7254. entry.relobj_,
  7255. reloc_section);
  7256. }
  7257. // Target_mips methods.
  7258. // Return the value to use for a dynamic symbol which requires special
  7259. // treatment. This is how we support equality comparisons of function
  7260. // pointers across shared library boundaries, as described in the
  7261. // processor specific ABI supplement.
  7262. template<int size, bool big_endian>
  7263. uint64_t
  7264. Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
  7265. {
  7266. uint64_t value = 0;
  7267. const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
  7268. if (!mips_sym->has_lazy_stub())
  7269. {
  7270. if (mips_sym->has_plt_offset())
  7271. {
  7272. // We distinguish between PLT entries and lazy-binding stubs by
  7273. // giving the former an st_other value of STO_MIPS_PLT. Set the
  7274. // value to the stub address if there are any relocations in the
  7275. // binary where pointer equality matters.
  7276. if (mips_sym->pointer_equality_needed())
  7277. {
  7278. // Prefer a standard MIPS PLT entry.
  7279. if (mips_sym->has_mips_plt_offset())
  7280. value = this->plt_section()->mips_entry_address(mips_sym);
  7281. else
  7282. value = this->plt_section()->comp_entry_address(mips_sym) + 1;
  7283. }
  7284. else
  7285. value = 0;
  7286. }
  7287. }
  7288. else
  7289. {
  7290. // First, set stub offsets for symbols. This method expects that the
  7291. // number of entries in dynamic symbol table is set.
  7292. this->mips_stubs_section()->set_lazy_stub_offsets();
  7293. // The run-time linker uses the st_value field of the symbol
  7294. // to reset the global offset table entry for this external
  7295. // to its stub address when unlinking a shared object.
  7296. value = this->mips_stubs_section()->stub_address(mips_sym);
  7297. }
  7298. if (mips_sym->has_mips16_fn_stub())
  7299. {
  7300. // If we have a MIPS16 function with a stub, the dynamic symbol must
  7301. // refer to the stub, since only the stub uses the standard calling
  7302. // conventions.
  7303. value = mips_sym->template
  7304. get_mips16_fn_stub<big_endian>()->output_address();
  7305. }
  7306. return value;
  7307. }
  7308. // Get the dynamic reloc section, creating it if necessary. It's always
  7309. // .rel.dyn, even for MIPS64.
  7310. template<int size, bool big_endian>
  7311. typename Target_mips<size, big_endian>::Reloc_section*
  7312. Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
  7313. {
  7314. if (this->rel_dyn_ == NULL)
  7315. {
  7316. gold_assert(layout != NULL);
  7317. this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
  7318. layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
  7319. elfcpp::SHF_ALLOC, this->rel_dyn_,
  7320. ORDER_DYNAMIC_RELOCS, false);
  7321. // First entry in .rel.dyn has to be null.
  7322. // This is hack - we define dummy output data and set its address to 0,
  7323. // and define absolute R_MIPS_NONE relocation with offset 0 against it.
  7324. // This ensures that the entry is null.
  7325. Output_data* od = new Output_data_zero_fill(0, 0);
  7326. od->set_address(0);
  7327. this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
  7328. }
  7329. return this->rel_dyn_;
  7330. }
  7331. // Get the GOT section, creating it if necessary.
  7332. template<int size, bool big_endian>
  7333. Mips_output_data_got<size, big_endian>*
  7334. Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
  7335. Layout* layout)
  7336. {
  7337. if (this->got_ == NULL)
  7338. {
  7339. gold_assert(symtab != NULL && layout != NULL);
  7340. this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
  7341. layout);
  7342. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  7343. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
  7344. elfcpp::SHF_MIPS_GPREL),
  7345. this->got_, ORDER_DATA, false);
  7346. // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
  7347. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  7348. Symbol_table::PREDEFINED,
  7349. this->got_,
  7350. 0, 0, elfcpp::STT_OBJECT,
  7351. elfcpp::STB_GLOBAL,
  7352. elfcpp::STV_HIDDEN, 0,
  7353. false, false);
  7354. }
  7355. return this->got_;
  7356. }
  7357. // Calculate value of _gp symbol.
  7358. template<int size, bool big_endian>
  7359. void
  7360. Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
  7361. {
  7362. gold_assert(this->gp_ == NULL);
  7363. Sized_symbol<size>* gp =
  7364. static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
  7365. // Set _gp symbol if the linker script hasn't created it.
  7366. if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
  7367. {
  7368. // If there is no .got section, gp should be based on .sdata.
  7369. Output_data* gp_section = (this->got_ != NULL
  7370. ? this->got_->output_section()
  7371. : layout->find_output_section(".sdata"));
  7372. if (gp_section != NULL)
  7373. gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
  7374. "_gp", NULL, Symbol_table::PREDEFINED,
  7375. gp_section, MIPS_GP_OFFSET, 0,
  7376. elfcpp::STT_NOTYPE,
  7377. elfcpp::STB_LOCAL,
  7378. elfcpp::STV_DEFAULT,
  7379. 0, false, false));
  7380. }
  7381. this->gp_ = gp;
  7382. }
  7383. // Set the dynamic symbol indexes. INDEX is the index of the first
  7384. // global dynamic symbol. Pointers to the symbols are stored into the
  7385. // vector SYMS. The names are added to DYNPOOL. This returns an
  7386. // updated dynamic symbol index.
  7387. template<int size, bool big_endian>
  7388. unsigned int
  7389. Target_mips<size, big_endian>::do_set_dynsym_indexes(
  7390. std::vector<Symbol*>* dyn_symbols, unsigned int index,
  7391. std::vector<Symbol*>* syms, Stringpool* dynpool,
  7392. Versions* versions, Symbol_table* symtab) const
  7393. {
  7394. std::vector<Symbol*> non_got_symbols;
  7395. std::vector<Symbol*> got_symbols;
  7396. reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
  7397. &got_symbols);
  7398. for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
  7399. p != non_got_symbols.end();
  7400. ++p)
  7401. {
  7402. Symbol* sym = *p;
  7403. // Note that SYM may already have a dynamic symbol index, since
  7404. // some symbols appear more than once in the symbol table, with
  7405. // and without a version.
  7406. if (!sym->has_dynsym_index())
  7407. {
  7408. sym->set_dynsym_index(index);
  7409. ++index;
  7410. syms->push_back(sym);
  7411. dynpool->add(sym->name(), false, NULL);
  7412. // Record any version information.
  7413. if (sym->version() != NULL)
  7414. versions->record_version(symtab, dynpool, sym);
  7415. // If the symbol is defined in a dynamic object and is
  7416. // referenced in a regular object, then mark the dynamic
  7417. // object as needed. This is used to implement --as-needed.
  7418. if (sym->is_from_dynobj() && sym->in_reg())
  7419. sym->object()->set_is_needed();
  7420. }
  7421. }
  7422. for (std::vector<Symbol*>::iterator p = got_symbols.begin();
  7423. p != got_symbols.end();
  7424. ++p)
  7425. {
  7426. Symbol* sym = *p;
  7427. if (!sym->has_dynsym_index())
  7428. {
  7429. // Record any version information.
  7430. if (sym->version() != NULL)
  7431. versions->record_version(symtab, dynpool, sym);
  7432. }
  7433. }
  7434. index = versions->finalize(symtab, index, syms);
  7435. int got_sym_count = 0;
  7436. for (std::vector<Symbol*>::iterator p = got_symbols.begin();
  7437. p != got_symbols.end();
  7438. ++p)
  7439. {
  7440. Symbol* sym = *p;
  7441. if (!sym->has_dynsym_index())
  7442. {
  7443. ++got_sym_count;
  7444. sym->set_dynsym_index(index);
  7445. ++index;
  7446. syms->push_back(sym);
  7447. dynpool->add(sym->name(), false, NULL);
  7448. // If the symbol is defined in a dynamic object and is
  7449. // referenced in a regular object, then mark the dynamic
  7450. // object as needed. This is used to implement --as-needed.
  7451. if (sym->is_from_dynobj() && sym->in_reg())
  7452. sym->object()->set_is_needed();
  7453. }
  7454. }
  7455. // Set index of the first symbol that has .got entry.
  7456. this->got_->set_first_global_got_dynsym_index(
  7457. got_sym_count > 0 ? index - got_sym_count : -1U);
  7458. if (this->mips_stubs_ != NULL)
  7459. this->mips_stubs_->set_dynsym_count(index);
  7460. return index;
  7461. }
  7462. // Create a PLT entry for a global symbol referenced by r_type relocation.
  7463. template<int size, bool big_endian>
  7464. void
  7465. Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
  7466. Layout* layout,
  7467. Mips_symbol<size>* gsym,
  7468. unsigned int r_type)
  7469. {
  7470. if (gsym->has_lazy_stub() || gsym->has_plt_offset())
  7471. return;
  7472. if (this->plt_ == NULL)
  7473. {
  7474. // Create the GOT section first.
  7475. this->got_section(symtab, layout);
  7476. this->got_plt_ = new Output_data_space(4, "** GOT PLT");
  7477. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  7478. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
  7479. this->got_plt_, ORDER_DATA, false);
  7480. // The first two entries are reserved.
  7481. this->got_plt_->set_current_data_size(2 * size/8);
  7482. this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
  7483. this->got_plt_,
  7484. this);
  7485. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  7486. (elfcpp::SHF_ALLOC
  7487. | elfcpp::SHF_EXECINSTR),
  7488. this->plt_, ORDER_PLT, false);
  7489. // Make the sh_info field of .rel.plt point to .plt.
  7490. Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
  7491. rel_plt_os->set_info_section(this->plt_->output_section());
  7492. }
  7493. this->plt_->add_entry(gsym, r_type);
  7494. }
  7495. // Get the .MIPS.stubs section, creating it if necessary.
  7496. template<int size, bool big_endian>
  7497. Mips_output_data_mips_stubs<size, big_endian>*
  7498. Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
  7499. {
  7500. if (this->mips_stubs_ == NULL)
  7501. {
  7502. this->mips_stubs_ =
  7503. new Mips_output_data_mips_stubs<size, big_endian>(this);
  7504. layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
  7505. (elfcpp::SHF_ALLOC
  7506. | elfcpp::SHF_EXECINSTR),
  7507. this->mips_stubs_, ORDER_PLT, false);
  7508. }
  7509. return this->mips_stubs_;
  7510. }
  7511. // Get the LA25 stub section, creating it if necessary.
  7512. template<int size, bool big_endian>
  7513. Mips_output_data_la25_stub<size, big_endian>*
  7514. Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
  7515. {
  7516. if (this->la25_stub_ == NULL)
  7517. {
  7518. this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
  7519. layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
  7520. (elfcpp::SHF_ALLOC
  7521. | elfcpp::SHF_EXECINSTR),
  7522. this->la25_stub_, ORDER_TEXT, false);
  7523. }
  7524. return this->la25_stub_;
  7525. }
  7526. // Process the relocations to determine unreferenced sections for
  7527. // garbage collection.
  7528. template<int size, bool big_endian>
  7529. void
  7530. Target_mips<size, big_endian>::gc_process_relocs(
  7531. Symbol_table* symtab,
  7532. Layout* layout,
  7533. Sized_relobj_file<size, big_endian>* object,
  7534. unsigned int data_shndx,
  7535. unsigned int sh_type,
  7536. const unsigned char* prelocs,
  7537. size_t reloc_count,
  7538. Output_section* output_section,
  7539. bool needs_special_offset_handling,
  7540. size_t local_symbol_count,
  7541. const unsigned char* plocal_symbols)
  7542. {
  7543. typedef Target_mips<size, big_endian> Mips;
  7544. if (sh_type == elfcpp::SHT_REL)
  7545. {
  7546. typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
  7547. Classify_reloc;
  7548. gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
  7549. symtab,
  7550. layout,
  7551. this,
  7552. object,
  7553. data_shndx,
  7554. prelocs,
  7555. reloc_count,
  7556. output_section,
  7557. needs_special_offset_handling,
  7558. local_symbol_count,
  7559. plocal_symbols);
  7560. }
  7561. else if (sh_type == elfcpp::SHT_RELA)
  7562. {
  7563. typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  7564. Classify_reloc;
  7565. gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
  7566. symtab,
  7567. layout,
  7568. this,
  7569. object,
  7570. data_shndx,
  7571. prelocs,
  7572. reloc_count,
  7573. output_section,
  7574. needs_special_offset_handling,
  7575. local_symbol_count,
  7576. plocal_symbols);
  7577. }
  7578. else
  7579. gold_unreachable();
  7580. }
  7581. // Scan relocations for a section.
  7582. template<int size, bool big_endian>
  7583. void
  7584. Target_mips<size, big_endian>::scan_relocs(
  7585. Symbol_table* symtab,
  7586. Layout* layout,
  7587. Sized_relobj_file<size, big_endian>* object,
  7588. unsigned int data_shndx,
  7589. unsigned int sh_type,
  7590. const unsigned char* prelocs,
  7591. size_t reloc_count,
  7592. Output_section* output_section,
  7593. bool needs_special_offset_handling,
  7594. size_t local_symbol_count,
  7595. const unsigned char* plocal_symbols)
  7596. {
  7597. typedef Target_mips<size, big_endian> Mips;
  7598. if (sh_type == elfcpp::SHT_REL)
  7599. {
  7600. typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
  7601. Classify_reloc;
  7602. gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
  7603. symtab,
  7604. layout,
  7605. this,
  7606. object,
  7607. data_shndx,
  7608. prelocs,
  7609. reloc_count,
  7610. output_section,
  7611. needs_special_offset_handling,
  7612. local_symbol_count,
  7613. plocal_symbols);
  7614. }
  7615. else if (sh_type == elfcpp::SHT_RELA)
  7616. {
  7617. typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  7618. Classify_reloc;
  7619. gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
  7620. symtab,
  7621. layout,
  7622. this,
  7623. object,
  7624. data_shndx,
  7625. prelocs,
  7626. reloc_count,
  7627. output_section,
  7628. needs_special_offset_handling,
  7629. local_symbol_count,
  7630. plocal_symbols);
  7631. }
  7632. }
  7633. template<int size, bool big_endian>
  7634. bool
  7635. Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
  7636. {
  7637. return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
  7638. || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
  7639. || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
  7640. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
  7641. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
  7642. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
  7643. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2
  7644. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R6);
  7645. }
  7646. // Return the MACH for a MIPS e_flags value.
  7647. template<int size, bool big_endian>
  7648. unsigned int
  7649. Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
  7650. {
  7651. switch (flags & elfcpp::EF_MIPS_MACH)
  7652. {
  7653. case elfcpp::E_MIPS_MACH_3900:
  7654. return mach_mips3900;
  7655. case elfcpp::E_MIPS_MACH_4010:
  7656. return mach_mips4010;
  7657. case elfcpp::E_MIPS_MACH_4100:
  7658. return mach_mips4100;
  7659. case elfcpp::E_MIPS_MACH_4111:
  7660. return mach_mips4111;
  7661. case elfcpp::E_MIPS_MACH_4120:
  7662. return mach_mips4120;
  7663. case elfcpp::E_MIPS_MACH_4650:
  7664. return mach_mips4650;
  7665. case elfcpp::E_MIPS_MACH_5400:
  7666. return mach_mips5400;
  7667. case elfcpp::E_MIPS_MACH_5500:
  7668. return mach_mips5500;
  7669. case elfcpp::E_MIPS_MACH_5900:
  7670. return mach_mips5900;
  7671. case elfcpp::E_MIPS_MACH_9000:
  7672. return mach_mips9000;
  7673. case elfcpp::E_MIPS_MACH_SB1:
  7674. return mach_mips_sb1;
  7675. case elfcpp::E_MIPS_MACH_LS2E:
  7676. return mach_mips_loongson_2e;
  7677. case elfcpp::E_MIPS_MACH_LS2F:
  7678. return mach_mips_loongson_2f;
  7679. case elfcpp::E_MIPS_MACH_GS464:
  7680. return mach_mips_gs464;
  7681. case elfcpp::E_MIPS_MACH_GS464E:
  7682. return mach_mips_gs464e;
  7683. case elfcpp::E_MIPS_MACH_GS264E:
  7684. return mach_mips_gs264e;
  7685. case elfcpp::E_MIPS_MACH_OCTEON3:
  7686. return mach_mips_octeon3;
  7687. case elfcpp::E_MIPS_MACH_OCTEON2:
  7688. return mach_mips_octeon2;
  7689. case elfcpp::E_MIPS_MACH_OCTEON:
  7690. return mach_mips_octeon;
  7691. case elfcpp::E_MIPS_MACH_XLR:
  7692. return mach_mips_xlr;
  7693. default:
  7694. switch (flags & elfcpp::EF_MIPS_ARCH)
  7695. {
  7696. default:
  7697. case elfcpp::E_MIPS_ARCH_1:
  7698. return mach_mips3000;
  7699. case elfcpp::E_MIPS_ARCH_2:
  7700. return mach_mips6000;
  7701. case elfcpp::E_MIPS_ARCH_3:
  7702. return mach_mips4000;
  7703. case elfcpp::E_MIPS_ARCH_4:
  7704. return mach_mips8000;
  7705. case elfcpp::E_MIPS_ARCH_5:
  7706. return mach_mips5;
  7707. case elfcpp::E_MIPS_ARCH_32:
  7708. return mach_mipsisa32;
  7709. case elfcpp::E_MIPS_ARCH_64:
  7710. return mach_mipsisa64;
  7711. case elfcpp::E_MIPS_ARCH_32R2:
  7712. return mach_mipsisa32r2;
  7713. case elfcpp::E_MIPS_ARCH_32R6:
  7714. return mach_mipsisa32r6;
  7715. case elfcpp::E_MIPS_ARCH_64R2:
  7716. return mach_mipsisa64r2;
  7717. case elfcpp::E_MIPS_ARCH_64R6:
  7718. return mach_mipsisa64r6;
  7719. }
  7720. }
  7721. return 0;
  7722. }
  7723. // Return the MACH for each .MIPS.abiflags ISA Extension.
  7724. template<int size, bool big_endian>
  7725. unsigned int
  7726. Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
  7727. {
  7728. switch (isa_ext)
  7729. {
  7730. case elfcpp::AFL_EXT_3900:
  7731. return mach_mips3900;
  7732. case elfcpp::AFL_EXT_4010:
  7733. return mach_mips4010;
  7734. case elfcpp::AFL_EXT_4100:
  7735. return mach_mips4100;
  7736. case elfcpp::AFL_EXT_4111:
  7737. return mach_mips4111;
  7738. case elfcpp::AFL_EXT_4120:
  7739. return mach_mips4120;
  7740. case elfcpp::AFL_EXT_4650:
  7741. return mach_mips4650;
  7742. case elfcpp::AFL_EXT_5400:
  7743. return mach_mips5400;
  7744. case elfcpp::AFL_EXT_5500:
  7745. return mach_mips5500;
  7746. case elfcpp::AFL_EXT_5900:
  7747. return mach_mips5900;
  7748. case elfcpp::AFL_EXT_10000:
  7749. return mach_mips10000;
  7750. case elfcpp::AFL_EXT_LOONGSON_2E:
  7751. return mach_mips_loongson_2e;
  7752. case elfcpp::AFL_EXT_LOONGSON_2F:
  7753. return mach_mips_loongson_2f;
  7754. case elfcpp::AFL_EXT_SB1:
  7755. return mach_mips_sb1;
  7756. case elfcpp::AFL_EXT_OCTEON:
  7757. return mach_mips_octeon;
  7758. case elfcpp::AFL_EXT_OCTEONP:
  7759. return mach_mips_octeonp;
  7760. case elfcpp::AFL_EXT_OCTEON2:
  7761. return mach_mips_octeon2;
  7762. case elfcpp::AFL_EXT_XLR:
  7763. return mach_mips_xlr;
  7764. default:
  7765. return mach_mips3000;
  7766. }
  7767. }
  7768. // Return the .MIPS.abiflags value representing each ISA Extension.
  7769. template<int size, bool big_endian>
  7770. unsigned int
  7771. Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
  7772. {
  7773. switch (mips_mach)
  7774. {
  7775. case mach_mips3900:
  7776. return elfcpp::AFL_EXT_3900;
  7777. case mach_mips4010:
  7778. return elfcpp::AFL_EXT_4010;
  7779. case mach_mips4100:
  7780. return elfcpp::AFL_EXT_4100;
  7781. case mach_mips4111:
  7782. return elfcpp::AFL_EXT_4111;
  7783. case mach_mips4120:
  7784. return elfcpp::AFL_EXT_4120;
  7785. case mach_mips4650:
  7786. return elfcpp::AFL_EXT_4650;
  7787. case mach_mips5400:
  7788. return elfcpp::AFL_EXT_5400;
  7789. case mach_mips5500:
  7790. return elfcpp::AFL_EXT_5500;
  7791. case mach_mips5900:
  7792. return elfcpp::AFL_EXT_5900;
  7793. case mach_mips10000:
  7794. return elfcpp::AFL_EXT_10000;
  7795. case mach_mips_loongson_2e:
  7796. return elfcpp::AFL_EXT_LOONGSON_2E;
  7797. case mach_mips_loongson_2f:
  7798. return elfcpp::AFL_EXT_LOONGSON_2F;
  7799. case mach_mips_sb1:
  7800. return elfcpp::AFL_EXT_SB1;
  7801. case mach_mips_octeon:
  7802. return elfcpp::AFL_EXT_OCTEON;
  7803. case mach_mips_octeonp:
  7804. return elfcpp::AFL_EXT_OCTEONP;
  7805. case mach_mips_octeon3:
  7806. return elfcpp::AFL_EXT_OCTEON3;
  7807. case mach_mips_octeon2:
  7808. return elfcpp::AFL_EXT_OCTEON2;
  7809. case mach_mips_xlr:
  7810. return elfcpp::AFL_EXT_XLR;
  7811. default:
  7812. return 0;
  7813. }
  7814. }
  7815. // Update the isa_level, isa_rev, isa_ext fields of abiflags.
  7816. template<int size, bool big_endian>
  7817. void
  7818. Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
  7819. elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
  7820. {
  7821. int new_isa = 0;
  7822. switch (e_flags & elfcpp::EF_MIPS_ARCH)
  7823. {
  7824. case elfcpp::E_MIPS_ARCH_1:
  7825. new_isa = this->level_rev(1, 0);
  7826. break;
  7827. case elfcpp::E_MIPS_ARCH_2:
  7828. new_isa = this->level_rev(2, 0);
  7829. break;
  7830. case elfcpp::E_MIPS_ARCH_3:
  7831. new_isa = this->level_rev(3, 0);
  7832. break;
  7833. case elfcpp::E_MIPS_ARCH_4:
  7834. new_isa = this->level_rev(4, 0);
  7835. break;
  7836. case elfcpp::E_MIPS_ARCH_5:
  7837. new_isa = this->level_rev(5, 0);
  7838. break;
  7839. case elfcpp::E_MIPS_ARCH_32:
  7840. new_isa = this->level_rev(32, 1);
  7841. break;
  7842. case elfcpp::E_MIPS_ARCH_32R2:
  7843. new_isa = this->level_rev(32, 2);
  7844. break;
  7845. case elfcpp::E_MIPS_ARCH_32R6:
  7846. new_isa = this->level_rev(32, 6);
  7847. break;
  7848. case elfcpp::E_MIPS_ARCH_64:
  7849. new_isa = this->level_rev(64, 1);
  7850. break;
  7851. case elfcpp::E_MIPS_ARCH_64R2:
  7852. new_isa = this->level_rev(64, 2);
  7853. break;
  7854. case elfcpp::E_MIPS_ARCH_64R6:
  7855. new_isa = this->level_rev(64, 6);
  7856. break;
  7857. default:
  7858. gold_error(_("%s: Unknown architecture %s"), name.c_str(),
  7859. this->elf_mips_mach_name(e_flags));
  7860. }
  7861. if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
  7862. {
  7863. // Decode a single value into level and revision.
  7864. abiflags->isa_level = new_isa >> 3;
  7865. abiflags->isa_rev = new_isa & 0x7;
  7866. }
  7867. // Update the isa_ext if needed.
  7868. if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
  7869. this->elf_mips_mach(e_flags)))
  7870. abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
  7871. }
  7872. // Infer the content of the ABI flags based on the elf header.
  7873. template<int size, bool big_endian>
  7874. void
  7875. Target_mips<size, big_endian>::infer_abiflags(
  7876. Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
  7877. {
  7878. const Attributes_section_data* pasd = relobj->attributes_section_data();
  7879. int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
  7880. elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
  7881. this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
  7882. if (pasd != NULL)
  7883. {
  7884. // Read fp_abi from the .gnu.attribute section.
  7885. const Object_attribute* attr =
  7886. pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
  7887. attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
  7888. }
  7889. abiflags->fp_abi = attr_fp_abi;
  7890. abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
  7891. abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
  7892. abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
  7893. : elfcpp::AFL_REG_64;
  7894. if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
  7895. || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
  7896. || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
  7897. && abiflags->gpr_size == elfcpp::AFL_REG_32))
  7898. abiflags->cpr1_size = elfcpp::AFL_REG_32;
  7899. else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
  7900. || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
  7901. || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
  7902. abiflags->cpr1_size = elfcpp::AFL_REG_64;
  7903. if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
  7904. abiflags->ases |= elfcpp::AFL_ASE_MDMX;
  7905. if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
  7906. abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
  7907. if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
  7908. abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
  7909. if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
  7910. && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
  7911. && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
  7912. && abiflags->isa_level >= 32
  7913. && abiflags->ases != elfcpp::AFL_ASE_LOONGSON_EXT)
  7914. abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
  7915. }
  7916. // Create abiflags from elf header or from .MIPS.abiflags section.
  7917. template<int size, bool big_endian>
  7918. void
  7919. Target_mips<size, big_endian>::create_abiflags(
  7920. Mips_relobj<size, big_endian>* relobj,
  7921. Mips_abiflags<big_endian>* abiflags)
  7922. {
  7923. Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
  7924. Mips_abiflags<big_endian> header_abiflags;
  7925. this->infer_abiflags(relobj, &header_abiflags);
  7926. if (sec_abiflags == NULL)
  7927. {
  7928. // If there is no input .MIPS.abiflags section, use abiflags created
  7929. // from elf header.
  7930. *abiflags = header_abiflags;
  7931. return;
  7932. }
  7933. this->has_abiflags_section_ = true;
  7934. // It is not possible to infer the correct ISA revision for R3 or R5
  7935. // so drop down to R2 for the checks.
  7936. unsigned char isa_rev = sec_abiflags->isa_rev;
  7937. if (isa_rev == 3 || isa_rev == 5)
  7938. isa_rev = 2;
  7939. // Check compatibility between abiflags created from elf header
  7940. // and abiflags from .MIPS.abiflags section in this object file.
  7941. if (this->level_rev(sec_abiflags->isa_level, isa_rev)
  7942. < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
  7943. gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
  7944. relobj->name().c_str());
  7945. if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
  7946. && sec_abiflags->fp_abi != header_abiflags.fp_abi)
  7947. gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
  7948. ".MIPS.abiflags"), relobj->name().c_str());
  7949. if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
  7950. gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
  7951. relobj->name().c_str());
  7952. // The isa_ext is allowed to be an extension of what can be inferred
  7953. // from e_flags.
  7954. if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
  7955. this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
  7956. gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
  7957. ".MIPS.abiflags"), relobj->name().c_str());
  7958. if (sec_abiflags->flags2 != 0)
  7959. gold_warning(_("%s: Unexpected flag in the flags2 field of "
  7960. ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
  7961. sec_abiflags->flags2);
  7962. // Use abiflags from .MIPS.abiflags section.
  7963. *abiflags = *sec_abiflags;
  7964. }
  7965. // Return the meaning of fp_abi, or "unknown" if not known.
  7966. template<int size, bool big_endian>
  7967. const char*
  7968. Target_mips<size, big_endian>::fp_abi_string(int fp)
  7969. {
  7970. switch (fp)
  7971. {
  7972. case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
  7973. return "-mdouble-float";
  7974. case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
  7975. return "-msingle-float";
  7976. case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
  7977. return "-msoft-float";
  7978. case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
  7979. return _("-mips32r2 -mfp64 (12 callee-saved)");
  7980. case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
  7981. return "-mfpxx";
  7982. case elfcpp::Val_GNU_MIPS_ABI_FP_64:
  7983. return "-mgp32 -mfp64";
  7984. case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
  7985. return "-mgp32 -mfp64 -mno-odd-spreg";
  7986. default:
  7987. return "unknown";
  7988. }
  7989. }
  7990. // Select fp_abi.
  7991. template<int size, bool big_endian>
  7992. int
  7993. Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
  7994. int out_fp)
  7995. {
  7996. if (in_fp == out_fp)
  7997. return out_fp;
  7998. if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
  7999. return in_fp;
  8000. else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
  8001. && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
  8002. || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
  8003. || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
  8004. return in_fp;
  8005. else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
  8006. && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
  8007. || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
  8008. || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
  8009. return out_fp; // Keep the current setting.
  8010. else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
  8011. && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
  8012. return in_fp;
  8013. else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
  8014. && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
  8015. return out_fp; // Keep the current setting.
  8016. else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
  8017. gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
  8018. fp_abi_string(in_fp), fp_abi_string(out_fp));
  8019. return out_fp;
  8020. }
  8021. // Merge attributes from input object.
  8022. template<int size, bool big_endian>
  8023. void
  8024. Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
  8025. const Attributes_section_data* pasd)
  8026. {
  8027. // Return if there is no attributes section data.
  8028. if (pasd == NULL)
  8029. return;
  8030. // If output has no object attributes, just copy.
  8031. if (this->attributes_section_data_ == NULL)
  8032. {
  8033. this->attributes_section_data_ = new Attributes_section_data(*pasd);
  8034. return;
  8035. }
  8036. Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
  8037. Object_attribute::OBJ_ATTR_GNU);
  8038. out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
  8039. out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
  8040. // Merge Tag_compatibility attributes and any common GNU ones.
  8041. this->attributes_section_data_->merge(name.c_str(), pasd);
  8042. }
  8043. // Merge abiflags from input object.
  8044. template<int size, bool big_endian>
  8045. void
  8046. Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
  8047. Mips_abiflags<big_endian>* in_abiflags)
  8048. {
  8049. // If output has no abiflags, just copy.
  8050. if (this->abiflags_ == NULL)
  8051. {
  8052. this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
  8053. return;
  8054. }
  8055. this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
  8056. this->abiflags_->fp_abi);
  8057. // Merge abiflags.
  8058. this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
  8059. in_abiflags->isa_level);
  8060. this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
  8061. in_abiflags->isa_rev);
  8062. this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
  8063. in_abiflags->gpr_size);
  8064. this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
  8065. in_abiflags->cpr1_size);
  8066. this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
  8067. in_abiflags->cpr2_size);
  8068. this->abiflags_->ases |= in_abiflags->ases;
  8069. this->abiflags_->flags1 |= in_abiflags->flags1;
  8070. }
  8071. // Check whether machine EXTENSION is an extension of machine BASE.
  8072. template<int size, bool big_endian>
  8073. bool
  8074. Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
  8075. unsigned int extension)
  8076. {
  8077. if (extension == base)
  8078. return true;
  8079. if ((base == mach_mipsisa32)
  8080. && this->mips_mach_extends(mach_mipsisa64, extension))
  8081. return true;
  8082. if ((base == mach_mipsisa32r2)
  8083. && this->mips_mach_extends(mach_mipsisa64r2, extension))
  8084. return true;
  8085. for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
  8086. if (extension == this->mips_mach_extensions_[i].first)
  8087. {
  8088. extension = this->mips_mach_extensions_[i].second;
  8089. if (extension == base)
  8090. return true;
  8091. }
  8092. return false;
  8093. }
  8094. // Merge file header flags from input object.
  8095. template<int size, bool big_endian>
  8096. void
  8097. Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
  8098. elfcpp::Elf_Word in_flags)
  8099. {
  8100. // If flags are not set yet, just copy them.
  8101. if (!this->are_processor_specific_flags_set())
  8102. {
  8103. this->set_processor_specific_flags(in_flags);
  8104. this->mach_ = this->elf_mips_mach(in_flags);
  8105. return;
  8106. }
  8107. elfcpp::Elf_Word new_flags = in_flags;
  8108. elfcpp::Elf_Word old_flags = this->processor_specific_flags();
  8109. elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
  8110. merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
  8111. // Check flag compatibility.
  8112. new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
  8113. old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
  8114. // Some IRIX 6 BSD-compatibility objects have this bit set. It
  8115. // doesn't seem to matter.
  8116. new_flags &= ~elfcpp::EF_MIPS_XGOT;
  8117. old_flags &= ~elfcpp::EF_MIPS_XGOT;
  8118. // MIPSpro generates ucode info in n64 objects. Again, we should
  8119. // just be able to ignore this.
  8120. new_flags &= ~elfcpp::EF_MIPS_UCODE;
  8121. old_flags &= ~elfcpp::EF_MIPS_UCODE;
  8122. if (new_flags == old_flags)
  8123. {
  8124. this->set_processor_specific_flags(merged_flags);
  8125. return;
  8126. }
  8127. if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
  8128. != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
  8129. gold_warning(_("%s: linking abicalls files with non-abicalls files"),
  8130. name.c_str());
  8131. if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
  8132. merged_flags |= elfcpp::EF_MIPS_CPIC;
  8133. if (!(new_flags & elfcpp::EF_MIPS_PIC))
  8134. merged_flags &= ~elfcpp::EF_MIPS_PIC;
  8135. new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
  8136. old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
  8137. // Compare the ISAs.
  8138. if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
  8139. gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
  8140. else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
  8141. {
  8142. // Output ISA isn't the same as, or an extension of, input ISA.
  8143. if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
  8144. {
  8145. // Copy the architecture info from input object to output. Also copy
  8146. // the 32-bit flag (if set) so that we continue to recognise
  8147. // output as a 32-bit binary.
  8148. this->mach_ = this->elf_mips_mach(in_flags);
  8149. merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
  8150. merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
  8151. | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
  8152. // Update the ABI flags isa_level, isa_rev, isa_ext fields.
  8153. this->update_abiflags_isa(name, merged_flags, this->abiflags_);
  8154. // Copy across the ABI flags if output doesn't use them
  8155. // and if that was what caused us to treat input object as 32-bit.
  8156. if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
  8157. && this->mips_32bit_flags(new_flags)
  8158. && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
  8159. merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
  8160. }
  8161. else
  8162. // The ISAs aren't compatible.
  8163. gold_error(_("%s: linking %s module with previous %s modules"),
  8164. name.c_str(), this->elf_mips_mach_name(in_flags),
  8165. this->elf_mips_mach_name(merged_flags));
  8166. }
  8167. new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
  8168. | elfcpp::EF_MIPS_32BITMODE));
  8169. old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
  8170. | elfcpp::EF_MIPS_32BITMODE));
  8171. // Compare ABIs.
  8172. if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
  8173. {
  8174. // Only error if both are set (to different values).
  8175. if ((new_flags & elfcpp::EF_MIPS_ABI)
  8176. && (old_flags & elfcpp::EF_MIPS_ABI))
  8177. gold_error(_("%s: ABI mismatch: linking %s module with "
  8178. "previous %s modules"), name.c_str(),
  8179. this->elf_mips_abi_name(in_flags),
  8180. this->elf_mips_abi_name(merged_flags));
  8181. new_flags &= ~elfcpp::EF_MIPS_ABI;
  8182. old_flags &= ~elfcpp::EF_MIPS_ABI;
  8183. }
  8184. // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
  8185. // and allow arbitrary mixing of the remaining ASEs (retain the union).
  8186. if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
  8187. != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
  8188. {
  8189. int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
  8190. int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
  8191. int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
  8192. int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
  8193. int micro_mis = old_m16 && new_micro;
  8194. int m16_mis = old_micro && new_m16;
  8195. if (m16_mis || micro_mis)
  8196. gold_error(_("%s: ASE mismatch: linking %s module with "
  8197. "previous %s modules"), name.c_str(),
  8198. m16_mis ? "MIPS16" : "microMIPS",
  8199. m16_mis ? "microMIPS" : "MIPS16");
  8200. merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
  8201. new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
  8202. old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
  8203. }
  8204. // Compare NaN encodings.
  8205. if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
  8206. {
  8207. gold_error(_("%s: linking %s module with previous %s modules"),
  8208. name.c_str(),
  8209. (new_flags & elfcpp::EF_MIPS_NAN2008
  8210. ? "-mnan=2008" : "-mnan=legacy"),
  8211. (old_flags & elfcpp::EF_MIPS_NAN2008
  8212. ? "-mnan=2008" : "-mnan=legacy"));
  8213. new_flags &= ~elfcpp::EF_MIPS_NAN2008;
  8214. old_flags &= ~elfcpp::EF_MIPS_NAN2008;
  8215. }
  8216. // Compare FP64 state.
  8217. if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
  8218. {
  8219. gold_error(_("%s: linking %s module with previous %s modules"),
  8220. name.c_str(),
  8221. (new_flags & elfcpp::EF_MIPS_FP64
  8222. ? "-mfp64" : "-mfp32"),
  8223. (old_flags & elfcpp::EF_MIPS_FP64
  8224. ? "-mfp64" : "-mfp32"));
  8225. new_flags &= ~elfcpp::EF_MIPS_FP64;
  8226. old_flags &= ~elfcpp::EF_MIPS_FP64;
  8227. }
  8228. // Warn about any other mismatches.
  8229. if (new_flags != old_flags)
  8230. gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
  8231. "modules (0x%x)"), name.c_str(), new_flags, old_flags);
  8232. this->set_processor_specific_flags(merged_flags);
  8233. }
  8234. // Adjust ELF file header.
  8235. template<int size, bool big_endian>
  8236. void
  8237. Target_mips<size, big_endian>::do_adjust_elf_header(
  8238. unsigned char* view,
  8239. int len)
  8240. {
  8241. gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
  8242. elfcpp::Ehdr<size, big_endian> ehdr(view);
  8243. unsigned char e_ident[elfcpp::EI_NIDENT];
  8244. elfcpp::Elf_Word flags = this->processor_specific_flags();
  8245. memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
  8246. unsigned char ei_abiversion = 0;
  8247. elfcpp::Elf_Half type = ehdr.get_e_type();
  8248. if (type == elfcpp::ET_EXEC
  8249. && parameters->options().copyreloc()
  8250. && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
  8251. == elfcpp::EF_MIPS_CPIC)
  8252. ei_abiversion = 1;
  8253. if (this->abiflags_ != NULL
  8254. && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
  8255. || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
  8256. ei_abiversion = 3;
  8257. e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
  8258. elfcpp::Ehdr_write<size, big_endian> oehdr(view);
  8259. oehdr.put_e_ident(e_ident);
  8260. if (this->entry_symbol_is_compressed_)
  8261. oehdr.put_e_entry(ehdr.get_e_entry() + 1);
  8262. }
  8263. // do_make_elf_object to override the same function in the base class.
  8264. // We need to use a target-specific sub-class of
  8265. // Sized_relobj_file<size, big_endian> to store Mips specific information.
  8266. // Hence we need to have our own ELF object creation.
  8267. template<int size, bool big_endian>
  8268. Object*
  8269. Target_mips<size, big_endian>::do_make_elf_object(
  8270. const std::string& name,
  8271. Input_file* input_file,
  8272. off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
  8273. {
  8274. int et = ehdr.get_e_type();
  8275. // ET_EXEC files are valid input for --just-symbols/-R,
  8276. // and we treat them as relocatable objects.
  8277. if (et == elfcpp::ET_REL
  8278. || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
  8279. {
  8280. Mips_relobj<size, big_endian>* obj =
  8281. new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
  8282. obj->setup();
  8283. return obj;
  8284. }
  8285. else if (et == elfcpp::ET_DYN)
  8286. {
  8287. // TODO(sasa): Should we create Mips_dynobj?
  8288. return Target::do_make_elf_object(name, input_file, offset, ehdr);
  8289. }
  8290. else
  8291. {
  8292. gold_error(_("%s: unsupported ELF file type %d"),
  8293. name.c_str(), et);
  8294. return NULL;
  8295. }
  8296. }
  8297. // Finalize the sections.
  8298. template <int size, bool big_endian>
  8299. void
  8300. Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
  8301. const Input_objects* input_objects,
  8302. Symbol_table* symtab)
  8303. {
  8304. const bool relocatable = parameters->options().relocatable();
  8305. // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
  8306. // DT_FINI have correct values.
  8307. Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
  8308. symtab->lookup(parameters->options().init()));
  8309. if (init != NULL && (init->is_mips16() || init->is_micromips()))
  8310. init->set_value(init->value() | 1);
  8311. Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
  8312. symtab->lookup(parameters->options().fini()));
  8313. if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
  8314. fini->set_value(fini->value() | 1);
  8315. // Check whether the entry symbol is mips16 or micromips. This is needed to
  8316. // adjust entry address in ELF header.
  8317. Mips_symbol<size>* entry =
  8318. static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
  8319. this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
  8320. || entry->is_micromips()));
  8321. if (!parameters->doing_static_link()
  8322. && (strcmp(parameters->options().hash_style(), "gnu") == 0
  8323. || strcmp(parameters->options().hash_style(), "both") == 0))
  8324. {
  8325. // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
  8326. // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
  8327. // MIPS ABI requires a mapping between the GOT and the symbol table.
  8328. gold_error(".gnu.hash is incompatible with the MIPS ABI");
  8329. }
  8330. // Check whether the final section that was scanned has HI16 or GOT16
  8331. // relocations without the corresponding LO16 part.
  8332. if (this->got16_addends_.size() > 0)
  8333. gold_error("Can't find matching LO16 reloc");
  8334. Valtype gprmask = 0;
  8335. Valtype cprmask1 = 0;
  8336. Valtype cprmask2 = 0;
  8337. Valtype cprmask3 = 0;
  8338. Valtype cprmask4 = 0;
  8339. bool has_reginfo_section = false;
  8340. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  8341. p != input_objects->relobj_end();
  8342. ++p)
  8343. {
  8344. Mips_relobj<size, big_endian>* relobj =
  8345. Mips_relobj<size, big_endian>::as_mips_relobj(*p);
  8346. // Check for any mips16 stub sections that we can discard.
  8347. if (!relocatable)
  8348. relobj->discard_mips16_stub_sections(symtab);
  8349. if (!relobj->merge_processor_specific_data())
  8350. continue;
  8351. // Merge .reginfo contents of input objects.
  8352. if (relobj->has_reginfo_section())
  8353. {
  8354. has_reginfo_section = true;
  8355. gprmask |= relobj->gprmask();
  8356. cprmask1 |= relobj->cprmask1();
  8357. cprmask2 |= relobj->cprmask2();
  8358. cprmask3 |= relobj->cprmask3();
  8359. cprmask4 |= relobj->cprmask4();
  8360. }
  8361. // Merge processor specific flags.
  8362. Mips_abiflags<big_endian> in_abiflags;
  8363. this->create_abiflags(relobj, &in_abiflags);
  8364. this->merge_obj_e_flags(relobj->name(),
  8365. relobj->processor_specific_flags());
  8366. this->merge_obj_abiflags(relobj->name(), &in_abiflags);
  8367. this->merge_obj_attributes(relobj->name(),
  8368. relobj->attributes_section_data());
  8369. }
  8370. // Create a .gnu.attributes section if we have merged any attributes
  8371. // from inputs.
  8372. if (this->attributes_section_data_ != NULL)
  8373. {
  8374. Output_attributes_section_data* attributes_section =
  8375. new Output_attributes_section_data(*this->attributes_section_data_);
  8376. layout->add_output_section_data(".gnu.attributes",
  8377. elfcpp::SHT_GNU_ATTRIBUTES, 0,
  8378. attributes_section, ORDER_INVALID, false);
  8379. }
  8380. // Create .MIPS.abiflags output section if there is an input section.
  8381. if (this->has_abiflags_section_)
  8382. {
  8383. Mips_output_section_abiflags<size, big_endian>* abiflags_section =
  8384. new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
  8385. Output_section* os =
  8386. layout->add_output_section_data(".MIPS.abiflags",
  8387. elfcpp::SHT_MIPS_ABIFLAGS,
  8388. elfcpp::SHF_ALLOC,
  8389. abiflags_section, ORDER_INVALID, false);
  8390. if (!relocatable && os != NULL)
  8391. {
  8392. Output_segment* abiflags_segment =
  8393. layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
  8394. abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
  8395. }
  8396. }
  8397. if (has_reginfo_section && !parameters->options().gc_sections())
  8398. {
  8399. // Create .reginfo output section.
  8400. Mips_output_section_reginfo<size, big_endian>* reginfo_section =
  8401. new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
  8402. cprmask1, cprmask2,
  8403. cprmask3, cprmask4);
  8404. Output_section* os =
  8405. layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
  8406. elfcpp::SHF_ALLOC, reginfo_section,
  8407. ORDER_INVALID, false);
  8408. if (!relocatable && os != NULL)
  8409. {
  8410. Output_segment* reginfo_segment =
  8411. layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
  8412. elfcpp::PF_R);
  8413. reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
  8414. }
  8415. }
  8416. if (this->plt_ != NULL)
  8417. {
  8418. // Set final PLT offsets for symbols.
  8419. this->plt_section()->set_plt_offsets();
  8420. // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
  8421. // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
  8422. // there are no standard PLT entries present.
  8423. unsigned char nonvis = 0;
  8424. if (this->is_output_micromips()
  8425. && !this->plt_section()->has_standard_entries())
  8426. nonvis = elfcpp::STO_MICROMIPS >> 2;
  8427. symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
  8428. Symbol_table::PREDEFINED,
  8429. this->plt_,
  8430. 0, 0, elfcpp::STT_FUNC,
  8431. elfcpp::STB_LOCAL,
  8432. elfcpp::STV_DEFAULT, nonvis,
  8433. false, false);
  8434. }
  8435. if (this->mips_stubs_ != NULL)
  8436. {
  8437. // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
  8438. unsigned char nonvis = 0;
  8439. if (this->is_output_micromips())
  8440. nonvis = elfcpp::STO_MICROMIPS >> 2;
  8441. symtab->define_in_output_data("_MIPS_STUBS_", NULL,
  8442. Symbol_table::PREDEFINED,
  8443. this->mips_stubs_,
  8444. 0, 0, elfcpp::STT_FUNC,
  8445. elfcpp::STB_LOCAL,
  8446. elfcpp::STV_DEFAULT, nonvis,
  8447. false, false);
  8448. }
  8449. if (!relocatable && !parameters->doing_static_link())
  8450. // In case there is no .got section, create one.
  8451. this->got_section(symtab, layout);
  8452. // Emit any relocs we saved in an attempt to avoid generating COPY
  8453. // relocs.
  8454. if (this->copy_relocs_.any_saved_relocs())
  8455. this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
  8456. this);
  8457. // Set _gp value.
  8458. this->set_gp(layout, symtab);
  8459. // Emit dynamic relocs.
  8460. for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
  8461. p != this->dyn_relocs_.end();
  8462. ++p)
  8463. p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
  8464. if (this->has_got_section())
  8465. this->got_section()->lay_out_got(layout, symtab, input_objects);
  8466. if (this->mips_stubs_ != NULL)
  8467. this->mips_stubs_->set_needs_dynsym_value();
  8468. // Check for functions that might need $25 to be valid on entry.
  8469. // TODO(sasa): Can we do this without iterating over all symbols?
  8470. typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
  8471. symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
  8472. symtab));
  8473. // Add NULL segment.
  8474. if (!relocatable)
  8475. layout->make_output_segment(elfcpp::PT_NULL, 0);
  8476. // Fill in some more dynamic tags.
  8477. // TODO(sasa): Add more dynamic tags.
  8478. const Reloc_section* rel_plt = (this->plt_ == NULL
  8479. ? NULL : this->plt_->rel_plt());
  8480. layout->add_target_dynamic_tags(true, this->got_, rel_plt,
  8481. this->rel_dyn_, true, false);
  8482. Output_data_dynamic* const odyn = layout->dynamic_data();
  8483. if (odyn != NULL
  8484. && !relocatable
  8485. && !parameters->doing_static_link())
  8486. {
  8487. unsigned int d_val;
  8488. // This element holds a 32-bit version id for the Runtime
  8489. // Linker Interface. This will start at integer value 1.
  8490. d_val = 0x01;
  8491. odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
  8492. // Dynamic flags
  8493. d_val = elfcpp::RHF_NOTPOT;
  8494. odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
  8495. // Save layout for using when emitting custom dynamic tags.
  8496. this->layout_ = layout;
  8497. // This member holds the base address of the segment.
  8498. odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
  8499. // This member holds the number of entries in the .dynsym section.
  8500. odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
  8501. // This member holds the index of the first dynamic symbol
  8502. // table entry that corresponds to an entry in the global offset table.
  8503. odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
  8504. // This member holds the number of local GOT entries.
  8505. odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
  8506. this->got_->get_local_gotno());
  8507. if (this->plt_ != NULL)
  8508. // DT_MIPS_PLTGOT dynamic tag
  8509. odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
  8510. if (!parameters->options().shared())
  8511. {
  8512. this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
  8513. layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
  8514. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
  8515. this->rld_map_, ORDER_INVALID, false);
  8516. // __RLD_MAP will be filled in by the runtime loader to contain
  8517. // a pointer to the _r_debug structure.
  8518. Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
  8519. Symbol_table::PREDEFINED,
  8520. this->rld_map_,
  8521. 0, 0, elfcpp::STT_OBJECT,
  8522. elfcpp::STB_GLOBAL,
  8523. elfcpp::STV_DEFAULT, 0,
  8524. false, false);
  8525. if (!rld_map->is_forced_local())
  8526. rld_map->set_needs_dynsym_entry();
  8527. if (!parameters->options().pie())
  8528. // This member holds the absolute address of the debug pointer.
  8529. odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
  8530. else
  8531. // This member holds the offset to the debug pointer,
  8532. // relative to the address of the tag.
  8533. odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
  8534. }
  8535. }
  8536. }
  8537. // Get the custom dynamic tag value.
  8538. template<int size, bool big_endian>
  8539. unsigned int
  8540. Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
  8541. {
  8542. switch (tag)
  8543. {
  8544. case elfcpp::DT_MIPS_BASE_ADDRESS:
  8545. {
  8546. // The base address of the segment.
  8547. // At this point, the segment list has been sorted into final order,
  8548. // so just return vaddr of the first readable PT_LOAD segment.
  8549. Output_segment* seg =
  8550. this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
  8551. gold_assert(seg != NULL);
  8552. return seg->vaddr();
  8553. }
  8554. case elfcpp::DT_MIPS_SYMTABNO:
  8555. // The number of entries in the .dynsym section.
  8556. return this->get_dt_mips_symtabno();
  8557. case elfcpp::DT_MIPS_GOTSYM:
  8558. {
  8559. // The index of the first dynamic symbol table entry that corresponds
  8560. // to an entry in the GOT.
  8561. if (this->got_->first_global_got_dynsym_index() != -1U)
  8562. return this->got_->first_global_got_dynsym_index();
  8563. else
  8564. // In case if we don't have global GOT symbols we default to setting
  8565. // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
  8566. return this->get_dt_mips_symtabno();
  8567. }
  8568. case elfcpp::DT_MIPS_RLD_MAP_REL:
  8569. {
  8570. // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
  8571. // relative to the address of the tag.
  8572. Output_data_dynamic* const odyn = this->layout_->dynamic_data();
  8573. unsigned int entry_offset =
  8574. odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
  8575. gold_assert(entry_offset != -1U);
  8576. return this->rld_map_->address() - (odyn->address() + entry_offset);
  8577. }
  8578. default:
  8579. gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
  8580. }
  8581. return (unsigned int)-1;
  8582. }
  8583. // Relocate section data.
  8584. template<int size, bool big_endian>
  8585. void
  8586. Target_mips<size, big_endian>::relocate_section(
  8587. const Relocate_info<size, big_endian>* relinfo,
  8588. unsigned int sh_type,
  8589. const unsigned char* prelocs,
  8590. size_t reloc_count,
  8591. Output_section* output_section,
  8592. bool needs_special_offset_handling,
  8593. unsigned char* view,
  8594. Mips_address address,
  8595. section_size_type view_size,
  8596. const Reloc_symbol_changes* reloc_symbol_changes)
  8597. {
  8598. typedef Target_mips<size, big_endian> Mips;
  8599. typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
  8600. if (sh_type == elfcpp::SHT_REL)
  8601. {
  8602. typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
  8603. Classify_reloc;
  8604. gold::relocate_section<size, big_endian, Mips, Mips_relocate,
  8605. gold::Default_comdat_behavior, Classify_reloc>(
  8606. relinfo,
  8607. this,
  8608. prelocs,
  8609. reloc_count,
  8610. output_section,
  8611. needs_special_offset_handling,
  8612. view,
  8613. address,
  8614. view_size,
  8615. reloc_symbol_changes);
  8616. }
  8617. else if (sh_type == elfcpp::SHT_RELA)
  8618. {
  8619. typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  8620. Classify_reloc;
  8621. gold::relocate_section<size, big_endian, Mips, Mips_relocate,
  8622. gold::Default_comdat_behavior, Classify_reloc>(
  8623. relinfo,
  8624. this,
  8625. prelocs,
  8626. reloc_count,
  8627. output_section,
  8628. needs_special_offset_handling,
  8629. view,
  8630. address,
  8631. view_size,
  8632. reloc_symbol_changes);
  8633. }
  8634. }
  8635. // Return the size of a relocation while scanning during a relocatable
  8636. // link.
  8637. unsigned int
  8638. mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
  8639. {
  8640. switch (r_type)
  8641. {
  8642. case elfcpp::R_MIPS_NONE:
  8643. case elfcpp::R_MIPS_TLS_DTPMOD64:
  8644. case elfcpp::R_MIPS_TLS_DTPREL64:
  8645. case elfcpp::R_MIPS_TLS_TPREL64:
  8646. return 0;
  8647. case elfcpp::R_MIPS_32:
  8648. case elfcpp::R_MIPS_TLS_DTPMOD32:
  8649. case elfcpp::R_MIPS_TLS_DTPREL32:
  8650. case elfcpp::R_MIPS_TLS_TPREL32:
  8651. case elfcpp::R_MIPS_REL32:
  8652. case elfcpp::R_MIPS_PC32:
  8653. case elfcpp::R_MIPS_GPREL32:
  8654. case elfcpp::R_MIPS_JALR:
  8655. case elfcpp::R_MIPS_EH:
  8656. return 4;
  8657. case elfcpp::R_MIPS_16:
  8658. case elfcpp::R_MIPS_HI16:
  8659. case elfcpp::R_MIPS_LO16:
  8660. case elfcpp::R_MIPS_HIGHER:
  8661. case elfcpp::R_MIPS_HIGHEST:
  8662. case elfcpp::R_MIPS_GPREL16:
  8663. case elfcpp::R_MIPS16_HI16:
  8664. case elfcpp::R_MIPS16_LO16:
  8665. case elfcpp::R_MIPS_PC16:
  8666. case elfcpp::R_MIPS_PCHI16:
  8667. case elfcpp::R_MIPS_PCLO16:
  8668. case elfcpp::R_MIPS_GOT16:
  8669. case elfcpp::R_MIPS16_GOT16:
  8670. case elfcpp::R_MIPS_CALL16:
  8671. case elfcpp::R_MIPS16_CALL16:
  8672. case elfcpp::R_MIPS_GOT_HI16:
  8673. case elfcpp::R_MIPS_CALL_HI16:
  8674. case elfcpp::R_MIPS_GOT_LO16:
  8675. case elfcpp::R_MIPS_CALL_LO16:
  8676. case elfcpp::R_MIPS_TLS_DTPREL_HI16:
  8677. case elfcpp::R_MIPS_TLS_DTPREL_LO16:
  8678. case elfcpp::R_MIPS_TLS_TPREL_HI16:
  8679. case elfcpp::R_MIPS_TLS_TPREL_LO16:
  8680. case elfcpp::R_MIPS16_GPREL:
  8681. case elfcpp::R_MIPS_GOT_DISP:
  8682. case elfcpp::R_MIPS_LITERAL:
  8683. case elfcpp::R_MIPS_GOT_PAGE:
  8684. case elfcpp::R_MIPS_GOT_OFST:
  8685. case elfcpp::R_MIPS_TLS_GD:
  8686. case elfcpp::R_MIPS_TLS_LDM:
  8687. case elfcpp::R_MIPS_TLS_GOTTPREL:
  8688. return 2;
  8689. // These relocations are not byte sized
  8690. case elfcpp::R_MIPS_26:
  8691. case elfcpp::R_MIPS16_26:
  8692. case elfcpp::R_MIPS_PC21_S2:
  8693. case elfcpp::R_MIPS_PC26_S2:
  8694. case elfcpp::R_MIPS_PC18_S3:
  8695. case elfcpp::R_MIPS_PC19_S2:
  8696. return 4;
  8697. case elfcpp::R_MIPS_COPY:
  8698. case elfcpp::R_MIPS_JUMP_SLOT:
  8699. object->error(_("unexpected reloc %u in object file"), r_type);
  8700. return 0;
  8701. default:
  8702. object->error(_("unsupported reloc %u in object file"), r_type);
  8703. return 0;
  8704. }
  8705. }
  8706. // Scan the relocs during a relocatable link.
  8707. template<int size, bool big_endian>
  8708. void
  8709. Target_mips<size, big_endian>::scan_relocatable_relocs(
  8710. Symbol_table* symtab,
  8711. Layout* layout,
  8712. Sized_relobj_file<size, big_endian>* object,
  8713. unsigned int data_shndx,
  8714. unsigned int sh_type,
  8715. const unsigned char* prelocs,
  8716. size_t reloc_count,
  8717. Output_section* output_section,
  8718. bool needs_special_offset_handling,
  8719. size_t local_symbol_count,
  8720. const unsigned char* plocal_symbols,
  8721. Relocatable_relocs* rr)
  8722. {
  8723. if (sh_type == elfcpp::SHT_REL)
  8724. {
  8725. typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
  8726. Classify_reloc;
  8727. typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
  8728. Scan_relocatable_relocs;
  8729. gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
  8730. symtab,
  8731. layout,
  8732. object,
  8733. data_shndx,
  8734. prelocs,
  8735. reloc_count,
  8736. output_section,
  8737. needs_special_offset_handling,
  8738. local_symbol_count,
  8739. plocal_symbols,
  8740. rr);
  8741. }
  8742. else if (sh_type == elfcpp::SHT_RELA)
  8743. {
  8744. typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  8745. Classify_reloc;
  8746. typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
  8747. Scan_relocatable_relocs;
  8748. gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
  8749. symtab,
  8750. layout,
  8751. object,
  8752. data_shndx,
  8753. prelocs,
  8754. reloc_count,
  8755. output_section,
  8756. needs_special_offset_handling,
  8757. local_symbol_count,
  8758. plocal_symbols,
  8759. rr);
  8760. }
  8761. else
  8762. gold_unreachable();
  8763. }
  8764. // Scan the relocs for --emit-relocs.
  8765. template<int size, bool big_endian>
  8766. void
  8767. Target_mips<size, big_endian>::emit_relocs_scan(
  8768. Symbol_table* symtab,
  8769. Layout* layout,
  8770. Sized_relobj_file<size, big_endian>* object,
  8771. unsigned int data_shndx,
  8772. unsigned int sh_type,
  8773. const unsigned char* prelocs,
  8774. size_t reloc_count,
  8775. Output_section* output_section,
  8776. bool needs_special_offset_handling,
  8777. size_t local_symbol_count,
  8778. const unsigned char* plocal_syms,
  8779. Relocatable_relocs* rr)
  8780. {
  8781. if (sh_type == elfcpp::SHT_REL)
  8782. {
  8783. typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
  8784. Classify_reloc;
  8785. typedef gold::Default_emit_relocs_strategy<Classify_reloc>
  8786. Emit_relocs_strategy;
  8787. gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
  8788. symtab,
  8789. layout,
  8790. object,
  8791. data_shndx,
  8792. prelocs,
  8793. reloc_count,
  8794. output_section,
  8795. needs_special_offset_handling,
  8796. local_symbol_count,
  8797. plocal_syms,
  8798. rr);
  8799. }
  8800. else if (sh_type == elfcpp::SHT_RELA)
  8801. {
  8802. typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  8803. Classify_reloc;
  8804. typedef gold::Default_emit_relocs_strategy<Classify_reloc>
  8805. Emit_relocs_strategy;
  8806. gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
  8807. symtab,
  8808. layout,
  8809. object,
  8810. data_shndx,
  8811. prelocs,
  8812. reloc_count,
  8813. output_section,
  8814. needs_special_offset_handling,
  8815. local_symbol_count,
  8816. plocal_syms,
  8817. rr);
  8818. }
  8819. else
  8820. gold_unreachable();
  8821. }
  8822. // Emit relocations for a section.
  8823. template<int size, bool big_endian>
  8824. void
  8825. Target_mips<size, big_endian>::relocate_relocs(
  8826. const Relocate_info<size, big_endian>* relinfo,
  8827. unsigned int sh_type,
  8828. const unsigned char* prelocs,
  8829. size_t reloc_count,
  8830. Output_section* output_section,
  8831. typename elfcpp::Elf_types<size>::Elf_Off
  8832. offset_in_output_section,
  8833. unsigned char* view,
  8834. Mips_address view_address,
  8835. section_size_type view_size,
  8836. unsigned char* reloc_view,
  8837. section_size_type reloc_view_size)
  8838. {
  8839. if (sh_type == elfcpp::SHT_REL)
  8840. {
  8841. typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
  8842. Classify_reloc;
  8843. gold::relocate_relocs<size, big_endian, Classify_reloc>(
  8844. relinfo,
  8845. prelocs,
  8846. reloc_count,
  8847. output_section,
  8848. offset_in_output_section,
  8849. view,
  8850. view_address,
  8851. view_size,
  8852. reloc_view,
  8853. reloc_view_size);
  8854. }
  8855. else if (sh_type == elfcpp::SHT_RELA)
  8856. {
  8857. typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  8858. Classify_reloc;
  8859. gold::relocate_relocs<size, big_endian, Classify_reloc>(
  8860. relinfo,
  8861. prelocs,
  8862. reloc_count,
  8863. output_section,
  8864. offset_in_output_section,
  8865. view,
  8866. view_address,
  8867. view_size,
  8868. reloc_view,
  8869. reloc_view_size);
  8870. }
  8871. else
  8872. gold_unreachable();
  8873. }
  8874. // Perform target-specific processing in a relocatable link. This is
  8875. // only used if we use the relocation strategy RELOC_SPECIAL.
  8876. template<int size, bool big_endian>
  8877. void
  8878. Target_mips<size, big_endian>::relocate_special_relocatable(
  8879. const Relocate_info<size, big_endian>* relinfo,
  8880. unsigned int sh_type,
  8881. const unsigned char* preloc_in,
  8882. size_t relnum,
  8883. Output_section* output_section,
  8884. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  8885. unsigned char* view,
  8886. Mips_address view_address,
  8887. section_size_type,
  8888. unsigned char* preloc_out)
  8889. {
  8890. // We can only handle REL type relocation sections.
  8891. gold_assert(sh_type == elfcpp::SHT_REL);
  8892. typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
  8893. Reltype;
  8894. typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
  8895. Reltype_write;
  8896. typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
  8897. const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
  8898. Mips_relobj<size, big_endian>* object =
  8899. Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
  8900. const unsigned int local_count = object->local_symbol_count();
  8901. Reltype reloc(preloc_in);
  8902. Reltype_write reloc_write(preloc_out);
  8903. elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
  8904. const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  8905. const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  8906. // Get the new symbol index.
  8907. // We only use RELOC_SPECIAL strategy in local relocations.
  8908. gold_assert(r_sym < local_count);
  8909. // We are adjusting a section symbol. We need to find
  8910. // the symbol table index of the section symbol for
  8911. // the output section corresponding to input section
  8912. // in which this symbol is defined.
  8913. bool is_ordinary;
  8914. unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
  8915. gold_assert(is_ordinary);
  8916. Output_section* os = object->output_section(shndx);
  8917. gold_assert(os != NULL);
  8918. gold_assert(os->needs_symtab_index());
  8919. unsigned int new_symndx = os->symtab_index();
  8920. // Get the new offset--the location in the output section where
  8921. // this relocation should be applied.
  8922. Mips_address offset = reloc.get_r_offset();
  8923. Mips_address new_offset;
  8924. if (offset_in_output_section != invalid_address)
  8925. new_offset = offset + offset_in_output_section;
  8926. else
  8927. {
  8928. section_offset_type sot_offset =
  8929. convert_types<section_offset_type, Mips_address>(offset);
  8930. section_offset_type new_sot_offset =
  8931. output_section->output_offset(object, relinfo->data_shndx,
  8932. sot_offset);
  8933. gold_assert(new_sot_offset != -1);
  8934. new_offset = new_sot_offset;
  8935. }
  8936. // In an object file, r_offset is an offset within the section.
  8937. // In an executable or dynamic object, generated by
  8938. // --emit-relocs, r_offset is an absolute address.
  8939. if (!parameters->options().relocatable())
  8940. {
  8941. new_offset += view_address;
  8942. if (offset_in_output_section != invalid_address)
  8943. new_offset -= offset_in_output_section;
  8944. }
  8945. reloc_write.put_r_offset(new_offset);
  8946. reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
  8947. // Handle the reloc addend.
  8948. // The relocation uses a section symbol in the input file.
  8949. // We are adjusting it to use a section symbol in the output
  8950. // file. The input section symbol refers to some address in
  8951. // the input section. We need the relocation in the output
  8952. // file to refer to that same address. This adjustment to
  8953. // the addend is the same calculation we use for a simple
  8954. // absolute relocation for the input section symbol.
  8955. Valtype calculated_value = 0;
  8956. const Symbol_value<size>* psymval = object->local_symbol(r_sym);
  8957. unsigned char* paddend = view + offset;
  8958. typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
  8959. switch (r_type)
  8960. {
  8961. case elfcpp::R_MIPS_26:
  8962. reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
  8963. offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
  8964. false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
  8965. false, &calculated_value);
  8966. break;
  8967. default:
  8968. gold_unreachable();
  8969. }
  8970. // Report any errors.
  8971. switch (reloc_status)
  8972. {
  8973. case Reloc_funcs::STATUS_OKAY:
  8974. break;
  8975. case Reloc_funcs::STATUS_OVERFLOW:
  8976. gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
  8977. _("relocation overflow: "
  8978. "%u against local symbol %u in %s"),
  8979. r_type, r_sym, object->name().c_str());
  8980. break;
  8981. case Reloc_funcs::STATUS_BAD_RELOC:
  8982. gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
  8983. _("unexpected opcode while processing relocation"));
  8984. break;
  8985. default:
  8986. gold_unreachable();
  8987. }
  8988. }
  8989. // Optimize the TLS relocation type based on what we know about the
  8990. // symbol. IS_FINAL is true if the final address of this symbol is
  8991. // known at link time.
  8992. template<int size, bool big_endian>
  8993. tls::Tls_optimization
  8994. Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
  8995. {
  8996. // FIXME: Currently we do not do any TLS optimization.
  8997. return tls::TLSOPT_NONE;
  8998. }
  8999. // Scan a relocation for a local symbol.
  9000. template<int size, bool big_endian>
  9001. inline void
  9002. Target_mips<size, big_endian>::Scan::local(
  9003. Symbol_table* symtab,
  9004. Layout* layout,
  9005. Target_mips<size, big_endian>* target,
  9006. Sized_relobj_file<size, big_endian>* object,
  9007. unsigned int data_shndx,
  9008. Output_section* output_section,
  9009. const Relatype* rela,
  9010. const Reltype* rel,
  9011. unsigned int rel_type,
  9012. unsigned int r_type,
  9013. const elfcpp::Sym<size, big_endian>& lsym,
  9014. bool is_discarded)
  9015. {
  9016. if (is_discarded)
  9017. return;
  9018. Mips_address r_offset;
  9019. unsigned int r_sym;
  9020. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
  9021. if (rel_type == elfcpp::SHT_RELA)
  9022. {
  9023. r_offset = rela->get_r_offset();
  9024. r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
  9025. get_r_sym(rela);
  9026. r_addend = rela->get_r_addend();
  9027. }
  9028. else
  9029. {
  9030. r_offset = rel->get_r_offset();
  9031. r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
  9032. get_r_sym(rel);
  9033. r_addend = 0;
  9034. }
  9035. Mips_relobj<size, big_endian>* mips_obj =
  9036. Mips_relobj<size, big_endian>::as_mips_relobj(object);
  9037. if (mips_obj->is_mips16_stub_section(data_shndx))
  9038. {
  9039. mips_obj->get_mips16_stub_section(data_shndx)
  9040. ->new_local_reloc_found(r_type, r_sym);
  9041. }
  9042. if (r_type == elfcpp::R_MIPS_NONE)
  9043. // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
  9044. // mips16 stub.
  9045. return;
  9046. if (!mips16_call_reloc(r_type)
  9047. && !mips_obj->section_allows_mips16_refs(data_shndx))
  9048. // This reloc would need to refer to a MIPS16 hard-float stub, if
  9049. // there is one. We ignore MIPS16 stub sections and .pdr section when
  9050. // looking for relocs that would need to refer to MIPS16 stubs.
  9051. mips_obj->add_local_non_16bit_call(r_sym);
  9052. if (r_type == elfcpp::R_MIPS16_26
  9053. && !mips_obj->section_allows_mips16_refs(data_shndx))
  9054. mips_obj->add_local_16bit_call(r_sym);
  9055. switch (r_type)
  9056. {
  9057. case elfcpp::R_MIPS_GOT16:
  9058. case elfcpp::R_MIPS_CALL16:
  9059. case elfcpp::R_MIPS_CALL_HI16:
  9060. case elfcpp::R_MIPS_CALL_LO16:
  9061. case elfcpp::R_MIPS_GOT_HI16:
  9062. case elfcpp::R_MIPS_GOT_LO16:
  9063. case elfcpp::R_MIPS_GOT_PAGE:
  9064. case elfcpp::R_MIPS_GOT_OFST:
  9065. case elfcpp::R_MIPS_GOT_DISP:
  9066. case elfcpp::R_MIPS_TLS_GOTTPREL:
  9067. case elfcpp::R_MIPS_TLS_GD:
  9068. case elfcpp::R_MIPS_TLS_LDM:
  9069. case elfcpp::R_MIPS16_GOT16:
  9070. case elfcpp::R_MIPS16_CALL16:
  9071. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  9072. case elfcpp::R_MIPS16_TLS_GD:
  9073. case elfcpp::R_MIPS16_TLS_LDM:
  9074. case elfcpp::R_MICROMIPS_GOT16:
  9075. case elfcpp::R_MICROMIPS_CALL16:
  9076. case elfcpp::R_MICROMIPS_CALL_HI16:
  9077. case elfcpp::R_MICROMIPS_CALL_LO16:
  9078. case elfcpp::R_MICROMIPS_GOT_HI16:
  9079. case elfcpp::R_MICROMIPS_GOT_LO16:
  9080. case elfcpp::R_MICROMIPS_GOT_PAGE:
  9081. case elfcpp::R_MICROMIPS_GOT_OFST:
  9082. case elfcpp::R_MICROMIPS_GOT_DISP:
  9083. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  9084. case elfcpp::R_MICROMIPS_TLS_GD:
  9085. case elfcpp::R_MICROMIPS_TLS_LDM:
  9086. case elfcpp::R_MIPS_EH:
  9087. // We need a GOT section.
  9088. target->got_section(symtab, layout);
  9089. break;
  9090. default:
  9091. break;
  9092. }
  9093. if (call_lo16_reloc(r_type)
  9094. || got_lo16_reloc(r_type)
  9095. || got_disp_reloc(r_type)
  9096. || eh_reloc(r_type))
  9097. {
  9098. // We may need a local GOT entry for this relocation. We
  9099. // don't count R_MIPS_GOT_PAGE because we can estimate the
  9100. // maximum number of pages needed by looking at the size of
  9101. // the segment. Similar comments apply to R_MIPS*_GOT16 and
  9102. // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
  9103. // R_MIPS_CALL_HI16 because these are always followed by an
  9104. // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
  9105. Mips_output_data_got<size, big_endian>* got =
  9106. target->got_section(symtab, layout);
  9107. bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
  9108. got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
  9109. is_section_symbol);
  9110. }
  9111. switch (r_type)
  9112. {
  9113. case elfcpp::R_MIPS_CALL16:
  9114. case elfcpp::R_MIPS16_CALL16:
  9115. case elfcpp::R_MICROMIPS_CALL16:
  9116. gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
  9117. (unsigned long)r_offset);
  9118. return;
  9119. case elfcpp::R_MIPS_GOT_PAGE:
  9120. case elfcpp::R_MICROMIPS_GOT_PAGE:
  9121. case elfcpp::R_MIPS16_GOT16:
  9122. case elfcpp::R_MIPS_GOT16:
  9123. case elfcpp::R_MIPS_GOT_HI16:
  9124. case elfcpp::R_MIPS_GOT_LO16:
  9125. case elfcpp::R_MICROMIPS_GOT16:
  9126. case elfcpp::R_MICROMIPS_GOT_HI16:
  9127. case elfcpp::R_MICROMIPS_GOT_LO16:
  9128. {
  9129. // This relocation needs a page entry in the GOT.
  9130. // Get the section contents.
  9131. section_size_type view_size = 0;
  9132. const unsigned char* view = object->section_contents(data_shndx,
  9133. &view_size, false);
  9134. view += r_offset;
  9135. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  9136. Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
  9137. : r_addend);
  9138. if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
  9139. target->got16_addends_.push_back(got16_addend<size, big_endian>(
  9140. object, data_shndx, r_type, r_sym, addend));
  9141. else
  9142. target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
  9143. break;
  9144. }
  9145. case elfcpp::R_MIPS_HI16:
  9146. case elfcpp::R_MIPS_PCHI16:
  9147. case elfcpp::R_MIPS16_HI16:
  9148. case elfcpp::R_MICROMIPS_HI16:
  9149. // Record the reloc so that we can check whether the corresponding LO16
  9150. // part exists.
  9151. if (rel_type == elfcpp::SHT_REL)
  9152. target->got16_addends_.push_back(got16_addend<size, big_endian>(
  9153. object, data_shndx, r_type, r_sym, 0));
  9154. break;
  9155. case elfcpp::R_MIPS_LO16:
  9156. case elfcpp::R_MIPS_PCLO16:
  9157. case elfcpp::R_MIPS16_LO16:
  9158. case elfcpp::R_MICROMIPS_LO16:
  9159. {
  9160. if (rel_type != elfcpp::SHT_REL)
  9161. break;
  9162. // Find corresponding GOT16/HI16 relocation.
  9163. // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
  9164. // be immediately following. However, for the IRIX6 ABI, the next
  9165. // relocation may be a composed relocation consisting of several
  9166. // relocations for the same address. In that case, the R_MIPS_LO16
  9167. // relocation may occur as one of these. We permit a similar
  9168. // extension in general, as that is useful for GCC.
  9169. // In some cases GCC dead code elimination removes the LO16 but
  9170. // keeps the corresponding HI16. This is strictly speaking a
  9171. // violation of the ABI but not immediately harmful.
  9172. typename std::list<got16_addend<size, big_endian> >::iterator it =
  9173. target->got16_addends_.begin();
  9174. while (it != target->got16_addends_.end())
  9175. {
  9176. got16_addend<size, big_endian> _got16_addend = *it;
  9177. // TODO(sasa): Split got16_addends_ list into two lists - one for
  9178. // GOT16 relocs and the other for HI16 relocs.
  9179. // Report an error if we find HI16 or GOT16 reloc from the
  9180. // previous section without the matching LO16 part.
  9181. if (_got16_addend.object != object
  9182. || _got16_addend.shndx != data_shndx)
  9183. {
  9184. gold_error("Can't find matching LO16 reloc");
  9185. break;
  9186. }
  9187. if (_got16_addend.r_sym != r_sym
  9188. || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
  9189. {
  9190. ++it;
  9191. continue;
  9192. }
  9193. // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
  9194. // For GOT16, we need to calculate combined addend and record GOT page
  9195. // entry.
  9196. if (got16_reloc(_got16_addend.r_type))
  9197. {
  9198. section_size_type view_size = 0;
  9199. const unsigned char* view = object->section_contents(data_shndx,
  9200. &view_size,
  9201. false);
  9202. view += r_offset;
  9203. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  9204. int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
  9205. addend = (_got16_addend.addend << 16) + addend;
  9206. target->got_section()->record_got_page_entry(mips_obj, r_sym,
  9207. addend);
  9208. }
  9209. it = target->got16_addends_.erase(it);
  9210. }
  9211. break;
  9212. }
  9213. }
  9214. switch (r_type)
  9215. {
  9216. case elfcpp::R_MIPS_32:
  9217. case elfcpp::R_MIPS_REL32:
  9218. case elfcpp::R_MIPS_64:
  9219. {
  9220. if (parameters->options().output_is_position_independent())
  9221. {
  9222. // If building a shared library (or a position-independent
  9223. // executable), we need to create a dynamic relocation for
  9224. // this location.
  9225. if (is_readonly_section(output_section))
  9226. break;
  9227. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  9228. rel_dyn->add_symbolless_local_addend(object, r_sym,
  9229. elfcpp::R_MIPS_REL32,
  9230. output_section, data_shndx,
  9231. r_offset);
  9232. }
  9233. break;
  9234. }
  9235. case elfcpp::R_MIPS_TLS_GOTTPREL:
  9236. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  9237. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  9238. case elfcpp::R_MIPS_TLS_LDM:
  9239. case elfcpp::R_MIPS16_TLS_LDM:
  9240. case elfcpp::R_MICROMIPS_TLS_LDM:
  9241. case elfcpp::R_MIPS_TLS_GD:
  9242. case elfcpp::R_MIPS16_TLS_GD:
  9243. case elfcpp::R_MICROMIPS_TLS_GD:
  9244. {
  9245. bool output_is_shared = parameters->options().shared();
  9246. const tls::Tls_optimization optimized_type
  9247. = Target_mips<size, big_endian>::optimize_tls_reloc(
  9248. !output_is_shared, r_type);
  9249. switch (r_type)
  9250. {
  9251. case elfcpp::R_MIPS_TLS_GD:
  9252. case elfcpp::R_MIPS16_TLS_GD:
  9253. case elfcpp::R_MICROMIPS_TLS_GD:
  9254. if (optimized_type == tls::TLSOPT_NONE)
  9255. {
  9256. // Create a pair of GOT entries for the module index and
  9257. // dtv-relative offset.
  9258. Mips_output_data_got<size, big_endian>* got =
  9259. target->got_section(symtab, layout);
  9260. unsigned int shndx = lsym.get_st_shndx();
  9261. bool is_ordinary;
  9262. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  9263. if (!is_ordinary)
  9264. {
  9265. object->error(_("local symbol %u has bad shndx %u"),
  9266. r_sym, shndx);
  9267. break;
  9268. }
  9269. got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
  9270. shndx, false);
  9271. }
  9272. else
  9273. {
  9274. // FIXME: TLS optimization not supported yet.
  9275. gold_unreachable();
  9276. }
  9277. break;
  9278. case elfcpp::R_MIPS_TLS_LDM:
  9279. case elfcpp::R_MIPS16_TLS_LDM:
  9280. case elfcpp::R_MICROMIPS_TLS_LDM:
  9281. if (optimized_type == tls::TLSOPT_NONE)
  9282. {
  9283. // We always record LDM symbols as local with index 0.
  9284. target->got_section()->record_local_got_symbol(mips_obj, 0,
  9285. r_addend, r_type,
  9286. -1U, false);
  9287. }
  9288. else
  9289. {
  9290. // FIXME: TLS optimization not supported yet.
  9291. gold_unreachable();
  9292. }
  9293. break;
  9294. case elfcpp::R_MIPS_TLS_GOTTPREL:
  9295. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  9296. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  9297. layout->set_has_static_tls();
  9298. if (optimized_type == tls::TLSOPT_NONE)
  9299. {
  9300. // Create a GOT entry for the tp-relative offset.
  9301. Mips_output_data_got<size, big_endian>* got =
  9302. target->got_section(symtab, layout);
  9303. got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
  9304. -1U, false);
  9305. }
  9306. else
  9307. {
  9308. // FIXME: TLS optimization not supported yet.
  9309. gold_unreachable();
  9310. }
  9311. break;
  9312. default:
  9313. gold_unreachable();
  9314. }
  9315. }
  9316. break;
  9317. default:
  9318. break;
  9319. }
  9320. // Refuse some position-dependent relocations when creating a
  9321. // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
  9322. // not PIC, but we can create dynamic relocations and the result
  9323. // will be fine. Also do not refuse R_MIPS_LO16, which can be
  9324. // combined with R_MIPS_GOT16.
  9325. if (parameters->options().shared())
  9326. {
  9327. switch (r_type)
  9328. {
  9329. case elfcpp::R_MIPS16_HI16:
  9330. case elfcpp::R_MIPS_HI16:
  9331. case elfcpp::R_MIPS_HIGHER:
  9332. case elfcpp::R_MIPS_HIGHEST:
  9333. case elfcpp::R_MICROMIPS_HI16:
  9334. case elfcpp::R_MICROMIPS_HIGHER:
  9335. case elfcpp::R_MICROMIPS_HIGHEST:
  9336. // Don't refuse a high part relocation if it's against
  9337. // no symbol (e.g. part of a compound relocation).
  9338. if (r_sym == 0)
  9339. break;
  9340. // Fall through.
  9341. case elfcpp::R_MIPS16_26:
  9342. case elfcpp::R_MIPS_26:
  9343. case elfcpp::R_MICROMIPS_26_S1:
  9344. gold_error(_("%s: relocation %u against `%s' can not be used when "
  9345. "making a shared object; recompile with -fPIC"),
  9346. object->name().c_str(), r_type, "a local symbol");
  9347. default:
  9348. break;
  9349. }
  9350. }
  9351. }
  9352. template<int size, bool big_endian>
  9353. inline void
  9354. Target_mips<size, big_endian>::Scan::local(
  9355. Symbol_table* symtab,
  9356. Layout* layout,
  9357. Target_mips<size, big_endian>* target,
  9358. Sized_relobj_file<size, big_endian>* object,
  9359. unsigned int data_shndx,
  9360. Output_section* output_section,
  9361. const Reltype& reloc,
  9362. unsigned int r_type,
  9363. const elfcpp::Sym<size, big_endian>& lsym,
  9364. bool is_discarded)
  9365. {
  9366. if (is_discarded)
  9367. return;
  9368. local(
  9369. symtab,
  9370. layout,
  9371. target,
  9372. object,
  9373. data_shndx,
  9374. output_section,
  9375. (const Relatype*) NULL,
  9376. &reloc,
  9377. elfcpp::SHT_REL,
  9378. r_type,
  9379. lsym, is_discarded);
  9380. }
  9381. template<int size, bool big_endian>
  9382. inline void
  9383. Target_mips<size, big_endian>::Scan::local(
  9384. Symbol_table* symtab,
  9385. Layout* layout,
  9386. Target_mips<size, big_endian>* target,
  9387. Sized_relobj_file<size, big_endian>* object,
  9388. unsigned int data_shndx,
  9389. Output_section* output_section,
  9390. const Relatype& reloc,
  9391. unsigned int r_type,
  9392. const elfcpp::Sym<size, big_endian>& lsym,
  9393. bool is_discarded)
  9394. {
  9395. if (is_discarded)
  9396. return;
  9397. local(
  9398. symtab,
  9399. layout,
  9400. target,
  9401. object,
  9402. data_shndx,
  9403. output_section,
  9404. &reloc,
  9405. (const Reltype*) NULL,
  9406. elfcpp::SHT_RELA,
  9407. r_type,
  9408. lsym, is_discarded);
  9409. }
  9410. // Scan a relocation for a global symbol.
  9411. template<int size, bool big_endian>
  9412. inline void
  9413. Target_mips<size, big_endian>::Scan::global(
  9414. Symbol_table* symtab,
  9415. Layout* layout,
  9416. Target_mips<size, big_endian>* target,
  9417. Sized_relobj_file<size, big_endian>* object,
  9418. unsigned int data_shndx,
  9419. Output_section* output_section,
  9420. const Relatype* rela,
  9421. const Reltype* rel,
  9422. unsigned int rel_type,
  9423. unsigned int r_type,
  9424. Symbol* gsym)
  9425. {
  9426. Mips_address r_offset;
  9427. unsigned int r_sym;
  9428. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
  9429. if (rel_type == elfcpp::SHT_RELA)
  9430. {
  9431. r_offset = rela->get_r_offset();
  9432. r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
  9433. get_r_sym(rela);
  9434. r_addend = rela->get_r_addend();
  9435. }
  9436. else
  9437. {
  9438. r_offset = rel->get_r_offset();
  9439. r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
  9440. get_r_sym(rel);
  9441. r_addend = 0;
  9442. }
  9443. Mips_relobj<size, big_endian>* mips_obj =
  9444. Mips_relobj<size, big_endian>::as_mips_relobj(object);
  9445. Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
  9446. if (mips_obj->is_mips16_stub_section(data_shndx))
  9447. {
  9448. mips_obj->get_mips16_stub_section(data_shndx)
  9449. ->new_global_reloc_found(r_type, mips_sym);
  9450. }
  9451. if (r_type == elfcpp::R_MIPS_NONE)
  9452. // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
  9453. // mips16 stub.
  9454. return;
  9455. if (!mips16_call_reloc(r_type)
  9456. && !mips_obj->section_allows_mips16_refs(data_shndx))
  9457. // This reloc would need to refer to a MIPS16 hard-float stub, if
  9458. // there is one. We ignore MIPS16 stub sections and .pdr section when
  9459. // looking for relocs that would need to refer to MIPS16 stubs.
  9460. mips_sym->set_need_fn_stub();
  9461. // We need PLT entries if there are static-only relocations against
  9462. // an externally-defined function. This can technically occur for
  9463. // shared libraries if there are branches to the symbol, although it
  9464. // is unlikely that this will be used in practice due to the short
  9465. // ranges involved. It can occur for any relative or absolute relocation
  9466. // in executables; in that case, the PLT entry becomes the function's
  9467. // canonical address.
  9468. bool static_reloc = false;
  9469. // Set CAN_MAKE_DYNAMIC to true if we can convert this
  9470. // relocation into a dynamic one.
  9471. bool can_make_dynamic = false;
  9472. switch (r_type)
  9473. {
  9474. case elfcpp::R_MIPS_GOT16:
  9475. case elfcpp::R_MIPS_CALL16:
  9476. case elfcpp::R_MIPS_CALL_HI16:
  9477. case elfcpp::R_MIPS_CALL_LO16:
  9478. case elfcpp::R_MIPS_GOT_HI16:
  9479. case elfcpp::R_MIPS_GOT_LO16:
  9480. case elfcpp::R_MIPS_GOT_PAGE:
  9481. case elfcpp::R_MIPS_GOT_OFST:
  9482. case elfcpp::R_MIPS_GOT_DISP:
  9483. case elfcpp::R_MIPS_TLS_GOTTPREL:
  9484. case elfcpp::R_MIPS_TLS_GD:
  9485. case elfcpp::R_MIPS_TLS_LDM:
  9486. case elfcpp::R_MIPS16_GOT16:
  9487. case elfcpp::R_MIPS16_CALL16:
  9488. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  9489. case elfcpp::R_MIPS16_TLS_GD:
  9490. case elfcpp::R_MIPS16_TLS_LDM:
  9491. case elfcpp::R_MICROMIPS_GOT16:
  9492. case elfcpp::R_MICROMIPS_CALL16:
  9493. case elfcpp::R_MICROMIPS_CALL_HI16:
  9494. case elfcpp::R_MICROMIPS_CALL_LO16:
  9495. case elfcpp::R_MICROMIPS_GOT_HI16:
  9496. case elfcpp::R_MICROMIPS_GOT_LO16:
  9497. case elfcpp::R_MICROMIPS_GOT_PAGE:
  9498. case elfcpp::R_MICROMIPS_GOT_OFST:
  9499. case elfcpp::R_MICROMIPS_GOT_DISP:
  9500. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  9501. case elfcpp::R_MICROMIPS_TLS_GD:
  9502. case elfcpp::R_MICROMIPS_TLS_LDM:
  9503. case elfcpp::R_MIPS_EH:
  9504. // We need a GOT section.
  9505. target->got_section(symtab, layout);
  9506. break;
  9507. // This is just a hint; it can safely be ignored. Don't set
  9508. // has_static_relocs for the corresponding symbol.
  9509. case elfcpp::R_MIPS_JALR:
  9510. case elfcpp::R_MICROMIPS_JALR:
  9511. break;
  9512. case elfcpp::R_MIPS_GPREL16:
  9513. case elfcpp::R_MIPS_GPREL32:
  9514. case elfcpp::R_MIPS16_GPREL:
  9515. case elfcpp::R_MICROMIPS_GPREL16:
  9516. // TODO(sasa)
  9517. // GP-relative relocations always resolve to a definition in a
  9518. // regular input file, ignoring the one-definition rule. This is
  9519. // important for the GP setup sequence in NewABI code, which
  9520. // always resolves to a local function even if other relocations
  9521. // against the symbol wouldn't.
  9522. //constrain_symbol_p = FALSE;
  9523. break;
  9524. case elfcpp::R_MIPS_32:
  9525. case elfcpp::R_MIPS_REL32:
  9526. case elfcpp::R_MIPS_64:
  9527. if ((parameters->options().shared()
  9528. || (strcmp(gsym->name(), "__gnu_local_gp") != 0
  9529. && (!is_readonly_section(output_section)
  9530. || mips_obj->is_pic())))
  9531. && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
  9532. {
  9533. if (r_type != elfcpp::R_MIPS_REL32)
  9534. mips_sym->set_pointer_equality_needed();
  9535. can_make_dynamic = true;
  9536. break;
  9537. }
  9538. // Fall through.
  9539. default:
  9540. // Most static relocations require pointer equality, except
  9541. // for branches.
  9542. mips_sym->set_pointer_equality_needed();
  9543. // Fall through.
  9544. case elfcpp::R_MIPS_26:
  9545. case elfcpp::R_MIPS_PC16:
  9546. case elfcpp::R_MIPS_PC21_S2:
  9547. case elfcpp::R_MIPS_PC26_S2:
  9548. case elfcpp::R_MIPS16_26:
  9549. case elfcpp::R_MICROMIPS_26_S1:
  9550. case elfcpp::R_MICROMIPS_PC7_S1:
  9551. case elfcpp::R_MICROMIPS_PC10_S1:
  9552. case elfcpp::R_MICROMIPS_PC16_S1:
  9553. case elfcpp::R_MICROMIPS_PC23_S2:
  9554. static_reloc = true;
  9555. mips_sym->set_has_static_relocs();
  9556. break;
  9557. }
  9558. // If there are call relocations against an externally-defined symbol,
  9559. // see whether we can create a MIPS lazy-binding stub for it. We can
  9560. // only do this if all references to the function are through call
  9561. // relocations, and in that case, the traditional lazy-binding stubs
  9562. // are much more efficient than PLT entries.
  9563. switch (r_type)
  9564. {
  9565. case elfcpp::R_MIPS16_CALL16:
  9566. case elfcpp::R_MIPS_CALL16:
  9567. case elfcpp::R_MIPS_CALL_HI16:
  9568. case elfcpp::R_MIPS_CALL_LO16:
  9569. case elfcpp::R_MIPS_JALR:
  9570. case elfcpp::R_MICROMIPS_CALL16:
  9571. case elfcpp::R_MICROMIPS_CALL_HI16:
  9572. case elfcpp::R_MICROMIPS_CALL_LO16:
  9573. case elfcpp::R_MICROMIPS_JALR:
  9574. if (!mips_sym->no_lazy_stub())
  9575. {
  9576. if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
  9577. // Calls from shared objects to undefined symbols of type
  9578. // STT_NOTYPE need lazy-binding stub.
  9579. || (mips_sym->is_undefined() && parameters->options().shared()))
  9580. target->mips_stubs_section(layout)->make_entry(mips_sym);
  9581. }
  9582. break;
  9583. default:
  9584. {
  9585. // We must not create a stub for a symbol that has relocations
  9586. // related to taking the function's address.
  9587. mips_sym->set_no_lazy_stub();
  9588. target->remove_lazy_stub_entry(mips_sym);
  9589. break;
  9590. }
  9591. }
  9592. if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
  9593. mips_sym->is_mips16()))
  9594. mips_sym->set_has_nonpic_branches();
  9595. // R_MIPS_HI16 against _gp_disp is used for $gp setup,
  9596. // and has a special meaning.
  9597. bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
  9598. && strcmp(gsym->name(), "_gp_disp") == 0
  9599. && (hi16_reloc(r_type) || lo16_reloc(r_type)));
  9600. if (static_reloc && gsym->needs_plt_entry())
  9601. {
  9602. target->make_plt_entry(symtab, layout, mips_sym, r_type);
  9603. // Since this is not a PC-relative relocation, we may be
  9604. // taking the address of a function. In that case we need to
  9605. // set the entry in the dynamic symbol table to the address of
  9606. // the PLT entry.
  9607. if (gsym->is_from_dynobj() && !parameters->options().shared())
  9608. {
  9609. gsym->set_needs_dynsym_value();
  9610. // We distinguish between PLT entries and lazy-binding stubs by
  9611. // giving the former an st_other value of STO_MIPS_PLT. Set the
  9612. // flag if there are any relocations in the binary where pointer
  9613. // equality matters.
  9614. if (mips_sym->pointer_equality_needed())
  9615. mips_sym->set_mips_plt();
  9616. }
  9617. }
  9618. if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
  9619. {
  9620. // Absolute addressing relocations.
  9621. // Make a dynamic relocation if necessary.
  9622. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  9623. {
  9624. if (gsym->may_need_copy_reloc())
  9625. {
  9626. target->copy_reloc(symtab, layout, object, data_shndx,
  9627. output_section, gsym, r_type, r_offset);
  9628. }
  9629. else if (can_make_dynamic)
  9630. {
  9631. // Create .rel.dyn section.
  9632. target->rel_dyn_section(layout);
  9633. target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
  9634. data_shndx, output_section, r_offset);
  9635. }
  9636. else
  9637. gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
  9638. gsym->name());
  9639. }
  9640. }
  9641. bool for_call = false;
  9642. switch (r_type)
  9643. {
  9644. case elfcpp::R_MIPS_CALL16:
  9645. case elfcpp::R_MIPS16_CALL16:
  9646. case elfcpp::R_MICROMIPS_CALL16:
  9647. case elfcpp::R_MIPS_CALL_HI16:
  9648. case elfcpp::R_MIPS_CALL_LO16:
  9649. case elfcpp::R_MICROMIPS_CALL_HI16:
  9650. case elfcpp::R_MICROMIPS_CALL_LO16:
  9651. for_call = true;
  9652. // Fall through.
  9653. case elfcpp::R_MIPS16_GOT16:
  9654. case elfcpp::R_MIPS_GOT16:
  9655. case elfcpp::R_MIPS_GOT_HI16:
  9656. case elfcpp::R_MIPS_GOT_LO16:
  9657. case elfcpp::R_MICROMIPS_GOT16:
  9658. case elfcpp::R_MICROMIPS_GOT_HI16:
  9659. case elfcpp::R_MICROMIPS_GOT_LO16:
  9660. case elfcpp::R_MIPS_GOT_DISP:
  9661. case elfcpp::R_MICROMIPS_GOT_DISP:
  9662. case elfcpp::R_MIPS_EH:
  9663. {
  9664. // The symbol requires a GOT entry.
  9665. Mips_output_data_got<size, big_endian>* got =
  9666. target->got_section(symtab, layout);
  9667. got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
  9668. for_call);
  9669. mips_sym->set_global_got_area(GGA_NORMAL);
  9670. }
  9671. break;
  9672. case elfcpp::R_MIPS_GOT_PAGE:
  9673. case elfcpp::R_MICROMIPS_GOT_PAGE:
  9674. {
  9675. // This relocation needs a page entry in the GOT.
  9676. // Get the section contents.
  9677. section_size_type view_size = 0;
  9678. const unsigned char* view =
  9679. object->section_contents(data_shndx, &view_size, false);
  9680. view += r_offset;
  9681. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  9682. Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
  9683. : r_addend);
  9684. Mips_output_data_got<size, big_endian>* got =
  9685. target->got_section(symtab, layout);
  9686. got->record_got_page_entry(mips_obj, r_sym, addend);
  9687. // If this is a global, overridable symbol, GOT_PAGE will
  9688. // decay to GOT_DISP, so we'll need a GOT entry for it.
  9689. bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
  9690. && !mips_sym->object()->is_dynamic()
  9691. && !mips_sym->is_undefined());
  9692. if (!def_regular
  9693. || (parameters->options().output_is_position_independent()
  9694. && !parameters->options().Bsymbolic()
  9695. && !mips_sym->is_forced_local()))
  9696. {
  9697. got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
  9698. for_call);
  9699. mips_sym->set_global_got_area(GGA_NORMAL);
  9700. }
  9701. }
  9702. break;
  9703. case elfcpp::R_MIPS_TLS_GOTTPREL:
  9704. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  9705. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  9706. case elfcpp::R_MIPS_TLS_LDM:
  9707. case elfcpp::R_MIPS16_TLS_LDM:
  9708. case elfcpp::R_MICROMIPS_TLS_LDM:
  9709. case elfcpp::R_MIPS_TLS_GD:
  9710. case elfcpp::R_MIPS16_TLS_GD:
  9711. case elfcpp::R_MICROMIPS_TLS_GD:
  9712. {
  9713. const bool is_final = gsym->final_value_is_known();
  9714. const tls::Tls_optimization optimized_type =
  9715. Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
  9716. switch (r_type)
  9717. {
  9718. case elfcpp::R_MIPS_TLS_GD:
  9719. case elfcpp::R_MIPS16_TLS_GD:
  9720. case elfcpp::R_MICROMIPS_TLS_GD:
  9721. if (optimized_type == tls::TLSOPT_NONE)
  9722. {
  9723. // Create a pair of GOT entries for the module index and
  9724. // dtv-relative offset.
  9725. Mips_output_data_got<size, big_endian>* got =
  9726. target->got_section(symtab, layout);
  9727. got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
  9728. false);
  9729. }
  9730. else
  9731. {
  9732. // FIXME: TLS optimization not supported yet.
  9733. gold_unreachable();
  9734. }
  9735. break;
  9736. case elfcpp::R_MIPS_TLS_LDM:
  9737. case elfcpp::R_MIPS16_TLS_LDM:
  9738. case elfcpp::R_MICROMIPS_TLS_LDM:
  9739. if (optimized_type == tls::TLSOPT_NONE)
  9740. {
  9741. // We always record LDM symbols as local with index 0.
  9742. target->got_section()->record_local_got_symbol(mips_obj, 0,
  9743. r_addend, r_type,
  9744. -1U, false);
  9745. }
  9746. else
  9747. {
  9748. // FIXME: TLS optimization not supported yet.
  9749. gold_unreachable();
  9750. }
  9751. break;
  9752. case elfcpp::R_MIPS_TLS_GOTTPREL:
  9753. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  9754. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  9755. layout->set_has_static_tls();
  9756. if (optimized_type == tls::TLSOPT_NONE)
  9757. {
  9758. // Create a GOT entry for the tp-relative offset.
  9759. Mips_output_data_got<size, big_endian>* got =
  9760. target->got_section(symtab, layout);
  9761. got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
  9762. false);
  9763. }
  9764. else
  9765. {
  9766. // FIXME: TLS optimization not supported yet.
  9767. gold_unreachable();
  9768. }
  9769. break;
  9770. default:
  9771. gold_unreachable();
  9772. }
  9773. }
  9774. break;
  9775. case elfcpp::R_MIPS_COPY:
  9776. case elfcpp::R_MIPS_JUMP_SLOT:
  9777. // These are relocations which should only be seen by the
  9778. // dynamic linker, and should never be seen here.
  9779. gold_error(_("%s: unexpected reloc %u in object file"),
  9780. object->name().c_str(), r_type);
  9781. break;
  9782. default:
  9783. break;
  9784. }
  9785. // Refuse some position-dependent relocations when creating a
  9786. // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
  9787. // not PIC, but we can create dynamic relocations and the result
  9788. // will be fine. Also do not refuse R_MIPS_LO16, which can be
  9789. // combined with R_MIPS_GOT16.
  9790. if (parameters->options().shared())
  9791. {
  9792. switch (r_type)
  9793. {
  9794. case elfcpp::R_MIPS16_HI16:
  9795. case elfcpp::R_MIPS_HI16:
  9796. case elfcpp::R_MIPS_HIGHER:
  9797. case elfcpp::R_MIPS_HIGHEST:
  9798. case elfcpp::R_MICROMIPS_HI16:
  9799. case elfcpp::R_MICROMIPS_HIGHER:
  9800. case elfcpp::R_MICROMIPS_HIGHEST:
  9801. // Don't refuse a high part relocation if it's against
  9802. // no symbol (e.g. part of a compound relocation).
  9803. if (r_sym == 0)
  9804. break;
  9805. // R_MIPS_HI16 against _gp_disp is used for $gp setup,
  9806. // and has a special meaning.
  9807. if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
  9808. break;
  9809. // Fall through.
  9810. case elfcpp::R_MIPS16_26:
  9811. case elfcpp::R_MIPS_26:
  9812. case elfcpp::R_MICROMIPS_26_S1:
  9813. gold_error(_("%s: relocation %u against `%s' can not be used when "
  9814. "making a shared object; recompile with -fPIC"),
  9815. object->name().c_str(), r_type, gsym->name());
  9816. default:
  9817. break;
  9818. }
  9819. }
  9820. }
  9821. template<int size, bool big_endian>
  9822. inline void
  9823. Target_mips<size, big_endian>::Scan::global(
  9824. Symbol_table* symtab,
  9825. Layout* layout,
  9826. Target_mips<size, big_endian>* target,
  9827. Sized_relobj_file<size, big_endian>* object,
  9828. unsigned int data_shndx,
  9829. Output_section* output_section,
  9830. const Relatype& reloc,
  9831. unsigned int r_type,
  9832. Symbol* gsym)
  9833. {
  9834. global(
  9835. symtab,
  9836. layout,
  9837. target,
  9838. object,
  9839. data_shndx,
  9840. output_section,
  9841. &reloc,
  9842. (const Reltype*) NULL,
  9843. elfcpp::SHT_RELA,
  9844. r_type,
  9845. gsym);
  9846. }
  9847. template<int size, bool big_endian>
  9848. inline void
  9849. Target_mips<size, big_endian>::Scan::global(
  9850. Symbol_table* symtab,
  9851. Layout* layout,
  9852. Target_mips<size, big_endian>* target,
  9853. Sized_relobj_file<size, big_endian>* object,
  9854. unsigned int data_shndx,
  9855. Output_section* output_section,
  9856. const Reltype& reloc,
  9857. unsigned int r_type,
  9858. Symbol* gsym)
  9859. {
  9860. global(
  9861. symtab,
  9862. layout,
  9863. target,
  9864. object,
  9865. data_shndx,
  9866. output_section,
  9867. (const Relatype*) NULL,
  9868. &reloc,
  9869. elfcpp::SHT_REL,
  9870. r_type,
  9871. gsym);
  9872. }
  9873. // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
  9874. // In cases where Scan::local() or Scan::global() has created
  9875. // a dynamic relocation, the addend of the relocation is carried
  9876. // in the data, and we must not apply the static relocation.
  9877. template<int size, bool big_endian>
  9878. inline bool
  9879. Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
  9880. const Mips_symbol<size>* gsym,
  9881. unsigned int r_type,
  9882. Output_section* output_section,
  9883. Target_mips* target)
  9884. {
  9885. // If the output section is not allocated, then we didn't call
  9886. // scan_relocs, we didn't create a dynamic reloc, and we must apply
  9887. // the reloc here.
  9888. if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
  9889. return true;
  9890. if (gsym == NULL)
  9891. return true;
  9892. else
  9893. {
  9894. // For global symbols, we use the same helper routines used in the
  9895. // scan pass.
  9896. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
  9897. && !gsym->may_need_copy_reloc())
  9898. {
  9899. // We have generated dynamic reloc (R_MIPS_REL32).
  9900. bool multi_got = false;
  9901. if (target->has_got_section())
  9902. multi_got = target->got_section()->multi_got();
  9903. bool has_got_offset;
  9904. if (!multi_got)
  9905. has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
  9906. else
  9907. has_got_offset = gsym->global_gotoffset() != -1U;
  9908. if (!has_got_offset)
  9909. return true;
  9910. else
  9911. // Apply the relocation only if the symbol is in the local got.
  9912. // Do not apply the relocation if the symbol is in the global
  9913. // got.
  9914. return symbol_references_local(gsym, gsym->has_dynsym_index());
  9915. }
  9916. else
  9917. // We have not generated dynamic reloc.
  9918. return true;
  9919. }
  9920. }
  9921. // Perform a relocation.
  9922. template<int size, bool big_endian>
  9923. inline bool
  9924. Target_mips<size, big_endian>::Relocate::relocate(
  9925. const Relocate_info<size, big_endian>* relinfo,
  9926. unsigned int rel_type,
  9927. Target_mips* target,
  9928. Output_section* output_section,
  9929. size_t relnum,
  9930. const unsigned char* preloc,
  9931. const Sized_symbol<size>* gsym,
  9932. const Symbol_value<size>* psymval,
  9933. unsigned char* view,
  9934. Mips_address address,
  9935. section_size_type)
  9936. {
  9937. Mips_address r_offset;
  9938. unsigned int r_sym;
  9939. unsigned int r_type;
  9940. unsigned int r_type2;
  9941. unsigned int r_type3;
  9942. unsigned char r_ssym;
  9943. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
  9944. // r_offset and r_type of the next relocation is needed for resolving multiple
  9945. // consecutive relocations with the same offset.
  9946. Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
  9947. unsigned int next_r_type = elfcpp::R_MIPS_NONE;
  9948. elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
  9949. size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
  9950. if (rel_type == elfcpp::SHT_RELA)
  9951. {
  9952. const Relatype rela(preloc);
  9953. r_offset = rela.get_r_offset();
  9954. r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
  9955. get_r_sym(&rela);
  9956. r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
  9957. get_r_type(&rela);
  9958. r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
  9959. get_r_type2(&rela);
  9960. r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
  9961. get_r_type3(&rela);
  9962. r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
  9963. get_r_ssym(&rela);
  9964. r_addend = rela.get_r_addend();
  9965. // If this is not last relocation, get r_offset and r_type of the next
  9966. // relocation.
  9967. if (relnum + 1 < reloc_count)
  9968. {
  9969. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  9970. const Relatype next_rela(preloc + reloc_size);
  9971. next_r_offset = next_rela.get_r_offset();
  9972. next_r_type =
  9973. Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
  9974. get_r_type(&next_rela);
  9975. }
  9976. }
  9977. else
  9978. {
  9979. const Reltype rel(preloc);
  9980. r_offset = rel.get_r_offset();
  9981. r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
  9982. get_r_sym(&rel);
  9983. r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
  9984. get_r_type(&rel);
  9985. r_ssym = 0;
  9986. r_type2 = elfcpp::R_MIPS_NONE;
  9987. r_type3 = elfcpp::R_MIPS_NONE;
  9988. r_addend = 0;
  9989. // If this is not last relocation, get r_offset and r_type of the next
  9990. // relocation.
  9991. if (relnum + 1 < reloc_count)
  9992. {
  9993. const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
  9994. const Reltype next_rel(preloc + reloc_size);
  9995. next_r_offset = next_rel.get_r_offset();
  9996. next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
  9997. get_r_type(&next_rel);
  9998. }
  9999. }
  10000. typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
  10001. typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
  10002. Mips_relobj<size, big_endian>* object =
  10003. Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
  10004. bool target_is_16_bit_code = false;
  10005. bool target_is_micromips_code = false;
  10006. bool cross_mode_jump;
  10007. Symbol_value<size> symval;
  10008. const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
  10009. bool changed_symbol_value = false;
  10010. if (gsym == NULL)
  10011. {
  10012. target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
  10013. target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
  10014. if (target_is_16_bit_code || target_is_micromips_code)
  10015. {
  10016. // MIPS16/microMIPS text labels should be treated as odd.
  10017. symval.set_output_value(psymval->value(object, 1));
  10018. psymval = &symval;
  10019. changed_symbol_value = true;
  10020. }
  10021. }
  10022. else
  10023. {
  10024. target_is_16_bit_code = mips_sym->is_mips16();
  10025. target_is_micromips_code = mips_sym->is_micromips();
  10026. // If this is a mips16/microMIPS text symbol, add 1 to the value to make
  10027. // it odd. This will cause something like .word SYM to come up with
  10028. // the right value when it is loaded into the PC.
  10029. if ((mips_sym->is_mips16() || mips_sym->is_micromips())
  10030. && psymval->value(object, 0) != 0)
  10031. {
  10032. symval.set_output_value(psymval->value(object, 0) | 1);
  10033. psymval = &symval;
  10034. changed_symbol_value = true;
  10035. }
  10036. // Pick the value to use for symbols defined in shared objects.
  10037. if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
  10038. || mips_sym->has_lazy_stub())
  10039. {
  10040. Mips_address value;
  10041. if (!mips_sym->has_lazy_stub())
  10042. {
  10043. // Prefer a standard MIPS PLT entry.
  10044. if (mips_sym->has_mips_plt_offset())
  10045. {
  10046. value = target->plt_section()->mips_entry_address(mips_sym);
  10047. target_is_micromips_code = false;
  10048. target_is_16_bit_code = false;
  10049. }
  10050. else
  10051. {
  10052. value = (target->plt_section()->comp_entry_address(mips_sym)
  10053. + 1);
  10054. if (target->is_output_micromips())
  10055. target_is_micromips_code = true;
  10056. else
  10057. target_is_16_bit_code = true;
  10058. }
  10059. }
  10060. else
  10061. value = target->mips_stubs_section()->stub_address(mips_sym);
  10062. symval.set_output_value(value);
  10063. psymval = &symval;
  10064. }
  10065. }
  10066. // TRUE if the symbol referred to by this relocation is "_gp_disp".
  10067. // Note that such a symbol must always be a global symbol.
  10068. bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
  10069. && !object->is_newabi());
  10070. // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
  10071. // Note that such a symbol must always be a global symbol.
  10072. bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
  10073. if (gp_disp)
  10074. {
  10075. if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
  10076. gold_error_at_location(relinfo, relnum, r_offset,
  10077. _("relocations against _gp_disp are permitted only"
  10078. " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
  10079. }
  10080. else if (gnu_local_gp)
  10081. {
  10082. // __gnu_local_gp is _gp symbol.
  10083. symval.set_output_value(target->adjusted_gp_value(object));
  10084. psymval = &symval;
  10085. }
  10086. // If this is a reference to a 16-bit function with a stub, we need
  10087. // to redirect the relocation to the stub unless:
  10088. //
  10089. // (a) the relocation is for a MIPS16 JAL;
  10090. //
  10091. // (b) the relocation is for a MIPS16 PIC call, and there are no
  10092. // non-MIPS16 uses of the GOT slot; or
  10093. //
  10094. // (c) the section allows direct references to MIPS16 functions.
  10095. if (r_type != elfcpp::R_MIPS16_26
  10096. && ((mips_sym != NULL
  10097. && mips_sym->has_mips16_fn_stub()
  10098. && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
  10099. || (mips_sym == NULL
  10100. && object->get_local_mips16_fn_stub(r_sym) != NULL))
  10101. && !object->section_allows_mips16_refs(relinfo->data_shndx))
  10102. {
  10103. // This is a 32- or 64-bit call to a 16-bit function. We should
  10104. // have already noticed that we were going to need the
  10105. // stub.
  10106. Mips_address value;
  10107. if (mips_sym == NULL)
  10108. value = object->get_local_mips16_fn_stub(r_sym)->output_address();
  10109. else
  10110. {
  10111. gold_assert(mips_sym->need_fn_stub());
  10112. if (mips_sym->has_la25_stub())
  10113. value = target->la25_stub_section()->stub_address(mips_sym);
  10114. else
  10115. {
  10116. value = mips_sym->template
  10117. get_mips16_fn_stub<big_endian>()->output_address();
  10118. }
  10119. }
  10120. symval.set_output_value(value);
  10121. psymval = &symval;
  10122. changed_symbol_value = true;
  10123. // The target is 16-bit, but the stub isn't.
  10124. target_is_16_bit_code = false;
  10125. }
  10126. // If this is a MIPS16 call with a stub, that is made through the PLT or
  10127. // to a standard MIPS function, we need to redirect the call to the stub.
  10128. // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
  10129. // indirect calls should use an indirect stub instead.
  10130. else if (r_type == elfcpp::R_MIPS16_26
  10131. && ((mips_sym != NULL
  10132. && (mips_sym->has_mips16_call_stub()
  10133. || mips_sym->has_mips16_call_fp_stub()))
  10134. || (mips_sym == NULL
  10135. && object->get_local_mips16_call_stub(r_sym) != NULL))
  10136. && ((mips_sym != NULL && mips_sym->has_plt_offset())
  10137. || !target_is_16_bit_code))
  10138. {
  10139. Mips16_stub_section<size, big_endian>* call_stub;
  10140. if (mips_sym == NULL)
  10141. call_stub = object->get_local_mips16_call_stub(r_sym);
  10142. else
  10143. {
  10144. // If both call_stub and call_fp_stub are defined, we can figure
  10145. // out which one to use by checking which one appears in the input
  10146. // file.
  10147. if (mips_sym->has_mips16_call_stub()
  10148. && mips_sym->has_mips16_call_fp_stub())
  10149. {
  10150. call_stub = NULL;
  10151. for (unsigned int i = 1; i < object->shnum(); ++i)
  10152. {
  10153. if (object->is_mips16_call_fp_stub_section(i))
  10154. {
  10155. call_stub = mips_sym->template
  10156. get_mips16_call_fp_stub<big_endian>();
  10157. break;
  10158. }
  10159. }
  10160. if (call_stub == NULL)
  10161. call_stub =
  10162. mips_sym->template get_mips16_call_stub<big_endian>();
  10163. }
  10164. else if (mips_sym->has_mips16_call_stub())
  10165. call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
  10166. else
  10167. call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
  10168. }
  10169. symval.set_output_value(call_stub->output_address());
  10170. psymval = &symval;
  10171. changed_symbol_value = true;
  10172. }
  10173. // If this is a direct call to a PIC function, redirect to the
  10174. // non-PIC stub.
  10175. else if (mips_sym != NULL
  10176. && mips_sym->has_la25_stub()
  10177. && relocation_needs_la25_stub<size, big_endian>(
  10178. object, r_type, target_is_16_bit_code))
  10179. {
  10180. Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
  10181. if (mips_sym->is_micromips())
  10182. value += 1;
  10183. symval.set_output_value(value);
  10184. psymval = &symval;
  10185. }
  10186. // For direct MIPS16 and microMIPS calls make sure the compressed PLT
  10187. // entry is used if a standard PLT entry has also been made.
  10188. else if ((r_type == elfcpp::R_MIPS16_26
  10189. || r_type == elfcpp::R_MICROMIPS_26_S1)
  10190. && mips_sym != NULL
  10191. && mips_sym->has_plt_offset()
  10192. && mips_sym->has_comp_plt_offset()
  10193. && mips_sym->has_mips_plt_offset())
  10194. {
  10195. Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
  10196. + 1);
  10197. symval.set_output_value(value);
  10198. psymval = &symval;
  10199. target_is_16_bit_code = !target->is_output_micromips();
  10200. target_is_micromips_code = target->is_output_micromips();
  10201. }
  10202. // Make sure MIPS16 and microMIPS are not used together.
  10203. if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
  10204. || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
  10205. {
  10206. gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
  10207. }
  10208. // Calls from 16-bit code to 32-bit code and vice versa require the
  10209. // mode change. However, we can ignore calls to undefined weak symbols,
  10210. // which should never be executed at runtime. This exception is important
  10211. // because the assembly writer may have "known" that any definition of the
  10212. // symbol would be 16-bit code, and that direct jumps were therefore
  10213. // acceptable.
  10214. cross_mode_jump =
  10215. (!(gsym != NULL && gsym->is_weak_undefined())
  10216. && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
  10217. || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
  10218. || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
  10219. && (target_is_16_bit_code || target_is_micromips_code))));
  10220. bool local = (mips_sym == NULL
  10221. || (mips_sym->got_only_for_calls()
  10222. ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
  10223. : symbol_references_local(mips_sym,
  10224. mips_sym->has_dynsym_index())));
  10225. // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
  10226. // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
  10227. // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
  10228. if (got_page_reloc(r_type) && !local)
  10229. r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
  10230. : elfcpp::R_MIPS_GOT_DISP);
  10231. unsigned int got_offset = 0;
  10232. int gp_offset = 0;
  10233. // Whether we have to extract addend from instruction.
  10234. bool extract_addend = rel_type == elfcpp::SHT_REL;
  10235. unsigned int r_types[3] = { r_type, r_type2, r_type3 };
  10236. Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
  10237. // For Mips64 N64 ABI, there may be up to three operations specified per
  10238. // record, by the fields r_type, r_type2, and r_type3. The first operation
  10239. // takes its addend from the relocation record. Each subsequent operation
  10240. // takes as its addend the result of the previous operation.
  10241. // The first operation in a record which references a symbol uses the symbol
  10242. // implied by r_sym. The next operation in a record which references a symbol
  10243. // uses the special symbol value given by the r_ssym field. A third operation
  10244. // in a record which references a symbol will assume a NULL symbol,
  10245. // i.e. value zero.
  10246. // TODO(Vladimir)
  10247. // Check if a record references to a symbol.
  10248. for (unsigned int i = 0; i < 3; ++i)
  10249. {
  10250. if (r_types[i] == elfcpp::R_MIPS_NONE)
  10251. break;
  10252. // If we didn't apply previous relocation, use its result as addend
  10253. // for current.
  10254. if (this->calculate_only_)
  10255. {
  10256. r_addend = this->calculated_value_;
  10257. extract_addend = false;
  10258. }
  10259. // In the N32 and 64-bit ABIs there may be multiple consecutive
  10260. // relocations for the same offset. In that case we are
  10261. // supposed to treat the output of each relocation as the addend
  10262. // for the next. For N64 ABI, we are checking offsets only in a
  10263. // third operation in a record (r_type3).
  10264. this->calculate_only_ =
  10265. (object->is_n64() && i < 2
  10266. ? r_types[i+1] != elfcpp::R_MIPS_NONE
  10267. : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
  10268. if (object->is_n64())
  10269. {
  10270. if (i == 1)
  10271. {
  10272. // Handle special symbol for r_type2 relocation type.
  10273. switch (r_ssym)
  10274. {
  10275. case RSS_UNDEF:
  10276. symval.set_output_value(0);
  10277. break;
  10278. case RSS_GP:
  10279. symval.set_output_value(target->gp_value());
  10280. break;
  10281. case RSS_GP0:
  10282. symval.set_output_value(object->gp_value());
  10283. break;
  10284. case RSS_LOC:
  10285. symval.set_output_value(address);
  10286. break;
  10287. default:
  10288. gold_unreachable();
  10289. }
  10290. psymval = &symval;
  10291. }
  10292. else if (i == 2)
  10293. {
  10294. // For r_type3 symbol value is 0.
  10295. symval.set_output_value(0);
  10296. }
  10297. }
  10298. bool update_got_entry = false;
  10299. switch (r_types[i])
  10300. {
  10301. case elfcpp::R_MIPS_NONE:
  10302. break;
  10303. case elfcpp::R_MIPS_16:
  10304. reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
  10305. extract_addend,
  10306. this->calculate_only_,
  10307. &this->calculated_value_);
  10308. break;
  10309. case elfcpp::R_MIPS_32:
  10310. if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
  10311. target))
  10312. reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
  10313. extract_addend,
  10314. this->calculate_only_,
  10315. &this->calculated_value_);
  10316. if (mips_sym != NULL
  10317. && (mips_sym->is_mips16() || mips_sym->is_micromips())
  10318. && mips_sym->global_got_area() == GGA_RELOC_ONLY)
  10319. {
  10320. // If mips_sym->has_mips16_fn_stub() is false, symbol value is
  10321. // already updated by adding +1.
  10322. if (mips_sym->has_mips16_fn_stub())
  10323. {
  10324. gold_assert(mips_sym->need_fn_stub());
  10325. Mips16_stub_section<size, big_endian>* fn_stub =
  10326. mips_sym->template get_mips16_fn_stub<big_endian>();
  10327. symval.set_output_value(fn_stub->output_address());
  10328. psymval = &symval;
  10329. }
  10330. got_offset = mips_sym->global_gotoffset();
  10331. update_got_entry = true;
  10332. }
  10333. break;
  10334. case elfcpp::R_MIPS_64:
  10335. if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
  10336. target))
  10337. reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
  10338. extract_addend,
  10339. this->calculate_only_,
  10340. &this->calculated_value_, false);
  10341. else if (target->is_output_n64() && r_addend != 0)
  10342. // Only apply the addend. The static relocation was RELA, but the
  10343. // dynamic relocation is REL, so we need to apply the addend.
  10344. reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
  10345. extract_addend,
  10346. this->calculate_only_,
  10347. &this->calculated_value_, true);
  10348. break;
  10349. case elfcpp::R_MIPS_REL32:
  10350. gold_unreachable();
  10351. case elfcpp::R_MIPS_PC32:
  10352. reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
  10353. r_addend, extract_addend,
  10354. this->calculate_only_,
  10355. &this->calculated_value_);
  10356. break;
  10357. case elfcpp::R_MIPS16_26:
  10358. // The calculation for R_MIPS16_26 is just the same as for an
  10359. // R_MIPS_26. It's only the storage of the relocated field into
  10360. // the output file that's different. So, we just fall through to the
  10361. // R_MIPS_26 case here.
  10362. case elfcpp::R_MIPS_26:
  10363. case elfcpp::R_MICROMIPS_26_S1:
  10364. reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
  10365. gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
  10366. r_types[i], target->jal_to_bal(), this->calculate_only_,
  10367. &this->calculated_value_);
  10368. break;
  10369. case elfcpp::R_MIPS_HI16:
  10370. case elfcpp::R_MIPS16_HI16:
  10371. case elfcpp::R_MICROMIPS_HI16:
  10372. if (rel_type == elfcpp::SHT_RELA)
  10373. reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
  10374. r_addend, address,
  10375. gp_disp, r_types[i],
  10376. extract_addend, 0,
  10377. target,
  10378. this->calculate_only_,
  10379. &this->calculated_value_);
  10380. else if (rel_type == elfcpp::SHT_REL)
  10381. reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
  10382. address, gp_disp, r_types[i],
  10383. r_sym, extract_addend);
  10384. else
  10385. gold_unreachable();
  10386. break;
  10387. case elfcpp::R_MIPS_LO16:
  10388. case elfcpp::R_MIPS16_LO16:
  10389. case elfcpp::R_MICROMIPS_LO16:
  10390. case elfcpp::R_MICROMIPS_HI0_LO16:
  10391. reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
  10392. r_addend, extract_addend, address,
  10393. gp_disp, r_types[i], r_sym,
  10394. rel_type, this->calculate_only_,
  10395. &this->calculated_value_);
  10396. break;
  10397. case elfcpp::R_MIPS_LITERAL:
  10398. case elfcpp::R_MICROMIPS_LITERAL:
  10399. // Because we don't merge literal sections, we can handle this
  10400. // just like R_MIPS_GPREL16. In the long run, we should merge
  10401. // shared literals, and then we will need to additional work
  10402. // here.
  10403. // Fall through.
  10404. case elfcpp::R_MIPS_GPREL16:
  10405. case elfcpp::R_MIPS16_GPREL:
  10406. case elfcpp::R_MICROMIPS_GPREL7_S2:
  10407. case elfcpp::R_MICROMIPS_GPREL16:
  10408. reloc_status = Reloc_funcs::relgprel(view, object, psymval,
  10409. target->adjusted_gp_value(object),
  10410. r_addend, extract_addend,
  10411. gsym == NULL, r_types[i],
  10412. this->calculate_only_,
  10413. &this->calculated_value_);
  10414. break;
  10415. case elfcpp::R_MIPS_PC16:
  10416. reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
  10417. r_addend, extract_addend,
  10418. this->calculate_only_,
  10419. &this->calculated_value_);
  10420. break;
  10421. case elfcpp::R_MIPS_PC21_S2:
  10422. reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
  10423. r_addend, extract_addend,
  10424. this->calculate_only_,
  10425. &this->calculated_value_);
  10426. break;
  10427. case elfcpp::R_MIPS_PC26_S2:
  10428. reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
  10429. r_addend, extract_addend,
  10430. this->calculate_only_,
  10431. &this->calculated_value_);
  10432. break;
  10433. case elfcpp::R_MIPS_PC18_S3:
  10434. reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
  10435. r_addend, extract_addend,
  10436. this->calculate_only_,
  10437. &this->calculated_value_);
  10438. break;
  10439. case elfcpp::R_MIPS_PC19_S2:
  10440. reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
  10441. r_addend, extract_addend,
  10442. this->calculate_only_,
  10443. &this->calculated_value_);
  10444. break;
  10445. case elfcpp::R_MIPS_PCHI16:
  10446. if (rel_type == elfcpp::SHT_RELA)
  10447. reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
  10448. r_addend, address,
  10449. extract_addend, 0,
  10450. this->calculate_only_,
  10451. &this->calculated_value_);
  10452. else if (rel_type == elfcpp::SHT_REL)
  10453. reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
  10454. r_addend, address, r_sym,
  10455. extract_addend);
  10456. else
  10457. gold_unreachable();
  10458. break;
  10459. case elfcpp::R_MIPS_PCLO16:
  10460. reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
  10461. extract_addend, address, r_sym,
  10462. rel_type, this->calculate_only_,
  10463. &this->calculated_value_);
  10464. break;
  10465. case elfcpp::R_MICROMIPS_PC7_S1:
  10466. reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
  10467. address, r_addend,
  10468. extract_addend,
  10469. this->calculate_only_,
  10470. &this->calculated_value_);
  10471. break;
  10472. case elfcpp::R_MICROMIPS_PC10_S1:
  10473. reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
  10474. psymval, address,
  10475. r_addend, extract_addend,
  10476. this->calculate_only_,
  10477. &this->calculated_value_);
  10478. break;
  10479. case elfcpp::R_MICROMIPS_PC16_S1:
  10480. reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
  10481. psymval, address,
  10482. r_addend, extract_addend,
  10483. this->calculate_only_,
  10484. &this->calculated_value_);
  10485. break;
  10486. case elfcpp::R_MIPS_GPREL32:
  10487. reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
  10488. target->adjusted_gp_value(object),
  10489. r_addend, extract_addend,
  10490. this->calculate_only_,
  10491. &this->calculated_value_);
  10492. break;
  10493. case elfcpp::R_MIPS_GOT_HI16:
  10494. case elfcpp::R_MIPS_CALL_HI16:
  10495. case elfcpp::R_MICROMIPS_GOT_HI16:
  10496. case elfcpp::R_MICROMIPS_CALL_HI16:
  10497. if (gsym != NULL)
  10498. got_offset = target->got_section()->got_offset(gsym,
  10499. GOT_TYPE_STANDARD,
  10500. object);
  10501. else
  10502. got_offset = target->got_section()->got_offset(r_sym,
  10503. GOT_TYPE_STANDARD,
  10504. object, r_addend);
  10505. gp_offset = target->got_section()->gp_offset(got_offset, object);
  10506. reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
  10507. this->calculate_only_,
  10508. &this->calculated_value_);
  10509. update_got_entry = changed_symbol_value;
  10510. break;
  10511. case elfcpp::R_MIPS_GOT_LO16:
  10512. case elfcpp::R_MIPS_CALL_LO16:
  10513. case elfcpp::R_MICROMIPS_GOT_LO16:
  10514. case elfcpp::R_MICROMIPS_CALL_LO16:
  10515. if (gsym != NULL)
  10516. got_offset = target->got_section()->got_offset(gsym,
  10517. GOT_TYPE_STANDARD,
  10518. object);
  10519. else
  10520. got_offset = target->got_section()->got_offset(r_sym,
  10521. GOT_TYPE_STANDARD,
  10522. object, r_addend);
  10523. gp_offset = target->got_section()->gp_offset(got_offset, object);
  10524. reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
  10525. this->calculate_only_,
  10526. &this->calculated_value_);
  10527. update_got_entry = changed_symbol_value;
  10528. break;
  10529. case elfcpp::R_MIPS_GOT_DISP:
  10530. case elfcpp::R_MICROMIPS_GOT_DISP:
  10531. case elfcpp::R_MIPS_EH:
  10532. if (gsym != NULL)
  10533. got_offset = target->got_section()->got_offset(gsym,
  10534. GOT_TYPE_STANDARD,
  10535. object);
  10536. else
  10537. got_offset = target->got_section()->got_offset(r_sym,
  10538. GOT_TYPE_STANDARD,
  10539. object, r_addend);
  10540. gp_offset = target->got_section()->gp_offset(got_offset, object);
  10541. if (eh_reloc(r_types[i]))
  10542. reloc_status = Reloc_funcs::releh(view, gp_offset,
  10543. this->calculate_only_,
  10544. &this->calculated_value_);
  10545. else
  10546. reloc_status = Reloc_funcs::relgot(view, gp_offset,
  10547. this->calculate_only_,
  10548. &this->calculated_value_);
  10549. break;
  10550. case elfcpp::R_MIPS_CALL16:
  10551. case elfcpp::R_MIPS16_CALL16:
  10552. case elfcpp::R_MICROMIPS_CALL16:
  10553. gold_assert(gsym != NULL);
  10554. got_offset = target->got_section()->got_offset(gsym,
  10555. GOT_TYPE_STANDARD,
  10556. object);
  10557. gp_offset = target->got_section()->gp_offset(got_offset, object);
  10558. reloc_status = Reloc_funcs::relgot(view, gp_offset,
  10559. this->calculate_only_,
  10560. &this->calculated_value_);
  10561. // TODO(sasa): We should also initialize update_got_entry
  10562. // in other place swhere relgot is called.
  10563. update_got_entry = changed_symbol_value;
  10564. break;
  10565. case elfcpp::R_MIPS_GOT16:
  10566. case elfcpp::R_MIPS16_GOT16:
  10567. case elfcpp::R_MICROMIPS_GOT16:
  10568. if (gsym != NULL)
  10569. {
  10570. got_offset = target->got_section()->got_offset(gsym,
  10571. GOT_TYPE_STANDARD,
  10572. object);
  10573. gp_offset = target->got_section()->gp_offset(got_offset, object);
  10574. reloc_status = Reloc_funcs::relgot(view, gp_offset,
  10575. this->calculate_only_,
  10576. &this->calculated_value_);
  10577. }
  10578. else
  10579. {
  10580. if (rel_type == elfcpp::SHT_RELA)
  10581. reloc_status = Reloc_funcs::do_relgot16_local(view, object,
  10582. psymval, r_addend,
  10583. extract_addend, 0,
  10584. target,
  10585. this->calculate_only_,
  10586. &this->calculated_value_);
  10587. else if (rel_type == elfcpp::SHT_REL)
  10588. reloc_status = Reloc_funcs::relgot16_local(view, object,
  10589. psymval, r_addend,
  10590. extract_addend,
  10591. r_types[i], r_sym);
  10592. else
  10593. gold_unreachable();
  10594. }
  10595. update_got_entry = changed_symbol_value;
  10596. break;
  10597. case elfcpp::R_MIPS_TLS_GD:
  10598. case elfcpp::R_MIPS16_TLS_GD:
  10599. case elfcpp::R_MICROMIPS_TLS_GD:
  10600. if (gsym != NULL)
  10601. got_offset = target->got_section()->got_offset(gsym,
  10602. GOT_TYPE_TLS_PAIR,
  10603. object);
  10604. else
  10605. got_offset = target->got_section()->got_offset(r_sym,
  10606. GOT_TYPE_TLS_PAIR,
  10607. object, r_addend);
  10608. gp_offset = target->got_section()->gp_offset(got_offset, object);
  10609. reloc_status = Reloc_funcs::relgot(view, gp_offset,
  10610. this->calculate_only_,
  10611. &this->calculated_value_);
  10612. break;
  10613. case elfcpp::R_MIPS_TLS_GOTTPREL:
  10614. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  10615. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  10616. if (gsym != NULL)
  10617. got_offset = target->got_section()->got_offset(gsym,
  10618. GOT_TYPE_TLS_OFFSET,
  10619. object);
  10620. else
  10621. got_offset = target->got_section()->got_offset(r_sym,
  10622. GOT_TYPE_TLS_OFFSET,
  10623. object, r_addend);
  10624. gp_offset = target->got_section()->gp_offset(got_offset, object);
  10625. reloc_status = Reloc_funcs::relgot(view, gp_offset,
  10626. this->calculate_only_,
  10627. &this->calculated_value_);
  10628. break;
  10629. case elfcpp::R_MIPS_TLS_LDM:
  10630. case elfcpp::R_MIPS16_TLS_LDM:
  10631. case elfcpp::R_MICROMIPS_TLS_LDM:
  10632. // Relocate the field with the offset of the GOT entry for
  10633. // the module index.
  10634. got_offset = target->got_section()->tls_ldm_offset(object);
  10635. gp_offset = target->got_section()->gp_offset(got_offset, object);
  10636. reloc_status = Reloc_funcs::relgot(view, gp_offset,
  10637. this->calculate_only_,
  10638. &this->calculated_value_);
  10639. break;
  10640. case elfcpp::R_MIPS_GOT_PAGE:
  10641. case elfcpp::R_MICROMIPS_GOT_PAGE:
  10642. reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
  10643. r_addend, extract_addend,
  10644. this->calculate_only_,
  10645. &this->calculated_value_);
  10646. break;
  10647. case elfcpp::R_MIPS_GOT_OFST:
  10648. case elfcpp::R_MICROMIPS_GOT_OFST:
  10649. reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
  10650. r_addend, extract_addend,
  10651. local, this->calculate_only_,
  10652. &this->calculated_value_);
  10653. break;
  10654. case elfcpp::R_MIPS_JALR:
  10655. case elfcpp::R_MICROMIPS_JALR:
  10656. // This relocation is only a hint. In some cases, we optimize
  10657. // it into a bal instruction. But we don't try to optimize
  10658. // when the symbol does not resolve locally.
  10659. if (gsym == NULL
  10660. || symbol_calls_local(gsym, gsym->has_dynsym_index()))
  10661. reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
  10662. r_addend, extract_addend,
  10663. cross_mode_jump, r_types[i],
  10664. target->jalr_to_bal(),
  10665. target->jr_to_b(),
  10666. this->calculate_only_,
  10667. &this->calculated_value_);
  10668. break;
  10669. case elfcpp::R_MIPS_TLS_DTPREL_HI16:
  10670. case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
  10671. case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
  10672. reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
  10673. elfcpp::DTP_OFFSET, r_addend,
  10674. extract_addend,
  10675. this->calculate_only_,
  10676. &this->calculated_value_);
  10677. break;
  10678. case elfcpp::R_MIPS_TLS_DTPREL_LO16:
  10679. case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
  10680. case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
  10681. reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
  10682. elfcpp::DTP_OFFSET, r_addend,
  10683. extract_addend,
  10684. this->calculate_only_,
  10685. &this->calculated_value_);
  10686. break;
  10687. case elfcpp::R_MIPS_TLS_DTPREL32:
  10688. case elfcpp::R_MIPS_TLS_DTPREL64:
  10689. reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
  10690. elfcpp::DTP_OFFSET, r_addend,
  10691. extract_addend,
  10692. this->calculate_only_,
  10693. &this->calculated_value_);
  10694. break;
  10695. case elfcpp::R_MIPS_TLS_TPREL_HI16:
  10696. case elfcpp::R_MIPS16_TLS_TPREL_HI16:
  10697. case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
  10698. reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
  10699. elfcpp::TP_OFFSET, r_addend,
  10700. extract_addend,
  10701. this->calculate_only_,
  10702. &this->calculated_value_);
  10703. break;
  10704. case elfcpp::R_MIPS_TLS_TPREL_LO16:
  10705. case elfcpp::R_MIPS16_TLS_TPREL_LO16:
  10706. case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
  10707. reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
  10708. elfcpp::TP_OFFSET, r_addend,
  10709. extract_addend,
  10710. this->calculate_only_,
  10711. &this->calculated_value_);
  10712. break;
  10713. case elfcpp::R_MIPS_TLS_TPREL32:
  10714. case elfcpp::R_MIPS_TLS_TPREL64:
  10715. reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
  10716. elfcpp::TP_OFFSET, r_addend,
  10717. extract_addend,
  10718. this->calculate_only_,
  10719. &this->calculated_value_);
  10720. break;
  10721. case elfcpp::R_MIPS_SUB:
  10722. case elfcpp::R_MICROMIPS_SUB:
  10723. reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
  10724. extract_addend,
  10725. this->calculate_only_,
  10726. &this->calculated_value_);
  10727. break;
  10728. case elfcpp::R_MIPS_HIGHER:
  10729. case elfcpp::R_MICROMIPS_HIGHER:
  10730. reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
  10731. extract_addend,
  10732. this->calculate_only_,
  10733. &this->calculated_value_);
  10734. break;
  10735. case elfcpp::R_MIPS_HIGHEST:
  10736. case elfcpp::R_MICROMIPS_HIGHEST:
  10737. reloc_status = Reloc_funcs::relhighest(view, object, psymval,
  10738. r_addend, extract_addend,
  10739. this->calculate_only_,
  10740. &this->calculated_value_);
  10741. break;
  10742. default:
  10743. gold_error_at_location(relinfo, relnum, r_offset,
  10744. _("unsupported reloc %u"), r_types[i]);
  10745. break;
  10746. }
  10747. if (update_got_entry)
  10748. {
  10749. Mips_output_data_got<size, big_endian>* got = target->got_section();
  10750. if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
  10751. got->update_got_entry(got->get_primary_got_offset(mips_sym),
  10752. psymval->value(object, 0));
  10753. else
  10754. got->update_got_entry(got_offset, psymval->value(object, 0));
  10755. }
  10756. }
  10757. bool jal_shuffle = jal_reloc(r_type);
  10758. Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
  10759. // Report any errors.
  10760. switch (reloc_status)
  10761. {
  10762. case Reloc_funcs::STATUS_OKAY:
  10763. break;
  10764. case Reloc_funcs::STATUS_OVERFLOW:
  10765. if (gsym == NULL)
  10766. gold_error_at_location(relinfo, relnum, r_offset,
  10767. _("relocation overflow: "
  10768. "%u against local symbol %u in %s"),
  10769. r_type, r_sym, object->name().c_str());
  10770. else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
  10771. gold_error_at_location(relinfo, relnum, r_offset,
  10772. _("relocation overflow: "
  10773. "%u against '%s' defined in %s"),
  10774. r_type, gsym->demangled_name().c_str(),
  10775. gsym->object()->name().c_str());
  10776. else
  10777. gold_error_at_location(relinfo, relnum, r_offset,
  10778. _("relocation overflow: %u against '%s'"),
  10779. r_type, gsym->demangled_name().c_str());
  10780. break;
  10781. case Reloc_funcs::STATUS_BAD_RELOC:
  10782. gold_error_at_location(relinfo, relnum, r_offset,
  10783. _("unexpected opcode while processing relocation"));
  10784. break;
  10785. case Reloc_funcs::STATUS_PCREL_UNALIGNED:
  10786. gold_error_at_location(relinfo, relnum, r_offset,
  10787. _("unaligned PC-relative relocation"));
  10788. break;
  10789. default:
  10790. gold_unreachable();
  10791. }
  10792. return true;
  10793. }
  10794. // Get the Reference_flags for a particular relocation.
  10795. template<int size, bool big_endian>
  10796. int
  10797. Target_mips<size, big_endian>::Scan::get_reference_flags(
  10798. unsigned int r_type)
  10799. {
  10800. switch (r_type)
  10801. {
  10802. case elfcpp::R_MIPS_NONE:
  10803. // No symbol reference.
  10804. return 0;
  10805. case elfcpp::R_MIPS_16:
  10806. case elfcpp::R_MIPS_32:
  10807. case elfcpp::R_MIPS_64:
  10808. case elfcpp::R_MIPS_HI16:
  10809. case elfcpp::R_MIPS_LO16:
  10810. case elfcpp::R_MIPS_HIGHER:
  10811. case elfcpp::R_MIPS_HIGHEST:
  10812. case elfcpp::R_MIPS16_HI16:
  10813. case elfcpp::R_MIPS16_LO16:
  10814. case elfcpp::R_MICROMIPS_HI16:
  10815. case elfcpp::R_MICROMIPS_LO16:
  10816. case elfcpp::R_MICROMIPS_HIGHER:
  10817. case elfcpp::R_MICROMIPS_HIGHEST:
  10818. return Symbol::ABSOLUTE_REF;
  10819. case elfcpp::R_MIPS_26:
  10820. case elfcpp::R_MIPS16_26:
  10821. case elfcpp::R_MICROMIPS_26_S1:
  10822. return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
  10823. case elfcpp::R_MIPS_PC18_S3:
  10824. case elfcpp::R_MIPS_PC19_S2:
  10825. case elfcpp::R_MIPS_PCHI16:
  10826. case elfcpp::R_MIPS_PCLO16:
  10827. case elfcpp::R_MIPS_GPREL32:
  10828. case elfcpp::R_MIPS_GPREL16:
  10829. case elfcpp::R_MIPS_REL32:
  10830. case elfcpp::R_MIPS16_GPREL:
  10831. return Symbol::RELATIVE_REF;
  10832. case elfcpp::R_MIPS_PC16:
  10833. case elfcpp::R_MIPS_PC32:
  10834. case elfcpp::R_MIPS_PC21_S2:
  10835. case elfcpp::R_MIPS_PC26_S2:
  10836. case elfcpp::R_MIPS_JALR:
  10837. case elfcpp::R_MICROMIPS_JALR:
  10838. return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  10839. case elfcpp::R_MIPS_GOT16:
  10840. case elfcpp::R_MIPS_CALL16:
  10841. case elfcpp::R_MIPS_GOT_DISP:
  10842. case elfcpp::R_MIPS_GOT_HI16:
  10843. case elfcpp::R_MIPS_GOT_LO16:
  10844. case elfcpp::R_MIPS_CALL_HI16:
  10845. case elfcpp::R_MIPS_CALL_LO16:
  10846. case elfcpp::R_MIPS_LITERAL:
  10847. case elfcpp::R_MIPS_GOT_PAGE:
  10848. case elfcpp::R_MIPS_GOT_OFST:
  10849. case elfcpp::R_MIPS16_GOT16:
  10850. case elfcpp::R_MIPS16_CALL16:
  10851. case elfcpp::R_MICROMIPS_GOT16:
  10852. case elfcpp::R_MICROMIPS_CALL16:
  10853. case elfcpp::R_MICROMIPS_GOT_HI16:
  10854. case elfcpp::R_MICROMIPS_GOT_LO16:
  10855. case elfcpp::R_MICROMIPS_CALL_HI16:
  10856. case elfcpp::R_MICROMIPS_CALL_LO16:
  10857. case elfcpp::R_MIPS_EH:
  10858. // Absolute in GOT.
  10859. return Symbol::RELATIVE_REF;
  10860. case elfcpp::R_MIPS_TLS_DTPMOD32:
  10861. case elfcpp::R_MIPS_TLS_DTPREL32:
  10862. case elfcpp::R_MIPS_TLS_DTPMOD64:
  10863. case elfcpp::R_MIPS_TLS_DTPREL64:
  10864. case elfcpp::R_MIPS_TLS_GD:
  10865. case elfcpp::R_MIPS_TLS_LDM:
  10866. case elfcpp::R_MIPS_TLS_DTPREL_HI16:
  10867. case elfcpp::R_MIPS_TLS_DTPREL_LO16:
  10868. case elfcpp::R_MIPS_TLS_GOTTPREL:
  10869. case elfcpp::R_MIPS_TLS_TPREL32:
  10870. case elfcpp::R_MIPS_TLS_TPREL64:
  10871. case elfcpp::R_MIPS_TLS_TPREL_HI16:
  10872. case elfcpp::R_MIPS_TLS_TPREL_LO16:
  10873. case elfcpp::R_MIPS16_TLS_GD:
  10874. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  10875. case elfcpp::R_MICROMIPS_TLS_GD:
  10876. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  10877. case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
  10878. case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
  10879. return Symbol::TLS_REF;
  10880. case elfcpp::R_MIPS_COPY:
  10881. case elfcpp::R_MIPS_JUMP_SLOT:
  10882. default:
  10883. // Not expected. We will give an error later.
  10884. return 0;
  10885. }
  10886. }
  10887. // Report an unsupported relocation against a local symbol.
  10888. template<int size, bool big_endian>
  10889. void
  10890. Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
  10891. Sized_relobj_file<size, big_endian>* object,
  10892. unsigned int r_type)
  10893. {
  10894. gold_error(_("%s: unsupported reloc %u against local symbol"),
  10895. object->name().c_str(), r_type);
  10896. }
  10897. // Report an unsupported relocation against a global symbol.
  10898. template<int size, bool big_endian>
  10899. void
  10900. Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
  10901. Sized_relobj_file<size, big_endian>* object,
  10902. unsigned int r_type,
  10903. Symbol* gsym)
  10904. {
  10905. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  10906. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  10907. }
  10908. // Return printable name for ABI.
  10909. template<int size, bool big_endian>
  10910. const char*
  10911. Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
  10912. {
  10913. switch (e_flags & elfcpp::EF_MIPS_ABI)
  10914. {
  10915. case 0:
  10916. if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
  10917. return "N32";
  10918. else if (size == 64)
  10919. return "64";
  10920. else
  10921. return "none";
  10922. case elfcpp::E_MIPS_ABI_O32:
  10923. return "O32";
  10924. case elfcpp::E_MIPS_ABI_O64:
  10925. return "O64";
  10926. case elfcpp::E_MIPS_ABI_EABI32:
  10927. return "EABI32";
  10928. case elfcpp::E_MIPS_ABI_EABI64:
  10929. return "EABI64";
  10930. default:
  10931. return "unknown abi";
  10932. }
  10933. }
  10934. template<int size, bool big_endian>
  10935. const char*
  10936. Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
  10937. {
  10938. switch (e_flags & elfcpp::EF_MIPS_MACH)
  10939. {
  10940. case elfcpp::E_MIPS_MACH_3900:
  10941. return "mips:3900";
  10942. case elfcpp::E_MIPS_MACH_4010:
  10943. return "mips:4010";
  10944. case elfcpp::E_MIPS_MACH_4100:
  10945. return "mips:4100";
  10946. case elfcpp::E_MIPS_MACH_4111:
  10947. return "mips:4111";
  10948. case elfcpp::E_MIPS_MACH_4120:
  10949. return "mips:4120";
  10950. case elfcpp::E_MIPS_MACH_4650:
  10951. return "mips:4650";
  10952. case elfcpp::E_MIPS_MACH_5400:
  10953. return "mips:5400";
  10954. case elfcpp::E_MIPS_MACH_5500:
  10955. return "mips:5500";
  10956. case elfcpp::E_MIPS_MACH_5900:
  10957. return "mips:5900";
  10958. case elfcpp::E_MIPS_MACH_SB1:
  10959. return "mips:sb1";
  10960. case elfcpp::E_MIPS_MACH_9000:
  10961. return "mips:9000";
  10962. case elfcpp::E_MIPS_MACH_LS2E:
  10963. return "mips:loongson_2e";
  10964. case elfcpp::E_MIPS_MACH_LS2F:
  10965. return "mips:loongson_2f";
  10966. case elfcpp::E_MIPS_MACH_GS464:
  10967. return "mips:gs464";
  10968. case elfcpp::E_MIPS_MACH_GS464E:
  10969. return "mips:gs464e";
  10970. case elfcpp::E_MIPS_MACH_GS264E:
  10971. return "mips:gs264e";
  10972. case elfcpp::E_MIPS_MACH_OCTEON:
  10973. return "mips:octeon";
  10974. case elfcpp::E_MIPS_MACH_OCTEON2:
  10975. return "mips:octeon2";
  10976. case elfcpp::E_MIPS_MACH_OCTEON3:
  10977. return "mips:octeon3";
  10978. case elfcpp::E_MIPS_MACH_XLR:
  10979. return "mips:xlr";
  10980. default:
  10981. switch (e_flags & elfcpp::EF_MIPS_ARCH)
  10982. {
  10983. default:
  10984. case elfcpp::E_MIPS_ARCH_1:
  10985. return "mips:3000";
  10986. case elfcpp::E_MIPS_ARCH_2:
  10987. return "mips:6000";
  10988. case elfcpp::E_MIPS_ARCH_3:
  10989. return "mips:4000";
  10990. case elfcpp::E_MIPS_ARCH_4:
  10991. return "mips:8000";
  10992. case elfcpp::E_MIPS_ARCH_5:
  10993. return "mips:mips5";
  10994. case elfcpp::E_MIPS_ARCH_32:
  10995. return "mips:isa32";
  10996. case elfcpp::E_MIPS_ARCH_64:
  10997. return "mips:isa64";
  10998. case elfcpp::E_MIPS_ARCH_32R2:
  10999. return "mips:isa32r2";
  11000. case elfcpp::E_MIPS_ARCH_32R6:
  11001. return "mips:isa32r6";
  11002. case elfcpp::E_MIPS_ARCH_64R2:
  11003. return "mips:isa64r2";
  11004. case elfcpp::E_MIPS_ARCH_64R6:
  11005. return "mips:isa64r6";
  11006. }
  11007. }
  11008. return "unknown CPU";
  11009. }
  11010. template<int size, bool big_endian>
  11011. const Target::Target_info Target_mips<size, big_endian>::mips_info =
  11012. {
  11013. size, // size
  11014. big_endian, // is_big_endian
  11015. elfcpp::EM_MIPS, // machine_code
  11016. true, // has_make_symbol
  11017. false, // has_resolve
  11018. false, // has_code_fill
  11019. true, // is_default_stack_executable
  11020. false, // can_icf_inline_merge_sections
  11021. '\0', // wrap_char
  11022. size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
  11023. 0x400000, // default_text_segment_address
  11024. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  11025. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  11026. false, // isolate_execinstr
  11027. 0, // rosegment_gap
  11028. elfcpp::SHN_UNDEF, // small_common_shndx
  11029. elfcpp::SHN_UNDEF, // large_common_shndx
  11030. 0, // small_common_section_flags
  11031. 0, // large_common_section_flags
  11032. NULL, // attributes_section
  11033. NULL, // attributes_vendor
  11034. "__start", // entry_symbol_name
  11035. 32, // hash_entry_size
  11036. elfcpp::SHT_PROGBITS, // unwind_section_type
  11037. };
  11038. template<int size, bool big_endian>
  11039. class Target_mips_nacl : public Target_mips<size, big_endian>
  11040. {
  11041. public:
  11042. Target_mips_nacl()
  11043. : Target_mips<size, big_endian>(&mips_nacl_info)
  11044. { }
  11045. private:
  11046. static const Target::Target_info mips_nacl_info;
  11047. };
  11048. template<int size, bool big_endian>
  11049. const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
  11050. {
  11051. size, // size
  11052. big_endian, // is_big_endian
  11053. elfcpp::EM_MIPS, // machine_code
  11054. true, // has_make_symbol
  11055. false, // has_resolve
  11056. false, // has_code_fill
  11057. true, // is_default_stack_executable
  11058. false, // can_icf_inline_merge_sections
  11059. '\0', // wrap_char
  11060. "/lib/ld.so.1", // dynamic_linker
  11061. 0x20000, // default_text_segment_address
  11062. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  11063. 0x10000, // common_pagesize (overridable by -z common-page-size)
  11064. true, // isolate_execinstr
  11065. 0x10000000, // rosegment_gap
  11066. elfcpp::SHN_UNDEF, // small_common_shndx
  11067. elfcpp::SHN_UNDEF, // large_common_shndx
  11068. 0, // small_common_section_flags
  11069. 0, // large_common_section_flags
  11070. NULL, // attributes_section
  11071. NULL, // attributes_vendor
  11072. "_start", // entry_symbol_name
  11073. 32, // hash_entry_size
  11074. elfcpp::SHT_PROGBITS, // unwind_section_type
  11075. };
  11076. // Target selector for Mips. Note this is never instantiated directly.
  11077. // It's only used in Target_selector_mips_nacl, below.
  11078. template<int size, bool big_endian>
  11079. class Target_selector_mips : public Target_selector
  11080. {
  11081. public:
  11082. Target_selector_mips()
  11083. : Target_selector(elfcpp::EM_MIPS, size, big_endian,
  11084. (size == 64 ?
  11085. (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
  11086. (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
  11087. (size == 64 ?
  11088. (big_endian ? "elf64btsmip" : "elf64ltsmip") :
  11089. (big_endian ? "elf32btsmip" : "elf32ltsmip")))
  11090. { }
  11091. Target* do_instantiate_target()
  11092. { return new Target_mips<size, big_endian>(); }
  11093. };
  11094. template<int size, bool big_endian>
  11095. class Target_selector_mips_nacl
  11096. : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
  11097. Target_mips_nacl<size, big_endian> >
  11098. {
  11099. public:
  11100. Target_selector_mips_nacl()
  11101. : Target_selector_nacl<Target_selector_mips<size, big_endian>,
  11102. Target_mips_nacl<size, big_endian> >(
  11103. // NaCl currently supports only MIPS32 little-endian.
  11104. "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
  11105. { }
  11106. };
  11107. Target_selector_mips_nacl<32, true> target_selector_mips32;
  11108. Target_selector_mips_nacl<32, false> target_selector_mips32el;
  11109. Target_selector_mips_nacl<64, true> target_selector_mips64;
  11110. Target_selector_mips_nacl<64, false> target_selector_mips64el;
  11111. } // End anonymous namespace.