elfxx-mips.c 501 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769
  1. /* MIPS-specific support for ELF
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Most of the information added by Ian Lance Taylor, Cygnus Support,
  4. <ian@cygnus.com>.
  5. N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
  6. <mark@codesourcery.com>
  7. Traditional MIPS targets support added by Koundinya.K, Dansk Data
  8. Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
  9. This file is part of BFD, the Binary File Descriptor library.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 3 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  21. MA 02110-1301, USA. */
  22. /* This file handles functionality common to the different MIPS ABI's. */
  23. #include "sysdep.h"
  24. #include "bfd.h"
  25. #include "libbfd.h"
  26. #include "libiberty.h"
  27. #include "elf-bfd.h"
  28. #include "ecoff-bfd.h"
  29. #include "elfxx-mips.h"
  30. #include "elf/mips.h"
  31. #include "elf-vxworks.h"
  32. #include "dwarf2.h"
  33. /* Get the ECOFF swapping routines. */
  34. #include "coff/sym.h"
  35. #include "coff/symconst.h"
  36. #include "coff/ecoff.h"
  37. #include "coff/mips.h"
  38. #include "hashtab.h"
  39. /* Types of TLS GOT entry. */
  40. enum mips_got_tls_type {
  41. GOT_TLS_NONE,
  42. GOT_TLS_GD,
  43. GOT_TLS_LDM,
  44. GOT_TLS_IE
  45. };
  46. /* This structure is used to hold information about one GOT entry.
  47. There are four types of entry:
  48. (1) an absolute address
  49. requires: abfd == NULL
  50. fields: d.address
  51. (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
  52. requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
  53. fields: abfd, symndx, d.addend, tls_type
  54. (3) a SYMBOL address, where SYMBOL is not local to an input bfd
  55. requires: abfd != NULL, symndx == -1
  56. fields: d.h, tls_type
  57. (4) a TLS LDM slot
  58. requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
  59. fields: none; there's only one of these per GOT. */
  60. struct mips_got_entry
  61. {
  62. /* One input bfd that needs the GOT entry. */
  63. bfd *abfd;
  64. /* The index of the symbol, as stored in the relocation r_info, if
  65. we have a local symbol; -1 otherwise. */
  66. long symndx;
  67. union
  68. {
  69. /* If abfd == NULL, an address that must be stored in the got. */
  70. bfd_vma address;
  71. /* If abfd != NULL && symndx != -1, the addend of the relocation
  72. that should be added to the symbol value. */
  73. bfd_vma addend;
  74. /* If abfd != NULL && symndx == -1, the hash table entry
  75. corresponding to a symbol in the GOT. The symbol's entry
  76. is in the local area if h->global_got_area is GGA_NONE,
  77. otherwise it is in the global area. */
  78. struct mips_elf_link_hash_entry *h;
  79. } d;
  80. /* The TLS type of this GOT entry. An LDM GOT entry will be a local
  81. symbol entry with r_symndx == 0. */
  82. unsigned char tls_type;
  83. /* True if we have filled in the GOT contents for a TLS entry,
  84. and created the associated relocations. */
  85. unsigned char tls_initialized;
  86. /* The offset from the beginning of the .got section to the entry
  87. corresponding to this symbol+addend. If it's a global symbol
  88. whose offset is yet to be decided, it's going to be -1. */
  89. long gotidx;
  90. };
  91. /* This structure represents a GOT page reference from an input bfd.
  92. Each instance represents a symbol + ADDEND, where the representation
  93. of the symbol depends on whether it is local to the input bfd.
  94. If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
  95. Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
  96. Page references with SYMNDX >= 0 always become page references
  97. in the output. Page references with SYMNDX < 0 only become page
  98. references if the symbol binds locally; in other cases, the page
  99. reference decays to a global GOT reference. */
  100. struct mips_got_page_ref
  101. {
  102. long symndx;
  103. union
  104. {
  105. struct mips_elf_link_hash_entry *h;
  106. bfd *abfd;
  107. } u;
  108. bfd_vma addend;
  109. };
  110. /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
  111. The structures form a non-overlapping list that is sorted by increasing
  112. MIN_ADDEND. */
  113. struct mips_got_page_range
  114. {
  115. struct mips_got_page_range *next;
  116. bfd_signed_vma min_addend;
  117. bfd_signed_vma max_addend;
  118. };
  119. /* This structure describes the range of addends that are applied to page
  120. relocations against a given section. */
  121. struct mips_got_page_entry
  122. {
  123. /* The section that these entries are based on. */
  124. asection *sec;
  125. /* The ranges for this page entry. */
  126. struct mips_got_page_range *ranges;
  127. /* The maximum number of page entries needed for RANGES. */
  128. bfd_vma num_pages;
  129. };
  130. /* This structure is used to hold .got information when linking. */
  131. struct mips_got_info
  132. {
  133. /* The number of global .got entries. */
  134. unsigned int global_gotno;
  135. /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
  136. unsigned int reloc_only_gotno;
  137. /* The number of .got slots used for TLS. */
  138. unsigned int tls_gotno;
  139. /* The first unused TLS .got entry. Used only during
  140. mips_elf_initialize_tls_index. */
  141. unsigned int tls_assigned_gotno;
  142. /* The number of local .got entries, eventually including page entries. */
  143. unsigned int local_gotno;
  144. /* The maximum number of page entries needed. */
  145. unsigned int page_gotno;
  146. /* The number of relocations needed for the GOT entries. */
  147. unsigned int relocs;
  148. /* The first unused local .got entry. */
  149. unsigned int assigned_low_gotno;
  150. /* The last unused local .got entry. */
  151. unsigned int assigned_high_gotno;
  152. /* A hash table holding members of the got. */
  153. struct htab *got_entries;
  154. /* A hash table holding mips_got_page_ref structures. */
  155. struct htab *got_page_refs;
  156. /* A hash table of mips_got_page_entry structures. */
  157. struct htab *got_page_entries;
  158. /* In multi-got links, a pointer to the next got (err, rather, most
  159. of the time, it points to the previous got). */
  160. struct mips_got_info *next;
  161. };
  162. /* Structure passed when merging bfds' gots. */
  163. struct mips_elf_got_per_bfd_arg
  164. {
  165. /* The output bfd. */
  166. bfd *obfd;
  167. /* The link information. */
  168. struct bfd_link_info *info;
  169. /* A pointer to the primary got, i.e., the one that's going to get
  170. the implicit relocations from DT_MIPS_LOCAL_GOTNO and
  171. DT_MIPS_GOTSYM. */
  172. struct mips_got_info *primary;
  173. /* A non-primary got we're trying to merge with other input bfd's
  174. gots. */
  175. struct mips_got_info *current;
  176. /* The maximum number of got entries that can be addressed with a
  177. 16-bit offset. */
  178. unsigned int max_count;
  179. /* The maximum number of page entries needed by each got. */
  180. unsigned int max_pages;
  181. /* The total number of global entries which will live in the
  182. primary got and be automatically relocated. This includes
  183. those not referenced by the primary GOT but included in
  184. the "master" GOT. */
  185. unsigned int global_count;
  186. };
  187. /* A structure used to pass information to htab_traverse callbacks
  188. when laying out the GOT. */
  189. struct mips_elf_traverse_got_arg
  190. {
  191. struct bfd_link_info *info;
  192. struct mips_got_info *g;
  193. int value;
  194. };
  195. struct _mips_elf_section_data
  196. {
  197. struct bfd_elf_section_data elf;
  198. union
  199. {
  200. bfd_byte *tdata;
  201. } u;
  202. };
  203. #define mips_elf_section_data(sec) \
  204. ((struct _mips_elf_section_data *) elf_section_data (sec))
  205. #define is_mips_elf(bfd) \
  206. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  207. && elf_tdata (bfd) != NULL \
  208. && elf_object_id (bfd) == MIPS_ELF_DATA)
  209. /* The ABI says that every symbol used by dynamic relocations must have
  210. a global GOT entry. Among other things, this provides the dynamic
  211. linker with a free, directly-indexed cache. The GOT can therefore
  212. contain symbols that are not referenced by GOT relocations themselves
  213. (in other words, it may have symbols that are not referenced by things
  214. like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
  215. GOT relocations are less likely to overflow if we put the associated
  216. GOT entries towards the beginning. We therefore divide the global
  217. GOT entries into two areas: "normal" and "reloc-only". Entries in
  218. the first area can be used for both dynamic relocations and GP-relative
  219. accesses, while those in the "reloc-only" area are for dynamic
  220. relocations only.
  221. These GGA_* ("Global GOT Area") values are organised so that lower
  222. values are more general than higher values. Also, non-GGA_NONE
  223. values are ordered by the position of the area in the GOT. */
  224. #define GGA_NORMAL 0
  225. #define GGA_RELOC_ONLY 1
  226. #define GGA_NONE 2
  227. /* Information about a non-PIC interface to a PIC function. There are
  228. two ways of creating these interfaces. The first is to add:
  229. lui $25,%hi(func)
  230. addiu $25,$25,%lo(func)
  231. immediately before a PIC function "func". The second is to add:
  232. lui $25,%hi(func)
  233. j func
  234. addiu $25,$25,%lo(func)
  235. to a separate trampoline section.
  236. Stubs of the first kind go in a new section immediately before the
  237. target function. Stubs of the second kind go in a single section
  238. pointed to by the hash table's "strampoline" field. */
  239. struct mips_elf_la25_stub {
  240. /* The generated section that contains this stub. */
  241. asection *stub_section;
  242. /* The offset of the stub from the start of STUB_SECTION. */
  243. bfd_vma offset;
  244. /* One symbol for the original function. Its location is available
  245. in H->root.root.u.def. */
  246. struct mips_elf_link_hash_entry *h;
  247. };
  248. /* Macros for populating a mips_elf_la25_stub. */
  249. #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
  250. #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
  251. #define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
  252. #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
  253. #define LA25_LUI_MICROMIPS(VAL) \
  254. (0x41b90000 | (VAL)) /* lui t9,VAL */
  255. #define LA25_J_MICROMIPS(VAL) \
  256. (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
  257. #define LA25_ADDIU_MICROMIPS(VAL) \
  258. (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
  259. /* This structure is passed to mips_elf_sort_hash_table_f when sorting
  260. the dynamic symbols. */
  261. struct mips_elf_hash_sort_data
  262. {
  263. /* The symbol in the global GOT with the lowest dynamic symbol table
  264. index. */
  265. struct elf_link_hash_entry *low;
  266. /* The least dynamic symbol table index corresponding to a non-TLS
  267. symbol with a GOT entry. */
  268. bfd_size_type min_got_dynindx;
  269. /* The greatest dynamic symbol table index corresponding to a symbol
  270. with a GOT entry that is not referenced (e.g., a dynamic symbol
  271. with dynamic relocations pointing to it from non-primary GOTs). */
  272. bfd_size_type max_unref_got_dynindx;
  273. /* The greatest dynamic symbol table index corresponding to a local
  274. symbol. */
  275. bfd_size_type max_local_dynindx;
  276. /* The greatest dynamic symbol table index corresponding to an external
  277. symbol without a GOT entry. */
  278. bfd_size_type max_non_got_dynindx;
  279. /* If non-NULL, output BFD for .MIPS.xhash finalization. */
  280. bfd *output_bfd;
  281. /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
  282. real final dynindx. */
  283. bfd_byte *mipsxhash;
  284. };
  285. /* We make up to two PLT entries if needed, one for standard MIPS code
  286. and one for compressed code, either a MIPS16 or microMIPS one. We
  287. keep a separate record of traditional lazy-binding stubs, for easier
  288. processing. */
  289. struct plt_entry
  290. {
  291. /* Traditional SVR4 stub offset, or -1 if none. */
  292. bfd_vma stub_offset;
  293. /* Standard PLT entry offset, or -1 if none. */
  294. bfd_vma mips_offset;
  295. /* Compressed PLT entry offset, or -1 if none. */
  296. bfd_vma comp_offset;
  297. /* The corresponding .got.plt index, or -1 if none. */
  298. bfd_vma gotplt_index;
  299. /* Whether we need a standard PLT entry. */
  300. unsigned int need_mips : 1;
  301. /* Whether we need a compressed PLT entry. */
  302. unsigned int need_comp : 1;
  303. };
  304. /* The MIPS ELF linker needs additional information for each symbol in
  305. the global hash table. */
  306. struct mips_elf_link_hash_entry
  307. {
  308. struct elf_link_hash_entry root;
  309. /* External symbol information. */
  310. EXTR esym;
  311. /* The la25 stub we have created for ths symbol, if any. */
  312. struct mips_elf_la25_stub *la25_stub;
  313. /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
  314. this symbol. */
  315. unsigned int possibly_dynamic_relocs;
  316. /* If there is a stub that 32 bit functions should use to call this
  317. 16 bit function, this points to the section containing the stub. */
  318. asection *fn_stub;
  319. /* If there is a stub that 16 bit functions should use to call this
  320. 32 bit function, this points to the section containing the stub. */
  321. asection *call_stub;
  322. /* This is like the call_stub field, but it is used if the function
  323. being called returns a floating point value. */
  324. asection *call_fp_stub;
  325. /* If non-zero, location in .MIPS.xhash to write real final dynindx. */
  326. bfd_vma mipsxhash_loc;
  327. /* The highest GGA_* value that satisfies all references to this symbol. */
  328. unsigned int global_got_area : 2;
  329. /* True if all GOT relocations against this symbol are for calls. This is
  330. a looser condition than no_fn_stub below, because there may be other
  331. non-call non-GOT relocations against the symbol. */
  332. unsigned int got_only_for_calls : 1;
  333. /* True if one of the relocations described by possibly_dynamic_relocs
  334. is against a readonly section. */
  335. unsigned int readonly_reloc : 1;
  336. /* True if there is a relocation against this symbol that must be
  337. resolved by the static linker (in other words, if the relocation
  338. cannot possibly be made dynamic). */
  339. unsigned int has_static_relocs : 1;
  340. /* True if we must not create a .MIPS.stubs entry for this symbol.
  341. This is set, for example, if there are relocations related to
  342. taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
  343. See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
  344. unsigned int no_fn_stub : 1;
  345. /* Whether we need the fn_stub; this is true if this symbol appears
  346. in any relocs other than a 16 bit call. */
  347. unsigned int need_fn_stub : 1;
  348. /* True if this symbol is referenced by branch relocations from
  349. any non-PIC input file. This is used to determine whether an
  350. la25 stub is required. */
  351. unsigned int has_nonpic_branches : 1;
  352. /* Does this symbol need a traditional MIPS lazy-binding stub
  353. (as opposed to a PLT entry)? */
  354. unsigned int needs_lazy_stub : 1;
  355. /* Does this symbol resolve to a PLT entry? */
  356. unsigned int use_plt_entry : 1;
  357. };
  358. /* MIPS ELF linker hash table. */
  359. struct mips_elf_link_hash_table
  360. {
  361. struct elf_link_hash_table root;
  362. /* The number of .rtproc entries. */
  363. bfd_size_type procedure_count;
  364. /* The size of the .compact_rel section (if SGI_COMPAT). */
  365. bfd_size_type compact_rel_size;
  366. /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
  367. is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
  368. bool use_rld_obj_head;
  369. /* The __rld_map or __rld_obj_head symbol. */
  370. struct elf_link_hash_entry *rld_symbol;
  371. /* This is set if we see any mips16 stub sections. */
  372. bool mips16_stubs_seen;
  373. /* True if we can generate copy relocs and PLTs. */
  374. bool use_plts_and_copy_relocs;
  375. /* True if we can only use 32-bit microMIPS instructions. */
  376. bool insn32;
  377. /* True if we suppress checks for invalid branches between ISA modes. */
  378. bool ignore_branch_isa;
  379. /* True if we are targetting R6 compact branches. */
  380. bool compact_branches;
  381. /* True if we already reported the small-data section overflow. */
  382. bool small_data_overflow_reported;
  383. /* True if we use the special `__gnu_absolute_zero' symbol. */
  384. bool use_absolute_zero;
  385. /* True if we have been configured for a GNU target. */
  386. bool gnu_target;
  387. /* Shortcuts to some dynamic sections, or NULL if they are not
  388. being used. */
  389. asection *srelplt2;
  390. asection *sstubs;
  391. /* The master GOT information. */
  392. struct mips_got_info *got_info;
  393. /* The global symbol in the GOT with the lowest index in the dynamic
  394. symbol table. */
  395. struct elf_link_hash_entry *global_gotsym;
  396. /* The size of the PLT header in bytes. */
  397. bfd_vma plt_header_size;
  398. /* The size of a standard PLT entry in bytes. */
  399. bfd_vma plt_mips_entry_size;
  400. /* The size of a compressed PLT entry in bytes. */
  401. bfd_vma plt_comp_entry_size;
  402. /* The offset of the next standard PLT entry to create. */
  403. bfd_vma plt_mips_offset;
  404. /* The offset of the next compressed PLT entry to create. */
  405. bfd_vma plt_comp_offset;
  406. /* The index of the next .got.plt entry to create. */
  407. bfd_vma plt_got_index;
  408. /* The number of functions that need a lazy-binding stub. */
  409. bfd_vma lazy_stub_count;
  410. /* The size of a function stub entry in bytes. */
  411. bfd_vma function_stub_size;
  412. /* The number of reserved entries at the beginning of the GOT. */
  413. unsigned int reserved_gotno;
  414. /* The section used for mips_elf_la25_stub trampolines.
  415. See the comment above that structure for details. */
  416. asection *strampoline;
  417. /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
  418. pairs. */
  419. htab_t la25_stubs;
  420. /* A function FN (NAME, IS, OS) that creates a new input section
  421. called NAME and links it to output section OS. If IS is nonnull,
  422. the new section should go immediately before it, otherwise it
  423. should go at the (current) beginning of OS.
  424. The function returns the new section on success, otherwise it
  425. returns null. */
  426. asection *(*add_stub_section) (const char *, asection *, asection *);
  427. /* Is the PLT header compressed? */
  428. unsigned int plt_header_is_comp : 1;
  429. };
  430. /* Get the MIPS ELF linker hash table from a link_info structure. */
  431. #define mips_elf_hash_table(p) \
  432. ((is_elf_hash_table ((p)->hash) \
  433. && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA) \
  434. ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
  435. /* A structure used to communicate with htab_traverse callbacks. */
  436. struct mips_htab_traverse_info
  437. {
  438. /* The usual link-wide information. */
  439. struct bfd_link_info *info;
  440. bfd *output_bfd;
  441. /* Starts off FALSE and is set to TRUE if the link should be aborted. */
  442. bool error;
  443. };
  444. /* MIPS ELF private object data. */
  445. struct mips_elf_obj_tdata
  446. {
  447. /* Generic ELF private object data. */
  448. struct elf_obj_tdata root;
  449. /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
  450. bfd *abi_fp_bfd;
  451. /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
  452. bfd *abi_msa_bfd;
  453. /* The abiflags for this object. */
  454. Elf_Internal_ABIFlags_v0 abiflags;
  455. bool abiflags_valid;
  456. /* The GOT requirements of input bfds. */
  457. struct mips_got_info *got;
  458. /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
  459. included directly in this one, but there's no point to wasting
  460. the memory just for the infrequently called find_nearest_line. */
  461. struct mips_elf_find_line *find_line_info;
  462. /* An array of stub sections indexed by symbol number. */
  463. asection **local_stubs;
  464. asection **local_call_stubs;
  465. /* The Irix 5 support uses two virtual sections, which represent
  466. text/data symbols defined in dynamic objects. */
  467. asymbol *elf_data_symbol;
  468. asymbol *elf_text_symbol;
  469. asection *elf_data_section;
  470. asection *elf_text_section;
  471. };
  472. /* Get MIPS ELF private object data from BFD's tdata. */
  473. #define mips_elf_tdata(bfd) \
  474. ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
  475. #define TLS_RELOC_P(r_type) \
  476. (r_type == R_MIPS_TLS_DTPMOD32 \
  477. || r_type == R_MIPS_TLS_DTPMOD64 \
  478. || r_type == R_MIPS_TLS_DTPREL32 \
  479. || r_type == R_MIPS_TLS_DTPREL64 \
  480. || r_type == R_MIPS_TLS_GD \
  481. || r_type == R_MIPS_TLS_LDM \
  482. || r_type == R_MIPS_TLS_DTPREL_HI16 \
  483. || r_type == R_MIPS_TLS_DTPREL_LO16 \
  484. || r_type == R_MIPS_TLS_GOTTPREL \
  485. || r_type == R_MIPS_TLS_TPREL32 \
  486. || r_type == R_MIPS_TLS_TPREL64 \
  487. || r_type == R_MIPS_TLS_TPREL_HI16 \
  488. || r_type == R_MIPS_TLS_TPREL_LO16 \
  489. || r_type == R_MIPS16_TLS_GD \
  490. || r_type == R_MIPS16_TLS_LDM \
  491. || r_type == R_MIPS16_TLS_DTPREL_HI16 \
  492. || r_type == R_MIPS16_TLS_DTPREL_LO16 \
  493. || r_type == R_MIPS16_TLS_GOTTPREL \
  494. || r_type == R_MIPS16_TLS_TPREL_HI16 \
  495. || r_type == R_MIPS16_TLS_TPREL_LO16 \
  496. || r_type == R_MICROMIPS_TLS_GD \
  497. || r_type == R_MICROMIPS_TLS_LDM \
  498. || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
  499. || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
  500. || r_type == R_MICROMIPS_TLS_GOTTPREL \
  501. || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
  502. || r_type == R_MICROMIPS_TLS_TPREL_LO16)
  503. /* Structure used to pass information to mips_elf_output_extsym. */
  504. struct extsym_info
  505. {
  506. bfd *abfd;
  507. struct bfd_link_info *info;
  508. struct ecoff_debug_info *debug;
  509. const struct ecoff_debug_swap *swap;
  510. bool failed;
  511. };
  512. /* The names of the runtime procedure table symbols used on IRIX5. */
  513. static const char * const mips_elf_dynsym_rtproc_names[] =
  514. {
  515. "_procedure_table",
  516. "_procedure_string_table",
  517. "_procedure_table_size",
  518. NULL
  519. };
  520. /* These structures are used to generate the .compact_rel section on
  521. IRIX5. */
  522. typedef struct
  523. {
  524. unsigned long id1; /* Always one? */
  525. unsigned long num; /* Number of compact relocation entries. */
  526. unsigned long id2; /* Always two? */
  527. unsigned long offset; /* The file offset of the first relocation. */
  528. unsigned long reserved0; /* Zero? */
  529. unsigned long reserved1; /* Zero? */
  530. } Elf32_compact_rel;
  531. typedef struct
  532. {
  533. bfd_byte id1[4];
  534. bfd_byte num[4];
  535. bfd_byte id2[4];
  536. bfd_byte offset[4];
  537. bfd_byte reserved0[4];
  538. bfd_byte reserved1[4];
  539. } Elf32_External_compact_rel;
  540. typedef struct
  541. {
  542. unsigned int ctype : 1; /* 1: long 0: short format. See below. */
  543. unsigned int rtype : 4; /* Relocation types. See below. */
  544. unsigned int dist2to : 8;
  545. unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
  546. unsigned long konst; /* KONST field. See below. */
  547. unsigned long vaddr; /* VADDR to be relocated. */
  548. } Elf32_crinfo;
  549. typedef struct
  550. {
  551. unsigned int ctype : 1; /* 1: long 0: short format. See below. */
  552. unsigned int rtype : 4; /* Relocation types. See below. */
  553. unsigned int dist2to : 8;
  554. unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
  555. unsigned long konst; /* KONST field. See below. */
  556. } Elf32_crinfo2;
  557. typedef struct
  558. {
  559. bfd_byte info[4];
  560. bfd_byte konst[4];
  561. bfd_byte vaddr[4];
  562. } Elf32_External_crinfo;
  563. typedef struct
  564. {
  565. bfd_byte info[4];
  566. bfd_byte konst[4];
  567. } Elf32_External_crinfo2;
  568. /* These are the constants used to swap the bitfields in a crinfo. */
  569. #define CRINFO_CTYPE (0x1U)
  570. #define CRINFO_CTYPE_SH (31)
  571. #define CRINFO_RTYPE (0xfU)
  572. #define CRINFO_RTYPE_SH (27)
  573. #define CRINFO_DIST2TO (0xffU)
  574. #define CRINFO_DIST2TO_SH (19)
  575. #define CRINFO_RELVADDR (0x7ffffU)
  576. #define CRINFO_RELVADDR_SH (0)
  577. /* A compact relocation info has long (3 words) or short (2 words)
  578. formats. A short format doesn't have VADDR field and relvaddr
  579. fields contains ((VADDR - vaddr of the previous entry) >> 2). */
  580. #define CRF_MIPS_LONG 1
  581. #define CRF_MIPS_SHORT 0
  582. /* There are 4 types of compact relocation at least. The value KONST
  583. has different meaning for each type:
  584. (type) (konst)
  585. CT_MIPS_REL32 Address in data
  586. CT_MIPS_WORD Address in word (XXX)
  587. CT_MIPS_GPHI_LO GP - vaddr
  588. CT_MIPS_JMPAD Address to jump
  589. */
  590. #define CRT_MIPS_REL32 0xa
  591. #define CRT_MIPS_WORD 0xb
  592. #define CRT_MIPS_GPHI_LO 0xc
  593. #define CRT_MIPS_JMPAD 0xd
  594. #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
  595. #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
  596. #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
  597. #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
  598. /* The structure of the runtime procedure descriptor created by the
  599. loader for use by the static exception system. */
  600. typedef struct runtime_pdr {
  601. bfd_vma adr; /* Memory address of start of procedure. */
  602. long regmask; /* Save register mask. */
  603. long regoffset; /* Save register offset. */
  604. long fregmask; /* Save floating point register mask. */
  605. long fregoffset; /* Save floating point register offset. */
  606. long frameoffset; /* Frame size. */
  607. short framereg; /* Frame pointer register. */
  608. short pcreg; /* Offset or reg of return pc. */
  609. long irpss; /* Index into the runtime string table. */
  610. long reserved;
  611. struct exception_info *exception_info;/* Pointer to exception array. */
  612. } RPDR, *pRPDR;
  613. #define cbRPDR sizeof (RPDR)
  614. #define rpdNil ((pRPDR) 0)
  615. static struct mips_got_entry *mips_elf_create_local_got_entry
  616. (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
  617. struct mips_elf_link_hash_entry *, int);
  618. static bool mips_elf_sort_hash_table_f
  619. (struct mips_elf_link_hash_entry *, void *);
  620. static bfd_vma mips_elf_high
  621. (bfd_vma);
  622. static bool mips_elf_create_dynamic_relocation
  623. (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
  624. struct mips_elf_link_hash_entry *, asection *, bfd_vma,
  625. bfd_vma *, asection *);
  626. static bfd_vma mips_elf_adjust_gp
  627. (bfd *, struct mips_got_info *, bfd *);
  628. /* This will be used when we sort the dynamic relocation records. */
  629. static bfd *reldyn_sorting_bfd;
  630. /* True if ABFD is for CPUs with load interlocking that include
  631. non-MIPS1 CPUs and R3900. */
  632. #define LOAD_INTERLOCKS_P(abfd) \
  633. ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
  634. || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
  635. /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
  636. This should be safe for all architectures. We enable this predicate
  637. for RM9000 for now. */
  638. #define JAL_TO_BAL_P(abfd) \
  639. ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
  640. /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
  641. This should be safe for all architectures. We enable this predicate for
  642. all CPUs. */
  643. #define JALR_TO_BAL_P(abfd) 1
  644. /* True if ABFD is for CPUs that are faster if JR is converted to B.
  645. This should be safe for all architectures. We enable this predicate for
  646. all CPUs. */
  647. #define JR_TO_B_P(abfd) 1
  648. /* True if ABFD is a PIC object. */
  649. #define PIC_OBJECT_P(abfd) \
  650. ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
  651. /* Nonzero if ABFD is using the O32 ABI. */
  652. #define ABI_O32_P(abfd) \
  653. ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
  654. /* Nonzero if ABFD is using the N32 ABI. */
  655. #define ABI_N32_P(abfd) \
  656. ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
  657. /* Nonzero if ABFD is using the N64 ABI. */
  658. #define ABI_64_P(abfd) \
  659. (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
  660. /* Nonzero if ABFD is using NewABI conventions. */
  661. #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
  662. /* Nonzero if ABFD has microMIPS code. */
  663. #define MICROMIPS_P(abfd) \
  664. ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
  665. /* Nonzero if ABFD is MIPS R6. */
  666. #define MIPSR6_P(abfd) \
  667. ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
  668. || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
  669. /* The IRIX compatibility level we are striving for. */
  670. #define IRIX_COMPAT(abfd) \
  671. (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
  672. /* Whether we are trying to be compatible with IRIX at all. */
  673. #define SGI_COMPAT(abfd) \
  674. (IRIX_COMPAT (abfd) != ict_none)
  675. /* The name of the options section. */
  676. #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
  677. (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
  678. /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
  679. Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
  680. #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
  681. (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
  682. /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
  683. #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
  684. (strcmp (NAME, ".MIPS.abiflags") == 0)
  685. /* Whether the section is readonly. */
  686. #define MIPS_ELF_READONLY_SECTION(sec) \
  687. ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
  688. == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
  689. /* The name of the stub section. */
  690. #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
  691. /* The size of an external REL relocation. */
  692. #define MIPS_ELF_REL_SIZE(abfd) \
  693. (get_elf_backend_data (abfd)->s->sizeof_rel)
  694. /* The size of an external RELA relocation. */
  695. #define MIPS_ELF_RELA_SIZE(abfd) \
  696. (get_elf_backend_data (abfd)->s->sizeof_rela)
  697. /* The size of an external dynamic table entry. */
  698. #define MIPS_ELF_DYN_SIZE(abfd) \
  699. (get_elf_backend_data (abfd)->s->sizeof_dyn)
  700. /* The size of a GOT entry. */
  701. #define MIPS_ELF_GOT_SIZE(abfd) \
  702. (get_elf_backend_data (abfd)->s->arch_size / 8)
  703. /* The size of the .rld_map section. */
  704. #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
  705. (get_elf_backend_data (abfd)->s->arch_size / 8)
  706. /* The size of a symbol-table entry. */
  707. #define MIPS_ELF_SYM_SIZE(abfd) \
  708. (get_elf_backend_data (abfd)->s->sizeof_sym)
  709. /* The default alignment for sections, as a power of two. */
  710. #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
  711. (get_elf_backend_data (abfd)->s->log_file_align)
  712. /* Get word-sized data. */
  713. #define MIPS_ELF_GET_WORD(abfd, ptr) \
  714. (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
  715. /* Put out word-sized data. */
  716. #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
  717. (ABI_64_P (abfd) \
  718. ? bfd_put_64 (abfd, val, ptr) \
  719. : bfd_put_32 (abfd, val, ptr))
  720. /* The opcode for word-sized loads (LW or LD). */
  721. #define MIPS_ELF_LOAD_WORD(abfd) \
  722. (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
  723. /* Add a dynamic symbol table-entry. */
  724. #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
  725. _bfd_elf_add_dynamic_entry (info, tag, val)
  726. #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
  727. (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
  728. /* The name of the dynamic relocation section. */
  729. #define MIPS_ELF_REL_DYN_NAME(INFO) \
  730. (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
  731. ? ".rela.dyn" : ".rel.dyn")
  732. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
  733. from smaller values. Start with zero, widen, *then* decrement. */
  734. #define MINUS_ONE (((bfd_vma)0) - 1)
  735. #define MINUS_TWO (((bfd_vma)0) - 2)
  736. /* The value to write into got[1] for SVR4 targets, to identify it is
  737. a GNU object. The dynamic linker can then use got[1] to store the
  738. module pointer. */
  739. #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
  740. ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
  741. /* The offset of $gp from the beginning of the .got section. */
  742. #define ELF_MIPS_GP_OFFSET(INFO) \
  743. (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
  744. ? 0x0 : 0x7ff0)
  745. /* The maximum size of the GOT for it to be addressable using 16-bit
  746. offsets from $gp. */
  747. #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
  748. /* Instructions which appear in a stub. */
  749. #define STUB_LW(abfd) \
  750. ((ABI_64_P (abfd) \
  751. ? 0xdf998010 /* ld t9,0x8010(gp) */ \
  752. : 0x8f998010)) /* lw t9,0x8010(gp) */
  753. #define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
  754. #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
  755. #define STUB_JALR 0x0320f809 /* jalr ra,t9 */
  756. #define STUB_JALRC 0xf8190000 /* jalrc ra,t9 */
  757. #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
  758. #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
  759. #define STUB_LI16S(abfd, VAL) \
  760. ((ABI_64_P (abfd) \
  761. ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
  762. : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
  763. /* Likewise for the microMIPS ASE. */
  764. #define STUB_LW_MICROMIPS(abfd) \
  765. (ABI_64_P (abfd) \
  766. ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
  767. : 0xff3c8010) /* lw t9,0x8010(gp) */
  768. #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
  769. #define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
  770. #define STUB_LUI_MICROMIPS(VAL) \
  771. (0x41b80000 + (VAL)) /* lui t8,VAL */
  772. #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
  773. #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
  774. #define STUB_ORI_MICROMIPS(VAL) \
  775. (0x53180000 + (VAL)) /* ori t8,t8,VAL */
  776. #define STUB_LI16U_MICROMIPS(VAL) \
  777. (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
  778. #define STUB_LI16S_MICROMIPS(abfd, VAL) \
  779. (ABI_64_P (abfd) \
  780. ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
  781. : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
  782. #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
  783. #define MIPS_FUNCTION_STUB_BIG_SIZE 20
  784. #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
  785. #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
  786. #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
  787. #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
  788. /* The name of the dynamic interpreter. This is put in the .interp
  789. section. */
  790. #define ELF_DYNAMIC_INTERPRETER(abfd) \
  791. (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
  792. : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
  793. : "/usr/lib/libc.so.1")
  794. #ifdef BFD64
  795. #define MNAME(bfd,pre,pos) \
  796. (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
  797. #define ELF_R_SYM(bfd, i) \
  798. (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
  799. #define ELF_R_TYPE(bfd, i) \
  800. (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
  801. #define ELF_R_INFO(bfd, s, t) \
  802. (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
  803. #else
  804. #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
  805. #define ELF_R_SYM(bfd, i) \
  806. (ELF32_R_SYM (i))
  807. #define ELF_R_TYPE(bfd, i) \
  808. (ELF32_R_TYPE (i))
  809. #define ELF_R_INFO(bfd, s, t) \
  810. (ELF32_R_INFO (s, t))
  811. #endif
  812. /* The mips16 compiler uses a couple of special sections to handle
  813. floating point arguments.
  814. Section names that look like .mips16.fn.FNNAME contain stubs that
  815. copy floating point arguments from the fp regs to the gp regs and
  816. then jump to FNNAME. If any 32 bit function calls FNNAME, the
  817. call should be redirected to the stub instead. If no 32 bit
  818. function calls FNNAME, the stub should be discarded. We need to
  819. consider any reference to the function, not just a call, because
  820. if the address of the function is taken we will need the stub,
  821. since the address might be passed to a 32 bit function.
  822. Section names that look like .mips16.call.FNNAME contain stubs
  823. that copy floating point arguments from the gp regs to the fp
  824. regs and then jump to FNNAME. If FNNAME is a 32 bit function,
  825. then any 16 bit function that calls FNNAME should be redirected
  826. to the stub instead. If FNNAME is not a 32 bit function, the
  827. stub should be discarded.
  828. .mips16.call.fp.FNNAME sections are similar, but contain stubs
  829. which call FNNAME and then copy the return value from the fp regs
  830. to the gp regs. These stubs store the return value in $18 while
  831. calling FNNAME; any function which might call one of these stubs
  832. must arrange to save $18 around the call. (This case is not
  833. needed for 32 bit functions that call 16 bit functions, because
  834. 16 bit functions always return floating point values in both
  835. $f0/$f1 and $2/$3.)
  836. Note that in all cases FNNAME might be defined statically.
  837. Therefore, FNNAME is not used literally. Instead, the relocation
  838. information will indicate which symbol the section is for.
  839. We record any stubs that we find in the symbol table. */
  840. #define FN_STUB ".mips16.fn."
  841. #define CALL_STUB ".mips16.call."
  842. #define CALL_FP_STUB ".mips16.call.fp."
  843. #define FN_STUB_P(name) startswith (name, FN_STUB)
  844. #define CALL_STUB_P(name) startswith (name, CALL_STUB)
  845. #define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
  846. /* The format of the first PLT entry in an O32 executable. */
  847. static const bfd_vma mips_o32_exec_plt0_entry[] =
  848. {
  849. 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
  850. 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
  851. 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
  852. 0x031cc023, /* subu $24, $24, $28 */
  853. 0x03e07825, /* or t7, ra, zero */
  854. 0x0018c082, /* srl $24, $24, 2 */
  855. 0x0320f809, /* jalr $25 */
  856. 0x2718fffe /* subu $24, $24, 2 */
  857. };
  858. /* The format of the first PLT entry in an O32 executable using compact
  859. jumps. */
  860. static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
  861. {
  862. 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
  863. 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
  864. 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
  865. 0x031cc023, /* subu $24, $24, $28 */
  866. 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
  867. 0x0018c082, /* srl $24, $24, 2 */
  868. 0x2718fffe, /* subu $24, $24, 2 */
  869. 0xf8190000 /* jalrc $25 */
  870. };
  871. /* The format of the first PLT entry in an N32 executable. Different
  872. because gp ($28) is not available; we use t2 ($14) instead. */
  873. static const bfd_vma mips_n32_exec_plt0_entry[] =
  874. {
  875. 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
  876. 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
  877. 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
  878. 0x030ec023, /* subu $24, $24, $14 */
  879. 0x03e07825, /* or t7, ra, zero */
  880. 0x0018c082, /* srl $24, $24, 2 */
  881. 0x0320f809, /* jalr $25 */
  882. 0x2718fffe /* subu $24, $24, 2 */
  883. };
  884. /* The format of the first PLT entry in an N32 executable using compact
  885. jumps. Different because gp ($28) is not available; we use t2 ($14)
  886. instead. */
  887. static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
  888. {
  889. 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
  890. 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
  891. 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
  892. 0x030ec023, /* subu $24, $24, $14 */
  893. 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
  894. 0x0018c082, /* srl $24, $24, 2 */
  895. 0x2718fffe, /* subu $24, $24, 2 */
  896. 0xf8190000 /* jalrc $25 */
  897. };
  898. /* The format of the first PLT entry in an N64 executable. Different
  899. from N32 because of the increased size of GOT entries. */
  900. static const bfd_vma mips_n64_exec_plt0_entry[] =
  901. {
  902. 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
  903. 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
  904. 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
  905. 0x030ec023, /* subu $24, $24, $14 */
  906. 0x03e07825, /* or t7, ra, zero */
  907. 0x0018c0c2, /* srl $24, $24, 3 */
  908. 0x0320f809, /* jalr $25 */
  909. 0x2718fffe /* subu $24, $24, 2 */
  910. };
  911. /* The format of the first PLT entry in an N64 executable using compact
  912. jumps. Different from N32 because of the increased size of GOT
  913. entries. */
  914. static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
  915. {
  916. 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
  917. 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
  918. 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
  919. 0x030ec023, /* subu $24, $24, $14 */
  920. 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
  921. 0x0018c0c2, /* srl $24, $24, 3 */
  922. 0x2718fffe, /* subu $24, $24, 2 */
  923. 0xf8190000 /* jalrc $25 */
  924. };
  925. /* The format of the microMIPS first PLT entry in an O32 executable.
  926. We rely on v0 ($2) rather than t8 ($24) to contain the address
  927. of the GOTPLT entry handled, so this stub may only be used when
  928. all the subsequent PLT entries are microMIPS code too.
  929. The trailing NOP is for alignment and correct disassembly only. */
  930. static const bfd_vma micromips_o32_exec_plt0_entry[] =
  931. {
  932. 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
  933. 0xff23, 0x0000, /* lw $25, 0($3) */
  934. 0x0535, /* subu $2, $2, $3 */
  935. 0x2525, /* srl $2, $2, 2 */
  936. 0x3302, 0xfffe, /* subu $24, $2, 2 */
  937. 0x0dff, /* move $15, $31 */
  938. 0x45f9, /* jalrs $25 */
  939. 0x0f83, /* move $28, $3 */
  940. 0x0c00 /* nop */
  941. };
  942. /* The format of the microMIPS first PLT entry in an O32 executable
  943. in the insn32 mode. */
  944. static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
  945. {
  946. 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
  947. 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
  948. 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
  949. 0x0398, 0xc1d0, /* subu $24, $24, $28 */
  950. 0x001f, 0x7a90, /* or $15, $31, zero */
  951. 0x0318, 0x1040, /* srl $24, $24, 2 */
  952. 0x03f9, 0x0f3c, /* jalr $25 */
  953. 0x3318, 0xfffe /* subu $24, $24, 2 */
  954. };
  955. /* The format of subsequent standard PLT entries. */
  956. static const bfd_vma mips_exec_plt_entry[] =
  957. {
  958. 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
  959. 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
  960. 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
  961. 0x03200008 /* jr $25 */
  962. };
  963. static const bfd_vma mipsr6_exec_plt_entry[] =
  964. {
  965. 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
  966. 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
  967. 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
  968. 0x03200009 /* jr $25 */
  969. };
  970. static const bfd_vma mipsr6_exec_plt_entry_compact[] =
  971. {
  972. 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
  973. 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
  974. 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
  975. 0xd8190000 /* jic $25, 0 */
  976. };
  977. /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
  978. and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
  979. directly addressable. */
  980. static const bfd_vma mips16_o32_exec_plt_entry[] =
  981. {
  982. 0xb203, /* lw $2, 12($pc) */
  983. 0x9a60, /* lw $3, 0($2) */
  984. 0x651a, /* move $24, $2 */
  985. 0xeb00, /* jr $3 */
  986. 0x653b, /* move $25, $3 */
  987. 0x6500, /* nop */
  988. 0x0000, 0x0000 /* .word (.got.plt entry) */
  989. };
  990. /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
  991. as a temporary because t8 ($24) is not addressable with ADDIUPC. */
  992. static const bfd_vma micromips_o32_exec_plt_entry[] =
  993. {
  994. 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
  995. 0xff22, 0x0000, /* lw $25, 0($2) */
  996. 0x4599, /* jr $25 */
  997. 0x0f02 /* move $24, $2 */
  998. };
  999. /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
  1000. static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
  1001. {
  1002. 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
  1003. 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
  1004. 0x0019, 0x0f3c, /* jr $25 */
  1005. 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
  1006. };
  1007. /* The format of the first PLT entry in a VxWorks executable. */
  1008. static const bfd_vma mips_vxworks_exec_plt0_entry[] =
  1009. {
  1010. 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
  1011. 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
  1012. 0x8f390008, /* lw t9, 8(t9) */
  1013. 0x00000000, /* nop */
  1014. 0x03200008, /* jr t9 */
  1015. 0x00000000 /* nop */
  1016. };
  1017. /* The format of subsequent PLT entries. */
  1018. static const bfd_vma mips_vxworks_exec_plt_entry[] =
  1019. {
  1020. 0x10000000, /* b .PLT_resolver */
  1021. 0x24180000, /* li t8, <pltindex> */
  1022. 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
  1023. 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
  1024. 0x8f390000, /* lw t9, 0(t9) */
  1025. 0x00000000, /* nop */
  1026. 0x03200008, /* jr t9 */
  1027. 0x00000000 /* nop */
  1028. };
  1029. /* The format of the first PLT entry in a VxWorks shared object. */
  1030. static const bfd_vma mips_vxworks_shared_plt0_entry[] =
  1031. {
  1032. 0x8f990008, /* lw t9, 8(gp) */
  1033. 0x00000000, /* nop */
  1034. 0x03200008, /* jr t9 */
  1035. 0x00000000, /* nop */
  1036. 0x00000000, /* nop */
  1037. 0x00000000 /* nop */
  1038. };
  1039. /* The format of subsequent PLT entries. */
  1040. static const bfd_vma mips_vxworks_shared_plt_entry[] =
  1041. {
  1042. 0x10000000, /* b .PLT_resolver */
  1043. 0x24180000 /* li t8, <pltindex> */
  1044. };
  1045. /* microMIPS 32-bit opcode helper installer. */
  1046. static void
  1047. bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
  1048. {
  1049. bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
  1050. bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
  1051. }
  1052. /* microMIPS 32-bit opcode helper retriever. */
  1053. static bfd_vma
  1054. bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
  1055. {
  1056. return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
  1057. }
  1058. /* Look up an entry in a MIPS ELF linker hash table. */
  1059. #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
  1060. ((struct mips_elf_link_hash_entry *) \
  1061. elf_link_hash_lookup (&(table)->root, (string), (create), \
  1062. (copy), (follow)))
  1063. /* Traverse a MIPS ELF linker hash table. */
  1064. #define mips_elf_link_hash_traverse(table, func, info) \
  1065. (elf_link_hash_traverse \
  1066. (&(table)->root, \
  1067. (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
  1068. (info)))
  1069. /* Find the base offsets for thread-local storage in this object,
  1070. for GD/LD and IE/LE respectively. */
  1071. #define TP_OFFSET 0x7000
  1072. #define DTP_OFFSET 0x8000
  1073. static bfd_vma
  1074. dtprel_base (struct bfd_link_info *info)
  1075. {
  1076. /* If tls_sec is NULL, we should have signalled an error already. */
  1077. if (elf_hash_table (info)->tls_sec == NULL)
  1078. return 0;
  1079. return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
  1080. }
  1081. static bfd_vma
  1082. tprel_base (struct bfd_link_info *info)
  1083. {
  1084. /* If tls_sec is NULL, we should have signalled an error already. */
  1085. if (elf_hash_table (info)->tls_sec == NULL)
  1086. return 0;
  1087. return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
  1088. }
  1089. /* Create an entry in a MIPS ELF linker hash table. */
  1090. static struct bfd_hash_entry *
  1091. mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
  1092. struct bfd_hash_table *table, const char *string)
  1093. {
  1094. struct mips_elf_link_hash_entry *ret =
  1095. (struct mips_elf_link_hash_entry *) entry;
  1096. /* Allocate the structure if it has not already been allocated by a
  1097. subclass. */
  1098. if (ret == NULL)
  1099. ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
  1100. if (ret == NULL)
  1101. return (struct bfd_hash_entry *) ret;
  1102. /* Call the allocation method of the superclass. */
  1103. ret = ((struct mips_elf_link_hash_entry *)
  1104. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  1105. table, string));
  1106. if (ret != NULL)
  1107. {
  1108. /* Set local fields. */
  1109. memset (&ret->esym, 0, sizeof (EXTR));
  1110. /* We use -2 as a marker to indicate that the information has
  1111. not been set. -1 means there is no associated ifd. */
  1112. ret->esym.ifd = -2;
  1113. ret->la25_stub = 0;
  1114. ret->possibly_dynamic_relocs = 0;
  1115. ret->fn_stub = NULL;
  1116. ret->call_stub = NULL;
  1117. ret->call_fp_stub = NULL;
  1118. ret->mipsxhash_loc = 0;
  1119. ret->global_got_area = GGA_NONE;
  1120. ret->got_only_for_calls = true;
  1121. ret->readonly_reloc = false;
  1122. ret->has_static_relocs = false;
  1123. ret->no_fn_stub = false;
  1124. ret->need_fn_stub = false;
  1125. ret->has_nonpic_branches = false;
  1126. ret->needs_lazy_stub = false;
  1127. ret->use_plt_entry = false;
  1128. }
  1129. return (struct bfd_hash_entry *) ret;
  1130. }
  1131. /* Allocate MIPS ELF private object data. */
  1132. bool
  1133. _bfd_mips_elf_mkobject (bfd *abfd)
  1134. {
  1135. return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
  1136. MIPS_ELF_DATA);
  1137. }
  1138. bool
  1139. _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
  1140. {
  1141. if (!sec->used_by_bfd)
  1142. {
  1143. struct _mips_elf_section_data *sdata;
  1144. size_t amt = sizeof (*sdata);
  1145. sdata = bfd_zalloc (abfd, amt);
  1146. if (sdata == NULL)
  1147. return false;
  1148. sec->used_by_bfd = sdata;
  1149. }
  1150. return _bfd_elf_new_section_hook (abfd, sec);
  1151. }
  1152. /* Read ECOFF debugging information from a .mdebug section into a
  1153. ecoff_debug_info structure. */
  1154. bool
  1155. _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
  1156. struct ecoff_debug_info *debug)
  1157. {
  1158. HDRR *symhdr;
  1159. const struct ecoff_debug_swap *swap;
  1160. char *ext_hdr;
  1161. swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1162. memset (debug, 0, sizeof (*debug));
  1163. ext_hdr = bfd_malloc (swap->external_hdr_size);
  1164. if (ext_hdr == NULL && swap->external_hdr_size != 0)
  1165. goto error_return;
  1166. if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
  1167. swap->external_hdr_size))
  1168. goto error_return;
  1169. symhdr = &debug->symbolic_header;
  1170. (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
  1171. /* The symbolic header contains absolute file offsets and sizes to
  1172. read. */
  1173. #define READ(ptr, offset, count, size, type) \
  1174. do \
  1175. { \
  1176. size_t amt; \
  1177. debug->ptr = NULL; \
  1178. if (symhdr->count == 0) \
  1179. break; \
  1180. if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
  1181. { \
  1182. bfd_set_error (bfd_error_file_too_big); \
  1183. goto error_return; \
  1184. } \
  1185. if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
  1186. goto error_return; \
  1187. debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \
  1188. if (debug->ptr == NULL) \
  1189. goto error_return; \
  1190. } while (0)
  1191. READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
  1192. READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
  1193. READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
  1194. READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
  1195. READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
  1196. READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
  1197. union aux_ext *);
  1198. READ (ss, cbSsOffset, issMax, sizeof (char), char *);
  1199. READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
  1200. READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
  1201. READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
  1202. READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
  1203. #undef READ
  1204. debug->fdr = NULL;
  1205. return true;
  1206. error_return:
  1207. free (ext_hdr);
  1208. free (debug->line);
  1209. free (debug->external_dnr);
  1210. free (debug->external_pdr);
  1211. free (debug->external_sym);
  1212. free (debug->external_opt);
  1213. free (debug->external_aux);
  1214. free (debug->ss);
  1215. free (debug->ssext);
  1216. free (debug->external_fdr);
  1217. free (debug->external_rfd);
  1218. free (debug->external_ext);
  1219. return false;
  1220. }
  1221. /* Swap RPDR (runtime procedure table entry) for output. */
  1222. static void
  1223. ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
  1224. {
  1225. H_PUT_S32 (abfd, in->adr, ex->p_adr);
  1226. H_PUT_32 (abfd, in->regmask, ex->p_regmask);
  1227. H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
  1228. H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
  1229. H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
  1230. H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
  1231. H_PUT_16 (abfd, in->framereg, ex->p_framereg);
  1232. H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
  1233. H_PUT_32 (abfd, in->irpss, ex->p_irpss);
  1234. }
  1235. /* Create a runtime procedure table from the .mdebug section. */
  1236. static bool
  1237. mips_elf_create_procedure_table (void *handle, bfd *abfd,
  1238. struct bfd_link_info *info, asection *s,
  1239. struct ecoff_debug_info *debug)
  1240. {
  1241. const struct ecoff_debug_swap *swap;
  1242. HDRR *hdr = &debug->symbolic_header;
  1243. RPDR *rpdr, *rp;
  1244. struct rpdr_ext *erp;
  1245. void *rtproc;
  1246. struct pdr_ext *epdr;
  1247. struct sym_ext *esym;
  1248. char *ss, **sv;
  1249. char *str;
  1250. bfd_size_type size;
  1251. bfd_size_type count;
  1252. unsigned long sindex;
  1253. unsigned long i;
  1254. PDR pdr;
  1255. SYMR sym;
  1256. const char *no_name_func = _("static procedure (no name)");
  1257. epdr = NULL;
  1258. rpdr = NULL;
  1259. esym = NULL;
  1260. ss = NULL;
  1261. sv = NULL;
  1262. swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1263. sindex = strlen (no_name_func) + 1;
  1264. count = hdr->ipdMax;
  1265. if (count > 0)
  1266. {
  1267. size = swap->external_pdr_size;
  1268. epdr = bfd_malloc (size * count);
  1269. if (epdr == NULL)
  1270. goto error_return;
  1271. if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
  1272. goto error_return;
  1273. size = sizeof (RPDR);
  1274. rp = rpdr = bfd_malloc (size * count);
  1275. if (rpdr == NULL)
  1276. goto error_return;
  1277. size = sizeof (char *);
  1278. sv = bfd_malloc (size * count);
  1279. if (sv == NULL)
  1280. goto error_return;
  1281. count = hdr->isymMax;
  1282. size = swap->external_sym_size;
  1283. esym = bfd_malloc (size * count);
  1284. if (esym == NULL)
  1285. goto error_return;
  1286. if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
  1287. goto error_return;
  1288. count = hdr->issMax;
  1289. ss = bfd_malloc (count);
  1290. if (ss == NULL)
  1291. goto error_return;
  1292. if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
  1293. goto error_return;
  1294. count = hdr->ipdMax;
  1295. for (i = 0; i < (unsigned long) count; i++, rp++)
  1296. {
  1297. (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
  1298. (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
  1299. rp->adr = sym.value;
  1300. rp->regmask = pdr.regmask;
  1301. rp->regoffset = pdr.regoffset;
  1302. rp->fregmask = pdr.fregmask;
  1303. rp->fregoffset = pdr.fregoffset;
  1304. rp->frameoffset = pdr.frameoffset;
  1305. rp->framereg = pdr.framereg;
  1306. rp->pcreg = pdr.pcreg;
  1307. rp->irpss = sindex;
  1308. sv[i] = ss + sym.iss;
  1309. sindex += strlen (sv[i]) + 1;
  1310. }
  1311. }
  1312. size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
  1313. size = BFD_ALIGN (size, 16);
  1314. rtproc = bfd_alloc (abfd, size);
  1315. if (rtproc == NULL)
  1316. {
  1317. mips_elf_hash_table (info)->procedure_count = 0;
  1318. goto error_return;
  1319. }
  1320. mips_elf_hash_table (info)->procedure_count = count + 2;
  1321. erp = rtproc;
  1322. memset (erp, 0, sizeof (struct rpdr_ext));
  1323. erp++;
  1324. str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
  1325. strcpy (str, no_name_func);
  1326. str += strlen (no_name_func) + 1;
  1327. for (i = 0; i < count; i++)
  1328. {
  1329. ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
  1330. strcpy (str, sv[i]);
  1331. str += strlen (sv[i]) + 1;
  1332. }
  1333. H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
  1334. /* Set the size and contents of .rtproc section. */
  1335. s->size = size;
  1336. s->contents = rtproc;
  1337. /* Skip this section later on (I don't think this currently
  1338. matters, but someday it might). */
  1339. s->map_head.link_order = NULL;
  1340. free (epdr);
  1341. free (rpdr);
  1342. free (esym);
  1343. free (ss);
  1344. free (sv);
  1345. return true;
  1346. error_return:
  1347. free (epdr);
  1348. free (rpdr);
  1349. free (esym);
  1350. free (ss);
  1351. free (sv);
  1352. return false;
  1353. }
  1354. /* We're going to create a stub for H. Create a symbol for the stub's
  1355. value and size, to help make the disassembly easier to read. */
  1356. static bool
  1357. mips_elf_create_stub_symbol (struct bfd_link_info *info,
  1358. struct mips_elf_link_hash_entry *h,
  1359. const char *prefix, asection *s, bfd_vma value,
  1360. bfd_vma size)
  1361. {
  1362. bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
  1363. struct bfd_link_hash_entry *bh;
  1364. struct elf_link_hash_entry *elfh;
  1365. char *name;
  1366. bool res;
  1367. if (micromips_p)
  1368. value |= 1;
  1369. /* Create a new symbol. */
  1370. name = concat (prefix, h->root.root.root.string, NULL);
  1371. bh = NULL;
  1372. res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
  1373. BSF_LOCAL, s, value, NULL,
  1374. true, false, &bh);
  1375. free (name);
  1376. if (! res)
  1377. return false;
  1378. /* Make it a local function. */
  1379. elfh = (struct elf_link_hash_entry *) bh;
  1380. elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
  1381. elfh->size = size;
  1382. elfh->forced_local = 1;
  1383. if (micromips_p)
  1384. elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
  1385. return true;
  1386. }
  1387. /* We're about to redefine H. Create a symbol to represent H's
  1388. current value and size, to help make the disassembly easier
  1389. to read. */
  1390. static bool
  1391. mips_elf_create_shadow_symbol (struct bfd_link_info *info,
  1392. struct mips_elf_link_hash_entry *h,
  1393. const char *prefix)
  1394. {
  1395. struct bfd_link_hash_entry *bh;
  1396. struct elf_link_hash_entry *elfh;
  1397. char *name;
  1398. asection *s;
  1399. bfd_vma value;
  1400. bool res;
  1401. /* Read the symbol's value. */
  1402. BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
  1403. || h->root.root.type == bfd_link_hash_defweak);
  1404. s = h->root.root.u.def.section;
  1405. value = h->root.root.u.def.value;
  1406. /* Create a new symbol. */
  1407. name = concat (prefix, h->root.root.root.string, NULL);
  1408. bh = NULL;
  1409. res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
  1410. BSF_LOCAL, s, value, NULL,
  1411. true, false, &bh);
  1412. free (name);
  1413. if (! res)
  1414. return false;
  1415. /* Make it local and copy the other attributes from H. */
  1416. elfh = (struct elf_link_hash_entry *) bh;
  1417. elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
  1418. elfh->other = h->root.other;
  1419. elfh->size = h->root.size;
  1420. elfh->forced_local = 1;
  1421. return true;
  1422. }
  1423. /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
  1424. function rather than to a hard-float stub. */
  1425. static bool
  1426. section_allows_mips16_refs_p (asection *section)
  1427. {
  1428. const char *name;
  1429. name = bfd_section_name (section);
  1430. return (FN_STUB_P (name)
  1431. || CALL_STUB_P (name)
  1432. || CALL_FP_STUB_P (name)
  1433. || strcmp (name, ".pdr") == 0);
  1434. }
  1435. /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
  1436. stub section of some kind. Return the R_SYMNDX of the target
  1437. function, or 0 if we can't decide which function that is. */
  1438. static unsigned long
  1439. mips16_stub_symndx (const struct elf_backend_data *bed,
  1440. asection *sec ATTRIBUTE_UNUSED,
  1441. const Elf_Internal_Rela *relocs,
  1442. const Elf_Internal_Rela *relend)
  1443. {
  1444. int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
  1445. const Elf_Internal_Rela *rel;
  1446. /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
  1447. one in a compound relocation. */
  1448. for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
  1449. if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
  1450. return ELF_R_SYM (sec->owner, rel->r_info);
  1451. /* Otherwise trust the first relocation, whatever its kind. This is
  1452. the traditional behavior. */
  1453. if (relocs < relend)
  1454. return ELF_R_SYM (sec->owner, relocs->r_info);
  1455. return 0;
  1456. }
  1457. /* Check the mips16 stubs for a particular symbol, and see if we can
  1458. discard them. */
  1459. static void
  1460. mips_elf_check_mips16_stubs (struct bfd_link_info *info,
  1461. struct mips_elf_link_hash_entry *h)
  1462. {
  1463. /* Dynamic symbols must use the standard call interface, in case other
  1464. objects try to call them. */
  1465. if (h->fn_stub != NULL
  1466. && h->root.dynindx != -1)
  1467. {
  1468. mips_elf_create_shadow_symbol (info, h, ".mips16.");
  1469. h->need_fn_stub = true;
  1470. }
  1471. if (h->fn_stub != NULL
  1472. && ! h->need_fn_stub)
  1473. {
  1474. /* We don't need the fn_stub; the only references to this symbol
  1475. are 16 bit calls. Clobber the size to 0 to prevent it from
  1476. being included in the link. */
  1477. h->fn_stub->size = 0;
  1478. h->fn_stub->flags &= ~SEC_RELOC;
  1479. h->fn_stub->reloc_count = 0;
  1480. h->fn_stub->flags |= SEC_EXCLUDE;
  1481. h->fn_stub->output_section = bfd_abs_section_ptr;
  1482. }
  1483. if (h->call_stub != NULL
  1484. && ELF_ST_IS_MIPS16 (h->root.other))
  1485. {
  1486. /* We don't need the call_stub; this is a 16 bit function, so
  1487. calls from other 16 bit functions are OK. Clobber the size
  1488. to 0 to prevent it from being included in the link. */
  1489. h->call_stub->size = 0;
  1490. h->call_stub->flags &= ~SEC_RELOC;
  1491. h->call_stub->reloc_count = 0;
  1492. h->call_stub->flags |= SEC_EXCLUDE;
  1493. h->call_stub->output_section = bfd_abs_section_ptr;
  1494. }
  1495. if (h->call_fp_stub != NULL
  1496. && ELF_ST_IS_MIPS16 (h->root.other))
  1497. {
  1498. /* We don't need the call_stub; this is a 16 bit function, so
  1499. calls from other 16 bit functions are OK. Clobber the size
  1500. to 0 to prevent it from being included in the link. */
  1501. h->call_fp_stub->size = 0;
  1502. h->call_fp_stub->flags &= ~SEC_RELOC;
  1503. h->call_fp_stub->reloc_count = 0;
  1504. h->call_fp_stub->flags |= SEC_EXCLUDE;
  1505. h->call_fp_stub->output_section = bfd_abs_section_ptr;
  1506. }
  1507. }
  1508. /* Hashtable callbacks for mips_elf_la25_stubs. */
  1509. static hashval_t
  1510. mips_elf_la25_stub_hash (const void *entry_)
  1511. {
  1512. const struct mips_elf_la25_stub *entry;
  1513. entry = (struct mips_elf_la25_stub *) entry_;
  1514. return entry->h->root.root.u.def.section->id
  1515. + entry->h->root.root.u.def.value;
  1516. }
  1517. static int
  1518. mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
  1519. {
  1520. const struct mips_elf_la25_stub *entry1, *entry2;
  1521. entry1 = (struct mips_elf_la25_stub *) entry1_;
  1522. entry2 = (struct mips_elf_la25_stub *) entry2_;
  1523. return ((entry1->h->root.root.u.def.section
  1524. == entry2->h->root.root.u.def.section)
  1525. && (entry1->h->root.root.u.def.value
  1526. == entry2->h->root.root.u.def.value));
  1527. }
  1528. /* Called by the linker to set up the la25 stub-creation code. FN is
  1529. the linker's implementation of add_stub_function. Return true on
  1530. success. */
  1531. bool
  1532. _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
  1533. asection *(*fn) (const char *, asection *,
  1534. asection *))
  1535. {
  1536. struct mips_elf_link_hash_table *htab;
  1537. htab = mips_elf_hash_table (info);
  1538. if (htab == NULL)
  1539. return false;
  1540. htab->add_stub_section = fn;
  1541. htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
  1542. mips_elf_la25_stub_eq, NULL);
  1543. if (htab->la25_stubs == NULL)
  1544. return false;
  1545. return true;
  1546. }
  1547. /* Return true if H is a locally-defined PIC function, in the sense
  1548. that it or its fn_stub might need $25 to be valid on entry.
  1549. Note that MIPS16 functions set up $gp using PC-relative instructions,
  1550. so they themselves never need $25 to be valid. Only non-MIPS16
  1551. entry points are of interest here. */
  1552. static bool
  1553. mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
  1554. {
  1555. return ((h->root.root.type == bfd_link_hash_defined
  1556. || h->root.root.type == bfd_link_hash_defweak)
  1557. && h->root.def_regular
  1558. && !bfd_is_abs_section (h->root.root.u.def.section)
  1559. && !bfd_is_und_section (h->root.root.u.def.section)
  1560. && (!ELF_ST_IS_MIPS16 (h->root.other)
  1561. || (h->fn_stub && h->need_fn_stub))
  1562. && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
  1563. || ELF_ST_IS_MIPS_PIC (h->root.other)));
  1564. }
  1565. /* Set *SEC to the input section that contains the target of STUB.
  1566. Return the offset of the target from the start of that section. */
  1567. static bfd_vma
  1568. mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
  1569. asection **sec)
  1570. {
  1571. if (ELF_ST_IS_MIPS16 (stub->h->root.other))
  1572. {
  1573. BFD_ASSERT (stub->h->need_fn_stub);
  1574. *sec = stub->h->fn_stub;
  1575. return 0;
  1576. }
  1577. else
  1578. {
  1579. *sec = stub->h->root.root.u.def.section;
  1580. return stub->h->root.root.u.def.value;
  1581. }
  1582. }
  1583. /* STUB describes an la25 stub that we have decided to implement
  1584. by inserting an LUI/ADDIU pair before the target function.
  1585. Create the section and redirect the function symbol to it. */
  1586. static bool
  1587. mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
  1588. struct bfd_link_info *info)
  1589. {
  1590. struct mips_elf_link_hash_table *htab;
  1591. char *name;
  1592. asection *s, *input_section;
  1593. unsigned int align;
  1594. htab = mips_elf_hash_table (info);
  1595. if (htab == NULL)
  1596. return false;
  1597. /* Create a unique name for the new section. */
  1598. name = bfd_malloc (11 + sizeof (".text.stub."));
  1599. if (name == NULL)
  1600. return false;
  1601. sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
  1602. /* Create the section. */
  1603. mips_elf_get_la25_target (stub, &input_section);
  1604. s = htab->add_stub_section (name, input_section,
  1605. input_section->output_section);
  1606. if (s == NULL)
  1607. return false;
  1608. /* Make sure that any padding goes before the stub. */
  1609. align = input_section->alignment_power;
  1610. if (!bfd_set_section_alignment (s, align))
  1611. return false;
  1612. if (align > 3)
  1613. s->size = (1 << align) - 8;
  1614. /* Create a symbol for the stub. */
  1615. mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
  1616. stub->stub_section = s;
  1617. stub->offset = s->size;
  1618. /* Allocate room for it. */
  1619. s->size += 8;
  1620. return true;
  1621. }
  1622. /* STUB describes an la25 stub that we have decided to implement
  1623. with a separate trampoline. Allocate room for it and redirect
  1624. the function symbol to it. */
  1625. static bool
  1626. mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
  1627. struct bfd_link_info *info)
  1628. {
  1629. struct mips_elf_link_hash_table *htab;
  1630. asection *s;
  1631. htab = mips_elf_hash_table (info);
  1632. if (htab == NULL)
  1633. return false;
  1634. /* Create a trampoline section, if we haven't already. */
  1635. s = htab->strampoline;
  1636. if (s == NULL)
  1637. {
  1638. asection *input_section = stub->h->root.root.u.def.section;
  1639. s = htab->add_stub_section (".text", NULL,
  1640. input_section->output_section);
  1641. if (s == NULL || !bfd_set_section_alignment (s, 4))
  1642. return false;
  1643. htab->strampoline = s;
  1644. }
  1645. /* Create a symbol for the stub. */
  1646. mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
  1647. stub->stub_section = s;
  1648. stub->offset = s->size;
  1649. /* Allocate room for it. */
  1650. s->size += 16;
  1651. return true;
  1652. }
  1653. /* H describes a symbol that needs an la25 stub. Make sure that an
  1654. appropriate stub exists and point H at it. */
  1655. static bool
  1656. mips_elf_add_la25_stub (struct bfd_link_info *info,
  1657. struct mips_elf_link_hash_entry *h)
  1658. {
  1659. struct mips_elf_link_hash_table *htab;
  1660. struct mips_elf_la25_stub search, *stub;
  1661. bool use_trampoline_p;
  1662. asection *s;
  1663. bfd_vma value;
  1664. void **slot;
  1665. /* Describe the stub we want. */
  1666. search.stub_section = NULL;
  1667. search.offset = 0;
  1668. search.h = h;
  1669. /* See if we've already created an equivalent stub. */
  1670. htab = mips_elf_hash_table (info);
  1671. if (htab == NULL)
  1672. return false;
  1673. slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
  1674. if (slot == NULL)
  1675. return false;
  1676. stub = (struct mips_elf_la25_stub *) *slot;
  1677. if (stub != NULL)
  1678. {
  1679. /* We can reuse the existing stub. */
  1680. h->la25_stub = stub;
  1681. return true;
  1682. }
  1683. /* Create a permanent copy of ENTRY and add it to the hash table. */
  1684. stub = bfd_malloc (sizeof (search));
  1685. if (stub == NULL)
  1686. return false;
  1687. *stub = search;
  1688. *slot = stub;
  1689. /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
  1690. of the section and if we would need no more than 2 nops. */
  1691. value = mips_elf_get_la25_target (stub, &s);
  1692. if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
  1693. value &= ~1;
  1694. use_trampoline_p = (value != 0 || s->alignment_power > 4);
  1695. h->la25_stub = stub;
  1696. return (use_trampoline_p
  1697. ? mips_elf_add_la25_trampoline (stub, info)
  1698. : mips_elf_add_la25_intro (stub, info));
  1699. }
  1700. /* A mips_elf_link_hash_traverse callback that is called before sizing
  1701. sections. DATA points to a mips_htab_traverse_info structure. */
  1702. static bool
  1703. mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
  1704. {
  1705. struct mips_htab_traverse_info *hti;
  1706. hti = (struct mips_htab_traverse_info *) data;
  1707. if (!bfd_link_relocatable (hti->info))
  1708. mips_elf_check_mips16_stubs (hti->info, h);
  1709. if (mips_elf_local_pic_function_p (h))
  1710. {
  1711. /* PR 12845: If H is in a section that has been garbage
  1712. collected it will have its output section set to *ABS*. */
  1713. if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
  1714. return true;
  1715. /* H is a function that might need $25 to be valid on entry.
  1716. If we're creating a non-PIC relocatable object, mark H as
  1717. being PIC. If we're creating a non-relocatable object with
  1718. non-PIC branches and jumps to H, make sure that H has an la25
  1719. stub. */
  1720. if (bfd_link_relocatable (hti->info))
  1721. {
  1722. if (!PIC_OBJECT_P (hti->output_bfd))
  1723. h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
  1724. }
  1725. else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
  1726. {
  1727. hti->error = true;
  1728. return false;
  1729. }
  1730. }
  1731. return true;
  1732. }
  1733. /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
  1734. Most mips16 instructions are 16 bits, but these instructions
  1735. are 32 bits.
  1736. The format of these instructions is:
  1737. +--------------+--------------------------------+
  1738. | JALX | X| Imm 20:16 | Imm 25:21 |
  1739. +--------------+--------------------------------+
  1740. | Immediate 15:0 |
  1741. +-----------------------------------------------+
  1742. JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
  1743. Note that the immediate value in the first word is swapped.
  1744. When producing a relocatable object file, R_MIPS16_26 is
  1745. handled mostly like R_MIPS_26. In particular, the addend is
  1746. stored as a straight 26-bit value in a 32-bit instruction.
  1747. (gas makes life simpler for itself by never adjusting a
  1748. R_MIPS16_26 reloc to be against a section, so the addend is
  1749. always zero). However, the 32 bit instruction is stored as 2
  1750. 16-bit values, rather than a single 32-bit value. In a
  1751. big-endian file, the result is the same; in a little-endian
  1752. file, the two 16-bit halves of the 32 bit value are swapped.
  1753. This is so that a disassembler can recognize the jal
  1754. instruction.
  1755. When doing a final link, R_MIPS16_26 is treated as a 32 bit
  1756. instruction stored as two 16-bit values. The addend A is the
  1757. contents of the targ26 field. The calculation is the same as
  1758. R_MIPS_26. When storing the calculated value, reorder the
  1759. immediate value as shown above, and don't forget to store the
  1760. value as two 16-bit values.
  1761. To put it in MIPS ABI terms, the relocation field is T-targ26-16,
  1762. defined as
  1763. big-endian:
  1764. +--------+----------------------+
  1765. | | |
  1766. | | targ26-16 |
  1767. |31 26|25 0|
  1768. +--------+----------------------+
  1769. little-endian:
  1770. +----------+------+-------------+
  1771. | | | |
  1772. | sub1 | | sub2 |
  1773. |0 9|10 15|16 31|
  1774. +----------+--------------------+
  1775. where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
  1776. ((sub1 << 16) | sub2)).
  1777. When producing a relocatable object file, the calculation is
  1778. (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
  1779. When producing a fully linked file, the calculation is
  1780. let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
  1781. ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
  1782. The table below lists the other MIPS16 instruction relocations.
  1783. Each one is calculated in the same way as the non-MIPS16 relocation
  1784. given on the right, but using the extended MIPS16 layout of 16-bit
  1785. immediate fields:
  1786. R_MIPS16_GPREL R_MIPS_GPREL16
  1787. R_MIPS16_GOT16 R_MIPS_GOT16
  1788. R_MIPS16_CALL16 R_MIPS_CALL16
  1789. R_MIPS16_HI16 R_MIPS_HI16
  1790. R_MIPS16_LO16 R_MIPS_LO16
  1791. A typical instruction will have a format like this:
  1792. +--------------+--------------------------------+
  1793. | EXTEND | Imm 10:5 | Imm 15:11 |
  1794. +--------------+--------------------------------+
  1795. | Major | rx | ry | Imm 4:0 |
  1796. +--------------+--------------------------------+
  1797. EXTEND is the five bit value 11110. Major is the instruction
  1798. opcode.
  1799. All we need to do here is shuffle the bits appropriately.
  1800. As above, the two 16-bit halves must be swapped on a
  1801. little-endian system.
  1802. Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
  1803. relocatable field is shifted by 1 rather than 2 and the same bit
  1804. shuffling is done as with the relocations above. */
  1805. static inline bool
  1806. mips16_reloc_p (int r_type)
  1807. {
  1808. switch (r_type)
  1809. {
  1810. case R_MIPS16_26:
  1811. case R_MIPS16_GPREL:
  1812. case R_MIPS16_GOT16:
  1813. case R_MIPS16_CALL16:
  1814. case R_MIPS16_HI16:
  1815. case R_MIPS16_LO16:
  1816. case R_MIPS16_TLS_GD:
  1817. case R_MIPS16_TLS_LDM:
  1818. case R_MIPS16_TLS_DTPREL_HI16:
  1819. case R_MIPS16_TLS_DTPREL_LO16:
  1820. case R_MIPS16_TLS_GOTTPREL:
  1821. case R_MIPS16_TLS_TPREL_HI16:
  1822. case R_MIPS16_TLS_TPREL_LO16:
  1823. case R_MIPS16_PC16_S1:
  1824. return true;
  1825. default:
  1826. return false;
  1827. }
  1828. }
  1829. /* Check if a microMIPS reloc. */
  1830. static inline bool
  1831. micromips_reloc_p (unsigned int r_type)
  1832. {
  1833. return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
  1834. }
  1835. /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
  1836. on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
  1837. and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
  1838. static inline bool
  1839. micromips_reloc_shuffle_p (unsigned int r_type)
  1840. {
  1841. return (micromips_reloc_p (r_type)
  1842. && r_type != R_MICROMIPS_PC7_S1
  1843. && r_type != R_MICROMIPS_PC10_S1);
  1844. }
  1845. static inline bool
  1846. got16_reloc_p (int r_type)
  1847. {
  1848. return (r_type == R_MIPS_GOT16
  1849. || r_type == R_MIPS16_GOT16
  1850. || r_type == R_MICROMIPS_GOT16);
  1851. }
  1852. static inline bool
  1853. call16_reloc_p (int r_type)
  1854. {
  1855. return (r_type == R_MIPS_CALL16
  1856. || r_type == R_MIPS16_CALL16
  1857. || r_type == R_MICROMIPS_CALL16);
  1858. }
  1859. static inline bool
  1860. got_disp_reloc_p (unsigned int r_type)
  1861. {
  1862. return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
  1863. }
  1864. static inline bool
  1865. got_page_reloc_p (unsigned int r_type)
  1866. {
  1867. return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
  1868. }
  1869. static inline bool
  1870. got_lo16_reloc_p (unsigned int r_type)
  1871. {
  1872. return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
  1873. }
  1874. static inline bool
  1875. call_hi16_reloc_p (unsigned int r_type)
  1876. {
  1877. return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
  1878. }
  1879. static inline bool
  1880. call_lo16_reloc_p (unsigned int r_type)
  1881. {
  1882. return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
  1883. }
  1884. static inline bool
  1885. hi16_reloc_p (int r_type)
  1886. {
  1887. return (r_type == R_MIPS_HI16
  1888. || r_type == R_MIPS16_HI16
  1889. || r_type == R_MICROMIPS_HI16
  1890. || r_type == R_MIPS_PCHI16);
  1891. }
  1892. static inline bool
  1893. lo16_reloc_p (int r_type)
  1894. {
  1895. return (r_type == R_MIPS_LO16
  1896. || r_type == R_MIPS16_LO16
  1897. || r_type == R_MICROMIPS_LO16
  1898. || r_type == R_MIPS_PCLO16);
  1899. }
  1900. static inline bool
  1901. mips16_call_reloc_p (int r_type)
  1902. {
  1903. return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
  1904. }
  1905. static inline bool
  1906. jal_reloc_p (int r_type)
  1907. {
  1908. return (r_type == R_MIPS_26
  1909. || r_type == R_MIPS16_26
  1910. || r_type == R_MICROMIPS_26_S1);
  1911. }
  1912. static inline bool
  1913. b_reloc_p (int r_type)
  1914. {
  1915. return (r_type == R_MIPS_PC26_S2
  1916. || r_type == R_MIPS_PC21_S2
  1917. || r_type == R_MIPS_PC16
  1918. || r_type == R_MIPS_GNU_REL16_S2
  1919. || r_type == R_MIPS16_PC16_S1
  1920. || r_type == R_MICROMIPS_PC16_S1
  1921. || r_type == R_MICROMIPS_PC10_S1
  1922. || r_type == R_MICROMIPS_PC7_S1);
  1923. }
  1924. static inline bool
  1925. aligned_pcrel_reloc_p (int r_type)
  1926. {
  1927. return (r_type == R_MIPS_PC18_S3
  1928. || r_type == R_MIPS_PC19_S2);
  1929. }
  1930. static inline bool
  1931. branch_reloc_p (int r_type)
  1932. {
  1933. return (r_type == R_MIPS_26
  1934. || r_type == R_MIPS_PC26_S2
  1935. || r_type == R_MIPS_PC21_S2
  1936. || r_type == R_MIPS_PC16
  1937. || r_type == R_MIPS_GNU_REL16_S2);
  1938. }
  1939. static inline bool
  1940. mips16_branch_reloc_p (int r_type)
  1941. {
  1942. return (r_type == R_MIPS16_26
  1943. || r_type == R_MIPS16_PC16_S1);
  1944. }
  1945. static inline bool
  1946. micromips_branch_reloc_p (int r_type)
  1947. {
  1948. return (r_type == R_MICROMIPS_26_S1
  1949. || r_type == R_MICROMIPS_PC16_S1
  1950. || r_type == R_MICROMIPS_PC10_S1
  1951. || r_type == R_MICROMIPS_PC7_S1);
  1952. }
  1953. static inline bool
  1954. tls_gd_reloc_p (unsigned int r_type)
  1955. {
  1956. return (r_type == R_MIPS_TLS_GD
  1957. || r_type == R_MIPS16_TLS_GD
  1958. || r_type == R_MICROMIPS_TLS_GD);
  1959. }
  1960. static inline bool
  1961. tls_ldm_reloc_p (unsigned int r_type)
  1962. {
  1963. return (r_type == R_MIPS_TLS_LDM
  1964. || r_type == R_MIPS16_TLS_LDM
  1965. || r_type == R_MICROMIPS_TLS_LDM);
  1966. }
  1967. static inline bool
  1968. tls_gottprel_reloc_p (unsigned int r_type)
  1969. {
  1970. return (r_type == R_MIPS_TLS_GOTTPREL
  1971. || r_type == R_MIPS16_TLS_GOTTPREL
  1972. || r_type == R_MICROMIPS_TLS_GOTTPREL);
  1973. }
  1974. void
  1975. _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
  1976. bool jal_shuffle, bfd_byte *data)
  1977. {
  1978. bfd_vma first, second, val;
  1979. if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
  1980. return;
  1981. /* Pick up the first and second halfwords of the instruction. */
  1982. first = bfd_get_16 (abfd, data);
  1983. second = bfd_get_16 (abfd, data + 2);
  1984. if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
  1985. val = first << 16 | second;
  1986. else if (r_type != R_MIPS16_26)
  1987. val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
  1988. | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
  1989. else
  1990. val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
  1991. | ((first & 0x1f) << 21) | second);
  1992. bfd_put_32 (abfd, val, data);
  1993. }
  1994. void
  1995. _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
  1996. bool jal_shuffle, bfd_byte *data)
  1997. {
  1998. bfd_vma first, second, val;
  1999. if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
  2000. return;
  2001. val = bfd_get_32 (abfd, data);
  2002. if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
  2003. {
  2004. second = val & 0xffff;
  2005. first = val >> 16;
  2006. }
  2007. else if (r_type != R_MIPS16_26)
  2008. {
  2009. second = ((val >> 11) & 0xffe0) | (val & 0x1f);
  2010. first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
  2011. }
  2012. else
  2013. {
  2014. second = val & 0xffff;
  2015. first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
  2016. | ((val >> 21) & 0x1f);
  2017. }
  2018. bfd_put_16 (abfd, second, data + 2);
  2019. bfd_put_16 (abfd, first, data);
  2020. }
  2021. bfd_reloc_status_type
  2022. _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
  2023. arelent *reloc_entry, asection *input_section,
  2024. bool relocatable, void *data, bfd_vma gp)
  2025. {
  2026. bfd_vma relocation;
  2027. bfd_signed_vma val;
  2028. bfd_reloc_status_type status;
  2029. if (bfd_is_com_section (symbol->section))
  2030. relocation = 0;
  2031. else
  2032. relocation = symbol->value;
  2033. relocation += symbol->section->output_section->vma;
  2034. relocation += symbol->section->output_offset;
  2035. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  2036. return bfd_reloc_outofrange;
  2037. /* Set val to the offset into the section or symbol. */
  2038. val = reloc_entry->addend;
  2039. _bfd_mips_elf_sign_extend (val, 16);
  2040. /* Adjust val for the final section location and GP value. If we
  2041. are producing relocatable output, we don't want to do this for
  2042. an external symbol. */
  2043. if (! relocatable
  2044. || (symbol->flags & BSF_SECTION_SYM) != 0)
  2045. val += relocation - gp;
  2046. if (reloc_entry->howto->partial_inplace)
  2047. {
  2048. status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
  2049. (bfd_byte *) data
  2050. + reloc_entry->address);
  2051. if (status != bfd_reloc_ok)
  2052. return status;
  2053. }
  2054. else
  2055. reloc_entry->addend = val;
  2056. if (relocatable)
  2057. reloc_entry->address += input_section->output_offset;
  2058. return bfd_reloc_ok;
  2059. }
  2060. /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
  2061. R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
  2062. that contains the relocation field and DATA points to the start of
  2063. INPUT_SECTION. */
  2064. struct mips_hi16
  2065. {
  2066. struct mips_hi16 *next;
  2067. bfd_byte *data;
  2068. asection *input_section;
  2069. arelent rel;
  2070. };
  2071. /* FIXME: This should not be a static variable. */
  2072. static struct mips_hi16 *mips_hi16_list;
  2073. /* A howto special_function for REL *HI16 relocations. We can only
  2074. calculate the correct value once we've seen the partnering
  2075. *LO16 relocation, so just save the information for later.
  2076. The ABI requires that the *LO16 immediately follow the *HI16.
  2077. However, as a GNU extension, we permit an arbitrary number of
  2078. *HI16s to be associated with a single *LO16. This significantly
  2079. simplies the relocation handling in gcc. */
  2080. bfd_reloc_status_type
  2081. _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
  2082. asymbol *symbol ATTRIBUTE_UNUSED, void *data,
  2083. asection *input_section, bfd *output_bfd,
  2084. char **error_message ATTRIBUTE_UNUSED)
  2085. {
  2086. struct mips_hi16 *n;
  2087. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  2088. return bfd_reloc_outofrange;
  2089. n = bfd_malloc (sizeof *n);
  2090. if (n == NULL)
  2091. return bfd_reloc_outofrange;
  2092. n->next = mips_hi16_list;
  2093. n->data = data;
  2094. n->input_section = input_section;
  2095. n->rel = *reloc_entry;
  2096. mips_hi16_list = n;
  2097. if (output_bfd != NULL)
  2098. reloc_entry->address += input_section->output_offset;
  2099. return bfd_reloc_ok;
  2100. }
  2101. /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
  2102. like any other 16-bit relocation when applied to global symbols, but is
  2103. treated in the same as R_MIPS_HI16 when applied to local symbols. */
  2104. bfd_reloc_status_type
  2105. _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  2106. void *data, asection *input_section,
  2107. bfd *output_bfd, char **error_message)
  2108. {
  2109. if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
  2110. || bfd_is_und_section (bfd_asymbol_section (symbol))
  2111. || bfd_is_com_section (bfd_asymbol_section (symbol)))
  2112. /* The relocation is against a global symbol. */
  2113. return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  2114. input_section, output_bfd,
  2115. error_message);
  2116. return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
  2117. input_section, output_bfd, error_message);
  2118. }
  2119. /* A howto special_function for REL *LO16 relocations. The *LO16 itself
  2120. is a straightforward 16 bit inplace relocation, but we must deal with
  2121. any partnering high-part relocations as well. */
  2122. bfd_reloc_status_type
  2123. _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  2124. void *data, asection *input_section,
  2125. bfd *output_bfd, char **error_message)
  2126. {
  2127. bfd_vma vallo;
  2128. bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
  2129. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  2130. return bfd_reloc_outofrange;
  2131. _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
  2132. location);
  2133. vallo = bfd_get_32 (abfd, location);
  2134. _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
  2135. location);
  2136. while (mips_hi16_list != NULL)
  2137. {
  2138. bfd_reloc_status_type ret;
  2139. struct mips_hi16 *hi;
  2140. hi = mips_hi16_list;
  2141. /* R_MIPS*_GOT16 relocations are something of a special case. We
  2142. want to install the addend in the same way as for a R_MIPS*_HI16
  2143. relocation (with a rightshift of 16). However, since GOT16
  2144. relocations can also be used with global symbols, their howto
  2145. has a rightshift of 0. */
  2146. if (hi->rel.howto->type == R_MIPS_GOT16)
  2147. hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
  2148. else if (hi->rel.howto->type == R_MIPS16_GOT16)
  2149. hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
  2150. else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
  2151. hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
  2152. /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
  2153. carry or borrow will induce a change of +1 or -1 in the high part. */
  2154. hi->rel.addend += (vallo + 0x8000) & 0xffff;
  2155. ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
  2156. hi->input_section, output_bfd,
  2157. error_message);
  2158. if (ret != bfd_reloc_ok)
  2159. return ret;
  2160. mips_hi16_list = hi->next;
  2161. free (hi);
  2162. }
  2163. return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  2164. input_section, output_bfd,
  2165. error_message);
  2166. }
  2167. /* A generic howto special_function. This calculates and installs the
  2168. relocation itself, thus avoiding the oft-discussed problems in
  2169. bfd_perform_relocation and bfd_install_relocation. */
  2170. bfd_reloc_status_type
  2171. _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
  2172. asymbol *symbol, void *data ATTRIBUTE_UNUSED,
  2173. asection *input_section, bfd *output_bfd,
  2174. char **error_message ATTRIBUTE_UNUSED)
  2175. {
  2176. bfd_signed_vma val;
  2177. bfd_reloc_status_type status;
  2178. bool relocatable;
  2179. relocatable = (output_bfd != NULL);
  2180. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  2181. return bfd_reloc_outofrange;
  2182. /* Build up the field adjustment in VAL. */
  2183. val = 0;
  2184. if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
  2185. {
  2186. /* Either we're calculating the final field value or we have a
  2187. relocation against a section symbol. Add in the section's
  2188. offset or address. */
  2189. val += symbol->section->output_section->vma;
  2190. val += symbol->section->output_offset;
  2191. }
  2192. if (!relocatable)
  2193. {
  2194. /* We're calculating the final field value. Add in the symbol's value
  2195. and, if pc-relative, subtract the address of the field itself. */
  2196. val += symbol->value;
  2197. if (reloc_entry->howto->pc_relative)
  2198. {
  2199. val -= input_section->output_section->vma;
  2200. val -= input_section->output_offset;
  2201. val -= reloc_entry->address;
  2202. }
  2203. }
  2204. /* VAL is now the final adjustment. If we're keeping this relocation
  2205. in the output file, and if the relocation uses a separate addend,
  2206. we just need to add VAL to that addend. Otherwise we need to add
  2207. VAL to the relocation field itself. */
  2208. if (relocatable && !reloc_entry->howto->partial_inplace)
  2209. reloc_entry->addend += val;
  2210. else
  2211. {
  2212. bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
  2213. /* Add in the separate addend, if any. */
  2214. val += reloc_entry->addend;
  2215. /* Add VAL to the relocation field. */
  2216. _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
  2217. location);
  2218. status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
  2219. location);
  2220. _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
  2221. location);
  2222. if (status != bfd_reloc_ok)
  2223. return status;
  2224. }
  2225. if (relocatable)
  2226. reloc_entry->address += input_section->output_offset;
  2227. return bfd_reloc_ok;
  2228. }
  2229. /* Swap an entry in a .gptab section. Note that these routines rely
  2230. on the equivalence of the two elements of the union. */
  2231. static void
  2232. bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
  2233. Elf32_gptab *in)
  2234. {
  2235. in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
  2236. in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
  2237. }
  2238. static void
  2239. bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
  2240. Elf32_External_gptab *ex)
  2241. {
  2242. H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
  2243. H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
  2244. }
  2245. static void
  2246. bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
  2247. Elf32_External_compact_rel *ex)
  2248. {
  2249. H_PUT_32 (abfd, in->id1, ex->id1);
  2250. H_PUT_32 (abfd, in->num, ex->num);
  2251. H_PUT_32 (abfd, in->id2, ex->id2);
  2252. H_PUT_32 (abfd, in->offset, ex->offset);
  2253. H_PUT_32 (abfd, in->reserved0, ex->reserved0);
  2254. H_PUT_32 (abfd, in->reserved1, ex->reserved1);
  2255. }
  2256. static void
  2257. bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
  2258. Elf32_External_crinfo *ex)
  2259. {
  2260. unsigned long l;
  2261. l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
  2262. | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
  2263. | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
  2264. | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
  2265. H_PUT_32 (abfd, l, ex->info);
  2266. H_PUT_32 (abfd, in->konst, ex->konst);
  2267. H_PUT_32 (abfd, in->vaddr, ex->vaddr);
  2268. }
  2269. /* A .reginfo section holds a single Elf32_RegInfo structure. These
  2270. routines swap this structure in and out. They are used outside of
  2271. BFD, so they are globally visible. */
  2272. void
  2273. bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
  2274. Elf32_RegInfo *in)
  2275. {
  2276. in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
  2277. in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
  2278. in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
  2279. in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
  2280. in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
  2281. in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
  2282. }
  2283. void
  2284. bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
  2285. Elf32_External_RegInfo *ex)
  2286. {
  2287. H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
  2288. H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
  2289. H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
  2290. H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
  2291. H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
  2292. H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
  2293. }
  2294. /* In the 64 bit ABI, the .MIPS.options section holds register
  2295. information in an Elf64_Reginfo structure. These routines swap
  2296. them in and out. They are globally visible because they are used
  2297. outside of BFD. These routines are here so that gas can call them
  2298. without worrying about whether the 64 bit ABI has been included. */
  2299. void
  2300. bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
  2301. Elf64_Internal_RegInfo *in)
  2302. {
  2303. in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
  2304. in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
  2305. in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
  2306. in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
  2307. in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
  2308. in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
  2309. in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
  2310. }
  2311. void
  2312. bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
  2313. Elf64_External_RegInfo *ex)
  2314. {
  2315. H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
  2316. H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
  2317. H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
  2318. H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
  2319. H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
  2320. H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
  2321. H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
  2322. }
  2323. /* Swap in an options header. */
  2324. void
  2325. bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
  2326. Elf_Internal_Options *in)
  2327. {
  2328. in->kind = H_GET_8 (abfd, ex->kind);
  2329. in->size = H_GET_8 (abfd, ex->size);
  2330. in->section = H_GET_16 (abfd, ex->section);
  2331. in->info = H_GET_32 (abfd, ex->info);
  2332. }
  2333. /* Swap out an options header. */
  2334. void
  2335. bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
  2336. Elf_External_Options *ex)
  2337. {
  2338. H_PUT_8 (abfd, in->kind, ex->kind);
  2339. H_PUT_8 (abfd, in->size, ex->size);
  2340. H_PUT_16 (abfd, in->section, ex->section);
  2341. H_PUT_32 (abfd, in->info, ex->info);
  2342. }
  2343. /* Swap in an abiflags structure. */
  2344. void
  2345. bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
  2346. const Elf_External_ABIFlags_v0 *ex,
  2347. Elf_Internal_ABIFlags_v0 *in)
  2348. {
  2349. in->version = H_GET_16 (abfd, ex->version);
  2350. in->isa_level = H_GET_8 (abfd, ex->isa_level);
  2351. in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
  2352. in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
  2353. in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
  2354. in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
  2355. in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
  2356. in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
  2357. in->ases = H_GET_32 (abfd, ex->ases);
  2358. in->flags1 = H_GET_32 (abfd, ex->flags1);
  2359. in->flags2 = H_GET_32 (abfd, ex->flags2);
  2360. }
  2361. /* Swap out an abiflags structure. */
  2362. void
  2363. bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
  2364. const Elf_Internal_ABIFlags_v0 *in,
  2365. Elf_External_ABIFlags_v0 *ex)
  2366. {
  2367. H_PUT_16 (abfd, in->version, ex->version);
  2368. H_PUT_8 (abfd, in->isa_level, ex->isa_level);
  2369. H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
  2370. H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
  2371. H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
  2372. H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
  2373. H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
  2374. H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
  2375. H_PUT_32 (abfd, in->ases, ex->ases);
  2376. H_PUT_32 (abfd, in->flags1, ex->flags1);
  2377. H_PUT_32 (abfd, in->flags2, ex->flags2);
  2378. }
  2379. /* This function is called via qsort() to sort the dynamic relocation
  2380. entries by increasing r_symndx value. */
  2381. static int
  2382. sort_dynamic_relocs (const void *arg1, const void *arg2)
  2383. {
  2384. Elf_Internal_Rela int_reloc1;
  2385. Elf_Internal_Rela int_reloc2;
  2386. int diff;
  2387. bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
  2388. bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
  2389. diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
  2390. if (diff != 0)
  2391. return diff;
  2392. if (int_reloc1.r_offset < int_reloc2.r_offset)
  2393. return -1;
  2394. if (int_reloc1.r_offset > int_reloc2.r_offset)
  2395. return 1;
  2396. return 0;
  2397. }
  2398. /* Like sort_dynamic_relocs, but used for elf64 relocations. */
  2399. static int
  2400. sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
  2401. const void *arg2 ATTRIBUTE_UNUSED)
  2402. {
  2403. #ifdef BFD64
  2404. Elf_Internal_Rela int_reloc1[3];
  2405. Elf_Internal_Rela int_reloc2[3];
  2406. (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
  2407. (reldyn_sorting_bfd, arg1, int_reloc1);
  2408. (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
  2409. (reldyn_sorting_bfd, arg2, int_reloc2);
  2410. if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
  2411. return -1;
  2412. if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
  2413. return 1;
  2414. if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
  2415. return -1;
  2416. if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
  2417. return 1;
  2418. return 0;
  2419. #else
  2420. abort ();
  2421. #endif
  2422. }
  2423. /* This routine is used to write out ECOFF debugging external symbol
  2424. information. It is called via mips_elf_link_hash_traverse. The
  2425. ECOFF external symbol information must match the ELF external
  2426. symbol information. Unfortunately, at this point we don't know
  2427. whether a symbol is required by reloc information, so the two
  2428. tables may wind up being different. We must sort out the external
  2429. symbol information before we can set the final size of the .mdebug
  2430. section, and we must set the size of the .mdebug section before we
  2431. can relocate any sections, and we can't know which symbols are
  2432. required by relocation until we relocate the sections.
  2433. Fortunately, it is relatively unlikely that any symbol will be
  2434. stripped but required by a reloc. In particular, it can not happen
  2435. when generating a final executable. */
  2436. static bool
  2437. mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
  2438. {
  2439. struct extsym_info *einfo = data;
  2440. bool strip;
  2441. asection *sec, *output_section;
  2442. if (h->root.indx == -2)
  2443. strip = false;
  2444. else if ((h->root.def_dynamic
  2445. || h->root.ref_dynamic
  2446. || h->root.type == bfd_link_hash_new)
  2447. && !h->root.def_regular
  2448. && !h->root.ref_regular)
  2449. strip = true;
  2450. else if (einfo->info->strip == strip_all
  2451. || (einfo->info->strip == strip_some
  2452. && bfd_hash_lookup (einfo->info->keep_hash,
  2453. h->root.root.root.string,
  2454. false, false) == NULL))
  2455. strip = true;
  2456. else
  2457. strip = false;
  2458. if (strip)
  2459. return true;
  2460. if (h->esym.ifd == -2)
  2461. {
  2462. h->esym.jmptbl = 0;
  2463. h->esym.cobol_main = 0;
  2464. h->esym.weakext = 0;
  2465. h->esym.reserved = 0;
  2466. h->esym.ifd = ifdNil;
  2467. h->esym.asym.value = 0;
  2468. h->esym.asym.st = stGlobal;
  2469. if (h->root.root.type == bfd_link_hash_undefined
  2470. || h->root.root.type == bfd_link_hash_undefweak)
  2471. {
  2472. const char *name;
  2473. /* Use undefined class. Also, set class and type for some
  2474. special symbols. */
  2475. name = h->root.root.root.string;
  2476. if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
  2477. || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
  2478. {
  2479. h->esym.asym.sc = scData;
  2480. h->esym.asym.st = stLabel;
  2481. h->esym.asym.value = 0;
  2482. }
  2483. else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
  2484. {
  2485. h->esym.asym.sc = scAbs;
  2486. h->esym.asym.st = stLabel;
  2487. h->esym.asym.value =
  2488. mips_elf_hash_table (einfo->info)->procedure_count;
  2489. }
  2490. else
  2491. h->esym.asym.sc = scUndefined;
  2492. }
  2493. else if (h->root.root.type != bfd_link_hash_defined
  2494. && h->root.root.type != bfd_link_hash_defweak)
  2495. h->esym.asym.sc = scAbs;
  2496. else
  2497. {
  2498. const char *name;
  2499. sec = h->root.root.u.def.section;
  2500. output_section = sec->output_section;
  2501. /* When making a shared library and symbol h is the one from
  2502. the another shared library, OUTPUT_SECTION may be null. */
  2503. if (output_section == NULL)
  2504. h->esym.asym.sc = scUndefined;
  2505. else
  2506. {
  2507. name = bfd_section_name (output_section);
  2508. if (strcmp (name, ".text") == 0)
  2509. h->esym.asym.sc = scText;
  2510. else if (strcmp (name, ".data") == 0)
  2511. h->esym.asym.sc = scData;
  2512. else if (strcmp (name, ".sdata") == 0)
  2513. h->esym.asym.sc = scSData;
  2514. else if (strcmp (name, ".rodata") == 0
  2515. || strcmp (name, ".rdata") == 0)
  2516. h->esym.asym.sc = scRData;
  2517. else if (strcmp (name, ".bss") == 0)
  2518. h->esym.asym.sc = scBss;
  2519. else if (strcmp (name, ".sbss") == 0)
  2520. h->esym.asym.sc = scSBss;
  2521. else if (strcmp (name, ".init") == 0)
  2522. h->esym.asym.sc = scInit;
  2523. else if (strcmp (name, ".fini") == 0)
  2524. h->esym.asym.sc = scFini;
  2525. else
  2526. h->esym.asym.sc = scAbs;
  2527. }
  2528. }
  2529. h->esym.asym.reserved = 0;
  2530. h->esym.asym.index = indexNil;
  2531. }
  2532. if (h->root.root.type == bfd_link_hash_common)
  2533. h->esym.asym.value = h->root.root.u.c.size;
  2534. else if (h->root.root.type == bfd_link_hash_defined
  2535. || h->root.root.type == bfd_link_hash_defweak)
  2536. {
  2537. if (h->esym.asym.sc == scCommon)
  2538. h->esym.asym.sc = scBss;
  2539. else if (h->esym.asym.sc == scSCommon)
  2540. h->esym.asym.sc = scSBss;
  2541. sec = h->root.root.u.def.section;
  2542. output_section = sec->output_section;
  2543. if (output_section != NULL)
  2544. h->esym.asym.value = (h->root.root.u.def.value
  2545. + sec->output_offset
  2546. + output_section->vma);
  2547. else
  2548. h->esym.asym.value = 0;
  2549. }
  2550. else
  2551. {
  2552. struct mips_elf_link_hash_entry *hd = h;
  2553. while (hd->root.root.type == bfd_link_hash_indirect)
  2554. hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
  2555. if (hd->needs_lazy_stub)
  2556. {
  2557. BFD_ASSERT (hd->root.plt.plist != NULL);
  2558. BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
  2559. /* Set type and value for a symbol with a function stub. */
  2560. h->esym.asym.st = stProc;
  2561. sec = hd->root.root.u.def.section;
  2562. if (sec == NULL)
  2563. h->esym.asym.value = 0;
  2564. else
  2565. {
  2566. output_section = sec->output_section;
  2567. if (output_section != NULL)
  2568. h->esym.asym.value = (hd->root.plt.plist->stub_offset
  2569. + sec->output_offset
  2570. + output_section->vma);
  2571. else
  2572. h->esym.asym.value = 0;
  2573. }
  2574. }
  2575. }
  2576. if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
  2577. h->root.root.root.string,
  2578. &h->esym))
  2579. {
  2580. einfo->failed = true;
  2581. return false;
  2582. }
  2583. return true;
  2584. }
  2585. /* A comparison routine used to sort .gptab entries. */
  2586. static int
  2587. gptab_compare (const void *p1, const void *p2)
  2588. {
  2589. const Elf32_gptab *a1 = p1;
  2590. const Elf32_gptab *a2 = p2;
  2591. return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
  2592. }
  2593. /* Functions to manage the got entry hash table. */
  2594. /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
  2595. hash number. */
  2596. static inline hashval_t
  2597. mips_elf_hash_bfd_vma (bfd_vma addr)
  2598. {
  2599. #ifdef BFD64
  2600. return addr + (addr >> 32);
  2601. #else
  2602. return addr;
  2603. #endif
  2604. }
  2605. static hashval_t
  2606. mips_elf_got_entry_hash (const void *entry_)
  2607. {
  2608. const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
  2609. return (entry->symndx
  2610. + ((entry->tls_type == GOT_TLS_LDM) << 18)
  2611. + (entry->tls_type == GOT_TLS_LDM ? 0
  2612. : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
  2613. : entry->symndx >= 0 ? (entry->abfd->id
  2614. + mips_elf_hash_bfd_vma (entry->d.addend))
  2615. : entry->d.h->root.root.root.hash));
  2616. }
  2617. static int
  2618. mips_elf_got_entry_eq (const void *entry1, const void *entry2)
  2619. {
  2620. const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
  2621. const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
  2622. return (e1->symndx == e2->symndx
  2623. && e1->tls_type == e2->tls_type
  2624. && (e1->tls_type == GOT_TLS_LDM ? true
  2625. : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
  2626. : e1->symndx >= 0 ? (e1->abfd == e2->abfd
  2627. && e1->d.addend == e2->d.addend)
  2628. : e2->abfd && e1->d.h == e2->d.h));
  2629. }
  2630. static hashval_t
  2631. mips_got_page_ref_hash (const void *ref_)
  2632. {
  2633. const struct mips_got_page_ref *ref;
  2634. ref = (const struct mips_got_page_ref *) ref_;
  2635. return ((ref->symndx >= 0
  2636. ? (hashval_t) (ref->u.abfd->id + ref->symndx)
  2637. : ref->u.h->root.root.root.hash)
  2638. + mips_elf_hash_bfd_vma (ref->addend));
  2639. }
  2640. static int
  2641. mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
  2642. {
  2643. const struct mips_got_page_ref *ref1, *ref2;
  2644. ref1 = (const struct mips_got_page_ref *) ref1_;
  2645. ref2 = (const struct mips_got_page_ref *) ref2_;
  2646. return (ref1->symndx == ref2->symndx
  2647. && (ref1->symndx < 0
  2648. ? ref1->u.h == ref2->u.h
  2649. : ref1->u.abfd == ref2->u.abfd)
  2650. && ref1->addend == ref2->addend);
  2651. }
  2652. static hashval_t
  2653. mips_got_page_entry_hash (const void *entry_)
  2654. {
  2655. const struct mips_got_page_entry *entry;
  2656. entry = (const struct mips_got_page_entry *) entry_;
  2657. return entry->sec->id;
  2658. }
  2659. static int
  2660. mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
  2661. {
  2662. const struct mips_got_page_entry *entry1, *entry2;
  2663. entry1 = (const struct mips_got_page_entry *) entry1_;
  2664. entry2 = (const struct mips_got_page_entry *) entry2_;
  2665. return entry1->sec == entry2->sec;
  2666. }
  2667. /* Create and return a new mips_got_info structure. */
  2668. static struct mips_got_info *
  2669. mips_elf_create_got_info (bfd *abfd)
  2670. {
  2671. struct mips_got_info *g;
  2672. g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
  2673. if (g == NULL)
  2674. return NULL;
  2675. g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
  2676. mips_elf_got_entry_eq, NULL);
  2677. if (g->got_entries == NULL)
  2678. return NULL;
  2679. g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
  2680. mips_got_page_ref_eq, NULL);
  2681. if (g->got_page_refs == NULL)
  2682. return NULL;
  2683. return g;
  2684. }
  2685. /* Return the GOT info for input bfd ABFD, trying to create a new one if
  2686. CREATE_P and if ABFD doesn't already have a GOT. */
  2687. static struct mips_got_info *
  2688. mips_elf_bfd_got (bfd *abfd, bool create_p)
  2689. {
  2690. struct mips_elf_obj_tdata *tdata;
  2691. if (!is_mips_elf (abfd))
  2692. return NULL;
  2693. tdata = mips_elf_tdata (abfd);
  2694. if (!tdata->got && create_p)
  2695. tdata->got = mips_elf_create_got_info (abfd);
  2696. return tdata->got;
  2697. }
  2698. /* Record that ABFD should use output GOT G. */
  2699. static void
  2700. mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
  2701. {
  2702. struct mips_elf_obj_tdata *tdata;
  2703. BFD_ASSERT (is_mips_elf (abfd));
  2704. tdata = mips_elf_tdata (abfd);
  2705. if (tdata->got)
  2706. {
  2707. /* The GOT structure itself and the hash table entries are
  2708. allocated to a bfd, but the hash tables aren't. */
  2709. htab_delete (tdata->got->got_entries);
  2710. htab_delete (tdata->got->got_page_refs);
  2711. if (tdata->got->got_page_entries)
  2712. htab_delete (tdata->got->got_page_entries);
  2713. }
  2714. tdata->got = g;
  2715. }
  2716. /* Return the dynamic relocation section. If it doesn't exist, try to
  2717. create a new it if CREATE_P, otherwise return NULL. Also return NULL
  2718. if creation fails. */
  2719. static asection *
  2720. mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
  2721. {
  2722. const char *dname;
  2723. asection *sreloc;
  2724. bfd *dynobj;
  2725. dname = MIPS_ELF_REL_DYN_NAME (info);
  2726. dynobj = elf_hash_table (info)->dynobj;
  2727. sreloc = bfd_get_linker_section (dynobj, dname);
  2728. if (sreloc == NULL && create_p)
  2729. {
  2730. sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
  2731. (SEC_ALLOC
  2732. | SEC_LOAD
  2733. | SEC_HAS_CONTENTS
  2734. | SEC_IN_MEMORY
  2735. | SEC_LINKER_CREATED
  2736. | SEC_READONLY));
  2737. if (sreloc == NULL
  2738. || !bfd_set_section_alignment (sreloc,
  2739. MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
  2740. return NULL;
  2741. }
  2742. return sreloc;
  2743. }
  2744. /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
  2745. static int
  2746. mips_elf_reloc_tls_type (unsigned int r_type)
  2747. {
  2748. if (tls_gd_reloc_p (r_type))
  2749. return GOT_TLS_GD;
  2750. if (tls_ldm_reloc_p (r_type))
  2751. return GOT_TLS_LDM;
  2752. if (tls_gottprel_reloc_p (r_type))
  2753. return GOT_TLS_IE;
  2754. return GOT_TLS_NONE;
  2755. }
  2756. /* Return the number of GOT slots needed for GOT TLS type TYPE. */
  2757. static int
  2758. mips_tls_got_entries (unsigned int type)
  2759. {
  2760. switch (type)
  2761. {
  2762. case GOT_TLS_GD:
  2763. case GOT_TLS_LDM:
  2764. return 2;
  2765. case GOT_TLS_IE:
  2766. return 1;
  2767. case GOT_TLS_NONE:
  2768. return 0;
  2769. }
  2770. abort ();
  2771. }
  2772. /* Count the number of relocations needed for a TLS GOT entry, with
  2773. access types from TLS_TYPE, and symbol H (or a local symbol if H
  2774. is NULL). */
  2775. static int
  2776. mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
  2777. struct elf_link_hash_entry *h)
  2778. {
  2779. int indx = 0;
  2780. bool need_relocs = false;
  2781. bool dyn = elf_hash_table (info)->dynamic_sections_created;
  2782. if (h != NULL
  2783. && h->dynindx != -1
  2784. && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
  2785. && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
  2786. indx = h->dynindx;
  2787. if ((bfd_link_dll (info) || indx != 0)
  2788. && (h == NULL
  2789. || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2790. || h->root.type != bfd_link_hash_undefweak))
  2791. need_relocs = true;
  2792. if (!need_relocs)
  2793. return 0;
  2794. switch (tls_type)
  2795. {
  2796. case GOT_TLS_GD:
  2797. return indx != 0 ? 2 : 1;
  2798. case GOT_TLS_IE:
  2799. return 1;
  2800. case GOT_TLS_LDM:
  2801. return bfd_link_dll (info) ? 1 : 0;
  2802. default:
  2803. return 0;
  2804. }
  2805. }
  2806. /* Add the number of GOT entries and TLS relocations required by ENTRY
  2807. to G. */
  2808. static void
  2809. mips_elf_count_got_entry (struct bfd_link_info *info,
  2810. struct mips_got_info *g,
  2811. struct mips_got_entry *entry)
  2812. {
  2813. if (entry->tls_type)
  2814. {
  2815. g->tls_gotno += mips_tls_got_entries (entry->tls_type);
  2816. g->relocs += mips_tls_got_relocs (info, entry->tls_type,
  2817. entry->symndx < 0
  2818. ? &entry->d.h->root : NULL);
  2819. }
  2820. else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
  2821. g->local_gotno += 1;
  2822. else
  2823. g->global_gotno += 1;
  2824. }
  2825. /* Output a simple dynamic relocation into SRELOC. */
  2826. static void
  2827. mips_elf_output_dynamic_relocation (bfd *output_bfd,
  2828. asection *sreloc,
  2829. unsigned long reloc_index,
  2830. unsigned long indx,
  2831. int r_type,
  2832. bfd_vma offset)
  2833. {
  2834. Elf_Internal_Rela rel[3];
  2835. memset (rel, 0, sizeof (rel));
  2836. rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
  2837. rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
  2838. if (ABI_64_P (output_bfd))
  2839. {
  2840. (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
  2841. (output_bfd, &rel[0],
  2842. (sreloc->contents
  2843. + reloc_index * sizeof (Elf64_Mips_External_Rel)));
  2844. }
  2845. else
  2846. bfd_elf32_swap_reloc_out
  2847. (output_bfd, &rel[0],
  2848. (sreloc->contents
  2849. + reloc_index * sizeof (Elf32_External_Rel)));
  2850. }
  2851. /* Initialize a set of TLS GOT entries for one symbol. */
  2852. static void
  2853. mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
  2854. struct mips_got_entry *entry,
  2855. struct mips_elf_link_hash_entry *h,
  2856. bfd_vma value)
  2857. {
  2858. bool dyn = elf_hash_table (info)->dynamic_sections_created;
  2859. struct mips_elf_link_hash_table *htab;
  2860. int indx;
  2861. asection *sreloc, *sgot;
  2862. bfd_vma got_offset, got_offset2;
  2863. bool need_relocs = false;
  2864. htab = mips_elf_hash_table (info);
  2865. if (htab == NULL)
  2866. return;
  2867. sgot = htab->root.sgot;
  2868. indx = 0;
  2869. if (h != NULL
  2870. && h->root.dynindx != -1
  2871. && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
  2872. && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
  2873. indx = h->root.dynindx;
  2874. if (entry->tls_initialized)
  2875. return;
  2876. if ((bfd_link_dll (info) || indx != 0)
  2877. && (h == NULL
  2878. || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
  2879. || h->root.type != bfd_link_hash_undefweak))
  2880. need_relocs = true;
  2881. /* MINUS_ONE means the symbol is not defined in this object. It may not
  2882. be defined at all; assume that the value doesn't matter in that
  2883. case. Otherwise complain if we would use the value. */
  2884. BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
  2885. || h->root.root.type == bfd_link_hash_undefweak);
  2886. /* Emit necessary relocations. */
  2887. sreloc = mips_elf_rel_dyn_section (info, false);
  2888. got_offset = entry->gotidx;
  2889. switch (entry->tls_type)
  2890. {
  2891. case GOT_TLS_GD:
  2892. /* General Dynamic. */
  2893. got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
  2894. if (need_relocs)
  2895. {
  2896. mips_elf_output_dynamic_relocation
  2897. (abfd, sreloc, sreloc->reloc_count++, indx,
  2898. ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
  2899. sgot->output_offset + sgot->output_section->vma + got_offset);
  2900. if (indx)
  2901. mips_elf_output_dynamic_relocation
  2902. (abfd, sreloc, sreloc->reloc_count++, indx,
  2903. ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
  2904. sgot->output_offset + sgot->output_section->vma + got_offset2);
  2905. else
  2906. MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
  2907. sgot->contents + got_offset2);
  2908. }
  2909. else
  2910. {
  2911. MIPS_ELF_PUT_WORD (abfd, 1,
  2912. sgot->contents + got_offset);
  2913. MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
  2914. sgot->contents + got_offset2);
  2915. }
  2916. break;
  2917. case GOT_TLS_IE:
  2918. /* Initial Exec model. */
  2919. if (need_relocs)
  2920. {
  2921. if (indx == 0)
  2922. MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
  2923. sgot->contents + got_offset);
  2924. else
  2925. MIPS_ELF_PUT_WORD (abfd, 0,
  2926. sgot->contents + got_offset);
  2927. mips_elf_output_dynamic_relocation
  2928. (abfd, sreloc, sreloc->reloc_count++, indx,
  2929. ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
  2930. sgot->output_offset + sgot->output_section->vma + got_offset);
  2931. }
  2932. else
  2933. MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
  2934. sgot->contents + got_offset);
  2935. break;
  2936. case GOT_TLS_LDM:
  2937. /* The initial offset is zero, and the LD offsets will include the
  2938. bias by DTP_OFFSET. */
  2939. MIPS_ELF_PUT_WORD (abfd, 0,
  2940. sgot->contents + got_offset
  2941. + MIPS_ELF_GOT_SIZE (abfd));
  2942. if (!bfd_link_dll (info))
  2943. MIPS_ELF_PUT_WORD (abfd, 1,
  2944. sgot->contents + got_offset);
  2945. else
  2946. mips_elf_output_dynamic_relocation
  2947. (abfd, sreloc, sreloc->reloc_count++, indx,
  2948. ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
  2949. sgot->output_offset + sgot->output_section->vma + got_offset);
  2950. break;
  2951. default:
  2952. abort ();
  2953. }
  2954. entry->tls_initialized = true;
  2955. }
  2956. /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
  2957. for global symbol H. .got.plt comes before the GOT, so the offset
  2958. will be negative. */
  2959. static bfd_vma
  2960. mips_elf_gotplt_index (struct bfd_link_info *info,
  2961. struct elf_link_hash_entry *h)
  2962. {
  2963. bfd_vma got_address, got_value;
  2964. struct mips_elf_link_hash_table *htab;
  2965. htab = mips_elf_hash_table (info);
  2966. BFD_ASSERT (htab != NULL);
  2967. BFD_ASSERT (h->plt.plist != NULL);
  2968. BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
  2969. /* Calculate the address of the associated .got.plt entry. */
  2970. got_address = (htab->root.sgotplt->output_section->vma
  2971. + htab->root.sgotplt->output_offset
  2972. + (h->plt.plist->gotplt_index
  2973. * MIPS_ELF_GOT_SIZE (info->output_bfd)));
  2974. /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
  2975. got_value = (htab->root.hgot->root.u.def.section->output_section->vma
  2976. + htab->root.hgot->root.u.def.section->output_offset
  2977. + htab->root.hgot->root.u.def.value);
  2978. return got_address - got_value;
  2979. }
  2980. /* Return the GOT offset for address VALUE. If there is not yet a GOT
  2981. entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
  2982. create a TLS GOT entry instead. Return -1 if no satisfactory GOT
  2983. offset can be found. */
  2984. static bfd_vma
  2985. mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
  2986. bfd_vma value, unsigned long r_symndx,
  2987. struct mips_elf_link_hash_entry *h, int r_type)
  2988. {
  2989. struct mips_elf_link_hash_table *htab;
  2990. struct mips_got_entry *entry;
  2991. htab = mips_elf_hash_table (info);
  2992. BFD_ASSERT (htab != NULL);
  2993. entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
  2994. r_symndx, h, r_type);
  2995. if (!entry)
  2996. return MINUS_ONE;
  2997. if (entry->tls_type)
  2998. mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
  2999. return entry->gotidx;
  3000. }
  3001. /* Return the GOT index of global symbol H in the primary GOT. */
  3002. static bfd_vma
  3003. mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
  3004. struct elf_link_hash_entry *h)
  3005. {
  3006. struct mips_elf_link_hash_table *htab;
  3007. long global_got_dynindx;
  3008. struct mips_got_info *g;
  3009. bfd_vma got_index;
  3010. htab = mips_elf_hash_table (info);
  3011. BFD_ASSERT (htab != NULL);
  3012. global_got_dynindx = 0;
  3013. if (htab->global_gotsym != NULL)
  3014. global_got_dynindx = htab->global_gotsym->dynindx;
  3015. /* Once we determine the global GOT entry with the lowest dynamic
  3016. symbol table index, we must put all dynamic symbols with greater
  3017. indices into the primary GOT. That makes it easy to calculate the
  3018. GOT offset. */
  3019. BFD_ASSERT (h->dynindx >= global_got_dynindx);
  3020. g = mips_elf_bfd_got (obfd, false);
  3021. got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
  3022. * MIPS_ELF_GOT_SIZE (obfd));
  3023. BFD_ASSERT (got_index < htab->root.sgot->size);
  3024. return got_index;
  3025. }
  3026. /* Return the GOT index for the global symbol indicated by H, which is
  3027. referenced by a relocation of type R_TYPE in IBFD. */
  3028. static bfd_vma
  3029. mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
  3030. struct elf_link_hash_entry *h, int r_type)
  3031. {
  3032. struct mips_elf_link_hash_table *htab;
  3033. struct mips_got_info *g;
  3034. struct mips_got_entry lookup, *entry;
  3035. bfd_vma gotidx;
  3036. htab = mips_elf_hash_table (info);
  3037. BFD_ASSERT (htab != NULL);
  3038. g = mips_elf_bfd_got (ibfd, false);
  3039. BFD_ASSERT (g);
  3040. lookup.tls_type = mips_elf_reloc_tls_type (r_type);
  3041. if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
  3042. return mips_elf_primary_global_got_index (obfd, info, h);
  3043. lookup.abfd = ibfd;
  3044. lookup.symndx = -1;
  3045. lookup.d.h = (struct mips_elf_link_hash_entry *) h;
  3046. entry = htab_find (g->got_entries, &lookup);
  3047. BFD_ASSERT (entry);
  3048. gotidx = entry->gotidx;
  3049. BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
  3050. if (lookup.tls_type)
  3051. {
  3052. bfd_vma value = MINUS_ONE;
  3053. if ((h->root.type == bfd_link_hash_defined
  3054. || h->root.type == bfd_link_hash_defweak)
  3055. && h->root.u.def.section->output_section)
  3056. value = (h->root.u.def.value
  3057. + h->root.u.def.section->output_offset
  3058. + h->root.u.def.section->output_section->vma);
  3059. mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
  3060. }
  3061. return gotidx;
  3062. }
  3063. /* Find a GOT page entry that points to within 32KB of VALUE. These
  3064. entries are supposed to be placed at small offsets in the GOT, i.e.,
  3065. within 32KB of GP. Return the index of the GOT entry, or -1 if no
  3066. entry could be created. If OFFSETP is nonnull, use it to return the
  3067. offset of the GOT entry from VALUE. */
  3068. static bfd_vma
  3069. mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
  3070. bfd_vma value, bfd_vma *offsetp)
  3071. {
  3072. bfd_vma page, got_index;
  3073. struct mips_got_entry *entry;
  3074. page = (value + 0x8000) & ~(bfd_vma) 0xffff;
  3075. entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
  3076. NULL, R_MIPS_GOT_PAGE);
  3077. if (!entry)
  3078. return MINUS_ONE;
  3079. got_index = entry->gotidx;
  3080. if (offsetp)
  3081. *offsetp = value - entry->d.address;
  3082. return got_index;
  3083. }
  3084. /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
  3085. EXTERNAL is true if the relocation was originally against a global
  3086. symbol that binds locally. */
  3087. static bfd_vma
  3088. mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
  3089. bfd_vma value, bool external)
  3090. {
  3091. struct mips_got_entry *entry;
  3092. /* GOT16 relocations against local symbols are followed by a LO16
  3093. relocation; those against global symbols are not. Thus if the
  3094. symbol was originally local, the GOT16 relocation should load the
  3095. equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
  3096. if (! external)
  3097. value = mips_elf_high (value) << 16;
  3098. /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
  3099. R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
  3100. same in all cases. */
  3101. entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
  3102. NULL, R_MIPS_GOT16);
  3103. if (entry)
  3104. return entry->gotidx;
  3105. else
  3106. return MINUS_ONE;
  3107. }
  3108. /* Returns the offset for the entry at the INDEXth position
  3109. in the GOT. */
  3110. static bfd_vma
  3111. mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
  3112. bfd *input_bfd, bfd_vma got_index)
  3113. {
  3114. struct mips_elf_link_hash_table *htab;
  3115. asection *sgot;
  3116. bfd_vma gp;
  3117. htab = mips_elf_hash_table (info);
  3118. BFD_ASSERT (htab != NULL);
  3119. sgot = htab->root.sgot;
  3120. gp = _bfd_get_gp_value (output_bfd)
  3121. + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
  3122. return sgot->output_section->vma + sgot->output_offset + got_index - gp;
  3123. }
  3124. /* Create and return a local GOT entry for VALUE, which was calculated
  3125. from a symbol belonging to INPUT_SECTON. Return NULL if it could not
  3126. be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
  3127. instead. */
  3128. static struct mips_got_entry *
  3129. mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
  3130. bfd *ibfd, bfd_vma value,
  3131. unsigned long r_symndx,
  3132. struct mips_elf_link_hash_entry *h,
  3133. int r_type)
  3134. {
  3135. struct mips_got_entry lookup, *entry;
  3136. void **loc;
  3137. struct mips_got_info *g;
  3138. struct mips_elf_link_hash_table *htab;
  3139. bfd_vma gotidx;
  3140. htab = mips_elf_hash_table (info);
  3141. BFD_ASSERT (htab != NULL);
  3142. g = mips_elf_bfd_got (ibfd, false);
  3143. if (g == NULL)
  3144. {
  3145. g = mips_elf_bfd_got (abfd, false);
  3146. BFD_ASSERT (g != NULL);
  3147. }
  3148. /* This function shouldn't be called for symbols that live in the global
  3149. area of the GOT. */
  3150. BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
  3151. lookup.tls_type = mips_elf_reloc_tls_type (r_type);
  3152. if (lookup.tls_type)
  3153. {
  3154. lookup.abfd = ibfd;
  3155. if (tls_ldm_reloc_p (r_type))
  3156. {
  3157. lookup.symndx = 0;
  3158. lookup.d.addend = 0;
  3159. }
  3160. else if (h == NULL)
  3161. {
  3162. lookup.symndx = r_symndx;
  3163. lookup.d.addend = 0;
  3164. }
  3165. else
  3166. {
  3167. lookup.symndx = -1;
  3168. lookup.d.h = h;
  3169. }
  3170. entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
  3171. BFD_ASSERT (entry);
  3172. gotidx = entry->gotidx;
  3173. BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
  3174. return entry;
  3175. }
  3176. lookup.abfd = NULL;
  3177. lookup.symndx = -1;
  3178. lookup.d.address = value;
  3179. loc = htab_find_slot (g->got_entries, &lookup, INSERT);
  3180. if (!loc)
  3181. return NULL;
  3182. entry = (struct mips_got_entry *) *loc;
  3183. if (entry)
  3184. return entry;
  3185. if (g->assigned_low_gotno > g->assigned_high_gotno)
  3186. {
  3187. /* We didn't allocate enough space in the GOT. */
  3188. _bfd_error_handler
  3189. (_("not enough GOT space for local GOT entries"));
  3190. bfd_set_error (bfd_error_bad_value);
  3191. return NULL;
  3192. }
  3193. entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
  3194. if (!entry)
  3195. return NULL;
  3196. if (got16_reloc_p (r_type)
  3197. || call16_reloc_p (r_type)
  3198. || got_page_reloc_p (r_type)
  3199. || got_disp_reloc_p (r_type))
  3200. lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
  3201. else
  3202. lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
  3203. *entry = lookup;
  3204. *loc = entry;
  3205. MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
  3206. /* These GOT entries need a dynamic relocation on VxWorks. */
  3207. if (htab->root.target_os == is_vxworks)
  3208. {
  3209. Elf_Internal_Rela outrel;
  3210. asection *s;
  3211. bfd_byte *rloc;
  3212. bfd_vma got_address;
  3213. s = mips_elf_rel_dyn_section (info, false);
  3214. got_address = (htab->root.sgot->output_section->vma
  3215. + htab->root.sgot->output_offset
  3216. + entry->gotidx);
  3217. rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
  3218. outrel.r_offset = got_address;
  3219. outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
  3220. outrel.r_addend = value;
  3221. bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
  3222. }
  3223. return entry;
  3224. }
  3225. /* Return the number of dynamic section symbols required by OUTPUT_BFD.
  3226. The number might be exact or a worst-case estimate, depending on how
  3227. much information is available to elf_backend_omit_section_dynsym at
  3228. the current linking stage. */
  3229. static bfd_size_type
  3230. count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
  3231. {
  3232. bfd_size_type count;
  3233. count = 0;
  3234. if (bfd_link_pic (info)
  3235. || elf_hash_table (info)->is_relocatable_executable)
  3236. {
  3237. asection *p;
  3238. const struct elf_backend_data *bed;
  3239. bed = get_elf_backend_data (output_bfd);
  3240. for (p = output_bfd->sections; p ; p = p->next)
  3241. if ((p->flags & SEC_EXCLUDE) == 0
  3242. && (p->flags & SEC_ALLOC) != 0
  3243. && elf_hash_table (info)->dynamic_relocs
  3244. && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
  3245. ++count;
  3246. }
  3247. return count;
  3248. }
  3249. /* Sort the dynamic symbol table so that symbols that need GOT entries
  3250. appear towards the end. */
  3251. static bool
  3252. mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
  3253. {
  3254. struct mips_elf_link_hash_table *htab;
  3255. struct mips_elf_hash_sort_data hsd;
  3256. struct mips_got_info *g;
  3257. htab = mips_elf_hash_table (info);
  3258. BFD_ASSERT (htab != NULL);
  3259. if (htab->root.dynsymcount == 0)
  3260. return true;
  3261. g = htab->got_info;
  3262. if (g == NULL)
  3263. return true;
  3264. hsd.low = NULL;
  3265. hsd.max_unref_got_dynindx
  3266. = hsd.min_got_dynindx
  3267. = (htab->root.dynsymcount - g->reloc_only_gotno);
  3268. /* Add 1 to local symbol indices to account for the mandatory NULL entry
  3269. at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
  3270. hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
  3271. hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
  3272. hsd.output_bfd = abfd;
  3273. if (htab->root.dynobj != NULL
  3274. && htab->root.dynamic_sections_created
  3275. && info->emit_gnu_hash)
  3276. {
  3277. asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
  3278. BFD_ASSERT (s != NULL);
  3279. hsd.mipsxhash = s->contents;
  3280. BFD_ASSERT (hsd.mipsxhash != NULL);
  3281. }
  3282. else
  3283. hsd.mipsxhash = NULL;
  3284. mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
  3285. /* There should have been enough room in the symbol table to
  3286. accommodate both the GOT and non-GOT symbols. */
  3287. BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
  3288. BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
  3289. BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
  3290. BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
  3291. /* Now we know which dynamic symbol has the lowest dynamic symbol
  3292. table index in the GOT. */
  3293. htab->global_gotsym = hsd.low;
  3294. return true;
  3295. }
  3296. /* If H needs a GOT entry, assign it the highest available dynamic
  3297. index. Otherwise, assign it the lowest available dynamic
  3298. index. */
  3299. static bool
  3300. mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
  3301. {
  3302. struct mips_elf_hash_sort_data *hsd = data;
  3303. /* Symbols without dynamic symbol table entries aren't interesting
  3304. at all. */
  3305. if (h->root.dynindx == -1)
  3306. return true;
  3307. switch (h->global_got_area)
  3308. {
  3309. case GGA_NONE:
  3310. if (h->root.forced_local)
  3311. h->root.dynindx = hsd->max_local_dynindx++;
  3312. else
  3313. h->root.dynindx = hsd->max_non_got_dynindx++;
  3314. break;
  3315. case GGA_NORMAL:
  3316. h->root.dynindx = --hsd->min_got_dynindx;
  3317. hsd->low = (struct elf_link_hash_entry *) h;
  3318. break;
  3319. case GGA_RELOC_ONLY:
  3320. if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
  3321. hsd->low = (struct elf_link_hash_entry *) h;
  3322. h->root.dynindx = hsd->max_unref_got_dynindx++;
  3323. break;
  3324. }
  3325. /* Populate the .MIPS.xhash translation table entry with
  3326. the symbol dynindx. */
  3327. if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
  3328. bfd_put_32 (hsd->output_bfd, h->root.dynindx,
  3329. hsd->mipsxhash + h->mipsxhash_loc);
  3330. return true;
  3331. }
  3332. /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
  3333. (which is owned by the caller and shouldn't be added to the
  3334. hash table directly). */
  3335. static bool
  3336. mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
  3337. struct mips_got_entry *lookup)
  3338. {
  3339. struct mips_elf_link_hash_table *htab;
  3340. struct mips_got_entry *entry;
  3341. struct mips_got_info *g;
  3342. void **loc, **bfd_loc;
  3343. /* Make sure there's a slot for this entry in the master GOT. */
  3344. htab = mips_elf_hash_table (info);
  3345. g = htab->got_info;
  3346. loc = htab_find_slot (g->got_entries, lookup, INSERT);
  3347. if (!loc)
  3348. return false;
  3349. /* Populate the entry if it isn't already. */
  3350. entry = (struct mips_got_entry *) *loc;
  3351. if (!entry)
  3352. {
  3353. entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
  3354. if (!entry)
  3355. return false;
  3356. lookup->tls_initialized = false;
  3357. lookup->gotidx = -1;
  3358. *entry = *lookup;
  3359. *loc = entry;
  3360. }
  3361. /* Reuse the same GOT entry for the BFD's GOT. */
  3362. g = mips_elf_bfd_got (abfd, true);
  3363. if (!g)
  3364. return false;
  3365. bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
  3366. if (!bfd_loc)
  3367. return false;
  3368. if (!*bfd_loc)
  3369. *bfd_loc = entry;
  3370. return true;
  3371. }
  3372. /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
  3373. entry for it. FOR_CALL is true if the caller is only interested in
  3374. using the GOT entry for calls. */
  3375. static bool
  3376. mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
  3377. bfd *abfd, struct bfd_link_info *info,
  3378. bool for_call, int r_type)
  3379. {
  3380. struct mips_elf_link_hash_table *htab;
  3381. struct mips_elf_link_hash_entry *hmips;
  3382. struct mips_got_entry entry;
  3383. unsigned char tls_type;
  3384. htab = mips_elf_hash_table (info);
  3385. BFD_ASSERT (htab != NULL);
  3386. hmips = (struct mips_elf_link_hash_entry *) h;
  3387. if (!for_call)
  3388. hmips->got_only_for_calls = false;
  3389. /* A global symbol in the GOT must also be in the dynamic symbol
  3390. table. */
  3391. if (h->dynindx == -1)
  3392. {
  3393. switch (ELF_ST_VISIBILITY (h->other))
  3394. {
  3395. case STV_INTERNAL:
  3396. case STV_HIDDEN:
  3397. _bfd_mips_elf_hide_symbol (info, h, true);
  3398. break;
  3399. }
  3400. if (!bfd_elf_link_record_dynamic_symbol (info, h))
  3401. return false;
  3402. }
  3403. tls_type = mips_elf_reloc_tls_type (r_type);
  3404. if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
  3405. hmips->global_got_area = GGA_NORMAL;
  3406. entry.abfd = abfd;
  3407. entry.symndx = -1;
  3408. entry.d.h = (struct mips_elf_link_hash_entry *) h;
  3409. entry.tls_type = tls_type;
  3410. return mips_elf_record_got_entry (info, abfd, &entry);
  3411. }
  3412. /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
  3413. where SYMNDX is a local symbol. Reserve a GOT entry for it. */
  3414. static bool
  3415. mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
  3416. struct bfd_link_info *info, int r_type)
  3417. {
  3418. struct mips_elf_link_hash_table *htab;
  3419. struct mips_got_info *g;
  3420. struct mips_got_entry entry;
  3421. htab = mips_elf_hash_table (info);
  3422. BFD_ASSERT (htab != NULL);
  3423. g = htab->got_info;
  3424. BFD_ASSERT (g != NULL);
  3425. entry.abfd = abfd;
  3426. entry.symndx = symndx;
  3427. entry.d.addend = addend;
  3428. entry.tls_type = mips_elf_reloc_tls_type (r_type);
  3429. return mips_elf_record_got_entry (info, abfd, &entry);
  3430. }
  3431. /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
  3432. H is the symbol's hash table entry, or null if SYMNDX is local
  3433. to ABFD. */
  3434. static bool
  3435. mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
  3436. long symndx, struct elf_link_hash_entry *h,
  3437. bfd_signed_vma addend)
  3438. {
  3439. struct mips_elf_link_hash_table *htab;
  3440. struct mips_got_info *g1, *g2;
  3441. struct mips_got_page_ref lookup, *entry;
  3442. void **loc, **bfd_loc;
  3443. htab = mips_elf_hash_table (info);
  3444. BFD_ASSERT (htab != NULL);
  3445. g1 = htab->got_info;
  3446. BFD_ASSERT (g1 != NULL);
  3447. if (h)
  3448. {
  3449. lookup.symndx = -1;
  3450. lookup.u.h = (struct mips_elf_link_hash_entry *) h;
  3451. }
  3452. else
  3453. {
  3454. lookup.symndx = symndx;
  3455. lookup.u.abfd = abfd;
  3456. }
  3457. lookup.addend = addend;
  3458. loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
  3459. if (loc == NULL)
  3460. return false;
  3461. entry = (struct mips_got_page_ref *) *loc;
  3462. if (!entry)
  3463. {
  3464. entry = bfd_alloc (abfd, sizeof (*entry));
  3465. if (!entry)
  3466. return false;
  3467. *entry = lookup;
  3468. *loc = entry;
  3469. }
  3470. /* Add the same entry to the BFD's GOT. */
  3471. g2 = mips_elf_bfd_got (abfd, true);
  3472. if (!g2)
  3473. return false;
  3474. bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
  3475. if (!bfd_loc)
  3476. return false;
  3477. if (!*bfd_loc)
  3478. *bfd_loc = entry;
  3479. return true;
  3480. }
  3481. /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
  3482. static void
  3483. mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
  3484. unsigned int n)
  3485. {
  3486. asection *s;
  3487. struct mips_elf_link_hash_table *htab;
  3488. htab = mips_elf_hash_table (info);
  3489. BFD_ASSERT (htab != NULL);
  3490. s = mips_elf_rel_dyn_section (info, false);
  3491. BFD_ASSERT (s != NULL);
  3492. if (htab->root.target_os == is_vxworks)
  3493. s->size += n * MIPS_ELF_RELA_SIZE (abfd);
  3494. else
  3495. {
  3496. if (s->size == 0)
  3497. {
  3498. /* Make room for a null element. */
  3499. s->size += MIPS_ELF_REL_SIZE (abfd);
  3500. ++s->reloc_count;
  3501. }
  3502. s->size += n * MIPS_ELF_REL_SIZE (abfd);
  3503. }
  3504. }
  3505. /* A htab_traverse callback for GOT entries, with DATA pointing to a
  3506. mips_elf_traverse_got_arg structure. Count the number of GOT
  3507. entries and TLS relocs. Set DATA->value to true if we need
  3508. to resolve indirect or warning symbols and then recreate the GOT. */
  3509. static int
  3510. mips_elf_check_recreate_got (void **entryp, void *data)
  3511. {
  3512. struct mips_got_entry *entry;
  3513. struct mips_elf_traverse_got_arg *arg;
  3514. entry = (struct mips_got_entry *) *entryp;
  3515. arg = (struct mips_elf_traverse_got_arg *) data;
  3516. if (entry->abfd != NULL && entry->symndx == -1)
  3517. {
  3518. struct mips_elf_link_hash_entry *h;
  3519. h = entry->d.h;
  3520. if (h->root.root.type == bfd_link_hash_indirect
  3521. || h->root.root.type == bfd_link_hash_warning)
  3522. {
  3523. arg->value = true;
  3524. return 0;
  3525. }
  3526. }
  3527. mips_elf_count_got_entry (arg->info, arg->g, entry);
  3528. return 1;
  3529. }
  3530. /* A htab_traverse callback for GOT entries, with DATA pointing to a
  3531. mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
  3532. converting entries for indirect and warning symbols into entries
  3533. for the target symbol. Set DATA->g to null on error. */
  3534. static int
  3535. mips_elf_recreate_got (void **entryp, void *data)
  3536. {
  3537. struct mips_got_entry new_entry, *entry;
  3538. struct mips_elf_traverse_got_arg *arg;
  3539. void **slot;
  3540. entry = (struct mips_got_entry *) *entryp;
  3541. arg = (struct mips_elf_traverse_got_arg *) data;
  3542. if (entry->abfd != NULL
  3543. && entry->symndx == -1
  3544. && (entry->d.h->root.root.type == bfd_link_hash_indirect
  3545. || entry->d.h->root.root.type == bfd_link_hash_warning))
  3546. {
  3547. struct mips_elf_link_hash_entry *h;
  3548. new_entry = *entry;
  3549. entry = &new_entry;
  3550. h = entry->d.h;
  3551. do
  3552. {
  3553. BFD_ASSERT (h->global_got_area == GGA_NONE);
  3554. h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
  3555. }
  3556. while (h->root.root.type == bfd_link_hash_indirect
  3557. || h->root.root.type == bfd_link_hash_warning);
  3558. entry->d.h = h;
  3559. }
  3560. slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
  3561. if (slot == NULL)
  3562. {
  3563. arg->g = NULL;
  3564. return 0;
  3565. }
  3566. if (*slot == NULL)
  3567. {
  3568. if (entry == &new_entry)
  3569. {
  3570. entry = bfd_alloc (entry->abfd, sizeof (*entry));
  3571. if (!entry)
  3572. {
  3573. arg->g = NULL;
  3574. return 0;
  3575. }
  3576. *entry = new_entry;
  3577. }
  3578. *slot = entry;
  3579. mips_elf_count_got_entry (arg->info, arg->g, entry);
  3580. }
  3581. return 1;
  3582. }
  3583. /* Return the maximum number of GOT page entries required for RANGE. */
  3584. static bfd_vma
  3585. mips_elf_pages_for_range (const struct mips_got_page_range *range)
  3586. {
  3587. return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
  3588. }
  3589. /* Record that G requires a page entry that can reach SEC + ADDEND. */
  3590. static bool
  3591. mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
  3592. asection *sec, bfd_signed_vma addend)
  3593. {
  3594. struct mips_got_info *g = arg->g;
  3595. struct mips_got_page_entry lookup, *entry;
  3596. struct mips_got_page_range **range_ptr, *range;
  3597. bfd_vma old_pages, new_pages;
  3598. void **loc;
  3599. /* Find the mips_got_page_entry hash table entry for this section. */
  3600. lookup.sec = sec;
  3601. loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
  3602. if (loc == NULL)
  3603. return false;
  3604. /* Create a mips_got_page_entry if this is the first time we've
  3605. seen the section. */
  3606. entry = (struct mips_got_page_entry *) *loc;
  3607. if (!entry)
  3608. {
  3609. entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
  3610. if (!entry)
  3611. return false;
  3612. entry->sec = sec;
  3613. *loc = entry;
  3614. }
  3615. /* Skip over ranges whose maximum extent cannot share a page entry
  3616. with ADDEND. */
  3617. range_ptr = &entry->ranges;
  3618. while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
  3619. range_ptr = &(*range_ptr)->next;
  3620. /* If we scanned to the end of the list, or found a range whose
  3621. minimum extent cannot share a page entry with ADDEND, create
  3622. a new singleton range. */
  3623. range = *range_ptr;
  3624. if (!range || addend < range->min_addend - 0xffff)
  3625. {
  3626. range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
  3627. if (!range)
  3628. return false;
  3629. range->next = *range_ptr;
  3630. range->min_addend = addend;
  3631. range->max_addend = addend;
  3632. *range_ptr = range;
  3633. entry->num_pages++;
  3634. g->page_gotno++;
  3635. return true;
  3636. }
  3637. /* Remember how many pages the old range contributed. */
  3638. old_pages = mips_elf_pages_for_range (range);
  3639. /* Update the ranges. */
  3640. if (addend < range->min_addend)
  3641. range->min_addend = addend;
  3642. else if (addend > range->max_addend)
  3643. {
  3644. if (range->next && addend >= range->next->min_addend - 0xffff)
  3645. {
  3646. old_pages += mips_elf_pages_for_range (range->next);
  3647. range->max_addend = range->next->max_addend;
  3648. range->next = range->next->next;
  3649. }
  3650. else
  3651. range->max_addend = addend;
  3652. }
  3653. /* Record any change in the total estimate. */
  3654. new_pages = mips_elf_pages_for_range (range);
  3655. if (old_pages != new_pages)
  3656. {
  3657. entry->num_pages += new_pages - old_pages;
  3658. g->page_gotno += new_pages - old_pages;
  3659. }
  3660. return true;
  3661. }
  3662. /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
  3663. and for which DATA points to a mips_elf_traverse_got_arg. Work out
  3664. whether the page reference described by *REFP needs a GOT page entry,
  3665. and record that entry in DATA->g if so. Set DATA->g to null on failure. */
  3666. static int
  3667. mips_elf_resolve_got_page_ref (void **refp, void *data)
  3668. {
  3669. struct mips_got_page_ref *ref;
  3670. struct mips_elf_traverse_got_arg *arg;
  3671. struct mips_elf_link_hash_table *htab;
  3672. asection *sec;
  3673. bfd_vma addend;
  3674. ref = (struct mips_got_page_ref *) *refp;
  3675. arg = (struct mips_elf_traverse_got_arg *) data;
  3676. htab = mips_elf_hash_table (arg->info);
  3677. if (ref->symndx < 0)
  3678. {
  3679. struct mips_elf_link_hash_entry *h;
  3680. /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
  3681. h = ref->u.h;
  3682. if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
  3683. return 1;
  3684. /* Ignore undefined symbols; we'll issue an error later if
  3685. appropriate. */
  3686. if (!((h->root.root.type == bfd_link_hash_defined
  3687. || h->root.root.type == bfd_link_hash_defweak)
  3688. && h->root.root.u.def.section))
  3689. return 1;
  3690. sec = h->root.root.u.def.section;
  3691. addend = h->root.root.u.def.value + ref->addend;
  3692. }
  3693. else
  3694. {
  3695. Elf_Internal_Sym *isym;
  3696. /* Read in the symbol. */
  3697. isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
  3698. ref->symndx);
  3699. if (isym == NULL)
  3700. {
  3701. arg->g = NULL;
  3702. return 0;
  3703. }
  3704. /* Get the associated input section. */
  3705. sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
  3706. if (sec == NULL)
  3707. {
  3708. arg->g = NULL;
  3709. return 0;
  3710. }
  3711. /* If this is a mergable section, work out the section and offset
  3712. of the merged data. For section symbols, the addend specifies
  3713. of the offset _of_ the first byte in the data, otherwise it
  3714. specifies the offset _from_ the first byte. */
  3715. if (sec->flags & SEC_MERGE)
  3716. {
  3717. void *secinfo;
  3718. secinfo = elf_section_data (sec)->sec_info;
  3719. if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  3720. addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
  3721. isym->st_value + ref->addend);
  3722. else
  3723. addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
  3724. isym->st_value) + ref->addend;
  3725. }
  3726. else
  3727. addend = isym->st_value + ref->addend;
  3728. }
  3729. if (!mips_elf_record_got_page_entry (arg, sec, addend))
  3730. {
  3731. arg->g = NULL;
  3732. return 0;
  3733. }
  3734. return 1;
  3735. }
  3736. /* If any entries in G->got_entries are for indirect or warning symbols,
  3737. replace them with entries for the target symbol. Convert g->got_page_refs
  3738. into got_page_entry structures and estimate the number of page entries
  3739. that they require. */
  3740. static bool
  3741. mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
  3742. struct mips_got_info *g)
  3743. {
  3744. struct mips_elf_traverse_got_arg tga;
  3745. struct mips_got_info oldg;
  3746. oldg = *g;
  3747. tga.info = info;
  3748. tga.g = g;
  3749. tga.value = false;
  3750. htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
  3751. if (tga.value)
  3752. {
  3753. *g = oldg;
  3754. g->got_entries = htab_create (htab_size (oldg.got_entries),
  3755. mips_elf_got_entry_hash,
  3756. mips_elf_got_entry_eq, NULL);
  3757. if (!g->got_entries)
  3758. return false;
  3759. htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
  3760. if (!tga.g)
  3761. return false;
  3762. htab_delete (oldg.got_entries);
  3763. }
  3764. g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
  3765. mips_got_page_entry_eq, NULL);
  3766. if (g->got_page_entries == NULL)
  3767. return false;
  3768. tga.info = info;
  3769. tga.g = g;
  3770. htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
  3771. return true;
  3772. }
  3773. /* Return true if a GOT entry for H should live in the local rather than
  3774. global GOT area. */
  3775. static bool
  3776. mips_use_local_got_p (struct bfd_link_info *info,
  3777. struct mips_elf_link_hash_entry *h)
  3778. {
  3779. /* Symbols that aren't in the dynamic symbol table must live in the
  3780. local GOT. This includes symbols that are completely undefined
  3781. and which therefore don't bind locally. We'll report undefined
  3782. symbols later if appropriate. */
  3783. if (h->root.dynindx == -1)
  3784. return true;
  3785. /* Absolute symbols, if ever they need a GOT entry, cannot ever go
  3786. to the local GOT, as they would be implicitly relocated by the
  3787. base address by the dynamic loader. */
  3788. if (bfd_is_abs_symbol (&h->root.root))
  3789. return false;
  3790. /* Symbols that bind locally can (and in the case of forced-local
  3791. symbols, must) live in the local GOT. */
  3792. if (h->got_only_for_calls
  3793. ? SYMBOL_CALLS_LOCAL (info, &h->root)
  3794. : SYMBOL_REFERENCES_LOCAL (info, &h->root))
  3795. return true;
  3796. /* If this is an executable that must provide a definition of the symbol,
  3797. either though PLTs or copy relocations, then that address should go in
  3798. the local rather than global GOT. */
  3799. if (bfd_link_executable (info) && h->has_static_relocs)
  3800. return true;
  3801. return false;
  3802. }
  3803. /* A mips_elf_link_hash_traverse callback for which DATA points to the
  3804. link_info structure. Decide whether the hash entry needs an entry in
  3805. the global part of the primary GOT, setting global_got_area accordingly.
  3806. Count the number of global symbols that are in the primary GOT only
  3807. because they have relocations against them (reloc_only_gotno). */
  3808. static bool
  3809. mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
  3810. {
  3811. struct bfd_link_info *info;
  3812. struct mips_elf_link_hash_table *htab;
  3813. struct mips_got_info *g;
  3814. info = (struct bfd_link_info *) data;
  3815. htab = mips_elf_hash_table (info);
  3816. g = htab->got_info;
  3817. if (h->global_got_area != GGA_NONE)
  3818. {
  3819. /* Make a final decision about whether the symbol belongs in the
  3820. local or global GOT. */
  3821. if (mips_use_local_got_p (info, h))
  3822. /* The symbol belongs in the local GOT. We no longer need this
  3823. entry if it was only used for relocations; those relocations
  3824. will be against the null or section symbol instead of H. */
  3825. h->global_got_area = GGA_NONE;
  3826. else if (htab->root.target_os == is_vxworks
  3827. && h->got_only_for_calls
  3828. && h->root.plt.plist->mips_offset != MINUS_ONE)
  3829. /* On VxWorks, calls can refer directly to the .got.plt entry;
  3830. they don't need entries in the regular GOT. .got.plt entries
  3831. will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
  3832. h->global_got_area = GGA_NONE;
  3833. else if (h->global_got_area == GGA_RELOC_ONLY)
  3834. {
  3835. g->reloc_only_gotno++;
  3836. g->global_gotno++;
  3837. }
  3838. }
  3839. return 1;
  3840. }
  3841. /* A htab_traverse callback for GOT entries. Add each one to the GOT
  3842. given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
  3843. static int
  3844. mips_elf_add_got_entry (void **entryp, void *data)
  3845. {
  3846. struct mips_got_entry *entry;
  3847. struct mips_elf_traverse_got_arg *arg;
  3848. void **slot;
  3849. entry = (struct mips_got_entry *) *entryp;
  3850. arg = (struct mips_elf_traverse_got_arg *) data;
  3851. slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
  3852. if (!slot)
  3853. {
  3854. arg->g = NULL;
  3855. return 0;
  3856. }
  3857. if (!*slot)
  3858. {
  3859. *slot = entry;
  3860. mips_elf_count_got_entry (arg->info, arg->g, entry);
  3861. }
  3862. return 1;
  3863. }
  3864. /* A htab_traverse callback for GOT page entries. Add each one to the GOT
  3865. given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
  3866. static int
  3867. mips_elf_add_got_page_entry (void **entryp, void *data)
  3868. {
  3869. struct mips_got_page_entry *entry;
  3870. struct mips_elf_traverse_got_arg *arg;
  3871. void **slot;
  3872. entry = (struct mips_got_page_entry *) *entryp;
  3873. arg = (struct mips_elf_traverse_got_arg *) data;
  3874. slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
  3875. if (!slot)
  3876. {
  3877. arg->g = NULL;
  3878. return 0;
  3879. }
  3880. if (!*slot)
  3881. {
  3882. *slot = entry;
  3883. arg->g->page_gotno += entry->num_pages;
  3884. }
  3885. return 1;
  3886. }
  3887. /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
  3888. this would lead to overflow, 1 if they were merged successfully,
  3889. and 0 if a merge failed due to lack of memory. (These values are chosen
  3890. so that nonnegative return values can be returned by a htab_traverse
  3891. callback.) */
  3892. static int
  3893. mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
  3894. struct mips_got_info *to,
  3895. struct mips_elf_got_per_bfd_arg *arg)
  3896. {
  3897. struct mips_elf_traverse_got_arg tga;
  3898. unsigned int estimate;
  3899. /* Work out how many page entries we would need for the combined GOT. */
  3900. estimate = arg->max_pages;
  3901. if (estimate >= from->page_gotno + to->page_gotno)
  3902. estimate = from->page_gotno + to->page_gotno;
  3903. /* And conservatively estimate how many local and TLS entries
  3904. would be needed. */
  3905. estimate += from->local_gotno + to->local_gotno;
  3906. estimate += from->tls_gotno + to->tls_gotno;
  3907. /* If we're merging with the primary got, any TLS relocations will
  3908. come after the full set of global entries. Otherwise estimate those
  3909. conservatively as well. */
  3910. if (to == arg->primary && from->tls_gotno + to->tls_gotno)
  3911. estimate += arg->global_count;
  3912. else
  3913. estimate += from->global_gotno + to->global_gotno;
  3914. /* Bail out if the combined GOT might be too big. */
  3915. if (estimate > arg->max_count)
  3916. return -1;
  3917. /* Transfer the bfd's got information from FROM to TO. */
  3918. tga.info = arg->info;
  3919. tga.g = to;
  3920. htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
  3921. if (!tga.g)
  3922. return 0;
  3923. htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
  3924. if (!tga.g)
  3925. return 0;
  3926. mips_elf_replace_bfd_got (abfd, to);
  3927. return 1;
  3928. }
  3929. /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
  3930. as possible of the primary got, since it doesn't require explicit
  3931. dynamic relocations, but don't use bfds that would reference global
  3932. symbols out of the addressable range. Failing the primary got,
  3933. attempt to merge with the current got, or finish the current got
  3934. and then make make the new got current. */
  3935. static bool
  3936. mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
  3937. struct mips_elf_got_per_bfd_arg *arg)
  3938. {
  3939. unsigned int estimate;
  3940. int result;
  3941. if (!mips_elf_resolve_final_got_entries (arg->info, g))
  3942. return false;
  3943. /* Work out the number of page, local and TLS entries. */
  3944. estimate = arg->max_pages;
  3945. if (estimate > g->page_gotno)
  3946. estimate = g->page_gotno;
  3947. estimate += g->local_gotno + g->tls_gotno;
  3948. /* We place TLS GOT entries after both locals and globals. The globals
  3949. for the primary GOT may overflow the normal GOT size limit, so be
  3950. sure not to merge a GOT which requires TLS with the primary GOT in that
  3951. case. This doesn't affect non-primary GOTs. */
  3952. estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
  3953. if (estimate <= arg->max_count)
  3954. {
  3955. /* If we don't have a primary GOT, use it as
  3956. a starting point for the primary GOT. */
  3957. if (!arg->primary)
  3958. {
  3959. arg->primary = g;
  3960. return true;
  3961. }
  3962. /* Try merging with the primary GOT. */
  3963. result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
  3964. if (result >= 0)
  3965. return result;
  3966. }
  3967. /* If we can merge with the last-created got, do it. */
  3968. if (arg->current)
  3969. {
  3970. result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
  3971. if (result >= 0)
  3972. return result;
  3973. }
  3974. /* Well, we couldn't merge, so create a new GOT. Don't check if it
  3975. fits; if it turns out that it doesn't, we'll get relocation
  3976. overflows anyway. */
  3977. g->next = arg->current;
  3978. arg->current = g;
  3979. return true;
  3980. }
  3981. /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
  3982. to GOTIDX, duplicating the entry if it has already been assigned
  3983. an index in a different GOT. */
  3984. static bool
  3985. mips_elf_set_gotidx (void **entryp, long gotidx)
  3986. {
  3987. struct mips_got_entry *entry;
  3988. entry = (struct mips_got_entry *) *entryp;
  3989. if (entry->gotidx > 0)
  3990. {
  3991. struct mips_got_entry *new_entry;
  3992. new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
  3993. if (!new_entry)
  3994. return false;
  3995. *new_entry = *entry;
  3996. *entryp = new_entry;
  3997. entry = new_entry;
  3998. }
  3999. entry->gotidx = gotidx;
  4000. return true;
  4001. }
  4002. /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
  4003. mips_elf_traverse_got_arg in which DATA->value is the size of one
  4004. GOT entry. Set DATA->g to null on failure. */
  4005. static int
  4006. mips_elf_initialize_tls_index (void **entryp, void *data)
  4007. {
  4008. struct mips_got_entry *entry;
  4009. struct mips_elf_traverse_got_arg *arg;
  4010. /* We're only interested in TLS symbols. */
  4011. entry = (struct mips_got_entry *) *entryp;
  4012. if (entry->tls_type == GOT_TLS_NONE)
  4013. return 1;
  4014. arg = (struct mips_elf_traverse_got_arg *) data;
  4015. if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
  4016. {
  4017. arg->g = NULL;
  4018. return 0;
  4019. }
  4020. /* Account for the entries we've just allocated. */
  4021. arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
  4022. return 1;
  4023. }
  4024. /* A htab_traverse callback for GOT entries, where DATA points to a
  4025. mips_elf_traverse_got_arg. Set the global_got_area of each global
  4026. symbol to DATA->value. */
  4027. static int
  4028. mips_elf_set_global_got_area (void **entryp, void *data)
  4029. {
  4030. struct mips_got_entry *entry;
  4031. struct mips_elf_traverse_got_arg *arg;
  4032. entry = (struct mips_got_entry *) *entryp;
  4033. arg = (struct mips_elf_traverse_got_arg *) data;
  4034. if (entry->abfd != NULL
  4035. && entry->symndx == -1
  4036. && entry->d.h->global_got_area != GGA_NONE)
  4037. entry->d.h->global_got_area = arg->value;
  4038. return 1;
  4039. }
  4040. /* A htab_traverse callback for secondary GOT entries, where DATA points
  4041. to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
  4042. and record the number of relocations they require. DATA->value is
  4043. the size of one GOT entry. Set DATA->g to null on failure. */
  4044. static int
  4045. mips_elf_set_global_gotidx (void **entryp, void *data)
  4046. {
  4047. struct mips_got_entry *entry;
  4048. struct mips_elf_traverse_got_arg *arg;
  4049. entry = (struct mips_got_entry *) *entryp;
  4050. arg = (struct mips_elf_traverse_got_arg *) data;
  4051. if (entry->abfd != NULL
  4052. && entry->symndx == -1
  4053. && entry->d.h->global_got_area != GGA_NONE)
  4054. {
  4055. if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
  4056. {
  4057. arg->g = NULL;
  4058. return 0;
  4059. }
  4060. arg->g->assigned_low_gotno += 1;
  4061. if (bfd_link_pic (arg->info)
  4062. || (elf_hash_table (arg->info)->dynamic_sections_created
  4063. && entry->d.h->root.def_dynamic
  4064. && !entry->d.h->root.def_regular))
  4065. arg->g->relocs += 1;
  4066. }
  4067. return 1;
  4068. }
  4069. /* A htab_traverse callback for GOT entries for which DATA is the
  4070. bfd_link_info. Forbid any global symbols from having traditional
  4071. lazy-binding stubs. */
  4072. static int
  4073. mips_elf_forbid_lazy_stubs (void **entryp, void *data)
  4074. {
  4075. struct bfd_link_info *info;
  4076. struct mips_elf_link_hash_table *htab;
  4077. struct mips_got_entry *entry;
  4078. entry = (struct mips_got_entry *) *entryp;
  4079. info = (struct bfd_link_info *) data;
  4080. htab = mips_elf_hash_table (info);
  4081. BFD_ASSERT (htab != NULL);
  4082. if (entry->abfd != NULL
  4083. && entry->symndx == -1
  4084. && entry->d.h->needs_lazy_stub)
  4085. {
  4086. entry->d.h->needs_lazy_stub = false;
  4087. htab->lazy_stub_count--;
  4088. }
  4089. return 1;
  4090. }
  4091. /* Return the offset of an input bfd IBFD's GOT from the beginning of
  4092. the primary GOT. */
  4093. static bfd_vma
  4094. mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
  4095. {
  4096. if (!g->next)
  4097. return 0;
  4098. g = mips_elf_bfd_got (ibfd, false);
  4099. if (! g)
  4100. return 0;
  4101. BFD_ASSERT (g->next);
  4102. g = g->next;
  4103. return (g->local_gotno + g->global_gotno + g->tls_gotno)
  4104. * MIPS_ELF_GOT_SIZE (abfd);
  4105. }
  4106. /* Turn a single GOT that is too big for 16-bit addressing into
  4107. a sequence of GOTs, each one 16-bit addressable. */
  4108. static bool
  4109. mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
  4110. asection *got, bfd_size_type pages)
  4111. {
  4112. struct mips_elf_link_hash_table *htab;
  4113. struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
  4114. struct mips_elf_traverse_got_arg tga;
  4115. struct mips_got_info *g, *gg;
  4116. unsigned int assign, needed_relocs;
  4117. bfd *dynobj, *ibfd;
  4118. dynobj = elf_hash_table (info)->dynobj;
  4119. htab = mips_elf_hash_table (info);
  4120. BFD_ASSERT (htab != NULL);
  4121. g = htab->got_info;
  4122. got_per_bfd_arg.obfd = abfd;
  4123. got_per_bfd_arg.info = info;
  4124. got_per_bfd_arg.current = NULL;
  4125. got_per_bfd_arg.primary = NULL;
  4126. got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
  4127. / MIPS_ELF_GOT_SIZE (abfd))
  4128. - htab->reserved_gotno);
  4129. got_per_bfd_arg.max_pages = pages;
  4130. /* The number of globals that will be included in the primary GOT.
  4131. See the calls to mips_elf_set_global_got_area below for more
  4132. information. */
  4133. got_per_bfd_arg.global_count = g->global_gotno;
  4134. /* Try to merge the GOTs of input bfds together, as long as they
  4135. don't seem to exceed the maximum GOT size, choosing one of them
  4136. to be the primary GOT. */
  4137. for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
  4138. {
  4139. gg = mips_elf_bfd_got (ibfd, false);
  4140. if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
  4141. return false;
  4142. }
  4143. /* If we do not find any suitable primary GOT, create an empty one. */
  4144. if (got_per_bfd_arg.primary == NULL)
  4145. g->next = mips_elf_create_got_info (abfd);
  4146. else
  4147. g->next = got_per_bfd_arg.primary;
  4148. g->next->next = got_per_bfd_arg.current;
  4149. /* GG is now the master GOT, and G is the primary GOT. */
  4150. gg = g;
  4151. g = g->next;
  4152. /* Map the output bfd to the primary got. That's what we're going
  4153. to use for bfds that use GOT16 or GOT_PAGE relocations that we
  4154. didn't mark in check_relocs, and we want a quick way to find it.
  4155. We can't just use gg->next because we're going to reverse the
  4156. list. */
  4157. mips_elf_replace_bfd_got (abfd, g);
  4158. /* Every symbol that is referenced in a dynamic relocation must be
  4159. present in the primary GOT, so arrange for them to appear after
  4160. those that are actually referenced. */
  4161. gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
  4162. g->global_gotno = gg->global_gotno;
  4163. tga.info = info;
  4164. tga.value = GGA_RELOC_ONLY;
  4165. htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
  4166. tga.value = GGA_NORMAL;
  4167. htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
  4168. /* Now go through the GOTs assigning them offset ranges.
  4169. [assigned_low_gotno, local_gotno[ will be set to the range of local
  4170. entries in each GOT. We can then compute the end of a GOT by
  4171. adding local_gotno to global_gotno. We reverse the list and make
  4172. it circular since then we'll be able to quickly compute the
  4173. beginning of a GOT, by computing the end of its predecessor. To
  4174. avoid special cases for the primary GOT, while still preserving
  4175. assertions that are valid for both single- and multi-got links,
  4176. we arrange for the main got struct to have the right number of
  4177. global entries, but set its local_gotno such that the initial
  4178. offset of the primary GOT is zero. Remember that the primary GOT
  4179. will become the last item in the circular linked list, so it
  4180. points back to the master GOT. */
  4181. gg->local_gotno = -g->global_gotno;
  4182. gg->global_gotno = g->global_gotno;
  4183. gg->tls_gotno = 0;
  4184. assign = 0;
  4185. gg->next = gg;
  4186. do
  4187. {
  4188. struct mips_got_info *gn;
  4189. assign += htab->reserved_gotno;
  4190. g->assigned_low_gotno = assign;
  4191. g->local_gotno += assign;
  4192. g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
  4193. g->assigned_high_gotno = g->local_gotno - 1;
  4194. assign = g->local_gotno + g->global_gotno + g->tls_gotno;
  4195. /* Take g out of the direct list, and push it onto the reversed
  4196. list that gg points to. g->next is guaranteed to be nonnull after
  4197. this operation, as required by mips_elf_initialize_tls_index. */
  4198. gn = g->next;
  4199. g->next = gg->next;
  4200. gg->next = g;
  4201. /* Set up any TLS entries. We always place the TLS entries after
  4202. all non-TLS entries. */
  4203. g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
  4204. tga.g = g;
  4205. tga.value = MIPS_ELF_GOT_SIZE (abfd);
  4206. htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
  4207. if (!tga.g)
  4208. return false;
  4209. BFD_ASSERT (g->tls_assigned_gotno == assign);
  4210. /* Move onto the next GOT. It will be a secondary GOT if nonull. */
  4211. g = gn;
  4212. /* Forbid global symbols in every non-primary GOT from having
  4213. lazy-binding stubs. */
  4214. if (g)
  4215. htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
  4216. }
  4217. while (g);
  4218. got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
  4219. needed_relocs = 0;
  4220. for (g = gg->next; g && g->next != gg; g = g->next)
  4221. {
  4222. unsigned int save_assign;
  4223. /* Assign offsets to global GOT entries and count how many
  4224. relocations they need. */
  4225. save_assign = g->assigned_low_gotno;
  4226. g->assigned_low_gotno = g->local_gotno;
  4227. tga.info = info;
  4228. tga.value = MIPS_ELF_GOT_SIZE (abfd);
  4229. tga.g = g;
  4230. htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
  4231. if (!tga.g)
  4232. return false;
  4233. BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
  4234. g->assigned_low_gotno = save_assign;
  4235. if (bfd_link_pic (info))
  4236. {
  4237. g->relocs += g->local_gotno - g->assigned_low_gotno;
  4238. BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
  4239. + g->next->global_gotno
  4240. + g->next->tls_gotno
  4241. + htab->reserved_gotno);
  4242. }
  4243. needed_relocs += g->relocs;
  4244. }
  4245. needed_relocs += g->relocs;
  4246. if (needed_relocs)
  4247. mips_elf_allocate_dynamic_relocations (dynobj, info,
  4248. needed_relocs);
  4249. return true;
  4250. }
  4251. /* Returns the first relocation of type r_type found, beginning with
  4252. RELOCATION. RELEND is one-past-the-end of the relocation table. */
  4253. static const Elf_Internal_Rela *
  4254. mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
  4255. const Elf_Internal_Rela *relocation,
  4256. const Elf_Internal_Rela *relend)
  4257. {
  4258. unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
  4259. while (relocation < relend)
  4260. {
  4261. if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
  4262. && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
  4263. return relocation;
  4264. ++relocation;
  4265. }
  4266. /* We didn't find it. */
  4267. return NULL;
  4268. }
  4269. /* Return whether an input relocation is against a local symbol. */
  4270. static bool
  4271. mips_elf_local_relocation_p (bfd *input_bfd,
  4272. const Elf_Internal_Rela *relocation,
  4273. asection **local_sections)
  4274. {
  4275. unsigned long r_symndx;
  4276. Elf_Internal_Shdr *symtab_hdr;
  4277. size_t extsymoff;
  4278. r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
  4279. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  4280. extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
  4281. if (r_symndx < extsymoff)
  4282. return true;
  4283. if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
  4284. return true;
  4285. return false;
  4286. }
  4287. /* Sign-extend VALUE, which has the indicated number of BITS. */
  4288. bfd_vma
  4289. _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
  4290. {
  4291. if (value & ((bfd_vma) 1 << (bits - 1)))
  4292. /* VALUE is negative. */
  4293. value |= ((bfd_vma) - 1) << bits;
  4294. return value;
  4295. }
  4296. /* Return non-zero if the indicated VALUE has overflowed the maximum
  4297. range expressible by a signed number with the indicated number of
  4298. BITS. */
  4299. static bool
  4300. mips_elf_overflow_p (bfd_vma value, int bits)
  4301. {
  4302. bfd_signed_vma svalue = (bfd_signed_vma) value;
  4303. if (svalue > (1 << (bits - 1)) - 1)
  4304. /* The value is too big. */
  4305. return true;
  4306. else if (svalue < -(1 << (bits - 1)))
  4307. /* The value is too small. */
  4308. return true;
  4309. /* All is well. */
  4310. return false;
  4311. }
  4312. /* Calculate the %high function. */
  4313. static bfd_vma
  4314. mips_elf_high (bfd_vma value)
  4315. {
  4316. return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
  4317. }
  4318. /* Calculate the %higher function. */
  4319. static bfd_vma
  4320. mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
  4321. {
  4322. #ifdef BFD64
  4323. return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
  4324. #else
  4325. abort ();
  4326. return MINUS_ONE;
  4327. #endif
  4328. }
  4329. /* Calculate the %highest function. */
  4330. static bfd_vma
  4331. mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
  4332. {
  4333. #ifdef BFD64
  4334. return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
  4335. #else
  4336. abort ();
  4337. return MINUS_ONE;
  4338. #endif
  4339. }
  4340. /* Create the .compact_rel section. */
  4341. static bool
  4342. mips_elf_create_compact_rel_section
  4343. (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
  4344. {
  4345. flagword flags;
  4346. register asection *s;
  4347. if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
  4348. {
  4349. flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
  4350. | SEC_READONLY);
  4351. s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
  4352. if (s == NULL
  4353. || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
  4354. return false;
  4355. s->size = sizeof (Elf32_External_compact_rel);
  4356. }
  4357. return true;
  4358. }
  4359. /* Create the .got section to hold the global offset table. */
  4360. static bool
  4361. mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
  4362. {
  4363. flagword flags;
  4364. register asection *s;
  4365. struct elf_link_hash_entry *h;
  4366. struct bfd_link_hash_entry *bh;
  4367. struct mips_elf_link_hash_table *htab;
  4368. htab = mips_elf_hash_table (info);
  4369. BFD_ASSERT (htab != NULL);
  4370. /* This function may be called more than once. */
  4371. if (htab->root.sgot)
  4372. return true;
  4373. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  4374. | SEC_LINKER_CREATED);
  4375. /* We have to use an alignment of 2**4 here because this is hardcoded
  4376. in the function stub generation and in the linker script. */
  4377. s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  4378. if (s == NULL
  4379. || !bfd_set_section_alignment (s, 4))
  4380. return false;
  4381. htab->root.sgot = s;
  4382. /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
  4383. linker script because we don't want to define the symbol if we
  4384. are not creating a global offset table. */
  4385. bh = NULL;
  4386. if (! (_bfd_generic_link_add_one_symbol
  4387. (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
  4388. 0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
  4389. return false;
  4390. h = (struct elf_link_hash_entry *) bh;
  4391. h->non_elf = 0;
  4392. h->def_regular = 1;
  4393. h->type = STT_OBJECT;
  4394. h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  4395. elf_hash_table (info)->hgot = h;
  4396. if (bfd_link_pic (info)
  4397. && ! bfd_elf_link_record_dynamic_symbol (info, h))
  4398. return false;
  4399. htab->got_info = mips_elf_create_got_info (abfd);
  4400. mips_elf_section_data (s)->elf.this_hdr.sh_flags
  4401. |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  4402. /* We also need a .got.plt section when generating PLTs. */
  4403. s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
  4404. SEC_ALLOC | SEC_LOAD
  4405. | SEC_HAS_CONTENTS
  4406. | SEC_IN_MEMORY
  4407. | SEC_LINKER_CREATED);
  4408. if (s == NULL)
  4409. return false;
  4410. htab->root.sgotplt = s;
  4411. return true;
  4412. }
  4413. /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
  4414. __GOTT_INDEX__ symbols. These symbols are only special for
  4415. shared objects; they are not used in executables. */
  4416. static bool
  4417. is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
  4418. {
  4419. return (mips_elf_hash_table (info)->root.target_os == is_vxworks
  4420. && bfd_link_pic (info)
  4421. && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
  4422. || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
  4423. }
  4424. /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
  4425. require an la25 stub. See also mips_elf_local_pic_function_p,
  4426. which determines whether the destination function ever requires a
  4427. stub. */
  4428. static bool
  4429. mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
  4430. bool target_is_16_bit_code_p)
  4431. {
  4432. /* We specifically ignore branches and jumps from EF_PIC objects,
  4433. where the onus is on the compiler or programmer to perform any
  4434. necessary initialization of $25. Sometimes such initialization
  4435. is unnecessary; for example, -mno-shared functions do not use
  4436. the incoming value of $25, and may therefore be called directly. */
  4437. if (PIC_OBJECT_P (input_bfd))
  4438. return false;
  4439. switch (r_type)
  4440. {
  4441. case R_MIPS_26:
  4442. case R_MIPS_PC16:
  4443. case R_MIPS_PC21_S2:
  4444. case R_MIPS_PC26_S2:
  4445. case R_MICROMIPS_26_S1:
  4446. case R_MICROMIPS_PC7_S1:
  4447. case R_MICROMIPS_PC10_S1:
  4448. case R_MICROMIPS_PC16_S1:
  4449. case R_MICROMIPS_PC23_S2:
  4450. return true;
  4451. case R_MIPS16_26:
  4452. return !target_is_16_bit_code_p;
  4453. default:
  4454. return false;
  4455. }
  4456. }
  4457. /* Obtain the field relocated by RELOCATION. */
  4458. static bfd_vma
  4459. mips_elf_obtain_contents (reloc_howto_type *howto,
  4460. const Elf_Internal_Rela *relocation,
  4461. bfd *input_bfd, bfd_byte *contents)
  4462. {
  4463. bfd_vma x = 0;
  4464. bfd_byte *location = contents + relocation->r_offset;
  4465. unsigned int size = bfd_get_reloc_size (howto);
  4466. /* Obtain the bytes. */
  4467. if (size != 0)
  4468. x = bfd_get (8 * size, input_bfd, location);
  4469. return x;
  4470. }
  4471. /* Store the field relocated by RELOCATION. */
  4472. static void
  4473. mips_elf_store_contents (reloc_howto_type *howto,
  4474. const Elf_Internal_Rela *relocation,
  4475. bfd *input_bfd, bfd_byte *contents, bfd_vma x)
  4476. {
  4477. bfd_byte *location = contents + relocation->r_offset;
  4478. unsigned int size = bfd_get_reloc_size (howto);
  4479. /* Put the value into the output. */
  4480. if (size != 0)
  4481. bfd_put (8 * size, input_bfd, x, location);
  4482. }
  4483. /* Try to patch a load from GOT instruction in CONTENTS pointed to by
  4484. RELOCATION described by HOWTO, with a move of 0 to the load target
  4485. register, returning TRUE if that is successful and FALSE otherwise.
  4486. If DOIT is FALSE, then only determine it patching is possible and
  4487. return status without actually changing CONTENTS.
  4488. */
  4489. static bool
  4490. mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
  4491. const Elf_Internal_Rela *relocation,
  4492. reloc_howto_type *howto, bool doit)
  4493. {
  4494. int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
  4495. bfd_byte *location = contents + relocation->r_offset;
  4496. bool nullified = true;
  4497. bfd_vma x;
  4498. _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
  4499. /* Obtain the current value. */
  4500. x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
  4501. /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
  4502. while RY is at bits [18:16] of the combined 32-bit instruction word. */
  4503. if (mips16_reloc_p (r_type)
  4504. && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
  4505. || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
  4506. x = (0x3cdU << 22) | (x & (7 << 16)) << 3; /* LI */
  4507. else if (micromips_reloc_p (r_type)
  4508. && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
  4509. x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
  4510. else if (((x >> 26) & 0x3f) == 0x23 /* LW */
  4511. || ((x >> 26) & 0x3f) == 0x37) /* LD */
  4512. x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
  4513. else
  4514. nullified = false;
  4515. /* Put the value into the output. */
  4516. if (doit && nullified)
  4517. mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
  4518. _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
  4519. return nullified;
  4520. }
  4521. /* Calculate the value produced by the RELOCATION (which comes from
  4522. the INPUT_BFD). The ADDEND is the addend to use for this
  4523. RELOCATION; RELOCATION->R_ADDEND is ignored.
  4524. The result of the relocation calculation is stored in VALUEP.
  4525. On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
  4526. is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
  4527. This function returns bfd_reloc_continue if the caller need take no
  4528. further action regarding this relocation, bfd_reloc_notsupported if
  4529. something goes dramatically wrong, bfd_reloc_overflow if an
  4530. overflow occurs, and bfd_reloc_ok to indicate success. */
  4531. static bfd_reloc_status_type
  4532. mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
  4533. asection *input_section, bfd_byte *contents,
  4534. struct bfd_link_info *info,
  4535. const Elf_Internal_Rela *relocation,
  4536. bfd_vma addend, reloc_howto_type *howto,
  4537. Elf_Internal_Sym *local_syms,
  4538. asection **local_sections, bfd_vma *valuep,
  4539. const char **namep,
  4540. bool *cross_mode_jump_p,
  4541. bool save_addend)
  4542. {
  4543. /* The eventual value we will return. */
  4544. bfd_vma value;
  4545. /* The address of the symbol against which the relocation is
  4546. occurring. */
  4547. bfd_vma symbol = 0;
  4548. /* The final GP value to be used for the relocatable, executable, or
  4549. shared object file being produced. */
  4550. bfd_vma gp;
  4551. /* The place (section offset or address) of the storage unit being
  4552. relocated. */
  4553. bfd_vma p;
  4554. /* The value of GP used to create the relocatable object. */
  4555. bfd_vma gp0;
  4556. /* The offset into the global offset table at which the address of
  4557. the relocation entry symbol, adjusted by the addend, resides
  4558. during execution. */
  4559. bfd_vma g = MINUS_ONE;
  4560. /* The section in which the symbol referenced by the relocation is
  4561. located. */
  4562. asection *sec = NULL;
  4563. struct mips_elf_link_hash_entry *h = NULL;
  4564. /* TRUE if the symbol referred to by this relocation is a local
  4565. symbol. */
  4566. bool local_p, was_local_p;
  4567. /* TRUE if the symbol referred to by this relocation is a section
  4568. symbol. */
  4569. bool section_p = false;
  4570. /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
  4571. bool gp_disp_p = false;
  4572. /* TRUE if the symbol referred to by this relocation is
  4573. "__gnu_local_gp". */
  4574. bool gnu_local_gp_p = false;
  4575. Elf_Internal_Shdr *symtab_hdr;
  4576. size_t extsymoff;
  4577. unsigned long r_symndx;
  4578. int r_type;
  4579. /* TRUE if overflow occurred during the calculation of the
  4580. relocation value. */
  4581. bool overflowed_p;
  4582. /* TRUE if this relocation refers to a MIPS16 function. */
  4583. bool target_is_16_bit_code_p = false;
  4584. bool target_is_micromips_code_p = false;
  4585. struct mips_elf_link_hash_table *htab;
  4586. bfd *dynobj;
  4587. bool resolved_to_zero;
  4588. dynobj = elf_hash_table (info)->dynobj;
  4589. htab = mips_elf_hash_table (info);
  4590. BFD_ASSERT (htab != NULL);
  4591. /* Parse the relocation. */
  4592. r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
  4593. r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
  4594. p = (input_section->output_section->vma
  4595. + input_section->output_offset
  4596. + relocation->r_offset);
  4597. /* Assume that there will be no overflow. */
  4598. overflowed_p = false;
  4599. /* Figure out whether or not the symbol is local, and get the offset
  4600. used in the array of hash table entries. */
  4601. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  4602. local_p = mips_elf_local_relocation_p (input_bfd, relocation,
  4603. local_sections);
  4604. was_local_p = local_p;
  4605. if (! elf_bad_symtab (input_bfd))
  4606. extsymoff = symtab_hdr->sh_info;
  4607. else
  4608. {
  4609. /* The symbol table does not follow the rule that local symbols
  4610. must come before globals. */
  4611. extsymoff = 0;
  4612. }
  4613. /* Figure out the value of the symbol. */
  4614. if (local_p)
  4615. {
  4616. bool micromips_p = MICROMIPS_P (abfd);
  4617. Elf_Internal_Sym *sym;
  4618. sym = local_syms + r_symndx;
  4619. sec = local_sections[r_symndx];
  4620. section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
  4621. symbol = sec->output_section->vma + sec->output_offset;
  4622. if (!section_p || (sec->flags & SEC_MERGE))
  4623. symbol += sym->st_value;
  4624. if ((sec->flags & SEC_MERGE) && section_p)
  4625. {
  4626. addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
  4627. addend -= symbol;
  4628. addend += sec->output_section->vma + sec->output_offset;
  4629. }
  4630. /* MIPS16/microMIPS text labels should be treated as odd. */
  4631. if (ELF_ST_IS_COMPRESSED (sym->st_other))
  4632. ++symbol;
  4633. /* Record the name of this symbol, for our caller. */
  4634. *namep = bfd_elf_string_from_elf_section (input_bfd,
  4635. symtab_hdr->sh_link,
  4636. sym->st_name);
  4637. if (*namep == NULL || **namep == '\0')
  4638. *namep = bfd_section_name (sec);
  4639. /* For relocations against a section symbol and ones against no
  4640. symbol (absolute relocations) infer the ISA mode from the addend. */
  4641. if (section_p || r_symndx == STN_UNDEF)
  4642. {
  4643. target_is_16_bit_code_p = (addend & 1) && !micromips_p;
  4644. target_is_micromips_code_p = (addend & 1) && micromips_p;
  4645. }
  4646. /* For relocations against an absolute symbol infer the ISA mode
  4647. from the value of the symbol plus addend. */
  4648. else if (bfd_is_abs_section (sec))
  4649. {
  4650. target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
  4651. target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
  4652. }
  4653. /* Otherwise just use the regular symbol annotation available. */
  4654. else
  4655. {
  4656. target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
  4657. target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
  4658. }
  4659. }
  4660. else
  4661. {
  4662. /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
  4663. /* For global symbols we look up the symbol in the hash-table. */
  4664. h = ((struct mips_elf_link_hash_entry *)
  4665. elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
  4666. /* Find the real hash-table entry for this symbol. */
  4667. while (h->root.root.type == bfd_link_hash_indirect
  4668. || h->root.root.type == bfd_link_hash_warning)
  4669. h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
  4670. /* Record the name of this symbol, for our caller. */
  4671. *namep = h->root.root.root.string;
  4672. /* See if this is the special _gp_disp symbol. Note that such a
  4673. symbol must always be a global symbol. */
  4674. if (strcmp (*namep, "_gp_disp") == 0
  4675. && ! NEWABI_P (input_bfd))
  4676. {
  4677. /* Relocations against _gp_disp are permitted only with
  4678. R_MIPS_HI16 and R_MIPS_LO16 relocations. */
  4679. if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
  4680. return bfd_reloc_notsupported;
  4681. gp_disp_p = true;
  4682. }
  4683. /* See if this is the special _gp symbol. Note that such a
  4684. symbol must always be a global symbol. */
  4685. else if (strcmp (*namep, "__gnu_local_gp") == 0)
  4686. gnu_local_gp_p = true;
  4687. /* If this symbol is defined, calculate its address. Note that
  4688. _gp_disp is a magic symbol, always implicitly defined by the
  4689. linker, so it's inappropriate to check to see whether or not
  4690. its defined. */
  4691. else if ((h->root.root.type == bfd_link_hash_defined
  4692. || h->root.root.type == bfd_link_hash_defweak)
  4693. && h->root.root.u.def.section)
  4694. {
  4695. sec = h->root.root.u.def.section;
  4696. if (sec->output_section)
  4697. symbol = (h->root.root.u.def.value
  4698. + sec->output_section->vma
  4699. + sec->output_offset);
  4700. else
  4701. symbol = h->root.root.u.def.value;
  4702. }
  4703. else if (h->root.root.type == bfd_link_hash_undefweak)
  4704. /* We allow relocations against undefined weak symbols, giving
  4705. it the value zero, so that you can undefined weak functions
  4706. and check to see if they exist by looking at their
  4707. addresses. */
  4708. symbol = 0;
  4709. else if (info->unresolved_syms_in_objects == RM_IGNORE
  4710. && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
  4711. symbol = 0;
  4712. else if (strcmp (*namep, SGI_COMPAT (input_bfd)
  4713. ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
  4714. {
  4715. /* If this is a dynamic link, we should have created a
  4716. _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
  4717. in _bfd_mips_elf_create_dynamic_sections.
  4718. Otherwise, we should define the symbol with a value of 0.
  4719. FIXME: It should probably get into the symbol table
  4720. somehow as well. */
  4721. BFD_ASSERT (! bfd_link_pic (info));
  4722. BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
  4723. symbol = 0;
  4724. }
  4725. else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
  4726. {
  4727. /* This is an optional symbol - an Irix specific extension to the
  4728. ELF spec. Ignore it for now.
  4729. XXX - FIXME - there is more to the spec for OPTIONAL symbols
  4730. than simply ignoring them, but we do not handle this for now.
  4731. For information see the "64-bit ELF Object File Specification"
  4732. which is available from here:
  4733. http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
  4734. symbol = 0;
  4735. }
  4736. else
  4737. {
  4738. bool reject_undefined
  4739. = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
  4740. && !info->warn_unresolved_syms)
  4741. || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
  4742. info->callbacks->undefined_symbol
  4743. (info, h->root.root.root.string, input_bfd,
  4744. input_section, relocation->r_offset, reject_undefined);
  4745. if (reject_undefined)
  4746. return bfd_reloc_undefined;
  4747. symbol = 0;
  4748. }
  4749. target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
  4750. target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
  4751. }
  4752. /* If this is a reference to a 16-bit function with a stub, we need
  4753. to redirect the relocation to the stub unless:
  4754. (a) the relocation is for a MIPS16 JAL;
  4755. (b) the relocation is for a MIPS16 PIC call, and there are no
  4756. non-MIPS16 uses of the GOT slot; or
  4757. (c) the section allows direct references to MIPS16 functions. */
  4758. if (r_type != R_MIPS16_26
  4759. && !bfd_link_relocatable (info)
  4760. && ((h != NULL
  4761. && h->fn_stub != NULL
  4762. && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
  4763. || (local_p
  4764. && mips_elf_tdata (input_bfd)->local_stubs != NULL
  4765. && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
  4766. && !section_allows_mips16_refs_p (input_section))
  4767. {
  4768. /* This is a 32- or 64-bit call to a 16-bit function. We should
  4769. have already noticed that we were going to need the
  4770. stub. */
  4771. if (local_p)
  4772. {
  4773. sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
  4774. value = 0;
  4775. }
  4776. else
  4777. {
  4778. BFD_ASSERT (h->need_fn_stub);
  4779. if (h->la25_stub)
  4780. {
  4781. /* If a LA25 header for the stub itself exists, point to the
  4782. prepended LUI/ADDIU sequence. */
  4783. sec = h->la25_stub->stub_section;
  4784. value = h->la25_stub->offset;
  4785. }
  4786. else
  4787. {
  4788. sec = h->fn_stub;
  4789. value = 0;
  4790. }
  4791. }
  4792. symbol = sec->output_section->vma + sec->output_offset + value;
  4793. /* The target is 16-bit, but the stub isn't. */
  4794. target_is_16_bit_code_p = false;
  4795. }
  4796. /* If this is a MIPS16 call with a stub, that is made through the PLT or
  4797. to a standard MIPS function, we need to redirect the call to the stub.
  4798. Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
  4799. indirect calls should use an indirect stub instead. */
  4800. else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
  4801. && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
  4802. || (local_p
  4803. && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
  4804. && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
  4805. && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
  4806. {
  4807. if (local_p)
  4808. sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
  4809. else
  4810. {
  4811. /* If both call_stub and call_fp_stub are defined, we can figure
  4812. out which one to use by checking which one appears in the input
  4813. file. */
  4814. if (h->call_stub != NULL && h->call_fp_stub != NULL)
  4815. {
  4816. asection *o;
  4817. sec = NULL;
  4818. for (o = input_bfd->sections; o != NULL; o = o->next)
  4819. {
  4820. if (CALL_FP_STUB_P (bfd_section_name (o)))
  4821. {
  4822. sec = h->call_fp_stub;
  4823. break;
  4824. }
  4825. }
  4826. if (sec == NULL)
  4827. sec = h->call_stub;
  4828. }
  4829. else if (h->call_stub != NULL)
  4830. sec = h->call_stub;
  4831. else
  4832. sec = h->call_fp_stub;
  4833. }
  4834. BFD_ASSERT (sec->size > 0);
  4835. symbol = sec->output_section->vma + sec->output_offset;
  4836. }
  4837. /* If this is a direct call to a PIC function, redirect to the
  4838. non-PIC stub. */
  4839. else if (h != NULL && h->la25_stub
  4840. && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
  4841. target_is_16_bit_code_p))
  4842. {
  4843. symbol = (h->la25_stub->stub_section->output_section->vma
  4844. + h->la25_stub->stub_section->output_offset
  4845. + h->la25_stub->offset);
  4846. if (ELF_ST_IS_MICROMIPS (h->root.other))
  4847. symbol |= 1;
  4848. }
  4849. /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
  4850. entry is used if a standard PLT entry has also been made. In this
  4851. case the symbol will have been set by mips_elf_set_plt_sym_value
  4852. to point to the standard PLT entry, so redirect to the compressed
  4853. one. */
  4854. else if ((mips16_branch_reloc_p (r_type)
  4855. || micromips_branch_reloc_p (r_type))
  4856. && !bfd_link_relocatable (info)
  4857. && h != NULL
  4858. && h->use_plt_entry
  4859. && h->root.plt.plist->comp_offset != MINUS_ONE
  4860. && h->root.plt.plist->mips_offset != MINUS_ONE)
  4861. {
  4862. bool micromips_p = MICROMIPS_P (abfd);
  4863. sec = htab->root.splt;
  4864. symbol = (sec->output_section->vma
  4865. + sec->output_offset
  4866. + htab->plt_header_size
  4867. + htab->plt_mips_offset
  4868. + h->root.plt.plist->comp_offset
  4869. + 1);
  4870. target_is_16_bit_code_p = !micromips_p;
  4871. target_is_micromips_code_p = micromips_p;
  4872. }
  4873. /* Make sure MIPS16 and microMIPS are not used together. */
  4874. if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
  4875. || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
  4876. {
  4877. _bfd_error_handler
  4878. (_("MIPS16 and microMIPS functions cannot call each other"));
  4879. return bfd_reloc_notsupported;
  4880. }
  4881. /* Calls from 16-bit code to 32-bit code and vice versa require the
  4882. mode change. However, we can ignore calls to undefined weak symbols,
  4883. which should never be executed at runtime. This exception is important
  4884. because the assembly writer may have "known" that any definition of the
  4885. symbol would be 16-bit code, and that direct jumps were therefore
  4886. acceptable. */
  4887. *cross_mode_jump_p = (!bfd_link_relocatable (info)
  4888. && !(h && h->root.root.type == bfd_link_hash_undefweak)
  4889. && ((mips16_branch_reloc_p (r_type)
  4890. && !target_is_16_bit_code_p)
  4891. || (micromips_branch_reloc_p (r_type)
  4892. && !target_is_micromips_code_p)
  4893. || ((branch_reloc_p (r_type)
  4894. || r_type == R_MIPS_JALR)
  4895. && (target_is_16_bit_code_p
  4896. || target_is_micromips_code_p))));
  4897. resolved_to_zero = (h != NULL
  4898. && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
  4899. switch (r_type)
  4900. {
  4901. case R_MIPS16_CALL16:
  4902. case R_MIPS16_GOT16:
  4903. case R_MIPS_CALL16:
  4904. case R_MIPS_GOT16:
  4905. case R_MIPS_GOT_PAGE:
  4906. case R_MIPS_GOT_DISP:
  4907. case R_MIPS_GOT_LO16:
  4908. case R_MIPS_CALL_LO16:
  4909. case R_MICROMIPS_CALL16:
  4910. case R_MICROMIPS_GOT16:
  4911. case R_MICROMIPS_GOT_PAGE:
  4912. case R_MICROMIPS_GOT_DISP:
  4913. case R_MICROMIPS_GOT_LO16:
  4914. case R_MICROMIPS_CALL_LO16:
  4915. if (resolved_to_zero
  4916. && !bfd_link_relocatable (info)
  4917. && mips_elf_nullify_got_load (input_bfd, contents,
  4918. relocation, howto, true))
  4919. return bfd_reloc_continue;
  4920. /* Fall through. */
  4921. case R_MIPS_GOT_HI16:
  4922. case R_MIPS_CALL_HI16:
  4923. case R_MICROMIPS_GOT_HI16:
  4924. case R_MICROMIPS_CALL_HI16:
  4925. if (resolved_to_zero
  4926. && htab->use_absolute_zero
  4927. && bfd_link_pic (info))
  4928. {
  4929. /* Redirect to the special `__gnu_absolute_zero' symbol. */
  4930. h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
  4931. false, false, false);
  4932. BFD_ASSERT (h != NULL);
  4933. }
  4934. break;
  4935. }
  4936. local_p = (h == NULL || mips_use_local_got_p (info, h));
  4937. gp0 = _bfd_get_gp_value (input_bfd);
  4938. gp = _bfd_get_gp_value (abfd);
  4939. if (htab->got_info)
  4940. gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
  4941. if (gnu_local_gp_p)
  4942. symbol = gp;
  4943. /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
  4944. to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
  4945. corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
  4946. if (got_page_reloc_p (r_type) && !local_p)
  4947. {
  4948. r_type = (micromips_reloc_p (r_type)
  4949. ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
  4950. addend = 0;
  4951. }
  4952. /* If we haven't already determined the GOT offset, and we're going
  4953. to need it, get it now. */
  4954. switch (r_type)
  4955. {
  4956. case R_MIPS16_CALL16:
  4957. case R_MIPS16_GOT16:
  4958. case R_MIPS_CALL16:
  4959. case R_MIPS_GOT16:
  4960. case R_MIPS_GOT_DISP:
  4961. case R_MIPS_GOT_HI16:
  4962. case R_MIPS_CALL_HI16:
  4963. case R_MIPS_GOT_LO16:
  4964. case R_MIPS_CALL_LO16:
  4965. case R_MICROMIPS_CALL16:
  4966. case R_MICROMIPS_GOT16:
  4967. case R_MICROMIPS_GOT_DISP:
  4968. case R_MICROMIPS_GOT_HI16:
  4969. case R_MICROMIPS_CALL_HI16:
  4970. case R_MICROMIPS_GOT_LO16:
  4971. case R_MICROMIPS_CALL_LO16:
  4972. case R_MIPS_TLS_GD:
  4973. case R_MIPS_TLS_GOTTPREL:
  4974. case R_MIPS_TLS_LDM:
  4975. case R_MIPS16_TLS_GD:
  4976. case R_MIPS16_TLS_GOTTPREL:
  4977. case R_MIPS16_TLS_LDM:
  4978. case R_MICROMIPS_TLS_GD:
  4979. case R_MICROMIPS_TLS_GOTTPREL:
  4980. case R_MICROMIPS_TLS_LDM:
  4981. /* Find the index into the GOT where this value is located. */
  4982. if (tls_ldm_reloc_p (r_type))
  4983. {
  4984. g = mips_elf_local_got_index (abfd, input_bfd, info,
  4985. 0, 0, NULL, r_type);
  4986. if (g == MINUS_ONE)
  4987. return bfd_reloc_outofrange;
  4988. }
  4989. else if (!local_p)
  4990. {
  4991. /* On VxWorks, CALL relocations should refer to the .got.plt
  4992. entry, which is initialized to point at the PLT stub. */
  4993. if (htab->root.target_os == is_vxworks
  4994. && (call_hi16_reloc_p (r_type)
  4995. || call_lo16_reloc_p (r_type)
  4996. || call16_reloc_p (r_type)))
  4997. {
  4998. BFD_ASSERT (addend == 0);
  4999. BFD_ASSERT (h->root.needs_plt);
  5000. g = mips_elf_gotplt_index (info, &h->root);
  5001. }
  5002. else
  5003. {
  5004. BFD_ASSERT (addend == 0);
  5005. g = mips_elf_global_got_index (abfd, info, input_bfd,
  5006. &h->root, r_type);
  5007. if (!TLS_RELOC_P (r_type)
  5008. && !elf_hash_table (info)->dynamic_sections_created)
  5009. /* This is a static link. We must initialize the GOT entry. */
  5010. MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
  5011. }
  5012. }
  5013. else if (htab->root.target_os != is_vxworks
  5014. && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
  5015. /* The calculation below does not involve "g". */
  5016. break;
  5017. else
  5018. {
  5019. g = mips_elf_local_got_index (abfd, input_bfd, info,
  5020. symbol + addend, r_symndx, h, r_type);
  5021. if (g == MINUS_ONE)
  5022. return bfd_reloc_outofrange;
  5023. }
  5024. /* Convert GOT indices to actual offsets. */
  5025. g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
  5026. break;
  5027. }
  5028. /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
  5029. symbols are resolved by the loader. Add them to .rela.dyn. */
  5030. if (h != NULL && is_gott_symbol (info, &h->root))
  5031. {
  5032. Elf_Internal_Rela outrel;
  5033. bfd_byte *loc;
  5034. asection *s;
  5035. s = mips_elf_rel_dyn_section (info, false);
  5036. loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
  5037. outrel.r_offset = (input_section->output_section->vma
  5038. + input_section->output_offset
  5039. + relocation->r_offset);
  5040. outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
  5041. outrel.r_addend = addend;
  5042. bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
  5043. /* If we've written this relocation for a readonly section,
  5044. we need to set DF_TEXTREL again, so that we do not delete the
  5045. DT_TEXTREL tag. */
  5046. if (MIPS_ELF_READONLY_SECTION (input_section))
  5047. info->flags |= DF_TEXTREL;
  5048. *valuep = 0;
  5049. return bfd_reloc_ok;
  5050. }
  5051. /* Figure out what kind of relocation is being performed. */
  5052. switch (r_type)
  5053. {
  5054. case R_MIPS_NONE:
  5055. return bfd_reloc_continue;
  5056. case R_MIPS_16:
  5057. if (howto->partial_inplace)
  5058. addend = _bfd_mips_elf_sign_extend (addend, 16);
  5059. value = symbol + addend;
  5060. overflowed_p = mips_elf_overflow_p (value, 16);
  5061. break;
  5062. case R_MIPS_32:
  5063. case R_MIPS_REL32:
  5064. case R_MIPS_64:
  5065. if ((bfd_link_pic (info)
  5066. || (htab->root.dynamic_sections_created
  5067. && h != NULL
  5068. && h->root.def_dynamic
  5069. && !h->root.def_regular
  5070. && !h->has_static_relocs))
  5071. && r_symndx != STN_UNDEF
  5072. && (h == NULL
  5073. || h->root.root.type != bfd_link_hash_undefweak
  5074. || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
  5075. && !resolved_to_zero))
  5076. && (input_section->flags & SEC_ALLOC) != 0)
  5077. {
  5078. /* If we're creating a shared library, then we can't know
  5079. where the symbol will end up. So, we create a relocation
  5080. record in the output, and leave the job up to the dynamic
  5081. linker. We must do the same for executable references to
  5082. shared library symbols, unless we've decided to use copy
  5083. relocs or PLTs instead. */
  5084. value = addend;
  5085. if (!mips_elf_create_dynamic_relocation (abfd,
  5086. info,
  5087. relocation,
  5088. h,
  5089. sec,
  5090. symbol,
  5091. &value,
  5092. input_section))
  5093. return bfd_reloc_undefined;
  5094. }
  5095. else
  5096. {
  5097. if (r_type != R_MIPS_REL32)
  5098. value = symbol + addend;
  5099. else
  5100. value = addend;
  5101. }
  5102. value &= howto->dst_mask;
  5103. break;
  5104. case R_MIPS_PC32:
  5105. value = symbol + addend - p;
  5106. value &= howto->dst_mask;
  5107. break;
  5108. case R_MIPS16_26:
  5109. /* The calculation for R_MIPS16_26 is just the same as for an
  5110. R_MIPS_26. It's only the storage of the relocated field into
  5111. the output file that's different. That's handled in
  5112. mips_elf_perform_relocation. So, we just fall through to the
  5113. R_MIPS_26 case here. */
  5114. case R_MIPS_26:
  5115. case R_MICROMIPS_26_S1:
  5116. {
  5117. unsigned int shift;
  5118. /* Shift is 2, unusually, for microMIPS JALX. */
  5119. shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
  5120. if (howto->partial_inplace && !section_p)
  5121. value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
  5122. else
  5123. value = addend;
  5124. value += symbol;
  5125. /* Make sure the target of a jump is suitably aligned. Bit 0 must
  5126. be the correct ISA mode selector except for weak undefined
  5127. symbols. */
  5128. if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5129. && (*cross_mode_jump_p
  5130. ? (value & 3) != (r_type == R_MIPS_26)
  5131. : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
  5132. return bfd_reloc_outofrange;
  5133. value >>= shift;
  5134. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5135. overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
  5136. value &= howto->dst_mask;
  5137. }
  5138. break;
  5139. case R_MIPS_TLS_DTPREL_HI16:
  5140. case R_MIPS16_TLS_DTPREL_HI16:
  5141. case R_MICROMIPS_TLS_DTPREL_HI16:
  5142. value = (mips_elf_high (addend + symbol - dtprel_base (info))
  5143. & howto->dst_mask);
  5144. break;
  5145. case R_MIPS_TLS_DTPREL_LO16:
  5146. case R_MIPS_TLS_DTPREL32:
  5147. case R_MIPS_TLS_DTPREL64:
  5148. case R_MIPS16_TLS_DTPREL_LO16:
  5149. case R_MICROMIPS_TLS_DTPREL_LO16:
  5150. value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
  5151. break;
  5152. case R_MIPS_TLS_TPREL_HI16:
  5153. case R_MIPS16_TLS_TPREL_HI16:
  5154. case R_MICROMIPS_TLS_TPREL_HI16:
  5155. value = (mips_elf_high (addend + symbol - tprel_base (info))
  5156. & howto->dst_mask);
  5157. break;
  5158. case R_MIPS_TLS_TPREL_LO16:
  5159. case R_MIPS_TLS_TPREL32:
  5160. case R_MIPS_TLS_TPREL64:
  5161. case R_MIPS16_TLS_TPREL_LO16:
  5162. case R_MICROMIPS_TLS_TPREL_LO16:
  5163. value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
  5164. break;
  5165. case R_MIPS_HI16:
  5166. case R_MIPS16_HI16:
  5167. case R_MICROMIPS_HI16:
  5168. if (!gp_disp_p)
  5169. {
  5170. value = mips_elf_high (addend + symbol);
  5171. value &= howto->dst_mask;
  5172. }
  5173. else
  5174. {
  5175. /* For MIPS16 ABI code we generate this sequence
  5176. 0: li $v0,%hi(_gp_disp)
  5177. 4: addiupc $v1,%lo(_gp_disp)
  5178. 8: sll $v0,16
  5179. 12: addu $v0,$v1
  5180. 14: move $gp,$v0
  5181. So the offsets of hi and lo relocs are the same, but the
  5182. base $pc is that used by the ADDIUPC instruction at $t9 + 4.
  5183. ADDIUPC clears the low two bits of the instruction address,
  5184. so the base is ($t9 + 4) & ~3. */
  5185. if (r_type == R_MIPS16_HI16)
  5186. value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
  5187. /* The microMIPS .cpload sequence uses the same assembly
  5188. instructions as the traditional psABI version, but the
  5189. incoming $t9 has the low bit set. */
  5190. else if (r_type == R_MICROMIPS_HI16)
  5191. value = mips_elf_high (addend + gp - p - 1);
  5192. else
  5193. value = mips_elf_high (addend + gp - p);
  5194. }
  5195. break;
  5196. case R_MIPS_LO16:
  5197. case R_MIPS16_LO16:
  5198. case R_MICROMIPS_LO16:
  5199. case R_MICROMIPS_HI0_LO16:
  5200. if (!gp_disp_p)
  5201. value = (symbol + addend) & howto->dst_mask;
  5202. else
  5203. {
  5204. /* See the comment for R_MIPS16_HI16 above for the reason
  5205. for this conditional. */
  5206. if (r_type == R_MIPS16_LO16)
  5207. value = addend + gp - (p & ~(bfd_vma) 0x3);
  5208. else if (r_type == R_MICROMIPS_LO16
  5209. || r_type == R_MICROMIPS_HI0_LO16)
  5210. value = addend + gp - p + 3;
  5211. else
  5212. value = addend + gp - p + 4;
  5213. /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
  5214. for overflow. But, on, say, IRIX5, relocations against
  5215. _gp_disp are normally generated from the .cpload
  5216. pseudo-op. It generates code that normally looks like
  5217. this:
  5218. lui $gp,%hi(_gp_disp)
  5219. addiu $gp,$gp,%lo(_gp_disp)
  5220. addu $gp,$gp,$t9
  5221. Here $t9 holds the address of the function being called,
  5222. as required by the MIPS ELF ABI. The R_MIPS_LO16
  5223. relocation can easily overflow in this situation, but the
  5224. R_MIPS_HI16 relocation will handle the overflow.
  5225. Therefore, we consider this a bug in the MIPS ABI, and do
  5226. not check for overflow here. */
  5227. }
  5228. break;
  5229. case R_MIPS_LITERAL:
  5230. case R_MICROMIPS_LITERAL:
  5231. /* Because we don't merge literal sections, we can handle this
  5232. just like R_MIPS_GPREL16. In the long run, we should merge
  5233. shared literals, and then we will need to additional work
  5234. here. */
  5235. /* Fall through. */
  5236. case R_MIPS16_GPREL:
  5237. /* The R_MIPS16_GPREL performs the same calculation as
  5238. R_MIPS_GPREL16, but stores the relocated bits in a different
  5239. order. We don't need to do anything special here; the
  5240. differences are handled in mips_elf_perform_relocation. */
  5241. case R_MIPS_GPREL16:
  5242. case R_MICROMIPS_GPREL7_S2:
  5243. case R_MICROMIPS_GPREL16:
  5244. /* Only sign-extend the addend if it was extracted from the
  5245. instruction. If the addend was separate, leave it alone,
  5246. otherwise we may lose significant bits. */
  5247. if (howto->partial_inplace)
  5248. addend = _bfd_mips_elf_sign_extend (addend, 16);
  5249. value = symbol + addend - gp;
  5250. /* If the symbol was local, any earlier relocatable links will
  5251. have adjusted its addend with the gp offset, so compensate
  5252. for that now. Don't do it for symbols forced local in this
  5253. link, though, since they won't have had the gp offset applied
  5254. to them before. */
  5255. if (was_local_p)
  5256. value += gp0;
  5257. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5258. overflowed_p = mips_elf_overflow_p (value, 16);
  5259. break;
  5260. case R_MIPS16_GOT16:
  5261. case R_MIPS16_CALL16:
  5262. case R_MIPS_GOT16:
  5263. case R_MIPS_CALL16:
  5264. case R_MICROMIPS_GOT16:
  5265. case R_MICROMIPS_CALL16:
  5266. /* VxWorks does not have separate local and global semantics for
  5267. R_MIPS*_GOT16; every relocation evaluates to "G". */
  5268. if (htab->root.target_os != is_vxworks && local_p)
  5269. {
  5270. value = mips_elf_got16_entry (abfd, input_bfd, info,
  5271. symbol + addend, !was_local_p);
  5272. if (value == MINUS_ONE)
  5273. return bfd_reloc_outofrange;
  5274. value
  5275. = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
  5276. overflowed_p = mips_elf_overflow_p (value, 16);
  5277. break;
  5278. }
  5279. /* Fall through. */
  5280. case R_MIPS_TLS_GD:
  5281. case R_MIPS_TLS_GOTTPREL:
  5282. case R_MIPS_TLS_LDM:
  5283. case R_MIPS_GOT_DISP:
  5284. case R_MIPS16_TLS_GD:
  5285. case R_MIPS16_TLS_GOTTPREL:
  5286. case R_MIPS16_TLS_LDM:
  5287. case R_MICROMIPS_TLS_GD:
  5288. case R_MICROMIPS_TLS_GOTTPREL:
  5289. case R_MICROMIPS_TLS_LDM:
  5290. case R_MICROMIPS_GOT_DISP:
  5291. value = g;
  5292. overflowed_p = mips_elf_overflow_p (value, 16);
  5293. break;
  5294. case R_MIPS_GPREL32:
  5295. value = (addend + symbol + gp0 - gp);
  5296. if (!save_addend)
  5297. value &= howto->dst_mask;
  5298. break;
  5299. case R_MIPS_PC16:
  5300. case R_MIPS_GNU_REL16_S2:
  5301. if (howto->partial_inplace)
  5302. addend = _bfd_mips_elf_sign_extend (addend, 18);
  5303. /* No need to exclude weak undefined symbols here as they resolve
  5304. to 0 and never set `*cross_mode_jump_p', so this alignment check
  5305. will never trigger for them. */
  5306. if (*cross_mode_jump_p
  5307. ? ((symbol + addend) & 3) != 1
  5308. : ((symbol + addend) & 3) != 0)
  5309. return bfd_reloc_outofrange;
  5310. value = symbol + addend - p;
  5311. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5312. overflowed_p = mips_elf_overflow_p (value, 18);
  5313. value >>= howto->rightshift;
  5314. value &= howto->dst_mask;
  5315. break;
  5316. case R_MIPS16_PC16_S1:
  5317. if (howto->partial_inplace)
  5318. addend = _bfd_mips_elf_sign_extend (addend, 17);
  5319. if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5320. && (*cross_mode_jump_p
  5321. ? ((symbol + addend) & 3) != 0
  5322. : ((symbol + addend) & 1) == 0))
  5323. return bfd_reloc_outofrange;
  5324. value = symbol + addend - p;
  5325. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5326. overflowed_p = mips_elf_overflow_p (value, 17);
  5327. value >>= howto->rightshift;
  5328. value &= howto->dst_mask;
  5329. break;
  5330. case R_MIPS_PC21_S2:
  5331. if (howto->partial_inplace)
  5332. addend = _bfd_mips_elf_sign_extend (addend, 23);
  5333. if ((symbol + addend) & 3)
  5334. return bfd_reloc_outofrange;
  5335. value = symbol + addend - p;
  5336. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5337. overflowed_p = mips_elf_overflow_p (value, 23);
  5338. value >>= howto->rightshift;
  5339. value &= howto->dst_mask;
  5340. break;
  5341. case R_MIPS_PC26_S2:
  5342. if (howto->partial_inplace)
  5343. addend = _bfd_mips_elf_sign_extend (addend, 28);
  5344. if ((symbol + addend) & 3)
  5345. return bfd_reloc_outofrange;
  5346. value = symbol + addend - p;
  5347. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5348. overflowed_p = mips_elf_overflow_p (value, 28);
  5349. value >>= howto->rightshift;
  5350. value &= howto->dst_mask;
  5351. break;
  5352. case R_MIPS_PC18_S3:
  5353. if (howto->partial_inplace)
  5354. addend = _bfd_mips_elf_sign_extend (addend, 21);
  5355. if ((symbol + addend) & 7)
  5356. return bfd_reloc_outofrange;
  5357. value = symbol + addend - ((p | 7) ^ 7);
  5358. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5359. overflowed_p = mips_elf_overflow_p (value, 21);
  5360. value >>= howto->rightshift;
  5361. value &= howto->dst_mask;
  5362. break;
  5363. case R_MIPS_PC19_S2:
  5364. if (howto->partial_inplace)
  5365. addend = _bfd_mips_elf_sign_extend (addend, 21);
  5366. if ((symbol + addend) & 3)
  5367. return bfd_reloc_outofrange;
  5368. value = symbol + addend - p;
  5369. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5370. overflowed_p = mips_elf_overflow_p (value, 21);
  5371. value >>= howto->rightshift;
  5372. value &= howto->dst_mask;
  5373. break;
  5374. case R_MIPS_PCHI16:
  5375. value = mips_elf_high (symbol + addend - p);
  5376. value &= howto->dst_mask;
  5377. break;
  5378. case R_MIPS_PCLO16:
  5379. if (howto->partial_inplace)
  5380. addend = _bfd_mips_elf_sign_extend (addend, 16);
  5381. value = symbol + addend - p;
  5382. value &= howto->dst_mask;
  5383. break;
  5384. case R_MICROMIPS_PC7_S1:
  5385. if (howto->partial_inplace)
  5386. addend = _bfd_mips_elf_sign_extend (addend, 8);
  5387. if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5388. && (*cross_mode_jump_p
  5389. ? ((symbol + addend + 2) & 3) != 0
  5390. : ((symbol + addend + 2) & 1) == 0))
  5391. return bfd_reloc_outofrange;
  5392. value = symbol + addend - p;
  5393. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5394. overflowed_p = mips_elf_overflow_p (value, 8);
  5395. value >>= howto->rightshift;
  5396. value &= howto->dst_mask;
  5397. break;
  5398. case R_MICROMIPS_PC10_S1:
  5399. if (howto->partial_inplace)
  5400. addend = _bfd_mips_elf_sign_extend (addend, 11);
  5401. if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5402. && (*cross_mode_jump_p
  5403. ? ((symbol + addend + 2) & 3) != 0
  5404. : ((symbol + addend + 2) & 1) == 0))
  5405. return bfd_reloc_outofrange;
  5406. value = symbol + addend - p;
  5407. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5408. overflowed_p = mips_elf_overflow_p (value, 11);
  5409. value >>= howto->rightshift;
  5410. value &= howto->dst_mask;
  5411. break;
  5412. case R_MICROMIPS_PC16_S1:
  5413. if (howto->partial_inplace)
  5414. addend = _bfd_mips_elf_sign_extend (addend, 17);
  5415. if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5416. && (*cross_mode_jump_p
  5417. ? ((symbol + addend) & 3) != 0
  5418. : ((symbol + addend) & 1) == 0))
  5419. return bfd_reloc_outofrange;
  5420. value = symbol + addend - p;
  5421. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5422. overflowed_p = mips_elf_overflow_p (value, 17);
  5423. value >>= howto->rightshift;
  5424. value &= howto->dst_mask;
  5425. break;
  5426. case R_MICROMIPS_PC23_S2:
  5427. if (howto->partial_inplace)
  5428. addend = _bfd_mips_elf_sign_extend (addend, 25);
  5429. value = symbol + addend - ((p | 3) ^ 3);
  5430. if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
  5431. overflowed_p = mips_elf_overflow_p (value, 25);
  5432. value >>= howto->rightshift;
  5433. value &= howto->dst_mask;
  5434. break;
  5435. case R_MIPS_GOT_HI16:
  5436. case R_MIPS_CALL_HI16:
  5437. case R_MICROMIPS_GOT_HI16:
  5438. case R_MICROMIPS_CALL_HI16:
  5439. /* We're allowed to handle these two relocations identically.
  5440. The dynamic linker is allowed to handle the CALL relocations
  5441. differently by creating a lazy evaluation stub. */
  5442. value = g;
  5443. value = mips_elf_high (value);
  5444. value &= howto->dst_mask;
  5445. break;
  5446. case R_MIPS_GOT_LO16:
  5447. case R_MIPS_CALL_LO16:
  5448. case R_MICROMIPS_GOT_LO16:
  5449. case R_MICROMIPS_CALL_LO16:
  5450. value = g & howto->dst_mask;
  5451. break;
  5452. case R_MIPS_GOT_PAGE:
  5453. case R_MICROMIPS_GOT_PAGE:
  5454. value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
  5455. if (value == MINUS_ONE)
  5456. return bfd_reloc_outofrange;
  5457. value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
  5458. overflowed_p = mips_elf_overflow_p (value, 16);
  5459. break;
  5460. case R_MIPS_GOT_OFST:
  5461. case R_MICROMIPS_GOT_OFST:
  5462. if (local_p)
  5463. mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
  5464. else
  5465. value = addend;
  5466. overflowed_p = mips_elf_overflow_p (value, 16);
  5467. break;
  5468. case R_MIPS_SUB:
  5469. case R_MICROMIPS_SUB:
  5470. value = symbol - addend;
  5471. value &= howto->dst_mask;
  5472. break;
  5473. case R_MIPS_HIGHER:
  5474. case R_MICROMIPS_HIGHER:
  5475. value = mips_elf_higher (addend + symbol);
  5476. value &= howto->dst_mask;
  5477. break;
  5478. case R_MIPS_HIGHEST:
  5479. case R_MICROMIPS_HIGHEST:
  5480. value = mips_elf_highest (addend + symbol);
  5481. value &= howto->dst_mask;
  5482. break;
  5483. case R_MIPS_SCN_DISP:
  5484. case R_MICROMIPS_SCN_DISP:
  5485. value = symbol + addend - sec->output_offset;
  5486. value &= howto->dst_mask;
  5487. break;
  5488. case R_MIPS_JALR:
  5489. case R_MICROMIPS_JALR:
  5490. /* This relocation is only a hint. In some cases, we optimize
  5491. it into a bal instruction. But we don't try to optimize
  5492. when the symbol does not resolve locally. */
  5493. if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
  5494. return bfd_reloc_continue;
  5495. /* We can't optimize cross-mode jumps either. */
  5496. if (*cross_mode_jump_p)
  5497. return bfd_reloc_continue;
  5498. value = symbol + addend;
  5499. /* Neither we can non-instruction-aligned targets. */
  5500. if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
  5501. return bfd_reloc_continue;
  5502. break;
  5503. case R_MIPS_PJUMP:
  5504. case R_MIPS_GNU_VTINHERIT:
  5505. case R_MIPS_GNU_VTENTRY:
  5506. /* We don't do anything with these at present. */
  5507. return bfd_reloc_continue;
  5508. default:
  5509. /* An unrecognized relocation type. */
  5510. return bfd_reloc_notsupported;
  5511. }
  5512. /* Store the VALUE for our caller. */
  5513. *valuep = value;
  5514. return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
  5515. }
  5516. /* It has been determined that the result of the RELOCATION is the
  5517. VALUE. Use HOWTO to place VALUE into the output file at the
  5518. appropriate position. The SECTION is the section to which the
  5519. relocation applies.
  5520. CROSS_MODE_JUMP_P is true if the relocation field
  5521. is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
  5522. Returns FALSE if anything goes wrong. */
  5523. static bool
  5524. mips_elf_perform_relocation (struct bfd_link_info *info,
  5525. reloc_howto_type *howto,
  5526. const Elf_Internal_Rela *relocation,
  5527. bfd_vma value, bfd *input_bfd,
  5528. asection *input_section, bfd_byte *contents,
  5529. bool cross_mode_jump_p)
  5530. {
  5531. bfd_vma x;
  5532. bfd_byte *location;
  5533. int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
  5534. /* Figure out where the relocation is occurring. */
  5535. location = contents + relocation->r_offset;
  5536. _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
  5537. /* Obtain the current value. */
  5538. x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
  5539. /* Clear the field we are setting. */
  5540. x &= ~howto->dst_mask;
  5541. /* Set the field. */
  5542. x |= (value & howto->dst_mask);
  5543. /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
  5544. if (!cross_mode_jump_p && jal_reloc_p (r_type))
  5545. {
  5546. bfd_vma opcode = x >> 26;
  5547. if (r_type == R_MIPS16_26 ? opcode == 0x7
  5548. : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
  5549. : opcode == 0x1d)
  5550. {
  5551. info->callbacks->einfo
  5552. (_("%X%H: unsupported JALX to the same ISA mode\n"),
  5553. input_bfd, input_section, relocation->r_offset);
  5554. return true;
  5555. }
  5556. }
  5557. if (cross_mode_jump_p && jal_reloc_p (r_type))
  5558. {
  5559. bool ok;
  5560. bfd_vma opcode = x >> 26;
  5561. bfd_vma jalx_opcode;
  5562. /* Check to see if the opcode is already JAL or JALX. */
  5563. if (r_type == R_MIPS16_26)
  5564. {
  5565. ok = ((opcode == 0x6) || (opcode == 0x7));
  5566. jalx_opcode = 0x7;
  5567. }
  5568. else if (r_type == R_MICROMIPS_26_S1)
  5569. {
  5570. ok = ((opcode == 0x3d) || (opcode == 0x3c));
  5571. jalx_opcode = 0x3c;
  5572. }
  5573. else
  5574. {
  5575. ok = ((opcode == 0x3) || (opcode == 0x1d));
  5576. jalx_opcode = 0x1d;
  5577. }
  5578. /* If the opcode is not JAL or JALX, there's a problem. We cannot
  5579. convert J or JALS to JALX. */
  5580. if (!ok)
  5581. {
  5582. info->callbacks->einfo
  5583. (_("%X%H: unsupported jump between ISA modes; "
  5584. "consider recompiling with interlinking enabled\n"),
  5585. input_bfd, input_section, relocation->r_offset);
  5586. return true;
  5587. }
  5588. /* Make this the JALX opcode. */
  5589. x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
  5590. }
  5591. else if (cross_mode_jump_p && b_reloc_p (r_type))
  5592. {
  5593. bool ok = false;
  5594. bfd_vma opcode = x >> 16;
  5595. bfd_vma jalx_opcode = 0;
  5596. bfd_vma sign_bit = 0;
  5597. bfd_vma addr;
  5598. bfd_vma dest;
  5599. if (r_type == R_MICROMIPS_PC16_S1)
  5600. {
  5601. ok = opcode == 0x4060;
  5602. jalx_opcode = 0x3c;
  5603. sign_bit = 0x10000;
  5604. value <<= 1;
  5605. }
  5606. else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
  5607. {
  5608. ok = opcode == 0x411;
  5609. jalx_opcode = 0x1d;
  5610. sign_bit = 0x20000;
  5611. value <<= 2;
  5612. }
  5613. if (ok && !bfd_link_pic (info))
  5614. {
  5615. addr = (input_section->output_section->vma
  5616. + input_section->output_offset
  5617. + relocation->r_offset
  5618. + 4);
  5619. dest = (addr
  5620. + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
  5621. if ((addr >> 28) << 28 != (dest >> 28) << 28)
  5622. {
  5623. info->callbacks->einfo
  5624. (_("%X%H: cannot convert branch between ISA modes "
  5625. "to JALX: relocation out of range\n"),
  5626. input_bfd, input_section, relocation->r_offset);
  5627. return true;
  5628. }
  5629. /* Make this the JALX opcode. */
  5630. x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
  5631. }
  5632. else if (!mips_elf_hash_table (info)->ignore_branch_isa)
  5633. {
  5634. info->callbacks->einfo
  5635. (_("%X%H: unsupported branch between ISA modes\n"),
  5636. input_bfd, input_section, relocation->r_offset);
  5637. return true;
  5638. }
  5639. }
  5640. /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
  5641. range. */
  5642. if (!bfd_link_relocatable (info)
  5643. && !cross_mode_jump_p
  5644. && ((JAL_TO_BAL_P (input_bfd)
  5645. && r_type == R_MIPS_26
  5646. && (x >> 26) == 0x3) /* jal addr */
  5647. || (JALR_TO_BAL_P (input_bfd)
  5648. && r_type == R_MIPS_JALR
  5649. && x == 0x0320f809) /* jalr t9 */
  5650. || (JR_TO_B_P (input_bfd)
  5651. && r_type == R_MIPS_JALR
  5652. && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
  5653. {
  5654. bfd_vma addr;
  5655. bfd_vma dest;
  5656. bfd_signed_vma off;
  5657. addr = (input_section->output_section->vma
  5658. + input_section->output_offset
  5659. + relocation->r_offset
  5660. + 4);
  5661. if (r_type == R_MIPS_26)
  5662. dest = (value << 2) | ((addr >> 28) << 28);
  5663. else
  5664. dest = value;
  5665. off = dest - addr;
  5666. if (off <= 0x1ffff && off >= -0x20000)
  5667. {
  5668. if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
  5669. x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
  5670. else
  5671. x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
  5672. }
  5673. }
  5674. /* Put the value into the output. */
  5675. mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
  5676. _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
  5677. location);
  5678. return true;
  5679. }
  5680. /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
  5681. is the original relocation, which is now being transformed into a
  5682. dynamic relocation. The ADDENDP is adjusted if necessary; the
  5683. caller should store the result in place of the original addend. */
  5684. static bool
  5685. mips_elf_create_dynamic_relocation (bfd *output_bfd,
  5686. struct bfd_link_info *info,
  5687. const Elf_Internal_Rela *rel,
  5688. struct mips_elf_link_hash_entry *h,
  5689. asection *sec, bfd_vma symbol,
  5690. bfd_vma *addendp, asection *input_section)
  5691. {
  5692. Elf_Internal_Rela outrel[3];
  5693. asection *sreloc;
  5694. bfd *dynobj;
  5695. int r_type;
  5696. long indx;
  5697. bool defined_p;
  5698. struct mips_elf_link_hash_table *htab;
  5699. htab = mips_elf_hash_table (info);
  5700. BFD_ASSERT (htab != NULL);
  5701. r_type = ELF_R_TYPE (output_bfd, rel->r_info);
  5702. dynobj = elf_hash_table (info)->dynobj;
  5703. sreloc = mips_elf_rel_dyn_section (info, false);
  5704. BFD_ASSERT (sreloc != NULL);
  5705. BFD_ASSERT (sreloc->contents != NULL);
  5706. BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
  5707. < sreloc->size);
  5708. outrel[0].r_offset =
  5709. _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
  5710. if (ABI_64_P (output_bfd))
  5711. {
  5712. outrel[1].r_offset =
  5713. _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
  5714. outrel[2].r_offset =
  5715. _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
  5716. }
  5717. if (outrel[0].r_offset == MINUS_ONE)
  5718. /* The relocation field has been deleted. */
  5719. return true;
  5720. if (outrel[0].r_offset == MINUS_TWO)
  5721. {
  5722. /* The relocation field has been converted into a relative value of
  5723. some sort. Functions like _bfd_elf_write_section_eh_frame expect
  5724. the field to be fully relocated, so add in the symbol's value. */
  5725. *addendp += symbol;
  5726. return true;
  5727. }
  5728. /* We must now calculate the dynamic symbol table index to use
  5729. in the relocation. */
  5730. if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
  5731. {
  5732. BFD_ASSERT (htab->root.target_os == is_vxworks
  5733. || h->global_got_area != GGA_NONE);
  5734. indx = h->root.dynindx;
  5735. if (SGI_COMPAT (output_bfd))
  5736. defined_p = h->root.def_regular;
  5737. else
  5738. /* ??? glibc's ld.so just adds the final GOT entry to the
  5739. relocation field. It therefore treats relocs against
  5740. defined symbols in the same way as relocs against
  5741. undefined symbols. */
  5742. defined_p = false;
  5743. }
  5744. else
  5745. {
  5746. if (sec != NULL && bfd_is_abs_section (sec))
  5747. indx = 0;
  5748. else if (sec == NULL || sec->owner == NULL)
  5749. {
  5750. bfd_set_error (bfd_error_bad_value);
  5751. return false;
  5752. }
  5753. else
  5754. {
  5755. indx = elf_section_data (sec->output_section)->dynindx;
  5756. if (indx == 0)
  5757. {
  5758. asection *osec = htab->root.text_index_section;
  5759. indx = elf_section_data (osec)->dynindx;
  5760. }
  5761. if (indx == 0)
  5762. abort ();
  5763. }
  5764. /* Instead of generating a relocation using the section
  5765. symbol, we may as well make it a fully relative
  5766. relocation. We want to avoid generating relocations to
  5767. local symbols because we used to generate them
  5768. incorrectly, without adding the original symbol value,
  5769. which is mandated by the ABI for section symbols. In
  5770. order to give dynamic loaders and applications time to
  5771. phase out the incorrect use, we refrain from emitting
  5772. section-relative relocations. It's not like they're
  5773. useful, after all. This should be a bit more efficient
  5774. as well. */
  5775. /* ??? Although this behavior is compatible with glibc's ld.so,
  5776. the ABI says that relocations against STN_UNDEF should have
  5777. a symbol value of 0. Irix rld honors this, so relocations
  5778. against STN_UNDEF have no effect. */
  5779. if (!SGI_COMPAT (output_bfd))
  5780. indx = 0;
  5781. defined_p = true;
  5782. }
  5783. /* If the relocation was previously an absolute relocation and
  5784. this symbol will not be referred to by the relocation, we must
  5785. adjust it by the value we give it in the dynamic symbol table.
  5786. Otherwise leave the job up to the dynamic linker. */
  5787. if (defined_p && r_type != R_MIPS_REL32)
  5788. *addendp += symbol;
  5789. if (htab->root.target_os == is_vxworks)
  5790. /* VxWorks uses non-relative relocations for this. */
  5791. outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
  5792. else
  5793. /* The relocation is always an REL32 relocation because we don't
  5794. know where the shared library will wind up at load-time. */
  5795. outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
  5796. R_MIPS_REL32);
  5797. /* For strict adherence to the ABI specification, we should
  5798. generate a R_MIPS_64 relocation record by itself before the
  5799. _REL32/_64 record as well, such that the addend is read in as
  5800. a 64-bit value (REL32 is a 32-bit relocation, after all).
  5801. However, since none of the existing ELF64 MIPS dynamic
  5802. loaders seems to care, we don't waste space with these
  5803. artificial relocations. If this turns out to not be true,
  5804. mips_elf_allocate_dynamic_relocation() should be tweaked so
  5805. as to make room for a pair of dynamic relocations per
  5806. invocation if ABI_64_P, and here we should generate an
  5807. additional relocation record with R_MIPS_64 by itself for a
  5808. NULL symbol before this relocation record. */
  5809. outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
  5810. ABI_64_P (output_bfd)
  5811. ? R_MIPS_64
  5812. : R_MIPS_NONE);
  5813. outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
  5814. /* Adjust the output offset of the relocation to reference the
  5815. correct location in the output file. */
  5816. outrel[0].r_offset += (input_section->output_section->vma
  5817. + input_section->output_offset);
  5818. outrel[1].r_offset += (input_section->output_section->vma
  5819. + input_section->output_offset);
  5820. outrel[2].r_offset += (input_section->output_section->vma
  5821. + input_section->output_offset);
  5822. /* Put the relocation back out. We have to use the special
  5823. relocation outputter in the 64-bit case since the 64-bit
  5824. relocation format is non-standard. */
  5825. if (ABI_64_P (output_bfd))
  5826. {
  5827. (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
  5828. (output_bfd, &outrel[0],
  5829. (sreloc->contents
  5830. + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
  5831. }
  5832. else if (htab->root.target_os == is_vxworks)
  5833. {
  5834. /* VxWorks uses RELA rather than REL dynamic relocations. */
  5835. outrel[0].r_addend = *addendp;
  5836. bfd_elf32_swap_reloca_out
  5837. (output_bfd, &outrel[0],
  5838. (sreloc->contents
  5839. + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
  5840. }
  5841. else
  5842. bfd_elf32_swap_reloc_out
  5843. (output_bfd, &outrel[0],
  5844. (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
  5845. /* We've now added another relocation. */
  5846. ++sreloc->reloc_count;
  5847. /* Make sure the output section is writable. The dynamic linker
  5848. will be writing to it. */
  5849. elf_section_data (input_section->output_section)->this_hdr.sh_flags
  5850. |= SHF_WRITE;
  5851. /* On IRIX5, make an entry of compact relocation info. */
  5852. if (IRIX_COMPAT (output_bfd) == ict_irix5)
  5853. {
  5854. asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
  5855. bfd_byte *cr;
  5856. if (scpt)
  5857. {
  5858. Elf32_crinfo cptrel;
  5859. mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
  5860. cptrel.vaddr = (rel->r_offset
  5861. + input_section->output_section->vma
  5862. + input_section->output_offset);
  5863. if (r_type == R_MIPS_REL32)
  5864. mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
  5865. else
  5866. mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
  5867. mips_elf_set_cr_dist2to (cptrel, 0);
  5868. cptrel.konst = *addendp;
  5869. cr = (scpt->contents
  5870. + sizeof (Elf32_External_compact_rel));
  5871. mips_elf_set_cr_relvaddr (cptrel, 0);
  5872. bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
  5873. ((Elf32_External_crinfo *) cr
  5874. + scpt->reloc_count));
  5875. ++scpt->reloc_count;
  5876. }
  5877. }
  5878. /* If we've written this relocation for a readonly section,
  5879. we need to set DF_TEXTREL again, so that we do not delete the
  5880. DT_TEXTREL tag. */
  5881. if (MIPS_ELF_READONLY_SECTION (input_section))
  5882. info->flags |= DF_TEXTREL;
  5883. return true;
  5884. }
  5885. /* Return the MACH for a MIPS e_flags value. */
  5886. unsigned long
  5887. _bfd_elf_mips_mach (flagword flags)
  5888. {
  5889. switch (flags & EF_MIPS_MACH)
  5890. {
  5891. case E_MIPS_MACH_3900:
  5892. return bfd_mach_mips3900;
  5893. case E_MIPS_MACH_4010:
  5894. return bfd_mach_mips4010;
  5895. case E_MIPS_MACH_4100:
  5896. return bfd_mach_mips4100;
  5897. case E_MIPS_MACH_4111:
  5898. return bfd_mach_mips4111;
  5899. case E_MIPS_MACH_4120:
  5900. return bfd_mach_mips4120;
  5901. case E_MIPS_MACH_4650:
  5902. return bfd_mach_mips4650;
  5903. case E_MIPS_MACH_5400:
  5904. return bfd_mach_mips5400;
  5905. case E_MIPS_MACH_5500:
  5906. return bfd_mach_mips5500;
  5907. case E_MIPS_MACH_5900:
  5908. return bfd_mach_mips5900;
  5909. case E_MIPS_MACH_9000:
  5910. return bfd_mach_mips9000;
  5911. case E_MIPS_MACH_SB1:
  5912. return bfd_mach_mips_sb1;
  5913. case E_MIPS_MACH_LS2E:
  5914. return bfd_mach_mips_loongson_2e;
  5915. case E_MIPS_MACH_LS2F:
  5916. return bfd_mach_mips_loongson_2f;
  5917. case E_MIPS_MACH_GS464:
  5918. return bfd_mach_mips_gs464;
  5919. case E_MIPS_MACH_GS464E:
  5920. return bfd_mach_mips_gs464e;
  5921. case E_MIPS_MACH_GS264E:
  5922. return bfd_mach_mips_gs264e;
  5923. case E_MIPS_MACH_OCTEON3:
  5924. return bfd_mach_mips_octeon3;
  5925. case E_MIPS_MACH_OCTEON2:
  5926. return bfd_mach_mips_octeon2;
  5927. case E_MIPS_MACH_OCTEON:
  5928. return bfd_mach_mips_octeon;
  5929. case E_MIPS_MACH_XLR:
  5930. return bfd_mach_mips_xlr;
  5931. case E_MIPS_MACH_IAMR2:
  5932. return bfd_mach_mips_interaptiv_mr2;
  5933. default:
  5934. switch (flags & EF_MIPS_ARCH)
  5935. {
  5936. default:
  5937. case E_MIPS_ARCH_1:
  5938. return bfd_mach_mips3000;
  5939. case E_MIPS_ARCH_2:
  5940. return bfd_mach_mips6000;
  5941. case E_MIPS_ARCH_3:
  5942. return bfd_mach_mips4000;
  5943. case E_MIPS_ARCH_4:
  5944. return bfd_mach_mips8000;
  5945. case E_MIPS_ARCH_5:
  5946. return bfd_mach_mips5;
  5947. case E_MIPS_ARCH_32:
  5948. return bfd_mach_mipsisa32;
  5949. case E_MIPS_ARCH_64:
  5950. return bfd_mach_mipsisa64;
  5951. case E_MIPS_ARCH_32R2:
  5952. return bfd_mach_mipsisa32r2;
  5953. case E_MIPS_ARCH_64R2:
  5954. return bfd_mach_mipsisa64r2;
  5955. case E_MIPS_ARCH_32R6:
  5956. return bfd_mach_mipsisa32r6;
  5957. case E_MIPS_ARCH_64R6:
  5958. return bfd_mach_mipsisa64r6;
  5959. }
  5960. }
  5961. return 0;
  5962. }
  5963. /* Return printable name for ABI. */
  5964. static inline char *
  5965. elf_mips_abi_name (bfd *abfd)
  5966. {
  5967. flagword flags;
  5968. flags = elf_elfheader (abfd)->e_flags;
  5969. switch (flags & EF_MIPS_ABI)
  5970. {
  5971. case 0:
  5972. if (ABI_N32_P (abfd))
  5973. return "N32";
  5974. else if (ABI_64_P (abfd))
  5975. return "64";
  5976. else
  5977. return "none";
  5978. case E_MIPS_ABI_O32:
  5979. return "O32";
  5980. case E_MIPS_ABI_O64:
  5981. return "O64";
  5982. case E_MIPS_ABI_EABI32:
  5983. return "EABI32";
  5984. case E_MIPS_ABI_EABI64:
  5985. return "EABI64";
  5986. default:
  5987. return "unknown abi";
  5988. }
  5989. }
  5990. /* MIPS ELF uses two common sections. One is the usual one, and the
  5991. other is for small objects. All the small objects are kept
  5992. together, and then referenced via the gp pointer, which yields
  5993. faster assembler code. This is what we use for the small common
  5994. section. This approach is copied from ecoff.c. */
  5995. static asection mips_elf_scom_section;
  5996. static const asymbol mips_elf_scom_symbol =
  5997. GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
  5998. static asection mips_elf_scom_section =
  5999. BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
  6000. ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
  6001. /* MIPS ELF also uses an acommon section, which represents an
  6002. allocated common symbol which may be overridden by a
  6003. definition in a shared library. */
  6004. static asection mips_elf_acom_section;
  6005. static const asymbol mips_elf_acom_symbol =
  6006. GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
  6007. static asection mips_elf_acom_section =
  6008. BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
  6009. ".acommon", 0, SEC_ALLOC);
  6010. /* This is used for both the 32-bit and the 64-bit ABI. */
  6011. void
  6012. _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
  6013. {
  6014. elf_symbol_type *elfsym;
  6015. /* Handle the special MIPS section numbers that a symbol may use. */
  6016. elfsym = (elf_symbol_type *) asym;
  6017. switch (elfsym->internal_elf_sym.st_shndx)
  6018. {
  6019. case SHN_MIPS_ACOMMON:
  6020. /* This section is used in a dynamically linked executable file.
  6021. It is an allocated common section. The dynamic linker can
  6022. either resolve these symbols to something in a shared
  6023. library, or it can just leave them here. For our purposes,
  6024. we can consider these symbols to be in a new section. */
  6025. asym->section = &mips_elf_acom_section;
  6026. break;
  6027. case SHN_COMMON:
  6028. /* Common symbols less than the GP size are automatically
  6029. treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
  6030. if (asym->value > elf_gp_size (abfd)
  6031. || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
  6032. || IRIX_COMPAT (abfd) == ict_irix6)
  6033. break;
  6034. /* Fall through. */
  6035. case SHN_MIPS_SCOMMON:
  6036. asym->section = &mips_elf_scom_section;
  6037. asym->value = elfsym->internal_elf_sym.st_size;
  6038. break;
  6039. case SHN_MIPS_SUNDEFINED:
  6040. asym->section = bfd_und_section_ptr;
  6041. break;
  6042. case SHN_MIPS_TEXT:
  6043. {
  6044. asection *section = bfd_get_section_by_name (abfd, ".text");
  6045. if (section != NULL)
  6046. {
  6047. asym->section = section;
  6048. /* MIPS_TEXT is a bit special, the address is not an offset
  6049. to the base of the .text section. So subtract the section
  6050. base address to make it an offset. */
  6051. asym->value -= section->vma;
  6052. }
  6053. }
  6054. break;
  6055. case SHN_MIPS_DATA:
  6056. {
  6057. asection *section = bfd_get_section_by_name (abfd, ".data");
  6058. if (section != NULL)
  6059. {
  6060. asym->section = section;
  6061. /* MIPS_DATA is a bit special, the address is not an offset
  6062. to the base of the .data section. So subtract the section
  6063. base address to make it an offset. */
  6064. asym->value -= section->vma;
  6065. }
  6066. }
  6067. break;
  6068. }
  6069. /* If this is an odd-valued function symbol, assume it's a MIPS16
  6070. or microMIPS one. */
  6071. if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
  6072. && (asym->value & 1) != 0)
  6073. {
  6074. asym->value--;
  6075. if (MICROMIPS_P (abfd))
  6076. elfsym->internal_elf_sym.st_other
  6077. = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
  6078. else
  6079. elfsym->internal_elf_sym.st_other
  6080. = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
  6081. }
  6082. }
  6083. /* Implement elf_backend_eh_frame_address_size. This differs from
  6084. the default in the way it handles EABI64.
  6085. EABI64 was originally specified as an LP64 ABI, and that is what
  6086. -mabi=eabi normally gives on a 64-bit target. However, gcc has
  6087. historically accepted the combination of -mabi=eabi and -mlong32,
  6088. and this ILP32 variation has become semi-official over time.
  6089. Both forms use elf32 and have pointer-sized FDE addresses.
  6090. If an EABI object was generated by GCC 4.0 or above, it will have
  6091. an empty .gcc_compiled_longXX section, where XX is the size of longs
  6092. in bits. Unfortunately, ILP32 objects generated by earlier compilers
  6093. have no special marking to distinguish them from LP64 objects.
  6094. We don't want users of the official LP64 ABI to be punished for the
  6095. existence of the ILP32 variant, but at the same time, we don't want
  6096. to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
  6097. We therefore take the following approach:
  6098. - If ABFD contains a .gcc_compiled_longXX section, use it to
  6099. determine the pointer size.
  6100. - Otherwise check the type of the first relocation. Assume that
  6101. the LP64 ABI is being used if the relocation is of type R_MIPS_64.
  6102. - Otherwise punt.
  6103. The second check is enough to detect LP64 objects generated by pre-4.0
  6104. compilers because, in the kind of output generated by those compilers,
  6105. the first relocation will be associated with either a CIE personality
  6106. routine or an FDE start address. Furthermore, the compilers never
  6107. used a special (non-pointer) encoding for this ABI.
  6108. Checking the relocation type should also be safe because there is no
  6109. reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
  6110. did so. */
  6111. unsigned int
  6112. _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
  6113. {
  6114. if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
  6115. return 8;
  6116. if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
  6117. {
  6118. bool long32_p, long64_p;
  6119. long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
  6120. long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
  6121. if (long32_p && long64_p)
  6122. return 0;
  6123. if (long32_p)
  6124. return 4;
  6125. if (long64_p)
  6126. return 8;
  6127. if (sec->reloc_count > 0
  6128. && elf_section_data (sec)->relocs != NULL
  6129. && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
  6130. == R_MIPS_64))
  6131. return 8;
  6132. return 0;
  6133. }
  6134. return 4;
  6135. }
  6136. /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
  6137. relocations against two unnamed section symbols to resolve to the
  6138. same address. For example, if we have code like:
  6139. lw $4,%got_disp(.data)($gp)
  6140. lw $25,%got_disp(.text)($gp)
  6141. jalr $25
  6142. then the linker will resolve both relocations to .data and the program
  6143. will jump there rather than to .text.
  6144. We can work around this problem by giving names to local section symbols.
  6145. This is also what the MIPSpro tools do. */
  6146. bool
  6147. _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
  6148. {
  6149. return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
  6150. }
  6151. /* Work over a section just before writing it out. This routine is
  6152. used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
  6153. sections that need the SHF_MIPS_GPREL flag by name; there has to be
  6154. a better way. */
  6155. bool
  6156. _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
  6157. {
  6158. if (hdr->sh_type == SHT_MIPS_REGINFO
  6159. && hdr->sh_size > 0)
  6160. {
  6161. bfd_byte buf[4];
  6162. BFD_ASSERT (hdr->contents == NULL);
  6163. if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
  6164. {
  6165. _bfd_error_handler
  6166. (_("%pB: incorrect `.reginfo' section size; "
  6167. "expected %" PRIu64 ", got %" PRIu64),
  6168. abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
  6169. (uint64_t) hdr->sh_size);
  6170. bfd_set_error (bfd_error_bad_value);
  6171. return false;
  6172. }
  6173. if (bfd_seek (abfd,
  6174. hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
  6175. SEEK_SET) != 0)
  6176. return false;
  6177. H_PUT_32 (abfd, elf_gp (abfd), buf);
  6178. if (bfd_bwrite (buf, 4, abfd) != 4)
  6179. return false;
  6180. }
  6181. if (hdr->sh_type == SHT_MIPS_OPTIONS
  6182. && hdr->bfd_section != NULL
  6183. && mips_elf_section_data (hdr->bfd_section) != NULL
  6184. && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
  6185. {
  6186. bfd_byte *contents, *l, *lend;
  6187. /* We stored the section contents in the tdata field in the
  6188. set_section_contents routine. We save the section contents
  6189. so that we don't have to read them again.
  6190. At this point we know that elf_gp is set, so we can look
  6191. through the section contents to see if there is an
  6192. ODK_REGINFO structure. */
  6193. contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
  6194. l = contents;
  6195. lend = contents + hdr->sh_size;
  6196. while (l + sizeof (Elf_External_Options) <= lend)
  6197. {
  6198. Elf_Internal_Options intopt;
  6199. bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
  6200. &intopt);
  6201. if (intopt.size < sizeof (Elf_External_Options))
  6202. {
  6203. _bfd_error_handler
  6204. /* xgettext:c-format */
  6205. (_("%pB: warning: bad `%s' option size %u smaller than"
  6206. " its header"),
  6207. abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
  6208. break;
  6209. }
  6210. if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
  6211. {
  6212. bfd_byte buf[8];
  6213. if (bfd_seek (abfd,
  6214. (hdr->sh_offset
  6215. + (l - contents)
  6216. + sizeof (Elf_External_Options)
  6217. + (sizeof (Elf64_External_RegInfo) - 8)),
  6218. SEEK_SET) != 0)
  6219. return false;
  6220. H_PUT_64 (abfd, elf_gp (abfd), buf);
  6221. if (bfd_bwrite (buf, 8, abfd) != 8)
  6222. return false;
  6223. }
  6224. else if (intopt.kind == ODK_REGINFO)
  6225. {
  6226. bfd_byte buf[4];
  6227. if (bfd_seek (abfd,
  6228. (hdr->sh_offset
  6229. + (l - contents)
  6230. + sizeof (Elf_External_Options)
  6231. + (sizeof (Elf32_External_RegInfo) - 4)),
  6232. SEEK_SET) != 0)
  6233. return false;
  6234. H_PUT_32 (abfd, elf_gp (abfd), buf);
  6235. if (bfd_bwrite (buf, 4, abfd) != 4)
  6236. return false;
  6237. }
  6238. l += intopt.size;
  6239. }
  6240. }
  6241. if (hdr->bfd_section != NULL)
  6242. {
  6243. const char *name = bfd_section_name (hdr->bfd_section);
  6244. /* .sbss is not handled specially here because the GNU/Linux
  6245. prelinker can convert .sbss from NOBITS to PROGBITS and
  6246. changing it back to NOBITS breaks the binary. The entry in
  6247. _bfd_mips_elf_special_sections will ensure the correct flags
  6248. are set on .sbss if BFD creates it without reading it from an
  6249. input file, and without special handling here the flags set
  6250. on it in an input file will be followed. */
  6251. if (strcmp (name, ".sdata") == 0
  6252. || strcmp (name, ".lit8") == 0
  6253. || strcmp (name, ".lit4") == 0)
  6254. hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  6255. else if (strcmp (name, ".srdata") == 0)
  6256. hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
  6257. else if (strcmp (name, ".compact_rel") == 0)
  6258. hdr->sh_flags = 0;
  6259. else if (strcmp (name, ".rtproc") == 0)
  6260. {
  6261. if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
  6262. {
  6263. unsigned int adjust;
  6264. adjust = hdr->sh_size % hdr->sh_addralign;
  6265. if (adjust != 0)
  6266. hdr->sh_size += hdr->sh_addralign - adjust;
  6267. }
  6268. }
  6269. }
  6270. return true;
  6271. }
  6272. /* Handle a MIPS specific section when reading an object file. This
  6273. is called when elfcode.h finds a section with an unknown type.
  6274. This routine supports both the 32-bit and 64-bit ELF ABI. */
  6275. bool
  6276. _bfd_mips_elf_section_from_shdr (bfd *abfd,
  6277. Elf_Internal_Shdr *hdr,
  6278. const char *name,
  6279. int shindex)
  6280. {
  6281. flagword flags = 0;
  6282. /* There ought to be a place to keep ELF backend specific flags, but
  6283. at the moment there isn't one. We just keep track of the
  6284. sections by their name, instead. Fortunately, the ABI gives
  6285. suggested names for all the MIPS specific sections, so we will
  6286. probably get away with this. */
  6287. switch (hdr->sh_type)
  6288. {
  6289. case SHT_MIPS_LIBLIST:
  6290. if (strcmp (name, ".liblist") != 0)
  6291. return false;
  6292. break;
  6293. case SHT_MIPS_MSYM:
  6294. if (strcmp (name, ".msym") != 0)
  6295. return false;
  6296. break;
  6297. case SHT_MIPS_CONFLICT:
  6298. if (strcmp (name, ".conflict") != 0)
  6299. return false;
  6300. break;
  6301. case SHT_MIPS_GPTAB:
  6302. if (! startswith (name, ".gptab."))
  6303. return false;
  6304. break;
  6305. case SHT_MIPS_UCODE:
  6306. if (strcmp (name, ".ucode") != 0)
  6307. return false;
  6308. break;
  6309. case SHT_MIPS_DEBUG:
  6310. if (strcmp (name, ".mdebug") != 0)
  6311. return false;
  6312. flags = SEC_DEBUGGING;
  6313. break;
  6314. case SHT_MIPS_REGINFO:
  6315. if (strcmp (name, ".reginfo") != 0
  6316. || hdr->sh_size != sizeof (Elf32_External_RegInfo))
  6317. return false;
  6318. flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
  6319. break;
  6320. case SHT_MIPS_IFACE:
  6321. if (strcmp (name, ".MIPS.interfaces") != 0)
  6322. return false;
  6323. break;
  6324. case SHT_MIPS_CONTENT:
  6325. if (! startswith (name, ".MIPS.content"))
  6326. return false;
  6327. break;
  6328. case SHT_MIPS_OPTIONS:
  6329. if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
  6330. return false;
  6331. break;
  6332. case SHT_MIPS_ABIFLAGS:
  6333. if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
  6334. return false;
  6335. flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
  6336. break;
  6337. case SHT_MIPS_DWARF:
  6338. if (! startswith (name, ".debug_")
  6339. && ! startswith (name, ".gnu.debuglto_.debug_")
  6340. && ! startswith (name, ".zdebug_")
  6341. && ! startswith (name, ".gnu.debuglto_.zdebug_"))
  6342. return false;
  6343. break;
  6344. case SHT_MIPS_SYMBOL_LIB:
  6345. if (strcmp (name, ".MIPS.symlib") != 0)
  6346. return false;
  6347. break;
  6348. case SHT_MIPS_EVENTS:
  6349. if (! startswith (name, ".MIPS.events")
  6350. && ! startswith (name, ".MIPS.post_rel"))
  6351. return false;
  6352. break;
  6353. case SHT_MIPS_XHASH:
  6354. if (strcmp (name, ".MIPS.xhash") != 0)
  6355. return false;
  6356. default:
  6357. break;
  6358. }
  6359. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  6360. return false;
  6361. if (hdr->sh_flags & SHF_MIPS_GPREL)
  6362. flags |= SEC_SMALL_DATA;
  6363. if (flags)
  6364. {
  6365. if (!bfd_set_section_flags (hdr->bfd_section,
  6366. (bfd_section_flags (hdr->bfd_section)
  6367. | flags)))
  6368. return false;
  6369. }
  6370. if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
  6371. {
  6372. Elf_External_ABIFlags_v0 ext;
  6373. if (! bfd_get_section_contents (abfd, hdr->bfd_section,
  6374. &ext, 0, sizeof ext))
  6375. return false;
  6376. bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
  6377. &mips_elf_tdata (abfd)->abiflags);
  6378. if (mips_elf_tdata (abfd)->abiflags.version != 0)
  6379. return false;
  6380. mips_elf_tdata (abfd)->abiflags_valid = true;
  6381. }
  6382. /* FIXME: We should record sh_info for a .gptab section. */
  6383. /* For a .reginfo section, set the gp value in the tdata information
  6384. from the contents of this section. We need the gp value while
  6385. processing relocs, so we just get it now. The .reginfo section
  6386. is not used in the 64-bit MIPS ELF ABI. */
  6387. if (hdr->sh_type == SHT_MIPS_REGINFO)
  6388. {
  6389. Elf32_External_RegInfo ext;
  6390. Elf32_RegInfo s;
  6391. if (! bfd_get_section_contents (abfd, hdr->bfd_section,
  6392. &ext, 0, sizeof ext))
  6393. return false;
  6394. bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
  6395. elf_gp (abfd) = s.ri_gp_value;
  6396. }
  6397. /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
  6398. set the gp value based on what we find. We may see both
  6399. SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
  6400. they should agree. */
  6401. if (hdr->sh_type == SHT_MIPS_OPTIONS)
  6402. {
  6403. bfd_byte *contents, *l, *lend;
  6404. contents = bfd_malloc (hdr->sh_size);
  6405. if (contents == NULL)
  6406. return false;
  6407. if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
  6408. 0, hdr->sh_size))
  6409. {
  6410. free (contents);
  6411. return false;
  6412. }
  6413. l = contents;
  6414. lend = contents + hdr->sh_size;
  6415. while (l + sizeof (Elf_External_Options) <= lend)
  6416. {
  6417. Elf_Internal_Options intopt;
  6418. bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
  6419. &intopt);
  6420. if (intopt.size < sizeof (Elf_External_Options))
  6421. {
  6422. _bfd_error_handler
  6423. /* xgettext:c-format */
  6424. (_("%pB: warning: bad `%s' option size %u smaller than"
  6425. " its header"),
  6426. abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
  6427. break;
  6428. }
  6429. if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
  6430. {
  6431. Elf64_Internal_RegInfo intreg;
  6432. bfd_mips_elf64_swap_reginfo_in
  6433. (abfd,
  6434. ((Elf64_External_RegInfo *)
  6435. (l + sizeof (Elf_External_Options))),
  6436. &intreg);
  6437. elf_gp (abfd) = intreg.ri_gp_value;
  6438. }
  6439. else if (intopt.kind == ODK_REGINFO)
  6440. {
  6441. Elf32_RegInfo intreg;
  6442. bfd_mips_elf32_swap_reginfo_in
  6443. (abfd,
  6444. ((Elf32_External_RegInfo *)
  6445. (l + sizeof (Elf_External_Options))),
  6446. &intreg);
  6447. elf_gp (abfd) = intreg.ri_gp_value;
  6448. }
  6449. l += intopt.size;
  6450. }
  6451. free (contents);
  6452. }
  6453. return true;
  6454. }
  6455. /* Set the correct type for a MIPS ELF section. We do this by the
  6456. section name, which is a hack, but ought to work. This routine is
  6457. used by both the 32-bit and the 64-bit ABI. */
  6458. bool
  6459. _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
  6460. {
  6461. const char *name = bfd_section_name (sec);
  6462. if (strcmp (name, ".liblist") == 0)
  6463. {
  6464. hdr->sh_type = SHT_MIPS_LIBLIST;
  6465. hdr->sh_info = sec->size / sizeof (Elf32_Lib);
  6466. /* The sh_link field is set in final_write_processing. */
  6467. }
  6468. else if (strcmp (name, ".conflict") == 0)
  6469. hdr->sh_type = SHT_MIPS_CONFLICT;
  6470. else if (startswith (name, ".gptab."))
  6471. {
  6472. hdr->sh_type = SHT_MIPS_GPTAB;
  6473. hdr->sh_entsize = sizeof (Elf32_External_gptab);
  6474. /* The sh_info field is set in final_write_processing. */
  6475. }
  6476. else if (strcmp (name, ".ucode") == 0)
  6477. hdr->sh_type = SHT_MIPS_UCODE;
  6478. else if (strcmp (name, ".mdebug") == 0)
  6479. {
  6480. hdr->sh_type = SHT_MIPS_DEBUG;
  6481. /* In a shared object on IRIX 5.3, the .mdebug section has an
  6482. entsize of 0. FIXME: Does this matter? */
  6483. if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
  6484. hdr->sh_entsize = 0;
  6485. else
  6486. hdr->sh_entsize = 1;
  6487. }
  6488. else if (strcmp (name, ".reginfo") == 0)
  6489. {
  6490. hdr->sh_type = SHT_MIPS_REGINFO;
  6491. /* In a shared object on IRIX 5.3, the .reginfo section has an
  6492. entsize of 0x18. FIXME: Does this matter? */
  6493. if (SGI_COMPAT (abfd))
  6494. {
  6495. if ((abfd->flags & DYNAMIC) != 0)
  6496. hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
  6497. else
  6498. hdr->sh_entsize = 1;
  6499. }
  6500. else
  6501. hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
  6502. }
  6503. else if (SGI_COMPAT (abfd)
  6504. && (strcmp (name, ".hash") == 0
  6505. || strcmp (name, ".dynamic") == 0
  6506. || strcmp (name, ".dynstr") == 0))
  6507. {
  6508. if (SGI_COMPAT (abfd))
  6509. hdr->sh_entsize = 0;
  6510. #if 0
  6511. /* This isn't how the IRIX6 linker behaves. */
  6512. hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
  6513. #endif
  6514. }
  6515. else if (strcmp (name, ".got") == 0
  6516. || strcmp (name, ".srdata") == 0
  6517. || strcmp (name, ".sdata") == 0
  6518. || strcmp (name, ".sbss") == 0
  6519. || strcmp (name, ".lit4") == 0
  6520. || strcmp (name, ".lit8") == 0)
  6521. hdr->sh_flags |= SHF_MIPS_GPREL;
  6522. else if (strcmp (name, ".MIPS.interfaces") == 0)
  6523. {
  6524. hdr->sh_type = SHT_MIPS_IFACE;
  6525. hdr->sh_flags |= SHF_MIPS_NOSTRIP;
  6526. }
  6527. else if (startswith (name, ".MIPS.content"))
  6528. {
  6529. hdr->sh_type = SHT_MIPS_CONTENT;
  6530. hdr->sh_flags |= SHF_MIPS_NOSTRIP;
  6531. /* The sh_info field is set in final_write_processing. */
  6532. }
  6533. else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
  6534. {
  6535. hdr->sh_type = SHT_MIPS_OPTIONS;
  6536. hdr->sh_entsize = 1;
  6537. hdr->sh_flags |= SHF_MIPS_NOSTRIP;
  6538. }
  6539. else if (startswith (name, ".MIPS.abiflags"))
  6540. {
  6541. hdr->sh_type = SHT_MIPS_ABIFLAGS;
  6542. hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
  6543. }
  6544. else if (startswith (name, ".debug_")
  6545. || startswith (name, ".gnu.debuglto_.debug_")
  6546. || startswith (name, ".zdebug_")
  6547. || startswith (name, ".gnu.debuglto_.zdebug_"))
  6548. {
  6549. hdr->sh_type = SHT_MIPS_DWARF;
  6550. /* Irix facilities such as libexc expect a single .debug_frame
  6551. per executable, the system ones have NOSTRIP set and the linker
  6552. doesn't merge sections with different flags so ... */
  6553. if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
  6554. hdr->sh_flags |= SHF_MIPS_NOSTRIP;
  6555. }
  6556. else if (strcmp (name, ".MIPS.symlib") == 0)
  6557. {
  6558. hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
  6559. /* The sh_link and sh_info fields are set in
  6560. final_write_processing. */
  6561. }
  6562. else if (startswith (name, ".MIPS.events")
  6563. || startswith (name, ".MIPS.post_rel"))
  6564. {
  6565. hdr->sh_type = SHT_MIPS_EVENTS;
  6566. hdr->sh_flags |= SHF_MIPS_NOSTRIP;
  6567. /* The sh_link field is set in final_write_processing. */
  6568. }
  6569. else if (strcmp (name, ".msym") == 0)
  6570. {
  6571. hdr->sh_type = SHT_MIPS_MSYM;
  6572. hdr->sh_flags |= SHF_ALLOC;
  6573. hdr->sh_entsize = 8;
  6574. }
  6575. else if (strcmp (name, ".MIPS.xhash") == 0)
  6576. {
  6577. hdr->sh_type = SHT_MIPS_XHASH;
  6578. hdr->sh_flags |= SHF_ALLOC;
  6579. hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
  6580. }
  6581. /* The generic elf_fake_sections will set up REL_HDR using the default
  6582. kind of relocations. We used to set up a second header for the
  6583. non-default kind of relocations here, but only NewABI would use
  6584. these, and the IRIX ld doesn't like resulting empty RELA sections.
  6585. Thus we create those header only on demand now. */
  6586. return true;
  6587. }
  6588. /* Given a BFD section, try to locate the corresponding ELF section
  6589. index. This is used by both the 32-bit and the 64-bit ABI.
  6590. Actually, it's not clear to me that the 64-bit ABI supports these,
  6591. but for non-PIC objects we will certainly want support for at least
  6592. the .scommon section. */
  6593. bool
  6594. _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
  6595. asection *sec, int *retval)
  6596. {
  6597. if (strcmp (bfd_section_name (sec), ".scommon") == 0)
  6598. {
  6599. *retval = SHN_MIPS_SCOMMON;
  6600. return true;
  6601. }
  6602. if (strcmp (bfd_section_name (sec), ".acommon") == 0)
  6603. {
  6604. *retval = SHN_MIPS_ACOMMON;
  6605. return true;
  6606. }
  6607. return false;
  6608. }
  6609. /* Hook called by the linker routine which adds symbols from an object
  6610. file. We must handle the special MIPS section numbers here. */
  6611. bool
  6612. _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
  6613. Elf_Internal_Sym *sym, const char **namep,
  6614. flagword *flagsp ATTRIBUTE_UNUSED,
  6615. asection **secp, bfd_vma *valp)
  6616. {
  6617. if (SGI_COMPAT (abfd)
  6618. && (abfd->flags & DYNAMIC) != 0
  6619. && strcmp (*namep, "_rld_new_interface") == 0)
  6620. {
  6621. /* Skip IRIX5 rld entry name. */
  6622. *namep = NULL;
  6623. return true;
  6624. }
  6625. /* Shared objects may have a dynamic symbol '_gp_disp' defined as
  6626. a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
  6627. by setting a DT_NEEDED for the shared object. Since _gp_disp is
  6628. a magic symbol resolved by the linker, we ignore this bogus definition
  6629. of _gp_disp. New ABI objects do not suffer from this problem so this
  6630. is not done for them. */
  6631. if (!NEWABI_P(abfd)
  6632. && (sym->st_shndx == SHN_ABS)
  6633. && (strcmp (*namep, "_gp_disp") == 0))
  6634. {
  6635. *namep = NULL;
  6636. return true;
  6637. }
  6638. switch (sym->st_shndx)
  6639. {
  6640. case SHN_COMMON:
  6641. /* Common symbols less than the GP size are automatically
  6642. treated as SHN_MIPS_SCOMMON symbols. */
  6643. if (sym->st_size > elf_gp_size (abfd)
  6644. || ELF_ST_TYPE (sym->st_info) == STT_TLS
  6645. || IRIX_COMPAT (abfd) == ict_irix6)
  6646. break;
  6647. /* Fall through. */
  6648. case SHN_MIPS_SCOMMON:
  6649. *secp = bfd_make_section_old_way (abfd, ".scommon");
  6650. (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
  6651. *valp = sym->st_size;
  6652. break;
  6653. case SHN_MIPS_TEXT:
  6654. /* This section is used in a shared object. */
  6655. if (mips_elf_tdata (abfd)->elf_text_section == NULL)
  6656. {
  6657. asymbol *elf_text_symbol;
  6658. asection *elf_text_section;
  6659. size_t amt = sizeof (asection);
  6660. elf_text_section = bfd_zalloc (abfd, amt);
  6661. if (elf_text_section == NULL)
  6662. return false;
  6663. amt = sizeof (asymbol);
  6664. elf_text_symbol = bfd_zalloc (abfd, amt);
  6665. if (elf_text_symbol == NULL)
  6666. return false;
  6667. /* Initialize the section. */
  6668. mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
  6669. mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
  6670. elf_text_section->symbol = elf_text_symbol;
  6671. elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
  6672. elf_text_section->name = ".text";
  6673. elf_text_section->flags = SEC_NO_FLAGS;
  6674. elf_text_section->output_section = NULL;
  6675. elf_text_section->owner = abfd;
  6676. elf_text_symbol->name = ".text";
  6677. elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
  6678. elf_text_symbol->section = elf_text_section;
  6679. }
  6680. /* This code used to do *secp = bfd_und_section_ptr if
  6681. bfd_link_pic (info). I don't know why, and that doesn't make sense,
  6682. so I took it out. */
  6683. *secp = mips_elf_tdata (abfd)->elf_text_section;
  6684. break;
  6685. case SHN_MIPS_ACOMMON:
  6686. /* Fall through. XXX Can we treat this as allocated data? */
  6687. case SHN_MIPS_DATA:
  6688. /* This section is used in a shared object. */
  6689. if (mips_elf_tdata (abfd)->elf_data_section == NULL)
  6690. {
  6691. asymbol *elf_data_symbol;
  6692. asection *elf_data_section;
  6693. size_t amt = sizeof (asection);
  6694. elf_data_section = bfd_zalloc (abfd, amt);
  6695. if (elf_data_section == NULL)
  6696. return false;
  6697. amt = sizeof (asymbol);
  6698. elf_data_symbol = bfd_zalloc (abfd, amt);
  6699. if (elf_data_symbol == NULL)
  6700. return false;
  6701. /* Initialize the section. */
  6702. mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
  6703. mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
  6704. elf_data_section->symbol = elf_data_symbol;
  6705. elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
  6706. elf_data_section->name = ".data";
  6707. elf_data_section->flags = SEC_NO_FLAGS;
  6708. elf_data_section->output_section = NULL;
  6709. elf_data_section->owner = abfd;
  6710. elf_data_symbol->name = ".data";
  6711. elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
  6712. elf_data_symbol->section = elf_data_section;
  6713. }
  6714. /* This code used to do *secp = bfd_und_section_ptr if
  6715. bfd_link_pic (info). I don't know why, and that doesn't make sense,
  6716. so I took it out. */
  6717. *secp = mips_elf_tdata (abfd)->elf_data_section;
  6718. break;
  6719. case SHN_MIPS_SUNDEFINED:
  6720. *secp = bfd_und_section_ptr;
  6721. break;
  6722. }
  6723. if (SGI_COMPAT (abfd)
  6724. && ! bfd_link_pic (info)
  6725. && info->output_bfd->xvec == abfd->xvec
  6726. && strcmp (*namep, "__rld_obj_head") == 0)
  6727. {
  6728. struct elf_link_hash_entry *h;
  6729. struct bfd_link_hash_entry *bh;
  6730. /* Mark __rld_obj_head as dynamic. */
  6731. bh = NULL;
  6732. if (! (_bfd_generic_link_add_one_symbol
  6733. (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
  6734. get_elf_backend_data (abfd)->collect, &bh)))
  6735. return false;
  6736. h = (struct elf_link_hash_entry *) bh;
  6737. h->non_elf = 0;
  6738. h->def_regular = 1;
  6739. h->type = STT_OBJECT;
  6740. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6741. return false;
  6742. mips_elf_hash_table (info)->use_rld_obj_head = true;
  6743. mips_elf_hash_table (info)->rld_symbol = h;
  6744. }
  6745. /* If this is a mips16 text symbol, add 1 to the value to make it
  6746. odd. This will cause something like .word SYM to come up with
  6747. the right value when it is loaded into the PC. */
  6748. if (ELF_ST_IS_COMPRESSED (sym->st_other))
  6749. ++*valp;
  6750. return true;
  6751. }
  6752. /* This hook function is called before the linker writes out a global
  6753. symbol. We mark symbols as small common if appropriate. This is
  6754. also where we undo the increment of the value for a mips16 symbol. */
  6755. int
  6756. _bfd_mips_elf_link_output_symbol_hook
  6757. (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  6758. const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
  6759. asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
  6760. {
  6761. /* If we see a common symbol, which implies a relocatable link, then
  6762. if a symbol was small common in an input file, mark it as small
  6763. common in the output file. */
  6764. if (sym->st_shndx == SHN_COMMON
  6765. && strcmp (input_sec->name, ".scommon") == 0)
  6766. sym->st_shndx = SHN_MIPS_SCOMMON;
  6767. if (ELF_ST_IS_COMPRESSED (sym->st_other))
  6768. sym->st_value &= ~1;
  6769. return 1;
  6770. }
  6771. /* Functions for the dynamic linker. */
  6772. /* Create dynamic sections when linking against a dynamic object. */
  6773. bool
  6774. _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  6775. {
  6776. struct elf_link_hash_entry *h;
  6777. struct bfd_link_hash_entry *bh;
  6778. flagword flags;
  6779. register asection *s;
  6780. const char * const *namep;
  6781. struct mips_elf_link_hash_table *htab;
  6782. htab = mips_elf_hash_table (info);
  6783. BFD_ASSERT (htab != NULL);
  6784. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  6785. | SEC_LINKER_CREATED | SEC_READONLY);
  6786. /* The psABI requires a read-only .dynamic section, but the VxWorks
  6787. EABI doesn't. */
  6788. if (htab->root.target_os != is_vxworks)
  6789. {
  6790. s = bfd_get_linker_section (abfd, ".dynamic");
  6791. if (s != NULL)
  6792. {
  6793. if (!bfd_set_section_flags (s, flags))
  6794. return false;
  6795. }
  6796. }
  6797. /* We need to create .got section. */
  6798. if (!mips_elf_create_got_section (abfd, info))
  6799. return false;
  6800. if (! mips_elf_rel_dyn_section (info, true))
  6801. return false;
  6802. /* Create .stub section. */
  6803. s = bfd_make_section_anyway_with_flags (abfd,
  6804. MIPS_ELF_STUB_SECTION_NAME (abfd),
  6805. flags | SEC_CODE);
  6806. if (s == NULL
  6807. || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
  6808. return false;
  6809. htab->sstubs = s;
  6810. if (!mips_elf_hash_table (info)->use_rld_obj_head
  6811. && bfd_link_executable (info)
  6812. && bfd_get_linker_section (abfd, ".rld_map") == NULL)
  6813. {
  6814. s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
  6815. flags &~ (flagword) SEC_READONLY);
  6816. if (s == NULL
  6817. || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
  6818. return false;
  6819. }
  6820. /* Create .MIPS.xhash section. */
  6821. if (info->emit_gnu_hash)
  6822. s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
  6823. flags | SEC_READONLY);
  6824. /* On IRIX5, we adjust add some additional symbols and change the
  6825. alignments of several sections. There is no ABI documentation
  6826. indicating that this is necessary on IRIX6, nor any evidence that
  6827. the linker takes such action. */
  6828. if (IRIX_COMPAT (abfd) == ict_irix5)
  6829. {
  6830. for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
  6831. {
  6832. bh = NULL;
  6833. if (! (_bfd_generic_link_add_one_symbol
  6834. (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
  6835. NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
  6836. return false;
  6837. h = (struct elf_link_hash_entry *) bh;
  6838. h->mark = 1;
  6839. h->non_elf = 0;
  6840. h->def_regular = 1;
  6841. h->type = STT_SECTION;
  6842. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6843. return false;
  6844. }
  6845. /* We need to create a .compact_rel section. */
  6846. if (SGI_COMPAT (abfd))
  6847. {
  6848. if (!mips_elf_create_compact_rel_section (abfd, info))
  6849. return false;
  6850. }
  6851. /* Change alignments of some sections. */
  6852. s = bfd_get_linker_section (abfd, ".hash");
  6853. if (s != NULL)
  6854. bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
  6855. s = bfd_get_linker_section (abfd, ".dynsym");
  6856. if (s != NULL)
  6857. bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
  6858. s = bfd_get_linker_section (abfd, ".dynstr");
  6859. if (s != NULL)
  6860. bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
  6861. /* ??? */
  6862. s = bfd_get_section_by_name (abfd, ".reginfo");
  6863. if (s != NULL)
  6864. bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
  6865. s = bfd_get_linker_section (abfd, ".dynamic");
  6866. if (s != NULL)
  6867. bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
  6868. }
  6869. if (bfd_link_executable (info))
  6870. {
  6871. const char *name;
  6872. name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
  6873. bh = NULL;
  6874. if (!(_bfd_generic_link_add_one_symbol
  6875. (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
  6876. NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
  6877. return false;
  6878. h = (struct elf_link_hash_entry *) bh;
  6879. h->non_elf = 0;
  6880. h->def_regular = 1;
  6881. h->type = STT_SECTION;
  6882. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6883. return false;
  6884. if (! mips_elf_hash_table (info)->use_rld_obj_head)
  6885. {
  6886. /* __rld_map is a four byte word located in the .data section
  6887. and is filled in by the rtld to contain a pointer to
  6888. the _r_debug structure. Its symbol value will be set in
  6889. _bfd_mips_elf_finish_dynamic_symbol. */
  6890. s = bfd_get_linker_section (abfd, ".rld_map");
  6891. BFD_ASSERT (s != NULL);
  6892. name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
  6893. bh = NULL;
  6894. if (!(_bfd_generic_link_add_one_symbol
  6895. (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
  6896. get_elf_backend_data (abfd)->collect, &bh)))
  6897. return false;
  6898. h = (struct elf_link_hash_entry *) bh;
  6899. h->non_elf = 0;
  6900. h->def_regular = 1;
  6901. h->type = STT_OBJECT;
  6902. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6903. return false;
  6904. mips_elf_hash_table (info)->rld_symbol = h;
  6905. }
  6906. }
  6907. /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
  6908. Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
  6909. if (!_bfd_elf_create_dynamic_sections (abfd, info))
  6910. return false;
  6911. /* Do the usual VxWorks handling. */
  6912. if (htab->root.target_os == is_vxworks
  6913. && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
  6914. return false;
  6915. return true;
  6916. }
  6917. /* Return true if relocation REL against section SEC is a REL rather than
  6918. RELA relocation. RELOCS is the first relocation in the section and
  6919. ABFD is the bfd that contains SEC. */
  6920. static bool
  6921. mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
  6922. const Elf_Internal_Rela *relocs,
  6923. const Elf_Internal_Rela *rel)
  6924. {
  6925. Elf_Internal_Shdr *rel_hdr;
  6926. const struct elf_backend_data *bed;
  6927. /* To determine which flavor of relocation this is, we depend on the
  6928. fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
  6929. rel_hdr = elf_section_data (sec)->rel.hdr;
  6930. if (rel_hdr == NULL)
  6931. return false;
  6932. bed = get_elf_backend_data (abfd);
  6933. return ((size_t) (rel - relocs)
  6934. < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
  6935. }
  6936. /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
  6937. HOWTO is the relocation's howto and CONTENTS points to the contents
  6938. of the section that REL is against. */
  6939. static bfd_vma
  6940. mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
  6941. reloc_howto_type *howto, bfd_byte *contents)
  6942. {
  6943. bfd_byte *location;
  6944. unsigned int r_type;
  6945. bfd_vma addend;
  6946. bfd_vma bytes;
  6947. r_type = ELF_R_TYPE (abfd, rel->r_info);
  6948. location = contents + rel->r_offset;
  6949. /* Get the addend, which is stored in the input file. */
  6950. _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
  6951. bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
  6952. _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
  6953. addend = bytes & howto->src_mask;
  6954. /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
  6955. accordingly. */
  6956. if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
  6957. addend <<= 1;
  6958. return addend;
  6959. }
  6960. /* REL is a relocation in ABFD that needs a partnering LO16 relocation
  6961. and *ADDEND is the addend for REL itself. Look for the LO16 relocation
  6962. and update *ADDEND with the final addend. Return true on success
  6963. or false if the LO16 could not be found. RELEND is the exclusive
  6964. upper bound on the relocations for REL's section. */
  6965. static bool
  6966. mips_elf_add_lo16_rel_addend (bfd *abfd,
  6967. const Elf_Internal_Rela *rel,
  6968. const Elf_Internal_Rela *relend,
  6969. bfd_byte *contents, bfd_vma *addend)
  6970. {
  6971. unsigned int r_type, lo16_type;
  6972. const Elf_Internal_Rela *lo16_relocation;
  6973. reloc_howto_type *lo16_howto;
  6974. bfd_vma l;
  6975. r_type = ELF_R_TYPE (abfd, rel->r_info);
  6976. if (mips16_reloc_p (r_type))
  6977. lo16_type = R_MIPS16_LO16;
  6978. else if (micromips_reloc_p (r_type))
  6979. lo16_type = R_MICROMIPS_LO16;
  6980. else if (r_type == R_MIPS_PCHI16)
  6981. lo16_type = R_MIPS_PCLO16;
  6982. else
  6983. lo16_type = R_MIPS_LO16;
  6984. /* The combined value is the sum of the HI16 addend, left-shifted by
  6985. sixteen bits, and the LO16 addend, sign extended. (Usually, the
  6986. code does a `lui' of the HI16 value, and then an `addiu' of the
  6987. LO16 value.)
  6988. Scan ahead to find a matching LO16 relocation.
  6989. According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
  6990. be immediately following. However, for the IRIX6 ABI, the next
  6991. relocation may be a composed relocation consisting of several
  6992. relocations for the same address. In that case, the R_MIPS_LO16
  6993. relocation may occur as one of these. We permit a similar
  6994. extension in general, as that is useful for GCC.
  6995. In some cases GCC dead code elimination removes the LO16 but keeps
  6996. the corresponding HI16. This is strictly speaking a violation of
  6997. the ABI but not immediately harmful. */
  6998. lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
  6999. if (lo16_relocation == NULL)
  7000. return false;
  7001. /* Obtain the addend kept there. */
  7002. lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
  7003. l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
  7004. l <<= lo16_howto->rightshift;
  7005. l = _bfd_mips_elf_sign_extend (l, 16);
  7006. *addend <<= 16;
  7007. *addend += l;
  7008. return true;
  7009. }
  7010. /* Try to read the contents of section SEC in bfd ABFD. Return true and
  7011. store the contents in *CONTENTS on success. Assume that *CONTENTS
  7012. already holds the contents if it is nonull on entry. */
  7013. static bool
  7014. mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
  7015. {
  7016. if (*contents)
  7017. return true;
  7018. /* Get cached copy if it exists. */
  7019. if (elf_section_data (sec)->this_hdr.contents != NULL)
  7020. {
  7021. *contents = elf_section_data (sec)->this_hdr.contents;
  7022. return true;
  7023. }
  7024. return bfd_malloc_and_get_section (abfd, sec, contents);
  7025. }
  7026. /* Make a new PLT record to keep internal data. */
  7027. static struct plt_entry *
  7028. mips_elf_make_plt_record (bfd *abfd)
  7029. {
  7030. struct plt_entry *entry;
  7031. entry = bfd_zalloc (abfd, sizeof (*entry));
  7032. if (entry == NULL)
  7033. return NULL;
  7034. entry->stub_offset = MINUS_ONE;
  7035. entry->mips_offset = MINUS_ONE;
  7036. entry->comp_offset = MINUS_ONE;
  7037. entry->gotplt_index = MINUS_ONE;
  7038. return entry;
  7039. }
  7040. /* Define the special `__gnu_absolute_zero' symbol. We only need this
  7041. for PIC code, as otherwise there is no load-time relocation involved
  7042. and local GOT entries whose value is zero at static link time will
  7043. retain their value at load time. */
  7044. static bool
  7045. mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
  7046. struct mips_elf_link_hash_table *htab,
  7047. unsigned int r_type)
  7048. {
  7049. union
  7050. {
  7051. struct elf_link_hash_entry *eh;
  7052. struct bfd_link_hash_entry *bh;
  7053. }
  7054. hzero;
  7055. BFD_ASSERT (!htab->use_absolute_zero);
  7056. BFD_ASSERT (bfd_link_pic (info));
  7057. hzero.bh = NULL;
  7058. if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
  7059. BSF_GLOBAL, bfd_abs_section_ptr, 0,
  7060. NULL, false, false, &hzero.bh))
  7061. return false;
  7062. BFD_ASSERT (hzero.bh != NULL);
  7063. hzero.eh->size = 0;
  7064. hzero.eh->type = STT_NOTYPE;
  7065. hzero.eh->other = STV_PROTECTED;
  7066. hzero.eh->def_regular = 1;
  7067. hzero.eh->non_elf = 0;
  7068. if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
  7069. return false;
  7070. htab->use_absolute_zero = true;
  7071. return true;
  7072. }
  7073. /* Look through the relocs for a section during the first phase, and
  7074. allocate space in the global offset table and record the need for
  7075. standard MIPS and compressed procedure linkage table entries. */
  7076. bool
  7077. _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
  7078. asection *sec, const Elf_Internal_Rela *relocs)
  7079. {
  7080. const char *name;
  7081. bfd *dynobj;
  7082. Elf_Internal_Shdr *symtab_hdr;
  7083. struct elf_link_hash_entry **sym_hashes;
  7084. size_t extsymoff;
  7085. const Elf_Internal_Rela *rel;
  7086. const Elf_Internal_Rela *rel_end;
  7087. asection *sreloc;
  7088. const struct elf_backend_data *bed;
  7089. struct mips_elf_link_hash_table *htab;
  7090. bfd_byte *contents;
  7091. bfd_vma addend;
  7092. reloc_howto_type *howto;
  7093. if (bfd_link_relocatable (info))
  7094. return true;
  7095. htab = mips_elf_hash_table (info);
  7096. BFD_ASSERT (htab != NULL);
  7097. dynobj = elf_hash_table (info)->dynobj;
  7098. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  7099. sym_hashes = elf_sym_hashes (abfd);
  7100. extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
  7101. bed = get_elf_backend_data (abfd);
  7102. rel_end = relocs + sec->reloc_count;
  7103. /* Check for the mips16 stub sections. */
  7104. name = bfd_section_name (sec);
  7105. if (FN_STUB_P (name))
  7106. {
  7107. unsigned long r_symndx;
  7108. /* Look at the relocation information to figure out which symbol
  7109. this is for. */
  7110. r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
  7111. if (r_symndx == 0)
  7112. {
  7113. _bfd_error_handler
  7114. /* xgettext:c-format */
  7115. (_("%pB: warning: cannot determine the target function for"
  7116. " stub section `%s'"),
  7117. abfd, name);
  7118. bfd_set_error (bfd_error_bad_value);
  7119. return false;
  7120. }
  7121. if (r_symndx < extsymoff
  7122. || sym_hashes[r_symndx - extsymoff] == NULL)
  7123. {
  7124. asection *o;
  7125. /* This stub is for a local symbol. This stub will only be
  7126. needed if there is some relocation in this BFD, other
  7127. than a 16 bit function call, which refers to this symbol. */
  7128. for (o = abfd->sections; o != NULL; o = o->next)
  7129. {
  7130. Elf_Internal_Rela *sec_relocs;
  7131. const Elf_Internal_Rela *r, *rend;
  7132. /* We can ignore stub sections when looking for relocs. */
  7133. if ((o->flags & SEC_RELOC) == 0
  7134. || o->reloc_count == 0
  7135. || section_allows_mips16_refs_p (o))
  7136. continue;
  7137. sec_relocs
  7138. = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
  7139. info->keep_memory);
  7140. if (sec_relocs == NULL)
  7141. return false;
  7142. rend = sec_relocs + o->reloc_count;
  7143. for (r = sec_relocs; r < rend; r++)
  7144. if (ELF_R_SYM (abfd, r->r_info) == r_symndx
  7145. && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
  7146. break;
  7147. if (elf_section_data (o)->relocs != sec_relocs)
  7148. free (sec_relocs);
  7149. if (r < rend)
  7150. break;
  7151. }
  7152. if (o == NULL)
  7153. {
  7154. /* There is no non-call reloc for this stub, so we do
  7155. not need it. Since this function is called before
  7156. the linker maps input sections to output sections, we
  7157. can easily discard it by setting the SEC_EXCLUDE
  7158. flag. */
  7159. sec->flags |= SEC_EXCLUDE;
  7160. return true;
  7161. }
  7162. /* Record this stub in an array of local symbol stubs for
  7163. this BFD. */
  7164. if (mips_elf_tdata (abfd)->local_stubs == NULL)
  7165. {
  7166. unsigned long symcount;
  7167. asection **n;
  7168. bfd_size_type amt;
  7169. if (elf_bad_symtab (abfd))
  7170. symcount = NUM_SHDR_ENTRIES (symtab_hdr);
  7171. else
  7172. symcount = symtab_hdr->sh_info;
  7173. amt = symcount * sizeof (asection *);
  7174. n = bfd_zalloc (abfd, amt);
  7175. if (n == NULL)
  7176. return false;
  7177. mips_elf_tdata (abfd)->local_stubs = n;
  7178. }
  7179. sec->flags |= SEC_KEEP;
  7180. mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
  7181. /* We don't need to set mips16_stubs_seen in this case.
  7182. That flag is used to see whether we need to look through
  7183. the global symbol table for stubs. We don't need to set
  7184. it here, because we just have a local stub. */
  7185. }
  7186. else
  7187. {
  7188. struct mips_elf_link_hash_entry *h;
  7189. h = ((struct mips_elf_link_hash_entry *)
  7190. sym_hashes[r_symndx - extsymoff]);
  7191. while (h->root.root.type == bfd_link_hash_indirect
  7192. || h->root.root.type == bfd_link_hash_warning)
  7193. h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
  7194. /* H is the symbol this stub is for. */
  7195. /* If we already have an appropriate stub for this function, we
  7196. don't need another one, so we can discard this one. Since
  7197. this function is called before the linker maps input sections
  7198. to output sections, we can easily discard it by setting the
  7199. SEC_EXCLUDE flag. */
  7200. if (h->fn_stub != NULL)
  7201. {
  7202. sec->flags |= SEC_EXCLUDE;
  7203. return true;
  7204. }
  7205. sec->flags |= SEC_KEEP;
  7206. h->fn_stub = sec;
  7207. mips_elf_hash_table (info)->mips16_stubs_seen = true;
  7208. }
  7209. }
  7210. else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
  7211. {
  7212. unsigned long r_symndx;
  7213. struct mips_elf_link_hash_entry *h;
  7214. asection **loc;
  7215. /* Look at the relocation information to figure out which symbol
  7216. this is for. */
  7217. r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
  7218. if (r_symndx == 0)
  7219. {
  7220. _bfd_error_handler
  7221. /* xgettext:c-format */
  7222. (_("%pB: warning: cannot determine the target function for"
  7223. " stub section `%s'"),
  7224. abfd, name);
  7225. bfd_set_error (bfd_error_bad_value);
  7226. return false;
  7227. }
  7228. if (r_symndx < extsymoff
  7229. || sym_hashes[r_symndx - extsymoff] == NULL)
  7230. {
  7231. asection *o;
  7232. /* This stub is for a local symbol. This stub will only be
  7233. needed if there is some relocation (R_MIPS16_26) in this BFD
  7234. that refers to this symbol. */
  7235. for (o = abfd->sections; o != NULL; o = o->next)
  7236. {
  7237. Elf_Internal_Rela *sec_relocs;
  7238. const Elf_Internal_Rela *r, *rend;
  7239. /* We can ignore stub sections when looking for relocs. */
  7240. if ((o->flags & SEC_RELOC) == 0
  7241. || o->reloc_count == 0
  7242. || section_allows_mips16_refs_p (o))
  7243. continue;
  7244. sec_relocs
  7245. = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
  7246. info->keep_memory);
  7247. if (sec_relocs == NULL)
  7248. return false;
  7249. rend = sec_relocs + o->reloc_count;
  7250. for (r = sec_relocs; r < rend; r++)
  7251. if (ELF_R_SYM (abfd, r->r_info) == r_symndx
  7252. && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
  7253. break;
  7254. if (elf_section_data (o)->relocs != sec_relocs)
  7255. free (sec_relocs);
  7256. if (r < rend)
  7257. break;
  7258. }
  7259. if (o == NULL)
  7260. {
  7261. /* There is no non-call reloc for this stub, so we do
  7262. not need it. Since this function is called before
  7263. the linker maps input sections to output sections, we
  7264. can easily discard it by setting the SEC_EXCLUDE
  7265. flag. */
  7266. sec->flags |= SEC_EXCLUDE;
  7267. return true;
  7268. }
  7269. /* Record this stub in an array of local symbol call_stubs for
  7270. this BFD. */
  7271. if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
  7272. {
  7273. unsigned long symcount;
  7274. asection **n;
  7275. bfd_size_type amt;
  7276. if (elf_bad_symtab (abfd))
  7277. symcount = NUM_SHDR_ENTRIES (symtab_hdr);
  7278. else
  7279. symcount = symtab_hdr->sh_info;
  7280. amt = symcount * sizeof (asection *);
  7281. n = bfd_zalloc (abfd, amt);
  7282. if (n == NULL)
  7283. return false;
  7284. mips_elf_tdata (abfd)->local_call_stubs = n;
  7285. }
  7286. sec->flags |= SEC_KEEP;
  7287. mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
  7288. /* We don't need to set mips16_stubs_seen in this case.
  7289. That flag is used to see whether we need to look through
  7290. the global symbol table for stubs. We don't need to set
  7291. it here, because we just have a local stub. */
  7292. }
  7293. else
  7294. {
  7295. h = ((struct mips_elf_link_hash_entry *)
  7296. sym_hashes[r_symndx - extsymoff]);
  7297. /* H is the symbol this stub is for. */
  7298. if (CALL_FP_STUB_P (name))
  7299. loc = &h->call_fp_stub;
  7300. else
  7301. loc = &h->call_stub;
  7302. /* If we already have an appropriate stub for this function, we
  7303. don't need another one, so we can discard this one. Since
  7304. this function is called before the linker maps input sections
  7305. to output sections, we can easily discard it by setting the
  7306. SEC_EXCLUDE flag. */
  7307. if (*loc != NULL)
  7308. {
  7309. sec->flags |= SEC_EXCLUDE;
  7310. return true;
  7311. }
  7312. sec->flags |= SEC_KEEP;
  7313. *loc = sec;
  7314. mips_elf_hash_table (info)->mips16_stubs_seen = true;
  7315. }
  7316. }
  7317. sreloc = NULL;
  7318. contents = NULL;
  7319. for (rel = relocs; rel < rel_end; ++rel)
  7320. {
  7321. unsigned long r_symndx;
  7322. unsigned int r_type;
  7323. struct elf_link_hash_entry *h;
  7324. bool can_make_dynamic_p;
  7325. bool call_reloc_p;
  7326. bool constrain_symbol_p;
  7327. r_symndx = ELF_R_SYM (abfd, rel->r_info);
  7328. r_type = ELF_R_TYPE (abfd, rel->r_info);
  7329. if (r_symndx < extsymoff)
  7330. h = NULL;
  7331. else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
  7332. {
  7333. _bfd_error_handler
  7334. /* xgettext:c-format */
  7335. (_("%pB: malformed reloc detected for section %s"),
  7336. abfd, name);
  7337. bfd_set_error (bfd_error_bad_value);
  7338. return false;
  7339. }
  7340. else
  7341. {
  7342. h = sym_hashes[r_symndx - extsymoff];
  7343. if (h != NULL)
  7344. {
  7345. while (h->root.type == bfd_link_hash_indirect
  7346. || h->root.type == bfd_link_hash_warning)
  7347. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  7348. }
  7349. }
  7350. /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
  7351. relocation into a dynamic one. */
  7352. can_make_dynamic_p = false;
  7353. /* Set CALL_RELOC_P to true if the relocation is for a call,
  7354. and if pointer equality therefore doesn't matter. */
  7355. call_reloc_p = false;
  7356. /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
  7357. into account when deciding how to define the symbol. */
  7358. constrain_symbol_p = true;
  7359. switch (r_type)
  7360. {
  7361. case R_MIPS_CALL16:
  7362. case R_MIPS_CALL_HI16:
  7363. case R_MIPS_CALL_LO16:
  7364. case R_MIPS16_CALL16:
  7365. case R_MICROMIPS_CALL16:
  7366. case R_MICROMIPS_CALL_HI16:
  7367. case R_MICROMIPS_CALL_LO16:
  7368. call_reloc_p = true;
  7369. /* Fall through. */
  7370. case R_MIPS_GOT16:
  7371. case R_MIPS_GOT_LO16:
  7372. case R_MIPS_GOT_PAGE:
  7373. case R_MIPS_GOT_DISP:
  7374. case R_MIPS16_GOT16:
  7375. case R_MICROMIPS_GOT16:
  7376. case R_MICROMIPS_GOT_LO16:
  7377. case R_MICROMIPS_GOT_PAGE:
  7378. case R_MICROMIPS_GOT_DISP:
  7379. /* If we have a symbol that will resolve to zero at static link
  7380. time and it is used by a GOT relocation applied to code we
  7381. cannot relax to an immediate zero load, then we will be using
  7382. the special `__gnu_absolute_zero' symbol whose value is zero
  7383. at dynamic load time. We ignore HI16-type GOT relocations at
  7384. this stage, because their handling will depend entirely on
  7385. the corresponding LO16-type GOT relocation. */
  7386. if (!call_hi16_reloc_p (r_type)
  7387. && h != NULL
  7388. && bfd_link_pic (info)
  7389. && !htab->use_absolute_zero
  7390. && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  7391. {
  7392. bool rel_reloc;
  7393. if (!mips_elf_get_section_contents (abfd, sec, &contents))
  7394. return false;
  7395. rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
  7396. howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
  7397. if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
  7398. false))
  7399. if (!mips_elf_define_absolute_zero (abfd, info, htab, r_type))
  7400. return false;
  7401. }
  7402. /* Fall through. */
  7403. case R_MIPS_GOT_HI16:
  7404. case R_MIPS_GOT_OFST:
  7405. case R_MIPS_TLS_GOTTPREL:
  7406. case R_MIPS_TLS_GD:
  7407. case R_MIPS_TLS_LDM:
  7408. case R_MIPS16_TLS_GOTTPREL:
  7409. case R_MIPS16_TLS_GD:
  7410. case R_MIPS16_TLS_LDM:
  7411. case R_MICROMIPS_GOT_HI16:
  7412. case R_MICROMIPS_GOT_OFST:
  7413. case R_MICROMIPS_TLS_GOTTPREL:
  7414. case R_MICROMIPS_TLS_GD:
  7415. case R_MICROMIPS_TLS_LDM:
  7416. if (dynobj == NULL)
  7417. elf_hash_table (info)->dynobj = dynobj = abfd;
  7418. if (!mips_elf_create_got_section (dynobj, info))
  7419. return false;
  7420. if (htab->root.target_os == is_vxworks
  7421. && !bfd_link_pic (info))
  7422. {
  7423. _bfd_error_handler
  7424. /* xgettext:c-format */
  7425. (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
  7426. abfd, (uint64_t) rel->r_offset);
  7427. bfd_set_error (bfd_error_bad_value);
  7428. return false;
  7429. }
  7430. can_make_dynamic_p = true;
  7431. break;
  7432. case R_MIPS_NONE:
  7433. case R_MIPS_JALR:
  7434. case R_MICROMIPS_JALR:
  7435. /* These relocations have empty fields and are purely there to
  7436. provide link information. The symbol value doesn't matter. */
  7437. constrain_symbol_p = false;
  7438. break;
  7439. case R_MIPS_GPREL16:
  7440. case R_MIPS_GPREL32:
  7441. case R_MIPS16_GPREL:
  7442. case R_MICROMIPS_GPREL16:
  7443. /* GP-relative relocations always resolve to a definition in a
  7444. regular input file, ignoring the one-definition rule. This is
  7445. important for the GP setup sequence in NewABI code, which
  7446. always resolves to a local function even if other relocations
  7447. against the symbol wouldn't. */
  7448. constrain_symbol_p = false;
  7449. break;
  7450. case R_MIPS_32:
  7451. case R_MIPS_REL32:
  7452. case R_MIPS_64:
  7453. /* In VxWorks executables, references to external symbols
  7454. must be handled using copy relocs or PLT entries; it is not
  7455. possible to convert this relocation into a dynamic one.
  7456. For executables that use PLTs and copy-relocs, we have a
  7457. choice between converting the relocation into a dynamic
  7458. one or using copy relocations or PLT entries. It is
  7459. usually better to do the former, unless the relocation is
  7460. against a read-only section. */
  7461. if ((bfd_link_pic (info)
  7462. || (h != NULL
  7463. && htab->root.target_os != is_vxworks
  7464. && strcmp (h->root.root.string, "__gnu_local_gp") != 0
  7465. && !(!info->nocopyreloc
  7466. && !PIC_OBJECT_P (abfd)
  7467. && MIPS_ELF_READONLY_SECTION (sec))))
  7468. && (sec->flags & SEC_ALLOC) != 0)
  7469. {
  7470. can_make_dynamic_p = true;
  7471. if (dynobj == NULL)
  7472. elf_hash_table (info)->dynobj = dynobj = abfd;
  7473. }
  7474. break;
  7475. case R_MIPS_26:
  7476. case R_MIPS_PC16:
  7477. case R_MIPS_PC21_S2:
  7478. case R_MIPS_PC26_S2:
  7479. case R_MIPS16_26:
  7480. case R_MIPS16_PC16_S1:
  7481. case R_MICROMIPS_26_S1:
  7482. case R_MICROMIPS_PC7_S1:
  7483. case R_MICROMIPS_PC10_S1:
  7484. case R_MICROMIPS_PC16_S1:
  7485. case R_MICROMIPS_PC23_S2:
  7486. call_reloc_p = true;
  7487. break;
  7488. }
  7489. if (h)
  7490. {
  7491. if (constrain_symbol_p)
  7492. {
  7493. if (!can_make_dynamic_p)
  7494. ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
  7495. if (!call_reloc_p)
  7496. h->pointer_equality_needed = 1;
  7497. /* We must not create a stub for a symbol that has
  7498. relocations related to taking the function's address.
  7499. This doesn't apply to VxWorks, where CALL relocs refer
  7500. to a .got.plt entry instead of a normal .got entry. */
  7501. if (htab->root.target_os != is_vxworks
  7502. && (!can_make_dynamic_p || !call_reloc_p))
  7503. ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
  7504. }
  7505. /* Relocations against the special VxWorks __GOTT_BASE__ and
  7506. __GOTT_INDEX__ symbols must be left to the loader. Allocate
  7507. room for them in .rela.dyn. */
  7508. if (is_gott_symbol (info, h))
  7509. {
  7510. if (sreloc == NULL)
  7511. {
  7512. sreloc = mips_elf_rel_dyn_section (info, true);
  7513. if (sreloc == NULL)
  7514. return false;
  7515. }
  7516. mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
  7517. if (MIPS_ELF_READONLY_SECTION (sec))
  7518. /* We tell the dynamic linker that there are
  7519. relocations against the text segment. */
  7520. info->flags |= DF_TEXTREL;
  7521. }
  7522. }
  7523. else if (call_lo16_reloc_p (r_type)
  7524. || got_lo16_reloc_p (r_type)
  7525. || got_disp_reloc_p (r_type)
  7526. || (got16_reloc_p (r_type)
  7527. && htab->root.target_os == is_vxworks))
  7528. {
  7529. /* We may need a local GOT entry for this relocation. We
  7530. don't count R_MIPS_GOT_PAGE because we can estimate the
  7531. maximum number of pages needed by looking at the size of
  7532. the segment. Similar comments apply to R_MIPS*_GOT16 and
  7533. R_MIPS*_CALL16, except on VxWorks, where GOT relocations
  7534. always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
  7535. R_MIPS_CALL_HI16 because these are always followed by an
  7536. R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
  7537. if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
  7538. rel->r_addend, info, r_type))
  7539. return false;
  7540. }
  7541. if (h != NULL
  7542. && mips_elf_relocation_needs_la25_stub (abfd, r_type,
  7543. ELF_ST_IS_MIPS16 (h->other)))
  7544. ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
  7545. switch (r_type)
  7546. {
  7547. case R_MIPS_CALL16:
  7548. case R_MIPS16_CALL16:
  7549. case R_MICROMIPS_CALL16:
  7550. if (h == NULL)
  7551. {
  7552. _bfd_error_handler
  7553. /* xgettext:c-format */
  7554. (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
  7555. abfd, (uint64_t) rel->r_offset);
  7556. bfd_set_error (bfd_error_bad_value);
  7557. return false;
  7558. }
  7559. /* Fall through. */
  7560. case R_MIPS_CALL_HI16:
  7561. case R_MIPS_CALL_LO16:
  7562. case R_MICROMIPS_CALL_HI16:
  7563. case R_MICROMIPS_CALL_LO16:
  7564. if (h != NULL)
  7565. {
  7566. /* Make sure there is room in the regular GOT to hold the
  7567. function's address. We may eliminate it in favour of
  7568. a .got.plt entry later; see mips_elf_count_got_symbols. */
  7569. if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
  7570. r_type))
  7571. return false;
  7572. /* We need a stub, not a plt entry for the undefined
  7573. function. But we record it as if it needs plt. See
  7574. _bfd_elf_adjust_dynamic_symbol. */
  7575. h->needs_plt = 1;
  7576. h->type = STT_FUNC;
  7577. }
  7578. break;
  7579. case R_MIPS_GOT_PAGE:
  7580. case R_MICROMIPS_GOT_PAGE:
  7581. case R_MIPS16_GOT16:
  7582. case R_MIPS_GOT16:
  7583. case R_MIPS_GOT_HI16:
  7584. case R_MIPS_GOT_LO16:
  7585. case R_MICROMIPS_GOT16:
  7586. case R_MICROMIPS_GOT_HI16:
  7587. case R_MICROMIPS_GOT_LO16:
  7588. if (!h || got_page_reloc_p (r_type))
  7589. {
  7590. /* This relocation needs (or may need, if h != NULL) a
  7591. page entry in the GOT. For R_MIPS_GOT_PAGE we do not
  7592. know for sure until we know whether the symbol is
  7593. preemptible. */
  7594. if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
  7595. {
  7596. if (!mips_elf_get_section_contents (abfd, sec, &contents))
  7597. return false;
  7598. howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
  7599. addend = mips_elf_read_rel_addend (abfd, rel,
  7600. howto, contents);
  7601. if (got16_reloc_p (r_type))
  7602. mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
  7603. contents, &addend);
  7604. else
  7605. addend <<= howto->rightshift;
  7606. }
  7607. else
  7608. addend = rel->r_addend;
  7609. if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
  7610. h, addend))
  7611. return false;
  7612. if (h)
  7613. {
  7614. struct mips_elf_link_hash_entry *hmips =
  7615. (struct mips_elf_link_hash_entry *) h;
  7616. /* This symbol is definitely not overridable. */
  7617. if (hmips->root.def_regular
  7618. && ! (bfd_link_pic (info) && ! info->symbolic
  7619. && ! hmips->root.forced_local))
  7620. h = NULL;
  7621. }
  7622. }
  7623. /* If this is a global, overridable symbol, GOT_PAGE will
  7624. decay to GOT_DISP, so we'll need a GOT entry for it. */
  7625. /* Fall through. */
  7626. case R_MIPS_GOT_DISP:
  7627. case R_MICROMIPS_GOT_DISP:
  7628. if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
  7629. false, r_type))
  7630. return false;
  7631. break;
  7632. case R_MIPS_TLS_GOTTPREL:
  7633. case R_MIPS16_TLS_GOTTPREL:
  7634. case R_MICROMIPS_TLS_GOTTPREL:
  7635. if (bfd_link_pic (info))
  7636. info->flags |= DF_STATIC_TLS;
  7637. /* Fall through */
  7638. case R_MIPS_TLS_LDM:
  7639. case R_MIPS16_TLS_LDM:
  7640. case R_MICROMIPS_TLS_LDM:
  7641. if (tls_ldm_reloc_p (r_type))
  7642. {
  7643. r_symndx = STN_UNDEF;
  7644. h = NULL;
  7645. }
  7646. /* Fall through */
  7647. case R_MIPS_TLS_GD:
  7648. case R_MIPS16_TLS_GD:
  7649. case R_MICROMIPS_TLS_GD:
  7650. /* This symbol requires a global offset table entry, or two
  7651. for TLS GD relocations. */
  7652. if (h != NULL)
  7653. {
  7654. if (!mips_elf_record_global_got_symbol (h, abfd, info,
  7655. false, r_type))
  7656. return false;
  7657. }
  7658. else
  7659. {
  7660. if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
  7661. rel->r_addend,
  7662. info, r_type))
  7663. return false;
  7664. }
  7665. break;
  7666. case R_MIPS_32:
  7667. case R_MIPS_REL32:
  7668. case R_MIPS_64:
  7669. /* In VxWorks executables, references to external symbols
  7670. are handled using copy relocs or PLT stubs, so there's
  7671. no need to add a .rela.dyn entry for this relocation. */
  7672. if (can_make_dynamic_p)
  7673. {
  7674. if (sreloc == NULL)
  7675. {
  7676. sreloc = mips_elf_rel_dyn_section (info, true);
  7677. if (sreloc == NULL)
  7678. return false;
  7679. }
  7680. if (bfd_link_pic (info) && h == NULL)
  7681. {
  7682. /* When creating a shared object, we must copy these
  7683. reloc types into the output file as R_MIPS_REL32
  7684. relocs. Make room for this reloc in .rel(a).dyn. */
  7685. mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
  7686. if (MIPS_ELF_READONLY_SECTION (sec))
  7687. /* We tell the dynamic linker that there are
  7688. relocations against the text segment. */
  7689. info->flags |= DF_TEXTREL;
  7690. }
  7691. else
  7692. {
  7693. struct mips_elf_link_hash_entry *hmips;
  7694. /* For a shared object, we must copy this relocation
  7695. unless the symbol turns out to be undefined and
  7696. weak with non-default visibility, in which case
  7697. it will be left as zero.
  7698. We could elide R_MIPS_REL32 for locally binding symbols
  7699. in shared libraries, but do not yet do so.
  7700. For an executable, we only need to copy this
  7701. reloc if the symbol is defined in a dynamic
  7702. object. */
  7703. hmips = (struct mips_elf_link_hash_entry *) h;
  7704. ++hmips->possibly_dynamic_relocs;
  7705. if (MIPS_ELF_READONLY_SECTION (sec))
  7706. /* We need it to tell the dynamic linker if there
  7707. are relocations against the text segment. */
  7708. hmips->readonly_reloc = true;
  7709. }
  7710. }
  7711. if (SGI_COMPAT (abfd))
  7712. mips_elf_hash_table (info)->compact_rel_size +=
  7713. sizeof (Elf32_External_crinfo);
  7714. break;
  7715. case R_MIPS_26:
  7716. case R_MIPS_GPREL16:
  7717. case R_MIPS_LITERAL:
  7718. case R_MIPS_GPREL32:
  7719. case R_MICROMIPS_26_S1:
  7720. case R_MICROMIPS_GPREL16:
  7721. case R_MICROMIPS_LITERAL:
  7722. case R_MICROMIPS_GPREL7_S2:
  7723. if (SGI_COMPAT (abfd))
  7724. mips_elf_hash_table (info)->compact_rel_size +=
  7725. sizeof (Elf32_External_crinfo);
  7726. break;
  7727. /* This relocation describes the C++ object vtable hierarchy.
  7728. Reconstruct it for later use during GC. */
  7729. case R_MIPS_GNU_VTINHERIT:
  7730. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  7731. return false;
  7732. break;
  7733. /* This relocation describes which C++ vtable entries are actually
  7734. used. Record for later use during GC. */
  7735. case R_MIPS_GNU_VTENTRY:
  7736. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
  7737. return false;
  7738. break;
  7739. default:
  7740. break;
  7741. }
  7742. /* Record the need for a PLT entry. At this point we don't know
  7743. yet if we are going to create a PLT in the first place, but
  7744. we only record whether the relocation requires a standard MIPS
  7745. or a compressed code entry anyway. If we don't make a PLT after
  7746. all, then we'll just ignore these arrangements. Likewise if
  7747. a PLT entry is not created because the symbol is satisfied
  7748. locally. */
  7749. if (h != NULL
  7750. && (branch_reloc_p (r_type)
  7751. || mips16_branch_reloc_p (r_type)
  7752. || micromips_branch_reloc_p (r_type))
  7753. && !SYMBOL_CALLS_LOCAL (info, h))
  7754. {
  7755. if (h->plt.plist == NULL)
  7756. h->plt.plist = mips_elf_make_plt_record (abfd);
  7757. if (h->plt.plist == NULL)
  7758. return false;
  7759. if (branch_reloc_p (r_type))
  7760. h->plt.plist->need_mips = true;
  7761. else
  7762. h->plt.plist->need_comp = true;
  7763. }
  7764. /* See if this reloc would need to refer to a MIPS16 hard-float stub,
  7765. if there is one. We only need to handle global symbols here;
  7766. we decide whether to keep or delete stubs for local symbols
  7767. when processing the stub's relocations. */
  7768. if (h != NULL
  7769. && !mips16_call_reloc_p (r_type)
  7770. && !section_allows_mips16_refs_p (sec))
  7771. {
  7772. struct mips_elf_link_hash_entry *mh;
  7773. mh = (struct mips_elf_link_hash_entry *) h;
  7774. mh->need_fn_stub = true;
  7775. }
  7776. /* Refuse some position-dependent relocations when creating a
  7777. shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
  7778. not PIC, but we can create dynamic relocations and the result
  7779. will be fine. Also do not refuse R_MIPS_LO16, which can be
  7780. combined with R_MIPS_GOT16. */
  7781. if (bfd_link_pic (info))
  7782. {
  7783. switch (r_type)
  7784. {
  7785. case R_MIPS_TLS_TPREL_HI16:
  7786. case R_MIPS16_TLS_TPREL_HI16:
  7787. case R_MICROMIPS_TLS_TPREL_HI16:
  7788. case R_MIPS_TLS_TPREL_LO16:
  7789. case R_MIPS16_TLS_TPREL_LO16:
  7790. case R_MICROMIPS_TLS_TPREL_LO16:
  7791. /* These are okay in PIE, but not in a shared library. */
  7792. if (bfd_link_executable (info))
  7793. break;
  7794. /* FALLTHROUGH */
  7795. case R_MIPS16_HI16:
  7796. case R_MIPS_HI16:
  7797. case R_MIPS_HIGHER:
  7798. case R_MIPS_HIGHEST:
  7799. case R_MICROMIPS_HI16:
  7800. case R_MICROMIPS_HIGHER:
  7801. case R_MICROMIPS_HIGHEST:
  7802. /* Don't refuse a high part relocation if it's against
  7803. no symbol (e.g. part of a compound relocation). */
  7804. if (r_symndx == STN_UNDEF)
  7805. break;
  7806. /* Likewise an absolute symbol. */
  7807. if (h != NULL && bfd_is_abs_symbol (&h->root))
  7808. break;
  7809. /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
  7810. and has a special meaning. */
  7811. if (!NEWABI_P (abfd) && h != NULL
  7812. && strcmp (h->root.root.string, "_gp_disp") == 0)
  7813. break;
  7814. /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
  7815. if (is_gott_symbol (info, h))
  7816. break;
  7817. /* FALLTHROUGH */
  7818. case R_MIPS16_26:
  7819. case R_MIPS_26:
  7820. case R_MICROMIPS_26_S1:
  7821. howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
  7822. /* An error for unsupported relocations is raised as part
  7823. of the above search, so we can skip the following. */
  7824. if (howto != NULL)
  7825. info->callbacks->einfo
  7826. /* xgettext:c-format */
  7827. (_("%X%H: relocation %s against `%s' cannot be used"
  7828. " when making a shared object; recompile with -fPIC\n"),
  7829. abfd, sec, rel->r_offset, howto->name,
  7830. (h) ? h->root.root.string : "a local symbol");
  7831. break;
  7832. default:
  7833. break;
  7834. }
  7835. }
  7836. }
  7837. return true;
  7838. }
  7839. /* Allocate space for global sym dynamic relocs. */
  7840. static bool
  7841. allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  7842. {
  7843. struct bfd_link_info *info = inf;
  7844. bfd *dynobj;
  7845. struct mips_elf_link_hash_entry *hmips;
  7846. struct mips_elf_link_hash_table *htab;
  7847. htab = mips_elf_hash_table (info);
  7848. BFD_ASSERT (htab != NULL);
  7849. dynobj = elf_hash_table (info)->dynobj;
  7850. hmips = (struct mips_elf_link_hash_entry *) h;
  7851. /* VxWorks executables are handled elsewhere; we only need to
  7852. allocate relocations in shared objects. */
  7853. if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
  7854. return true;
  7855. /* Ignore indirect symbols. All relocations against such symbols
  7856. will be redirected to the target symbol. */
  7857. if (h->root.type == bfd_link_hash_indirect)
  7858. return true;
  7859. /* If this symbol is defined in a dynamic object, or we are creating
  7860. a shared library, we will need to copy any R_MIPS_32 or
  7861. R_MIPS_REL32 relocs against it into the output file. */
  7862. if (! bfd_link_relocatable (info)
  7863. && hmips->possibly_dynamic_relocs != 0
  7864. && (h->root.type == bfd_link_hash_defweak
  7865. || (!h->def_regular && !ELF_COMMON_DEF_P (h))
  7866. || bfd_link_pic (info)))
  7867. {
  7868. bool do_copy = true;
  7869. if (h->root.type == bfd_link_hash_undefweak)
  7870. {
  7871. /* Do not copy relocations for undefined weak symbols that
  7872. we are not going to export. */
  7873. if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  7874. do_copy = false;
  7875. /* Make sure undefined weak symbols are output as a dynamic
  7876. symbol in PIEs. */
  7877. else if (h->dynindx == -1 && !h->forced_local)
  7878. {
  7879. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  7880. return false;
  7881. }
  7882. }
  7883. if (do_copy)
  7884. {
  7885. /* Even though we don't directly need a GOT entry for this symbol,
  7886. the SVR4 psABI requires it to have a dynamic symbol table
  7887. index greater that DT_MIPS_GOTSYM if there are dynamic
  7888. relocations against it.
  7889. VxWorks does not enforce the same mapping between the GOT
  7890. and the symbol table, so the same requirement does not
  7891. apply there. */
  7892. if (htab->root.target_os != is_vxworks)
  7893. {
  7894. if (hmips->global_got_area > GGA_RELOC_ONLY)
  7895. hmips->global_got_area = GGA_RELOC_ONLY;
  7896. hmips->got_only_for_calls = false;
  7897. }
  7898. mips_elf_allocate_dynamic_relocations
  7899. (dynobj, info, hmips->possibly_dynamic_relocs);
  7900. if (hmips->readonly_reloc)
  7901. /* We tell the dynamic linker that there are relocations
  7902. against the text segment. */
  7903. info->flags |= DF_TEXTREL;
  7904. }
  7905. }
  7906. return true;
  7907. }
  7908. /* Adjust a symbol defined by a dynamic object and referenced by a
  7909. regular object. The current definition is in some section of the
  7910. dynamic object, but we're not including those sections. We have to
  7911. change the definition to something the rest of the link can
  7912. understand. */
  7913. bool
  7914. _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  7915. struct elf_link_hash_entry *h)
  7916. {
  7917. bfd *dynobj;
  7918. struct mips_elf_link_hash_entry *hmips;
  7919. struct mips_elf_link_hash_table *htab;
  7920. asection *s, *srel;
  7921. htab = mips_elf_hash_table (info);
  7922. BFD_ASSERT (htab != NULL);
  7923. dynobj = elf_hash_table (info)->dynobj;
  7924. hmips = (struct mips_elf_link_hash_entry *) h;
  7925. /* Make sure we know what is going on here. */
  7926. if (dynobj == NULL
  7927. || (! h->needs_plt
  7928. && ! h->is_weakalias
  7929. && (! h->def_dynamic
  7930. || ! h->ref_regular
  7931. || h->def_regular)))
  7932. {
  7933. if (h->type == STT_GNU_IFUNC)
  7934. _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
  7935. h->root.root.string);
  7936. else
  7937. _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
  7938. h->root.root.string);
  7939. return true;
  7940. }
  7941. hmips = (struct mips_elf_link_hash_entry *) h;
  7942. /* If there are call relocations against an externally-defined symbol,
  7943. see whether we can create a MIPS lazy-binding stub for it. We can
  7944. only do this if all references to the function are through call
  7945. relocations, and in that case, the traditional lazy-binding stubs
  7946. are much more efficient than PLT entries.
  7947. Traditional stubs are only available on SVR4 psABI-based systems;
  7948. VxWorks always uses PLTs instead. */
  7949. if (htab->root.target_os != is_vxworks
  7950. && h->needs_plt
  7951. && !hmips->no_fn_stub)
  7952. {
  7953. if (! elf_hash_table (info)->dynamic_sections_created)
  7954. return true;
  7955. /* If this symbol is not defined in a regular file, then set
  7956. the symbol to the stub location. This is required to make
  7957. function pointers compare as equal between the normal
  7958. executable and the shared library. */
  7959. if (!h->def_regular
  7960. && !bfd_is_abs_section (htab->sstubs->output_section))
  7961. {
  7962. hmips->needs_lazy_stub = true;
  7963. htab->lazy_stub_count++;
  7964. return true;
  7965. }
  7966. }
  7967. /* As above, VxWorks requires PLT entries for externally-defined
  7968. functions that are only accessed through call relocations.
  7969. Both VxWorks and non-VxWorks targets also need PLT entries if there
  7970. are static-only relocations against an externally-defined function.
  7971. This can technically occur for shared libraries if there are
  7972. branches to the symbol, although it is unlikely that this will be
  7973. used in practice due to the short ranges involved. It can occur
  7974. for any relative or absolute relocation in executables; in that
  7975. case, the PLT entry becomes the function's canonical address. */
  7976. else if (((h->needs_plt && !hmips->no_fn_stub)
  7977. || (h->type == STT_FUNC && hmips->has_static_relocs))
  7978. && htab->use_plts_and_copy_relocs
  7979. && !SYMBOL_CALLS_LOCAL (info, h)
  7980. && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  7981. && h->root.type == bfd_link_hash_undefweak))
  7982. {
  7983. bool micromips_p = MICROMIPS_P (info->output_bfd);
  7984. bool newabi_p = NEWABI_P (info->output_bfd);
  7985. /* If this is the first symbol to need a PLT entry, then make some
  7986. basic setup. Also work out PLT entry sizes. We'll need them
  7987. for PLT offset calculations. */
  7988. if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
  7989. {
  7990. BFD_ASSERT (htab->root.sgotplt->size == 0);
  7991. BFD_ASSERT (htab->plt_got_index == 0);
  7992. /* If we're using the PLT additions to the psABI, each PLT
  7993. entry is 16 bytes and the PLT0 entry is 32 bytes.
  7994. Encourage better cache usage by aligning. We do this
  7995. lazily to avoid pessimizing traditional objects. */
  7996. if (htab->root.target_os != is_vxworks
  7997. && !bfd_set_section_alignment (htab->root.splt, 5))
  7998. return false;
  7999. /* Make sure that .got.plt is word-aligned. We do this lazily
  8000. for the same reason as above. */
  8001. if (!bfd_set_section_alignment (htab->root.sgotplt,
  8002. MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
  8003. return false;
  8004. /* On non-VxWorks targets, the first two entries in .got.plt
  8005. are reserved. */
  8006. if (htab->root.target_os != is_vxworks)
  8007. htab->plt_got_index
  8008. += (get_elf_backend_data (dynobj)->got_header_size
  8009. / MIPS_ELF_GOT_SIZE (dynobj));
  8010. /* On VxWorks, also allocate room for the header's
  8011. .rela.plt.unloaded entries. */
  8012. if (htab->root.target_os == is_vxworks
  8013. && !bfd_link_pic (info))
  8014. htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
  8015. /* Now work out the sizes of individual PLT entries. */
  8016. if (htab->root.target_os == is_vxworks
  8017. && bfd_link_pic (info))
  8018. htab->plt_mips_entry_size
  8019. = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
  8020. else if (htab->root.target_os == is_vxworks)
  8021. htab->plt_mips_entry_size
  8022. = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
  8023. else if (newabi_p)
  8024. htab->plt_mips_entry_size
  8025. = 4 * ARRAY_SIZE (mips_exec_plt_entry);
  8026. else if (!micromips_p)
  8027. {
  8028. htab->plt_mips_entry_size
  8029. = 4 * ARRAY_SIZE (mips_exec_plt_entry);
  8030. htab->plt_comp_entry_size
  8031. = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
  8032. }
  8033. else if (htab->insn32)
  8034. {
  8035. htab->plt_mips_entry_size
  8036. = 4 * ARRAY_SIZE (mips_exec_plt_entry);
  8037. htab->plt_comp_entry_size
  8038. = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
  8039. }
  8040. else
  8041. {
  8042. htab->plt_mips_entry_size
  8043. = 4 * ARRAY_SIZE (mips_exec_plt_entry);
  8044. htab->plt_comp_entry_size
  8045. = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
  8046. }
  8047. }
  8048. if (h->plt.plist == NULL)
  8049. h->plt.plist = mips_elf_make_plt_record (dynobj);
  8050. if (h->plt.plist == NULL)
  8051. return false;
  8052. /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
  8053. n32 or n64, so always use a standard entry there.
  8054. If the symbol has a MIPS16 call stub and gets a PLT entry, then
  8055. all MIPS16 calls will go via that stub, and there is no benefit
  8056. to having a MIPS16 entry. And in the case of call_stub a
  8057. standard entry actually has to be used as the stub ends with a J
  8058. instruction. */
  8059. if (newabi_p
  8060. || htab->root.target_os == is_vxworks
  8061. || hmips->call_stub
  8062. || hmips->call_fp_stub)
  8063. {
  8064. h->plt.plist->need_mips = true;
  8065. h->plt.plist->need_comp = false;
  8066. }
  8067. /* Otherwise, if there are no direct calls to the function, we
  8068. have a free choice of whether to use standard or compressed
  8069. entries. Prefer microMIPS entries if the object is known to
  8070. contain microMIPS code, so that it becomes possible to create
  8071. pure microMIPS binaries. Prefer standard entries otherwise,
  8072. because MIPS16 ones are no smaller and are usually slower. */
  8073. if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
  8074. {
  8075. if (micromips_p)
  8076. h->plt.plist->need_comp = true;
  8077. else
  8078. h->plt.plist->need_mips = true;
  8079. }
  8080. if (h->plt.plist->need_mips)
  8081. {
  8082. h->plt.plist->mips_offset = htab->plt_mips_offset;
  8083. htab->plt_mips_offset += htab->plt_mips_entry_size;
  8084. }
  8085. if (h->plt.plist->need_comp)
  8086. {
  8087. h->plt.plist->comp_offset = htab->plt_comp_offset;
  8088. htab->plt_comp_offset += htab->plt_comp_entry_size;
  8089. }
  8090. /* Reserve the corresponding .got.plt entry now too. */
  8091. h->plt.plist->gotplt_index = htab->plt_got_index++;
  8092. /* If the output file has no definition of the symbol, set the
  8093. symbol's value to the address of the stub. */
  8094. if (!bfd_link_pic (info) && !h->def_regular)
  8095. hmips->use_plt_entry = true;
  8096. /* Make room for the R_MIPS_JUMP_SLOT relocation. */
  8097. htab->root.srelplt->size += (htab->root.target_os == is_vxworks
  8098. ? MIPS_ELF_RELA_SIZE (dynobj)
  8099. : MIPS_ELF_REL_SIZE (dynobj));
  8100. /* Make room for the .rela.plt.unloaded relocations. */
  8101. if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
  8102. htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
  8103. /* All relocations against this symbol that could have been made
  8104. dynamic will now refer to the PLT entry instead. */
  8105. hmips->possibly_dynamic_relocs = 0;
  8106. return true;
  8107. }
  8108. /* If this is a weak symbol, and there is a real definition, the
  8109. processor independent code will have arranged for us to see the
  8110. real definition first, and we can just use the same value. */
  8111. if (h->is_weakalias)
  8112. {
  8113. struct elf_link_hash_entry *def = weakdef (h);
  8114. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  8115. h->root.u.def.section = def->root.u.def.section;
  8116. h->root.u.def.value = def->root.u.def.value;
  8117. return true;
  8118. }
  8119. /* Otherwise, there is nothing further to do for symbols defined
  8120. in regular objects. */
  8121. if (h->def_regular)
  8122. return true;
  8123. /* There's also nothing more to do if we'll convert all relocations
  8124. against this symbol into dynamic relocations. */
  8125. if (!hmips->has_static_relocs)
  8126. return true;
  8127. /* We're now relying on copy relocations. Complain if we have
  8128. some that we can't convert. */
  8129. if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
  8130. {
  8131. _bfd_error_handler (_("non-dynamic relocations refer to "
  8132. "dynamic symbol %s"),
  8133. h->root.root.string);
  8134. bfd_set_error (bfd_error_bad_value);
  8135. return false;
  8136. }
  8137. /* We must allocate the symbol in our .dynbss section, which will
  8138. become part of the .bss section of the executable. There will be
  8139. an entry for this symbol in the .dynsym section. The dynamic
  8140. object will contain position independent code, so all references
  8141. from the dynamic object to this symbol will go through the global
  8142. offset table. The dynamic linker will use the .dynsym entry to
  8143. determine the address it must put in the global offset table, so
  8144. both the dynamic object and the regular object will refer to the
  8145. same memory location for the variable. */
  8146. if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  8147. {
  8148. s = htab->root.sdynrelro;
  8149. srel = htab->root.sreldynrelro;
  8150. }
  8151. else
  8152. {
  8153. s = htab->root.sdynbss;
  8154. srel = htab->root.srelbss;
  8155. }
  8156. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
  8157. {
  8158. if (htab->root.target_os == is_vxworks)
  8159. srel->size += sizeof (Elf32_External_Rela);
  8160. else
  8161. mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
  8162. h->needs_copy = 1;
  8163. }
  8164. /* All relocations against this symbol that could have been made
  8165. dynamic will now refer to the local copy instead. */
  8166. hmips->possibly_dynamic_relocs = 0;
  8167. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  8168. }
  8169. /* This function is called after all the input files have been read,
  8170. and the input sections have been assigned to output sections. We
  8171. check for any mips16 stub sections that we can discard. */
  8172. bool
  8173. _bfd_mips_elf_always_size_sections (bfd *output_bfd,
  8174. struct bfd_link_info *info)
  8175. {
  8176. asection *sect;
  8177. struct mips_elf_link_hash_table *htab;
  8178. struct mips_htab_traverse_info hti;
  8179. htab = mips_elf_hash_table (info);
  8180. BFD_ASSERT (htab != NULL);
  8181. /* The .reginfo section has a fixed size. */
  8182. sect = bfd_get_section_by_name (output_bfd, ".reginfo");
  8183. if (sect != NULL)
  8184. {
  8185. bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
  8186. sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
  8187. }
  8188. /* The .MIPS.abiflags section has a fixed size. */
  8189. sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
  8190. if (sect != NULL)
  8191. {
  8192. bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
  8193. sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
  8194. }
  8195. hti.info = info;
  8196. hti.output_bfd = output_bfd;
  8197. hti.error = false;
  8198. mips_elf_link_hash_traverse (mips_elf_hash_table (info),
  8199. mips_elf_check_symbols, &hti);
  8200. if (hti.error)
  8201. return false;
  8202. return true;
  8203. }
  8204. /* If the link uses a GOT, lay it out and work out its size. */
  8205. static bool
  8206. mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
  8207. {
  8208. bfd *dynobj;
  8209. asection *s;
  8210. struct mips_got_info *g;
  8211. bfd_size_type loadable_size = 0;
  8212. bfd_size_type page_gotno;
  8213. bfd *ibfd;
  8214. struct mips_elf_traverse_got_arg tga;
  8215. struct mips_elf_link_hash_table *htab;
  8216. htab = mips_elf_hash_table (info);
  8217. BFD_ASSERT (htab != NULL);
  8218. s = htab->root.sgot;
  8219. if (s == NULL)
  8220. return true;
  8221. dynobj = elf_hash_table (info)->dynobj;
  8222. g = htab->got_info;
  8223. /* Allocate room for the reserved entries. VxWorks always reserves
  8224. 3 entries; other objects only reserve 2 entries. */
  8225. BFD_ASSERT (g->assigned_low_gotno == 0);
  8226. if (htab->root.target_os == is_vxworks)
  8227. htab->reserved_gotno = 3;
  8228. else
  8229. htab->reserved_gotno = 2;
  8230. g->local_gotno += htab->reserved_gotno;
  8231. g->assigned_low_gotno = htab->reserved_gotno;
  8232. /* Decide which symbols need to go in the global part of the GOT and
  8233. count the number of reloc-only GOT symbols. */
  8234. mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
  8235. if (!mips_elf_resolve_final_got_entries (info, g))
  8236. return false;
  8237. /* Calculate the total loadable size of the output. That
  8238. will give us the maximum number of GOT_PAGE entries
  8239. required. */
  8240. for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
  8241. {
  8242. asection *subsection;
  8243. for (subsection = ibfd->sections;
  8244. subsection;
  8245. subsection = subsection->next)
  8246. {
  8247. if ((subsection->flags & SEC_ALLOC) == 0)
  8248. continue;
  8249. loadable_size += ((subsection->size + 0xf)
  8250. &~ (bfd_size_type) 0xf);
  8251. }
  8252. }
  8253. if (htab->root.target_os == is_vxworks)
  8254. /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
  8255. relocations against local symbols evaluate to "G", and the EABI does
  8256. not include R_MIPS_GOT_PAGE. */
  8257. page_gotno = 0;
  8258. else
  8259. /* Assume there are two loadable segments consisting of contiguous
  8260. sections. Is 5 enough? */
  8261. page_gotno = (loadable_size >> 16) + 5;
  8262. /* Choose the smaller of the two page estimates; both are intended to be
  8263. conservative. */
  8264. if (page_gotno > g->page_gotno)
  8265. page_gotno = g->page_gotno;
  8266. g->local_gotno += page_gotno;
  8267. g->assigned_high_gotno = g->local_gotno - 1;
  8268. s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
  8269. s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
  8270. s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
  8271. /* VxWorks does not support multiple GOTs. It initializes $gp to
  8272. __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
  8273. dynamic loader. */
  8274. if (htab->root.target_os != is_vxworks
  8275. && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
  8276. {
  8277. if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
  8278. return false;
  8279. }
  8280. else
  8281. {
  8282. /* Record that all bfds use G. This also has the effect of freeing
  8283. the per-bfd GOTs, which we no longer need. */
  8284. for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
  8285. if (mips_elf_bfd_got (ibfd, false))
  8286. mips_elf_replace_bfd_got (ibfd, g);
  8287. mips_elf_replace_bfd_got (output_bfd, g);
  8288. /* Set up TLS entries. */
  8289. g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
  8290. tga.info = info;
  8291. tga.g = g;
  8292. tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
  8293. htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
  8294. if (!tga.g)
  8295. return false;
  8296. BFD_ASSERT (g->tls_assigned_gotno
  8297. == g->global_gotno + g->local_gotno + g->tls_gotno);
  8298. /* Each VxWorks GOT entry needs an explicit relocation. */
  8299. if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
  8300. g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
  8301. /* Allocate room for the TLS relocations. */
  8302. if (g->relocs)
  8303. mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
  8304. }
  8305. return true;
  8306. }
  8307. /* Estimate the size of the .MIPS.stubs section. */
  8308. static void
  8309. mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
  8310. {
  8311. struct mips_elf_link_hash_table *htab;
  8312. bfd_size_type dynsymcount;
  8313. htab = mips_elf_hash_table (info);
  8314. BFD_ASSERT (htab != NULL);
  8315. if (htab->lazy_stub_count == 0)
  8316. return;
  8317. /* IRIX rld assumes that a function stub isn't at the end of the .text
  8318. section, so add a dummy entry to the end. */
  8319. htab->lazy_stub_count++;
  8320. /* Get a worst-case estimate of the number of dynamic symbols needed.
  8321. At this point, dynsymcount does not account for section symbols
  8322. and count_section_dynsyms may overestimate the number that will
  8323. be needed. */
  8324. dynsymcount = (elf_hash_table (info)->dynsymcount
  8325. + count_section_dynsyms (output_bfd, info));
  8326. /* Determine the size of one stub entry. There's no disadvantage
  8327. from using microMIPS code here, so for the sake of pure-microMIPS
  8328. binaries we prefer it whenever there's any microMIPS code in
  8329. output produced at all. This has a benefit of stubs being
  8330. shorter by 4 bytes each too, unless in the insn32 mode. */
  8331. if (!MICROMIPS_P (output_bfd))
  8332. htab->function_stub_size = (dynsymcount > 0x10000
  8333. ? MIPS_FUNCTION_STUB_BIG_SIZE
  8334. : MIPS_FUNCTION_STUB_NORMAL_SIZE);
  8335. else if (htab->insn32)
  8336. htab->function_stub_size = (dynsymcount > 0x10000
  8337. ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
  8338. : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
  8339. else
  8340. htab->function_stub_size = (dynsymcount > 0x10000
  8341. ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
  8342. : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
  8343. htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
  8344. }
  8345. /* A mips_elf_link_hash_traverse callback for which DATA points to a
  8346. mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
  8347. stub, allocate an entry in the stubs section. */
  8348. static bool
  8349. mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
  8350. {
  8351. struct mips_htab_traverse_info *hti = data;
  8352. struct mips_elf_link_hash_table *htab;
  8353. struct bfd_link_info *info;
  8354. bfd *output_bfd;
  8355. info = hti->info;
  8356. output_bfd = hti->output_bfd;
  8357. htab = mips_elf_hash_table (info);
  8358. BFD_ASSERT (htab != NULL);
  8359. if (h->needs_lazy_stub)
  8360. {
  8361. bool micromips_p = MICROMIPS_P (output_bfd);
  8362. unsigned int other = micromips_p ? STO_MICROMIPS : 0;
  8363. bfd_vma isa_bit = micromips_p;
  8364. BFD_ASSERT (htab->root.dynobj != NULL);
  8365. if (h->root.plt.plist == NULL)
  8366. h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
  8367. if (h->root.plt.plist == NULL)
  8368. {
  8369. hti->error = true;
  8370. return false;
  8371. }
  8372. h->root.root.u.def.section = htab->sstubs;
  8373. h->root.root.u.def.value = htab->sstubs->size + isa_bit;
  8374. h->root.plt.plist->stub_offset = htab->sstubs->size;
  8375. h->root.other = other;
  8376. htab->sstubs->size += htab->function_stub_size;
  8377. }
  8378. return true;
  8379. }
  8380. /* Allocate offsets in the stubs section to each symbol that needs one.
  8381. Set the final size of the .MIPS.stub section. */
  8382. static bool
  8383. mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
  8384. {
  8385. bfd *output_bfd = info->output_bfd;
  8386. bool micromips_p = MICROMIPS_P (output_bfd);
  8387. unsigned int other = micromips_p ? STO_MICROMIPS : 0;
  8388. bfd_vma isa_bit = micromips_p;
  8389. struct mips_elf_link_hash_table *htab;
  8390. struct mips_htab_traverse_info hti;
  8391. struct elf_link_hash_entry *h;
  8392. bfd *dynobj;
  8393. htab = mips_elf_hash_table (info);
  8394. BFD_ASSERT (htab != NULL);
  8395. if (htab->lazy_stub_count == 0)
  8396. return true;
  8397. htab->sstubs->size = 0;
  8398. hti.info = info;
  8399. hti.output_bfd = output_bfd;
  8400. hti.error = false;
  8401. mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
  8402. if (hti.error)
  8403. return false;
  8404. htab->sstubs->size += htab->function_stub_size;
  8405. BFD_ASSERT (htab->sstubs->size
  8406. == htab->lazy_stub_count * htab->function_stub_size);
  8407. dynobj = elf_hash_table (info)->dynobj;
  8408. BFD_ASSERT (dynobj != NULL);
  8409. h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
  8410. if (h == NULL)
  8411. return false;
  8412. h->root.u.def.value = isa_bit;
  8413. h->other = other;
  8414. h->type = STT_FUNC;
  8415. return true;
  8416. }
  8417. /* A mips_elf_link_hash_traverse callback for which DATA points to a
  8418. bfd_link_info. If H uses the address of a PLT entry as the value
  8419. of the symbol, then set the entry in the symbol table now. Prefer
  8420. a standard MIPS PLT entry. */
  8421. static bool
  8422. mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
  8423. {
  8424. struct bfd_link_info *info = data;
  8425. bool micromips_p = MICROMIPS_P (info->output_bfd);
  8426. struct mips_elf_link_hash_table *htab;
  8427. unsigned int other;
  8428. bfd_vma isa_bit;
  8429. bfd_vma val;
  8430. htab = mips_elf_hash_table (info);
  8431. BFD_ASSERT (htab != NULL);
  8432. if (h->use_plt_entry)
  8433. {
  8434. BFD_ASSERT (h->root.plt.plist != NULL);
  8435. BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
  8436. || h->root.plt.plist->comp_offset != MINUS_ONE);
  8437. val = htab->plt_header_size;
  8438. if (h->root.plt.plist->mips_offset != MINUS_ONE)
  8439. {
  8440. isa_bit = 0;
  8441. val += h->root.plt.plist->mips_offset;
  8442. other = 0;
  8443. }
  8444. else
  8445. {
  8446. isa_bit = 1;
  8447. val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
  8448. other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
  8449. }
  8450. val += isa_bit;
  8451. /* For VxWorks, point at the PLT load stub rather than the lazy
  8452. resolution stub; this stub will become the canonical function
  8453. address. */
  8454. if (htab->root.target_os == is_vxworks)
  8455. val += 8;
  8456. h->root.root.u.def.section = htab->root.splt;
  8457. h->root.root.u.def.value = val;
  8458. h->root.other = other;
  8459. }
  8460. return true;
  8461. }
  8462. /* Set the sizes of the dynamic sections. */
  8463. bool
  8464. _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
  8465. struct bfd_link_info *info)
  8466. {
  8467. bfd *dynobj;
  8468. asection *s, *sreldyn;
  8469. bool reltext;
  8470. struct mips_elf_link_hash_table *htab;
  8471. htab = mips_elf_hash_table (info);
  8472. BFD_ASSERT (htab != NULL);
  8473. dynobj = elf_hash_table (info)->dynobj;
  8474. BFD_ASSERT (dynobj != NULL);
  8475. if (elf_hash_table (info)->dynamic_sections_created)
  8476. {
  8477. /* Set the contents of the .interp section to the interpreter. */
  8478. if (bfd_link_executable (info) && !info->nointerp)
  8479. {
  8480. s = bfd_get_linker_section (dynobj, ".interp");
  8481. BFD_ASSERT (s != NULL);
  8482. s->size
  8483. = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
  8484. s->contents
  8485. = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
  8486. }
  8487. /* Figure out the size of the PLT header if we know that we
  8488. are using it. For the sake of cache alignment always use
  8489. a standard header whenever any standard entries are present
  8490. even if microMIPS entries are present as well. This also
  8491. lets the microMIPS header rely on the value of $v0 only set
  8492. by microMIPS entries, for a small size reduction.
  8493. Set symbol table entry values for symbols that use the
  8494. address of their PLT entry now that we can calculate it.
  8495. Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
  8496. haven't already in _bfd_elf_create_dynamic_sections. */
  8497. if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
  8498. {
  8499. bool micromips_p = (MICROMIPS_P (output_bfd)
  8500. && !htab->plt_mips_offset);
  8501. unsigned int other = micromips_p ? STO_MICROMIPS : 0;
  8502. bfd_vma isa_bit = micromips_p;
  8503. struct elf_link_hash_entry *h;
  8504. bfd_vma size;
  8505. BFD_ASSERT (htab->use_plts_and_copy_relocs);
  8506. BFD_ASSERT (htab->root.sgotplt->size == 0);
  8507. BFD_ASSERT (htab->root.splt->size == 0);
  8508. if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
  8509. size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
  8510. else if (htab->root.target_os == is_vxworks)
  8511. size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
  8512. else if (ABI_64_P (output_bfd))
  8513. size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
  8514. else if (ABI_N32_P (output_bfd))
  8515. size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
  8516. else if (!micromips_p)
  8517. size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
  8518. else if (htab->insn32)
  8519. size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
  8520. else
  8521. size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
  8522. htab->plt_header_is_comp = micromips_p;
  8523. htab->plt_header_size = size;
  8524. htab->root.splt->size = (size
  8525. + htab->plt_mips_offset
  8526. + htab->plt_comp_offset);
  8527. htab->root.sgotplt->size = (htab->plt_got_index
  8528. * MIPS_ELF_GOT_SIZE (dynobj));
  8529. mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
  8530. if (htab->root.hplt == NULL)
  8531. {
  8532. h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
  8533. "_PROCEDURE_LINKAGE_TABLE_");
  8534. htab->root.hplt = h;
  8535. if (h == NULL)
  8536. return false;
  8537. }
  8538. h = htab->root.hplt;
  8539. h->root.u.def.value = isa_bit;
  8540. h->other = other;
  8541. h->type = STT_FUNC;
  8542. }
  8543. }
  8544. /* Allocate space for global sym dynamic relocs. */
  8545. elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
  8546. mips_elf_estimate_stub_size (output_bfd, info);
  8547. if (!mips_elf_lay_out_got (output_bfd, info))
  8548. return false;
  8549. mips_elf_lay_out_lazy_stubs (info);
  8550. /* The check_relocs and adjust_dynamic_symbol entry points have
  8551. determined the sizes of the various dynamic sections. Allocate
  8552. memory for them. */
  8553. reltext = false;
  8554. for (s = dynobj->sections; s != NULL; s = s->next)
  8555. {
  8556. const char *name;
  8557. /* It's OK to base decisions on the section name, because none
  8558. of the dynobj section names depend upon the input files. */
  8559. name = bfd_section_name (s);
  8560. if ((s->flags & SEC_LINKER_CREATED) == 0)
  8561. continue;
  8562. if (startswith (name, ".rel"))
  8563. {
  8564. if (s->size != 0)
  8565. {
  8566. const char *outname;
  8567. asection *target;
  8568. /* If this relocation section applies to a read only
  8569. section, then we probably need a DT_TEXTREL entry.
  8570. If the relocation section is .rel(a).dyn, we always
  8571. assert a DT_TEXTREL entry rather than testing whether
  8572. there exists a relocation to a read only section or
  8573. not. */
  8574. outname = bfd_section_name (s->output_section);
  8575. target = bfd_get_section_by_name (output_bfd, outname + 4);
  8576. if ((target != NULL
  8577. && (target->flags & SEC_READONLY) != 0
  8578. && (target->flags & SEC_ALLOC) != 0)
  8579. || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
  8580. reltext = true;
  8581. /* We use the reloc_count field as a counter if we need
  8582. to copy relocs into the output file. */
  8583. if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
  8584. s->reloc_count = 0;
  8585. /* If combreloc is enabled, elf_link_sort_relocs() will
  8586. sort relocations, but in a different way than we do,
  8587. and before we're done creating relocations. Also, it
  8588. will move them around between input sections'
  8589. relocation's contents, so our sorting would be
  8590. broken, so don't let it run. */
  8591. info->combreloc = 0;
  8592. }
  8593. }
  8594. else if (bfd_link_executable (info)
  8595. && ! mips_elf_hash_table (info)->use_rld_obj_head
  8596. && startswith (name, ".rld_map"))
  8597. {
  8598. /* We add a room for __rld_map. It will be filled in by the
  8599. rtld to contain a pointer to the _r_debug structure. */
  8600. s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
  8601. }
  8602. else if (SGI_COMPAT (output_bfd)
  8603. && startswith (name, ".compact_rel"))
  8604. s->size += mips_elf_hash_table (info)->compact_rel_size;
  8605. else if (s == htab->root.splt)
  8606. {
  8607. /* If the last PLT entry has a branch delay slot, allocate
  8608. room for an extra nop to fill the delay slot. This is
  8609. for CPUs without load interlocking. */
  8610. if (! LOAD_INTERLOCKS_P (output_bfd)
  8611. && htab->root.target_os != is_vxworks
  8612. && s->size > 0)
  8613. s->size += 4;
  8614. }
  8615. else if (! startswith (name, ".init")
  8616. && s != htab->root.sgot
  8617. && s != htab->root.sgotplt
  8618. && s != htab->sstubs
  8619. && s != htab->root.sdynbss
  8620. && s != htab->root.sdynrelro)
  8621. {
  8622. /* It's not one of our sections, so don't allocate space. */
  8623. continue;
  8624. }
  8625. if (s->size == 0)
  8626. {
  8627. s->flags |= SEC_EXCLUDE;
  8628. continue;
  8629. }
  8630. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  8631. continue;
  8632. /* Allocate memory for the section contents. */
  8633. s->contents = bfd_zalloc (dynobj, s->size);
  8634. if (s->contents == NULL)
  8635. {
  8636. bfd_set_error (bfd_error_no_memory);
  8637. return false;
  8638. }
  8639. }
  8640. if (elf_hash_table (info)->dynamic_sections_created)
  8641. {
  8642. /* Add some entries to the .dynamic section. We fill in the
  8643. values later, in _bfd_mips_elf_finish_dynamic_sections, but we
  8644. must add the entries now so that we get the correct size for
  8645. the .dynamic section. */
  8646. /* SGI object has the equivalence of DT_DEBUG in the
  8647. DT_MIPS_RLD_MAP entry. This must come first because glibc
  8648. only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
  8649. may only look at the first one they see. */
  8650. if (!bfd_link_pic (info)
  8651. && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
  8652. return false;
  8653. if (bfd_link_executable (info)
  8654. && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
  8655. return false;
  8656. /* The DT_DEBUG entry may be filled in by the dynamic linker and
  8657. used by the debugger. */
  8658. if (bfd_link_executable (info)
  8659. && !SGI_COMPAT (output_bfd)
  8660. && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
  8661. return false;
  8662. if (reltext
  8663. && (SGI_COMPAT (output_bfd)
  8664. || htab->root.target_os == is_vxworks))
  8665. info->flags |= DF_TEXTREL;
  8666. if ((info->flags & DF_TEXTREL) != 0)
  8667. {
  8668. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
  8669. return false;
  8670. /* Clear the DF_TEXTREL flag. It will be set again if we
  8671. write out an actual text relocation; we may not, because
  8672. at this point we do not know whether e.g. any .eh_frame
  8673. absolute relocations have been converted to PC-relative. */
  8674. info->flags &= ~DF_TEXTREL;
  8675. }
  8676. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
  8677. return false;
  8678. sreldyn = mips_elf_rel_dyn_section (info, false);
  8679. if (htab->root.target_os == is_vxworks)
  8680. {
  8681. /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
  8682. use any of the DT_MIPS_* tags. */
  8683. if (sreldyn && sreldyn->size > 0)
  8684. {
  8685. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
  8686. return false;
  8687. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
  8688. return false;
  8689. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
  8690. return false;
  8691. }
  8692. }
  8693. else
  8694. {
  8695. if (sreldyn && sreldyn->size > 0
  8696. && !bfd_is_abs_section (sreldyn->output_section))
  8697. {
  8698. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
  8699. return false;
  8700. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
  8701. return false;
  8702. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
  8703. return false;
  8704. }
  8705. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
  8706. return false;
  8707. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
  8708. return false;
  8709. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
  8710. return false;
  8711. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
  8712. return false;
  8713. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
  8714. return false;
  8715. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
  8716. return false;
  8717. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
  8718. return false;
  8719. if (info->emit_gnu_hash
  8720. && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
  8721. return false;
  8722. if (IRIX_COMPAT (dynobj) == ict_irix5
  8723. && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
  8724. return false;
  8725. if (IRIX_COMPAT (dynobj) == ict_irix6
  8726. && (bfd_get_section_by_name
  8727. (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
  8728. && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
  8729. return false;
  8730. }
  8731. if (htab->root.splt->size > 0)
  8732. {
  8733. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
  8734. return false;
  8735. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
  8736. return false;
  8737. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
  8738. return false;
  8739. if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
  8740. return false;
  8741. }
  8742. if (htab->root.target_os == is_vxworks
  8743. && !elf_vxworks_add_dynamic_entries (output_bfd, info))
  8744. return false;
  8745. }
  8746. return true;
  8747. }
  8748. /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
  8749. Adjust its R_ADDEND field so that it is correct for the output file.
  8750. LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
  8751. and sections respectively; both use symbol indexes. */
  8752. static void
  8753. mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
  8754. bfd *input_bfd, Elf_Internal_Sym *local_syms,
  8755. asection **local_sections, Elf_Internal_Rela *rel)
  8756. {
  8757. unsigned int r_type, r_symndx;
  8758. Elf_Internal_Sym *sym;
  8759. asection *sec;
  8760. if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
  8761. {
  8762. r_type = ELF_R_TYPE (output_bfd, rel->r_info);
  8763. if (gprel16_reloc_p (r_type)
  8764. || r_type == R_MIPS_GPREL32
  8765. || literal_reloc_p (r_type))
  8766. {
  8767. rel->r_addend += _bfd_get_gp_value (input_bfd);
  8768. rel->r_addend -= _bfd_get_gp_value (output_bfd);
  8769. }
  8770. r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
  8771. sym = local_syms + r_symndx;
  8772. /* Adjust REL's addend to account for section merging. */
  8773. if (!bfd_link_relocatable (info))
  8774. {
  8775. sec = local_sections[r_symndx];
  8776. _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  8777. }
  8778. /* This would normally be done by the rela_normal code in elflink.c. */
  8779. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  8780. rel->r_addend += local_sections[r_symndx]->output_offset;
  8781. }
  8782. }
  8783. /* Handle relocations against symbols from removed linkonce sections,
  8784. or sections discarded by a linker script. We use this wrapper around
  8785. RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
  8786. on 64-bit ELF targets. In this case for any relocation handled, which
  8787. always be the first in a triplet, the remaining two have to be processed
  8788. together with the first, even if they are R_MIPS_NONE. It is the symbol
  8789. index referred by the first reloc that applies to all the three and the
  8790. remaining two never refer to an object symbol. And it is the final
  8791. relocation (the last non-null one) that determines the output field of
  8792. the whole relocation so retrieve the corresponding howto structure for
  8793. the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
  8794. Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
  8795. and therefore requires to be pasted in a loop. It also defines a block
  8796. and does not protect any of its arguments, hence the extra brackets. */
  8797. static void
  8798. mips_reloc_against_discarded_section (bfd *output_bfd,
  8799. struct bfd_link_info *info,
  8800. bfd *input_bfd, asection *input_section,
  8801. Elf_Internal_Rela **rel,
  8802. const Elf_Internal_Rela **relend,
  8803. bool rel_reloc,
  8804. reloc_howto_type *howto,
  8805. bfd_byte *contents)
  8806. {
  8807. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  8808. int count = bed->s->int_rels_per_ext_rel;
  8809. unsigned int r_type;
  8810. int i;
  8811. for (i = count - 1; i > 0; i--)
  8812. {
  8813. r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
  8814. if (r_type != R_MIPS_NONE)
  8815. {
  8816. howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
  8817. break;
  8818. }
  8819. }
  8820. do
  8821. {
  8822. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  8823. (*rel), count, (*relend),
  8824. howto, i, contents);
  8825. }
  8826. while (0);
  8827. }
  8828. /* Relocate a MIPS ELF section. */
  8829. int
  8830. _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
  8831. bfd *input_bfd, asection *input_section,
  8832. bfd_byte *contents, Elf_Internal_Rela *relocs,
  8833. Elf_Internal_Sym *local_syms,
  8834. asection **local_sections)
  8835. {
  8836. Elf_Internal_Rela *rel;
  8837. const Elf_Internal_Rela *relend;
  8838. bfd_vma addend = 0;
  8839. bool use_saved_addend_p = false;
  8840. relend = relocs + input_section->reloc_count;
  8841. for (rel = relocs; rel < relend; ++rel)
  8842. {
  8843. const char *name;
  8844. bfd_vma value = 0;
  8845. reloc_howto_type *howto;
  8846. bool cross_mode_jump_p = false;
  8847. /* TRUE if the relocation is a RELA relocation, rather than a
  8848. REL relocation. */
  8849. bool rela_relocation_p = true;
  8850. unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
  8851. const char *msg;
  8852. unsigned long r_symndx;
  8853. asection *sec;
  8854. Elf_Internal_Shdr *symtab_hdr;
  8855. struct elf_link_hash_entry *h;
  8856. bool rel_reloc;
  8857. rel_reloc = (NEWABI_P (input_bfd)
  8858. && mips_elf_rel_relocation_p (input_bfd, input_section,
  8859. relocs, rel));
  8860. /* Find the relocation howto for this relocation. */
  8861. howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
  8862. r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
  8863. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  8864. if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
  8865. {
  8866. sec = local_sections[r_symndx];
  8867. h = NULL;
  8868. }
  8869. else
  8870. {
  8871. unsigned long extsymoff;
  8872. extsymoff = 0;
  8873. if (!elf_bad_symtab (input_bfd))
  8874. extsymoff = symtab_hdr->sh_info;
  8875. h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
  8876. while (h->root.type == bfd_link_hash_indirect
  8877. || h->root.type == bfd_link_hash_warning)
  8878. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  8879. sec = NULL;
  8880. if (h->root.type == bfd_link_hash_defined
  8881. || h->root.type == bfd_link_hash_defweak)
  8882. sec = h->root.u.def.section;
  8883. }
  8884. if (sec != NULL && discarded_section (sec))
  8885. {
  8886. mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
  8887. input_section, &rel, &relend,
  8888. rel_reloc, howto, contents);
  8889. continue;
  8890. }
  8891. if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
  8892. {
  8893. /* Some 32-bit code uses R_MIPS_64. In particular, people use
  8894. 64-bit code, but make sure all their addresses are in the
  8895. lowermost or uppermost 32-bit section of the 64-bit address
  8896. space. Thus, when they use an R_MIPS_64 they mean what is
  8897. usually meant by R_MIPS_32, with the exception that the
  8898. stored value is sign-extended to 64 bits. */
  8899. howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
  8900. /* On big-endian systems, we need to lie about the position
  8901. of the reloc. */
  8902. if (bfd_big_endian (input_bfd))
  8903. rel->r_offset += 4;
  8904. }
  8905. if (!use_saved_addend_p)
  8906. {
  8907. /* If these relocations were originally of the REL variety,
  8908. we must pull the addend out of the field that will be
  8909. relocated. Otherwise, we simply use the contents of the
  8910. RELA relocation. */
  8911. if (mips_elf_rel_relocation_p (input_bfd, input_section,
  8912. relocs, rel))
  8913. {
  8914. rela_relocation_p = false;
  8915. addend = mips_elf_read_rel_addend (input_bfd, rel,
  8916. howto, contents);
  8917. if (hi16_reloc_p (r_type)
  8918. || (got16_reloc_p (r_type)
  8919. && mips_elf_local_relocation_p (input_bfd, rel,
  8920. local_sections)))
  8921. {
  8922. if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
  8923. contents, &addend))
  8924. {
  8925. if (h)
  8926. name = h->root.root.string;
  8927. else
  8928. name = bfd_elf_sym_name (input_bfd, symtab_hdr,
  8929. local_syms + r_symndx,
  8930. sec);
  8931. _bfd_error_handler
  8932. /* xgettext:c-format */
  8933. (_("%pB: can't find matching LO16 reloc against `%s'"
  8934. " for %s at %#" PRIx64 " in section `%pA'"),
  8935. input_bfd, name,
  8936. howto->name, (uint64_t) rel->r_offset, input_section);
  8937. }
  8938. }
  8939. else
  8940. addend <<= howto->rightshift;
  8941. }
  8942. else
  8943. addend = rel->r_addend;
  8944. mips_elf_adjust_addend (output_bfd, info, input_bfd,
  8945. local_syms, local_sections, rel);
  8946. }
  8947. if (bfd_link_relocatable (info))
  8948. {
  8949. if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
  8950. && bfd_big_endian (input_bfd))
  8951. rel->r_offset -= 4;
  8952. if (!rela_relocation_p && rel->r_addend)
  8953. {
  8954. addend += rel->r_addend;
  8955. if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
  8956. addend = mips_elf_high (addend);
  8957. else if (r_type == R_MIPS_HIGHER)
  8958. addend = mips_elf_higher (addend);
  8959. else if (r_type == R_MIPS_HIGHEST)
  8960. addend = mips_elf_highest (addend);
  8961. else
  8962. addend >>= howto->rightshift;
  8963. /* We use the source mask, rather than the destination
  8964. mask because the place to which we are writing will be
  8965. source of the addend in the final link. */
  8966. addend &= howto->src_mask;
  8967. if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
  8968. /* See the comment above about using R_MIPS_64 in the 32-bit
  8969. ABI. Here, we need to update the addend. It would be
  8970. possible to get away with just using the R_MIPS_32 reloc
  8971. but for endianness. */
  8972. {
  8973. bfd_vma sign_bits;
  8974. bfd_vma low_bits;
  8975. bfd_vma high_bits;
  8976. if (addend & ((bfd_vma) 1 << 31))
  8977. #ifdef BFD64
  8978. sign_bits = ((bfd_vma) 1 << 32) - 1;
  8979. #else
  8980. sign_bits = -1;
  8981. #endif
  8982. else
  8983. sign_bits = 0;
  8984. /* If we don't know that we have a 64-bit type,
  8985. do two separate stores. */
  8986. if (bfd_big_endian (input_bfd))
  8987. {
  8988. /* Store the sign-bits (which are most significant)
  8989. first. */
  8990. low_bits = sign_bits;
  8991. high_bits = addend;
  8992. }
  8993. else
  8994. {
  8995. low_bits = addend;
  8996. high_bits = sign_bits;
  8997. }
  8998. bfd_put_32 (input_bfd, low_bits,
  8999. contents + rel->r_offset);
  9000. bfd_put_32 (input_bfd, high_bits,
  9001. contents + rel->r_offset + 4);
  9002. continue;
  9003. }
  9004. if (! mips_elf_perform_relocation (info, howto, rel, addend,
  9005. input_bfd, input_section,
  9006. contents, false))
  9007. return false;
  9008. }
  9009. /* Go on to the next relocation. */
  9010. continue;
  9011. }
  9012. /* In the N32 and 64-bit ABIs there may be multiple consecutive
  9013. relocations for the same offset. In that case we are
  9014. supposed to treat the output of each relocation as the addend
  9015. for the next. */
  9016. if (rel + 1 < relend
  9017. && rel->r_offset == rel[1].r_offset
  9018. && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
  9019. use_saved_addend_p = true;
  9020. else
  9021. use_saved_addend_p = false;
  9022. /* Figure out what value we are supposed to relocate. */
  9023. switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
  9024. input_section, contents,
  9025. info, rel, addend, howto,
  9026. local_syms, local_sections,
  9027. &value, &name, &cross_mode_jump_p,
  9028. use_saved_addend_p))
  9029. {
  9030. case bfd_reloc_continue:
  9031. /* There's nothing to do. */
  9032. continue;
  9033. case bfd_reloc_undefined:
  9034. /* mips_elf_calculate_relocation already called the
  9035. undefined_symbol callback. There's no real point in
  9036. trying to perform the relocation at this point, so we
  9037. just skip ahead to the next relocation. */
  9038. continue;
  9039. case bfd_reloc_notsupported:
  9040. msg = _("internal error: unsupported relocation error");
  9041. info->callbacks->warning
  9042. (info, msg, name, input_bfd, input_section, rel->r_offset);
  9043. return false;
  9044. case bfd_reloc_overflow:
  9045. if (use_saved_addend_p)
  9046. /* Ignore overflow until we reach the last relocation for
  9047. a given location. */
  9048. ;
  9049. else
  9050. {
  9051. struct mips_elf_link_hash_table *htab;
  9052. htab = mips_elf_hash_table (info);
  9053. BFD_ASSERT (htab != NULL);
  9054. BFD_ASSERT (name != NULL);
  9055. if (!htab->small_data_overflow_reported
  9056. && (gprel16_reloc_p (howto->type)
  9057. || literal_reloc_p (howto->type)))
  9058. {
  9059. msg = _("small-data section exceeds 64KB;"
  9060. " lower small-data size limit (see option -G)");
  9061. htab->small_data_overflow_reported = true;
  9062. (*info->callbacks->einfo) ("%P: %s\n", msg);
  9063. }
  9064. (*info->callbacks->reloc_overflow)
  9065. (info, NULL, name, howto->name, (bfd_vma) 0,
  9066. input_bfd, input_section, rel->r_offset);
  9067. }
  9068. break;
  9069. case bfd_reloc_ok:
  9070. break;
  9071. case bfd_reloc_outofrange:
  9072. msg = NULL;
  9073. if (jal_reloc_p (howto->type))
  9074. msg = (cross_mode_jump_p
  9075. ? _("cannot convert a jump to JALX "
  9076. "for a non-word-aligned address")
  9077. : (howto->type == R_MIPS16_26
  9078. ? _("jump to a non-word-aligned address")
  9079. : _("jump to a non-instruction-aligned address")));
  9080. else if (b_reloc_p (howto->type))
  9081. msg = (cross_mode_jump_p
  9082. ? _("cannot convert a branch to JALX "
  9083. "for a non-word-aligned address")
  9084. : _("branch to a non-instruction-aligned address"));
  9085. else if (aligned_pcrel_reloc_p (howto->type))
  9086. msg = _("PC-relative load from unaligned address");
  9087. if (msg)
  9088. {
  9089. info->callbacks->einfo
  9090. ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
  9091. break;
  9092. }
  9093. /* Fall through. */
  9094. default:
  9095. abort ();
  9096. break;
  9097. }
  9098. /* If we've got another relocation for the address, keep going
  9099. until we reach the last one. */
  9100. if (use_saved_addend_p)
  9101. {
  9102. addend = value;
  9103. continue;
  9104. }
  9105. if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
  9106. /* See the comment above about using R_MIPS_64 in the 32-bit
  9107. ABI. Until now, we've been using the HOWTO for R_MIPS_32;
  9108. that calculated the right value. Now, however, we
  9109. sign-extend the 32-bit result to 64-bits, and store it as a
  9110. 64-bit value. We are especially generous here in that we
  9111. go to extreme lengths to support this usage on systems with
  9112. only a 32-bit VMA. */
  9113. {
  9114. bfd_vma sign_bits;
  9115. bfd_vma low_bits;
  9116. bfd_vma high_bits;
  9117. if (value & ((bfd_vma) 1 << 31))
  9118. #ifdef BFD64
  9119. sign_bits = ((bfd_vma) 1 << 32) - 1;
  9120. #else
  9121. sign_bits = -1;
  9122. #endif
  9123. else
  9124. sign_bits = 0;
  9125. /* If we don't know that we have a 64-bit type,
  9126. do two separate stores. */
  9127. if (bfd_big_endian (input_bfd))
  9128. {
  9129. /* Undo what we did above. */
  9130. rel->r_offset -= 4;
  9131. /* Store the sign-bits (which are most significant)
  9132. first. */
  9133. low_bits = sign_bits;
  9134. high_bits = value;
  9135. }
  9136. else
  9137. {
  9138. low_bits = value;
  9139. high_bits = sign_bits;
  9140. }
  9141. bfd_put_32 (input_bfd, low_bits,
  9142. contents + rel->r_offset);
  9143. bfd_put_32 (input_bfd, high_bits,
  9144. contents + rel->r_offset + 4);
  9145. continue;
  9146. }
  9147. /* Actually perform the relocation. */
  9148. if (! mips_elf_perform_relocation (info, howto, rel, value,
  9149. input_bfd, input_section,
  9150. contents, cross_mode_jump_p))
  9151. return false;
  9152. }
  9153. return true;
  9154. }
  9155. /* A function that iterates over each entry in la25_stubs and fills
  9156. in the code for each one. DATA points to a mips_htab_traverse_info. */
  9157. static int
  9158. mips_elf_create_la25_stub (void **slot, void *data)
  9159. {
  9160. struct mips_htab_traverse_info *hti;
  9161. struct mips_elf_link_hash_table *htab;
  9162. struct mips_elf_la25_stub *stub;
  9163. asection *s;
  9164. bfd_byte *loc;
  9165. bfd_vma offset, target, target_high, target_low;
  9166. bfd_vma branch_pc;
  9167. bfd_signed_vma pcrel_offset = 0;
  9168. stub = (struct mips_elf_la25_stub *) *slot;
  9169. hti = (struct mips_htab_traverse_info *) data;
  9170. htab = mips_elf_hash_table (hti->info);
  9171. BFD_ASSERT (htab != NULL);
  9172. /* Create the section contents, if we haven't already. */
  9173. s = stub->stub_section;
  9174. loc = s->contents;
  9175. if (loc == NULL)
  9176. {
  9177. loc = bfd_malloc (s->size);
  9178. if (loc == NULL)
  9179. {
  9180. hti->error = true;
  9181. return false;
  9182. }
  9183. s->contents = loc;
  9184. }
  9185. /* Work out where in the section this stub should go. */
  9186. offset = stub->offset;
  9187. /* We add 8 here to account for the LUI/ADDIU instructions
  9188. before the branch instruction. This cannot be moved down to
  9189. where pcrel_offset is calculated as 's' is updated in
  9190. mips_elf_get_la25_target. */
  9191. branch_pc = s->output_section->vma + s->output_offset + offset + 8;
  9192. /* Work out the target address. */
  9193. target = mips_elf_get_la25_target (stub, &s);
  9194. target += s->output_section->vma + s->output_offset;
  9195. target_high = ((target + 0x8000) >> 16) & 0xffff;
  9196. target_low = (target & 0xffff);
  9197. /* Calculate the PC of the compact branch instruction (for the case where
  9198. compact branches are used for either microMIPSR6 or MIPSR6 with
  9199. compact branches. Add 4-bytes to account for BC using the PC of the
  9200. next instruction as the base. */
  9201. pcrel_offset = target - (branch_pc + 4);
  9202. if (stub->stub_section != htab->strampoline)
  9203. {
  9204. /* This is a simple LUI/ADDIU stub. Zero out the beginning
  9205. of the section and write the two instructions at the end. */
  9206. memset (loc, 0, offset);
  9207. loc += offset;
  9208. if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
  9209. {
  9210. bfd_put_micromips_32 (hti->output_bfd,
  9211. LA25_LUI_MICROMIPS (target_high),
  9212. loc);
  9213. bfd_put_micromips_32 (hti->output_bfd,
  9214. LA25_ADDIU_MICROMIPS (target_low),
  9215. loc + 4);
  9216. }
  9217. else
  9218. {
  9219. bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
  9220. bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
  9221. }
  9222. }
  9223. else
  9224. {
  9225. /* This is trampoline. */
  9226. loc += offset;
  9227. if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
  9228. {
  9229. bfd_put_micromips_32 (hti->output_bfd,
  9230. LA25_LUI_MICROMIPS (target_high), loc);
  9231. bfd_put_micromips_32 (hti->output_bfd,
  9232. LA25_J_MICROMIPS (target), loc + 4);
  9233. bfd_put_micromips_32 (hti->output_bfd,
  9234. LA25_ADDIU_MICROMIPS (target_low), loc + 8);
  9235. bfd_put_32 (hti->output_bfd, 0, loc + 12);
  9236. }
  9237. else
  9238. {
  9239. bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
  9240. if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
  9241. {
  9242. bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
  9243. bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
  9244. }
  9245. else
  9246. {
  9247. bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
  9248. bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
  9249. }
  9250. bfd_put_32 (hti->output_bfd, 0, loc + 12);
  9251. }
  9252. }
  9253. return true;
  9254. }
  9255. /* If NAME is one of the special IRIX6 symbols defined by the linker,
  9256. adjust it appropriately now. */
  9257. static void
  9258. mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
  9259. const char *name, Elf_Internal_Sym *sym)
  9260. {
  9261. /* The linker script takes care of providing names and values for
  9262. these, but we must place them into the right sections. */
  9263. static const char* const text_section_symbols[] = {
  9264. "_ftext",
  9265. "_etext",
  9266. "__dso_displacement",
  9267. "__elf_header",
  9268. "__program_header_table",
  9269. NULL
  9270. };
  9271. static const char* const data_section_symbols[] = {
  9272. "_fdata",
  9273. "_edata",
  9274. "_end",
  9275. "_fbss",
  9276. NULL
  9277. };
  9278. const char* const *p;
  9279. int i;
  9280. for (i = 0; i < 2; ++i)
  9281. for (p = (i == 0) ? text_section_symbols : data_section_symbols;
  9282. *p;
  9283. ++p)
  9284. if (strcmp (*p, name) == 0)
  9285. {
  9286. /* All of these symbols are given type STT_SECTION by the
  9287. IRIX6 linker. */
  9288. sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  9289. sym->st_other = STO_PROTECTED;
  9290. /* The IRIX linker puts these symbols in special sections. */
  9291. if (i == 0)
  9292. sym->st_shndx = SHN_MIPS_TEXT;
  9293. else
  9294. sym->st_shndx = SHN_MIPS_DATA;
  9295. break;
  9296. }
  9297. }
  9298. /* Finish up dynamic symbol handling. We set the contents of various
  9299. dynamic sections here. */
  9300. bool
  9301. _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
  9302. struct bfd_link_info *info,
  9303. struct elf_link_hash_entry *h,
  9304. Elf_Internal_Sym *sym)
  9305. {
  9306. bfd *dynobj;
  9307. asection *sgot;
  9308. struct mips_got_info *g, *gg;
  9309. const char *name;
  9310. int idx;
  9311. struct mips_elf_link_hash_table *htab;
  9312. struct mips_elf_link_hash_entry *hmips;
  9313. htab = mips_elf_hash_table (info);
  9314. BFD_ASSERT (htab != NULL);
  9315. dynobj = elf_hash_table (info)->dynobj;
  9316. hmips = (struct mips_elf_link_hash_entry *) h;
  9317. BFD_ASSERT (htab->root.target_os != is_vxworks);
  9318. if (h->plt.plist != NULL
  9319. && (h->plt.plist->mips_offset != MINUS_ONE
  9320. || h->plt.plist->comp_offset != MINUS_ONE))
  9321. {
  9322. /* We've decided to create a PLT entry for this symbol. */
  9323. bfd_byte *loc;
  9324. bfd_vma header_address, got_address;
  9325. bfd_vma got_address_high, got_address_low, load;
  9326. bfd_vma got_index;
  9327. bfd_vma isa_bit;
  9328. got_index = h->plt.plist->gotplt_index;
  9329. BFD_ASSERT (htab->use_plts_and_copy_relocs);
  9330. BFD_ASSERT (h->dynindx != -1);
  9331. BFD_ASSERT (htab->root.splt != NULL);
  9332. BFD_ASSERT (got_index != MINUS_ONE);
  9333. BFD_ASSERT (!h->def_regular);
  9334. /* Calculate the address of the PLT header. */
  9335. isa_bit = htab->plt_header_is_comp;
  9336. header_address = (htab->root.splt->output_section->vma
  9337. + htab->root.splt->output_offset + isa_bit);
  9338. /* Calculate the address of the .got.plt entry. */
  9339. got_address = (htab->root.sgotplt->output_section->vma
  9340. + htab->root.sgotplt->output_offset
  9341. + got_index * MIPS_ELF_GOT_SIZE (dynobj));
  9342. got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
  9343. got_address_low = got_address & 0xffff;
  9344. /* The PLT sequence is not safe for N64 if .got.plt entry's address
  9345. cannot be loaded in two instructions. */
  9346. if (ABI_64_P (output_bfd)
  9347. && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
  9348. {
  9349. _bfd_error_handler
  9350. /* xgettext:c-format */
  9351. (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
  9352. "supported; consider using `-Ttext-segment=...'"),
  9353. output_bfd,
  9354. htab->root.sgotplt->output_section,
  9355. (int64_t) got_address);
  9356. bfd_set_error (bfd_error_no_error);
  9357. return false;
  9358. }
  9359. /* Initially point the .got.plt entry at the PLT header. */
  9360. loc = (htab->root.sgotplt->contents
  9361. + got_index * MIPS_ELF_GOT_SIZE (dynobj));
  9362. if (ABI_64_P (output_bfd))
  9363. bfd_put_64 (output_bfd, header_address, loc);
  9364. else
  9365. bfd_put_32 (output_bfd, header_address, loc);
  9366. /* Now handle the PLT itself. First the standard entry (the order
  9367. does not matter, we just have to pick one). */
  9368. if (h->plt.plist->mips_offset != MINUS_ONE)
  9369. {
  9370. const bfd_vma *plt_entry;
  9371. bfd_vma plt_offset;
  9372. plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
  9373. BFD_ASSERT (plt_offset <= htab->root.splt->size);
  9374. /* Find out where the .plt entry should go. */
  9375. loc = htab->root.splt->contents + plt_offset;
  9376. /* Pick the load opcode. */
  9377. load = MIPS_ELF_LOAD_WORD (output_bfd);
  9378. /* Fill in the PLT entry itself. */
  9379. if (MIPSR6_P (output_bfd))
  9380. plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
  9381. : mipsr6_exec_plt_entry;
  9382. else
  9383. plt_entry = mips_exec_plt_entry;
  9384. bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
  9385. bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
  9386. loc + 4);
  9387. if (! LOAD_INTERLOCKS_P (output_bfd)
  9388. || (MIPSR6_P (output_bfd) && htab->compact_branches))
  9389. {
  9390. bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
  9391. bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
  9392. }
  9393. else
  9394. {
  9395. bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
  9396. bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
  9397. loc + 12);
  9398. }
  9399. }
  9400. /* Now the compressed entry. They come after any standard ones. */
  9401. if (h->plt.plist->comp_offset != MINUS_ONE)
  9402. {
  9403. bfd_vma plt_offset;
  9404. plt_offset = (htab->plt_header_size + htab->plt_mips_offset
  9405. + h->plt.plist->comp_offset);
  9406. BFD_ASSERT (plt_offset <= htab->root.splt->size);
  9407. /* Find out where the .plt entry should go. */
  9408. loc = htab->root.splt->contents + plt_offset;
  9409. /* Fill in the PLT entry itself. */
  9410. if (!MICROMIPS_P (output_bfd))
  9411. {
  9412. const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
  9413. bfd_put_16 (output_bfd, plt_entry[0], loc);
  9414. bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
  9415. bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
  9416. bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
  9417. bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
  9418. bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
  9419. bfd_put_32 (output_bfd, got_address, loc + 12);
  9420. }
  9421. else if (htab->insn32)
  9422. {
  9423. const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
  9424. bfd_put_16 (output_bfd, plt_entry[0], loc);
  9425. bfd_put_16 (output_bfd, got_address_high, loc + 2);
  9426. bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
  9427. bfd_put_16 (output_bfd, got_address_low, loc + 6);
  9428. bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
  9429. bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
  9430. bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
  9431. bfd_put_16 (output_bfd, got_address_low, loc + 14);
  9432. }
  9433. else
  9434. {
  9435. const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
  9436. bfd_signed_vma gotpc_offset;
  9437. bfd_vma loc_address;
  9438. BFD_ASSERT (got_address % 4 == 0);
  9439. loc_address = (htab->root.splt->output_section->vma
  9440. + htab->root.splt->output_offset + plt_offset);
  9441. gotpc_offset = got_address - ((loc_address | 3) ^ 3);
  9442. /* ADDIUPC has a span of +/-16MB, check we're in range. */
  9443. if (gotpc_offset + 0x1000000 >= 0x2000000)
  9444. {
  9445. _bfd_error_handler
  9446. /* xgettext:c-format */
  9447. (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
  9448. "beyond the range of ADDIUPC"),
  9449. output_bfd,
  9450. htab->root.sgotplt->output_section,
  9451. (int64_t) gotpc_offset,
  9452. htab->root.splt->output_section);
  9453. bfd_set_error (bfd_error_no_error);
  9454. return false;
  9455. }
  9456. bfd_put_16 (output_bfd,
  9457. plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
  9458. bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
  9459. bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
  9460. bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
  9461. bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
  9462. bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
  9463. }
  9464. }
  9465. /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
  9466. mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
  9467. got_index - 2, h->dynindx,
  9468. R_MIPS_JUMP_SLOT, got_address);
  9469. /* We distinguish between PLT entries and lazy-binding stubs by
  9470. giving the former an st_other value of STO_MIPS_PLT. Set the
  9471. flag and leave the value if there are any relocations in the
  9472. binary where pointer equality matters. */
  9473. sym->st_shndx = SHN_UNDEF;
  9474. if (h->pointer_equality_needed)
  9475. sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
  9476. else
  9477. {
  9478. sym->st_value = 0;
  9479. sym->st_other = 0;
  9480. }
  9481. }
  9482. if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
  9483. {
  9484. /* We've decided to create a lazy-binding stub. */
  9485. bool micromips_p = MICROMIPS_P (output_bfd);
  9486. unsigned int other = micromips_p ? STO_MICROMIPS : 0;
  9487. bfd_vma stub_size = htab->function_stub_size;
  9488. bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
  9489. bfd_vma isa_bit = micromips_p;
  9490. bfd_vma stub_big_size;
  9491. if (!micromips_p)
  9492. stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
  9493. else if (htab->insn32)
  9494. stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
  9495. else
  9496. stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
  9497. /* This symbol has a stub. Set it up. */
  9498. BFD_ASSERT (h->dynindx != -1);
  9499. BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
  9500. /* Values up to 2^31 - 1 are allowed. Larger values would cause
  9501. sign extension at runtime in the stub, resulting in a negative
  9502. index value. */
  9503. if (h->dynindx & ~0x7fffffff)
  9504. return false;
  9505. /* Fill the stub. */
  9506. if (micromips_p)
  9507. {
  9508. idx = 0;
  9509. bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
  9510. stub + idx);
  9511. idx += 4;
  9512. if (htab->insn32)
  9513. {
  9514. bfd_put_micromips_32 (output_bfd,
  9515. STUB_MOVE32_MICROMIPS, stub + idx);
  9516. idx += 4;
  9517. }
  9518. else
  9519. {
  9520. bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
  9521. idx += 2;
  9522. }
  9523. if (stub_size == stub_big_size)
  9524. {
  9525. long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
  9526. bfd_put_micromips_32 (output_bfd,
  9527. STUB_LUI_MICROMIPS (dynindx_hi),
  9528. stub + idx);
  9529. idx += 4;
  9530. }
  9531. if (htab->insn32)
  9532. {
  9533. bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
  9534. stub + idx);
  9535. idx += 4;
  9536. }
  9537. else
  9538. {
  9539. bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
  9540. idx += 2;
  9541. }
  9542. /* If a large stub is not required and sign extension is not a
  9543. problem, then use legacy code in the stub. */
  9544. if (stub_size == stub_big_size)
  9545. bfd_put_micromips_32 (output_bfd,
  9546. STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
  9547. stub + idx);
  9548. else if (h->dynindx & ~0x7fff)
  9549. bfd_put_micromips_32 (output_bfd,
  9550. STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
  9551. stub + idx);
  9552. else
  9553. bfd_put_micromips_32 (output_bfd,
  9554. STUB_LI16S_MICROMIPS (output_bfd,
  9555. h->dynindx),
  9556. stub + idx);
  9557. }
  9558. else
  9559. {
  9560. idx = 0;
  9561. bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
  9562. idx += 4;
  9563. bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
  9564. idx += 4;
  9565. if (stub_size == stub_big_size)
  9566. {
  9567. bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
  9568. stub + idx);
  9569. idx += 4;
  9570. }
  9571. if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
  9572. {
  9573. bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
  9574. idx += 4;
  9575. }
  9576. /* If a large stub is not required and sign extension is not a
  9577. problem, then use legacy code in the stub. */
  9578. if (stub_size == stub_big_size)
  9579. bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
  9580. stub + idx);
  9581. else if (h->dynindx & ~0x7fff)
  9582. bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
  9583. stub + idx);
  9584. else
  9585. bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
  9586. stub + idx);
  9587. idx += 4;
  9588. if (MIPSR6_P (output_bfd) && htab->compact_branches)
  9589. bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
  9590. }
  9591. BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
  9592. memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
  9593. stub, stub_size);
  9594. /* Mark the symbol as undefined. stub_offset != -1 occurs
  9595. only for the referenced symbol. */
  9596. sym->st_shndx = SHN_UNDEF;
  9597. /* The run-time linker uses the st_value field of the symbol
  9598. to reset the global offset table entry for this external
  9599. to its stub address when unlinking a shared object. */
  9600. sym->st_value = (htab->sstubs->output_section->vma
  9601. + htab->sstubs->output_offset
  9602. + h->plt.plist->stub_offset
  9603. + isa_bit);
  9604. sym->st_other = other;
  9605. }
  9606. /* If we have a MIPS16 function with a stub, the dynamic symbol must
  9607. refer to the stub, since only the stub uses the standard calling
  9608. conventions. */
  9609. if (h->dynindx != -1 && hmips->fn_stub != NULL)
  9610. {
  9611. BFD_ASSERT (hmips->need_fn_stub);
  9612. sym->st_value = (hmips->fn_stub->output_section->vma
  9613. + hmips->fn_stub->output_offset);
  9614. sym->st_size = hmips->fn_stub->size;
  9615. sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
  9616. }
  9617. BFD_ASSERT (h->dynindx != -1
  9618. || h->forced_local);
  9619. sgot = htab->root.sgot;
  9620. g = htab->got_info;
  9621. BFD_ASSERT (g != NULL);
  9622. /* Run through the global symbol table, creating GOT entries for all
  9623. the symbols that need them. */
  9624. if (hmips->global_got_area != GGA_NONE)
  9625. {
  9626. bfd_vma offset;
  9627. bfd_vma value;
  9628. value = sym->st_value;
  9629. offset = mips_elf_primary_global_got_index (output_bfd, info, h);
  9630. MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
  9631. }
  9632. if (hmips->global_got_area != GGA_NONE && g->next)
  9633. {
  9634. struct mips_got_entry e, *p;
  9635. bfd_vma entry;
  9636. bfd_vma offset;
  9637. gg = g;
  9638. e.abfd = output_bfd;
  9639. e.symndx = -1;
  9640. e.d.h = hmips;
  9641. e.tls_type = GOT_TLS_NONE;
  9642. for (g = g->next; g->next != gg; g = g->next)
  9643. {
  9644. if (g->got_entries
  9645. && (p = (struct mips_got_entry *) htab_find (g->got_entries,
  9646. &e)))
  9647. {
  9648. offset = p->gotidx;
  9649. BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
  9650. if (bfd_link_pic (info)
  9651. || (elf_hash_table (info)->dynamic_sections_created
  9652. && p->d.h != NULL
  9653. && p->d.h->root.def_dynamic
  9654. && !p->d.h->root.def_regular))
  9655. {
  9656. /* Create an R_MIPS_REL32 relocation for this entry. Due to
  9657. the various compatibility problems, it's easier to mock
  9658. up an R_MIPS_32 or R_MIPS_64 relocation and leave
  9659. mips_elf_create_dynamic_relocation to calculate the
  9660. appropriate addend. */
  9661. Elf_Internal_Rela rel[3];
  9662. memset (rel, 0, sizeof (rel));
  9663. if (ABI_64_P (output_bfd))
  9664. rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
  9665. else
  9666. rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
  9667. rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
  9668. entry = 0;
  9669. if (! (mips_elf_create_dynamic_relocation
  9670. (output_bfd, info, rel,
  9671. e.d.h, NULL, sym->st_value, &entry, sgot)))
  9672. return false;
  9673. }
  9674. else
  9675. entry = sym->st_value;
  9676. MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
  9677. }
  9678. }
  9679. }
  9680. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  9681. name = h->root.root.string;
  9682. if (h == elf_hash_table (info)->hdynamic
  9683. || h == elf_hash_table (info)->hgot)
  9684. sym->st_shndx = SHN_ABS;
  9685. else if (strcmp (name, "_DYNAMIC_LINK") == 0
  9686. || strcmp (name, "_DYNAMIC_LINKING") == 0)
  9687. {
  9688. sym->st_shndx = SHN_ABS;
  9689. sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  9690. sym->st_value = 1;
  9691. }
  9692. else if (SGI_COMPAT (output_bfd))
  9693. {
  9694. if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
  9695. || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
  9696. {
  9697. sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  9698. sym->st_other = STO_PROTECTED;
  9699. sym->st_value = 0;
  9700. sym->st_shndx = SHN_MIPS_DATA;
  9701. }
  9702. else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
  9703. {
  9704. sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  9705. sym->st_other = STO_PROTECTED;
  9706. sym->st_value = mips_elf_hash_table (info)->procedure_count;
  9707. sym->st_shndx = SHN_ABS;
  9708. }
  9709. else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
  9710. {
  9711. if (h->type == STT_FUNC)
  9712. sym->st_shndx = SHN_MIPS_TEXT;
  9713. else if (h->type == STT_OBJECT)
  9714. sym->st_shndx = SHN_MIPS_DATA;
  9715. }
  9716. }
  9717. /* Emit a copy reloc, if needed. */
  9718. if (h->needs_copy)
  9719. {
  9720. asection *s;
  9721. bfd_vma symval;
  9722. BFD_ASSERT (h->dynindx != -1);
  9723. BFD_ASSERT (htab->use_plts_and_copy_relocs);
  9724. s = mips_elf_rel_dyn_section (info, false);
  9725. symval = (h->root.u.def.section->output_section->vma
  9726. + h->root.u.def.section->output_offset
  9727. + h->root.u.def.value);
  9728. mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
  9729. h->dynindx, R_MIPS_COPY, symval);
  9730. }
  9731. /* Handle the IRIX6-specific symbols. */
  9732. if (IRIX_COMPAT (output_bfd) == ict_irix6)
  9733. mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
  9734. /* Keep dynamic compressed symbols odd. This allows the dynamic linker
  9735. to treat compressed symbols like any other. */
  9736. if (ELF_ST_IS_MIPS16 (sym->st_other))
  9737. {
  9738. BFD_ASSERT (sym->st_value & 1);
  9739. sym->st_other -= STO_MIPS16;
  9740. }
  9741. else if (ELF_ST_IS_MICROMIPS (sym->st_other))
  9742. {
  9743. BFD_ASSERT (sym->st_value & 1);
  9744. sym->st_other -= STO_MICROMIPS;
  9745. }
  9746. return true;
  9747. }
  9748. /* Likewise, for VxWorks. */
  9749. bool
  9750. _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
  9751. struct bfd_link_info *info,
  9752. struct elf_link_hash_entry *h,
  9753. Elf_Internal_Sym *sym)
  9754. {
  9755. bfd *dynobj;
  9756. asection *sgot;
  9757. struct mips_got_info *g;
  9758. struct mips_elf_link_hash_table *htab;
  9759. struct mips_elf_link_hash_entry *hmips;
  9760. htab = mips_elf_hash_table (info);
  9761. BFD_ASSERT (htab != NULL);
  9762. dynobj = elf_hash_table (info)->dynobj;
  9763. hmips = (struct mips_elf_link_hash_entry *) h;
  9764. if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
  9765. {
  9766. bfd_byte *loc;
  9767. bfd_vma plt_address, got_address, got_offset, branch_offset;
  9768. Elf_Internal_Rela rel;
  9769. static const bfd_vma *plt_entry;
  9770. bfd_vma gotplt_index;
  9771. bfd_vma plt_offset;
  9772. plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
  9773. gotplt_index = h->plt.plist->gotplt_index;
  9774. BFD_ASSERT (h->dynindx != -1);
  9775. BFD_ASSERT (htab->root.splt != NULL);
  9776. BFD_ASSERT (gotplt_index != MINUS_ONE);
  9777. BFD_ASSERT (plt_offset <= htab->root.splt->size);
  9778. /* Calculate the address of the .plt entry. */
  9779. plt_address = (htab->root.splt->output_section->vma
  9780. + htab->root.splt->output_offset
  9781. + plt_offset);
  9782. /* Calculate the address of the .got.plt entry. */
  9783. got_address = (htab->root.sgotplt->output_section->vma
  9784. + htab->root.sgotplt->output_offset
  9785. + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
  9786. /* Calculate the offset of the .got.plt entry from
  9787. _GLOBAL_OFFSET_TABLE_. */
  9788. got_offset = mips_elf_gotplt_index (info, h);
  9789. /* Calculate the offset for the branch at the start of the PLT
  9790. entry. The branch jumps to the beginning of .plt. */
  9791. branch_offset = -(plt_offset / 4 + 1) & 0xffff;
  9792. /* Fill in the initial value of the .got.plt entry. */
  9793. bfd_put_32 (output_bfd, plt_address,
  9794. (htab->root.sgotplt->contents
  9795. + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
  9796. /* Find out where the .plt entry should go. */
  9797. loc = htab->root.splt->contents + plt_offset;
  9798. if (bfd_link_pic (info))
  9799. {
  9800. plt_entry = mips_vxworks_shared_plt_entry;
  9801. bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
  9802. bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
  9803. }
  9804. else
  9805. {
  9806. bfd_vma got_address_high, got_address_low;
  9807. plt_entry = mips_vxworks_exec_plt_entry;
  9808. got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
  9809. got_address_low = got_address & 0xffff;
  9810. bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
  9811. bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
  9812. bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
  9813. bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
  9814. bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
  9815. bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
  9816. bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
  9817. bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
  9818. loc = (htab->srelplt2->contents
  9819. + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
  9820. /* Emit a relocation for the .got.plt entry. */
  9821. rel.r_offset = got_address;
  9822. rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
  9823. rel.r_addend = plt_offset;
  9824. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  9825. /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
  9826. loc += sizeof (Elf32_External_Rela);
  9827. rel.r_offset = plt_address + 8;
  9828. rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
  9829. rel.r_addend = got_offset;
  9830. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  9831. /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
  9832. loc += sizeof (Elf32_External_Rela);
  9833. rel.r_offset += 4;
  9834. rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
  9835. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  9836. }
  9837. /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
  9838. loc = (htab->root.srelplt->contents
  9839. + gotplt_index * sizeof (Elf32_External_Rela));
  9840. rel.r_offset = got_address;
  9841. rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
  9842. rel.r_addend = 0;
  9843. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  9844. if (!h->def_regular)
  9845. sym->st_shndx = SHN_UNDEF;
  9846. }
  9847. BFD_ASSERT (h->dynindx != -1 || h->forced_local);
  9848. sgot = htab->root.sgot;
  9849. g = htab->got_info;
  9850. BFD_ASSERT (g != NULL);
  9851. /* See if this symbol has an entry in the GOT. */
  9852. if (hmips->global_got_area != GGA_NONE)
  9853. {
  9854. bfd_vma offset;
  9855. Elf_Internal_Rela outrel;
  9856. bfd_byte *loc;
  9857. asection *s;
  9858. /* Install the symbol value in the GOT. */
  9859. offset = mips_elf_primary_global_got_index (output_bfd, info, h);
  9860. MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
  9861. /* Add a dynamic relocation for it. */
  9862. s = mips_elf_rel_dyn_section (info, false);
  9863. loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
  9864. outrel.r_offset = (sgot->output_section->vma
  9865. + sgot->output_offset
  9866. + offset);
  9867. outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
  9868. outrel.r_addend = 0;
  9869. bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
  9870. }
  9871. /* Emit a copy reloc, if needed. */
  9872. if (h->needs_copy)
  9873. {
  9874. Elf_Internal_Rela rel;
  9875. asection *srel;
  9876. bfd_byte *loc;
  9877. BFD_ASSERT (h->dynindx != -1);
  9878. rel.r_offset = (h->root.u.def.section->output_section->vma
  9879. + h->root.u.def.section->output_offset
  9880. + h->root.u.def.value);
  9881. rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
  9882. rel.r_addend = 0;
  9883. if (h->root.u.def.section == htab->root.sdynrelro)
  9884. srel = htab->root.sreldynrelro;
  9885. else
  9886. srel = htab->root.srelbss;
  9887. loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
  9888. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  9889. ++srel->reloc_count;
  9890. }
  9891. /* If this is a mips16/microMIPS symbol, force the value to be even. */
  9892. if (ELF_ST_IS_COMPRESSED (sym->st_other))
  9893. sym->st_value &= ~1;
  9894. return true;
  9895. }
  9896. /* Write out a plt0 entry to the beginning of .plt. */
  9897. static bool
  9898. mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
  9899. {
  9900. bfd_byte *loc;
  9901. bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
  9902. static const bfd_vma *plt_entry;
  9903. struct mips_elf_link_hash_table *htab;
  9904. htab = mips_elf_hash_table (info);
  9905. BFD_ASSERT (htab != NULL);
  9906. if (ABI_64_P (output_bfd))
  9907. plt_entry = (htab->compact_branches
  9908. ? mipsr6_n64_exec_plt0_entry_compact
  9909. : mips_n64_exec_plt0_entry);
  9910. else if (ABI_N32_P (output_bfd))
  9911. plt_entry = (htab->compact_branches
  9912. ? mipsr6_n32_exec_plt0_entry_compact
  9913. : mips_n32_exec_plt0_entry);
  9914. else if (!htab->plt_header_is_comp)
  9915. plt_entry = (htab->compact_branches
  9916. ? mipsr6_o32_exec_plt0_entry_compact
  9917. : mips_o32_exec_plt0_entry);
  9918. else if (htab->insn32)
  9919. plt_entry = micromips_insn32_o32_exec_plt0_entry;
  9920. else
  9921. plt_entry = micromips_o32_exec_plt0_entry;
  9922. /* Calculate the value of .got.plt. */
  9923. gotplt_value = (htab->root.sgotplt->output_section->vma
  9924. + htab->root.sgotplt->output_offset);
  9925. gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
  9926. gotplt_value_low = gotplt_value & 0xffff;
  9927. /* The PLT sequence is not safe for N64 if .got.plt's address can
  9928. not be loaded in two instructions. */
  9929. if (ABI_64_P (output_bfd)
  9930. && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
  9931. {
  9932. _bfd_error_handler
  9933. /* xgettext:c-format */
  9934. (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
  9935. "supported; consider using `-Ttext-segment=...'"),
  9936. output_bfd,
  9937. htab->root.sgotplt->output_section,
  9938. (int64_t) gotplt_value);
  9939. bfd_set_error (bfd_error_no_error);
  9940. return false;
  9941. }
  9942. /* Install the PLT header. */
  9943. loc = htab->root.splt->contents;
  9944. if (plt_entry == micromips_o32_exec_plt0_entry)
  9945. {
  9946. bfd_vma gotpc_offset;
  9947. bfd_vma loc_address;
  9948. size_t i;
  9949. BFD_ASSERT (gotplt_value % 4 == 0);
  9950. loc_address = (htab->root.splt->output_section->vma
  9951. + htab->root.splt->output_offset);
  9952. gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
  9953. /* ADDIUPC has a span of +/-16MB, check we're in range. */
  9954. if (gotpc_offset + 0x1000000 >= 0x2000000)
  9955. {
  9956. _bfd_error_handler
  9957. /* xgettext:c-format */
  9958. (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
  9959. "beyond the range of ADDIUPC"),
  9960. output_bfd,
  9961. htab->root.sgotplt->output_section,
  9962. (int64_t) gotpc_offset,
  9963. htab->root.splt->output_section);
  9964. bfd_set_error (bfd_error_no_error);
  9965. return false;
  9966. }
  9967. bfd_put_16 (output_bfd,
  9968. plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
  9969. bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
  9970. for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
  9971. bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
  9972. }
  9973. else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
  9974. {
  9975. size_t i;
  9976. bfd_put_16 (output_bfd, plt_entry[0], loc);
  9977. bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
  9978. bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
  9979. bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
  9980. bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
  9981. bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
  9982. for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
  9983. bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
  9984. }
  9985. else
  9986. {
  9987. bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
  9988. bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
  9989. bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
  9990. bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
  9991. bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
  9992. bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
  9993. bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
  9994. bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
  9995. }
  9996. return true;
  9997. }
  9998. /* Install the PLT header for a VxWorks executable and finalize the
  9999. contents of .rela.plt.unloaded. */
  10000. static void
  10001. mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
  10002. {
  10003. Elf_Internal_Rela rela;
  10004. bfd_byte *loc;
  10005. bfd_vma got_value, got_value_high, got_value_low, plt_address;
  10006. static const bfd_vma *plt_entry;
  10007. struct mips_elf_link_hash_table *htab;
  10008. htab = mips_elf_hash_table (info);
  10009. BFD_ASSERT (htab != NULL);
  10010. plt_entry = mips_vxworks_exec_plt0_entry;
  10011. /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
  10012. got_value = (htab->root.hgot->root.u.def.section->output_section->vma
  10013. + htab->root.hgot->root.u.def.section->output_offset
  10014. + htab->root.hgot->root.u.def.value);
  10015. got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
  10016. got_value_low = got_value & 0xffff;
  10017. /* Calculate the address of the PLT header. */
  10018. plt_address = (htab->root.splt->output_section->vma
  10019. + htab->root.splt->output_offset);
  10020. /* Install the PLT header. */
  10021. loc = htab->root.splt->contents;
  10022. bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
  10023. bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
  10024. bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
  10025. bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
  10026. bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
  10027. bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
  10028. /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
  10029. loc = htab->srelplt2->contents;
  10030. rela.r_offset = plt_address;
  10031. rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
  10032. rela.r_addend = 0;
  10033. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  10034. loc += sizeof (Elf32_External_Rela);
  10035. /* Output the relocation for the following addiu of
  10036. %lo(_GLOBAL_OFFSET_TABLE_). */
  10037. rela.r_offset += 4;
  10038. rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
  10039. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  10040. loc += sizeof (Elf32_External_Rela);
  10041. /* Fix up the remaining relocations. They may have the wrong
  10042. symbol index for _G_O_T_ or _P_L_T_ depending on the order
  10043. in which symbols were output. */
  10044. while (loc < htab->srelplt2->contents + htab->srelplt2->size)
  10045. {
  10046. Elf_Internal_Rela rel;
  10047. bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
  10048. rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
  10049. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  10050. loc += sizeof (Elf32_External_Rela);
  10051. bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
  10052. rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
  10053. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  10054. loc += sizeof (Elf32_External_Rela);
  10055. bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
  10056. rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
  10057. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  10058. loc += sizeof (Elf32_External_Rela);
  10059. }
  10060. }
  10061. /* Install the PLT header for a VxWorks shared library. */
  10062. static void
  10063. mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
  10064. {
  10065. unsigned int i;
  10066. struct mips_elf_link_hash_table *htab;
  10067. htab = mips_elf_hash_table (info);
  10068. BFD_ASSERT (htab != NULL);
  10069. /* We just need to copy the entry byte-by-byte. */
  10070. for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
  10071. bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
  10072. htab->root.splt->contents + i * 4);
  10073. }
  10074. /* Finish up the dynamic sections. */
  10075. bool
  10076. _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
  10077. struct bfd_link_info *info)
  10078. {
  10079. bfd *dynobj;
  10080. asection *sdyn;
  10081. asection *sgot;
  10082. struct mips_got_info *gg, *g;
  10083. struct mips_elf_link_hash_table *htab;
  10084. htab = mips_elf_hash_table (info);
  10085. BFD_ASSERT (htab != NULL);
  10086. dynobj = elf_hash_table (info)->dynobj;
  10087. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  10088. sgot = htab->root.sgot;
  10089. gg = htab->got_info;
  10090. if (elf_hash_table (info)->dynamic_sections_created)
  10091. {
  10092. bfd_byte *b;
  10093. int dyn_to_skip = 0, dyn_skipped = 0;
  10094. BFD_ASSERT (sdyn != NULL);
  10095. BFD_ASSERT (gg != NULL);
  10096. g = mips_elf_bfd_got (output_bfd, false);
  10097. BFD_ASSERT (g != NULL);
  10098. for (b = sdyn->contents;
  10099. b < sdyn->contents + sdyn->size;
  10100. b += MIPS_ELF_DYN_SIZE (dynobj))
  10101. {
  10102. Elf_Internal_Dyn dyn;
  10103. const char *name;
  10104. size_t elemsize;
  10105. asection *s;
  10106. bool swap_out_p;
  10107. /* Read in the current dynamic entry. */
  10108. (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
  10109. /* Assume that we're going to modify it and write it out. */
  10110. swap_out_p = true;
  10111. switch (dyn.d_tag)
  10112. {
  10113. case DT_RELENT:
  10114. dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
  10115. break;
  10116. case DT_RELAENT:
  10117. BFD_ASSERT (htab->root.target_os == is_vxworks);
  10118. dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
  10119. break;
  10120. case DT_STRSZ:
  10121. /* Rewrite DT_STRSZ. */
  10122. dyn.d_un.d_val =
  10123. _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
  10124. break;
  10125. case DT_PLTGOT:
  10126. s = htab->root.sgot;
  10127. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  10128. break;
  10129. case DT_MIPS_PLTGOT:
  10130. s = htab->root.sgotplt;
  10131. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  10132. break;
  10133. case DT_MIPS_RLD_VERSION:
  10134. dyn.d_un.d_val = 1; /* XXX */
  10135. break;
  10136. case DT_MIPS_FLAGS:
  10137. dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
  10138. break;
  10139. case DT_MIPS_TIME_STAMP:
  10140. {
  10141. time_t t;
  10142. time (&t);
  10143. dyn.d_un.d_val = t;
  10144. }
  10145. break;
  10146. case DT_MIPS_ICHECKSUM:
  10147. /* XXX FIXME: */
  10148. swap_out_p = false;
  10149. break;
  10150. case DT_MIPS_IVERSION:
  10151. /* XXX FIXME: */
  10152. swap_out_p = false;
  10153. break;
  10154. case DT_MIPS_BASE_ADDRESS:
  10155. s = output_bfd->sections;
  10156. BFD_ASSERT (s != NULL);
  10157. dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
  10158. break;
  10159. case DT_MIPS_LOCAL_GOTNO:
  10160. dyn.d_un.d_val = g->local_gotno;
  10161. break;
  10162. case DT_MIPS_UNREFEXTNO:
  10163. /* The index into the dynamic symbol table which is the
  10164. entry of the first external symbol that is not
  10165. referenced within the same object. */
  10166. dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
  10167. break;
  10168. case DT_MIPS_GOTSYM:
  10169. if (htab->global_gotsym)
  10170. {
  10171. dyn.d_un.d_val = htab->global_gotsym->dynindx;
  10172. break;
  10173. }
  10174. /* In case if we don't have global got symbols we default
  10175. to setting DT_MIPS_GOTSYM to the same value as
  10176. DT_MIPS_SYMTABNO. */
  10177. /* Fall through. */
  10178. case DT_MIPS_SYMTABNO:
  10179. name = ".dynsym";
  10180. elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
  10181. s = bfd_get_linker_section (dynobj, name);
  10182. if (s != NULL)
  10183. dyn.d_un.d_val = s->size / elemsize;
  10184. else
  10185. dyn.d_un.d_val = 0;
  10186. break;
  10187. case DT_MIPS_HIPAGENO:
  10188. dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
  10189. break;
  10190. case DT_MIPS_RLD_MAP:
  10191. {
  10192. struct elf_link_hash_entry *h;
  10193. h = mips_elf_hash_table (info)->rld_symbol;
  10194. if (!h)
  10195. {
  10196. dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
  10197. swap_out_p = false;
  10198. break;
  10199. }
  10200. s = h->root.u.def.section;
  10201. /* The MIPS_RLD_MAP tag stores the absolute address of the
  10202. debug pointer. */
  10203. dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
  10204. + h->root.u.def.value);
  10205. }
  10206. break;
  10207. case DT_MIPS_RLD_MAP_REL:
  10208. {
  10209. struct elf_link_hash_entry *h;
  10210. bfd_vma dt_addr, rld_addr;
  10211. h = mips_elf_hash_table (info)->rld_symbol;
  10212. if (!h)
  10213. {
  10214. dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
  10215. swap_out_p = false;
  10216. break;
  10217. }
  10218. s = h->root.u.def.section;
  10219. /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
  10220. pointer, relative to the address of the tag. */
  10221. dt_addr = (sdyn->output_section->vma + sdyn->output_offset
  10222. + (b - sdyn->contents));
  10223. rld_addr = (s->output_section->vma + s->output_offset
  10224. + h->root.u.def.value);
  10225. dyn.d_un.d_ptr = rld_addr - dt_addr;
  10226. }
  10227. break;
  10228. case DT_MIPS_OPTIONS:
  10229. s = (bfd_get_section_by_name
  10230. (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
  10231. dyn.d_un.d_ptr = s->vma;
  10232. break;
  10233. case DT_PLTREL:
  10234. BFD_ASSERT (htab->use_plts_and_copy_relocs);
  10235. if (htab->root.target_os == is_vxworks)
  10236. dyn.d_un.d_val = DT_RELA;
  10237. else
  10238. dyn.d_un.d_val = DT_REL;
  10239. break;
  10240. case DT_PLTRELSZ:
  10241. BFD_ASSERT (htab->use_plts_and_copy_relocs);
  10242. dyn.d_un.d_val = htab->root.srelplt->size;
  10243. break;
  10244. case DT_JMPREL:
  10245. BFD_ASSERT (htab->use_plts_and_copy_relocs);
  10246. dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
  10247. + htab->root.srelplt->output_offset);
  10248. break;
  10249. case DT_TEXTREL:
  10250. /* If we didn't need any text relocations after all, delete
  10251. the dynamic tag. */
  10252. if (!(info->flags & DF_TEXTREL))
  10253. {
  10254. dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
  10255. swap_out_p = false;
  10256. }
  10257. break;
  10258. case DT_FLAGS:
  10259. /* If we didn't need any text relocations after all, clear
  10260. DF_TEXTREL from DT_FLAGS. */
  10261. if (!(info->flags & DF_TEXTREL))
  10262. dyn.d_un.d_val &= ~DF_TEXTREL;
  10263. else
  10264. swap_out_p = false;
  10265. break;
  10266. case DT_MIPS_XHASH:
  10267. name = ".MIPS.xhash";
  10268. s = bfd_get_linker_section (dynobj, name);
  10269. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  10270. break;
  10271. default:
  10272. swap_out_p = false;
  10273. if (htab->root.target_os == is_vxworks
  10274. && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
  10275. swap_out_p = true;
  10276. break;
  10277. }
  10278. if (swap_out_p || dyn_skipped)
  10279. (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
  10280. (dynobj, &dyn, b - dyn_skipped);
  10281. if (dyn_to_skip)
  10282. {
  10283. dyn_skipped += dyn_to_skip;
  10284. dyn_to_skip = 0;
  10285. }
  10286. }
  10287. /* Wipe out any trailing entries if we shifted down a dynamic tag. */
  10288. if (dyn_skipped > 0)
  10289. memset (b - dyn_skipped, 0, dyn_skipped);
  10290. }
  10291. if (sgot != NULL && sgot->size > 0
  10292. && !bfd_is_abs_section (sgot->output_section))
  10293. {
  10294. if (htab->root.target_os == is_vxworks)
  10295. {
  10296. /* The first entry of the global offset table points to the
  10297. ".dynamic" section. The second is initialized by the
  10298. loader and contains the shared library identifier.
  10299. The third is also initialized by the loader and points
  10300. to the lazy resolution stub. */
  10301. MIPS_ELF_PUT_WORD (output_bfd,
  10302. sdyn->output_offset + sdyn->output_section->vma,
  10303. sgot->contents);
  10304. MIPS_ELF_PUT_WORD (output_bfd, 0,
  10305. sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
  10306. MIPS_ELF_PUT_WORD (output_bfd, 0,
  10307. sgot->contents
  10308. + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
  10309. }
  10310. else
  10311. {
  10312. /* The first entry of the global offset table will be filled at
  10313. runtime. The second entry will be used by some runtime loaders.
  10314. This isn't the case of IRIX rld. */
  10315. MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
  10316. MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
  10317. sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
  10318. }
  10319. elf_section_data (sgot->output_section)->this_hdr.sh_entsize
  10320. = MIPS_ELF_GOT_SIZE (output_bfd);
  10321. }
  10322. /* Generate dynamic relocations for the non-primary gots. */
  10323. if (gg != NULL && gg->next)
  10324. {
  10325. Elf_Internal_Rela rel[3];
  10326. bfd_vma addend = 0;
  10327. memset (rel, 0, sizeof (rel));
  10328. rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
  10329. for (g = gg->next; g->next != gg; g = g->next)
  10330. {
  10331. bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
  10332. + g->next->tls_gotno;
  10333. MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
  10334. + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
  10335. MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
  10336. sgot->contents
  10337. + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
  10338. if (! bfd_link_pic (info))
  10339. continue;
  10340. for (; got_index < g->local_gotno; got_index++)
  10341. {
  10342. if (got_index >= g->assigned_low_gotno
  10343. && got_index <= g->assigned_high_gotno)
  10344. continue;
  10345. rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
  10346. = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
  10347. if (!(mips_elf_create_dynamic_relocation
  10348. (output_bfd, info, rel, NULL,
  10349. bfd_abs_section_ptr,
  10350. 0, &addend, sgot)))
  10351. return false;
  10352. BFD_ASSERT (addend == 0);
  10353. }
  10354. }
  10355. }
  10356. /* The generation of dynamic relocations for the non-primary gots
  10357. adds more dynamic relocations. We cannot count them until
  10358. here. */
  10359. if (elf_hash_table (info)->dynamic_sections_created)
  10360. {
  10361. bfd_byte *b;
  10362. bool swap_out_p;
  10363. BFD_ASSERT (sdyn != NULL);
  10364. for (b = sdyn->contents;
  10365. b < sdyn->contents + sdyn->size;
  10366. b += MIPS_ELF_DYN_SIZE (dynobj))
  10367. {
  10368. Elf_Internal_Dyn dyn;
  10369. asection *s;
  10370. /* Read in the current dynamic entry. */
  10371. (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
  10372. /* Assume that we're going to modify it and write it out. */
  10373. swap_out_p = true;
  10374. switch (dyn.d_tag)
  10375. {
  10376. case DT_RELSZ:
  10377. /* Reduce DT_RELSZ to account for any relocations we
  10378. decided not to make. This is for the n64 irix rld,
  10379. which doesn't seem to apply any relocations if there
  10380. are trailing null entries. */
  10381. s = mips_elf_rel_dyn_section (info, false);
  10382. dyn.d_un.d_val = (s->reloc_count
  10383. * (ABI_64_P (output_bfd)
  10384. ? sizeof (Elf64_Mips_External_Rel)
  10385. : sizeof (Elf32_External_Rel)));
  10386. /* Adjust the section size too. Tools like the prelinker
  10387. can reasonably expect the values to the same. */
  10388. BFD_ASSERT (!bfd_is_abs_section (s->output_section));
  10389. elf_section_data (s->output_section)->this_hdr.sh_size
  10390. = dyn.d_un.d_val;
  10391. break;
  10392. default:
  10393. swap_out_p = false;
  10394. break;
  10395. }
  10396. if (swap_out_p)
  10397. (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
  10398. (dynobj, &dyn, b);
  10399. }
  10400. }
  10401. {
  10402. asection *s;
  10403. Elf32_compact_rel cpt;
  10404. if (SGI_COMPAT (output_bfd))
  10405. {
  10406. /* Write .compact_rel section out. */
  10407. s = bfd_get_linker_section (dynobj, ".compact_rel");
  10408. if (s != NULL)
  10409. {
  10410. cpt.id1 = 1;
  10411. cpt.num = s->reloc_count;
  10412. cpt.id2 = 2;
  10413. cpt.offset = (s->output_section->filepos
  10414. + sizeof (Elf32_External_compact_rel));
  10415. cpt.reserved0 = 0;
  10416. cpt.reserved1 = 0;
  10417. bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
  10418. ((Elf32_External_compact_rel *)
  10419. s->contents));
  10420. /* Clean up a dummy stub function entry in .text. */
  10421. if (htab->sstubs != NULL
  10422. && htab->sstubs->contents != NULL)
  10423. {
  10424. file_ptr dummy_offset;
  10425. BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
  10426. dummy_offset = htab->sstubs->size - htab->function_stub_size;
  10427. memset (htab->sstubs->contents + dummy_offset, 0,
  10428. htab->function_stub_size);
  10429. }
  10430. }
  10431. }
  10432. /* The psABI says that the dynamic relocations must be sorted in
  10433. increasing order of r_symndx. The VxWorks EABI doesn't require
  10434. this, and because the code below handles REL rather than RELA
  10435. relocations, using it for VxWorks would be outright harmful. */
  10436. if (htab->root.target_os != is_vxworks)
  10437. {
  10438. s = mips_elf_rel_dyn_section (info, false);
  10439. if (s != NULL
  10440. && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
  10441. {
  10442. reldyn_sorting_bfd = output_bfd;
  10443. if (ABI_64_P (output_bfd))
  10444. qsort ((Elf64_External_Rel *) s->contents + 1,
  10445. s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
  10446. sort_dynamic_relocs_64);
  10447. else
  10448. qsort ((Elf32_External_Rel *) s->contents + 1,
  10449. s->reloc_count - 1, sizeof (Elf32_External_Rel),
  10450. sort_dynamic_relocs);
  10451. }
  10452. }
  10453. }
  10454. if (htab->root.splt && htab->root.splt->size > 0)
  10455. {
  10456. if (htab->root.target_os == is_vxworks)
  10457. {
  10458. if (bfd_link_pic (info))
  10459. mips_vxworks_finish_shared_plt (output_bfd, info);
  10460. else
  10461. mips_vxworks_finish_exec_plt (output_bfd, info);
  10462. }
  10463. else
  10464. {
  10465. BFD_ASSERT (!bfd_link_pic (info));
  10466. if (!mips_finish_exec_plt (output_bfd, info))
  10467. return false;
  10468. }
  10469. }
  10470. return true;
  10471. }
  10472. /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
  10473. static void
  10474. mips_set_isa_flags (bfd *abfd)
  10475. {
  10476. flagword val;
  10477. switch (bfd_get_mach (abfd))
  10478. {
  10479. default:
  10480. if (ABI_N32_P (abfd) || ABI_64_P (abfd))
  10481. val = E_MIPS_ARCH_3;
  10482. else
  10483. val = E_MIPS_ARCH_1;
  10484. break;
  10485. case bfd_mach_mips3000:
  10486. val = E_MIPS_ARCH_1;
  10487. break;
  10488. case bfd_mach_mips3900:
  10489. val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
  10490. break;
  10491. case bfd_mach_mips6000:
  10492. val = E_MIPS_ARCH_2;
  10493. break;
  10494. case bfd_mach_mips4010:
  10495. val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
  10496. break;
  10497. case bfd_mach_mips4000:
  10498. case bfd_mach_mips4300:
  10499. case bfd_mach_mips4400:
  10500. case bfd_mach_mips4600:
  10501. val = E_MIPS_ARCH_3;
  10502. break;
  10503. case bfd_mach_mips4100:
  10504. val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
  10505. break;
  10506. case bfd_mach_mips4111:
  10507. val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
  10508. break;
  10509. case bfd_mach_mips4120:
  10510. val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
  10511. break;
  10512. case bfd_mach_mips4650:
  10513. val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
  10514. break;
  10515. case bfd_mach_mips5400:
  10516. val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
  10517. break;
  10518. case bfd_mach_mips5500:
  10519. val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
  10520. break;
  10521. case bfd_mach_mips5900:
  10522. val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
  10523. break;
  10524. case bfd_mach_mips9000:
  10525. val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
  10526. break;
  10527. case bfd_mach_mips5000:
  10528. case bfd_mach_mips7000:
  10529. case bfd_mach_mips8000:
  10530. case bfd_mach_mips10000:
  10531. case bfd_mach_mips12000:
  10532. case bfd_mach_mips14000:
  10533. case bfd_mach_mips16000:
  10534. val = E_MIPS_ARCH_4;
  10535. break;
  10536. case bfd_mach_mips5:
  10537. val = E_MIPS_ARCH_5;
  10538. break;
  10539. case bfd_mach_mips_loongson_2e:
  10540. val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
  10541. break;
  10542. case bfd_mach_mips_loongson_2f:
  10543. val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
  10544. break;
  10545. case bfd_mach_mips_sb1:
  10546. val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
  10547. break;
  10548. case bfd_mach_mips_gs464:
  10549. val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
  10550. break;
  10551. case bfd_mach_mips_gs464e:
  10552. val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
  10553. break;
  10554. case bfd_mach_mips_gs264e:
  10555. val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
  10556. break;
  10557. case bfd_mach_mips_octeon:
  10558. case bfd_mach_mips_octeonp:
  10559. val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
  10560. break;
  10561. case bfd_mach_mips_octeon3:
  10562. val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
  10563. break;
  10564. case bfd_mach_mips_xlr:
  10565. val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
  10566. break;
  10567. case bfd_mach_mips_octeon2:
  10568. val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
  10569. break;
  10570. case bfd_mach_mipsisa32:
  10571. val = E_MIPS_ARCH_32;
  10572. break;
  10573. case bfd_mach_mipsisa64:
  10574. val = E_MIPS_ARCH_64;
  10575. break;
  10576. case bfd_mach_mipsisa32r2:
  10577. case bfd_mach_mipsisa32r3:
  10578. case bfd_mach_mipsisa32r5:
  10579. val = E_MIPS_ARCH_32R2;
  10580. break;
  10581. case bfd_mach_mips_interaptiv_mr2:
  10582. val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
  10583. break;
  10584. case bfd_mach_mipsisa64r2:
  10585. case bfd_mach_mipsisa64r3:
  10586. case bfd_mach_mipsisa64r5:
  10587. val = E_MIPS_ARCH_64R2;
  10588. break;
  10589. case bfd_mach_mipsisa32r6:
  10590. val = E_MIPS_ARCH_32R6;
  10591. break;
  10592. case bfd_mach_mipsisa64r6:
  10593. val = E_MIPS_ARCH_64R6;
  10594. break;
  10595. }
  10596. elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
  10597. elf_elfheader (abfd)->e_flags |= val;
  10598. }
  10599. /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
  10600. Don't do so for code sections. We want to keep ordering of HI16/LO16
  10601. as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
  10602. relocs to be sorted. */
  10603. bool
  10604. _bfd_mips_elf_sort_relocs_p (asection *sec)
  10605. {
  10606. return (sec->flags & SEC_CODE) == 0;
  10607. }
  10608. /* The final processing done just before writing out a MIPS ELF object
  10609. file. This gets the MIPS architecture right based on the machine
  10610. number. This is used by both the 32-bit and the 64-bit ABI. */
  10611. void
  10612. _bfd_mips_final_write_processing (bfd *abfd)
  10613. {
  10614. unsigned int i;
  10615. Elf_Internal_Shdr **hdrpp;
  10616. const char *name;
  10617. asection *sec;
  10618. /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
  10619. is nonzero. This is for compatibility with old objects, which used
  10620. a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
  10621. if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
  10622. mips_set_isa_flags (abfd);
  10623. /* Set the sh_info field for .gptab sections and other appropriate
  10624. info for each special section. */
  10625. for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
  10626. i < elf_numsections (abfd);
  10627. i++, hdrpp++)
  10628. {
  10629. switch ((*hdrpp)->sh_type)
  10630. {
  10631. case SHT_MIPS_MSYM:
  10632. case SHT_MIPS_LIBLIST:
  10633. sec = bfd_get_section_by_name (abfd, ".dynstr");
  10634. if (sec != NULL)
  10635. (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
  10636. break;
  10637. case SHT_MIPS_GPTAB:
  10638. BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
  10639. name = bfd_section_name ((*hdrpp)->bfd_section);
  10640. BFD_ASSERT (name != NULL
  10641. && startswith (name, ".gptab."));
  10642. sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
  10643. BFD_ASSERT (sec != NULL);
  10644. (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
  10645. break;
  10646. case SHT_MIPS_CONTENT:
  10647. BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
  10648. name = bfd_section_name ((*hdrpp)->bfd_section);
  10649. BFD_ASSERT (name != NULL
  10650. && startswith (name, ".MIPS.content"));
  10651. sec = bfd_get_section_by_name (abfd,
  10652. name + sizeof ".MIPS.content" - 1);
  10653. BFD_ASSERT (sec != NULL);
  10654. (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
  10655. break;
  10656. case SHT_MIPS_SYMBOL_LIB:
  10657. sec = bfd_get_section_by_name (abfd, ".dynsym");
  10658. if (sec != NULL)
  10659. (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
  10660. sec = bfd_get_section_by_name (abfd, ".liblist");
  10661. if (sec != NULL)
  10662. (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
  10663. break;
  10664. case SHT_MIPS_EVENTS:
  10665. BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
  10666. name = bfd_section_name ((*hdrpp)->bfd_section);
  10667. BFD_ASSERT (name != NULL);
  10668. if (startswith (name, ".MIPS.events"))
  10669. sec = bfd_get_section_by_name (abfd,
  10670. name + sizeof ".MIPS.events" - 1);
  10671. else
  10672. {
  10673. BFD_ASSERT (startswith (name, ".MIPS.post_rel"));
  10674. sec = bfd_get_section_by_name (abfd,
  10675. (name
  10676. + sizeof ".MIPS.post_rel" - 1));
  10677. }
  10678. BFD_ASSERT (sec != NULL);
  10679. (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
  10680. break;
  10681. case SHT_MIPS_XHASH:
  10682. sec = bfd_get_section_by_name (abfd, ".dynsym");
  10683. if (sec != NULL)
  10684. (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
  10685. }
  10686. }
  10687. }
  10688. bool
  10689. _bfd_mips_elf_final_write_processing (bfd *abfd)
  10690. {
  10691. _bfd_mips_final_write_processing (abfd);
  10692. return _bfd_elf_final_write_processing (abfd);
  10693. }
  10694. /* When creating an IRIX5 executable, we need REGINFO and RTPROC
  10695. segments. */
  10696. int
  10697. _bfd_mips_elf_additional_program_headers (bfd *abfd,
  10698. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  10699. {
  10700. asection *s;
  10701. int ret = 0;
  10702. /* See if we need a PT_MIPS_REGINFO segment. */
  10703. s = bfd_get_section_by_name (abfd, ".reginfo");
  10704. if (s && (s->flags & SEC_LOAD))
  10705. ++ret;
  10706. /* See if we need a PT_MIPS_ABIFLAGS segment. */
  10707. if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
  10708. ++ret;
  10709. /* See if we need a PT_MIPS_OPTIONS segment. */
  10710. if (IRIX_COMPAT (abfd) == ict_irix6
  10711. && bfd_get_section_by_name (abfd,
  10712. MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
  10713. ++ret;
  10714. /* See if we need a PT_MIPS_RTPROC segment. */
  10715. if (IRIX_COMPAT (abfd) == ict_irix5
  10716. && bfd_get_section_by_name (abfd, ".dynamic")
  10717. && bfd_get_section_by_name (abfd, ".mdebug"))
  10718. ++ret;
  10719. /* Allocate a PT_NULL header in dynamic objects. See
  10720. _bfd_mips_elf_modify_segment_map for details. */
  10721. if (!SGI_COMPAT (abfd)
  10722. && bfd_get_section_by_name (abfd, ".dynamic"))
  10723. ++ret;
  10724. return ret;
  10725. }
  10726. /* Modify the segment map for an IRIX5 executable. */
  10727. bool
  10728. _bfd_mips_elf_modify_segment_map (bfd *abfd,
  10729. struct bfd_link_info *info)
  10730. {
  10731. asection *s;
  10732. struct elf_segment_map *m, **pm;
  10733. size_t amt;
  10734. /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
  10735. segment. */
  10736. s = bfd_get_section_by_name (abfd, ".reginfo");
  10737. if (s != NULL && (s->flags & SEC_LOAD) != 0)
  10738. {
  10739. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  10740. if (m->p_type == PT_MIPS_REGINFO)
  10741. break;
  10742. if (m == NULL)
  10743. {
  10744. amt = sizeof *m;
  10745. m = bfd_zalloc (abfd, amt);
  10746. if (m == NULL)
  10747. return false;
  10748. m->p_type = PT_MIPS_REGINFO;
  10749. m->count = 1;
  10750. m->sections[0] = s;
  10751. /* We want to put it after the PHDR and INTERP segments. */
  10752. pm = &elf_seg_map (abfd);
  10753. while (*pm != NULL
  10754. && ((*pm)->p_type == PT_PHDR
  10755. || (*pm)->p_type == PT_INTERP))
  10756. pm = &(*pm)->next;
  10757. m->next = *pm;
  10758. *pm = m;
  10759. }
  10760. }
  10761. /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
  10762. segment. */
  10763. s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
  10764. if (s != NULL && (s->flags & SEC_LOAD) != 0)
  10765. {
  10766. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  10767. if (m->p_type == PT_MIPS_ABIFLAGS)
  10768. break;
  10769. if (m == NULL)
  10770. {
  10771. amt = sizeof *m;
  10772. m = bfd_zalloc (abfd, amt);
  10773. if (m == NULL)
  10774. return false;
  10775. m->p_type = PT_MIPS_ABIFLAGS;
  10776. m->count = 1;
  10777. m->sections[0] = s;
  10778. /* We want to put it after the PHDR and INTERP segments. */
  10779. pm = &elf_seg_map (abfd);
  10780. while (*pm != NULL
  10781. && ((*pm)->p_type == PT_PHDR
  10782. || (*pm)->p_type == PT_INTERP))
  10783. pm = &(*pm)->next;
  10784. m->next = *pm;
  10785. *pm = m;
  10786. }
  10787. }
  10788. /* For IRIX 6, we don't have .mdebug sections, nor does anything but
  10789. .dynamic end up in PT_DYNAMIC. However, we do have to insert a
  10790. PT_MIPS_OPTIONS segment immediately following the program header
  10791. table. */
  10792. if (NEWABI_P (abfd)
  10793. /* On non-IRIX6 new abi, we'll have already created a segment
  10794. for this section, so don't create another. I'm not sure this
  10795. is not also the case for IRIX 6, but I can't test it right
  10796. now. */
  10797. && IRIX_COMPAT (abfd) == ict_irix6)
  10798. {
  10799. for (s = abfd->sections; s; s = s->next)
  10800. if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
  10801. break;
  10802. if (s)
  10803. {
  10804. struct elf_segment_map *options_segment;
  10805. pm = &elf_seg_map (abfd);
  10806. while (*pm != NULL
  10807. && ((*pm)->p_type == PT_PHDR
  10808. || (*pm)->p_type == PT_INTERP))
  10809. pm = &(*pm)->next;
  10810. if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
  10811. {
  10812. amt = sizeof (struct elf_segment_map);
  10813. options_segment = bfd_zalloc (abfd, amt);
  10814. options_segment->next = *pm;
  10815. options_segment->p_type = PT_MIPS_OPTIONS;
  10816. options_segment->p_flags = PF_R;
  10817. options_segment->p_flags_valid = true;
  10818. options_segment->count = 1;
  10819. options_segment->sections[0] = s;
  10820. *pm = options_segment;
  10821. }
  10822. }
  10823. }
  10824. else
  10825. {
  10826. if (IRIX_COMPAT (abfd) == ict_irix5)
  10827. {
  10828. /* If there are .dynamic and .mdebug sections, we make a room
  10829. for the RTPROC header. FIXME: Rewrite without section names. */
  10830. if (bfd_get_section_by_name (abfd, ".interp") == NULL
  10831. && bfd_get_section_by_name (abfd, ".dynamic") != NULL
  10832. && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
  10833. {
  10834. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  10835. if (m->p_type == PT_MIPS_RTPROC)
  10836. break;
  10837. if (m == NULL)
  10838. {
  10839. amt = sizeof *m;
  10840. m = bfd_zalloc (abfd, amt);
  10841. if (m == NULL)
  10842. return false;
  10843. m->p_type = PT_MIPS_RTPROC;
  10844. s = bfd_get_section_by_name (abfd, ".rtproc");
  10845. if (s == NULL)
  10846. {
  10847. m->count = 0;
  10848. m->p_flags = 0;
  10849. m->p_flags_valid = 1;
  10850. }
  10851. else
  10852. {
  10853. m->count = 1;
  10854. m->sections[0] = s;
  10855. }
  10856. /* We want to put it after the DYNAMIC segment. */
  10857. pm = &elf_seg_map (abfd);
  10858. while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
  10859. pm = &(*pm)->next;
  10860. if (*pm != NULL)
  10861. pm = &(*pm)->next;
  10862. m->next = *pm;
  10863. *pm = m;
  10864. }
  10865. }
  10866. }
  10867. /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
  10868. .dynstr, .dynsym, and .hash sections, and everything in
  10869. between. */
  10870. for (pm = &elf_seg_map (abfd); *pm != NULL;
  10871. pm = &(*pm)->next)
  10872. if ((*pm)->p_type == PT_DYNAMIC)
  10873. break;
  10874. m = *pm;
  10875. /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
  10876. glibc's dynamic linker has traditionally derived the number of
  10877. tags from the p_filesz field, and sometimes allocates stack
  10878. arrays of that size. An overly-big PT_DYNAMIC segment can
  10879. be actively harmful in such cases. Making PT_DYNAMIC contain
  10880. other sections can also make life hard for the prelinker,
  10881. which might move one of the other sections to a different
  10882. PT_LOAD segment. */
  10883. if (SGI_COMPAT (abfd)
  10884. && m != NULL
  10885. && m->count == 1
  10886. && strcmp (m->sections[0]->name, ".dynamic") == 0)
  10887. {
  10888. static const char *sec_names[] =
  10889. {
  10890. ".dynamic", ".dynstr", ".dynsym", ".hash"
  10891. };
  10892. bfd_vma low, high;
  10893. unsigned int i, c;
  10894. struct elf_segment_map *n;
  10895. low = ~(bfd_vma) 0;
  10896. high = 0;
  10897. for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
  10898. {
  10899. s = bfd_get_section_by_name (abfd, sec_names[i]);
  10900. if (s != NULL && (s->flags & SEC_LOAD) != 0)
  10901. {
  10902. bfd_size_type sz;
  10903. if (low > s->vma)
  10904. low = s->vma;
  10905. sz = s->size;
  10906. if (high < s->vma + sz)
  10907. high = s->vma + sz;
  10908. }
  10909. }
  10910. c = 0;
  10911. for (s = abfd->sections; s != NULL; s = s->next)
  10912. if ((s->flags & SEC_LOAD) != 0
  10913. && s->vma >= low
  10914. && s->vma + s->size <= high)
  10915. ++c;
  10916. amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
  10917. n = bfd_zalloc (abfd, amt);
  10918. if (n == NULL)
  10919. return false;
  10920. *n = *m;
  10921. n->count = c;
  10922. i = 0;
  10923. for (s = abfd->sections; s != NULL; s = s->next)
  10924. {
  10925. if ((s->flags & SEC_LOAD) != 0
  10926. && s->vma >= low
  10927. && s->vma + s->size <= high)
  10928. {
  10929. n->sections[i] = s;
  10930. ++i;
  10931. }
  10932. }
  10933. *pm = n;
  10934. }
  10935. }
  10936. /* Allocate a spare program header in dynamic objects so that tools
  10937. like the prelinker can add an extra PT_LOAD entry.
  10938. If the prelinker needs to make room for a new PT_LOAD entry, its
  10939. standard procedure is to move the first (read-only) sections into
  10940. the new (writable) segment. However, the MIPS ABI requires
  10941. .dynamic to be in a read-only segment, and the section will often
  10942. start within sizeof (ElfNN_Phdr) bytes of the last program header.
  10943. Although the prelinker could in principle move .dynamic to a
  10944. writable segment, it seems better to allocate a spare program
  10945. header instead, and avoid the need to move any sections.
  10946. There is a long tradition of allocating spare dynamic tags,
  10947. so allocating a spare program header seems like a natural
  10948. extension.
  10949. If INFO is NULL, we may be copying an already prelinked binary
  10950. with objcopy or strip, so do not add this header. */
  10951. if (info != NULL
  10952. && !SGI_COMPAT (abfd)
  10953. && bfd_get_section_by_name (abfd, ".dynamic"))
  10954. {
  10955. for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
  10956. if ((*pm)->p_type == PT_NULL)
  10957. break;
  10958. if (*pm == NULL)
  10959. {
  10960. m = bfd_zalloc (abfd, sizeof (*m));
  10961. if (m == NULL)
  10962. return false;
  10963. m->p_type = PT_NULL;
  10964. *pm = m;
  10965. }
  10966. }
  10967. return true;
  10968. }
  10969. /* Return the section that should be marked against GC for a given
  10970. relocation. */
  10971. asection *
  10972. _bfd_mips_elf_gc_mark_hook (asection *sec,
  10973. struct bfd_link_info *info,
  10974. Elf_Internal_Rela *rel,
  10975. struct elf_link_hash_entry *h,
  10976. Elf_Internal_Sym *sym)
  10977. {
  10978. /* ??? Do mips16 stub sections need to be handled special? */
  10979. if (h != NULL)
  10980. switch (ELF_R_TYPE (sec->owner, rel->r_info))
  10981. {
  10982. case R_MIPS_GNU_VTINHERIT:
  10983. case R_MIPS_GNU_VTENTRY:
  10984. return NULL;
  10985. }
  10986. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  10987. }
  10988. /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
  10989. bool
  10990. _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
  10991. elf_gc_mark_hook_fn gc_mark_hook)
  10992. {
  10993. bfd *sub;
  10994. _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
  10995. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  10996. {
  10997. asection *o;
  10998. if (! is_mips_elf (sub))
  10999. continue;
  11000. for (o = sub->sections; o != NULL; o = o->next)
  11001. if (!o->gc_mark
  11002. && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
  11003. {
  11004. if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
  11005. return false;
  11006. }
  11007. }
  11008. return true;
  11009. }
  11010. /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
  11011. hiding the old indirect symbol. Process additional relocation
  11012. information. Also called for weakdefs, in which case we just let
  11013. _bfd_elf_link_hash_copy_indirect copy the flags for us. */
  11014. void
  11015. _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
  11016. struct elf_link_hash_entry *dir,
  11017. struct elf_link_hash_entry *ind)
  11018. {
  11019. struct mips_elf_link_hash_entry *dirmips, *indmips;
  11020. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  11021. dirmips = (struct mips_elf_link_hash_entry *) dir;
  11022. indmips = (struct mips_elf_link_hash_entry *) ind;
  11023. /* Any absolute non-dynamic relocations against an indirect or weak
  11024. definition will be against the target symbol. */
  11025. if (indmips->has_static_relocs)
  11026. dirmips->has_static_relocs = true;
  11027. if (ind->root.type != bfd_link_hash_indirect)
  11028. return;
  11029. dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
  11030. if (indmips->readonly_reloc)
  11031. dirmips->readonly_reloc = true;
  11032. if (indmips->no_fn_stub)
  11033. dirmips->no_fn_stub = true;
  11034. if (indmips->fn_stub)
  11035. {
  11036. dirmips->fn_stub = indmips->fn_stub;
  11037. indmips->fn_stub = NULL;
  11038. }
  11039. if (indmips->need_fn_stub)
  11040. {
  11041. dirmips->need_fn_stub = true;
  11042. indmips->need_fn_stub = false;
  11043. }
  11044. if (indmips->call_stub)
  11045. {
  11046. dirmips->call_stub = indmips->call_stub;
  11047. indmips->call_stub = NULL;
  11048. }
  11049. if (indmips->call_fp_stub)
  11050. {
  11051. dirmips->call_fp_stub = indmips->call_fp_stub;
  11052. indmips->call_fp_stub = NULL;
  11053. }
  11054. if (indmips->global_got_area < dirmips->global_got_area)
  11055. dirmips->global_got_area = indmips->global_got_area;
  11056. if (indmips->global_got_area < GGA_NONE)
  11057. indmips->global_got_area = GGA_NONE;
  11058. if (indmips->has_nonpic_branches)
  11059. dirmips->has_nonpic_branches = true;
  11060. }
  11061. /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
  11062. to hide it. It has to remain global (it will also be protected) so as to
  11063. be assigned a global GOT entry, which will then remain unchanged at load
  11064. time. */
  11065. void
  11066. _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
  11067. struct elf_link_hash_entry *entry,
  11068. bool force_local)
  11069. {
  11070. struct mips_elf_link_hash_table *htab;
  11071. htab = mips_elf_hash_table (info);
  11072. BFD_ASSERT (htab != NULL);
  11073. if (htab->use_absolute_zero
  11074. && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
  11075. return;
  11076. _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
  11077. }
  11078. #define PDR_SIZE 32
  11079. bool
  11080. _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
  11081. struct bfd_link_info *info)
  11082. {
  11083. asection *o;
  11084. bool ret = false;
  11085. unsigned char *tdata;
  11086. size_t i, skip;
  11087. o = bfd_get_section_by_name (abfd, ".pdr");
  11088. if (! o)
  11089. return false;
  11090. if (o->size == 0)
  11091. return false;
  11092. if (o->size % PDR_SIZE != 0)
  11093. return false;
  11094. if (o->output_section != NULL
  11095. && bfd_is_abs_section (o->output_section))
  11096. return false;
  11097. tdata = bfd_zmalloc (o->size / PDR_SIZE);
  11098. if (! tdata)
  11099. return false;
  11100. cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
  11101. info->keep_memory);
  11102. if (!cookie->rels)
  11103. {
  11104. free (tdata);
  11105. return false;
  11106. }
  11107. cookie->rel = cookie->rels;
  11108. cookie->relend = cookie->rels + o->reloc_count;
  11109. for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
  11110. {
  11111. if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
  11112. {
  11113. tdata[i] = 1;
  11114. skip ++;
  11115. }
  11116. }
  11117. if (skip != 0)
  11118. {
  11119. mips_elf_section_data (o)->u.tdata = tdata;
  11120. if (o->rawsize == 0)
  11121. o->rawsize = o->size;
  11122. o->size -= skip * PDR_SIZE;
  11123. ret = true;
  11124. }
  11125. else
  11126. free (tdata);
  11127. if (! info->keep_memory)
  11128. free (cookie->rels);
  11129. return ret;
  11130. }
  11131. bool
  11132. _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
  11133. {
  11134. if (strcmp (sec->name, ".pdr") == 0)
  11135. return true;
  11136. return false;
  11137. }
  11138. bool
  11139. _bfd_mips_elf_write_section (bfd *output_bfd,
  11140. struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
  11141. asection *sec, bfd_byte *contents)
  11142. {
  11143. bfd_byte *to, *from, *end;
  11144. int i;
  11145. if (strcmp (sec->name, ".pdr") != 0)
  11146. return false;
  11147. if (mips_elf_section_data (sec)->u.tdata == NULL)
  11148. return false;
  11149. to = contents;
  11150. end = contents + sec->size;
  11151. for (from = contents, i = 0;
  11152. from < end;
  11153. from += PDR_SIZE, i++)
  11154. {
  11155. if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
  11156. continue;
  11157. if (to != from)
  11158. memcpy (to, from, PDR_SIZE);
  11159. to += PDR_SIZE;
  11160. }
  11161. bfd_set_section_contents (output_bfd, sec->output_section, contents,
  11162. sec->output_offset, sec->size);
  11163. return true;
  11164. }
  11165. /* microMIPS code retains local labels for linker relaxation. Omit them
  11166. from output by default for clarity. */
  11167. bool
  11168. _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
  11169. {
  11170. return _bfd_elf_is_local_label_name (abfd, sym->name);
  11171. }
  11172. /* MIPS ELF uses a special find_nearest_line routine in order the
  11173. handle the ECOFF debugging information. */
  11174. struct mips_elf_find_line
  11175. {
  11176. struct ecoff_debug_info d;
  11177. struct ecoff_find_line i;
  11178. };
  11179. bool
  11180. _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
  11181. asection *section, bfd_vma offset,
  11182. const char **filename_ptr,
  11183. const char **functionname_ptr,
  11184. unsigned int *line_ptr,
  11185. unsigned int *discriminator_ptr)
  11186. {
  11187. asection *msec;
  11188. if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
  11189. filename_ptr, functionname_ptr,
  11190. line_ptr, discriminator_ptr,
  11191. dwarf_debug_sections,
  11192. &elf_tdata (abfd)->dwarf2_find_line_info)
  11193. == 1)
  11194. return true;
  11195. if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
  11196. filename_ptr, functionname_ptr,
  11197. line_ptr))
  11198. {
  11199. if (!*functionname_ptr)
  11200. _bfd_elf_find_function (abfd, symbols, section, offset,
  11201. *filename_ptr ? NULL : filename_ptr,
  11202. functionname_ptr);
  11203. return true;
  11204. }
  11205. msec = bfd_get_section_by_name (abfd, ".mdebug");
  11206. if (msec != NULL)
  11207. {
  11208. flagword origflags;
  11209. struct mips_elf_find_line *fi;
  11210. const struct ecoff_debug_swap * const swap =
  11211. get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  11212. /* If we are called during a link, mips_elf_final_link may have
  11213. cleared the SEC_HAS_CONTENTS field. We force it back on here
  11214. if appropriate (which it normally will be). */
  11215. origflags = msec->flags;
  11216. if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
  11217. msec->flags |= SEC_HAS_CONTENTS;
  11218. fi = mips_elf_tdata (abfd)->find_line_info;
  11219. if (fi == NULL)
  11220. {
  11221. bfd_size_type external_fdr_size;
  11222. char *fraw_src;
  11223. char *fraw_end;
  11224. struct fdr *fdr_ptr;
  11225. bfd_size_type amt = sizeof (struct mips_elf_find_line);
  11226. fi = bfd_zalloc (abfd, amt);
  11227. if (fi == NULL)
  11228. {
  11229. msec->flags = origflags;
  11230. return false;
  11231. }
  11232. if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
  11233. {
  11234. msec->flags = origflags;
  11235. return false;
  11236. }
  11237. /* Swap in the FDR information. */
  11238. amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
  11239. fi->d.fdr = bfd_alloc (abfd, amt);
  11240. if (fi->d.fdr == NULL)
  11241. {
  11242. msec->flags = origflags;
  11243. return false;
  11244. }
  11245. external_fdr_size = swap->external_fdr_size;
  11246. fdr_ptr = fi->d.fdr;
  11247. fraw_src = (char *) fi->d.external_fdr;
  11248. fraw_end = (fraw_src
  11249. + fi->d.symbolic_header.ifdMax * external_fdr_size);
  11250. for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
  11251. (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
  11252. mips_elf_tdata (abfd)->find_line_info = fi;
  11253. /* Note that we don't bother to ever free this information.
  11254. find_nearest_line is either called all the time, as in
  11255. objdump -l, so the information should be saved, or it is
  11256. rarely called, as in ld error messages, so the memory
  11257. wasted is unimportant. Still, it would probably be a
  11258. good idea for free_cached_info to throw it away. */
  11259. }
  11260. if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
  11261. &fi->i, filename_ptr, functionname_ptr,
  11262. line_ptr))
  11263. {
  11264. msec->flags = origflags;
  11265. return true;
  11266. }
  11267. msec->flags = origflags;
  11268. }
  11269. /* Fall back on the generic ELF find_nearest_line routine. */
  11270. return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
  11271. filename_ptr, functionname_ptr,
  11272. line_ptr, discriminator_ptr);
  11273. }
  11274. bool
  11275. _bfd_mips_elf_find_inliner_info (bfd *abfd,
  11276. const char **filename_ptr,
  11277. const char **functionname_ptr,
  11278. unsigned int *line_ptr)
  11279. {
  11280. bool found;
  11281. found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
  11282. functionname_ptr, line_ptr,
  11283. & elf_tdata (abfd)->dwarf2_find_line_info);
  11284. return found;
  11285. }
  11286. /* When are writing out the .options or .MIPS.options section,
  11287. remember the bytes we are writing out, so that we can install the
  11288. GP value in the section_processing routine. */
  11289. bool
  11290. _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
  11291. const void *location,
  11292. file_ptr offset, bfd_size_type count)
  11293. {
  11294. if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
  11295. {
  11296. bfd_byte *c;
  11297. if (elf_section_data (section) == NULL)
  11298. {
  11299. size_t amt = sizeof (struct bfd_elf_section_data);
  11300. section->used_by_bfd = bfd_zalloc (abfd, amt);
  11301. if (elf_section_data (section) == NULL)
  11302. return false;
  11303. }
  11304. c = mips_elf_section_data (section)->u.tdata;
  11305. if (c == NULL)
  11306. {
  11307. c = bfd_zalloc (abfd, section->size);
  11308. if (c == NULL)
  11309. return false;
  11310. mips_elf_section_data (section)->u.tdata = c;
  11311. }
  11312. memcpy (c + offset, location, count);
  11313. }
  11314. return _bfd_elf_set_section_contents (abfd, section, location, offset,
  11315. count);
  11316. }
  11317. /* This is almost identical to bfd_generic_get_... except that some
  11318. MIPS relocations need to be handled specially. Sigh. */
  11319. bfd_byte *
  11320. _bfd_elf_mips_get_relocated_section_contents
  11321. (bfd *abfd,
  11322. struct bfd_link_info *link_info,
  11323. struct bfd_link_order *link_order,
  11324. bfd_byte *data,
  11325. bool relocatable,
  11326. asymbol **symbols)
  11327. {
  11328. bfd *input_bfd = link_order->u.indirect.section->owner;
  11329. asection *input_section = link_order->u.indirect.section;
  11330. long reloc_size;
  11331. arelent **reloc_vector;
  11332. long reloc_count;
  11333. reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  11334. if (reloc_size < 0)
  11335. return NULL;
  11336. /* Read in the section. */
  11337. if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
  11338. return NULL;
  11339. if (data == NULL)
  11340. return NULL;
  11341. if (reloc_size == 0)
  11342. return data;
  11343. reloc_vector = (arelent **) bfd_malloc (reloc_size);
  11344. if (reloc_vector == NULL)
  11345. {
  11346. struct mips_hi16 **hip, *hi;
  11347. error_return:
  11348. /* If we are going to return an error, remove entries on
  11349. mips_hi16_list that point into this section's data. Data
  11350. will typically be freed on return from this function. */
  11351. hip = &mips_hi16_list;
  11352. while ((hi = *hip) != NULL)
  11353. {
  11354. if (hi->input_section == input_section)
  11355. {
  11356. *hip = hi->next;
  11357. free (hi);
  11358. }
  11359. else
  11360. hip = &hi->next;
  11361. }
  11362. data = NULL;
  11363. goto out;
  11364. }
  11365. reloc_count = bfd_canonicalize_reloc (input_bfd,
  11366. input_section,
  11367. reloc_vector,
  11368. symbols);
  11369. if (reloc_count < 0)
  11370. goto error_return;
  11371. if (reloc_count > 0)
  11372. {
  11373. arelent **parent;
  11374. /* for mips */
  11375. int gp_found;
  11376. bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
  11377. {
  11378. struct bfd_hash_entry *h;
  11379. struct bfd_link_hash_entry *lh;
  11380. /* Skip all this stuff if we aren't mixing formats. */
  11381. if (abfd && input_bfd
  11382. && abfd->xvec == input_bfd->xvec)
  11383. lh = 0;
  11384. else
  11385. {
  11386. h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
  11387. lh = (struct bfd_link_hash_entry *) h;
  11388. }
  11389. lookup:
  11390. if (lh)
  11391. {
  11392. switch (lh->type)
  11393. {
  11394. case bfd_link_hash_undefined:
  11395. case bfd_link_hash_undefweak:
  11396. case bfd_link_hash_common:
  11397. gp_found = 0;
  11398. break;
  11399. case bfd_link_hash_defined:
  11400. case bfd_link_hash_defweak:
  11401. gp_found = 1;
  11402. gp = lh->u.def.value;
  11403. break;
  11404. case bfd_link_hash_indirect:
  11405. case bfd_link_hash_warning:
  11406. lh = lh->u.i.link;
  11407. /* @@FIXME ignoring warning for now */
  11408. goto lookup;
  11409. case bfd_link_hash_new:
  11410. default:
  11411. abort ();
  11412. }
  11413. }
  11414. else
  11415. gp_found = 0;
  11416. }
  11417. /* end mips */
  11418. for (parent = reloc_vector; *parent != NULL; parent++)
  11419. {
  11420. char *error_message = NULL;
  11421. asymbol *symbol;
  11422. bfd_reloc_status_type r;
  11423. symbol = *(*parent)->sym_ptr_ptr;
  11424. /* PR ld/19628: A specially crafted input file
  11425. can result in a NULL symbol pointer here. */
  11426. if (symbol == NULL)
  11427. {
  11428. link_info->callbacks->einfo
  11429. /* xgettext:c-format */
  11430. (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
  11431. abfd, input_section, (* parent)->address);
  11432. goto error_return;
  11433. }
  11434. /* Zap reloc field when the symbol is from a discarded
  11435. section, ignoring any addend. Do the same when called
  11436. from bfd_simple_get_relocated_section_contents for
  11437. undefined symbols in debug sections. This is to keep
  11438. debug info reasonably sane, in particular so that
  11439. DW_FORM_ref_addr to another file's .debug_info isn't
  11440. confused with an offset into the current file's
  11441. .debug_info. */
  11442. if ((symbol->section != NULL && discarded_section (symbol->section))
  11443. || (symbol->section == bfd_und_section_ptr
  11444. && (input_section->flags & SEC_DEBUGGING) != 0
  11445. && link_info->input_bfds == link_info->output_bfd))
  11446. {
  11447. bfd_vma off;
  11448. static reloc_howto_type none_howto
  11449. = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
  11450. "unused", false, 0, 0, false);
  11451. off = ((*parent)->address
  11452. * bfd_octets_per_byte (input_bfd, input_section));
  11453. _bfd_clear_contents ((*parent)->howto, input_bfd,
  11454. input_section, data, off);
  11455. (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  11456. (*parent)->addend = 0;
  11457. (*parent)->howto = &none_howto;
  11458. r = bfd_reloc_ok;
  11459. }
  11460. /* Specific to MIPS: Deal with relocation types that require
  11461. knowing the gp of the output bfd. */
  11462. /* If we've managed to find the gp and have a special
  11463. function for the relocation then go ahead, else default
  11464. to the generic handling. */
  11465. else if (gp_found
  11466. && ((*parent)->howto->special_function
  11467. == _bfd_mips_elf32_gprel16_reloc))
  11468. r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
  11469. input_section, relocatable,
  11470. data, gp);
  11471. else
  11472. r = bfd_perform_relocation (input_bfd,
  11473. *parent,
  11474. data,
  11475. input_section,
  11476. relocatable ? abfd : NULL,
  11477. &error_message);
  11478. if (relocatable)
  11479. {
  11480. asection *os = input_section->output_section;
  11481. /* A partial link, so keep the relocs. */
  11482. os->orelocation[os->reloc_count] = *parent;
  11483. os->reloc_count++;
  11484. }
  11485. if (r != bfd_reloc_ok)
  11486. {
  11487. switch (r)
  11488. {
  11489. case bfd_reloc_undefined:
  11490. (*link_info->callbacks->undefined_symbol)
  11491. (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  11492. input_bfd, input_section, (*parent)->address, true);
  11493. break;
  11494. case bfd_reloc_dangerous:
  11495. BFD_ASSERT (error_message != NULL);
  11496. (*link_info->callbacks->reloc_dangerous)
  11497. (link_info, error_message,
  11498. input_bfd, input_section, (*parent)->address);
  11499. break;
  11500. case bfd_reloc_overflow:
  11501. (*link_info->callbacks->reloc_overflow)
  11502. (link_info, NULL,
  11503. bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  11504. (*parent)->howto->name, (*parent)->addend,
  11505. input_bfd, input_section, (*parent)->address);
  11506. break;
  11507. case bfd_reloc_outofrange:
  11508. /* PR ld/13730:
  11509. This error can result when processing some partially
  11510. complete binaries. Do not abort, but issue an error
  11511. message instead. */
  11512. link_info->callbacks->einfo
  11513. /* xgettext:c-format */
  11514. (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
  11515. abfd, input_section, * parent);
  11516. goto error_return;
  11517. case bfd_reloc_notsupported:
  11518. /* PR ld/17512
  11519. This error can result when processing a corrupt binary.
  11520. Do not abort. Issue an error message instead. */
  11521. link_info->callbacks->einfo
  11522. /* xgettext:c-format */
  11523. (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
  11524. abfd, input_section, * parent);
  11525. goto error_return;
  11526. default:
  11527. /* PR 17512; file: 90c2a92e.
  11528. Report unexpected results, without aborting. */
  11529. link_info->callbacks->einfo
  11530. /* xgettext:c-format */
  11531. (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
  11532. abfd, input_section, * parent, r);
  11533. break;
  11534. }
  11535. }
  11536. }
  11537. }
  11538. out:
  11539. free (reloc_vector);
  11540. return data;
  11541. }
  11542. static bool
  11543. mips_elf_relax_delete_bytes (bfd *abfd,
  11544. asection *sec, bfd_vma addr, int count)
  11545. {
  11546. Elf_Internal_Shdr *symtab_hdr;
  11547. unsigned int sec_shndx;
  11548. bfd_byte *contents;
  11549. Elf_Internal_Rela *irel, *irelend;
  11550. Elf_Internal_Sym *isym;
  11551. Elf_Internal_Sym *isymend;
  11552. struct elf_link_hash_entry **sym_hashes;
  11553. struct elf_link_hash_entry **end_hashes;
  11554. struct elf_link_hash_entry **start_hashes;
  11555. unsigned int symcount;
  11556. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  11557. contents = elf_section_data (sec)->this_hdr.contents;
  11558. irel = elf_section_data (sec)->relocs;
  11559. irelend = irel + sec->reloc_count;
  11560. /* Actually delete the bytes. */
  11561. memmove (contents + addr, contents + addr + count,
  11562. (size_t) (sec->size - addr - count));
  11563. sec->size -= count;
  11564. /* Adjust all the relocs. */
  11565. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  11566. {
  11567. /* Get the new reloc address. */
  11568. if (irel->r_offset > addr)
  11569. irel->r_offset -= count;
  11570. }
  11571. BFD_ASSERT (addr % 2 == 0);
  11572. BFD_ASSERT (count % 2 == 0);
  11573. /* Adjust the local symbols defined in this section. */
  11574. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11575. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  11576. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  11577. if (isym->st_shndx == sec_shndx && isym->st_value > addr)
  11578. isym->st_value -= count;
  11579. /* Now adjust the global symbols defined in this section. */
  11580. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  11581. - symtab_hdr->sh_info);
  11582. sym_hashes = start_hashes = elf_sym_hashes (abfd);
  11583. end_hashes = sym_hashes + symcount;
  11584. for (; sym_hashes < end_hashes; sym_hashes++)
  11585. {
  11586. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  11587. if ((sym_hash->root.type == bfd_link_hash_defined
  11588. || sym_hash->root.type == bfd_link_hash_defweak)
  11589. && sym_hash->root.u.def.section == sec)
  11590. {
  11591. bfd_vma value = sym_hash->root.u.def.value;
  11592. if (ELF_ST_IS_MICROMIPS (sym_hash->other))
  11593. value &= MINUS_TWO;
  11594. if (value > addr)
  11595. sym_hash->root.u.def.value -= count;
  11596. }
  11597. }
  11598. return true;
  11599. }
  11600. /* Opcodes needed for microMIPS relaxation as found in
  11601. opcodes/micromips-opc.c. */
  11602. struct opcode_descriptor {
  11603. unsigned long match;
  11604. unsigned long mask;
  11605. };
  11606. /* The $ra register aka $31. */
  11607. #define RA 31
  11608. /* 32-bit instruction format register fields. */
  11609. #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
  11610. #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
  11611. /* Check if a 5-bit register index can be abbreviated to 3 bits. */
  11612. #define OP16_VALID_REG(r) \
  11613. ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
  11614. /* 32-bit and 16-bit branches. */
  11615. static const struct opcode_descriptor b_insns_32[] = {
  11616. { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
  11617. { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
  11618. { 0, 0 } /* End marker for find_match(). */
  11619. };
  11620. static const struct opcode_descriptor bc_insn_32 =
  11621. { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
  11622. static const struct opcode_descriptor bz_insn_32 =
  11623. { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
  11624. static const struct opcode_descriptor bzal_insn_32 =
  11625. { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
  11626. static const struct opcode_descriptor beq_insn_32 =
  11627. { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
  11628. static const struct opcode_descriptor b_insn_16 =
  11629. { /* "b", "mD", */ 0xcc00, 0xfc00 };
  11630. static const struct opcode_descriptor bz_insn_16 =
  11631. { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
  11632. /* 32-bit and 16-bit branch EQ and NE zero. */
  11633. /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
  11634. eq and second the ne. This convention is used when replacing a
  11635. 32-bit BEQ/BNE with the 16-bit version. */
  11636. #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
  11637. static const struct opcode_descriptor bz_rs_insns_32[] = {
  11638. { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
  11639. { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
  11640. { 0, 0 } /* End marker for find_match(). */
  11641. };
  11642. static const struct opcode_descriptor bz_rt_insns_32[] = {
  11643. { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
  11644. { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
  11645. { 0, 0 } /* End marker for find_match(). */
  11646. };
  11647. static const struct opcode_descriptor bzc_insns_32[] = {
  11648. { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
  11649. { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
  11650. { 0, 0 } /* End marker for find_match(). */
  11651. };
  11652. static const struct opcode_descriptor bz_insns_16[] = {
  11653. { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
  11654. { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
  11655. { 0, 0 } /* End marker for find_match(). */
  11656. };
  11657. /* Switch between a 5-bit register index and its 3-bit shorthand. */
  11658. #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
  11659. #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
  11660. /* 32-bit instructions with a delay slot. */
  11661. static const struct opcode_descriptor jal_insn_32_bd16 =
  11662. { /* "jals", "a", */ 0x74000000, 0xfc000000 };
  11663. static const struct opcode_descriptor jal_insn_32_bd32 =
  11664. { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
  11665. static const struct opcode_descriptor jal_x_insn_32_bd32 =
  11666. { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
  11667. static const struct opcode_descriptor j_insn_32 =
  11668. { /* "j", "a", */ 0xd4000000, 0xfc000000 };
  11669. static const struct opcode_descriptor jalr_insn_32 =
  11670. { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
  11671. /* This table can be compacted, because no opcode replacement is made. */
  11672. static const struct opcode_descriptor ds_insns_32_bd16[] = {
  11673. { /* "jals", "a", */ 0x74000000, 0xfc000000 },
  11674. { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
  11675. { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
  11676. { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
  11677. { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
  11678. { /* "j", "a", */ 0xd4000000, 0xfc000000 },
  11679. { 0, 0 } /* End marker for find_match(). */
  11680. };
  11681. /* This table can be compacted, because no opcode replacement is made. */
  11682. static const struct opcode_descriptor ds_insns_32_bd32[] = {
  11683. { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
  11684. { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
  11685. { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
  11686. { 0, 0 } /* End marker for find_match(). */
  11687. };
  11688. /* 16-bit instructions with a delay slot. */
  11689. static const struct opcode_descriptor jalr_insn_16_bd16 =
  11690. { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
  11691. static const struct opcode_descriptor jalr_insn_16_bd32 =
  11692. { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
  11693. static const struct opcode_descriptor jr_insn_16 =
  11694. { /* "jr", "mj", */ 0x4580, 0xffe0 };
  11695. #define JR16_REG(opcode) ((opcode) & 0x1f)
  11696. /* This table can be compacted, because no opcode replacement is made. */
  11697. static const struct opcode_descriptor ds_insns_16_bd16[] = {
  11698. { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
  11699. { /* "b", "mD", */ 0xcc00, 0xfc00 },
  11700. { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
  11701. { /* "jr", "mj", */ 0x4580, 0xffe0 },
  11702. { 0, 0 } /* End marker for find_match(). */
  11703. };
  11704. /* LUI instruction. */
  11705. static const struct opcode_descriptor lui_insn =
  11706. { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
  11707. /* ADDIU instruction. */
  11708. static const struct opcode_descriptor addiu_insn =
  11709. { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
  11710. static const struct opcode_descriptor addiupc_insn =
  11711. { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
  11712. #define ADDIUPC_REG_FIELD(r) \
  11713. (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
  11714. /* Relaxable instructions in a JAL delay slot: MOVE. */
  11715. /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
  11716. (ADDU, OR) have rd in 15:11 and rs in 10:16. */
  11717. #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
  11718. #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
  11719. #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
  11720. #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
  11721. static const struct opcode_descriptor move_insns_32[] = {
  11722. { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
  11723. { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
  11724. { 0, 0 } /* End marker for find_match(). */
  11725. };
  11726. static const struct opcode_descriptor move_insn_16 =
  11727. { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
  11728. /* NOP instructions. */
  11729. static const struct opcode_descriptor nop_insn_32 =
  11730. { /* "nop", "", */ 0x00000000, 0xffffffff };
  11731. static const struct opcode_descriptor nop_insn_16 =
  11732. { /* "nop", "", */ 0x0c00, 0xffff };
  11733. /* Instruction match support. */
  11734. #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
  11735. static int
  11736. find_match (unsigned long opcode, const struct opcode_descriptor insn[])
  11737. {
  11738. unsigned long indx;
  11739. for (indx = 0; insn[indx].mask != 0; indx++)
  11740. if (MATCH (opcode, insn[indx]))
  11741. return indx;
  11742. return -1;
  11743. }
  11744. /* Branch and delay slot decoding support. */
  11745. /* If PTR points to what *might* be a 16-bit branch or jump, then
  11746. return the minimum length of its delay slot, otherwise return 0.
  11747. Non-zero results are not definitive as we might be checking against
  11748. the second half of another instruction. */
  11749. static int
  11750. check_br16_dslot (bfd *abfd, bfd_byte *ptr)
  11751. {
  11752. unsigned long opcode;
  11753. int bdsize;
  11754. opcode = bfd_get_16 (abfd, ptr);
  11755. if (MATCH (opcode, jalr_insn_16_bd32) != 0)
  11756. /* 16-bit branch/jump with a 32-bit delay slot. */
  11757. bdsize = 4;
  11758. else if (MATCH (opcode, jalr_insn_16_bd16) != 0
  11759. || find_match (opcode, ds_insns_16_bd16) >= 0)
  11760. /* 16-bit branch/jump with a 16-bit delay slot. */
  11761. bdsize = 2;
  11762. else
  11763. /* No delay slot. */
  11764. bdsize = 0;
  11765. return bdsize;
  11766. }
  11767. /* If PTR points to what *might* be a 32-bit branch or jump, then
  11768. return the minimum length of its delay slot, otherwise return 0.
  11769. Non-zero results are not definitive as we might be checking against
  11770. the second half of another instruction. */
  11771. static int
  11772. check_br32_dslot (bfd *abfd, bfd_byte *ptr)
  11773. {
  11774. unsigned long opcode;
  11775. int bdsize;
  11776. opcode = bfd_get_micromips_32 (abfd, ptr);
  11777. if (find_match (opcode, ds_insns_32_bd32) >= 0)
  11778. /* 32-bit branch/jump with a 32-bit delay slot. */
  11779. bdsize = 4;
  11780. else if (find_match (opcode, ds_insns_32_bd16) >= 0)
  11781. /* 32-bit branch/jump with a 16-bit delay slot. */
  11782. bdsize = 2;
  11783. else
  11784. /* No delay slot. */
  11785. bdsize = 0;
  11786. return bdsize;
  11787. }
  11788. /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
  11789. that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
  11790. static bool
  11791. check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
  11792. {
  11793. unsigned long opcode;
  11794. opcode = bfd_get_16 (abfd, ptr);
  11795. if (MATCH (opcode, b_insn_16)
  11796. /* B16 */
  11797. || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
  11798. /* JR16 */
  11799. || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
  11800. /* BEQZ16, BNEZ16 */
  11801. || (MATCH (opcode, jalr_insn_16_bd32)
  11802. /* JALR16 */
  11803. && reg != JR16_REG (opcode) && reg != RA))
  11804. return true;
  11805. return false;
  11806. }
  11807. /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
  11808. then return TRUE, otherwise FALSE. */
  11809. static bool
  11810. check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
  11811. {
  11812. unsigned long opcode;
  11813. opcode = bfd_get_micromips_32 (abfd, ptr);
  11814. if (MATCH (opcode, j_insn_32)
  11815. /* J */
  11816. || MATCH (opcode, bc_insn_32)
  11817. /* BC1F, BC1T, BC2F, BC2T */
  11818. || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
  11819. /* JAL, JALX */
  11820. || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
  11821. /* BGEZ, BGTZ, BLEZ, BLTZ */
  11822. || (MATCH (opcode, bzal_insn_32)
  11823. /* BGEZAL, BLTZAL */
  11824. && reg != OP32_SREG (opcode) && reg != RA)
  11825. || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
  11826. /* JALR, JALR.HB, BEQ, BNE */
  11827. && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
  11828. return true;
  11829. return false;
  11830. }
  11831. /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
  11832. IRELEND) at OFFSET indicate that there must be a compact branch there,
  11833. then return TRUE, otherwise FALSE. */
  11834. static bool
  11835. check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
  11836. const Elf_Internal_Rela *internal_relocs,
  11837. const Elf_Internal_Rela *irelend)
  11838. {
  11839. const Elf_Internal_Rela *irel;
  11840. unsigned long opcode;
  11841. opcode = bfd_get_micromips_32 (abfd, ptr);
  11842. if (find_match (opcode, bzc_insns_32) < 0)
  11843. return false;
  11844. for (irel = internal_relocs; irel < irelend; irel++)
  11845. if (irel->r_offset == offset
  11846. && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
  11847. return true;
  11848. return false;
  11849. }
  11850. /* Bitsize checking. */
  11851. #define IS_BITSIZE(val, N) \
  11852. (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
  11853. - (1ULL << ((N) - 1))) == (val))
  11854. bool
  11855. _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
  11856. struct bfd_link_info *link_info,
  11857. bool *again)
  11858. {
  11859. bool insn32 = mips_elf_hash_table (link_info)->insn32;
  11860. Elf_Internal_Shdr *symtab_hdr;
  11861. Elf_Internal_Rela *internal_relocs;
  11862. Elf_Internal_Rela *irel, *irelend;
  11863. bfd_byte *contents = NULL;
  11864. Elf_Internal_Sym *isymbuf = NULL;
  11865. /* Assume nothing changes. */
  11866. *again = false;
  11867. /* We don't have to do anything for a relocatable link, if
  11868. this section does not have relocs, or if this is not a
  11869. code section. */
  11870. if (bfd_link_relocatable (link_info)
  11871. || (sec->flags & SEC_RELOC) == 0
  11872. || sec->reloc_count == 0
  11873. || (sec->flags & SEC_CODE) == 0)
  11874. return true;
  11875. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11876. /* Get a copy of the native relocations. */
  11877. internal_relocs = (_bfd_elf_link_read_relocs
  11878. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  11879. link_info->keep_memory));
  11880. if (internal_relocs == NULL)
  11881. goto error_return;
  11882. /* Walk through them looking for relaxing opportunities. */
  11883. irelend = internal_relocs + sec->reloc_count;
  11884. for (irel = internal_relocs; irel < irelend; irel++)
  11885. {
  11886. unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
  11887. unsigned int r_type = ELF32_R_TYPE (irel->r_info);
  11888. bool target_is_micromips_code_p;
  11889. unsigned long opcode;
  11890. bfd_vma symval;
  11891. bfd_vma pcrval;
  11892. bfd_byte *ptr;
  11893. int fndopc;
  11894. /* The number of bytes to delete for relaxation and from where
  11895. to delete these bytes starting at irel->r_offset. */
  11896. int delcnt = 0;
  11897. int deloff = 0;
  11898. /* If this isn't something that can be relaxed, then ignore
  11899. this reloc. */
  11900. if (r_type != R_MICROMIPS_HI16
  11901. && r_type != R_MICROMIPS_PC16_S1
  11902. && r_type != R_MICROMIPS_26_S1)
  11903. continue;
  11904. /* Get the section contents if we haven't done so already. */
  11905. if (contents == NULL)
  11906. {
  11907. /* Get cached copy if it exists. */
  11908. if (elf_section_data (sec)->this_hdr.contents != NULL)
  11909. contents = elf_section_data (sec)->this_hdr.contents;
  11910. /* Go get them off disk. */
  11911. else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  11912. goto error_return;
  11913. }
  11914. ptr = contents + irel->r_offset;
  11915. /* Read this BFD's local symbols if we haven't done so already. */
  11916. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  11917. {
  11918. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  11919. if (isymbuf == NULL)
  11920. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  11921. symtab_hdr->sh_info, 0,
  11922. NULL, NULL, NULL);
  11923. if (isymbuf == NULL)
  11924. goto error_return;
  11925. }
  11926. /* Get the value of the symbol referred to by the reloc. */
  11927. if (r_symndx < symtab_hdr->sh_info)
  11928. {
  11929. /* A local symbol. */
  11930. Elf_Internal_Sym *isym;
  11931. asection *sym_sec;
  11932. isym = isymbuf + r_symndx;
  11933. if (isym->st_shndx == SHN_UNDEF)
  11934. sym_sec = bfd_und_section_ptr;
  11935. else if (isym->st_shndx == SHN_ABS)
  11936. sym_sec = bfd_abs_section_ptr;
  11937. else if (isym->st_shndx == SHN_COMMON)
  11938. sym_sec = bfd_com_section_ptr;
  11939. else
  11940. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  11941. symval = (isym->st_value
  11942. + sym_sec->output_section->vma
  11943. + sym_sec->output_offset);
  11944. target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
  11945. }
  11946. else
  11947. {
  11948. unsigned long indx;
  11949. struct elf_link_hash_entry *h;
  11950. /* An external symbol. */
  11951. indx = r_symndx - symtab_hdr->sh_info;
  11952. h = elf_sym_hashes (abfd)[indx];
  11953. BFD_ASSERT (h != NULL);
  11954. if (h->root.type != bfd_link_hash_defined
  11955. && h->root.type != bfd_link_hash_defweak)
  11956. /* This appears to be a reference to an undefined
  11957. symbol. Just ignore it -- it will be caught by the
  11958. regular reloc processing. */
  11959. continue;
  11960. symval = (h->root.u.def.value
  11961. + h->root.u.def.section->output_section->vma
  11962. + h->root.u.def.section->output_offset);
  11963. target_is_micromips_code_p = (!h->needs_plt
  11964. && ELF_ST_IS_MICROMIPS (h->other));
  11965. }
  11966. /* For simplicity of coding, we are going to modify the
  11967. section contents, the section relocs, and the BFD symbol
  11968. table. We must tell the rest of the code not to free up this
  11969. information. It would be possible to instead create a table
  11970. of changes which have to be made, as is done in coff-mips.c;
  11971. that would be more work, but would require less memory when
  11972. the linker is run. */
  11973. /* Only 32-bit instructions relaxed. */
  11974. if (irel->r_offset + 4 > sec->size)
  11975. continue;
  11976. opcode = bfd_get_micromips_32 (abfd, ptr);
  11977. /* This is the pc-relative distance from the instruction the
  11978. relocation is applied to, to the symbol referred. */
  11979. pcrval = (symval
  11980. - (sec->output_section->vma + sec->output_offset)
  11981. - irel->r_offset);
  11982. /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
  11983. of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
  11984. R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
  11985. (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
  11986. where pcrval has first to be adjusted to apply against the LO16
  11987. location (we make the adjustment later on, when we have figured
  11988. out the offset). */
  11989. if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
  11990. {
  11991. bool bzc = false;
  11992. unsigned long nextopc;
  11993. unsigned long reg;
  11994. bfd_vma offset;
  11995. /* Give up if the previous reloc was a HI16 against this symbol
  11996. too. */
  11997. if (irel > internal_relocs
  11998. && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
  11999. && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
  12000. continue;
  12001. /* Or if the next reloc is not a LO16 against this symbol. */
  12002. if (irel + 1 >= irelend
  12003. || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
  12004. || ELF32_R_SYM (irel[1].r_info) != r_symndx)
  12005. continue;
  12006. /* Or if the second next reloc is a LO16 against this symbol too. */
  12007. if (irel + 2 >= irelend
  12008. && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
  12009. && ELF32_R_SYM (irel[2].r_info) == r_symndx)
  12010. continue;
  12011. /* See if the LUI instruction *might* be in a branch delay slot.
  12012. We check whether what looks like a 16-bit branch or jump is
  12013. actually an immediate argument to a compact branch, and let
  12014. it through if so. */
  12015. if (irel->r_offset >= 2
  12016. && check_br16_dslot (abfd, ptr - 2)
  12017. && !(irel->r_offset >= 4
  12018. && (bzc = check_relocated_bzc (abfd,
  12019. ptr - 4, irel->r_offset - 4,
  12020. internal_relocs, irelend))))
  12021. continue;
  12022. if (irel->r_offset >= 4
  12023. && !bzc
  12024. && check_br32_dslot (abfd, ptr - 4))
  12025. continue;
  12026. reg = OP32_SREG (opcode);
  12027. /* We only relax adjacent instructions or ones separated with
  12028. a branch or jump that has a delay slot. The branch or jump
  12029. must not fiddle with the register used to hold the address.
  12030. Subtract 4 for the LUI itself. */
  12031. offset = irel[1].r_offset - irel[0].r_offset;
  12032. switch (offset - 4)
  12033. {
  12034. case 0:
  12035. break;
  12036. case 2:
  12037. if (check_br16 (abfd, ptr + 4, reg))
  12038. break;
  12039. continue;
  12040. case 4:
  12041. if (check_br32 (abfd, ptr + 4, reg))
  12042. break;
  12043. continue;
  12044. default:
  12045. continue;
  12046. }
  12047. nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
  12048. /* Give up unless the same register is used with both
  12049. relocations. */
  12050. if (OP32_SREG (nextopc) != reg)
  12051. continue;
  12052. /* Now adjust pcrval, subtracting the offset to the LO16 reloc
  12053. and rounding up to take masking of the two LSBs into account. */
  12054. pcrval = ((pcrval - offset + 3) | 3) ^ 3;
  12055. /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
  12056. if (IS_BITSIZE (symval, 16))
  12057. {
  12058. /* Fix the relocation's type. */
  12059. irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
  12060. /* Instructions using R_MICROMIPS_LO16 have the base or
  12061. source register in bits 20:16. This register becomes $0
  12062. (zero) as the result of the R_MICROMIPS_HI16 being 0. */
  12063. nextopc &= ~0x001f0000;
  12064. bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
  12065. contents + irel[1].r_offset);
  12066. }
  12067. /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
  12068. We add 4 to take LUI deletion into account while checking
  12069. the PC-relative distance. */
  12070. else if (symval % 4 == 0
  12071. && IS_BITSIZE (pcrval + 4, 25)
  12072. && MATCH (nextopc, addiu_insn)
  12073. && OP32_TREG (nextopc) == OP32_SREG (nextopc)
  12074. && OP16_VALID_REG (OP32_TREG (nextopc)))
  12075. {
  12076. /* Fix the relocation's type. */
  12077. irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
  12078. /* Replace ADDIU with the ADDIUPC version. */
  12079. nextopc = (addiupc_insn.match
  12080. | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
  12081. bfd_put_micromips_32 (abfd, nextopc,
  12082. contents + irel[1].r_offset);
  12083. }
  12084. /* Can't do anything, give up, sigh... */
  12085. else
  12086. continue;
  12087. /* Fix the relocation's type. */
  12088. irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
  12089. /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
  12090. delcnt = 4;
  12091. deloff = 0;
  12092. }
  12093. /* Compact branch relaxation -- due to the multitude of macros
  12094. employed by the compiler/assembler, compact branches are not
  12095. always generated. Obviously, this can/will be fixed elsewhere,
  12096. but there is no drawback in double checking it here. */
  12097. else if (r_type == R_MICROMIPS_PC16_S1
  12098. && irel->r_offset + 5 < sec->size
  12099. && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
  12100. || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
  12101. && ((!insn32
  12102. && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
  12103. nop_insn_16) ? 2 : 0))
  12104. || (irel->r_offset + 7 < sec->size
  12105. && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
  12106. ptr + 4),
  12107. nop_insn_32) ? 4 : 0))))
  12108. {
  12109. unsigned long reg;
  12110. reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
  12111. /* Replace BEQZ/BNEZ with the compact version. */
  12112. opcode = (bzc_insns_32[fndopc].match
  12113. | BZC32_REG_FIELD (reg)
  12114. | (opcode & 0xffff)); /* Addend value. */
  12115. bfd_put_micromips_32 (abfd, opcode, ptr);
  12116. /* Delete the delay slot NOP: two or four bytes from
  12117. irel->offset + 4; delcnt has already been set above. */
  12118. deloff = 4;
  12119. }
  12120. /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
  12121. to check the distance from the next instruction, so subtract 2. */
  12122. else if (!insn32
  12123. && r_type == R_MICROMIPS_PC16_S1
  12124. && IS_BITSIZE (pcrval - 2, 11)
  12125. && find_match (opcode, b_insns_32) >= 0)
  12126. {
  12127. /* Fix the relocation's type. */
  12128. irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
  12129. /* Replace the 32-bit opcode with a 16-bit opcode. */
  12130. bfd_put_16 (abfd,
  12131. (b_insn_16.match
  12132. | (opcode & 0x3ff)), /* Addend value. */
  12133. ptr);
  12134. /* Delete 2 bytes from irel->r_offset + 2. */
  12135. delcnt = 2;
  12136. deloff = 2;
  12137. }
  12138. /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
  12139. to check the distance from the next instruction, so subtract 2. */
  12140. else if (!insn32
  12141. && r_type == R_MICROMIPS_PC16_S1
  12142. && IS_BITSIZE (pcrval - 2, 8)
  12143. && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
  12144. && OP16_VALID_REG (OP32_SREG (opcode)))
  12145. || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
  12146. && OP16_VALID_REG (OP32_TREG (opcode)))))
  12147. {
  12148. unsigned long reg;
  12149. reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
  12150. /* Fix the relocation's type. */
  12151. irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
  12152. /* Replace the 32-bit opcode with a 16-bit opcode. */
  12153. bfd_put_16 (abfd,
  12154. (bz_insns_16[fndopc].match
  12155. | BZ16_REG_FIELD (reg)
  12156. | (opcode & 0x7f)), /* Addend value. */
  12157. ptr);
  12158. /* Delete 2 bytes from irel->r_offset + 2. */
  12159. delcnt = 2;
  12160. deloff = 2;
  12161. }
  12162. /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
  12163. else if (!insn32
  12164. && r_type == R_MICROMIPS_26_S1
  12165. && target_is_micromips_code_p
  12166. && irel->r_offset + 7 < sec->size
  12167. && MATCH (opcode, jal_insn_32_bd32))
  12168. {
  12169. unsigned long n32opc;
  12170. bool relaxed = false;
  12171. n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
  12172. if (MATCH (n32opc, nop_insn_32))
  12173. {
  12174. /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
  12175. bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
  12176. relaxed = true;
  12177. }
  12178. else if (find_match (n32opc, move_insns_32) >= 0)
  12179. {
  12180. /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
  12181. bfd_put_16 (abfd,
  12182. (move_insn_16.match
  12183. | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
  12184. | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
  12185. ptr + 4);
  12186. relaxed = true;
  12187. }
  12188. /* Other 32-bit instructions relaxable to 16-bit
  12189. instructions will be handled here later. */
  12190. if (relaxed)
  12191. {
  12192. /* JAL with 32-bit delay slot that is changed to a JALS
  12193. with 16-bit delay slot. */
  12194. bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
  12195. /* Delete 2 bytes from irel->r_offset + 6. */
  12196. delcnt = 2;
  12197. deloff = 6;
  12198. }
  12199. }
  12200. if (delcnt != 0)
  12201. {
  12202. /* Note that we've changed the relocs, section contents, etc. */
  12203. elf_section_data (sec)->relocs = internal_relocs;
  12204. elf_section_data (sec)->this_hdr.contents = contents;
  12205. symtab_hdr->contents = (unsigned char *) isymbuf;
  12206. /* Delete bytes depending on the delcnt and deloff. */
  12207. if (!mips_elf_relax_delete_bytes (abfd, sec,
  12208. irel->r_offset + deloff, delcnt))
  12209. goto error_return;
  12210. /* That will change things, so we should relax again.
  12211. Note that this is not required, and it may be slow. */
  12212. *again = true;
  12213. }
  12214. }
  12215. if (isymbuf != NULL
  12216. && symtab_hdr->contents != (unsigned char *) isymbuf)
  12217. {
  12218. if (! link_info->keep_memory)
  12219. free (isymbuf);
  12220. else
  12221. {
  12222. /* Cache the symbols for elf_link_input_bfd. */
  12223. symtab_hdr->contents = (unsigned char *) isymbuf;
  12224. }
  12225. }
  12226. if (contents != NULL
  12227. && elf_section_data (sec)->this_hdr.contents != contents)
  12228. {
  12229. if (! link_info->keep_memory)
  12230. free (contents);
  12231. else
  12232. {
  12233. /* Cache the section contents for elf_link_input_bfd. */
  12234. elf_section_data (sec)->this_hdr.contents = contents;
  12235. }
  12236. }
  12237. if (elf_section_data (sec)->relocs != internal_relocs)
  12238. free (internal_relocs);
  12239. return true;
  12240. error_return:
  12241. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  12242. free (isymbuf);
  12243. if (elf_section_data (sec)->this_hdr.contents != contents)
  12244. free (contents);
  12245. if (elf_section_data (sec)->relocs != internal_relocs)
  12246. free (internal_relocs);
  12247. return false;
  12248. }
  12249. /* Create a MIPS ELF linker hash table. */
  12250. struct bfd_link_hash_table *
  12251. _bfd_mips_elf_link_hash_table_create (bfd *abfd)
  12252. {
  12253. struct mips_elf_link_hash_table *ret;
  12254. size_t amt = sizeof (struct mips_elf_link_hash_table);
  12255. ret = bfd_zmalloc (amt);
  12256. if (ret == NULL)
  12257. return NULL;
  12258. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  12259. mips_elf_link_hash_newfunc,
  12260. sizeof (struct mips_elf_link_hash_entry),
  12261. MIPS_ELF_DATA))
  12262. {
  12263. free (ret);
  12264. return NULL;
  12265. }
  12266. ret->root.init_plt_refcount.plist = NULL;
  12267. ret->root.init_plt_offset.plist = NULL;
  12268. return &ret->root.root;
  12269. }
  12270. /* Likewise, but indicate that the target is VxWorks. */
  12271. struct bfd_link_hash_table *
  12272. _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
  12273. {
  12274. struct bfd_link_hash_table *ret;
  12275. ret = _bfd_mips_elf_link_hash_table_create (abfd);
  12276. if (ret)
  12277. {
  12278. struct mips_elf_link_hash_table *htab;
  12279. htab = (struct mips_elf_link_hash_table *) ret;
  12280. htab->use_plts_and_copy_relocs = true;
  12281. }
  12282. return ret;
  12283. }
  12284. /* A function that the linker calls if we are allowed to use PLTs
  12285. and copy relocs. */
  12286. void
  12287. _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
  12288. {
  12289. mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
  12290. }
  12291. /* A function that the linker calls to select between all or only
  12292. 32-bit microMIPS instructions, and between making or ignoring
  12293. branch relocation checks for invalid transitions between ISA modes.
  12294. Also record whether we have been configured for a GNU target. */
  12295. void
  12296. _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
  12297. bool ignore_branch_isa,
  12298. bool gnu_target)
  12299. {
  12300. mips_elf_hash_table (info)->insn32 = insn32;
  12301. mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
  12302. mips_elf_hash_table (info)->gnu_target = gnu_target;
  12303. }
  12304. /* A function that the linker calls to enable use of compact branches in
  12305. linker generated code for MIPSR6. */
  12306. void
  12307. _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
  12308. {
  12309. mips_elf_hash_table (info)->compact_branches = on;
  12310. }
  12311. /* Structure for saying that BFD machine EXTENSION extends BASE. */
  12312. struct mips_mach_extension
  12313. {
  12314. unsigned long extension, base;
  12315. };
  12316. /* An array describing how BFD machines relate to one another. The entries
  12317. are ordered topologically with MIPS I extensions listed last. */
  12318. static const struct mips_mach_extension mips_mach_extensions[] =
  12319. {
  12320. /* MIPS64r2 extensions. */
  12321. { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
  12322. { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
  12323. { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
  12324. { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
  12325. { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
  12326. { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
  12327. { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
  12328. /* MIPS64 extensions. */
  12329. { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
  12330. { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
  12331. { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
  12332. /* MIPS V extensions. */
  12333. { bfd_mach_mipsisa64, bfd_mach_mips5 },
  12334. /* R10000 extensions. */
  12335. { bfd_mach_mips12000, bfd_mach_mips10000 },
  12336. { bfd_mach_mips14000, bfd_mach_mips10000 },
  12337. { bfd_mach_mips16000, bfd_mach_mips10000 },
  12338. /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
  12339. vr5400 ISA, but doesn't include the multimedia stuff. It seems
  12340. better to allow vr5400 and vr5500 code to be merged anyway, since
  12341. many libraries will just use the core ISA. Perhaps we could add
  12342. some sort of ASE flag if this ever proves a problem. */
  12343. { bfd_mach_mips5500, bfd_mach_mips5400 },
  12344. { bfd_mach_mips5400, bfd_mach_mips5000 },
  12345. /* MIPS IV extensions. */
  12346. { bfd_mach_mips5, bfd_mach_mips8000 },
  12347. { bfd_mach_mips10000, bfd_mach_mips8000 },
  12348. { bfd_mach_mips5000, bfd_mach_mips8000 },
  12349. { bfd_mach_mips7000, bfd_mach_mips8000 },
  12350. { bfd_mach_mips9000, bfd_mach_mips8000 },
  12351. /* VR4100 extensions. */
  12352. { bfd_mach_mips4120, bfd_mach_mips4100 },
  12353. { bfd_mach_mips4111, bfd_mach_mips4100 },
  12354. /* MIPS III extensions. */
  12355. { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
  12356. { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
  12357. { bfd_mach_mips8000, bfd_mach_mips4000 },
  12358. { bfd_mach_mips4650, bfd_mach_mips4000 },
  12359. { bfd_mach_mips4600, bfd_mach_mips4000 },
  12360. { bfd_mach_mips4400, bfd_mach_mips4000 },
  12361. { bfd_mach_mips4300, bfd_mach_mips4000 },
  12362. { bfd_mach_mips4100, bfd_mach_mips4000 },
  12363. { bfd_mach_mips5900, bfd_mach_mips4000 },
  12364. /* MIPS32r3 extensions. */
  12365. { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
  12366. /* MIPS32r2 extensions. */
  12367. { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
  12368. /* MIPS32 extensions. */
  12369. { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
  12370. /* MIPS II extensions. */
  12371. { bfd_mach_mips4000, bfd_mach_mips6000 },
  12372. { bfd_mach_mipsisa32, bfd_mach_mips6000 },
  12373. { bfd_mach_mips4010, bfd_mach_mips6000 },
  12374. /* MIPS I extensions. */
  12375. { bfd_mach_mips6000, bfd_mach_mips3000 },
  12376. { bfd_mach_mips3900, bfd_mach_mips3000 }
  12377. };
  12378. /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
  12379. static bool
  12380. mips_mach_extends_p (unsigned long base, unsigned long extension)
  12381. {
  12382. size_t i;
  12383. if (extension == base)
  12384. return true;
  12385. if (base == bfd_mach_mipsisa32
  12386. && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
  12387. return true;
  12388. if (base == bfd_mach_mipsisa32r2
  12389. && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
  12390. return true;
  12391. for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
  12392. if (extension == mips_mach_extensions[i].extension)
  12393. {
  12394. extension = mips_mach_extensions[i].base;
  12395. if (extension == base)
  12396. return true;
  12397. }
  12398. return false;
  12399. }
  12400. /* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
  12401. static unsigned long
  12402. bfd_mips_isa_ext_mach (unsigned int isa_ext)
  12403. {
  12404. switch (isa_ext)
  12405. {
  12406. case AFL_EXT_3900: return bfd_mach_mips3900;
  12407. case AFL_EXT_4010: return bfd_mach_mips4010;
  12408. case AFL_EXT_4100: return bfd_mach_mips4100;
  12409. case AFL_EXT_4111: return bfd_mach_mips4111;
  12410. case AFL_EXT_4120: return bfd_mach_mips4120;
  12411. case AFL_EXT_4650: return bfd_mach_mips4650;
  12412. case AFL_EXT_5400: return bfd_mach_mips5400;
  12413. case AFL_EXT_5500: return bfd_mach_mips5500;
  12414. case AFL_EXT_5900: return bfd_mach_mips5900;
  12415. case AFL_EXT_10000: return bfd_mach_mips10000;
  12416. case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
  12417. case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
  12418. case AFL_EXT_SB1: return bfd_mach_mips_sb1;
  12419. case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
  12420. case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
  12421. case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
  12422. case AFL_EXT_XLR: return bfd_mach_mips_xlr;
  12423. default: return bfd_mach_mips3000;
  12424. }
  12425. }
  12426. /* Return the .MIPS.abiflags value representing each ISA Extension. */
  12427. unsigned int
  12428. bfd_mips_isa_ext (bfd *abfd)
  12429. {
  12430. switch (bfd_get_mach (abfd))
  12431. {
  12432. case bfd_mach_mips3900: return AFL_EXT_3900;
  12433. case bfd_mach_mips4010: return AFL_EXT_4010;
  12434. case bfd_mach_mips4100: return AFL_EXT_4100;
  12435. case bfd_mach_mips4111: return AFL_EXT_4111;
  12436. case bfd_mach_mips4120: return AFL_EXT_4120;
  12437. case bfd_mach_mips4650: return AFL_EXT_4650;
  12438. case bfd_mach_mips5400: return AFL_EXT_5400;
  12439. case bfd_mach_mips5500: return AFL_EXT_5500;
  12440. case bfd_mach_mips5900: return AFL_EXT_5900;
  12441. case bfd_mach_mips10000: return AFL_EXT_10000;
  12442. case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
  12443. case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
  12444. case bfd_mach_mips_sb1: return AFL_EXT_SB1;
  12445. case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
  12446. case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
  12447. case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
  12448. case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
  12449. case bfd_mach_mips_xlr: return AFL_EXT_XLR;
  12450. case bfd_mach_mips_interaptiv_mr2:
  12451. return AFL_EXT_INTERAPTIV_MR2;
  12452. default: return 0;
  12453. }
  12454. }
  12455. /* Encode ISA level and revision as a single value. */
  12456. #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
  12457. /* Decode a single value into level and revision. */
  12458. #define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
  12459. #define ISA_REV(LEVREV) ((LEVREV) & 0x7)
  12460. /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
  12461. static void
  12462. update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
  12463. {
  12464. int new_isa = 0;
  12465. switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
  12466. {
  12467. case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
  12468. case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
  12469. case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
  12470. case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
  12471. case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
  12472. case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
  12473. case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
  12474. case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
  12475. case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
  12476. case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
  12477. case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
  12478. default:
  12479. _bfd_error_handler
  12480. /* xgettext:c-format */
  12481. (_("%pB: unknown architecture %s"),
  12482. abfd, bfd_printable_name (abfd));
  12483. }
  12484. if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
  12485. {
  12486. abiflags->isa_level = ISA_LEVEL (new_isa);
  12487. abiflags->isa_rev = ISA_REV (new_isa);
  12488. }
  12489. /* Update the isa_ext if ABFD describes a further extension. */
  12490. if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
  12491. bfd_get_mach (abfd)))
  12492. abiflags->isa_ext = bfd_mips_isa_ext (abfd);
  12493. }
  12494. /* Return true if the given ELF header flags describe a 32-bit binary. */
  12495. static bool
  12496. mips_32bit_flags_p (flagword flags)
  12497. {
  12498. return ((flags & EF_MIPS_32BITMODE) != 0
  12499. || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
  12500. || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
  12501. || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
  12502. || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
  12503. || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
  12504. || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
  12505. || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
  12506. }
  12507. /* Infer the content of the ABI flags based on the elf header. */
  12508. static void
  12509. infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
  12510. {
  12511. obj_attribute *in_attr;
  12512. memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
  12513. update_mips_abiflags_isa (abfd, abiflags);
  12514. if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
  12515. abiflags->gpr_size = AFL_REG_32;
  12516. else
  12517. abiflags->gpr_size = AFL_REG_64;
  12518. abiflags->cpr1_size = AFL_REG_NONE;
  12519. in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
  12520. abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
  12521. if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
  12522. || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
  12523. || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
  12524. && abiflags->gpr_size == AFL_REG_32))
  12525. abiflags->cpr1_size = AFL_REG_32;
  12526. else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
  12527. || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
  12528. || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
  12529. abiflags->cpr1_size = AFL_REG_64;
  12530. abiflags->cpr2_size = AFL_REG_NONE;
  12531. if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
  12532. abiflags->ases |= AFL_ASE_MDMX;
  12533. if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
  12534. abiflags->ases |= AFL_ASE_MIPS16;
  12535. if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
  12536. abiflags->ases |= AFL_ASE_MICROMIPS;
  12537. if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
  12538. && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
  12539. && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
  12540. && abiflags->isa_level >= 32
  12541. && abiflags->ases != AFL_ASE_LOONGSON_EXT)
  12542. abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
  12543. }
  12544. /* We need to use a special link routine to handle the .reginfo and
  12545. the .mdebug sections. We need to merge all instances of these
  12546. sections together, not write them all out sequentially. */
  12547. bool
  12548. _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
  12549. {
  12550. asection *o;
  12551. struct bfd_link_order *p;
  12552. asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
  12553. asection *rtproc_sec, *abiflags_sec;
  12554. Elf32_RegInfo reginfo;
  12555. struct ecoff_debug_info debug;
  12556. struct mips_htab_traverse_info hti;
  12557. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12558. const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
  12559. HDRR *symhdr = &debug.symbolic_header;
  12560. void *mdebug_handle = NULL;
  12561. asection *s;
  12562. EXTR esym;
  12563. unsigned int i;
  12564. bfd_size_type amt;
  12565. struct mips_elf_link_hash_table *htab;
  12566. static const char * const secname[] =
  12567. {
  12568. ".text", ".init", ".fini", ".data",
  12569. ".rodata", ".sdata", ".sbss", ".bss"
  12570. };
  12571. static const int sc[] =
  12572. {
  12573. scText, scInit, scFini, scData,
  12574. scRData, scSData, scSBss, scBss
  12575. };
  12576. htab = mips_elf_hash_table (info);
  12577. BFD_ASSERT (htab != NULL);
  12578. /* Sort the dynamic symbols so that those with GOT entries come after
  12579. those without. */
  12580. if (!mips_elf_sort_hash_table (abfd, info))
  12581. return false;
  12582. /* Create any scheduled LA25 stubs. */
  12583. hti.info = info;
  12584. hti.output_bfd = abfd;
  12585. hti.error = false;
  12586. htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
  12587. if (hti.error)
  12588. return false;
  12589. /* Get a value for the GP register. */
  12590. if (elf_gp (abfd) == 0)
  12591. {
  12592. struct bfd_link_hash_entry *h;
  12593. h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
  12594. if (h != NULL && h->type == bfd_link_hash_defined)
  12595. elf_gp (abfd) = (h->u.def.value
  12596. + h->u.def.section->output_section->vma
  12597. + h->u.def.section->output_offset);
  12598. else if (htab->root.target_os == is_vxworks
  12599. && (h = bfd_link_hash_lookup (info->hash,
  12600. "_GLOBAL_OFFSET_TABLE_",
  12601. false, false, true))
  12602. && h->type == bfd_link_hash_defined)
  12603. elf_gp (abfd) = (h->u.def.section->output_section->vma
  12604. + h->u.def.section->output_offset
  12605. + h->u.def.value);
  12606. else if (bfd_link_relocatable (info))
  12607. {
  12608. bfd_vma lo = MINUS_ONE;
  12609. /* Find the GP-relative section with the lowest offset. */
  12610. for (o = abfd->sections; o != NULL; o = o->next)
  12611. if (o->vma < lo
  12612. && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
  12613. lo = o->vma;
  12614. /* And calculate GP relative to that. */
  12615. elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
  12616. }
  12617. else
  12618. {
  12619. /* If the relocate_section function needs to do a reloc
  12620. involving the GP value, it should make a reloc_dangerous
  12621. callback to warn that GP is not defined. */
  12622. }
  12623. }
  12624. /* Go through the sections and collect the .reginfo and .mdebug
  12625. information. */
  12626. abiflags_sec = NULL;
  12627. reginfo_sec = NULL;
  12628. mdebug_sec = NULL;
  12629. gptab_data_sec = NULL;
  12630. gptab_bss_sec = NULL;
  12631. for (o = abfd->sections; o != NULL; o = o->next)
  12632. {
  12633. if (strcmp (o->name, ".MIPS.abiflags") == 0)
  12634. {
  12635. /* We have found the .MIPS.abiflags section in the output file.
  12636. Look through all the link_orders comprising it and remove them.
  12637. The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
  12638. for (p = o->map_head.link_order; p != NULL; p = p->next)
  12639. {
  12640. asection *input_section;
  12641. if (p->type != bfd_indirect_link_order)
  12642. {
  12643. if (p->type == bfd_data_link_order)
  12644. continue;
  12645. abort ();
  12646. }
  12647. input_section = p->u.indirect.section;
  12648. /* Hack: reset the SEC_HAS_CONTENTS flag so that
  12649. elf_link_input_bfd ignores this section. */
  12650. input_section->flags &= ~SEC_HAS_CONTENTS;
  12651. }
  12652. /* Size has been set in _bfd_mips_elf_always_size_sections. */
  12653. BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
  12654. /* Skip this section later on (I don't think this currently
  12655. matters, but someday it might). */
  12656. o->map_head.link_order = NULL;
  12657. abiflags_sec = o;
  12658. }
  12659. if (strcmp (o->name, ".reginfo") == 0)
  12660. {
  12661. memset (&reginfo, 0, sizeof reginfo);
  12662. /* We have found the .reginfo section in the output file.
  12663. Look through all the link_orders comprising it and merge
  12664. the information together. */
  12665. for (p = o->map_head.link_order; p != NULL; p = p->next)
  12666. {
  12667. asection *input_section;
  12668. bfd *input_bfd;
  12669. Elf32_External_RegInfo ext;
  12670. Elf32_RegInfo sub;
  12671. bfd_size_type sz;
  12672. if (p->type != bfd_indirect_link_order)
  12673. {
  12674. if (p->type == bfd_data_link_order)
  12675. continue;
  12676. abort ();
  12677. }
  12678. input_section = p->u.indirect.section;
  12679. input_bfd = input_section->owner;
  12680. sz = (input_section->size < sizeof (ext)
  12681. ? input_section->size : sizeof (ext));
  12682. memset (&ext, 0, sizeof (ext));
  12683. if (! bfd_get_section_contents (input_bfd, input_section,
  12684. &ext, 0, sz))
  12685. return false;
  12686. bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
  12687. reginfo.ri_gprmask |= sub.ri_gprmask;
  12688. reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
  12689. reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
  12690. reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
  12691. reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
  12692. /* ri_gp_value is set by the function
  12693. `_bfd_mips_elf_section_processing' when the section is
  12694. finally written out. */
  12695. /* Hack: reset the SEC_HAS_CONTENTS flag so that
  12696. elf_link_input_bfd ignores this section. */
  12697. input_section->flags &= ~SEC_HAS_CONTENTS;
  12698. }
  12699. /* Size has been set in _bfd_mips_elf_always_size_sections. */
  12700. BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
  12701. /* Skip this section later on (I don't think this currently
  12702. matters, but someday it might). */
  12703. o->map_head.link_order = NULL;
  12704. reginfo_sec = o;
  12705. }
  12706. if (strcmp (o->name, ".mdebug") == 0)
  12707. {
  12708. struct extsym_info einfo;
  12709. bfd_vma last;
  12710. /* We have found the .mdebug section in the output file.
  12711. Look through all the link_orders comprising it and merge
  12712. the information together. */
  12713. symhdr->magic = swap->sym_magic;
  12714. /* FIXME: What should the version stamp be? */
  12715. symhdr->vstamp = 0;
  12716. symhdr->ilineMax = 0;
  12717. symhdr->cbLine = 0;
  12718. symhdr->idnMax = 0;
  12719. symhdr->ipdMax = 0;
  12720. symhdr->isymMax = 0;
  12721. symhdr->ioptMax = 0;
  12722. symhdr->iauxMax = 0;
  12723. symhdr->issMax = 0;
  12724. symhdr->issExtMax = 0;
  12725. symhdr->ifdMax = 0;
  12726. symhdr->crfd = 0;
  12727. symhdr->iextMax = 0;
  12728. /* We accumulate the debugging information itself in the
  12729. debug_info structure. */
  12730. debug.line = NULL;
  12731. debug.external_dnr = NULL;
  12732. debug.external_pdr = NULL;
  12733. debug.external_sym = NULL;
  12734. debug.external_opt = NULL;
  12735. debug.external_aux = NULL;
  12736. debug.ss = NULL;
  12737. debug.ssext = debug.ssext_end = NULL;
  12738. debug.external_fdr = NULL;
  12739. debug.external_rfd = NULL;
  12740. debug.external_ext = debug.external_ext_end = NULL;
  12741. mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
  12742. if (mdebug_handle == NULL)
  12743. return false;
  12744. esym.jmptbl = 0;
  12745. esym.cobol_main = 0;
  12746. esym.weakext = 0;
  12747. esym.reserved = 0;
  12748. esym.ifd = ifdNil;
  12749. esym.asym.iss = issNil;
  12750. esym.asym.st = stLocal;
  12751. esym.asym.reserved = 0;
  12752. esym.asym.index = indexNil;
  12753. last = 0;
  12754. for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
  12755. {
  12756. esym.asym.sc = sc[i];
  12757. s = bfd_get_section_by_name (abfd, secname[i]);
  12758. if (s != NULL)
  12759. {
  12760. esym.asym.value = s->vma;
  12761. last = s->vma + s->size;
  12762. }
  12763. else
  12764. esym.asym.value = last;
  12765. if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
  12766. secname[i], &esym))
  12767. return false;
  12768. }
  12769. for (p = o->map_head.link_order; p != NULL; p = p->next)
  12770. {
  12771. asection *input_section;
  12772. bfd *input_bfd;
  12773. const struct ecoff_debug_swap *input_swap;
  12774. struct ecoff_debug_info input_debug;
  12775. char *eraw_src;
  12776. char *eraw_end;
  12777. if (p->type != bfd_indirect_link_order)
  12778. {
  12779. if (p->type == bfd_data_link_order)
  12780. continue;
  12781. abort ();
  12782. }
  12783. input_section = p->u.indirect.section;
  12784. input_bfd = input_section->owner;
  12785. if (!is_mips_elf (input_bfd))
  12786. {
  12787. /* I don't know what a non MIPS ELF bfd would be
  12788. doing with a .mdebug section, but I don't really
  12789. want to deal with it. */
  12790. continue;
  12791. }
  12792. input_swap = (get_elf_backend_data (input_bfd)
  12793. ->elf_backend_ecoff_debug_swap);
  12794. BFD_ASSERT (p->size == input_section->size);
  12795. /* The ECOFF linking code expects that we have already
  12796. read in the debugging information and set up an
  12797. ecoff_debug_info structure, so we do that now. */
  12798. if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
  12799. &input_debug))
  12800. return false;
  12801. if (! (bfd_ecoff_debug_accumulate
  12802. (mdebug_handle, abfd, &debug, swap, input_bfd,
  12803. &input_debug, input_swap, info)))
  12804. return false;
  12805. /* Loop through the external symbols. For each one with
  12806. interesting information, try to find the symbol in
  12807. the linker global hash table and save the information
  12808. for the output external symbols. */
  12809. eraw_src = input_debug.external_ext;
  12810. eraw_end = (eraw_src
  12811. + (input_debug.symbolic_header.iextMax
  12812. * input_swap->external_ext_size));
  12813. for (;
  12814. eraw_src < eraw_end;
  12815. eraw_src += input_swap->external_ext_size)
  12816. {
  12817. EXTR ext;
  12818. const char *name;
  12819. struct mips_elf_link_hash_entry *h;
  12820. (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
  12821. if (ext.asym.sc == scNil
  12822. || ext.asym.sc == scUndefined
  12823. || ext.asym.sc == scSUndefined)
  12824. continue;
  12825. name = input_debug.ssext + ext.asym.iss;
  12826. h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
  12827. name, false, false, true);
  12828. if (h == NULL || h->esym.ifd != -2)
  12829. continue;
  12830. if (ext.ifd != -1)
  12831. {
  12832. BFD_ASSERT (ext.ifd
  12833. < input_debug.symbolic_header.ifdMax);
  12834. ext.ifd = input_debug.ifdmap[ext.ifd];
  12835. }
  12836. h->esym = ext;
  12837. }
  12838. /* Free up the information we just read. */
  12839. free (input_debug.line);
  12840. free (input_debug.external_dnr);
  12841. free (input_debug.external_pdr);
  12842. free (input_debug.external_sym);
  12843. free (input_debug.external_opt);
  12844. free (input_debug.external_aux);
  12845. free (input_debug.ss);
  12846. free (input_debug.ssext);
  12847. free (input_debug.external_fdr);
  12848. free (input_debug.external_rfd);
  12849. free (input_debug.external_ext);
  12850. /* Hack: reset the SEC_HAS_CONTENTS flag so that
  12851. elf_link_input_bfd ignores this section. */
  12852. input_section->flags &= ~SEC_HAS_CONTENTS;
  12853. }
  12854. if (SGI_COMPAT (abfd) && bfd_link_pic (info))
  12855. {
  12856. /* Create .rtproc section. */
  12857. rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
  12858. if (rtproc_sec == NULL)
  12859. {
  12860. flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
  12861. | SEC_LINKER_CREATED | SEC_READONLY);
  12862. rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
  12863. ".rtproc",
  12864. flags);
  12865. if (rtproc_sec == NULL
  12866. || !bfd_set_section_alignment (rtproc_sec, 4))
  12867. return false;
  12868. }
  12869. if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
  12870. info, rtproc_sec,
  12871. &debug))
  12872. return false;
  12873. }
  12874. /* Build the external symbol information. */
  12875. einfo.abfd = abfd;
  12876. einfo.info = info;
  12877. einfo.debug = &debug;
  12878. einfo.swap = swap;
  12879. einfo.failed = false;
  12880. mips_elf_link_hash_traverse (mips_elf_hash_table (info),
  12881. mips_elf_output_extsym, &einfo);
  12882. if (einfo.failed)
  12883. return false;
  12884. /* Set the size of the .mdebug section. */
  12885. o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
  12886. /* Skip this section later on (I don't think this currently
  12887. matters, but someday it might). */
  12888. o->map_head.link_order = NULL;
  12889. mdebug_sec = o;
  12890. }
  12891. if (startswith (o->name, ".gptab."))
  12892. {
  12893. const char *subname;
  12894. unsigned int c;
  12895. Elf32_gptab *tab;
  12896. Elf32_External_gptab *ext_tab;
  12897. unsigned int j;
  12898. /* The .gptab.sdata and .gptab.sbss sections hold
  12899. information describing how the small data area would
  12900. change depending upon the -G switch. These sections
  12901. not used in executables files. */
  12902. if (! bfd_link_relocatable (info))
  12903. {
  12904. for (p = o->map_head.link_order; p != NULL; p = p->next)
  12905. {
  12906. asection *input_section;
  12907. if (p->type != bfd_indirect_link_order)
  12908. {
  12909. if (p->type == bfd_data_link_order)
  12910. continue;
  12911. abort ();
  12912. }
  12913. input_section = p->u.indirect.section;
  12914. /* Hack: reset the SEC_HAS_CONTENTS flag so that
  12915. elf_link_input_bfd ignores this section. */
  12916. input_section->flags &= ~SEC_HAS_CONTENTS;
  12917. }
  12918. /* Skip this section later on (I don't think this
  12919. currently matters, but someday it might). */
  12920. o->map_head.link_order = NULL;
  12921. /* Really remove the section. */
  12922. bfd_section_list_remove (abfd, o);
  12923. --abfd->section_count;
  12924. continue;
  12925. }
  12926. /* There is one gptab for initialized data, and one for
  12927. uninitialized data. */
  12928. if (strcmp (o->name, ".gptab.sdata") == 0)
  12929. gptab_data_sec = o;
  12930. else if (strcmp (o->name, ".gptab.sbss") == 0)
  12931. gptab_bss_sec = o;
  12932. else
  12933. {
  12934. _bfd_error_handler
  12935. /* xgettext:c-format */
  12936. (_("%pB: illegal section name `%pA'"), abfd, o);
  12937. bfd_set_error (bfd_error_nonrepresentable_section);
  12938. return false;
  12939. }
  12940. /* The linker script always combines .gptab.data and
  12941. .gptab.sdata into .gptab.sdata, and likewise for
  12942. .gptab.bss and .gptab.sbss. It is possible that there is
  12943. no .sdata or .sbss section in the output file, in which
  12944. case we must change the name of the output section. */
  12945. subname = o->name + sizeof ".gptab" - 1;
  12946. if (bfd_get_section_by_name (abfd, subname) == NULL)
  12947. {
  12948. if (o == gptab_data_sec)
  12949. o->name = ".gptab.data";
  12950. else
  12951. o->name = ".gptab.bss";
  12952. subname = o->name + sizeof ".gptab" - 1;
  12953. BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
  12954. }
  12955. /* Set up the first entry. */
  12956. c = 1;
  12957. amt = c * sizeof (Elf32_gptab);
  12958. tab = bfd_malloc (amt);
  12959. if (tab == NULL)
  12960. return false;
  12961. tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
  12962. tab[0].gt_header.gt_unused = 0;
  12963. /* Combine the input sections. */
  12964. for (p = o->map_head.link_order; p != NULL; p = p->next)
  12965. {
  12966. asection *input_section;
  12967. bfd *input_bfd;
  12968. bfd_size_type size;
  12969. unsigned long last;
  12970. bfd_size_type gpentry;
  12971. if (p->type != bfd_indirect_link_order)
  12972. {
  12973. if (p->type == bfd_data_link_order)
  12974. continue;
  12975. abort ();
  12976. }
  12977. input_section = p->u.indirect.section;
  12978. input_bfd = input_section->owner;
  12979. /* Combine the gptab entries for this input section one
  12980. by one. We know that the input gptab entries are
  12981. sorted by ascending -G value. */
  12982. size = input_section->size;
  12983. last = 0;
  12984. for (gpentry = sizeof (Elf32_External_gptab);
  12985. gpentry < size;
  12986. gpentry += sizeof (Elf32_External_gptab))
  12987. {
  12988. Elf32_External_gptab ext_gptab;
  12989. Elf32_gptab int_gptab;
  12990. unsigned long val;
  12991. unsigned long add;
  12992. bool exact;
  12993. unsigned int look;
  12994. if (! (bfd_get_section_contents
  12995. (input_bfd, input_section, &ext_gptab, gpentry,
  12996. sizeof (Elf32_External_gptab))))
  12997. {
  12998. free (tab);
  12999. return false;
  13000. }
  13001. bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
  13002. &int_gptab);
  13003. val = int_gptab.gt_entry.gt_g_value;
  13004. add = int_gptab.gt_entry.gt_bytes - last;
  13005. exact = false;
  13006. for (look = 1; look < c; look++)
  13007. {
  13008. if (tab[look].gt_entry.gt_g_value >= val)
  13009. tab[look].gt_entry.gt_bytes += add;
  13010. if (tab[look].gt_entry.gt_g_value == val)
  13011. exact = true;
  13012. }
  13013. if (! exact)
  13014. {
  13015. Elf32_gptab *new_tab;
  13016. unsigned int max;
  13017. /* We need a new table entry. */
  13018. amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
  13019. new_tab = bfd_realloc (tab, amt);
  13020. if (new_tab == NULL)
  13021. {
  13022. free (tab);
  13023. return false;
  13024. }
  13025. tab = new_tab;
  13026. tab[c].gt_entry.gt_g_value = val;
  13027. tab[c].gt_entry.gt_bytes = add;
  13028. /* Merge in the size for the next smallest -G
  13029. value, since that will be implied by this new
  13030. value. */
  13031. max = 0;
  13032. for (look = 1; look < c; look++)
  13033. {
  13034. if (tab[look].gt_entry.gt_g_value < val
  13035. && (max == 0
  13036. || (tab[look].gt_entry.gt_g_value
  13037. > tab[max].gt_entry.gt_g_value)))
  13038. max = look;
  13039. }
  13040. if (max != 0)
  13041. tab[c].gt_entry.gt_bytes +=
  13042. tab[max].gt_entry.gt_bytes;
  13043. ++c;
  13044. }
  13045. last = int_gptab.gt_entry.gt_bytes;
  13046. }
  13047. /* Hack: reset the SEC_HAS_CONTENTS flag so that
  13048. elf_link_input_bfd ignores this section. */
  13049. input_section->flags &= ~SEC_HAS_CONTENTS;
  13050. }
  13051. /* The table must be sorted by -G value. */
  13052. if (c > 2)
  13053. qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
  13054. /* Swap out the table. */
  13055. amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
  13056. ext_tab = bfd_alloc (abfd, amt);
  13057. if (ext_tab == NULL)
  13058. {
  13059. free (tab);
  13060. return false;
  13061. }
  13062. for (j = 0; j < c; j++)
  13063. bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
  13064. free (tab);
  13065. o->size = c * sizeof (Elf32_External_gptab);
  13066. o->contents = (bfd_byte *) ext_tab;
  13067. /* Skip this section later on (I don't think this currently
  13068. matters, but someday it might). */
  13069. o->map_head.link_order = NULL;
  13070. }
  13071. }
  13072. /* Invoke the regular ELF backend linker to do all the work. */
  13073. if (!bfd_elf_final_link (abfd, info))
  13074. return false;
  13075. /* Now write out the computed sections. */
  13076. if (abiflags_sec != NULL)
  13077. {
  13078. Elf_External_ABIFlags_v0 ext;
  13079. Elf_Internal_ABIFlags_v0 *abiflags;
  13080. abiflags = &mips_elf_tdata (abfd)->abiflags;
  13081. /* Set up the abiflags if no valid input sections were found. */
  13082. if (!mips_elf_tdata (abfd)->abiflags_valid)
  13083. {
  13084. infer_mips_abiflags (abfd, abiflags);
  13085. mips_elf_tdata (abfd)->abiflags_valid = true;
  13086. }
  13087. bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
  13088. if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
  13089. return false;
  13090. }
  13091. if (reginfo_sec != NULL)
  13092. {
  13093. Elf32_External_RegInfo ext;
  13094. bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
  13095. if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
  13096. return false;
  13097. }
  13098. if (mdebug_sec != NULL)
  13099. {
  13100. BFD_ASSERT (abfd->output_has_begun);
  13101. if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
  13102. swap, info,
  13103. mdebug_sec->filepos))
  13104. return false;
  13105. bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
  13106. }
  13107. if (gptab_data_sec != NULL)
  13108. {
  13109. if (! bfd_set_section_contents (abfd, gptab_data_sec,
  13110. gptab_data_sec->contents,
  13111. 0, gptab_data_sec->size))
  13112. return false;
  13113. }
  13114. if (gptab_bss_sec != NULL)
  13115. {
  13116. if (! bfd_set_section_contents (abfd, gptab_bss_sec,
  13117. gptab_bss_sec->contents,
  13118. 0, gptab_bss_sec->size))
  13119. return false;
  13120. }
  13121. if (SGI_COMPAT (abfd))
  13122. {
  13123. rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
  13124. if (rtproc_sec != NULL)
  13125. {
  13126. if (! bfd_set_section_contents (abfd, rtproc_sec,
  13127. rtproc_sec->contents,
  13128. 0, rtproc_sec->size))
  13129. return false;
  13130. }
  13131. }
  13132. return true;
  13133. }
  13134. /* Merge object file header flags from IBFD into OBFD. Raise an error
  13135. if there are conflicting settings. */
  13136. static bool
  13137. mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
  13138. {
  13139. bfd *obfd = info->output_bfd;
  13140. struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
  13141. flagword old_flags;
  13142. flagword new_flags;
  13143. bool ok;
  13144. new_flags = elf_elfheader (ibfd)->e_flags;
  13145. elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
  13146. old_flags = elf_elfheader (obfd)->e_flags;
  13147. /* Check flag compatibility. */
  13148. new_flags &= ~EF_MIPS_NOREORDER;
  13149. old_flags &= ~EF_MIPS_NOREORDER;
  13150. /* Some IRIX 6 BSD-compatibility objects have this bit set. It
  13151. doesn't seem to matter. */
  13152. new_flags &= ~EF_MIPS_XGOT;
  13153. old_flags &= ~EF_MIPS_XGOT;
  13154. /* MIPSpro generates ucode info in n64 objects. Again, we should
  13155. just be able to ignore this. */
  13156. new_flags &= ~EF_MIPS_UCODE;
  13157. old_flags &= ~EF_MIPS_UCODE;
  13158. /* DSOs should only be linked with CPIC code. */
  13159. if ((ibfd->flags & DYNAMIC) != 0)
  13160. new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
  13161. if (new_flags == old_flags)
  13162. return true;
  13163. ok = true;
  13164. if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
  13165. != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
  13166. {
  13167. _bfd_error_handler
  13168. (_("%pB: warning: linking abicalls files with non-abicalls files"),
  13169. ibfd);
  13170. ok = true;
  13171. }
  13172. if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
  13173. elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
  13174. if (! (new_flags & EF_MIPS_PIC))
  13175. elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
  13176. new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
  13177. old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
  13178. /* Compare the ISAs. */
  13179. if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
  13180. {
  13181. _bfd_error_handler
  13182. (_("%pB: linking 32-bit code with 64-bit code"),
  13183. ibfd);
  13184. ok = false;
  13185. }
  13186. else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
  13187. {
  13188. /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
  13189. if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
  13190. {
  13191. /* Copy the architecture info from IBFD to OBFD. Also copy
  13192. the 32-bit flag (if set) so that we continue to recognise
  13193. OBFD as a 32-bit binary. */
  13194. bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
  13195. elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
  13196. elf_elfheader (obfd)->e_flags
  13197. |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
  13198. /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
  13199. update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
  13200. /* Copy across the ABI flags if OBFD doesn't use them
  13201. and if that was what caused us to treat IBFD as 32-bit. */
  13202. if ((old_flags & EF_MIPS_ABI) == 0
  13203. && mips_32bit_flags_p (new_flags)
  13204. && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
  13205. elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
  13206. }
  13207. else
  13208. {
  13209. /* The ISAs aren't compatible. */
  13210. _bfd_error_handler
  13211. /* xgettext:c-format */
  13212. (_("%pB: linking %s module with previous %s modules"),
  13213. ibfd,
  13214. bfd_printable_name (ibfd),
  13215. bfd_printable_name (obfd));
  13216. ok = false;
  13217. }
  13218. }
  13219. new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
  13220. old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
  13221. /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
  13222. does set EI_CLASS differently from any 32-bit ABI. */
  13223. if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
  13224. || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
  13225. != elf_elfheader (obfd)->e_ident[EI_CLASS]))
  13226. {
  13227. /* Only error if both are set (to different values). */
  13228. if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
  13229. || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
  13230. != elf_elfheader (obfd)->e_ident[EI_CLASS]))
  13231. {
  13232. _bfd_error_handler
  13233. /* xgettext:c-format */
  13234. (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
  13235. ibfd,
  13236. elf_mips_abi_name (ibfd),
  13237. elf_mips_abi_name (obfd));
  13238. ok = false;
  13239. }
  13240. new_flags &= ~EF_MIPS_ABI;
  13241. old_flags &= ~EF_MIPS_ABI;
  13242. }
  13243. /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
  13244. and allow arbitrary mixing of the remaining ASEs (retain the union). */
  13245. if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
  13246. {
  13247. int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
  13248. int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
  13249. int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
  13250. int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
  13251. int micro_mis = old_m16 && new_micro;
  13252. int m16_mis = old_micro && new_m16;
  13253. if (m16_mis || micro_mis)
  13254. {
  13255. _bfd_error_handler
  13256. /* xgettext:c-format */
  13257. (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
  13258. ibfd,
  13259. m16_mis ? "MIPS16" : "microMIPS",
  13260. m16_mis ? "microMIPS" : "MIPS16");
  13261. ok = false;
  13262. }
  13263. elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
  13264. new_flags &= ~ EF_MIPS_ARCH_ASE;
  13265. old_flags &= ~ EF_MIPS_ARCH_ASE;
  13266. }
  13267. /* Compare NaN encodings. */
  13268. if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
  13269. {
  13270. /* xgettext:c-format */
  13271. _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
  13272. ibfd,
  13273. (new_flags & EF_MIPS_NAN2008
  13274. ? "-mnan=2008" : "-mnan=legacy"),
  13275. (old_flags & EF_MIPS_NAN2008
  13276. ? "-mnan=2008" : "-mnan=legacy"));
  13277. ok = false;
  13278. new_flags &= ~EF_MIPS_NAN2008;
  13279. old_flags &= ~EF_MIPS_NAN2008;
  13280. }
  13281. /* Compare FP64 state. */
  13282. if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
  13283. {
  13284. /* xgettext:c-format */
  13285. _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
  13286. ibfd,
  13287. (new_flags & EF_MIPS_FP64
  13288. ? "-mfp64" : "-mfp32"),
  13289. (old_flags & EF_MIPS_FP64
  13290. ? "-mfp64" : "-mfp32"));
  13291. ok = false;
  13292. new_flags &= ~EF_MIPS_FP64;
  13293. old_flags &= ~EF_MIPS_FP64;
  13294. }
  13295. /* Warn about any other mismatches */
  13296. if (new_flags != old_flags)
  13297. {
  13298. /* xgettext:c-format */
  13299. _bfd_error_handler
  13300. (_("%pB: uses different e_flags (%#x) fields than previous modules "
  13301. "(%#x)"),
  13302. ibfd, new_flags, old_flags);
  13303. ok = false;
  13304. }
  13305. return ok;
  13306. }
  13307. /* Merge object attributes from IBFD into OBFD. Raise an error if
  13308. there are conflicting attributes. */
  13309. static bool
  13310. mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
  13311. {
  13312. bfd *obfd = info->output_bfd;
  13313. obj_attribute *in_attr;
  13314. obj_attribute *out_attr;
  13315. bfd *abi_fp_bfd;
  13316. bfd *abi_msa_bfd;
  13317. abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
  13318. in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
  13319. if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
  13320. mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
  13321. abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
  13322. if (!abi_msa_bfd
  13323. && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
  13324. mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
  13325. if (!elf_known_obj_attributes_proc (obfd)[0].i)
  13326. {
  13327. /* This is the first object. Copy the attributes. */
  13328. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  13329. /* Use the Tag_null value to indicate the attributes have been
  13330. initialized. */
  13331. elf_known_obj_attributes_proc (obfd)[0].i = 1;
  13332. return true;
  13333. }
  13334. /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
  13335. non-conflicting ones. */
  13336. out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
  13337. if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
  13338. {
  13339. int out_fp, in_fp;
  13340. out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
  13341. in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
  13342. out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
  13343. if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
  13344. out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
  13345. else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
  13346. && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
  13347. || in_fp == Val_GNU_MIPS_ABI_FP_64
  13348. || in_fp == Val_GNU_MIPS_ABI_FP_64A))
  13349. {
  13350. mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
  13351. out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
  13352. }
  13353. else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
  13354. && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
  13355. || out_fp == Val_GNU_MIPS_ABI_FP_64
  13356. || out_fp == Val_GNU_MIPS_ABI_FP_64A))
  13357. /* Keep the current setting. */;
  13358. else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
  13359. && in_fp == Val_GNU_MIPS_ABI_FP_64)
  13360. {
  13361. mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
  13362. out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
  13363. }
  13364. else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
  13365. && out_fp == Val_GNU_MIPS_ABI_FP_64)
  13366. /* Keep the current setting. */;
  13367. else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
  13368. {
  13369. const char *out_string, *in_string;
  13370. out_string = _bfd_mips_fp_abi_string (out_fp);
  13371. in_string = _bfd_mips_fp_abi_string (in_fp);
  13372. /* First warn about cases involving unrecognised ABIs. */
  13373. if (!out_string && !in_string)
  13374. /* xgettext:c-format */
  13375. _bfd_error_handler
  13376. (_("warning: %pB uses unknown floating point ABI %d "
  13377. "(set by %pB), %pB uses unknown floating point ABI %d"),
  13378. obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
  13379. else if (!out_string)
  13380. _bfd_error_handler
  13381. /* xgettext:c-format */
  13382. (_("warning: %pB uses unknown floating point ABI %d "
  13383. "(set by %pB), %pB uses %s"),
  13384. obfd, out_fp, abi_fp_bfd, ibfd, in_string);
  13385. else if (!in_string)
  13386. _bfd_error_handler
  13387. /* xgettext:c-format */
  13388. (_("warning: %pB uses %s (set by %pB), "
  13389. "%pB uses unknown floating point ABI %d"),
  13390. obfd, out_string, abi_fp_bfd, ibfd, in_fp);
  13391. else
  13392. {
  13393. /* If one of the bfds is soft-float, the other must be
  13394. hard-float. The exact choice of hard-float ABI isn't
  13395. really relevant to the error message. */
  13396. if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
  13397. out_string = "-mhard-float";
  13398. else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
  13399. in_string = "-mhard-float";
  13400. _bfd_error_handler
  13401. /* xgettext:c-format */
  13402. (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
  13403. obfd, out_string, abi_fp_bfd, ibfd, in_string);
  13404. }
  13405. }
  13406. }
  13407. /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
  13408. non-conflicting ones. */
  13409. if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
  13410. {
  13411. out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
  13412. if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
  13413. out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
  13414. else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
  13415. switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
  13416. {
  13417. case Val_GNU_MIPS_ABI_MSA_128:
  13418. _bfd_error_handler
  13419. /* xgettext:c-format */
  13420. (_("warning: %pB uses %s (set by %pB), "
  13421. "%pB uses unknown MSA ABI %d"),
  13422. obfd, "-mmsa", abi_msa_bfd,
  13423. ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
  13424. break;
  13425. default:
  13426. switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
  13427. {
  13428. case Val_GNU_MIPS_ABI_MSA_128:
  13429. _bfd_error_handler
  13430. /* xgettext:c-format */
  13431. (_("warning: %pB uses unknown MSA ABI %d "
  13432. "(set by %pB), %pB uses %s"),
  13433. obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
  13434. abi_msa_bfd, ibfd, "-mmsa");
  13435. break;
  13436. default:
  13437. _bfd_error_handler
  13438. /* xgettext:c-format */
  13439. (_("warning: %pB uses unknown MSA ABI %d "
  13440. "(set by %pB), %pB uses unknown MSA ABI %d"),
  13441. obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
  13442. abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
  13443. break;
  13444. }
  13445. }
  13446. }
  13447. /* Merge Tag_compatibility attributes and any common GNU ones. */
  13448. return _bfd_elf_merge_object_attributes (ibfd, info);
  13449. }
  13450. /* Merge object ABI flags from IBFD into OBFD. Raise an error if
  13451. there are conflicting settings. */
  13452. static bool
  13453. mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
  13454. {
  13455. obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
  13456. struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
  13457. struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
  13458. /* Update the output abiflags fp_abi using the computed fp_abi. */
  13459. out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
  13460. #define max(a, b) ((a) > (b) ? (a) : (b))
  13461. /* Merge abiflags. */
  13462. out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
  13463. in_tdata->abiflags.isa_level);
  13464. out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
  13465. in_tdata->abiflags.isa_rev);
  13466. out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
  13467. in_tdata->abiflags.gpr_size);
  13468. out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
  13469. in_tdata->abiflags.cpr1_size);
  13470. out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
  13471. in_tdata->abiflags.cpr2_size);
  13472. #undef max
  13473. out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
  13474. out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
  13475. return true;
  13476. }
  13477. /* Merge backend specific data from an object file to the output
  13478. object file when linking. */
  13479. bool
  13480. _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  13481. {
  13482. bfd *obfd = info->output_bfd;
  13483. struct mips_elf_obj_tdata *out_tdata;
  13484. struct mips_elf_obj_tdata *in_tdata;
  13485. bool null_input_bfd = true;
  13486. asection *sec;
  13487. bool ok;
  13488. /* Check if we have the same endianness. */
  13489. if (! _bfd_generic_verify_endian_match (ibfd, info))
  13490. {
  13491. _bfd_error_handler
  13492. (_("%pB: endianness incompatible with that of the selected emulation"),
  13493. ibfd);
  13494. return false;
  13495. }
  13496. if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
  13497. return true;
  13498. in_tdata = mips_elf_tdata (ibfd);
  13499. out_tdata = mips_elf_tdata (obfd);
  13500. if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
  13501. {
  13502. _bfd_error_handler
  13503. (_("%pB: ABI is incompatible with that of the selected emulation"),
  13504. ibfd);
  13505. return false;
  13506. }
  13507. /* Check to see if the input BFD actually contains any sections. If not,
  13508. then it has no attributes, and its flags may not have been initialized
  13509. either, but it cannot actually cause any incompatibility. */
  13510. /* FIXME: This excludes any input shared library from consideration. */
  13511. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  13512. {
  13513. /* Ignore synthetic sections and empty .text, .data and .bss sections
  13514. which are automatically generated by gas. Also ignore fake
  13515. (s)common sections, since merely defining a common symbol does
  13516. not affect compatibility. */
  13517. if ((sec->flags & SEC_IS_COMMON) == 0
  13518. && strcmp (sec->name, ".reginfo")
  13519. && strcmp (sec->name, ".mdebug")
  13520. && (sec->size != 0
  13521. || (strcmp (sec->name, ".text")
  13522. && strcmp (sec->name, ".data")
  13523. && strcmp (sec->name, ".bss"))))
  13524. {
  13525. null_input_bfd = false;
  13526. break;
  13527. }
  13528. }
  13529. if (null_input_bfd)
  13530. return true;
  13531. /* Populate abiflags using existing information. */
  13532. if (in_tdata->abiflags_valid)
  13533. {
  13534. obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
  13535. Elf_Internal_ABIFlags_v0 in_abiflags;
  13536. Elf_Internal_ABIFlags_v0 abiflags;
  13537. /* Set up the FP ABI attribute from the abiflags if it is not already
  13538. set. */
  13539. if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
  13540. in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
  13541. infer_mips_abiflags (ibfd, &abiflags);
  13542. in_abiflags = in_tdata->abiflags;
  13543. /* It is not possible to infer the correct ISA revision
  13544. for R3 or R5 so drop down to R2 for the checks. */
  13545. if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
  13546. in_abiflags.isa_rev = 2;
  13547. if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
  13548. < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
  13549. _bfd_error_handler
  13550. (_("%pB: warning: inconsistent ISA between e_flags and "
  13551. ".MIPS.abiflags"), ibfd);
  13552. if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
  13553. && in_abiflags.fp_abi != abiflags.fp_abi)
  13554. _bfd_error_handler
  13555. (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
  13556. ".MIPS.abiflags"), ibfd);
  13557. if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
  13558. _bfd_error_handler
  13559. (_("%pB: warning: inconsistent ASEs between e_flags and "
  13560. ".MIPS.abiflags"), ibfd);
  13561. /* The isa_ext is allowed to be an extension of what can be inferred
  13562. from e_flags. */
  13563. if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
  13564. bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
  13565. _bfd_error_handler
  13566. (_("%pB: warning: inconsistent ISA extensions between e_flags and "
  13567. ".MIPS.abiflags"), ibfd);
  13568. if (in_abiflags.flags2 != 0)
  13569. _bfd_error_handler
  13570. (_("%pB: warning: unexpected flag in the flags2 field of "
  13571. ".MIPS.abiflags (0x%lx)"), ibfd,
  13572. in_abiflags.flags2);
  13573. }
  13574. else
  13575. {
  13576. infer_mips_abiflags (ibfd, &in_tdata->abiflags);
  13577. in_tdata->abiflags_valid = true;
  13578. }
  13579. if (!out_tdata->abiflags_valid)
  13580. {
  13581. /* Copy input abiflags if output abiflags are not already valid. */
  13582. out_tdata->abiflags = in_tdata->abiflags;
  13583. out_tdata->abiflags_valid = true;
  13584. }
  13585. if (! elf_flags_init (obfd))
  13586. {
  13587. elf_flags_init (obfd) = true;
  13588. elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  13589. elf_elfheader (obfd)->e_ident[EI_CLASS]
  13590. = elf_elfheader (ibfd)->e_ident[EI_CLASS];
  13591. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  13592. && (bfd_get_arch_info (obfd)->the_default
  13593. || mips_mach_extends_p (bfd_get_mach (obfd),
  13594. bfd_get_mach (ibfd))))
  13595. {
  13596. if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  13597. bfd_get_mach (ibfd)))
  13598. return false;
  13599. /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
  13600. update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
  13601. }
  13602. ok = true;
  13603. }
  13604. else
  13605. ok = mips_elf_merge_obj_e_flags (ibfd, info);
  13606. ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
  13607. ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
  13608. if (!ok)
  13609. {
  13610. bfd_set_error (bfd_error_bad_value);
  13611. return false;
  13612. }
  13613. return true;
  13614. }
  13615. /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
  13616. bool
  13617. _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
  13618. {
  13619. BFD_ASSERT (!elf_flags_init (abfd)
  13620. || elf_elfheader (abfd)->e_flags == flags);
  13621. elf_elfheader (abfd)->e_flags = flags;
  13622. elf_flags_init (abfd) = true;
  13623. return true;
  13624. }
  13625. char *
  13626. _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
  13627. {
  13628. switch (dtag)
  13629. {
  13630. default: return "";
  13631. case DT_MIPS_RLD_VERSION:
  13632. return "MIPS_RLD_VERSION";
  13633. case DT_MIPS_TIME_STAMP:
  13634. return "MIPS_TIME_STAMP";
  13635. case DT_MIPS_ICHECKSUM:
  13636. return "MIPS_ICHECKSUM";
  13637. case DT_MIPS_IVERSION:
  13638. return "MIPS_IVERSION";
  13639. case DT_MIPS_FLAGS:
  13640. return "MIPS_FLAGS";
  13641. case DT_MIPS_BASE_ADDRESS:
  13642. return "MIPS_BASE_ADDRESS";
  13643. case DT_MIPS_MSYM:
  13644. return "MIPS_MSYM";
  13645. case DT_MIPS_CONFLICT:
  13646. return "MIPS_CONFLICT";
  13647. case DT_MIPS_LIBLIST:
  13648. return "MIPS_LIBLIST";
  13649. case DT_MIPS_LOCAL_GOTNO:
  13650. return "MIPS_LOCAL_GOTNO";
  13651. case DT_MIPS_CONFLICTNO:
  13652. return "MIPS_CONFLICTNO";
  13653. case DT_MIPS_LIBLISTNO:
  13654. return "MIPS_LIBLISTNO";
  13655. case DT_MIPS_SYMTABNO:
  13656. return "MIPS_SYMTABNO";
  13657. case DT_MIPS_UNREFEXTNO:
  13658. return "MIPS_UNREFEXTNO";
  13659. case DT_MIPS_GOTSYM:
  13660. return "MIPS_GOTSYM";
  13661. case DT_MIPS_HIPAGENO:
  13662. return "MIPS_HIPAGENO";
  13663. case DT_MIPS_RLD_MAP:
  13664. return "MIPS_RLD_MAP";
  13665. case DT_MIPS_RLD_MAP_REL:
  13666. return "MIPS_RLD_MAP_REL";
  13667. case DT_MIPS_DELTA_CLASS:
  13668. return "MIPS_DELTA_CLASS";
  13669. case DT_MIPS_DELTA_CLASS_NO:
  13670. return "MIPS_DELTA_CLASS_NO";
  13671. case DT_MIPS_DELTA_INSTANCE:
  13672. return "MIPS_DELTA_INSTANCE";
  13673. case DT_MIPS_DELTA_INSTANCE_NO:
  13674. return "MIPS_DELTA_INSTANCE_NO";
  13675. case DT_MIPS_DELTA_RELOC:
  13676. return "MIPS_DELTA_RELOC";
  13677. case DT_MIPS_DELTA_RELOC_NO:
  13678. return "MIPS_DELTA_RELOC_NO";
  13679. case DT_MIPS_DELTA_SYM:
  13680. return "MIPS_DELTA_SYM";
  13681. case DT_MIPS_DELTA_SYM_NO:
  13682. return "MIPS_DELTA_SYM_NO";
  13683. case DT_MIPS_DELTA_CLASSSYM:
  13684. return "MIPS_DELTA_CLASSSYM";
  13685. case DT_MIPS_DELTA_CLASSSYM_NO:
  13686. return "MIPS_DELTA_CLASSSYM_NO";
  13687. case DT_MIPS_CXX_FLAGS:
  13688. return "MIPS_CXX_FLAGS";
  13689. case DT_MIPS_PIXIE_INIT:
  13690. return "MIPS_PIXIE_INIT";
  13691. case DT_MIPS_SYMBOL_LIB:
  13692. return "MIPS_SYMBOL_LIB";
  13693. case DT_MIPS_LOCALPAGE_GOTIDX:
  13694. return "MIPS_LOCALPAGE_GOTIDX";
  13695. case DT_MIPS_LOCAL_GOTIDX:
  13696. return "MIPS_LOCAL_GOTIDX";
  13697. case DT_MIPS_HIDDEN_GOTIDX:
  13698. return "MIPS_HIDDEN_GOTIDX";
  13699. case DT_MIPS_PROTECTED_GOTIDX:
  13700. return "MIPS_PROTECTED_GOT_IDX";
  13701. case DT_MIPS_OPTIONS:
  13702. return "MIPS_OPTIONS";
  13703. case DT_MIPS_INTERFACE:
  13704. return "MIPS_INTERFACE";
  13705. case DT_MIPS_DYNSTR_ALIGN:
  13706. return "DT_MIPS_DYNSTR_ALIGN";
  13707. case DT_MIPS_INTERFACE_SIZE:
  13708. return "DT_MIPS_INTERFACE_SIZE";
  13709. case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
  13710. return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
  13711. case DT_MIPS_PERF_SUFFIX:
  13712. return "DT_MIPS_PERF_SUFFIX";
  13713. case DT_MIPS_COMPACT_SIZE:
  13714. return "DT_MIPS_COMPACT_SIZE";
  13715. case DT_MIPS_GP_VALUE:
  13716. return "DT_MIPS_GP_VALUE";
  13717. case DT_MIPS_AUX_DYNAMIC:
  13718. return "DT_MIPS_AUX_DYNAMIC";
  13719. case DT_MIPS_PLTGOT:
  13720. return "DT_MIPS_PLTGOT";
  13721. case DT_MIPS_RWPLT:
  13722. return "DT_MIPS_RWPLT";
  13723. case DT_MIPS_XHASH:
  13724. return "DT_MIPS_XHASH";
  13725. }
  13726. }
  13727. /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
  13728. not known. */
  13729. const char *
  13730. _bfd_mips_fp_abi_string (int fp)
  13731. {
  13732. switch (fp)
  13733. {
  13734. /* These strings aren't translated because they're simply
  13735. option lists. */
  13736. case Val_GNU_MIPS_ABI_FP_DOUBLE:
  13737. return "-mdouble-float";
  13738. case Val_GNU_MIPS_ABI_FP_SINGLE:
  13739. return "-msingle-float";
  13740. case Val_GNU_MIPS_ABI_FP_SOFT:
  13741. return "-msoft-float";
  13742. case Val_GNU_MIPS_ABI_FP_OLD_64:
  13743. return _("-mips32r2 -mfp64 (12 callee-saved)");
  13744. case Val_GNU_MIPS_ABI_FP_XX:
  13745. return "-mfpxx";
  13746. case Val_GNU_MIPS_ABI_FP_64:
  13747. return "-mgp32 -mfp64";
  13748. case Val_GNU_MIPS_ABI_FP_64A:
  13749. return "-mgp32 -mfp64 -mno-odd-spreg";
  13750. default:
  13751. return 0;
  13752. }
  13753. }
  13754. static void
  13755. print_mips_ases (FILE *file, unsigned int mask)
  13756. {
  13757. if (mask & AFL_ASE_DSP)
  13758. fputs ("\n\tDSP ASE", file);
  13759. if (mask & AFL_ASE_DSPR2)
  13760. fputs ("\n\tDSP R2 ASE", file);
  13761. if (mask & AFL_ASE_DSPR3)
  13762. fputs ("\n\tDSP R3 ASE", file);
  13763. if (mask & AFL_ASE_EVA)
  13764. fputs ("\n\tEnhanced VA Scheme", file);
  13765. if (mask & AFL_ASE_MCU)
  13766. fputs ("\n\tMCU (MicroController) ASE", file);
  13767. if (mask & AFL_ASE_MDMX)
  13768. fputs ("\n\tMDMX ASE", file);
  13769. if (mask & AFL_ASE_MIPS3D)
  13770. fputs ("\n\tMIPS-3D ASE", file);
  13771. if (mask & AFL_ASE_MT)
  13772. fputs ("\n\tMT ASE", file);
  13773. if (mask & AFL_ASE_SMARTMIPS)
  13774. fputs ("\n\tSmartMIPS ASE", file);
  13775. if (mask & AFL_ASE_VIRT)
  13776. fputs ("\n\tVZ ASE", file);
  13777. if (mask & AFL_ASE_MSA)
  13778. fputs ("\n\tMSA ASE", file);
  13779. if (mask & AFL_ASE_MIPS16)
  13780. fputs ("\n\tMIPS16 ASE", file);
  13781. if (mask & AFL_ASE_MICROMIPS)
  13782. fputs ("\n\tMICROMIPS ASE", file);
  13783. if (mask & AFL_ASE_XPA)
  13784. fputs ("\n\tXPA ASE", file);
  13785. if (mask & AFL_ASE_MIPS16E2)
  13786. fputs ("\n\tMIPS16e2 ASE", file);
  13787. if (mask & AFL_ASE_CRC)
  13788. fputs ("\n\tCRC ASE", file);
  13789. if (mask & AFL_ASE_GINV)
  13790. fputs ("\n\tGINV ASE", file);
  13791. if (mask & AFL_ASE_LOONGSON_MMI)
  13792. fputs ("\n\tLoongson MMI ASE", file);
  13793. if (mask & AFL_ASE_LOONGSON_CAM)
  13794. fputs ("\n\tLoongson CAM ASE", file);
  13795. if (mask & AFL_ASE_LOONGSON_EXT)
  13796. fputs ("\n\tLoongson EXT ASE", file);
  13797. if (mask & AFL_ASE_LOONGSON_EXT2)
  13798. fputs ("\n\tLoongson EXT2 ASE", file);
  13799. if (mask == 0)
  13800. fprintf (file, "\n\t%s", _("None"));
  13801. else if ((mask & ~AFL_ASE_MASK) != 0)
  13802. fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
  13803. }
  13804. static void
  13805. print_mips_isa_ext (FILE *file, unsigned int isa_ext)
  13806. {
  13807. switch (isa_ext)
  13808. {
  13809. case 0:
  13810. fputs (_("None"), file);
  13811. break;
  13812. case AFL_EXT_XLR:
  13813. fputs ("RMI XLR", file);
  13814. break;
  13815. case AFL_EXT_OCTEON3:
  13816. fputs ("Cavium Networks Octeon3", file);
  13817. break;
  13818. case AFL_EXT_OCTEON2:
  13819. fputs ("Cavium Networks Octeon2", file);
  13820. break;
  13821. case AFL_EXT_OCTEONP:
  13822. fputs ("Cavium Networks OcteonP", file);
  13823. break;
  13824. case AFL_EXT_OCTEON:
  13825. fputs ("Cavium Networks Octeon", file);
  13826. break;
  13827. case AFL_EXT_5900:
  13828. fputs ("Toshiba R5900", file);
  13829. break;
  13830. case AFL_EXT_4650:
  13831. fputs ("MIPS R4650", file);
  13832. break;
  13833. case AFL_EXT_4010:
  13834. fputs ("LSI R4010", file);
  13835. break;
  13836. case AFL_EXT_4100:
  13837. fputs ("NEC VR4100", file);
  13838. break;
  13839. case AFL_EXT_3900:
  13840. fputs ("Toshiba R3900", file);
  13841. break;
  13842. case AFL_EXT_10000:
  13843. fputs ("MIPS R10000", file);
  13844. break;
  13845. case AFL_EXT_SB1:
  13846. fputs ("Broadcom SB-1", file);
  13847. break;
  13848. case AFL_EXT_4111:
  13849. fputs ("NEC VR4111/VR4181", file);
  13850. break;
  13851. case AFL_EXT_4120:
  13852. fputs ("NEC VR4120", file);
  13853. break;
  13854. case AFL_EXT_5400:
  13855. fputs ("NEC VR5400", file);
  13856. break;
  13857. case AFL_EXT_5500:
  13858. fputs ("NEC VR5500", file);
  13859. break;
  13860. case AFL_EXT_LOONGSON_2E:
  13861. fputs ("ST Microelectronics Loongson 2E", file);
  13862. break;
  13863. case AFL_EXT_LOONGSON_2F:
  13864. fputs ("ST Microelectronics Loongson 2F", file);
  13865. break;
  13866. case AFL_EXT_INTERAPTIV_MR2:
  13867. fputs ("Imagination interAptiv MR2", file);
  13868. break;
  13869. default:
  13870. fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
  13871. break;
  13872. }
  13873. }
  13874. static void
  13875. print_mips_fp_abi_value (FILE *file, int val)
  13876. {
  13877. switch (val)
  13878. {
  13879. case Val_GNU_MIPS_ABI_FP_ANY:
  13880. fprintf (file, _("Hard or soft float\n"));
  13881. break;
  13882. case Val_GNU_MIPS_ABI_FP_DOUBLE:
  13883. fprintf (file, _("Hard float (double precision)\n"));
  13884. break;
  13885. case Val_GNU_MIPS_ABI_FP_SINGLE:
  13886. fprintf (file, _("Hard float (single precision)\n"));
  13887. break;
  13888. case Val_GNU_MIPS_ABI_FP_SOFT:
  13889. fprintf (file, _("Soft float\n"));
  13890. break;
  13891. case Val_GNU_MIPS_ABI_FP_OLD_64:
  13892. fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
  13893. break;
  13894. case Val_GNU_MIPS_ABI_FP_XX:
  13895. fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
  13896. break;
  13897. case Val_GNU_MIPS_ABI_FP_64:
  13898. fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
  13899. break;
  13900. case Val_GNU_MIPS_ABI_FP_64A:
  13901. fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
  13902. break;
  13903. default:
  13904. fprintf (file, "??? (%d)\n", val);
  13905. break;
  13906. }
  13907. }
  13908. static int
  13909. get_mips_reg_size (int reg_size)
  13910. {
  13911. return (reg_size == AFL_REG_NONE) ? 0
  13912. : (reg_size == AFL_REG_32) ? 32
  13913. : (reg_size == AFL_REG_64) ? 64
  13914. : (reg_size == AFL_REG_128) ? 128
  13915. : -1;
  13916. }
  13917. bool
  13918. _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
  13919. {
  13920. FILE *file = ptr;
  13921. BFD_ASSERT (abfd != NULL && ptr != NULL);
  13922. /* Print normal ELF private data. */
  13923. _bfd_elf_print_private_bfd_data (abfd, ptr);
  13924. /* xgettext:c-format */
  13925. fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
  13926. if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
  13927. fprintf (file, _(" [abi=O32]"));
  13928. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
  13929. fprintf (file, _(" [abi=O64]"));
  13930. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
  13931. fprintf (file, _(" [abi=EABI32]"));
  13932. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
  13933. fprintf (file, _(" [abi=EABI64]"));
  13934. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
  13935. fprintf (file, _(" [abi unknown]"));
  13936. else if (ABI_N32_P (abfd))
  13937. fprintf (file, _(" [abi=N32]"));
  13938. else if (ABI_64_P (abfd))
  13939. fprintf (file, _(" [abi=64]"));
  13940. else
  13941. fprintf (file, _(" [no abi set]"));
  13942. if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
  13943. fprintf (file, " [mips1]");
  13944. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
  13945. fprintf (file, " [mips2]");
  13946. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
  13947. fprintf (file, " [mips3]");
  13948. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
  13949. fprintf (file, " [mips4]");
  13950. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
  13951. fprintf (file, " [mips5]");
  13952. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
  13953. fprintf (file, " [mips32]");
  13954. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
  13955. fprintf (file, " [mips64]");
  13956. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
  13957. fprintf (file, " [mips32r2]");
  13958. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
  13959. fprintf (file, " [mips64r2]");
  13960. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
  13961. fprintf (file, " [mips32r6]");
  13962. else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
  13963. fprintf (file, " [mips64r6]");
  13964. else
  13965. fprintf (file, _(" [unknown ISA]"));
  13966. if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
  13967. fprintf (file, " [mdmx]");
  13968. if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
  13969. fprintf (file, " [mips16]");
  13970. if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
  13971. fprintf (file, " [micromips]");
  13972. if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
  13973. fprintf (file, " [nan2008]");
  13974. if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
  13975. fprintf (file, " [old fp64]");
  13976. if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
  13977. fprintf (file, " [32bitmode]");
  13978. else
  13979. fprintf (file, _(" [not 32bitmode]"));
  13980. if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
  13981. fprintf (file, " [noreorder]");
  13982. if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
  13983. fprintf (file, " [PIC]");
  13984. if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
  13985. fprintf (file, " [CPIC]");
  13986. if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
  13987. fprintf (file, " [XGOT]");
  13988. if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
  13989. fprintf (file, " [UCODE]");
  13990. fputc ('\n', file);
  13991. if (mips_elf_tdata (abfd)->abiflags_valid)
  13992. {
  13993. Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
  13994. fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
  13995. fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
  13996. if (abiflags->isa_rev > 1)
  13997. fprintf (file, "r%d", abiflags->isa_rev);
  13998. fprintf (file, "\nGPR size: %d",
  13999. get_mips_reg_size (abiflags->gpr_size));
  14000. fprintf (file, "\nCPR1 size: %d",
  14001. get_mips_reg_size (abiflags->cpr1_size));
  14002. fprintf (file, "\nCPR2 size: %d",
  14003. get_mips_reg_size (abiflags->cpr2_size));
  14004. fputs ("\nFP ABI: ", file);
  14005. print_mips_fp_abi_value (file, abiflags->fp_abi);
  14006. fputs ("ISA Extension: ", file);
  14007. print_mips_isa_ext (file, abiflags->isa_ext);
  14008. fputs ("\nASEs:", file);
  14009. print_mips_ases (file, abiflags->ases);
  14010. fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
  14011. fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
  14012. fputc ('\n', file);
  14013. }
  14014. return true;
  14015. }
  14016. const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
  14017. {
  14018. { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
  14019. { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
  14020. { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
  14021. { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
  14022. { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
  14023. { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
  14024. { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
  14025. { NULL, 0, 0, 0, 0 }
  14026. };
  14027. /* Merge non visibility st_other attributes. Ensure that the
  14028. STO_OPTIONAL flag is copied into h->other, even if this is not a
  14029. definiton of the symbol. */
  14030. void
  14031. _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
  14032. unsigned int st_other,
  14033. bool definition,
  14034. bool dynamic ATTRIBUTE_UNUSED)
  14035. {
  14036. if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
  14037. {
  14038. unsigned char other;
  14039. other = (definition ? st_other : h->other);
  14040. other &= ~ELF_ST_VISIBILITY (-1);
  14041. h->other = other | ELF_ST_VISIBILITY (h->other);
  14042. }
  14043. if (!definition
  14044. && ELF_MIPS_IS_OPTIONAL (st_other))
  14045. h->other |= STO_OPTIONAL;
  14046. }
  14047. /* Decide whether an undefined symbol is special and can be ignored.
  14048. This is the case for OPTIONAL symbols on IRIX. */
  14049. bool
  14050. _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
  14051. {
  14052. return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
  14053. }
  14054. bool
  14055. _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
  14056. {
  14057. return (sym->st_shndx == SHN_COMMON
  14058. || sym->st_shndx == SHN_MIPS_ACOMMON
  14059. || sym->st_shndx == SHN_MIPS_SCOMMON);
  14060. }
  14061. /* Return address for Ith PLT stub in section PLT, for relocation REL
  14062. or (bfd_vma) -1 if it should not be included. */
  14063. bfd_vma
  14064. _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
  14065. const arelent *rel ATTRIBUTE_UNUSED)
  14066. {
  14067. return (plt->vma
  14068. + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
  14069. + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
  14070. }
  14071. /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
  14072. and microMIPS PLT slots we may have a many-to-one mapping between .plt
  14073. and .got.plt and also the slots may be of a different size each we walk
  14074. the PLT manually fetching instructions and matching them against known
  14075. patterns. To make things easier standard MIPS slots, if any, always come
  14076. first. As we don't create proper ELF symbols we use the UDATA.I member
  14077. of ASYMBOL to carry ISA annotation. The encoding used is the same as
  14078. with the ST_OTHER member of the ELF symbol. */
  14079. long
  14080. _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
  14081. long symcount ATTRIBUTE_UNUSED,
  14082. asymbol **syms ATTRIBUTE_UNUSED,
  14083. long dynsymcount, asymbol **dynsyms,
  14084. asymbol **ret)
  14085. {
  14086. static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
  14087. static const char microsuffix[] = "@micromipsplt";
  14088. static const char m16suffix[] = "@mips16plt";
  14089. static const char mipssuffix[] = "@plt";
  14090. bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
  14091. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  14092. bool micromips_p = MICROMIPS_P (abfd);
  14093. Elf_Internal_Shdr *hdr;
  14094. bfd_byte *plt_data;
  14095. bfd_vma plt_offset;
  14096. unsigned int other;
  14097. bfd_vma entry_size;
  14098. bfd_vma plt0_size;
  14099. asection *relplt;
  14100. bfd_vma opcode;
  14101. asection *plt;
  14102. asymbol *send;
  14103. size_t size;
  14104. char *names;
  14105. long counti;
  14106. arelent *p;
  14107. asymbol *s;
  14108. char *nend;
  14109. long count;
  14110. long pi;
  14111. long i;
  14112. long n;
  14113. *ret = NULL;
  14114. if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
  14115. return 0;
  14116. relplt = bfd_get_section_by_name (abfd, ".rel.plt");
  14117. if (relplt == NULL)
  14118. return 0;
  14119. hdr = &elf_section_data (relplt)->this_hdr;
  14120. if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
  14121. return 0;
  14122. plt = bfd_get_section_by_name (abfd, ".plt");
  14123. if (plt == NULL)
  14124. return 0;
  14125. slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
  14126. if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
  14127. return -1;
  14128. p = relplt->relocation;
  14129. /* Calculating the exact amount of space required for symbols would
  14130. require two passes over the PLT, so just pessimise assuming two
  14131. PLT slots per relocation. */
  14132. count = relplt->size / hdr->sh_entsize;
  14133. counti = count * bed->s->int_rels_per_ext_rel;
  14134. size = 2 * count * sizeof (asymbol);
  14135. size += count * (sizeof (mipssuffix) +
  14136. (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
  14137. for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
  14138. size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
  14139. /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
  14140. size += sizeof (asymbol) + sizeof (pltname);
  14141. if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
  14142. return -1;
  14143. if (plt->size < 16)
  14144. return -1;
  14145. s = *ret = bfd_malloc (size);
  14146. if (s == NULL)
  14147. return -1;
  14148. send = s + 2 * count + 1;
  14149. names = (char *) send;
  14150. nend = (char *) s + size;
  14151. n = 0;
  14152. opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
  14153. if (opcode == 0x3302fffe)
  14154. {
  14155. if (!micromips_p)
  14156. return -1;
  14157. plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
  14158. other = STO_MICROMIPS;
  14159. }
  14160. else if (opcode == 0x0398c1d0)
  14161. {
  14162. if (!micromips_p)
  14163. return -1;
  14164. plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
  14165. other = STO_MICROMIPS;
  14166. }
  14167. else
  14168. {
  14169. plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
  14170. other = 0;
  14171. }
  14172. s->the_bfd = abfd;
  14173. s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
  14174. s->section = plt;
  14175. s->value = 0;
  14176. s->name = names;
  14177. s->udata.i = other;
  14178. memcpy (names, pltname, sizeof (pltname));
  14179. names += sizeof (pltname);
  14180. ++s, ++n;
  14181. pi = 0;
  14182. for (plt_offset = plt0_size;
  14183. plt_offset + 8 <= plt->size && s < send;
  14184. plt_offset += entry_size)
  14185. {
  14186. bfd_vma gotplt_addr;
  14187. const char *suffix;
  14188. bfd_vma gotplt_hi;
  14189. bfd_vma gotplt_lo;
  14190. size_t suffixlen;
  14191. opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
  14192. /* Check if the second word matches the expected MIPS16 instruction. */
  14193. if (opcode == 0x651aeb00)
  14194. {
  14195. if (micromips_p)
  14196. return -1;
  14197. /* Truncated table??? */
  14198. if (plt_offset + 16 > plt->size)
  14199. break;
  14200. gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
  14201. entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
  14202. suffixlen = sizeof (m16suffix);
  14203. suffix = m16suffix;
  14204. other = STO_MIPS16;
  14205. }
  14206. /* Likewise the expected microMIPS instruction (no insn32 mode). */
  14207. else if (opcode == 0xff220000)
  14208. {
  14209. if (!micromips_p)
  14210. return -1;
  14211. gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
  14212. gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
  14213. gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
  14214. gotplt_lo <<= 2;
  14215. gotplt_addr = gotplt_hi + gotplt_lo;
  14216. gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
  14217. entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
  14218. suffixlen = sizeof (microsuffix);
  14219. suffix = microsuffix;
  14220. other = STO_MICROMIPS;
  14221. }
  14222. /* Likewise the expected microMIPS instruction (insn32 mode). */
  14223. else if ((opcode & 0xffff0000) == 0xff2f0000)
  14224. {
  14225. gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
  14226. gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
  14227. gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
  14228. gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
  14229. gotplt_addr = gotplt_hi + gotplt_lo;
  14230. entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
  14231. suffixlen = sizeof (microsuffix);
  14232. suffix = microsuffix;
  14233. other = STO_MICROMIPS;
  14234. }
  14235. /* Otherwise assume standard MIPS code. */
  14236. else
  14237. {
  14238. gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
  14239. gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
  14240. gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
  14241. gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
  14242. gotplt_addr = gotplt_hi + gotplt_lo;
  14243. entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
  14244. suffixlen = sizeof (mipssuffix);
  14245. suffix = mipssuffix;
  14246. other = 0;
  14247. }
  14248. /* Truncated table??? */
  14249. if (plt_offset + entry_size > plt->size)
  14250. break;
  14251. for (i = 0;
  14252. i < count && p[pi].address != gotplt_addr;
  14253. i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
  14254. if (i < count)
  14255. {
  14256. size_t namelen;
  14257. size_t len;
  14258. *s = **p[pi].sym_ptr_ptr;
  14259. /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
  14260. we are defining a symbol, ensure one of them is set. */
  14261. if ((s->flags & BSF_LOCAL) == 0)
  14262. s->flags |= BSF_GLOBAL;
  14263. s->flags |= BSF_SYNTHETIC;
  14264. s->section = plt;
  14265. s->value = plt_offset;
  14266. s->name = names;
  14267. s->udata.i = other;
  14268. len = strlen ((*p[pi].sym_ptr_ptr)->name);
  14269. namelen = len + suffixlen;
  14270. if (names + namelen > nend)
  14271. break;
  14272. memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
  14273. names += len;
  14274. memcpy (names, suffix, suffixlen);
  14275. names += suffixlen;
  14276. ++s, ++n;
  14277. pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
  14278. }
  14279. }
  14280. free (plt_data);
  14281. return n;
  14282. }
  14283. /* Return the ABI flags associated with ABFD if available. */
  14284. Elf_Internal_ABIFlags_v0 *
  14285. bfd_mips_elf_get_abiflags (bfd *abfd)
  14286. {
  14287. struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
  14288. return tdata->abiflags_valid ? &tdata->abiflags : NULL;
  14289. }
  14290. /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
  14291. field. Taken from `libc-abis.h' generated at GNU libc build time.
  14292. Using a MIPS_ prefix as other libc targets use different values. */
  14293. enum
  14294. {
  14295. MIPS_LIBC_ABI_DEFAULT = 0,
  14296. MIPS_LIBC_ABI_MIPS_PLT,
  14297. MIPS_LIBC_ABI_UNIQUE,
  14298. MIPS_LIBC_ABI_MIPS_O32_FP64,
  14299. MIPS_LIBC_ABI_ABSOLUTE,
  14300. MIPS_LIBC_ABI_XHASH,
  14301. MIPS_LIBC_ABI_MAX
  14302. };
  14303. bool
  14304. _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
  14305. {
  14306. struct mips_elf_link_hash_table *htab = NULL;
  14307. Elf_Internal_Ehdr *i_ehdrp;
  14308. if (!_bfd_elf_init_file_header (abfd, link_info))
  14309. return false;
  14310. i_ehdrp = elf_elfheader (abfd);
  14311. if (link_info)
  14312. {
  14313. htab = mips_elf_hash_table (link_info);
  14314. BFD_ASSERT (htab != NULL);
  14315. }
  14316. if (htab != NULL
  14317. && htab->use_plts_and_copy_relocs
  14318. && htab->root.target_os != is_vxworks)
  14319. i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
  14320. if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
  14321. || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
  14322. i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
  14323. /* Mark that we need support for absolute symbols in the dynamic loader. */
  14324. if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
  14325. i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
  14326. /* Mark that we need support for .MIPS.xhash in the dynamic linker,
  14327. if it is the only hash section that will be created. */
  14328. if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
  14329. i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
  14330. return true;
  14331. }
  14332. int
  14333. _bfd_mips_elf_compact_eh_encoding
  14334. (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
  14335. {
  14336. return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
  14337. }
  14338. /* Return the opcode for can't unwind. */
  14339. int
  14340. _bfd_mips_elf_cant_unwind_opcode
  14341. (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
  14342. {
  14343. return COMPACT_EH_CANT_UNWIND_OPCODE;
  14344. }
  14345. /* Record a position XLAT_LOC in the xlat translation table, associated with
  14346. the hash entry H. The entry in the translation table will later be
  14347. populated with the real symbol dynindx. */
  14348. void
  14349. _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
  14350. bfd_vma xlat_loc)
  14351. {
  14352. struct mips_elf_link_hash_entry *hmips;
  14353. hmips = (struct mips_elf_link_hash_entry *) h;
  14354. hmips->mipsxhash_loc = xlat_loc;
  14355. }