elf32-nios2.c 164 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297
  1. /* 32-bit ELF support for Nios II.
  2. Copyright (C) 2012-2015 Free Software Foundation, Inc.
  3. Contributed by Nigel Gray (ngray@altera.com).
  4. Contributed by Mentor Graphics, Inc.
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. /* This file handles Altera Nios II ELF targets. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "libbfd.h"
  22. #include "bfdlink.h"
  23. #include "genlink.h"
  24. #include "elf-bfd.h"
  25. #include "elf/nios2.h"
  26. #include "opcode/nios2.h"
  27. #include "elf32-nios2.h"
  28. /* Use RELA relocations. */
  29. #ifndef USE_RELA
  30. #define USE_RELA
  31. #endif
  32. #ifdef USE_REL
  33. #undef USE_REL
  34. #endif
  35. /* Forward declarations. */
  36. static bfd_reloc_status_type nios2_elf32_ignore_reloc
  37. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  38. static bfd_reloc_status_type nios2_elf32_hi16_relocate
  39. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  40. static bfd_reloc_status_type nios2_elf32_lo16_relocate
  41. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  42. static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
  43. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  44. static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
  45. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  46. static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
  47. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  48. static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
  49. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  50. static bfd_reloc_status_type nios2_elf32_call26_relocate
  51. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  52. static bfd_reloc_status_type nios2_elf32_gprel_relocate
  53. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  54. static bfd_reloc_status_type nios2_elf32_ujmp_relocate
  55. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  56. static bfd_reloc_status_type nios2_elf32_cjmp_relocate
  57. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  58. static bfd_reloc_status_type nios2_elf32_callr_relocate
  59. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  60. /* Target vector. */
  61. extern const bfd_target nios2_elf32_le_vec;
  62. extern const bfd_target nios2_elf32_be_vec;
  63. /* Offset of tp and dtp pointers from start of TLS block. */
  64. #define TP_OFFSET 0x7000
  65. #define DTP_OFFSET 0x8000
  66. /* The relocation tables used for SHT_REL sections. There are separate
  67. tables for R1 and R2 encodings. */
  68. static reloc_howto_type elf_nios2_r1_howto_table_rel[] = {
  69. /* No relocation. */
  70. HOWTO (R_NIOS2_NONE, /* type */
  71. 0, /* rightshift */
  72. 3, /* size (0 = byte, 1 = short, 2 = long) */
  73. 0, /* bitsize */
  74. FALSE, /* pc_relative */
  75. 0, /* bitpos */
  76. complain_overflow_dont, /* complain_on_overflow */
  77. bfd_elf_generic_reloc, /* special_function */
  78. "R_NIOS2_NONE", /* name */
  79. FALSE, /* partial_inplace */
  80. 0, /* src_mask */
  81. 0, /* dst_mask */
  82. FALSE), /* pcrel_offset */
  83. /* 16-bit signed immediate relocation. */
  84. HOWTO (R_NIOS2_S16, /* type */
  85. 0, /* rightshift */
  86. 2, /* size (0 = byte, 1 = short, 2 = long) */
  87. 16, /* bitsize */
  88. FALSE, /* pc_relative */
  89. 6, /* bitpos */
  90. complain_overflow_signed, /* complain on overflow */
  91. bfd_elf_generic_reloc, /* special function */
  92. "R_NIOS2_S16", /* name */
  93. FALSE, /* partial_inplace */
  94. 0x003fffc0, /* src_mask */
  95. 0x003fffc0, /* dest_mask */
  96. FALSE), /* pcrel_offset */
  97. /* 16-bit unsigned immediate relocation. */
  98. HOWTO (R_NIOS2_U16, /* type */
  99. 0, /* rightshift */
  100. 2, /* size (0 = byte, 1 = short, 2 = long) */
  101. 16, /* bitsize */
  102. FALSE, /* pc_relative */
  103. 6, /* bitpos */
  104. complain_overflow_unsigned, /* complain on overflow */
  105. bfd_elf_generic_reloc, /* special function */
  106. "R_NIOS2_U16", /* name */
  107. FALSE, /* partial_inplace */
  108. 0x003fffc0, /* src_mask */
  109. 0x003fffc0, /* dest_mask */
  110. FALSE), /* pcrel_offset */
  111. HOWTO (R_NIOS2_PCREL16, /* type */
  112. 0, /* rightshift */
  113. 2, /* size (0 = byte, 1 = short, 2 = long) */
  114. 16, /* bitsize */
  115. TRUE, /* pc_relative */
  116. 6, /* bitpos */
  117. complain_overflow_signed, /* complain on overflow */
  118. nios2_elf32_pcrel16_relocate, /* special function */
  119. "R_NIOS2_PCREL16", /* name */
  120. FALSE, /* partial_inplace */
  121. 0x003fffc0, /* src_mask */
  122. 0x003fffc0, /* dest_mask */
  123. TRUE), /* pcrel_offset */
  124. HOWTO (R_NIOS2_CALL26, /* type */
  125. 2, /* rightshift */
  126. 2, /* size (0 = byte, 1 = short, 2 = long) */
  127. 26, /* bitsize */
  128. FALSE, /* pc_relative */
  129. 6, /* bitpos */
  130. complain_overflow_dont, /* complain on overflow */
  131. nios2_elf32_call26_relocate, /* special function */
  132. "R_NIOS2_CALL26", /* name */
  133. FALSE, /* partial_inplace */
  134. 0xffffffc0, /* src_mask */
  135. 0xffffffc0, /* dst_mask */
  136. FALSE), /* pcrel_offset */
  137. HOWTO (R_NIOS2_IMM5,
  138. 0,
  139. 2,
  140. 5,
  141. FALSE,
  142. 6,
  143. complain_overflow_bitfield,
  144. bfd_elf_generic_reloc,
  145. "R_NIOS2_IMM5",
  146. FALSE,
  147. 0x000007c0,
  148. 0x000007c0,
  149. FALSE),
  150. HOWTO (R_NIOS2_CACHE_OPX,
  151. 0,
  152. 2,
  153. 5,
  154. FALSE,
  155. 22,
  156. complain_overflow_bitfield,
  157. bfd_elf_generic_reloc,
  158. "R_NIOS2_CACHE_OPX",
  159. FALSE,
  160. 0x07c00000,
  161. 0x07c00000,
  162. FALSE),
  163. HOWTO (R_NIOS2_IMM6,
  164. 0,
  165. 2,
  166. 6,
  167. FALSE,
  168. 6,
  169. complain_overflow_bitfield,
  170. bfd_elf_generic_reloc,
  171. "R_NIOS2_IMM6",
  172. FALSE,
  173. 0x00000fc0,
  174. 0x00000fc0,
  175. FALSE),
  176. HOWTO (R_NIOS2_IMM8,
  177. 0,
  178. 2,
  179. 8,
  180. FALSE,
  181. 6,
  182. complain_overflow_bitfield,
  183. bfd_elf_generic_reloc,
  184. "R_NIOS2_IMM8",
  185. FALSE,
  186. 0x00003fc0,
  187. 0x00003fc0,
  188. FALSE),
  189. HOWTO (R_NIOS2_HI16,
  190. 0,
  191. 2,
  192. 32,
  193. FALSE,
  194. 6,
  195. complain_overflow_dont,
  196. nios2_elf32_hi16_relocate,
  197. "R_NIOS2_HI16",
  198. FALSE,
  199. 0x003fffc0,
  200. 0x003fffc0,
  201. FALSE),
  202. HOWTO (R_NIOS2_LO16,
  203. 0,
  204. 2,
  205. 32,
  206. FALSE,
  207. 6,
  208. complain_overflow_dont,
  209. nios2_elf32_lo16_relocate,
  210. "R_NIOS2_LO16",
  211. FALSE,
  212. 0x003fffc0,
  213. 0x003fffc0,
  214. FALSE),
  215. HOWTO (R_NIOS2_HIADJ16,
  216. 0,
  217. 2,
  218. 32,
  219. FALSE,
  220. 6,
  221. complain_overflow_dont,
  222. nios2_elf32_hiadj16_relocate,
  223. "R_NIOS2_HIADJ16",
  224. FALSE,
  225. 0x003fffc0,
  226. 0x003fffc0,
  227. FALSE),
  228. HOWTO (R_NIOS2_BFD_RELOC_32,
  229. 0,
  230. 2, /* long */
  231. 32,
  232. FALSE,
  233. 0,
  234. complain_overflow_dont,
  235. bfd_elf_generic_reloc,
  236. "R_NIOS2_BFD_RELOC32",
  237. FALSE,
  238. 0xffffffff,
  239. 0xffffffff,
  240. FALSE),
  241. HOWTO (R_NIOS2_BFD_RELOC_16,
  242. 0,
  243. 1, /* short */
  244. 16,
  245. FALSE,
  246. 0,
  247. complain_overflow_bitfield,
  248. bfd_elf_generic_reloc,
  249. "R_NIOS2_BFD_RELOC16",
  250. FALSE,
  251. 0x0000ffff,
  252. 0x0000ffff,
  253. FALSE),
  254. HOWTO (R_NIOS2_BFD_RELOC_8,
  255. 0,
  256. 0, /* byte */
  257. 8,
  258. FALSE,
  259. 0,
  260. complain_overflow_bitfield,
  261. bfd_elf_generic_reloc,
  262. "R_NIOS2_BFD_RELOC8",
  263. FALSE,
  264. 0x000000ff,
  265. 0x000000ff,
  266. FALSE),
  267. HOWTO (R_NIOS2_GPREL,
  268. 0,
  269. 2,
  270. 32,
  271. FALSE,
  272. 6,
  273. complain_overflow_dont,
  274. nios2_elf32_gprel_relocate,
  275. "R_NIOS2_GPREL",
  276. FALSE,
  277. 0x003fffc0,
  278. 0x003fffc0,
  279. FALSE),
  280. HOWTO (R_NIOS2_GNU_VTINHERIT,
  281. 0,
  282. 2, /* short */
  283. 0,
  284. FALSE,
  285. 0,
  286. complain_overflow_dont,
  287. NULL,
  288. "R_NIOS2_GNU_VTINHERIT",
  289. FALSE,
  290. 0,
  291. 0,
  292. FALSE),
  293. HOWTO (R_NIOS2_GNU_VTENTRY,
  294. 0,
  295. 2, /* byte */
  296. 0,
  297. FALSE,
  298. 0,
  299. complain_overflow_dont,
  300. _bfd_elf_rel_vtable_reloc_fn,
  301. "R_NIOS2_GNU_VTENTRY",
  302. FALSE,
  303. 0,
  304. 0,
  305. FALSE),
  306. HOWTO (R_NIOS2_UJMP,
  307. 0,
  308. 2,
  309. 32,
  310. FALSE,
  311. 6,
  312. complain_overflow_dont,
  313. nios2_elf32_ujmp_relocate,
  314. "R_NIOS2_UJMP",
  315. FALSE,
  316. 0x003fffc0,
  317. 0x003fffc0,
  318. FALSE),
  319. HOWTO (R_NIOS2_CJMP,
  320. 0,
  321. 2,
  322. 32,
  323. FALSE,
  324. 6,
  325. complain_overflow_dont,
  326. nios2_elf32_cjmp_relocate,
  327. "R_NIOS2_CJMP",
  328. FALSE,
  329. 0x003fffc0,
  330. 0x003fffc0,
  331. FALSE),
  332. HOWTO (R_NIOS2_CALLR,
  333. 0,
  334. 2,
  335. 32,
  336. FALSE,
  337. 6,
  338. complain_overflow_dont,
  339. nios2_elf32_callr_relocate,
  340. "R_NIOS2_CALLR",
  341. FALSE,
  342. 0x003fffc0,
  343. 0x003fffc0,
  344. FALSE),
  345. HOWTO (R_NIOS2_ALIGN,
  346. 0,
  347. 2,
  348. 0,
  349. FALSE,
  350. 0,
  351. complain_overflow_dont,
  352. nios2_elf32_ignore_reloc,
  353. "R_NIOS2_ALIGN",
  354. FALSE,
  355. 0,
  356. 0,
  357. TRUE),
  358. HOWTO (R_NIOS2_GOT16,
  359. 0,
  360. 2,
  361. 16,
  362. FALSE,
  363. 6,
  364. complain_overflow_bitfield,
  365. bfd_elf_generic_reloc,
  366. "R_NIOS2_GOT16",
  367. FALSE,
  368. 0x003fffc0,
  369. 0x003fffc0,
  370. FALSE),
  371. HOWTO (R_NIOS2_CALL16,
  372. 0,
  373. 2,
  374. 16,
  375. FALSE,
  376. 6,
  377. complain_overflow_bitfield,
  378. bfd_elf_generic_reloc,
  379. "R_NIOS2_CALL16",
  380. FALSE,
  381. 0x003fffc0,
  382. 0x003fffc0,
  383. FALSE),
  384. HOWTO (R_NIOS2_GOTOFF_LO,
  385. 0,
  386. 2,
  387. 16,
  388. FALSE,
  389. 6,
  390. complain_overflow_dont,
  391. bfd_elf_generic_reloc,
  392. "R_NIOS2_GOTOFF_LO",
  393. FALSE,
  394. 0x003fffc0,
  395. 0x003fffc0,
  396. FALSE),
  397. HOWTO (R_NIOS2_GOTOFF_HA,
  398. 0,
  399. 2,
  400. 16,
  401. FALSE,
  402. 6,
  403. complain_overflow_dont,
  404. bfd_elf_generic_reloc,
  405. "R_NIOS2_GOTOFF_HA",
  406. FALSE,
  407. 0x003fffc0,
  408. 0x003fffc0,
  409. FALSE),
  410. HOWTO (R_NIOS2_PCREL_LO,
  411. 0,
  412. 2,
  413. 16,
  414. TRUE,
  415. 6,
  416. complain_overflow_dont,
  417. nios2_elf32_pcrel_lo16_relocate,
  418. "R_NIOS2_PCREL_LO",
  419. FALSE,
  420. 0x003fffc0,
  421. 0x003fffc0,
  422. TRUE),
  423. HOWTO (R_NIOS2_PCREL_HA,
  424. 0,
  425. 2,
  426. 16,
  427. FALSE, /* This is a PC-relative relocation, but we need to subtract
  428. PC ourselves before the HIADJ. */
  429. 6,
  430. complain_overflow_dont,
  431. nios2_elf32_pcrel_hiadj16_relocate,
  432. "R_NIOS2_PCREL_HA",
  433. FALSE,
  434. 0x003fffc0,
  435. 0x003fffc0,
  436. TRUE),
  437. HOWTO (R_NIOS2_TLS_GD16,
  438. 0,
  439. 2,
  440. 16,
  441. FALSE,
  442. 6,
  443. complain_overflow_bitfield,
  444. bfd_elf_generic_reloc,
  445. "R_NIOS2_TLS_GD16",
  446. FALSE,
  447. 0x003fffc0,
  448. 0x003fffc0,
  449. FALSE),
  450. HOWTO (R_NIOS2_TLS_LDM16,
  451. 0,
  452. 2,
  453. 16,
  454. FALSE,
  455. 6,
  456. complain_overflow_bitfield,
  457. bfd_elf_generic_reloc,
  458. "R_NIOS2_TLS_LDM16",
  459. FALSE,
  460. 0x003fffc0,
  461. 0x003fffc0,
  462. FALSE),
  463. HOWTO (R_NIOS2_TLS_LDO16,
  464. 0,
  465. 2,
  466. 16,
  467. FALSE,
  468. 6,
  469. complain_overflow_bitfield,
  470. bfd_elf_generic_reloc,
  471. "R_NIOS2_TLS_LDO16",
  472. FALSE,
  473. 0x003fffc0,
  474. 0x003fffc0,
  475. FALSE),
  476. HOWTO (R_NIOS2_TLS_IE16,
  477. 0,
  478. 2,
  479. 16,
  480. FALSE,
  481. 6,
  482. complain_overflow_bitfield,
  483. bfd_elf_generic_reloc,
  484. "R_NIOS2_TLS_IE16",
  485. FALSE,
  486. 0x003fffc0,
  487. 0x003fffc0,
  488. FALSE),
  489. HOWTO (R_NIOS2_TLS_LE16,
  490. 0,
  491. 2,
  492. 16,
  493. FALSE,
  494. 6,
  495. complain_overflow_bitfield,
  496. bfd_elf_generic_reloc,
  497. "R_NIOS2_TLS_LE16",
  498. FALSE,
  499. 0x003fffc0,
  500. 0x003fffc0,
  501. FALSE),
  502. HOWTO (R_NIOS2_TLS_DTPMOD,
  503. 0,
  504. 2,
  505. 32,
  506. FALSE,
  507. 0,
  508. complain_overflow_dont,
  509. bfd_elf_generic_reloc,
  510. "R_NIOS2_TLS_DTPMOD",
  511. FALSE,
  512. 0xffffffff,
  513. 0xffffffff,
  514. FALSE),
  515. HOWTO (R_NIOS2_TLS_DTPREL,
  516. 0,
  517. 2,
  518. 32,
  519. FALSE,
  520. 0,
  521. complain_overflow_dont,
  522. bfd_elf_generic_reloc,
  523. "R_NIOS2_TLS_DTPREL",
  524. FALSE,
  525. 0xffffffff,
  526. 0xffffffff,
  527. FALSE),
  528. HOWTO (R_NIOS2_TLS_TPREL,
  529. 0,
  530. 2,
  531. 32,
  532. FALSE,
  533. 0,
  534. complain_overflow_dont,
  535. bfd_elf_generic_reloc,
  536. "R_NIOS2_TLS_TPREL",
  537. FALSE,
  538. 0xffffffff,
  539. 0xffffffff,
  540. FALSE),
  541. HOWTO (R_NIOS2_COPY,
  542. 0,
  543. 2,
  544. 32,
  545. FALSE,
  546. 0,
  547. complain_overflow_dont,
  548. bfd_elf_generic_reloc,
  549. "R_NIOS2_COPY",
  550. FALSE,
  551. 0,
  552. 0,
  553. FALSE),
  554. HOWTO (R_NIOS2_GLOB_DAT,
  555. 0,
  556. 2,
  557. 32,
  558. FALSE,
  559. 0,
  560. complain_overflow_dont,
  561. bfd_elf_generic_reloc,
  562. "R_NIOS2_GLOB_DAT",
  563. FALSE,
  564. 0xffffffff,
  565. 0xffffffff,
  566. FALSE),
  567. HOWTO (R_NIOS2_JUMP_SLOT,
  568. 0,
  569. 2,
  570. 32,
  571. FALSE,
  572. 0,
  573. complain_overflow_dont,
  574. bfd_elf_generic_reloc,
  575. "R_NIOS2_JUMP_SLOT",
  576. FALSE,
  577. 0xffffffff,
  578. 0xffffffff,
  579. FALSE),
  580. HOWTO (R_NIOS2_RELATIVE,
  581. 0,
  582. 2,
  583. 32,
  584. FALSE,
  585. 0,
  586. complain_overflow_dont,
  587. bfd_elf_generic_reloc,
  588. "R_NIOS2_RELATIVE",
  589. FALSE,
  590. 0xffffffff,
  591. 0xffffffff,
  592. FALSE),
  593. HOWTO (R_NIOS2_GOTOFF,
  594. 0,
  595. 2,
  596. 32,
  597. FALSE,
  598. 0,
  599. complain_overflow_dont,
  600. bfd_elf_generic_reloc,
  601. "R_NIOS2_GOTOFF",
  602. FALSE,
  603. 0xffffffff,
  604. 0xffffffff,
  605. FALSE),
  606. HOWTO (R_NIOS2_CALL26_NOAT, /* type */
  607. 2, /* rightshift */
  608. 2, /* size (0 = byte, 1 = short, 2 = long) */
  609. 26, /* bitsize */
  610. FALSE, /* pc_relative */
  611. 6, /* bitpos */
  612. complain_overflow_dont, /* complain on overflow */
  613. nios2_elf32_call26_relocate, /* special function */
  614. "R_NIOS2_CALL26_NOAT", /* name */
  615. FALSE, /* partial_inplace */
  616. 0xffffffc0, /* src_mask */
  617. 0xffffffc0, /* dst_mask */
  618. FALSE), /* pcrel_offset */
  619. HOWTO (R_NIOS2_GOT_LO,
  620. 0,
  621. 2,
  622. 16,
  623. FALSE,
  624. 6,
  625. complain_overflow_dont,
  626. bfd_elf_generic_reloc,
  627. "R_NIOS2_GOT_LO",
  628. FALSE,
  629. 0x003fffc0,
  630. 0x003fffc0,
  631. FALSE),
  632. HOWTO (R_NIOS2_GOT_HA,
  633. 0,
  634. 2,
  635. 16,
  636. FALSE,
  637. 6,
  638. complain_overflow_dont,
  639. bfd_elf_generic_reloc,
  640. "R_NIOS2_GOT_HA",
  641. FALSE,
  642. 0x003fffc0,
  643. 0x003fffc0,
  644. FALSE),
  645. HOWTO (R_NIOS2_CALL_LO,
  646. 0,
  647. 2,
  648. 16,
  649. FALSE,
  650. 6,
  651. complain_overflow_dont,
  652. bfd_elf_generic_reloc,
  653. "R_NIOS2_CALL_LO",
  654. FALSE,
  655. 0x003fffc0,
  656. 0x003fffc0,
  657. FALSE),
  658. HOWTO (R_NIOS2_CALL_HA,
  659. 0,
  660. 2,
  661. 16,
  662. FALSE,
  663. 6,
  664. complain_overflow_dont,
  665. bfd_elf_generic_reloc,
  666. "R_NIOS2_CALL_HA",
  667. FALSE,
  668. 0x003fffc0,
  669. 0x003fffc0,
  670. FALSE),
  671. /* Add other relocations here. */
  672. };
  673. static reloc_howto_type elf_nios2_r2_howto_table_rel[] = {
  674. /* No relocation. */
  675. HOWTO (R_NIOS2_NONE, /* type */
  676. 0, /* rightshift */
  677. 0, /* size (0 = byte, 1 = short, 2 = long) */
  678. 0, /* bitsize */
  679. FALSE, /* pc_relative */
  680. 0, /* bitpos */
  681. complain_overflow_dont, /* complain_on_overflow */
  682. bfd_elf_generic_reloc, /* special_function */
  683. "R_NIOS2_NONE", /* name */
  684. FALSE, /* partial_inplace */
  685. 0, /* src_mask */
  686. 0, /* dst_mask */
  687. FALSE), /* pcrel_offset */
  688. /* 16-bit signed immediate relocation. */
  689. HOWTO (R_NIOS2_S16, /* type */
  690. 0, /* rightshift */
  691. 2, /* size (0 = byte, 1 = short, 2 = long) */
  692. 16, /* bitsize */
  693. FALSE, /* pc_relative */
  694. 16, /* bitpos */
  695. complain_overflow_signed, /* complain on overflow */
  696. bfd_elf_generic_reloc, /* special function */
  697. "R_NIOS2_S16", /* name */
  698. FALSE, /* partial_inplace */
  699. 0xffff0000, /* src_mask */
  700. 0xffff0000, /* dest_mask */
  701. FALSE), /* pcrel_offset */
  702. /* 16-bit unsigned immediate relocation. */
  703. HOWTO (R_NIOS2_U16, /* type */
  704. 0, /* rightshift */
  705. 2, /* size (0 = byte, 1 = short, 2 = long) */
  706. 16, /* bitsize */
  707. FALSE, /* pc_relative */
  708. 16, /* bitpos */
  709. complain_overflow_unsigned, /* complain on overflow */
  710. bfd_elf_generic_reloc, /* special function */
  711. "R_NIOS2_U16", /* name */
  712. FALSE, /* partial_inplace */
  713. 0xffff0000, /* src_mask */
  714. 0xffff0000, /* dest_mask */
  715. FALSE), /* pcrel_offset */
  716. HOWTO (R_NIOS2_PCREL16, /* type */
  717. 0, /* rightshift */
  718. 2, /* size (0 = byte, 1 = short, 2 = long) */
  719. 16, /* bitsize */
  720. TRUE, /* pc_relative */
  721. 16, /* bitpos */
  722. complain_overflow_signed, /* complain on overflow */
  723. nios2_elf32_pcrel16_relocate, /* special function */
  724. "R_NIOS2_PCREL16", /* name */
  725. FALSE, /* partial_inplace */
  726. 0xffff0000, /* src_mask */
  727. 0xffff0000, /* dest_mask */
  728. TRUE), /* pcrel_offset */
  729. HOWTO (R_NIOS2_CALL26, /* type */
  730. 2, /* rightshift */
  731. 2, /* size (0 = byte, 1 = short, 2 = long) */
  732. 26, /* bitsize */
  733. FALSE, /* pc_relative */
  734. 6, /* bitpos */
  735. complain_overflow_dont, /* complain on overflow */
  736. nios2_elf32_call26_relocate, /* special function */
  737. "R_NIOS2_CALL26", /* name */
  738. FALSE, /* partial_inplace */
  739. 0xffffffc0, /* src_mask */
  740. 0xffffffc0, /* dst_mask */
  741. FALSE), /* pcrel_offset */
  742. HOWTO (R_NIOS2_IMM5,
  743. 0,
  744. 2,
  745. 5,
  746. FALSE,
  747. 21,
  748. complain_overflow_bitfield,
  749. bfd_elf_generic_reloc,
  750. "R_NIOS2_IMM5",
  751. FALSE,
  752. 0x03e00000,
  753. 0x03e00000,
  754. FALSE),
  755. HOWTO (R_NIOS2_CACHE_OPX,
  756. 0,
  757. 2,
  758. 5,
  759. FALSE,
  760. 11,
  761. complain_overflow_bitfield,
  762. bfd_elf_generic_reloc,
  763. "R_NIOS2_CACHE_OPX",
  764. FALSE,
  765. 0x0000f800,
  766. 0x0000f800,
  767. FALSE),
  768. HOWTO (R_NIOS2_IMM6,
  769. 0,
  770. 2,
  771. 6,
  772. FALSE,
  773. 26,
  774. complain_overflow_bitfield,
  775. bfd_elf_generic_reloc,
  776. "R_NIOS2_IMM6",
  777. FALSE,
  778. 0xfc000000,
  779. 0xfc000000,
  780. FALSE),
  781. HOWTO (R_NIOS2_IMM8,
  782. 0,
  783. 2,
  784. 8,
  785. FALSE,
  786. 24,
  787. complain_overflow_bitfield,
  788. bfd_elf_generic_reloc,
  789. "R_NIOS2_IMM8",
  790. FALSE,
  791. 0xff000000,
  792. 0xff000000,
  793. FALSE),
  794. HOWTO (R_NIOS2_HI16,
  795. 0,
  796. 2,
  797. 32,
  798. FALSE,
  799. 16,
  800. complain_overflow_dont,
  801. nios2_elf32_hi16_relocate,
  802. "R_NIOS2_HI16",
  803. FALSE,
  804. 0xffff0000,
  805. 0xffff0000,
  806. FALSE),
  807. HOWTO (R_NIOS2_LO16,
  808. 0,
  809. 2,
  810. 32,
  811. FALSE,
  812. 16,
  813. complain_overflow_dont,
  814. nios2_elf32_lo16_relocate,
  815. "R_NIOS2_LO16",
  816. FALSE,
  817. 0xffff0000,
  818. 0xffff0000,
  819. FALSE),
  820. HOWTO (R_NIOS2_HIADJ16,
  821. 0,
  822. 2,
  823. 32,
  824. FALSE,
  825. 16,
  826. complain_overflow_dont,
  827. nios2_elf32_hiadj16_relocate,
  828. "R_NIOS2_HIADJ16",
  829. FALSE,
  830. 0xffff0000,
  831. 0xffff0000,
  832. FALSE),
  833. HOWTO (R_NIOS2_BFD_RELOC_32,
  834. 0,
  835. 2, /* long */
  836. 32,
  837. FALSE,
  838. 0,
  839. complain_overflow_dont,
  840. bfd_elf_generic_reloc,
  841. "R_NIOS2_BFD_RELOC32",
  842. FALSE,
  843. 0xffffffff,
  844. 0xffffffff,
  845. FALSE),
  846. HOWTO (R_NIOS2_BFD_RELOC_16,
  847. 0,
  848. 1, /* short */
  849. 16,
  850. FALSE,
  851. 0,
  852. complain_overflow_bitfield,
  853. bfd_elf_generic_reloc,
  854. "R_NIOS2_BFD_RELOC16",
  855. FALSE,
  856. 0x0000ffff,
  857. 0x0000ffff,
  858. FALSE),
  859. HOWTO (R_NIOS2_BFD_RELOC_8,
  860. 0,
  861. 0, /* byte */
  862. 8,
  863. FALSE,
  864. 0,
  865. complain_overflow_bitfield,
  866. bfd_elf_generic_reloc,
  867. "R_NIOS2_BFD_RELOC8",
  868. FALSE,
  869. 0x000000ff,
  870. 0x000000ff,
  871. FALSE),
  872. HOWTO (R_NIOS2_GPREL,
  873. 0,
  874. 2,
  875. 32,
  876. FALSE,
  877. 16,
  878. complain_overflow_dont,
  879. nios2_elf32_gprel_relocate,
  880. "R_NIOS2_GPREL",
  881. FALSE,
  882. 0xffff0000,
  883. 0xffff0000,
  884. FALSE),
  885. HOWTO (R_NIOS2_GNU_VTINHERIT,
  886. 0,
  887. 2, /* short */
  888. 0,
  889. FALSE,
  890. 0,
  891. complain_overflow_dont,
  892. NULL,
  893. "R_NIOS2_GNU_VTINHERIT",
  894. FALSE,
  895. 0,
  896. 0,
  897. FALSE),
  898. HOWTO (R_NIOS2_GNU_VTENTRY,
  899. 0,
  900. 2, /* byte */
  901. 0,
  902. FALSE,
  903. 0,
  904. complain_overflow_dont,
  905. _bfd_elf_rel_vtable_reloc_fn,
  906. "R_NIOS2_GNU_VTENTRY",
  907. FALSE,
  908. 0,
  909. 0,
  910. FALSE),
  911. HOWTO (R_NIOS2_UJMP,
  912. 0,
  913. 2,
  914. 32,
  915. FALSE,
  916. 16,
  917. complain_overflow_dont,
  918. nios2_elf32_ujmp_relocate,
  919. "R_NIOS2_UJMP",
  920. FALSE,
  921. 0xffff0000,
  922. 0xffff0000,
  923. FALSE),
  924. HOWTO (R_NIOS2_CJMP,
  925. 0,
  926. 2,
  927. 32,
  928. FALSE,
  929. 16,
  930. complain_overflow_dont,
  931. nios2_elf32_cjmp_relocate,
  932. "R_NIOS2_CJMP",
  933. FALSE,
  934. 0xffff0000,
  935. 0xffff0000,
  936. FALSE),
  937. HOWTO (R_NIOS2_CALLR,
  938. 0,
  939. 2,
  940. 32,
  941. FALSE,
  942. 16,
  943. complain_overflow_dont,
  944. nios2_elf32_callr_relocate,
  945. "R_NIOS2_CALLR",
  946. FALSE,
  947. 0xffff0000,
  948. 0xffff0000,
  949. FALSE),
  950. HOWTO (R_NIOS2_ALIGN,
  951. 0,
  952. 2,
  953. 0,
  954. FALSE,
  955. 0,
  956. complain_overflow_dont,
  957. nios2_elf32_ignore_reloc,
  958. "R_NIOS2_ALIGN",
  959. FALSE,
  960. 0,
  961. 0,
  962. TRUE),
  963. HOWTO (R_NIOS2_GOT16,
  964. 0,
  965. 2,
  966. 16,
  967. FALSE,
  968. 16,
  969. complain_overflow_bitfield,
  970. bfd_elf_generic_reloc,
  971. "R_NIOS2_GOT16",
  972. FALSE,
  973. 0xffff0000,
  974. 0xffff0000,
  975. FALSE),
  976. HOWTO (R_NIOS2_CALL16,
  977. 0,
  978. 2,
  979. 16,
  980. FALSE,
  981. 16,
  982. complain_overflow_bitfield,
  983. bfd_elf_generic_reloc,
  984. "R_NIOS2_CALL16",
  985. FALSE,
  986. 0xffff0000,
  987. 0xffff0000,
  988. FALSE),
  989. HOWTO (R_NIOS2_GOTOFF_LO,
  990. 0,
  991. 2,
  992. 16,
  993. FALSE,
  994. 16,
  995. complain_overflow_dont,
  996. bfd_elf_generic_reloc,
  997. "R_NIOS2_GOTOFF_LO",
  998. FALSE,
  999. 0xffff0000,
  1000. 0xffff0000,
  1001. FALSE),
  1002. HOWTO (R_NIOS2_GOTOFF_HA,
  1003. 0,
  1004. 2,
  1005. 16,
  1006. FALSE,
  1007. 16,
  1008. complain_overflow_dont,
  1009. bfd_elf_generic_reloc,
  1010. "R_NIOS2_GOTOFF_HA",
  1011. FALSE,
  1012. 0xffff0000,
  1013. 0xffff0000,
  1014. FALSE),
  1015. HOWTO (R_NIOS2_PCREL_LO,
  1016. 0,
  1017. 2,
  1018. 16,
  1019. TRUE,
  1020. 16,
  1021. complain_overflow_dont,
  1022. nios2_elf32_pcrel_lo16_relocate,
  1023. "R_NIOS2_PCREL_LO",
  1024. FALSE,
  1025. 0xffff0000,
  1026. 0xffff0000,
  1027. TRUE),
  1028. HOWTO (R_NIOS2_PCREL_HA,
  1029. 0,
  1030. 2,
  1031. 16,
  1032. FALSE, /* This is a PC-relative relocation, but we need to subtract
  1033. PC ourselves before the HIADJ. */
  1034. 16,
  1035. complain_overflow_dont,
  1036. nios2_elf32_pcrel_hiadj16_relocate,
  1037. "R_NIOS2_PCREL_HA",
  1038. FALSE,
  1039. 0xffff0000,
  1040. 0xffff0000,
  1041. TRUE),
  1042. HOWTO (R_NIOS2_TLS_GD16,
  1043. 0,
  1044. 2,
  1045. 16,
  1046. FALSE,
  1047. 16,
  1048. complain_overflow_bitfield,
  1049. bfd_elf_generic_reloc,
  1050. "R_NIOS2_TLS_GD16",
  1051. FALSE,
  1052. 0xffff0000,
  1053. 0xffff0000,
  1054. FALSE),
  1055. HOWTO (R_NIOS2_TLS_LDM16,
  1056. 0,
  1057. 2,
  1058. 16,
  1059. FALSE,
  1060. 16,
  1061. complain_overflow_bitfield,
  1062. bfd_elf_generic_reloc,
  1063. "R_NIOS2_TLS_LDM16",
  1064. FALSE,
  1065. 0xffff0000,
  1066. 0xffff0000,
  1067. FALSE),
  1068. HOWTO (R_NIOS2_TLS_LDO16,
  1069. 0,
  1070. 2,
  1071. 16,
  1072. FALSE,
  1073. 16,
  1074. complain_overflow_bitfield,
  1075. bfd_elf_generic_reloc,
  1076. "R_NIOS2_TLS_LDO16",
  1077. FALSE,
  1078. 0xffff0000,
  1079. 0xffff0000,
  1080. FALSE),
  1081. HOWTO (R_NIOS2_TLS_IE16,
  1082. 0,
  1083. 2,
  1084. 16,
  1085. FALSE,
  1086. 16,
  1087. complain_overflow_bitfield,
  1088. bfd_elf_generic_reloc,
  1089. "R_NIOS2_TLS_IE16",
  1090. FALSE,
  1091. 0xffff0000,
  1092. 0xffff0000,
  1093. FALSE),
  1094. HOWTO (R_NIOS2_TLS_LE16,
  1095. 0,
  1096. 2,
  1097. 16,
  1098. FALSE,
  1099. 16,
  1100. complain_overflow_bitfield,
  1101. bfd_elf_generic_reloc,
  1102. "R_NIOS2_TLS_LE16",
  1103. FALSE,
  1104. 0xffff0000,
  1105. 0xffff0000,
  1106. FALSE),
  1107. HOWTO (R_NIOS2_TLS_DTPMOD,
  1108. 0,
  1109. 2,
  1110. 32,
  1111. FALSE,
  1112. 0,
  1113. complain_overflow_dont,
  1114. bfd_elf_generic_reloc,
  1115. "R_NIOS2_TLS_DTPMOD",
  1116. FALSE,
  1117. 0xffffffff,
  1118. 0xffffffff,
  1119. FALSE),
  1120. HOWTO (R_NIOS2_TLS_DTPREL,
  1121. 0,
  1122. 2,
  1123. 32,
  1124. FALSE,
  1125. 0,
  1126. complain_overflow_dont,
  1127. bfd_elf_generic_reloc,
  1128. "R_NIOS2_TLS_DTPREL",
  1129. FALSE,
  1130. 0xffffffff,
  1131. 0xffffffff,
  1132. FALSE),
  1133. HOWTO (R_NIOS2_TLS_TPREL,
  1134. 0,
  1135. 2,
  1136. 32,
  1137. FALSE,
  1138. 0,
  1139. complain_overflow_dont,
  1140. bfd_elf_generic_reloc,
  1141. "R_NIOS2_TLS_TPREL",
  1142. FALSE,
  1143. 0xffffffff,
  1144. 0xffffffff,
  1145. FALSE),
  1146. HOWTO (R_NIOS2_COPY,
  1147. 0,
  1148. 2,
  1149. 32,
  1150. FALSE,
  1151. 0,
  1152. complain_overflow_dont,
  1153. bfd_elf_generic_reloc,
  1154. "R_NIOS2_COPY",
  1155. FALSE,
  1156. 0,
  1157. 0,
  1158. FALSE),
  1159. HOWTO (R_NIOS2_GLOB_DAT,
  1160. 0,
  1161. 2,
  1162. 32,
  1163. FALSE,
  1164. 0,
  1165. complain_overflow_dont,
  1166. bfd_elf_generic_reloc,
  1167. "R_NIOS2_GLOB_DAT",
  1168. FALSE,
  1169. 0xffffffff,
  1170. 0xffffffff,
  1171. FALSE),
  1172. HOWTO (R_NIOS2_JUMP_SLOT,
  1173. 0,
  1174. 2,
  1175. 32,
  1176. FALSE,
  1177. 0,
  1178. complain_overflow_dont,
  1179. bfd_elf_generic_reloc,
  1180. "R_NIOS2_JUMP_SLOT",
  1181. FALSE,
  1182. 0xffffffff,
  1183. 0xffffffff,
  1184. FALSE),
  1185. HOWTO (R_NIOS2_RELATIVE,
  1186. 0,
  1187. 2,
  1188. 32,
  1189. FALSE,
  1190. 0,
  1191. complain_overflow_dont,
  1192. bfd_elf_generic_reloc,
  1193. "R_NIOS2_RELATIVE",
  1194. FALSE,
  1195. 0xffffffff,
  1196. 0xffffffff,
  1197. FALSE),
  1198. HOWTO (R_NIOS2_GOTOFF,
  1199. 0,
  1200. 2,
  1201. 32,
  1202. FALSE,
  1203. 0,
  1204. complain_overflow_dont,
  1205. bfd_elf_generic_reloc,
  1206. "R_NIOS2_GOTOFF",
  1207. FALSE,
  1208. 0xffffffff,
  1209. 0xffffffff,
  1210. FALSE),
  1211. HOWTO (R_NIOS2_CALL26_NOAT, /* type */
  1212. 2, /* rightshift */
  1213. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1214. 26, /* bitsize */
  1215. FALSE, /* pc_relative */
  1216. 6, /* bitpos */
  1217. complain_overflow_dont, /* complain on overflow */
  1218. nios2_elf32_call26_relocate, /* special function */
  1219. "R_NIOS2_CALL26_NOAT", /* name */
  1220. FALSE, /* partial_inplace */
  1221. 0xffffffc0, /* src_mask */
  1222. 0xffffffc0, /* dst_mask */
  1223. FALSE), /* pcrel_offset */
  1224. HOWTO (R_NIOS2_GOT_LO,
  1225. 0,
  1226. 2,
  1227. 16,
  1228. FALSE,
  1229. 16,
  1230. complain_overflow_dont,
  1231. bfd_elf_generic_reloc,
  1232. "R_NIOS2_GOT_LO",
  1233. FALSE,
  1234. 0xffff0000,
  1235. 0xffff0000,
  1236. FALSE),
  1237. HOWTO (R_NIOS2_GOT_HA,
  1238. 0,
  1239. 2,
  1240. 16,
  1241. FALSE,
  1242. 16,
  1243. complain_overflow_dont,
  1244. bfd_elf_generic_reloc,
  1245. "R_NIOS2_GOT_HA",
  1246. FALSE,
  1247. 0xffff0000,
  1248. 0xffff0000,
  1249. FALSE),
  1250. HOWTO (R_NIOS2_CALL_LO,
  1251. 0,
  1252. 2,
  1253. 16,
  1254. FALSE,
  1255. 16,
  1256. complain_overflow_dont,
  1257. bfd_elf_generic_reloc,
  1258. "R_NIOS2_CALL_LO",
  1259. FALSE,
  1260. 0xffff0000,
  1261. 0xffff0000,
  1262. FALSE),
  1263. HOWTO (R_NIOS2_CALL_HA,
  1264. 0,
  1265. 2,
  1266. 16,
  1267. FALSE,
  1268. 16,
  1269. complain_overflow_dont,
  1270. bfd_elf_generic_reloc,
  1271. "R_NIOS2_CALL_HA",
  1272. FALSE,
  1273. 0xffff0000,
  1274. 0xffff0000,
  1275. FALSE),
  1276. HOWTO (R_NIOS2_R2_S12,
  1277. 0,
  1278. 2,
  1279. 12,
  1280. FALSE,
  1281. 16,
  1282. complain_overflow_signed,
  1283. bfd_elf_generic_reloc,
  1284. "R_NIOS2_R2_S12",
  1285. FALSE,
  1286. 0x0fff0000,
  1287. 0x0fff0000,
  1288. FALSE),
  1289. HOWTO (R_NIOS2_R2_I10_1_PCREL,
  1290. 1,
  1291. 1,
  1292. 10,
  1293. TRUE,
  1294. 6,
  1295. complain_overflow_signed,
  1296. bfd_elf_generic_reloc, /* FIXME? */
  1297. "R_NIOS2_R2_I10_1_PCREL",
  1298. FALSE,
  1299. 0xffc0,
  1300. 0xffc0,
  1301. TRUE),
  1302. HOWTO (R_NIOS2_R2_T1I7_1_PCREL,
  1303. 1,
  1304. 1,
  1305. 7,
  1306. TRUE,
  1307. 9,
  1308. complain_overflow_signed,
  1309. bfd_elf_generic_reloc, /* FIXME? */
  1310. "R_NIOS2_R2_T1I7_1_PCREL",
  1311. FALSE,
  1312. 0xfe00,
  1313. 0xfe00,
  1314. TRUE),
  1315. HOWTO (R_NIOS2_R2_T1I7_2,
  1316. 2,
  1317. 1,
  1318. 7,
  1319. FALSE,
  1320. 9,
  1321. complain_overflow_unsigned,
  1322. bfd_elf_generic_reloc,
  1323. "R_NIOS2_R2_T1I7_2",
  1324. FALSE,
  1325. 0xfe00,
  1326. 0xfe00,
  1327. FALSE),
  1328. HOWTO (R_NIOS2_R2_T2I4,
  1329. 0,
  1330. 1,
  1331. 4,
  1332. FALSE,
  1333. 12,
  1334. complain_overflow_unsigned,
  1335. bfd_elf_generic_reloc,
  1336. "R_NIOS2_R2_T2I4",
  1337. FALSE,
  1338. 0xf000,
  1339. 0xf000,
  1340. FALSE),
  1341. HOWTO (R_NIOS2_R2_T2I4_1,
  1342. 1,
  1343. 1,
  1344. 4,
  1345. FALSE,
  1346. 12,
  1347. complain_overflow_unsigned,
  1348. bfd_elf_generic_reloc,
  1349. "R_NIOS2_R2_T2I4_1",
  1350. FALSE,
  1351. 0xf000,
  1352. 0xf000,
  1353. FALSE),
  1354. HOWTO (R_NIOS2_R2_T2I4_2,
  1355. 2,
  1356. 1,
  1357. 4,
  1358. FALSE,
  1359. 12,
  1360. complain_overflow_unsigned,
  1361. bfd_elf_generic_reloc,
  1362. "R_NIOS2_R2_T2I4_2",
  1363. FALSE,
  1364. 0xf000,
  1365. 0xf000,
  1366. FALSE),
  1367. HOWTO (R_NIOS2_R2_X1I7_2,
  1368. 2,
  1369. 1,
  1370. 7,
  1371. FALSE,
  1372. 6,
  1373. complain_overflow_unsigned,
  1374. bfd_elf_generic_reloc,
  1375. "R_NIOS2_R2_X1I7_2",
  1376. FALSE,
  1377. 0x1fc0,
  1378. 0x1fc0,
  1379. FALSE),
  1380. HOWTO (R_NIOS2_R2_X2L5,
  1381. 0,
  1382. 1,
  1383. 5,
  1384. FALSE,
  1385. 6,
  1386. complain_overflow_unsigned,
  1387. bfd_elf_generic_reloc,
  1388. "R_NIOS2_R2_X2L5",
  1389. FALSE,
  1390. 0x07c0,
  1391. 0x07c0,
  1392. FALSE),
  1393. HOWTO (R_NIOS2_R2_F1I5_2,
  1394. 2,
  1395. 1,
  1396. 5,
  1397. FALSE,
  1398. 6,
  1399. complain_overflow_unsigned,
  1400. bfd_elf_generic_reloc,
  1401. "R_NIOS2_R2_F1L5_2",
  1402. FALSE,
  1403. 0x07c0,
  1404. 0x07c0,
  1405. FALSE),
  1406. HOWTO (R_NIOS2_R2_L5I4X1,
  1407. 2,
  1408. 1,
  1409. 4,
  1410. FALSE,
  1411. 6,
  1412. complain_overflow_unsigned,
  1413. bfd_elf_generic_reloc,
  1414. "R_NIOS2_R2_L5I4X1",
  1415. FALSE,
  1416. 0x03c0,
  1417. 0x03c0,
  1418. FALSE),
  1419. HOWTO (R_NIOS2_R2_T1X1I6,
  1420. 0,
  1421. 1,
  1422. 6,
  1423. FALSE,
  1424. 9,
  1425. complain_overflow_unsigned,
  1426. bfd_elf_generic_reloc,
  1427. "R_NIOS2_R2_T1X1I6",
  1428. FALSE,
  1429. 0x7e00,
  1430. 0x7e00,
  1431. FALSE),
  1432. HOWTO (R_NIOS2_R2_T1X1I6_2,
  1433. 2,
  1434. 2,
  1435. 6,
  1436. FALSE,
  1437. 9,
  1438. complain_overflow_unsigned,
  1439. bfd_elf_generic_reloc,
  1440. "R_NIOS2_R2_T1I1X6_2",
  1441. FALSE,
  1442. 0x7e00,
  1443. 0x7e00,
  1444. FALSE),
  1445. /* Add other relocations here. */
  1446. };
  1447. static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
  1448. /* Return true if producing output for a R2 BFD. */
  1449. #define BFD_IS_R2(abfd) (bfd_get_mach (abfd) == bfd_mach_nios2r2)
  1450. /* Return the howto for relocation RTYPE. */
  1451. static reloc_howto_type *
  1452. lookup_howto (unsigned int rtype, bfd *abfd)
  1453. {
  1454. static int initialized = 0;
  1455. int i;
  1456. /* R2 relocations are a superset of R1, so use that for the lookup
  1457. table. */
  1458. int r1_howto_tbl_size = (int) (sizeof (elf_nios2_r1_howto_table_rel)
  1459. / sizeof (elf_nios2_r1_howto_table_rel[0]));
  1460. int r2_howto_tbl_size = (int) (sizeof (elf_nios2_r2_howto_table_rel)
  1461. / sizeof (elf_nios2_r2_howto_table_rel[0]));
  1462. if (!initialized)
  1463. {
  1464. initialized = 1;
  1465. memset (elf_code_to_howto_index, 0xff,
  1466. sizeof (elf_code_to_howto_index));
  1467. for (i = 0; i < r2_howto_tbl_size; i++)
  1468. {
  1469. elf_code_to_howto_index[elf_nios2_r2_howto_table_rel[i].type] = i;
  1470. if (i < r1_howto_tbl_size)
  1471. BFD_ASSERT (elf_nios2_r2_howto_table_rel[i].type
  1472. == elf_nios2_r1_howto_table_rel[i].type);
  1473. }
  1474. }
  1475. BFD_ASSERT (rtype <= R_NIOS2_ILLEGAL);
  1476. i = elf_code_to_howto_index[rtype];
  1477. if (BFD_IS_R2 (abfd))
  1478. {
  1479. if (i >= r2_howto_tbl_size)
  1480. return 0;
  1481. return elf_nios2_r2_howto_table_rel + i;
  1482. }
  1483. else
  1484. {
  1485. if (i >= r1_howto_tbl_size)
  1486. return 0;
  1487. return elf_nios2_r1_howto_table_rel + i;
  1488. }
  1489. }
  1490. /* Map for converting BFD reloc types to Nios II reloc types. */
  1491. struct elf_reloc_map
  1492. {
  1493. bfd_reloc_code_real_type bfd_val;
  1494. enum elf_nios2_reloc_type elf_val;
  1495. };
  1496. static const struct elf_reloc_map nios2_reloc_map[] = {
  1497. {BFD_RELOC_NONE, R_NIOS2_NONE},
  1498. {BFD_RELOC_NIOS2_S16, R_NIOS2_S16},
  1499. {BFD_RELOC_NIOS2_U16, R_NIOS2_U16},
  1500. {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16},
  1501. {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26},
  1502. {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5},
  1503. {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX},
  1504. {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6},
  1505. {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8},
  1506. {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16},
  1507. {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16},
  1508. {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16},
  1509. {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32},
  1510. {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16},
  1511. {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8},
  1512. {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL},
  1513. {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT},
  1514. {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY},
  1515. {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP},
  1516. {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP},
  1517. {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR},
  1518. {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN},
  1519. {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16},
  1520. {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16},
  1521. {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO},
  1522. {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA},
  1523. {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO},
  1524. {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA},
  1525. {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16},
  1526. {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16},
  1527. {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16},
  1528. {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16},
  1529. {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16},
  1530. {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD},
  1531. {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL},
  1532. {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL},
  1533. {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY},
  1534. {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT},
  1535. {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT},
  1536. {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE},
  1537. {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF},
  1538. {BFD_RELOC_NIOS2_CALL26_NOAT, R_NIOS2_CALL26_NOAT},
  1539. {BFD_RELOC_NIOS2_GOT_LO, R_NIOS2_GOT_LO},
  1540. {BFD_RELOC_NIOS2_GOT_HA, R_NIOS2_GOT_HA},
  1541. {BFD_RELOC_NIOS2_CALL_LO, R_NIOS2_CALL_LO},
  1542. {BFD_RELOC_NIOS2_CALL_HA, R_NIOS2_CALL_HA},
  1543. {BFD_RELOC_NIOS2_R2_S12, R_NIOS2_R2_S12},
  1544. {BFD_RELOC_NIOS2_R2_I10_1_PCREL, R_NIOS2_R2_I10_1_PCREL},
  1545. {BFD_RELOC_NIOS2_R2_T1I7_1_PCREL, R_NIOS2_R2_T1I7_1_PCREL},
  1546. {BFD_RELOC_NIOS2_R2_T1I7_2, R_NIOS2_R2_T1I7_2},
  1547. {BFD_RELOC_NIOS2_R2_T2I4, R_NIOS2_R2_T2I4},
  1548. {BFD_RELOC_NIOS2_R2_T2I4_1, R_NIOS2_R2_T2I4_1},
  1549. {BFD_RELOC_NIOS2_R2_T2I4_2, R_NIOS2_R2_T2I4_2},
  1550. {BFD_RELOC_NIOS2_R2_X1I7_2, R_NIOS2_R2_X1I7_2},
  1551. {BFD_RELOC_NIOS2_R2_X2L5, R_NIOS2_R2_X2L5},
  1552. {BFD_RELOC_NIOS2_R2_F1I5_2, R_NIOS2_R2_F1I5_2},
  1553. {BFD_RELOC_NIOS2_R2_L5I4X1, R_NIOS2_R2_L5I4X1},
  1554. {BFD_RELOC_NIOS2_R2_T1X1I6, R_NIOS2_R2_T1X1I6},
  1555. {BFD_RELOC_NIOS2_R2_T1X1I6_2, R_NIOS2_R2_T1X1I6_2},
  1556. };
  1557. enum elf32_nios2_stub_type
  1558. {
  1559. nios2_stub_call26_before,
  1560. nios2_stub_call26_after,
  1561. nios2_stub_none
  1562. };
  1563. struct elf32_nios2_stub_hash_entry
  1564. {
  1565. /* Base hash table entry structure. */
  1566. struct bfd_hash_entry bh_root;
  1567. /* The stub section. */
  1568. asection *stub_sec;
  1569. /* Offset within stub_sec of the beginning of this stub. */
  1570. bfd_vma stub_offset;
  1571. /* Given the symbol's value and its section we can determine its final
  1572. value when building the stubs (so the stub knows where to jump. */
  1573. bfd_vma target_value;
  1574. asection *target_section;
  1575. enum elf32_nios2_stub_type stub_type;
  1576. /* The symbol table entry, if any, that this was derived from. */
  1577. struct elf32_nios2_link_hash_entry *hh;
  1578. /* And the reloc addend that this was derived from. */
  1579. bfd_vma addend;
  1580. /* Where this stub is being called from, or, in the case of combined
  1581. stub sections, the first input section in the group. */
  1582. asection *id_sec;
  1583. };
  1584. #define nios2_stub_hash_entry(ent) \
  1585. ((struct elf32_nios2_stub_hash_entry *)(ent))
  1586. #define nios2_stub_hash_lookup(table, string, create, copy) \
  1587. ((struct elf32_nios2_stub_hash_entry *) \
  1588. bfd_hash_lookup ((table), (string), (create), (copy)))
  1589. /* The Nios II linker needs to keep track of the number of relocs that it
  1590. decides to copy as dynamic relocs in check_relocs for each symbol.
  1591. This is so that it can later discard them if they are found to be
  1592. unnecessary. We store the information in a field extending the
  1593. regular ELF linker hash table. */
  1594. struct elf32_nios2_dyn_relocs
  1595. {
  1596. struct elf32_nios2_dyn_relocs *next;
  1597. /* The input section of the reloc. */
  1598. asection *sec;
  1599. /* Total number of relocs copied for the input section. */
  1600. bfd_size_type count;
  1601. /* Number of pc-relative relocs copied for the input section. */
  1602. bfd_size_type pc_count;
  1603. };
  1604. /* Nios II ELF linker hash entry. */
  1605. struct elf32_nios2_link_hash_entry
  1606. {
  1607. struct elf_link_hash_entry root;
  1608. /* A pointer to the most recently used stub hash entry against this
  1609. symbol. */
  1610. struct elf32_nios2_stub_hash_entry *hsh_cache;
  1611. /* Track dynamic relocs copied for this symbol. */
  1612. struct elf32_nios2_dyn_relocs *dyn_relocs;
  1613. #define GOT_UNKNOWN 0
  1614. #define GOT_NORMAL 1
  1615. #define GOT_TLS_GD 2
  1616. #define GOT_TLS_IE 4
  1617. unsigned char tls_type;
  1618. /* We need to detect and take special action for symbols which are only
  1619. referenced with %call() and not with %got(). Such symbols do not need
  1620. a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
  1621. linking will not work if the dynamic GOT reloc exists.
  1622. To check for this condition efficiently, we compare got_types_used against
  1623. CALL_USED, meaning
  1624. (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
  1625. */
  1626. #define GOT_USED 1
  1627. #define CALL_USED 2
  1628. unsigned char got_types_used;
  1629. };
  1630. #define elf32_nios2_hash_entry(ent) \
  1631. ((struct elf32_nios2_link_hash_entry *) (ent))
  1632. /* Get the Nios II elf linker hash table from a link_info structure. */
  1633. #define elf32_nios2_hash_table(info) \
  1634. ((struct elf32_nios2_link_hash_table *) ((info)->hash))
  1635. /* Nios II ELF linker hash table. */
  1636. struct elf32_nios2_link_hash_table
  1637. {
  1638. /* The main hash table. */
  1639. struct elf_link_hash_table root;
  1640. /* The stub hash table. */
  1641. struct bfd_hash_table bstab;
  1642. /* Linker stub bfd. */
  1643. bfd *stub_bfd;
  1644. /* Linker call-backs. */
  1645. asection * (*add_stub_section) (const char *, asection *, bfd_boolean);
  1646. void (*layout_sections_again) (void);
  1647. /* Array to keep track of which stub sections have been created, and
  1648. information on stub grouping. */
  1649. struct map_stub
  1650. {
  1651. /* These are the section to which stubs in the group will be
  1652. attached. */
  1653. asection *first_sec, *last_sec;
  1654. /* The stub sections. There might be stubs inserted either before
  1655. or after the real section.*/
  1656. asection *first_stub_sec, *last_stub_sec;
  1657. } *stub_group;
  1658. /* Assorted information used by nios2_elf32_size_stubs. */
  1659. unsigned int bfd_count;
  1660. unsigned int top_index;
  1661. asection **input_list;
  1662. Elf_Internal_Sym **all_local_syms;
  1663. /* Short-cuts to get to dynamic linker sections. */
  1664. asection *sdynbss;
  1665. asection *srelbss;
  1666. asection *sbss;
  1667. /* GOT pointer symbol _gp_got. */
  1668. struct elf_link_hash_entry *h_gp_got;
  1669. union {
  1670. bfd_signed_vma refcount;
  1671. bfd_vma offset;
  1672. } tls_ldm_got;
  1673. /* Small local sym cache. */
  1674. struct sym_cache sym_cache;
  1675. bfd_vma res_n_size;
  1676. };
  1677. struct nios2_elf32_obj_tdata
  1678. {
  1679. struct elf_obj_tdata root;
  1680. /* tls_type for each local got entry. */
  1681. char *local_got_tls_type;
  1682. /* TRUE if TLS GD relocs have been seen for this object. */
  1683. bfd_boolean has_tlsgd;
  1684. };
  1685. #define elf32_nios2_tdata(abfd) \
  1686. ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
  1687. #define elf32_nios2_local_got_tls_type(abfd) \
  1688. (elf32_nios2_tdata (abfd)->local_got_tls_type)
  1689. /* The name of the dynamic interpreter. This is put in the .interp
  1690. section. */
  1691. #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
  1692. /* PLT implementation for position-dependent code. */
  1693. static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
  1694. 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
  1695. 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
  1696. 0x7800683a /* jmp r15 */
  1697. };
  1698. static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
  1699. 0x03800034, /* movhi r14, %hiadj(res_0) */
  1700. 0x73800004, /* addi r14, r14, %lo(res_0) */
  1701. 0x7b9fc83a, /* sub r15, r15, r14 */
  1702. 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
  1703. 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
  1704. 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
  1705. 0x6800683a /* jmp r13 */
  1706. };
  1707. /* PLT implementation for position-independent code. */
  1708. static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
  1709. 0x03c00034, /* movhi r15, %hiadj(index * 4) */
  1710. 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
  1711. 0x00000006 /* br .PLTresolve */
  1712. };
  1713. static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
  1714. 0x001ce03a, /* nextpc r14 */
  1715. 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
  1716. 0x6b9b883a, /* add r13, r13, r14 */
  1717. 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
  1718. 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
  1719. 0x6800683a /* jmp r13 */
  1720. };
  1721. /* CALL26 stub. */
  1722. static const bfd_vma nios2_call26_stub_entry[] = {
  1723. 0x00400034, /* orhi at, r0, %hiadj(dest) */
  1724. 0x08400004, /* addi at, at, %lo(dest) */
  1725. 0x0800683a /* jmp at */
  1726. };
  1727. /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
  1728. static void
  1729. nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
  1730. {
  1731. bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
  1732. BFD_ASSERT(value <= 0xffff);
  1733. bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
  1734. sec->contents + offset);
  1735. }
  1736. /* Install COUNT 32-bit values DATA starting at offset OFFSET into
  1737. section SEC. */
  1738. static void
  1739. nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
  1740. int count)
  1741. {
  1742. while (count--)
  1743. {
  1744. bfd_put_32 (sec->owner, *data, sec->contents + offset);
  1745. offset += 4;
  1746. ++data;
  1747. }
  1748. }
  1749. /* The usual way of loading a 32-bit constant into a Nios II register is to
  1750. load the high 16 bits in one instruction and then add the low 16 bits with
  1751. a signed add. This means that the high halfword needs to be adjusted to
  1752. compensate for the sign bit of the low halfword. This function returns the
  1753. adjusted high halfword for a given 32-bit constant. */
  1754. static
  1755. bfd_vma hiadj (bfd_vma symbol_value)
  1756. {
  1757. return ((symbol_value + 0x8000) >> 16) & 0xffff;
  1758. }
  1759. /* Implement elf_backend_grok_prstatus:
  1760. Support for core dump NOTE sections. */
  1761. static bfd_boolean
  1762. nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  1763. {
  1764. int offset;
  1765. size_t size;
  1766. switch (note->descsz)
  1767. {
  1768. default:
  1769. return FALSE;
  1770. case 212: /* Linux/Nios II */
  1771. /* pr_cursig */
  1772. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  1773. /* pr_pid */
  1774. elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
  1775. /* pr_reg */
  1776. offset = 72;
  1777. size = 136;
  1778. break;
  1779. }
  1780. /* Make a ".reg/999" section. */
  1781. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  1782. size, note->descpos + offset);
  1783. }
  1784. /* Implement elf_backend_grok_psinfo. */
  1785. static bfd_boolean
  1786. nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  1787. {
  1788. switch (note->descsz)
  1789. {
  1790. default:
  1791. return FALSE;
  1792. case 124: /* Linux/Nios II elf_prpsinfo */
  1793. elf_tdata (abfd)->core->program
  1794. = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  1795. elf_tdata (abfd)->core->command
  1796. = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  1797. }
  1798. /* Note that for some reason, a spurious space is tacked
  1799. onto the end of the args in some (at least one anyway)
  1800. implementations, so strip it off if it exists. */
  1801. {
  1802. char *command = elf_tdata (abfd)->core->command;
  1803. int n = strlen (command);
  1804. if (0 < n && command[n - 1] == ' ')
  1805. command[n - 1] = '\0';
  1806. }
  1807. return TRUE;
  1808. }
  1809. /* Assorted hash table functions. */
  1810. /* Initialize an entry in the stub hash table. */
  1811. static struct bfd_hash_entry *
  1812. stub_hash_newfunc (struct bfd_hash_entry *entry,
  1813. struct bfd_hash_table *table,
  1814. const char *string)
  1815. {
  1816. /* Allocate the structure if it has not already been allocated by a
  1817. subclass. */
  1818. if (entry == NULL)
  1819. {
  1820. entry = bfd_hash_allocate (table,
  1821. sizeof (struct elf32_nios2_stub_hash_entry));
  1822. if (entry == NULL)
  1823. return entry;
  1824. }
  1825. /* Call the allocation method of the superclass. */
  1826. entry = bfd_hash_newfunc (entry, table, string);
  1827. if (entry != NULL)
  1828. {
  1829. struct elf32_nios2_stub_hash_entry *hsh;
  1830. /* Initialize the local fields. */
  1831. hsh = (struct elf32_nios2_stub_hash_entry *) entry;
  1832. hsh->stub_sec = NULL;
  1833. hsh->stub_offset = 0;
  1834. hsh->target_value = 0;
  1835. hsh->target_section = NULL;
  1836. hsh->stub_type = nios2_stub_none;
  1837. hsh->hh = NULL;
  1838. hsh->id_sec = NULL;
  1839. }
  1840. return entry;
  1841. }
  1842. /* Create an entry in a Nios II ELF linker hash table. */
  1843. static struct bfd_hash_entry *
  1844. link_hash_newfunc (struct bfd_hash_entry *entry,
  1845. struct bfd_hash_table *table, const char *string)
  1846. {
  1847. /* Allocate the structure if it has not already been allocated by a
  1848. subclass. */
  1849. if (entry == NULL)
  1850. {
  1851. entry = bfd_hash_allocate (table,
  1852. sizeof (struct elf32_nios2_link_hash_entry));
  1853. if (entry == NULL)
  1854. return entry;
  1855. }
  1856. /* Call the allocation method of the superclass. */
  1857. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  1858. if (entry)
  1859. {
  1860. struct elf32_nios2_link_hash_entry *eh;
  1861. eh = (struct elf32_nios2_link_hash_entry *) entry;
  1862. eh->hsh_cache = NULL;
  1863. eh->dyn_relocs = NULL;
  1864. eh->tls_type = GOT_UNKNOWN;
  1865. eh->got_types_used = 0;
  1866. }
  1867. return entry;
  1868. }
  1869. /* Section name for stubs is the associated section name plus this
  1870. string. */
  1871. #define STUB_SUFFIX ".stub"
  1872. /* Build a name for an entry in the stub hash table. */
  1873. static char *
  1874. nios2_stub_name (const asection *input_section,
  1875. const asection *sym_sec,
  1876. const struct elf32_nios2_link_hash_entry *hh,
  1877. const Elf_Internal_Rela *rel,
  1878. enum elf32_nios2_stub_type stub_type)
  1879. {
  1880. char *stub_name;
  1881. bfd_size_type len;
  1882. char stubpos = (stub_type == nios2_stub_call26_before) ? 'b' : 'a';
  1883. if (hh)
  1884. {
  1885. len = 8 + 1 + 1 + 1+ strlen (hh->root.root.root.string) + 1 + 8 + 1;
  1886. stub_name = bfd_malloc (len);
  1887. if (stub_name != NULL)
  1888. {
  1889. sprintf (stub_name, "%08x_%c_%s+%x",
  1890. input_section->id & 0xffffffff,
  1891. stubpos,
  1892. hh->root.root.root.string,
  1893. (int) rel->r_addend & 0xffffffff);
  1894. }
  1895. }
  1896. else
  1897. {
  1898. len = 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
  1899. stub_name = bfd_malloc (len);
  1900. if (stub_name != NULL)
  1901. {
  1902. sprintf (stub_name, "%08x_%c_%x:%x+%x",
  1903. input_section->id & 0xffffffff,
  1904. stubpos,
  1905. sym_sec->id & 0xffffffff,
  1906. (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
  1907. (int) rel->r_addend & 0xffffffff);
  1908. }
  1909. }
  1910. return stub_name;
  1911. }
  1912. /* Look up an entry in the stub hash. Stub entries are cached because
  1913. creating the stub name takes a bit of time. */
  1914. static struct elf32_nios2_stub_hash_entry *
  1915. nios2_get_stub_entry (const asection *input_section,
  1916. const asection *sym_sec,
  1917. struct elf32_nios2_link_hash_entry *hh,
  1918. const Elf_Internal_Rela *rel,
  1919. struct elf32_nios2_link_hash_table *htab,
  1920. enum elf32_nios2_stub_type stub_type)
  1921. {
  1922. struct elf32_nios2_stub_hash_entry *hsh;
  1923. const asection *id_sec;
  1924. /* If this input section is part of a group of sections sharing one
  1925. stub section, then use the id of the first/last section in the group,
  1926. depending on the stub section placement relative to the group.
  1927. Stub names need to include a section id, as there may well be
  1928. more than one stub used to reach say, printf, and we need to
  1929. distinguish between them. */
  1930. if (stub_type == nios2_stub_call26_before)
  1931. id_sec = htab->stub_group[input_section->id].first_sec;
  1932. else
  1933. id_sec = htab->stub_group[input_section->id].last_sec;
  1934. if (hh != NULL && hh->hsh_cache != NULL
  1935. && hh->hsh_cache->hh == hh
  1936. && hh->hsh_cache->id_sec == id_sec
  1937. && hh->hsh_cache->stub_type == stub_type)
  1938. {
  1939. hsh = hh->hsh_cache;
  1940. }
  1941. else
  1942. {
  1943. char *stub_name;
  1944. stub_name = nios2_stub_name (id_sec, sym_sec, hh, rel, stub_type);
  1945. if (stub_name == NULL)
  1946. return NULL;
  1947. hsh = nios2_stub_hash_lookup (&htab->bstab,
  1948. stub_name, FALSE, FALSE);
  1949. if (hh != NULL)
  1950. hh->hsh_cache = hsh;
  1951. free (stub_name);
  1952. }
  1953. return hsh;
  1954. }
  1955. /* Add a new stub entry to the stub hash. Not all fields of the new
  1956. stub entry are initialised. */
  1957. static struct elf32_nios2_stub_hash_entry *
  1958. nios2_add_stub (const char *stub_name,
  1959. asection *section,
  1960. struct elf32_nios2_link_hash_table *htab,
  1961. enum elf32_nios2_stub_type stub_type)
  1962. {
  1963. asection *link_sec;
  1964. asection *stub_sec;
  1965. asection **secptr, **linkptr;
  1966. struct elf32_nios2_stub_hash_entry *hsh;
  1967. bfd_boolean afterp;
  1968. if (stub_type == nios2_stub_call26_before)
  1969. {
  1970. link_sec = htab->stub_group[section->id].first_sec;
  1971. secptr = &(htab->stub_group[section->id].first_stub_sec);
  1972. linkptr = &(htab->stub_group[link_sec->id].first_stub_sec);
  1973. afterp = FALSE;
  1974. }
  1975. else
  1976. {
  1977. link_sec = htab->stub_group[section->id].last_sec;
  1978. secptr = &(htab->stub_group[section->id].last_stub_sec);
  1979. linkptr = &(htab->stub_group[link_sec->id].last_stub_sec);
  1980. afterp = TRUE;
  1981. }
  1982. stub_sec = *secptr;
  1983. if (stub_sec == NULL)
  1984. {
  1985. stub_sec = *linkptr;
  1986. if (stub_sec == NULL)
  1987. {
  1988. size_t namelen;
  1989. bfd_size_type len;
  1990. char *s_name;
  1991. namelen = strlen (link_sec->name);
  1992. len = namelen + sizeof (STUB_SUFFIX);
  1993. s_name = bfd_alloc (htab->stub_bfd, len);
  1994. if (s_name == NULL)
  1995. return NULL;
  1996. memcpy (s_name, link_sec->name, namelen);
  1997. memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
  1998. stub_sec = (*htab->add_stub_section) (s_name, link_sec, afterp);
  1999. if (stub_sec == NULL)
  2000. return NULL;
  2001. *linkptr = stub_sec;
  2002. }
  2003. *secptr = stub_sec;
  2004. }
  2005. /* Enter this entry into the linker stub hash table. */
  2006. hsh = nios2_stub_hash_lookup (&htab->bstab, stub_name,
  2007. TRUE, FALSE);
  2008. if (hsh == NULL)
  2009. {
  2010. (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
  2011. section->owner,
  2012. stub_name);
  2013. return NULL;
  2014. }
  2015. hsh->stub_sec = stub_sec;
  2016. hsh->stub_offset = 0;
  2017. hsh->id_sec = link_sec;
  2018. return hsh;
  2019. }
  2020. /* Set up various things so that we can make a list of input sections
  2021. for each output section included in the link. Returns -1 on error,
  2022. 0 when no stubs will be needed, and 1 on success. */
  2023. int
  2024. nios2_elf32_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
  2025. {
  2026. bfd *input_bfd;
  2027. unsigned int bfd_count;
  2028. unsigned int top_id, top_index;
  2029. asection *section;
  2030. asection **input_list, **list;
  2031. bfd_size_type amt;
  2032. struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
  2033. /* Count the number of input BFDs and find the top input section id. */
  2034. for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
  2035. input_bfd != NULL;
  2036. input_bfd = input_bfd->link.next)
  2037. {
  2038. bfd_count += 1;
  2039. for (section = input_bfd->sections;
  2040. section != NULL;
  2041. section = section->next)
  2042. {
  2043. if (top_id < section->id)
  2044. top_id = section->id;
  2045. }
  2046. }
  2047. htab->bfd_count = bfd_count;
  2048. amt = sizeof (struct map_stub) * (top_id + 1);
  2049. htab->stub_group = bfd_zmalloc (amt);
  2050. if (htab->stub_group == NULL)
  2051. return -1;
  2052. /* We can't use output_bfd->section_count here to find the top output
  2053. section index as some sections may have been removed, and
  2054. strip_excluded_output_sections doesn't renumber the indices. */
  2055. for (section = output_bfd->sections, top_index = 0;
  2056. section != NULL;
  2057. section = section->next)
  2058. {
  2059. if (top_index < section->index)
  2060. top_index = section->index;
  2061. }
  2062. htab->top_index = top_index;
  2063. amt = sizeof (asection *) * (top_index + 1);
  2064. input_list = bfd_malloc (amt);
  2065. htab->input_list = input_list;
  2066. if (input_list == NULL)
  2067. return -1;
  2068. /* For sections we aren't interested in, mark their entries with a
  2069. value we can check later. */
  2070. list = input_list + top_index;
  2071. do
  2072. *list = bfd_abs_section_ptr;
  2073. while (list-- != input_list);
  2074. for (section = output_bfd->sections;
  2075. section != NULL;
  2076. section = section->next)
  2077. {
  2078. /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
  2079. * have PC relative relocs in them but no code flag set. */
  2080. if (((section->flags & SEC_CODE) != 0) ||
  2081. strcmp(".ctors", section->name) ||
  2082. strcmp(".dtors", section->name))
  2083. input_list[section->index] = NULL;
  2084. }
  2085. return 1;
  2086. }
  2087. /* The linker repeatedly calls this function for each input section,
  2088. in the order that input sections are linked into output sections.
  2089. Build lists of input sections to determine groupings between which
  2090. we may insert linker stubs. */
  2091. void
  2092. nios2_elf32_next_input_section (struct bfd_link_info *info, asection *isec)
  2093. {
  2094. struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
  2095. if (isec->output_section->index <= htab->top_index)
  2096. {
  2097. asection **list = htab->input_list + isec->output_section->index;
  2098. if (*list != bfd_abs_section_ptr)
  2099. {
  2100. /* Steal the last_sec pointer for our list.
  2101. This happens to make the list in reverse order,
  2102. which is what we want. */
  2103. htab->stub_group[isec->id].last_sec = *list;
  2104. *list = isec;
  2105. }
  2106. }
  2107. }
  2108. /* Segment mask for CALL26 relocation relaxation. */
  2109. #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
  2110. /* Fudge factor for approximate maximum size of all stubs that might
  2111. be inserted by the linker. This does not actually limit the number
  2112. of stubs that might be inserted, and only affects strategy for grouping
  2113. and placement of stubs. Perhaps this should be computed based on number
  2114. of relocations seen, or be specifiable on the command line. */
  2115. #define MAX_STUB_SECTION_SIZE 0xffff
  2116. /* See whether we can group stub sections together. Grouping stub
  2117. sections may result in fewer stubs. More importantly, we need to
  2118. put all .init* and .fini* stubs at the end of the .init or
  2119. .fini output sections respectively, because glibc splits the
  2120. _init and _fini functions into multiple parts. Putting a stub in
  2121. the middle of a function is not a good idea.
  2122. Rather than computing groups of a maximum fixed size, for Nios II
  2123. CALL26 relaxation it makes more sense to compute the groups based on
  2124. sections that fit within a 256MB address segment. Also do not allow
  2125. a group to span more than one output section, since different output
  2126. sections might correspond to different memory banks on a bare-metal
  2127. target, etc. */
  2128. static void
  2129. group_sections (struct elf32_nios2_link_hash_table *htab)
  2130. {
  2131. asection **list = htab->input_list + htab->top_index;
  2132. do
  2133. {
  2134. /* The list is in reverse order so we'll search backwards looking
  2135. for the first section that begins in the same memory segment,
  2136. marking sections along the way to point at the tail for this
  2137. group. */
  2138. asection *tail = *list;
  2139. if (tail == bfd_abs_section_ptr)
  2140. continue;
  2141. while (tail != NULL)
  2142. {
  2143. bfd_vma start = tail->output_section->vma + tail->output_offset;
  2144. bfd_vma end = start + tail->size;
  2145. bfd_vma segment = CALL26_SEGMENT (end);
  2146. asection *prev;
  2147. if (segment != CALL26_SEGMENT (start)
  2148. || segment != CALL26_SEGMENT (end + MAX_STUB_SECTION_SIZE))
  2149. /* This section spans more than one memory segment, or is
  2150. close enough to the end of the segment that adding stub
  2151. sections before it might cause it to move so that it
  2152. spans memory segments, or that stubs added at the end of
  2153. this group might overflow into the next memory segment.
  2154. Put it in a group by itself to localize the effects. */
  2155. {
  2156. prev = htab->stub_group[tail->id].last_sec;
  2157. htab->stub_group[tail->id].last_sec = tail;
  2158. htab->stub_group[tail->id].first_sec = tail;
  2159. }
  2160. else
  2161. /* Collect more sections for this group. */
  2162. {
  2163. asection *curr, *first;
  2164. for (curr = tail; ; curr = prev)
  2165. {
  2166. prev = htab->stub_group[curr->id].last_sec;
  2167. if (!prev
  2168. || tail->output_section != prev->output_section
  2169. || (CALL26_SEGMENT (prev->output_section->vma
  2170. + prev->output_offset)
  2171. != segment))
  2172. break;
  2173. }
  2174. first = curr;
  2175. for (curr = tail; ; curr = prev)
  2176. {
  2177. prev = htab->stub_group[curr->id].last_sec;
  2178. htab->stub_group[curr->id].last_sec = tail;
  2179. htab->stub_group[curr->id].first_sec = first;
  2180. if (curr == first)
  2181. break;
  2182. }
  2183. }
  2184. /* Reset tail for the next group. */
  2185. tail = prev;
  2186. }
  2187. }
  2188. while (list-- != htab->input_list);
  2189. free (htab->input_list);
  2190. }
  2191. /* Determine the type of stub needed, if any, for a call. */
  2192. static enum elf32_nios2_stub_type
  2193. nios2_type_of_stub (asection *input_sec,
  2194. const Elf_Internal_Rela *rel,
  2195. struct elf32_nios2_link_hash_entry *hh,
  2196. struct elf32_nios2_link_hash_table *htab,
  2197. bfd_vma destination,
  2198. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2199. {
  2200. bfd_vma location, segment, start, end;
  2201. asection *s0, *s1, *s;
  2202. if (hh != NULL &&
  2203. !(hh->root.root.type == bfd_link_hash_defined
  2204. || hh->root.root.type == bfd_link_hash_defweak))
  2205. return nios2_stub_none;
  2206. /* Determine where the call point is. */
  2207. location = (input_sec->output_section->vma
  2208. + input_sec->output_offset + rel->r_offset);
  2209. segment = CALL26_SEGMENT (location);
  2210. /* Nios II CALL and JMPI instructions can transfer control to addresses
  2211. within the same 256MB segment as the PC. */
  2212. if (segment == CALL26_SEGMENT (destination))
  2213. return nios2_stub_none;
  2214. /* Find the start and end addresses of the stub group. Also account for
  2215. any already-created stub sections for this group. Note that for stubs
  2216. in the end section, only the first instruction of the last stub
  2217. (12 bytes long) needs to be within range. */
  2218. s0 = htab->stub_group[input_sec->id].first_sec;
  2219. s = htab->stub_group[s0->id].first_stub_sec;
  2220. if (s != NULL && s->size > 0)
  2221. start = s->output_section->vma + s->output_offset;
  2222. else
  2223. start = s0->output_section->vma + s0->output_offset;
  2224. s1 = htab->stub_group[input_sec->id].last_sec;
  2225. s = htab->stub_group[s1->id].last_stub_sec;
  2226. if (s != NULL && s->size > 0)
  2227. end = s->output_section->vma + s->output_offset + s->size - 8;
  2228. else
  2229. end = s1->output_section->vma + s1->output_offset + s1->size;
  2230. BFD_ASSERT (start < end);
  2231. BFD_ASSERT (start <= location);
  2232. BFD_ASSERT (location < end);
  2233. /* Put stubs at the end of the group unless that is not a valid
  2234. location and the beginning of the group is. It might be that
  2235. neither the beginning nor end works if we have an input section
  2236. so large that it spans multiple segment boundaries. In that
  2237. case, punt; the end result will be a relocation overflow error no
  2238. matter what we do here.
  2239. Note that adding stubs pushes up the addresses of all subsequent
  2240. sections, so that stubs allocated on one pass through the
  2241. relaxation loop may not be valid on the next pass. (E.g., we may
  2242. allocate a stub at the beginning of the section on one pass and
  2243. find that the call site has been bumped into the next memory
  2244. segment on the next pass.) The important thing to note is that
  2245. we never try to reclaim the space allocated to such unused stubs,
  2246. so code size and section addresses can only increase with each
  2247. iteration. Accounting for the start and end addresses of the
  2248. already-created stub sections ensures that when the algorithm
  2249. converges, it converges accurately, with the entire appropriate
  2250. stub section accessible from the call site and not just the
  2251. address at the start or end of the stub group proper. */
  2252. if (segment == CALL26_SEGMENT (end))
  2253. return nios2_stub_call26_after;
  2254. else if (segment == CALL26_SEGMENT (start))
  2255. return nios2_stub_call26_before;
  2256. else
  2257. /* Perhaps this should be a dedicated error code. */
  2258. return nios2_stub_none;
  2259. }
  2260. static bfd_boolean
  2261. nios2_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
  2262. {
  2263. struct elf32_nios2_stub_hash_entry *hsh
  2264. = (struct elf32_nios2_stub_hash_entry *) gen_entry;
  2265. asection *stub_sec = hsh->stub_sec;
  2266. bfd_vma sym_value;
  2267. /* Make a note of the offset within the stubs for this entry. */
  2268. hsh->stub_offset = stub_sec->size;
  2269. switch (hsh->stub_type)
  2270. {
  2271. case nios2_stub_call26_before:
  2272. case nios2_stub_call26_after:
  2273. /* A call26 stub looks like:
  2274. orhi at, %hiadj(dest)
  2275. addi at, at, %lo(dest)
  2276. jmp at
  2277. Note that call/jmpi instructions can't be used in PIC code
  2278. so there is no reason for the stub to be PIC, either. */
  2279. sym_value = (hsh->target_value
  2280. + hsh->target_section->output_offset
  2281. + hsh->target_section->output_section->vma
  2282. + hsh->addend);
  2283. nios2_elf32_install_data (stub_sec, nios2_call26_stub_entry,
  2284. hsh->stub_offset, 3);
  2285. nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset,
  2286. hiadj (sym_value));
  2287. nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset + 4,
  2288. (sym_value & 0xffff));
  2289. stub_sec->size += 12;
  2290. break;
  2291. default:
  2292. BFD_FAIL ();
  2293. return FALSE;
  2294. }
  2295. return TRUE;
  2296. }
  2297. /* As above, but don't actually build the stub. Just bump offset so
  2298. we know stub section sizes. */
  2299. static bfd_boolean
  2300. nios2_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
  2301. {
  2302. struct elf32_nios2_stub_hash_entry *hsh
  2303. = (struct elf32_nios2_stub_hash_entry *) gen_entry;
  2304. switch (hsh->stub_type)
  2305. {
  2306. case nios2_stub_call26_before:
  2307. case nios2_stub_call26_after:
  2308. hsh->stub_sec->size += 12;
  2309. break;
  2310. default:
  2311. BFD_FAIL ();
  2312. return FALSE;
  2313. }
  2314. return TRUE;
  2315. }
  2316. /* Read in all local syms for all input bfds.
  2317. Returns -1 on error, 0 otherwise. */
  2318. static int
  2319. get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
  2320. struct bfd_link_info *info)
  2321. {
  2322. unsigned int bfd_indx;
  2323. Elf_Internal_Sym *local_syms, **all_local_syms;
  2324. struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
  2325. /* We want to read in symbol extension records only once. To do this
  2326. we need to read in the local symbols in parallel and save them for
  2327. later use; so hold pointers to the local symbols in an array. */
  2328. bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
  2329. all_local_syms = bfd_zmalloc (amt);
  2330. htab->all_local_syms = all_local_syms;
  2331. if (all_local_syms == NULL)
  2332. return -1;
  2333. /* Walk over all the input BFDs, swapping in local symbols. */
  2334. for (bfd_indx = 0;
  2335. input_bfd != NULL;
  2336. input_bfd = input_bfd->link.next, bfd_indx++)
  2337. {
  2338. Elf_Internal_Shdr *symtab_hdr;
  2339. /* We'll need the symbol table in a second. */
  2340. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2341. if (symtab_hdr->sh_info == 0)
  2342. continue;
  2343. /* We need an array of the local symbols attached to the input bfd. */
  2344. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  2345. if (local_syms == NULL)
  2346. {
  2347. local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  2348. symtab_hdr->sh_info, 0,
  2349. NULL, NULL, NULL);
  2350. /* Cache them for elf_link_input_bfd. */
  2351. symtab_hdr->contents = (unsigned char *) local_syms;
  2352. }
  2353. if (local_syms == NULL)
  2354. return -1;
  2355. all_local_syms[bfd_indx] = local_syms;
  2356. }
  2357. return 0;
  2358. }
  2359. /* Determine and set the size of the stub section for a final link. */
  2360. bfd_boolean
  2361. nios2_elf32_size_stubs (bfd *output_bfd, bfd *stub_bfd,
  2362. struct bfd_link_info *info,
  2363. asection *(*add_stub_section) (const char *,
  2364. asection *, bfd_boolean),
  2365. void (*layout_sections_again) (void))
  2366. {
  2367. bfd_boolean stub_changed = FALSE;
  2368. struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
  2369. /* Stash our params away. */
  2370. htab->stub_bfd = stub_bfd;
  2371. htab->add_stub_section = add_stub_section;
  2372. htab->layout_sections_again = layout_sections_again;
  2373. /* FIXME: We only compute the section groups once. This could cause
  2374. problems if adding a large stub section causes following sections,
  2375. or parts of them, to move into another segment. However, this seems
  2376. to be consistent with the way other back ends handle this.... */
  2377. group_sections (htab);
  2378. if (get_local_syms (output_bfd, info->input_bfds, info))
  2379. {
  2380. if (htab->all_local_syms)
  2381. goto error_ret_free_local;
  2382. return FALSE;
  2383. }
  2384. while (1)
  2385. {
  2386. bfd *input_bfd;
  2387. unsigned int bfd_indx;
  2388. asection *stub_sec;
  2389. for (input_bfd = info->input_bfds, bfd_indx = 0;
  2390. input_bfd != NULL;
  2391. input_bfd = input_bfd->link.next, bfd_indx++)
  2392. {
  2393. Elf_Internal_Shdr *symtab_hdr;
  2394. asection *section;
  2395. Elf_Internal_Sym *local_syms;
  2396. /* We'll need the symbol table in a second. */
  2397. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2398. if (symtab_hdr->sh_info == 0)
  2399. continue;
  2400. local_syms = htab->all_local_syms[bfd_indx];
  2401. /* Walk over each section attached to the input bfd. */
  2402. for (section = input_bfd->sections;
  2403. section != NULL;
  2404. section = section->next)
  2405. {
  2406. Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  2407. /* If there aren't any relocs, then there's nothing more
  2408. to do. */
  2409. if ((section->flags & SEC_RELOC) == 0
  2410. || section->reloc_count == 0)
  2411. continue;
  2412. /* If this section is a link-once section that will be
  2413. discarded, then don't create any stubs. */
  2414. if (section->output_section == NULL
  2415. || section->output_section->owner != output_bfd)
  2416. continue;
  2417. /* Get the relocs. */
  2418. internal_relocs
  2419. = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
  2420. info->keep_memory);
  2421. if (internal_relocs == NULL)
  2422. goto error_ret_free_local;
  2423. /* Now examine each relocation. */
  2424. irela = internal_relocs;
  2425. irelaend = irela + section->reloc_count;
  2426. for (; irela < irelaend; irela++)
  2427. {
  2428. unsigned int r_type, r_indx;
  2429. enum elf32_nios2_stub_type stub_type;
  2430. struct elf32_nios2_stub_hash_entry *hsh;
  2431. asection *sym_sec;
  2432. bfd_vma sym_value;
  2433. bfd_vma destination;
  2434. struct elf32_nios2_link_hash_entry *hh;
  2435. char *stub_name;
  2436. const asection *id_sec;
  2437. r_type = ELF32_R_TYPE (irela->r_info);
  2438. r_indx = ELF32_R_SYM (irela->r_info);
  2439. if (r_type >= (unsigned int) R_NIOS2_ILLEGAL)
  2440. {
  2441. bfd_set_error (bfd_error_bad_value);
  2442. error_ret_free_internal:
  2443. if (elf_section_data (section)->relocs == NULL)
  2444. free (internal_relocs);
  2445. goto error_ret_free_local;
  2446. }
  2447. /* Only look for stubs on CALL and JMPI instructions. */
  2448. if (r_type != (unsigned int) R_NIOS2_CALL26)
  2449. continue;
  2450. /* Now determine the call target, its name, value,
  2451. section. */
  2452. sym_sec = NULL;
  2453. sym_value = 0;
  2454. destination = 0;
  2455. hh = NULL;
  2456. if (r_indx < symtab_hdr->sh_info)
  2457. {
  2458. /* It's a local symbol. */
  2459. Elf_Internal_Sym *sym;
  2460. Elf_Internal_Shdr *hdr;
  2461. unsigned int shndx;
  2462. sym = local_syms + r_indx;
  2463. if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  2464. sym_value = sym->st_value;
  2465. shndx = sym->st_shndx;
  2466. if (shndx < elf_numsections (input_bfd))
  2467. {
  2468. hdr = elf_elfsections (input_bfd)[shndx];
  2469. sym_sec = hdr->bfd_section;
  2470. destination = (sym_value + irela->r_addend
  2471. + sym_sec->output_offset
  2472. + sym_sec->output_section->vma);
  2473. }
  2474. }
  2475. else
  2476. {
  2477. /* It's an external symbol. */
  2478. int e_indx;
  2479. e_indx = r_indx - symtab_hdr->sh_info;
  2480. hh = ((struct elf32_nios2_link_hash_entry *)
  2481. elf_sym_hashes (input_bfd)[e_indx]);
  2482. while (hh->root.root.type == bfd_link_hash_indirect
  2483. || hh->root.root.type == bfd_link_hash_warning)
  2484. hh = ((struct elf32_nios2_link_hash_entry *)
  2485. hh->root.root.u.i.link);
  2486. if (hh->root.root.type == bfd_link_hash_defined
  2487. || hh->root.root.type == bfd_link_hash_defweak)
  2488. {
  2489. sym_sec = hh->root.root.u.def.section;
  2490. sym_value = hh->root.root.u.def.value;
  2491. if (sym_sec->output_section != NULL)
  2492. destination = (sym_value + irela->r_addend
  2493. + sym_sec->output_offset
  2494. + sym_sec->output_section->vma);
  2495. else
  2496. continue;
  2497. }
  2498. else if (hh->root.root.type == bfd_link_hash_undefweak)
  2499. {
  2500. if (! bfd_link_pic (info))
  2501. continue;
  2502. }
  2503. else if (hh->root.root.type == bfd_link_hash_undefined)
  2504. {
  2505. if (! (info->unresolved_syms_in_objects == RM_IGNORE
  2506. && (ELF_ST_VISIBILITY (hh->root.other)
  2507. == STV_DEFAULT)))
  2508. continue;
  2509. }
  2510. else
  2511. {
  2512. bfd_set_error (bfd_error_bad_value);
  2513. goto error_ret_free_internal;
  2514. }
  2515. }
  2516. /* Determine what (if any) linker stub is needed. */
  2517. stub_type = nios2_type_of_stub (section, irela, hh, htab,
  2518. destination, info);
  2519. if (stub_type == nios2_stub_none)
  2520. continue;
  2521. /* Support for grouping stub sections. */
  2522. if (stub_type == nios2_stub_call26_before)
  2523. id_sec = htab->stub_group[section->id].first_sec;
  2524. else
  2525. id_sec = htab->stub_group[section->id].last_sec;
  2526. /* Get the name of this stub. */
  2527. stub_name = nios2_stub_name (id_sec, sym_sec, hh, irela,
  2528. stub_type);
  2529. if (!stub_name)
  2530. goto error_ret_free_internal;
  2531. hsh = nios2_stub_hash_lookup (&htab->bstab,
  2532. stub_name,
  2533. FALSE, FALSE);
  2534. if (hsh != NULL)
  2535. {
  2536. /* The proper stub has already been created. */
  2537. free (stub_name);
  2538. continue;
  2539. }
  2540. hsh = nios2_add_stub (stub_name, section, htab, stub_type);
  2541. if (hsh == NULL)
  2542. {
  2543. free (stub_name);
  2544. goto error_ret_free_internal;
  2545. }
  2546. hsh->target_value = sym_value;
  2547. hsh->target_section = sym_sec;
  2548. hsh->stub_type = stub_type;
  2549. hsh->hh = hh;
  2550. hsh->addend = irela->r_addend;
  2551. stub_changed = TRUE;
  2552. }
  2553. /* We're done with the internal relocs, free them. */
  2554. if (elf_section_data (section)->relocs == NULL)
  2555. free (internal_relocs);
  2556. }
  2557. }
  2558. if (!stub_changed)
  2559. break;
  2560. /* OK, we've added some stubs. Find out the new size of the
  2561. stub sections. */
  2562. for (stub_sec = htab->stub_bfd->sections;
  2563. stub_sec != NULL;
  2564. stub_sec = stub_sec->next)
  2565. stub_sec->size = 0;
  2566. bfd_hash_traverse (&htab->bstab, nios2_size_one_stub, htab);
  2567. /* Ask the linker to do its stuff. */
  2568. (*htab->layout_sections_again) ();
  2569. stub_changed = FALSE;
  2570. }
  2571. free (htab->all_local_syms);
  2572. return TRUE;
  2573. error_ret_free_local:
  2574. free (htab->all_local_syms);
  2575. return FALSE;
  2576. }
  2577. /* Build all the stubs associated with the current output file. The
  2578. stubs are kept in a hash table attached to the main linker hash
  2579. table. This function is called via nios2elf_finish in the linker. */
  2580. bfd_boolean
  2581. nios2_elf32_build_stubs (struct bfd_link_info *info)
  2582. {
  2583. asection *stub_sec;
  2584. struct bfd_hash_table *table;
  2585. struct elf32_nios2_link_hash_table *htab;
  2586. htab = elf32_nios2_hash_table (info);
  2587. for (stub_sec = htab->stub_bfd->sections;
  2588. stub_sec != NULL;
  2589. stub_sec = stub_sec->next)
  2590. /* The stub_bfd may contain non-stub sections if it is also the
  2591. dynobj. Any such non-stub sections are created with the
  2592. SEC_LINKER_CREATED flag set, while stub sections do not
  2593. have that flag. Ignore any non-stub sections here. */
  2594. if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
  2595. {
  2596. bfd_size_type size;
  2597. /* Allocate memory to hold the linker stubs. */
  2598. size = stub_sec->size;
  2599. stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
  2600. if (stub_sec->contents == NULL && size != 0)
  2601. return FALSE;
  2602. stub_sec->size = 0;
  2603. }
  2604. /* Build the stubs as directed by the stub hash table. */
  2605. table = &htab->bstab;
  2606. bfd_hash_traverse (table, nios2_build_one_stub, info);
  2607. return TRUE;
  2608. }
  2609. #define is_nios2_elf(bfd) \
  2610. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  2611. && elf_object_id (bfd) == NIOS2_ELF_DATA)
  2612. /* Merge backend specific data from an object file to the output
  2613. object file when linking. */
  2614. static bfd_boolean
  2615. nios2_elf32_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
  2616. {
  2617. flagword old_flags;
  2618. flagword new_flags;
  2619. if (!is_nios2_elf (ibfd) || !is_nios2_elf (obfd))
  2620. return TRUE;
  2621. /* Check if we have the same endianness. */
  2622. if (! _bfd_generic_verify_endian_match (ibfd, obfd))
  2623. return FALSE;
  2624. new_flags = elf_elfheader (ibfd)->e_flags;
  2625. old_flags = elf_elfheader (obfd)->e_flags;
  2626. if (!elf_flags_init (obfd))
  2627. {
  2628. /* First call, no flags set. */
  2629. elf_flags_init (obfd) = TRUE;
  2630. elf_elfheader (obfd)->e_flags = new_flags;
  2631. switch (new_flags)
  2632. {
  2633. default:
  2634. case EF_NIOS2_ARCH_R1:
  2635. bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r1);
  2636. break;
  2637. case EF_NIOS2_ARCH_R2:
  2638. if (bfd_big_endian (ibfd))
  2639. {
  2640. (*_bfd_error_handler)
  2641. (_("error: %B: Big-endian R2 is not supported."), ibfd);
  2642. bfd_set_error (bfd_error_bad_value);
  2643. return FALSE;
  2644. }
  2645. bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r2);
  2646. break;
  2647. }
  2648. }
  2649. /* Incompatible flags. */
  2650. else if (new_flags != old_flags)
  2651. {
  2652. /* So far, the only incompatible flags denote incompatible
  2653. architectures. */
  2654. (*_bfd_error_handler)
  2655. (_("error: %B: Conflicting CPU architectures %d/%d"),
  2656. ibfd, new_flags, old_flags);
  2657. bfd_set_error (bfd_error_bad_value);
  2658. return FALSE;
  2659. }
  2660. /* Merge Tag_compatibility attributes and any common GNU ones. */
  2661. _bfd_elf_merge_object_attributes (ibfd, obfd);
  2662. return TRUE;
  2663. }
  2664. /* Implement bfd_elf32_bfd_reloc_type_lookup:
  2665. Given a BFD reloc type, return a howto structure. */
  2666. static reloc_howto_type *
  2667. nios2_elf32_bfd_reloc_type_lookup (bfd *abfd,
  2668. bfd_reloc_code_real_type code)
  2669. {
  2670. int i;
  2671. for (i = 0;
  2672. i < (int) (sizeof (nios2_reloc_map) / sizeof (struct elf_reloc_map));
  2673. ++i)
  2674. if (nios2_reloc_map[i].bfd_val == code)
  2675. return lookup_howto (nios2_reloc_map[i].elf_val, abfd);
  2676. return NULL;
  2677. }
  2678. /* Implement bfd_elf32_bfd_reloc_name_lookup:
  2679. Given a reloc name, return a howto structure. */
  2680. static reloc_howto_type *
  2681. nios2_elf32_bfd_reloc_name_lookup (bfd *abfd,
  2682. const char *r_name)
  2683. {
  2684. int i;
  2685. reloc_howto_type *howto_tbl;
  2686. int howto_tbl_size;
  2687. if (BFD_IS_R2 (abfd))
  2688. {
  2689. howto_tbl = elf_nios2_r2_howto_table_rel;
  2690. howto_tbl_size = (int) (sizeof (elf_nios2_r2_howto_table_rel)
  2691. / sizeof (elf_nios2_r2_howto_table_rel[0]));
  2692. }
  2693. else
  2694. {
  2695. howto_tbl = elf_nios2_r1_howto_table_rel;
  2696. howto_tbl_size = (int) (sizeof (elf_nios2_r1_howto_table_rel)
  2697. / sizeof (elf_nios2_r1_howto_table_rel[0]));
  2698. }
  2699. for (i = 0; i < howto_tbl_size; i++)
  2700. if (howto_tbl[i].name && strcasecmp (howto_tbl[i].name, r_name) == 0)
  2701. return howto_tbl + i;
  2702. return NULL;
  2703. }
  2704. /* Implement elf_info_to_howto:
  2705. Given a ELF32 relocation, fill in a arelent structure. */
  2706. static void
  2707. nios2_elf32_info_to_howto (bfd *abfd, arelent *cache_ptr,
  2708. Elf_Internal_Rela *dst)
  2709. {
  2710. unsigned int r_type;
  2711. r_type = ELF32_R_TYPE (dst->r_info);
  2712. cache_ptr->howto = lookup_howto (r_type, abfd);
  2713. }
  2714. /* Return the base VMA address which should be subtracted from real addresses
  2715. when resolving @dtpoff relocation.
  2716. This is PT_TLS segment p_vaddr. */
  2717. static bfd_vma
  2718. dtpoff_base (struct bfd_link_info *info)
  2719. {
  2720. /* If tls_sec is NULL, we should have signalled an error already. */
  2721. if (elf_hash_table (info)->tls_sec == NULL)
  2722. return 0;
  2723. return elf_hash_table (info)->tls_sec->vma;
  2724. }
  2725. /* Return the relocation value for @tpoff relocation
  2726. if STT_TLS virtual address is ADDRESS. */
  2727. static bfd_vma
  2728. tpoff (struct bfd_link_info *info, bfd_vma address)
  2729. {
  2730. struct elf_link_hash_table *htab = elf_hash_table (info);
  2731. /* If tls_sec is NULL, we should have signalled an error already. */
  2732. if (htab->tls_sec == NULL)
  2733. return 0;
  2734. return address - htab->tls_sec->vma;
  2735. }
  2736. /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
  2737. dangerous relocation. */
  2738. static bfd_boolean
  2739. nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
  2740. {
  2741. bfd_boolean gp_found;
  2742. struct bfd_hash_entry *h;
  2743. struct bfd_link_hash_entry *lh;
  2744. /* If we've already figured out what GP will be, just return it. */
  2745. *pgp = _bfd_get_gp_value (output_bfd);
  2746. if (*pgp)
  2747. return TRUE;
  2748. h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
  2749. lh = (struct bfd_link_hash_entry *) h;
  2750. lookup:
  2751. if (lh)
  2752. {
  2753. switch (lh->type)
  2754. {
  2755. case bfd_link_hash_undefined:
  2756. case bfd_link_hash_undefweak:
  2757. case bfd_link_hash_common:
  2758. gp_found = FALSE;
  2759. break;
  2760. case bfd_link_hash_defined:
  2761. case bfd_link_hash_defweak:
  2762. gp_found = TRUE;
  2763. *pgp = lh->u.def.value;
  2764. break;
  2765. case bfd_link_hash_indirect:
  2766. case bfd_link_hash_warning:
  2767. lh = lh->u.i.link;
  2768. /* @@FIXME ignoring warning for now */
  2769. goto lookup;
  2770. case bfd_link_hash_new:
  2771. default:
  2772. abort ();
  2773. }
  2774. }
  2775. else
  2776. gp_found = FALSE;
  2777. if (!gp_found)
  2778. {
  2779. /* Only get the error once. */
  2780. *pgp = 4;
  2781. _bfd_set_gp_value (output_bfd, *pgp);
  2782. return FALSE;
  2783. }
  2784. _bfd_set_gp_value (output_bfd, *pgp);
  2785. return TRUE;
  2786. }
  2787. /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
  2788. if it's not available as we don't have a link_info pointer available here
  2789. to look it up in the output symbol table. We don't need to adjust the
  2790. symbol value for an external symbol if we are producing relocatable
  2791. output. */
  2792. static bfd_reloc_status_type
  2793. nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
  2794. char **error_message, bfd_vma *pgp)
  2795. {
  2796. if (bfd_is_und_section (symbol->section) && !relocatable)
  2797. {
  2798. *pgp = 0;
  2799. return bfd_reloc_undefined;
  2800. }
  2801. *pgp = _bfd_get_gp_value (output_bfd);
  2802. if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
  2803. {
  2804. if (relocatable)
  2805. {
  2806. /* Make up a value. */
  2807. *pgp = symbol->section->output_section->vma + 0x4000;
  2808. _bfd_set_gp_value (output_bfd, *pgp);
  2809. }
  2810. else
  2811. {
  2812. *error_message
  2813. = (char *) _("global pointer relative relocation when _gp not defined");
  2814. return bfd_reloc_dangerous;
  2815. }
  2816. }
  2817. return bfd_reloc_ok;
  2818. }
  2819. /* Do the relocations that require special handling. */
  2820. static bfd_reloc_status_type
  2821. nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
  2822. asection *input_section,
  2823. bfd_byte *data, bfd_vma offset,
  2824. bfd_vma symbol_value, bfd_vma addend)
  2825. {
  2826. symbol_value = symbol_value + addend;
  2827. addend = 0;
  2828. symbol_value = (symbol_value >> 16) & 0xffff;
  2829. return _bfd_final_link_relocate (howto, abfd, input_section,
  2830. data, offset, symbol_value, addend);
  2831. }
  2832. static bfd_reloc_status_type
  2833. nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
  2834. asection *input_section,
  2835. bfd_byte *data, bfd_vma offset,
  2836. bfd_vma symbol_value, bfd_vma addend)
  2837. {
  2838. symbol_value = symbol_value + addend;
  2839. addend = 0;
  2840. symbol_value = symbol_value & 0xffff;
  2841. return _bfd_final_link_relocate (howto, abfd, input_section,
  2842. data, offset, symbol_value, addend);
  2843. }
  2844. static bfd_reloc_status_type
  2845. nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
  2846. asection *input_section,
  2847. bfd_byte *data, bfd_vma offset,
  2848. bfd_vma symbol_value, bfd_vma addend)
  2849. {
  2850. symbol_value = symbol_value + addend;
  2851. addend = 0;
  2852. symbol_value = hiadj(symbol_value);
  2853. return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
  2854. symbol_value, addend);
  2855. }
  2856. static bfd_reloc_status_type
  2857. nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
  2858. asection *input_section,
  2859. bfd_byte *data, bfd_vma offset,
  2860. bfd_vma symbol_value, bfd_vma addend)
  2861. {
  2862. symbol_value = symbol_value + addend;
  2863. addend = 0;
  2864. symbol_value = symbol_value & 0xffff;
  2865. return _bfd_final_link_relocate (howto, abfd, input_section,
  2866. data, offset, symbol_value, addend);
  2867. }
  2868. static bfd_reloc_status_type
  2869. nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
  2870. asection *input_section,
  2871. bfd_byte *data, bfd_vma offset,
  2872. bfd_vma symbol_value, bfd_vma addend)
  2873. {
  2874. symbol_value = symbol_value + addend;
  2875. symbol_value -= (input_section->output_section->vma
  2876. + input_section->output_offset);
  2877. symbol_value -= offset;
  2878. addend = 0;
  2879. symbol_value = hiadj(symbol_value);
  2880. return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
  2881. symbol_value, addend);
  2882. }
  2883. static bfd_reloc_status_type
  2884. nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
  2885. asection *input_section,
  2886. bfd_byte *data, bfd_vma offset,
  2887. bfd_vma symbol_value, bfd_vma addend)
  2888. {
  2889. /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
  2890. so we need to subtract 4 before doing a final_link_relocate. */
  2891. symbol_value = symbol_value + addend - 4;
  2892. addend = 0;
  2893. return _bfd_final_link_relocate (howto, abfd, input_section,
  2894. data, offset, symbol_value, addend);
  2895. }
  2896. static bfd_reloc_status_type
  2897. nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
  2898. asection *input_section,
  2899. bfd_byte *data, bfd_vma offset,
  2900. bfd_vma symbol_value, bfd_vma addend)
  2901. {
  2902. /* Check that the relocation is in the same page as the current address. */
  2903. if (CALL26_SEGMENT (symbol_value + addend)
  2904. != CALL26_SEGMENT (input_section->output_section->vma
  2905. + input_section->output_offset
  2906. + offset))
  2907. return bfd_reloc_overflow;
  2908. /* Check that the target address is correctly aligned on a 4-byte
  2909. boundary. */
  2910. if ((symbol_value + addend) & 0x3)
  2911. return bfd_reloc_overflow;
  2912. return _bfd_final_link_relocate (howto, abfd, input_section,
  2913. data, offset, symbol_value, addend);
  2914. }
  2915. static bfd_reloc_status_type
  2916. nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
  2917. asection *input_section,
  2918. bfd_byte *data, bfd_vma offset,
  2919. bfd_vma symbol_value, bfd_vma addend)
  2920. {
  2921. /* Because we need the output_bfd, the special handling is done
  2922. in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
  2923. return _bfd_final_link_relocate (howto, abfd, input_section,
  2924. data, offset, symbol_value, addend);
  2925. }
  2926. static bfd_reloc_status_type
  2927. nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
  2928. asection *input_section,
  2929. bfd_byte *data, bfd_vma offset,
  2930. bfd_vma symbol_value, bfd_vma addend)
  2931. {
  2932. bfd_vma symbol_lo16, symbol_hi16;
  2933. bfd_reloc_status_type r;
  2934. symbol_value = symbol_value + addend;
  2935. addend = 0;
  2936. symbol_hi16 = (symbol_value >> 16) & 0xffff;
  2937. symbol_lo16 = symbol_value & 0xffff;
  2938. r = _bfd_final_link_relocate (howto, abfd, input_section,
  2939. data, offset, symbol_hi16, addend);
  2940. if (r == bfd_reloc_ok)
  2941. return _bfd_final_link_relocate (howto, abfd, input_section,
  2942. data, offset + 4, symbol_lo16, addend);
  2943. return r;
  2944. }
  2945. static bfd_reloc_status_type
  2946. nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
  2947. asection *input_section,
  2948. bfd_byte *data, bfd_vma offset,
  2949. bfd_vma symbol_value, bfd_vma addend)
  2950. {
  2951. bfd_vma symbol_lo16, symbol_hi16;
  2952. bfd_reloc_status_type r;
  2953. symbol_value = symbol_value + addend;
  2954. addend = 0;
  2955. symbol_hi16 = (symbol_value >> 16) & 0xffff;
  2956. symbol_lo16 = symbol_value & 0xffff;
  2957. r = _bfd_final_link_relocate (howto, abfd, input_section,
  2958. data, offset, symbol_hi16, addend);
  2959. if (r == bfd_reloc_ok)
  2960. return _bfd_final_link_relocate (howto, abfd, input_section,
  2961. data, offset + 4, symbol_lo16, addend);
  2962. return r;
  2963. }
  2964. static bfd_reloc_status_type
  2965. nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
  2966. asection *input_section,
  2967. bfd_byte *data, bfd_vma offset,
  2968. bfd_vma symbol_value, bfd_vma addend)
  2969. {
  2970. bfd_vma symbol_lo16, symbol_hi16;
  2971. bfd_reloc_status_type r;
  2972. symbol_value = symbol_value + addend;
  2973. addend = 0;
  2974. symbol_hi16 = (symbol_value >> 16) & 0xffff;
  2975. symbol_lo16 = symbol_value & 0xffff;
  2976. r = _bfd_final_link_relocate (howto, abfd, input_section,
  2977. data, offset, symbol_hi16, addend);
  2978. if (r == bfd_reloc_ok)
  2979. return _bfd_final_link_relocate (howto, abfd, input_section,
  2980. data, offset + 4, symbol_lo16, addend);
  2981. return r;
  2982. }
  2983. /* HOWTO handlers for relocations that require special handling. */
  2984. /* This is for relocations used only when relaxing to ensure
  2985. changes in size of section don't screw up .align. */
  2986. static bfd_reloc_status_type
  2987. nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
  2988. asymbol *symbol ATTRIBUTE_UNUSED,
  2989. void *data ATTRIBUTE_UNUSED, asection *input_section,
  2990. bfd *output_bfd,
  2991. char **error_message ATTRIBUTE_UNUSED)
  2992. {
  2993. if (output_bfd != NULL)
  2994. reloc_entry->address += input_section->output_offset;
  2995. return bfd_reloc_ok;
  2996. }
  2997. static bfd_reloc_status_type
  2998. nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  2999. void *data, asection *input_section,
  3000. bfd *output_bfd,
  3001. char **error_message ATTRIBUTE_UNUSED)
  3002. {
  3003. /* This part is from bfd_elf_generic_reloc. */
  3004. if (output_bfd != NULL
  3005. && (symbol->flags & BSF_SECTION_SYM) == 0
  3006. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3007. {
  3008. reloc_entry->address += input_section->output_offset;
  3009. return bfd_reloc_ok;
  3010. }
  3011. if (output_bfd != NULL)
  3012. /* FIXME: See bfd_perform_relocation. Is this right? */
  3013. return bfd_reloc_continue;
  3014. return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
  3015. input_section,
  3016. data, reloc_entry->address,
  3017. (symbol->value
  3018. + symbol->section->output_section->vma
  3019. + symbol->section->output_offset),
  3020. reloc_entry->addend);
  3021. }
  3022. static bfd_reloc_status_type
  3023. nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  3024. void *data, asection *input_section,
  3025. bfd *output_bfd,
  3026. char **error_message ATTRIBUTE_UNUSED)
  3027. {
  3028. /* This part is from bfd_elf_generic_reloc. */
  3029. if (output_bfd != NULL
  3030. && (symbol->flags & BSF_SECTION_SYM) == 0
  3031. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3032. {
  3033. reloc_entry->address += input_section->output_offset;
  3034. return bfd_reloc_ok;
  3035. }
  3036. if (output_bfd != NULL)
  3037. /* FIXME: See bfd_perform_relocation. Is this right? */
  3038. return bfd_reloc_continue;
  3039. return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
  3040. input_section,
  3041. data, reloc_entry->address,
  3042. (symbol->value
  3043. + symbol->section->output_section->vma
  3044. + symbol->section->output_offset),
  3045. reloc_entry->addend);
  3046. }
  3047. static bfd_reloc_status_type
  3048. nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  3049. void *data, asection *input_section,
  3050. bfd *output_bfd,
  3051. char **error_message ATTRIBUTE_UNUSED)
  3052. {
  3053. /* This part is from bfd_elf_generic_reloc. */
  3054. if (output_bfd != NULL
  3055. && (symbol->flags & BSF_SECTION_SYM) == 0
  3056. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3057. {
  3058. reloc_entry->address += input_section->output_offset;
  3059. return bfd_reloc_ok;
  3060. }
  3061. if (output_bfd != NULL)
  3062. /* FIXME: See bfd_perform_relocation. Is this right? */
  3063. return bfd_reloc_continue;
  3064. return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
  3065. input_section,
  3066. data, reloc_entry->address,
  3067. (symbol->value
  3068. + symbol->section->output_section->vma
  3069. + symbol->section->output_offset),
  3070. reloc_entry->addend);
  3071. }
  3072. static bfd_reloc_status_type
  3073. nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
  3074. asymbol *symbol, void *data,
  3075. asection *input_section, bfd *output_bfd,
  3076. char **error_message ATTRIBUTE_UNUSED)
  3077. {
  3078. /* This part is from bfd_elf_generic_reloc. */
  3079. if (output_bfd != NULL
  3080. && (symbol->flags & BSF_SECTION_SYM) == 0
  3081. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3082. {
  3083. reloc_entry->address += input_section->output_offset;
  3084. return bfd_reloc_ok;
  3085. }
  3086. if (output_bfd != NULL)
  3087. /* FIXME: See bfd_perform_relocation. Is this right? */
  3088. return bfd_reloc_continue;
  3089. return nios2_elf32_do_pcrel_lo16_relocate (
  3090. abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
  3091. (symbol->value + symbol->section->output_section->vma
  3092. + symbol->section->output_offset),
  3093. reloc_entry->addend);
  3094. }
  3095. static bfd_reloc_status_type
  3096. nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
  3097. asymbol *symbol, void *data,
  3098. asection *input_section, bfd *output_bfd,
  3099. char **error_message ATTRIBUTE_UNUSED)
  3100. {
  3101. /* This part is from bfd_elf_generic_reloc. */
  3102. if (output_bfd != NULL
  3103. && (symbol->flags & BSF_SECTION_SYM) == 0
  3104. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3105. {
  3106. reloc_entry->address += input_section->output_offset;
  3107. return bfd_reloc_ok;
  3108. }
  3109. if (output_bfd != NULL)
  3110. /* FIXME: See bfd_perform_relocation. Is this right? */
  3111. return bfd_reloc_continue;
  3112. return nios2_elf32_do_pcrel_hiadj16_relocate (
  3113. abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
  3114. (symbol->value + symbol->section->output_section->vma
  3115. + symbol->section->output_offset),
  3116. reloc_entry->addend);
  3117. }
  3118. static bfd_reloc_status_type
  3119. nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  3120. void *data, asection *input_section,
  3121. bfd *output_bfd,
  3122. char **error_message ATTRIBUTE_UNUSED)
  3123. {
  3124. /* This part is from bfd_elf_generic_reloc. */
  3125. if (output_bfd != NULL
  3126. && (symbol->flags & BSF_SECTION_SYM) == 0
  3127. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3128. {
  3129. reloc_entry->address += input_section->output_offset;
  3130. return bfd_reloc_ok;
  3131. }
  3132. if (output_bfd != NULL)
  3133. /* FIXME: See bfd_perform_relocation. Is this right? */
  3134. return bfd_reloc_continue;
  3135. return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
  3136. input_section,
  3137. data, reloc_entry->address,
  3138. (symbol->value
  3139. + symbol->section->output_section->vma
  3140. + symbol->section->output_offset),
  3141. reloc_entry->addend);
  3142. }
  3143. static bfd_reloc_status_type
  3144. nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  3145. void *data, asection *input_section,
  3146. bfd *output_bfd,
  3147. char **error_message ATTRIBUTE_UNUSED)
  3148. {
  3149. /* This part is from bfd_elf_generic_reloc. */
  3150. if (output_bfd != NULL
  3151. && (symbol->flags & BSF_SECTION_SYM) == 0
  3152. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3153. {
  3154. reloc_entry->address += input_section->output_offset;
  3155. return bfd_reloc_ok;
  3156. }
  3157. if (output_bfd != NULL)
  3158. /* FIXME: See bfd_perform_relocation. Is this right? */
  3159. return bfd_reloc_continue;
  3160. return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
  3161. input_section,
  3162. data, reloc_entry->address,
  3163. (symbol->value
  3164. + symbol->section->output_section->vma
  3165. + symbol->section->output_offset),
  3166. reloc_entry->addend);
  3167. }
  3168. static bfd_reloc_status_type
  3169. nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  3170. void *data, asection *input_section,
  3171. bfd *output_bfd, char **msg)
  3172. {
  3173. bfd_vma relocation;
  3174. bfd_vma gp;
  3175. bfd_reloc_status_type r;
  3176. /* This part is from bfd_elf_generic_reloc. */
  3177. if (output_bfd != NULL
  3178. && (symbol->flags & BSF_SECTION_SYM) == 0
  3179. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3180. {
  3181. reloc_entry->address += input_section->output_offset;
  3182. return bfd_reloc_ok;
  3183. }
  3184. if (output_bfd != NULL)
  3185. /* FIXME: See bfd_perform_relocation. Is this right? */
  3186. return bfd_reloc_continue;
  3187. relocation = (symbol->value
  3188. + symbol->section->output_section->vma
  3189. + symbol->section->output_offset);
  3190. /* This assumes we've already cached the _gp symbol. */
  3191. r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
  3192. if (r == bfd_reloc_ok)
  3193. {
  3194. relocation = relocation + reloc_entry->addend - gp;
  3195. reloc_entry->addend = 0;
  3196. if ((signed) relocation < -32768 || (signed) relocation > 32767)
  3197. {
  3198. *msg = _("global pointer relative address out of range");
  3199. r = bfd_reloc_outofrange;
  3200. }
  3201. else
  3202. r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
  3203. input_section,
  3204. data, reloc_entry->address,
  3205. relocation, reloc_entry->addend);
  3206. }
  3207. return r;
  3208. }
  3209. static bfd_reloc_status_type
  3210. nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  3211. void *data, asection *input_section,
  3212. bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
  3213. {
  3214. /* This part is from bfd_elf_generic_reloc. */
  3215. if (output_bfd != NULL
  3216. && (symbol->flags & BSF_SECTION_SYM) == 0
  3217. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3218. {
  3219. reloc_entry->address += input_section->output_offset;
  3220. return bfd_reloc_ok;
  3221. }
  3222. if (output_bfd != NULL)
  3223. /* FIXME: See bfd_perform_relocation. Is this right? */
  3224. return bfd_reloc_continue;
  3225. return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
  3226. input_section,
  3227. data, reloc_entry->address,
  3228. (symbol->value
  3229. + symbol->section->output_section->vma
  3230. + symbol->section->output_offset),
  3231. reloc_entry->addend);
  3232. }
  3233. static bfd_reloc_status_type
  3234. nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  3235. void *data, asection *input_section,
  3236. bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
  3237. {
  3238. /* This part is from bfd_elf_generic_reloc. */
  3239. if (output_bfd != NULL
  3240. && (symbol->flags & BSF_SECTION_SYM) == 0
  3241. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3242. {
  3243. reloc_entry->address += input_section->output_offset;
  3244. return bfd_reloc_ok;
  3245. }
  3246. if (output_bfd != NULL)
  3247. /* FIXME: See bfd_perform_relocation. Is this right? */
  3248. return bfd_reloc_continue;
  3249. return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
  3250. input_section,
  3251. data, reloc_entry->address,
  3252. (symbol->value
  3253. + symbol->section->output_section->vma
  3254. + symbol->section->output_offset),
  3255. reloc_entry->addend);
  3256. }
  3257. static bfd_reloc_status_type
  3258. nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  3259. void *data, asection *input_section,
  3260. bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
  3261. {
  3262. /* This part is from bfd_elf_generic_reloc. */
  3263. if (output_bfd != NULL
  3264. && (symbol->flags & BSF_SECTION_SYM) == 0
  3265. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  3266. {
  3267. reloc_entry->address += input_section->output_offset;
  3268. return bfd_reloc_ok;
  3269. }
  3270. if (output_bfd != NULL)
  3271. /* FIXME: See bfd_perform_relocation. Is this right? */
  3272. return bfd_reloc_continue;
  3273. return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
  3274. input_section,
  3275. data, reloc_entry->address,
  3276. (symbol->value
  3277. + symbol->section->output_section->vma
  3278. + symbol->section->output_offset),
  3279. reloc_entry->addend);
  3280. }
  3281. /* Implement elf_backend_relocate_section. */
  3282. static bfd_boolean
  3283. nios2_elf32_relocate_section (bfd *output_bfd,
  3284. struct bfd_link_info *info,
  3285. bfd *input_bfd,
  3286. asection *input_section,
  3287. bfd_byte *contents,
  3288. Elf_Internal_Rela *relocs,
  3289. Elf_Internal_Sym *local_syms,
  3290. asection **local_sections)
  3291. {
  3292. Elf_Internal_Shdr *symtab_hdr;
  3293. struct elf_link_hash_entry **sym_hashes;
  3294. Elf_Internal_Rela *rel;
  3295. Elf_Internal_Rela *relend;
  3296. struct elf32_nios2_link_hash_table *htab;
  3297. asection *sgot;
  3298. asection *splt;
  3299. asection *sreloc = NULL;
  3300. bfd_vma *local_got_offsets;
  3301. bfd_vma got_base;
  3302. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  3303. sym_hashes = elf_sym_hashes (input_bfd);
  3304. relend = relocs + input_section->reloc_count;
  3305. htab = elf32_nios2_hash_table (info);
  3306. sgot = htab->root.sgot;
  3307. splt = htab->root.splt;
  3308. local_got_offsets = elf_local_got_offsets (input_bfd);
  3309. if (elf32_nios2_hash_table (info)->h_gp_got == NULL)
  3310. got_base = 0;
  3311. else
  3312. got_base = elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value;
  3313. for (rel = relocs; rel < relend; rel++)
  3314. {
  3315. reloc_howto_type *howto;
  3316. unsigned long r_symndx;
  3317. Elf_Internal_Sym *sym;
  3318. asection *sec;
  3319. struct elf_link_hash_entry *h;
  3320. struct elf32_nios2_link_hash_entry *eh;
  3321. bfd_vma relocation;
  3322. bfd_vma gp;
  3323. bfd_vma reloc_address;
  3324. bfd_reloc_status_type r = bfd_reloc_ok;
  3325. const char *name = NULL;
  3326. int r_type;
  3327. const char *format;
  3328. char msgbuf[256];
  3329. const char* msg = (const char*) NULL;
  3330. bfd_boolean unresolved_reloc;
  3331. bfd_vma off;
  3332. int use_plt;
  3333. r_type = ELF32_R_TYPE (rel->r_info);
  3334. r_symndx = ELF32_R_SYM (rel->r_info);
  3335. howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info), output_bfd);
  3336. h = NULL;
  3337. sym = NULL;
  3338. sec = NULL;
  3339. if (r_symndx < symtab_hdr->sh_info)
  3340. {
  3341. sym = local_syms + r_symndx;
  3342. sec = local_sections[r_symndx];
  3343. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  3344. }
  3345. else
  3346. {
  3347. bfd_boolean warned, ignored;
  3348. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  3349. r_symndx, symtab_hdr, sym_hashes,
  3350. h, sec, relocation,
  3351. unresolved_reloc, warned, ignored);
  3352. }
  3353. if (sec && discarded_section (sec))
  3354. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3355. rel, 1, relend, howto, 0, contents);
  3356. /* Nothing more to do unless this is a final link. */
  3357. if (bfd_link_relocatable (info))
  3358. continue;
  3359. if (sec && sec->output_section)
  3360. reloc_address = (sec->output_section->vma + sec->output_offset
  3361. + rel->r_offset);
  3362. else
  3363. reloc_address = 0;
  3364. if (howto)
  3365. {
  3366. switch (howto->type)
  3367. {
  3368. case R_NIOS2_HI16:
  3369. r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
  3370. input_section,
  3371. contents, rel->r_offset,
  3372. relocation, rel->r_addend);
  3373. break;
  3374. case R_NIOS2_LO16:
  3375. r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
  3376. input_section,
  3377. contents, rel->r_offset,
  3378. relocation, rel->r_addend);
  3379. break;
  3380. case R_NIOS2_PCREL_LO:
  3381. r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
  3382. input_section,
  3383. contents,
  3384. rel->r_offset,
  3385. relocation,
  3386. rel->r_addend);
  3387. break;
  3388. case R_NIOS2_HIADJ16:
  3389. r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
  3390. input_section, contents,
  3391. rel->r_offset, relocation,
  3392. rel->r_addend);
  3393. break;
  3394. case R_NIOS2_PCREL_HA:
  3395. r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
  3396. input_section,
  3397. contents,
  3398. rel->r_offset,
  3399. relocation,
  3400. rel->r_addend);
  3401. break;
  3402. case R_NIOS2_PCREL16:
  3403. r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
  3404. input_section, contents,
  3405. rel->r_offset, relocation,
  3406. rel->r_addend);
  3407. break;
  3408. case R_NIOS2_GPREL:
  3409. /* Turns an absolute address into a gp-relative address. */
  3410. if (!nios2_elf_assign_gp (output_bfd, &gp, info))
  3411. {
  3412. format = _("global pointer relative relocation at address "
  3413. "0x%08x when _gp not defined\n");
  3414. sprintf (msgbuf, format, reloc_address);
  3415. msg = msgbuf;
  3416. r = bfd_reloc_dangerous;
  3417. }
  3418. else
  3419. {
  3420. bfd_vma symbol_address = rel->r_addend + relocation;
  3421. relocation = relocation + rel->r_addend - gp;
  3422. rel->r_addend = 0;
  3423. if (((signed) relocation < -32768
  3424. || (signed) relocation > 32767)
  3425. && (!h
  3426. || h->root.type == bfd_link_hash_defined
  3427. || h->root.type == bfd_link_hash_defweak))
  3428. {
  3429. format = _("Unable to reach %s (at 0x%08x) from the "
  3430. "global pointer (at 0x%08x) because the "
  3431. "offset (%d) is out of the allowed range, "
  3432. "-32678 to 32767.\n" );
  3433. sprintf (msgbuf, format, name, symbol_address, gp,
  3434. (signed)relocation);
  3435. msg = msgbuf;
  3436. r = bfd_reloc_outofrange;
  3437. }
  3438. else
  3439. r = _bfd_final_link_relocate (howto, input_bfd,
  3440. input_section, contents,
  3441. rel->r_offset, relocation,
  3442. rel->r_addend);
  3443. }
  3444. break;
  3445. case R_NIOS2_UJMP:
  3446. r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
  3447. input_section,
  3448. contents, rel->r_offset,
  3449. relocation, rel->r_addend);
  3450. break;
  3451. case R_NIOS2_CJMP:
  3452. r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
  3453. input_section,
  3454. contents, rel->r_offset,
  3455. relocation, rel->r_addend);
  3456. break;
  3457. case R_NIOS2_CALLR:
  3458. r = nios2_elf32_do_callr_relocate (input_bfd, howto,
  3459. input_section, contents,
  3460. rel->r_offset, relocation,
  3461. rel->r_addend);
  3462. break;
  3463. case R_NIOS2_CALL26:
  3464. case R_NIOS2_CALL26_NOAT:
  3465. /* If we have a call to an undefined weak symbol, we just want
  3466. to stuff a zero in the bits of the call instruction and
  3467. bypass the normal call26 relocation handling, because it'll
  3468. diagnose an overflow error if address 0 isn't in the same
  3469. 256MB segment as the call site. Presumably the call
  3470. should be guarded by a null check anyway. */
  3471. if (h != NULL && h->root.type == bfd_link_hash_undefweak)
  3472. {
  3473. BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
  3474. r = _bfd_final_link_relocate (howto, input_bfd,
  3475. input_section, contents,
  3476. rel->r_offset, relocation,
  3477. rel->r_addend);
  3478. break;
  3479. }
  3480. /* Handle relocations which should use the PLT entry.
  3481. NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
  3482. which may point to a PLT entry, but we don't need to handle
  3483. that here. If we created a PLT entry, all branches in this
  3484. object should go to it. */
  3485. if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
  3486. {
  3487. /* If we've created a .plt section, and assigned a PLT entry
  3488. to this function, it should not be known to bind locally.
  3489. If it were, we would have cleared the PLT entry. */
  3490. BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
  3491. relocation = (splt->output_section->vma
  3492. + splt->output_offset
  3493. + h->plt.offset);
  3494. unresolved_reloc = FALSE;
  3495. }
  3496. /* Detect R_NIOS2_CALL26 relocations that would overflow the
  3497. 256MB segment. Replace the target with a reference to a
  3498. trampoline instead.
  3499. Note that htab->stub_group is null if relaxation has been
  3500. disabled by the --no-relax linker command-line option, so
  3501. we can use that to skip this processing entirely. */
  3502. if (howto->type == R_NIOS2_CALL26 && htab->stub_group)
  3503. {
  3504. bfd_vma dest = relocation + rel->r_addend;
  3505. enum elf32_nios2_stub_type stub_type;
  3506. eh = (struct elf32_nios2_link_hash_entry *)h;
  3507. stub_type = nios2_type_of_stub (input_section, rel, eh,
  3508. htab, dest, NULL);
  3509. if (stub_type != nios2_stub_none)
  3510. {
  3511. struct elf32_nios2_stub_hash_entry *hsh;
  3512. hsh = nios2_get_stub_entry (input_section, sec,
  3513. eh, rel, htab, stub_type);
  3514. if (hsh == NULL)
  3515. {
  3516. r = bfd_reloc_undefined;
  3517. break;
  3518. }
  3519. dest = (hsh->stub_offset
  3520. + hsh->stub_sec->output_offset
  3521. + hsh->stub_sec->output_section->vma);
  3522. r = nios2_elf32_do_call26_relocate (input_bfd, howto,
  3523. input_section,
  3524. contents,
  3525. rel->r_offset,
  3526. dest, 0);
  3527. break;
  3528. }
  3529. }
  3530. /* Normal case. */
  3531. r = nios2_elf32_do_call26_relocate (input_bfd, howto,
  3532. input_section, contents,
  3533. rel->r_offset, relocation,
  3534. rel->r_addend);
  3535. break;
  3536. case R_NIOS2_ALIGN:
  3537. r = bfd_reloc_ok;
  3538. /* For symmetry this would be
  3539. r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
  3540. input_section, contents,
  3541. rel->r_offset, relocation,
  3542. rel->r_addend);
  3543. but do_ignore_reloc would do no more than return
  3544. bfd_reloc_ok. */
  3545. break;
  3546. case R_NIOS2_GOT16:
  3547. case R_NIOS2_CALL16:
  3548. case R_NIOS2_GOT_LO:
  3549. case R_NIOS2_GOT_HA:
  3550. case R_NIOS2_CALL_LO:
  3551. case R_NIOS2_CALL_HA:
  3552. /* Relocation is to the entry for this symbol in the
  3553. global offset table. */
  3554. if (sgot == NULL)
  3555. {
  3556. r = bfd_reloc_notsupported;
  3557. break;
  3558. }
  3559. use_plt = 0;
  3560. if (h != NULL)
  3561. {
  3562. bfd_boolean dyn;
  3563. eh = (struct elf32_nios2_link_hash_entry *)h;
  3564. use_plt = (eh->got_types_used == CALL_USED
  3565. && h->plt.offset != (bfd_vma) -1);
  3566. off = h->got.offset;
  3567. BFD_ASSERT (off != (bfd_vma) -1);
  3568. dyn = elf_hash_table (info)->dynamic_sections_created;
  3569. if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  3570. bfd_link_pic (info),
  3571. h)
  3572. || (bfd_link_pic (info)
  3573. && SYMBOL_REFERENCES_LOCAL (info, h))
  3574. || (ELF_ST_VISIBILITY (h->other)
  3575. && h->root.type == bfd_link_hash_undefweak))
  3576. {
  3577. /* This is actually a static link, or it is a -Bsymbolic
  3578. link and the symbol is defined locally. We must
  3579. initialize this entry in the global offset table.
  3580. Since the offset must always be a multiple of 4, we
  3581. use the least significant bit to record whether we
  3582. have initialized it already.
  3583. When doing a dynamic link, we create a .rela.got
  3584. relocation entry to initialize the value. This is
  3585. done in the finish_dynamic_symbol routine. */
  3586. if ((off & 1) != 0)
  3587. off &= ~1;
  3588. else
  3589. {
  3590. bfd_put_32 (output_bfd, relocation,
  3591. sgot->contents + off);
  3592. h->got.offset |= 1;
  3593. }
  3594. }
  3595. else
  3596. unresolved_reloc = FALSE;
  3597. }
  3598. else
  3599. {
  3600. BFD_ASSERT (local_got_offsets != NULL
  3601. && local_got_offsets[r_symndx] != (bfd_vma) -1);
  3602. off = local_got_offsets[r_symndx];
  3603. /* The offset must always be a multiple of 4. We use the
  3604. least significant bit to record whether we have already
  3605. generated the necessary reloc. */
  3606. if ((off & 1) != 0)
  3607. off &= ~1;
  3608. else
  3609. {
  3610. bfd_put_32 (output_bfd, relocation,
  3611. sgot->contents + off);
  3612. if (bfd_link_pic (info))
  3613. {
  3614. asection *srelgot;
  3615. Elf_Internal_Rela outrel;
  3616. bfd_byte *loc;
  3617. srelgot = htab->root.srelgot;
  3618. BFD_ASSERT (srelgot != NULL);
  3619. outrel.r_addend = relocation;
  3620. outrel.r_offset = (sgot->output_section->vma
  3621. + sgot->output_offset
  3622. + off);
  3623. outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
  3624. loc = srelgot->contents;
  3625. loc += (srelgot->reloc_count++ *
  3626. sizeof (Elf32_External_Rela));
  3627. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3628. }
  3629. local_got_offsets[r_symndx] |= 1;
  3630. }
  3631. }
  3632. if (use_plt && bfd_link_pic (info))
  3633. {
  3634. off = ((h->plt.offset - 24) / 12 + 3) * 4;
  3635. relocation = (htab->root.sgotplt->output_offset + off
  3636. - got_base);
  3637. }
  3638. else
  3639. relocation = sgot->output_offset + off - got_base;
  3640. /* This relocation does not use the addend. */
  3641. rel->r_addend = 0;
  3642. switch (howto->type)
  3643. {
  3644. case R_NIOS2_GOT_LO:
  3645. case R_NIOS2_CALL_LO:
  3646. r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
  3647. input_section, contents,
  3648. rel->r_offset, relocation,
  3649. rel->r_addend);
  3650. break;
  3651. case R_NIOS2_GOT_HA:
  3652. case R_NIOS2_CALL_HA:
  3653. r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
  3654. input_section, contents,
  3655. rel->r_offset,
  3656. relocation,
  3657. rel->r_addend);
  3658. break;
  3659. default:
  3660. r = _bfd_final_link_relocate (howto, input_bfd,
  3661. input_section, contents,
  3662. rel->r_offset, relocation,
  3663. rel->r_addend);
  3664. break;
  3665. }
  3666. break;
  3667. case R_NIOS2_GOTOFF_LO:
  3668. case R_NIOS2_GOTOFF_HA:
  3669. case R_NIOS2_GOTOFF:
  3670. /* Relocation is relative to the global offset table pointer. */
  3671. BFD_ASSERT (sgot != NULL);
  3672. if (sgot == NULL)
  3673. {
  3674. r = bfd_reloc_notsupported;
  3675. break;
  3676. }
  3677. /* Note that sgot->output_offset is not involved in this
  3678. calculation. We always want the start of .got. */
  3679. relocation -= sgot->output_section->vma;
  3680. /* Now we adjust the relocation to be relative to the GOT pointer
  3681. (the _gp_got symbol), which possibly contains the 0x8000 bias. */
  3682. relocation -= got_base;
  3683. switch (howto->type)
  3684. {
  3685. case R_NIOS2_GOTOFF_LO:
  3686. r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
  3687. input_section, contents,
  3688. rel->r_offset, relocation,
  3689. rel->r_addend);
  3690. break;
  3691. case R_NIOS2_GOTOFF_HA:
  3692. r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
  3693. input_section, contents,
  3694. rel->r_offset,
  3695. relocation,
  3696. rel->r_addend);
  3697. break;
  3698. default:
  3699. r = _bfd_final_link_relocate (howto, input_bfd,
  3700. input_section, contents,
  3701. rel->r_offset, relocation,
  3702. rel->r_addend);
  3703. break;
  3704. }
  3705. break;
  3706. case R_NIOS2_TLS_LDO16:
  3707. relocation -= dtpoff_base (info) + DTP_OFFSET;
  3708. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  3709. contents, rel->r_offset,
  3710. relocation, rel->r_addend);
  3711. break;
  3712. case R_NIOS2_TLS_LDM16:
  3713. if (htab->root.sgot == NULL)
  3714. abort ();
  3715. off = htab->tls_ldm_got.offset;
  3716. if ((off & 1) != 0)
  3717. off &= ~1;
  3718. else
  3719. {
  3720. /* If we don't know the module number, create a relocation
  3721. for it. */
  3722. if (bfd_link_pic (info))
  3723. {
  3724. Elf_Internal_Rela outrel;
  3725. bfd_byte *loc;
  3726. if (htab->root.srelgot == NULL)
  3727. abort ();
  3728. outrel.r_addend = 0;
  3729. outrel.r_offset = (htab->root.sgot->output_section->vma
  3730. + htab->root.sgot->output_offset
  3731. + off);
  3732. outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
  3733. loc = htab->root.srelgot->contents;
  3734. loc += (htab->root.srelgot->reloc_count++
  3735. * sizeof (Elf32_External_Rela));
  3736. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3737. }
  3738. else
  3739. bfd_put_32 (output_bfd, 1,
  3740. htab->root.sgot->contents + off);
  3741. htab->tls_ldm_got.offset |= 1;
  3742. }
  3743. relocation = htab->root.sgot->output_offset + off - got_base;
  3744. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  3745. contents, rel->r_offset,
  3746. relocation, rel->r_addend);
  3747. break;
  3748. case R_NIOS2_TLS_GD16:
  3749. case R_NIOS2_TLS_IE16:
  3750. {
  3751. int indx;
  3752. char tls_type;
  3753. if (htab->root.sgot == NULL)
  3754. abort ();
  3755. indx = 0;
  3756. if (h != NULL)
  3757. {
  3758. bfd_boolean dyn;
  3759. dyn = htab->root.dynamic_sections_created;
  3760. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  3761. bfd_link_pic (info),
  3762. h)
  3763. && (!bfd_link_pic (info)
  3764. || !SYMBOL_REFERENCES_LOCAL (info, h)))
  3765. {
  3766. unresolved_reloc = FALSE;
  3767. indx = h->dynindx;
  3768. }
  3769. off = h->got.offset;
  3770. tls_type = (((struct elf32_nios2_link_hash_entry *) h)
  3771. ->tls_type);
  3772. }
  3773. else
  3774. {
  3775. if (local_got_offsets == NULL)
  3776. abort ();
  3777. off = local_got_offsets[r_symndx];
  3778. tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
  3779. [r_symndx]);
  3780. }
  3781. if (tls_type == GOT_UNKNOWN)
  3782. abort ();
  3783. if ((off & 1) != 0)
  3784. off &= ~1;
  3785. else
  3786. {
  3787. bfd_boolean need_relocs = FALSE;
  3788. Elf_Internal_Rela outrel;
  3789. bfd_byte *loc = NULL;
  3790. int cur_off = off;
  3791. /* The GOT entries have not been initialized yet. Do it
  3792. now, and emit any relocations. If both an IE GOT and a
  3793. GD GOT are necessary, we emit the GD first. */
  3794. if ((bfd_link_pic (info) || indx != 0)
  3795. && (h == NULL
  3796. || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  3797. || h->root.type != bfd_link_hash_undefweak))
  3798. {
  3799. need_relocs = TRUE;
  3800. if (htab->root.srelgot == NULL)
  3801. abort ();
  3802. loc = htab->root.srelgot->contents;
  3803. loc += (htab->root.srelgot->reloc_count *
  3804. sizeof (Elf32_External_Rela));
  3805. }
  3806. if (tls_type & GOT_TLS_GD)
  3807. {
  3808. if (need_relocs)
  3809. {
  3810. outrel.r_addend = 0;
  3811. outrel.r_offset = (htab->root.sgot->output_section->vma
  3812. + htab->root.sgot->output_offset
  3813. + cur_off);
  3814. outrel.r_info = ELF32_R_INFO (indx,
  3815. R_NIOS2_TLS_DTPMOD);
  3816. bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  3817. loc);
  3818. htab->root.srelgot->reloc_count++;
  3819. loc += sizeof (Elf32_External_Rela);
  3820. if (indx == 0)
  3821. bfd_put_32 (output_bfd,
  3822. (relocation - dtpoff_base (info) -
  3823. DTP_OFFSET),
  3824. htab->root.sgot->contents + cur_off + 4);
  3825. else
  3826. {
  3827. outrel.r_addend = 0;
  3828. outrel.r_info = ELF32_R_INFO (indx,
  3829. R_NIOS2_TLS_DTPREL);
  3830. outrel.r_offset += 4;
  3831. bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  3832. loc);
  3833. htab->root.srelgot->reloc_count++;
  3834. loc += sizeof (Elf32_External_Rela);
  3835. }
  3836. }
  3837. else
  3838. {
  3839. /* If we are not emitting relocations for a
  3840. general dynamic reference, then we must be in a
  3841. static link or an executable link with the
  3842. symbol binding locally. Mark it as belonging
  3843. to module 1, the executable. */
  3844. bfd_put_32 (output_bfd, 1,
  3845. htab->root.sgot->contents + cur_off);
  3846. bfd_put_32 (output_bfd, (relocation -
  3847. dtpoff_base (info) -
  3848. DTP_OFFSET),
  3849. htab->root.sgot->contents + cur_off + 4);
  3850. }
  3851. cur_off += 8;
  3852. }
  3853. if (tls_type & GOT_TLS_IE)
  3854. {
  3855. if (need_relocs)
  3856. {
  3857. if (indx == 0)
  3858. outrel.r_addend = (relocation -
  3859. dtpoff_base (info));
  3860. else
  3861. outrel.r_addend = 0;
  3862. outrel.r_offset = (htab->root.sgot->output_section->vma
  3863. + htab->root.sgot->output_offset
  3864. + cur_off);
  3865. outrel.r_info = ELF32_R_INFO (indx,
  3866. R_NIOS2_TLS_TPREL);
  3867. bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  3868. loc);
  3869. htab->root.srelgot->reloc_count++;
  3870. loc += sizeof (Elf32_External_Rela);
  3871. }
  3872. else
  3873. bfd_put_32 (output_bfd, (tpoff (info, relocation)
  3874. - TP_OFFSET),
  3875. htab->root.sgot->contents + cur_off);
  3876. cur_off += 4;
  3877. }
  3878. if (h != NULL)
  3879. h->got.offset |= 1;
  3880. else
  3881. local_got_offsets[r_symndx] |= 1;
  3882. }
  3883. if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
  3884. off += 8;
  3885. relocation = htab->root.sgot->output_offset + off - got_base;
  3886. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  3887. contents, rel->r_offset,
  3888. relocation, rel->r_addend);
  3889. }
  3890. break;
  3891. case R_NIOS2_TLS_LE16:
  3892. if (bfd_link_dll (info))
  3893. {
  3894. (*_bfd_error_handler)
  3895. (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
  3896. "permitted in shared object"),
  3897. input_bfd, input_section,
  3898. (long) rel->r_offset, howto->name);
  3899. return FALSE;
  3900. }
  3901. else
  3902. relocation = tpoff (info, relocation) - TP_OFFSET;
  3903. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  3904. contents, rel->r_offset,
  3905. relocation, rel->r_addend);
  3906. break;
  3907. case R_NIOS2_BFD_RELOC_32:
  3908. if (bfd_link_pic (info)
  3909. && (input_section->flags & SEC_ALLOC) != 0
  3910. && (h == NULL
  3911. || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  3912. || h->root.type != bfd_link_hash_undefweak))
  3913. {
  3914. Elf_Internal_Rela outrel;
  3915. bfd_byte *loc;
  3916. bfd_boolean skip, relocate;
  3917. /* When generating a shared object, these relocations
  3918. are copied into the output file to be resolved at run
  3919. time. */
  3920. skip = FALSE;
  3921. relocate = FALSE;
  3922. outrel.r_offset
  3923. = _bfd_elf_section_offset (output_bfd, info,
  3924. input_section, rel->r_offset);
  3925. if (outrel.r_offset == (bfd_vma) -1)
  3926. skip = TRUE;
  3927. else if (outrel.r_offset == (bfd_vma) -2)
  3928. skip = TRUE, relocate = TRUE;
  3929. outrel.r_offset += (input_section->output_section->vma
  3930. + input_section->output_offset);
  3931. if (skip)
  3932. memset (&outrel, 0, sizeof outrel);
  3933. else if (h != NULL
  3934. && h->dynindx != -1
  3935. && (!bfd_link_pic (info)
  3936. || !SYMBOLIC_BIND (info, h)
  3937. || !h->def_regular))
  3938. {
  3939. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  3940. outrel.r_addend = rel->r_addend;
  3941. }
  3942. else
  3943. {
  3944. /* This symbol is local, or marked to become local. */
  3945. outrel.r_addend = relocation + rel->r_addend;
  3946. relocate = TRUE;
  3947. outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
  3948. }
  3949. sreloc = elf_section_data (input_section)->sreloc;
  3950. if (sreloc == NULL)
  3951. abort ();
  3952. loc = sreloc->contents;
  3953. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  3954. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3955. /* This reloc will be computed at runtime, so there's no
  3956. need to do anything now, except for R_NIOS2_BFD_RELOC_32
  3957. relocations that have been turned into
  3958. R_NIOS2_RELATIVE. */
  3959. if (!relocate)
  3960. break;
  3961. }
  3962. r = _bfd_final_link_relocate (howto, input_bfd,
  3963. input_section, contents,
  3964. rel->r_offset, relocation,
  3965. rel->r_addend);
  3966. break;
  3967. case R_NIOS2_TLS_DTPREL:
  3968. relocation -= dtpoff_base (info);
  3969. /* Fall through. */
  3970. default:
  3971. r = _bfd_final_link_relocate (howto, input_bfd,
  3972. input_section, contents,
  3973. rel->r_offset, relocation,
  3974. rel->r_addend);
  3975. break;
  3976. }
  3977. }
  3978. else
  3979. r = bfd_reloc_notsupported;
  3980. if (r != bfd_reloc_ok)
  3981. {
  3982. if (h != NULL)
  3983. name = h->root.root.string;
  3984. else
  3985. {
  3986. name = bfd_elf_string_from_elf_section (input_bfd,
  3987. symtab_hdr->sh_link,
  3988. sym->st_name);
  3989. if (name == NULL || *name == '\0')
  3990. name = bfd_section_name (input_bfd, sec);
  3991. }
  3992. switch (r)
  3993. {
  3994. case bfd_reloc_overflow:
  3995. r = info->callbacks->reloc_overflow (info, NULL, name,
  3996. howto->name, (bfd_vma) 0,
  3997. input_bfd, input_section,
  3998. rel->r_offset);
  3999. break;
  4000. case bfd_reloc_undefined:
  4001. r = info->callbacks->undefined_symbol (info, name, input_bfd,
  4002. input_section,
  4003. rel->r_offset, TRUE);
  4004. break;
  4005. case bfd_reloc_outofrange:
  4006. if (msg == NULL)
  4007. msg = _("relocation out of range");
  4008. break;
  4009. case bfd_reloc_notsupported:
  4010. if (msg == NULL)
  4011. msg = _("unsupported relocation");
  4012. break;
  4013. case bfd_reloc_dangerous:
  4014. if (msg == NULL)
  4015. msg = _("dangerous relocation");
  4016. break;
  4017. default:
  4018. if (msg == NULL)
  4019. msg = _("unknown error");
  4020. break;
  4021. }
  4022. if (msg)
  4023. {
  4024. r = info->callbacks->warning
  4025. (info, msg, name, input_bfd, input_section, rel->r_offset);
  4026. return FALSE;
  4027. }
  4028. }
  4029. }
  4030. return TRUE;
  4031. }
  4032. /* Implement elf-backend_section_flags:
  4033. Convert NIOS2 specific section flags to bfd internal section flags. */
  4034. static bfd_boolean
  4035. nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
  4036. {
  4037. if (hdr->sh_flags & SHF_NIOS2_GPREL)
  4038. *flags |= SEC_SMALL_DATA;
  4039. return TRUE;
  4040. }
  4041. /* Implement elf_backend_fake_sections:
  4042. Set the correct type for an NIOS2 ELF section. We do this by the
  4043. section name, which is a hack, but ought to work. */
  4044. static bfd_boolean
  4045. nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
  4046. Elf_Internal_Shdr *hdr, asection *sec)
  4047. {
  4048. register const char *name = bfd_get_section_name (abfd, sec);
  4049. if ((sec->flags & SEC_SMALL_DATA)
  4050. || strcmp (name, ".sdata") == 0
  4051. || strcmp (name, ".sbss") == 0
  4052. || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
  4053. hdr->sh_flags |= SHF_NIOS2_GPREL;
  4054. return TRUE;
  4055. }
  4056. /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
  4057. shortcuts to them in our hash table. */
  4058. static bfd_boolean
  4059. create_got_section (bfd *dynobj, struct bfd_link_info *info)
  4060. {
  4061. struct elf32_nios2_link_hash_table *htab;
  4062. struct elf_link_hash_entry *h;
  4063. htab = elf32_nios2_hash_table (info);
  4064. if (! _bfd_elf_create_got_section (dynobj, info))
  4065. return FALSE;
  4066. /* In order for the two loads in .PLTresolve to share the same %hiadj,
  4067. _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
  4068. if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4))
  4069. return FALSE;
  4070. /* The Nios II ABI specifies that GOT-relative relocations are relative
  4071. to the linker-created symbol _gp_got, rather than using
  4072. _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
  4073. points to the base of the GOT while _gp_got may include a bias. */
  4074. h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.sgotplt,
  4075. "_gp_got");
  4076. elf32_nios2_hash_table (info)->h_gp_got = h;
  4077. if (h == NULL)
  4078. return FALSE;
  4079. return TRUE;
  4080. }
  4081. /* Implement elf_backend_create_dynamic_sections:
  4082. Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
  4083. .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
  4084. hash table. */
  4085. static bfd_boolean
  4086. nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
  4087. {
  4088. struct elf32_nios2_link_hash_table *htab;
  4089. htab = elf32_nios2_hash_table (info);
  4090. if (!htab->root.sgot && !create_got_section (dynobj, info))
  4091. return FALSE;
  4092. _bfd_elf_create_dynamic_sections (dynobj, info);
  4093. /* In order for the two loads in a shared object .PLTresolve to share the
  4094. same %hiadj, the start of the PLT (as well as the GOT) must be aligned
  4095. to a 16-byte boundary. This is because the addresses for these loads
  4096. include the -(.plt+4) PIC correction. */
  4097. if (!bfd_set_section_alignment (dynobj, htab->root.splt, 4))
  4098. return FALSE;
  4099. htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
  4100. if (!htab->sdynbss)
  4101. return FALSE;
  4102. if (!bfd_link_pic (info))
  4103. {
  4104. htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
  4105. if (!htab->srelbss)
  4106. return FALSE;
  4107. }
  4108. return TRUE;
  4109. }
  4110. /* Implement elf_backend_copy_indirect_symbol:
  4111. Copy the extra info we tack onto an elf_link_hash_entry. */
  4112. static void
  4113. nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
  4114. struct elf_link_hash_entry *dir,
  4115. struct elf_link_hash_entry *ind)
  4116. {
  4117. struct elf32_nios2_link_hash_entry *edir, *eind;
  4118. edir = (struct elf32_nios2_link_hash_entry *) dir;
  4119. eind = (struct elf32_nios2_link_hash_entry *) ind;
  4120. if (eind->dyn_relocs != NULL)
  4121. {
  4122. if (edir->dyn_relocs != NULL)
  4123. {
  4124. struct elf32_nios2_dyn_relocs **pp;
  4125. struct elf32_nios2_dyn_relocs *p;
  4126. /* Add reloc counts against the indirect sym to the direct sym
  4127. list. Merge any entries against the same section. */
  4128. for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
  4129. {
  4130. struct elf32_nios2_dyn_relocs *q;
  4131. for (q = edir->dyn_relocs; q != NULL; q = q->next)
  4132. if (q->sec == p->sec)
  4133. {
  4134. q->pc_count += p->pc_count;
  4135. q->count += p->count;
  4136. *pp = p->next;
  4137. break;
  4138. }
  4139. if (q == NULL)
  4140. pp = &p->next;
  4141. }
  4142. *pp = edir->dyn_relocs;
  4143. }
  4144. edir->dyn_relocs = eind->dyn_relocs;
  4145. eind->dyn_relocs = NULL;
  4146. }
  4147. if (ind->root.type == bfd_link_hash_indirect
  4148. && dir->got.refcount <= 0)
  4149. {
  4150. edir->tls_type = eind->tls_type;
  4151. eind->tls_type = GOT_UNKNOWN;
  4152. }
  4153. edir->got_types_used |= eind->got_types_used;
  4154. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  4155. }
  4156. /* Set the right machine number for a NIOS2 ELF file. */
  4157. static bfd_boolean
  4158. nios2_elf32_object_p (bfd *abfd)
  4159. {
  4160. unsigned long mach;
  4161. mach = elf_elfheader (abfd)->e_flags;
  4162. switch (mach)
  4163. {
  4164. default:
  4165. case EF_NIOS2_ARCH_R1:
  4166. bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r1);
  4167. break;
  4168. case EF_NIOS2_ARCH_R2:
  4169. bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r2);
  4170. break;
  4171. }
  4172. return TRUE;
  4173. }
  4174. /* Implement elf_backend_check_relocs:
  4175. Look through the relocs for a section during the first phase. */
  4176. static bfd_boolean
  4177. nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
  4178. asection *sec, const Elf_Internal_Rela *relocs)
  4179. {
  4180. bfd *dynobj;
  4181. Elf_Internal_Shdr *symtab_hdr;
  4182. struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
  4183. const Elf_Internal_Rela *rel;
  4184. const Elf_Internal_Rela *rel_end;
  4185. struct elf32_nios2_link_hash_table *htab;
  4186. asection *sgot;
  4187. asection *srelgot;
  4188. asection *sreloc = NULL;
  4189. bfd_signed_vma *local_got_refcounts;
  4190. if (bfd_link_relocatable (info))
  4191. return TRUE;
  4192. dynobj = elf_hash_table (info)->dynobj;
  4193. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  4194. sym_hashes = elf_sym_hashes (abfd);
  4195. sym_hashes_end = (sym_hashes
  4196. + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
  4197. if (!elf_bad_symtab (abfd))
  4198. sym_hashes_end -= symtab_hdr->sh_info;
  4199. local_got_refcounts = elf_local_got_refcounts (abfd);
  4200. htab = elf32_nios2_hash_table (info);
  4201. sgot = htab->root.sgot;
  4202. srelgot = htab->root.srelgot;
  4203. rel_end = relocs + sec->reloc_count;
  4204. for (rel = relocs; rel < rel_end; rel++)
  4205. {
  4206. unsigned int r_type;
  4207. struct elf_link_hash_entry *h;
  4208. unsigned long r_symndx;
  4209. r_symndx = ELF32_R_SYM (rel->r_info);
  4210. if (r_symndx < symtab_hdr->sh_info)
  4211. h = NULL;
  4212. else
  4213. {
  4214. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  4215. while (h->root.type == bfd_link_hash_indirect
  4216. || h->root.type == bfd_link_hash_warning)
  4217. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4218. /* PR15323, ref flags aren't set for references in the same
  4219. object. */
  4220. h->root.non_ir_ref = 1;
  4221. }
  4222. r_type = ELF32_R_TYPE (rel->r_info);
  4223. switch (r_type)
  4224. {
  4225. case R_NIOS2_GOT16:
  4226. case R_NIOS2_GOT_LO:
  4227. case R_NIOS2_GOT_HA:
  4228. case R_NIOS2_CALL16:
  4229. case R_NIOS2_CALL_LO:
  4230. case R_NIOS2_CALL_HA:
  4231. case R_NIOS2_TLS_GD16:
  4232. case R_NIOS2_TLS_IE16:
  4233. /* This symbol requires a global offset table entry. */
  4234. {
  4235. int tls_type, old_tls_type;
  4236. switch (r_type)
  4237. {
  4238. default:
  4239. case R_NIOS2_GOT16:
  4240. case R_NIOS2_GOT_LO:
  4241. case R_NIOS2_GOT_HA:
  4242. case R_NIOS2_CALL16:
  4243. case R_NIOS2_CALL_LO:
  4244. case R_NIOS2_CALL_HA:
  4245. tls_type = GOT_NORMAL;
  4246. break;
  4247. case R_NIOS2_TLS_GD16:
  4248. tls_type = GOT_TLS_GD;
  4249. break;
  4250. case R_NIOS2_TLS_IE16:
  4251. tls_type = GOT_TLS_IE;
  4252. break;
  4253. }
  4254. if (dynobj == NULL)
  4255. {
  4256. /* Create the .got section. */
  4257. elf_hash_table (info)->dynobj = dynobj = abfd;
  4258. nios2_elf32_create_dynamic_sections (dynobj, info);
  4259. }
  4260. if (sgot == NULL)
  4261. {
  4262. sgot = htab->root.sgot;
  4263. BFD_ASSERT (sgot != NULL);
  4264. }
  4265. if (srelgot == NULL
  4266. && (h != NULL || bfd_link_pic (info)))
  4267. {
  4268. srelgot = htab->root.srelgot;
  4269. BFD_ASSERT (srelgot != NULL);
  4270. }
  4271. if (h != NULL)
  4272. {
  4273. struct elf32_nios2_link_hash_entry *eh
  4274. = (struct elf32_nios2_link_hash_entry *)h;
  4275. h->got.refcount++;
  4276. old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
  4277. if (r_type == R_NIOS2_CALL16
  4278. || r_type == R_NIOS2_CALL_LO
  4279. || r_type == R_NIOS2_CALL_HA)
  4280. {
  4281. /* Make sure a plt entry is created for this symbol if
  4282. it turns out to be a function defined by a dynamic
  4283. object. */
  4284. h->plt.refcount++;
  4285. h->needs_plt = 1;
  4286. h->type = STT_FUNC;
  4287. eh->got_types_used |= CALL_USED;
  4288. }
  4289. else
  4290. eh->got_types_used |= GOT_USED;
  4291. }
  4292. else
  4293. {
  4294. /* This is a global offset table entry for a local symbol. */
  4295. if (local_got_refcounts == NULL)
  4296. {
  4297. bfd_size_type size;
  4298. size = symtab_hdr->sh_info;
  4299. size *= (sizeof (bfd_signed_vma) + sizeof (char));
  4300. local_got_refcounts
  4301. = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
  4302. if (local_got_refcounts == NULL)
  4303. return FALSE;
  4304. elf_local_got_refcounts (abfd) = local_got_refcounts;
  4305. elf32_nios2_local_got_tls_type (abfd)
  4306. = (char *) (local_got_refcounts + symtab_hdr->sh_info);
  4307. }
  4308. local_got_refcounts[r_symndx]++;
  4309. old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
  4310. }
  4311. /* We will already have issued an error message if there is a
  4312. TLS / non-TLS mismatch, based on the symbol type. We don't
  4313. support any linker relaxations. So just combine any TLS
  4314. types needed. */
  4315. if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
  4316. && tls_type != GOT_NORMAL)
  4317. tls_type |= old_tls_type;
  4318. if (old_tls_type != tls_type)
  4319. {
  4320. if (h != NULL)
  4321. elf32_nios2_hash_entry (h)->tls_type = tls_type;
  4322. else
  4323. elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
  4324. }
  4325. }
  4326. /* Fall through */
  4327. case R_NIOS2_TLS_LDM16:
  4328. if (r_type == R_NIOS2_TLS_LDM16)
  4329. htab->tls_ldm_got.refcount++;
  4330. if (htab->root.sgot == NULL)
  4331. {
  4332. if (htab->root.dynobj == NULL)
  4333. htab->root.dynobj = abfd;
  4334. if (!create_got_section (htab->root.dynobj, info))
  4335. return FALSE;
  4336. }
  4337. break;
  4338. /* This relocation describes the C++ object vtable hierarchy.
  4339. Reconstruct it for later use during GC. */
  4340. case R_NIOS2_GNU_VTINHERIT:
  4341. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  4342. return FALSE;
  4343. break;
  4344. /* This relocation describes which C++ vtable entries are actually
  4345. used. Record for later use during GC. */
  4346. case R_NIOS2_GNU_VTENTRY:
  4347. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  4348. return FALSE;
  4349. break;
  4350. case R_NIOS2_BFD_RELOC_32:
  4351. case R_NIOS2_CALL26:
  4352. case R_NIOS2_CALL26_NOAT:
  4353. case R_NIOS2_HIADJ16:
  4354. case R_NIOS2_LO16:
  4355. if (h != NULL)
  4356. {
  4357. /* If this reloc is in a read-only section, we might
  4358. need a copy reloc. We can't check reliably at this
  4359. stage whether the section is read-only, as input
  4360. sections have not yet been mapped to output sections.
  4361. Tentatively set the flag for now, and correct in
  4362. adjust_dynamic_symbol. */
  4363. if (!bfd_link_pic (info))
  4364. h->non_got_ref = 1;
  4365. /* Make sure a plt entry is created for this symbol if it
  4366. turns out to be a function defined by a dynamic object. */
  4367. h->plt.refcount++;
  4368. if (r_type == R_NIOS2_CALL26 || r_type == R_NIOS2_CALL26_NOAT)
  4369. h->needs_plt = 1;
  4370. }
  4371. /* If we are creating a shared library, we need to copy the
  4372. reloc into the shared library. */
  4373. if (bfd_link_pic (info)
  4374. && (sec->flags & SEC_ALLOC) != 0
  4375. && (r_type == R_NIOS2_BFD_RELOC_32
  4376. || (h != NULL && ! h->needs_plt
  4377. && (! SYMBOLIC_BIND (info, h) || ! h->def_regular))))
  4378. {
  4379. struct elf32_nios2_dyn_relocs *p;
  4380. struct elf32_nios2_dyn_relocs **head;
  4381. /* When creating a shared object, we must copy these
  4382. reloc types into the output file. We create a reloc
  4383. section in dynobj and make room for this reloc. */
  4384. if (sreloc == NULL)
  4385. {
  4386. sreloc = _bfd_elf_make_dynamic_reloc_section
  4387. (sec, dynobj, 2, abfd, TRUE);
  4388. if (sreloc == NULL)
  4389. return FALSE;
  4390. }
  4391. /* If this is a global symbol, we count the number of
  4392. relocations we need for this symbol. */
  4393. if (h != NULL)
  4394. head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
  4395. else
  4396. {
  4397. /* Track dynamic relocs needed for local syms too.
  4398. We really need local syms available to do this
  4399. easily. Oh well. */
  4400. asection *s;
  4401. void *vpp;
  4402. Elf_Internal_Sym *isym;
  4403. isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  4404. abfd, r_symndx);
  4405. if (isym == NULL)
  4406. return FALSE;
  4407. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  4408. if (s == NULL)
  4409. s = sec;
  4410. vpp = &elf_section_data (s)->local_dynrel;
  4411. head = (struct elf32_nios2_dyn_relocs **) vpp;
  4412. }
  4413. p = *head;
  4414. if (p == NULL || p->sec != sec)
  4415. {
  4416. bfd_size_type amt = sizeof *p;
  4417. p = ((struct elf32_nios2_dyn_relocs *)
  4418. bfd_alloc (htab->root.dynobj, amt));
  4419. if (p == NULL)
  4420. return FALSE;
  4421. p->next = *head;
  4422. *head = p;
  4423. p->sec = sec;
  4424. p->count = 0;
  4425. p->pc_count = 0;
  4426. }
  4427. p->count += 1;
  4428. }
  4429. break;
  4430. }
  4431. }
  4432. return TRUE;
  4433. }
  4434. /* Implement elf_backend_gc_mark_hook:
  4435. Return the section that should be marked against GC for a given
  4436. relocation. */
  4437. static asection *
  4438. nios2_elf32_gc_mark_hook (asection *sec,
  4439. struct bfd_link_info *info,
  4440. Elf_Internal_Rela *rel,
  4441. struct elf_link_hash_entry *h,
  4442. Elf_Internal_Sym *sym)
  4443. {
  4444. if (h != NULL)
  4445. switch (ELF32_R_TYPE (rel->r_info))
  4446. {
  4447. case R_NIOS2_GNU_VTINHERIT:
  4448. case R_NIOS2_GNU_VTENTRY:
  4449. return NULL;
  4450. }
  4451. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  4452. }
  4453. /* Implement elf_backend_gc_sweep_hook:
  4454. Update the got entry reference counts for the section being removed. */
  4455. static bfd_boolean
  4456. nios2_elf32_gc_sweep_hook (bfd *abfd,
  4457. struct bfd_link_info *info,
  4458. asection *sec,
  4459. const Elf_Internal_Rela *relocs)
  4460. {
  4461. Elf_Internal_Shdr *symtab_hdr;
  4462. struct elf_link_hash_entry **sym_hashes;
  4463. bfd_signed_vma *local_got_refcounts;
  4464. const Elf_Internal_Rela *rel, *relend;
  4465. bfd *dynobj;
  4466. if (bfd_link_relocatable (info))
  4467. return TRUE;
  4468. elf_section_data (sec)->local_dynrel = NULL;
  4469. dynobj = elf_hash_table (info)->dynobj;
  4470. if (dynobj == NULL)
  4471. return TRUE;
  4472. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  4473. sym_hashes = elf_sym_hashes (abfd);
  4474. local_got_refcounts = elf_local_got_refcounts (abfd);
  4475. relend = relocs + sec->reloc_count;
  4476. for (rel = relocs; rel < relend; rel++)
  4477. {
  4478. unsigned long r_symndx;
  4479. struct elf_link_hash_entry *h = NULL;
  4480. int r_type;
  4481. r_symndx = ELF32_R_SYM (rel->r_info);
  4482. if (r_symndx >= symtab_hdr->sh_info)
  4483. {
  4484. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  4485. while (h->root.type == bfd_link_hash_indirect
  4486. || h->root.type == bfd_link_hash_warning)
  4487. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4488. }
  4489. r_type = ELF32_R_TYPE (rel->r_info);
  4490. switch (r_type)
  4491. {
  4492. case R_NIOS2_GOT16:
  4493. case R_NIOS2_GOT_LO:
  4494. case R_NIOS2_GOT_HA:
  4495. case R_NIOS2_CALL16:
  4496. case R_NIOS2_CALL_LO:
  4497. case R_NIOS2_CALL_HA:
  4498. if (h != NULL)
  4499. {
  4500. if (h->got.refcount > 0)
  4501. --h->got.refcount;
  4502. }
  4503. else if (local_got_refcounts != NULL)
  4504. {
  4505. if (local_got_refcounts[r_symndx] > 0)
  4506. --local_got_refcounts[r_symndx];
  4507. }
  4508. break;
  4509. case R_NIOS2_PCREL_LO:
  4510. case R_NIOS2_PCREL_HA:
  4511. case R_NIOS2_BFD_RELOC_32:
  4512. case R_NIOS2_CALL26:
  4513. case R_NIOS2_CALL26_NOAT:
  4514. if (h != NULL)
  4515. {
  4516. struct elf32_nios2_link_hash_entry *eh;
  4517. struct elf32_nios2_dyn_relocs **pp;
  4518. struct elf32_nios2_dyn_relocs *p;
  4519. eh = (struct elf32_nios2_link_hash_entry *) h;
  4520. if (h->plt.refcount > 0)
  4521. --h->plt.refcount;
  4522. if (r_type == R_NIOS2_PCREL_LO || r_type == R_NIOS2_PCREL_HA
  4523. || r_type == R_NIOS2_BFD_RELOC_32)
  4524. {
  4525. for (pp = &eh->dyn_relocs; (p = *pp) != NULL;
  4526. pp = &p->next)
  4527. if (p->sec == sec)
  4528. {
  4529. p->count -= 1;
  4530. if (p->count == 0)
  4531. *pp = p->next;
  4532. break;
  4533. }
  4534. }
  4535. }
  4536. break;
  4537. default:
  4538. break;
  4539. }
  4540. }
  4541. return TRUE;
  4542. }
  4543. /* Implement elf_backend_finish_dynamic_symbols:
  4544. Finish up dynamic symbol handling. We set the contents of various
  4545. dynamic sections here. */
  4546. static bfd_boolean
  4547. nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
  4548. struct bfd_link_info *info,
  4549. struct elf_link_hash_entry *h,
  4550. Elf_Internal_Sym *sym)
  4551. {
  4552. struct elf32_nios2_link_hash_table *htab;
  4553. struct elf32_nios2_link_hash_entry *eh
  4554. = (struct elf32_nios2_link_hash_entry *)h;
  4555. int use_plt;
  4556. htab = elf32_nios2_hash_table (info);
  4557. if (h->plt.offset != (bfd_vma) -1)
  4558. {
  4559. asection *splt;
  4560. asection *sgotplt;
  4561. asection *srela;
  4562. bfd_vma plt_index;
  4563. bfd_vma got_offset;
  4564. Elf_Internal_Rela rela;
  4565. bfd_byte *loc;
  4566. bfd_vma got_address;
  4567. /* This symbol has an entry in the procedure linkage table. Set
  4568. it up. */
  4569. BFD_ASSERT (h->dynindx != -1);
  4570. splt = htab->root.splt;
  4571. sgotplt = htab->root.sgotplt;
  4572. srela = htab->root.srelplt;
  4573. BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
  4574. /* Emit the PLT entry. */
  4575. if (bfd_link_pic (info))
  4576. {
  4577. nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
  4578. 3);
  4579. plt_index = (h->plt.offset - 24) / 12;
  4580. got_offset = (plt_index + 3) * 4;
  4581. nios2_elf32_install_imm16 (splt, h->plt.offset,
  4582. hiadj(plt_index * 4));
  4583. nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
  4584. (plt_index * 4) & 0xffff);
  4585. nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
  4586. 0xfff4 - h->plt.offset);
  4587. got_address = (sgotplt->output_section->vma + sgotplt->output_offset
  4588. + got_offset);
  4589. /* Fill in the entry in the global offset table. There are no
  4590. res_n slots for a shared object PLT, instead the .got.plt entries
  4591. point to the PLT entries. */
  4592. bfd_put_32 (output_bfd,
  4593. splt->output_section->vma + splt->output_offset
  4594. + h->plt.offset, sgotplt->contents + got_offset);
  4595. }
  4596. else
  4597. {
  4598. plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
  4599. got_offset = (plt_index + 3) * 4;
  4600. nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
  4601. got_address = (sgotplt->output_section->vma + sgotplt->output_offset
  4602. + got_offset);
  4603. nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
  4604. nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
  4605. got_address & 0xffff);
  4606. /* Fill in the entry in the global offset table. */
  4607. bfd_put_32 (output_bfd,
  4608. splt->output_section->vma + splt->output_offset
  4609. + plt_index * 4, sgotplt->contents + got_offset);
  4610. }
  4611. /* Fill in the entry in the .rela.plt section. */
  4612. rela.r_offset = got_address;
  4613. rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
  4614. rela.r_addend = 0;
  4615. loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
  4616. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  4617. if (!h->def_regular)
  4618. {
  4619. /* Mark the symbol as undefined, rather than as defined in
  4620. the .plt section. Leave the value alone. */
  4621. sym->st_shndx = SHN_UNDEF;
  4622. /* If the symbol is weak, we do need to clear the value.
  4623. Otherwise, the PLT entry would provide a definition for
  4624. the symbol even if the symbol wasn't defined anywhere,
  4625. and so the symbol would never be NULL. */
  4626. if (!h->ref_regular_nonweak)
  4627. sym->st_value = 0;
  4628. }
  4629. }
  4630. use_plt = (eh->got_types_used == CALL_USED
  4631. && h->plt.offset != (bfd_vma) -1);
  4632. if (!use_plt && h->got.offset != (bfd_vma) -1
  4633. && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
  4634. && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
  4635. {
  4636. asection *sgot;
  4637. asection *srela;
  4638. Elf_Internal_Rela rela;
  4639. bfd_byte *loc;
  4640. bfd_vma offset;
  4641. /* This symbol has an entry in the global offset table. Set it
  4642. up. */
  4643. sgot = htab->root.sgot;
  4644. srela = htab->root.srelgot;
  4645. BFD_ASSERT (sgot != NULL && srela != NULL);
  4646. offset = (h->got.offset & ~(bfd_vma) 1);
  4647. rela.r_offset = (sgot->output_section->vma
  4648. + sgot->output_offset + offset);
  4649. /* If this is a -Bsymbolic link, and the symbol is defined
  4650. locally, we just want to emit a RELATIVE reloc. Likewise if
  4651. the symbol was forced to be local because of a version file.
  4652. The entry in the global offset table will already have been
  4653. initialized in the relocate_section function. */
  4654. if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
  4655. {
  4656. rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
  4657. rela.r_addend = bfd_get_signed_32 (output_bfd,
  4658. (sgot->contents + offset));
  4659. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
  4660. }
  4661. else
  4662. {
  4663. bfd_put_32 (output_bfd, (bfd_vma) 0,
  4664. sgot->contents + offset);
  4665. rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
  4666. rela.r_addend = 0;
  4667. }
  4668. loc = srela->contents;
  4669. loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
  4670. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  4671. }
  4672. if (use_plt && h->got.offset != (bfd_vma) -1)
  4673. {
  4674. bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
  4675. asection *sgot = htab->root.sgot;
  4676. asection *splt = htab->root.splt;
  4677. bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
  4678. + h->plt.offset),
  4679. sgot->contents + offset);
  4680. }
  4681. if (h->needs_copy)
  4682. {
  4683. asection *s;
  4684. Elf_Internal_Rela rela;
  4685. bfd_byte *loc;
  4686. /* This symbol needs a copy reloc. Set it up. */
  4687. BFD_ASSERT (h->dynindx != -1
  4688. && (h->root.type == bfd_link_hash_defined
  4689. || h->root.type == bfd_link_hash_defweak));
  4690. s = htab->srelbss;
  4691. BFD_ASSERT (s != NULL);
  4692. rela.r_offset = (h->root.u.def.value
  4693. + h->root.u.def.section->output_section->vma
  4694. + h->root.u.def.section->output_offset);
  4695. rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
  4696. rela.r_addend = 0;
  4697. loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
  4698. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  4699. }
  4700. /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
  4701. if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  4702. || h == elf_hash_table (info)->hgot
  4703. || h == elf32_nios2_hash_table (info)->h_gp_got)
  4704. sym->st_shndx = SHN_ABS;
  4705. return TRUE;
  4706. }
  4707. /* Implement elf_backend_finish_dynamic_sections. */
  4708. static bfd_boolean
  4709. nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
  4710. struct bfd_link_info *info)
  4711. {
  4712. bfd *dynobj;
  4713. asection *sgotplt;
  4714. asection *sdyn;
  4715. struct elf32_nios2_link_hash_table *htab;
  4716. htab = elf32_nios2_hash_table (info);
  4717. dynobj = elf_hash_table (info)->dynobj;
  4718. sgotplt = htab->root.sgotplt;
  4719. BFD_ASSERT (sgotplt != NULL);
  4720. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  4721. if (elf_hash_table (info)->dynamic_sections_created)
  4722. {
  4723. asection *splt;
  4724. Elf32_External_Dyn *dyncon, *dynconend;
  4725. splt = htab->root.splt;
  4726. BFD_ASSERT (splt != NULL && sdyn != NULL);
  4727. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  4728. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  4729. for (; dyncon < dynconend; dyncon++)
  4730. {
  4731. Elf_Internal_Dyn dyn;
  4732. asection *s;
  4733. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  4734. switch (dyn.d_tag)
  4735. {
  4736. default:
  4737. break;
  4738. case DT_PLTGOT:
  4739. s = htab->root.sgot;
  4740. BFD_ASSERT (s != NULL);
  4741. dyn.d_un.d_ptr = s->output_section->vma;
  4742. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4743. break;
  4744. case DT_JMPREL:
  4745. s = htab->root.srelplt;
  4746. BFD_ASSERT (s != NULL);
  4747. dyn.d_un.d_ptr = s->output_section->vma;
  4748. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4749. break;
  4750. case DT_PLTRELSZ:
  4751. s = htab->root.srelplt;
  4752. BFD_ASSERT (s != NULL);
  4753. dyn.d_un.d_val = s->size;
  4754. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4755. break;
  4756. case DT_RELASZ:
  4757. /* The procedure linkage table relocs (DT_JMPREL) should
  4758. not be included in the overall relocs (DT_RELA).
  4759. Therefore, we override the DT_RELASZ entry here to
  4760. make it not include the JMPREL relocs. Since the
  4761. linker script arranges for .rela.plt to follow all
  4762. other relocation sections, we don't have to worry
  4763. about changing the DT_RELA entry. */
  4764. s = htab->root.srelplt;
  4765. if (s != NULL)
  4766. dyn.d_un.d_val -= s->size;
  4767. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4768. break;
  4769. case DT_NIOS2_GP:
  4770. s = htab->root.sgot;
  4771. BFD_ASSERT (s != NULL);
  4772. dyn.d_un.d_ptr = s->output_section->vma + 0x7ff0;
  4773. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4774. break;
  4775. }
  4776. }
  4777. /* Fill in the first entry in the procedure linkage table. */
  4778. if (splt->size > 0)
  4779. {
  4780. bfd_vma got_address = (sgotplt->output_section->vma
  4781. + sgotplt->output_offset);
  4782. if (bfd_link_pic (info))
  4783. {
  4784. bfd_vma corrected = got_address - (splt->output_section->vma
  4785. + splt->output_offset + 4);
  4786. nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
  4787. nios2_elf32_install_imm16 (splt, 4, hiadj (corrected));
  4788. nios2_elf32_install_imm16 (splt, 12, (corrected & 0xffff) + 4);
  4789. nios2_elf32_install_imm16 (splt, 16, (corrected & 0xffff) + 8);
  4790. }
  4791. else
  4792. {
  4793. /* Divide by 4 here, not 3 because we already corrected for the
  4794. res_N branches. */
  4795. bfd_vma res_size = (splt->size - 28) / 4;
  4796. bfd_vma res_start = (splt->output_section->vma
  4797. + splt->output_offset);
  4798. bfd_vma res_offset;
  4799. for (res_offset = 0; res_offset < res_size; res_offset += 4)
  4800. bfd_put_32 (output_bfd,
  4801. 6 | ((res_size - (res_offset + 4)) << 6),
  4802. splt->contents + res_offset);
  4803. nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
  4804. nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
  4805. nios2_elf32_install_imm16 (splt, res_size + 4,
  4806. res_start & 0xffff);
  4807. nios2_elf32_install_imm16 (splt, res_size + 12,
  4808. hiadj (got_address));
  4809. nios2_elf32_install_imm16 (splt, res_size + 16,
  4810. (got_address & 0xffff) + 4);
  4811. nios2_elf32_install_imm16 (splt, res_size + 20,
  4812. (got_address & 0xffff) + 8);
  4813. }
  4814. }
  4815. }
  4816. /* Fill in the first three entries in the global offset table. */
  4817. if (sgotplt->size > 0)
  4818. {
  4819. if (sdyn == NULL)
  4820. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
  4821. else
  4822. bfd_put_32 (output_bfd,
  4823. sdyn->output_section->vma + sdyn->output_offset,
  4824. sgotplt->contents);
  4825. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
  4826. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
  4827. }
  4828. elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
  4829. return TRUE;
  4830. }
  4831. /* Implement elf_backend_adjust_dynamic_symbol:
  4832. Adjust a symbol defined by a dynamic object and referenced by a
  4833. regular object. The current definition is in some section of the
  4834. dynamic object, but we're not including those sections. We have to
  4835. change the definition to something the rest of the link can
  4836. understand. */
  4837. static bfd_boolean
  4838. nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
  4839. struct elf_link_hash_entry *h)
  4840. {
  4841. struct elf32_nios2_link_hash_table *htab;
  4842. bfd *dynobj;
  4843. asection *s;
  4844. unsigned align2;
  4845. htab = elf32_nios2_hash_table (info);
  4846. dynobj = elf_hash_table (info)->dynobj;
  4847. /* Make sure we know what is going on here. */
  4848. BFD_ASSERT (dynobj != NULL
  4849. && (h->needs_plt
  4850. || h->u.weakdef != NULL
  4851. || (h->def_dynamic
  4852. && h->ref_regular
  4853. && !h->def_regular)));
  4854. /* If this is a function, put it in the procedure linkage table. We
  4855. will fill in the contents of the procedure linkage table later,
  4856. when we know the address of the .got section. */
  4857. if (h->type == STT_FUNC || h->needs_plt)
  4858. {
  4859. if (h->plt.refcount <= 0
  4860. || SYMBOL_CALLS_LOCAL (info, h)
  4861. || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  4862. && h->root.type == bfd_link_hash_undefweak))
  4863. {
  4864. /* This case can occur if we saw a PLT reloc in an input
  4865. file, but the symbol was never referred to by a dynamic
  4866. object, or if all references were garbage collected. In
  4867. such a case, we don't actually need to build a procedure
  4868. linkage table, and we can just do a PCREL reloc instead. */
  4869. h->plt.offset = (bfd_vma) -1;
  4870. h->needs_plt = 0;
  4871. }
  4872. return TRUE;
  4873. }
  4874. /* Reinitialize the plt offset now that it is not used as a reference
  4875. count any more. */
  4876. h->plt.offset = (bfd_vma) -1;
  4877. /* If this is a weak symbol, and there is a real definition, the
  4878. processor independent code will have arranged for us to see the
  4879. real definition first, and we can just use the same value. */
  4880. if (h->u.weakdef != NULL)
  4881. {
  4882. BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
  4883. || h->u.weakdef->root.type == bfd_link_hash_defweak);
  4884. h->root.u.def.section = h->u.weakdef->root.u.def.section;
  4885. h->root.u.def.value = h->u.weakdef->root.u.def.value;
  4886. return TRUE;
  4887. }
  4888. /* If there are no non-GOT references, we do not need a copy
  4889. relocation. */
  4890. if (!h->non_got_ref)
  4891. return TRUE;
  4892. /* This is a reference to a symbol defined by a dynamic object which
  4893. is not a function.
  4894. If we are creating a shared library, we must presume that the
  4895. only references to the symbol are via the global offset table.
  4896. For such cases we need not do anything here; the relocations will
  4897. be handled correctly by relocate_section. */
  4898. if (bfd_link_pic (info))
  4899. return TRUE;
  4900. if (h->size == 0)
  4901. {
  4902. (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
  4903. h->root.root.string);
  4904. return TRUE;
  4905. }
  4906. /* We must allocate the symbol in our .dynbss section, which will
  4907. become part of the .bss section of the executable. There will be
  4908. an entry for this symbol in the .dynsym section. The dynamic
  4909. object will contain position independent code, so all references
  4910. from the dynamic object to this symbol will go through the global
  4911. offset table. The dynamic linker will use the .dynsym entry to
  4912. determine the address it must put in the global offset table, so
  4913. both the dynamic object and the regular object will refer to the
  4914. same memory location for the variable. */
  4915. s = htab->sdynbss;
  4916. BFD_ASSERT (s != NULL);
  4917. /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
  4918. copy the initial value out of the dynamic object and into the
  4919. runtime process image. We need to remember the offset into the
  4920. .rela.bss section we are going to use. */
  4921. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
  4922. {
  4923. asection *srel;
  4924. srel = htab->srelbss;
  4925. BFD_ASSERT (srel != NULL);
  4926. srel->size += sizeof (Elf32_External_Rela);
  4927. h->needs_copy = 1;
  4928. }
  4929. align2 = bfd_log2 (h->size);
  4930. if (align2 > h->root.u.def.section->alignment_power)
  4931. align2 = h->root.u.def.section->alignment_power;
  4932. /* Align dynbss. */
  4933. s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
  4934. if (align2 > bfd_get_section_alignment (dynobj, s)
  4935. && !bfd_set_section_alignment (dynobj, s, align2))
  4936. return FALSE;
  4937. /* Define the symbol as being at this point in the section. */
  4938. h->root.u.def.section = s;
  4939. h->root.u.def.value = s->size;
  4940. /* Increment the section size to make room for the symbol. */
  4941. s->size += h->size;
  4942. return TRUE;
  4943. }
  4944. /* Worker function for nios2_elf32_size_dynamic_sections. */
  4945. static bfd_boolean
  4946. adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
  4947. {
  4948. struct bfd_link_info *info;
  4949. struct elf32_nios2_link_hash_table *htab;
  4950. if (h->root.type == bfd_link_hash_indirect)
  4951. return TRUE;
  4952. if (h->root.type == bfd_link_hash_warning)
  4953. /* When warning symbols are created, they **replace** the "real"
  4954. entry in the hash table, thus we never get to see the real
  4955. symbol in a hash traversal. So look at it now. */
  4956. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4957. info = (struct bfd_link_info *) inf;
  4958. htab = elf32_nios2_hash_table (info);
  4959. if (h->plt.offset != (bfd_vma)-1)
  4960. h->plt.offset += htab->res_n_size;
  4961. if (htab->root.splt == h->root.u.def.section)
  4962. h->root.u.def.value += htab->res_n_size;
  4963. return TRUE;
  4964. }
  4965. /* Another worker function for nios2_elf32_size_dynamic_sections.
  4966. Allocate space in .plt, .got and associated reloc sections for
  4967. dynamic relocs. */
  4968. static bfd_boolean
  4969. allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
  4970. {
  4971. struct bfd_link_info *info;
  4972. struct elf32_nios2_link_hash_table *htab;
  4973. struct elf32_nios2_link_hash_entry *eh;
  4974. struct elf32_nios2_dyn_relocs *p;
  4975. int use_plt;
  4976. if (h->root.type == bfd_link_hash_indirect)
  4977. return TRUE;
  4978. if (h->root.type == bfd_link_hash_warning)
  4979. /* When warning symbols are created, they **replace** the "real"
  4980. entry in the hash table, thus we never get to see the real
  4981. symbol in a hash traversal. So look at it now. */
  4982. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4983. info = (struct bfd_link_info *) inf;
  4984. htab = elf32_nios2_hash_table (info);
  4985. if (htab->root.dynamic_sections_created
  4986. && h->plt.refcount > 0)
  4987. {
  4988. /* Make sure this symbol is output as a dynamic symbol.
  4989. Undefined weak syms won't yet be marked as dynamic. */
  4990. if (h->dynindx == -1
  4991. && !h->forced_local
  4992. && !bfd_elf_link_record_dynamic_symbol (info, h))
  4993. return FALSE;
  4994. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
  4995. {
  4996. asection *s = htab->root.splt;
  4997. /* Allocate room for the header. */
  4998. if (s->size == 0)
  4999. {
  5000. if (bfd_link_pic (info))
  5001. s->size = 24;
  5002. else
  5003. s->size = 28;
  5004. }
  5005. h->plt.offset = s->size;
  5006. /* If this symbol is not defined in a regular file, and we are
  5007. not generating a shared library, then set the symbol to this
  5008. location in the .plt. This is required to make function
  5009. pointers compare as equal between the normal executable and
  5010. the shared library. */
  5011. if (! bfd_link_pic (info)
  5012. && !h->def_regular)
  5013. {
  5014. h->root.u.def.section = s;
  5015. h->root.u.def.value = h->plt.offset;
  5016. }
  5017. /* Make room for this entry. */
  5018. s->size += 12;
  5019. /* We also need to make an entry in the .rela.plt section. */
  5020. htab->root.srelplt->size += sizeof (Elf32_External_Rela);
  5021. /* And the .got.plt section. */
  5022. htab->root.sgotplt->size += 4;
  5023. }
  5024. else
  5025. {
  5026. h->plt.offset = (bfd_vma) -1;
  5027. h->needs_plt = 0;
  5028. }
  5029. }
  5030. else
  5031. {
  5032. h->plt.offset = (bfd_vma) -1;
  5033. h->needs_plt = 0;
  5034. }
  5035. eh = (struct elf32_nios2_link_hash_entry *) h;
  5036. use_plt = (eh->got_types_used == CALL_USED
  5037. && h->plt.offset != (bfd_vma) -1);
  5038. if (h->got.refcount > 0)
  5039. {
  5040. asection *s;
  5041. bfd_boolean dyn;
  5042. int tls_type = eh->tls_type;
  5043. int indx;
  5044. /* Make sure this symbol is output as a dynamic symbol.
  5045. Undefined weak syms won't yet be marked as dynamic. */
  5046. if (h->dynindx == -1
  5047. && !h->forced_local
  5048. && !bfd_elf_link_record_dynamic_symbol (info, h))
  5049. return FALSE;
  5050. s = htab->root.sgot;
  5051. h->got.offset = s->size;
  5052. if (tls_type == GOT_UNKNOWN)
  5053. abort ();
  5054. if (tls_type == GOT_NORMAL)
  5055. /* Non-TLS symbols need one GOT slot. */
  5056. s->size += 4;
  5057. else
  5058. {
  5059. if (tls_type & GOT_TLS_GD)
  5060. /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
  5061. s->size += 8;
  5062. if (tls_type & GOT_TLS_IE)
  5063. /* R_NIOS2_TLS_IE16 needs one GOT slot. */
  5064. s->size += 4;
  5065. }
  5066. dyn = htab->root.dynamic_sections_created;
  5067. indx = 0;
  5068. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
  5069. && (!bfd_link_pic (info)
  5070. || !SYMBOL_REFERENCES_LOCAL (info, h)))
  5071. indx = h->dynindx;
  5072. if (tls_type != GOT_NORMAL
  5073. && (bfd_link_pic (info) || indx != 0)
  5074. && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  5075. || h->root.type != bfd_link_hash_undefweak))
  5076. {
  5077. if (tls_type & GOT_TLS_IE)
  5078. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  5079. if (tls_type & GOT_TLS_GD)
  5080. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  5081. if ((tls_type & GOT_TLS_GD) && indx != 0)
  5082. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  5083. }
  5084. else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  5085. || h->root.type != bfd_link_hash_undefweak)
  5086. && !use_plt
  5087. && (bfd_link_pic (info)
  5088. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
  5089. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  5090. }
  5091. else
  5092. h->got.offset = (bfd_vma) -1;
  5093. if (eh->dyn_relocs == NULL)
  5094. return TRUE;
  5095. /* In the shared -Bsymbolic case, discard space allocated for
  5096. dynamic pc-relative relocs against symbols which turn out to be
  5097. defined in regular objects. For the normal shared case, discard
  5098. space for pc-relative relocs that have become local due to symbol
  5099. visibility changes. */
  5100. if (bfd_link_pic (info))
  5101. {
  5102. if (h->def_regular
  5103. && (h->forced_local || SYMBOLIC_BIND (info, h)))
  5104. {
  5105. struct elf32_nios2_dyn_relocs **pp;
  5106. for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
  5107. {
  5108. p->count -= p->pc_count;
  5109. p->pc_count = 0;
  5110. if (p->count == 0)
  5111. *pp = p->next;
  5112. else
  5113. pp = &p->next;
  5114. }
  5115. }
  5116. /* Also discard relocs on undefined weak syms with non-default
  5117. visibility. */
  5118. if (eh->dyn_relocs != NULL
  5119. && h->root.type == bfd_link_hash_undefweak)
  5120. {
  5121. if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  5122. eh->dyn_relocs = NULL;
  5123. /* Make sure undefined weak symbols are output as a dynamic
  5124. symbol in PIEs. */
  5125. else if (h->dynindx == -1
  5126. && !h->forced_local
  5127. && !bfd_elf_link_record_dynamic_symbol (info, h))
  5128. return FALSE;
  5129. }
  5130. }
  5131. else
  5132. {
  5133. /* For the non-shared case, discard space for relocs against
  5134. symbols which turn out to need copy relocs or are not
  5135. dynamic. */
  5136. if (!h->non_got_ref
  5137. && ((h->def_dynamic && !h->def_regular)
  5138. || (htab->root.dynamic_sections_created
  5139. && (h->root.type == bfd_link_hash_undefweak
  5140. || h->root.type == bfd_link_hash_undefined))))
  5141. {
  5142. /* Make sure this symbol is output as a dynamic symbol.
  5143. Undefined weak syms won't yet be marked as dynamic. */
  5144. if (h->dynindx == -1
  5145. && !h->forced_local
  5146. && !bfd_elf_link_record_dynamic_symbol (info, h))
  5147. return FALSE;
  5148. /* If that succeeded, we know we'll be keeping all the
  5149. relocs. */
  5150. if (h->dynindx != -1)
  5151. goto keep;
  5152. }
  5153. eh->dyn_relocs = NULL;
  5154. keep: ;
  5155. }
  5156. /* Finally, allocate space. */
  5157. for (p = eh->dyn_relocs; p != NULL; p = p->next)
  5158. {
  5159. asection *sreloc = elf_section_data (p->sec)->sreloc;
  5160. sreloc->size += p->count * sizeof (Elf32_External_Rela);
  5161. }
  5162. return TRUE;
  5163. }
  5164. /* Implement elf_backend_size_dynamic_sections:
  5165. Set the sizes of the dynamic sections. */
  5166. static bfd_boolean
  5167. nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  5168. struct bfd_link_info *info)
  5169. {
  5170. bfd *dynobj;
  5171. asection *s;
  5172. bfd_boolean plt;
  5173. bfd_boolean got;
  5174. bfd_boolean relocs;
  5175. bfd *ibfd;
  5176. struct elf32_nios2_link_hash_table *htab;
  5177. htab = elf32_nios2_hash_table (info);
  5178. dynobj = elf_hash_table (info)->dynobj;
  5179. BFD_ASSERT (dynobj != NULL);
  5180. htab->res_n_size = 0;
  5181. if (elf_hash_table (info)->dynamic_sections_created)
  5182. {
  5183. /* Set the contents of the .interp section to the interpreter. */
  5184. if (bfd_link_executable (info) && !info->nointerp)
  5185. {
  5186. s = bfd_get_linker_section (dynobj, ".interp");
  5187. BFD_ASSERT (s != NULL);
  5188. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  5189. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  5190. }
  5191. }
  5192. else
  5193. {
  5194. /* We may have created entries in the .rela.got section.
  5195. However, if we are not creating the dynamic sections, we will
  5196. not actually use these entries. Reset the size of .rela.got,
  5197. which will cause it to get stripped from the output file
  5198. below. */
  5199. s = htab->root.srelgot;
  5200. if (s != NULL)
  5201. s->size = 0;
  5202. }
  5203. /* Set up .got offsets for local syms, and space for local dynamic
  5204. relocs. */
  5205. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  5206. {
  5207. bfd_signed_vma *local_got;
  5208. bfd_signed_vma *end_local_got;
  5209. char *local_tls_type;
  5210. bfd_size_type locsymcount;
  5211. Elf_Internal_Shdr *symtab_hdr;
  5212. asection *srel;
  5213. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  5214. continue;
  5215. for (s = ibfd->sections; s != NULL; s = s->next)
  5216. {
  5217. struct elf32_nios2_dyn_relocs *p;
  5218. for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
  5219. {
  5220. if (!bfd_is_abs_section (p->sec)
  5221. && bfd_is_abs_section (p->sec->output_section))
  5222. {
  5223. /* Input section has been discarded, either because
  5224. it is a copy of a linkonce section or due to
  5225. linker script /DISCARD/, so we'll be discarding
  5226. the relocs too. */
  5227. }
  5228. else if (p->count != 0)
  5229. {
  5230. srel = elf_section_data (p->sec)->sreloc;
  5231. srel->size += p->count * sizeof (Elf32_External_Rela);
  5232. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  5233. info->flags |= DF_TEXTREL;
  5234. }
  5235. }
  5236. }
  5237. local_got = elf_local_got_refcounts (ibfd);
  5238. if (!local_got)
  5239. continue;
  5240. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  5241. locsymcount = symtab_hdr->sh_info;
  5242. end_local_got = local_got + locsymcount;
  5243. local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
  5244. s = htab->root.sgot;
  5245. srel = htab->root.srelgot;
  5246. for (; local_got < end_local_got; ++local_got, ++local_tls_type)
  5247. {
  5248. if (*local_got > 0)
  5249. {
  5250. *local_got = s->size;
  5251. if (*local_tls_type & GOT_TLS_GD)
  5252. /* TLS_GD relocs need an 8-byte structure in the GOT. */
  5253. s->size += 8;
  5254. if (*local_tls_type & GOT_TLS_IE)
  5255. s->size += 4;
  5256. if (*local_tls_type == GOT_NORMAL)
  5257. s->size += 4;
  5258. if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
  5259. srel->size += sizeof (Elf32_External_Rela);
  5260. }
  5261. else
  5262. *local_got = (bfd_vma) -1;
  5263. }
  5264. }
  5265. if (htab->tls_ldm_got.refcount > 0)
  5266. {
  5267. /* Allocate two GOT entries and one dynamic relocation (if necessary)
  5268. for R_NIOS2_TLS_LDM16 relocations. */
  5269. htab->tls_ldm_got.offset = htab->root.sgot->size;
  5270. htab->root.sgot->size += 8;
  5271. if (bfd_link_pic (info))
  5272. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  5273. }
  5274. else
  5275. htab->tls_ldm_got.offset = -1;
  5276. /* Allocate global sym .plt and .got entries, and space for global
  5277. sym dynamic relocs. */
  5278. elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
  5279. if (elf_hash_table (info)->dynamic_sections_created)
  5280. {
  5281. /* If the .got section is more than 0x8000 bytes, we add
  5282. 0x8000 to the value of _gp_got, so that 16-bit relocations
  5283. have a greater chance of working. */
  5284. if (htab->root.sgot->size >= 0x8000
  5285. && elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value == 0)
  5286. elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value = 0x8000;
  5287. }
  5288. /* The check_relocs and adjust_dynamic_symbol entry points have
  5289. determined the sizes of the various dynamic sections. Allocate
  5290. memory for them. */
  5291. plt = FALSE;
  5292. got = FALSE;
  5293. relocs = FALSE;
  5294. for (s = dynobj->sections; s != NULL; s = s->next)
  5295. {
  5296. const char *name;
  5297. if ((s->flags & SEC_LINKER_CREATED) == 0)
  5298. continue;
  5299. /* It's OK to base decisions on the section name, because none
  5300. of the dynobj section names depend upon the input files. */
  5301. name = bfd_get_section_name (dynobj, s);
  5302. if (strcmp (name, ".plt") == 0)
  5303. {
  5304. /* Remember whether there is a PLT. */
  5305. plt = s->size != 0;
  5306. /* Correct for the number of res_N branches. */
  5307. if (plt && !bfd_link_pic (info))
  5308. {
  5309. htab->res_n_size = (s->size-28) / 3;
  5310. s->size += htab->res_n_size;
  5311. }
  5312. }
  5313. else if (CONST_STRNEQ (name, ".rela"))
  5314. {
  5315. if (s->size != 0)
  5316. {
  5317. relocs = TRUE;
  5318. /* We use the reloc_count field as a counter if we need
  5319. to copy relocs into the output file. */
  5320. s->reloc_count = 0;
  5321. }
  5322. }
  5323. else if (CONST_STRNEQ (name, ".got"))
  5324. got = s->size != 0;
  5325. else if (strcmp (name, ".dynbss") != 0)
  5326. /* It's not one of our sections, so don't allocate space. */
  5327. continue;
  5328. if (s->size == 0)
  5329. {
  5330. /* If we don't need this section, strip it from the
  5331. output file. This is mostly to handle .rela.bss and
  5332. .rela.plt. We must create both sections in
  5333. create_dynamic_sections, because they must be created
  5334. before the linker maps input sections to output
  5335. sections. The linker does that before
  5336. adjust_dynamic_symbol is called, and it is that
  5337. function which decides whether anything needs to go
  5338. into these sections. */
  5339. s->flags |= SEC_EXCLUDE;
  5340. continue;
  5341. }
  5342. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  5343. continue;
  5344. /* Allocate memory for the section contents. */
  5345. /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
  5346. Unused entries should be reclaimed before the section's contents
  5347. are written out, but at the moment this does not happen. Thus in
  5348. order to prevent writing out garbage, we initialize the section's
  5349. contents to zero. */
  5350. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  5351. if (s->contents == NULL)
  5352. return FALSE;
  5353. }
  5354. /* Adjust dynamic symbols that point to the plt to account for the
  5355. now-known number of resN slots. */
  5356. if (htab->res_n_size)
  5357. elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
  5358. if (elf_hash_table (info)->dynamic_sections_created)
  5359. {
  5360. /* Add some entries to the .dynamic section. We fill in the
  5361. values later, in elf_nios2_finish_dynamic_sections, but we
  5362. must add the entries now so that we get the correct size for
  5363. the .dynamic section. The DT_DEBUG entry is filled in by the
  5364. dynamic linker and used by the debugger. */
  5365. #define add_dynamic_entry(TAG, VAL) \
  5366. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  5367. if (!bfd_link_pic (info) && !add_dynamic_entry (DT_DEBUG, 0))
  5368. return FALSE;
  5369. if (got && !add_dynamic_entry (DT_PLTGOT, 0))
  5370. return FALSE;
  5371. if (plt
  5372. && (!add_dynamic_entry (DT_PLTRELSZ, 0)
  5373. || !add_dynamic_entry (DT_PLTREL, DT_RELA)
  5374. || !add_dynamic_entry (DT_JMPREL, 0)))
  5375. return FALSE;
  5376. if (relocs
  5377. && (!add_dynamic_entry (DT_RELA, 0)
  5378. || !add_dynamic_entry (DT_RELASZ, 0)
  5379. || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
  5380. return FALSE;
  5381. if (!bfd_link_pic (info) && !add_dynamic_entry (DT_NIOS2_GP, 0))
  5382. return FALSE;
  5383. if ((info->flags & DF_TEXTREL) != 0
  5384. && !add_dynamic_entry (DT_TEXTREL, 0))
  5385. return FALSE;
  5386. }
  5387. #undef add_dynamic_entry
  5388. return TRUE;
  5389. }
  5390. /* Free the derived linker hash table. */
  5391. static void
  5392. nios2_elf32_link_hash_table_free (bfd *obfd)
  5393. {
  5394. struct elf32_nios2_link_hash_table *htab
  5395. = (struct elf32_nios2_link_hash_table *) obfd->link.hash;
  5396. bfd_hash_table_free (&htab->bstab);
  5397. _bfd_elf_link_hash_table_free (obfd);
  5398. }
  5399. /* Implement bfd_elf32_bfd_link_hash_table_create. */
  5400. static struct bfd_link_hash_table *
  5401. nios2_elf32_link_hash_table_create (bfd *abfd)
  5402. {
  5403. struct elf32_nios2_link_hash_table *ret;
  5404. bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
  5405. ret = bfd_zmalloc (amt);
  5406. if (ret == NULL)
  5407. return NULL;
  5408. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  5409. link_hash_newfunc,
  5410. sizeof (struct
  5411. elf32_nios2_link_hash_entry),
  5412. NIOS2_ELF_DATA))
  5413. {
  5414. free (ret);
  5415. return NULL;
  5416. }
  5417. /* Init the stub hash table too. */
  5418. if (!bfd_hash_table_init (&ret->bstab, stub_hash_newfunc,
  5419. sizeof (struct elf32_nios2_stub_hash_entry)))
  5420. {
  5421. _bfd_elf_link_hash_table_free (abfd);
  5422. return NULL;
  5423. }
  5424. ret->root.root.hash_table_free = nios2_elf32_link_hash_table_free;
  5425. return &ret->root.root;
  5426. }
  5427. /* Implement elf_backend_reloc_type_class. */
  5428. static enum elf_reloc_type_class
  5429. nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  5430. const asection *rel_sec ATTRIBUTE_UNUSED,
  5431. const Elf_Internal_Rela *rela)
  5432. {
  5433. switch ((int) ELF32_R_TYPE (rela->r_info))
  5434. {
  5435. case R_NIOS2_RELATIVE:
  5436. return reloc_class_relative;
  5437. case R_NIOS2_JUMP_SLOT:
  5438. return reloc_class_plt;
  5439. case R_NIOS2_COPY:
  5440. return reloc_class_copy;
  5441. default:
  5442. return reloc_class_normal;
  5443. }
  5444. }
  5445. /* Return 1 if target is one of ours. */
  5446. static bfd_boolean
  5447. is_nios2_elf_target (const struct bfd_target *targ)
  5448. {
  5449. return (targ == &nios2_elf32_le_vec
  5450. || targ == &nios2_elf32_be_vec);
  5451. }
  5452. /* Implement elf_backend_add_symbol_hook.
  5453. This hook is called by the linker when adding symbols from an object
  5454. file. We use it to put .comm items in .sbss, and not .bss. */
  5455. static bfd_boolean
  5456. nios2_elf_add_symbol_hook (bfd *abfd,
  5457. struct bfd_link_info *info,
  5458. Elf_Internal_Sym *sym,
  5459. const char **namep ATTRIBUTE_UNUSED,
  5460. flagword *flagsp ATTRIBUTE_UNUSED,
  5461. asection **secp,
  5462. bfd_vma *valp)
  5463. {
  5464. bfd *dynobj;
  5465. if (sym->st_shndx == SHN_COMMON
  5466. && !bfd_link_relocatable (info)
  5467. && sym->st_size <= elf_gp_size (abfd)
  5468. && is_nios2_elf_target (info->output_bfd->xvec))
  5469. {
  5470. /* Common symbols less than or equal to -G nn bytes are automatically
  5471. put into .sbss. */
  5472. struct elf32_nios2_link_hash_table *htab;
  5473. htab = elf32_nios2_hash_table (info);
  5474. if (htab->sbss == NULL)
  5475. {
  5476. flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
  5477. dynobj = elf_hash_table (info)->dynobj;
  5478. if (!dynobj)
  5479. dynobj = abfd;
  5480. htab->sbss = bfd_make_section_anyway_with_flags (dynobj, ".sbss",
  5481. flags);
  5482. if (htab->sbss == NULL)
  5483. return FALSE;
  5484. }
  5485. *secp = htab->sbss;
  5486. *valp = sym->st_size;
  5487. }
  5488. return TRUE;
  5489. }
  5490. /* Implement elf_backend_can_make_relative_eh_frame:
  5491. Decide whether to attempt to turn absptr or lsda encodings in
  5492. shared libraries into pcrel within the given input section. */
  5493. static bfd_boolean
  5494. nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
  5495. struct bfd_link_info *info
  5496. ATTRIBUTE_UNUSED,
  5497. asection *eh_frame_section
  5498. ATTRIBUTE_UNUSED)
  5499. {
  5500. /* We can't use PC-relative encodings in the .eh_frame section. */
  5501. return FALSE;
  5502. }
  5503. /* Implement elf_backend_special_sections. */
  5504. const struct bfd_elf_special_section elf32_nios2_special_sections[] =
  5505. {
  5506. { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
  5507. SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
  5508. { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
  5509. SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
  5510. { NULL, 0, 0, 0, 0 }
  5511. };
  5512. #define ELF_ARCH bfd_arch_nios2
  5513. #define ELF_TARGET_ID NIOS2_ELF_DATA
  5514. #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
  5515. /* The Nios II MMU uses a 4K page size. */
  5516. #define ELF_MAXPAGESIZE 0x1000
  5517. #define bfd_elf32_bfd_link_hash_table_create \
  5518. nios2_elf32_link_hash_table_create
  5519. #define bfd_elf32_bfd_merge_private_bfd_data \
  5520. nios2_elf32_merge_private_bfd_data
  5521. /* Relocation table lookup macros. */
  5522. #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
  5523. #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
  5524. /* JUMP_TABLE_LINK macros. */
  5525. /* elf_info_to_howto (using RELA relocations). */
  5526. #define elf_info_to_howto nios2_elf32_info_to_howto
  5527. /* elf backend functions. */
  5528. #define elf_backend_can_gc_sections 1
  5529. #define elf_backend_can_refcount 1
  5530. #define elf_backend_plt_readonly 1
  5531. #define elf_backend_want_got_plt 1
  5532. #define elf_backend_rela_normal 1
  5533. #define elf_backend_relocate_section nios2_elf32_relocate_section
  5534. #define elf_backend_section_flags nios2_elf32_section_flags
  5535. #define elf_backend_fake_sections nios2_elf32_fake_sections
  5536. #define elf_backend_check_relocs nios2_elf32_check_relocs
  5537. #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
  5538. #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
  5539. #define elf_backend_create_dynamic_sections \
  5540. nios2_elf32_create_dynamic_sections
  5541. #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
  5542. #define elf_backend_finish_dynamic_sections \
  5543. nios2_elf32_finish_dynamic_sections
  5544. #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
  5545. #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
  5546. #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
  5547. #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
  5548. #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
  5549. #define elf_backend_object_p nios2_elf32_object_p
  5550. #define elf_backend_grok_prstatus nios2_grok_prstatus
  5551. #define elf_backend_grok_psinfo nios2_grok_psinfo
  5552. #undef elf_backend_can_make_relative_eh_frame
  5553. #define elf_backend_can_make_relative_eh_frame \
  5554. nios2_elf32_can_make_relative_eh_frame
  5555. #define elf_backend_special_sections elf32_nios2_special_sections
  5556. #define TARGET_LITTLE_SYM nios2_elf32_le_vec
  5557. #define TARGET_LITTLE_NAME "elf32-littlenios2"
  5558. #define TARGET_BIG_SYM nios2_elf32_be_vec
  5559. #define TARGET_BIG_NAME "elf32-bignios2"
  5560. #define elf_backend_got_header_size 12
  5561. #define elf_backend_default_execstack 0
  5562. #include "elf32-target.h"