mips.cc 367 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576
  1. // mips.cc -- mips target support for gold.
  2. // Copyright (C) 2011-2015 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 "nacl.h"
  40. namespace
  41. {
  42. using namespace gold;
  43. template<int size, bool big_endian>
  44. class Mips_output_data_plt;
  45. template<int size, bool big_endian>
  46. class Mips_output_data_got;
  47. template<int size, bool big_endian>
  48. class Target_mips;
  49. template<int size, bool big_endian>
  50. class Mips_output_section_reginfo;
  51. template<int size, bool big_endian>
  52. class Mips_output_data_la25_stub;
  53. template<int size, bool big_endian>
  54. class Mips_output_data_mips_stubs;
  55. template<int size>
  56. class Mips_symbol;
  57. template<int size, bool big_endian>
  58. class Mips_got_info;
  59. template<int size, bool big_endian>
  60. class Mips_relobj;
  61. class Mips16_stub_section_base;
  62. template<int size, bool big_endian>
  63. class Mips16_stub_section;
  64. // The ABI says that every symbol used by dynamic relocations must have
  65. // a global GOT entry. Among other things, this provides the dynamic
  66. // linker with a free, directly-indexed cache. The GOT can therefore
  67. // contain symbols that are not referenced by GOT relocations themselves
  68. // (in other words, it may have symbols that are not referenced by things
  69. // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
  70. // GOT relocations are less likely to overflow if we put the associated
  71. // GOT entries towards the beginning. We therefore divide the global
  72. // GOT entries into two areas: "normal" and "reloc-only". Entries in
  73. // the first area can be used for both dynamic relocations and GP-relative
  74. // accesses, while those in the "reloc-only" area are for dynamic
  75. // relocations only.
  76. // These GGA_* ("Global GOT Area") values are organised so that lower
  77. // values are more general than higher values. Also, non-GGA_NONE
  78. // values are ordered by the position of the area in the GOT.
  79. enum Global_got_area
  80. {
  81. GGA_NORMAL = 0,
  82. GGA_RELOC_ONLY = 1,
  83. GGA_NONE = 2
  84. };
  85. // The types of GOT entries needed for this platform.
  86. // These values are exposed to the ABI in an incremental link.
  87. // Do not renumber existing values without changing the version
  88. // number of the .gnu_incremental_inputs section.
  89. enum Got_type
  90. {
  91. GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
  92. GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
  93. GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
  94. // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
  95. GOT_TYPE_STANDARD_MULTIGOT = 3,
  96. GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
  97. GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
  98. };
  99. // TLS type of GOT entry.
  100. enum Got_tls_type
  101. {
  102. GOT_TLS_NONE = 0,
  103. GOT_TLS_GD = 1,
  104. GOT_TLS_LDM = 2,
  105. GOT_TLS_IE = 4
  106. };
  107. // Return TRUE if a relocation of type R_TYPE from OBJECT might
  108. // require an la25 stub. See also local_pic_function, which determines
  109. // whether the destination function ever requires a stub.
  110. template<int size, bool big_endian>
  111. static inline bool
  112. relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
  113. unsigned int r_type, bool target_is_16_bit_code)
  114. {
  115. // We specifically ignore branches and jumps from EF_PIC objects,
  116. // where the onus is on the compiler or programmer to perform any
  117. // necessary initialization of $25. Sometimes such initialization
  118. // is unnecessary; for example, -mno-shared functions do not use
  119. // the incoming value of $25, and may therefore be called directly.
  120. if (object->is_pic())
  121. return false;
  122. switch (r_type)
  123. {
  124. case elfcpp::R_MIPS_26:
  125. case elfcpp::R_MIPS_PC16:
  126. case elfcpp::R_MICROMIPS_26_S1:
  127. case elfcpp::R_MICROMIPS_PC7_S1:
  128. case elfcpp::R_MICROMIPS_PC10_S1:
  129. case elfcpp::R_MICROMIPS_PC16_S1:
  130. case elfcpp::R_MICROMIPS_PC23_S2:
  131. return true;
  132. case elfcpp::R_MIPS16_26:
  133. return !target_is_16_bit_code;
  134. default:
  135. return false;
  136. }
  137. }
  138. // Return true if SYM is a locally-defined PIC function, in the sense
  139. // that it or its fn_stub might need $25 to be valid on entry.
  140. // Note that MIPS16 functions set up $gp using PC-relative instructions,
  141. // so they themselves never need $25 to be valid. Only non-MIPS16
  142. // entry points are of interest here.
  143. template<int size, bool big_endian>
  144. static inline bool
  145. local_pic_function(Mips_symbol<size>* sym)
  146. {
  147. bool def_regular = (sym->source() == Symbol::FROM_OBJECT
  148. && !sym->object()->is_dynamic()
  149. && !sym->is_undefined());
  150. if (sym->is_defined() && def_regular)
  151. {
  152. Mips_relobj<size, big_endian>* object =
  153. static_cast<Mips_relobj<size, big_endian>*>(sym->object());
  154. if ((object->is_pic() || sym->is_pic())
  155. && (!sym->is_mips16()
  156. || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
  157. return true;
  158. }
  159. return false;
  160. }
  161. static inline bool
  162. hi16_reloc(int r_type)
  163. {
  164. return (r_type == elfcpp::R_MIPS_HI16
  165. || r_type == elfcpp::R_MIPS16_HI16
  166. || r_type == elfcpp::R_MICROMIPS_HI16);
  167. }
  168. static inline bool
  169. lo16_reloc(int r_type)
  170. {
  171. return (r_type == elfcpp::R_MIPS_LO16
  172. || r_type == elfcpp::R_MIPS16_LO16
  173. || r_type == elfcpp::R_MICROMIPS_LO16);
  174. }
  175. static inline bool
  176. got16_reloc(unsigned int r_type)
  177. {
  178. return (r_type == elfcpp::R_MIPS_GOT16
  179. || r_type == elfcpp::R_MIPS16_GOT16
  180. || r_type == elfcpp::R_MICROMIPS_GOT16);
  181. }
  182. static inline bool
  183. call_lo16_reloc(unsigned int r_type)
  184. {
  185. return (r_type == elfcpp::R_MIPS_CALL_LO16
  186. || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
  187. }
  188. static inline bool
  189. got_lo16_reloc(unsigned int r_type)
  190. {
  191. return (r_type == elfcpp::R_MIPS_GOT_LO16
  192. || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
  193. }
  194. static inline bool
  195. got_disp_reloc(unsigned int r_type)
  196. {
  197. return (r_type == elfcpp::R_MIPS_GOT_DISP
  198. || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
  199. }
  200. static inline bool
  201. got_page_reloc(unsigned int r_type)
  202. {
  203. return (r_type == elfcpp::R_MIPS_GOT_PAGE
  204. || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
  205. }
  206. static inline bool
  207. tls_gd_reloc(unsigned int r_type)
  208. {
  209. return (r_type == elfcpp::R_MIPS_TLS_GD
  210. || r_type == elfcpp::R_MIPS16_TLS_GD
  211. || r_type == elfcpp::R_MICROMIPS_TLS_GD);
  212. }
  213. static inline bool
  214. tls_gottprel_reloc(unsigned int r_type)
  215. {
  216. return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
  217. || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
  218. || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
  219. }
  220. static inline bool
  221. tls_ldm_reloc(unsigned int r_type)
  222. {
  223. return (r_type == elfcpp::R_MIPS_TLS_LDM
  224. || r_type == elfcpp::R_MIPS16_TLS_LDM
  225. || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
  226. }
  227. static inline bool
  228. mips16_call_reloc(unsigned int r_type)
  229. {
  230. return (r_type == elfcpp::R_MIPS16_26
  231. || r_type == elfcpp::R_MIPS16_CALL16);
  232. }
  233. static inline bool
  234. jal_reloc(unsigned int r_type)
  235. {
  236. return (r_type == elfcpp::R_MIPS_26
  237. || r_type == elfcpp::R_MIPS16_26
  238. || r_type == elfcpp::R_MICROMIPS_26_S1);
  239. }
  240. static inline bool
  241. micromips_branch_reloc(unsigned int r_type)
  242. {
  243. return (r_type == elfcpp::R_MICROMIPS_26_S1
  244. || r_type == elfcpp::R_MICROMIPS_PC16_S1
  245. || r_type == elfcpp::R_MICROMIPS_PC10_S1
  246. || r_type == elfcpp::R_MICROMIPS_PC7_S1);
  247. }
  248. // Check if R_TYPE is a MIPS16 reloc.
  249. static inline bool
  250. mips16_reloc(unsigned int r_type)
  251. {
  252. switch (r_type)
  253. {
  254. case elfcpp::R_MIPS16_26:
  255. case elfcpp::R_MIPS16_GPREL:
  256. case elfcpp::R_MIPS16_GOT16:
  257. case elfcpp::R_MIPS16_CALL16:
  258. case elfcpp::R_MIPS16_HI16:
  259. case elfcpp::R_MIPS16_LO16:
  260. case elfcpp::R_MIPS16_TLS_GD:
  261. case elfcpp::R_MIPS16_TLS_LDM:
  262. case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
  263. case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
  264. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  265. case elfcpp::R_MIPS16_TLS_TPREL_HI16:
  266. case elfcpp::R_MIPS16_TLS_TPREL_LO16:
  267. return true;
  268. default:
  269. return false;
  270. }
  271. }
  272. // Check if R_TYPE is a microMIPS reloc.
  273. static inline bool
  274. micromips_reloc(unsigned int r_type)
  275. {
  276. switch (r_type)
  277. {
  278. case elfcpp::R_MICROMIPS_26_S1:
  279. case elfcpp::R_MICROMIPS_HI16:
  280. case elfcpp::R_MICROMIPS_LO16:
  281. case elfcpp::R_MICROMIPS_GPREL16:
  282. case elfcpp::R_MICROMIPS_LITERAL:
  283. case elfcpp::R_MICROMIPS_GOT16:
  284. case elfcpp::R_MICROMIPS_PC7_S1:
  285. case elfcpp::R_MICROMIPS_PC10_S1:
  286. case elfcpp::R_MICROMIPS_PC16_S1:
  287. case elfcpp::R_MICROMIPS_CALL16:
  288. case elfcpp::R_MICROMIPS_GOT_DISP:
  289. case elfcpp::R_MICROMIPS_GOT_PAGE:
  290. case elfcpp::R_MICROMIPS_GOT_OFST:
  291. case elfcpp::R_MICROMIPS_GOT_HI16:
  292. case elfcpp::R_MICROMIPS_GOT_LO16:
  293. case elfcpp::R_MICROMIPS_SUB:
  294. case elfcpp::R_MICROMIPS_HIGHER:
  295. case elfcpp::R_MICROMIPS_HIGHEST:
  296. case elfcpp::R_MICROMIPS_CALL_HI16:
  297. case elfcpp::R_MICROMIPS_CALL_LO16:
  298. case elfcpp::R_MICROMIPS_SCN_DISP:
  299. case elfcpp::R_MICROMIPS_JALR:
  300. case elfcpp::R_MICROMIPS_HI0_LO16:
  301. case elfcpp::R_MICROMIPS_TLS_GD:
  302. case elfcpp::R_MICROMIPS_TLS_LDM:
  303. case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
  304. case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
  305. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  306. case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
  307. case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
  308. case elfcpp::R_MICROMIPS_GPREL7_S2:
  309. case elfcpp::R_MICROMIPS_PC23_S2:
  310. return true;
  311. default:
  312. return false;
  313. }
  314. }
  315. static inline bool
  316. is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
  317. {
  318. switch (high_reloc)
  319. {
  320. case elfcpp::R_MIPS_HI16:
  321. case elfcpp::R_MIPS_GOT16:
  322. return lo16_reloc == elfcpp::R_MIPS_LO16;
  323. case elfcpp::R_MIPS16_HI16:
  324. case elfcpp::R_MIPS16_GOT16:
  325. return lo16_reloc == elfcpp::R_MIPS16_LO16;
  326. case elfcpp::R_MICROMIPS_HI16:
  327. case elfcpp::R_MICROMIPS_GOT16:
  328. return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
  329. default:
  330. return false;
  331. }
  332. }
  333. // This class is used to hold information about one GOT entry.
  334. // There are three types of entry:
  335. //
  336. // (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
  337. // (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
  338. // (2) a SYMBOL address, where SYMBOL is not local to an input object
  339. // (object != NULL, symndx == -1)
  340. // (3) a TLS LDM slot
  341. // (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
  342. template<int size, bool big_endian>
  343. class Mips_got_entry
  344. {
  345. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  346. public:
  347. Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
  348. Mips_address addend, unsigned char tls_type,
  349. unsigned int shndx)
  350. : object_(object), symndx_(symndx), tls_type_(tls_type), shndx_(shndx)
  351. { this->d.addend = addend; }
  352. Mips_got_entry(Mips_relobj<size, big_endian>* object, Mips_symbol<size>* sym,
  353. unsigned char tls_type)
  354. : object_(object), symndx_(-1U), tls_type_(tls_type), shndx_(-1U)
  355. { this->d.sym = sym; }
  356. // Return whether this entry is for a local symbol.
  357. bool
  358. is_for_local_symbol() const
  359. { return this->symndx_ != -1U; }
  360. // Return whether this entry is for a global symbol.
  361. bool
  362. is_for_global_symbol() const
  363. { return this->symndx_ == -1U; }
  364. // Return the hash of this entry.
  365. size_t
  366. hash() const
  367. {
  368. if (this->tls_type_ == GOT_TLS_LDM)
  369. return this->symndx_ + (1 << 18);
  370. if (this->symndx_ != -1U)
  371. {
  372. uintptr_t object_id = reinterpret_cast<uintptr_t>(this->object());
  373. return this->symndx_ + object_id + this->d.addend;
  374. }
  375. else
  376. {
  377. uintptr_t sym_id = reinterpret_cast<uintptr_t>(this->d.sym);
  378. return this->symndx_ + sym_id;
  379. }
  380. }
  381. // Return whether this entry is equal to OTHER.
  382. bool
  383. equals(Mips_got_entry<size, big_endian>* other) const
  384. {
  385. if (this->symndx_ != other->symndx_
  386. || this->tls_type_ != other->tls_type_)
  387. return false;
  388. if (this->tls_type_ == GOT_TLS_LDM)
  389. return true;
  390. if (this->symndx_ != -1U)
  391. return (this->object() == other->object()
  392. && this->d.addend == other->d.addend);
  393. else
  394. return this->d.sym == other->d.sym;
  395. }
  396. // Return input object that needs this GOT entry.
  397. Mips_relobj<size, big_endian>*
  398. object() const
  399. {
  400. gold_assert(this->object_ != NULL);
  401. return this->object_;
  402. }
  403. // Return local symbol index for local GOT entries.
  404. unsigned int
  405. symndx() const
  406. {
  407. gold_assert(this->symndx_ != -1U);
  408. return this->symndx_;
  409. }
  410. // Return the relocation addend for local GOT entries.
  411. Mips_address
  412. addend() const
  413. {
  414. gold_assert(this->symndx_ != -1U);
  415. return this->d.addend;
  416. }
  417. // Return global symbol for global GOT entries.
  418. Mips_symbol<size>*
  419. sym() const
  420. {
  421. gold_assert(this->symndx_ == -1U);
  422. return this->d.sym;
  423. }
  424. // Return whether this is a TLS GOT entry.
  425. bool
  426. is_tls_entry() const
  427. { return this->tls_type_ != GOT_TLS_NONE; }
  428. // Return TLS type of this GOT entry.
  429. unsigned char
  430. tls_type() const
  431. { return this->tls_type_; }
  432. // Return section index of the local symbol for local GOT entries.
  433. unsigned int
  434. shndx() const
  435. { return this->shndx_; }
  436. private:
  437. // The input object that needs the GOT entry.
  438. Mips_relobj<size, big_endian>* object_;
  439. // The index of the symbol if we have a local symbol; -1 otherwise.
  440. unsigned int symndx_;
  441. union
  442. {
  443. // If symndx != -1, the addend of the relocation that should be added to the
  444. // symbol value.
  445. Mips_address addend;
  446. // If symndx == -1, the global symbol corresponding to this GOT entry. The
  447. // symbol's entry is in the local area if mips_sym->global_got_area is
  448. // GGA_NONE, otherwise it is in the global area.
  449. Mips_symbol<size>* sym;
  450. } d;
  451. // The TLS type of this GOT entry. An LDM GOT entry will be a local
  452. // symbol entry with r_symndx == 0.
  453. unsigned char tls_type_;
  454. // For local GOT entries, section index of the local symbol.
  455. unsigned int shndx_;
  456. };
  457. // Hash for Mips_got_entry.
  458. template<int size, bool big_endian>
  459. class Mips_got_entry_hash
  460. {
  461. public:
  462. size_t
  463. operator()(Mips_got_entry<size, big_endian>* entry) const
  464. { return entry->hash(); }
  465. };
  466. // Equality for Mips_got_entry.
  467. template<int size, bool big_endian>
  468. class Mips_got_entry_eq
  469. {
  470. public:
  471. bool
  472. operator()(Mips_got_entry<size, big_endian>* e1,
  473. Mips_got_entry<size, big_endian>* e2) const
  474. { return e1->equals(e2); }
  475. };
  476. // Got_page_range. This class describes a range of addends: [MIN_ADDEND,
  477. // MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
  478. // increasing MIN_ADDEND.
  479. struct Got_page_range
  480. {
  481. Got_page_range()
  482. : next(NULL), min_addend(0), max_addend(0)
  483. { }
  484. Got_page_range* next;
  485. int min_addend;
  486. int max_addend;
  487. // Return the maximum number of GOT page entries required.
  488. int
  489. get_max_pages()
  490. { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
  491. };
  492. // Got_page_entry. This class describes the range of addends that are applied
  493. // to page relocations against a given symbol.
  494. struct Got_page_entry
  495. {
  496. Got_page_entry()
  497. : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
  498. { }
  499. Got_page_entry(Object* object_, unsigned int symndx_)
  500. : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
  501. { }
  502. // The input object that needs the GOT page entry.
  503. Object* object;
  504. // The index of the symbol, as stored in the relocation r_info.
  505. unsigned int symndx;
  506. // The ranges for this page entry.
  507. Got_page_range* ranges;
  508. // The maximum number of page entries needed for RANGES.
  509. unsigned int num_pages;
  510. };
  511. // Hash for Got_page_entry.
  512. struct Got_page_entry_hash
  513. {
  514. size_t
  515. operator()(Got_page_entry* entry) const
  516. { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
  517. };
  518. // Equality for Got_page_entry.
  519. struct Got_page_entry_eq
  520. {
  521. bool
  522. operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
  523. {
  524. return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
  525. }
  526. };
  527. // This class is used to hold .got information when linking.
  528. template<int size, bool big_endian>
  529. class Mips_got_info
  530. {
  531. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  532. typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
  533. Reloc_section;
  534. typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
  535. // Unordered set of GOT entries.
  536. typedef Unordered_set<Mips_got_entry<size, big_endian>*,
  537. Mips_got_entry_hash<size, big_endian>,
  538. Mips_got_entry_eq<size, big_endian> > Got_entry_set;
  539. // Unordered set of GOT page entries.
  540. typedef Unordered_set<Got_page_entry*,
  541. Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
  542. public:
  543. Mips_got_info()
  544. : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
  545. tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
  546. got_entries_(), got_page_entries_(), got_page_offset_start_(0),
  547. got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
  548. offset_(0)
  549. { }
  550. // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
  551. // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
  552. void
  553. record_local_got_symbol(Mips_relobj<size, big_endian>* object,
  554. unsigned int symndx, Mips_address addend,
  555. unsigned int r_type, unsigned int shndx);
  556. // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
  557. // in OBJECT. FOR_CALL is true if the caller is only interested in
  558. // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
  559. // relocation.
  560. void
  561. record_global_got_symbol(Mips_symbol<size>* mips_sym,
  562. Mips_relobj<size, big_endian>* object,
  563. unsigned int r_type, bool dyn_reloc, bool for_call);
  564. // Add ENTRY to master GOT and to OBJECT's GOT.
  565. void
  566. record_got_entry(Mips_got_entry<size, big_endian>* entry,
  567. Mips_relobj<size, big_endian>* object);
  568. // Record that OBJECT has a page relocation against symbol SYMNDX and
  569. // that ADDEND is the addend for that relocation.
  570. void
  571. record_got_page_entry(Mips_relobj<size, big_endian>* object,
  572. unsigned int symndx, int addend);
  573. // Create all entries that should be in the local part of the GOT.
  574. void
  575. add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
  576. // Create GOT page entries.
  577. void
  578. add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
  579. // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
  580. void
  581. add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
  582. unsigned int non_reloc_only_global_gotno);
  583. // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
  584. void
  585. add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
  586. // Create TLS GOT entries.
  587. void
  588. add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
  589. // Decide whether the symbol needs an entry in the global part of the primary
  590. // GOT, setting global_got_area accordingly. Count the number of global
  591. // symbols that are in the primary GOT only because they have dynamic
  592. // relocations R_MIPS_REL32 against them (reloc_only_gotno).
  593. void
  594. count_got_symbols(Symbol_table* symtab);
  595. // Return the offset of GOT page entry for VALUE.
  596. unsigned int
  597. get_got_page_offset(Mips_address value,
  598. Mips_output_data_got<size, big_endian>* got);
  599. // Count the number of GOT entries required.
  600. void
  601. count_got_entries();
  602. // Count the number of GOT entries required by ENTRY. Accumulate the result.
  603. void
  604. count_got_entry(Mips_got_entry<size, big_endian>* entry);
  605. // Add FROM's GOT entries.
  606. void
  607. add_got_entries(Mips_got_info<size, big_endian>* from);
  608. // Add FROM's GOT page entries.
  609. void
  610. add_got_page_entries(Mips_got_info<size, big_endian>* from);
  611. // Return GOT size.
  612. unsigned int
  613. got_size() const
  614. { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
  615. + this->tls_gotno_) * size/8);
  616. }
  617. // Return the number of local GOT entries.
  618. unsigned int
  619. local_gotno() const
  620. { return this->local_gotno_; }
  621. // Return the maximum number of page GOT entries needed.
  622. unsigned int
  623. page_gotno() const
  624. { return this->page_gotno_; }
  625. // Return the number of global GOT entries.
  626. unsigned int
  627. global_gotno() const
  628. { return this->global_gotno_; }
  629. // Set the number of global GOT entries.
  630. void
  631. set_global_gotno(unsigned int global_gotno)
  632. { this->global_gotno_ = global_gotno; }
  633. // Return the number of GGA_RELOC_ONLY global GOT entries.
  634. unsigned int
  635. reloc_only_gotno() const
  636. { return this->reloc_only_gotno_; }
  637. // Return the number of TLS GOT entries.
  638. unsigned int
  639. tls_gotno() const
  640. { return this->tls_gotno_; }
  641. // Return the GOT type for this GOT. Used for multi-GOT links only.
  642. unsigned int
  643. multigot_got_type(unsigned int got_type) const
  644. {
  645. switch (got_type)
  646. {
  647. case GOT_TYPE_STANDARD:
  648. return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
  649. case GOT_TYPE_TLS_OFFSET:
  650. return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
  651. case GOT_TYPE_TLS_PAIR:
  652. return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
  653. default:
  654. gold_unreachable();
  655. }
  656. }
  657. // Remove lazy-binding stubs for global symbols in this GOT.
  658. void
  659. remove_lazy_stubs(Target_mips<size, big_endian>* target);
  660. // Return offset of this GOT from the start of .got section.
  661. unsigned int
  662. offset() const
  663. { return this->offset_; }
  664. // Set offset of this GOT from the start of .got section.
  665. void
  666. set_offset(unsigned int offset)
  667. { this->offset_ = offset; }
  668. // Set index of this GOT in multi-GOT links.
  669. void
  670. set_index(unsigned int index)
  671. { this->index_ = index; }
  672. // Return next GOT in multi-GOT links.
  673. Mips_got_info<size, big_endian>*
  674. next() const
  675. { return this->next_; }
  676. // Set next GOT in multi-GOT links.
  677. void
  678. set_next(Mips_got_info<size, big_endian>* next)
  679. { this->next_ = next; }
  680. // Return the offset of TLS LDM entry for this GOT.
  681. unsigned int
  682. tls_ldm_offset() const
  683. { return this->tls_ldm_offset_; }
  684. // Set the offset of TLS LDM entry for this GOT.
  685. void
  686. set_tls_ldm_offset(unsigned int tls_ldm_offset)
  687. { this->tls_ldm_offset_ = tls_ldm_offset; }
  688. Unordered_set<Mips_symbol<size>*>&
  689. global_got_symbols()
  690. { return this->global_got_symbols_; }
  691. // Return the GOT_TLS_* type required by relocation type R_TYPE.
  692. static int
  693. mips_elf_reloc_tls_type(unsigned int r_type)
  694. {
  695. if (tls_gd_reloc(r_type))
  696. return GOT_TLS_GD;
  697. if (tls_ldm_reloc(r_type))
  698. return GOT_TLS_LDM;
  699. if (tls_gottprel_reloc(r_type))
  700. return GOT_TLS_IE;
  701. return GOT_TLS_NONE;
  702. }
  703. // Return the number of GOT slots needed for GOT TLS type TYPE.
  704. static int
  705. mips_tls_got_entries(unsigned int type)
  706. {
  707. switch (type)
  708. {
  709. case GOT_TLS_GD:
  710. case GOT_TLS_LDM:
  711. return 2;
  712. case GOT_TLS_IE:
  713. return 1;
  714. case GOT_TLS_NONE:
  715. return 0;
  716. default:
  717. gold_unreachable();
  718. }
  719. }
  720. private:
  721. // The number of local GOT entries.
  722. unsigned int local_gotno_;
  723. // The maximum number of page GOT entries needed.
  724. unsigned int page_gotno_;
  725. // The number of global GOT entries.
  726. unsigned int global_gotno_;
  727. // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
  728. unsigned int reloc_only_gotno_;
  729. // The number of TLS GOT entries.
  730. unsigned int tls_gotno_;
  731. // The offset of TLS LDM entry for this GOT.
  732. unsigned int tls_ldm_offset_;
  733. // All symbols that have global GOT entry.
  734. Unordered_set<Mips_symbol<size>*> global_got_symbols_;
  735. // A hash table holding GOT entries.
  736. Got_entry_set got_entries_;
  737. // A hash table of GOT page entries.
  738. Got_page_entry_set got_page_entries_;
  739. // The offset of first GOT page entry for this GOT.
  740. unsigned int got_page_offset_start_;
  741. // The offset of next available GOT page entry for this GOT.
  742. unsigned int got_page_offset_next_;
  743. // A hash table that maps GOT page entry value to the GOT offset where
  744. // the entry is located.
  745. Got_page_offsets got_page_offsets_;
  746. // In multi-GOT links, a pointer to the next GOT.
  747. Mips_got_info<size, big_endian>* next_;
  748. // Index of this GOT in multi-GOT links.
  749. unsigned int index_;
  750. // The offset of this GOT in multi-GOT links.
  751. unsigned int offset_;
  752. };
  753. // This is a helper class used during relocation scan. It records GOT16 addend.
  754. template<int size, bool big_endian>
  755. struct got16_addend
  756. {
  757. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  758. got16_addend(const Sized_relobj_file<size, big_endian>* _object,
  759. unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
  760. Mips_address _addend)
  761. : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
  762. addend(_addend)
  763. { }
  764. const Sized_relobj_file<size, big_endian>* object;
  765. unsigned int shndx;
  766. unsigned int r_type;
  767. unsigned int r_sym;
  768. Mips_address addend;
  769. };
  770. // Mips_symbol class. Holds additional symbol information needed for Mips.
  771. template<int size>
  772. class Mips_symbol : public Sized_symbol<size>
  773. {
  774. public:
  775. Mips_symbol()
  776. : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
  777. has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
  778. pointer_equality_needed_(false), global_got_area_(GGA_NONE),
  779. global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
  780. needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
  781. comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
  782. mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
  783. { }
  784. // Return whether this is a MIPS16 symbol.
  785. bool
  786. is_mips16() const
  787. {
  788. // (st_other & STO_MIPS16) == STO_MIPS16
  789. return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
  790. == elfcpp::STO_MIPS16 >> 2);
  791. }
  792. // Return whether this is a microMIPS symbol.
  793. bool
  794. is_micromips() const
  795. {
  796. // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
  797. return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
  798. == elfcpp::STO_MICROMIPS >> 2);
  799. }
  800. // Return whether the symbol needs MIPS16 fn_stub.
  801. bool
  802. need_fn_stub() const
  803. { return this->need_fn_stub_; }
  804. // Set that the symbol needs MIPS16 fn_stub.
  805. void
  806. set_need_fn_stub()
  807. { this->need_fn_stub_ = true; }
  808. // Return whether this symbol is referenced by branch relocations from
  809. // any non-PIC input file.
  810. bool
  811. has_nonpic_branches() const
  812. { return this->has_nonpic_branches_; }
  813. // Set that this symbol is referenced by branch relocations from
  814. // any non-PIC input file.
  815. void
  816. set_has_nonpic_branches()
  817. { this->has_nonpic_branches_ = true; }
  818. // Return the offset of the la25 stub for this symbol from the start of the
  819. // la25 stub section.
  820. unsigned int
  821. la25_stub_offset() const
  822. { return this->la25_stub_offset_; }
  823. // Set the offset of the la25 stub for this symbol from the start of the
  824. // la25 stub section.
  825. void
  826. set_la25_stub_offset(unsigned int offset)
  827. { this->la25_stub_offset_ = offset; }
  828. // Return whether the symbol has la25 stub. This is true if this symbol is
  829. // for a PIC function, and there are non-PIC branches and jumps to it.
  830. bool
  831. has_la25_stub() const
  832. { return this->la25_stub_offset_ != -1U; }
  833. // Return whether there is a relocation against this symbol that must be
  834. // resolved by the static linker (that is, the relocation cannot possibly
  835. // be made dynamic).
  836. bool
  837. has_static_relocs() const
  838. { return this->has_static_relocs_; }
  839. // Set that there is a relocation against this symbol that must be resolved
  840. // by the static linker (that is, the relocation cannot possibly be made
  841. // dynamic).
  842. void
  843. set_has_static_relocs()
  844. { this->has_static_relocs_ = true; }
  845. // Return whether we must not create a lazy-binding stub for this symbol.
  846. bool
  847. no_lazy_stub() const
  848. { return this->no_lazy_stub_; }
  849. // Set that we must not create a lazy-binding stub for this symbol.
  850. void
  851. set_no_lazy_stub()
  852. { this->no_lazy_stub_ = true; }
  853. // Return the offset of the lazy-binding stub for this symbol from the start
  854. // of .MIPS.stubs section.
  855. unsigned int
  856. lazy_stub_offset() const
  857. { return this->lazy_stub_offset_; }
  858. // Set the offset of the lazy-binding stub for this symbol from the start
  859. // of .MIPS.stubs section.
  860. void
  861. set_lazy_stub_offset(unsigned int offset)
  862. { this->lazy_stub_offset_ = offset; }
  863. // Return whether there are any relocations for this symbol where
  864. // pointer equality matters.
  865. bool
  866. pointer_equality_needed() const
  867. { return this->pointer_equality_needed_; }
  868. // Set that there are relocations for this symbol where pointer equality
  869. // matters.
  870. void
  871. set_pointer_equality_needed()
  872. { this->pointer_equality_needed_ = true; }
  873. // Return global GOT area where this symbol in located.
  874. Global_got_area
  875. global_got_area() const
  876. { return this->global_got_area_; }
  877. // Set global GOT area where this symbol in located.
  878. void
  879. set_global_got_area(Global_got_area global_got_area)
  880. { this->global_got_area_ = global_got_area; }
  881. // Return the global GOT offset for this symbol. For multi-GOT links, this
  882. // returns the offset from the start of .got section to the first GOT entry
  883. // for the symbol. Note that in multi-GOT links the symbol can have entry
  884. // in more than one GOT.
  885. unsigned int
  886. global_gotoffset() const
  887. { return this->global_gotoffset_; }
  888. // Set the global GOT offset for this symbol. Note that in multi-GOT links
  889. // the symbol can have entry in more than one GOT. This method will set
  890. // the offset only if it is less than current offset.
  891. void
  892. set_global_gotoffset(unsigned int offset)
  893. {
  894. if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
  895. this->global_gotoffset_ = offset;
  896. }
  897. // Return whether all GOT relocations for this symbol are for calls.
  898. bool
  899. got_only_for_calls() const
  900. { return this->got_only_for_calls_; }
  901. // Set that there is a GOT relocation for this symbol that is not for call.
  902. void
  903. set_got_not_only_for_calls()
  904. { this->got_only_for_calls_ = false; }
  905. // Return whether this is a PIC symbol.
  906. bool
  907. is_pic() const
  908. {
  909. // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
  910. return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
  911. == (elfcpp::STO_MIPS_PIC >> 2));
  912. }
  913. // Set the flag in st_other field that marks this symbol as PIC.
  914. void
  915. set_pic()
  916. {
  917. if (this->is_mips16())
  918. // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
  919. this->set_nonvis((this->nonvis()
  920. & ~((elfcpp::STO_MIPS16 >> 2)
  921. | (elfcpp::STO_MIPS_FLAGS >> 2)))
  922. | (elfcpp::STO_MIPS_PIC >> 2));
  923. else
  924. // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
  925. this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
  926. | (elfcpp::STO_MIPS_PIC >> 2));
  927. }
  928. // Set the flag in st_other field that marks this symbol as PLT.
  929. void
  930. set_mips_plt()
  931. {
  932. if (this->is_mips16())
  933. // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
  934. this->set_nonvis((this->nonvis()
  935. & ((elfcpp::STO_MIPS16 >> 2)
  936. | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
  937. | (elfcpp::STO_MIPS_PLT >> 2));
  938. else
  939. // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
  940. this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
  941. | (elfcpp::STO_MIPS_PLT >> 2));
  942. }
  943. // Downcast a base pointer to a Mips_symbol pointer.
  944. static Mips_symbol<size>*
  945. as_mips_sym(Symbol* sym)
  946. { return static_cast<Mips_symbol<size>*>(sym); }
  947. // Downcast a base pointer to a Mips_symbol pointer.
  948. static const Mips_symbol<size>*
  949. as_mips_sym(const Symbol* sym)
  950. { return static_cast<const Mips_symbol<size>*>(sym); }
  951. // Return whether the symbol has lazy-binding stub.
  952. bool
  953. has_lazy_stub() const
  954. { return this->has_lazy_stub_; }
  955. // Set whether the symbol has lazy-binding stub.
  956. void
  957. set_has_lazy_stub(bool has_lazy_stub)
  958. { this->has_lazy_stub_ = has_lazy_stub; }
  959. // Return whether the symbol needs a standard PLT entry.
  960. bool
  961. needs_mips_plt() const
  962. { return this->needs_mips_plt_; }
  963. // Set whether the symbol needs a standard PLT entry.
  964. void
  965. set_needs_mips_plt(bool needs_mips_plt)
  966. { this->needs_mips_plt_ = needs_mips_plt; }
  967. // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
  968. // entry.
  969. bool
  970. needs_comp_plt() const
  971. { return this->needs_comp_plt_; }
  972. // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
  973. void
  974. set_needs_comp_plt(bool needs_comp_plt)
  975. { this->needs_comp_plt_ = needs_comp_plt; }
  976. // Return standard PLT entry offset, or -1 if none.
  977. unsigned int
  978. mips_plt_offset() const
  979. { return this->mips_plt_offset_; }
  980. // Set standard PLT entry offset.
  981. void
  982. set_mips_plt_offset(unsigned int mips_plt_offset)
  983. { this->mips_plt_offset_ = mips_plt_offset; }
  984. // Return whether the symbol has standard PLT entry.
  985. bool
  986. has_mips_plt_offset() const
  987. { return this->mips_plt_offset_ != -1U; }
  988. // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
  989. unsigned int
  990. comp_plt_offset() const
  991. { return this->comp_plt_offset_; }
  992. // Set compressed (MIPS16 or microMIPS) PLT entry offset.
  993. void
  994. set_comp_plt_offset(unsigned int comp_plt_offset)
  995. { this->comp_plt_offset_ = comp_plt_offset; }
  996. // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
  997. bool
  998. has_comp_plt_offset() const
  999. { return this->comp_plt_offset_ != -1U; }
  1000. // Return MIPS16 fn stub for a symbol.
  1001. template<bool big_endian>
  1002. Mips16_stub_section<size, big_endian>*
  1003. get_mips16_fn_stub() const
  1004. {
  1005. return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
  1006. }
  1007. // Set MIPS16 fn stub for a symbol.
  1008. void
  1009. set_mips16_fn_stub(Mips16_stub_section_base* stub)
  1010. { this->mips16_fn_stub_ = stub; }
  1011. // Return whether symbol has MIPS16 fn stub.
  1012. bool
  1013. has_mips16_fn_stub() const
  1014. { return this->mips16_fn_stub_ != NULL; }
  1015. // Return MIPS16 call stub for a symbol.
  1016. template<bool big_endian>
  1017. Mips16_stub_section<size, big_endian>*
  1018. get_mips16_call_stub() const
  1019. {
  1020. return static_cast<Mips16_stub_section<size, big_endian>*>(
  1021. mips16_call_stub_);
  1022. }
  1023. // Set MIPS16 call stub for a symbol.
  1024. void
  1025. set_mips16_call_stub(Mips16_stub_section_base* stub)
  1026. { this->mips16_call_stub_ = stub; }
  1027. // Return whether symbol has MIPS16 call stub.
  1028. bool
  1029. has_mips16_call_stub() const
  1030. { return this->mips16_call_stub_ != NULL; }
  1031. // Return MIPS16 call_fp stub for a symbol.
  1032. template<bool big_endian>
  1033. Mips16_stub_section<size, big_endian>*
  1034. get_mips16_call_fp_stub() const
  1035. {
  1036. return static_cast<Mips16_stub_section<size, big_endian>*>(
  1037. mips16_call_fp_stub_);
  1038. }
  1039. // Set MIPS16 call_fp stub for a symbol.
  1040. void
  1041. set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
  1042. { this->mips16_call_fp_stub_ = stub; }
  1043. // Return whether symbol has MIPS16 call_fp stub.
  1044. bool
  1045. has_mips16_call_fp_stub() const
  1046. { return this->mips16_call_fp_stub_ != NULL; }
  1047. bool
  1048. get_applied_secondary_got_fixup() const
  1049. { return applied_secondary_got_fixup_; }
  1050. void
  1051. set_applied_secondary_got_fixup()
  1052. { this->applied_secondary_got_fixup_ = true; }
  1053. private:
  1054. // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
  1055. // appears in any relocs other than a 16 bit call.
  1056. bool need_fn_stub_;
  1057. // True if this symbol is referenced by branch relocations from
  1058. // any non-PIC input file. This is used to determine whether an
  1059. // la25 stub is required.
  1060. bool has_nonpic_branches_;
  1061. // The offset of the la25 stub for this symbol from the start of the
  1062. // la25 stub section.
  1063. unsigned int la25_stub_offset_;
  1064. // True if there is a relocation against this symbol that must be
  1065. // resolved by the static linker (that is, the relocation cannot
  1066. // possibly be made dynamic).
  1067. bool has_static_relocs_;
  1068. // Whether we must not create a lazy-binding stub for this symbol.
  1069. // This is true if the symbol has relocations related to taking the
  1070. // function's address.
  1071. bool no_lazy_stub_;
  1072. // The offset of the lazy-binding stub for this symbol from the start of
  1073. // .MIPS.stubs section.
  1074. unsigned int lazy_stub_offset_;
  1075. // True if there are any relocations for this symbol where pointer equality
  1076. // matters.
  1077. bool pointer_equality_needed_;
  1078. // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
  1079. // in the global part of the GOT.
  1080. Global_got_area global_got_area_;
  1081. // The global GOT offset for this symbol. For multi-GOT links, this is offset
  1082. // from the start of .got section to the first GOT entry for the symbol.
  1083. // Note that in multi-GOT links the symbol can have entry in more than one GOT.
  1084. unsigned int global_gotoffset_;
  1085. // Whether all GOT relocations for this symbol are for calls.
  1086. bool got_only_for_calls_;
  1087. // Whether the symbol has lazy-binding stub.
  1088. bool has_lazy_stub_;
  1089. // Whether the symbol needs a standard PLT entry.
  1090. bool needs_mips_plt_;
  1091. // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
  1092. bool needs_comp_plt_;
  1093. // Standard PLT entry offset, or -1 if none.
  1094. unsigned int mips_plt_offset_;
  1095. // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
  1096. unsigned int comp_plt_offset_;
  1097. // MIPS16 fn stub for a symbol.
  1098. Mips16_stub_section_base* mips16_fn_stub_;
  1099. // MIPS16 call stub for a symbol.
  1100. Mips16_stub_section_base* mips16_call_stub_;
  1101. // MIPS16 call_fp stub for a symbol.
  1102. Mips16_stub_section_base* mips16_call_fp_stub_;
  1103. bool applied_secondary_got_fixup_;
  1104. };
  1105. // Mips16_stub_section class.
  1106. // The mips16 compiler uses a couple of special sections to handle
  1107. // floating point arguments.
  1108. // Section names that look like .mips16.fn.FNNAME contain stubs that
  1109. // copy floating point arguments from the fp regs to the gp regs and
  1110. // then jump to FNNAME. If any 32 bit function calls FNNAME, the
  1111. // call should be redirected to the stub instead. If no 32 bit
  1112. // function calls FNNAME, the stub should be discarded. We need to
  1113. // consider any reference to the function, not just a call, because
  1114. // if the address of the function is taken we will need the stub,
  1115. // since the address might be passed to a 32 bit function.
  1116. // Section names that look like .mips16.call.FNNAME contain stubs
  1117. // that copy floating point arguments from the gp regs to the fp
  1118. // regs and then jump to FNNAME. If FNNAME is a 32 bit function,
  1119. // then any 16 bit function that calls FNNAME should be redirected
  1120. // to the stub instead. If FNNAME is not a 32 bit function, the
  1121. // stub should be discarded.
  1122. // .mips16.call.fp.FNNAME sections are similar, but contain stubs
  1123. // which call FNNAME and then copy the return value from the fp regs
  1124. // to the gp regs. These stubs store the return address in $18 while
  1125. // calling FNNAME; any function which might call one of these stubs
  1126. // must arrange to save $18 around the call. (This case is not
  1127. // needed for 32 bit functions that call 16 bit functions, because
  1128. // 16 bit functions always return floating point values in both
  1129. // $f0/$f1 and $2/$3.)
  1130. // Note that in all cases FNNAME might be defined statically.
  1131. // Therefore, FNNAME is not used literally. Instead, the relocation
  1132. // information will indicate which symbol the section is for.
  1133. // We record any stubs that we find in the symbol table.
  1134. // TODO(sasa): All mips16 stub sections should be emitted in the .text section.
  1135. class Mips16_stub_section_base { };
  1136. template<int size, bool big_endian>
  1137. class Mips16_stub_section : public Mips16_stub_section_base
  1138. {
  1139. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1140. public:
  1141. Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
  1142. : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
  1143. found_r_mips_none_(false)
  1144. {
  1145. gold_assert(object->is_mips16_fn_stub_section(shndx)
  1146. || object->is_mips16_call_stub_section(shndx)
  1147. || object->is_mips16_call_fp_stub_section(shndx));
  1148. }
  1149. // Return the object of this stub section.
  1150. Mips_relobj<size, big_endian>*
  1151. object() const
  1152. { return this->object_; }
  1153. // Return the size of a section.
  1154. uint64_t
  1155. section_size() const
  1156. { return this->object_->section_size(this->shndx_); }
  1157. // Return section index of this stub section.
  1158. unsigned int
  1159. shndx() const
  1160. { return this->shndx_; }
  1161. // Return symbol index, if stub is for a local function.
  1162. unsigned int
  1163. r_sym() const
  1164. { return this->r_sym_; }
  1165. // Return symbol, if stub is for a global function.
  1166. Mips_symbol<size>*
  1167. gsym() const
  1168. { return this->gsym_; }
  1169. // Return whether stub is for a local function.
  1170. bool
  1171. is_for_local_function() const
  1172. { return this->gsym_ == NULL; }
  1173. // This method is called when a new relocation R_TYPE for local symbol R_SYM
  1174. // is found in the stub section. Try to find stub target.
  1175. void
  1176. new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
  1177. {
  1178. // To find target symbol for this stub, trust the first R_MIPS_NONE
  1179. // relocation, if any. Otherwise trust the first relocation, whatever
  1180. // its kind.
  1181. if (this->found_r_mips_none_)
  1182. return;
  1183. if (r_type == elfcpp::R_MIPS_NONE)
  1184. {
  1185. this->r_sym_ = r_sym;
  1186. this->gsym_ = NULL;
  1187. this->found_r_mips_none_ = true;
  1188. }
  1189. else if (!is_target_found())
  1190. this->r_sym_ = r_sym;
  1191. }
  1192. // This method is called when a new relocation R_TYPE for global symbol GSYM
  1193. // is found in the stub section. Try to find stub target.
  1194. void
  1195. new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
  1196. {
  1197. // To find target symbol for this stub, trust the first R_MIPS_NONE
  1198. // relocation, if any. Otherwise trust the first relocation, whatever
  1199. // its kind.
  1200. if (this->found_r_mips_none_)
  1201. return;
  1202. if (r_type == elfcpp::R_MIPS_NONE)
  1203. {
  1204. this->gsym_ = gsym;
  1205. this->r_sym_ = 0;
  1206. this->found_r_mips_none_ = true;
  1207. }
  1208. else if (!is_target_found())
  1209. this->gsym_ = gsym;
  1210. }
  1211. // Return whether we found the stub target.
  1212. bool
  1213. is_target_found() const
  1214. { return this->r_sym_ != 0 || this->gsym_ != NULL; }
  1215. // Return whether this is a fn stub.
  1216. bool
  1217. is_fn_stub() const
  1218. { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
  1219. // Return whether this is a call stub.
  1220. bool
  1221. is_call_stub() const
  1222. { return this->object_->is_mips16_call_stub_section(this->shndx_); }
  1223. // Return whether this is a call_fp stub.
  1224. bool
  1225. is_call_fp_stub() const
  1226. { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
  1227. // Return the output address.
  1228. Mips_address
  1229. output_address() const
  1230. {
  1231. return (this->object_->output_section(this->shndx_)->address()
  1232. + this->object_->output_section_offset(this->shndx_));
  1233. }
  1234. private:
  1235. // The object of this stub section.
  1236. Mips_relobj<size, big_endian>* object_;
  1237. // The section index of this stub section.
  1238. unsigned int shndx_;
  1239. // The symbol index, if stub is for a local function.
  1240. unsigned int r_sym_;
  1241. // The symbol, if stub is for a global function.
  1242. Mips_symbol<size>* gsym_;
  1243. // True if we found R_MIPS_NONE relocation in this stub.
  1244. bool found_r_mips_none_;
  1245. };
  1246. // Mips_relobj class.
  1247. template<int size, bool big_endian>
  1248. class Mips_relobj : public Sized_relobj_file<size, big_endian>
  1249. {
  1250. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1251. typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
  1252. Mips16_stubs_int_map;
  1253. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  1254. public:
  1255. Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
  1256. const typename elfcpp::Ehdr<size, big_endian>& ehdr)
  1257. : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
  1258. processor_specific_flags_(0), local_symbol_is_mips16_(),
  1259. local_symbol_is_micromips_(), mips16_stub_sections_(),
  1260. local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
  1261. local_mips16_call_stubs_(), gp_(0), got_info_(NULL),
  1262. section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
  1263. section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U), gprmask_(0),
  1264. cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
  1265. {
  1266. this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
  1267. this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
  1268. this->is_n64_ = elfcpp::abi_64(ehdr.get_e_ident()[elfcpp::EI_CLASS]);
  1269. }
  1270. ~Mips_relobj()
  1271. { }
  1272. // Downcast a base pointer to a Mips_relobj pointer. This is
  1273. // not type-safe but we only use Mips_relobj not the base class.
  1274. static Mips_relobj<size, big_endian>*
  1275. as_mips_relobj(Relobj* relobj)
  1276. { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
  1277. // Downcast a base pointer to a Mips_relobj pointer. This is
  1278. // not type-safe but we only use Mips_relobj not the base class.
  1279. static const Mips_relobj<size, big_endian>*
  1280. as_mips_relobj(const Relobj* relobj)
  1281. { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
  1282. // Processor-specific flags in ELF file header. This is valid only after
  1283. // reading symbols.
  1284. elfcpp::Elf_Word
  1285. processor_specific_flags() const
  1286. { return this->processor_specific_flags_; }
  1287. // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
  1288. // index. This is only valid after do_count_local_symbol is called.
  1289. bool
  1290. local_symbol_is_mips16(unsigned int r_sym) const
  1291. {
  1292. gold_assert(r_sym < this->local_symbol_is_mips16_.size());
  1293. return this->local_symbol_is_mips16_[r_sym];
  1294. }
  1295. // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
  1296. // index. This is only valid after do_count_local_symbol is called.
  1297. bool
  1298. local_symbol_is_micromips(unsigned int r_sym) const
  1299. {
  1300. gold_assert(r_sym < this->local_symbol_is_micromips_.size());
  1301. return this->local_symbol_is_micromips_[r_sym];
  1302. }
  1303. // Get or create MIPS16 stub section.
  1304. Mips16_stub_section<size, big_endian>*
  1305. get_mips16_stub_section(unsigned int shndx)
  1306. {
  1307. typename Mips16_stubs_int_map::const_iterator it =
  1308. this->mips16_stub_sections_.find(shndx);
  1309. if (it != this->mips16_stub_sections_.end())
  1310. return (*it).second;
  1311. Mips16_stub_section<size, big_endian>* stub_section =
  1312. new Mips16_stub_section<size, big_endian>(this, shndx);
  1313. this->mips16_stub_sections_.insert(
  1314. std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
  1315. stub_section->shndx(), stub_section));
  1316. return stub_section;
  1317. }
  1318. // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
  1319. // object doesn't have fn stub for R_SYM.
  1320. Mips16_stub_section<size, big_endian>*
  1321. get_local_mips16_fn_stub(unsigned int r_sym) const
  1322. {
  1323. typename Mips16_stubs_int_map::const_iterator it =
  1324. this->local_mips16_fn_stubs_.find(r_sym);
  1325. if (it != this->local_mips16_fn_stubs_.end())
  1326. return (*it).second;
  1327. return NULL;
  1328. }
  1329. // Record that this object has MIPS16 fn stub for local symbol. This method
  1330. // is only called if we decided not to discard the stub.
  1331. void
  1332. add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
  1333. {
  1334. gold_assert(stub->is_for_local_function());
  1335. unsigned int r_sym = stub->r_sym();
  1336. this->local_mips16_fn_stubs_.insert(
  1337. std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
  1338. r_sym, stub));
  1339. }
  1340. // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
  1341. // object doesn't have call stub for R_SYM.
  1342. Mips16_stub_section<size, big_endian>*
  1343. get_local_mips16_call_stub(unsigned int r_sym) const
  1344. {
  1345. typename Mips16_stubs_int_map::const_iterator it =
  1346. this->local_mips16_call_stubs_.find(r_sym);
  1347. if (it != this->local_mips16_call_stubs_.end())
  1348. return (*it).second;
  1349. return NULL;
  1350. }
  1351. // Record that this object has MIPS16 call stub for local symbol. This method
  1352. // is only called if we decided not to discard the stub.
  1353. void
  1354. add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
  1355. {
  1356. gold_assert(stub->is_for_local_function());
  1357. unsigned int r_sym = stub->r_sym();
  1358. this->local_mips16_call_stubs_.insert(
  1359. std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
  1360. r_sym, stub));
  1361. }
  1362. // Record that we found "non 16-bit" call relocation against local symbol
  1363. // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
  1364. // is one.
  1365. void
  1366. add_local_non_16bit_call(unsigned int symndx)
  1367. { this->local_non_16bit_calls_.insert(symndx); }
  1368. // Return true if there is any "non 16-bit" call relocation against local
  1369. // symbol SYMNDX in this object.
  1370. bool
  1371. has_local_non_16bit_call_relocs(unsigned int symndx)
  1372. {
  1373. return (this->local_non_16bit_calls_.find(symndx)
  1374. != this->local_non_16bit_calls_.end());
  1375. }
  1376. // Record that we found 16-bit call relocation R_MIPS16_26 against local
  1377. // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
  1378. // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
  1379. void
  1380. add_local_16bit_call(unsigned int symndx)
  1381. { this->local_16bit_calls_.insert(symndx); }
  1382. // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
  1383. // symbol SYMNDX in this object.
  1384. bool
  1385. has_local_16bit_call_relocs(unsigned int symndx)
  1386. {
  1387. return (this->local_16bit_calls_.find(symndx)
  1388. != this->local_16bit_calls_.end());
  1389. }
  1390. // Get gp value that was used to create this object.
  1391. Mips_address
  1392. gp_value() const
  1393. { return this->gp_; }
  1394. // Return whether the object is a PIC object.
  1395. bool
  1396. is_pic() const
  1397. { return this->is_pic_; }
  1398. // Return whether the object uses N32 ABI.
  1399. bool
  1400. is_n32() const
  1401. { return this->is_n32_; }
  1402. // Return whether the object uses N64 ABI.
  1403. bool
  1404. is_n64() const
  1405. { return this->is_n64_; }
  1406. // Return whether the object uses NewABI conventions.
  1407. bool
  1408. is_newabi() const
  1409. { return this->is_n32_ || this->is_n64_; }
  1410. // Return Mips_got_info for this object.
  1411. Mips_got_info<size, big_endian>*
  1412. get_got_info() const
  1413. { return this->got_info_; }
  1414. // Return Mips_got_info for this object. Create new info if it doesn't exist.
  1415. Mips_got_info<size, big_endian>*
  1416. get_or_create_got_info()
  1417. {
  1418. if (!this->got_info_)
  1419. this->got_info_ = new Mips_got_info<size, big_endian>();
  1420. return this->got_info_;
  1421. }
  1422. // Set Mips_got_info for this object.
  1423. void
  1424. set_got_info(Mips_got_info<size, big_endian>* got_info)
  1425. { this->got_info_ = got_info; }
  1426. // Whether a section SHDNX is a MIPS16 stub section. This is only valid
  1427. // after do_read_symbols is called.
  1428. bool
  1429. is_mips16_stub_section(unsigned int shndx)
  1430. {
  1431. return (is_mips16_fn_stub_section(shndx)
  1432. || is_mips16_call_stub_section(shndx)
  1433. || is_mips16_call_fp_stub_section(shndx));
  1434. }
  1435. // Return TRUE if relocations in section SHNDX can refer directly to a
  1436. // MIPS16 function rather than to a hard-float stub. This is only valid
  1437. // after do_read_symbols is called.
  1438. bool
  1439. section_allows_mips16_refs(unsigned int shndx)
  1440. {
  1441. return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
  1442. }
  1443. // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
  1444. // after do_read_symbols is called.
  1445. bool
  1446. is_mips16_fn_stub_section(unsigned int shndx)
  1447. {
  1448. gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
  1449. return this->section_is_mips16_fn_stub_[shndx];
  1450. }
  1451. // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
  1452. // after do_read_symbols is called.
  1453. bool
  1454. is_mips16_call_stub_section(unsigned int shndx)
  1455. {
  1456. gold_assert(shndx < this->section_is_mips16_call_stub_.size());
  1457. return this->section_is_mips16_call_stub_[shndx];
  1458. }
  1459. // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
  1460. // valid after do_read_symbols is called.
  1461. bool
  1462. is_mips16_call_fp_stub_section(unsigned int shndx)
  1463. {
  1464. gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
  1465. return this->section_is_mips16_call_fp_stub_[shndx];
  1466. }
  1467. // Discard MIPS16 stub secions that are not needed.
  1468. void
  1469. discard_mips16_stub_sections(Symbol_table* symtab);
  1470. // Return gprmask from the .reginfo section of this object.
  1471. Valtype
  1472. gprmask() const
  1473. { return this->gprmask_; }
  1474. // Return cprmask1 from the .reginfo section of this object.
  1475. Valtype
  1476. cprmask1() const
  1477. { return this->cprmask1_; }
  1478. // Return cprmask2 from the .reginfo section of this object.
  1479. Valtype
  1480. cprmask2() const
  1481. { return this->cprmask2_; }
  1482. // Return cprmask3 from the .reginfo section of this object.
  1483. Valtype
  1484. cprmask3() const
  1485. { return this->cprmask3_; }
  1486. // Return cprmask4 from the .reginfo section of this object.
  1487. Valtype
  1488. cprmask4() const
  1489. { return this->cprmask4_; }
  1490. protected:
  1491. // Count the local symbols.
  1492. void
  1493. do_count_local_symbols(Stringpool_template<char>*,
  1494. Stringpool_template<char>*);
  1495. // Read the symbol information.
  1496. void
  1497. do_read_symbols(Read_symbols_data* sd);
  1498. private:
  1499. // processor-specific flags in ELF file header.
  1500. elfcpp::Elf_Word processor_specific_flags_;
  1501. // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
  1502. // This is only valid after do_count_local_symbol is called.
  1503. std::vector<bool> local_symbol_is_mips16_;
  1504. // Bit vector to tell if a local symbol is a microMIPS symbol or not.
  1505. // This is only valid after do_count_local_symbol is called.
  1506. std::vector<bool> local_symbol_is_micromips_;
  1507. // Map from section index to the MIPS16 stub for that section. This contains
  1508. // all stubs found in this object.
  1509. Mips16_stubs_int_map mips16_stub_sections_;
  1510. // Local symbols that have "non 16-bit" call relocation. This relocation
  1511. // would need to refer to a MIPS16 fn stub, if there is one.
  1512. std::set<unsigned int> local_non_16bit_calls_;
  1513. // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
  1514. // call or call_fp stubs will only be needed if there is some R_MIPS16_26
  1515. // relocation that refers to the stub symbol.
  1516. std::set<unsigned int> local_16bit_calls_;
  1517. // Map from local symbol index to the MIPS16 fn stub for that symbol.
  1518. // This contains only the stubs that we decided not to discard.
  1519. Mips16_stubs_int_map local_mips16_fn_stubs_;
  1520. // Map from local symbol index to the MIPS16 call stub for that symbol.
  1521. // This contains only the stubs that we decided not to discard.
  1522. Mips16_stubs_int_map local_mips16_call_stubs_;
  1523. // gp value that was used to create this object.
  1524. Mips_address gp_;
  1525. // Whether the object is a PIC object.
  1526. bool is_pic_ : 1;
  1527. // Whether the object uses N32 ABI.
  1528. bool is_n32_ : 1;
  1529. // Whether the object uses N64 ABI.
  1530. bool is_n64_ : 1;
  1531. // The Mips_got_info for this object.
  1532. Mips_got_info<size, big_endian>* got_info_;
  1533. // Bit vector to tell if a section is a MIPS16 fn stub section or not.
  1534. // This is only valid after do_read_symbols is called.
  1535. std::vector<bool> section_is_mips16_fn_stub_;
  1536. // Bit vector to tell if a section is a MIPS16 call stub section or not.
  1537. // This is only valid after do_read_symbols is called.
  1538. std::vector<bool> section_is_mips16_call_stub_;
  1539. // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
  1540. // This is only valid after do_read_symbols is called.
  1541. std::vector<bool> section_is_mips16_call_fp_stub_;
  1542. // .pdr section index.
  1543. unsigned int pdr_shndx_;
  1544. // gprmask from the .reginfo section of this object.
  1545. Valtype gprmask_;
  1546. // cprmask1 from the .reginfo section of this object.
  1547. Valtype cprmask1_;
  1548. // cprmask2 from the .reginfo section of this object.
  1549. Valtype cprmask2_;
  1550. // cprmask3 from the .reginfo section of this object.
  1551. Valtype cprmask3_;
  1552. // cprmask4 from the .reginfo section of this object.
  1553. Valtype cprmask4_;
  1554. };
  1555. // Mips_output_data_got class.
  1556. template<int size, bool big_endian>
  1557. class Mips_output_data_got : public Output_data_got<size, big_endian>
  1558. {
  1559. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1560. typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
  1561. Reloc_section;
  1562. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  1563. public:
  1564. Mips_output_data_got(Target_mips<size, big_endian>* target,
  1565. Symbol_table* symtab, Layout* layout)
  1566. : Output_data_got<size, big_endian>(), target_(target),
  1567. symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
  1568. first_global_got_dynsym_index_(-1U), primary_got_(NULL),
  1569. secondary_got_relocs_()
  1570. {
  1571. this->master_got_info_ = new Mips_got_info<size, big_endian>();
  1572. this->set_addralign(16);
  1573. }
  1574. // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
  1575. // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
  1576. void
  1577. record_local_got_symbol(Mips_relobj<size, big_endian>* object,
  1578. unsigned int symndx, Mips_address addend,
  1579. unsigned int r_type, unsigned int shndx)
  1580. {
  1581. this->master_got_info_->record_local_got_symbol(object, symndx, addend,
  1582. r_type, shndx);
  1583. }
  1584. // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
  1585. // in OBJECT. FOR_CALL is true if the caller is only interested in
  1586. // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
  1587. // relocation.
  1588. void
  1589. record_global_got_symbol(Mips_symbol<size>* mips_sym,
  1590. Mips_relobj<size, big_endian>* object,
  1591. unsigned int r_type, bool dyn_reloc, bool for_call)
  1592. {
  1593. this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
  1594. dyn_reloc, for_call);
  1595. }
  1596. // Record that OBJECT has a page relocation against symbol SYMNDX and
  1597. // that ADDEND is the addend for that relocation.
  1598. void
  1599. record_got_page_entry(Mips_relobj<size, big_endian>* object,
  1600. unsigned int symndx, int addend)
  1601. { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
  1602. // Add a static entry for the GOT entry at OFFSET. GSYM is a global
  1603. // symbol and R_TYPE is the code of a dynamic relocation that needs to be
  1604. // applied in a static link.
  1605. void
  1606. add_static_reloc(unsigned int got_offset, unsigned int r_type,
  1607. Mips_symbol<size>* gsym)
  1608. { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
  1609. // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
  1610. // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
  1611. // relocation that needs to be applied in a static link.
  1612. void
  1613. add_static_reloc(unsigned int got_offset, unsigned int r_type,
  1614. Sized_relobj_file<size, big_endian>* relobj,
  1615. unsigned int index)
  1616. {
  1617. this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
  1618. index));
  1619. }
  1620. // Record that global symbol GSYM has R_TYPE dynamic relocation in the
  1621. // secondary GOT at OFFSET.
  1622. void
  1623. add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
  1624. Mips_symbol<size>* gsym)
  1625. {
  1626. this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
  1627. r_type, gsym));
  1628. }
  1629. // Update GOT entry at OFFSET with VALUE.
  1630. void
  1631. update_got_entry(unsigned int offset, Mips_address value)
  1632. {
  1633. elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
  1634. }
  1635. // Return the number of entries in local part of the GOT. This includes
  1636. // local entries, page entries and 2 reserved entries.
  1637. unsigned int
  1638. get_local_gotno() const
  1639. {
  1640. if (!this->multi_got())
  1641. {
  1642. return (2 + this->master_got_info_->local_gotno()
  1643. + this->master_got_info_->page_gotno());
  1644. }
  1645. else
  1646. return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
  1647. }
  1648. // Return dynamic symbol table index of the first symbol with global GOT
  1649. // entry.
  1650. unsigned int
  1651. first_global_got_dynsym_index() const
  1652. { return this->first_global_got_dynsym_index_; }
  1653. // Set dynamic symbol table index of the first symbol with global GOT entry.
  1654. void
  1655. set_first_global_got_dynsym_index(unsigned int index)
  1656. { this->first_global_got_dynsym_index_ = index; }
  1657. // Lay out the GOT. Add local, global and TLS entries. If GOT is
  1658. // larger than 64K, create multi-GOT.
  1659. void
  1660. lay_out_got(Layout* layout, Symbol_table* symtab,
  1661. const Input_objects* input_objects);
  1662. // Create multi-GOT. For every GOT, add local, global and TLS entries.
  1663. void
  1664. lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
  1665. // Attempt to merge GOTs of different input objects.
  1666. void
  1667. merge_gots(const Input_objects* input_objects);
  1668. // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
  1669. // this would lead to overflow, true if they were merged successfully.
  1670. bool
  1671. merge_got_with(Mips_got_info<size, big_endian>* from,
  1672. Mips_relobj<size, big_endian>* object,
  1673. Mips_got_info<size, big_endian>* to);
  1674. // Return the offset of GOT page entry for VALUE. For multi-GOT links,
  1675. // use OBJECT's GOT.
  1676. unsigned int
  1677. get_got_page_offset(Mips_address value,
  1678. const Mips_relobj<size, big_endian>* object)
  1679. {
  1680. Mips_got_info<size, big_endian>* g = (!this->multi_got()
  1681. ? this->master_got_info_
  1682. : object->get_got_info());
  1683. gold_assert(g != NULL);
  1684. return g->get_got_page_offset(value, this);
  1685. }
  1686. // Return the GOT offset of type GOT_TYPE of the global symbol
  1687. // GSYM. For multi-GOT links, use OBJECT's GOT.
  1688. unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
  1689. Mips_relobj<size, big_endian>* object) const
  1690. {
  1691. if (!this->multi_got())
  1692. return gsym->got_offset(got_type);
  1693. else
  1694. {
  1695. Mips_got_info<size, big_endian>* g = object->get_got_info();
  1696. gold_assert(g != NULL);
  1697. return gsym->got_offset(g->multigot_got_type(got_type));
  1698. }
  1699. }
  1700. // Return the GOT offset of type GOT_TYPE of the local symbol
  1701. // SYMNDX.
  1702. unsigned int
  1703. got_offset(unsigned int symndx, unsigned int got_type,
  1704. Sized_relobj_file<size, big_endian>* object) const
  1705. { return object->local_got_offset(symndx, got_type); }
  1706. // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
  1707. unsigned int
  1708. tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
  1709. {
  1710. Mips_got_info<size, big_endian>* g = (!this->multi_got()
  1711. ? this->master_got_info_
  1712. : object->get_got_info());
  1713. gold_assert(g != NULL);
  1714. return g->tls_ldm_offset();
  1715. }
  1716. // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
  1717. void
  1718. set_tls_ldm_offset(unsigned int tls_ldm_offset,
  1719. Mips_relobj<size, big_endian>* object)
  1720. {
  1721. Mips_got_info<size, big_endian>* g = (!this->multi_got()
  1722. ? this->master_got_info_
  1723. : object->get_got_info());
  1724. gold_assert(g != NULL);
  1725. g->set_tls_ldm_offset(tls_ldm_offset);
  1726. }
  1727. // Return true for multi-GOT links.
  1728. bool
  1729. multi_got() const
  1730. { return this->primary_got_ != NULL; }
  1731. // Return the offset of OBJECT's GOT from the start of .got section.
  1732. unsigned int
  1733. get_got_offset(const Mips_relobj<size, big_endian>* object)
  1734. {
  1735. if (!this->multi_got())
  1736. return 0;
  1737. else
  1738. {
  1739. Mips_got_info<size, big_endian>* g = object->get_got_info();
  1740. return g != NULL ? g->offset() : 0;
  1741. }
  1742. }
  1743. // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
  1744. void
  1745. add_reloc_only_entries()
  1746. { this->master_got_info_->add_reloc_only_entries(this); }
  1747. // Return offset of the primary GOT's entry for global symbol.
  1748. unsigned int
  1749. get_primary_got_offset(const Mips_symbol<size>* sym) const
  1750. {
  1751. gold_assert(sym->global_got_area() != GGA_NONE);
  1752. return (this->get_local_gotno() + sym->dynsym_index()
  1753. - this->first_global_got_dynsym_index()) * size/8;
  1754. }
  1755. // For the entry at offset GOT_OFFSET, return its offset from the gp.
  1756. // Input argument GOT_OFFSET is always global offset from the start of
  1757. // .got section, for both single and multi-GOT links.
  1758. // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
  1759. // links, the return value is object_got_offset - 0x7FF0, where
  1760. // object_got_offset is offset in the OBJECT's GOT.
  1761. int
  1762. gp_offset(unsigned int got_offset,
  1763. const Mips_relobj<size, big_endian>* object) const
  1764. {
  1765. return (this->address() + got_offset
  1766. - this->target_->adjusted_gp_value(object));
  1767. }
  1768. protected:
  1769. // Write out the GOT table.
  1770. void
  1771. do_write(Output_file*);
  1772. private:
  1773. // This class represent dynamic relocations that need to be applied by
  1774. // gold because we are using TLS relocations in a static link.
  1775. class Static_reloc
  1776. {
  1777. public:
  1778. Static_reloc(unsigned int got_offset, unsigned int r_type,
  1779. Mips_symbol<size>* gsym)
  1780. : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
  1781. { this->u_.global.symbol = gsym; }
  1782. Static_reloc(unsigned int got_offset, unsigned int r_type,
  1783. Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
  1784. : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
  1785. {
  1786. this->u_.local.relobj = relobj;
  1787. this->u_.local.index = index;
  1788. }
  1789. // Return the GOT offset.
  1790. unsigned int
  1791. got_offset() const
  1792. { return this->got_offset_; }
  1793. // Relocation type.
  1794. unsigned int
  1795. r_type() const
  1796. { return this->r_type_; }
  1797. // Whether the symbol is global or not.
  1798. bool
  1799. symbol_is_global() const
  1800. { return this->symbol_is_global_; }
  1801. // For a relocation against a global symbol, the global symbol.
  1802. Mips_symbol<size>*
  1803. symbol() const
  1804. {
  1805. gold_assert(this->symbol_is_global_);
  1806. return this->u_.global.symbol;
  1807. }
  1808. // For a relocation against a local symbol, the defining object.
  1809. Sized_relobj_file<size, big_endian>*
  1810. relobj() const
  1811. {
  1812. gold_assert(!this->symbol_is_global_);
  1813. return this->u_.local.relobj;
  1814. }
  1815. // For a relocation against a local symbol, the local symbol index.
  1816. unsigned int
  1817. index() const
  1818. {
  1819. gold_assert(!this->symbol_is_global_);
  1820. return this->u_.local.index;
  1821. }
  1822. private:
  1823. // GOT offset of the entry to which this relocation is applied.
  1824. unsigned int got_offset_;
  1825. // Type of relocation.
  1826. unsigned int r_type_;
  1827. // Whether this relocation is against a global symbol.
  1828. bool symbol_is_global_;
  1829. // A global or local symbol.
  1830. union
  1831. {
  1832. struct
  1833. {
  1834. // For a global symbol, the symbol itself.
  1835. Mips_symbol<size>* symbol;
  1836. } global;
  1837. struct
  1838. {
  1839. // For a local symbol, the object defining object.
  1840. Sized_relobj_file<size, big_endian>* relobj;
  1841. // For a local symbol, the symbol index.
  1842. unsigned int index;
  1843. } local;
  1844. } u_;
  1845. };
  1846. // The target.
  1847. Target_mips<size, big_endian>* target_;
  1848. // The symbol table.
  1849. Symbol_table* symbol_table_;
  1850. // The layout.
  1851. Layout* layout_;
  1852. // Static relocs to be applied to the GOT.
  1853. std::vector<Static_reloc> static_relocs_;
  1854. // .got section view.
  1855. unsigned char* got_view_;
  1856. // The dynamic symbol table index of the first symbol with global GOT entry.
  1857. unsigned int first_global_got_dynsym_index_;
  1858. // The master GOT information.
  1859. Mips_got_info<size, big_endian>* master_got_info_;
  1860. // The primary GOT information.
  1861. Mips_got_info<size, big_endian>* primary_got_;
  1862. // Secondary GOT fixups.
  1863. std::vector<Static_reloc> secondary_got_relocs_;
  1864. };
  1865. // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
  1866. // two ways of creating these interfaces. The first is to add:
  1867. //
  1868. // lui $25,%hi(func)
  1869. // j func
  1870. // addiu $25,$25,%lo(func)
  1871. //
  1872. // to a separate trampoline section. The second is to add:
  1873. //
  1874. // lui $25,%hi(func)
  1875. // addiu $25,$25,%lo(func)
  1876. //
  1877. // immediately before a PIC function "func", but only if a function is at the
  1878. // beginning of the section, and the section is not too heavily aligned (i.e we
  1879. // would need to add no more than 2 nops before the stub.)
  1880. //
  1881. // We only create stubs of the first type.
  1882. template<int size, bool big_endian>
  1883. class Mips_output_data_la25_stub : public Output_section_data
  1884. {
  1885. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1886. public:
  1887. Mips_output_data_la25_stub()
  1888. : Output_section_data(size == 32 ? 4 : 8), symbols_()
  1889. { }
  1890. // Create LA25 stub for a symbol.
  1891. void
  1892. create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
  1893. Mips_symbol<size>* gsym);
  1894. // Return output address of a stub.
  1895. Mips_address
  1896. stub_address(const Mips_symbol<size>* sym) const
  1897. {
  1898. gold_assert(sym->has_la25_stub());
  1899. return this->address() + sym->la25_stub_offset();
  1900. }
  1901. protected:
  1902. void
  1903. do_adjust_output_section(Output_section* os)
  1904. { os->set_entsize(0); }
  1905. private:
  1906. // Template for standard LA25 stub.
  1907. static const uint32_t la25_stub_entry[];
  1908. // Template for microMIPS LA25 stub.
  1909. static const uint32_t la25_stub_micromips_entry[];
  1910. // Set the final size.
  1911. void
  1912. set_final_data_size()
  1913. { this->set_data_size(this->symbols_.size() * 16); }
  1914. // Create a symbol for SYM stub's value and size, to help make the
  1915. // disassembly easier to read.
  1916. void
  1917. create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
  1918. Target_mips<size, big_endian>* target, uint64_t symsize);
  1919. // Write out the LA25 stub section.
  1920. void
  1921. do_write(Output_file*);
  1922. // Symbols that have LA25 stubs.
  1923. Unordered_set<Mips_symbol<size>*> symbols_;
  1924. };
  1925. // A class to handle the PLT data.
  1926. template<int size, bool big_endian>
  1927. class Mips_output_data_plt : public Output_section_data
  1928. {
  1929. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  1930. typedef Output_data_reloc<elfcpp::SHT_REL, true,
  1931. size, big_endian> Reloc_section;
  1932. public:
  1933. // Create the PLT section. The ordinary .got section is an argument,
  1934. // since we need to refer to the start.
  1935. Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
  1936. Target_mips<size, big_endian>* target)
  1937. : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
  1938. plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
  1939. target_(target)
  1940. {
  1941. this->rel_ = new Reloc_section(false);
  1942. layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
  1943. elfcpp::SHF_ALLOC, this->rel_,
  1944. ORDER_DYNAMIC_PLT_RELOCS, false);
  1945. }
  1946. // Add an entry to the PLT for a symbol referenced by r_type relocation.
  1947. void
  1948. add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
  1949. // Return the .rel.plt section data.
  1950. const Reloc_section*
  1951. rel_plt() const
  1952. { return this->rel_; }
  1953. // Return the number of PLT entries.
  1954. unsigned int
  1955. entry_count() const
  1956. { return this->symbols_.size(); }
  1957. // Return the offset of the first non-reserved PLT entry.
  1958. unsigned int
  1959. first_plt_entry_offset() const
  1960. { return sizeof(plt0_entry_o32); }
  1961. // Return the size of a PLT entry.
  1962. unsigned int
  1963. plt_entry_size() const
  1964. { return sizeof(plt_entry); }
  1965. // Set final PLT offsets. For each symbol, determine whether standard or
  1966. // compressed (MIPS16 or microMIPS) PLT entry is used.
  1967. void
  1968. set_plt_offsets();
  1969. // Return the offset of the first standard PLT entry.
  1970. unsigned int
  1971. first_mips_plt_offset() const
  1972. { return this->plt_header_size_; }
  1973. // Return the offset of the first compressed PLT entry.
  1974. unsigned int
  1975. first_comp_plt_offset() const
  1976. { return this->plt_header_size_ + this->plt_mips_offset_; }
  1977. // Return whether there are any standard PLT entries.
  1978. bool
  1979. has_standard_entries() const
  1980. { return this->plt_mips_offset_ > 0; }
  1981. // Return the output address of standard PLT entry.
  1982. Mips_address
  1983. mips_entry_address(const Mips_symbol<size>* sym) const
  1984. {
  1985. gold_assert (sym->has_mips_plt_offset());
  1986. return (this->address() + this->first_mips_plt_offset()
  1987. + sym->mips_plt_offset());
  1988. }
  1989. // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
  1990. Mips_address
  1991. comp_entry_address(const Mips_symbol<size>* sym) const
  1992. {
  1993. gold_assert (sym->has_comp_plt_offset());
  1994. return (this->address() + this->first_comp_plt_offset()
  1995. + sym->comp_plt_offset());
  1996. }
  1997. protected:
  1998. void
  1999. do_adjust_output_section(Output_section* os)
  2000. { os->set_entsize(0); }
  2001. // Write to a map file.
  2002. void
  2003. do_print_to_mapfile(Mapfile* mapfile) const
  2004. { mapfile->print_output_data(this, _(".plt")); }
  2005. private:
  2006. // Template for the first PLT entry.
  2007. static const uint32_t plt0_entry_o32[];
  2008. static const uint32_t plt0_entry_n32[];
  2009. static const uint32_t plt0_entry_n64[];
  2010. static const uint32_t plt0_entry_micromips_o32[];
  2011. static const uint32_t plt0_entry_micromips32_o32[];
  2012. // Template for subsequent PLT entries.
  2013. static const uint32_t plt_entry[];
  2014. static const uint32_t plt_entry_mips16_o32[];
  2015. static const uint32_t plt_entry_micromips_o32[];
  2016. static const uint32_t plt_entry_micromips32_o32[];
  2017. // Set the final size.
  2018. void
  2019. set_final_data_size()
  2020. {
  2021. this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
  2022. + this->plt_comp_offset_);
  2023. }
  2024. // Write out the PLT data.
  2025. void
  2026. do_write(Output_file*);
  2027. // Return whether the plt header contains microMIPS code. For the sake of
  2028. // cache alignment always use a standard header whenever any standard entries
  2029. // are present even if microMIPS entries are present as well. This also lets
  2030. // the microMIPS header rely on the value of $v0 only set by microMIPS
  2031. // entries, for a small size reduction.
  2032. bool
  2033. is_plt_header_compressed() const
  2034. {
  2035. gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
  2036. return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
  2037. }
  2038. // Return the size of the PLT header.
  2039. unsigned int
  2040. get_plt_header_size() const
  2041. {
  2042. if (this->target_->is_output_n64())
  2043. return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
  2044. else if (this->target_->is_output_n32())
  2045. return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
  2046. else if (!this->is_plt_header_compressed())
  2047. return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
  2048. else if (this->target_->use_32bit_micromips_instructions())
  2049. return (2 * sizeof(plt0_entry_micromips32_o32)
  2050. / sizeof(plt0_entry_micromips32_o32[0]));
  2051. else
  2052. return (2 * sizeof(plt0_entry_micromips_o32)
  2053. / sizeof(plt0_entry_micromips_o32[0]));
  2054. }
  2055. // Return the PLT header entry.
  2056. const uint32_t*
  2057. get_plt_header_entry() const
  2058. {
  2059. if (this->target_->is_output_n64())
  2060. return plt0_entry_n64;
  2061. else if (this->target_->is_output_n32())
  2062. return plt0_entry_n32;
  2063. else if (!this->is_plt_header_compressed())
  2064. return plt0_entry_o32;
  2065. else if (this->target_->use_32bit_micromips_instructions())
  2066. return plt0_entry_micromips32_o32;
  2067. else
  2068. return plt0_entry_micromips_o32;
  2069. }
  2070. // Return the size of the standard PLT entry.
  2071. unsigned int
  2072. standard_plt_entry_size() const
  2073. { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
  2074. // Return the size of the compressed PLT entry.
  2075. unsigned int
  2076. compressed_plt_entry_size() const
  2077. {
  2078. gold_assert(!this->target_->is_output_newabi());
  2079. if (!this->target_->is_output_micromips())
  2080. return (2 * sizeof(plt_entry_mips16_o32)
  2081. / sizeof(plt_entry_mips16_o32[0]));
  2082. else if (this->target_->use_32bit_micromips_instructions())
  2083. return (2 * sizeof(plt_entry_micromips32_o32)
  2084. / sizeof(plt_entry_micromips32_o32[0]));
  2085. else
  2086. return (2 * sizeof(plt_entry_micromips_o32)
  2087. / sizeof(plt_entry_micromips_o32[0]));
  2088. }
  2089. // The reloc section.
  2090. Reloc_section* rel_;
  2091. // The .got.plt section.
  2092. Output_data_space* got_plt_;
  2093. // Symbols that have PLT entry.
  2094. std::vector<Mips_symbol<size>*> symbols_;
  2095. // The offset of the next standard PLT entry to create.
  2096. unsigned int plt_mips_offset_;
  2097. // The offset of the next compressed PLT entry to create.
  2098. unsigned int plt_comp_offset_;
  2099. // The size of the PLT header in bytes.
  2100. unsigned int plt_header_size_;
  2101. // The target.
  2102. Target_mips<size, big_endian>* target_;
  2103. };
  2104. // A class to handle the .MIPS.stubs data.
  2105. template<int size, bool big_endian>
  2106. class Mips_output_data_mips_stubs : public Output_section_data
  2107. {
  2108. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  2109. public:
  2110. Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
  2111. : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
  2112. stub_offsets_are_set_(false), target_(target)
  2113. { }
  2114. // Create entry for a symbol.
  2115. void
  2116. make_entry(Mips_symbol<size>*);
  2117. // Remove entry for a symbol.
  2118. void
  2119. remove_entry(Mips_symbol<size>* gsym);
  2120. // Set stub offsets for symbols. This method expects that the number of
  2121. // entries in dynamic symbol table is set.
  2122. void
  2123. set_lazy_stub_offsets();
  2124. void
  2125. set_needs_dynsym_value();
  2126. // Set the number of entries in dynamic symbol table.
  2127. void
  2128. set_dynsym_count(unsigned int dynsym_count)
  2129. { this->dynsym_count_ = dynsym_count; }
  2130. // Return maximum size of the stub, ie. the stub size if the dynamic symbol
  2131. // count is greater than 0x10000. If the dynamic symbol count is less than
  2132. // 0x10000, the stub will be 4 bytes smaller.
  2133. // There's no disadvantage from using microMIPS code here, so for the sake of
  2134. // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
  2135. // output produced at all. This has a benefit of stubs being shorter by
  2136. // 4 bytes each too, unless in the insn32 mode.
  2137. unsigned int
  2138. stub_max_size() const
  2139. {
  2140. if (!this->target_->is_output_micromips()
  2141. || this->target_->use_32bit_micromips_instructions())
  2142. return 20;
  2143. else
  2144. return 16;
  2145. }
  2146. // Return the size of the stub. This method expects that the final dynsym
  2147. // count is set.
  2148. unsigned int
  2149. stub_size() const
  2150. {
  2151. gold_assert(this->dynsym_count_ != -1U);
  2152. if (this->dynsym_count_ > 0x10000)
  2153. return this->stub_max_size();
  2154. else
  2155. return this->stub_max_size() - 4;
  2156. }
  2157. // Return output address of a stub.
  2158. Mips_address
  2159. stub_address(const Mips_symbol<size>* sym) const
  2160. {
  2161. gold_assert(sym->has_lazy_stub());
  2162. return this->address() + sym->lazy_stub_offset();
  2163. }
  2164. protected:
  2165. void
  2166. do_adjust_output_section(Output_section* os)
  2167. { os->set_entsize(0); }
  2168. // Write to a map file.
  2169. void
  2170. do_print_to_mapfile(Mapfile* mapfile) const
  2171. { mapfile->print_output_data(this, _(".MIPS.stubs")); }
  2172. private:
  2173. static const uint32_t lazy_stub_normal_1[];
  2174. static const uint32_t lazy_stub_normal_1_n64[];
  2175. static const uint32_t lazy_stub_normal_2[];
  2176. static const uint32_t lazy_stub_normal_2_n64[];
  2177. static const uint32_t lazy_stub_big[];
  2178. static const uint32_t lazy_stub_big_n64[];
  2179. static const uint32_t lazy_stub_micromips_normal_1[];
  2180. static const uint32_t lazy_stub_micromips_normal_1_n64[];
  2181. static const uint32_t lazy_stub_micromips_normal_2[];
  2182. static const uint32_t lazy_stub_micromips_normal_2_n64[];
  2183. static const uint32_t lazy_stub_micromips_big[];
  2184. static const uint32_t lazy_stub_micromips_big_n64[];
  2185. static const uint32_t lazy_stub_micromips32_normal_1[];
  2186. static const uint32_t lazy_stub_micromips32_normal_1_n64[];
  2187. static const uint32_t lazy_stub_micromips32_normal_2[];
  2188. static const uint32_t lazy_stub_micromips32_normal_2_n64[];
  2189. static const uint32_t lazy_stub_micromips32_big[];
  2190. static const uint32_t lazy_stub_micromips32_big_n64[];
  2191. // Set the final size.
  2192. void
  2193. set_final_data_size()
  2194. { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
  2195. // Write out the .MIPS.stubs data.
  2196. void
  2197. do_write(Output_file*);
  2198. // .MIPS.stubs symbols
  2199. Unordered_set<Mips_symbol<size>*> symbols_;
  2200. // Number of entries in dynamic symbol table.
  2201. unsigned int dynsym_count_;
  2202. // Whether the stub offsets are set.
  2203. bool stub_offsets_are_set_;
  2204. // The target.
  2205. Target_mips<size, big_endian>* target_;
  2206. };
  2207. // This class handles Mips .reginfo output section.
  2208. template<int size, bool big_endian>
  2209. class Mips_output_section_reginfo : public Output_section
  2210. {
  2211. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  2212. public:
  2213. Mips_output_section_reginfo(const char* name, elfcpp::Elf_Word type,
  2214. elfcpp::Elf_Xword flags,
  2215. Target_mips<size, big_endian>* target)
  2216. : Output_section(name, type, flags), target_(target), gprmask_(0),
  2217. cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
  2218. { }
  2219. // Downcast a base pointer to a Mips_output_section_reginfo pointer.
  2220. static Mips_output_section_reginfo<size, big_endian>*
  2221. as_mips_output_section_reginfo(Output_section* os)
  2222. { return static_cast<Mips_output_section_reginfo<size, big_endian>*>(os); }
  2223. // Set masks of the output .reginfo section.
  2224. void
  2225. set_masks(Valtype gprmask, Valtype cprmask1, Valtype cprmask2,
  2226. Valtype cprmask3, Valtype cprmask4)
  2227. {
  2228. this->gprmask_ = gprmask;
  2229. this->cprmask1_ = cprmask1;
  2230. this->cprmask2_ = cprmask2;
  2231. this->cprmask3_ = cprmask3;
  2232. this->cprmask4_ = cprmask4;
  2233. }
  2234. protected:
  2235. // Set the final data size.
  2236. void
  2237. set_final_data_size()
  2238. { this->set_data_size(24); }
  2239. // Write out reginfo section.
  2240. void
  2241. do_write(Output_file* of);
  2242. private:
  2243. Target_mips<size, big_endian>* target_;
  2244. // gprmask of the output .reginfo section.
  2245. Valtype gprmask_;
  2246. // cprmask1 of the output .reginfo section.
  2247. Valtype cprmask1_;
  2248. // cprmask2 of the output .reginfo section.
  2249. Valtype cprmask2_;
  2250. // cprmask3 of the output .reginfo section.
  2251. Valtype cprmask3_;
  2252. // cprmask4 of the output .reginfo section.
  2253. Valtype cprmask4_;
  2254. };
  2255. // The MIPS target has relocation types which default handling of relocatable
  2256. // relocation cannot process. So we have to extend the default code.
  2257. template<bool big_endian, int sh_type, typename Classify_reloc>
  2258. class Mips_scan_relocatable_relocs :
  2259. public Default_scan_relocatable_relocs<sh_type, Classify_reloc>
  2260. {
  2261. public:
  2262. // Return the strategy to use for a local symbol which is a section
  2263. // symbol, given the relocation type.
  2264. inline Relocatable_relocs::Reloc_strategy
  2265. local_section_strategy(unsigned int r_type, Relobj* object)
  2266. {
  2267. if (sh_type == elfcpp::SHT_RELA)
  2268. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  2269. else
  2270. {
  2271. switch (r_type)
  2272. {
  2273. case elfcpp::R_MIPS_26:
  2274. return Relocatable_relocs::RELOC_SPECIAL;
  2275. default:
  2276. return Default_scan_relocatable_relocs<sh_type, Classify_reloc>::
  2277. local_section_strategy(r_type, object);
  2278. }
  2279. }
  2280. }
  2281. };
  2282. // Mips_copy_relocs class. The only difference from the base class is the
  2283. // method emit_mips, which should be called instead of Copy_reloc_entry::emit.
  2284. // Mips cannot convert all relocation types to dynamic relocs. If a reloc
  2285. // cannot be made dynamic, a COPY reloc is emitted.
  2286. template<int sh_type, int size, bool big_endian>
  2287. class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
  2288. {
  2289. public:
  2290. Mips_copy_relocs()
  2291. : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
  2292. { }
  2293. // Emit any saved relocations which turn out to be needed. This is
  2294. // called after all the relocs have been scanned.
  2295. void
  2296. emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
  2297. Symbol_table*, Layout*, Target_mips<size, big_endian>*);
  2298. private:
  2299. typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
  2300. Copy_reloc_entry;
  2301. // Emit this reloc if appropriate. This is called after we have
  2302. // scanned all the relocations, so we know whether we emitted a
  2303. // COPY relocation for SYM_.
  2304. void
  2305. emit_entry(Copy_reloc_entry& entry,
  2306. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
  2307. Symbol_table* symtab, Layout* layout,
  2308. Target_mips<size, big_endian>* target);
  2309. };
  2310. // Return true if the symbol SYM should be considered to resolve local
  2311. // to the current module, and false otherwise. The logic is taken from
  2312. // GNU ld's method _bfd_elf_symbol_refs_local_p.
  2313. static bool
  2314. symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
  2315. bool local_protected)
  2316. {
  2317. // If it's a local sym, of course we resolve locally.
  2318. if (sym == NULL)
  2319. return true;
  2320. // STV_HIDDEN or STV_INTERNAL ones must be local.
  2321. if (sym->visibility() == elfcpp::STV_HIDDEN
  2322. || sym->visibility() == elfcpp::STV_INTERNAL)
  2323. return true;
  2324. // If we don't have a definition in a regular file, then we can't
  2325. // resolve locally. The sym is either undefined or dynamic.
  2326. if (sym->source() != Symbol::FROM_OBJECT || sym->object()->is_dynamic()
  2327. || sym->is_undefined())
  2328. return false;
  2329. // Forced local symbols resolve locally.
  2330. if (sym->is_forced_local())
  2331. return true;
  2332. // As do non-dynamic symbols.
  2333. if (!has_dynsym_entry)
  2334. return true;
  2335. // At this point, we know the symbol is defined and dynamic. In an
  2336. // executable it must resolve locally, likewise when building symbolic
  2337. // shared libraries.
  2338. if (parameters->options().output_is_executable()
  2339. || parameters->options().Bsymbolic())
  2340. return true;
  2341. // Now deal with defined dynamic symbols in shared libraries. Ones
  2342. // with default visibility might not resolve locally.
  2343. if (sym->visibility() == elfcpp::STV_DEFAULT)
  2344. return false;
  2345. // STV_PROTECTED non-function symbols are local.
  2346. if (sym->type() != elfcpp::STT_FUNC)
  2347. return true;
  2348. // Function pointer equality tests may require that STV_PROTECTED
  2349. // symbols be treated as dynamic symbols. If the address of a
  2350. // function not defined in an executable is set to that function's
  2351. // plt entry in the executable, then the address of the function in
  2352. // a shared library must also be the plt entry in the executable.
  2353. return local_protected;
  2354. }
  2355. // Return TRUE if references to this symbol always reference the symbol in this
  2356. // object.
  2357. static bool
  2358. symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
  2359. {
  2360. return symbol_refs_local(sym, has_dynsym_entry, false);
  2361. }
  2362. // Return TRUE if calls to this symbol always call the version in this object.
  2363. static bool
  2364. symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
  2365. {
  2366. return symbol_refs_local(sym, has_dynsym_entry, true);
  2367. }
  2368. // Compare GOT offsets of two symbols.
  2369. template<int size, bool big_endian>
  2370. static bool
  2371. got_offset_compare(Symbol* sym1, Symbol* sym2)
  2372. {
  2373. Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
  2374. Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
  2375. unsigned int area1 = mips_sym1->global_got_area();
  2376. unsigned int area2 = mips_sym2->global_got_area();
  2377. gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
  2378. // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
  2379. if (area1 != area2)
  2380. return area1 < area2;
  2381. return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
  2382. }
  2383. // This method divides dynamic symbols into symbols that have GOT entry, and
  2384. // symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
  2385. // Mips ABI requires that symbols with the GOT entry must be at the end of
  2386. // dynamic symbol table, and the order in dynamic symbol table must match the
  2387. // order in GOT.
  2388. template<int size, bool big_endian>
  2389. static void
  2390. reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
  2391. std::vector<Symbol*>* non_got_symbols,
  2392. std::vector<Symbol*>* got_symbols)
  2393. {
  2394. for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
  2395. p != dyn_symbols->end();
  2396. ++p)
  2397. {
  2398. Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
  2399. if (mips_sym->global_got_area() == GGA_NORMAL
  2400. || mips_sym->global_got_area() == GGA_RELOC_ONLY)
  2401. got_symbols->push_back(mips_sym);
  2402. else
  2403. non_got_symbols->push_back(mips_sym);
  2404. }
  2405. std::sort(got_symbols->begin(), got_symbols->end(),
  2406. got_offset_compare<size, big_endian>);
  2407. }
  2408. // Functor class for processing the global symbol table.
  2409. template<int size, bool big_endian>
  2410. class Symbol_visitor_check_symbols
  2411. {
  2412. public:
  2413. Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
  2414. Layout* layout, Symbol_table* symtab)
  2415. : target_(target), layout_(layout), symtab_(symtab)
  2416. { }
  2417. void
  2418. operator()(Sized_symbol<size>* sym)
  2419. {
  2420. Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
  2421. if (local_pic_function<size, big_endian>(mips_sym))
  2422. {
  2423. // SYM is a function that might need $25 to be valid on entry.
  2424. // If we're creating a non-PIC relocatable object, mark SYM as
  2425. // being PIC. If we're creating a non-relocatable object with
  2426. // non-PIC branches and jumps to SYM, make sure that SYM has an la25
  2427. // stub.
  2428. if (parameters->options().relocatable())
  2429. {
  2430. if (!parameters->options().output_is_position_independent())
  2431. mips_sym->set_pic();
  2432. }
  2433. else if (mips_sym->has_nonpic_branches())
  2434. {
  2435. this->target_->la25_stub_section(layout_)
  2436. ->create_la25_stub(this->symtab_, this->target_, mips_sym);
  2437. }
  2438. }
  2439. }
  2440. private:
  2441. Target_mips<size, big_endian>* target_;
  2442. Layout* layout_;
  2443. Symbol_table* symtab_;
  2444. };
  2445. template<int size, bool big_endian>
  2446. class Target_mips : public Sized_target<size, big_endian>
  2447. {
  2448. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  2449. typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
  2450. Reloc_section;
  2451. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
  2452. Reloca_section;
  2453. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
  2454. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  2455. public:
  2456. Target_mips(const Target::Target_info* info = &mips_info)
  2457. : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
  2458. got_plt_(NULL), rel_dyn_(NULL), copy_relocs_(),
  2459. dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
  2460. mips_stubs_(NULL), ei_class_(0), mach_(0), layout_(NULL),
  2461. got16_addends_(), entry_symbol_is_compressed_(false), insn32_(false)
  2462. {
  2463. this->add_machine_extensions();
  2464. }
  2465. // The offset of $gp from the beginning of the .got section.
  2466. static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
  2467. // The maximum size of the GOT for it to be addressable using 16-bit
  2468. // offsets from $gp.
  2469. static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
  2470. // Make a new symbol table entry for the Mips target.
  2471. Sized_symbol<size>*
  2472. make_symbol() const
  2473. { return new Mips_symbol<size>(); }
  2474. // Process the relocations to determine unreferenced sections for
  2475. // garbage collection.
  2476. void
  2477. gc_process_relocs(Symbol_table* symtab,
  2478. Layout* layout,
  2479. Sized_relobj_file<size, big_endian>* object,
  2480. unsigned int data_shndx,
  2481. unsigned int sh_type,
  2482. const unsigned char* prelocs,
  2483. size_t reloc_count,
  2484. Output_section* output_section,
  2485. bool needs_special_offset_handling,
  2486. size_t local_symbol_count,
  2487. const unsigned char* plocal_symbols);
  2488. // Scan the relocations to look for symbol adjustments.
  2489. void
  2490. scan_relocs(Symbol_table* symtab,
  2491. Layout* layout,
  2492. Sized_relobj_file<size, big_endian>* object,
  2493. unsigned int data_shndx,
  2494. unsigned int sh_type,
  2495. const unsigned char* prelocs,
  2496. size_t reloc_count,
  2497. Output_section* output_section,
  2498. bool needs_special_offset_handling,
  2499. size_t local_symbol_count,
  2500. const unsigned char* plocal_symbols);
  2501. // Finalize the sections.
  2502. void
  2503. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  2504. // Relocate a section.
  2505. void
  2506. relocate_section(const Relocate_info<size, big_endian>*,
  2507. unsigned int sh_type,
  2508. const unsigned char* prelocs,
  2509. size_t reloc_count,
  2510. Output_section* output_section,
  2511. bool needs_special_offset_handling,
  2512. unsigned char* view,
  2513. Mips_address view_address,
  2514. section_size_type view_size,
  2515. const Reloc_symbol_changes*);
  2516. // Scan the relocs during a relocatable link.
  2517. void
  2518. scan_relocatable_relocs(Symbol_table* symtab,
  2519. Layout* layout,
  2520. Sized_relobj_file<size, big_endian>* object,
  2521. unsigned int data_shndx,
  2522. unsigned int sh_type,
  2523. const unsigned char* prelocs,
  2524. size_t reloc_count,
  2525. Output_section* output_section,
  2526. bool needs_special_offset_handling,
  2527. size_t local_symbol_count,
  2528. const unsigned char* plocal_symbols,
  2529. Relocatable_relocs*);
  2530. // Emit relocations for a section.
  2531. void
  2532. relocate_relocs(const Relocate_info<size, big_endian>*,
  2533. unsigned int sh_type,
  2534. const unsigned char* prelocs,
  2535. size_t reloc_count,
  2536. Output_section* output_section,
  2537. typename elfcpp::Elf_types<size>::Elf_Off
  2538. offset_in_output_section,
  2539. const Relocatable_relocs*,
  2540. unsigned char* view,
  2541. Mips_address view_address,
  2542. section_size_type view_size,
  2543. unsigned char* reloc_view,
  2544. section_size_type reloc_view_size);
  2545. // Perform target-specific processing in a relocatable link. This is
  2546. // only used if we use the relocation strategy RELOC_SPECIAL.
  2547. void
  2548. relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
  2549. unsigned int sh_type,
  2550. const unsigned char* preloc_in,
  2551. size_t relnum,
  2552. Output_section* output_section,
  2553. typename elfcpp::Elf_types<size>::Elf_Off
  2554. offset_in_output_section,
  2555. unsigned char* view,
  2556. Mips_address view_address,
  2557. section_size_type view_size,
  2558. unsigned char* preloc_out);
  2559. // Return whether SYM is defined by the ABI.
  2560. bool
  2561. do_is_defined_by_abi(const Symbol* sym) const
  2562. {
  2563. return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
  2564. || (strcmp(sym->name(), "_gp_disp") == 0)
  2565. || (strcmp(sym->name(), "___tls_get_addr") == 0));
  2566. }
  2567. // Return the number of entries in the GOT.
  2568. unsigned int
  2569. got_entry_count() const
  2570. {
  2571. if (!this->has_got_section())
  2572. return 0;
  2573. return this->got_size() / (size/8);
  2574. }
  2575. // Return the number of entries in the PLT.
  2576. unsigned int
  2577. plt_entry_count() const
  2578. {
  2579. if (this->plt_ == NULL)
  2580. return 0;
  2581. return this->plt_->entry_count();
  2582. }
  2583. // Return the offset of the first non-reserved PLT entry.
  2584. unsigned int
  2585. first_plt_entry_offset() const
  2586. { return this->plt_->first_plt_entry_offset(); }
  2587. // Return the size of each PLT entry.
  2588. unsigned int
  2589. plt_entry_size() const
  2590. { return this->plt_->plt_entry_size(); }
  2591. // Get the GOT section, creating it if necessary.
  2592. Mips_output_data_got<size, big_endian>*
  2593. got_section(Symbol_table*, Layout*);
  2594. // Get the GOT section.
  2595. Mips_output_data_got<size, big_endian>*
  2596. got_section() const
  2597. {
  2598. gold_assert(this->got_ != NULL);
  2599. return this->got_;
  2600. }
  2601. // Get the .MIPS.stubs section, creating it if necessary.
  2602. Mips_output_data_mips_stubs<size, big_endian>*
  2603. mips_stubs_section(Layout* layout);
  2604. // Get the .MIPS.stubs section.
  2605. Mips_output_data_mips_stubs<size, big_endian>*
  2606. mips_stubs_section() const
  2607. {
  2608. gold_assert(this->mips_stubs_ != NULL);
  2609. return this->mips_stubs_;
  2610. }
  2611. // Get the LA25 stub section, creating it if necessary.
  2612. Mips_output_data_la25_stub<size, big_endian>*
  2613. la25_stub_section(Layout*);
  2614. // Get the LA25 stub section.
  2615. Mips_output_data_la25_stub<size, big_endian>*
  2616. la25_stub_section()
  2617. {
  2618. gold_assert(this->la25_stub_ != NULL);
  2619. return this->la25_stub_;
  2620. }
  2621. // Get gp value. It has the value of .got + 0x7FF0.
  2622. Mips_address
  2623. gp_value() const
  2624. {
  2625. if (this->gp_ != NULL)
  2626. return this->gp_->value();
  2627. return 0;
  2628. }
  2629. // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
  2630. // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
  2631. Mips_address
  2632. adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
  2633. {
  2634. if (this->gp_ == NULL)
  2635. return 0;
  2636. bool multi_got = false;
  2637. if (this->has_got_section())
  2638. multi_got = this->got_section()->multi_got();
  2639. if (!multi_got)
  2640. return this->gp_->value();
  2641. else
  2642. return this->gp_->value() + this->got_section()->get_got_offset(object);
  2643. }
  2644. // Get the dynamic reloc section, creating it if necessary.
  2645. Reloc_section*
  2646. rel_dyn_section(Layout*);
  2647. bool
  2648. do_has_custom_set_dynsym_indexes() const
  2649. { return true; }
  2650. // Don't emit input .reginfo sections to output .reginfo.
  2651. bool
  2652. do_should_include_section(elfcpp::Elf_Word sh_type) const
  2653. { return sh_type != elfcpp::SHT_MIPS_REGINFO; }
  2654. // Set the dynamic symbol indexes. INDEX is the index of the first
  2655. // global dynamic symbol. Pointers to the symbols are stored into the
  2656. // vector SYMS. The names are added to DYNPOOL. This returns an
  2657. // updated dynamic symbol index.
  2658. unsigned int
  2659. do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
  2660. std::vector<Symbol*>* syms, Stringpool* dynpool,
  2661. Versions* versions, Symbol_table* symtab) const;
  2662. // Remove .MIPS.stubs entry for a symbol.
  2663. void
  2664. remove_lazy_stub_entry(Mips_symbol<size>* sym)
  2665. {
  2666. if (this->mips_stubs_ != NULL)
  2667. this->mips_stubs_->remove_entry(sym);
  2668. }
  2669. // The value to write into got[1] for SVR4 targets, to identify it is
  2670. // a GNU object. The dynamic linker can then use got[1] to store the
  2671. // module pointer.
  2672. uint64_t
  2673. mips_elf_gnu_got1_mask()
  2674. {
  2675. if (this->is_output_n64())
  2676. return (uint64_t)1 << 63;
  2677. else
  2678. return 1 << 31;
  2679. }
  2680. // Whether the output has microMIPS code. This is valid only after
  2681. // merge_processor_specific_flags() is called.
  2682. bool
  2683. is_output_micromips() const
  2684. {
  2685. gold_assert(this->are_processor_specific_flags_set());
  2686. return elfcpp::is_micromips(this->processor_specific_flags());
  2687. }
  2688. // Whether the output uses N32 ABI. This is valid only after
  2689. // merge_processor_specific_flags() is called.
  2690. bool
  2691. is_output_n32() const
  2692. {
  2693. gold_assert(this->are_processor_specific_flags_set());
  2694. return elfcpp::abi_n32(this->processor_specific_flags());
  2695. }
  2696. // Whether the output uses N64 ABI. This is valid only after
  2697. // merge_processor_specific_flags() is called.
  2698. bool
  2699. is_output_n64() const
  2700. {
  2701. gold_assert(this->are_processor_specific_flags_set());
  2702. return elfcpp::abi_64(this->ei_class_);
  2703. }
  2704. // Whether the output uses NEWABI. This is valid only after
  2705. // merge_processor_specific_flags() is called.
  2706. bool
  2707. is_output_newabi() const
  2708. { return this->is_output_n32() || this->is_output_n64(); }
  2709. // Whether we can only use 32-bit microMIPS instructions.
  2710. bool
  2711. use_32bit_micromips_instructions() const
  2712. { return this->insn32_; }
  2713. protected:
  2714. // Return the value to use for a dynamic symbol which requires special
  2715. // treatment. This is how we support equality comparisons of function
  2716. // pointers across shared library boundaries, as described in the
  2717. // processor specific ABI supplement.
  2718. uint64_t
  2719. do_dynsym_value(const Symbol* gsym) const;
  2720. // Make an ELF object.
  2721. Object*
  2722. do_make_elf_object(const std::string&, Input_file*, off_t,
  2723. const elfcpp::Ehdr<size, big_endian>& ehdr);
  2724. Object*
  2725. do_make_elf_object(const std::string&, Input_file*, off_t,
  2726. const elfcpp::Ehdr<size, !big_endian>&)
  2727. { gold_unreachable(); }
  2728. // Make an output section.
  2729. Output_section*
  2730. do_make_output_section(const char* name, elfcpp::Elf_Word type,
  2731. elfcpp::Elf_Xword flags)
  2732. {
  2733. if (type == elfcpp::SHT_MIPS_REGINFO)
  2734. return new Mips_output_section_reginfo<size, big_endian>(name, type,
  2735. flags, this);
  2736. else
  2737. return new Output_section(name, type, flags);
  2738. }
  2739. // Adjust ELF file header.
  2740. void
  2741. do_adjust_elf_header(unsigned char* view, int len);
  2742. // Get the custom dynamic tag value.
  2743. unsigned int
  2744. do_dynamic_tag_custom_value(elfcpp::DT) const;
  2745. // Adjust the value written to the dynamic symbol table.
  2746. virtual void
  2747. do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
  2748. {
  2749. elfcpp::Sym<size, big_endian> isym(view);
  2750. elfcpp::Sym_write<size, big_endian> osym(view);
  2751. const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
  2752. // Keep dynamic compressed symbols odd. This allows the dynamic linker
  2753. // to treat compressed symbols like any other.
  2754. Mips_address value = isym.get_st_value();
  2755. if (mips_sym->is_mips16() && value != 0)
  2756. {
  2757. if (!mips_sym->has_mips16_fn_stub())
  2758. value |= 1;
  2759. else
  2760. {
  2761. // If we have a MIPS16 function with a stub, the dynamic symbol
  2762. // must refer to the stub, since only the stub uses the standard
  2763. // calling conventions. Stub contains MIPS32 code, so don't add +1
  2764. // in this case.
  2765. // There is a code which does this in the method
  2766. // Target_mips::do_dynsym_value, but that code will only be
  2767. // executed if the symbol is from dynobj.
  2768. // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
  2769. // table.
  2770. Mips16_stub_section<size, big_endian>* fn_stub =
  2771. mips_sym->template get_mips16_fn_stub<big_endian>();
  2772. value = fn_stub->output_address();
  2773. osym.put_st_size(fn_stub->section_size());
  2774. }
  2775. osym.put_st_value(value);
  2776. osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
  2777. mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
  2778. }
  2779. else if ((mips_sym->is_micromips()
  2780. // Stubs are always microMIPS if there is any microMIPS code in
  2781. // the output.
  2782. || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
  2783. && value != 0)
  2784. {
  2785. osym.put_st_value(value | 1);
  2786. osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
  2787. mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
  2788. }
  2789. }
  2790. private:
  2791. // The class which scans relocations.
  2792. class Scan
  2793. {
  2794. public:
  2795. Scan()
  2796. { }
  2797. static inline int
  2798. get_reference_flags(unsigned int r_type);
  2799. inline void
  2800. local(Symbol_table* symtab, Layout* layout, Target_mips* target,
  2801. Sized_relobj_file<size, big_endian>* object,
  2802. unsigned int data_shndx,
  2803. Output_section* output_section,
  2804. const elfcpp::Rel<size, big_endian>& reloc, unsigned int r_type,
  2805. const elfcpp::Sym<size, big_endian>& lsym,
  2806. bool is_discarded);
  2807. inline void
  2808. local(Symbol_table* symtab, Layout* layout, Target_mips* target,
  2809. Sized_relobj_file<size, big_endian>* object,
  2810. unsigned int data_shndx,
  2811. Output_section* output_section,
  2812. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  2813. const elfcpp::Sym<size, big_endian>& lsym,
  2814. bool is_discarded);
  2815. inline void
  2816. local(Symbol_table* symtab, Layout* layout, Target_mips* target,
  2817. Sized_relobj_file<size, big_endian>* object,
  2818. unsigned int data_shndx,
  2819. Output_section* output_section,
  2820. const elfcpp::Rela<size, big_endian>* rela,
  2821. const elfcpp::Rel<size, big_endian>* rel,
  2822. unsigned int rel_type,
  2823. unsigned int r_type,
  2824. const elfcpp::Sym<size, big_endian>& lsym,
  2825. bool is_discarded);
  2826. inline void
  2827. global(Symbol_table* symtab, Layout* layout, Target_mips* target,
  2828. Sized_relobj_file<size, big_endian>* object,
  2829. unsigned int data_shndx,
  2830. Output_section* output_section,
  2831. const elfcpp::Rel<size, big_endian>& reloc, unsigned int r_type,
  2832. Symbol* gsym);
  2833. inline void
  2834. global(Symbol_table* symtab, Layout* layout, Target_mips* target,
  2835. Sized_relobj_file<size, big_endian>* object,
  2836. unsigned int data_shndx,
  2837. Output_section* output_section,
  2838. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  2839. Symbol* gsym);
  2840. inline void
  2841. global(Symbol_table* symtab, Layout* layout, Target_mips* target,
  2842. Sized_relobj_file<size, big_endian>* object,
  2843. unsigned int data_shndx,
  2844. Output_section* output_section,
  2845. const elfcpp::Rela<size, big_endian>* rela,
  2846. const elfcpp::Rel<size, big_endian>* rel,
  2847. unsigned int rel_type,
  2848. unsigned int r_type,
  2849. Symbol* gsym);
  2850. inline bool
  2851. local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
  2852. Target_mips*,
  2853. Sized_relobj_file<size, big_endian>*,
  2854. unsigned int,
  2855. Output_section*,
  2856. const elfcpp::Rel<size, big_endian>&,
  2857. unsigned int,
  2858. const elfcpp::Sym<size, big_endian>&)
  2859. { return false; }
  2860. inline bool
  2861. global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
  2862. Target_mips*,
  2863. Sized_relobj_file<size, big_endian>*,
  2864. unsigned int,
  2865. Output_section*,
  2866. const elfcpp::Rel<size, big_endian>&,
  2867. unsigned int, Symbol*)
  2868. { return false; }
  2869. inline bool
  2870. local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
  2871. Target_mips*,
  2872. Sized_relobj_file<size, big_endian>*,
  2873. unsigned int,
  2874. Output_section*,
  2875. const elfcpp::Rela<size, big_endian>&,
  2876. unsigned int,
  2877. const elfcpp::Sym<size, big_endian>&)
  2878. { return false; }
  2879. inline bool
  2880. global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
  2881. Target_mips*,
  2882. Sized_relobj_file<size, big_endian>*,
  2883. unsigned int,
  2884. Output_section*,
  2885. const elfcpp::Rela<size, big_endian>&,
  2886. unsigned int, Symbol*)
  2887. { return false; }
  2888. private:
  2889. static void
  2890. unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
  2891. unsigned int r_type);
  2892. static void
  2893. unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
  2894. unsigned int r_type, Symbol*);
  2895. };
  2896. // The class which implements relocation.
  2897. class Relocate
  2898. {
  2899. public:
  2900. Relocate()
  2901. { }
  2902. ~Relocate()
  2903. { }
  2904. // Return whether the R_MIPS_32 relocation needs to be applied.
  2905. inline bool
  2906. should_apply_r_mips_32_reloc(const Mips_symbol<size>* gsym,
  2907. unsigned int r_type,
  2908. Output_section* output_section,
  2909. Target_mips* target);
  2910. // Do a relocation. Return false if the caller should not issue
  2911. // any warnings about this relocation.
  2912. inline bool
  2913. relocate(const Relocate_info<size, big_endian>*, Target_mips*,
  2914. Output_section*, size_t relnum,
  2915. const elfcpp::Rela<size, big_endian>*,
  2916. const elfcpp::Rel<size, big_endian>*,
  2917. unsigned int,
  2918. unsigned int, const Sized_symbol<size>*,
  2919. const Symbol_value<size>*,
  2920. unsigned char*,
  2921. Mips_address,
  2922. section_size_type);
  2923. inline bool
  2924. relocate(const Relocate_info<size, big_endian>*, Target_mips*,
  2925. Output_section*, size_t relnum,
  2926. const elfcpp::Rel<size, big_endian>&,
  2927. unsigned int, const Sized_symbol<size>*,
  2928. const Symbol_value<size>*,
  2929. unsigned char*,
  2930. Mips_address,
  2931. section_size_type);
  2932. inline bool
  2933. relocate(const Relocate_info<size, big_endian>*, Target_mips*,
  2934. Output_section*, size_t relnum,
  2935. const elfcpp::Rela<size, big_endian>&,
  2936. unsigned int, const Sized_symbol<size>*,
  2937. const Symbol_value<size>*,
  2938. unsigned char*,
  2939. Mips_address,
  2940. section_size_type);
  2941. };
  2942. // A class which returns the size required for a relocation type,
  2943. // used while scanning relocs during a relocatable link.
  2944. class Relocatable_size_for_reloc
  2945. {
  2946. public:
  2947. unsigned int
  2948. get_size_for_reloc(unsigned int, Relobj*);
  2949. };
  2950. // This POD class holds the dynamic relocations that should be emitted instead
  2951. // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
  2952. // relocations if it turns out that the symbol does not have static
  2953. // relocations.
  2954. class Dyn_reloc
  2955. {
  2956. public:
  2957. Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
  2958. Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
  2959. Output_section* output_section, Mips_address r_offset)
  2960. : sym_(sym), r_type_(r_type), relobj_(relobj),
  2961. shndx_(shndx), output_section_(output_section),
  2962. r_offset_(r_offset)
  2963. { }
  2964. // Emit this reloc if appropriate. This is called after we have
  2965. // scanned all the relocations, so we know whether the symbol has
  2966. // static relocations.
  2967. void
  2968. emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
  2969. Symbol_table* symtab)
  2970. {
  2971. if (!this->sym_->has_static_relocs())
  2972. {
  2973. got->record_global_got_symbol(this->sym_, this->relobj_,
  2974. this->r_type_, true, false);
  2975. if (!symbol_references_local(this->sym_,
  2976. this->sym_->should_add_dynsym_entry(symtab)))
  2977. rel_dyn->add_global(this->sym_, this->r_type_,
  2978. this->output_section_, this->relobj_,
  2979. this->shndx_, this->r_offset_);
  2980. else
  2981. rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
  2982. this->output_section_, this->relobj_,
  2983. this->shndx_, this->r_offset_);
  2984. }
  2985. }
  2986. private:
  2987. Mips_symbol<size>* sym_;
  2988. unsigned int r_type_;
  2989. Mips_relobj<size, big_endian>* relobj_;
  2990. unsigned int shndx_;
  2991. Output_section* output_section_;
  2992. Mips_address r_offset_;
  2993. };
  2994. // Adjust TLS relocation type based on the options and whether this
  2995. // is a local symbol.
  2996. static tls::Tls_optimization
  2997. optimize_tls_reloc(bool is_final, int r_type);
  2998. // Return whether there is a GOT section.
  2999. bool
  3000. has_got_section() const
  3001. { return this->got_ != NULL; }
  3002. // Check whether the given ELF header flags describe a 32-bit binary.
  3003. bool
  3004. mips_32bit_flags(elfcpp::Elf_Word);
  3005. enum Mips_mach {
  3006. mach_mips3000 = 3000,
  3007. mach_mips3900 = 3900,
  3008. mach_mips4000 = 4000,
  3009. mach_mips4010 = 4010,
  3010. mach_mips4100 = 4100,
  3011. mach_mips4111 = 4111,
  3012. mach_mips4120 = 4120,
  3013. mach_mips4300 = 4300,
  3014. mach_mips4400 = 4400,
  3015. mach_mips4600 = 4600,
  3016. mach_mips4650 = 4650,
  3017. mach_mips5000 = 5000,
  3018. mach_mips5400 = 5400,
  3019. mach_mips5500 = 5500,
  3020. mach_mips6000 = 6000,
  3021. mach_mips7000 = 7000,
  3022. mach_mips8000 = 8000,
  3023. mach_mips9000 = 9000,
  3024. mach_mips10000 = 10000,
  3025. mach_mips12000 = 12000,
  3026. mach_mips14000 = 14000,
  3027. mach_mips16000 = 16000,
  3028. mach_mips16 = 16,
  3029. mach_mips5 = 5,
  3030. mach_mips_loongson_2e = 3001,
  3031. mach_mips_loongson_2f = 3002,
  3032. mach_mips_loongson_3a = 3003,
  3033. mach_mips_sb1 = 12310201, // octal 'SB', 01
  3034. mach_mips_octeon = 6501,
  3035. mach_mips_octeonp = 6601,
  3036. mach_mips_octeon2 = 6502,
  3037. mach_mips_xlr = 887682, // decimal 'XLR'
  3038. mach_mipsisa32 = 32,
  3039. mach_mipsisa32r2 = 33,
  3040. mach_mipsisa64 = 64,
  3041. mach_mipsisa64r2 = 65,
  3042. mach_mips_micromips = 96
  3043. };
  3044. // Return the MACH for a MIPS e_flags value.
  3045. unsigned int
  3046. elf_mips_mach(elfcpp::Elf_Word);
  3047. // Check whether machine EXTENSION is an extension of machine BASE.
  3048. bool
  3049. mips_mach_extends(unsigned int, unsigned int);
  3050. // Merge processor specific flags.
  3051. void
  3052. merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word,
  3053. unsigned char, bool);
  3054. // True if we are linking for CPUs that are faster if JAL is converted to BAL.
  3055. static inline bool
  3056. jal_to_bal()
  3057. { return false; }
  3058. // True if we are linking for CPUs that are faster if JALR is converted to
  3059. // BAL. This should be safe for all architectures. We enable this predicate
  3060. // for all CPUs.
  3061. static inline bool
  3062. jalr_to_bal()
  3063. { return true; }
  3064. // True if we are linking for CPUs that are faster if JR is converted to B.
  3065. // This should be safe for all architectures. We enable this predicate for
  3066. // all CPUs.
  3067. static inline bool
  3068. jr_to_b()
  3069. { return true; }
  3070. // Return the size of the GOT section.
  3071. section_size_type
  3072. got_size() const
  3073. {
  3074. gold_assert(this->got_ != NULL);
  3075. return this->got_->data_size();
  3076. }
  3077. // Create a PLT entry for a global symbol referenced by r_type relocation.
  3078. void
  3079. make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
  3080. unsigned int r_type);
  3081. // Get the PLT section.
  3082. Mips_output_data_plt<size, big_endian>*
  3083. plt_section() const
  3084. {
  3085. gold_assert(this->plt_ != NULL);
  3086. return this->plt_;
  3087. }
  3088. // Get the GOT PLT section.
  3089. const Mips_output_data_plt<size, big_endian>*
  3090. got_plt_section() const
  3091. {
  3092. gold_assert(this->got_plt_ != NULL);
  3093. return this->got_plt_;
  3094. }
  3095. // Copy a relocation against a global symbol.
  3096. void
  3097. copy_reloc(Symbol_table* symtab, Layout* layout,
  3098. Sized_relobj_file<size, big_endian>* object,
  3099. unsigned int shndx, Output_section* output_section,
  3100. Symbol* sym, const elfcpp::Rel<size, big_endian>& reloc)
  3101. {
  3102. this->copy_relocs_.copy_reloc(symtab, layout,
  3103. symtab->get_sized_symbol<size>(sym),
  3104. object, shndx, output_section,
  3105. reloc, this->rel_dyn_section(layout));
  3106. }
  3107. void
  3108. dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
  3109. Mips_relobj<size, big_endian>* relobj,
  3110. unsigned int shndx, Output_section* output_section,
  3111. Mips_address r_offset)
  3112. {
  3113. this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
  3114. output_section, r_offset));
  3115. }
  3116. // Calculate value of _gp symbol.
  3117. void
  3118. set_gp(Layout*, Symbol_table*);
  3119. const char*
  3120. elf_mips_abi_name(elfcpp::Elf_Word e_flags, unsigned char ei_class);
  3121. const char*
  3122. elf_mips_mach_name(elfcpp::Elf_Word e_flags);
  3123. // Adds entries that describe how machines relate to one another. The entries
  3124. // are ordered topologically with MIPS I extensions listed last. First
  3125. // element is extension, second element is base.
  3126. void
  3127. add_machine_extensions()
  3128. {
  3129. // MIPS64r2 extensions.
  3130. this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
  3131. this->add_extension(mach_mips_octeonp, mach_mips_octeon);
  3132. this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
  3133. // MIPS64 extensions.
  3134. this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
  3135. this->add_extension(mach_mips_sb1, mach_mipsisa64);
  3136. this->add_extension(mach_mips_xlr, mach_mipsisa64);
  3137. this->add_extension(mach_mips_loongson_3a, mach_mipsisa64);
  3138. // MIPS V extensions.
  3139. this->add_extension(mach_mipsisa64, mach_mips5);
  3140. // R10000 extensions.
  3141. this->add_extension(mach_mips12000, mach_mips10000);
  3142. this->add_extension(mach_mips14000, mach_mips10000);
  3143. this->add_extension(mach_mips16000, mach_mips10000);
  3144. // R5000 extensions. Note: the vr5500 ISA is an extension of the core
  3145. // vr5400 ISA, but doesn't include the multimedia stuff. It seems
  3146. // better to allow vr5400 and vr5500 code to be merged anyway, since
  3147. // many libraries will just use the core ISA. Perhaps we could add
  3148. // some sort of ASE flag if this ever proves a problem.
  3149. this->add_extension(mach_mips5500, mach_mips5400);
  3150. this->add_extension(mach_mips5400, mach_mips5000);
  3151. // MIPS IV extensions.
  3152. this->add_extension(mach_mips5, mach_mips8000);
  3153. this->add_extension(mach_mips10000, mach_mips8000);
  3154. this->add_extension(mach_mips5000, mach_mips8000);
  3155. this->add_extension(mach_mips7000, mach_mips8000);
  3156. this->add_extension(mach_mips9000, mach_mips8000);
  3157. // VR4100 extensions.
  3158. this->add_extension(mach_mips4120, mach_mips4100);
  3159. this->add_extension(mach_mips4111, mach_mips4100);
  3160. // MIPS III extensions.
  3161. this->add_extension(mach_mips_loongson_2e, mach_mips4000);
  3162. this->add_extension(mach_mips_loongson_2f, mach_mips4000);
  3163. this->add_extension(mach_mips8000, mach_mips4000);
  3164. this->add_extension(mach_mips4650, mach_mips4000);
  3165. this->add_extension(mach_mips4600, mach_mips4000);
  3166. this->add_extension(mach_mips4400, mach_mips4000);
  3167. this->add_extension(mach_mips4300, mach_mips4000);
  3168. this->add_extension(mach_mips4100, mach_mips4000);
  3169. this->add_extension(mach_mips4010, mach_mips4000);
  3170. // MIPS32 extensions.
  3171. this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
  3172. // MIPS II extensions.
  3173. this->add_extension(mach_mips4000, mach_mips6000);
  3174. this->add_extension(mach_mipsisa32, mach_mips6000);
  3175. // MIPS I extensions.
  3176. this->add_extension(mach_mips6000, mach_mips3000);
  3177. this->add_extension(mach_mips3900, mach_mips3000);
  3178. }
  3179. // Add value to MIPS extenstions.
  3180. void
  3181. add_extension(unsigned int base, unsigned int extension)
  3182. {
  3183. std::pair<unsigned int, unsigned int> ext(base, extension);
  3184. this->mips_mach_extensions_.push_back(ext);
  3185. }
  3186. // Return the number of entries in the .dynsym section.
  3187. unsigned int get_dt_mips_symtabno() const
  3188. {
  3189. return ((unsigned int)(this->layout_->dynsym_section()->data_size()
  3190. / elfcpp::Elf_sizes<size>::sym_size));
  3191. // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
  3192. }
  3193. // Information about this specific target which we pass to the
  3194. // general Target structure.
  3195. static const Target::Target_info mips_info;
  3196. // The GOT section.
  3197. Mips_output_data_got<size, big_endian>* got_;
  3198. // gp symbol. It has the value of .got + 0x7FF0.
  3199. Sized_symbol<size>* gp_;
  3200. // The PLT section.
  3201. Mips_output_data_plt<size, big_endian>* plt_;
  3202. // The GOT PLT section.
  3203. Output_data_space* got_plt_;
  3204. // The dynamic reloc section.
  3205. Reloc_section* rel_dyn_;
  3206. // Relocs saved to avoid a COPY reloc.
  3207. Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
  3208. // A list of dyn relocs to be saved.
  3209. std::vector<Dyn_reloc> dyn_relocs_;
  3210. // The LA25 stub section.
  3211. Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
  3212. // Architecture extensions.
  3213. std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
  3214. // .MIPS.stubs
  3215. Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
  3216. unsigned char ei_class_;
  3217. unsigned int mach_;
  3218. Layout* layout_;
  3219. typename std::list<got16_addend<size, big_endian> > got16_addends_;
  3220. // Whether the entry symbol is mips16 or micromips.
  3221. bool entry_symbol_is_compressed_;
  3222. // Whether we can use only 32-bit microMIPS instructions.
  3223. // TODO(sasa): This should be a linker option.
  3224. bool insn32_;
  3225. };
  3226. // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
  3227. // It records high part of the relocation pair.
  3228. template<int size, bool big_endian>
  3229. struct reloc_high
  3230. {
  3231. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  3232. reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
  3233. const Symbol_value<size>* _psymval, Mips_address _addend,
  3234. unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
  3235. Mips_address _address = 0, bool _gp_disp = false)
  3236. : view(_view), object(_object), psymval(_psymval), addend(_addend),
  3237. r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
  3238. address(_address), gp_disp(_gp_disp)
  3239. { }
  3240. unsigned char* view;
  3241. const Mips_relobj<size, big_endian>* object;
  3242. const Symbol_value<size>* psymval;
  3243. Mips_address addend;
  3244. unsigned int r_type;
  3245. unsigned int r_sym;
  3246. bool extract_addend;
  3247. Mips_address address;
  3248. bool gp_disp;
  3249. };
  3250. template<int size, bool big_endian>
  3251. class Mips_relocate_functions : public Relocate_functions<size, big_endian>
  3252. {
  3253. typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
  3254. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
  3255. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
  3256. public:
  3257. typedef enum
  3258. {
  3259. STATUS_OKAY, // No error during relocation.
  3260. STATUS_OVERFLOW, // Relocation overflow.
  3261. STATUS_BAD_RELOC // Relocation cannot be applied.
  3262. } Status;
  3263. private:
  3264. typedef Relocate_functions<size, big_endian> Base;
  3265. typedef Mips_relocate_functions<size, big_endian> This;
  3266. static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
  3267. static typename std::list<reloc_high<size, big_endian> > got16_relocs;
  3268. // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
  3269. // Most mips16 instructions are 16 bits, but these instructions
  3270. // are 32 bits.
  3271. //
  3272. // The format of these instructions is:
  3273. //
  3274. // +--------------+--------------------------------+
  3275. // | JALX | X| Imm 20:16 | Imm 25:21 |
  3276. // +--------------+--------------------------------+
  3277. // | Immediate 15:0 |
  3278. // +-----------------------------------------------+
  3279. //
  3280. // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
  3281. // Note that the immediate value in the first word is swapped.
  3282. //
  3283. // When producing a relocatable object file, R_MIPS16_26 is
  3284. // handled mostly like R_MIPS_26. In particular, the addend is
  3285. // stored as a straight 26-bit value in a 32-bit instruction.
  3286. // (gas makes life simpler for itself by never adjusting a
  3287. // R_MIPS16_26 reloc to be against a section, so the addend is
  3288. // always zero). However, the 32 bit instruction is stored as 2
  3289. // 16-bit values, rather than a single 32-bit value. In a
  3290. // big-endian file, the result is the same; in a little-endian
  3291. // file, the two 16-bit halves of the 32 bit value are swapped.
  3292. // This is so that a disassembler can recognize the jal
  3293. // instruction.
  3294. //
  3295. // When doing a final link, R_MIPS16_26 is treated as a 32 bit
  3296. // instruction stored as two 16-bit values. The addend A is the
  3297. // contents of the targ26 field. The calculation is the same as
  3298. // R_MIPS_26. When storing the calculated value, reorder the
  3299. // immediate value as shown above, and don't forget to store the
  3300. // value as two 16-bit values.
  3301. //
  3302. // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
  3303. // defined as
  3304. //
  3305. // big-endian:
  3306. // +--------+----------------------+
  3307. // | | |
  3308. // | | targ26-16 |
  3309. // |31 26|25 0|
  3310. // +--------+----------------------+
  3311. //
  3312. // little-endian:
  3313. // +----------+------+-------------+
  3314. // | | | |
  3315. // | sub1 | | sub2 |
  3316. // |0 9|10 15|16 31|
  3317. // +----------+--------------------+
  3318. // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
  3319. // ((sub1 << 16) | sub2)).
  3320. //
  3321. // When producing a relocatable object file, the calculation is
  3322. // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
  3323. // When producing a fully linked file, the calculation is
  3324. // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
  3325. // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
  3326. //
  3327. // The table below lists the other MIPS16 instruction relocations.
  3328. // Each one is calculated in the same way as the non-MIPS16 relocation
  3329. // given on the right, but using the extended MIPS16 layout of 16-bit
  3330. // immediate fields:
  3331. //
  3332. // R_MIPS16_GPREL R_MIPS_GPREL16
  3333. // R_MIPS16_GOT16 R_MIPS_GOT16
  3334. // R_MIPS16_CALL16 R_MIPS_CALL16
  3335. // R_MIPS16_HI16 R_MIPS_HI16
  3336. // R_MIPS16_LO16 R_MIPS_LO16
  3337. //
  3338. // A typical instruction will have a format like this:
  3339. //
  3340. // +--------------+--------------------------------+
  3341. // | EXTEND | Imm 10:5 | Imm 15:11 |
  3342. // +--------------+--------------------------------+
  3343. // | Major | rx | ry | Imm 4:0 |
  3344. // +--------------+--------------------------------+
  3345. //
  3346. // EXTEND is the five bit value 11110. Major is the instruction
  3347. // opcode.
  3348. //
  3349. // All we need to do here is shuffle the bits appropriately.
  3350. // As above, the two 16-bit halves must be swapped on a
  3351. // little-endian system.
  3352. // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
  3353. // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
  3354. // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
  3355. static inline bool
  3356. should_shuffle_micromips_reloc(unsigned int r_type)
  3357. {
  3358. return (micromips_reloc(r_type)
  3359. && r_type != elfcpp::R_MICROMIPS_PC7_S1
  3360. && r_type != elfcpp::R_MICROMIPS_PC10_S1);
  3361. }
  3362. static void
  3363. mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
  3364. bool jal_shuffle)
  3365. {
  3366. if (!mips16_reloc(r_type)
  3367. && !should_shuffle_micromips_reloc(r_type))
  3368. return;
  3369. // Pick up the first and second halfwords of the instruction.
  3370. Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
  3371. Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
  3372. Valtype32 val;
  3373. if (micromips_reloc(r_type)
  3374. || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
  3375. val = first << 16 | second;
  3376. else if (r_type != elfcpp::R_MIPS16_26)
  3377. val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
  3378. | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
  3379. else
  3380. val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
  3381. | ((first & 0x1f) << 21) | second);
  3382. elfcpp::Swap<32, big_endian>::writeval(view, val);
  3383. }
  3384. static void
  3385. mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
  3386. {
  3387. if (!mips16_reloc(r_type)
  3388. && !should_shuffle_micromips_reloc(r_type))
  3389. return;
  3390. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  3391. Valtype16 first, second;
  3392. if (micromips_reloc(r_type)
  3393. || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
  3394. {
  3395. second = val & 0xffff;
  3396. first = val >> 16;
  3397. }
  3398. else if (r_type != elfcpp::R_MIPS16_26)
  3399. {
  3400. second = ((val >> 11) & 0xffe0) | (val & 0x1f);
  3401. first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
  3402. }
  3403. else
  3404. {
  3405. second = val & 0xffff;
  3406. first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
  3407. | ((val >> 21) & 0x1f);
  3408. }
  3409. elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
  3410. elfcpp::Swap<16, big_endian>::writeval(view, first);
  3411. }
  3412. public:
  3413. // R_MIPS_16: S + sign-extend(A)
  3414. static inline typename This::Status
  3415. rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3416. const Symbol_value<size>* psymval, Mips_address addend_a,
  3417. bool extract_addend, unsigned int r_type)
  3418. {
  3419. mips_reloc_unshuffle(view, r_type, false);
  3420. Valtype16* wv = reinterpret_cast<Valtype16*>(view);
  3421. Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
  3422. Valtype32 addend = (extract_addend ? Bits<16>::sign_extend32(val)
  3423. : Bits<16>::sign_extend32(addend_a));
  3424. Valtype32 x = psymval->value(object, addend);
  3425. val = Bits<16>::bit_select32(val, x, 0xffffU);
  3426. elfcpp::Swap<16, big_endian>::writeval(wv, val);
  3427. mips_reloc_shuffle(view, r_type, false);
  3428. return (Bits<16>::has_overflow32(x)
  3429. ? This::STATUS_OVERFLOW
  3430. : This::STATUS_OKAY);
  3431. }
  3432. // R_MIPS_32: S + A
  3433. static inline typename This::Status
  3434. rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3435. const Symbol_value<size>* psymval, Mips_address addend_a,
  3436. bool extract_addend, unsigned int r_type)
  3437. {
  3438. mips_reloc_unshuffle(view, r_type, false);
  3439. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3440. Valtype32 addend = (extract_addend
  3441. ? elfcpp::Swap<32, big_endian>::readval(wv)
  3442. : Bits<32>::sign_extend32(addend_a));
  3443. Valtype32 x = psymval->value(object, addend);
  3444. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  3445. mips_reloc_shuffle(view, r_type, false);
  3446. return This::STATUS_OKAY;
  3447. }
  3448. // R_MIPS_JALR, R_MICROMIPS_JALR
  3449. static inline typename This::Status
  3450. reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3451. const Symbol_value<size>* psymval, Mips_address address,
  3452. Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
  3453. unsigned int r_type, bool jalr_to_bal, bool jr_to_b)
  3454. {
  3455. mips_reloc_unshuffle(view, r_type, false);
  3456. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3457. Valtype32 addend = extract_addend ? 0 : addend_a;
  3458. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3459. // Try converting J(AL)R to B(AL), if the target is in range.
  3460. if (!parameters->options().relocatable()
  3461. && r_type == elfcpp::R_MIPS_JALR
  3462. && !cross_mode_jump
  3463. && ((jalr_to_bal && val == 0x0320f809) // jalr t9
  3464. || (jr_to_b && val == 0x03200008))) // jr t9
  3465. {
  3466. int offset = psymval->value(object, addend) - (address + 4);
  3467. if (!Bits<18>::has_overflow32(offset))
  3468. {
  3469. if (val == 0x03200008) // jr t9
  3470. val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
  3471. else
  3472. val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
  3473. }
  3474. }
  3475. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3476. mips_reloc_shuffle(view, r_type, false);
  3477. return This::STATUS_OKAY;
  3478. }
  3479. // R_MIPS_PC32: S + A - P
  3480. static inline typename This::Status
  3481. relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3482. const Symbol_value<size>* psymval, Mips_address address,
  3483. Mips_address addend_a, bool extract_addend, unsigned int r_type)
  3484. {
  3485. mips_reloc_unshuffle(view, r_type, false);
  3486. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3487. Valtype32 addend = (extract_addend
  3488. ? elfcpp::Swap<32, big_endian>::readval(wv)
  3489. : Bits<32>::sign_extend32(addend_a));
  3490. Valtype32 x = psymval->value(object, addend) - address;
  3491. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  3492. mips_reloc_shuffle(view, r_type, false);
  3493. return This::STATUS_OKAY;
  3494. }
  3495. // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
  3496. static inline typename This::Status
  3497. rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3498. const Symbol_value<size>* psymval, Mips_address address,
  3499. bool local, Mips_address addend_a, bool extract_addend,
  3500. const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
  3501. bool jal_to_bal)
  3502. {
  3503. mips_reloc_unshuffle(view, r_type, false);
  3504. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3505. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3506. Valtype32 addend;
  3507. if (extract_addend)
  3508. {
  3509. if (r_type == elfcpp::R_MICROMIPS_26_S1)
  3510. addend = (val & 0x03ffffff) << 1;
  3511. else
  3512. addend = (val & 0x03ffffff) << 2;
  3513. }
  3514. else
  3515. addend = addend_a;
  3516. // Make sure the target of JALX is word-aligned. Bit 0 must be
  3517. // the correct ISA mode selector and bit 1 must be 0.
  3518. if (cross_mode_jump
  3519. && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
  3520. {
  3521. gold_warning(_("JALX to a non-word-aligned address"));
  3522. mips_reloc_shuffle(view, r_type, !parameters->options().relocatable());
  3523. return This::STATUS_BAD_RELOC;
  3524. }
  3525. // Shift is 2, unusually, for microMIPS JALX.
  3526. unsigned int shift =
  3527. (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
  3528. Valtype32 x;
  3529. if (local)
  3530. x = addend | ((address + 4) & (0xfc000000 << shift));
  3531. else
  3532. {
  3533. if (shift == 1)
  3534. x = Bits<27>::sign_extend32(addend);
  3535. else
  3536. x = Bits<28>::sign_extend32(addend);
  3537. }
  3538. x = psymval->value(object, x) >> shift;
  3539. if (!local && !gsym->is_weak_undefined())
  3540. {
  3541. if ((x >> 26) != ((address + 4) >> (26 + shift)))
  3542. {
  3543. gold_error(_("relocation truncated to fit: %u against '%s'"),
  3544. r_type, gsym->name());
  3545. return This::STATUS_OVERFLOW;
  3546. }
  3547. }
  3548. val = Bits<32>::bit_select32(val, x, 0x03ffffff);
  3549. // If required, turn JAL into JALX.
  3550. if (cross_mode_jump)
  3551. {
  3552. bool ok;
  3553. Valtype32 opcode = val >> 26;
  3554. Valtype32 jalx_opcode;
  3555. // Check to see if the opcode is already JAL or JALX.
  3556. if (r_type == elfcpp::R_MIPS16_26)
  3557. {
  3558. ok = (opcode == 0x6) || (opcode == 0x7);
  3559. jalx_opcode = 0x7;
  3560. }
  3561. else if (r_type == elfcpp::R_MICROMIPS_26_S1)
  3562. {
  3563. ok = (opcode == 0x3d) || (opcode == 0x3c);
  3564. jalx_opcode = 0x3c;
  3565. }
  3566. else
  3567. {
  3568. ok = (opcode == 0x3) || (opcode == 0x1d);
  3569. jalx_opcode = 0x1d;
  3570. }
  3571. // If the opcode is not JAL or JALX, there's a problem. We cannot
  3572. // convert J or JALS to JALX.
  3573. if (!ok)
  3574. {
  3575. gold_error(_("Unsupported jump between ISA modes; consider "
  3576. "recompiling with interlinking enabled."));
  3577. return This::STATUS_BAD_RELOC;
  3578. }
  3579. // Make this the JALX opcode.
  3580. val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
  3581. }
  3582. // Try converting JAL to BAL, if the target is in range.
  3583. if (!parameters->options().relocatable()
  3584. && !cross_mode_jump
  3585. && ((jal_to_bal
  3586. && r_type == elfcpp::R_MIPS_26
  3587. && (val >> 26) == 0x3))) // jal addr
  3588. {
  3589. Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
  3590. int offset = dest - (address + 4);
  3591. if (!Bits<18>::has_overflow32(offset))
  3592. {
  3593. if (val == 0x03200008) // jr t9
  3594. val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
  3595. else
  3596. val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
  3597. }
  3598. }
  3599. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3600. mips_reloc_shuffle(view, r_type, !parameters->options().relocatable());
  3601. return This::STATUS_OKAY;
  3602. }
  3603. // R_MIPS_PC16
  3604. static inline typename This::Status
  3605. relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3606. const Symbol_value<size>* psymval, Mips_address address,
  3607. Mips_address addend_a, bool extract_addend, unsigned int r_type)
  3608. {
  3609. mips_reloc_unshuffle(view, r_type, false);
  3610. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3611. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3612. Valtype32 addend = extract_addend ? (val & 0xffff) << 2 : addend_a;
  3613. addend = Bits<18>::sign_extend32(addend);
  3614. Valtype32 x = psymval->value(object, addend) - address;
  3615. val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
  3616. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3617. mips_reloc_shuffle(view, r_type, false);
  3618. return (Bits<18>::has_overflow32(x)
  3619. ? This::STATUS_OVERFLOW
  3620. : This::STATUS_OKAY);
  3621. }
  3622. // R_MICROMIPS_PC7_S1
  3623. static inline typename This::Status
  3624. relmicromips_pc7_s1(unsigned char* view,
  3625. const Mips_relobj<size, big_endian>* object,
  3626. const Symbol_value<size>* psymval, Mips_address address,
  3627. Mips_address addend_a, bool extract_addend,
  3628. unsigned int r_type)
  3629. {
  3630. mips_reloc_unshuffle(view, r_type, false);
  3631. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3632. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3633. Valtype32 addend = extract_addend ? (val & 0x7f) << 1 : addend_a;
  3634. addend = Bits<8>::sign_extend32(addend);
  3635. Valtype32 x = psymval->value(object, addend) - address;
  3636. val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
  3637. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3638. mips_reloc_shuffle(view, r_type, false);
  3639. return (Bits<8>::has_overflow32(x)
  3640. ? This::STATUS_OVERFLOW
  3641. : This::STATUS_OKAY);
  3642. }
  3643. // R_MICROMIPS_PC10_S1
  3644. static inline typename This::Status
  3645. relmicromips_pc10_s1(unsigned char* view,
  3646. const Mips_relobj<size, big_endian>* object,
  3647. const Symbol_value<size>* psymval, Mips_address address,
  3648. Mips_address addend_a, bool extract_addend,
  3649. unsigned int r_type)
  3650. {
  3651. mips_reloc_unshuffle(view, r_type, false);
  3652. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3653. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3654. Valtype32 addend = extract_addend ? (val & 0x3ff) << 1 : addend_a;
  3655. addend = Bits<11>::sign_extend32(addend);
  3656. Valtype32 x = psymval->value(object, addend) - address;
  3657. val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
  3658. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3659. mips_reloc_shuffle(view, r_type, false);
  3660. return (Bits<11>::has_overflow32(x)
  3661. ? This::STATUS_OVERFLOW
  3662. : This::STATUS_OKAY);
  3663. }
  3664. // R_MICROMIPS_PC16_S1
  3665. static inline typename This::Status
  3666. relmicromips_pc16_s1(unsigned char* view,
  3667. const Mips_relobj<size, big_endian>* object,
  3668. const Symbol_value<size>* psymval, Mips_address address,
  3669. Mips_address addend_a, bool extract_addend,
  3670. unsigned int r_type)
  3671. {
  3672. mips_reloc_unshuffle(view, r_type, false);
  3673. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3674. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3675. Valtype32 addend = extract_addend ? (val & 0xffff) << 1 : addend_a;
  3676. addend = Bits<17>::sign_extend32(addend);
  3677. Valtype32 x = psymval->value(object, addend) - address;
  3678. val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
  3679. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3680. mips_reloc_shuffle(view, r_type, false);
  3681. return (Bits<17>::has_overflow32(x)
  3682. ? This::STATUS_OVERFLOW
  3683. : This::STATUS_OKAY);
  3684. }
  3685. // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
  3686. static inline typename This::Status
  3687. relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3688. const Symbol_value<size>* psymval, Mips_address addend,
  3689. Mips_address address, bool gp_disp, unsigned int r_type,
  3690. unsigned int r_sym, bool extract_addend)
  3691. {
  3692. // Record the relocation. It will be resolved when we find lo16 part.
  3693. hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
  3694. addend, r_type, r_sym, extract_addend, address,
  3695. gp_disp));
  3696. return This::STATUS_OKAY;
  3697. }
  3698. // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
  3699. static inline typename This::Status
  3700. do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3701. const Symbol_value<size>* psymval, Mips_address addend_hi,
  3702. Mips_address address, bool is_gp_disp, unsigned int r_type,
  3703. bool extract_addend, Valtype32 addend_lo,
  3704. Target_mips<size, big_endian>* target)
  3705. {
  3706. mips_reloc_unshuffle(view, r_type, false);
  3707. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3708. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3709. Valtype32 addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
  3710. : addend_hi);
  3711. Valtype32 value;
  3712. if (!is_gp_disp)
  3713. value = psymval->value(object, addend);
  3714. else
  3715. {
  3716. // For MIPS16 ABI code we generate this sequence
  3717. // 0: li $v0,%hi(_gp_disp)
  3718. // 4: addiupc $v1,%lo(_gp_disp)
  3719. // 8: sll $v0,16
  3720. // 12: addu $v0,$v1
  3721. // 14: move $gp,$v0
  3722. // So the offsets of hi and lo relocs are the same, but the
  3723. // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
  3724. // ADDIUPC clears the low two bits of the instruction address,
  3725. // so the base is ($t9 + 4) & ~3.
  3726. Valtype32 gp_disp;
  3727. if (r_type == elfcpp::R_MIPS16_HI16)
  3728. gp_disp = (target->adjusted_gp_value(object)
  3729. - ((address + 4) & ~0x3));
  3730. // The microMIPS .cpload sequence uses the same assembly
  3731. // instructions as the traditional psABI version, but the
  3732. // incoming $t9 has the low bit set.
  3733. else if (r_type == elfcpp::R_MICROMIPS_HI16)
  3734. gp_disp = target->adjusted_gp_value(object) - address - 1;
  3735. else
  3736. gp_disp = target->adjusted_gp_value(object) - address;
  3737. value = gp_disp + addend;
  3738. }
  3739. Valtype32 x = ((value + 0x8000) >> 16) & 0xffff;
  3740. val = Bits<32>::bit_select32(val, x, 0xffff);
  3741. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3742. mips_reloc_shuffle(view, r_type, false);
  3743. return (is_gp_disp && Bits<16>::has_overflow32(x)
  3744. ? This::STATUS_OVERFLOW
  3745. : This::STATUS_OKAY);
  3746. }
  3747. // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
  3748. static inline typename This::Status
  3749. relgot16_local(unsigned char* view,
  3750. const Mips_relobj<size, big_endian>* object,
  3751. const Symbol_value<size>* psymval, Mips_address addend_a,
  3752. bool extract_addend, unsigned int r_type, unsigned int r_sym)
  3753. {
  3754. // Record the relocation. It will be resolved when we find lo16 part.
  3755. got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
  3756. addend_a, r_type, r_sym, extract_addend));
  3757. return This::STATUS_OKAY;
  3758. }
  3759. // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
  3760. static inline typename This::Status
  3761. do_relgot16_local(unsigned char* view,
  3762. const Mips_relobj<size, big_endian>* object,
  3763. const Symbol_value<size>* psymval, Mips_address addend_hi,
  3764. unsigned int r_type, bool extract_addend,
  3765. Valtype32 addend_lo, Target_mips<size, big_endian>* target)
  3766. {
  3767. mips_reloc_unshuffle(view, r_type, false);
  3768. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3769. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3770. Valtype32 addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
  3771. : addend_hi);
  3772. // Find GOT page entry.
  3773. Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
  3774. & 0xffff;
  3775. value <<= 16;
  3776. unsigned int got_offset =
  3777. target->got_section()->get_got_page_offset(value, object);
  3778. // Resolve the relocation.
  3779. Valtype32 x = target->got_section()->gp_offset(got_offset, object);
  3780. val = Bits<32>::bit_select32(val, x, 0xffff);
  3781. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3782. mips_reloc_shuffle(view, r_type, false);
  3783. return (Bits<16>::has_overflow32(x)
  3784. ? This::STATUS_OVERFLOW
  3785. : This::STATUS_OKAY);
  3786. }
  3787. // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
  3788. static inline typename This::Status
  3789. rello16(Target_mips<size, big_endian>* target, unsigned char* view,
  3790. const Mips_relobj<size, big_endian>* object,
  3791. const Symbol_value<size>* psymval, Mips_address addend_a,
  3792. bool extract_addend, Mips_address address, bool is_gp_disp,
  3793. unsigned int r_type, unsigned int r_sym)
  3794. {
  3795. mips_reloc_unshuffle(view, r_type, false);
  3796. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3797. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3798. Valtype32 addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
  3799. : addend_a);
  3800. // Resolve pending R_MIPS_HI16 relocations.
  3801. typename std::list<reloc_high<size, big_endian> >::iterator it =
  3802. hi16_relocs.begin();
  3803. while (it != hi16_relocs.end())
  3804. {
  3805. reloc_high<size, big_endian> hi16 = *it;
  3806. if (hi16.r_sym == r_sym
  3807. && is_matching_lo16_reloc(hi16.r_type, r_type))
  3808. {
  3809. if (do_relhi16(hi16.view, hi16.object, hi16.psymval, hi16.addend,
  3810. hi16.address, hi16.gp_disp, hi16.r_type,
  3811. hi16.extract_addend, addend, target)
  3812. == This::STATUS_OVERFLOW)
  3813. return This::STATUS_OVERFLOW;
  3814. it = hi16_relocs.erase(it);
  3815. }
  3816. else
  3817. ++it;
  3818. }
  3819. // Resolve pending local R_MIPS_GOT16 relocations.
  3820. typename std::list<reloc_high<size, big_endian> >::iterator it2 =
  3821. got16_relocs.begin();
  3822. while (it2 != got16_relocs.end())
  3823. {
  3824. reloc_high<size, big_endian> got16 = *it2;
  3825. if (got16.r_sym == r_sym
  3826. && is_matching_lo16_reloc(got16.r_type, r_type))
  3827. {
  3828. if (do_relgot16_local(got16.view, got16.object, got16.psymval,
  3829. got16.addend, got16.r_type,
  3830. got16.extract_addend, addend,
  3831. target) == This::STATUS_OVERFLOW)
  3832. return This::STATUS_OVERFLOW;
  3833. it2 = got16_relocs.erase(it2);
  3834. }
  3835. else
  3836. ++it2;
  3837. }
  3838. // Resolve R_MIPS_LO16 relocation.
  3839. Valtype32 x;
  3840. if (!is_gp_disp)
  3841. x = psymval->value(object, addend);
  3842. else
  3843. {
  3844. // See the comment for R_MIPS16_HI16 above for the reason
  3845. // for this conditional.
  3846. Valtype32 gp_disp;
  3847. if (r_type == elfcpp::R_MIPS16_LO16)
  3848. gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
  3849. else if (r_type == elfcpp::R_MICROMIPS_LO16
  3850. || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
  3851. gp_disp = target->adjusted_gp_value(object) - address + 3;
  3852. else
  3853. gp_disp = target->adjusted_gp_value(object) - address + 4;
  3854. // The MIPS ABI requires checking the R_MIPS_LO16 relocation
  3855. // for overflow. Relocations against _gp_disp are normally
  3856. // generated from the .cpload pseudo-op. It generates code
  3857. // that normally looks like this:
  3858. // lui $gp,%hi(_gp_disp)
  3859. // addiu $gp,$gp,%lo(_gp_disp)
  3860. // addu $gp,$gp,$t9
  3861. // Here $t9 holds the address of the function being called,
  3862. // as required by the MIPS ELF ABI. The R_MIPS_LO16
  3863. // relocation can easily overflow in this situation, but the
  3864. // R_MIPS_HI16 relocation will handle the overflow.
  3865. // Therefore, we consider this a bug in the MIPS ABI, and do
  3866. // not check for overflow here.
  3867. x = gp_disp + addend;
  3868. }
  3869. val = Bits<32>::bit_select32(val, x, 0xffff);
  3870. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3871. mips_reloc_shuffle(view, r_type, false);
  3872. return This::STATUS_OKAY;
  3873. }
  3874. // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
  3875. // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
  3876. // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
  3877. // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
  3878. // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
  3879. // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
  3880. static inline typename This::Status
  3881. relgot(unsigned char* view, int gp_offset, unsigned int r_type)
  3882. {
  3883. mips_reloc_unshuffle(view, r_type, false);
  3884. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3885. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3886. Valtype32 x = gp_offset;
  3887. val = Bits<32>::bit_select32(val, x, 0xffff);
  3888. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3889. mips_reloc_shuffle(view, r_type, false);
  3890. return (Bits<16>::has_overflow32(x)
  3891. ? This::STATUS_OVERFLOW
  3892. : This::STATUS_OKAY);
  3893. }
  3894. // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
  3895. static inline typename This::Status
  3896. relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
  3897. const Mips_relobj<size, big_endian>* object,
  3898. const Symbol_value<size>* psymval, Mips_address addend_a,
  3899. bool extract_addend, unsigned int r_type)
  3900. {
  3901. mips_reloc_unshuffle(view, r_type, false);
  3902. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3903. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  3904. Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
  3905. // Find a GOT page entry that points to within 32KB of symbol + addend.
  3906. Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
  3907. unsigned int got_offset =
  3908. target->got_section()->get_got_page_offset(value, object);
  3909. Valtype32 x = target->got_section()->gp_offset(got_offset, object);
  3910. val = Bits<32>::bit_select32(val, x, 0xffff);
  3911. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3912. mips_reloc_shuffle(view, r_type, false);
  3913. return (Bits<16>::has_overflow32(x)
  3914. ? This::STATUS_OVERFLOW
  3915. : This::STATUS_OKAY);
  3916. }
  3917. // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
  3918. static inline typename This::Status
  3919. relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
  3920. const Mips_relobj<size, big_endian>* object,
  3921. const Symbol_value<size>* psymval, Mips_address addend_a,
  3922. bool extract_addend, bool local, unsigned int r_type)
  3923. {
  3924. mips_reloc_unshuffle(view, r_type, false);
  3925. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3926. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  3927. Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
  3928. // For a local symbol, find a GOT page entry that points to within 32KB of
  3929. // symbol + addend. Relocation value is the offset of the GOT page entry's
  3930. // value from symbol + addend.
  3931. // For a global symbol, relocation value is addend.
  3932. Valtype32 x;
  3933. if (local)
  3934. {
  3935. // Find GOT page entry.
  3936. Mips_address value = ((psymval->value(object, addend) + 0x8000)
  3937. & ~0xffff);
  3938. target->got_section()->get_got_page_offset(value, object);
  3939. x = psymval->value(object, addend) - value;
  3940. }
  3941. else
  3942. x = addend;
  3943. val = Bits<32>::bit_select32(val, x, 0xffff);
  3944. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3945. mips_reloc_shuffle(view, r_type, false);
  3946. return (Bits<16>::has_overflow32(x)
  3947. ? This::STATUS_OVERFLOW
  3948. : This::STATUS_OKAY);
  3949. }
  3950. // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
  3951. // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
  3952. static inline typename This::Status
  3953. relgot_hi16(unsigned char* view, int gp_offset, unsigned int r_type)
  3954. {
  3955. mips_reloc_unshuffle(view, r_type, false);
  3956. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3957. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3958. Valtype32 x = gp_offset;
  3959. x = ((x + 0x8000) >> 16) & 0xffff;
  3960. val = Bits<32>::bit_select32(val, x, 0xffff);
  3961. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3962. mips_reloc_shuffle(view, r_type, false);
  3963. return This::STATUS_OKAY;
  3964. }
  3965. // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
  3966. // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
  3967. static inline typename This::Status
  3968. relgot_lo16(unsigned char* view, int gp_offset, unsigned int r_type)
  3969. {
  3970. mips_reloc_unshuffle(view, r_type, false);
  3971. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3972. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3973. Valtype32 x = gp_offset;
  3974. val = Bits<32>::bit_select32(val, x, 0xffff);
  3975. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3976. mips_reloc_shuffle(view, r_type, false);
  3977. return This::STATUS_OKAY;
  3978. }
  3979. // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
  3980. // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
  3981. static inline typename This::Status
  3982. relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  3983. const Symbol_value<size>* psymval, Mips_address gp,
  3984. Mips_address addend_a, bool extract_addend, bool local,
  3985. unsigned int r_type)
  3986. {
  3987. mips_reloc_unshuffle(view, r_type, false);
  3988. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  3989. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  3990. Valtype32 addend;
  3991. if (extract_addend)
  3992. {
  3993. if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
  3994. addend = (val & 0x7f) << 2;
  3995. else
  3996. addend = val & 0xffff;
  3997. // Only sign-extend the addend if it was extracted from the
  3998. // instruction. If the addend was separate, leave it alone,
  3999. // otherwise we may lose significant bits.
  4000. addend = Bits<16>::sign_extend32(addend);
  4001. }
  4002. else
  4003. addend = addend_a;
  4004. Valtype32 x = psymval->value(object, addend) - gp;
  4005. // If the symbol was local, any earlier relocatable links will
  4006. // have adjusted its addend with the gp offset, so compensate
  4007. // for that now. Don't do it for symbols forced local in this
  4008. // link, though, since they won't have had the gp offset applied
  4009. // to them before.
  4010. if (local)
  4011. x += object->gp_value();
  4012. if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
  4013. val = Bits<32>::bit_select32(val, x, 0x7f);
  4014. else
  4015. val = Bits<32>::bit_select32(val, x, 0xffff);
  4016. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4017. mips_reloc_shuffle(view, r_type, false);
  4018. if (Bits<16>::has_overflow32(x))
  4019. {
  4020. gold_error(_("small-data section exceeds 64KB; lower small-data size "
  4021. "limit (see option -G)"));
  4022. return This::STATUS_OVERFLOW;
  4023. }
  4024. return This::STATUS_OKAY;
  4025. }
  4026. // R_MIPS_GPREL32
  4027. static inline typename This::Status
  4028. relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4029. const Symbol_value<size>* psymval, Mips_address gp,
  4030. Mips_address addend_a, bool extract_addend, unsigned int r_type)
  4031. {
  4032. mips_reloc_unshuffle(view, r_type, false);
  4033. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4034. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4035. Valtype32 addend = extract_addend ? val : addend_a;
  4036. // R_MIPS_GPREL32 relocations are defined for local symbols only.
  4037. Valtype32 x = psymval->value(object, addend) + object->gp_value() - gp;
  4038. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  4039. mips_reloc_shuffle(view, r_type, false);
  4040. return This::STATUS_OKAY;
  4041. }
  4042. // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
  4043. // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
  4044. // R_MICROMIPS_TLS_DTPREL_HI16
  4045. static inline typename This::Status
  4046. tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4047. const Symbol_value<size>* psymval, Valtype32 tp_offset,
  4048. Mips_address addend_a, bool extract_addend, unsigned int r_type)
  4049. {
  4050. mips_reloc_unshuffle(view, r_type, false);
  4051. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4052. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4053. Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
  4054. // tls symbol values are relative to tls_segment()->vaddr()
  4055. Valtype32 x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
  4056. val = Bits<32>::bit_select32(val, x, 0xffff);
  4057. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4058. mips_reloc_shuffle(view, r_type, false);
  4059. return This::STATUS_OKAY;
  4060. }
  4061. // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
  4062. // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
  4063. // R_MICROMIPS_TLS_DTPREL_LO16,
  4064. static inline typename This::Status
  4065. tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4066. const Symbol_value<size>* psymval, Valtype32 tp_offset,
  4067. Mips_address addend_a, bool extract_addend, unsigned int r_type)
  4068. {
  4069. mips_reloc_unshuffle(view, r_type, false);
  4070. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4071. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4072. Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
  4073. // tls symbol values are relative to tls_segment()->vaddr()
  4074. Valtype32 x = psymval->value(object, addend) - tp_offset;
  4075. val = Bits<32>::bit_select32(val, x, 0xffff);
  4076. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  4077. mips_reloc_shuffle(view, r_type, false);
  4078. return This::STATUS_OKAY;
  4079. }
  4080. // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
  4081. // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
  4082. static inline typename This::Status
  4083. tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4084. const Symbol_value<size>* psymval, Valtype32 tp_offset,
  4085. Mips_address addend_a, bool extract_addend, unsigned int r_type)
  4086. {
  4087. mips_reloc_unshuffle(view, r_type, false);
  4088. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4089. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4090. Valtype32 addend = extract_addend ? val : addend_a;
  4091. // tls symbol values are relative to tls_segment()->vaddr()
  4092. Valtype32 x = psymval->value(object, addend) - tp_offset;
  4093. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  4094. mips_reloc_shuffle(view, r_type, false);
  4095. return This::STATUS_OKAY;
  4096. }
  4097. // R_MIPS_SUB, R_MICROMIPS_SUB
  4098. static inline typename This::Status
  4099. relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
  4100. const Symbol_value<size>* psymval, Mips_address addend_a,
  4101. bool extract_addend, unsigned int r_type)
  4102. {
  4103. mips_reloc_unshuffle(view, r_type, false);
  4104. Valtype32* wv = reinterpret_cast<Valtype32*>(view);
  4105. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
  4106. Valtype32 addend = extract_addend ? val : addend_a;
  4107. Valtype32 x = psymval->value(object, -addend);
  4108. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  4109. mips_reloc_shuffle(view, r_type, false);
  4110. return This::STATUS_OKAY;
  4111. }
  4112. };
  4113. template<int size, bool big_endian>
  4114. typename std::list<reloc_high<size, big_endian> >
  4115. Mips_relocate_functions<size, big_endian>::hi16_relocs;
  4116. template<int size, bool big_endian>
  4117. typename std::list<reloc_high<size, big_endian> >
  4118. Mips_relocate_functions<size, big_endian>::got16_relocs;
  4119. // Mips_got_info methods.
  4120. // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
  4121. // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
  4122. template<int size, bool big_endian>
  4123. void
  4124. Mips_got_info<size, big_endian>::record_local_got_symbol(
  4125. Mips_relobj<size, big_endian>* object, unsigned int symndx,
  4126. Mips_address addend, unsigned int r_type, unsigned int shndx)
  4127. {
  4128. Mips_got_entry<size, big_endian>* entry =
  4129. new Mips_got_entry<size, big_endian>(object, symndx, addend,
  4130. mips_elf_reloc_tls_type(r_type),
  4131. shndx);
  4132. this->record_got_entry(entry, object);
  4133. }
  4134. // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
  4135. // in OBJECT. FOR_CALL is true if the caller is only interested in
  4136. // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
  4137. // relocation.
  4138. template<int size, bool big_endian>
  4139. void
  4140. Mips_got_info<size, big_endian>::record_global_got_symbol(
  4141. Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
  4142. unsigned int r_type, bool dyn_reloc, bool for_call)
  4143. {
  4144. if (!for_call)
  4145. mips_sym->set_got_not_only_for_calls();
  4146. // A global symbol in the GOT must also be in the dynamic symbol table.
  4147. if (!mips_sym->needs_dynsym_entry())
  4148. {
  4149. switch (mips_sym->visibility())
  4150. {
  4151. case elfcpp::STV_INTERNAL:
  4152. case elfcpp::STV_HIDDEN:
  4153. mips_sym->set_is_forced_local();
  4154. break;
  4155. default:
  4156. mips_sym->set_needs_dynsym_entry();
  4157. break;
  4158. }
  4159. }
  4160. unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
  4161. if (tls_type == GOT_TLS_NONE)
  4162. this->global_got_symbols_.insert(mips_sym);
  4163. if (dyn_reloc)
  4164. {
  4165. if (mips_sym->global_got_area() == GGA_NONE)
  4166. mips_sym->set_global_got_area(GGA_RELOC_ONLY);
  4167. return;
  4168. }
  4169. Mips_got_entry<size, big_endian>* entry =
  4170. new Mips_got_entry<size, big_endian>(object, mips_sym, tls_type);
  4171. this->record_got_entry(entry, object);
  4172. }
  4173. // Add ENTRY to master GOT and to OBJECT's GOT.
  4174. template<int size, bool big_endian>
  4175. void
  4176. Mips_got_info<size, big_endian>::record_got_entry(
  4177. Mips_got_entry<size, big_endian>* entry,
  4178. Mips_relobj<size, big_endian>* object)
  4179. {
  4180. if (this->got_entries_.find(entry) == this->got_entries_.end())
  4181. this->got_entries_.insert(entry);
  4182. // Create the GOT entry for the OBJECT's GOT.
  4183. Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
  4184. Mips_got_entry<size, big_endian>* entry2 =
  4185. new Mips_got_entry<size, big_endian>(*entry);
  4186. if (g->got_entries_.find(entry2) == g->got_entries_.end())
  4187. g->got_entries_.insert(entry2);
  4188. }
  4189. // Record that OBJECT has a page relocation against symbol SYMNDX and
  4190. // that ADDEND is the addend for that relocation.
  4191. // This function creates an upper bound on the number of GOT slots
  4192. // required; no attempt is made to combine references to non-overridable
  4193. // global symbols across multiple input files.
  4194. template<int size, bool big_endian>
  4195. void
  4196. Mips_got_info<size, big_endian>::record_got_page_entry(
  4197. Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
  4198. {
  4199. struct Got_page_range **range_ptr, *range;
  4200. int old_pages, new_pages;
  4201. // Find the Got_page_entry for this symbol.
  4202. Got_page_entry* entry = new Got_page_entry(object, symndx);
  4203. typename Got_page_entry_set::iterator it =
  4204. this->got_page_entries_.find(entry);
  4205. if (it != this->got_page_entries_.end())
  4206. entry = *it;
  4207. else
  4208. this->got_page_entries_.insert(entry);
  4209. // Add the same entry to the OBJECT's GOT.
  4210. Got_page_entry* entry2 = NULL;
  4211. Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
  4212. if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
  4213. {
  4214. entry2 = new Got_page_entry(*entry);
  4215. g2->got_page_entries_.insert(entry2);
  4216. }
  4217. // Skip over ranges whose maximum extent cannot share a page entry
  4218. // with ADDEND.
  4219. range_ptr = &entry->ranges;
  4220. while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
  4221. range_ptr = &(*range_ptr)->next;
  4222. // If we scanned to the end of the list, or found a range whose
  4223. // minimum extent cannot share a page entry with ADDEND, create
  4224. // a new singleton range.
  4225. range = *range_ptr;
  4226. if (!range || addend < range->min_addend - 0xffff)
  4227. {
  4228. range = new Got_page_range();
  4229. range->next = *range_ptr;
  4230. range->min_addend = addend;
  4231. range->max_addend = addend;
  4232. *range_ptr = range;
  4233. ++entry->num_pages;
  4234. if (entry2 != NULL)
  4235. ++entry2->num_pages;
  4236. ++this->page_gotno_;
  4237. ++g2->page_gotno_;
  4238. return;
  4239. }
  4240. // Remember how many pages the old range contributed.
  4241. old_pages = range->get_max_pages();
  4242. // Update the ranges.
  4243. if (addend < range->min_addend)
  4244. range->min_addend = addend;
  4245. else if (addend > range->max_addend)
  4246. {
  4247. if (range->next && addend >= range->next->min_addend - 0xffff)
  4248. {
  4249. old_pages += range->next->get_max_pages();
  4250. range->max_addend = range->next->max_addend;
  4251. range->next = range->next->next;
  4252. }
  4253. else
  4254. range->max_addend = addend;
  4255. }
  4256. // Record any change in the total estimate.
  4257. new_pages = range->get_max_pages();
  4258. if (old_pages != new_pages)
  4259. {
  4260. entry->num_pages += new_pages - old_pages;
  4261. if (entry2 != NULL)
  4262. entry2->num_pages += new_pages - old_pages;
  4263. this->page_gotno_ += new_pages - old_pages;
  4264. g2->page_gotno_ += new_pages - old_pages;
  4265. }
  4266. }
  4267. // Create all entries that should be in the local part of the GOT.
  4268. template<int size, bool big_endian>
  4269. void
  4270. Mips_got_info<size, big_endian>::add_local_entries(
  4271. Target_mips<size, big_endian>* target, Layout* layout)
  4272. {
  4273. Mips_output_data_got<size, big_endian>* got = target->got_section();
  4274. // First two GOT entries are reserved. The first entry will be filled at
  4275. // runtime. The second entry will be used by some runtime loaders.
  4276. got->add_constant(0);
  4277. got->add_constant(target->mips_elf_gnu_got1_mask());
  4278. for (typename Got_entry_set::iterator
  4279. p = this->got_entries_.begin();
  4280. p != this->got_entries_.end();
  4281. ++p)
  4282. {
  4283. Mips_got_entry<size, big_endian>* entry = *p;
  4284. if (entry->is_for_local_symbol() && !entry->is_tls_entry())
  4285. {
  4286. got->add_local(entry->object(), entry->symndx(),
  4287. GOT_TYPE_STANDARD);
  4288. unsigned int got_offset = entry->object()->local_got_offset(
  4289. entry->symndx(), GOT_TYPE_STANDARD);
  4290. if (got->multi_got() && this->index_ > 0
  4291. && parameters->options().output_is_position_independent())
  4292. target->rel_dyn_section(layout)->add_local(entry->object(),
  4293. entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
  4294. }
  4295. }
  4296. this->add_page_entries(target, layout);
  4297. // Add global entries that should be in the local area.
  4298. for (typename Got_entry_set::iterator
  4299. p = this->got_entries_.begin();
  4300. p != this->got_entries_.end();
  4301. ++p)
  4302. {
  4303. Mips_got_entry<size, big_endian>* entry = *p;
  4304. if (!entry->is_for_global_symbol())
  4305. continue;
  4306. Mips_symbol<size>* mips_sym = entry->sym();
  4307. if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
  4308. {
  4309. unsigned int got_type;
  4310. if (!got->multi_got())
  4311. got_type = GOT_TYPE_STANDARD;
  4312. else
  4313. got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
  4314. if (got->add_global(mips_sym, got_type))
  4315. {
  4316. mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
  4317. if (got->multi_got() && this->index_ > 0
  4318. && parameters->options().output_is_position_independent())
  4319. target->rel_dyn_section(layout)->add_symbolless_global_addend(
  4320. mips_sym, elfcpp::R_MIPS_REL32, got,
  4321. mips_sym->got_offset(got_type));
  4322. }
  4323. }
  4324. }
  4325. }
  4326. // Create GOT page entries.
  4327. template<int size, bool big_endian>
  4328. void
  4329. Mips_got_info<size, big_endian>::add_page_entries(
  4330. Target_mips<size, big_endian>* target, Layout* layout)
  4331. {
  4332. if (this->page_gotno_ == 0)
  4333. return;
  4334. Mips_output_data_got<size, big_endian>* got = target->got_section();
  4335. this->got_page_offset_start_ = got->add_constant(0);
  4336. if (got->multi_got() && this->index_ > 0
  4337. && parameters->options().output_is_position_independent())
  4338. target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
  4339. this->got_page_offset_start_);
  4340. int num_entries = this->page_gotno_;
  4341. unsigned int prev_offset = this->got_page_offset_start_;
  4342. while (--num_entries > 0)
  4343. {
  4344. unsigned int next_offset = got->add_constant(0);
  4345. if (got->multi_got() && this->index_ > 0
  4346. && parameters->options().output_is_position_independent())
  4347. target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
  4348. next_offset);
  4349. gold_assert(next_offset == prev_offset + size/8);
  4350. prev_offset = next_offset;
  4351. }
  4352. this->got_page_offset_next_ = this->got_page_offset_start_;
  4353. }
  4354. // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
  4355. template<int size, bool big_endian>
  4356. void
  4357. Mips_got_info<size, big_endian>::add_global_entries(
  4358. Target_mips<size, big_endian>* target, Layout* layout,
  4359. unsigned int non_reloc_only_global_gotno)
  4360. {
  4361. Mips_output_data_got<size, big_endian>* got = target->got_section();
  4362. // Add GGA_NORMAL entries.
  4363. unsigned int count = 0;
  4364. for (typename Got_entry_set::iterator
  4365. p = this->got_entries_.begin();
  4366. p != this->got_entries_.end();
  4367. ++p)
  4368. {
  4369. Mips_got_entry<size, big_endian>* entry = *p;
  4370. if (!entry->is_for_global_symbol())
  4371. continue;
  4372. Mips_symbol<size>* mips_sym = entry->sym();
  4373. if (mips_sym->global_got_area() != GGA_NORMAL)
  4374. continue;
  4375. unsigned int got_type;
  4376. if (!got->multi_got())
  4377. got_type = GOT_TYPE_STANDARD;
  4378. else
  4379. // In multi-GOT links, global symbol can be in both primary and
  4380. // secondary GOT(s). By creating custom GOT type
  4381. // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
  4382. // is added to secondary GOT(s).
  4383. got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
  4384. if (!got->add_global(mips_sym, got_type))
  4385. continue;
  4386. mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
  4387. if (got->multi_got() && this->index_ == 0)
  4388. count++;
  4389. if (got->multi_got() && this->index_ > 0)
  4390. {
  4391. if (parameters->options().output_is_position_independent()
  4392. || (!parameters->doing_static_link()
  4393. && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
  4394. {
  4395. target->rel_dyn_section(layout)->add_global(
  4396. mips_sym, elfcpp::R_MIPS_REL32, got,
  4397. mips_sym->got_offset(got_type));
  4398. got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
  4399. elfcpp::R_MIPS_REL32, mips_sym);
  4400. }
  4401. }
  4402. }
  4403. if (!got->multi_got() || this->index_ == 0)
  4404. {
  4405. if (got->multi_got())
  4406. {
  4407. // We need to allocate space in the primary GOT for GGA_NORMAL entries
  4408. // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
  4409. // entries correspond to dynamic symbol indexes.
  4410. while (count < non_reloc_only_global_gotno)
  4411. {
  4412. got->add_constant(0);
  4413. ++count;
  4414. }
  4415. }
  4416. // Add GGA_RELOC_ONLY entries.
  4417. got->add_reloc_only_entries();
  4418. }
  4419. }
  4420. // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
  4421. template<int size, bool big_endian>
  4422. void
  4423. Mips_got_info<size, big_endian>::add_reloc_only_entries(
  4424. Mips_output_data_got<size, big_endian>* got)
  4425. {
  4426. for (typename Unordered_set<Mips_symbol<size>*>::iterator
  4427. p = this->global_got_symbols_.begin();
  4428. p != this->global_got_symbols_.end();
  4429. ++p)
  4430. {
  4431. Mips_symbol<size>* mips_sym = *p;
  4432. if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
  4433. {
  4434. unsigned int got_type;
  4435. if (!got->multi_got())
  4436. got_type = GOT_TYPE_STANDARD;
  4437. else
  4438. got_type = GOT_TYPE_STANDARD_MULTIGOT;
  4439. if (got->add_global(mips_sym, got_type))
  4440. mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
  4441. }
  4442. }
  4443. }
  4444. // Create TLS GOT entries.
  4445. template<int size, bool big_endian>
  4446. void
  4447. Mips_got_info<size, big_endian>::add_tls_entries(
  4448. Target_mips<size, big_endian>* target, Layout* layout)
  4449. {
  4450. Mips_output_data_got<size, big_endian>* got = target->got_section();
  4451. // Add local tls entries.
  4452. for (typename Got_entry_set::iterator
  4453. p = this->got_entries_.begin();
  4454. p != this->got_entries_.end();
  4455. ++p)
  4456. {
  4457. Mips_got_entry<size, big_endian>* entry = *p;
  4458. if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
  4459. continue;
  4460. if (entry->tls_type() == GOT_TLS_GD)
  4461. {
  4462. unsigned int got_type = GOT_TYPE_TLS_PAIR;
  4463. unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
  4464. : elfcpp::R_MIPS_TLS_DTPMOD64);
  4465. unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
  4466. : elfcpp::R_MIPS_TLS_DTPREL64);
  4467. if (!parameters->doing_static_link())
  4468. {
  4469. got->add_local_pair_with_rel(entry->object(), entry->symndx(),
  4470. entry->shndx(), got_type,
  4471. target->rel_dyn_section(layout),
  4472. r_type1);
  4473. unsigned int got_offset =
  4474. entry->object()->local_got_offset(entry->symndx(), got_type);
  4475. got->add_static_reloc(got_offset + size/8, r_type2,
  4476. entry->object(), entry->symndx());
  4477. }
  4478. else
  4479. {
  4480. // We are doing a static link. Mark it as belong to module 1,
  4481. // the executable.
  4482. unsigned int got_offset = got->add_constant(1);
  4483. entry->object()->set_local_got_offset(entry->symndx(), got_type,
  4484. got_offset);
  4485. got->add_constant(0);
  4486. got->add_static_reloc(got_offset + size/8, r_type2,
  4487. entry->object(), entry->symndx());
  4488. }
  4489. }
  4490. else if (entry->tls_type() == GOT_TLS_IE)
  4491. {
  4492. unsigned int got_type = GOT_TYPE_TLS_OFFSET;
  4493. unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
  4494. : elfcpp::R_MIPS_TLS_TPREL64);
  4495. if (!parameters->doing_static_link())
  4496. got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
  4497. target->rel_dyn_section(layout), r_type);
  4498. else
  4499. {
  4500. got->add_local(entry->object(), entry->symndx(), got_type);
  4501. unsigned int got_offset =
  4502. entry->object()->local_got_offset(entry->symndx(), got_type);
  4503. got->add_static_reloc(got_offset, r_type, entry->object(),
  4504. entry->symndx());
  4505. }
  4506. }
  4507. else if (entry->tls_type() == GOT_TLS_LDM)
  4508. {
  4509. unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
  4510. : elfcpp::R_MIPS_TLS_DTPMOD64);
  4511. unsigned int got_offset;
  4512. if (!parameters->doing_static_link())
  4513. {
  4514. got_offset = got->add_constant(0);
  4515. target->rel_dyn_section(layout)->add_local(
  4516. entry->object(), 0, r_type, got, got_offset);
  4517. }
  4518. else
  4519. // We are doing a static link. Just mark it as belong to module 1,
  4520. // the executable.
  4521. got_offset = got->add_constant(1);
  4522. got->add_constant(0);
  4523. got->set_tls_ldm_offset(got_offset, entry->object());
  4524. }
  4525. else
  4526. gold_unreachable();
  4527. }
  4528. // Add global tls entries.
  4529. for (typename Got_entry_set::iterator
  4530. p = this->got_entries_.begin();
  4531. p != this->got_entries_.end();
  4532. ++p)
  4533. {
  4534. Mips_got_entry<size, big_endian>* entry = *p;
  4535. if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
  4536. continue;
  4537. Mips_symbol<size>* mips_sym = entry->sym();
  4538. if (entry->tls_type() == GOT_TLS_GD)
  4539. {
  4540. unsigned int got_type;
  4541. if (!got->multi_got())
  4542. got_type = GOT_TYPE_TLS_PAIR;
  4543. else
  4544. got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
  4545. unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
  4546. : elfcpp::R_MIPS_TLS_DTPMOD64);
  4547. unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
  4548. : elfcpp::R_MIPS_TLS_DTPREL64);
  4549. if (!parameters->doing_static_link())
  4550. got->add_global_pair_with_rel(mips_sym, got_type,
  4551. target->rel_dyn_section(layout), r_type1, r_type2);
  4552. else
  4553. {
  4554. // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
  4555. // GOT entries. The first one is initialized to be 1, which is the
  4556. // module index for the main executable and the second one 0. A
  4557. // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
  4558. // the second GOT entry and will be applied by gold.
  4559. unsigned int got_offset = got->add_constant(1);
  4560. mips_sym->set_got_offset(got_type, got_offset);
  4561. got->add_constant(0);
  4562. got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
  4563. }
  4564. }
  4565. else if (entry->tls_type() == GOT_TLS_IE)
  4566. {
  4567. unsigned int got_type;
  4568. if (!got->multi_got())
  4569. got_type = GOT_TYPE_TLS_OFFSET;
  4570. else
  4571. got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
  4572. unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
  4573. : elfcpp::R_MIPS_TLS_TPREL64);
  4574. if (!parameters->doing_static_link())
  4575. got->add_global_with_rel(mips_sym, got_type,
  4576. target->rel_dyn_section(layout), r_type);
  4577. else
  4578. {
  4579. got->add_global(mips_sym, got_type);
  4580. unsigned int got_offset = mips_sym->got_offset(got_type);
  4581. got->add_static_reloc(got_offset, r_type, mips_sym);
  4582. }
  4583. }
  4584. else
  4585. gold_unreachable();
  4586. }
  4587. }
  4588. // Decide whether the symbol needs an entry in the global part of the primary
  4589. // GOT, setting global_got_area accordingly. Count the number of global
  4590. // symbols that are in the primary GOT only because they have dynamic
  4591. // relocations R_MIPS_REL32 against them (reloc_only_gotno).
  4592. template<int size, bool big_endian>
  4593. void
  4594. Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
  4595. {
  4596. for (typename Unordered_set<Mips_symbol<size>*>::iterator
  4597. p = this->global_got_symbols_.begin();
  4598. p != this->global_got_symbols_.end();
  4599. ++p)
  4600. {
  4601. Mips_symbol<size>* sym = *p;
  4602. // Make a final decision about whether the symbol belongs in the
  4603. // local or global GOT. Symbols that bind locally can (and in the
  4604. // case of forced-local symbols, must) live in the local GOT.
  4605. // Those that are aren't in the dynamic symbol table must also
  4606. // live in the local GOT.
  4607. if (!sym->should_add_dynsym_entry(symtab)
  4608. || (sym->got_only_for_calls()
  4609. ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
  4610. : symbol_references_local(sym,
  4611. sym->should_add_dynsym_entry(symtab))))
  4612. // The symbol belongs in the local GOT. We no longer need this
  4613. // entry if it was only used for relocations; those relocations
  4614. // will be against the null or section symbol instead.
  4615. sym->set_global_got_area(GGA_NONE);
  4616. else if (sym->global_got_area() == GGA_RELOC_ONLY)
  4617. {
  4618. ++this->reloc_only_gotno_;
  4619. ++this->global_gotno_ ;
  4620. }
  4621. }
  4622. }
  4623. // Return the offset of GOT page entry for VALUE. Initialize the entry with
  4624. // VALUE if it is not initialized.
  4625. template<int size, bool big_endian>
  4626. unsigned int
  4627. Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
  4628. Mips_output_data_got<size, big_endian>* got)
  4629. {
  4630. typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
  4631. if (it != this->got_page_offsets_.end())
  4632. return it->second;
  4633. gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
  4634. + (size/8) * this->page_gotno_);
  4635. unsigned int got_offset = this->got_page_offset_next_;
  4636. this->got_page_offsets_[value] = got_offset;
  4637. this->got_page_offset_next_ += size/8;
  4638. got->update_got_entry(got_offset, value);
  4639. return got_offset;
  4640. }
  4641. // Remove lazy-binding stubs for global symbols in this GOT.
  4642. template<int size, bool big_endian>
  4643. void
  4644. Mips_got_info<size, big_endian>::remove_lazy_stubs(
  4645. Target_mips<size, big_endian>* target)
  4646. {
  4647. for (typename Got_entry_set::iterator
  4648. p = this->got_entries_.begin();
  4649. p != this->got_entries_.end();
  4650. ++p)
  4651. {
  4652. Mips_got_entry<size, big_endian>* entry = *p;
  4653. if (entry->is_for_global_symbol())
  4654. target->remove_lazy_stub_entry(entry->sym());
  4655. }
  4656. }
  4657. // Count the number of GOT entries required.
  4658. template<int size, bool big_endian>
  4659. void
  4660. Mips_got_info<size, big_endian>::count_got_entries()
  4661. {
  4662. for (typename Got_entry_set::iterator
  4663. p = this->got_entries_.begin();
  4664. p != this->got_entries_.end();
  4665. ++p)
  4666. {
  4667. this->count_got_entry(*p);
  4668. }
  4669. }
  4670. // Count the number of GOT entries required by ENTRY. Accumulate the result.
  4671. template<int size, bool big_endian>
  4672. void
  4673. Mips_got_info<size, big_endian>::count_got_entry(
  4674. Mips_got_entry<size, big_endian>* entry)
  4675. {
  4676. if (entry->is_tls_entry())
  4677. this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
  4678. else if (entry->is_for_local_symbol()
  4679. || entry->sym()->global_got_area() == GGA_NONE)
  4680. ++this->local_gotno_;
  4681. else
  4682. ++this->global_gotno_;
  4683. }
  4684. // Add FROM's GOT entries.
  4685. template<int size, bool big_endian>
  4686. void
  4687. Mips_got_info<size, big_endian>::add_got_entries(
  4688. Mips_got_info<size, big_endian>* from)
  4689. {
  4690. for (typename Got_entry_set::iterator
  4691. p = from->got_entries_.begin();
  4692. p != from->got_entries_.end();
  4693. ++p)
  4694. {
  4695. Mips_got_entry<size, big_endian>* entry = *p;
  4696. if (this->got_entries_.find(entry) == this->got_entries_.end())
  4697. {
  4698. Mips_got_entry<size, big_endian>* entry2 =
  4699. new Mips_got_entry<size, big_endian>(*entry);
  4700. this->got_entries_.insert(entry2);
  4701. this->count_got_entry(entry);
  4702. }
  4703. }
  4704. }
  4705. // Add FROM's GOT page entries.
  4706. template<int size, bool big_endian>
  4707. void
  4708. Mips_got_info<size, big_endian>::add_got_page_entries(
  4709. Mips_got_info<size, big_endian>* from)
  4710. {
  4711. for (typename Got_page_entry_set::iterator
  4712. p = from->got_page_entries_.begin();
  4713. p != from->got_page_entries_.end();
  4714. ++p)
  4715. {
  4716. Got_page_entry* entry = *p;
  4717. if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
  4718. {
  4719. Got_page_entry* entry2 = new Got_page_entry(*entry);
  4720. this->got_page_entries_.insert(entry2);
  4721. this->page_gotno_ += entry->num_pages;
  4722. }
  4723. }
  4724. }
  4725. // Mips_output_data_got methods.
  4726. // Lay out the GOT. Add local, global and TLS entries. If GOT is
  4727. // larger than 64K, create multi-GOT.
  4728. template<int size, bool big_endian>
  4729. void
  4730. Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
  4731. Symbol_table* symtab, const Input_objects* input_objects)
  4732. {
  4733. // Decide which symbols need to go in the global part of the GOT and
  4734. // count the number of reloc-only GOT symbols.
  4735. this->master_got_info_->count_got_symbols(symtab);
  4736. // Count the number of GOT entries.
  4737. this->master_got_info_->count_got_entries();
  4738. unsigned int got_size = this->master_got_info_->got_size();
  4739. if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
  4740. this->lay_out_multi_got(layout, input_objects);
  4741. else
  4742. {
  4743. // Record that all objects use single GOT.
  4744. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  4745. p != input_objects->relobj_end();
  4746. ++p)
  4747. {
  4748. Mips_relobj<size, big_endian>* object =
  4749. Mips_relobj<size, big_endian>::as_mips_relobj(*p);
  4750. if (object->get_got_info() != NULL)
  4751. object->set_got_info(this->master_got_info_);
  4752. }
  4753. this->master_got_info_->add_local_entries(this->target_, layout);
  4754. this->master_got_info_->add_global_entries(this->target_, layout,
  4755. /*not used*/-1U);
  4756. this->master_got_info_->add_tls_entries(this->target_, layout);
  4757. }
  4758. }
  4759. // Create multi-GOT. For every GOT, add local, global and TLS entries.
  4760. template<int size, bool big_endian>
  4761. void
  4762. Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
  4763. const Input_objects* input_objects)
  4764. {
  4765. // Try to merge the GOTs of input objects together, as long as they
  4766. // don't seem to exceed the maximum GOT size, choosing one of them
  4767. // to be the primary GOT.
  4768. this->merge_gots(input_objects);
  4769. // Every symbol that is referenced in a dynamic relocation must be
  4770. // present in the primary GOT.
  4771. this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
  4772. // Add GOT entries.
  4773. unsigned int i = 0;
  4774. unsigned int offset = 0;
  4775. Mips_got_info<size, big_endian>* g = this->primary_got_;
  4776. do
  4777. {
  4778. g->set_index(i);
  4779. g->set_offset(offset);
  4780. g->add_local_entries(this->target_, layout);
  4781. if (i == 0)
  4782. g->add_global_entries(this->target_, layout,
  4783. (this->master_got_info_->global_gotno()
  4784. - this->master_got_info_->reloc_only_gotno()));
  4785. else
  4786. g->add_global_entries(this->target_, layout, /*not used*/-1U);
  4787. g->add_tls_entries(this->target_, layout);
  4788. // Forbid global symbols in every non-primary GOT from having
  4789. // lazy-binding stubs.
  4790. if (i > 0)
  4791. g->remove_lazy_stubs(this->target_);
  4792. ++i;
  4793. offset += g->got_size();
  4794. g = g->next();
  4795. }
  4796. while (g);
  4797. }
  4798. // Attempt to merge GOTs of different input objects. Try to use as much as
  4799. // possible of the primary GOT, since it doesn't require explicit dynamic
  4800. // relocations, but don't use objects that would reference global symbols
  4801. // out of the addressable range. Failing the primary GOT, attempt to merge
  4802. // with the current GOT, or finish the current GOT and then make make the new
  4803. // GOT current.
  4804. template<int size, bool big_endian>
  4805. void
  4806. Mips_output_data_got<size, big_endian>::merge_gots(
  4807. const Input_objects* input_objects)
  4808. {
  4809. gold_assert(this->primary_got_ == NULL);
  4810. Mips_got_info<size, big_endian>* current = NULL;
  4811. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  4812. p != input_objects->relobj_end();
  4813. ++p)
  4814. {
  4815. Mips_relobj<size, big_endian>* object =
  4816. Mips_relobj<size, big_endian>::as_mips_relobj(*p);
  4817. Mips_got_info<size, big_endian>* g = object->get_got_info();
  4818. if (g == NULL)
  4819. continue;
  4820. g->count_got_entries();
  4821. // Work out the number of page, local and TLS entries.
  4822. unsigned int estimate = this->master_got_info_->page_gotno();
  4823. if (estimate > g->page_gotno())
  4824. estimate = g->page_gotno();
  4825. estimate += g->local_gotno() + g->tls_gotno();
  4826. // We place TLS GOT entries after both locals and globals. The globals
  4827. // for the primary GOT may overflow the normal GOT size limit, so be
  4828. // sure not to merge a GOT which requires TLS with the primary GOT in that
  4829. // case. This doesn't affect non-primary GOTs.
  4830. estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
  4831. : g->global_gotno());
  4832. unsigned int max_count =
  4833. Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
  4834. if (estimate <= max_count)
  4835. {
  4836. // If we don't have a primary GOT, use it as
  4837. // a starting point for the primary GOT.
  4838. if (!this->primary_got_)
  4839. {
  4840. this->primary_got_ = g;
  4841. continue;
  4842. }
  4843. // Try merging with the primary GOT.
  4844. if (this->merge_got_with(g, object, this->primary_got_))
  4845. continue;
  4846. }
  4847. // If we can merge with the last-created GOT, do it.
  4848. if (current && this->merge_got_with(g, object, current))
  4849. continue;
  4850. // Well, we couldn't merge, so create a new GOT. Don't check if it
  4851. // fits; if it turns out that it doesn't, we'll get relocation
  4852. // overflows anyway.
  4853. g->set_next(current);
  4854. current = g;
  4855. }
  4856. // If we do not find any suitable primary GOT, create an empty one.
  4857. if (this->primary_got_ == NULL)
  4858. this->primary_got_ = new Mips_got_info<size, big_endian>();
  4859. // Link primary GOT with secondary GOTs.
  4860. this->primary_got_->set_next(current);
  4861. }
  4862. // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
  4863. // this would lead to overflow, true if they were merged successfully.
  4864. template<int size, bool big_endian>
  4865. bool
  4866. Mips_output_data_got<size, big_endian>::merge_got_with(
  4867. Mips_got_info<size, big_endian>* from,
  4868. Mips_relobj<size, big_endian>* object,
  4869. Mips_got_info<size, big_endian>* to)
  4870. {
  4871. // Work out how many page entries we would need for the combined GOT.
  4872. unsigned int estimate = this->master_got_info_->page_gotno();
  4873. if (estimate >= from->page_gotno() + to->page_gotno())
  4874. estimate = from->page_gotno() + to->page_gotno();
  4875. // Conservatively estimate how many local and TLS entries would be needed.
  4876. estimate += from->local_gotno() + to->local_gotno();
  4877. estimate += from->tls_gotno() + to->tls_gotno();
  4878. // If we're merging with the primary got, any TLS relocations will
  4879. // come after the full set of global entries. Otherwise estimate those
  4880. // conservatively as well.
  4881. if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
  4882. estimate += this->master_got_info_->global_gotno();
  4883. else
  4884. estimate += from->global_gotno() + to->global_gotno();
  4885. // Bail out if the combined GOT might be too big.
  4886. unsigned int max_count =
  4887. Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
  4888. if (estimate > max_count)
  4889. return false;
  4890. // Transfer the object's GOT information from FROM to TO.
  4891. to->add_got_entries(from);
  4892. to->add_got_page_entries(from);
  4893. // Record that OBJECT should use output GOT TO.
  4894. object->set_got_info(to);
  4895. return true;
  4896. }
  4897. // Write out the GOT.
  4898. template<int size, bool big_endian>
  4899. void
  4900. Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
  4901. {
  4902. // Call parent to write out GOT.
  4903. Output_data_got<size, big_endian>::do_write(of);
  4904. const off_t offset = this->offset();
  4905. const section_size_type oview_size =
  4906. convert_to_section_size_type(this->data_size());
  4907. unsigned char* const oview = of->get_output_view(offset, oview_size);
  4908. // Needed for fixing values of .got section.
  4909. this->got_view_ = oview;
  4910. // Write lazy stub addresses.
  4911. for (typename Unordered_set<Mips_symbol<size>*>::iterator
  4912. p = this->master_got_info_->global_got_symbols().begin();
  4913. p != this->master_got_info_->global_got_symbols().end();
  4914. ++p)
  4915. {
  4916. Mips_symbol<size>* mips_sym = *p;
  4917. if (mips_sym->has_lazy_stub())
  4918. {
  4919. Valtype* wv = reinterpret_cast<Valtype*>(
  4920. oview + this->get_primary_got_offset(mips_sym));
  4921. Valtype value =
  4922. this->target_->mips_stubs_section()->stub_address(mips_sym);
  4923. elfcpp::Swap<size, big_endian>::writeval(wv, value);
  4924. }
  4925. }
  4926. // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
  4927. for (typename Unordered_set<Mips_symbol<size>*>::iterator
  4928. p = this->master_got_info_->global_got_symbols().begin();
  4929. p != this->master_got_info_->global_got_symbols().end();
  4930. ++p)
  4931. {
  4932. Mips_symbol<size>* mips_sym = *p;
  4933. if (!this->multi_got()
  4934. && (mips_sym->is_mips16() || mips_sym->is_micromips())
  4935. && mips_sym->global_got_area() == GGA_NONE
  4936. && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
  4937. {
  4938. Valtype* wv = reinterpret_cast<Valtype*>(
  4939. oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
  4940. Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
  4941. if (value != 0)
  4942. {
  4943. value |= 1;
  4944. elfcpp::Swap<size, big_endian>::writeval(wv, value);
  4945. }
  4946. }
  4947. }
  4948. if (!this->secondary_got_relocs_.empty())
  4949. {
  4950. // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
  4951. // secondary GOT entries with non-zero initial value copy the value
  4952. // to the corresponding primary GOT entry, and set the secondary GOT
  4953. // entry to zero.
  4954. // TODO(sasa): This is workaround. It needs to be investigated further.
  4955. for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
  4956. {
  4957. Static_reloc& reloc(this->secondary_got_relocs_[i]);
  4958. if (reloc.symbol_is_global())
  4959. {
  4960. Mips_symbol<size>* gsym = reloc.symbol();
  4961. gold_assert(gsym != NULL);
  4962. unsigned got_offset = reloc.got_offset();
  4963. gold_assert(got_offset < oview_size);
  4964. // Find primary GOT entry.
  4965. Valtype* wv_prim = reinterpret_cast<Valtype*>(
  4966. oview + this->get_primary_got_offset(gsym));
  4967. // Find secondary GOT entry.
  4968. Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
  4969. Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
  4970. if (value != 0)
  4971. {
  4972. elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
  4973. elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
  4974. gsym->set_applied_secondary_got_fixup();
  4975. }
  4976. }
  4977. }
  4978. of->write_output_view(offset, oview_size, oview);
  4979. }
  4980. // We are done if there is no fix up.
  4981. if (this->static_relocs_.empty())
  4982. return;
  4983. Output_segment* tls_segment = this->layout_->tls_segment();
  4984. gold_assert(tls_segment != NULL);
  4985. for (size_t i = 0; i < this->static_relocs_.size(); ++i)
  4986. {
  4987. Static_reloc& reloc(this->static_relocs_[i]);
  4988. Mips_address value;
  4989. if (!reloc.symbol_is_global())
  4990. {
  4991. Sized_relobj_file<size, big_endian>* object = reloc.relobj();
  4992. const Symbol_value<size>* psymval =
  4993. object->local_symbol(reloc.index());
  4994. // We are doing static linking. Issue an error and skip this
  4995. // relocation if the symbol is undefined or in a discarded_section.
  4996. bool is_ordinary;
  4997. unsigned int shndx = psymval->input_shndx(&is_ordinary);
  4998. if ((shndx == elfcpp::SHN_UNDEF)
  4999. || (is_ordinary
  5000. && shndx != elfcpp::SHN_UNDEF
  5001. && !object->is_section_included(shndx)
  5002. && !this->symbol_table_->is_section_folded(object, shndx)))
  5003. {
  5004. gold_error(_("undefined or discarded local symbol %u from "
  5005. " object %s in GOT"),
  5006. reloc.index(), reloc.relobj()->name().c_str());
  5007. continue;
  5008. }
  5009. value = psymval->value(object, 0);
  5010. }
  5011. else
  5012. {
  5013. const Mips_symbol<size>* gsym = reloc.symbol();
  5014. gold_assert(gsym != NULL);
  5015. // We are doing static linking. Issue an error and skip this
  5016. // relocation if the symbol is undefined or in a discarded_section
  5017. // unless it is a weakly_undefined symbol.
  5018. if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
  5019. && !gsym->is_weak_undefined())
  5020. {
  5021. gold_error(_("undefined or discarded symbol %s in GOT"),
  5022. gsym->name());
  5023. continue;
  5024. }
  5025. if (!gsym->is_weak_undefined())
  5026. value = gsym->value();
  5027. else
  5028. value = 0;
  5029. }
  5030. unsigned got_offset = reloc.got_offset();
  5031. gold_assert(got_offset < oview_size);
  5032. Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
  5033. Valtype x;
  5034. switch (reloc.r_type())
  5035. {
  5036. case elfcpp::R_MIPS_TLS_DTPMOD32:
  5037. case elfcpp::R_MIPS_TLS_DTPMOD64:
  5038. x = value;
  5039. break;
  5040. case elfcpp::R_MIPS_TLS_DTPREL32:
  5041. case elfcpp::R_MIPS_TLS_DTPREL64:
  5042. x = value - elfcpp::DTP_OFFSET;
  5043. break;
  5044. case elfcpp::R_MIPS_TLS_TPREL32:
  5045. case elfcpp::R_MIPS_TLS_TPREL64:
  5046. x = value - elfcpp::TP_OFFSET;
  5047. break;
  5048. default:
  5049. gold_unreachable();
  5050. break;
  5051. }
  5052. elfcpp::Swap<size, big_endian>::writeval(wv, x);
  5053. }
  5054. of->write_output_view(offset, oview_size, oview);
  5055. }
  5056. // Mips_relobj methods.
  5057. // Count the local symbols. The Mips backend needs to know if a symbol
  5058. // is a MIPS16 or microMIPS function or not. For global symbols, it is easy
  5059. // because the Symbol object keeps the ELF symbol type and st_other field.
  5060. // For local symbol it is harder because we cannot access this information.
  5061. // So we override the do_count_local_symbol in parent and scan local symbols to
  5062. // mark MIPS16 and microMIPS functions. This is not the most efficient way but
  5063. // I do not want to slow down other ports by calling a per symbol target hook
  5064. // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
  5065. template<int size, bool big_endian>
  5066. void
  5067. Mips_relobj<size, big_endian>::do_count_local_symbols(
  5068. Stringpool_template<char>* pool,
  5069. Stringpool_template<char>* dynpool)
  5070. {
  5071. // Ask parent to count the local symbols.
  5072. Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
  5073. const unsigned int loccount = this->local_symbol_count();
  5074. if (loccount == 0)
  5075. return;
  5076. // Initialize the mips16 and micromips function bit-vector.
  5077. this->local_symbol_is_mips16_.resize(loccount, false);
  5078. this->local_symbol_is_micromips_.resize(loccount, false);
  5079. // Read the symbol table section header.
  5080. const unsigned int symtab_shndx = this->symtab_shndx();
  5081. elfcpp::Shdr<size, big_endian>
  5082. symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
  5083. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  5084. // Read the local symbols.
  5085. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  5086. gold_assert(loccount == symtabshdr.get_sh_info());
  5087. off_t locsize = loccount * sym_size;
  5088. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  5089. locsize, true, true);
  5090. // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
  5091. // Skip the first dummy symbol.
  5092. psyms += sym_size;
  5093. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  5094. {
  5095. elfcpp::Sym<size, big_endian> sym(psyms);
  5096. unsigned char st_other = sym.get_st_other();
  5097. this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
  5098. this->local_symbol_is_micromips_[i] =
  5099. elfcpp::elf_st_is_micromips(st_other);
  5100. }
  5101. }
  5102. // Read the symbol information.
  5103. template<int size, bool big_endian>
  5104. void
  5105. Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
  5106. {
  5107. // Call parent class to read symbol information.
  5108. this->base_read_symbols(sd);
  5109. // Read processor-specific flags in ELF file header.
  5110. const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
  5111. elfcpp::Elf_sizes<size>::ehdr_size,
  5112. true, false);
  5113. elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
  5114. this->processor_specific_flags_ = ehdr.get_e_flags();
  5115. // Get the section names.
  5116. const unsigned char* pnamesu = sd->section_names->data();
  5117. const char* pnames = reinterpret_cast<const char*>(pnamesu);
  5118. // Initialize the mips16 stub section bit-vectors.
  5119. this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
  5120. this->section_is_mips16_call_stub_.resize(this->shnum(), false);
  5121. this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
  5122. const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  5123. const unsigned char* pshdrs = sd->section_headers->data();
  5124. const unsigned char* ps = pshdrs + shdr_size;
  5125. for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
  5126. {
  5127. elfcpp::Shdr<size, big_endian> shdr(ps);
  5128. if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
  5129. {
  5130. // Read the gp value that was used to create this object. We need the
  5131. // gp value while processing relocs. The .reginfo section is not used
  5132. // in the 64-bit MIPS ELF ABI.
  5133. section_offset_type section_offset = shdr.get_sh_offset();
  5134. section_size_type section_size =
  5135. convert_to_section_size_type(shdr.get_sh_size());
  5136. const unsigned char* view =
  5137. this->get_view(section_offset, section_size, true, false);
  5138. this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
  5139. // Read the rest of .reginfo.
  5140. this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
  5141. this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
  5142. this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
  5143. this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
  5144. this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
  5145. }
  5146. const char* name = pnames + shdr.get_sh_name();
  5147. this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
  5148. this->section_is_mips16_call_stub_[i] =
  5149. is_prefix_of(".mips16.call.", name);
  5150. this->section_is_mips16_call_fp_stub_[i] =
  5151. is_prefix_of(".mips16.call.fp.", name);
  5152. if (strcmp(name, ".pdr") == 0)
  5153. {
  5154. gold_assert(this->pdr_shndx_ == -1U);
  5155. this->pdr_shndx_ = i;
  5156. }
  5157. }
  5158. }
  5159. // Discard MIPS16 stub secions that are not needed.
  5160. template<int size, bool big_endian>
  5161. void
  5162. Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
  5163. {
  5164. for (typename Mips16_stubs_int_map::const_iterator
  5165. it = this->mips16_stub_sections_.begin();
  5166. it != this->mips16_stub_sections_.end(); ++it)
  5167. {
  5168. Mips16_stub_section<size, big_endian>* stub_section = it->second;
  5169. if (!stub_section->is_target_found())
  5170. {
  5171. gold_error(_("no relocation found in mips16 stub section '%s'"),
  5172. stub_section->object()
  5173. ->section_name(stub_section->shndx()).c_str());
  5174. }
  5175. bool discard = false;
  5176. if (stub_section->is_for_local_function())
  5177. {
  5178. if (stub_section->is_fn_stub())
  5179. {
  5180. // This stub is for a local symbol. This stub will only
  5181. // be needed if there is some relocation in this object,
  5182. // other than a 16 bit function call, which refers to this
  5183. // symbol.
  5184. if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
  5185. discard = true;
  5186. else
  5187. this->add_local_mips16_fn_stub(stub_section);
  5188. }
  5189. else
  5190. {
  5191. // This stub is for a local symbol. This stub will only
  5192. // be needed if there is some relocation (R_MIPS16_26) in
  5193. // this object that refers to this symbol.
  5194. gold_assert(stub_section->is_call_stub()
  5195. || stub_section->is_call_fp_stub());
  5196. if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
  5197. discard = true;
  5198. else
  5199. this->add_local_mips16_call_stub(stub_section);
  5200. }
  5201. }
  5202. else
  5203. {
  5204. Mips_symbol<size>* gsym = stub_section->gsym();
  5205. if (stub_section->is_fn_stub())
  5206. {
  5207. if (gsym->has_mips16_fn_stub())
  5208. // We already have a stub for this function.
  5209. discard = true;
  5210. else
  5211. {
  5212. gsym->set_mips16_fn_stub(stub_section);
  5213. if (gsym->should_add_dynsym_entry(symtab))
  5214. {
  5215. // If we have a MIPS16 function with a stub, the
  5216. // dynamic symbol must refer to the stub, since only
  5217. // the stub uses the standard calling conventions.
  5218. gsym->set_need_fn_stub();
  5219. if (gsym->is_from_dynobj())
  5220. gsym->set_needs_dynsym_value();
  5221. }
  5222. }
  5223. if (!gsym->need_fn_stub())
  5224. discard = true;
  5225. }
  5226. else if (stub_section->is_call_stub())
  5227. {
  5228. if (gsym->is_mips16())
  5229. // We don't need the call_stub; this is a 16 bit
  5230. // function, so calls from other 16 bit functions are
  5231. // OK.
  5232. discard = true;
  5233. else if (gsym->has_mips16_call_stub())
  5234. // We already have a stub for this function.
  5235. discard = true;
  5236. else
  5237. gsym->set_mips16_call_stub(stub_section);
  5238. }
  5239. else
  5240. {
  5241. gold_assert(stub_section->is_call_fp_stub());
  5242. if (gsym->is_mips16())
  5243. // We don't need the call_stub; this is a 16 bit
  5244. // function, so calls from other 16 bit functions are
  5245. // OK.
  5246. discard = true;
  5247. else if (gsym->has_mips16_call_fp_stub())
  5248. // We already have a stub for this function.
  5249. discard = true;
  5250. else
  5251. gsym->set_mips16_call_fp_stub(stub_section);
  5252. }
  5253. }
  5254. if (discard)
  5255. this->set_output_section(stub_section->shndx(), NULL);
  5256. }
  5257. }
  5258. // Mips_output_data_la25_stub methods.
  5259. // Template for standard LA25 stub.
  5260. template<int size, bool big_endian>
  5261. const uint32_t
  5262. Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
  5263. {
  5264. 0x3c190000, // lui $25,%hi(func)
  5265. 0x08000000, // j func
  5266. 0x27390000, // add $25,$25,%lo(func)
  5267. 0x00000000 // nop
  5268. };
  5269. // Template for microMIPS LA25 stub.
  5270. template<int size, bool big_endian>
  5271. const uint32_t
  5272. Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
  5273. {
  5274. 0x41b9, 0x0000, // lui t9,%hi(func)
  5275. 0xd400, 0x0000, // j func
  5276. 0x3339, 0x0000, // addiu t9,t9,%lo(func)
  5277. 0x0000, 0x0000 // nop
  5278. };
  5279. // Create la25 stub for a symbol.
  5280. template<int size, bool big_endian>
  5281. void
  5282. Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
  5283. Symbol_table* symtab, Target_mips<size, big_endian>* target,
  5284. Mips_symbol<size>* gsym)
  5285. {
  5286. if (!gsym->has_la25_stub())
  5287. {
  5288. gsym->set_la25_stub_offset(this->symbols_.size() * 16);
  5289. this->symbols_.insert(gsym);
  5290. this->create_stub_symbol(gsym, symtab, target, 16);
  5291. }
  5292. }
  5293. // Create a symbol for SYM stub's value and size, to help make the disassembly
  5294. // easier to read.
  5295. template<int size, bool big_endian>
  5296. void
  5297. Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
  5298. Mips_symbol<size>* sym, Symbol_table* symtab,
  5299. Target_mips<size, big_endian>* target, uint64_t symsize)
  5300. {
  5301. std::string name(".pic.");
  5302. name += sym->name();
  5303. unsigned int offset = sym->la25_stub_offset();
  5304. if (sym->is_micromips())
  5305. offset |= 1;
  5306. // Make it a local function.
  5307. Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
  5308. Symbol_table::PREDEFINED,
  5309. target->la25_stub_section(),
  5310. offset, symsize, elfcpp::STT_FUNC,
  5311. elfcpp::STB_LOCAL,
  5312. elfcpp::STV_DEFAULT, 0,
  5313. false, false);
  5314. new_sym->set_is_forced_local();
  5315. }
  5316. // Write out la25 stubs. This uses the hand-coded instructions above,
  5317. // and adjusts them as needed.
  5318. template<int size, bool big_endian>
  5319. void
  5320. Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
  5321. {
  5322. const off_t offset = this->offset();
  5323. const section_size_type oview_size =
  5324. convert_to_section_size_type(this->data_size());
  5325. unsigned char* const oview = of->get_output_view(offset, oview_size);
  5326. for (typename Unordered_set<Mips_symbol<size>*>::iterator
  5327. p = this->symbols_.begin();
  5328. p != this->symbols_.end();
  5329. ++p)
  5330. {
  5331. Mips_symbol<size>* sym = *p;
  5332. unsigned char* pov = oview + sym->la25_stub_offset();
  5333. Mips_address target = sym->value();
  5334. if (!sym->is_micromips())
  5335. {
  5336. elfcpp::Swap<32, big_endian>::writeval(pov,
  5337. la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
  5338. elfcpp::Swap<32, big_endian>::writeval(pov + 4,
  5339. la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
  5340. elfcpp::Swap<32, big_endian>::writeval(pov + 8,
  5341. la25_stub_entry[2] | (target & 0xffff));
  5342. elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
  5343. }
  5344. else
  5345. {
  5346. target |= 1;
  5347. // First stub instruction. Paste high 16-bits of the target.
  5348. elfcpp::Swap<16, big_endian>::writeval(pov,
  5349. la25_stub_micromips_entry[0]);
  5350. elfcpp::Swap<16, big_endian>::writeval(pov + 2,
  5351. ((target + 0x8000) >> 16) & 0xffff);
  5352. // Second stub instruction. Paste low 26-bits of the target, shifted
  5353. // right by 1.
  5354. elfcpp::Swap<16, big_endian>::writeval(pov + 4,
  5355. la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
  5356. elfcpp::Swap<16, big_endian>::writeval(pov + 6,
  5357. la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
  5358. // Third stub instruction. Paste low 16-bits of the target.
  5359. elfcpp::Swap<16, big_endian>::writeval(pov + 8,
  5360. la25_stub_micromips_entry[4]);
  5361. elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
  5362. // Fourth stub instruction.
  5363. elfcpp::Swap<16, big_endian>::writeval(pov + 12,
  5364. la25_stub_micromips_entry[6]);
  5365. elfcpp::Swap<16, big_endian>::writeval(pov + 14,
  5366. la25_stub_micromips_entry[7]);
  5367. }
  5368. }
  5369. of->write_output_view(offset, oview_size, oview);
  5370. }
  5371. // Mips_output_data_plt methods.
  5372. // The format of the first PLT entry in an O32 executable.
  5373. template<int size, bool big_endian>
  5374. const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
  5375. {
  5376. 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
  5377. 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
  5378. 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
  5379. 0x031cc023, // subu $24, $24, $28
  5380. 0x03e07825, // or $15, $31, zero
  5381. 0x0018c082, // srl $24, $24, 2
  5382. 0x0320f809, // jalr $25
  5383. 0x2718fffe // subu $24, $24, 2
  5384. };
  5385. // The format of the first PLT entry in an N32 executable. Different
  5386. // because gp ($28) is not available; we use t2 ($14) instead.
  5387. template<int size, bool big_endian>
  5388. const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
  5389. {
  5390. 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
  5391. 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
  5392. 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
  5393. 0x030ec023, // subu $24, $24, $14
  5394. 0x03e07825, // or $15, $31, zero
  5395. 0x0018c082, // srl $24, $24, 2
  5396. 0x0320f809, // jalr $25
  5397. 0x2718fffe // subu $24, $24, 2
  5398. };
  5399. // The format of the first PLT entry in an N64 executable. Different
  5400. // from N32 because of the increased size of GOT entries.
  5401. template<int size, bool big_endian>
  5402. const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
  5403. {
  5404. 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
  5405. 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
  5406. 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
  5407. 0x030ec023, // subu $24, $24, $14
  5408. 0x03e07825, // or $15, $31, zero
  5409. 0x0018c0c2, // srl $24, $24, 3
  5410. 0x0320f809, // jalr $25
  5411. 0x2718fffe // subu $24, $24, 2
  5412. };
  5413. // The format of the microMIPS first PLT entry in an O32 executable.
  5414. // We rely on v0 ($2) rather than t8 ($24) to contain the address
  5415. // of the GOTPLT entry handled, so this stub may only be used when
  5416. // all the subsequent PLT entries are microMIPS code too.
  5417. //
  5418. // The trailing NOP is for alignment and correct disassembly only.
  5419. template<int size, bool big_endian>
  5420. const uint32_t Mips_output_data_plt<size, big_endian>::
  5421. plt0_entry_micromips_o32[] =
  5422. {
  5423. 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
  5424. 0xff23, 0x0000, // lw $25, 0($3)
  5425. 0x0535, // subu $2, $2, $3
  5426. 0x2525, // srl $2, $2, 2
  5427. 0x3302, 0xfffe, // subu $24, $2, 2
  5428. 0x0dff, // move $15, $31
  5429. 0x45f9, // jalrs $25
  5430. 0x0f83, // move $28, $3
  5431. 0x0c00 // nop
  5432. };
  5433. // The format of the microMIPS first PLT entry in an O32 executable
  5434. // in the insn32 mode.
  5435. template<int size, bool big_endian>
  5436. const uint32_t Mips_output_data_plt<size, big_endian>::
  5437. plt0_entry_micromips32_o32[] =
  5438. {
  5439. 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
  5440. 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
  5441. 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
  5442. 0x0398, 0xc1d0, // subu $24, $24, $28
  5443. 0x001f, 0x7a90, // or $15, $31, zero
  5444. 0x0318, 0x1040, // srl $24, $24, 2
  5445. 0x03f9, 0x0f3c, // jalr $25
  5446. 0x3318, 0xfffe // subu $24, $24, 2
  5447. };
  5448. // The format of subsequent standard entries in the PLT.
  5449. template<int size, bool big_endian>
  5450. const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
  5451. {
  5452. 0x3c0f0000, // lui $15, %hi(.got.plt entry)
  5453. 0x8df90000, // l[wd] $25, %lo(.got.plt entry)($15)
  5454. 0x03200008, // jr $25
  5455. 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
  5456. };
  5457. // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
  5458. // temporary because t8 ($24) and t9 ($25) are not directly addressable.
  5459. // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
  5460. // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
  5461. // target function address in register v0.
  5462. template<int size, bool big_endian>
  5463. const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
  5464. {
  5465. 0xb303, // lw $3, 12($pc)
  5466. 0x651b, // move $24, $3
  5467. 0x9b60, // lw $3, 0($3)
  5468. 0xeb00, // jr $3
  5469. 0x653b, // move $25, $3
  5470. 0x6500, // nop
  5471. 0x0000, 0x0000 // .word (.got.plt entry)
  5472. };
  5473. // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
  5474. // as a temporary because t8 ($24) is not addressable with ADDIUPC.
  5475. template<int size, bool big_endian>
  5476. const uint32_t Mips_output_data_plt<size, big_endian>::
  5477. plt_entry_micromips_o32[] =
  5478. {
  5479. 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
  5480. 0xff22, 0x0000, // lw $25, 0($2)
  5481. 0x4599, // jr $25
  5482. 0x0f02 // move $24, $2
  5483. };
  5484. // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
  5485. template<int size, bool big_endian>
  5486. const uint32_t Mips_output_data_plt<size, big_endian>::
  5487. plt_entry_micromips32_o32[] =
  5488. {
  5489. 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
  5490. 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
  5491. 0x0019, 0x0f3c, // jr $25
  5492. 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
  5493. };
  5494. // Add an entry to the PLT for a symbol referenced by r_type relocation.
  5495. template<int size, bool big_endian>
  5496. void
  5497. Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
  5498. unsigned int r_type)
  5499. {
  5500. gold_assert(!gsym->has_plt_offset());
  5501. // Final PLT offset for a symbol will be set in method set_plt_offsets().
  5502. gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
  5503. + sizeof(plt0_entry_o32));
  5504. this->symbols_.push_back(gsym);
  5505. // Record whether the relocation requires a standard MIPS
  5506. // or a compressed code entry.
  5507. if (jal_reloc(r_type))
  5508. {
  5509. if (r_type == elfcpp::R_MIPS_26)
  5510. gsym->set_needs_mips_plt(true);
  5511. else
  5512. gsym->set_needs_comp_plt(true);
  5513. }
  5514. section_offset_type got_offset = this->got_plt_->current_data_size();
  5515. // Every PLT entry needs a GOT entry which points back to the PLT
  5516. // entry (this will be changed by the dynamic linker, normally
  5517. // lazily when the function is called).
  5518. this->got_plt_->set_current_data_size(got_offset + size/8);
  5519. gsym->set_needs_dynsym_entry();
  5520. this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
  5521. got_offset);
  5522. }
  5523. // Set final PLT offsets. For each symbol, determine whether standard or
  5524. // compressed (MIPS16 or microMIPS) PLT entry is used.
  5525. template<int size, bool big_endian>
  5526. void
  5527. Mips_output_data_plt<size, big_endian>::set_plt_offsets()
  5528. {
  5529. // The sizes of individual PLT entries.
  5530. unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
  5531. unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
  5532. ? this->compressed_plt_entry_size() : 0);
  5533. for (typename std::vector<Mips_symbol<size>*>::const_iterator
  5534. p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
  5535. {
  5536. Mips_symbol<size>* mips_sym = *p;
  5537. // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
  5538. // so always use a standard entry there.
  5539. //
  5540. // If the symbol has a MIPS16 call stub and gets a PLT entry, then
  5541. // all MIPS16 calls will go via that stub, and there is no benefit
  5542. // to having a MIPS16 entry. And in the case of call_stub a
  5543. // standard entry actually has to be used as the stub ends with a J
  5544. // instruction.
  5545. if (this->target_->is_output_newabi()
  5546. || mips_sym->has_mips16_call_stub()
  5547. || mips_sym->has_mips16_call_fp_stub())
  5548. {
  5549. mips_sym->set_needs_mips_plt(true);
  5550. mips_sym->set_needs_comp_plt(false);
  5551. }
  5552. // Otherwise, if there are no direct calls to the function, we
  5553. // have a free choice of whether to use standard or compressed
  5554. // entries. Prefer microMIPS entries if the object is known to
  5555. // contain microMIPS code, so that it becomes possible to create
  5556. // pure microMIPS binaries. Prefer standard entries otherwise,
  5557. // because MIPS16 ones are no smaller and are usually slower.
  5558. if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
  5559. {
  5560. if (this->target_->is_output_micromips())
  5561. mips_sym->set_needs_comp_plt(true);
  5562. else
  5563. mips_sym->set_needs_mips_plt(true);
  5564. }
  5565. if (mips_sym->needs_mips_plt())
  5566. {
  5567. mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
  5568. this->plt_mips_offset_ += plt_mips_entry_size;
  5569. }
  5570. if (mips_sym->needs_comp_plt())
  5571. {
  5572. mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
  5573. this->plt_comp_offset_ += plt_comp_entry_size;
  5574. }
  5575. }
  5576. // Figure out the size of the PLT header if we know that we are using it.
  5577. if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
  5578. this->plt_header_size_ = this->get_plt_header_size();
  5579. }
  5580. // Write out the PLT. This uses the hand-coded instructions above,
  5581. // and adjusts them as needed.
  5582. template<int size, bool big_endian>
  5583. void
  5584. Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
  5585. {
  5586. const off_t offset = this->offset();
  5587. const section_size_type oview_size =
  5588. convert_to_section_size_type(this->data_size());
  5589. unsigned char* const oview = of->get_output_view(offset, oview_size);
  5590. const off_t gotplt_file_offset = this->got_plt_->offset();
  5591. const section_size_type gotplt_size =
  5592. convert_to_section_size_type(this->got_plt_->data_size());
  5593. unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
  5594. gotplt_size);
  5595. unsigned char* pov = oview;
  5596. Mips_address plt_address = this->address();
  5597. // Calculate the address of .got.plt.
  5598. Mips_address gotplt_addr = this->got_plt_->address();
  5599. Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
  5600. Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
  5601. // The PLT sequence is not safe for N64 if .got.plt's address can
  5602. // not be loaded in two instructions.
  5603. gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
  5604. || ~(gotplt_addr | 0x7fffffff) == 0);
  5605. // Write the PLT header.
  5606. const uint32_t* plt0_entry = this->get_plt_header_entry();
  5607. if (plt0_entry == plt0_entry_micromips_o32)
  5608. {
  5609. // Write microMIPS PLT header.
  5610. gold_assert(gotplt_addr % 4 == 0);
  5611. Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
  5612. // ADDIUPC has a span of +/-16MB, check we're in range.
  5613. if (gotpc_offset + 0x1000000 >= 0x2000000)
  5614. {
  5615. gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
  5616. "ADDIUPC"), (long)gotpc_offset);
  5617. return;
  5618. }
  5619. elfcpp::Swap<16, big_endian>::writeval(pov,
  5620. plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
  5621. elfcpp::Swap<16, big_endian>::writeval(pov + 2,
  5622. (gotpc_offset >> 2) & 0xffff);
  5623. pov += 4;
  5624. for (unsigned int i = 2;
  5625. i < (sizeof(plt0_entry_micromips_o32)
  5626. / sizeof(plt0_entry_micromips_o32[0]));
  5627. i++)
  5628. {
  5629. elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
  5630. pov += 2;
  5631. }
  5632. }
  5633. else if (plt0_entry == plt0_entry_micromips32_o32)
  5634. {
  5635. // Write microMIPS PLT header in insn32 mode.
  5636. elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
  5637. elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
  5638. elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
  5639. elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
  5640. elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
  5641. elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
  5642. pov += 12;
  5643. for (unsigned int i = 6;
  5644. i < (sizeof(plt0_entry_micromips32_o32)
  5645. / sizeof(plt0_entry_micromips32_o32[0]));
  5646. i++)
  5647. {
  5648. elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
  5649. pov += 2;
  5650. }
  5651. }
  5652. else
  5653. {
  5654. // Write standard PLT header.
  5655. elfcpp::Swap<32, big_endian>::writeval(pov,
  5656. plt0_entry[0] | gotplt_addr_high);
  5657. elfcpp::Swap<32, big_endian>::writeval(pov + 4,
  5658. plt0_entry[1] | gotplt_addr_low);
  5659. elfcpp::Swap<32, big_endian>::writeval(pov + 8,
  5660. plt0_entry[2] | gotplt_addr_low);
  5661. pov += 12;
  5662. for (int i = 3; i < 8; i++)
  5663. {
  5664. elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
  5665. pov += 4;
  5666. }
  5667. }
  5668. unsigned char* gotplt_pov = gotplt_view;
  5669. unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
  5670. // The first two entries in .got.plt are reserved.
  5671. elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
  5672. elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
  5673. unsigned int gotplt_offset = 2 * got_entry_size;
  5674. gotplt_pov += 2 * got_entry_size;
  5675. // Calculate the address of the PLT header.
  5676. Mips_address header_address = (plt_address
  5677. + (this->is_plt_header_compressed() ? 1 : 0));
  5678. // Initialize compressed PLT area view.
  5679. unsigned char* pov2 = pov + this->plt_mips_offset_;
  5680. // Write the PLT entries.
  5681. for (typename std::vector<Mips_symbol<size>*>::const_iterator
  5682. p = this->symbols_.begin();
  5683. p != this->symbols_.end();
  5684. ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
  5685. {
  5686. Mips_symbol<size>* mips_sym = *p;
  5687. // Calculate the address of the .got.plt entry.
  5688. uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
  5689. uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
  5690. & 0xffff);
  5691. uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
  5692. // Initially point the .got.plt entry at the PLT header.
  5693. if (this->target_->is_output_n64())
  5694. elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
  5695. else
  5696. elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
  5697. // Now handle the PLT itself. First the standard entry.
  5698. if (mips_sym->has_mips_plt_offset())
  5699. {
  5700. // Pick the load opcode (LW or LD).
  5701. uint64_t load = this->target_->is_output_n64() ? 0xdc000000
  5702. : 0x8c000000;
  5703. // Fill in the PLT entry itself.
  5704. elfcpp::Swap<32, big_endian>::writeval(pov,
  5705. plt_entry[0] | gotplt_entry_addr_hi);
  5706. elfcpp::Swap<32, big_endian>::writeval(pov + 4,
  5707. plt_entry[1] | gotplt_entry_addr_lo | load);
  5708. elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_entry[2]);
  5709. elfcpp::Swap<32, big_endian>::writeval(pov + 12,
  5710. plt_entry[3] | gotplt_entry_addr_lo);
  5711. pov += 16;
  5712. }
  5713. // Now the compressed entry. They come after any standard ones.
  5714. if (mips_sym->has_comp_plt_offset())
  5715. {
  5716. if (!this->target_->is_output_micromips())
  5717. {
  5718. // Write MIPS16 PLT entry.
  5719. const uint32_t* plt_entry = plt_entry_mips16_o32;
  5720. elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
  5721. elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
  5722. elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
  5723. elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
  5724. elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
  5725. elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
  5726. elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
  5727. gotplt_entry_addr);
  5728. pov2 += 16;
  5729. }
  5730. else if (this->target_->use_32bit_micromips_instructions())
  5731. {
  5732. // Write microMIPS PLT entry in insn32 mode.
  5733. const uint32_t* plt_entry = plt_entry_micromips32_o32;
  5734. elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
  5735. elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
  5736. gotplt_entry_addr_hi);
  5737. elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
  5738. elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
  5739. gotplt_entry_addr_lo);
  5740. elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
  5741. elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
  5742. elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
  5743. elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
  5744. gotplt_entry_addr_lo);
  5745. pov2 += 16;
  5746. }
  5747. else
  5748. {
  5749. // Write microMIPS PLT entry.
  5750. const uint32_t* plt_entry = plt_entry_micromips_o32;
  5751. gold_assert(gotplt_entry_addr % 4 == 0);
  5752. Mips_address loc_address = plt_address + pov2 - oview;
  5753. int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
  5754. // ADDIUPC has a span of +/-16MB, check we're in range.
  5755. if (gotpc_offset + 0x1000000 >= 0x2000000)
  5756. {
  5757. gold_error(_(".got.plt offset of %ld from .plt beyond the "
  5758. "range of ADDIUPC"), (long)gotpc_offset);
  5759. return;
  5760. }
  5761. elfcpp::Swap<16, big_endian>::writeval(pov2,
  5762. plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
  5763. elfcpp::Swap<16, big_endian>::writeval(
  5764. pov2 + 2, (gotpc_offset >> 2) & 0xffff);
  5765. elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
  5766. elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
  5767. elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
  5768. elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
  5769. pov2 += 12;
  5770. }
  5771. }
  5772. }
  5773. // Check the number of bytes written for standard entries.
  5774. gold_assert(static_cast<section_size_type>(
  5775. pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
  5776. // Check the number of bytes written for compressed entries.
  5777. gold_assert((static_cast<section_size_type>(pov2 - pov)
  5778. == this->plt_comp_offset_));
  5779. // Check the total number of bytes written.
  5780. gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
  5781. gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
  5782. == gotplt_size);
  5783. of->write_output_view(offset, oview_size, oview);
  5784. of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
  5785. }
  5786. // Mips_output_data_mips_stubs methods.
  5787. // The format of the lazy binding stub when dynamic symbol count is less than
  5788. // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
  5789. template<int size, bool big_endian>
  5790. const uint32_t
  5791. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
  5792. {
  5793. 0x8f998010, // lw t9,0x8010(gp)
  5794. 0x03e07825, // or t7,ra,zero
  5795. 0x0320f809, // jalr t9,ra
  5796. 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
  5797. };
  5798. // The format of the lazy binding stub when dynamic symbol count is less than
  5799. // 64K, dynamic symbol index is less than 32K, and ABI is N64.
  5800. template<int size, bool big_endian>
  5801. const uint32_t
  5802. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
  5803. {
  5804. 0xdf998010, // ld t9,0x8010(gp)
  5805. 0x03e07825, // or t7,ra,zero
  5806. 0x0320f809, // jalr t9,ra
  5807. 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
  5808. };
  5809. // The format of the lazy binding stub when dynamic symbol count is less than
  5810. // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
  5811. template<int size, bool big_endian>
  5812. const uint32_t
  5813. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
  5814. {
  5815. 0x8f998010, // lw t9,0x8010(gp)
  5816. 0x03e07825, // or t7,ra,zero
  5817. 0x0320f809, // jalr t9,ra
  5818. 0x34180000 // ori t8,zero,DYN_INDEX unsigned
  5819. };
  5820. // The format of the lazy binding stub when dynamic symbol count is less than
  5821. // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
  5822. template<int size, bool big_endian>
  5823. const uint32_t
  5824. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
  5825. {
  5826. 0xdf998010, // ld t9,0x8010(gp)
  5827. 0x03e07825, // or t7,ra,zero
  5828. 0x0320f809, // jalr t9,ra
  5829. 0x34180000 // ori t8,zero,DYN_INDEX unsigned
  5830. };
  5831. // The format of the lazy binding stub when dynamic symbol count is greater than
  5832. // 64K, and ABI is not N64.
  5833. template<int size, bool big_endian>
  5834. const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
  5835. {
  5836. 0x8f998010, // lw t9,0x8010(gp)
  5837. 0x03e07825, // or t7,ra,zero
  5838. 0x3c180000, // lui t8,DYN_INDEX
  5839. 0x0320f809, // jalr t9,ra
  5840. 0x37180000 // ori t8,t8,DYN_INDEX
  5841. };
  5842. // The format of the lazy binding stub when dynamic symbol count is greater than
  5843. // 64K, and ABI is N64.
  5844. template<int size, bool big_endian>
  5845. const uint32_t
  5846. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
  5847. {
  5848. 0xdf998010, // ld t9,0x8010(gp)
  5849. 0x03e07825, // or t7,ra,zero
  5850. 0x3c180000, // lui t8,DYN_INDEX
  5851. 0x0320f809, // jalr t9,ra
  5852. 0x37180000 // ori t8,t8,DYN_INDEX
  5853. };
  5854. // microMIPS stubs.
  5855. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  5856. // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
  5857. template<int size, bool big_endian>
  5858. const uint32_t
  5859. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
  5860. {
  5861. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  5862. 0x0dff, // move t7,ra
  5863. 0x45d9, // jalr t9
  5864. 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
  5865. };
  5866. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  5867. // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
  5868. template<int size, bool big_endian>
  5869. const uint32_t
  5870. Mips_output_data_mips_stubs<size, big_endian>::
  5871. lazy_stub_micromips_normal_1_n64[] =
  5872. {
  5873. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  5874. 0x0dff, // move t7,ra
  5875. 0x45d9, // jalr t9
  5876. 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
  5877. };
  5878. // The format of the microMIPS lazy binding stub when dynamic symbol
  5879. // count is less than 64K, dynamic symbol index is between 32K and 64K,
  5880. // and ABI is not N64.
  5881. template<int size, bool big_endian>
  5882. const uint32_t
  5883. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
  5884. {
  5885. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  5886. 0x0dff, // move t7,ra
  5887. 0x45d9, // jalr t9
  5888. 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
  5889. };
  5890. // The format of the microMIPS lazy binding stub when dynamic symbol
  5891. // count is less than 64K, dynamic symbol index is between 32K and 64K,
  5892. // and ABI is N64.
  5893. template<int size, bool big_endian>
  5894. const uint32_t
  5895. Mips_output_data_mips_stubs<size, big_endian>::
  5896. lazy_stub_micromips_normal_2_n64[] =
  5897. {
  5898. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  5899. 0x0dff, // move t7,ra
  5900. 0x45d9, // jalr t9
  5901. 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
  5902. };
  5903. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  5904. // greater than 64K, and ABI is not N64.
  5905. template<int size, bool big_endian>
  5906. const uint32_t
  5907. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
  5908. {
  5909. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  5910. 0x0dff, // move t7,ra
  5911. 0x41b8, 0x0000, // lui t8,DYN_INDEX
  5912. 0x45d9, // jalr t9
  5913. 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
  5914. };
  5915. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  5916. // greater than 64K, and ABI is N64.
  5917. template<int size, bool big_endian>
  5918. const uint32_t
  5919. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
  5920. {
  5921. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  5922. 0x0dff, // move t7,ra
  5923. 0x41b8, 0x0000, // lui t8,DYN_INDEX
  5924. 0x45d9, // jalr t9
  5925. 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
  5926. };
  5927. // 32-bit microMIPS stubs.
  5928. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  5929. // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
  5930. // can use only 32-bit instructions.
  5931. template<int size, bool big_endian>
  5932. const uint32_t
  5933. Mips_output_data_mips_stubs<size, big_endian>::
  5934. lazy_stub_micromips32_normal_1[] =
  5935. {
  5936. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  5937. 0x001f, 0x7a90, // or t7,ra,zero
  5938. 0x03f9, 0x0f3c, // jalr ra,t9
  5939. 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
  5940. };
  5941. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  5942. // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
  5943. // use only 32-bit instructions.
  5944. template<int size, bool big_endian>
  5945. const uint32_t
  5946. Mips_output_data_mips_stubs<size, big_endian>::
  5947. lazy_stub_micromips32_normal_1_n64[] =
  5948. {
  5949. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  5950. 0x001f, 0x7a90, // or t7,ra,zero
  5951. 0x03f9, 0x0f3c, // jalr ra,t9
  5952. 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
  5953. };
  5954. // The format of the microMIPS lazy binding stub when dynamic symbol
  5955. // count is less than 64K, dynamic symbol index is between 32K and 64K,
  5956. // ABI is not N64, and we can use only 32-bit instructions.
  5957. template<int size, bool big_endian>
  5958. const uint32_t
  5959. Mips_output_data_mips_stubs<size, big_endian>::
  5960. lazy_stub_micromips32_normal_2[] =
  5961. {
  5962. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  5963. 0x001f, 0x7a90, // or t7,ra,zero
  5964. 0x03f9, 0x0f3c, // jalr ra,t9
  5965. 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
  5966. };
  5967. // The format of the microMIPS lazy binding stub when dynamic symbol
  5968. // count is less than 64K, dynamic symbol index is between 32K and 64K,
  5969. // ABI is N64, and we can use only 32-bit instructions.
  5970. template<int size, bool big_endian>
  5971. const uint32_t
  5972. Mips_output_data_mips_stubs<size, big_endian>::
  5973. lazy_stub_micromips32_normal_2_n64[] =
  5974. {
  5975. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  5976. 0x001f, 0x7a90, // or t7,ra,zero
  5977. 0x03f9, 0x0f3c, // jalr ra,t9
  5978. 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
  5979. };
  5980. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  5981. // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
  5982. template<int size, bool big_endian>
  5983. const uint32_t
  5984. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
  5985. {
  5986. 0xff3c, 0x8010, // lw t9,0x8010(gp)
  5987. 0x001f, 0x7a90, // or t7,ra,zero
  5988. 0x41b8, 0x0000, // lui t8,DYN_INDEX
  5989. 0x03f9, 0x0f3c, // jalr ra,t9
  5990. 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
  5991. };
  5992. // The format of the microMIPS lazy binding stub when dynamic symbol count is
  5993. // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
  5994. template<int size, bool big_endian>
  5995. const uint32_t
  5996. Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
  5997. {
  5998. 0xdf3c, 0x8010, // ld t9,0x8010(gp)
  5999. 0x001f, 0x7a90, // or t7,ra,zero
  6000. 0x41b8, 0x0000, // lui t8,DYN_INDEX
  6001. 0x03f9, 0x0f3c, // jalr ra,t9
  6002. 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
  6003. };
  6004. // Create entry for a symbol.
  6005. template<int size, bool big_endian>
  6006. void
  6007. Mips_output_data_mips_stubs<size, big_endian>::make_entry(
  6008. Mips_symbol<size>* gsym)
  6009. {
  6010. if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
  6011. {
  6012. this->symbols_.insert(gsym);
  6013. gsym->set_has_lazy_stub(true);
  6014. }
  6015. }
  6016. // Remove entry for a symbol.
  6017. template<int size, bool big_endian>
  6018. void
  6019. Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
  6020. Mips_symbol<size>* gsym)
  6021. {
  6022. if (gsym->has_lazy_stub())
  6023. {
  6024. this->symbols_.erase(gsym);
  6025. gsym->set_has_lazy_stub(false);
  6026. }
  6027. }
  6028. // Set stub offsets for symbols. This method expects that the number of
  6029. // entries in dynamic symbol table is set.
  6030. template<int size, bool big_endian>
  6031. void
  6032. Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
  6033. {
  6034. gold_assert(this->dynsym_count_ != -1U);
  6035. if (this->stub_offsets_are_set_)
  6036. return;
  6037. unsigned int stub_size = this->stub_size();
  6038. unsigned int offset = 0;
  6039. for (typename Unordered_set<Mips_symbol<size>*>::const_iterator
  6040. p = this->symbols_.begin();
  6041. p != this->symbols_.end();
  6042. ++p, offset += stub_size)
  6043. {
  6044. Mips_symbol<size>* mips_sym = *p;
  6045. mips_sym->set_lazy_stub_offset(offset);
  6046. }
  6047. this->stub_offsets_are_set_ = true;
  6048. }
  6049. template<int size, bool big_endian>
  6050. void
  6051. Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
  6052. {
  6053. for (typename Unordered_set<Mips_symbol<size>*>::const_iterator
  6054. p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
  6055. {
  6056. Mips_symbol<size>* sym = *p;
  6057. if (sym->is_from_dynobj())
  6058. sym->set_needs_dynsym_value();
  6059. }
  6060. }
  6061. // Write out the .MIPS.stubs. This uses the hand-coded instructions and
  6062. // adjusts them as needed.
  6063. template<int size, bool big_endian>
  6064. void
  6065. Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
  6066. {
  6067. const off_t offset = this->offset();
  6068. const section_size_type oview_size =
  6069. convert_to_section_size_type(this->data_size());
  6070. unsigned char* const oview = of->get_output_view(offset, oview_size);
  6071. bool big_stub = this->dynsym_count_ > 0x10000;
  6072. unsigned char* pov = oview;
  6073. for (typename Unordered_set<Mips_symbol<size>*>::const_iterator
  6074. p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
  6075. {
  6076. Mips_symbol<size>* sym = *p;
  6077. const uint32_t* lazy_stub;
  6078. bool n64 = this->target_->is_output_n64();
  6079. if (!this->target_->is_output_micromips())
  6080. {
  6081. // Write standard (non-microMIPS) stub.
  6082. if (!big_stub)
  6083. {
  6084. if (sym->dynsym_index() & ~0x7fff)
  6085. // Dynsym index is between 32K and 64K.
  6086. lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
  6087. else
  6088. // Dynsym index is less than 32K.
  6089. lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
  6090. }
  6091. else
  6092. lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
  6093. unsigned int i = 0;
  6094. elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
  6095. elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
  6096. pov += 8;
  6097. i += 2;
  6098. if (big_stub)
  6099. {
  6100. // LUI instruction of the big stub. Paste high 16 bits of the
  6101. // dynsym index.
  6102. elfcpp::Swap<32, big_endian>::writeval(pov,
  6103. lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
  6104. pov += 4;
  6105. i += 1;
  6106. }
  6107. elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
  6108. // Last stub instruction. Paste low 16 bits of the dynsym index.
  6109. elfcpp::Swap<32, big_endian>::writeval(pov + 4,
  6110. lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
  6111. pov += 8;
  6112. }
  6113. else if (this->target_->use_32bit_micromips_instructions())
  6114. {
  6115. // Write microMIPS stub in insn32 mode.
  6116. if (!big_stub)
  6117. {
  6118. if (sym->dynsym_index() & ~0x7fff)
  6119. // Dynsym index is between 32K and 64K.
  6120. lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
  6121. : lazy_stub_micromips32_normal_2;
  6122. else
  6123. // Dynsym index is less than 32K.
  6124. lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
  6125. : lazy_stub_micromips32_normal_1;
  6126. }
  6127. else
  6128. lazy_stub = n64 ? lazy_stub_micromips32_big_n64
  6129. : lazy_stub_micromips32_big;
  6130. unsigned int i = 0;
  6131. // First stub instruction. We emit 32-bit microMIPS instructions by
  6132. // emitting two 16-bit parts because on microMIPS the 16-bit part of
  6133. // the instruction where the opcode is must always come first, for
  6134. // both little and big endian.
  6135. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  6136. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
  6137. // Second stub instruction.
  6138. elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
  6139. elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
  6140. pov += 8;
  6141. i += 4;
  6142. if (big_stub)
  6143. {
  6144. // LUI instruction of the big stub. Paste high 16 bits of the
  6145. // dynsym index.
  6146. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  6147. elfcpp::Swap<16, big_endian>::writeval(pov + 2,
  6148. (sym->dynsym_index() >> 16) & 0x7fff);
  6149. pov += 4;
  6150. i += 2;
  6151. }
  6152. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  6153. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
  6154. // Last stub instruction. Paste low 16 bits of the dynsym index.
  6155. elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
  6156. elfcpp::Swap<16, big_endian>::writeval(pov + 6,
  6157. sym->dynsym_index() & 0xffff);
  6158. pov += 8;
  6159. }
  6160. else
  6161. {
  6162. // Write microMIPS stub.
  6163. if (!big_stub)
  6164. {
  6165. if (sym->dynsym_index() & ~0x7fff)
  6166. // Dynsym index is between 32K and 64K.
  6167. lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
  6168. : lazy_stub_micromips_normal_2;
  6169. else
  6170. // Dynsym index is less than 32K.
  6171. lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
  6172. : lazy_stub_micromips_normal_1;
  6173. }
  6174. else
  6175. lazy_stub = n64 ? lazy_stub_micromips_big_n64
  6176. : lazy_stub_micromips_big;
  6177. unsigned int i = 0;
  6178. // First stub instruction. We emit 32-bit microMIPS instructions by
  6179. // emitting two 16-bit parts because on microMIPS the 16-bit part of
  6180. // the instruction where the opcode is must always come first, for
  6181. // both little and big endian.
  6182. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  6183. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
  6184. // Second stub instruction.
  6185. elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
  6186. pov += 6;
  6187. i += 3;
  6188. if (big_stub)
  6189. {
  6190. // LUI instruction of the big stub. Paste high 16 bits of the
  6191. // dynsym index.
  6192. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  6193. elfcpp::Swap<16, big_endian>::writeval(pov + 2,
  6194. (sym->dynsym_index() >> 16) & 0x7fff);
  6195. pov += 4;
  6196. i += 2;
  6197. }
  6198. elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
  6199. // Last stub instruction. Paste low 16 bits of the dynsym index.
  6200. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
  6201. elfcpp::Swap<16, big_endian>::writeval(pov + 4,
  6202. sym->dynsym_index() & 0xffff);
  6203. pov += 6;
  6204. }
  6205. }
  6206. // We always allocate 20 bytes for every stub, because final dynsym count is
  6207. // not known in method do_finalize_sections. There are 4 unused bytes per
  6208. // stub if final dynsym count is less than 0x10000.
  6209. unsigned int used = pov - oview;
  6210. unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
  6211. gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
  6212. // Fill the unused space with zeroes.
  6213. // TODO(sasa): Can we strip unused bytes during the relaxation?
  6214. if (unused > 0)
  6215. memset(pov, 0, unused);
  6216. of->write_output_view(offset, oview_size, oview);
  6217. }
  6218. // Mips_output_section_reginfo methods.
  6219. template<int size, bool big_endian>
  6220. void
  6221. Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
  6222. {
  6223. off_t offset = this->offset();
  6224. off_t data_size = this->data_size();
  6225. unsigned char* view = of->get_output_view(offset, data_size);
  6226. elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
  6227. elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
  6228. elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
  6229. elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
  6230. elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
  6231. // Write the gp value.
  6232. elfcpp::Swap<size, big_endian>::writeval(view + 20,
  6233. this->target_->gp_value());
  6234. of->write_output_view(offset, data_size, view);
  6235. }
  6236. // Mips_copy_relocs methods.
  6237. // Emit any saved relocs.
  6238. template<int sh_type, int size, bool big_endian>
  6239. void
  6240. Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
  6241. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
  6242. Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
  6243. {
  6244. for (typename Copy_relocs<sh_type, size, big_endian>::
  6245. Copy_reloc_entries::iterator p = this->entries_.begin();
  6246. p != this->entries_.end();
  6247. ++p)
  6248. emit_entry(*p, reloc_section, symtab, layout, target);
  6249. // We no longer need the saved information.
  6250. this->entries_.clear();
  6251. }
  6252. // Emit the reloc if appropriate.
  6253. template<int sh_type, int size, bool big_endian>
  6254. void
  6255. Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
  6256. Copy_reloc_entry& entry,
  6257. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
  6258. Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
  6259. {
  6260. // If the symbol is no longer defined in a dynamic object, then we
  6261. // emitted a COPY relocation, and we do not want to emit this
  6262. // dynamic relocation.
  6263. if (!entry.sym_->is_from_dynobj())
  6264. return;
  6265. bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
  6266. || entry.reloc_type_ == elfcpp::R_MIPS_REL32
  6267. || entry.reloc_type_ == elfcpp::R_MIPS_64);
  6268. Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
  6269. if (can_make_dynamic && !sym->has_static_relocs())
  6270. {
  6271. Mips_relobj<size, big_endian>* object =
  6272. Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
  6273. target->got_section(symtab, layout)->record_global_got_symbol(
  6274. sym, object, entry.reloc_type_, true, false);
  6275. if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
  6276. target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
  6277. entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
  6278. else
  6279. target->rel_dyn_section(layout)->add_symbolless_global_addend(
  6280. sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
  6281. entry.shndx_, entry.address_);
  6282. }
  6283. else
  6284. this->make_copy_reloc(symtab, layout,
  6285. static_cast<Sized_symbol<size>*>(entry.sym_),
  6286. reloc_section);
  6287. }
  6288. // Target_mips methods.
  6289. // Return the value to use for a dynamic symbol which requires special
  6290. // treatment. This is how we support equality comparisons of function
  6291. // pointers across shared library boundaries, as described in the
  6292. // processor specific ABI supplement.
  6293. template<int size, bool big_endian>
  6294. uint64_t
  6295. Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
  6296. {
  6297. uint64_t value = 0;
  6298. const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
  6299. if (!mips_sym->has_lazy_stub())
  6300. {
  6301. if (mips_sym->has_plt_offset())
  6302. {
  6303. // We distinguish between PLT entries and lazy-binding stubs by
  6304. // giving the former an st_other value of STO_MIPS_PLT. Set the
  6305. // value to the stub address if there are any relocations in the
  6306. // binary where pointer equality matters.
  6307. if (mips_sym->pointer_equality_needed())
  6308. {
  6309. // Prefer a standard MIPS PLT entry.
  6310. if (mips_sym->has_mips_plt_offset())
  6311. value = this->plt_section()->mips_entry_address(mips_sym);
  6312. else
  6313. value = this->plt_section()->comp_entry_address(mips_sym) + 1;
  6314. }
  6315. else
  6316. value = 0;
  6317. }
  6318. }
  6319. else
  6320. {
  6321. // First, set stub offsets for symbols. This method expects that the
  6322. // number of entries in dynamic symbol table is set.
  6323. this->mips_stubs_section()->set_lazy_stub_offsets();
  6324. // The run-time linker uses the st_value field of the symbol
  6325. // to reset the global offset table entry for this external
  6326. // to its stub address when unlinking a shared object.
  6327. value = this->mips_stubs_section()->stub_address(mips_sym);
  6328. }
  6329. if (mips_sym->has_mips16_fn_stub())
  6330. {
  6331. // If we have a MIPS16 function with a stub, the dynamic symbol must
  6332. // refer to the stub, since only the stub uses the standard calling
  6333. // conventions.
  6334. value = mips_sym->template
  6335. get_mips16_fn_stub<big_endian>()->output_address();
  6336. }
  6337. return value;
  6338. }
  6339. // Get the dynamic reloc section, creating it if necessary. It's always
  6340. // .rel.dyn, even for MIPS64.
  6341. template<int size, bool big_endian>
  6342. typename Target_mips<size, big_endian>::Reloc_section*
  6343. Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
  6344. {
  6345. if (this->rel_dyn_ == NULL)
  6346. {
  6347. gold_assert(layout != NULL);
  6348. this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
  6349. layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
  6350. elfcpp::SHF_ALLOC, this->rel_dyn_,
  6351. ORDER_DYNAMIC_RELOCS, false);
  6352. // First entry in .rel.dyn has to be null.
  6353. // This is hack - we define dummy output data and set its address to 0,
  6354. // and define absolute R_MIPS_NONE relocation with offset 0 against it.
  6355. // This ensures that the entry is null.
  6356. Output_data* od = new Output_data_zero_fill(0, 0);
  6357. od->set_address(0);
  6358. this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
  6359. }
  6360. return this->rel_dyn_;
  6361. }
  6362. // Get the GOT section, creating it if necessary.
  6363. template<int size, bool big_endian>
  6364. Mips_output_data_got<size, big_endian>*
  6365. Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
  6366. Layout* layout)
  6367. {
  6368. if (this->got_ == NULL)
  6369. {
  6370. gold_assert(symtab != NULL && layout != NULL);
  6371. this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
  6372. layout);
  6373. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  6374. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
  6375. elfcpp::SHF_MIPS_GPREL),
  6376. this->got_, ORDER_DATA, false);
  6377. // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
  6378. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  6379. Symbol_table::PREDEFINED,
  6380. this->got_,
  6381. 0, 0, elfcpp::STT_OBJECT,
  6382. elfcpp::STB_GLOBAL,
  6383. elfcpp::STV_DEFAULT, 0,
  6384. false, false);
  6385. }
  6386. return this->got_;
  6387. }
  6388. // Calculate value of _gp symbol.
  6389. template<int size, bool big_endian>
  6390. void
  6391. Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
  6392. {
  6393. if (this->gp_ != NULL)
  6394. return;
  6395. Output_data* section = layout->find_output_section(".got");
  6396. if (section == NULL)
  6397. {
  6398. // If there is no .got section, gp should be based on .sdata.
  6399. // TODO(sasa): This is probably not needed. This was needed for older
  6400. // MIPS architectures which accessed both GOT and .sdata section using
  6401. // gp-relative addressing. Modern Mips Linux ELF architectures don't
  6402. // access .sdata using gp-relative addressing.
  6403. for (Layout::Section_list::const_iterator
  6404. p = layout->section_list().begin();
  6405. p != layout->section_list().end();
  6406. ++p)
  6407. {
  6408. if (strcmp((*p)->name(), ".sdata") == 0)
  6409. {
  6410. section = *p;
  6411. break;
  6412. }
  6413. }
  6414. }
  6415. Sized_symbol<size>* gp =
  6416. static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
  6417. if (gp != NULL)
  6418. {
  6419. if (gp->source() != Symbol::IS_CONSTANT && section != NULL)
  6420. gp->init_output_data(gp->name(), NULL, section, MIPS_GP_OFFSET, 0,
  6421. elfcpp::STT_OBJECT,
  6422. elfcpp::STB_GLOBAL,
  6423. elfcpp::STV_DEFAULT, 0,
  6424. false, false);
  6425. this->gp_ = gp;
  6426. }
  6427. else if (section != NULL)
  6428. {
  6429. gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
  6430. "_gp", NULL, Symbol_table::PREDEFINED,
  6431. section, MIPS_GP_OFFSET, 0,
  6432. elfcpp::STT_OBJECT,
  6433. elfcpp::STB_GLOBAL,
  6434. elfcpp::STV_DEFAULT,
  6435. 0, false, false));
  6436. this->gp_ = gp;
  6437. }
  6438. }
  6439. // Set the dynamic symbol indexes. INDEX is the index of the first
  6440. // global dynamic symbol. Pointers to the symbols are stored into the
  6441. // vector SYMS. The names are added to DYNPOOL. This returns an
  6442. // updated dynamic symbol index.
  6443. template<int size, bool big_endian>
  6444. unsigned int
  6445. Target_mips<size, big_endian>::do_set_dynsym_indexes(
  6446. std::vector<Symbol*>* dyn_symbols, unsigned int index,
  6447. std::vector<Symbol*>* syms, Stringpool* dynpool,
  6448. Versions* versions, Symbol_table* symtab) const
  6449. {
  6450. std::vector<Symbol*> non_got_symbols;
  6451. std::vector<Symbol*> got_symbols;
  6452. reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
  6453. &got_symbols);
  6454. for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
  6455. p != non_got_symbols.end();
  6456. ++p)
  6457. {
  6458. Symbol* sym = *p;
  6459. // Note that SYM may already have a dynamic symbol index, since
  6460. // some symbols appear more than once in the symbol table, with
  6461. // and without a version.
  6462. if (!sym->has_dynsym_index())
  6463. {
  6464. sym->set_dynsym_index(index);
  6465. ++index;
  6466. syms->push_back(sym);
  6467. dynpool->add(sym->name(), false, NULL);
  6468. // Record any version information.
  6469. if (sym->version() != NULL)
  6470. versions->record_version(symtab, dynpool, sym);
  6471. // If the symbol is defined in a dynamic object and is
  6472. // referenced in a regular object, then mark the dynamic
  6473. // object as needed. This is used to implement --as-needed.
  6474. if (sym->is_from_dynobj() && sym->in_reg())
  6475. sym->object()->set_is_needed();
  6476. }
  6477. }
  6478. for (std::vector<Symbol*>::iterator p = got_symbols.begin();
  6479. p != got_symbols.end();
  6480. ++p)
  6481. {
  6482. Symbol* sym = *p;
  6483. if (!sym->has_dynsym_index())
  6484. {
  6485. // Record any version information.
  6486. if (sym->version() != NULL)
  6487. versions->record_version(symtab, dynpool, sym);
  6488. }
  6489. }
  6490. index = versions->finalize(symtab, index, syms);
  6491. int got_sym_count = 0;
  6492. for (std::vector<Symbol*>::iterator p = got_symbols.begin();
  6493. p != got_symbols.end();
  6494. ++p)
  6495. {
  6496. Symbol* sym = *p;
  6497. if (!sym->has_dynsym_index())
  6498. {
  6499. ++got_sym_count;
  6500. sym->set_dynsym_index(index);
  6501. ++index;
  6502. syms->push_back(sym);
  6503. dynpool->add(sym->name(), false, NULL);
  6504. // If the symbol is defined in a dynamic object and is
  6505. // referenced in a regular object, then mark the dynamic
  6506. // object as needed. This is used to implement --as-needed.
  6507. if (sym->is_from_dynobj() && sym->in_reg())
  6508. sym->object()->set_is_needed();
  6509. }
  6510. }
  6511. // Set index of the first symbol that has .got entry.
  6512. this->got_->set_first_global_got_dynsym_index(
  6513. got_sym_count > 0 ? index - got_sym_count : -1U);
  6514. if (this->mips_stubs_ != NULL)
  6515. this->mips_stubs_->set_dynsym_count(index);
  6516. return index;
  6517. }
  6518. // Create a PLT entry for a global symbol referenced by r_type relocation.
  6519. template<int size, bool big_endian>
  6520. void
  6521. Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
  6522. Layout* layout,
  6523. Mips_symbol<size>* gsym,
  6524. unsigned int r_type)
  6525. {
  6526. if (gsym->has_lazy_stub() || gsym->has_plt_offset())
  6527. return;
  6528. if (this->plt_ == NULL)
  6529. {
  6530. // Create the GOT section first.
  6531. this->got_section(symtab, layout);
  6532. this->got_plt_ = new Output_data_space(4, "** GOT PLT");
  6533. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  6534. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
  6535. this->got_plt_, ORDER_DATA, false);
  6536. // The first two entries are reserved.
  6537. this->got_plt_->set_current_data_size(2 * size/8);
  6538. this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
  6539. this->got_plt_,
  6540. this);
  6541. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  6542. (elfcpp::SHF_ALLOC
  6543. | elfcpp::SHF_EXECINSTR),
  6544. this->plt_, ORDER_PLT, false);
  6545. }
  6546. this->plt_->add_entry(gsym, r_type);
  6547. }
  6548. // Get the .MIPS.stubs section, creating it if necessary.
  6549. template<int size, bool big_endian>
  6550. Mips_output_data_mips_stubs<size, big_endian>*
  6551. Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
  6552. {
  6553. if (this->mips_stubs_ == NULL)
  6554. {
  6555. this->mips_stubs_ =
  6556. new Mips_output_data_mips_stubs<size, big_endian>(this);
  6557. layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
  6558. (elfcpp::SHF_ALLOC
  6559. | elfcpp::SHF_EXECINSTR),
  6560. this->mips_stubs_, ORDER_PLT, false);
  6561. }
  6562. return this->mips_stubs_;
  6563. }
  6564. // Get the LA25 stub section, creating it if necessary.
  6565. template<int size, bool big_endian>
  6566. Mips_output_data_la25_stub<size, big_endian>*
  6567. Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
  6568. {
  6569. if (this->la25_stub_ == NULL)
  6570. {
  6571. this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
  6572. layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
  6573. (elfcpp::SHF_ALLOC
  6574. | elfcpp::SHF_EXECINSTR),
  6575. this->la25_stub_, ORDER_TEXT, false);
  6576. }
  6577. return this->la25_stub_;
  6578. }
  6579. // Process the relocations to determine unreferenced sections for
  6580. // garbage collection.
  6581. template<int size, bool big_endian>
  6582. void
  6583. Target_mips<size, big_endian>::gc_process_relocs(
  6584. Symbol_table* symtab,
  6585. Layout* layout,
  6586. Sized_relobj_file<size, big_endian>* object,
  6587. unsigned int data_shndx,
  6588. unsigned int,
  6589. const unsigned char* prelocs,
  6590. size_t reloc_count,
  6591. Output_section* output_section,
  6592. bool needs_special_offset_handling,
  6593. size_t local_symbol_count,
  6594. const unsigned char* plocal_symbols)
  6595. {
  6596. typedef Target_mips<size, big_endian> Mips;
  6597. typedef typename Target_mips<size, big_endian>::Scan Scan;
  6598. gold::gc_process_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan,
  6599. typename Target_mips::Relocatable_size_for_reloc>(
  6600. symtab,
  6601. layout,
  6602. this,
  6603. object,
  6604. data_shndx,
  6605. prelocs,
  6606. reloc_count,
  6607. output_section,
  6608. needs_special_offset_handling,
  6609. local_symbol_count,
  6610. plocal_symbols);
  6611. }
  6612. // Scan relocations for a section.
  6613. template<int size, bool big_endian>
  6614. void
  6615. Target_mips<size, big_endian>::scan_relocs(
  6616. Symbol_table* symtab,
  6617. Layout* layout,
  6618. Sized_relobj_file<size, big_endian>* object,
  6619. unsigned int data_shndx,
  6620. unsigned int sh_type,
  6621. const unsigned char* prelocs,
  6622. size_t reloc_count,
  6623. Output_section* output_section,
  6624. bool needs_special_offset_handling,
  6625. size_t local_symbol_count,
  6626. const unsigned char* plocal_symbols)
  6627. {
  6628. typedef Target_mips<size, big_endian> Mips;
  6629. typedef typename Target_mips<size, big_endian>::Scan Scan;
  6630. if (sh_type == elfcpp::SHT_REL)
  6631. gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan>(
  6632. symtab,
  6633. layout,
  6634. this,
  6635. object,
  6636. data_shndx,
  6637. prelocs,
  6638. reloc_count,
  6639. output_section,
  6640. needs_special_offset_handling,
  6641. local_symbol_count,
  6642. plocal_symbols);
  6643. else if (sh_type == elfcpp::SHT_RELA)
  6644. gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_RELA, Scan>(
  6645. symtab,
  6646. layout,
  6647. this,
  6648. object,
  6649. data_shndx,
  6650. prelocs,
  6651. reloc_count,
  6652. output_section,
  6653. needs_special_offset_handling,
  6654. local_symbol_count,
  6655. plocal_symbols);
  6656. }
  6657. template<int size, bool big_endian>
  6658. bool
  6659. Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
  6660. {
  6661. return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
  6662. || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
  6663. || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
  6664. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
  6665. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
  6666. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
  6667. || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2);
  6668. }
  6669. // Return the MACH for a MIPS e_flags value.
  6670. template<int size, bool big_endian>
  6671. unsigned int
  6672. Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
  6673. {
  6674. switch (flags & elfcpp::EF_MIPS_MACH)
  6675. {
  6676. case elfcpp::E_MIPS_MACH_3900:
  6677. return mach_mips3900;
  6678. case elfcpp::E_MIPS_MACH_4010:
  6679. return mach_mips4010;
  6680. case elfcpp::E_MIPS_MACH_4100:
  6681. return mach_mips4100;
  6682. case elfcpp::E_MIPS_MACH_4111:
  6683. return mach_mips4111;
  6684. case elfcpp::E_MIPS_MACH_4120:
  6685. return mach_mips4120;
  6686. case elfcpp::E_MIPS_MACH_4650:
  6687. return mach_mips4650;
  6688. case elfcpp::E_MIPS_MACH_5400:
  6689. return mach_mips5400;
  6690. case elfcpp::E_MIPS_MACH_5500:
  6691. return mach_mips5500;
  6692. case elfcpp::E_MIPS_MACH_9000:
  6693. return mach_mips9000;
  6694. case elfcpp::E_MIPS_MACH_SB1:
  6695. return mach_mips_sb1;
  6696. case elfcpp::E_MIPS_MACH_LS2E:
  6697. return mach_mips_loongson_2e;
  6698. case elfcpp::E_MIPS_MACH_LS2F:
  6699. return mach_mips_loongson_2f;
  6700. case elfcpp::E_MIPS_MACH_LS3A:
  6701. return mach_mips_loongson_3a;
  6702. case elfcpp::E_MIPS_MACH_OCTEON2:
  6703. return mach_mips_octeon2;
  6704. case elfcpp::E_MIPS_MACH_OCTEON:
  6705. return mach_mips_octeon;
  6706. case elfcpp::E_MIPS_MACH_XLR:
  6707. return mach_mips_xlr;
  6708. default:
  6709. switch (flags & elfcpp::EF_MIPS_ARCH)
  6710. {
  6711. default:
  6712. case elfcpp::E_MIPS_ARCH_1:
  6713. return mach_mips3000;
  6714. case elfcpp::E_MIPS_ARCH_2:
  6715. return mach_mips6000;
  6716. case elfcpp::E_MIPS_ARCH_3:
  6717. return mach_mips4000;
  6718. case elfcpp::E_MIPS_ARCH_4:
  6719. return mach_mips8000;
  6720. case elfcpp::E_MIPS_ARCH_5:
  6721. return mach_mips5;
  6722. case elfcpp::E_MIPS_ARCH_32:
  6723. return mach_mipsisa32;
  6724. case elfcpp::E_MIPS_ARCH_64:
  6725. return mach_mipsisa64;
  6726. case elfcpp::E_MIPS_ARCH_32R2:
  6727. return mach_mipsisa32r2;
  6728. case elfcpp::E_MIPS_ARCH_64R2:
  6729. return mach_mipsisa64r2;
  6730. }
  6731. }
  6732. return 0;
  6733. }
  6734. // Check whether machine EXTENSION is an extension of machine BASE.
  6735. template<int size, bool big_endian>
  6736. bool
  6737. Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
  6738. unsigned int extension)
  6739. {
  6740. if (extension == base)
  6741. return true;
  6742. if ((base == mach_mipsisa32)
  6743. && this->mips_mach_extends(mach_mipsisa64, extension))
  6744. return true;
  6745. if ((base == mach_mipsisa32r2)
  6746. && this->mips_mach_extends(mach_mipsisa64r2, extension))
  6747. return true;
  6748. for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
  6749. if (extension == this->mips_mach_extensions_[i].first)
  6750. {
  6751. extension = this->mips_mach_extensions_[i].second;
  6752. if (extension == base)
  6753. return true;
  6754. }
  6755. return false;
  6756. }
  6757. template<int size, bool big_endian>
  6758. void
  6759. Target_mips<size, big_endian>::merge_processor_specific_flags(
  6760. const std::string& name, elfcpp::Elf_Word in_flags,
  6761. unsigned char in_ei_class, bool dyn_obj)
  6762. {
  6763. // If flags are not set yet, just copy them.
  6764. if (!this->are_processor_specific_flags_set())
  6765. {
  6766. this->set_processor_specific_flags(in_flags);
  6767. this->ei_class_ = in_ei_class;
  6768. this->mach_ = this->elf_mips_mach(in_flags);
  6769. return;
  6770. }
  6771. elfcpp::Elf_Word new_flags = in_flags;
  6772. elfcpp::Elf_Word old_flags = this->processor_specific_flags();
  6773. elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
  6774. merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
  6775. // Check flag compatibility.
  6776. new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
  6777. old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
  6778. // Some IRIX 6 BSD-compatibility objects have this bit set. It
  6779. // doesn't seem to matter.
  6780. new_flags &= ~elfcpp::EF_MIPS_XGOT;
  6781. old_flags &= ~elfcpp::EF_MIPS_XGOT;
  6782. // MIPSpro generates ucode info in n64 objects. Again, we should
  6783. // just be able to ignore this.
  6784. new_flags &= ~elfcpp::EF_MIPS_UCODE;
  6785. old_flags &= ~elfcpp::EF_MIPS_UCODE;
  6786. // DSOs should only be linked with CPIC code.
  6787. if (dyn_obj)
  6788. new_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC;
  6789. if (new_flags == old_flags)
  6790. {
  6791. this->set_processor_specific_flags(merged_flags);
  6792. return;
  6793. }
  6794. if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
  6795. != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
  6796. gold_warning(_("%s: linking abicalls files with non-abicalls files"),
  6797. name.c_str());
  6798. if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
  6799. merged_flags |= elfcpp::EF_MIPS_CPIC;
  6800. if (!(new_flags & elfcpp::EF_MIPS_PIC))
  6801. merged_flags &= ~elfcpp::EF_MIPS_PIC;
  6802. new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
  6803. old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
  6804. // Compare the ISAs.
  6805. if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
  6806. gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
  6807. else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
  6808. {
  6809. // Output ISA isn't the same as, or an extension of, input ISA.
  6810. if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
  6811. {
  6812. // Copy the architecture info from input object to output. Also copy
  6813. // the 32-bit flag (if set) so that we continue to recognise
  6814. // output as a 32-bit binary.
  6815. this->mach_ = this->elf_mips_mach(in_flags);
  6816. merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
  6817. merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
  6818. | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
  6819. // Copy across the ABI flags if output doesn't use them
  6820. // and if that was what caused us to treat input object as 32-bit.
  6821. if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
  6822. && this->mips_32bit_flags(new_flags)
  6823. && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
  6824. merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
  6825. }
  6826. else
  6827. // The ISAs aren't compatible.
  6828. gold_error(_("%s: linking %s module with previous %s modules"),
  6829. name.c_str(), this->elf_mips_mach_name(in_flags),
  6830. this->elf_mips_mach_name(merged_flags));
  6831. }
  6832. new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
  6833. | elfcpp::EF_MIPS_32BITMODE));
  6834. old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
  6835. | elfcpp::EF_MIPS_32BITMODE));
  6836. // Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it does set
  6837. // EI_CLASS differently from any 32-bit ABI.
  6838. if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI)
  6839. || (in_ei_class != this->ei_class_))
  6840. {
  6841. // Only error if both are set (to different values).
  6842. if (((new_flags & elfcpp::EF_MIPS_ABI)
  6843. && (old_flags & elfcpp::EF_MIPS_ABI))
  6844. || (in_ei_class != this->ei_class_))
  6845. gold_error(_("%s: ABI mismatch: linking %s module with "
  6846. "previous %s modules"), name.c_str(),
  6847. this->elf_mips_abi_name(in_flags, in_ei_class),
  6848. this->elf_mips_abi_name(merged_flags, this->ei_class_));
  6849. new_flags &= ~elfcpp::EF_MIPS_ABI;
  6850. old_flags &= ~elfcpp::EF_MIPS_ABI;
  6851. }
  6852. // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
  6853. // and allow arbitrary mixing of the remaining ASEs (retain the union).
  6854. if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
  6855. != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
  6856. {
  6857. int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
  6858. int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
  6859. int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
  6860. int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
  6861. int micro_mis = old_m16 && new_micro;
  6862. int m16_mis = old_micro && new_m16;
  6863. if (m16_mis || micro_mis)
  6864. gold_error(_("%s: ASE mismatch: linking %s module with "
  6865. "previous %s modules"), name.c_str(),
  6866. m16_mis ? "MIPS16" : "microMIPS",
  6867. m16_mis ? "microMIPS" : "MIPS16");
  6868. merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
  6869. new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
  6870. old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
  6871. }
  6872. // Warn about any other mismatches.
  6873. if (new_flags != old_flags)
  6874. gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
  6875. "modules (0x%x)"), name.c_str(), new_flags, old_flags);
  6876. this->set_processor_specific_flags(merged_flags);
  6877. }
  6878. // Adjust ELF file header.
  6879. template<int size, bool big_endian>
  6880. void
  6881. Target_mips<size, big_endian>::do_adjust_elf_header(
  6882. unsigned char* view,
  6883. int len)
  6884. {
  6885. gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
  6886. elfcpp::Ehdr<size, big_endian> ehdr(view);
  6887. unsigned char e_ident[elfcpp::EI_NIDENT];
  6888. memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
  6889. e_ident[elfcpp::EI_CLASS] = this->ei_class_;
  6890. elfcpp::Ehdr_write<size, big_endian> oehdr(view);
  6891. oehdr.put_e_ident(e_ident);
  6892. if (this->entry_symbol_is_compressed_)
  6893. oehdr.put_e_entry(ehdr.get_e_entry() + 1);
  6894. }
  6895. // do_make_elf_object to override the same function in the base class.
  6896. // We need to use a target-specific sub-class of
  6897. // Sized_relobj_file<size, big_endian> to store Mips specific information.
  6898. // Hence we need to have our own ELF object creation.
  6899. template<int size, bool big_endian>
  6900. Object*
  6901. Target_mips<size, big_endian>::do_make_elf_object(
  6902. const std::string& name,
  6903. Input_file* input_file,
  6904. off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
  6905. {
  6906. int et = ehdr.get_e_type();
  6907. // ET_EXEC files are valid input for --just-symbols/-R,
  6908. // and we treat them as relocatable objects.
  6909. if (et == elfcpp::ET_REL
  6910. || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
  6911. {
  6912. Mips_relobj<size, big_endian>* obj =
  6913. new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
  6914. obj->setup();
  6915. return obj;
  6916. }
  6917. else if (et == elfcpp::ET_DYN)
  6918. {
  6919. // TODO(sasa): Should we create Mips_dynobj?
  6920. return Target::do_make_elf_object(name, input_file, offset, ehdr);
  6921. }
  6922. else
  6923. {
  6924. gold_error(_("%s: unsupported ELF file type %d"),
  6925. name.c_str(), et);
  6926. return NULL;
  6927. }
  6928. }
  6929. // Finalize the sections.
  6930. template <int size, bool big_endian>
  6931. void
  6932. Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
  6933. const Input_objects* input_objects,
  6934. Symbol_table* symtab)
  6935. {
  6936. // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
  6937. // DT_FINI have correct values.
  6938. Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
  6939. symtab->lookup(parameters->options().init()));
  6940. if (init != NULL && (init->is_mips16() || init->is_micromips()))
  6941. init->set_value(init->value() | 1);
  6942. Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
  6943. symtab->lookup(parameters->options().fini()));
  6944. if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
  6945. fini->set_value(fini->value() | 1);
  6946. // Check whether the entry symbol is mips16 or micromips. This is needed to
  6947. // adjust entry address in ELF header.
  6948. Mips_symbol<size>* entry =
  6949. static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
  6950. this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
  6951. || entry->is_micromips()));
  6952. if (!parameters->doing_static_link()
  6953. && (strcmp(parameters->options().hash_style(), "gnu") == 0
  6954. || strcmp(parameters->options().hash_style(), "both") == 0))
  6955. {
  6956. // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
  6957. // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
  6958. // MIPS ABI requires a mapping between the GOT and the symbol table.
  6959. gold_error(".gnu.hash is incompatible with the MIPS ABI");
  6960. }
  6961. // Check whether the final section that was scanned has HI16 or GOT16
  6962. // relocations without the corresponding LO16 part.
  6963. if (this->got16_addends_.size() > 0)
  6964. gold_error("Can't find matching LO16 reloc");
  6965. // Set _gp value.
  6966. this->set_gp(layout, symtab);
  6967. // Check for any mips16 stub sections that we can discard.
  6968. if (!parameters->options().relocatable())
  6969. {
  6970. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  6971. p != input_objects->relobj_end();
  6972. ++p)
  6973. {
  6974. Mips_relobj<size, big_endian>* object =
  6975. Mips_relobj<size, big_endian>::as_mips_relobj(*p);
  6976. object->discard_mips16_stub_sections(symtab);
  6977. }
  6978. }
  6979. // Merge processor-specific flags.
  6980. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  6981. p != input_objects->relobj_end();
  6982. ++p)
  6983. {
  6984. Mips_relobj<size, big_endian>* relobj =
  6985. Mips_relobj<size, big_endian>::as_mips_relobj(*p);
  6986. Input_file::Format format = relobj->input_file()->format();
  6987. if (format == Input_file::FORMAT_ELF)
  6988. {
  6989. // Read processor-specific flags in ELF file header.
  6990. const unsigned char* pehdr = relobj->get_view(
  6991. elfcpp::file_header_offset,
  6992. elfcpp::Elf_sizes<size>::ehdr_size,
  6993. true, false);
  6994. elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
  6995. elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
  6996. unsigned char ei_class = ehdr.get_e_ident()[elfcpp::EI_CLASS];
  6997. this->merge_processor_specific_flags(relobj->name(), in_flags,
  6998. ei_class, false);
  6999. }
  7000. }
  7001. for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
  7002. p != input_objects->dynobj_end();
  7003. ++p)
  7004. {
  7005. Sized_dynobj<size, big_endian>* dynobj =
  7006. static_cast<Sized_dynobj<size, big_endian>*>(*p);
  7007. // Read processor-specific flags.
  7008. const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset,
  7009. elfcpp::Elf_sizes<size>::ehdr_size,
  7010. true, false);
  7011. elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
  7012. elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
  7013. unsigned char ei_class = ehdr.get_e_ident()[elfcpp::EI_CLASS];
  7014. this->merge_processor_specific_flags(dynobj->name(), in_flags, ei_class,
  7015. true);
  7016. }
  7017. // Merge .reginfo contents of input objects.
  7018. Valtype gprmask = 0;
  7019. Valtype cprmask1 = 0;
  7020. Valtype cprmask2 = 0;
  7021. Valtype cprmask3 = 0;
  7022. Valtype cprmask4 = 0;
  7023. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  7024. p != input_objects->relobj_end();
  7025. ++p)
  7026. {
  7027. Mips_relobj<size, big_endian>* relobj =
  7028. Mips_relobj<size, big_endian>::as_mips_relobj(*p);
  7029. gprmask |= relobj->gprmask();
  7030. cprmask1 |= relobj->cprmask1();
  7031. cprmask2 |= relobj->cprmask2();
  7032. cprmask3 |= relobj->cprmask3();
  7033. cprmask4 |= relobj->cprmask4();
  7034. }
  7035. if (this->plt_ != NULL)
  7036. {
  7037. // Set final PLT offsets for symbols.
  7038. this->plt_section()->set_plt_offsets();
  7039. // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
  7040. // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
  7041. // there are no standard PLT entries present.
  7042. unsigned char nonvis = 0;
  7043. if (this->is_output_micromips()
  7044. && !this->plt_section()->has_standard_entries())
  7045. nonvis = elfcpp::STO_MICROMIPS >> 2;
  7046. symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
  7047. Symbol_table::PREDEFINED,
  7048. this->plt_,
  7049. 0, 0, elfcpp::STT_FUNC,
  7050. elfcpp::STB_LOCAL,
  7051. elfcpp::STV_DEFAULT, nonvis,
  7052. false, false);
  7053. }
  7054. if (this->mips_stubs_ != NULL)
  7055. {
  7056. // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
  7057. unsigned char nonvis = 0;
  7058. if (this->is_output_micromips())
  7059. nonvis = elfcpp::STO_MICROMIPS >> 2;
  7060. symtab->define_in_output_data("_MIPS_STUBS_", NULL,
  7061. Symbol_table::PREDEFINED,
  7062. this->mips_stubs_,
  7063. 0, 0, elfcpp::STT_FUNC,
  7064. elfcpp::STB_LOCAL,
  7065. elfcpp::STV_DEFAULT, nonvis,
  7066. false, false);
  7067. }
  7068. if (!parameters->options().relocatable() && !parameters->doing_static_link())
  7069. // In case there is no .got section, create one.
  7070. this->got_section(symtab, layout);
  7071. // Emit any relocs we saved in an attempt to avoid generating COPY
  7072. // relocs.
  7073. if (this->copy_relocs_.any_saved_relocs())
  7074. this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
  7075. this);
  7076. // Emit dynamic relocs.
  7077. for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
  7078. p != this->dyn_relocs_.end();
  7079. ++p)
  7080. p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
  7081. if (this->has_got_section())
  7082. this->got_section()->lay_out_got(layout, symtab, input_objects);
  7083. if (this->mips_stubs_ != NULL)
  7084. this->mips_stubs_->set_needs_dynsym_value();
  7085. // Check for functions that might need $25 to be valid on entry.
  7086. // TODO(sasa): Can we do this without iterating over all symbols?
  7087. typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
  7088. symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
  7089. symtab));
  7090. // Add NULL segment.
  7091. if (!parameters->options().relocatable())
  7092. layout->make_output_segment(elfcpp::PT_NULL, 0);
  7093. for (Layout::Section_list::const_iterator p = layout->section_list().begin();
  7094. p != layout->section_list().end();
  7095. ++p)
  7096. {
  7097. if ((*p)->type() == elfcpp::SHT_MIPS_REGINFO)
  7098. {
  7099. Mips_output_section_reginfo<size, big_endian>* reginfo =
  7100. Mips_output_section_reginfo<size, big_endian>::
  7101. as_mips_output_section_reginfo(*p);
  7102. reginfo->set_masks(gprmask, cprmask1, cprmask2, cprmask3, cprmask4);
  7103. if (!parameters->options().relocatable())
  7104. {
  7105. Output_segment* reginfo_segment =
  7106. layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
  7107. elfcpp::PF_R);
  7108. reginfo_segment->add_output_section_to_nonload(reginfo,
  7109. elfcpp::PF_R);
  7110. }
  7111. }
  7112. }
  7113. // Fill in some more dynamic tags.
  7114. // TODO(sasa): Add more dynamic tags.
  7115. const Reloc_section* rel_plt = (this->plt_ == NULL
  7116. ? NULL : this->plt_->rel_plt());
  7117. layout->add_target_dynamic_tags(true, this->got_, rel_plt,
  7118. this->rel_dyn_, true, false);
  7119. Output_data_dynamic* const odyn = layout->dynamic_data();
  7120. if (odyn != NULL
  7121. && !parameters->options().relocatable()
  7122. && !parameters->doing_static_link())
  7123. {
  7124. unsigned int d_val;
  7125. // This element holds a 32-bit version id for the Runtime
  7126. // Linker Interface. This will start at integer value 1.
  7127. d_val = 0x01;
  7128. odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
  7129. // Dynamic flags
  7130. d_val = elfcpp::RHF_NOTPOT;
  7131. odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
  7132. // Save layout for using when emiting custom dynamic tags.
  7133. this->layout_ = layout;
  7134. // This member holds the base address of the segment.
  7135. odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
  7136. // This member holds the number of entries in the .dynsym section.
  7137. odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
  7138. // This member holds the index of the first dynamic symbol
  7139. // table entry that corresponds to an entry in the global offset table.
  7140. odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
  7141. // This member holds the number of local GOT entries.
  7142. odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
  7143. this->got_->get_local_gotno());
  7144. if (this->plt_ != NULL)
  7145. // DT_MIPS_PLTGOT dynamic tag
  7146. odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
  7147. }
  7148. }
  7149. // Get the custom dynamic tag value.
  7150. template<int size, bool big_endian>
  7151. unsigned int
  7152. Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
  7153. {
  7154. switch (tag)
  7155. {
  7156. case elfcpp::DT_MIPS_BASE_ADDRESS:
  7157. {
  7158. // The base address of the segment.
  7159. // At this point, the segment list has been sorted into final order,
  7160. // so just return vaddr of the first readable PT_LOAD segment.
  7161. Output_segment* seg =
  7162. this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
  7163. gold_assert(seg != NULL);
  7164. return seg->vaddr();
  7165. }
  7166. case elfcpp::DT_MIPS_SYMTABNO:
  7167. // The number of entries in the .dynsym section.
  7168. return this->get_dt_mips_symtabno();
  7169. case elfcpp::DT_MIPS_GOTSYM:
  7170. {
  7171. // The index of the first dynamic symbol table entry that corresponds
  7172. // to an entry in the GOT.
  7173. if (this->got_->first_global_got_dynsym_index() != -1U)
  7174. return this->got_->first_global_got_dynsym_index();
  7175. else
  7176. // In case if we don't have global GOT symbols we default to setting
  7177. // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
  7178. return this->get_dt_mips_symtabno();
  7179. }
  7180. default:
  7181. gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
  7182. }
  7183. return (unsigned int)-1;
  7184. }
  7185. // Relocate section data.
  7186. template<int size, bool big_endian>
  7187. void
  7188. Target_mips<size, big_endian>::relocate_section(
  7189. const Relocate_info<size, big_endian>* relinfo,
  7190. unsigned int sh_type,
  7191. const unsigned char* prelocs,
  7192. size_t reloc_count,
  7193. Output_section* output_section,
  7194. bool needs_special_offset_handling,
  7195. unsigned char* view,
  7196. Mips_address address,
  7197. section_size_type view_size,
  7198. const Reloc_symbol_changes* reloc_symbol_changes)
  7199. {
  7200. typedef Target_mips<size, big_endian> Mips;
  7201. typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
  7202. if (sh_type == elfcpp::SHT_REL)
  7203. gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_REL,
  7204. Mips_relocate, gold::Default_comdat_behavior>(
  7205. relinfo,
  7206. this,
  7207. prelocs,
  7208. reloc_count,
  7209. output_section,
  7210. needs_special_offset_handling,
  7211. view,
  7212. address,
  7213. view_size,
  7214. reloc_symbol_changes);
  7215. else if (sh_type == elfcpp::SHT_RELA)
  7216. gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_RELA,
  7217. Mips_relocate, gold::Default_comdat_behavior>(
  7218. relinfo,
  7219. this,
  7220. prelocs,
  7221. reloc_count,
  7222. output_section,
  7223. needs_special_offset_handling,
  7224. view,
  7225. address,
  7226. view_size,
  7227. reloc_symbol_changes);
  7228. }
  7229. // Return the size of a relocation while scanning during a relocatable
  7230. // link.
  7231. template<int size, bool big_endian>
  7232. unsigned int
  7233. Target_mips<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
  7234. unsigned int r_type,
  7235. Relobj* object)
  7236. {
  7237. switch (r_type)
  7238. {
  7239. case elfcpp::R_MIPS_NONE:
  7240. case elfcpp::R_MIPS_TLS_DTPMOD64:
  7241. case elfcpp::R_MIPS_TLS_DTPREL64:
  7242. case elfcpp::R_MIPS_TLS_TPREL64:
  7243. return 0;
  7244. case elfcpp::R_MIPS_32:
  7245. case elfcpp::R_MIPS_TLS_DTPMOD32:
  7246. case elfcpp::R_MIPS_TLS_DTPREL32:
  7247. case elfcpp::R_MIPS_TLS_TPREL32:
  7248. case elfcpp::R_MIPS_REL32:
  7249. case elfcpp::R_MIPS_PC32:
  7250. case elfcpp::R_MIPS_GPREL32:
  7251. case elfcpp::R_MIPS_JALR:
  7252. return 4;
  7253. case elfcpp::R_MIPS_16:
  7254. case elfcpp::R_MIPS_HI16:
  7255. case elfcpp::R_MIPS_LO16:
  7256. case elfcpp::R_MIPS_GPREL16:
  7257. case elfcpp::R_MIPS16_HI16:
  7258. case elfcpp::R_MIPS16_LO16:
  7259. case elfcpp::R_MIPS_PC16:
  7260. case elfcpp::R_MIPS_GOT16:
  7261. case elfcpp::R_MIPS16_GOT16:
  7262. case elfcpp::R_MIPS_CALL16:
  7263. case elfcpp::R_MIPS16_CALL16:
  7264. case elfcpp::R_MIPS_GOT_HI16:
  7265. case elfcpp::R_MIPS_CALL_HI16:
  7266. case elfcpp::R_MIPS_GOT_LO16:
  7267. case elfcpp::R_MIPS_CALL_LO16:
  7268. case elfcpp::R_MIPS_TLS_DTPREL_HI16:
  7269. case elfcpp::R_MIPS_TLS_DTPREL_LO16:
  7270. case elfcpp::R_MIPS_TLS_TPREL_HI16:
  7271. case elfcpp::R_MIPS_TLS_TPREL_LO16:
  7272. case elfcpp::R_MIPS16_GPREL:
  7273. case elfcpp::R_MIPS_GOT_DISP:
  7274. case elfcpp::R_MIPS_LITERAL:
  7275. case elfcpp::R_MIPS_GOT_PAGE:
  7276. case elfcpp::R_MIPS_GOT_OFST:
  7277. case elfcpp::R_MIPS_TLS_GD:
  7278. case elfcpp::R_MIPS_TLS_LDM:
  7279. case elfcpp::R_MIPS_TLS_GOTTPREL:
  7280. return 2;
  7281. // These relocations are not byte sized
  7282. case elfcpp::R_MIPS_26:
  7283. case elfcpp::R_MIPS16_26:
  7284. return 4;
  7285. case elfcpp::R_MIPS_COPY:
  7286. case elfcpp::R_MIPS_JUMP_SLOT:
  7287. object->error(_("unexpected reloc %u in object file"), r_type);
  7288. return 0;
  7289. default:
  7290. object->error(_("unsupported reloc %u in object file"), r_type);
  7291. return 0;
  7292. }
  7293. }
  7294. // Scan the relocs during a relocatable link.
  7295. template<int size, bool big_endian>
  7296. void
  7297. Target_mips<size, big_endian>::scan_relocatable_relocs(
  7298. Symbol_table* symtab,
  7299. Layout* layout,
  7300. Sized_relobj_file<size, big_endian>* object,
  7301. unsigned int data_shndx,
  7302. unsigned int sh_type,
  7303. const unsigned char* prelocs,
  7304. size_t reloc_count,
  7305. Output_section* output_section,
  7306. bool needs_special_offset_handling,
  7307. size_t local_symbol_count,
  7308. const unsigned char* plocal_symbols,
  7309. Relocatable_relocs* rr)
  7310. {
  7311. gold_assert(sh_type == elfcpp::SHT_REL);
  7312. typedef Mips_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
  7313. Relocatable_size_for_reloc> Scan_relocatable_relocs;
  7314. gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_REL,
  7315. Scan_relocatable_relocs>(
  7316. symtab,
  7317. layout,
  7318. object,
  7319. data_shndx,
  7320. prelocs,
  7321. reloc_count,
  7322. output_section,
  7323. needs_special_offset_handling,
  7324. local_symbol_count,
  7325. plocal_symbols,
  7326. rr);
  7327. }
  7328. // Emit relocations for a section.
  7329. template<int size, bool big_endian>
  7330. void
  7331. Target_mips<size, big_endian>::relocate_relocs(
  7332. const Relocate_info<size, big_endian>* relinfo,
  7333. unsigned int sh_type,
  7334. const unsigned char* prelocs,
  7335. size_t reloc_count,
  7336. Output_section* output_section,
  7337. typename elfcpp::Elf_types<size>::Elf_Off
  7338. offset_in_output_section,
  7339. const Relocatable_relocs* rr,
  7340. unsigned char* view,
  7341. Mips_address view_address,
  7342. section_size_type view_size,
  7343. unsigned char* reloc_view,
  7344. section_size_type reloc_view_size)
  7345. {
  7346. gold_assert(sh_type == elfcpp::SHT_REL);
  7347. gold::relocate_relocs<size, big_endian, elfcpp::SHT_REL>(
  7348. relinfo,
  7349. prelocs,
  7350. reloc_count,
  7351. output_section,
  7352. offset_in_output_section,
  7353. rr,
  7354. view,
  7355. view_address,
  7356. view_size,
  7357. reloc_view,
  7358. reloc_view_size);
  7359. }
  7360. // Perform target-specific processing in a relocatable link. This is
  7361. // only used if we use the relocation strategy RELOC_SPECIAL.
  7362. template<int size, bool big_endian>
  7363. void
  7364. Target_mips<size, big_endian>::relocate_special_relocatable(
  7365. const Relocate_info<size, big_endian>* relinfo,
  7366. unsigned int sh_type,
  7367. const unsigned char* preloc_in,
  7368. size_t relnum,
  7369. Output_section* output_section,
  7370. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  7371. unsigned char* view,
  7372. Mips_address view_address,
  7373. section_size_type,
  7374. unsigned char* preloc_out)
  7375. {
  7376. // We can only handle REL type relocation sections.
  7377. gold_assert(sh_type == elfcpp::SHT_REL);
  7378. typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
  7379. Reltype;
  7380. typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
  7381. Reltype_write;
  7382. typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
  7383. const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
  7384. Mips_relobj<size, big_endian>* object =
  7385. Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
  7386. const unsigned int local_count = object->local_symbol_count();
  7387. Reltype reloc(preloc_in);
  7388. Reltype_write reloc_write(preloc_out);
  7389. elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
  7390. const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  7391. const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  7392. // Get the new symbol index.
  7393. // We only use RELOC_SPECIAL strategy in local relocations.
  7394. gold_assert(r_sym < local_count);
  7395. // We are adjusting a section symbol. We need to find
  7396. // the symbol table index of the section symbol for
  7397. // the output section corresponding to input section
  7398. // in which this symbol is defined.
  7399. bool is_ordinary;
  7400. unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
  7401. gold_assert(is_ordinary);
  7402. Output_section* os = object->output_section(shndx);
  7403. gold_assert(os != NULL);
  7404. gold_assert(os->needs_symtab_index());
  7405. unsigned int new_symndx = os->symtab_index();
  7406. // Get the new offset--the location in the output section where
  7407. // this relocation should be applied.
  7408. Mips_address offset = reloc.get_r_offset();
  7409. Mips_address new_offset;
  7410. if (offset_in_output_section != invalid_address)
  7411. new_offset = offset + offset_in_output_section;
  7412. else
  7413. {
  7414. section_offset_type sot_offset =
  7415. convert_types<section_offset_type, Mips_address>(offset);
  7416. section_offset_type new_sot_offset =
  7417. output_section->output_offset(object, relinfo->data_shndx,
  7418. sot_offset);
  7419. gold_assert(new_sot_offset != -1);
  7420. new_offset = new_sot_offset;
  7421. }
  7422. // In an object file, r_offset is an offset within the section.
  7423. // In an executable or dynamic object, generated by
  7424. // --emit-relocs, r_offset is an absolute address.
  7425. if (!parameters->options().relocatable())
  7426. {
  7427. new_offset += view_address;
  7428. if (offset_in_output_section != invalid_address)
  7429. new_offset -= offset_in_output_section;
  7430. }
  7431. reloc_write.put_r_offset(new_offset);
  7432. reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
  7433. // Handle the reloc addend.
  7434. // The relocation uses a section symbol in the input file.
  7435. // We are adjusting it to use a section symbol in the output
  7436. // file. The input section symbol refers to some address in
  7437. // the input section. We need the relocation in the output
  7438. // file to refer to that same address. This adjustment to
  7439. // the addend is the same calculation we use for a simple
  7440. // absolute relocation for the input section symbol.
  7441. const Symbol_value<size>* psymval = object->local_symbol(r_sym);
  7442. unsigned char* paddend = view + offset;
  7443. typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
  7444. switch (r_type)
  7445. {
  7446. case elfcpp::R_MIPS_26:
  7447. reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
  7448. offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
  7449. false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal());
  7450. break;
  7451. default:
  7452. gold_unreachable();
  7453. }
  7454. // Report any errors.
  7455. switch (reloc_status)
  7456. {
  7457. case Reloc_funcs::STATUS_OKAY:
  7458. break;
  7459. case Reloc_funcs::STATUS_OVERFLOW:
  7460. gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
  7461. _("relocation overflow"));
  7462. break;
  7463. case Reloc_funcs::STATUS_BAD_RELOC:
  7464. gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
  7465. _("unexpected opcode while processing relocation"));
  7466. break;
  7467. default:
  7468. gold_unreachable();
  7469. }
  7470. }
  7471. // Optimize the TLS relocation type based on what we know about the
  7472. // symbol. IS_FINAL is true if the final address of this symbol is
  7473. // known at link time.
  7474. template<int size, bool big_endian>
  7475. tls::Tls_optimization
  7476. Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
  7477. {
  7478. // FIXME: Currently we do not do any TLS optimization.
  7479. return tls::TLSOPT_NONE;
  7480. }
  7481. // Scan a relocation for a local symbol.
  7482. template<int size, bool big_endian>
  7483. inline void
  7484. Target_mips<size, big_endian>::Scan::local(
  7485. Symbol_table* symtab,
  7486. Layout* layout,
  7487. Target_mips<size, big_endian>* target,
  7488. Sized_relobj_file<size, big_endian>* object,
  7489. unsigned int data_shndx,
  7490. Output_section* output_section,
  7491. const elfcpp::Rela<size, big_endian>* rela,
  7492. const elfcpp::Rel<size, big_endian>* rel,
  7493. unsigned int rel_type,
  7494. unsigned int r_type,
  7495. const elfcpp::Sym<size, big_endian>& lsym,
  7496. bool is_discarded)
  7497. {
  7498. if (is_discarded)
  7499. return;
  7500. Mips_address r_offset;
  7501. typename elfcpp::Elf_types<size>::Elf_WXword r_info;
  7502. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
  7503. if (rel_type == elfcpp::SHT_RELA)
  7504. {
  7505. r_offset = rela->get_r_offset();
  7506. r_info = rela->get_r_info();
  7507. r_addend = rela->get_r_addend();
  7508. }
  7509. else
  7510. {
  7511. r_offset = rel->get_r_offset();
  7512. r_info = rel->get_r_info();
  7513. r_addend = 0;
  7514. }
  7515. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  7516. Mips_relobj<size, big_endian>* mips_obj =
  7517. Mips_relobj<size, big_endian>::as_mips_relobj(object);
  7518. if (mips_obj->is_mips16_stub_section(data_shndx))
  7519. {
  7520. mips_obj->get_mips16_stub_section(data_shndx)
  7521. ->new_local_reloc_found(r_type, r_sym);
  7522. }
  7523. if (r_type == elfcpp::R_MIPS_NONE)
  7524. // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
  7525. // mips16 stub.
  7526. return;
  7527. if (!mips16_call_reloc(r_type)
  7528. && !mips_obj->section_allows_mips16_refs(data_shndx))
  7529. // This reloc would need to refer to a MIPS16 hard-float stub, if
  7530. // there is one. We ignore MIPS16 stub sections and .pdr section when
  7531. // looking for relocs that would need to refer to MIPS16 stubs.
  7532. mips_obj->add_local_non_16bit_call(r_sym);
  7533. if (r_type == elfcpp::R_MIPS16_26
  7534. && !mips_obj->section_allows_mips16_refs(data_shndx))
  7535. mips_obj->add_local_16bit_call(r_sym);
  7536. switch (r_type)
  7537. {
  7538. case elfcpp::R_MIPS_GOT16:
  7539. case elfcpp::R_MIPS_CALL16:
  7540. case elfcpp::R_MIPS_CALL_HI16:
  7541. case elfcpp::R_MIPS_CALL_LO16:
  7542. case elfcpp::R_MIPS_GOT_HI16:
  7543. case elfcpp::R_MIPS_GOT_LO16:
  7544. case elfcpp::R_MIPS_GOT_PAGE:
  7545. case elfcpp::R_MIPS_GOT_OFST:
  7546. case elfcpp::R_MIPS_GOT_DISP:
  7547. case elfcpp::R_MIPS_TLS_GOTTPREL:
  7548. case elfcpp::R_MIPS_TLS_GD:
  7549. case elfcpp::R_MIPS_TLS_LDM:
  7550. case elfcpp::R_MIPS16_GOT16:
  7551. case elfcpp::R_MIPS16_CALL16:
  7552. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  7553. case elfcpp::R_MIPS16_TLS_GD:
  7554. case elfcpp::R_MIPS16_TLS_LDM:
  7555. case elfcpp::R_MICROMIPS_GOT16:
  7556. case elfcpp::R_MICROMIPS_CALL16:
  7557. case elfcpp::R_MICROMIPS_CALL_HI16:
  7558. case elfcpp::R_MICROMIPS_CALL_LO16:
  7559. case elfcpp::R_MICROMIPS_GOT_HI16:
  7560. case elfcpp::R_MICROMIPS_GOT_LO16:
  7561. case elfcpp::R_MICROMIPS_GOT_PAGE:
  7562. case elfcpp::R_MICROMIPS_GOT_OFST:
  7563. case elfcpp::R_MICROMIPS_GOT_DISP:
  7564. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  7565. case elfcpp::R_MICROMIPS_TLS_GD:
  7566. case elfcpp::R_MICROMIPS_TLS_LDM:
  7567. // We need a GOT section.
  7568. target->got_section(symtab, layout);
  7569. break;
  7570. default:
  7571. break;
  7572. }
  7573. if (call_lo16_reloc(r_type)
  7574. || got_lo16_reloc(r_type)
  7575. || got_disp_reloc(r_type))
  7576. {
  7577. // We may need a local GOT entry for this relocation. We
  7578. // don't count R_MIPS_GOT_PAGE because we can estimate the
  7579. // maximum number of pages needed by looking at the size of
  7580. // the segment. Similar comments apply to R_MIPS*_GOT16 and
  7581. // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
  7582. // R_MIPS_CALL_HI16 because these are always followed by an
  7583. // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
  7584. Mips_output_data_got<size, big_endian>* got =
  7585. target->got_section(symtab, layout);
  7586. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  7587. got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U);
  7588. }
  7589. switch (r_type)
  7590. {
  7591. case elfcpp::R_MIPS_CALL16:
  7592. case elfcpp::R_MIPS16_CALL16:
  7593. case elfcpp::R_MICROMIPS_CALL16:
  7594. gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
  7595. (unsigned long)r_offset);
  7596. return;
  7597. case elfcpp::R_MIPS_GOT_PAGE:
  7598. case elfcpp::R_MICROMIPS_GOT_PAGE:
  7599. case elfcpp::R_MIPS16_GOT16:
  7600. case elfcpp::R_MIPS_GOT16:
  7601. case elfcpp::R_MIPS_GOT_HI16:
  7602. case elfcpp::R_MIPS_GOT_LO16:
  7603. case elfcpp::R_MICROMIPS_GOT16:
  7604. case elfcpp::R_MICROMIPS_GOT_HI16:
  7605. case elfcpp::R_MICROMIPS_GOT_LO16:
  7606. {
  7607. // This relocation needs a page entry in the GOT.
  7608. // Get the section contents.
  7609. section_size_type view_size = 0;
  7610. const unsigned char* view = object->section_contents(data_shndx,
  7611. &view_size, false);
  7612. view += r_offset;
  7613. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  7614. Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
  7615. : r_addend);
  7616. if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
  7617. target->got16_addends_.push_back(got16_addend<size, big_endian>(
  7618. object, data_shndx, r_type, r_sym, addend));
  7619. else
  7620. target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
  7621. break;
  7622. }
  7623. case elfcpp::R_MIPS_HI16:
  7624. case elfcpp::R_MIPS16_HI16:
  7625. case elfcpp::R_MICROMIPS_HI16:
  7626. // Record the reloc so that we can check whether the corresponding LO16
  7627. // part exists.
  7628. if (rel_type == elfcpp::SHT_REL)
  7629. target->got16_addends_.push_back(got16_addend<size, big_endian>(
  7630. object, data_shndx, r_type, r_sym, 0));
  7631. break;
  7632. case elfcpp::R_MIPS_LO16:
  7633. case elfcpp::R_MIPS16_LO16:
  7634. case elfcpp::R_MICROMIPS_LO16:
  7635. {
  7636. if (rel_type != elfcpp::SHT_REL)
  7637. break;
  7638. // Find corresponding GOT16/HI16 relocation.
  7639. // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
  7640. // be immediately following. However, for the IRIX6 ABI, the next
  7641. // relocation may be a composed relocation consisting of several
  7642. // relocations for the same address. In that case, the R_MIPS_LO16
  7643. // relocation may occur as one of these. We permit a similar
  7644. // extension in general, as that is useful for GCC.
  7645. // In some cases GCC dead code elimination removes the LO16 but
  7646. // keeps the corresponding HI16. This is strictly speaking a
  7647. // violation of the ABI but not immediately harmful.
  7648. typename std::list<got16_addend<size, big_endian> >::iterator it =
  7649. target->got16_addends_.begin();
  7650. while (it != target->got16_addends_.end())
  7651. {
  7652. got16_addend<size, big_endian> _got16_addend = *it;
  7653. // TODO(sasa): Split got16_addends_ list into two lists - one for
  7654. // GOT16 relocs and the other for HI16 relocs.
  7655. // Report an error if we find HI16 or GOT16 reloc from the
  7656. // previous section without the matching LO16 part.
  7657. if (_got16_addend.object != object
  7658. || _got16_addend.shndx != data_shndx)
  7659. {
  7660. gold_error("Can't find matching LO16 reloc");
  7661. break;
  7662. }
  7663. if (_got16_addend.r_sym != r_sym
  7664. || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
  7665. {
  7666. ++it;
  7667. continue;
  7668. }
  7669. // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
  7670. // For GOT16, we need to calculate combined addend and record GOT page
  7671. // entry.
  7672. if (got16_reloc(_got16_addend.r_type))
  7673. {
  7674. section_size_type view_size = 0;
  7675. const unsigned char* view = object->section_contents(data_shndx,
  7676. &view_size,
  7677. false);
  7678. view += r_offset;
  7679. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  7680. int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
  7681. addend = (_got16_addend.addend << 16) + addend;
  7682. target->got_section()->record_got_page_entry(mips_obj, r_sym,
  7683. addend);
  7684. }
  7685. it = target->got16_addends_.erase(it);
  7686. }
  7687. break;
  7688. }
  7689. }
  7690. switch (r_type)
  7691. {
  7692. case elfcpp::R_MIPS_32:
  7693. case elfcpp::R_MIPS_REL32:
  7694. case elfcpp::R_MIPS_64:
  7695. {
  7696. if (parameters->options().output_is_position_independent())
  7697. {
  7698. // If building a shared library (or a position-independent
  7699. // executable), we need to create a dynamic relocation for
  7700. // this location.
  7701. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7702. unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
  7703. rel_dyn->add_symbolless_local_addend(object, r_sym,
  7704. elfcpp::R_MIPS_REL32,
  7705. output_section, data_shndx,
  7706. r_offset);
  7707. }
  7708. break;
  7709. }
  7710. case elfcpp::R_MIPS_TLS_GOTTPREL:
  7711. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  7712. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  7713. case elfcpp::R_MIPS_TLS_LDM:
  7714. case elfcpp::R_MIPS16_TLS_LDM:
  7715. case elfcpp::R_MICROMIPS_TLS_LDM:
  7716. case elfcpp::R_MIPS_TLS_GD:
  7717. case elfcpp::R_MIPS16_TLS_GD:
  7718. case elfcpp::R_MICROMIPS_TLS_GD:
  7719. {
  7720. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  7721. bool output_is_shared = parameters->options().shared();
  7722. const tls::Tls_optimization optimized_type
  7723. = Target_mips<size, big_endian>::optimize_tls_reloc(
  7724. !output_is_shared, r_type);
  7725. switch (r_type)
  7726. {
  7727. case elfcpp::R_MIPS_TLS_GD:
  7728. case elfcpp::R_MIPS16_TLS_GD:
  7729. case elfcpp::R_MICROMIPS_TLS_GD:
  7730. if (optimized_type == tls::TLSOPT_NONE)
  7731. {
  7732. // Create a pair of GOT entries for the module index and
  7733. // dtv-relative offset.
  7734. Mips_output_data_got<size, big_endian>* got =
  7735. target->got_section(symtab, layout);
  7736. unsigned int shndx = lsym.get_st_shndx();
  7737. bool is_ordinary;
  7738. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  7739. if (!is_ordinary)
  7740. {
  7741. object->error(_("local symbol %u has bad shndx %u"),
  7742. r_sym, shndx);
  7743. break;
  7744. }
  7745. got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
  7746. shndx);
  7747. }
  7748. else
  7749. {
  7750. // FIXME: TLS optimization not supported yet.
  7751. gold_unreachable();
  7752. }
  7753. break;
  7754. case elfcpp::R_MIPS_TLS_LDM:
  7755. case elfcpp::R_MIPS16_TLS_LDM:
  7756. case elfcpp::R_MICROMIPS_TLS_LDM:
  7757. if (optimized_type == tls::TLSOPT_NONE)
  7758. {
  7759. // We always record LDM symbols as local with index 0.
  7760. target->got_section()->record_local_got_symbol(mips_obj, 0,
  7761. r_addend, r_type,
  7762. -1U);
  7763. }
  7764. else
  7765. {
  7766. // FIXME: TLS optimization not supported yet.
  7767. gold_unreachable();
  7768. }
  7769. break;
  7770. case elfcpp::R_MIPS_TLS_GOTTPREL:
  7771. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  7772. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  7773. layout->set_has_static_tls();
  7774. if (optimized_type == tls::TLSOPT_NONE)
  7775. {
  7776. // Create a GOT entry for the tp-relative offset.
  7777. Mips_output_data_got<size, big_endian>* got =
  7778. target->got_section(symtab, layout);
  7779. got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
  7780. -1U);
  7781. }
  7782. else
  7783. {
  7784. // FIXME: TLS optimization not supported yet.
  7785. gold_unreachable();
  7786. }
  7787. break;
  7788. default:
  7789. gold_unreachable();
  7790. }
  7791. }
  7792. break;
  7793. default:
  7794. break;
  7795. }
  7796. // Refuse some position-dependent relocations when creating a
  7797. // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
  7798. // not PIC, but we can create dynamic relocations and the result
  7799. // will be fine. Also do not refuse R_MIPS_LO16, which can be
  7800. // combined with R_MIPS_GOT16.
  7801. if (parameters->options().shared())
  7802. {
  7803. switch (r_type)
  7804. {
  7805. case elfcpp::R_MIPS16_HI16:
  7806. case elfcpp::R_MIPS_HI16:
  7807. case elfcpp::R_MICROMIPS_HI16:
  7808. // Don't refuse a high part relocation if it's against
  7809. // no symbol (e.g. part of a compound relocation).
  7810. if (r_sym == 0)
  7811. break;
  7812. // FALLTHROUGH
  7813. case elfcpp::R_MIPS16_26:
  7814. case elfcpp::R_MIPS_26:
  7815. case elfcpp::R_MICROMIPS_26_S1:
  7816. gold_error(_("%s: relocation %u against `%s' can not be used when "
  7817. "making a shared object; recompile with -fPIC"),
  7818. object->name().c_str(), r_type, "a local symbol");
  7819. default:
  7820. break;
  7821. }
  7822. }
  7823. }
  7824. template<int size, bool big_endian>
  7825. inline void
  7826. Target_mips<size, big_endian>::Scan::local(
  7827. Symbol_table* symtab,
  7828. Layout* layout,
  7829. Target_mips<size, big_endian>* target,
  7830. Sized_relobj_file<size, big_endian>* object,
  7831. unsigned int data_shndx,
  7832. Output_section* output_section,
  7833. const elfcpp::Rel<size, big_endian>& reloc,
  7834. unsigned int r_type,
  7835. const elfcpp::Sym<size, big_endian>& lsym,
  7836. bool is_discarded)
  7837. {
  7838. if (is_discarded)
  7839. return;
  7840. local(
  7841. symtab,
  7842. layout,
  7843. target,
  7844. object,
  7845. data_shndx,
  7846. output_section,
  7847. (const elfcpp::Rela<size, big_endian>*) NULL,
  7848. &reloc,
  7849. elfcpp::SHT_REL,
  7850. r_type,
  7851. lsym, is_discarded);
  7852. }
  7853. template<int size, bool big_endian>
  7854. inline void
  7855. Target_mips<size, big_endian>::Scan::local(
  7856. Symbol_table* symtab,
  7857. Layout* layout,
  7858. Target_mips<size, big_endian>* target,
  7859. Sized_relobj_file<size, big_endian>* object,
  7860. unsigned int data_shndx,
  7861. Output_section* output_section,
  7862. const elfcpp::Rela<size, big_endian>& reloc,
  7863. unsigned int r_type,
  7864. const elfcpp::Sym<size, big_endian>& lsym,
  7865. bool is_discarded)
  7866. {
  7867. if (is_discarded)
  7868. return;
  7869. local(
  7870. symtab,
  7871. layout,
  7872. target,
  7873. object,
  7874. data_shndx,
  7875. output_section,
  7876. &reloc,
  7877. (const elfcpp::Rel<size, big_endian>*) NULL,
  7878. elfcpp::SHT_RELA,
  7879. r_type,
  7880. lsym, is_discarded);
  7881. }
  7882. // Scan a relocation for a global symbol.
  7883. template<int size, bool big_endian>
  7884. inline void
  7885. Target_mips<size, big_endian>::Scan::global(
  7886. Symbol_table* symtab,
  7887. Layout* layout,
  7888. Target_mips<size, big_endian>* target,
  7889. Sized_relobj_file<size, big_endian>* object,
  7890. unsigned int data_shndx,
  7891. Output_section* output_section,
  7892. const elfcpp::Rela<size, big_endian>* rela,
  7893. const elfcpp::Rel<size, big_endian>* rel,
  7894. unsigned int rel_type,
  7895. unsigned int r_type,
  7896. Symbol* gsym)
  7897. {
  7898. Mips_address r_offset;
  7899. typename elfcpp::Elf_types<size>::Elf_WXword r_info;
  7900. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
  7901. if (rel_type == elfcpp::SHT_RELA)
  7902. {
  7903. r_offset = rela->get_r_offset();
  7904. r_info = rela->get_r_info();
  7905. r_addend = rela->get_r_addend();
  7906. }
  7907. else
  7908. {
  7909. r_offset = rel->get_r_offset();
  7910. r_info = rel->get_r_info();
  7911. r_addend = 0;
  7912. }
  7913. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  7914. Mips_relobj<size, big_endian>* mips_obj =
  7915. Mips_relobj<size, big_endian>::as_mips_relobj(object);
  7916. Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
  7917. if (mips_obj->is_mips16_stub_section(data_shndx))
  7918. {
  7919. mips_obj->get_mips16_stub_section(data_shndx)
  7920. ->new_global_reloc_found(r_type, mips_sym);
  7921. }
  7922. if (r_type == elfcpp::R_MIPS_NONE)
  7923. // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
  7924. // mips16 stub.
  7925. return;
  7926. if (!mips16_call_reloc(r_type)
  7927. && !mips_obj->section_allows_mips16_refs(data_shndx))
  7928. // This reloc would need to refer to a MIPS16 hard-float stub, if
  7929. // there is one. We ignore MIPS16 stub sections and .pdr section when
  7930. // looking for relocs that would need to refer to MIPS16 stubs.
  7931. mips_sym->set_need_fn_stub();
  7932. // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
  7933. // section. We check here to avoid creating a dynamic reloc against
  7934. // _GLOBAL_OFFSET_TABLE_.
  7935. if (!target->has_got_section()
  7936. && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
  7937. target->got_section(symtab, layout);
  7938. // We need PLT entries if there are static-only relocations against
  7939. // an externally-defined function. This can technically occur for
  7940. // shared libraries if there are branches to the symbol, although it
  7941. // is unlikely that this will be used in practice due to the short
  7942. // ranges involved. It can occur for any relative or absolute relocation
  7943. // in executables; in that case, the PLT entry becomes the function's
  7944. // canonical address.
  7945. bool static_reloc = false;
  7946. // Set CAN_MAKE_DYNAMIC to true if we can convert this
  7947. // relocation into a dynamic one.
  7948. bool can_make_dynamic = false;
  7949. switch (r_type)
  7950. {
  7951. case elfcpp::R_MIPS_GOT16:
  7952. case elfcpp::R_MIPS_CALL16:
  7953. case elfcpp::R_MIPS_CALL_HI16:
  7954. case elfcpp::R_MIPS_CALL_LO16:
  7955. case elfcpp::R_MIPS_GOT_HI16:
  7956. case elfcpp::R_MIPS_GOT_LO16:
  7957. case elfcpp::R_MIPS_GOT_PAGE:
  7958. case elfcpp::R_MIPS_GOT_OFST:
  7959. case elfcpp::R_MIPS_GOT_DISP:
  7960. case elfcpp::R_MIPS_TLS_GOTTPREL:
  7961. case elfcpp::R_MIPS_TLS_GD:
  7962. case elfcpp::R_MIPS_TLS_LDM:
  7963. case elfcpp::R_MIPS16_GOT16:
  7964. case elfcpp::R_MIPS16_CALL16:
  7965. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  7966. case elfcpp::R_MIPS16_TLS_GD:
  7967. case elfcpp::R_MIPS16_TLS_LDM:
  7968. case elfcpp::R_MICROMIPS_GOT16:
  7969. case elfcpp::R_MICROMIPS_CALL16:
  7970. case elfcpp::R_MICROMIPS_CALL_HI16:
  7971. case elfcpp::R_MICROMIPS_CALL_LO16:
  7972. case elfcpp::R_MICROMIPS_GOT_HI16:
  7973. case elfcpp::R_MICROMIPS_GOT_LO16:
  7974. case elfcpp::R_MICROMIPS_GOT_PAGE:
  7975. case elfcpp::R_MICROMIPS_GOT_OFST:
  7976. case elfcpp::R_MICROMIPS_GOT_DISP:
  7977. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  7978. case elfcpp::R_MICROMIPS_TLS_GD:
  7979. case elfcpp::R_MICROMIPS_TLS_LDM:
  7980. // We need a GOT section.
  7981. target->got_section(symtab, layout);
  7982. break;
  7983. // This is just a hint; it can safely be ignored. Don't set
  7984. // has_static_relocs for the corresponding symbol.
  7985. case elfcpp::R_MIPS_JALR:
  7986. case elfcpp::R_MICROMIPS_JALR:
  7987. break;
  7988. case elfcpp::R_MIPS_GPREL16:
  7989. case elfcpp::R_MIPS_GPREL32:
  7990. case elfcpp::R_MIPS16_GPREL:
  7991. case elfcpp::R_MICROMIPS_GPREL16:
  7992. // TODO(sasa)
  7993. // GP-relative relocations always resolve to a definition in a
  7994. // regular input file, ignoring the one-definition rule. This is
  7995. // important for the GP setup sequence in NewABI code, which
  7996. // always resolves to a local function even if other relocations
  7997. // against the symbol wouldn't.
  7998. //constrain_symbol_p = FALSE;
  7999. break;
  8000. case elfcpp::R_MIPS_32:
  8001. case elfcpp::R_MIPS_REL32:
  8002. case elfcpp::R_MIPS_64:
  8003. if (parameters->options().shared()
  8004. || strcmp(gsym->name(), "__gnu_local_gp") != 0)
  8005. {
  8006. if (r_type != elfcpp::R_MIPS_REL32)
  8007. {
  8008. static_reloc = true;
  8009. mips_sym->set_pointer_equality_needed();
  8010. }
  8011. can_make_dynamic = true;
  8012. break;
  8013. }
  8014. // Fall through.
  8015. default:
  8016. // Most static relocations require pointer equality, except
  8017. // for branches.
  8018. mips_sym->set_pointer_equality_needed();
  8019. // Fall through.
  8020. case elfcpp::R_MIPS_26:
  8021. case elfcpp::R_MIPS_PC16:
  8022. case elfcpp::R_MIPS16_26:
  8023. case elfcpp::R_MICROMIPS_26_S1:
  8024. case elfcpp::R_MICROMIPS_PC7_S1:
  8025. case elfcpp::R_MICROMIPS_PC10_S1:
  8026. case elfcpp::R_MICROMIPS_PC16_S1:
  8027. case elfcpp::R_MICROMIPS_PC23_S2:
  8028. static_reloc = true;
  8029. mips_sym->set_has_static_relocs();
  8030. break;
  8031. }
  8032. // If there are call relocations against an externally-defined symbol,
  8033. // see whether we can create a MIPS lazy-binding stub for it. We can
  8034. // only do this if all references to the function are through call
  8035. // relocations, and in that case, the traditional lazy-binding stubs
  8036. // are much more efficient than PLT entries.
  8037. switch (r_type)
  8038. {
  8039. case elfcpp::R_MIPS16_CALL16:
  8040. case elfcpp::R_MIPS_CALL16:
  8041. case elfcpp::R_MIPS_CALL_HI16:
  8042. case elfcpp::R_MIPS_CALL_LO16:
  8043. case elfcpp::R_MIPS_JALR:
  8044. case elfcpp::R_MICROMIPS_CALL16:
  8045. case elfcpp::R_MICROMIPS_CALL_HI16:
  8046. case elfcpp::R_MICROMIPS_CALL_LO16:
  8047. case elfcpp::R_MICROMIPS_JALR:
  8048. if (!mips_sym->no_lazy_stub())
  8049. {
  8050. if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
  8051. // Calls from shared objects to undefined symbols of type
  8052. // STT_NOTYPE need lazy-binding stub.
  8053. || (mips_sym->is_undefined() && parameters->options().shared()))
  8054. target->mips_stubs_section(layout)->make_entry(mips_sym);
  8055. }
  8056. break;
  8057. default:
  8058. {
  8059. // We must not create a stub for a symbol that has relocations
  8060. // related to taking the function's address.
  8061. mips_sym->set_no_lazy_stub();
  8062. target->remove_lazy_stub_entry(mips_sym);
  8063. break;
  8064. }
  8065. }
  8066. if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
  8067. mips_sym->is_mips16()))
  8068. mips_sym->set_has_nonpic_branches();
  8069. // R_MIPS_HI16 against _gp_disp is used for $gp setup,
  8070. // and has a special meaning.
  8071. bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
  8072. && strcmp(gsym->name(), "_gp_disp") == 0
  8073. && (hi16_reloc(r_type) || lo16_reloc(r_type)));
  8074. if (static_reloc && gsym->needs_plt_entry())
  8075. {
  8076. target->make_plt_entry(symtab, layout, mips_sym, r_type);
  8077. // Since this is not a PC-relative relocation, we may be
  8078. // taking the address of a function. In that case we need to
  8079. // set the entry in the dynamic symbol table to the address of
  8080. // the PLT entry.
  8081. if (gsym->is_from_dynobj() && !parameters->options().shared())
  8082. {
  8083. gsym->set_needs_dynsym_value();
  8084. // We distinguish between PLT entries and lazy-binding stubs by
  8085. // giving the former an st_other value of STO_MIPS_PLT. Set the
  8086. // flag if there are any relocations in the binary where pointer
  8087. // equality matters.
  8088. if (mips_sym->pointer_equality_needed())
  8089. mips_sym->set_mips_plt();
  8090. }
  8091. }
  8092. if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
  8093. {
  8094. // Absolute addressing relocations.
  8095. // Make a dynamic relocation if necessary.
  8096. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  8097. {
  8098. if (gsym->may_need_copy_reloc())
  8099. {
  8100. target->copy_reloc(symtab, layout, object,
  8101. data_shndx, output_section, gsym, *rel);
  8102. }
  8103. else if (can_make_dynamic)
  8104. {
  8105. // Create .rel.dyn section.
  8106. target->rel_dyn_section(layout);
  8107. target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
  8108. data_shndx, output_section, r_offset);
  8109. }
  8110. else
  8111. gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
  8112. gsym->name());
  8113. }
  8114. }
  8115. bool for_call = false;
  8116. switch (r_type)
  8117. {
  8118. case elfcpp::R_MIPS_CALL16:
  8119. case elfcpp::R_MIPS16_CALL16:
  8120. case elfcpp::R_MICROMIPS_CALL16:
  8121. case elfcpp::R_MIPS_CALL_HI16:
  8122. case elfcpp::R_MIPS_CALL_LO16:
  8123. case elfcpp::R_MICROMIPS_CALL_HI16:
  8124. case elfcpp::R_MICROMIPS_CALL_LO16:
  8125. for_call = true;
  8126. // Fall through.
  8127. case elfcpp::R_MIPS16_GOT16:
  8128. case elfcpp::R_MIPS_GOT16:
  8129. case elfcpp::R_MIPS_GOT_HI16:
  8130. case elfcpp::R_MIPS_GOT_LO16:
  8131. case elfcpp::R_MICROMIPS_GOT16:
  8132. case elfcpp::R_MICROMIPS_GOT_HI16:
  8133. case elfcpp::R_MICROMIPS_GOT_LO16:
  8134. case elfcpp::R_MIPS_GOT_DISP:
  8135. case elfcpp::R_MICROMIPS_GOT_DISP:
  8136. {
  8137. // The symbol requires a GOT entry.
  8138. Mips_output_data_got<size, big_endian>* got =
  8139. target->got_section(symtab, layout);
  8140. got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
  8141. for_call);
  8142. mips_sym->set_global_got_area(GGA_NORMAL);
  8143. }
  8144. break;
  8145. case elfcpp::R_MIPS_GOT_PAGE:
  8146. case elfcpp::R_MICROMIPS_GOT_PAGE:
  8147. {
  8148. // This relocation needs a page entry in the GOT.
  8149. // Get the section contents.
  8150. section_size_type view_size = 0;
  8151. const unsigned char* view =
  8152. object->section_contents(data_shndx, &view_size, false);
  8153. view += r_offset;
  8154. Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
  8155. Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
  8156. : r_addend);
  8157. Mips_output_data_got<size, big_endian>* got =
  8158. target->got_section(symtab, layout);
  8159. got->record_got_page_entry(mips_obj, r_sym, addend);
  8160. // If this is a global, overridable symbol, GOT_PAGE will
  8161. // decay to GOT_DISP, so we'll need a GOT entry for it.
  8162. bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
  8163. && !mips_sym->object()->is_dynamic()
  8164. && !mips_sym->is_undefined());
  8165. if (!def_regular
  8166. || (parameters->options().output_is_position_independent()
  8167. && !parameters->options().Bsymbolic()
  8168. && !mips_sym->is_forced_local()))
  8169. {
  8170. got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
  8171. for_call);
  8172. mips_sym->set_global_got_area(GGA_NORMAL);
  8173. }
  8174. }
  8175. break;
  8176. case elfcpp::R_MIPS_TLS_GOTTPREL:
  8177. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  8178. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  8179. case elfcpp::R_MIPS_TLS_LDM:
  8180. case elfcpp::R_MIPS16_TLS_LDM:
  8181. case elfcpp::R_MICROMIPS_TLS_LDM:
  8182. case elfcpp::R_MIPS_TLS_GD:
  8183. case elfcpp::R_MIPS16_TLS_GD:
  8184. case elfcpp::R_MICROMIPS_TLS_GD:
  8185. {
  8186. const bool is_final = gsym->final_value_is_known();
  8187. const tls::Tls_optimization optimized_type =
  8188. Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
  8189. switch (r_type)
  8190. {
  8191. case elfcpp::R_MIPS_TLS_GD:
  8192. case elfcpp::R_MIPS16_TLS_GD:
  8193. case elfcpp::R_MICROMIPS_TLS_GD:
  8194. if (optimized_type == tls::TLSOPT_NONE)
  8195. {
  8196. // Create a pair of GOT entries for the module index and
  8197. // dtv-relative offset.
  8198. Mips_output_data_got<size, big_endian>* got =
  8199. target->got_section(symtab, layout);
  8200. got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
  8201. false);
  8202. }
  8203. else
  8204. {
  8205. // FIXME: TLS optimization not supported yet.
  8206. gold_unreachable();
  8207. }
  8208. break;
  8209. case elfcpp::R_MIPS_TLS_LDM:
  8210. case elfcpp::R_MIPS16_TLS_LDM:
  8211. case elfcpp::R_MICROMIPS_TLS_LDM:
  8212. if (optimized_type == tls::TLSOPT_NONE)
  8213. {
  8214. // We always record LDM symbols as local with index 0.
  8215. target->got_section()->record_local_got_symbol(mips_obj, 0,
  8216. r_addend, r_type,
  8217. -1U);
  8218. }
  8219. else
  8220. {
  8221. // FIXME: TLS optimization not supported yet.
  8222. gold_unreachable();
  8223. }
  8224. break;
  8225. case elfcpp::R_MIPS_TLS_GOTTPREL:
  8226. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  8227. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  8228. layout->set_has_static_tls();
  8229. if (optimized_type == tls::TLSOPT_NONE)
  8230. {
  8231. // Create a GOT entry for the tp-relative offset.
  8232. Mips_output_data_got<size, big_endian>* got =
  8233. target->got_section(symtab, layout);
  8234. got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
  8235. false);
  8236. }
  8237. else
  8238. {
  8239. // FIXME: TLS optimization not supported yet.
  8240. gold_unreachable();
  8241. }
  8242. break;
  8243. default:
  8244. gold_unreachable();
  8245. }
  8246. }
  8247. break;
  8248. case elfcpp::R_MIPS_COPY:
  8249. case elfcpp::R_MIPS_JUMP_SLOT:
  8250. // These are relocations which should only be seen by the
  8251. // dynamic linker, and should never be seen here.
  8252. gold_error(_("%s: unexpected reloc %u in object file"),
  8253. object->name().c_str(), r_type);
  8254. break;
  8255. default:
  8256. break;
  8257. }
  8258. // Refuse some position-dependent relocations when creating a
  8259. // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
  8260. // not PIC, but we can create dynamic relocations and the result
  8261. // will be fine. Also do not refuse R_MIPS_LO16, which can be
  8262. // combined with R_MIPS_GOT16.
  8263. if (parameters->options().shared())
  8264. {
  8265. switch (r_type)
  8266. {
  8267. case elfcpp::R_MIPS16_HI16:
  8268. case elfcpp::R_MIPS_HI16:
  8269. case elfcpp::R_MICROMIPS_HI16:
  8270. // Don't refuse a high part relocation if it's against
  8271. // no symbol (e.g. part of a compound relocation).
  8272. if (r_sym == 0)
  8273. break;
  8274. // R_MIPS_HI16 against _gp_disp is used for $gp setup,
  8275. // and has a special meaning.
  8276. if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
  8277. break;
  8278. // FALLTHROUGH
  8279. case elfcpp::R_MIPS16_26:
  8280. case elfcpp::R_MIPS_26:
  8281. case elfcpp::R_MICROMIPS_26_S1:
  8282. gold_error(_("%s: relocation %u against `%s' can not be used when "
  8283. "making a shared object; recompile with -fPIC"),
  8284. object->name().c_str(), r_type, gsym->name());
  8285. default:
  8286. break;
  8287. }
  8288. }
  8289. }
  8290. template<int size, bool big_endian>
  8291. inline void
  8292. Target_mips<size, big_endian>::Scan::global(
  8293. Symbol_table* symtab,
  8294. Layout* layout,
  8295. Target_mips<size, big_endian>* target,
  8296. Sized_relobj_file<size, big_endian>* object,
  8297. unsigned int data_shndx,
  8298. Output_section* output_section,
  8299. const elfcpp::Rela<size, big_endian>& reloc,
  8300. unsigned int r_type,
  8301. Symbol* gsym)
  8302. {
  8303. global(
  8304. symtab,
  8305. layout,
  8306. target,
  8307. object,
  8308. data_shndx,
  8309. output_section,
  8310. &reloc,
  8311. (const elfcpp::Rel<size, big_endian>*) NULL,
  8312. elfcpp::SHT_RELA,
  8313. r_type,
  8314. gsym);
  8315. }
  8316. template<int size, bool big_endian>
  8317. inline void
  8318. Target_mips<size, big_endian>::Scan::global(
  8319. Symbol_table* symtab,
  8320. Layout* layout,
  8321. Target_mips<size, big_endian>* target,
  8322. Sized_relobj_file<size, big_endian>* object,
  8323. unsigned int data_shndx,
  8324. Output_section* output_section,
  8325. const elfcpp::Rel<size, big_endian>& reloc,
  8326. unsigned int r_type,
  8327. Symbol* gsym)
  8328. {
  8329. global(
  8330. symtab,
  8331. layout,
  8332. target,
  8333. object,
  8334. data_shndx,
  8335. output_section,
  8336. (const elfcpp::Rela<size, big_endian>*) NULL,
  8337. &reloc,
  8338. elfcpp::SHT_REL,
  8339. r_type,
  8340. gsym);
  8341. }
  8342. // Return whether a R_MIPS_32 relocation needs to be applied.
  8343. template<int size, bool big_endian>
  8344. inline bool
  8345. Target_mips<size, big_endian>::Relocate::should_apply_r_mips_32_reloc(
  8346. const Mips_symbol<size>* gsym,
  8347. unsigned int r_type,
  8348. Output_section* output_section,
  8349. Target_mips* target)
  8350. {
  8351. // If the output section is not allocated, then we didn't call
  8352. // scan_relocs, we didn't create a dynamic reloc, and we must apply
  8353. // the reloc here.
  8354. if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
  8355. return true;
  8356. if (gsym == NULL)
  8357. return true;
  8358. else
  8359. {
  8360. // For global symbols, we use the same helper routines used in the
  8361. // scan pass.
  8362. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
  8363. && !gsym->may_need_copy_reloc())
  8364. {
  8365. // We have generated dynamic reloc (R_MIPS_REL32).
  8366. bool multi_got = false;
  8367. if (target->has_got_section())
  8368. multi_got = target->got_section()->multi_got();
  8369. bool has_got_offset;
  8370. if (!multi_got)
  8371. has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
  8372. else
  8373. has_got_offset = gsym->global_gotoffset() != -1U;
  8374. if (!has_got_offset)
  8375. return true;
  8376. else
  8377. // Apply the relocation only if the symbol is in the local got.
  8378. // Do not apply the relocation if the symbol is in the global
  8379. // got.
  8380. return symbol_references_local(gsym, gsym->has_dynsym_index());
  8381. }
  8382. else
  8383. // We have not generated dynamic reloc.
  8384. return true;
  8385. }
  8386. }
  8387. // Perform a relocation.
  8388. template<int size, bool big_endian>
  8389. inline bool
  8390. Target_mips<size, big_endian>::Relocate::relocate(
  8391. const Relocate_info<size, big_endian>* relinfo,
  8392. Target_mips* target,
  8393. Output_section* output_section,
  8394. size_t relnum,
  8395. const elfcpp::Rela<size, big_endian>* rela,
  8396. const elfcpp::Rel<size, big_endian>* rel,
  8397. unsigned int rel_type,
  8398. unsigned int r_type,
  8399. const Sized_symbol<size>* gsym,
  8400. const Symbol_value<size>* psymval,
  8401. unsigned char* view,
  8402. Mips_address address,
  8403. section_size_type)
  8404. {
  8405. Mips_address r_offset;
  8406. typename elfcpp::Elf_types<size>::Elf_WXword r_info;
  8407. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
  8408. if (rel_type == elfcpp::SHT_RELA)
  8409. {
  8410. r_offset = rela->get_r_offset();
  8411. r_info = rela->get_r_info();
  8412. r_addend = rela->get_r_addend();
  8413. }
  8414. else
  8415. {
  8416. r_offset = rel->get_r_offset();
  8417. r_info = rel->get_r_info();
  8418. r_addend = 0;
  8419. }
  8420. typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
  8421. typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
  8422. Mips_relobj<size, big_endian>* object =
  8423. Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
  8424. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  8425. bool target_is_16_bit_code = false;
  8426. bool target_is_micromips_code = false;
  8427. bool cross_mode_jump;
  8428. Symbol_value<size> symval;
  8429. const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
  8430. bool changed_symbol_value = false;
  8431. if (gsym == NULL)
  8432. {
  8433. target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
  8434. target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
  8435. if (target_is_16_bit_code || target_is_micromips_code)
  8436. {
  8437. // MIPS16/microMIPS text labels should be treated as odd.
  8438. symval.set_output_value(psymval->value(object, 1));
  8439. psymval = &symval;
  8440. changed_symbol_value = true;
  8441. }
  8442. }
  8443. else
  8444. {
  8445. target_is_16_bit_code = mips_sym->is_mips16();
  8446. target_is_micromips_code = mips_sym->is_micromips();
  8447. // If this is a mips16/microMIPS text symbol, add 1 to the value to make
  8448. // it odd. This will cause something like .word SYM to come up with
  8449. // the right value when it is loaded into the PC.
  8450. if ((mips_sym->is_mips16() || mips_sym->is_micromips())
  8451. && psymval->value(object, 0) != 0)
  8452. {
  8453. symval.set_output_value(psymval->value(object, 0) | 1);
  8454. psymval = &symval;
  8455. changed_symbol_value = true;
  8456. }
  8457. // Pick the value to use for symbols defined in shared objects.
  8458. if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
  8459. || mips_sym->has_lazy_stub())
  8460. {
  8461. Mips_address value;
  8462. if (!mips_sym->has_lazy_stub())
  8463. {
  8464. // Prefer a standard MIPS PLT entry.
  8465. if (mips_sym->has_mips_plt_offset())
  8466. {
  8467. value = target->plt_section()->mips_entry_address(mips_sym);
  8468. target_is_micromips_code = false;
  8469. target_is_16_bit_code = false;
  8470. }
  8471. else
  8472. {
  8473. value = (target->plt_section()->comp_entry_address(mips_sym)
  8474. + 1);
  8475. if (target->is_output_micromips())
  8476. target_is_micromips_code = true;
  8477. else
  8478. target_is_16_bit_code = true;
  8479. }
  8480. }
  8481. else
  8482. value = target->mips_stubs_section()->stub_address(mips_sym);
  8483. symval.set_output_value(value);
  8484. psymval = &symval;
  8485. }
  8486. }
  8487. // TRUE if the symbol referred to by this relocation is "_gp_disp".
  8488. // Note that such a symbol must always be a global symbol.
  8489. bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
  8490. && !object->is_newabi());
  8491. // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
  8492. // Note that such a symbol must always be a global symbol.
  8493. bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
  8494. if (gp_disp)
  8495. {
  8496. if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
  8497. gold_error_at_location(relinfo, relnum, r_offset,
  8498. _("relocations against _gp_disp are permitted only"
  8499. " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
  8500. }
  8501. else if (gnu_local_gp)
  8502. {
  8503. // __gnu_local_gp is _gp symbol.
  8504. symval.set_output_value(target->adjusted_gp_value(object));
  8505. psymval = &symval;
  8506. }
  8507. // If this is a reference to a 16-bit function with a stub, we need
  8508. // to redirect the relocation to the stub unless:
  8509. //
  8510. // (a) the relocation is for a MIPS16 JAL;
  8511. //
  8512. // (b) the relocation is for a MIPS16 PIC call, and there are no
  8513. // non-MIPS16 uses of the GOT slot; or
  8514. //
  8515. // (c) the section allows direct references to MIPS16 functions.
  8516. if (r_type != elfcpp::R_MIPS16_26
  8517. && !parameters->options().relocatable()
  8518. && ((mips_sym != NULL
  8519. && mips_sym->has_mips16_fn_stub()
  8520. && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
  8521. || (mips_sym == NULL
  8522. && object->get_local_mips16_fn_stub(r_sym) != NULL))
  8523. && !object->section_allows_mips16_refs(relinfo->data_shndx))
  8524. {
  8525. // This is a 32- or 64-bit call to a 16-bit function. We should
  8526. // have already noticed that we were going to need the
  8527. // stub.
  8528. Mips_address value;
  8529. if (mips_sym == NULL)
  8530. value = object->get_local_mips16_fn_stub(r_sym)->output_address();
  8531. else
  8532. {
  8533. gold_assert(mips_sym->need_fn_stub());
  8534. if (mips_sym->has_la25_stub())
  8535. value = target->la25_stub_section()->stub_address(mips_sym);
  8536. else
  8537. {
  8538. value = mips_sym->template
  8539. get_mips16_fn_stub<big_endian>()->output_address();
  8540. }
  8541. }
  8542. symval.set_output_value(value);
  8543. psymval = &symval;
  8544. changed_symbol_value = true;
  8545. // The target is 16-bit, but the stub isn't.
  8546. target_is_16_bit_code = false;
  8547. }
  8548. // If this is a MIPS16 call with a stub, that is made through the PLT or
  8549. // to a standard MIPS function, we need to redirect the call to the stub.
  8550. // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
  8551. // indirect calls should use an indirect stub instead.
  8552. else if (r_type == elfcpp::R_MIPS16_26 && !parameters->options().relocatable()
  8553. && ((mips_sym != NULL
  8554. && (mips_sym->has_mips16_call_stub()
  8555. || mips_sym->has_mips16_call_fp_stub()))
  8556. || (mips_sym == NULL
  8557. && object->get_local_mips16_call_stub(r_sym) != NULL))
  8558. && ((mips_sym != NULL && mips_sym->has_plt_offset())
  8559. || !target_is_16_bit_code))
  8560. {
  8561. Mips16_stub_section<size, big_endian>* call_stub;
  8562. if (mips_sym == NULL)
  8563. call_stub = object->get_local_mips16_call_stub(r_sym);
  8564. else
  8565. {
  8566. // If both call_stub and call_fp_stub are defined, we can figure
  8567. // out which one to use by checking which one appears in the input
  8568. // file.
  8569. if (mips_sym->has_mips16_call_stub()
  8570. && mips_sym->has_mips16_call_fp_stub())
  8571. {
  8572. call_stub = NULL;
  8573. for (unsigned int i = 1; i < object->shnum(); ++i)
  8574. {
  8575. if (object->is_mips16_call_fp_stub_section(i))
  8576. {
  8577. call_stub = mips_sym->template
  8578. get_mips16_call_fp_stub<big_endian>();
  8579. break;
  8580. }
  8581. }
  8582. if (call_stub == NULL)
  8583. call_stub =
  8584. mips_sym->template get_mips16_call_stub<big_endian>();
  8585. }
  8586. else if (mips_sym->has_mips16_call_stub())
  8587. call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
  8588. else
  8589. call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
  8590. }
  8591. symval.set_output_value(call_stub->output_address());
  8592. psymval = &symval;
  8593. changed_symbol_value = true;
  8594. }
  8595. // If this is a direct call to a PIC function, redirect to the
  8596. // non-PIC stub.
  8597. else if (mips_sym != NULL
  8598. && mips_sym->has_la25_stub()
  8599. && relocation_needs_la25_stub<size, big_endian>(
  8600. object, r_type, target_is_16_bit_code))
  8601. {
  8602. Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
  8603. if (mips_sym->is_micromips())
  8604. value += 1;
  8605. symval.set_output_value(value);
  8606. psymval = &symval;
  8607. }
  8608. // For direct MIPS16 and microMIPS calls make sure the compressed PLT
  8609. // entry is used if a standard PLT entry has also been made.
  8610. else if ((r_type == elfcpp::R_MIPS16_26
  8611. || r_type == elfcpp::R_MICROMIPS_26_S1)
  8612. && !parameters->options().relocatable()
  8613. && mips_sym != NULL
  8614. && mips_sym->has_plt_offset()
  8615. && mips_sym->has_comp_plt_offset()
  8616. && mips_sym->has_mips_plt_offset())
  8617. {
  8618. Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
  8619. + 1);
  8620. symval.set_output_value(value);
  8621. psymval = &symval;
  8622. target_is_16_bit_code = !target->is_output_micromips();
  8623. target_is_micromips_code = target->is_output_micromips();
  8624. }
  8625. // Make sure MIPS16 and microMIPS are not used together.
  8626. if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
  8627. || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
  8628. {
  8629. gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
  8630. }
  8631. // Calls from 16-bit code to 32-bit code and vice versa require the
  8632. // mode change. However, we can ignore calls to undefined weak symbols,
  8633. // which should never be executed at runtime. This exception is important
  8634. // because the assembly writer may have "known" that any definition of the
  8635. // symbol would be 16-bit code, and that direct jumps were therefore
  8636. // acceptable.
  8637. cross_mode_jump =
  8638. (!parameters->options().relocatable()
  8639. && !(gsym != NULL && gsym->is_weak_undefined())
  8640. && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
  8641. || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
  8642. || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
  8643. && (target_is_16_bit_code || target_is_micromips_code))));
  8644. bool local = (mips_sym == NULL
  8645. || (mips_sym->got_only_for_calls()
  8646. ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
  8647. : symbol_references_local(mips_sym,
  8648. mips_sym->has_dynsym_index())));
  8649. // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
  8650. // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
  8651. // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
  8652. if (got_page_reloc(r_type) && !local)
  8653. r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
  8654. : elfcpp::R_MIPS_GOT_DISP);
  8655. unsigned int got_offset = 0;
  8656. int gp_offset = 0;
  8657. bool update_got_entry = false;
  8658. bool extract_addend = rel_type == elfcpp::SHT_REL;
  8659. switch (r_type)
  8660. {
  8661. case elfcpp::R_MIPS_NONE:
  8662. break;
  8663. case elfcpp::R_MIPS_16:
  8664. reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
  8665. extract_addend, r_type);
  8666. break;
  8667. case elfcpp::R_MIPS_32:
  8668. if (should_apply_r_mips_32_reloc(mips_sym, r_type, output_section,
  8669. target))
  8670. reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
  8671. extract_addend, r_type);
  8672. if (mips_sym != NULL
  8673. && (mips_sym->is_mips16() || mips_sym->is_micromips())
  8674. && mips_sym->global_got_area() == GGA_RELOC_ONLY)
  8675. {
  8676. // If mips_sym->has_mips16_fn_stub() is false, symbol value is
  8677. // already updated by adding +1.
  8678. if (mips_sym->has_mips16_fn_stub())
  8679. {
  8680. gold_assert(mips_sym->need_fn_stub());
  8681. Mips16_stub_section<size, big_endian>* fn_stub =
  8682. mips_sym->template get_mips16_fn_stub<big_endian>();
  8683. symval.set_output_value(fn_stub->output_address());
  8684. psymval = &symval;
  8685. }
  8686. got_offset = mips_sym->global_gotoffset();
  8687. update_got_entry = true;
  8688. }
  8689. break;
  8690. case elfcpp::R_MIPS_REL32:
  8691. gold_unreachable();
  8692. case elfcpp::R_MIPS_PC32:
  8693. reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
  8694. r_addend, extract_addend, r_type);
  8695. break;
  8696. case elfcpp::R_MIPS16_26:
  8697. // The calculation for R_MIPS16_26 is just the same as for an
  8698. // R_MIPS_26. It's only the storage of the relocated field into
  8699. // the output file that's different. So, we just fall through to the
  8700. // R_MIPS_26 case here.
  8701. case elfcpp::R_MIPS_26:
  8702. case elfcpp::R_MICROMIPS_26_S1:
  8703. reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
  8704. gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump, r_type,
  8705. target->jal_to_bal());
  8706. break;
  8707. case elfcpp::R_MIPS_HI16:
  8708. case elfcpp::R_MIPS16_HI16:
  8709. case elfcpp::R_MICROMIPS_HI16:
  8710. reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
  8711. address, gp_disp, r_type, r_sym,
  8712. extract_addend);
  8713. break;
  8714. case elfcpp::R_MIPS_LO16:
  8715. case elfcpp::R_MIPS16_LO16:
  8716. case elfcpp::R_MICROMIPS_LO16:
  8717. case elfcpp::R_MICROMIPS_HI0_LO16:
  8718. reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
  8719. r_addend, extract_addend, address,
  8720. gp_disp, r_type, r_sym);
  8721. break;
  8722. case elfcpp::R_MIPS_LITERAL:
  8723. case elfcpp::R_MICROMIPS_LITERAL:
  8724. // Because we don't merge literal sections, we can handle this
  8725. // just like R_MIPS_GPREL16. In the long run, we should merge
  8726. // shared literals, and then we will need to additional work
  8727. // here.
  8728. // Fall through.
  8729. case elfcpp::R_MIPS_GPREL16:
  8730. case elfcpp::R_MIPS16_GPREL:
  8731. case elfcpp::R_MICROMIPS_GPREL7_S2:
  8732. case elfcpp::R_MICROMIPS_GPREL16:
  8733. reloc_status = Reloc_funcs::relgprel(view, object, psymval,
  8734. target->adjusted_gp_value(object),
  8735. r_addend, extract_addend,
  8736. gsym == NULL, r_type);
  8737. break;
  8738. case elfcpp::R_MIPS_PC16:
  8739. reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
  8740. r_addend, extract_addend, r_type);
  8741. break;
  8742. case elfcpp::R_MICROMIPS_PC7_S1:
  8743. reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
  8744. address, r_addend,
  8745. extract_addend, r_type);
  8746. break;
  8747. case elfcpp::R_MICROMIPS_PC10_S1:
  8748. reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object, psymval,
  8749. address, r_addend,
  8750. extract_addend, r_type);
  8751. break;
  8752. case elfcpp::R_MICROMIPS_PC16_S1:
  8753. reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object, psymval,
  8754. address, r_addend,
  8755. extract_addend, r_type);
  8756. break;
  8757. case elfcpp::R_MIPS_GPREL32:
  8758. reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
  8759. target->adjusted_gp_value(object),
  8760. r_addend, extract_addend, r_type);
  8761. break;
  8762. case elfcpp::R_MIPS_GOT_HI16:
  8763. case elfcpp::R_MIPS_CALL_HI16:
  8764. case elfcpp::R_MICROMIPS_GOT_HI16:
  8765. case elfcpp::R_MICROMIPS_CALL_HI16:
  8766. if (gsym != NULL)
  8767. got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
  8768. object);
  8769. else
  8770. got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD,
  8771. object);
  8772. gp_offset = target->got_section()->gp_offset(got_offset, object);
  8773. reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset, r_type);
  8774. update_got_entry = changed_symbol_value;
  8775. break;
  8776. case elfcpp::R_MIPS_GOT_LO16:
  8777. case elfcpp::R_MIPS_CALL_LO16:
  8778. case elfcpp::R_MICROMIPS_GOT_LO16:
  8779. case elfcpp::R_MICROMIPS_CALL_LO16:
  8780. if (gsym != NULL)
  8781. got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
  8782. object);
  8783. else
  8784. got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD,
  8785. object);
  8786. gp_offset = target->got_section()->gp_offset(got_offset, object);
  8787. reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset, r_type);
  8788. update_got_entry = changed_symbol_value;
  8789. break;
  8790. case elfcpp::R_MIPS_GOT_DISP:
  8791. case elfcpp::R_MICROMIPS_GOT_DISP:
  8792. if (gsym != NULL)
  8793. got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
  8794. object);
  8795. else
  8796. got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD,
  8797. object);
  8798. gp_offset = target->got_section()->gp_offset(got_offset, object);
  8799. reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
  8800. break;
  8801. case elfcpp::R_MIPS_CALL16:
  8802. case elfcpp::R_MIPS16_CALL16:
  8803. case elfcpp::R_MICROMIPS_CALL16:
  8804. gold_assert(gsym != NULL);
  8805. got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
  8806. object);
  8807. gp_offset = target->got_section()->gp_offset(got_offset, object);
  8808. reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
  8809. // TODO(sasa): We should also initialize update_got_entry in other places
  8810. // where relgot is called.
  8811. update_got_entry = changed_symbol_value;
  8812. break;
  8813. case elfcpp::R_MIPS_GOT16:
  8814. case elfcpp::R_MIPS16_GOT16:
  8815. case elfcpp::R_MICROMIPS_GOT16:
  8816. if (gsym != NULL)
  8817. {
  8818. got_offset = target->got_section()->got_offset(gsym,
  8819. GOT_TYPE_STANDARD,
  8820. object);
  8821. gp_offset = target->got_section()->gp_offset(got_offset, object);
  8822. reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
  8823. }
  8824. else
  8825. reloc_status = Reloc_funcs::relgot16_local(view, object, psymval,
  8826. r_addend, extract_addend,
  8827. r_type, r_sym);
  8828. update_got_entry = changed_symbol_value;
  8829. break;
  8830. case elfcpp::R_MIPS_TLS_GD:
  8831. case elfcpp::R_MIPS16_TLS_GD:
  8832. case elfcpp::R_MICROMIPS_TLS_GD:
  8833. if (gsym != NULL)
  8834. got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_TLS_PAIR,
  8835. object);
  8836. else
  8837. got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_TLS_PAIR,
  8838. object);
  8839. gp_offset = target->got_section()->gp_offset(got_offset, object);
  8840. reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
  8841. break;
  8842. case elfcpp::R_MIPS_TLS_GOTTPREL:
  8843. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  8844. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  8845. if (gsym != NULL)
  8846. got_offset = target->got_section()->got_offset(gsym,
  8847. GOT_TYPE_TLS_OFFSET,
  8848. object);
  8849. else
  8850. got_offset = target->got_section()->got_offset(r_sym,
  8851. GOT_TYPE_TLS_OFFSET,
  8852. object);
  8853. gp_offset = target->got_section()->gp_offset(got_offset, object);
  8854. reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
  8855. break;
  8856. case elfcpp::R_MIPS_TLS_LDM:
  8857. case elfcpp::R_MIPS16_TLS_LDM:
  8858. case elfcpp::R_MICROMIPS_TLS_LDM:
  8859. // Relocate the field with the offset of the GOT entry for
  8860. // the module index.
  8861. got_offset = target->got_section()->tls_ldm_offset(object);
  8862. gp_offset = target->got_section()->gp_offset(got_offset, object);
  8863. reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
  8864. break;
  8865. case elfcpp::R_MIPS_GOT_PAGE:
  8866. case elfcpp::R_MICROMIPS_GOT_PAGE:
  8867. reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
  8868. r_addend, extract_addend, r_type);
  8869. break;
  8870. case elfcpp::R_MIPS_GOT_OFST:
  8871. case elfcpp::R_MICROMIPS_GOT_OFST:
  8872. reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
  8873. r_addend, extract_addend, local,
  8874. r_type);
  8875. break;
  8876. case elfcpp::R_MIPS_JALR:
  8877. case elfcpp::R_MICROMIPS_JALR:
  8878. // This relocation is only a hint. In some cases, we optimize
  8879. // it into a bal instruction. But we don't try to optimize
  8880. // when the symbol does not resolve locally.
  8881. if (gsym == NULL || symbol_calls_local(gsym, gsym->has_dynsym_index()))
  8882. reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
  8883. r_addend, extract_addend,
  8884. cross_mode_jump, r_type,
  8885. target->jalr_to_bal(),
  8886. target->jr_to_b());
  8887. break;
  8888. case elfcpp::R_MIPS_TLS_DTPREL_HI16:
  8889. case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
  8890. case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
  8891. reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
  8892. elfcpp::DTP_OFFSET, r_addend,
  8893. extract_addend, r_type);
  8894. break;
  8895. case elfcpp::R_MIPS_TLS_DTPREL_LO16:
  8896. case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
  8897. case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
  8898. reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
  8899. elfcpp::DTP_OFFSET, r_addend,
  8900. extract_addend, r_type);
  8901. break;
  8902. case elfcpp::R_MIPS_TLS_DTPREL32:
  8903. case elfcpp::R_MIPS_TLS_DTPREL64:
  8904. reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
  8905. elfcpp::DTP_OFFSET, r_addend,
  8906. extract_addend, r_type);
  8907. break;
  8908. case elfcpp::R_MIPS_TLS_TPREL_HI16:
  8909. case elfcpp::R_MIPS16_TLS_TPREL_HI16:
  8910. case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
  8911. reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
  8912. elfcpp::TP_OFFSET, r_addend,
  8913. extract_addend, r_type);
  8914. break;
  8915. case elfcpp::R_MIPS_TLS_TPREL_LO16:
  8916. case elfcpp::R_MIPS16_TLS_TPREL_LO16:
  8917. case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
  8918. reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
  8919. elfcpp::TP_OFFSET, r_addend,
  8920. extract_addend, r_type);
  8921. break;
  8922. case elfcpp::R_MIPS_TLS_TPREL32:
  8923. case elfcpp::R_MIPS_TLS_TPREL64:
  8924. reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
  8925. elfcpp::TP_OFFSET, r_addend,
  8926. extract_addend, r_type);
  8927. break;
  8928. case elfcpp::R_MIPS_SUB:
  8929. case elfcpp::R_MICROMIPS_SUB:
  8930. reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
  8931. extract_addend, r_type);
  8932. break;
  8933. default:
  8934. gold_error_at_location(relinfo, relnum, r_offset,
  8935. _("unsupported reloc %u"), r_type);
  8936. break;
  8937. }
  8938. if (update_got_entry)
  8939. {
  8940. Mips_output_data_got<size, big_endian>* got = target->got_section();
  8941. if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
  8942. got->update_got_entry(got->get_primary_got_offset(mips_sym),
  8943. psymval->value(object, 0));
  8944. else
  8945. got->update_got_entry(got_offset, psymval->value(object, 0));
  8946. }
  8947. // Report any errors.
  8948. switch (reloc_status)
  8949. {
  8950. case Reloc_funcs::STATUS_OKAY:
  8951. break;
  8952. case Reloc_funcs::STATUS_OVERFLOW:
  8953. gold_error_at_location(relinfo, relnum, r_offset,
  8954. _("relocation overflow"));
  8955. break;
  8956. case Reloc_funcs::STATUS_BAD_RELOC:
  8957. gold_error_at_location(relinfo, relnum, r_offset,
  8958. _("unexpected opcode while processing relocation"));
  8959. break;
  8960. default:
  8961. gold_unreachable();
  8962. }
  8963. return true;
  8964. }
  8965. template<int size, bool big_endian>
  8966. inline bool
  8967. Target_mips<size, big_endian>::Relocate::relocate(
  8968. const Relocate_info<size, big_endian>* relinfo,
  8969. Target_mips* target,
  8970. Output_section* output_section,
  8971. size_t relnum,
  8972. const elfcpp::Rela<size, big_endian>& reloc,
  8973. unsigned int r_type,
  8974. const Sized_symbol<size>* gsym,
  8975. const Symbol_value<size>* psymval,
  8976. unsigned char* view,
  8977. Mips_address address,
  8978. section_size_type view_size)
  8979. {
  8980. return relocate(
  8981. relinfo,
  8982. target,
  8983. output_section,
  8984. relnum,
  8985. &reloc,
  8986. (const elfcpp::Rel<size, big_endian>*) NULL,
  8987. elfcpp::SHT_RELA,
  8988. r_type,
  8989. gsym,
  8990. psymval,
  8991. view,
  8992. address,
  8993. view_size);
  8994. }
  8995. template<int size, bool big_endian>
  8996. inline bool
  8997. Target_mips<size, big_endian>::Relocate::relocate(
  8998. const Relocate_info<size, big_endian>* relinfo,
  8999. Target_mips* target,
  9000. Output_section* output_section,
  9001. size_t relnum,
  9002. const elfcpp::Rel<size, big_endian>& reloc,
  9003. unsigned int r_type,
  9004. const Sized_symbol<size>* gsym,
  9005. const Symbol_value<size>* psymval,
  9006. unsigned char* view,
  9007. Mips_address address,
  9008. section_size_type view_size)
  9009. {
  9010. return relocate(
  9011. relinfo,
  9012. target,
  9013. output_section,
  9014. relnum,
  9015. (const elfcpp::Rela<size, big_endian>*) NULL,
  9016. &reloc,
  9017. elfcpp::SHT_REL,
  9018. r_type,
  9019. gsym,
  9020. psymval,
  9021. view,
  9022. address,
  9023. view_size);
  9024. }
  9025. // Get the Reference_flags for a particular relocation.
  9026. template<int size, bool big_endian>
  9027. int
  9028. Target_mips<size, big_endian>::Scan::get_reference_flags(
  9029. unsigned int r_type)
  9030. {
  9031. switch (r_type)
  9032. {
  9033. case elfcpp::R_MIPS_NONE:
  9034. // No symbol reference.
  9035. return 0;
  9036. case elfcpp::R_MIPS_16:
  9037. case elfcpp::R_MIPS_32:
  9038. case elfcpp::R_MIPS_64:
  9039. case elfcpp::R_MIPS_HI16:
  9040. case elfcpp::R_MIPS_LO16:
  9041. case elfcpp::R_MIPS16_HI16:
  9042. case elfcpp::R_MIPS16_LO16:
  9043. case elfcpp::R_MICROMIPS_HI16:
  9044. case elfcpp::R_MICROMIPS_LO16:
  9045. return Symbol::ABSOLUTE_REF;
  9046. case elfcpp::R_MIPS_26:
  9047. case elfcpp::R_MIPS16_26:
  9048. case elfcpp::R_MICROMIPS_26_S1:
  9049. return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
  9050. case elfcpp::R_MIPS_GPREL32:
  9051. case elfcpp::R_MIPS_GPREL16:
  9052. case elfcpp::R_MIPS_REL32:
  9053. case elfcpp::R_MIPS16_GPREL:
  9054. return Symbol::RELATIVE_REF;
  9055. case elfcpp::R_MIPS_PC16:
  9056. case elfcpp::R_MIPS_PC32:
  9057. case elfcpp::R_MIPS_JALR:
  9058. case elfcpp::R_MICROMIPS_JALR:
  9059. return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  9060. case elfcpp::R_MIPS_GOT16:
  9061. case elfcpp::R_MIPS_CALL16:
  9062. case elfcpp::R_MIPS_GOT_DISP:
  9063. case elfcpp::R_MIPS_GOT_HI16:
  9064. case elfcpp::R_MIPS_GOT_LO16:
  9065. case elfcpp::R_MIPS_CALL_HI16:
  9066. case elfcpp::R_MIPS_CALL_LO16:
  9067. case elfcpp::R_MIPS_LITERAL:
  9068. case elfcpp::R_MIPS_GOT_PAGE:
  9069. case elfcpp::R_MIPS_GOT_OFST:
  9070. case elfcpp::R_MIPS16_GOT16:
  9071. case elfcpp::R_MIPS16_CALL16:
  9072. case elfcpp::R_MICROMIPS_GOT16:
  9073. case elfcpp::R_MICROMIPS_CALL16:
  9074. case elfcpp::R_MICROMIPS_GOT_HI16:
  9075. case elfcpp::R_MICROMIPS_GOT_LO16:
  9076. case elfcpp::R_MICROMIPS_CALL_HI16:
  9077. case elfcpp::R_MICROMIPS_CALL_LO16:
  9078. // Absolute in GOT.
  9079. return Symbol::RELATIVE_REF;
  9080. case elfcpp::R_MIPS_TLS_DTPMOD32:
  9081. case elfcpp::R_MIPS_TLS_DTPREL32:
  9082. case elfcpp::R_MIPS_TLS_DTPMOD64:
  9083. case elfcpp::R_MIPS_TLS_DTPREL64:
  9084. case elfcpp::R_MIPS_TLS_GD:
  9085. case elfcpp::R_MIPS_TLS_LDM:
  9086. case elfcpp::R_MIPS_TLS_DTPREL_HI16:
  9087. case elfcpp::R_MIPS_TLS_DTPREL_LO16:
  9088. case elfcpp::R_MIPS_TLS_GOTTPREL:
  9089. case elfcpp::R_MIPS_TLS_TPREL32:
  9090. case elfcpp::R_MIPS_TLS_TPREL64:
  9091. case elfcpp::R_MIPS_TLS_TPREL_HI16:
  9092. case elfcpp::R_MIPS_TLS_TPREL_LO16:
  9093. case elfcpp::R_MIPS16_TLS_GD:
  9094. case elfcpp::R_MIPS16_TLS_GOTTPREL:
  9095. case elfcpp::R_MICROMIPS_TLS_GD:
  9096. case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
  9097. case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
  9098. case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
  9099. return Symbol::TLS_REF;
  9100. case elfcpp::R_MIPS_COPY:
  9101. case elfcpp::R_MIPS_JUMP_SLOT:
  9102. default:
  9103. gold_unreachable();
  9104. // Not expected. We will give an error later.
  9105. return 0;
  9106. }
  9107. }
  9108. // Report an unsupported relocation against a local symbol.
  9109. template<int size, bool big_endian>
  9110. void
  9111. Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
  9112. Sized_relobj_file<size, big_endian>* object,
  9113. unsigned int r_type)
  9114. {
  9115. gold_error(_("%s: unsupported reloc %u against local symbol"),
  9116. object->name().c_str(), r_type);
  9117. }
  9118. // Report an unsupported relocation against a global symbol.
  9119. template<int size, bool big_endian>
  9120. void
  9121. Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
  9122. Sized_relobj_file<size, big_endian>* object,
  9123. unsigned int r_type,
  9124. Symbol* gsym)
  9125. {
  9126. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  9127. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  9128. }
  9129. // Return printable name for ABI.
  9130. template<int size, bool big_endian>
  9131. const char*
  9132. Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags,
  9133. unsigned char ei_class)
  9134. {
  9135. switch (e_flags & elfcpp::EF_MIPS_ABI)
  9136. {
  9137. case 0:
  9138. if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
  9139. return "N32";
  9140. else if (elfcpp::abi_64(ei_class))
  9141. return "64";
  9142. else
  9143. return "none";
  9144. case elfcpp::E_MIPS_ABI_O32:
  9145. return "O32";
  9146. case elfcpp::E_MIPS_ABI_O64:
  9147. return "O64";
  9148. case elfcpp::E_MIPS_ABI_EABI32:
  9149. return "EABI32";
  9150. case elfcpp::E_MIPS_ABI_EABI64:
  9151. return "EABI64";
  9152. default:
  9153. return "unknown abi";
  9154. }
  9155. }
  9156. template<int size, bool big_endian>
  9157. const char*
  9158. Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
  9159. {
  9160. switch (e_flags & elfcpp::EF_MIPS_MACH)
  9161. {
  9162. case elfcpp::E_MIPS_MACH_3900:
  9163. return "mips:3900";
  9164. case elfcpp::E_MIPS_MACH_4010:
  9165. return "mips:4010";
  9166. case elfcpp::E_MIPS_MACH_4100:
  9167. return "mips:4100";
  9168. case elfcpp::E_MIPS_MACH_4111:
  9169. return "mips:4111";
  9170. case elfcpp::E_MIPS_MACH_4120:
  9171. return "mips:4120";
  9172. case elfcpp::E_MIPS_MACH_4650:
  9173. return "mips:4650";
  9174. case elfcpp::E_MIPS_MACH_5400:
  9175. return "mips:5400";
  9176. case elfcpp::E_MIPS_MACH_5500:
  9177. return "mips:5500";
  9178. case elfcpp::E_MIPS_MACH_SB1:
  9179. return "mips:sb1";
  9180. case elfcpp::E_MIPS_MACH_9000:
  9181. return "mips:9000";
  9182. case elfcpp::E_MIPS_MACH_LS2E:
  9183. return "mips:loongson-2e";
  9184. case elfcpp::E_MIPS_MACH_LS2F:
  9185. return "mips:loongson-2f";
  9186. case elfcpp::E_MIPS_MACH_LS3A:
  9187. return "mips:loongson-3a";
  9188. case elfcpp::E_MIPS_MACH_OCTEON:
  9189. return "mips:octeon";
  9190. case elfcpp::E_MIPS_MACH_OCTEON2:
  9191. return "mips:octeon2";
  9192. case elfcpp::E_MIPS_MACH_XLR:
  9193. return "mips:xlr";
  9194. default:
  9195. switch (e_flags & elfcpp::EF_MIPS_ARCH)
  9196. {
  9197. default:
  9198. case elfcpp::E_MIPS_ARCH_1:
  9199. return "mips:3000";
  9200. case elfcpp::E_MIPS_ARCH_2:
  9201. return "mips:6000";
  9202. case elfcpp::E_MIPS_ARCH_3:
  9203. return "mips:4000";
  9204. case elfcpp::E_MIPS_ARCH_4:
  9205. return "mips:8000";
  9206. case elfcpp::E_MIPS_ARCH_5:
  9207. return "mips:mips5";
  9208. case elfcpp::E_MIPS_ARCH_32:
  9209. return "mips:isa32";
  9210. case elfcpp::E_MIPS_ARCH_64:
  9211. return "mips:isa64";
  9212. case elfcpp::E_MIPS_ARCH_32R2:
  9213. return "mips:isa32r2";
  9214. case elfcpp::E_MIPS_ARCH_64R2:
  9215. return "mips:isa64r2";
  9216. }
  9217. }
  9218. return "unknown CPU";
  9219. }
  9220. template<int size, bool big_endian>
  9221. const Target::Target_info Target_mips<size, big_endian>::mips_info =
  9222. {
  9223. size, // size
  9224. big_endian, // is_big_endian
  9225. elfcpp::EM_MIPS, // machine_code
  9226. true, // has_make_symbol
  9227. false, // has_resolve
  9228. false, // has_code_fill
  9229. true, // is_default_stack_executable
  9230. false, // can_icf_inline_merge_sections
  9231. '\0', // wrap_char
  9232. "/lib/ld.so.1", // dynamic_linker
  9233. 0x400000, // default_text_segment_address
  9234. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  9235. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  9236. false, // isolate_execinstr
  9237. 0, // rosegment_gap
  9238. elfcpp::SHN_UNDEF, // small_common_shndx
  9239. elfcpp::SHN_UNDEF, // large_common_shndx
  9240. 0, // small_common_section_flags
  9241. 0, // large_common_section_flags
  9242. NULL, // attributes_section
  9243. NULL, // attributes_vendor
  9244. "__start" // entry_symbol_name
  9245. };
  9246. template<int size, bool big_endian>
  9247. class Target_mips_nacl : public Target_mips<size, big_endian>
  9248. {
  9249. public:
  9250. Target_mips_nacl()
  9251. : Target_mips<size, big_endian>(&mips_nacl_info)
  9252. { }
  9253. private:
  9254. static const Target::Target_info mips_nacl_info;
  9255. };
  9256. template<int size, bool big_endian>
  9257. const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
  9258. {
  9259. size, // size
  9260. big_endian, // is_big_endian
  9261. elfcpp::EM_MIPS, // machine_code
  9262. true, // has_make_symbol
  9263. false, // has_resolve
  9264. false, // has_code_fill
  9265. true, // is_default_stack_executable
  9266. false, // can_icf_inline_merge_sections
  9267. '\0', // wrap_char
  9268. "/lib/ld.so.1", // dynamic_linker
  9269. 0x20000, // default_text_segment_address
  9270. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  9271. 0x10000, // common_pagesize (overridable by -z common-page-size)
  9272. true, // isolate_execinstr
  9273. 0x10000000, // rosegment_gap
  9274. elfcpp::SHN_UNDEF, // small_common_shndx
  9275. elfcpp::SHN_UNDEF, // large_common_shndx
  9276. 0, // small_common_section_flags
  9277. 0, // large_common_section_flags
  9278. NULL, // attributes_section
  9279. NULL, // attributes_vendor
  9280. "_start" // entry_symbol_name
  9281. };
  9282. // Target selector for Mips. Note this is never instantiated directly.
  9283. // It's only used in Target_selector_mips_nacl, below.
  9284. template<int size, bool big_endian>
  9285. class Target_selector_mips : public Target_selector
  9286. {
  9287. public:
  9288. Target_selector_mips()
  9289. : Target_selector(elfcpp::EM_MIPS, size, big_endian,
  9290. (size == 64 ?
  9291. (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
  9292. (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
  9293. (size == 64 ?
  9294. (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
  9295. (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")))
  9296. { }
  9297. Target* do_instantiate_target()
  9298. { return new Target_mips<size, big_endian>(); }
  9299. };
  9300. template<int size, bool big_endian>
  9301. class Target_selector_mips_nacl
  9302. : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
  9303. Target_mips_nacl<size, big_endian> >
  9304. {
  9305. public:
  9306. Target_selector_mips_nacl()
  9307. : Target_selector_nacl<Target_selector_mips<size, big_endian>,
  9308. Target_mips_nacl<size, big_endian> >(
  9309. // NaCl currently supports only MIPS32 little-endian.
  9310. "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
  9311. { }
  9312. };
  9313. Target_selector_mips_nacl<32, true> target_selector_mips32;
  9314. Target_selector_mips_nacl<32, false> target_selector_mips32el;
  9315. Target_selector_mips_nacl<64, true> target_selector_mips64;
  9316. Target_selector_mips_nacl<64, false> target_selector_mips64el;
  9317. } // End anonymous namespace.