elf32-sh.c 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720
  1. /* Renesas / SuperH SH specific support for 32-bit ELF
  2. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  3. Contributed by Ian Lance Taylor, Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "elf-vxworks.h"
  23. #include "elf/sh.h"
  24. #include "dwarf2.h"
  25. #include "libiberty.h"
  26. #include "../opcodes/sh-opc.h"
  27. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  28. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  29. static bfd_reloc_status_type sh_elf_reloc
  30. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  31. static bfd_reloc_status_type sh_elf_ignore_reloc
  32. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  33. static bool sh_elf_relax_delete_bytes
  34. (bfd *, asection *, bfd_vma, int);
  35. static bool sh_elf_align_loads
  36. (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bool *);
  37. static bool sh_elf_swap_insns
  38. (bfd *, asection *, void *, bfd_byte *, bfd_vma);
  39. static int sh_elf_optimized_tls_reloc
  40. (struct bfd_link_info *, int, int);
  41. static bfd_vma dtpoff_base
  42. (struct bfd_link_info *);
  43. static bfd_vma tpoff
  44. (struct bfd_link_info *, bfd_vma);
  45. /* The name of the dynamic interpreter. This is put in the .interp
  46. section. */
  47. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  48. /* FDPIC binaries have a default 128K stack. */
  49. #define DEFAULT_STACK_SIZE 0x20000
  50. #define MINUS_ONE ((bfd_vma) 0 - 1)
  51. /* Decide whether a reference to a symbol can be resolved locally or
  52. not. If the symbol is protected, we want the local address, but
  53. its function descriptor must be assigned by the dynamic linker. */
  54. #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
  55. (SYMBOL_REFERENCES_LOCAL (INFO, H) \
  56. || ! elf_hash_table (INFO)->dynamic_sections_created)
  57. #define SH_PARTIAL32 true
  58. #define SH_SRC_MASK32 0xffffffff
  59. #define SH_ELF_RELOC sh_elf_reloc
  60. static reloc_howto_type sh_elf_howto_table[] =
  61. {
  62. #include "elf32-sh-relocs.h"
  63. };
  64. #define SH_PARTIAL32 false
  65. #define SH_SRC_MASK32 0
  66. #define SH_ELF_RELOC bfd_elf_generic_reloc
  67. static reloc_howto_type sh_vxworks_howto_table[] =
  68. {
  69. #include "elf32-sh-relocs.h"
  70. };
  71. /* Return true if OUTPUT_BFD is a VxWorks object. */
  72. static bool
  73. vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
  74. {
  75. #if !defined SH_TARGET_ALREADY_DEFINED
  76. extern const bfd_target sh_elf32_vxworks_le_vec;
  77. extern const bfd_target sh_elf32_vxworks_vec;
  78. return (abfd->xvec == &sh_elf32_vxworks_le_vec
  79. || abfd->xvec == &sh_elf32_vxworks_vec);
  80. #else
  81. return false;
  82. #endif
  83. }
  84. /* Return true if OUTPUT_BFD is an FDPIC object. */
  85. static bool
  86. fdpic_object_p (bfd *abfd ATTRIBUTE_UNUSED)
  87. {
  88. #if !defined SH_TARGET_ALREADY_DEFINED
  89. extern const bfd_target sh_elf32_fdpic_le_vec;
  90. extern const bfd_target sh_elf32_fdpic_be_vec;
  91. return (abfd->xvec == &sh_elf32_fdpic_le_vec
  92. || abfd->xvec == &sh_elf32_fdpic_be_vec);
  93. #else
  94. return false;
  95. #endif
  96. }
  97. /* Return the howto table for ABFD. */
  98. static reloc_howto_type *
  99. get_howto_table (bfd *abfd)
  100. {
  101. if (vxworks_object_p (abfd))
  102. return sh_vxworks_howto_table;
  103. return sh_elf_howto_table;
  104. }
  105. static bfd_reloc_status_type
  106. sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
  107. asection *input_section, bfd_byte *contents,
  108. bfd_vma addr, asection *symbol_section,
  109. bfd_vma start, bfd_vma end)
  110. {
  111. static bfd_vma last_addr;
  112. static asection *last_symbol_section;
  113. bfd_byte *start_ptr, *ptr, *last_ptr;
  114. int diff, cum_diff;
  115. bfd_signed_vma x;
  116. int insn;
  117. /* Sanity check the address. */
  118. if (addr > bfd_get_section_limit (input_bfd, input_section))
  119. return bfd_reloc_outofrange;
  120. /* We require the start and end relocations to be processed consecutively -
  121. although we allow then to be processed forwards or backwards. */
  122. if (! last_addr)
  123. {
  124. last_addr = addr;
  125. last_symbol_section = symbol_section;
  126. return bfd_reloc_ok;
  127. }
  128. if (last_addr != addr)
  129. abort ();
  130. last_addr = 0;
  131. if (! symbol_section || last_symbol_section != symbol_section || end < start)
  132. return bfd_reloc_outofrange;
  133. /* Get the symbol_section contents. */
  134. if (symbol_section != input_section)
  135. {
  136. if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
  137. contents = elf_section_data (symbol_section)->this_hdr.contents;
  138. else
  139. {
  140. if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
  141. &contents))
  142. {
  143. free (contents);
  144. return bfd_reloc_outofrange;
  145. }
  146. }
  147. }
  148. #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
  149. start_ptr = contents + start;
  150. for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
  151. {
  152. for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
  153. ptr -= 2;
  154. ptr += 2;
  155. diff = (last_ptr - ptr) >> 1;
  156. cum_diff += diff & 1;
  157. cum_diff += diff;
  158. }
  159. /* Calculate the start / end values to load into rs / re minus four -
  160. so that will cancel out the four we would otherwise have to add to
  161. addr to get the value to subtract in order to get relative addressing. */
  162. if (cum_diff >= 0)
  163. {
  164. start -= 4;
  165. end = (ptr + cum_diff * 2) - contents;
  166. }
  167. else
  168. {
  169. bfd_vma start0 = start - 4;
  170. while (start0 && IS_PPI (contents + start0))
  171. start0 -= 2;
  172. start0 = start - 2 - ((start - start0) & 2);
  173. start = start0 - cum_diff - 2;
  174. end = start0;
  175. }
  176. if (elf_section_data (symbol_section)->this_hdr.contents != contents)
  177. free (contents);
  178. insn = bfd_get_16 (input_bfd, contents + addr);
  179. x = (insn & 0x200 ? end : start) - addr;
  180. if (input_section != symbol_section)
  181. x += ((symbol_section->output_section->vma + symbol_section->output_offset)
  182. - (input_section->output_section->vma
  183. + input_section->output_offset));
  184. x >>= 1;
  185. if (x < -128 || x > 127)
  186. return bfd_reloc_overflow;
  187. x = (insn & ~0xff) | (x & 0xff);
  188. bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
  189. return bfd_reloc_ok;
  190. }
  191. /* This function is used for normal relocs. This used to be like the COFF
  192. function, and is almost certainly incorrect for other ELF targets. */
  193. static bfd_reloc_status_type
  194. sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
  195. void *data, asection *input_section, bfd *output_bfd,
  196. char **error_message ATTRIBUTE_UNUSED)
  197. {
  198. bfd_vma insn;
  199. bfd_vma sym_value;
  200. enum elf_sh_reloc_type r_type;
  201. bfd_vma addr = reloc_entry->address;
  202. bfd_size_type octets = addr * OCTETS_PER_BYTE (abfd, input_section);
  203. bfd_byte *hit_data = (bfd_byte *) data + octets;
  204. r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
  205. if (output_bfd != NULL)
  206. {
  207. /* Partial linking--do nothing. */
  208. reloc_entry->address += input_section->output_offset;
  209. return bfd_reloc_ok;
  210. }
  211. /* Almost all relocs have to do with relaxing. If any work must be
  212. done for them, it has been done in sh_relax_section. */
  213. if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
  214. return bfd_reloc_ok;
  215. if (symbol_in != NULL
  216. && bfd_is_und_section (symbol_in->section))
  217. return bfd_reloc_undefined;
  218. /* PR 17512: file: 9891ca98. */
  219. if (octets + bfd_get_reloc_size (reloc_entry->howto)
  220. > bfd_get_section_limit_octets (abfd, input_section))
  221. return bfd_reloc_outofrange;
  222. if (bfd_is_com_section (symbol_in->section))
  223. sym_value = 0;
  224. else
  225. sym_value = (symbol_in->value +
  226. symbol_in->section->output_section->vma +
  227. symbol_in->section->output_offset);
  228. switch (r_type)
  229. {
  230. case R_SH_DIR32:
  231. insn = bfd_get_32 (abfd, hit_data);
  232. insn += sym_value + reloc_entry->addend;
  233. bfd_put_32 (abfd, insn, hit_data);
  234. break;
  235. case R_SH_IND12W:
  236. insn = bfd_get_16 (abfd, hit_data);
  237. sym_value += reloc_entry->addend;
  238. sym_value -= (input_section->output_section->vma
  239. + input_section->output_offset
  240. + addr
  241. + 4);
  242. sym_value += (((insn & 0xfff) ^ 0x800) - 0x800) << 1;
  243. insn = (insn & 0xf000) | ((sym_value >> 1) & 0xfff);
  244. bfd_put_16 (abfd, insn, hit_data);
  245. if (sym_value + 0x1000 >= 0x2000 || (sym_value & 1) != 0)
  246. return bfd_reloc_overflow;
  247. break;
  248. default:
  249. abort ();
  250. break;
  251. }
  252. return bfd_reloc_ok;
  253. }
  254. /* This function is used for relocs which are only used for relaxing,
  255. which the linker should otherwise ignore. */
  256. static bfd_reloc_status_type
  257. sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
  258. asymbol *symbol ATTRIBUTE_UNUSED,
  259. void *data ATTRIBUTE_UNUSED, asection *input_section,
  260. bfd *output_bfd,
  261. char **error_message ATTRIBUTE_UNUSED)
  262. {
  263. if (output_bfd != NULL)
  264. reloc_entry->address += input_section->output_offset;
  265. return bfd_reloc_ok;
  266. }
  267. /* This structure is used to map BFD reloc codes to SH ELF relocs. */
  268. struct elf_reloc_map
  269. {
  270. bfd_reloc_code_real_type bfd_reloc_val;
  271. unsigned char elf_reloc_val;
  272. };
  273. /* An array mapping BFD reloc codes to SH ELF relocs. */
  274. static const struct elf_reloc_map sh_reloc_map[] =
  275. {
  276. { BFD_RELOC_NONE, R_SH_NONE },
  277. { BFD_RELOC_32, R_SH_DIR32 },
  278. { BFD_RELOC_16, R_SH_DIR16 },
  279. { BFD_RELOC_8, R_SH_DIR8 },
  280. { BFD_RELOC_CTOR, R_SH_DIR32 },
  281. { BFD_RELOC_32_PCREL, R_SH_REL32 },
  282. { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
  283. { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
  284. { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
  285. { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
  286. { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
  287. { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
  288. { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
  289. { BFD_RELOC_SH_USES, R_SH_USES },
  290. { BFD_RELOC_SH_COUNT, R_SH_COUNT },
  291. { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
  292. { BFD_RELOC_SH_CODE, R_SH_CODE },
  293. { BFD_RELOC_SH_DATA, R_SH_DATA },
  294. { BFD_RELOC_SH_LABEL, R_SH_LABEL },
  295. { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
  296. { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
  297. { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
  298. { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
  299. { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
  300. { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
  301. { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
  302. { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
  303. { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
  304. { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
  305. { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
  306. { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
  307. { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
  308. { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
  309. { BFD_RELOC_SH_COPY, R_SH_COPY },
  310. { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
  311. { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
  312. { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
  313. { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
  314. { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
  315. { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
  316. { BFD_RELOC_SH_GOT20, R_SH_GOT20 },
  317. { BFD_RELOC_SH_GOTOFF20, R_SH_GOTOFF20 },
  318. { BFD_RELOC_SH_GOTFUNCDESC, R_SH_GOTFUNCDESC },
  319. { BFD_RELOC_SH_GOTFUNCDESC20, R_SH_GOTFUNCDESC20 },
  320. { BFD_RELOC_SH_GOTOFFFUNCDESC, R_SH_GOTOFFFUNCDESC },
  321. { BFD_RELOC_SH_GOTOFFFUNCDESC20, R_SH_GOTOFFFUNCDESC20 },
  322. { BFD_RELOC_SH_FUNCDESC, R_SH_FUNCDESC },
  323. };
  324. /* Given a BFD reloc code, return the howto structure for the
  325. corresponding SH ELF reloc. */
  326. static reloc_howto_type *
  327. sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
  328. {
  329. unsigned int i;
  330. for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
  331. {
  332. if (sh_reloc_map[i].bfd_reloc_val == code)
  333. return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
  334. }
  335. return NULL;
  336. }
  337. static reloc_howto_type *
  338. sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
  339. {
  340. unsigned int i;
  341. if (vxworks_object_p (abfd))
  342. {
  343. for (i = 0;
  344. i < (sizeof (sh_vxworks_howto_table)
  345. / sizeof (sh_vxworks_howto_table[0]));
  346. i++)
  347. if (sh_vxworks_howto_table[i].name != NULL
  348. && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
  349. return &sh_vxworks_howto_table[i];
  350. }
  351. else
  352. {
  353. for (i = 0;
  354. i < (sizeof (sh_elf_howto_table)
  355. / sizeof (sh_elf_howto_table[0]));
  356. i++)
  357. if (sh_elf_howto_table[i].name != NULL
  358. && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
  359. return &sh_elf_howto_table[i];
  360. }
  361. return NULL;
  362. }
  363. /* Given an ELF reloc, fill in the howto field of a relent. */
  364. static bool
  365. sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
  366. {
  367. unsigned int r;
  368. r = ELF32_R_TYPE (dst->r_info);
  369. if (r >= R_SH_FIRST_INVALID_RELOC_6
  370. || (r >= R_SH_FIRST_INVALID_RELOC && r <= R_SH_LAST_INVALID_RELOC)
  371. || (r >= R_SH_FIRST_INVALID_RELOC_2 && r <= R_SH_LAST_INVALID_RELOC_2)
  372. || (r >= R_SH_FIRST_INVALID_RELOC_3 && r <= R_SH_LAST_INVALID_RELOC_3)
  373. || (r >= R_SH_FIRST_INVALID_RELOC_4 && r <= R_SH_LAST_INVALID_RELOC_4)
  374. || (r >= R_SH_FIRST_INVALID_RELOC_5 && r <= R_SH_LAST_INVALID_RELOC_5))
  375. {
  376. /* xgettext:c-format */
  377. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  378. abfd, r);
  379. bfd_set_error (bfd_error_bad_value);
  380. return false;
  381. }
  382. cache_ptr->howto = get_howto_table (abfd) + r;
  383. return true;
  384. }
  385. /* This function handles relaxing for SH ELF. See the corresponding
  386. function in coff-sh.c for a description of what this does. FIXME:
  387. There is a lot of duplication here between this code and the COFF
  388. specific code. The format of relocs and symbols is wound deeply
  389. into this code, but it would still be better if the duplication
  390. could be eliminated somehow. Note in particular that although both
  391. functions use symbols like R_SH_CODE, those symbols have different
  392. values; in coff-sh.c they come from include/coff/sh.h, whereas here
  393. they come from enum elf_sh_reloc_type in include/elf/sh.h. */
  394. static bool
  395. sh_elf_relax_section (bfd *abfd, asection *sec,
  396. struct bfd_link_info *link_info, bool *again)
  397. {
  398. Elf_Internal_Shdr *symtab_hdr;
  399. Elf_Internal_Rela *internal_relocs;
  400. bool have_code;
  401. Elf_Internal_Rela *irel, *irelend;
  402. bfd_byte *contents = NULL;
  403. Elf_Internal_Sym *isymbuf = NULL;
  404. *again = false;
  405. if (bfd_link_relocatable (link_info)
  406. || (sec->flags & SEC_RELOC) == 0
  407. || sec->reloc_count == 0)
  408. return true;
  409. symtab_hdr = &elf_symtab_hdr (abfd);
  410. internal_relocs = (_bfd_elf_link_read_relocs
  411. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  412. link_info->keep_memory));
  413. if (internal_relocs == NULL)
  414. goto error_return;
  415. have_code = false;
  416. irelend = internal_relocs + sec->reloc_count;
  417. for (irel = internal_relocs; irel < irelend; irel++)
  418. {
  419. bfd_vma laddr, paddr, symval;
  420. unsigned short insn;
  421. Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
  422. bfd_signed_vma foff;
  423. if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
  424. have_code = true;
  425. if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
  426. continue;
  427. /* Get the section contents. */
  428. if (contents == NULL)
  429. {
  430. if (elf_section_data (sec)->this_hdr.contents != NULL)
  431. contents = elf_section_data (sec)->this_hdr.contents;
  432. else
  433. {
  434. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  435. goto error_return;
  436. }
  437. }
  438. /* The r_addend field of the R_SH_USES reloc will point us to
  439. the register load. The 4 is because the r_addend field is
  440. computed as though it were a jump offset, which are based
  441. from 4 bytes after the jump instruction. */
  442. laddr = irel->r_offset + 4 + irel->r_addend;
  443. if (laddr >= sec->size)
  444. {
  445. /* xgettext:c-format */
  446. _bfd_error_handler
  447. (_("%pB: %#" PRIx64 ": warning: bad R_SH_USES offset"),
  448. abfd, (uint64_t) irel->r_offset);
  449. continue;
  450. }
  451. insn = bfd_get_16 (abfd, contents + laddr);
  452. /* If the instruction is not mov.l NN,rN, we don't know what to
  453. do. */
  454. if ((insn & 0xf000) != 0xd000)
  455. {
  456. _bfd_error_handler
  457. /* xgettext:c-format */
  458. (_("%pB: %#" PRIx64 ": warning: "
  459. "R_SH_USES points to unrecognized insn 0x%x"),
  460. abfd, (uint64_t) irel->r_offset, insn);
  461. continue;
  462. }
  463. /* Get the address from which the register is being loaded. The
  464. displacement in the mov.l instruction is quadrupled. It is a
  465. displacement from four bytes after the movl instruction, but,
  466. before adding in the PC address, two least significant bits
  467. of the PC are cleared. We assume that the section is aligned
  468. on a four byte boundary. */
  469. paddr = insn & 0xff;
  470. paddr *= 4;
  471. paddr += (laddr + 4) &~ (bfd_vma) 3;
  472. if (paddr >= sec->size)
  473. {
  474. _bfd_error_handler
  475. /* xgettext:c-format */
  476. (_("%pB: %#" PRIx64 ": warning: bad R_SH_USES load offset"),
  477. abfd, (uint64_t) irel->r_offset);
  478. continue;
  479. }
  480. /* Get the reloc for the address from which the register is
  481. being loaded. This reloc will tell us which function is
  482. actually being called. */
  483. for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
  484. if (irelfn->r_offset == paddr
  485. && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
  486. break;
  487. if (irelfn >= irelend)
  488. {
  489. _bfd_error_handler
  490. /* xgettext:c-format */
  491. (_("%pB: %#" PRIx64 ": warning: could not find expected reloc"),
  492. abfd, (uint64_t) paddr);
  493. continue;
  494. }
  495. /* Read this BFD's symbols if we haven't done so already. */
  496. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  497. {
  498. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  499. if (isymbuf == NULL)
  500. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  501. symtab_hdr->sh_info, 0,
  502. NULL, NULL, NULL);
  503. if (isymbuf == NULL)
  504. goto error_return;
  505. }
  506. /* Get the value of the symbol referred to by the reloc. */
  507. if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
  508. {
  509. /* A local symbol. */
  510. Elf_Internal_Sym *isym;
  511. isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
  512. if (isym->st_shndx
  513. != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
  514. {
  515. _bfd_error_handler
  516. /* xgettext:c-format */
  517. (_("%pB: %#" PRIx64 ": warning: symbol in unexpected section"),
  518. abfd, (uint64_t) paddr);
  519. continue;
  520. }
  521. symval = (isym->st_value
  522. + sec->output_section->vma
  523. + sec->output_offset);
  524. }
  525. else
  526. {
  527. unsigned long indx;
  528. struct elf_link_hash_entry *h;
  529. indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
  530. h = elf_sym_hashes (abfd)[indx];
  531. BFD_ASSERT (h != NULL);
  532. if (h->root.type != bfd_link_hash_defined
  533. && h->root.type != bfd_link_hash_defweak)
  534. {
  535. /* This appears to be a reference to an undefined
  536. symbol. Just ignore it--it will be caught by the
  537. regular reloc processing. */
  538. continue;
  539. }
  540. symval = (h->root.u.def.value
  541. + h->root.u.def.section->output_section->vma
  542. + h->root.u.def.section->output_offset);
  543. }
  544. if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
  545. symval += bfd_get_32 (abfd, contents + paddr);
  546. else
  547. symval += irelfn->r_addend;
  548. /* See if this function call can be shortened. */
  549. foff = (symval
  550. - (irel->r_offset
  551. + sec->output_section->vma
  552. + sec->output_offset
  553. + 4));
  554. /* A branch to an address beyond ours might be increased by an
  555. .align that doesn't move when bytes behind us are deleted.
  556. So, we add some slop in this calculation to allow for
  557. that. */
  558. if (foff < -0x1000 || foff >= 0x1000 - 8)
  559. {
  560. /* After all that work, we can't shorten this function call. */
  561. continue;
  562. }
  563. /* Shorten the function call. */
  564. /* For simplicity of coding, we are going to modify the section
  565. contents, the section relocs, and the BFD symbol table. We
  566. must tell the rest of the code not to free up this
  567. information. It would be possible to instead create a table
  568. of changes which have to be made, as is done in coff-mips.c;
  569. that would be more work, but would require less memory when
  570. the linker is run. */
  571. elf_section_data (sec)->relocs = internal_relocs;
  572. elf_section_data (sec)->this_hdr.contents = contents;
  573. symtab_hdr->contents = (unsigned char *) isymbuf;
  574. /* Replace the jmp/jsr with a bra/bsr. */
  575. /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
  576. replace the jmp/jsr with a bra/bsr. */
  577. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
  578. /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
  579. here, but that only checks if the symbol is an external symbol,
  580. not if the symbol is in a different section. Besides, we need
  581. a consistent meaning for the relocation, so we just assume here that
  582. the value of the symbol is not available. */
  583. /* We can't fully resolve this yet, because the external
  584. symbol value may be changed by future relaxing. We let
  585. the final link phase handle it. */
  586. if (bfd_get_16 (abfd, contents + irel->r_offset) & 0x0020)
  587. bfd_put_16 (abfd, (bfd_vma) 0xa000, contents + irel->r_offset);
  588. else
  589. bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
  590. irel->r_addend = -4;
  591. /* When we calculated the symbol "value" we had an offset in the
  592. DIR32's word in memory (we read and add it above). However,
  593. the jsr we create does NOT have this offset encoded, so we
  594. have to add it to the addend to preserve it. */
  595. irel->r_addend += bfd_get_32 (abfd, contents + paddr);
  596. /* See if there is another R_SH_USES reloc referring to the same
  597. register load. */
  598. for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
  599. if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
  600. && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
  601. break;
  602. if (irelscan < irelend)
  603. {
  604. /* Some other function call depends upon this register load,
  605. and we have not yet converted that function call.
  606. Indeed, we may never be able to convert it. There is
  607. nothing else we can do at this point. */
  608. continue;
  609. }
  610. /* Look for a R_SH_COUNT reloc on the location where the
  611. function address is stored. Do this before deleting any
  612. bytes, to avoid confusion about the address. */
  613. for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
  614. if (irelcount->r_offset == paddr
  615. && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
  616. break;
  617. /* Delete the register load. */
  618. if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
  619. goto error_return;
  620. /* That will change things, so, just in case it permits some
  621. other function call to come within range, we should relax
  622. again. Note that this is not required, and it may be slow. */
  623. *again = true;
  624. /* Now check whether we got a COUNT reloc. */
  625. if (irelcount >= irelend)
  626. {
  627. _bfd_error_handler
  628. /* xgettext:c-format */
  629. (_("%pB: %#" PRIx64 ": warning: "
  630. "could not find expected COUNT reloc"),
  631. abfd, (uint64_t) paddr);
  632. continue;
  633. }
  634. /* The number of uses is stored in the r_addend field. We've
  635. just deleted one. */
  636. if (irelcount->r_addend == 0)
  637. {
  638. /* xgettext:c-format */
  639. _bfd_error_handler (_("%pB: %#" PRIx64 ": warning: bad count"),
  640. abfd, (uint64_t) paddr);
  641. continue;
  642. }
  643. --irelcount->r_addend;
  644. /* If there are no more uses, we can delete the address. Reload
  645. the address from irelfn, in case it was changed by the
  646. previous call to sh_elf_relax_delete_bytes. */
  647. if (irelcount->r_addend == 0)
  648. {
  649. if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
  650. goto error_return;
  651. }
  652. /* We've done all we can with that function call. */
  653. }
  654. /* Look for load and store instructions that we can align on four
  655. byte boundaries. */
  656. if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
  657. && have_code)
  658. {
  659. bool swapped;
  660. /* Get the section contents. */
  661. if (contents == NULL)
  662. {
  663. if (elf_section_data (sec)->this_hdr.contents != NULL)
  664. contents = elf_section_data (sec)->this_hdr.contents;
  665. else
  666. {
  667. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  668. goto error_return;
  669. }
  670. }
  671. if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
  672. &swapped))
  673. goto error_return;
  674. if (swapped)
  675. {
  676. elf_section_data (sec)->relocs = internal_relocs;
  677. elf_section_data (sec)->this_hdr.contents = contents;
  678. symtab_hdr->contents = (unsigned char *) isymbuf;
  679. }
  680. }
  681. if (isymbuf != NULL
  682. && symtab_hdr->contents != (unsigned char *) isymbuf)
  683. {
  684. if (! link_info->keep_memory)
  685. free (isymbuf);
  686. else
  687. {
  688. /* Cache the symbols for elf_link_input_bfd. */
  689. symtab_hdr->contents = (unsigned char *) isymbuf;
  690. }
  691. }
  692. if (contents != NULL
  693. && elf_section_data (sec)->this_hdr.contents != contents)
  694. {
  695. if (! link_info->keep_memory)
  696. free (contents);
  697. else
  698. {
  699. /* Cache the section contents for elf_link_input_bfd. */
  700. elf_section_data (sec)->this_hdr.contents = contents;
  701. }
  702. }
  703. if (elf_section_data (sec)->relocs != internal_relocs)
  704. free (internal_relocs);
  705. return true;
  706. error_return:
  707. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  708. free (isymbuf);
  709. if (elf_section_data (sec)->this_hdr.contents != contents)
  710. free (contents);
  711. if (elf_section_data (sec)->relocs != internal_relocs)
  712. free (internal_relocs);
  713. return false;
  714. }
  715. /* Delete some bytes from a section while relaxing. FIXME: There is a
  716. lot of duplication between this function and sh_relax_delete_bytes
  717. in coff-sh.c. */
  718. static bool
  719. sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
  720. int count)
  721. {
  722. Elf_Internal_Shdr *symtab_hdr;
  723. unsigned int sec_shndx;
  724. bfd_byte *contents;
  725. Elf_Internal_Rela *irel, *irelend;
  726. Elf_Internal_Rela *irelalign;
  727. bfd_vma toaddr;
  728. Elf_Internal_Sym *isymbuf, *isym, *isymend;
  729. struct elf_link_hash_entry **sym_hashes;
  730. struct elf_link_hash_entry **end_hashes;
  731. unsigned int symcount;
  732. asection *o;
  733. symtab_hdr = &elf_symtab_hdr (abfd);
  734. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  735. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  736. contents = elf_section_data (sec)->this_hdr.contents;
  737. /* The deletion must stop at the next ALIGN reloc for an alignment
  738. power larger than the number of bytes we are deleting. */
  739. irelalign = NULL;
  740. toaddr = sec->size;
  741. irel = elf_section_data (sec)->relocs;
  742. irelend = irel + sec->reloc_count;
  743. for (; irel < irelend; irel++)
  744. {
  745. if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
  746. && irel->r_offset > addr
  747. && count < (1 << irel->r_addend))
  748. {
  749. irelalign = irel;
  750. toaddr = irel->r_offset;
  751. break;
  752. }
  753. }
  754. /* Actually delete the bytes. */
  755. memmove (contents + addr, contents + addr + count,
  756. (size_t) (toaddr - addr - count));
  757. if (irelalign == NULL)
  758. sec->size -= count;
  759. else
  760. {
  761. int i;
  762. #define NOP_OPCODE (0x0009)
  763. BFD_ASSERT ((count & 1) == 0);
  764. for (i = 0; i < count; i += 2)
  765. bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
  766. }
  767. /* Adjust all the relocs. */
  768. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  769. {
  770. bfd_vma nraddr, stop;
  771. bfd_vma start = 0;
  772. int insn = 0;
  773. int off, adjust, oinsn;
  774. bfd_signed_vma voff = 0;
  775. bool overflow;
  776. /* Get the new reloc address. */
  777. nraddr = irel->r_offset;
  778. if ((irel->r_offset > addr
  779. && irel->r_offset < toaddr)
  780. || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
  781. && irel->r_offset == toaddr))
  782. nraddr -= count;
  783. /* See if this reloc was for the bytes we have deleted, in which
  784. case we no longer care about it. Don't delete relocs which
  785. represent addresses, though. */
  786. if (irel->r_offset >= addr
  787. && irel->r_offset < addr + count
  788. && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
  789. && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
  790. && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
  791. && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
  792. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  793. (int) R_SH_NONE);
  794. /* If this is a PC relative reloc, see if the range it covers
  795. includes the bytes we have deleted. */
  796. switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
  797. {
  798. default:
  799. break;
  800. case R_SH_DIR8WPN:
  801. case R_SH_IND12W:
  802. case R_SH_DIR8WPZ:
  803. case R_SH_DIR8WPL:
  804. start = irel->r_offset;
  805. insn = bfd_get_16 (abfd, contents + nraddr);
  806. break;
  807. }
  808. switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
  809. {
  810. default:
  811. start = stop = addr;
  812. break;
  813. case R_SH_DIR32:
  814. /* If this reloc is against a symbol defined in this
  815. section, and the symbol will not be adjusted below, we
  816. must check the addend to see it will put the value in
  817. range to be adjusted, and hence must be changed. */
  818. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  819. {
  820. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  821. if (isym->st_shndx == sec_shndx
  822. && (isym->st_value <= addr
  823. || isym->st_value >= toaddr))
  824. {
  825. bfd_vma val;
  826. if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
  827. {
  828. val = bfd_get_32 (abfd, contents + nraddr);
  829. val += isym->st_value;
  830. if (val > addr && val < toaddr)
  831. bfd_put_32 (abfd, val - count, contents + nraddr);
  832. }
  833. else
  834. {
  835. val = isym->st_value + irel->r_addend;
  836. if (val > addr && val < toaddr)
  837. irel->r_addend -= count;
  838. }
  839. }
  840. }
  841. start = stop = addr;
  842. break;
  843. case R_SH_DIR8WPN:
  844. off = insn & 0xff;
  845. if (off & 0x80)
  846. off -= 0x100;
  847. stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
  848. break;
  849. case R_SH_IND12W:
  850. off = insn & 0xfff;
  851. if (! off)
  852. {
  853. /* This has been made by previous relaxation. Since the
  854. relocation will be against an external symbol, the
  855. final relocation will just do the right thing. */
  856. start = stop = addr;
  857. }
  858. else
  859. {
  860. if (off & 0x800)
  861. off -= 0x1000;
  862. stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
  863. /* The addend will be against the section symbol, thus
  864. for adjusting the addend, the relevant start is the
  865. start of the section.
  866. N.B. If we want to abandon in-place changes here and
  867. test directly using symbol + addend, we have to take into
  868. account that the addend has already been adjusted by -4. */
  869. if (stop > addr && stop < toaddr)
  870. irel->r_addend -= count;
  871. }
  872. break;
  873. case R_SH_DIR8WPZ:
  874. off = insn & 0xff;
  875. stop = start + 4 + off * 2;
  876. break;
  877. case R_SH_DIR8WPL:
  878. off = insn & 0xff;
  879. stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
  880. break;
  881. case R_SH_SWITCH8:
  882. case R_SH_SWITCH16:
  883. case R_SH_SWITCH32:
  884. /* These relocs types represent
  885. .word L2-L1
  886. The r_addend field holds the difference between the reloc
  887. address and L1. That is the start of the reloc, and
  888. adding in the contents gives us the top. We must adjust
  889. both the r_offset field and the section contents.
  890. N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
  891. and the elf bfd r_offset is called r_vaddr. */
  892. stop = irel->r_offset;
  893. start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
  894. if (start > addr
  895. && start < toaddr
  896. && (stop <= addr || stop >= toaddr))
  897. irel->r_addend += count;
  898. else if (stop > addr
  899. && stop < toaddr
  900. && (start <= addr || start >= toaddr))
  901. irel->r_addend -= count;
  902. if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
  903. voff = bfd_get_signed_16 (abfd, contents + nraddr);
  904. else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
  905. voff = bfd_get_8 (abfd, contents + nraddr);
  906. else
  907. voff = bfd_get_signed_32 (abfd, contents + nraddr);
  908. stop = (bfd_vma) ((bfd_signed_vma) start + voff);
  909. break;
  910. case R_SH_USES:
  911. start = irel->r_offset;
  912. stop = (bfd_vma) ((bfd_signed_vma) start
  913. + (long) irel->r_addend
  914. + 4);
  915. break;
  916. }
  917. if (start > addr
  918. && start < toaddr
  919. && (stop <= addr || stop >= toaddr))
  920. adjust = count;
  921. else if (stop > addr
  922. && stop < toaddr
  923. && (start <= addr || start >= toaddr))
  924. adjust = - count;
  925. else
  926. adjust = 0;
  927. if (adjust != 0)
  928. {
  929. oinsn = insn;
  930. overflow = false;
  931. switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
  932. {
  933. default:
  934. abort ();
  935. break;
  936. case R_SH_DIR8WPN:
  937. case R_SH_DIR8WPZ:
  938. insn += adjust / 2;
  939. if ((oinsn & 0xff00) != (insn & 0xff00))
  940. overflow = true;
  941. bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
  942. break;
  943. case R_SH_IND12W:
  944. insn += adjust / 2;
  945. if ((oinsn & 0xf000) != (insn & 0xf000))
  946. overflow = true;
  947. bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
  948. break;
  949. case R_SH_DIR8WPL:
  950. BFD_ASSERT (adjust == count || count >= 4);
  951. if (count >= 4)
  952. insn += adjust / 4;
  953. else
  954. {
  955. if ((irel->r_offset & 3) == 0)
  956. ++insn;
  957. }
  958. if ((oinsn & 0xff00) != (insn & 0xff00))
  959. overflow = true;
  960. bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
  961. break;
  962. case R_SH_SWITCH8:
  963. voff += adjust;
  964. if (voff < 0 || voff >= 0xff)
  965. overflow = true;
  966. bfd_put_8 (abfd, voff, contents + nraddr);
  967. break;
  968. case R_SH_SWITCH16:
  969. voff += adjust;
  970. if (voff < - 0x8000 || voff >= 0x8000)
  971. overflow = true;
  972. bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
  973. break;
  974. case R_SH_SWITCH32:
  975. voff += adjust;
  976. bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
  977. break;
  978. case R_SH_USES:
  979. irel->r_addend += adjust;
  980. break;
  981. }
  982. if (overflow)
  983. {
  984. _bfd_error_handler
  985. /* xgettext:c-format */
  986. (_("%pB: %#" PRIx64 ": fatal: reloc overflow while relaxing"),
  987. abfd, (uint64_t) irel->r_offset);
  988. bfd_set_error (bfd_error_bad_value);
  989. return false;
  990. }
  991. }
  992. irel->r_offset = nraddr;
  993. }
  994. /* Look through all the other sections. If there contain any IMM32
  995. relocs against internal symbols which we are not going to adjust
  996. below, we may need to adjust the addends. */
  997. for (o = abfd->sections; o != NULL; o = o->next)
  998. {
  999. Elf_Internal_Rela *internal_relocs;
  1000. Elf_Internal_Rela *irelscan, *irelscanend;
  1001. bfd_byte *ocontents;
  1002. if (o == sec
  1003. || (o->flags & SEC_RELOC) == 0
  1004. || o->reloc_count == 0)
  1005. continue;
  1006. /* We always cache the relocs. Perhaps, if info->keep_memory is
  1007. FALSE, we should free them, if we are permitted to, when we
  1008. leave sh_coff_relax_section. */
  1009. internal_relocs = (_bfd_elf_link_read_relocs
  1010. (abfd, o, NULL, (Elf_Internal_Rela *) NULL, true));
  1011. if (internal_relocs == NULL)
  1012. return false;
  1013. ocontents = NULL;
  1014. irelscanend = internal_relocs + o->reloc_count;
  1015. for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
  1016. {
  1017. /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
  1018. if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
  1019. {
  1020. bfd_vma start, stop;
  1021. bfd_signed_vma voff;
  1022. if (ocontents == NULL)
  1023. {
  1024. if (elf_section_data (o)->this_hdr.contents != NULL)
  1025. ocontents = elf_section_data (o)->this_hdr.contents;
  1026. else
  1027. {
  1028. /* We always cache the section contents.
  1029. Perhaps, if info->keep_memory is FALSE, we
  1030. should free them, if we are permitted to,
  1031. when we leave sh_coff_relax_section. */
  1032. if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
  1033. {
  1034. free (ocontents);
  1035. return false;
  1036. }
  1037. elf_section_data (o)->this_hdr.contents = ocontents;
  1038. }
  1039. }
  1040. stop = irelscan->r_offset;
  1041. start
  1042. = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
  1043. /* STOP is in a different section, so it won't change. */
  1044. if (start > addr && start < toaddr)
  1045. irelscan->r_addend += count;
  1046. voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
  1047. stop = (bfd_vma) ((bfd_signed_vma) start + voff);
  1048. if (start > addr
  1049. && start < toaddr
  1050. && (stop <= addr || stop >= toaddr))
  1051. bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
  1052. ocontents + irelscan->r_offset);
  1053. else if (stop > addr
  1054. && stop < toaddr
  1055. && (start <= addr || start >= toaddr))
  1056. bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
  1057. ocontents + irelscan->r_offset);
  1058. }
  1059. if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
  1060. continue;
  1061. if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
  1062. continue;
  1063. isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
  1064. if (isym->st_shndx == sec_shndx
  1065. && (isym->st_value <= addr
  1066. || isym->st_value >= toaddr))
  1067. {
  1068. bfd_vma val;
  1069. if (ocontents == NULL)
  1070. {
  1071. if (elf_section_data (o)->this_hdr.contents != NULL)
  1072. ocontents = elf_section_data (o)->this_hdr.contents;
  1073. else
  1074. {
  1075. /* We always cache the section contents.
  1076. Perhaps, if info->keep_memory is FALSE, we
  1077. should free them, if we are permitted to,
  1078. when we leave sh_coff_relax_section. */
  1079. if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
  1080. {
  1081. free (ocontents);
  1082. return false;
  1083. }
  1084. elf_section_data (o)->this_hdr.contents = ocontents;
  1085. }
  1086. }
  1087. val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
  1088. val += isym->st_value;
  1089. if (val > addr && val < toaddr)
  1090. bfd_put_32 (abfd, val - count,
  1091. ocontents + irelscan->r_offset);
  1092. }
  1093. }
  1094. }
  1095. /* Adjust the local symbols defined in this section. */
  1096. isymend = isymbuf + symtab_hdr->sh_info;
  1097. for (isym = isymbuf; isym < isymend; isym++)
  1098. {
  1099. if (isym->st_shndx == sec_shndx
  1100. && isym->st_value > addr
  1101. && isym->st_value < toaddr)
  1102. isym->st_value -= count;
  1103. }
  1104. /* Now adjust the global symbols defined in this section. */
  1105. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1106. - symtab_hdr->sh_info);
  1107. sym_hashes = elf_sym_hashes (abfd);
  1108. end_hashes = sym_hashes + symcount;
  1109. for (; sym_hashes < end_hashes; sym_hashes++)
  1110. {
  1111. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1112. if ((sym_hash->root.type == bfd_link_hash_defined
  1113. || sym_hash->root.type == bfd_link_hash_defweak)
  1114. && sym_hash->root.u.def.section == sec
  1115. && sym_hash->root.u.def.value > addr
  1116. && sym_hash->root.u.def.value < toaddr)
  1117. {
  1118. sym_hash->root.u.def.value -= count;
  1119. }
  1120. }
  1121. /* See if we can move the ALIGN reloc forward. We have adjusted
  1122. r_offset for it already. */
  1123. if (irelalign != NULL)
  1124. {
  1125. bfd_vma alignto, alignaddr;
  1126. alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
  1127. alignaddr = BFD_ALIGN (irelalign->r_offset,
  1128. 1 << irelalign->r_addend);
  1129. if (alignto != alignaddr)
  1130. {
  1131. /* Tail recursion. */
  1132. return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
  1133. (int) (alignto - alignaddr));
  1134. }
  1135. }
  1136. return true;
  1137. }
  1138. /* Look for loads and stores which we can align to four byte
  1139. boundaries. This is like sh_align_loads in coff-sh.c. */
  1140. static bool
  1141. sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
  1142. Elf_Internal_Rela *internal_relocs,
  1143. bfd_byte *contents ATTRIBUTE_UNUSED,
  1144. bool *pswapped)
  1145. {
  1146. Elf_Internal_Rela *irel, *irelend;
  1147. bfd_vma *labels = NULL;
  1148. bfd_vma *label, *label_end;
  1149. bfd_size_type amt;
  1150. *pswapped = false;
  1151. irelend = internal_relocs + sec->reloc_count;
  1152. /* Get all the addresses with labels on them. */
  1153. amt = sec->reloc_count;
  1154. amt *= sizeof (bfd_vma);
  1155. labels = (bfd_vma *) bfd_malloc (amt);
  1156. if (labels == NULL)
  1157. goto error_return;
  1158. label_end = labels;
  1159. for (irel = internal_relocs; irel < irelend; irel++)
  1160. {
  1161. if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
  1162. {
  1163. *label_end = irel->r_offset;
  1164. ++label_end;
  1165. }
  1166. }
  1167. /* Note that the assembler currently always outputs relocs in
  1168. address order. If that ever changes, this code will need to sort
  1169. the label values and the relocs. */
  1170. label = labels;
  1171. for (irel = internal_relocs; irel < irelend; irel++)
  1172. {
  1173. bfd_vma start, stop;
  1174. if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
  1175. continue;
  1176. start = irel->r_offset;
  1177. for (irel++; irel < irelend; irel++)
  1178. if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
  1179. break;
  1180. if (irel < irelend)
  1181. stop = irel->r_offset;
  1182. else
  1183. stop = sec->size;
  1184. if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
  1185. internal_relocs, &label,
  1186. label_end, start, stop, pswapped))
  1187. goto error_return;
  1188. }
  1189. free (labels);
  1190. return true;
  1191. error_return:
  1192. free (labels);
  1193. return false;
  1194. }
  1195. /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
  1196. static bool
  1197. sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
  1198. bfd_byte *contents, bfd_vma addr)
  1199. {
  1200. Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
  1201. unsigned short i1, i2;
  1202. Elf_Internal_Rela *irel, *irelend;
  1203. /* Swap the instructions themselves. */
  1204. i1 = bfd_get_16 (abfd, contents + addr);
  1205. i2 = bfd_get_16 (abfd, contents + addr + 2);
  1206. bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
  1207. bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
  1208. /* Adjust all reloc addresses. */
  1209. irelend = internal_relocs + sec->reloc_count;
  1210. for (irel = internal_relocs; irel < irelend; irel++)
  1211. {
  1212. enum elf_sh_reloc_type type;
  1213. int add;
  1214. /* There are a few special types of relocs that we don't want to
  1215. adjust. These relocs do not apply to the instruction itself,
  1216. but are only associated with the address. */
  1217. type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
  1218. if (type == R_SH_ALIGN
  1219. || type == R_SH_CODE
  1220. || type == R_SH_DATA
  1221. || type == R_SH_LABEL)
  1222. continue;
  1223. /* If an R_SH_USES reloc points to one of the addresses being
  1224. swapped, we must adjust it. It would be incorrect to do this
  1225. for a jump, though, since we want to execute both
  1226. instructions after the jump. (We have avoided swapping
  1227. around a label, so the jump will not wind up executing an
  1228. instruction it shouldn't). */
  1229. if (type == R_SH_USES)
  1230. {
  1231. bfd_vma off;
  1232. off = irel->r_offset + 4 + irel->r_addend;
  1233. if (off == addr)
  1234. irel->r_offset += 2;
  1235. else if (off == addr + 2)
  1236. irel->r_offset -= 2;
  1237. }
  1238. if (irel->r_offset == addr)
  1239. {
  1240. irel->r_offset += 2;
  1241. add = -2;
  1242. }
  1243. else if (irel->r_offset == addr + 2)
  1244. {
  1245. irel->r_offset -= 2;
  1246. add = 2;
  1247. }
  1248. else
  1249. add = 0;
  1250. if (add != 0)
  1251. {
  1252. bfd_byte *loc;
  1253. unsigned short insn, oinsn;
  1254. bool overflow;
  1255. loc = contents + irel->r_offset;
  1256. overflow = false;
  1257. switch (type)
  1258. {
  1259. default:
  1260. break;
  1261. case R_SH_DIR8WPN:
  1262. case R_SH_DIR8WPZ:
  1263. insn = bfd_get_16 (abfd, loc);
  1264. oinsn = insn;
  1265. insn += add / 2;
  1266. if ((oinsn & 0xff00) != (insn & 0xff00))
  1267. overflow = true;
  1268. bfd_put_16 (abfd, (bfd_vma) insn, loc);
  1269. break;
  1270. case R_SH_IND12W:
  1271. insn = bfd_get_16 (abfd, loc);
  1272. oinsn = insn;
  1273. insn += add / 2;
  1274. if ((oinsn & 0xf000) != (insn & 0xf000))
  1275. overflow = true;
  1276. bfd_put_16 (abfd, (bfd_vma) insn, loc);
  1277. break;
  1278. case R_SH_DIR8WPL:
  1279. /* This reloc ignores the least significant 3 bits of
  1280. the program counter before adding in the offset.
  1281. This means that if ADDR is at an even address, the
  1282. swap will not affect the offset. If ADDR is an at an
  1283. odd address, then the instruction will be crossing a
  1284. four byte boundary, and must be adjusted. */
  1285. if ((addr & 3) != 0)
  1286. {
  1287. insn = bfd_get_16 (abfd, loc);
  1288. oinsn = insn;
  1289. insn += add / 2;
  1290. if ((oinsn & 0xff00) != (insn & 0xff00))
  1291. overflow = true;
  1292. bfd_put_16 (abfd, (bfd_vma) insn, loc);
  1293. }
  1294. break;
  1295. }
  1296. if (overflow)
  1297. {
  1298. _bfd_error_handler
  1299. /* xgettext:c-format */
  1300. (_("%pB: %#" PRIx64 ": fatal: reloc overflow while relaxing"),
  1301. abfd, (uint64_t) irel->r_offset);
  1302. bfd_set_error (bfd_error_bad_value);
  1303. return false;
  1304. }
  1305. }
  1306. }
  1307. return true;
  1308. }
  1309. /* Describes one of the various PLT styles. */
  1310. struct elf_sh_plt_info
  1311. {
  1312. /* The template for the first PLT entry, or NULL if there is no special
  1313. first entry. */
  1314. const bfd_byte *plt0_entry;
  1315. /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
  1316. bfd_vma plt0_entry_size;
  1317. /* Index I is the offset into PLT0_ENTRY of a pointer to
  1318. _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
  1319. if there is no such pointer. */
  1320. bfd_vma plt0_got_fields[3];
  1321. /* The template for a symbol's PLT entry. */
  1322. const bfd_byte *symbol_entry;
  1323. /* The size of SYMBOL_ENTRY in bytes. */
  1324. bfd_vma symbol_entry_size;
  1325. /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
  1326. on all targets. The comments by each member indicate the value
  1327. that the field must hold. */
  1328. struct {
  1329. bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
  1330. bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
  1331. bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
  1332. bool got20; /* TRUE if got_entry points to a movi20 instruction
  1333. (instead of a constant pool entry). */
  1334. } symbol_fields;
  1335. /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
  1336. bfd_vma symbol_resolve_offset;
  1337. /* A different PLT layout which can be used for the first
  1338. MAX_SHORT_PLT entries. It must share the same plt0. NULL in
  1339. other cases. */
  1340. const struct elf_sh_plt_info *short_plt;
  1341. };
  1342. /* The size in bytes of an entry in the procedure linkage table. */
  1343. #define ELF_PLT_ENTRY_SIZE 28
  1344. /* First entry in an absolute procedure linkage table look like this. */
  1345. /* Note - this code has been "optimised" not to use r2. r2 is used by
  1346. GCC to return the address of large structures, so it should not be
  1347. corrupted here. This does mean however, that this PLT does not conform
  1348. to the SH PIC ABI. That spec says that r0 contains the type of the PLT
  1349. and r2 contains the GOT id. This version stores the GOT id in r0 and
  1350. ignores the type. Loaders can easily detect this difference however,
  1351. since the type will always be 0 or 8, and the GOT ids will always be
  1352. greater than or equal to 12. */
  1353. static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
  1354. {
  1355. 0xd0, 0x05, /* mov.l 2f,r0 */
  1356. 0x60, 0x02, /* mov.l @r0,r0 */
  1357. 0x2f, 0x06, /* mov.l r0,@-r15 */
  1358. 0xd0, 0x03, /* mov.l 1f,r0 */
  1359. 0x60, 0x02, /* mov.l @r0,r0 */
  1360. 0x40, 0x2b, /* jmp @r0 */
  1361. 0x60, 0xf6, /* mov.l @r15+,r0 */
  1362. 0x00, 0x09, /* nop */
  1363. 0x00, 0x09, /* nop */
  1364. 0x00, 0x09, /* nop */
  1365. 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
  1366. 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
  1367. };
  1368. static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
  1369. {
  1370. 0x05, 0xd0, /* mov.l 2f,r0 */
  1371. 0x02, 0x60, /* mov.l @r0,r0 */
  1372. 0x06, 0x2f, /* mov.l r0,@-r15 */
  1373. 0x03, 0xd0, /* mov.l 1f,r0 */
  1374. 0x02, 0x60, /* mov.l @r0,r0 */
  1375. 0x2b, 0x40, /* jmp @r0 */
  1376. 0xf6, 0x60, /* mov.l @r15+,r0 */
  1377. 0x09, 0x00, /* nop */
  1378. 0x09, 0x00, /* nop */
  1379. 0x09, 0x00, /* nop */
  1380. 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
  1381. 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
  1382. };
  1383. /* Sebsequent entries in an absolute procedure linkage table look like
  1384. this. */
  1385. static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
  1386. {
  1387. 0xd0, 0x04, /* mov.l 1f,r0 */
  1388. 0x60, 0x02, /* mov.l @(r0,r12),r0 */
  1389. 0xd1, 0x02, /* mov.l 0f,r1 */
  1390. 0x40, 0x2b, /* jmp @r0 */
  1391. 0x60, 0x13, /* mov r1,r0 */
  1392. 0xd1, 0x03, /* mov.l 2f,r1 */
  1393. 0x40, 0x2b, /* jmp @r0 */
  1394. 0x00, 0x09, /* nop */
  1395. 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
  1396. 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
  1397. 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
  1398. };
  1399. static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
  1400. {
  1401. 0x04, 0xd0, /* mov.l 1f,r0 */
  1402. 0x02, 0x60, /* mov.l @r0,r0 */
  1403. 0x02, 0xd1, /* mov.l 0f,r1 */
  1404. 0x2b, 0x40, /* jmp @r0 */
  1405. 0x13, 0x60, /* mov r1,r0 */
  1406. 0x03, 0xd1, /* mov.l 2f,r1 */
  1407. 0x2b, 0x40, /* jmp @r0 */
  1408. 0x09, 0x00, /* nop */
  1409. 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
  1410. 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
  1411. 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
  1412. };
  1413. /* Entries in a PIC procedure linkage table look like this. */
  1414. static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
  1415. {
  1416. 0xd0, 0x04, /* mov.l 1f,r0 */
  1417. 0x00, 0xce, /* mov.l @(r0,r12),r0 */
  1418. 0x40, 0x2b, /* jmp @r0 */
  1419. 0x00, 0x09, /* nop */
  1420. 0x50, 0xc2, /* mov.l @(8,r12),r0 */
  1421. 0xd1, 0x03, /* mov.l 2f,r1 */
  1422. 0x40, 0x2b, /* jmp @r0 */
  1423. 0x50, 0xc1, /* mov.l @(4,r12),r0 */
  1424. 0x00, 0x09, /* nop */
  1425. 0x00, 0x09, /* nop */
  1426. 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
  1427. 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
  1428. };
  1429. static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
  1430. {
  1431. 0x04, 0xd0, /* mov.l 1f,r0 */
  1432. 0xce, 0x00, /* mov.l @(r0,r12),r0 */
  1433. 0x2b, 0x40, /* jmp @r0 */
  1434. 0x09, 0x00, /* nop */
  1435. 0xc2, 0x50, /* mov.l @(8,r12),r0 */
  1436. 0x03, 0xd1, /* mov.l 2f,r1 */
  1437. 0x2b, 0x40, /* jmp @r0 */
  1438. 0xc1, 0x50, /* mov.l @(4,r12),r0 */
  1439. 0x09, 0x00, /* nop */
  1440. 0x09, 0x00, /* nop */
  1441. 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
  1442. 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
  1443. };
  1444. static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
  1445. {
  1446. {
  1447. /* Big-endian non-PIC. */
  1448. elf_sh_plt0_entry_be,
  1449. ELF_PLT_ENTRY_SIZE,
  1450. { MINUS_ONE, 24, 20 },
  1451. elf_sh_plt_entry_be,
  1452. ELF_PLT_ENTRY_SIZE,
  1453. { 20, 16, 24, false },
  1454. 8,
  1455. NULL
  1456. },
  1457. {
  1458. /* Little-endian non-PIC. */
  1459. elf_sh_plt0_entry_le,
  1460. ELF_PLT_ENTRY_SIZE,
  1461. { MINUS_ONE, 24, 20 },
  1462. elf_sh_plt_entry_le,
  1463. ELF_PLT_ENTRY_SIZE,
  1464. { 20, 16, 24, false },
  1465. 8,
  1466. NULL
  1467. },
  1468. },
  1469. {
  1470. {
  1471. /* Big-endian PIC. */
  1472. elf_sh_plt0_entry_be,
  1473. ELF_PLT_ENTRY_SIZE,
  1474. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1475. elf_sh_pic_plt_entry_be,
  1476. ELF_PLT_ENTRY_SIZE,
  1477. { 20, MINUS_ONE, 24, false },
  1478. 8,
  1479. NULL
  1480. },
  1481. {
  1482. /* Little-endian PIC. */
  1483. elf_sh_plt0_entry_le,
  1484. ELF_PLT_ENTRY_SIZE,
  1485. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1486. elf_sh_pic_plt_entry_le,
  1487. ELF_PLT_ENTRY_SIZE,
  1488. { 20, MINUS_ONE, 24, false },
  1489. 8,
  1490. NULL
  1491. },
  1492. }
  1493. };
  1494. #define VXWORKS_PLT_HEADER_SIZE 12
  1495. #define VXWORKS_PLT_ENTRY_SIZE 24
  1496. static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
  1497. {
  1498. 0xd1, 0x01, /* mov.l @(8,pc),r1 */
  1499. 0x61, 0x12, /* mov.l @r1,r1 */
  1500. 0x41, 0x2b, /* jmp @r1 */
  1501. 0x00, 0x09, /* nop */
  1502. 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
  1503. };
  1504. static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
  1505. {
  1506. 0x01, 0xd1, /* mov.l @(8,pc),r1 */
  1507. 0x12, 0x61, /* mov.l @r1,r1 */
  1508. 0x2b, 0x41, /* jmp @r1 */
  1509. 0x09, 0x00, /* nop */
  1510. 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
  1511. };
  1512. static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
  1513. {
  1514. 0xd0, 0x01, /* mov.l @(8,pc),r0 */
  1515. 0x60, 0x02, /* mov.l @r0,r0 */
  1516. 0x40, 0x2b, /* jmp @r0 */
  1517. 0x00, 0x09, /* nop */
  1518. 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
  1519. 0xd0, 0x01, /* mov.l @(8,pc),r0 */
  1520. 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
  1521. 0x00, 0x09, /* nop */
  1522. 0x00, 0x09, /* nop */
  1523. 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
  1524. };
  1525. static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
  1526. {
  1527. 0x01, 0xd0, /* mov.l @(8,pc),r0 */
  1528. 0x02, 0x60, /* mov.l @r0,r0 */
  1529. 0x2b, 0x40, /* jmp @r0 */
  1530. 0x09, 0x00, /* nop */
  1531. 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
  1532. 0x01, 0xd0, /* mov.l @(8,pc),r0 */
  1533. 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
  1534. 0x09, 0x00, /* nop */
  1535. 0x09, 0x00, /* nop */
  1536. 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
  1537. };
  1538. static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
  1539. {
  1540. 0xd0, 0x01, /* mov.l @(8,pc),r0 */
  1541. 0x00, 0xce, /* mov.l @(r0,r12),r0 */
  1542. 0x40, 0x2b, /* jmp @r0 */
  1543. 0x00, 0x09, /* nop */
  1544. 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
  1545. 0xd0, 0x01, /* mov.l @(8,pc),r0 */
  1546. 0x51, 0xc2, /* mov.l @(8,r12),r1 */
  1547. 0x41, 0x2b, /* jmp @r1 */
  1548. 0x00, 0x09, /* nop */
  1549. 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
  1550. };
  1551. static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
  1552. {
  1553. 0x01, 0xd0, /* mov.l @(8,pc),r0 */
  1554. 0xce, 0x00, /* mov.l @(r0,r12),r0 */
  1555. 0x2b, 0x40, /* jmp @r0 */
  1556. 0x09, 0x00, /* nop */
  1557. 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
  1558. 0x01, 0xd0, /* mov.l @(8,pc),r0 */
  1559. 0xc2, 0x51, /* mov.l @(8,r12),r1 */
  1560. 0x2b, 0x41, /* jmp @r1 */
  1561. 0x09, 0x00, /* nop */
  1562. 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
  1563. };
  1564. static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
  1565. {
  1566. {
  1567. /* Big-endian non-PIC. */
  1568. vxworks_sh_plt0_entry_be,
  1569. VXWORKS_PLT_HEADER_SIZE,
  1570. { MINUS_ONE, MINUS_ONE, 8 },
  1571. vxworks_sh_plt_entry_be,
  1572. VXWORKS_PLT_ENTRY_SIZE,
  1573. { 8, 14, 20, false },
  1574. 12,
  1575. NULL
  1576. },
  1577. {
  1578. /* Little-endian non-PIC. */
  1579. vxworks_sh_plt0_entry_le,
  1580. VXWORKS_PLT_HEADER_SIZE,
  1581. { MINUS_ONE, MINUS_ONE, 8 },
  1582. vxworks_sh_plt_entry_le,
  1583. VXWORKS_PLT_ENTRY_SIZE,
  1584. { 8, 14, 20, false },
  1585. 12,
  1586. NULL
  1587. },
  1588. },
  1589. {
  1590. {
  1591. /* Big-endian PIC. */
  1592. NULL,
  1593. 0,
  1594. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1595. vxworks_sh_pic_plt_entry_be,
  1596. VXWORKS_PLT_ENTRY_SIZE,
  1597. { 8, MINUS_ONE, 20, false },
  1598. 12,
  1599. NULL
  1600. },
  1601. {
  1602. /* Little-endian PIC. */
  1603. NULL,
  1604. 0,
  1605. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1606. vxworks_sh_pic_plt_entry_le,
  1607. VXWORKS_PLT_ENTRY_SIZE,
  1608. { 8, MINUS_ONE, 20, false },
  1609. 12,
  1610. NULL
  1611. },
  1612. }
  1613. };
  1614. /* FDPIC PLT entries. Two unimplemented optimizations for lazy
  1615. binding are to omit the lazy binding stub when linking with -z now
  1616. and to move lazy binding stubs into a separate region for better
  1617. cache behavior. */
  1618. #define FDPIC_PLT_ENTRY_SIZE 28
  1619. #define FDPIC_PLT_LAZY_OFFSET 20
  1620. /* FIXME: The lazy binding stub requires a plt0 - which may need to be
  1621. duplicated if it is out of range, or which can be inlined. So
  1622. right now it is always inlined, which wastes a word per stub. It
  1623. might be easier to handle the duplication if we put the lazy
  1624. stubs separately. */
  1625. static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] =
  1626. {
  1627. 0xd0, 0x02, /* mov.l @(12,pc),r0 */
  1628. 0x01, 0xce, /* mov.l @(r0,r12),r1 */
  1629. 0x70, 0x04, /* add #4, r0 */
  1630. 0x41, 0x2b, /* jmp @r1 */
  1631. 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
  1632. 0x00, 0x09, /* nop */
  1633. 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
  1634. 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
  1635. 0x60, 0xc2, /* mov.l @r12,r0 */
  1636. 0x40, 0x2b, /* jmp @r0 */
  1637. 0x53, 0xc1, /* mov.l @(4,r12),r3 */
  1638. 0x00, 0x09, /* nop */
  1639. };
  1640. static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] =
  1641. {
  1642. 0x02, 0xd0, /* mov.l @(12,pc),r0 */
  1643. 0xce, 0x01, /* mov.l @(r0,r12),r1 */
  1644. 0x04, 0x70, /* add #4, r0 */
  1645. 0x2b, 0x41, /* jmp @r1 */
  1646. 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
  1647. 0x09, 0x00, /* nop */
  1648. 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
  1649. 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
  1650. 0xc2, 0x60, /* mov.l @r12,r0 */
  1651. 0x2b, 0x40, /* jmp @r0 */
  1652. 0xc1, 0x53, /* mov.l @(4,r12),r3 */
  1653. 0x09, 0x00, /* nop */
  1654. };
  1655. static const struct elf_sh_plt_info fdpic_sh_plts[2] = {
  1656. {
  1657. /* Big-endian PIC. */
  1658. NULL,
  1659. 0,
  1660. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1661. fdpic_sh_plt_entry_be,
  1662. FDPIC_PLT_ENTRY_SIZE,
  1663. { 12, MINUS_ONE, 16, false },
  1664. FDPIC_PLT_LAZY_OFFSET,
  1665. NULL
  1666. },
  1667. {
  1668. /* Little-endian PIC. */
  1669. NULL,
  1670. 0,
  1671. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1672. fdpic_sh_plt_entry_le,
  1673. FDPIC_PLT_ENTRY_SIZE,
  1674. { 12, MINUS_ONE, 16, false },
  1675. FDPIC_PLT_LAZY_OFFSET,
  1676. NULL
  1677. },
  1678. };
  1679. /* On SH2A, we can use the movi20 instruction to generate shorter PLT
  1680. entries for the first 64K slots. We use the normal FDPIC PLT entry
  1681. past that point; we could also use movi20s, which might be faster,
  1682. but would not be any smaller. */
  1683. #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
  1684. #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
  1685. static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] =
  1686. {
  1687. 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
  1688. 0x01, 0xce, /* mov.l @(r0,r12),r1 */
  1689. 0x70, 0x04, /* add #4, r0 */
  1690. 0x41, 0x2b, /* jmp @r1 */
  1691. 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
  1692. 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
  1693. 0x60, 0xc2, /* mov.l @r12,r0 */
  1694. 0x40, 0x2b, /* jmp @r0 */
  1695. 0x53, 0xc1, /* mov.l @(4,r12),r3 */
  1696. 0x00, 0x09, /* nop */
  1697. };
  1698. static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] =
  1699. {
  1700. 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
  1701. 0xce, 0x01, /* mov.l @(r0,r12),r1 */
  1702. 0x04, 0x70, /* add #4, r0 */
  1703. 0x2b, 0x41, /* jmp @r1 */
  1704. 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
  1705. 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
  1706. 0xc2, 0x60, /* mov.l @r12,r0 */
  1707. 0x2b, 0x40, /* jmp @r0 */
  1708. 0xc1, 0x53, /* mov.l @(4,r12),r3 */
  1709. 0x09, 0x00, /* nop */
  1710. };
  1711. static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = {
  1712. /* Big-endian FDPIC, max index 64K. */
  1713. NULL,
  1714. 0,
  1715. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1716. fdpic_sh2a_plt_entry_be,
  1717. FDPIC_SH2A_PLT_ENTRY_SIZE,
  1718. { 0, MINUS_ONE, 12, true },
  1719. FDPIC_SH2A_PLT_LAZY_OFFSET,
  1720. NULL
  1721. };
  1722. static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = {
  1723. /* Little-endian FDPIC, max index 64K. */
  1724. NULL,
  1725. 0,
  1726. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1727. fdpic_sh2a_plt_entry_le,
  1728. FDPIC_SH2A_PLT_ENTRY_SIZE,
  1729. { 0, MINUS_ONE, 12, true },
  1730. FDPIC_SH2A_PLT_LAZY_OFFSET,
  1731. NULL
  1732. };
  1733. static const struct elf_sh_plt_info fdpic_sh2a_plts[2] = {
  1734. {
  1735. /* Big-endian PIC. */
  1736. NULL,
  1737. 0,
  1738. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1739. fdpic_sh_plt_entry_be,
  1740. FDPIC_PLT_ENTRY_SIZE,
  1741. { 12, MINUS_ONE, 16, false },
  1742. FDPIC_PLT_LAZY_OFFSET,
  1743. &fdpic_sh2a_short_plt_be
  1744. },
  1745. {
  1746. /* Little-endian PIC. */
  1747. NULL,
  1748. 0,
  1749. { MINUS_ONE, MINUS_ONE, MINUS_ONE },
  1750. fdpic_sh_plt_entry_le,
  1751. FDPIC_PLT_ENTRY_SIZE,
  1752. { 12, MINUS_ONE, 16, false },
  1753. FDPIC_PLT_LAZY_OFFSET,
  1754. &fdpic_sh2a_short_plt_le
  1755. },
  1756. };
  1757. /* Return the type of PLT associated with ABFD. PIC_P is true if
  1758. the object is position-independent. */
  1759. static const struct elf_sh_plt_info *
  1760. get_plt_info (bfd *abfd, bool pic_p)
  1761. {
  1762. if (fdpic_object_p (abfd))
  1763. {
  1764. /* If any input file requires SH2A we can use a shorter PLT
  1765. sequence. */
  1766. if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base)
  1767. return &fdpic_sh2a_plts[!bfd_big_endian (abfd)];
  1768. else
  1769. return &fdpic_sh_plts[!bfd_big_endian (abfd)];
  1770. }
  1771. if (vxworks_object_p (abfd))
  1772. return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
  1773. return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
  1774. }
  1775. /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
  1776. VALUE is the field's value and CODE_P is true if VALUE refers to code,
  1777. not data. */
  1778. inline static void
  1779. install_plt_field (bfd *output_bfd, bool code_p ATTRIBUTE_UNUSED,
  1780. unsigned long value, bfd_byte *addr)
  1781. {
  1782. bfd_put_32 (output_bfd, value, addr);
  1783. }
  1784. /* The number of PLT entries which can use a shorter PLT, if any.
  1785. Currently always 64K, since only SH-2A FDPIC uses this; a
  1786. 20-bit movi20 can address that many function descriptors below
  1787. _GLOBAL_OFFSET_TABLE_. */
  1788. #define MAX_SHORT_PLT 65536
  1789. /* Return the index of the PLT entry at byte offset OFFSET. */
  1790. static bfd_vma
  1791. get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
  1792. {
  1793. bfd_vma plt_index = 0;
  1794. offset -= info->plt0_entry_size;
  1795. if (info->short_plt != NULL)
  1796. {
  1797. if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size)
  1798. {
  1799. plt_index = MAX_SHORT_PLT;
  1800. offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
  1801. }
  1802. else
  1803. info = info->short_plt;
  1804. }
  1805. return plt_index + offset / info->symbol_entry_size;
  1806. }
  1807. /* Do the inverse operation. */
  1808. static bfd_vma
  1809. get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index)
  1810. {
  1811. bfd_vma offset = 0;
  1812. if (info->short_plt != NULL)
  1813. {
  1814. if (plt_index > MAX_SHORT_PLT)
  1815. {
  1816. offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
  1817. plt_index -= MAX_SHORT_PLT;
  1818. }
  1819. else
  1820. info = info->short_plt;
  1821. }
  1822. return (offset + info->plt0_entry_size
  1823. + (plt_index * info->symbol_entry_size));
  1824. }
  1825. union gotref
  1826. {
  1827. bfd_signed_vma refcount;
  1828. bfd_vma offset;
  1829. };
  1830. /* sh ELF linker hash entry. */
  1831. struct elf_sh_link_hash_entry
  1832. {
  1833. struct elf_link_hash_entry root;
  1834. bfd_signed_vma gotplt_refcount;
  1835. /* A local function descriptor, for FDPIC. The refcount counts
  1836. R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
  1837. relocations; the PLT and GOT entry are accounted
  1838. for separately. After adjust_dynamic_symbol, the offset is
  1839. MINUS_ONE if there is no local descriptor (dynamic linker
  1840. managed and no PLT entry, or undefined weak non-dynamic).
  1841. During check_relocs we do not yet know whether the local
  1842. descriptor will be canonical. */
  1843. union gotref funcdesc;
  1844. /* How many of the above refcounted relocations were R_SH_FUNCDESC,
  1845. and thus require fixups or relocations. */
  1846. bfd_signed_vma abs_funcdesc_refcount;
  1847. enum got_type {
  1848. GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC
  1849. } got_type;
  1850. };
  1851. #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
  1852. struct sh_elf_obj_tdata
  1853. {
  1854. struct elf_obj_tdata root;
  1855. /* got_type for each local got entry. */
  1856. char *local_got_type;
  1857. /* Function descriptor refcount and offset for each local symbol. */
  1858. union gotref *local_funcdesc;
  1859. };
  1860. #define sh_elf_tdata(abfd) \
  1861. ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
  1862. #define sh_elf_local_got_type(abfd) \
  1863. (sh_elf_tdata (abfd)->local_got_type)
  1864. #define sh_elf_local_funcdesc(abfd) \
  1865. (sh_elf_tdata (abfd)->local_funcdesc)
  1866. #define is_sh_elf(bfd) \
  1867. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  1868. && elf_tdata (bfd) != NULL \
  1869. && elf_object_id (bfd) == SH_ELF_DATA)
  1870. /* Override the generic function because we need to store sh_elf_obj_tdata
  1871. as the specific tdata. */
  1872. static bool
  1873. sh_elf_mkobject (bfd *abfd)
  1874. {
  1875. return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata),
  1876. SH_ELF_DATA);
  1877. }
  1878. /* sh ELF linker hash table. */
  1879. struct elf_sh_link_hash_table
  1880. {
  1881. struct elf_link_hash_table root;
  1882. /* Short-cuts to get to dynamic linker sections. */
  1883. asection *sfuncdesc;
  1884. asection *srelfuncdesc;
  1885. asection *srofixup;
  1886. /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
  1887. asection *srelplt2;
  1888. /* A counter or offset to track a TLS got entry. */
  1889. union
  1890. {
  1891. bfd_signed_vma refcount;
  1892. bfd_vma offset;
  1893. } tls_ldm_got;
  1894. /* The type of PLT to use. */
  1895. const struct elf_sh_plt_info *plt_info;
  1896. /* True if the target system uses FDPIC. */
  1897. bool fdpic_p;
  1898. };
  1899. /* Traverse an sh ELF linker hash table. */
  1900. #define sh_elf_link_hash_traverse(table, func, info) \
  1901. (elf_link_hash_traverse \
  1902. (&(table)->root, \
  1903. (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
  1904. (info)))
  1905. /* Get the sh ELF linker hash table from a link_info structure. */
  1906. #define sh_elf_hash_table(p) \
  1907. ((is_elf_hash_table ((p)->hash) \
  1908. && elf_hash_table_id (elf_hash_table (p)) == SH_ELF_DATA) \
  1909. ? (struct elf_sh_link_hash_table *) (p)->hash : NULL)
  1910. /* Create an entry in an sh ELF linker hash table. */
  1911. static struct bfd_hash_entry *
  1912. sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
  1913. struct bfd_hash_table *table,
  1914. const char *string)
  1915. {
  1916. struct elf_sh_link_hash_entry *ret =
  1917. (struct elf_sh_link_hash_entry *) entry;
  1918. /* Allocate the structure if it has not already been allocated by a
  1919. subclass. */
  1920. if (ret == (struct elf_sh_link_hash_entry *) NULL)
  1921. ret = ((struct elf_sh_link_hash_entry *)
  1922. bfd_hash_allocate (table,
  1923. sizeof (struct elf_sh_link_hash_entry)));
  1924. if (ret == (struct elf_sh_link_hash_entry *) NULL)
  1925. return (struct bfd_hash_entry *) ret;
  1926. /* Call the allocation method of the superclass. */
  1927. ret = ((struct elf_sh_link_hash_entry *)
  1928. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  1929. table, string));
  1930. if (ret != (struct elf_sh_link_hash_entry *) NULL)
  1931. {
  1932. ret->gotplt_refcount = 0;
  1933. ret->funcdesc.refcount = 0;
  1934. ret->abs_funcdesc_refcount = 0;
  1935. ret->got_type = GOT_UNKNOWN;
  1936. }
  1937. return (struct bfd_hash_entry *) ret;
  1938. }
  1939. /* Create an sh ELF linker hash table. */
  1940. static struct bfd_link_hash_table *
  1941. sh_elf_link_hash_table_create (bfd *abfd)
  1942. {
  1943. struct elf_sh_link_hash_table *ret;
  1944. size_t amt = sizeof (struct elf_sh_link_hash_table);
  1945. ret = (struct elf_sh_link_hash_table *) bfd_zmalloc (amt);
  1946. if (ret == (struct elf_sh_link_hash_table *) NULL)
  1947. return NULL;
  1948. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  1949. sh_elf_link_hash_newfunc,
  1950. sizeof (struct elf_sh_link_hash_entry),
  1951. SH_ELF_DATA))
  1952. {
  1953. free (ret);
  1954. return NULL;
  1955. }
  1956. if (fdpic_object_p (abfd))
  1957. {
  1958. ret->root.dt_pltgot_required = true;
  1959. ret->fdpic_p = true;
  1960. }
  1961. return &ret->root.root;
  1962. }
  1963. static bool
  1964. sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
  1965. struct bfd_link_info *info, asection *p)
  1966. {
  1967. struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
  1968. /* Non-FDPIC binaries do not need dynamic symbols for sections. */
  1969. if (!htab->fdpic_p)
  1970. return true;
  1971. /* We need dynamic symbols for every section, since segments can
  1972. relocate independently. */
  1973. switch (elf_section_data (p)->this_hdr.sh_type)
  1974. {
  1975. case SHT_PROGBITS:
  1976. case SHT_NOBITS:
  1977. /* If sh_type is yet undecided, assume it could be
  1978. SHT_PROGBITS/SHT_NOBITS. */
  1979. case SHT_NULL:
  1980. return false;
  1981. /* There shouldn't be section relative relocations
  1982. against any other section. */
  1983. default:
  1984. return true;
  1985. }
  1986. }
  1987. /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
  1988. shortcuts to them in our hash table. */
  1989. static bool
  1990. create_got_section (bfd *dynobj, struct bfd_link_info *info)
  1991. {
  1992. struct elf_sh_link_hash_table *htab;
  1993. if (! _bfd_elf_create_got_section (dynobj, info))
  1994. return false;
  1995. htab = sh_elf_hash_table (info);
  1996. if (htab == NULL)
  1997. return false;
  1998. htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
  1999. (SEC_ALLOC | SEC_LOAD
  2000. | SEC_HAS_CONTENTS
  2001. | SEC_IN_MEMORY
  2002. | SEC_LINKER_CREATED));
  2003. if (htab->sfuncdesc == NULL
  2004. || !bfd_set_section_alignment (htab->sfuncdesc, 2))
  2005. return false;
  2006. htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
  2007. ".rela.got.funcdesc",
  2008. (SEC_ALLOC | SEC_LOAD
  2009. | SEC_HAS_CONTENTS
  2010. | SEC_IN_MEMORY
  2011. | SEC_LINKER_CREATED
  2012. | SEC_READONLY));
  2013. if (htab->srelfuncdesc == NULL
  2014. || !bfd_set_section_alignment (htab->srelfuncdesc, 2))
  2015. return false;
  2016. /* Also create .rofixup. */
  2017. htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
  2018. (SEC_ALLOC | SEC_LOAD
  2019. | SEC_HAS_CONTENTS
  2020. | SEC_IN_MEMORY
  2021. | SEC_LINKER_CREATED
  2022. | SEC_READONLY));
  2023. if (htab->srofixup == NULL
  2024. || !bfd_set_section_alignment (htab->srofixup, 2))
  2025. return false;
  2026. return true;
  2027. }
  2028. /* Create dynamic sections when linking against a dynamic object. */
  2029. static bool
  2030. sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  2031. {
  2032. struct elf_sh_link_hash_table *htab;
  2033. flagword flags, pltflags;
  2034. asection *s;
  2035. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2036. int ptralign = 0;
  2037. switch (bed->s->arch_size)
  2038. {
  2039. case 32:
  2040. ptralign = 2;
  2041. break;
  2042. case 64:
  2043. ptralign = 3;
  2044. break;
  2045. default:
  2046. bfd_set_error (bfd_error_bad_value);
  2047. return false;
  2048. }
  2049. htab = sh_elf_hash_table (info);
  2050. if (htab == NULL)
  2051. return false;
  2052. if (htab->root.dynamic_sections_created)
  2053. return true;
  2054. /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  2055. .rel[a].bss sections. */
  2056. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  2057. | SEC_LINKER_CREATED);
  2058. pltflags = flags;
  2059. pltflags |= SEC_CODE;
  2060. if (bed->plt_not_loaded)
  2061. pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
  2062. if (bed->plt_readonly)
  2063. pltflags |= SEC_READONLY;
  2064. s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
  2065. htab->root.splt = s;
  2066. if (s == NULL
  2067. || !bfd_set_section_alignment (s, bed->plt_alignment))
  2068. return false;
  2069. if (bed->want_plt_sym)
  2070. {
  2071. /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  2072. .plt section. */
  2073. struct elf_link_hash_entry *h;
  2074. struct bfd_link_hash_entry *bh = NULL;
  2075. if (! (_bfd_generic_link_add_one_symbol
  2076. (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
  2077. (bfd_vma) 0, (const char *) NULL, false,
  2078. get_elf_backend_data (abfd)->collect, &bh)))
  2079. return false;
  2080. h = (struct elf_link_hash_entry *) bh;
  2081. h->def_regular = 1;
  2082. h->type = STT_OBJECT;
  2083. htab->root.hplt = h;
  2084. if (bfd_link_pic (info)
  2085. && ! bfd_elf_link_record_dynamic_symbol (info, h))
  2086. return false;
  2087. }
  2088. s = bfd_make_section_anyway_with_flags (abfd,
  2089. bed->default_use_rela_p
  2090. ? ".rela.plt" : ".rel.plt",
  2091. flags | SEC_READONLY);
  2092. htab->root.srelplt = s;
  2093. if (s == NULL
  2094. || !bfd_set_section_alignment (s, ptralign))
  2095. return false;
  2096. if (htab->root.sgot == NULL
  2097. && !create_got_section (abfd, info))
  2098. return false;
  2099. if (bed->want_dynbss)
  2100. {
  2101. /* The .dynbss section is a place to put symbols which are defined
  2102. by dynamic objects, are referenced by regular objects, and are
  2103. not functions. We must allocate space for them in the process
  2104. image and use a R_*_COPY reloc to tell the dynamic linker to
  2105. initialize them at run time. The linker script puts the .dynbss
  2106. section into the .bss section of the final image. */
  2107. s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
  2108. SEC_ALLOC | SEC_LINKER_CREATED);
  2109. htab->root.sdynbss = s;
  2110. if (s == NULL)
  2111. return false;
  2112. /* The .rel[a].bss section holds copy relocs. This section is not
  2113. normally needed. We need to create it here, though, so that the
  2114. linker will map it to an output section. We can't just create it
  2115. only if we need it, because we will not know whether we need it
  2116. until we have seen all the input files, and the first time the
  2117. main linker code calls BFD after examining all the input files
  2118. (size_dynamic_sections) the input sections have already been
  2119. mapped to the output sections. If the section turns out not to
  2120. be needed, we can discard it later. We will never need this
  2121. section when generating a shared object, since they do not use
  2122. copy relocs. */
  2123. if (! bfd_link_pic (info))
  2124. {
  2125. s = bfd_make_section_anyway_with_flags (abfd,
  2126. (bed->default_use_rela_p
  2127. ? ".rela.bss" : ".rel.bss"),
  2128. flags | SEC_READONLY);
  2129. htab->root.srelbss = s;
  2130. if (s == NULL
  2131. || !bfd_set_section_alignment (s, ptralign))
  2132. return false;
  2133. }
  2134. }
  2135. if (htab->root.target_os == is_vxworks)
  2136. {
  2137. if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
  2138. return false;
  2139. }
  2140. return true;
  2141. }
  2142. /* Adjust a symbol defined by a dynamic object and referenced by a
  2143. regular object. The current definition is in some section of the
  2144. dynamic object, but we're not including those sections. We have to
  2145. change the definition to something the rest of the link can
  2146. understand. */
  2147. static bool
  2148. sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  2149. struct elf_link_hash_entry *h)
  2150. {
  2151. struct elf_sh_link_hash_table *htab;
  2152. asection *s;
  2153. htab = sh_elf_hash_table (info);
  2154. if (htab == NULL)
  2155. return false;
  2156. /* Make sure we know what is going on here. */
  2157. BFD_ASSERT (htab->root.dynobj != NULL
  2158. && (h->needs_plt
  2159. || h->is_weakalias
  2160. || (h->def_dynamic
  2161. && h->ref_regular
  2162. && !h->def_regular)));
  2163. /* If this is a function, put it in the procedure linkage table. We
  2164. will fill in the contents of the procedure linkage table later,
  2165. when we know the address of the .got section. */
  2166. if (h->type == STT_FUNC
  2167. || h->needs_plt)
  2168. {
  2169. if (h->plt.refcount <= 0
  2170. || SYMBOL_CALLS_LOCAL (info, h)
  2171. || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2172. && h->root.type == bfd_link_hash_undefweak))
  2173. {
  2174. /* This case can occur if we saw a PLT reloc in an input
  2175. file, but the symbol was never referred to by a dynamic
  2176. object. In such a case, we don't actually need to build
  2177. a procedure linkage table, and we can just do a REL32
  2178. reloc instead. */
  2179. h->plt.offset = (bfd_vma) -1;
  2180. h->needs_plt = 0;
  2181. }
  2182. return true;
  2183. }
  2184. else
  2185. h->plt.offset = (bfd_vma) -1;
  2186. /* If this is a weak symbol, and there is a real definition, the
  2187. processor independent code will have arranged for us to see the
  2188. real definition first, and we can just use the same value. */
  2189. if (h->is_weakalias)
  2190. {
  2191. struct elf_link_hash_entry *def = weakdef (h);
  2192. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  2193. h->root.u.def.section = def->root.u.def.section;
  2194. h->root.u.def.value = def->root.u.def.value;
  2195. if (info->nocopyreloc)
  2196. h->non_got_ref = def->non_got_ref;
  2197. return true;
  2198. }
  2199. /* This is a reference to a symbol defined by a dynamic object which
  2200. is not a function. */
  2201. /* If we are creating a shared library, we must presume that the
  2202. only references to the symbol are via the global offset table.
  2203. For such cases we need not do anything here; the relocations will
  2204. be handled correctly by relocate_section. */
  2205. if (bfd_link_pic (info))
  2206. return true;
  2207. /* If there are no references to this symbol that do not use the
  2208. GOT, we don't need to generate a copy reloc. */
  2209. if (!h->non_got_ref)
  2210. return true;
  2211. /* If -z nocopyreloc was given, we won't generate them either. */
  2212. if (0 && info->nocopyreloc)
  2213. {
  2214. h->non_got_ref = 0;
  2215. return true;
  2216. }
  2217. /* If we don't find any dynamic relocs in read-only sections, then
  2218. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  2219. if (0 && !_bfd_elf_readonly_dynrelocs (h))
  2220. {
  2221. h->non_got_ref = 0;
  2222. return true;
  2223. }
  2224. /* We must allocate the symbol in our .dynbss section, which will
  2225. become part of the .bss section of the executable. There will be
  2226. an entry for this symbol in the .dynsym section. The dynamic
  2227. object will contain position independent code, so all references
  2228. from the dynamic object to this symbol will go through the global
  2229. offset table. The dynamic linker will use the .dynsym entry to
  2230. determine the address it must put in the global offset table, so
  2231. both the dynamic object and the regular object will refer to the
  2232. same memory location for the variable. */
  2233. s = htab->root.sdynbss;
  2234. BFD_ASSERT (s != NULL);
  2235. /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
  2236. copy the initial value out of the dynamic object and into the
  2237. runtime process image. We need to remember the offset into the
  2238. .rela.bss section we are going to use. */
  2239. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  2240. {
  2241. asection *srel;
  2242. srel = htab->root.srelbss;
  2243. BFD_ASSERT (srel != NULL);
  2244. srel->size += sizeof (Elf32_External_Rela);
  2245. h->needs_copy = 1;
  2246. }
  2247. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  2248. }
  2249. /* Allocate space in .plt, .got and associated reloc sections for
  2250. dynamic relocs. */
  2251. static bool
  2252. allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  2253. {
  2254. struct bfd_link_info *info;
  2255. struct elf_sh_link_hash_table *htab;
  2256. struct elf_sh_link_hash_entry *eh;
  2257. struct elf_dyn_relocs *p;
  2258. if (h->root.type == bfd_link_hash_indirect)
  2259. return true;
  2260. info = (struct bfd_link_info *) inf;
  2261. htab = sh_elf_hash_table (info);
  2262. if (htab == NULL)
  2263. return false;
  2264. eh = (struct elf_sh_link_hash_entry *) h;
  2265. if ((h->got.refcount > 0
  2266. || h->forced_local)
  2267. && eh->gotplt_refcount > 0)
  2268. {
  2269. /* The symbol has been forced local, or we have some direct got refs,
  2270. so treat all the gotplt refs as got refs. */
  2271. h->got.refcount += eh->gotplt_refcount;
  2272. if (h->plt.refcount >= eh->gotplt_refcount)
  2273. h->plt.refcount -= eh->gotplt_refcount;
  2274. }
  2275. if (htab->root.dynamic_sections_created
  2276. && h->plt.refcount > 0
  2277. && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2278. || h->root.type != bfd_link_hash_undefweak))
  2279. {
  2280. /* Make sure this symbol is output as a dynamic symbol.
  2281. Undefined weak syms won't yet be marked as dynamic. */
  2282. if (h->dynindx == -1
  2283. && !h->forced_local)
  2284. {
  2285. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2286. return false;
  2287. }
  2288. if (bfd_link_pic (info)
  2289. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
  2290. {
  2291. asection *s = htab->root.splt;
  2292. const struct elf_sh_plt_info *plt_info;
  2293. /* If this is the first .plt entry, make room for the special
  2294. first entry. */
  2295. if (s->size == 0)
  2296. s->size += htab->plt_info->plt0_entry_size;
  2297. h->plt.offset = s->size;
  2298. /* If this symbol is not defined in a regular file, and we are
  2299. not generating a shared library, then set the symbol to this
  2300. location in the .plt. This is required to make function
  2301. pointers compare as equal between the normal executable and
  2302. the shared library. Skip this for FDPIC, since the
  2303. function's address will be the address of the canonical
  2304. function descriptor. */
  2305. if (!htab->fdpic_p && !bfd_link_pic (info) && !h->def_regular)
  2306. {
  2307. h->root.u.def.section = s;
  2308. h->root.u.def.value = h->plt.offset;
  2309. }
  2310. /* Make room for this entry. */
  2311. plt_info = htab->plt_info;
  2312. if (plt_info->short_plt != NULL
  2313. && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
  2314. plt_info = plt_info->short_plt;
  2315. s->size += plt_info->symbol_entry_size;
  2316. /* We also need to make an entry in the .got.plt section, which
  2317. will be placed in the .got section by the linker script. */
  2318. if (!htab->fdpic_p)
  2319. htab->root.sgotplt->size += 4;
  2320. else
  2321. htab->root.sgotplt->size += 8;
  2322. /* We also need to make an entry in the .rel.plt section. */
  2323. htab->root.srelplt->size += sizeof (Elf32_External_Rela);
  2324. if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
  2325. {
  2326. /* VxWorks executables have a second set of relocations
  2327. for each PLT entry. They go in a separate relocation
  2328. section, which is processed by the kernel loader. */
  2329. /* There is a relocation for the initial PLT entry:
  2330. an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
  2331. if (h->plt.offset == htab->plt_info->plt0_entry_size)
  2332. htab->srelplt2->size += sizeof (Elf32_External_Rela);
  2333. /* There are two extra relocations for each subsequent
  2334. PLT entry: an R_SH_DIR32 relocation for the GOT entry,
  2335. and an R_SH_DIR32 relocation for the PLT entry. */
  2336. htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
  2337. }
  2338. }
  2339. else
  2340. {
  2341. h->plt.offset = (bfd_vma) -1;
  2342. h->needs_plt = 0;
  2343. }
  2344. }
  2345. else
  2346. {
  2347. h->plt.offset = (bfd_vma) -1;
  2348. h->needs_plt = 0;
  2349. }
  2350. if (h->got.refcount > 0)
  2351. {
  2352. asection *s;
  2353. bool dyn;
  2354. enum got_type got_type = sh_elf_hash_entry (h)->got_type;
  2355. /* Make sure this symbol is output as a dynamic symbol.
  2356. Undefined weak syms won't yet be marked as dynamic. */
  2357. if (h->dynindx == -1
  2358. && !h->forced_local)
  2359. {
  2360. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2361. return false;
  2362. }
  2363. s = htab->root.sgot;
  2364. h->got.offset = s->size;
  2365. s->size += 4;
  2366. /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
  2367. if (got_type == GOT_TLS_GD)
  2368. s->size += 4;
  2369. dyn = htab->root.dynamic_sections_created;
  2370. if (!dyn)
  2371. {
  2372. /* No dynamic relocations required. */
  2373. if (htab->fdpic_p && !bfd_link_pic (info)
  2374. && h->root.type != bfd_link_hash_undefweak
  2375. && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
  2376. htab->srofixup->size += 4;
  2377. }
  2378. /* No dynamic relocations required when IE->LE conversion happens. */
  2379. else if (got_type == GOT_TLS_IE
  2380. && !h->def_dynamic
  2381. && !bfd_link_pic (info))
  2382. ;
  2383. /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
  2384. R_SH_TLS_GD needs one if local symbol and two if global. */
  2385. else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
  2386. || got_type == GOT_TLS_IE)
  2387. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  2388. else if (got_type == GOT_TLS_GD)
  2389. htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
  2390. else if (got_type == GOT_FUNCDESC)
  2391. {
  2392. if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
  2393. htab->srofixup->size += 4;
  2394. else
  2395. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  2396. }
  2397. else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2398. || h->root.type != bfd_link_hash_undefweak)
  2399. && (bfd_link_pic (info)
  2400. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
  2401. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  2402. else if (htab->fdpic_p
  2403. && !bfd_link_pic (info)
  2404. && got_type == GOT_NORMAL
  2405. && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2406. || h->root.type != bfd_link_hash_undefweak))
  2407. htab->srofixup->size += 4;
  2408. }
  2409. else
  2410. h->got.offset = (bfd_vma) -1;
  2411. /* Allocate space for any dynamic relocations to function
  2412. descriptors, canonical or otherwise. We need to relocate the
  2413. reference unless it resolves to zero, which only happens for
  2414. undefined weak symbols (either non-default visibility, or when
  2415. static linking). Any GOT slot is accounted for elsewhere. */
  2416. if (eh->abs_funcdesc_refcount > 0
  2417. && (h->root.type != bfd_link_hash_undefweak
  2418. || (htab->root.dynamic_sections_created
  2419. && ! SYMBOL_CALLS_LOCAL (info, h))))
  2420. {
  2421. if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
  2422. htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
  2423. else
  2424. htab->root.srelgot->size
  2425. += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
  2426. }
  2427. /* We must allocate a function descriptor if there are references to
  2428. a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
  2429. the dynamic linker isn't going to allocate it. None of this
  2430. applies if we already created one in .got.plt, but if the
  2431. canonical function descriptor can be in this object, there
  2432. won't be a PLT entry at all. */
  2433. if ((eh->funcdesc.refcount > 0
  2434. || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
  2435. && h->root.type != bfd_link_hash_undefweak
  2436. && SYMBOL_FUNCDESC_LOCAL (info, h))
  2437. {
  2438. /* Make room for this function descriptor. */
  2439. eh->funcdesc.offset = htab->sfuncdesc->size;
  2440. htab->sfuncdesc->size += 8;
  2441. /* We will need a relocation or two fixups to initialize the
  2442. function descriptor, so allocate those too. */
  2443. if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
  2444. htab->srofixup->size += 8;
  2445. else
  2446. htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
  2447. }
  2448. if (h->dyn_relocs == NULL)
  2449. return true;
  2450. /* In the shared -Bsymbolic case, discard space allocated for
  2451. dynamic pc-relative relocs against symbols which turn out to be
  2452. defined in regular objects. For the normal shared case, discard
  2453. space for pc-relative relocs that have become local due to symbol
  2454. visibility changes. */
  2455. if (bfd_link_pic (info))
  2456. {
  2457. if (SYMBOL_CALLS_LOCAL (info, h))
  2458. {
  2459. struct elf_dyn_relocs **pp;
  2460. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  2461. {
  2462. p->count -= p->pc_count;
  2463. p->pc_count = 0;
  2464. if (p->count == 0)
  2465. *pp = p->next;
  2466. else
  2467. pp = &p->next;
  2468. }
  2469. }
  2470. if (htab->root.target_os == is_vxworks)
  2471. {
  2472. struct elf_dyn_relocs **pp;
  2473. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  2474. {
  2475. if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
  2476. *pp = p->next;
  2477. else
  2478. pp = &p->next;
  2479. }
  2480. }
  2481. /* Also discard relocs on undefined weak syms with non-default
  2482. visibility. */
  2483. if (h->dyn_relocs != NULL
  2484. && h->root.type == bfd_link_hash_undefweak)
  2485. {
  2486. if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2487. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  2488. h->dyn_relocs = NULL;
  2489. /* Make sure undefined weak symbols are output as a dynamic
  2490. symbol in PIEs. */
  2491. else if (h->dynindx == -1
  2492. && !h->forced_local)
  2493. {
  2494. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2495. return false;
  2496. }
  2497. }
  2498. }
  2499. else
  2500. {
  2501. /* For the non-shared case, discard space for relocs against
  2502. symbols which turn out to need copy relocs or are not
  2503. dynamic. */
  2504. if (!h->non_got_ref
  2505. && ((h->def_dynamic
  2506. && !h->def_regular)
  2507. || (htab->root.dynamic_sections_created
  2508. && (h->root.type == bfd_link_hash_undefweak
  2509. || h->root.type == bfd_link_hash_undefined))))
  2510. {
  2511. /* Make sure this symbol is output as a dynamic symbol.
  2512. Undefined weak syms won't yet be marked as dynamic. */
  2513. if (h->dynindx == -1
  2514. && !h->forced_local)
  2515. {
  2516. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2517. return false;
  2518. }
  2519. /* If that succeeded, we know we'll be keeping all the
  2520. relocs. */
  2521. if (h->dynindx != -1)
  2522. goto keep;
  2523. }
  2524. h->dyn_relocs = NULL;
  2525. keep: ;
  2526. }
  2527. /* Finally, allocate space. */
  2528. for (p = h->dyn_relocs; p != NULL; p = p->next)
  2529. {
  2530. asection *sreloc = elf_section_data (p->sec)->sreloc;
  2531. sreloc->size += p->count * sizeof (Elf32_External_Rela);
  2532. /* If we need relocations, we do not need fixups. */
  2533. if (htab->fdpic_p && !bfd_link_pic (info))
  2534. htab->srofixup->size -= 4 * (p->count - p->pc_count);
  2535. }
  2536. return true;
  2537. }
  2538. /* This function is called after all the input files have been read,
  2539. and the input sections have been assigned to output sections.
  2540. It's a convenient place to determine the PLT style. */
  2541. static bool
  2542. sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
  2543. {
  2544. sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd,
  2545. bfd_link_pic (info));
  2546. if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_relocatable (info)
  2547. && !bfd_elf_stack_segment_size (output_bfd, info,
  2548. "__stacksize", DEFAULT_STACK_SIZE))
  2549. return false;
  2550. return true;
  2551. }
  2552. /* Set the sizes of the dynamic sections. */
  2553. static bool
  2554. sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  2555. struct bfd_link_info *info)
  2556. {
  2557. struct elf_sh_link_hash_table *htab;
  2558. bfd *dynobj;
  2559. asection *s;
  2560. bool relocs;
  2561. bfd *ibfd;
  2562. htab = sh_elf_hash_table (info);
  2563. if (htab == NULL)
  2564. return false;
  2565. dynobj = htab->root.dynobj;
  2566. BFD_ASSERT (dynobj != NULL);
  2567. if (htab->root.dynamic_sections_created)
  2568. {
  2569. /* Set the contents of the .interp section to the interpreter. */
  2570. if (bfd_link_executable (info) && !info->nointerp)
  2571. {
  2572. s = bfd_get_linker_section (dynobj, ".interp");
  2573. BFD_ASSERT (s != NULL);
  2574. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  2575. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  2576. }
  2577. }
  2578. /* Set up .got offsets for local syms, and space for local dynamic
  2579. relocs. */
  2580. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  2581. {
  2582. bfd_signed_vma *local_got;
  2583. bfd_signed_vma *end_local_got;
  2584. union gotref *local_funcdesc, *end_local_funcdesc;
  2585. char *local_got_type;
  2586. bfd_size_type locsymcount;
  2587. Elf_Internal_Shdr *symtab_hdr;
  2588. asection *srel;
  2589. if (! is_sh_elf (ibfd))
  2590. continue;
  2591. for (s = ibfd->sections; s != NULL; s = s->next)
  2592. {
  2593. struct elf_dyn_relocs *p;
  2594. for (p = ((struct elf_dyn_relocs *)
  2595. elf_section_data (s)->local_dynrel);
  2596. p != NULL;
  2597. p = p->next)
  2598. {
  2599. if (! bfd_is_abs_section (p->sec)
  2600. && bfd_is_abs_section (p->sec->output_section))
  2601. {
  2602. /* Input section has been discarded, either because
  2603. it is a copy of a linkonce section or due to
  2604. linker script /DISCARD/, so we'll be discarding
  2605. the relocs too. */
  2606. }
  2607. else if (htab->root.target_os == is_vxworks
  2608. && strcmp (p->sec->output_section->name,
  2609. ".tls_vars") == 0)
  2610. {
  2611. /* Relocations in vxworks .tls_vars sections are
  2612. handled specially by the loader. */
  2613. }
  2614. else if (p->count != 0)
  2615. {
  2616. srel = elf_section_data (p->sec)->sreloc;
  2617. srel->size += p->count * sizeof (Elf32_External_Rela);
  2618. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  2619. {
  2620. info->flags |= DF_TEXTREL;
  2621. info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
  2622. p->sec->owner, p->sec);
  2623. }
  2624. /* If we need relocations, we do not need fixups. */
  2625. if (htab->fdpic_p && !bfd_link_pic (info))
  2626. htab->srofixup->size -= 4 * (p->count - p->pc_count);
  2627. }
  2628. }
  2629. }
  2630. symtab_hdr = &elf_symtab_hdr (ibfd);
  2631. locsymcount = symtab_hdr->sh_info;
  2632. s = htab->root.sgot;
  2633. srel = htab->root.srelgot;
  2634. local_got = elf_local_got_refcounts (ibfd);
  2635. if (local_got)
  2636. {
  2637. end_local_got = local_got + locsymcount;
  2638. local_got_type = sh_elf_local_got_type (ibfd);
  2639. local_funcdesc = sh_elf_local_funcdesc (ibfd);
  2640. for (; local_got < end_local_got; ++local_got)
  2641. {
  2642. if (*local_got > 0)
  2643. {
  2644. *local_got = s->size;
  2645. s->size += 4;
  2646. if (*local_got_type == GOT_TLS_GD)
  2647. s->size += 4;
  2648. if (bfd_link_pic (info))
  2649. srel->size += sizeof (Elf32_External_Rela);
  2650. else
  2651. htab->srofixup->size += 4;
  2652. if (*local_got_type == GOT_FUNCDESC)
  2653. {
  2654. if (local_funcdesc == NULL)
  2655. {
  2656. bfd_size_type size;
  2657. size = locsymcount * sizeof (union gotref);
  2658. local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
  2659. size);
  2660. if (local_funcdesc == NULL)
  2661. return false;
  2662. sh_elf_local_funcdesc (ibfd) = local_funcdesc;
  2663. local_funcdesc += (local_got
  2664. - elf_local_got_refcounts (ibfd));
  2665. }
  2666. local_funcdesc->refcount++;
  2667. ++local_funcdesc;
  2668. }
  2669. }
  2670. else
  2671. *local_got = (bfd_vma) -1;
  2672. ++local_got_type;
  2673. }
  2674. }
  2675. local_funcdesc = sh_elf_local_funcdesc (ibfd);
  2676. if (local_funcdesc)
  2677. {
  2678. end_local_funcdesc = local_funcdesc + locsymcount;
  2679. for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
  2680. {
  2681. if (local_funcdesc->refcount > 0)
  2682. {
  2683. local_funcdesc->offset = htab->sfuncdesc->size;
  2684. htab->sfuncdesc->size += 8;
  2685. if (!bfd_link_pic (info))
  2686. htab->srofixup->size += 8;
  2687. else
  2688. htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
  2689. }
  2690. else
  2691. local_funcdesc->offset = MINUS_ONE;
  2692. }
  2693. }
  2694. }
  2695. if (htab->tls_ldm_got.refcount > 0)
  2696. {
  2697. /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
  2698. relocs. */
  2699. htab->tls_ldm_got.offset = htab->root.sgot->size;
  2700. htab->root.sgot->size += 8;
  2701. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  2702. }
  2703. else
  2704. htab->tls_ldm_got.offset = -1;
  2705. /* Only the reserved entries should be present. For FDPIC, they go at
  2706. the end of .got.plt. */
  2707. if (htab->fdpic_p)
  2708. {
  2709. BFD_ASSERT (htab->root.sgotplt && htab->root.sgotplt->size == 12);
  2710. htab->root.sgotplt->size = 0;
  2711. }
  2712. /* Allocate global sym .plt and .got entries, and space for global
  2713. sym dynamic relocs. */
  2714. elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
  2715. /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
  2716. end of the FDPIC .got.plt. */
  2717. if (htab->fdpic_p)
  2718. {
  2719. htab->root.hgot->root.u.def.value = htab->root.sgotplt->size;
  2720. htab->root.sgotplt->size += 12;
  2721. }
  2722. /* At the very end of the .rofixup section is a pointer to the GOT. */
  2723. if (htab->fdpic_p && htab->srofixup != NULL)
  2724. htab->srofixup->size += 4;
  2725. /* We now have determined the sizes of the various dynamic sections.
  2726. Allocate memory for them. */
  2727. relocs = false;
  2728. for (s = dynobj->sections; s != NULL; s = s->next)
  2729. {
  2730. if ((s->flags & SEC_LINKER_CREATED) == 0)
  2731. continue;
  2732. if (s == htab->root.splt
  2733. || s == htab->root.sgot
  2734. || s == htab->root.sgotplt
  2735. || s == htab->sfuncdesc
  2736. || s == htab->srofixup
  2737. || s == htab->root.sdynbss)
  2738. {
  2739. /* Strip this section if we don't need it; see the
  2740. comment below. */
  2741. }
  2742. else if (startswith (bfd_section_name (s), ".rela"))
  2743. {
  2744. if (s->size != 0 && s != htab->root.srelplt && s != htab->srelplt2)
  2745. relocs = true;
  2746. /* We use the reloc_count field as a counter if we need
  2747. to copy relocs into the output file. */
  2748. s->reloc_count = 0;
  2749. }
  2750. else
  2751. {
  2752. /* It's not one of our sections, so don't allocate space. */
  2753. continue;
  2754. }
  2755. if (s->size == 0)
  2756. {
  2757. /* If we don't need this section, strip it from the
  2758. output file. This is mostly to handle .rela.bss and
  2759. .rela.plt. We must create both sections in
  2760. create_dynamic_sections, because they must be created
  2761. before the linker maps input sections to output
  2762. sections. The linker does that before
  2763. adjust_dynamic_symbol is called, and it is that
  2764. function which decides whether anything needs to go
  2765. into these sections. */
  2766. s->flags |= SEC_EXCLUDE;
  2767. continue;
  2768. }
  2769. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  2770. continue;
  2771. /* Allocate memory for the section contents. We use bfd_zalloc
  2772. here in case unused entries are not reclaimed before the
  2773. section's contents are written out. This should not happen,
  2774. but this way if it does, we get a R_SH_NONE reloc instead
  2775. of garbage. */
  2776. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  2777. if (s->contents == NULL)
  2778. return false;
  2779. }
  2780. return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
  2781. relocs);
  2782. }
  2783. /* Add a dynamic relocation to the SRELOC section. */
  2784. inline static bfd_vma
  2785. sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
  2786. int reloc_type, long dynindx, bfd_vma addend)
  2787. {
  2788. Elf_Internal_Rela outrel;
  2789. bfd_vma reloc_offset;
  2790. outrel.r_offset = offset;
  2791. outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
  2792. outrel.r_addend = addend;
  2793. reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
  2794. BFD_ASSERT (reloc_offset < sreloc->size);
  2795. bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  2796. sreloc->contents + reloc_offset);
  2797. sreloc->reloc_count++;
  2798. return reloc_offset;
  2799. }
  2800. /* Add an FDPIC read-only fixup. */
  2801. inline static void
  2802. sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
  2803. {
  2804. bfd_vma fixup_offset;
  2805. fixup_offset = srofixup->reloc_count++ * 4;
  2806. BFD_ASSERT (fixup_offset < srofixup->size);
  2807. bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
  2808. }
  2809. /* Return the offset of the generated .got section from the
  2810. _GLOBAL_OFFSET_TABLE_ symbol. */
  2811. static bfd_signed_vma
  2812. sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
  2813. {
  2814. return (htab->root.sgot->output_offset - htab->root.sgotplt->output_offset
  2815. - htab->root.hgot->root.u.def.value);
  2816. }
  2817. /* Find the segment number in which OSEC, and output section, is
  2818. located. */
  2819. static unsigned
  2820. sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
  2821. {
  2822. Elf_Internal_Phdr *p = NULL;
  2823. if (output_bfd->xvec->flavour == bfd_target_elf_flavour
  2824. /* PR ld/17110: Do not look for output segments in an input bfd. */
  2825. && output_bfd->direction != read_direction)
  2826. p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
  2827. /* FIXME: Nothing ever says what this index is relative to. The kernel
  2828. supplies data in terms of the number of load segments but this is
  2829. a phdr index and the first phdr may not be a load segment. */
  2830. return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
  2831. }
  2832. static bool
  2833. sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
  2834. {
  2835. unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
  2836. return (seg != (unsigned) -1
  2837. && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
  2838. }
  2839. /* Generate the initial contents of a local function descriptor, along
  2840. with any relocations or fixups required. */
  2841. static bool
  2842. sh_elf_initialize_funcdesc (bfd *output_bfd,
  2843. struct bfd_link_info *info,
  2844. struct elf_link_hash_entry *h,
  2845. bfd_vma offset,
  2846. asection *section,
  2847. bfd_vma value)
  2848. {
  2849. struct elf_sh_link_hash_table *htab;
  2850. int dynindx;
  2851. bfd_vma addr, seg;
  2852. htab = sh_elf_hash_table (info);
  2853. /* FIXME: The ABI says that the offset to the function goes in the
  2854. descriptor, along with the segment index. We're RELA, so it could
  2855. go in the reloc instead... */
  2856. if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
  2857. {
  2858. section = h->root.u.def.section;
  2859. value = h->root.u.def.value;
  2860. }
  2861. if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
  2862. {
  2863. dynindx = elf_section_data (section->output_section)->dynindx;
  2864. addr = value + section->output_offset;
  2865. seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
  2866. }
  2867. else
  2868. {
  2869. BFD_ASSERT (h->dynindx != -1);
  2870. dynindx = h->dynindx;
  2871. addr = seg = 0;
  2872. }
  2873. if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
  2874. {
  2875. if (h == NULL || h->root.type != bfd_link_hash_undefweak)
  2876. {
  2877. sh_elf_add_rofixup (output_bfd, htab->srofixup,
  2878. offset
  2879. + htab->sfuncdesc->output_section->vma
  2880. + htab->sfuncdesc->output_offset);
  2881. sh_elf_add_rofixup (output_bfd, htab->srofixup,
  2882. offset + 4
  2883. + htab->sfuncdesc->output_section->vma
  2884. + htab->sfuncdesc->output_offset);
  2885. }
  2886. /* There are no dynamic relocations so fill in the final
  2887. address and gp value (barring fixups). */
  2888. addr += section->output_section->vma;
  2889. seg = htab->root.hgot->root.u.def.value
  2890. + htab->root.hgot->root.u.def.section->output_section->vma
  2891. + htab->root.hgot->root.u.def.section->output_offset;
  2892. }
  2893. else
  2894. sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
  2895. offset
  2896. + htab->sfuncdesc->output_section->vma
  2897. + htab->sfuncdesc->output_offset,
  2898. R_SH_FUNCDESC_VALUE, dynindx, 0);
  2899. bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
  2900. bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
  2901. return true;
  2902. }
  2903. /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
  2904. VALUE is the field's value. Return bfd_reloc_ok if successful or an error
  2905. otherwise. */
  2906. static bfd_reloc_status_type
  2907. install_movi20_field (bfd *output_bfd, unsigned long relocation,
  2908. bfd *input_bfd, asection *input_section,
  2909. bfd_byte *contents, bfd_vma offset)
  2910. {
  2911. unsigned long cur_val;
  2912. bfd_byte *addr;
  2913. bfd_reloc_status_type r;
  2914. if (offset > bfd_get_section_limit (input_bfd, input_section))
  2915. return bfd_reloc_outofrange;
  2916. r = bfd_check_overflow (complain_overflow_signed, 20, 0,
  2917. bfd_arch_bits_per_address (input_bfd), relocation);
  2918. if (r != bfd_reloc_ok)
  2919. return r;
  2920. addr = contents + offset;
  2921. cur_val = bfd_get_16 (output_bfd, addr);
  2922. bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
  2923. bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
  2924. return bfd_reloc_ok;
  2925. }
  2926. /* Relocate an SH ELF section. */
  2927. static int
  2928. sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
  2929. bfd *input_bfd, asection *input_section,
  2930. bfd_byte *contents, Elf_Internal_Rela *relocs,
  2931. Elf_Internal_Sym *local_syms,
  2932. asection **local_sections)
  2933. {
  2934. struct elf_sh_link_hash_table *htab;
  2935. Elf_Internal_Shdr *symtab_hdr;
  2936. struct elf_link_hash_entry **sym_hashes;
  2937. Elf_Internal_Rela *rel, *relend;
  2938. bfd_vma *local_got_offsets;
  2939. asection *sgot = NULL;
  2940. asection *sgotplt = NULL;
  2941. asection *splt = NULL;
  2942. asection *sreloc = NULL;
  2943. asection *srelgot = NULL;
  2944. bool is_vxworks_tls;
  2945. unsigned isec_segment, got_segment, plt_segment, check_segment[2];
  2946. bool fdpic_p = false;
  2947. if (!is_sh_elf (input_bfd))
  2948. {
  2949. bfd_set_error (bfd_error_wrong_format);
  2950. return false;
  2951. }
  2952. htab = sh_elf_hash_table (info);
  2953. if (htab != NULL)
  2954. {
  2955. sgot = htab->root.sgot;
  2956. sgotplt = htab->root.sgotplt;
  2957. srelgot = htab->root.srelgot;
  2958. splt = htab->root.splt;
  2959. fdpic_p = htab->fdpic_p;
  2960. }
  2961. symtab_hdr = &elf_symtab_hdr (input_bfd);
  2962. sym_hashes = elf_sym_hashes (input_bfd);
  2963. local_got_offsets = elf_local_got_offsets (input_bfd);
  2964. isec_segment = sh_elf_osec_to_segment (output_bfd,
  2965. input_section->output_section);
  2966. if (fdpic_p && sgot)
  2967. got_segment = sh_elf_osec_to_segment (output_bfd,
  2968. sgot->output_section);
  2969. else
  2970. got_segment = -1;
  2971. if (fdpic_p && splt)
  2972. plt_segment = sh_elf_osec_to_segment (output_bfd,
  2973. splt->output_section);
  2974. else
  2975. plt_segment = -1;
  2976. /* We have to handle relocations in vxworks .tls_vars sections
  2977. specially, because the dynamic loader is 'weird'. */
  2978. is_vxworks_tls = (htab && htab->root.target_os == is_vxworks && bfd_link_pic (info)
  2979. && !strcmp (input_section->output_section->name,
  2980. ".tls_vars"));
  2981. rel = relocs;
  2982. relend = relocs + input_section->reloc_count;
  2983. for (; rel < relend; rel++)
  2984. {
  2985. int r_type;
  2986. reloc_howto_type *howto;
  2987. unsigned long r_symndx;
  2988. Elf_Internal_Sym *sym;
  2989. asection *sec;
  2990. struct elf_link_hash_entry *h;
  2991. bfd_vma relocation;
  2992. bfd_vma addend = (bfd_vma) 0;
  2993. bfd_reloc_status_type r;
  2994. bfd_vma off;
  2995. enum got_type got_type;
  2996. const char *symname = NULL;
  2997. bool resolved_to_zero;
  2998. r_symndx = ELF32_R_SYM (rel->r_info);
  2999. r_type = ELF32_R_TYPE (rel->r_info);
  3000. /* Many of the relocs are only used for relaxing, and are
  3001. handled entirely by the relaxation code. */
  3002. if (r_type >= (int) R_SH_GNU_VTINHERIT
  3003. && r_type <= (int) R_SH_LABEL)
  3004. continue;
  3005. if (r_type == (int) R_SH_NONE)
  3006. continue;
  3007. if (r_type < 0
  3008. || r_type >= R_SH_max
  3009. || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
  3010. && r_type <= (int) R_SH_LAST_INVALID_RELOC)
  3011. || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
  3012. && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
  3013. || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
  3014. && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
  3015. || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
  3016. && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
  3017. || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
  3018. && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
  3019. || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
  3020. && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
  3021. {
  3022. bfd_set_error (bfd_error_bad_value);
  3023. return false;
  3024. }
  3025. howto = get_howto_table (output_bfd) + r_type;
  3026. /* For relocs that aren't partial_inplace, we get the addend from
  3027. the relocation. */
  3028. if (! howto->partial_inplace)
  3029. addend = rel->r_addend;
  3030. resolved_to_zero = false;
  3031. h = NULL;
  3032. sym = NULL;
  3033. sec = NULL;
  3034. check_segment[0] = -1;
  3035. check_segment[1] = -1;
  3036. if (r_symndx < symtab_hdr->sh_info)
  3037. {
  3038. sym = local_syms + r_symndx;
  3039. sec = local_sections[r_symndx];
  3040. symname = bfd_elf_string_from_elf_section
  3041. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  3042. if (symname == NULL || *symname == '\0')
  3043. symname = bfd_section_name (sec);
  3044. relocation = (sec->output_section->vma
  3045. + sec->output_offset
  3046. + sym->st_value);
  3047. if (sec != NULL && discarded_section (sec))
  3048. /* Handled below. */
  3049. ;
  3050. else if (bfd_link_relocatable (info))
  3051. {
  3052. /* This is a relocatable link. We don't have to change
  3053. anything, unless the reloc is against a section symbol,
  3054. in which case we have to adjust according to where the
  3055. section symbol winds up in the output section. */
  3056. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  3057. {
  3058. if (! howto->partial_inplace)
  3059. {
  3060. /* For relocations with the addend in the
  3061. relocation, we need just to update the addend.
  3062. All real relocs are of type partial_inplace; this
  3063. code is mostly for completeness. */
  3064. rel->r_addend += sec->output_offset;
  3065. continue;
  3066. }
  3067. /* Relocs of type partial_inplace need to pick up the
  3068. contents in the contents and add the offset resulting
  3069. from the changed location of the section symbol.
  3070. Using _bfd_final_link_relocate (e.g. goto
  3071. final_link_relocate) here would be wrong, because
  3072. relocations marked pc_relative would get the current
  3073. location subtracted, and we must only do that at the
  3074. final link. */
  3075. r = _bfd_relocate_contents (howto, input_bfd,
  3076. sec->output_offset
  3077. + sym->st_value,
  3078. contents + rel->r_offset);
  3079. goto relocation_done;
  3080. }
  3081. continue;
  3082. }
  3083. else if (! howto->partial_inplace)
  3084. {
  3085. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  3086. addend = rel->r_addend;
  3087. }
  3088. else if ((sec->flags & SEC_MERGE)
  3089. && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  3090. {
  3091. asection *msec;
  3092. if (howto->rightshift || howto->src_mask != 0xffffffff)
  3093. {
  3094. _bfd_error_handler
  3095. /* xgettext:c-format */
  3096. (_("%pB(%pA+%#" PRIx64 "): "
  3097. "%s relocation against SEC_MERGE section"),
  3098. input_bfd, input_section,
  3099. (uint64_t) rel->r_offset, howto->name);
  3100. return false;
  3101. }
  3102. addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3103. msec = sec;
  3104. addend =
  3105. _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
  3106. - relocation;
  3107. addend += msec->output_section->vma + msec->output_offset;
  3108. bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
  3109. addend = 0;
  3110. }
  3111. }
  3112. else
  3113. {
  3114. /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
  3115. relocation = 0;
  3116. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  3117. symname = h->root.root.string;
  3118. while (h->root.type == bfd_link_hash_indirect
  3119. || h->root.type == bfd_link_hash_warning)
  3120. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  3121. if (h->root.type == bfd_link_hash_defined
  3122. || h->root.type == bfd_link_hash_defweak)
  3123. {
  3124. bool dyn;
  3125. dyn = htab ? htab->root.dynamic_sections_created : false;
  3126. sec = h->root.u.def.section;
  3127. /* In these cases, we don't need the relocation value.
  3128. We check specially because in some obscure cases
  3129. sec->output_section will be NULL. */
  3130. if (r_type == R_SH_GOTPC
  3131. || r_type == R_SH_GOTPC_LOW16
  3132. || r_type == R_SH_GOTPC_MEDLOW16
  3133. || r_type == R_SH_GOTPC_MEDHI16
  3134. || r_type == R_SH_GOTPC_HI16
  3135. || ((r_type == R_SH_PLT32
  3136. || r_type == R_SH_PLT_LOW16
  3137. || r_type == R_SH_PLT_MEDLOW16
  3138. || r_type == R_SH_PLT_MEDHI16
  3139. || r_type == R_SH_PLT_HI16)
  3140. && h->plt.offset != (bfd_vma) -1)
  3141. || ((r_type == R_SH_GOT32
  3142. || r_type == R_SH_GOT20
  3143. || r_type == R_SH_GOTFUNCDESC
  3144. || r_type == R_SH_GOTFUNCDESC20
  3145. || r_type == R_SH_GOTOFFFUNCDESC
  3146. || r_type == R_SH_GOTOFFFUNCDESC20
  3147. || r_type == R_SH_FUNCDESC
  3148. || r_type == R_SH_GOT_LOW16
  3149. || r_type == R_SH_GOT_MEDLOW16
  3150. || r_type == R_SH_GOT_MEDHI16
  3151. || r_type == R_SH_GOT_HI16)
  3152. && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  3153. bfd_link_pic (info),
  3154. h)
  3155. && (! bfd_link_pic (info)
  3156. || (! info->symbolic && h->dynindx != -1)
  3157. || !h->def_regular))
  3158. /* The cases above are those in which relocation is
  3159. overwritten in the switch block below. The cases
  3160. below are those in which we must defer relocation
  3161. to run-time, because we can't resolve absolute
  3162. addresses when creating a shared library. */
  3163. || (bfd_link_pic (info)
  3164. && ((! info->symbolic && h->dynindx != -1)
  3165. || !h->def_regular)
  3166. && ((r_type == R_SH_DIR32
  3167. && !h->forced_local)
  3168. || (r_type == R_SH_REL32
  3169. && !SYMBOL_CALLS_LOCAL (info, h)))
  3170. && ((input_section->flags & SEC_ALLOC) != 0
  3171. /* DWARF will emit R_SH_DIR32 relocations in its
  3172. sections against symbols defined externally
  3173. in shared libraries. We can't do anything
  3174. with them here. */
  3175. || ((input_section->flags & SEC_DEBUGGING) != 0
  3176. && h->def_dynamic)))
  3177. /* Dynamic relocs are not propagated for SEC_DEBUGGING
  3178. sections because such sections are not SEC_ALLOC and
  3179. thus ld.so will not process them. */
  3180. || (sec->output_section == NULL
  3181. && ((input_section->flags & SEC_DEBUGGING) != 0
  3182. && h->def_dynamic))
  3183. || (sec->output_section == NULL
  3184. && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
  3185. || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
  3186. ;
  3187. else if (sec->output_section != NULL)
  3188. relocation = (h->root.u.def.value
  3189. + sec->output_section->vma
  3190. + sec->output_offset);
  3191. else if (!bfd_link_relocatable (info)
  3192. && (_bfd_elf_section_offset (output_bfd, info,
  3193. input_section,
  3194. rel->r_offset)
  3195. != (bfd_vma) -1))
  3196. {
  3197. _bfd_error_handler
  3198. /* xgettext:c-format */
  3199. (_("%pB(%pA+%#" PRIx64 "): "
  3200. "unresolvable %s relocation against symbol `%s'"),
  3201. input_bfd,
  3202. input_section,
  3203. (uint64_t) rel->r_offset,
  3204. howto->name,
  3205. h->root.root.string);
  3206. return false;
  3207. }
  3208. }
  3209. else if (h->root.type == bfd_link_hash_undefweak)
  3210. resolved_to_zero = UNDEFWEAK_NO_DYNAMIC_RELOC (info, h);
  3211. else if (info->unresolved_syms_in_objects == RM_IGNORE
  3212. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  3213. ;
  3214. else if (!bfd_link_relocatable (info))
  3215. info->callbacks->undefined_symbol
  3216. (info, h->root.root.string, input_bfd, input_section,
  3217. rel->r_offset,
  3218. (info->unresolved_syms_in_objects == RM_DIAGNOSE
  3219. && !info->warn_unresolved_syms)
  3220. || ELF_ST_VISIBILITY (h->other));
  3221. }
  3222. if (sec != NULL && discarded_section (sec))
  3223. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3224. rel, 1, relend, howto, 0, contents);
  3225. if (bfd_link_relocatable (info))
  3226. continue;
  3227. /* Check for inter-segment relocations in FDPIC files. Most
  3228. relocations connect the relocation site to the location of
  3229. the target symbol, but there are some exceptions below. */
  3230. check_segment[0] = isec_segment;
  3231. if (sec != NULL)
  3232. check_segment[1] = sh_elf_osec_to_segment (output_bfd,
  3233. sec->output_section);
  3234. else
  3235. check_segment[1] = -1;
  3236. switch ((int) r_type)
  3237. {
  3238. final_link_relocate:
  3239. /* COFF relocs don't use the addend. The addend is used for
  3240. R_SH_DIR32 to be compatible with other compilers. */
  3241. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  3242. contents, rel->r_offset,
  3243. relocation, addend);
  3244. break;
  3245. case R_SH_IND12W:
  3246. goto final_link_relocate;
  3247. case R_SH_DIR8WPN:
  3248. case R_SH_DIR8WPZ:
  3249. case R_SH_DIR8WPL:
  3250. /* If the reloc is against the start of this section, then
  3251. the assembler has already taken care of it and the reloc
  3252. is here only to assist in relaxing. If the reloc is not
  3253. against the start of this section, then it's against an
  3254. external symbol and we must deal with it ourselves. */
  3255. if (input_section->output_section->vma + input_section->output_offset
  3256. != relocation)
  3257. {
  3258. int disp = (relocation
  3259. - input_section->output_section->vma
  3260. - input_section->output_offset
  3261. - rel->r_offset);
  3262. int mask = 0;
  3263. switch (r_type)
  3264. {
  3265. case R_SH_DIR8WPN:
  3266. case R_SH_DIR8WPZ: mask = 1; break;
  3267. case R_SH_DIR8WPL: mask = 3; break;
  3268. default: mask = 0; break;
  3269. }
  3270. if (disp & mask)
  3271. {
  3272. _bfd_error_handler
  3273. /* xgettext:c-format */
  3274. (_("%pB: %#" PRIx64 ": fatal: "
  3275. "unaligned branch target for relax-support relocation"),
  3276. input_section->owner,
  3277. (uint64_t) rel->r_offset);
  3278. bfd_set_error (bfd_error_bad_value);
  3279. return false;
  3280. }
  3281. relocation -= 4;
  3282. goto final_link_relocate;
  3283. }
  3284. r = bfd_reloc_ok;
  3285. break;
  3286. default:
  3287. bfd_set_error (bfd_error_bad_value);
  3288. return false;
  3289. case R_SH_DIR16:
  3290. case R_SH_DIR8:
  3291. case R_SH_DIR8U:
  3292. case R_SH_DIR8S:
  3293. case R_SH_DIR4U:
  3294. goto final_link_relocate;
  3295. case R_SH_DIR8UL:
  3296. case R_SH_DIR4UL:
  3297. if (relocation & 3)
  3298. {
  3299. _bfd_error_handler
  3300. /* xgettext:c-format */
  3301. (_("%pB: %#" PRIx64 ": fatal: "
  3302. "unaligned %s relocation %#" PRIx64),
  3303. input_section->owner, (uint64_t) rel->r_offset,
  3304. howto->name, (uint64_t) relocation);
  3305. bfd_set_error (bfd_error_bad_value);
  3306. return false;
  3307. }
  3308. goto final_link_relocate;
  3309. case R_SH_DIR8UW:
  3310. case R_SH_DIR8SW:
  3311. case R_SH_DIR4UW:
  3312. if (relocation & 1)
  3313. {
  3314. _bfd_error_handler
  3315. /* xgettext:c-format */
  3316. (_("%pB: %#" PRIx64 ": fatal: "
  3317. "unaligned %s relocation %#" PRIx64 ""),
  3318. input_section->owner,
  3319. (uint64_t) rel->r_offset, howto->name,
  3320. (uint64_t) relocation);
  3321. bfd_set_error (bfd_error_bad_value);
  3322. return false;
  3323. }
  3324. goto final_link_relocate;
  3325. case R_SH_PSHA:
  3326. if ((signed int)relocation < -32
  3327. || (signed int)relocation > 32)
  3328. {
  3329. _bfd_error_handler
  3330. /* xgettext:c-format */
  3331. (_("%pB: %#" PRIx64 ": fatal: R_SH_PSHA relocation %" PRId64
  3332. " not in range -32..32"),
  3333. input_section->owner,
  3334. (uint64_t) rel->r_offset,
  3335. (int64_t) relocation);
  3336. bfd_set_error (bfd_error_bad_value);
  3337. return false;
  3338. }
  3339. goto final_link_relocate;
  3340. case R_SH_PSHL:
  3341. if ((signed int)relocation < -16
  3342. || (signed int)relocation > 16)
  3343. {
  3344. _bfd_error_handler
  3345. /* xgettext:c-format */
  3346. (_("%pB: %#" PRIx64 ": fatal: R_SH_PSHL relocation %" PRId64
  3347. " not in range -32..32"),
  3348. input_section->owner,
  3349. (uint64_t) rel->r_offset,
  3350. (int64_t) relocation);
  3351. bfd_set_error (bfd_error_bad_value);
  3352. return false;
  3353. }
  3354. goto final_link_relocate;
  3355. case R_SH_DIR32:
  3356. case R_SH_REL32:
  3357. if (bfd_link_pic (info)
  3358. && (h == NULL
  3359. || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  3360. && !resolved_to_zero)
  3361. || h->root.type != bfd_link_hash_undefweak)
  3362. && r_symndx != STN_UNDEF
  3363. && (input_section->flags & SEC_ALLOC) != 0
  3364. && !is_vxworks_tls
  3365. && (r_type == R_SH_DIR32
  3366. || !SYMBOL_CALLS_LOCAL (info, h)))
  3367. {
  3368. Elf_Internal_Rela outrel;
  3369. bfd_byte *loc;
  3370. bool skip, relocate;
  3371. /* When generating a shared object, these relocations
  3372. are copied into the output file to be resolved at run
  3373. time. */
  3374. if (sreloc == NULL)
  3375. {
  3376. sreloc = _bfd_elf_get_dynamic_reloc_section
  3377. (input_bfd, input_section, /*rela?*/ true);
  3378. if (sreloc == NULL)
  3379. return false;
  3380. }
  3381. skip = false;
  3382. relocate = false;
  3383. outrel.r_offset =
  3384. _bfd_elf_section_offset (output_bfd, info, input_section,
  3385. rel->r_offset);
  3386. if (outrel.r_offset == (bfd_vma) -1)
  3387. skip = true;
  3388. else if (outrel.r_offset == (bfd_vma) -2)
  3389. skip = true, relocate = true;
  3390. outrel.r_offset += (input_section->output_section->vma
  3391. + input_section->output_offset);
  3392. if (skip)
  3393. memset (&outrel, 0, sizeof outrel);
  3394. else if (r_type == R_SH_REL32)
  3395. {
  3396. BFD_ASSERT (h != NULL && h->dynindx != -1);
  3397. outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
  3398. outrel.r_addend
  3399. = (howto->partial_inplace
  3400. ? bfd_get_32 (input_bfd, contents + rel->r_offset)
  3401. : addend);
  3402. }
  3403. else if (fdpic_p
  3404. && (h == NULL
  3405. || ((info->symbolic || h->dynindx == -1)
  3406. && h->def_regular)))
  3407. {
  3408. int dynindx;
  3409. BFD_ASSERT (sec != NULL);
  3410. BFD_ASSERT (sec->output_section != NULL);
  3411. dynindx = elf_section_data (sec->output_section)->dynindx;
  3412. outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
  3413. outrel.r_addend = relocation;
  3414. outrel.r_addend
  3415. += (howto->partial_inplace
  3416. ? bfd_get_32 (input_bfd, contents + rel->r_offset)
  3417. : addend);
  3418. outrel.r_addend -= sec->output_section->vma;
  3419. }
  3420. else
  3421. {
  3422. /* h->dynindx may be -1 if this symbol was marked to
  3423. become local. */
  3424. if (h == NULL
  3425. || ((info->symbolic || h->dynindx == -1)
  3426. && h->def_regular))
  3427. {
  3428. relocate = howto->partial_inplace;
  3429. outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
  3430. }
  3431. else
  3432. {
  3433. BFD_ASSERT (h->dynindx != -1);
  3434. outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
  3435. }
  3436. outrel.r_addend = relocation;
  3437. outrel.r_addend
  3438. += (howto->partial_inplace
  3439. ? bfd_get_32 (input_bfd, contents + rel->r_offset)
  3440. : addend);
  3441. }
  3442. loc = sreloc->contents;
  3443. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  3444. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3445. check_segment[0] = check_segment[1] = -1;
  3446. /* If this reloc is against an external symbol, we do
  3447. not want to fiddle with the addend. Otherwise, we
  3448. need to include the symbol value so that it becomes
  3449. an addend for the dynamic reloc. */
  3450. if (! relocate)
  3451. continue;
  3452. }
  3453. else if (fdpic_p && !bfd_link_pic (info)
  3454. && r_type == R_SH_DIR32
  3455. && (input_section->flags & SEC_ALLOC) != 0)
  3456. {
  3457. bfd_vma offset;
  3458. BFD_ASSERT (htab);
  3459. if (sh_elf_osec_readonly_p (output_bfd,
  3460. input_section->output_section))
  3461. {
  3462. _bfd_error_handler
  3463. /* xgettext:c-format */
  3464. (_("%pB(%pA+%#" PRIx64 "): "
  3465. "cannot emit fixup to `%s' in read-only section"),
  3466. input_bfd,
  3467. input_section,
  3468. (uint64_t) rel->r_offset,
  3469. symname);
  3470. return false;
  3471. }
  3472. offset = _bfd_elf_section_offset (output_bfd, info,
  3473. input_section, rel->r_offset);
  3474. if (offset != (bfd_vma)-1)
  3475. sh_elf_add_rofixup (output_bfd, htab->srofixup,
  3476. input_section->output_section->vma
  3477. + input_section->output_offset
  3478. + rel->r_offset);
  3479. check_segment[0] = check_segment[1] = -1;
  3480. }
  3481. /* We don't want warnings for non-NULL tests on undefined weak
  3482. symbols. */
  3483. else if (r_type == R_SH_REL32
  3484. && h
  3485. && h->root.type == bfd_link_hash_undefweak)
  3486. check_segment[0] = check_segment[1] = -1;
  3487. goto final_link_relocate;
  3488. case R_SH_GOTPLT32:
  3489. /* Relocation is to the entry for this symbol in the
  3490. procedure linkage table. */
  3491. if (h == NULL
  3492. || h->forced_local
  3493. || ! bfd_link_pic (info)
  3494. || info->symbolic
  3495. || h->dynindx == -1
  3496. || h->plt.offset == (bfd_vma) -1
  3497. || h->got.offset != (bfd_vma) -1)
  3498. goto force_got;
  3499. /* Relocation is to the entry for this symbol in the global
  3500. offset table extension for the procedure linkage table. */
  3501. BFD_ASSERT (htab);
  3502. BFD_ASSERT (sgotplt != NULL);
  3503. relocation = (sgotplt->output_offset
  3504. + (get_plt_index (htab->plt_info, h->plt.offset)
  3505. + 3) * 4);
  3506. #ifdef GOT_BIAS
  3507. relocation -= GOT_BIAS;
  3508. #endif
  3509. goto final_link_relocate;
  3510. force_got:
  3511. case R_SH_GOT32:
  3512. case R_SH_GOT20:
  3513. /* Relocation is to the entry for this symbol in the global
  3514. offset table. */
  3515. BFD_ASSERT (htab);
  3516. BFD_ASSERT (sgot != NULL);
  3517. check_segment[0] = check_segment[1] = -1;
  3518. if (h != NULL)
  3519. {
  3520. bool dyn;
  3521. off = h->got.offset;
  3522. BFD_ASSERT (off != (bfd_vma) -1);
  3523. dyn = htab->root.dynamic_sections_created;
  3524. if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  3525. bfd_link_pic (info),
  3526. h)
  3527. || (bfd_link_pic (info)
  3528. && SYMBOL_REFERENCES_LOCAL (info, h))
  3529. || ((ELF_ST_VISIBILITY (h->other)
  3530. || resolved_to_zero)
  3531. && h->root.type == bfd_link_hash_undefweak))
  3532. {
  3533. /* This is actually a static link, or it is a
  3534. -Bsymbolic link and the symbol is defined
  3535. locally, or the symbol was forced to be local
  3536. because of a version file. We must initialize
  3537. this entry in the global offset table. Since the
  3538. offset must always be a multiple of 4, we use the
  3539. least significant bit to record whether we have
  3540. initialized it already.
  3541. When doing a dynamic link, we create a .rela.got
  3542. relocation entry to initialize the value. This
  3543. is done in the finish_dynamic_symbol routine. */
  3544. if ((off & 1) != 0)
  3545. off &= ~1;
  3546. else
  3547. {
  3548. bfd_put_32 (output_bfd, relocation,
  3549. sgot->contents + off);
  3550. h->got.offset |= 1;
  3551. /* If we initialize the GOT entry here with a valid
  3552. symbol address, also add a fixup. */
  3553. if (fdpic_p && !bfd_link_pic (info)
  3554. && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
  3555. && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  3556. || h->root.type != bfd_link_hash_undefweak))
  3557. sh_elf_add_rofixup (output_bfd, htab->srofixup,
  3558. sgot->output_section->vma
  3559. + sgot->output_offset
  3560. + off);
  3561. }
  3562. }
  3563. relocation = sh_elf_got_offset (htab) + off;
  3564. }
  3565. else
  3566. {
  3567. BFD_ASSERT (local_got_offsets != NULL
  3568. && local_got_offsets[r_symndx] != (bfd_vma) -1);
  3569. off = local_got_offsets[r_symndx];
  3570. /* The offset must always be a multiple of 4. We use
  3571. the least significant bit to record whether we have
  3572. already generated the necessary reloc. */
  3573. if ((off & 1) != 0)
  3574. off &= ~1;
  3575. else
  3576. {
  3577. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  3578. if (bfd_link_pic (info))
  3579. {
  3580. Elf_Internal_Rela outrel;
  3581. bfd_byte *loc;
  3582. outrel.r_offset = (sgot->output_section->vma
  3583. + sgot->output_offset
  3584. + off);
  3585. if (fdpic_p)
  3586. {
  3587. int dynindx
  3588. = elf_section_data (sec->output_section)->dynindx;
  3589. outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
  3590. outrel.r_addend = relocation;
  3591. outrel.r_addend -= sec->output_section->vma;
  3592. }
  3593. else
  3594. {
  3595. outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
  3596. outrel.r_addend = relocation;
  3597. }
  3598. loc = srelgot->contents;
  3599. loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  3600. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3601. }
  3602. else if (fdpic_p
  3603. && (sh_elf_local_got_type (input_bfd) [r_symndx]
  3604. == GOT_NORMAL))
  3605. sh_elf_add_rofixup (output_bfd, htab->srofixup,
  3606. sgot->output_section->vma
  3607. + sgot->output_offset
  3608. + off);
  3609. local_got_offsets[r_symndx] |= 1;
  3610. }
  3611. relocation = sh_elf_got_offset (htab) + off;
  3612. }
  3613. #ifdef GOT_BIAS
  3614. relocation -= GOT_BIAS;
  3615. #endif
  3616. if (r_type == R_SH_GOT20)
  3617. {
  3618. r = install_movi20_field (output_bfd, relocation + addend,
  3619. input_bfd, input_section, contents,
  3620. rel->r_offset);
  3621. break;
  3622. }
  3623. else
  3624. goto final_link_relocate;
  3625. case R_SH_GOTOFF:
  3626. case R_SH_GOTOFF20:
  3627. /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
  3628. we place at the start of the .got.plt section. This is the same
  3629. as the start of the output .got section, unless there are function
  3630. descriptors in front of it. */
  3631. BFD_ASSERT (htab);
  3632. BFD_ASSERT (sgotplt != NULL);
  3633. check_segment[0] = got_segment;
  3634. relocation -= sgotplt->output_section->vma + sgotplt->output_offset
  3635. + htab->root.hgot->root.u.def.value;
  3636. #ifdef GOT_BIAS
  3637. relocation -= GOT_BIAS;
  3638. #endif
  3639. addend = rel->r_addend;
  3640. if (r_type == R_SH_GOTOFF20)
  3641. {
  3642. r = install_movi20_field (output_bfd, relocation + addend,
  3643. input_bfd, input_section, contents,
  3644. rel->r_offset);
  3645. break;
  3646. }
  3647. else
  3648. goto final_link_relocate;
  3649. case R_SH_GOTPC:
  3650. /* Use global offset table as symbol value. */
  3651. BFD_ASSERT (sgotplt != NULL);
  3652. relocation = sgotplt->output_section->vma + sgotplt->output_offset;
  3653. #ifdef GOT_BIAS
  3654. relocation += GOT_BIAS;
  3655. #endif
  3656. addend = rel->r_addend;
  3657. goto final_link_relocate;
  3658. case R_SH_PLT32:
  3659. /* Relocation is to the entry for this symbol in the
  3660. procedure linkage table. */
  3661. /* Resolve a PLT reloc against a local symbol directly,
  3662. without using the procedure linkage table. */
  3663. if (h == NULL)
  3664. goto final_link_relocate;
  3665. /* We don't want to warn on calls to undefined weak symbols,
  3666. as calls to them must be protected by non-NULL tests
  3667. anyway, and unprotected calls would invoke undefined
  3668. behavior. */
  3669. if (h->root.type == bfd_link_hash_undefweak)
  3670. check_segment[0] = check_segment[1] = -1;
  3671. if (h->forced_local)
  3672. goto final_link_relocate;
  3673. if (h->plt.offset == (bfd_vma) -1)
  3674. {
  3675. /* We didn't make a PLT entry for this symbol. This
  3676. happens when statically linking PIC code, or when
  3677. using -Bsymbolic. */
  3678. goto final_link_relocate;
  3679. }
  3680. BFD_ASSERT (splt != NULL);
  3681. check_segment[1] = plt_segment;
  3682. relocation = (splt->output_section->vma
  3683. + splt->output_offset
  3684. + h->plt.offset);
  3685. addend = rel->r_addend;
  3686. goto final_link_relocate;
  3687. /* Relocation is to the canonical function descriptor for this
  3688. symbol, possibly via the GOT. Initialize the GOT
  3689. entry and function descriptor if necessary. */
  3690. case R_SH_GOTFUNCDESC:
  3691. case R_SH_GOTFUNCDESC20:
  3692. case R_SH_FUNCDESC:
  3693. {
  3694. int dynindx = -1;
  3695. asection *reloc_section;
  3696. bfd_vma reloc_offset;
  3697. int reloc_type = R_SH_FUNCDESC;
  3698. BFD_ASSERT (htab);
  3699. check_segment[0] = check_segment[1] = -1;
  3700. /* FIXME: See what FRV does for global symbols in the
  3701. executable, with --export-dynamic. Do they need ld.so
  3702. to allocate official descriptors? See what this code
  3703. does. */
  3704. relocation = 0;
  3705. addend = 0;
  3706. if (r_type == R_SH_FUNCDESC)
  3707. {
  3708. reloc_section = input_section;
  3709. reloc_offset = rel->r_offset;
  3710. }
  3711. else
  3712. {
  3713. reloc_section = sgot;
  3714. if (h != NULL)
  3715. reloc_offset = h->got.offset;
  3716. else
  3717. {
  3718. BFD_ASSERT (local_got_offsets != NULL);
  3719. reloc_offset = local_got_offsets[r_symndx];
  3720. }
  3721. BFD_ASSERT (reloc_offset != MINUS_ONE);
  3722. if (reloc_offset & 1)
  3723. {
  3724. reloc_offset &= ~1;
  3725. goto funcdesc_done_got;
  3726. }
  3727. }
  3728. if (h && h->root.type == bfd_link_hash_undefweak
  3729. && (SYMBOL_CALLS_LOCAL (info, h)
  3730. || !htab->root.dynamic_sections_created))
  3731. /* Undefined weak symbol which will not be dynamically
  3732. resolved later; leave it at zero. */
  3733. goto funcdesc_leave_zero;
  3734. else if (SYMBOL_CALLS_LOCAL (info, h)
  3735. && ! SYMBOL_FUNCDESC_LOCAL (info, h))
  3736. {
  3737. /* If the symbol needs a non-local function descriptor
  3738. but binds locally (i.e., its visibility is
  3739. protected), emit a dynamic relocation decayed to
  3740. section+offset. This is an optimization; the dynamic
  3741. linker would resolve our function descriptor request
  3742. to our copy of the function anyway. */
  3743. dynindx = elf_section_data (h->root.u.def.section
  3744. ->output_section)->dynindx;
  3745. relocation += h->root.u.def.section->output_offset
  3746. + h->root.u.def.value;
  3747. }
  3748. else if (! SYMBOL_FUNCDESC_LOCAL (info, h))
  3749. {
  3750. /* If the symbol is dynamic and there will be dynamic
  3751. symbol resolution because we are or are linked with a
  3752. shared library, emit a FUNCDESC relocation such that
  3753. the dynamic linker will allocate the function
  3754. descriptor. */
  3755. BFD_ASSERT (h->dynindx != -1);
  3756. dynindx = h->dynindx;
  3757. }
  3758. else
  3759. {
  3760. bfd_vma offset;
  3761. /* Otherwise, we know we have a private function
  3762. descriptor, so reference it directly. */
  3763. reloc_type = R_SH_DIR32;
  3764. dynindx = elf_section_data (htab->sfuncdesc
  3765. ->output_section)->dynindx;
  3766. if (h)
  3767. {
  3768. offset = sh_elf_hash_entry (h)->funcdesc.offset;
  3769. BFD_ASSERT (offset != MINUS_ONE);
  3770. if ((offset & 1) == 0)
  3771. {
  3772. if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
  3773. offset, NULL, 0))
  3774. return false;
  3775. sh_elf_hash_entry (h)->funcdesc.offset |= 1;
  3776. }
  3777. }
  3778. else
  3779. {
  3780. union gotref *local_funcdesc;
  3781. local_funcdesc = sh_elf_local_funcdesc (input_bfd);
  3782. offset = local_funcdesc[r_symndx].offset;
  3783. BFD_ASSERT (offset != MINUS_ONE);
  3784. if ((offset & 1) == 0)
  3785. {
  3786. if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
  3787. offset, sec,
  3788. sym->st_value))
  3789. return false;
  3790. local_funcdesc[r_symndx].offset |= 1;
  3791. }
  3792. }
  3793. relocation = htab->sfuncdesc->output_offset + (offset & ~1);
  3794. }
  3795. if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
  3796. {
  3797. bfd_vma offset;
  3798. if (sh_elf_osec_readonly_p (output_bfd,
  3799. reloc_section->output_section))
  3800. {
  3801. _bfd_error_handler
  3802. /* xgettext:c-format */
  3803. (_("%pB(%pA+%#" PRIx64 "): "
  3804. "cannot emit fixup to `%s' in read-only section"),
  3805. input_bfd,
  3806. input_section,
  3807. (uint64_t) rel->r_offset,
  3808. symname);
  3809. return false;
  3810. }
  3811. offset = _bfd_elf_section_offset (output_bfd, info,
  3812. reloc_section, reloc_offset);
  3813. if (offset != (bfd_vma)-1)
  3814. sh_elf_add_rofixup (output_bfd, htab->srofixup,
  3815. offset
  3816. + reloc_section->output_section->vma
  3817. + reloc_section->output_offset);
  3818. }
  3819. else if ((reloc_section->output_section->flags
  3820. & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
  3821. {
  3822. bfd_vma offset;
  3823. if (sh_elf_osec_readonly_p (output_bfd,
  3824. reloc_section->output_section))
  3825. {
  3826. info->callbacks->warning
  3827. (info,
  3828. _("cannot emit dynamic relocations in read-only section"),
  3829. symname, input_bfd, reloc_section, reloc_offset);
  3830. return false;
  3831. }
  3832. offset = _bfd_elf_section_offset (output_bfd, info,
  3833. reloc_section, reloc_offset);
  3834. if (offset != (bfd_vma)-1)
  3835. sh_elf_add_dyn_reloc (output_bfd, srelgot,
  3836. offset
  3837. + reloc_section->output_section->vma
  3838. + reloc_section->output_offset,
  3839. reloc_type, dynindx, relocation);
  3840. if (r_type == R_SH_FUNCDESC)
  3841. {
  3842. r = bfd_reloc_ok;
  3843. break;
  3844. }
  3845. else
  3846. {
  3847. relocation = 0;
  3848. goto funcdesc_leave_zero;
  3849. }
  3850. }
  3851. if (SYMBOL_FUNCDESC_LOCAL (info, h))
  3852. relocation += htab->sfuncdesc->output_section->vma;
  3853. funcdesc_leave_zero:
  3854. if (r_type != R_SH_FUNCDESC)
  3855. {
  3856. bfd_put_32 (output_bfd, relocation,
  3857. reloc_section->contents + reloc_offset);
  3858. if (h != NULL)
  3859. h->got.offset |= 1;
  3860. else
  3861. local_got_offsets[r_symndx] |= 1;
  3862. funcdesc_done_got:
  3863. relocation = sh_elf_got_offset (htab) + reloc_offset;
  3864. #ifdef GOT_BIAS
  3865. relocation -= GOT_BIAS;
  3866. #endif
  3867. }
  3868. if (r_type == R_SH_GOTFUNCDESC20)
  3869. {
  3870. r = install_movi20_field (output_bfd, relocation + addend,
  3871. input_bfd, input_section, contents,
  3872. rel->r_offset);
  3873. break;
  3874. }
  3875. else
  3876. goto final_link_relocate;
  3877. }
  3878. break;
  3879. case R_SH_GOTOFFFUNCDESC:
  3880. case R_SH_GOTOFFFUNCDESC20:
  3881. /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
  3882. executable and --export-dynamic. If such symbols get
  3883. ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
  3884. for them. */
  3885. BFD_ASSERT (htab);
  3886. check_segment[0] = check_segment[1] = -1;
  3887. relocation = 0;
  3888. addend = rel->r_addend;
  3889. if (h && (h->root.type == bfd_link_hash_undefweak
  3890. || !SYMBOL_FUNCDESC_LOCAL (info, h)))
  3891. {
  3892. _bfd_error_handler
  3893. /* xgettext:c-format */
  3894. (_("%pB(%pA+%#" PRIx64 "): "
  3895. "%s relocation against external symbol \"%s\""),
  3896. input_bfd, input_section, (uint64_t) rel->r_offset,
  3897. howto->name, h->root.root.string);
  3898. return false;
  3899. }
  3900. else
  3901. {
  3902. bfd_vma offset;
  3903. /* Otherwise, we know we have a private function
  3904. descriptor, so reference it directly. */
  3905. if (h)
  3906. {
  3907. offset = sh_elf_hash_entry (h)->funcdesc.offset;
  3908. BFD_ASSERT (offset != MINUS_ONE);
  3909. if ((offset & 1) == 0)
  3910. {
  3911. if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
  3912. offset, NULL, 0))
  3913. return false;
  3914. sh_elf_hash_entry (h)->funcdesc.offset |= 1;
  3915. }
  3916. }
  3917. else
  3918. {
  3919. union gotref *local_funcdesc;
  3920. local_funcdesc = sh_elf_local_funcdesc (input_bfd);
  3921. offset = local_funcdesc[r_symndx].offset;
  3922. BFD_ASSERT (offset != MINUS_ONE);
  3923. if ((offset & 1) == 0)
  3924. {
  3925. if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
  3926. offset, sec,
  3927. sym->st_value))
  3928. return false;
  3929. local_funcdesc[r_symndx].offset |= 1;
  3930. }
  3931. }
  3932. relocation = htab->sfuncdesc->output_offset + (offset & ~1);
  3933. }
  3934. relocation -= (htab->root.hgot->root.u.def.value
  3935. + sgotplt->output_offset);
  3936. #ifdef GOT_BIAS
  3937. relocation -= GOT_BIAS;
  3938. #endif
  3939. if (r_type == R_SH_GOTOFFFUNCDESC20)
  3940. {
  3941. r = install_movi20_field (output_bfd, relocation + addend,
  3942. input_bfd, input_section, contents,
  3943. rel->r_offset);
  3944. break;
  3945. }
  3946. else
  3947. goto final_link_relocate;
  3948. case R_SH_LOOP_START:
  3949. {
  3950. static bfd_vma start, end;
  3951. start = (relocation + rel->r_addend
  3952. - (sec->output_section->vma + sec->output_offset));
  3953. r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
  3954. rel->r_offset, sec, start, end);
  3955. break;
  3956. case R_SH_LOOP_END:
  3957. end = (relocation + rel->r_addend
  3958. - (sec->output_section->vma + sec->output_offset));
  3959. r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
  3960. rel->r_offset, sec, start, end);
  3961. break;
  3962. }
  3963. case R_SH_TLS_GD_32:
  3964. case R_SH_TLS_IE_32:
  3965. BFD_ASSERT (htab);
  3966. check_segment[0] = check_segment[1] = -1;
  3967. r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
  3968. got_type = GOT_UNKNOWN;
  3969. if (h == NULL && local_got_offsets)
  3970. got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
  3971. else if (h != NULL)
  3972. {
  3973. got_type = sh_elf_hash_entry (h)->got_type;
  3974. if (! bfd_link_pic (info)
  3975. && (h->dynindx == -1
  3976. || h->def_regular))
  3977. r_type = R_SH_TLS_LE_32;
  3978. }
  3979. if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
  3980. r_type = R_SH_TLS_IE_32;
  3981. if (r_type == R_SH_TLS_LE_32)
  3982. {
  3983. bfd_vma offset;
  3984. unsigned short insn;
  3985. if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
  3986. {
  3987. /* GD->LE transition:
  3988. mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
  3989. jsr @r1; add r12,r4; bra 3f; nop; .align 2;
  3990. 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
  3991. We change it into:
  3992. mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
  3993. nop; nop; ...
  3994. 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
  3995. offset = rel->r_offset;
  3996. if (offset < 16)
  3997. {
  3998. _bfd_error_handler
  3999. /* xgettext:c-format */
  4000. (_("%pB(%pA): offset in relocation for GD->LE translation is too small: %#" PRIx64),
  4001. input_bfd, input_section, (uint64_t) offset);
  4002. return false;
  4003. }
  4004. /* Size of GD instructions is 16 or 18. */
  4005. offset -= 16;
  4006. insn = bfd_get_16 (input_bfd, contents + offset + 0);
  4007. if ((insn & 0xff00) == 0xc700)
  4008. {
  4009. BFD_ASSERT (offset >= 2);
  4010. offset -= 2;
  4011. insn = bfd_get_16 (input_bfd, contents + offset + 0);
  4012. }
  4013. if ((insn & 0xff00) != 0xd400)
  4014. _bfd_error_handler
  4015. /* xgettext:c-format */ /* The backslash is to prevent bogus trigraph detection. */
  4016. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd4?\?)"),
  4017. input_bfd, input_section, (uint64_t) offset, (int) insn);
  4018. insn = bfd_get_16 (input_bfd, contents + offset + 2);
  4019. if ((insn & 0xff00) != 0xc700)
  4020. _bfd_error_handler
  4021. /* xgettext:c-format */
  4022. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xc7?\?)"),
  4023. input_bfd, input_section, (uint64_t) offset, (int) insn);
  4024. insn = bfd_get_16 (input_bfd, contents + offset + 4);
  4025. if ((insn & 0xff00) != 0xd100)
  4026. _bfd_error_handler
  4027. /* xgettext:c-format */
  4028. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd1?\?)"),
  4029. input_bfd, input_section, (uint64_t) offset, (int) insn);
  4030. insn = bfd_get_16 (input_bfd, contents + offset + 6);
  4031. if (insn != 0x310c)
  4032. _bfd_error_handler
  4033. /* xgettext:c-format */
  4034. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x310c)"),
  4035. input_bfd, input_section, (uint64_t) offset, (int) insn);
  4036. insn = bfd_get_16 (input_bfd, contents + offset + 8);
  4037. if (insn != 0x410b)
  4038. _bfd_error_handler
  4039. /* xgettext:c-format */
  4040. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x410b)"),
  4041. input_bfd, input_section, (uint64_t) offset, (int) insn);
  4042. insn = bfd_get_16 (input_bfd, contents + offset + 10);
  4043. if (insn != 0x34cc)
  4044. _bfd_error_handler
  4045. /* xgettext:c-format */
  4046. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x34cc)"),
  4047. input_bfd, input_section, (uint64_t) offset, (int) insn);
  4048. bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
  4049. bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
  4050. bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
  4051. bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
  4052. bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
  4053. }
  4054. else
  4055. {
  4056. int target;
  4057. /* IE->LE transition:
  4058. mov.l 1f,r0;
  4059. stc gbr,rN;
  4060. mov.l @(r0,r12),rM;
  4061. bra 2f;
  4062. add ...;
  4063. .align 2;
  4064. 1: x@GOTTPOFF;
  4065. 2:
  4066. We change it into:
  4067. mov.l .Ln,rM;
  4068. stc gbr,rN;
  4069. nop;
  4070. ...;
  4071. 1: x@TPOFF;
  4072. 2:. */
  4073. offset = rel->r_offset;
  4074. if (offset < 16)
  4075. {
  4076. _bfd_error_handler
  4077. /* xgettext:c-format */
  4078. (_("%pB(%pA): offset in relocation for IE->LE translation is too small: %#" PRIx64),
  4079. input_bfd, input_section, (uint64_t) offset);
  4080. return false;
  4081. }
  4082. /* Size of IE instructions is 10 or 12. */
  4083. offset -= 10;
  4084. insn = bfd_get_16 (input_bfd, contents + offset + 0);
  4085. if ((insn & 0xf0ff) == 0x0012)
  4086. {
  4087. BFD_ASSERT (offset >= 2);
  4088. offset -= 2;
  4089. insn = bfd_get_16 (input_bfd, contents + offset + 0);
  4090. }
  4091. if ((insn & 0xff00) != 0xd000)
  4092. _bfd_error_handler
  4093. /* xgettext:c-format */
  4094. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd0??: mov.l)"),
  4095. input_bfd, input_section, (uint64_t) offset, (int) insn);
  4096. target = insn & 0x00ff;
  4097. insn = bfd_get_16 (input_bfd, contents + offset + 2);
  4098. if ((insn & 0xf0ff) != 0x0012)
  4099. _bfd_error_handler
  4100. /* xgettext:c-format */
  4101. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x0?12: stc)"),
  4102. input_bfd, input_section, (uint64_t) (offset + 2), (int) insn);
  4103. insn = bfd_get_16 (input_bfd, contents + offset + 4);
  4104. if ((insn & 0xf0ff) != 0x00ce)
  4105. _bfd_error_handler
  4106. /* xgettext:c-format */
  4107. (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x0?ce: mov.l)"),
  4108. input_bfd, input_section, (uint64_t) (offset + 4), (int) insn);
  4109. insn = 0xd000 | (insn & 0x0f00) | target;
  4110. bfd_put_16 (output_bfd, insn, contents + offset + 0);
  4111. bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
  4112. }
  4113. bfd_put_32 (output_bfd, tpoff (info, relocation),
  4114. contents + rel->r_offset);
  4115. continue;
  4116. }
  4117. if (sgot == NULL || sgotplt == NULL)
  4118. abort ();
  4119. if (h != NULL)
  4120. off = h->got.offset;
  4121. else
  4122. {
  4123. if (local_got_offsets == NULL)
  4124. abort ();
  4125. off = local_got_offsets[r_symndx];
  4126. }
  4127. /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
  4128. if (r_type == R_SH_TLS_IE_32
  4129. && ! htab->root.dynamic_sections_created)
  4130. {
  4131. off &= ~1;
  4132. bfd_put_32 (output_bfd, tpoff (info, relocation),
  4133. sgot->contents + off);
  4134. bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
  4135. contents + rel->r_offset);
  4136. continue;
  4137. }
  4138. if ((off & 1) != 0)
  4139. off &= ~1;
  4140. else
  4141. {
  4142. Elf_Internal_Rela outrel;
  4143. bfd_byte *loc;
  4144. int dr_type, indx;
  4145. outrel.r_offset = (sgot->output_section->vma
  4146. + sgot->output_offset + off);
  4147. if (h == NULL || h->dynindx == -1)
  4148. indx = 0;
  4149. else
  4150. indx = h->dynindx;
  4151. dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
  4152. R_SH_TLS_TPOFF32);
  4153. if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
  4154. outrel.r_addend = relocation - dtpoff_base (info);
  4155. else
  4156. outrel.r_addend = 0;
  4157. outrel.r_info = ELF32_R_INFO (indx, dr_type);
  4158. loc = srelgot->contents;
  4159. loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  4160. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4161. if (r_type == R_SH_TLS_GD_32)
  4162. {
  4163. if (indx == 0)
  4164. {
  4165. bfd_put_32 (output_bfd,
  4166. relocation - dtpoff_base (info),
  4167. sgot->contents + off + 4);
  4168. }
  4169. else
  4170. {
  4171. outrel.r_info = ELF32_R_INFO (indx,
  4172. R_SH_TLS_DTPOFF32);
  4173. outrel.r_offset += 4;
  4174. outrel.r_addend = 0;
  4175. srelgot->reloc_count++;
  4176. loc += sizeof (Elf32_External_Rela);
  4177. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4178. }
  4179. }
  4180. if (h != NULL)
  4181. h->got.offset |= 1;
  4182. else
  4183. local_got_offsets[r_symndx] |= 1;
  4184. }
  4185. if (off >= (bfd_vma) -2)
  4186. abort ();
  4187. if (r_type == (int) ELF32_R_TYPE (rel->r_info))
  4188. relocation = sh_elf_got_offset (htab) + off;
  4189. else
  4190. {
  4191. bfd_vma offset;
  4192. unsigned short insn;
  4193. /* GD->IE transition:
  4194. mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
  4195. jsr @r1; add r12,r4; bra 3f; nop; .align 2;
  4196. 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
  4197. We change it into:
  4198. mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
  4199. nop; nop; bra 3f; nop; .align 2;
  4200. 1: .long x@TPOFF; 2:...; 3:. */
  4201. offset = rel->r_offset;
  4202. if (offset < 16)
  4203. {
  4204. _bfd_error_handler
  4205. /* xgettext:c-format */
  4206. (_("%pB(%pA): offset in relocation for GD->IE translation is too small: %#" PRIx64),
  4207. input_bfd, input_section, (uint64_t) offset);
  4208. return false;
  4209. }
  4210. /* Size of GD instructions is 16 or 18. */
  4211. offset -= 16;
  4212. insn = bfd_get_16 (input_bfd, contents + offset + 0);
  4213. if ((insn & 0xff00) == 0xc700)
  4214. {
  4215. BFD_ASSERT (offset >= 2);
  4216. offset -= 2;
  4217. insn = bfd_get_16 (input_bfd, contents + offset + 0);
  4218. }
  4219. BFD_ASSERT ((insn & 0xff00) == 0xd400);
  4220. /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
  4221. bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
  4222. insn = bfd_get_16 (input_bfd, contents + offset + 2);
  4223. BFD_ASSERT ((insn & 0xff00) == 0xc700);
  4224. insn = bfd_get_16 (input_bfd, contents + offset + 4);
  4225. BFD_ASSERT ((insn & 0xff00) == 0xd100);
  4226. insn = bfd_get_16 (input_bfd, contents + offset + 6);
  4227. BFD_ASSERT (insn == 0x310c);
  4228. insn = bfd_get_16 (input_bfd, contents + offset + 8);
  4229. BFD_ASSERT (insn == 0x410b);
  4230. insn = bfd_get_16 (input_bfd, contents + offset + 10);
  4231. BFD_ASSERT (insn == 0x34cc);
  4232. bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
  4233. bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
  4234. bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
  4235. bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
  4236. bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
  4237. bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
  4238. contents + rel->r_offset);
  4239. continue;
  4240. }
  4241. addend = rel->r_addend;
  4242. goto final_link_relocate;
  4243. case R_SH_TLS_LD_32:
  4244. BFD_ASSERT (htab);
  4245. check_segment[0] = check_segment[1] = -1;
  4246. if (! bfd_link_pic (info))
  4247. {
  4248. bfd_vma offset;
  4249. unsigned short insn;
  4250. /* LD->LE transition:
  4251. mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
  4252. jsr @r1; add r12,r4; bra 3f; nop; .align 2;
  4253. 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
  4254. We change it into:
  4255. stc gbr,r0; nop; nop; nop;
  4256. nop; nop; bra 3f; ...; 3:. */
  4257. offset = rel->r_offset;
  4258. if (offset < 16)
  4259. {
  4260. _bfd_error_handler
  4261. /* xgettext:c-format */
  4262. (_("%pB(%pA): offset in relocation for LD->LE translation is too small: %#" PRIx64),
  4263. input_bfd, input_section, (uint64_t) offset);
  4264. return false;
  4265. }
  4266. /* Size of LD instructions is 16 or 18. */
  4267. offset -= 16;
  4268. insn = bfd_get_16 (input_bfd, contents + offset + 0);
  4269. if ((insn & 0xff00) == 0xc700)
  4270. {
  4271. BFD_ASSERT (offset >= 2);
  4272. offset -= 2;
  4273. insn = bfd_get_16 (input_bfd, contents + offset + 0);
  4274. }
  4275. BFD_ASSERT ((insn & 0xff00) == 0xd400);
  4276. insn = bfd_get_16 (input_bfd, contents + offset + 2);
  4277. BFD_ASSERT ((insn & 0xff00) == 0xc700);
  4278. insn = bfd_get_16 (input_bfd, contents + offset + 4);
  4279. BFD_ASSERT ((insn & 0xff00) == 0xd100);
  4280. insn = bfd_get_16 (input_bfd, contents + offset + 6);
  4281. BFD_ASSERT (insn == 0x310c);
  4282. insn = bfd_get_16 (input_bfd, contents + offset + 8);
  4283. BFD_ASSERT (insn == 0x410b);
  4284. insn = bfd_get_16 (input_bfd, contents + offset + 10);
  4285. BFD_ASSERT (insn == 0x34cc);
  4286. bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
  4287. bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
  4288. bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
  4289. bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
  4290. bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
  4291. bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
  4292. continue;
  4293. }
  4294. if (sgot == NULL || sgotplt == NULL)
  4295. abort ();
  4296. off = htab->tls_ldm_got.offset;
  4297. if (off & 1)
  4298. off &= ~1;
  4299. else
  4300. {
  4301. Elf_Internal_Rela outrel;
  4302. bfd_byte *loc;
  4303. outrel.r_offset = (sgot->output_section->vma
  4304. + sgot->output_offset + off);
  4305. outrel.r_addend = 0;
  4306. outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
  4307. loc = srelgot->contents;
  4308. loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  4309. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4310. htab->tls_ldm_got.offset |= 1;
  4311. }
  4312. relocation = sh_elf_got_offset (htab) + off;
  4313. addend = rel->r_addend;
  4314. goto final_link_relocate;
  4315. case R_SH_TLS_LDO_32:
  4316. check_segment[0] = check_segment[1] = -1;
  4317. if (! bfd_link_pic (info))
  4318. relocation = tpoff (info, relocation);
  4319. else
  4320. relocation -= dtpoff_base (info);
  4321. addend = rel->r_addend;
  4322. goto final_link_relocate;
  4323. case R_SH_TLS_LE_32:
  4324. {
  4325. int indx;
  4326. Elf_Internal_Rela outrel;
  4327. bfd_byte *loc;
  4328. check_segment[0] = check_segment[1] = -1;
  4329. if (!bfd_link_dll (info))
  4330. {
  4331. relocation = tpoff (info, relocation);
  4332. addend = rel->r_addend;
  4333. goto final_link_relocate;
  4334. }
  4335. if (sreloc == NULL)
  4336. {
  4337. sreloc = _bfd_elf_get_dynamic_reloc_section
  4338. (input_bfd, input_section, /*rela?*/ true);
  4339. if (sreloc == NULL)
  4340. return false;
  4341. }
  4342. if (h == NULL || h->dynindx == -1)
  4343. indx = 0;
  4344. else
  4345. indx = h->dynindx;
  4346. outrel.r_offset = (input_section->output_section->vma
  4347. + input_section->output_offset
  4348. + rel->r_offset);
  4349. outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
  4350. if (indx == 0)
  4351. outrel.r_addend = relocation - dtpoff_base (info);
  4352. else
  4353. outrel.r_addend = 0;
  4354. loc = sreloc->contents;
  4355. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  4356. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4357. continue;
  4358. }
  4359. }
  4360. relocation_done:
  4361. if (fdpic_p && check_segment[0] != (unsigned) -1
  4362. && check_segment[0] != check_segment[1])
  4363. {
  4364. /* We don't want duplicate errors for undefined symbols. */
  4365. if (!h || h->root.type != bfd_link_hash_undefined)
  4366. {
  4367. if (bfd_link_pic (info))
  4368. {
  4369. info->callbacks->einfo
  4370. /* xgettext:c-format */
  4371. (_("%X%C: relocation to \"%s\" references a different segment\n"),
  4372. input_bfd, input_section, rel->r_offset, symname);
  4373. return false;
  4374. }
  4375. else
  4376. info->callbacks->einfo
  4377. /* xgettext:c-format */
  4378. (_("%C: warning: relocation to \"%s\" references a different segment\n"),
  4379. input_bfd, input_section, rel->r_offset, symname);
  4380. }
  4381. elf_elfheader (output_bfd)->e_flags |= EF_SH_PIC;
  4382. }
  4383. if (r != bfd_reloc_ok)
  4384. {
  4385. switch (r)
  4386. {
  4387. default:
  4388. case bfd_reloc_outofrange:
  4389. abort ();
  4390. case bfd_reloc_overflow:
  4391. {
  4392. const char *name;
  4393. if (h != NULL)
  4394. name = NULL;
  4395. else
  4396. {
  4397. name = (bfd_elf_string_from_elf_section
  4398. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  4399. if (name == NULL)
  4400. return false;
  4401. if (*name == '\0')
  4402. name = bfd_section_name (sec);
  4403. }
  4404. (*info->callbacks->reloc_overflow)
  4405. (info, (h ? &h->root : NULL), name, howto->name,
  4406. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  4407. }
  4408. break;
  4409. }
  4410. }
  4411. }
  4412. return true;
  4413. }
  4414. /* This is a version of bfd_generic_get_relocated_section_contents
  4415. which uses sh_elf_relocate_section. */
  4416. static bfd_byte *
  4417. sh_elf_get_relocated_section_contents (bfd *output_bfd,
  4418. struct bfd_link_info *link_info,
  4419. struct bfd_link_order *link_order,
  4420. bfd_byte *data,
  4421. bool relocatable,
  4422. asymbol **symbols)
  4423. {
  4424. Elf_Internal_Shdr *symtab_hdr;
  4425. asection *input_section = link_order->u.indirect.section;
  4426. bfd *input_bfd = input_section->owner;
  4427. asection **sections = NULL;
  4428. Elf_Internal_Rela *internal_relocs = NULL;
  4429. Elf_Internal_Sym *isymbuf = NULL;
  4430. /* We only need to handle the case of relaxing, or of having a
  4431. particular set of section contents, specially. */
  4432. if (relocatable
  4433. || elf_section_data (input_section)->this_hdr.contents == NULL)
  4434. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  4435. link_order, data,
  4436. relocatable,
  4437. symbols);
  4438. symtab_hdr = &elf_symtab_hdr (input_bfd);
  4439. memcpy (data, elf_section_data (input_section)->this_hdr.contents,
  4440. (size_t) input_section->size);
  4441. if ((input_section->flags & SEC_RELOC) != 0
  4442. && input_section->reloc_count > 0)
  4443. {
  4444. asection **secpp;
  4445. Elf_Internal_Sym *isym, *isymend;
  4446. bfd_size_type amt;
  4447. internal_relocs = (_bfd_elf_link_read_relocs
  4448. (input_bfd, input_section, NULL,
  4449. (Elf_Internal_Rela *) NULL, false));
  4450. if (internal_relocs == NULL)
  4451. goto error_return;
  4452. if (symtab_hdr->sh_info != 0)
  4453. {
  4454. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  4455. if (isymbuf == NULL)
  4456. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  4457. symtab_hdr->sh_info, 0,
  4458. NULL, NULL, NULL);
  4459. if (isymbuf == NULL)
  4460. goto error_return;
  4461. }
  4462. amt = symtab_hdr->sh_info;
  4463. amt *= sizeof (asection *);
  4464. sections = (asection **) bfd_malloc (amt);
  4465. if (sections == NULL && amt != 0)
  4466. goto error_return;
  4467. isymend = isymbuf + symtab_hdr->sh_info;
  4468. for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
  4469. {
  4470. asection *isec;
  4471. if (isym->st_shndx == SHN_UNDEF)
  4472. isec = bfd_und_section_ptr;
  4473. else if (isym->st_shndx == SHN_ABS)
  4474. isec = bfd_abs_section_ptr;
  4475. else if (isym->st_shndx == SHN_COMMON)
  4476. isec = bfd_com_section_ptr;
  4477. else
  4478. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  4479. *secpp = isec;
  4480. }
  4481. if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
  4482. input_section, data, internal_relocs,
  4483. isymbuf, sections))
  4484. goto error_return;
  4485. free (sections);
  4486. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  4487. free (isymbuf);
  4488. if (elf_section_data (input_section)->relocs != internal_relocs)
  4489. free (internal_relocs);
  4490. }
  4491. return data;
  4492. error_return:
  4493. free (sections);
  4494. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  4495. free (isymbuf);
  4496. if (elf_section_data (input_section)->relocs != internal_relocs)
  4497. free (internal_relocs);
  4498. return NULL;
  4499. }
  4500. /* Return the base VMA address which should be subtracted from real addresses
  4501. when resolving @dtpoff relocation.
  4502. This is PT_TLS segment p_vaddr. */
  4503. static bfd_vma
  4504. dtpoff_base (struct bfd_link_info *info)
  4505. {
  4506. /* If tls_sec is NULL, we should have signalled an error already. */
  4507. if (elf_hash_table (info)->tls_sec == NULL)
  4508. return 0;
  4509. return elf_hash_table (info)->tls_sec->vma;
  4510. }
  4511. /* Return the relocation value for R_SH_TLS_TPOFF32.. */
  4512. static bfd_vma
  4513. tpoff (struct bfd_link_info *info, bfd_vma address)
  4514. {
  4515. /* If tls_sec is NULL, we should have signalled an error already. */
  4516. if (elf_hash_table (info)->tls_sec == NULL)
  4517. return 0;
  4518. /* SH TLS ABI is variant I and static TLS block start just after tcbhead
  4519. structure which has 2 pointer fields. */
  4520. return (address - elf_hash_table (info)->tls_sec->vma
  4521. + align_power ((bfd_vma) 8,
  4522. elf_hash_table (info)->tls_sec->alignment_power));
  4523. }
  4524. static asection *
  4525. sh_elf_gc_mark_hook (asection *sec,
  4526. struct bfd_link_info *info,
  4527. Elf_Internal_Rela *rel,
  4528. struct elf_link_hash_entry *h,
  4529. Elf_Internal_Sym *sym)
  4530. {
  4531. if (h != NULL)
  4532. switch (ELF32_R_TYPE (rel->r_info))
  4533. {
  4534. case R_SH_GNU_VTINHERIT:
  4535. case R_SH_GNU_VTENTRY:
  4536. return NULL;
  4537. }
  4538. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  4539. }
  4540. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  4541. static void
  4542. sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
  4543. struct elf_link_hash_entry *dir,
  4544. struct elf_link_hash_entry *ind)
  4545. {
  4546. struct elf_sh_link_hash_entry *edir, *eind;
  4547. edir = (struct elf_sh_link_hash_entry *) dir;
  4548. eind = (struct elf_sh_link_hash_entry *) ind;
  4549. edir->gotplt_refcount = eind->gotplt_refcount;
  4550. eind->gotplt_refcount = 0;
  4551. edir->funcdesc.refcount += eind->funcdesc.refcount;
  4552. eind->funcdesc.refcount = 0;
  4553. edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
  4554. eind->abs_funcdesc_refcount = 0;
  4555. if (ind->root.type == bfd_link_hash_indirect
  4556. && dir->got.refcount <= 0)
  4557. {
  4558. edir->got_type = eind->got_type;
  4559. eind->got_type = GOT_UNKNOWN;
  4560. }
  4561. if (ind->root.type != bfd_link_hash_indirect
  4562. && dir->dynamic_adjusted)
  4563. {
  4564. /* If called to transfer flags for a weakdef during processing
  4565. of elf_adjust_dynamic_symbol, don't copy non_got_ref.
  4566. We clear it ourselves for ELIMINATE_COPY_RELOCS. */
  4567. if (dir->versioned != versioned_hidden)
  4568. dir->ref_dynamic |= ind->ref_dynamic;
  4569. dir->ref_regular |= ind->ref_regular;
  4570. dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
  4571. dir->needs_plt |= ind->needs_plt;
  4572. }
  4573. else
  4574. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  4575. }
  4576. static int
  4577. sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
  4578. int is_local)
  4579. {
  4580. if (bfd_link_pic (info))
  4581. return r_type;
  4582. switch (r_type)
  4583. {
  4584. case R_SH_TLS_GD_32:
  4585. case R_SH_TLS_IE_32:
  4586. if (is_local)
  4587. return R_SH_TLS_LE_32;
  4588. return R_SH_TLS_IE_32;
  4589. case R_SH_TLS_LD_32:
  4590. return R_SH_TLS_LE_32;
  4591. }
  4592. return r_type;
  4593. }
  4594. /* Look through the relocs for a section during the first phase.
  4595. Since we don't do .gots or .plts, we just need to consider the
  4596. virtual table relocs for gc. */
  4597. static bool
  4598. sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
  4599. const Elf_Internal_Rela *relocs)
  4600. {
  4601. Elf_Internal_Shdr *symtab_hdr;
  4602. struct elf_link_hash_entry **sym_hashes;
  4603. struct elf_sh_link_hash_table *htab;
  4604. const Elf_Internal_Rela *rel;
  4605. const Elf_Internal_Rela *rel_end;
  4606. asection *sreloc;
  4607. unsigned int r_type;
  4608. enum got_type got_type, old_got_type;
  4609. sreloc = NULL;
  4610. if (bfd_link_relocatable (info))
  4611. return true;
  4612. BFD_ASSERT (is_sh_elf (abfd));
  4613. symtab_hdr = &elf_symtab_hdr (abfd);
  4614. sym_hashes = elf_sym_hashes (abfd);
  4615. htab = sh_elf_hash_table (info);
  4616. if (htab == NULL)
  4617. return false;
  4618. rel_end = relocs + sec->reloc_count;
  4619. for (rel = relocs; rel < rel_end; rel++)
  4620. {
  4621. struct elf_link_hash_entry *h;
  4622. unsigned long r_symndx;
  4623. r_symndx = ELF32_R_SYM (rel->r_info);
  4624. r_type = ELF32_R_TYPE (rel->r_info);
  4625. if (r_symndx < symtab_hdr->sh_info)
  4626. h = NULL;
  4627. else
  4628. {
  4629. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  4630. while (h->root.type == bfd_link_hash_indirect
  4631. || h->root.type == bfd_link_hash_warning)
  4632. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4633. }
  4634. r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
  4635. if (! bfd_link_pic (info)
  4636. && r_type == R_SH_TLS_IE_32
  4637. && h != NULL
  4638. && h->root.type != bfd_link_hash_undefined
  4639. && h->root.type != bfd_link_hash_undefweak
  4640. && (h->dynindx == -1
  4641. || h->def_regular))
  4642. r_type = R_SH_TLS_LE_32;
  4643. if (htab->fdpic_p)
  4644. switch (r_type)
  4645. {
  4646. case R_SH_GOTOFFFUNCDESC:
  4647. case R_SH_GOTOFFFUNCDESC20:
  4648. case R_SH_FUNCDESC:
  4649. case R_SH_GOTFUNCDESC:
  4650. case R_SH_GOTFUNCDESC20:
  4651. if (h != NULL)
  4652. {
  4653. if (h->dynindx == -1)
  4654. switch (ELF_ST_VISIBILITY (h->other))
  4655. {
  4656. case STV_INTERNAL:
  4657. case STV_HIDDEN:
  4658. break;
  4659. default:
  4660. bfd_elf_link_record_dynamic_symbol (info, h);
  4661. break;
  4662. }
  4663. }
  4664. break;
  4665. }
  4666. /* Some relocs require a global offset table. */
  4667. if (htab->root.sgot == NULL)
  4668. {
  4669. switch (r_type)
  4670. {
  4671. case R_SH_DIR32:
  4672. /* This may require an rofixup. */
  4673. if (!htab->fdpic_p)
  4674. break;
  4675. /* Fall through. */
  4676. case R_SH_GOTPLT32:
  4677. case R_SH_GOT32:
  4678. case R_SH_GOT20:
  4679. case R_SH_GOTOFF:
  4680. case R_SH_GOTOFF20:
  4681. case R_SH_FUNCDESC:
  4682. case R_SH_GOTFUNCDESC:
  4683. case R_SH_GOTFUNCDESC20:
  4684. case R_SH_GOTOFFFUNCDESC:
  4685. case R_SH_GOTOFFFUNCDESC20:
  4686. case R_SH_GOTPC:
  4687. case R_SH_TLS_GD_32:
  4688. case R_SH_TLS_LD_32:
  4689. case R_SH_TLS_IE_32:
  4690. if (htab->root.dynobj == NULL)
  4691. htab->root.dynobj = abfd;
  4692. if (!create_got_section (htab->root.dynobj, info))
  4693. return false;
  4694. break;
  4695. default:
  4696. break;
  4697. }
  4698. }
  4699. switch (r_type)
  4700. {
  4701. /* This relocation describes the C++ object vtable hierarchy.
  4702. Reconstruct it for later use during GC. */
  4703. case R_SH_GNU_VTINHERIT:
  4704. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  4705. return false;
  4706. break;
  4707. /* This relocation describes which C++ vtable entries are actually
  4708. used. Record for later use during GC. */
  4709. case R_SH_GNU_VTENTRY:
  4710. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  4711. return false;
  4712. break;
  4713. case R_SH_TLS_IE_32:
  4714. if (bfd_link_pic (info))
  4715. info->flags |= DF_STATIC_TLS;
  4716. /* FALLTHROUGH */
  4717. force_got:
  4718. case R_SH_TLS_GD_32:
  4719. case R_SH_GOT32:
  4720. case R_SH_GOT20:
  4721. case R_SH_GOTFUNCDESC:
  4722. case R_SH_GOTFUNCDESC20:
  4723. switch (r_type)
  4724. {
  4725. default:
  4726. got_type = GOT_NORMAL;
  4727. break;
  4728. case R_SH_TLS_GD_32:
  4729. got_type = GOT_TLS_GD;
  4730. break;
  4731. case R_SH_TLS_IE_32:
  4732. got_type = GOT_TLS_IE;
  4733. break;
  4734. case R_SH_GOTFUNCDESC:
  4735. case R_SH_GOTFUNCDESC20:
  4736. got_type = GOT_FUNCDESC;
  4737. break;
  4738. }
  4739. if (h != NULL)
  4740. {
  4741. h->got.refcount += 1;
  4742. old_got_type = sh_elf_hash_entry (h)->got_type;
  4743. }
  4744. else
  4745. {
  4746. bfd_signed_vma *local_got_refcounts;
  4747. /* This is a global offset table entry for a local
  4748. symbol. */
  4749. local_got_refcounts = elf_local_got_refcounts (abfd);
  4750. if (local_got_refcounts == NULL)
  4751. {
  4752. bfd_size_type size;
  4753. size = symtab_hdr->sh_info;
  4754. size *= sizeof (bfd_signed_vma);
  4755. size += symtab_hdr->sh_info;
  4756. local_got_refcounts = ((bfd_signed_vma *)
  4757. bfd_zalloc (abfd, size));
  4758. if (local_got_refcounts == NULL)
  4759. return false;
  4760. elf_local_got_refcounts (abfd) = local_got_refcounts;
  4761. sh_elf_local_got_type (abfd)
  4762. = (char *) (local_got_refcounts + symtab_hdr->sh_info);
  4763. }
  4764. local_got_refcounts[r_symndx] += 1;
  4765. old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
  4766. }
  4767. /* If a TLS symbol is accessed using IE at least once,
  4768. there is no point to use dynamic model for it. */
  4769. if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
  4770. && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
  4771. {
  4772. if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
  4773. got_type = GOT_TLS_IE;
  4774. else
  4775. {
  4776. if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
  4777. && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
  4778. _bfd_error_handler
  4779. /* xgettext:c-format */
  4780. (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
  4781. abfd, h->root.root.string);
  4782. else if (old_got_type == GOT_FUNCDESC
  4783. || got_type == GOT_FUNCDESC)
  4784. _bfd_error_handler
  4785. /* xgettext:c-format */
  4786. (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
  4787. abfd, h->root.root.string);
  4788. else
  4789. _bfd_error_handler
  4790. /* xgettext:c-format */
  4791. (_("%pB: `%s' accessed both as normal and thread local symbol"),
  4792. abfd, h->root.root.string);
  4793. return false;
  4794. }
  4795. }
  4796. if (old_got_type != got_type)
  4797. {
  4798. if (h != NULL)
  4799. sh_elf_hash_entry (h)->got_type = got_type;
  4800. else
  4801. sh_elf_local_got_type (abfd) [r_symndx] = got_type;
  4802. }
  4803. break;
  4804. case R_SH_TLS_LD_32:
  4805. sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
  4806. break;
  4807. case R_SH_FUNCDESC:
  4808. case R_SH_GOTOFFFUNCDESC:
  4809. case R_SH_GOTOFFFUNCDESC20:
  4810. if (rel->r_addend)
  4811. {
  4812. _bfd_error_handler
  4813. (_("%pB: Function descriptor relocation with non-zero addend"),
  4814. abfd);
  4815. return false;
  4816. }
  4817. if (h == NULL)
  4818. {
  4819. union gotref *local_funcdesc;
  4820. /* We need a function descriptor for a local symbol. */
  4821. local_funcdesc = sh_elf_local_funcdesc (abfd);
  4822. if (local_funcdesc == NULL)
  4823. {
  4824. bfd_size_type size;
  4825. size = symtab_hdr->sh_info * sizeof (union gotref);
  4826. local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
  4827. if (local_funcdesc == NULL)
  4828. return false;
  4829. sh_elf_local_funcdesc (abfd) = local_funcdesc;
  4830. }
  4831. local_funcdesc[r_symndx].refcount += 1;
  4832. if (r_type == R_SH_FUNCDESC)
  4833. {
  4834. if (!bfd_link_pic (info))
  4835. htab->srofixup->size += 4;
  4836. else
  4837. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  4838. }
  4839. }
  4840. else
  4841. {
  4842. sh_elf_hash_entry (h)->funcdesc.refcount++;
  4843. if (r_type == R_SH_FUNCDESC)
  4844. sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
  4845. /* If there is a function descriptor reference, then
  4846. there should not be any non-FDPIC references. */
  4847. old_got_type = sh_elf_hash_entry (h)->got_type;
  4848. if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
  4849. {
  4850. if (old_got_type == GOT_NORMAL)
  4851. _bfd_error_handler
  4852. /* xgettext:c-format */
  4853. (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
  4854. abfd, h->root.root.string);
  4855. else
  4856. _bfd_error_handler
  4857. /* xgettext:c-format */
  4858. (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
  4859. abfd, h->root.root.string);
  4860. }
  4861. }
  4862. break;
  4863. case R_SH_GOTPLT32:
  4864. /* If this is a local symbol, we resolve it directly without
  4865. creating a procedure linkage table entry. */
  4866. if (h == NULL
  4867. || h->forced_local
  4868. || ! bfd_link_pic (info)
  4869. || info->symbolic
  4870. || h->dynindx == -1)
  4871. goto force_got;
  4872. h->needs_plt = 1;
  4873. h->plt.refcount += 1;
  4874. ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
  4875. break;
  4876. case R_SH_PLT32:
  4877. /* This symbol requires a procedure linkage table entry. We
  4878. actually build the entry in adjust_dynamic_symbol,
  4879. because this might be a case of linking PIC code which is
  4880. never referenced by a dynamic object, in which case we
  4881. don't need to generate a procedure linkage table entry
  4882. after all. */
  4883. /* If this is a local symbol, we resolve it directly without
  4884. creating a procedure linkage table entry. */
  4885. if (h == NULL)
  4886. continue;
  4887. if (h->forced_local)
  4888. break;
  4889. h->needs_plt = 1;
  4890. h->plt.refcount += 1;
  4891. break;
  4892. case R_SH_DIR32:
  4893. case R_SH_REL32:
  4894. if (h != NULL && ! bfd_link_pic (info))
  4895. {
  4896. h->non_got_ref = 1;
  4897. h->plt.refcount += 1;
  4898. }
  4899. /* If we are creating a shared library, and this is a reloc
  4900. against a global symbol, or a non PC relative reloc
  4901. against a local symbol, then we need to copy the reloc
  4902. into the shared library. However, if we are linking with
  4903. -Bsymbolic, we do not need to copy a reloc against a
  4904. global symbol which is defined in an object we are
  4905. including in the link (i.e., DEF_REGULAR is set). At
  4906. this point we have not seen all the input files, so it is
  4907. possible that DEF_REGULAR is not set now but will be set
  4908. later (it is never cleared). We account for that
  4909. possibility below by storing information in the
  4910. dyn_relocs field of the hash table entry. A similar
  4911. situation occurs when creating shared libraries and symbol
  4912. visibility changes render the symbol local.
  4913. If on the other hand, we are creating an executable, we
  4914. may need to keep relocations for symbols satisfied by a
  4915. dynamic library if we manage to avoid copy relocs for the
  4916. symbol. */
  4917. if ((bfd_link_pic (info)
  4918. && (sec->flags & SEC_ALLOC) != 0
  4919. && (r_type != R_SH_REL32
  4920. || (h != NULL
  4921. && (! info->symbolic
  4922. || h->root.type == bfd_link_hash_defweak
  4923. || !h->def_regular))))
  4924. || (! bfd_link_pic (info)
  4925. && (sec->flags & SEC_ALLOC) != 0
  4926. && h != NULL
  4927. && (h->root.type == bfd_link_hash_defweak
  4928. || !h->def_regular)))
  4929. {
  4930. struct elf_dyn_relocs *p;
  4931. struct elf_dyn_relocs **head;
  4932. if (htab->root.dynobj == NULL)
  4933. htab->root.dynobj = abfd;
  4934. /* When creating a shared object, we must copy these
  4935. reloc types into the output file. We create a reloc
  4936. section in dynobj and make room for this reloc. */
  4937. if (sreloc == NULL)
  4938. {
  4939. sreloc = _bfd_elf_make_dynamic_reloc_section
  4940. (sec, htab->root.dynobj, 2, abfd, /*rela?*/ true);
  4941. if (sreloc == NULL)
  4942. return false;
  4943. }
  4944. /* If this is a global symbol, we count the number of
  4945. relocations we need for this symbol. */
  4946. if (h != NULL)
  4947. head = &h->dyn_relocs;
  4948. else
  4949. {
  4950. /* Track dynamic relocs needed for local syms too. */
  4951. asection *s;
  4952. void *vpp;
  4953. Elf_Internal_Sym *isym;
  4954. isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
  4955. abfd, r_symndx);
  4956. if (isym == NULL)
  4957. return false;
  4958. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  4959. if (s == NULL)
  4960. s = sec;
  4961. vpp = &elf_section_data (s)->local_dynrel;
  4962. head = (struct elf_dyn_relocs **) vpp;
  4963. }
  4964. p = *head;
  4965. if (p == NULL || p->sec != sec)
  4966. {
  4967. size_t amt = sizeof (*p);
  4968. p = bfd_alloc (htab->root.dynobj, amt);
  4969. if (p == NULL)
  4970. return false;
  4971. p->next = *head;
  4972. *head = p;
  4973. p->sec = sec;
  4974. p->count = 0;
  4975. p->pc_count = 0;
  4976. }
  4977. p->count += 1;
  4978. if (r_type == R_SH_REL32)
  4979. p->pc_count += 1;
  4980. }
  4981. /* Allocate the fixup regardless of whether we need a relocation.
  4982. If we end up generating the relocation, we'll unallocate the
  4983. fixup. */
  4984. if (htab->fdpic_p && !bfd_link_pic (info)
  4985. && r_type == R_SH_DIR32
  4986. && (sec->flags & SEC_ALLOC) != 0)
  4987. htab->srofixup->size += 4;
  4988. break;
  4989. case R_SH_TLS_LE_32:
  4990. if (bfd_link_dll (info))
  4991. {
  4992. _bfd_error_handler
  4993. (_("%pB: TLS local exec code cannot be linked into shared objects"),
  4994. abfd);
  4995. return false;
  4996. }
  4997. break;
  4998. case R_SH_TLS_LDO_32:
  4999. /* Nothing to do. */
  5000. break;
  5001. default:
  5002. break;
  5003. }
  5004. }
  5005. return true;
  5006. }
  5007. #ifndef sh_elf_set_mach_from_flags
  5008. static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
  5009. static bool
  5010. sh_elf_set_mach_from_flags (bfd *abfd)
  5011. {
  5012. flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
  5013. if (flags >= ARRAY_SIZE (sh_ef_bfd_table))
  5014. return false;
  5015. if (sh_ef_bfd_table[flags] == 0)
  5016. return false;
  5017. bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
  5018. return true;
  5019. }
  5020. /* Reverse table lookup for sh_ef_bfd_table[].
  5021. Given a bfd MACH value from archures.c
  5022. return the equivalent ELF flags from the table.
  5023. Return -1 if no match is found. */
  5024. int
  5025. sh_elf_get_flags_from_mach (unsigned long mach)
  5026. {
  5027. int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
  5028. for (; i>0; i--)
  5029. if (sh_ef_bfd_table[i] == mach)
  5030. return i;
  5031. /* shouldn't get here */
  5032. BFD_FAIL();
  5033. return -1;
  5034. }
  5035. #endif /* not sh_elf_set_mach_from_flags */
  5036. #ifndef sh_elf_copy_private_data
  5037. /* Copy backend specific data from one object module to another */
  5038. static bool
  5039. sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
  5040. {
  5041. if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
  5042. return true;
  5043. if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
  5044. return false;
  5045. return sh_elf_set_mach_from_flags (obfd);
  5046. }
  5047. #endif /* not sh_elf_copy_private_data */
  5048. #ifndef sh_elf_merge_private_data
  5049. /* This function returns the ELF architecture number that
  5050. corresponds to the given arch_sh* flags. */
  5051. int
  5052. sh_find_elf_flags (unsigned int arch_set)
  5053. {
  5054. extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
  5055. unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
  5056. return sh_elf_get_flags_from_mach (bfd_mach);
  5057. }
  5058. /* Merge the architecture type of two BFD files, such that the
  5059. resultant architecture supports all the features required
  5060. by the two input BFDs.
  5061. If the input BFDs are multually incompatible - i.e. one uses
  5062. DSP while the other uses FPU - or there is no known architecture
  5063. that fits the requirements then an error is emitted. */
  5064. static bool
  5065. sh_merge_bfd_arch (bfd *ibfd, struct bfd_link_info *info)
  5066. {
  5067. bfd *obfd = info->output_bfd;
  5068. unsigned int old_arch, new_arch, merged_arch;
  5069. if (! _bfd_generic_verify_endian_match (ibfd, info))
  5070. return false;
  5071. old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
  5072. new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
  5073. merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
  5074. if (!SH_VALID_CO_ARCH_SET (merged_arch))
  5075. {
  5076. _bfd_error_handler
  5077. /* xgettext:c-format */
  5078. (_("%pB: uses %s instructions while previous modules "
  5079. "use %s instructions"),
  5080. ibfd,
  5081. SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
  5082. SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
  5083. bfd_set_error (bfd_error_bad_value);
  5084. return false;
  5085. }
  5086. else if (!SH_VALID_ARCH_SET (merged_arch))
  5087. {
  5088. _bfd_error_handler
  5089. /* xgettext:c-format */
  5090. (_("internal error: merge of architecture '%s' with "
  5091. "architecture '%s' produced unknown architecture"),
  5092. bfd_printable_name (obfd),
  5093. bfd_printable_name (ibfd));
  5094. bfd_set_error (bfd_error_bad_value);
  5095. return false;
  5096. }
  5097. bfd_default_set_arch_mach (obfd, bfd_arch_sh,
  5098. sh_get_bfd_mach_from_arch_set (merged_arch));
  5099. return true;
  5100. }
  5101. /* This routine initialises the elf flags when required and
  5102. calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
  5103. static bool
  5104. sh_elf_merge_private_data (bfd *ibfd, struct bfd_link_info *info)
  5105. {
  5106. bfd *obfd = info->output_bfd;
  5107. /* FIXME: What should be checked when linking shared libraries? */
  5108. if ((ibfd->flags & DYNAMIC) != 0)
  5109. return true;
  5110. if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
  5111. return true;
  5112. if (! elf_flags_init (obfd))
  5113. {
  5114. /* This happens when ld starts out with a 'blank' output file. */
  5115. elf_flags_init (obfd) = true;
  5116. elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  5117. sh_elf_set_mach_from_flags (obfd);
  5118. if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
  5119. elf_elfheader (obfd)->e_flags &= ~EF_SH_PIC;
  5120. }
  5121. if (! sh_merge_bfd_arch (ibfd, info))
  5122. {
  5123. _bfd_error_handler (_("%pB: uses instructions which are incompatible "
  5124. "with instructions used in previous modules"),
  5125. ibfd);
  5126. bfd_set_error (bfd_error_bad_value);
  5127. return false;
  5128. }
  5129. elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
  5130. elf_elfheader (obfd)->e_flags |=
  5131. sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
  5132. if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
  5133. {
  5134. _bfd_error_handler (_("%pB: attempt to mix FDPIC and non-FDPIC objects"),
  5135. ibfd);
  5136. bfd_set_error (bfd_error_bad_value);
  5137. return false;
  5138. }
  5139. return true;
  5140. }
  5141. #endif /* not sh_elf_merge_private_data */
  5142. /* Override the generic function because we need to store sh_elf_obj_tdata
  5143. as the specific tdata. We set also the machine architecture from flags
  5144. here. */
  5145. static bool
  5146. sh_elf_object_p (bfd *abfd)
  5147. {
  5148. if (! sh_elf_set_mach_from_flags (abfd))
  5149. return false;
  5150. return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
  5151. == fdpic_object_p (abfd));
  5152. }
  5153. /* Finish up dynamic symbol handling. We set the contents of various
  5154. dynamic sections here. */
  5155. static bool
  5156. sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
  5157. struct elf_link_hash_entry *h,
  5158. Elf_Internal_Sym *sym)
  5159. {
  5160. struct elf_sh_link_hash_table *htab;
  5161. htab = sh_elf_hash_table (info);
  5162. if (htab == NULL)
  5163. return false;
  5164. if (h->plt.offset != (bfd_vma) -1)
  5165. {
  5166. asection *splt;
  5167. asection *sgotplt;
  5168. asection *srelplt;
  5169. bfd_vma plt_index;
  5170. bfd_vma got_offset;
  5171. Elf_Internal_Rela rel;
  5172. bfd_byte *loc;
  5173. const struct elf_sh_plt_info *plt_info;
  5174. /* This symbol has an entry in the procedure linkage table. Set
  5175. it up. */
  5176. BFD_ASSERT (h->dynindx != -1);
  5177. splt = htab->root.splt;
  5178. sgotplt = htab->root.sgotplt;
  5179. srelplt = htab->root.srelplt;
  5180. BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
  5181. /* Get the index in the procedure linkage table which
  5182. corresponds to this symbol. This is the index of this symbol
  5183. in all the symbols for which we are making plt entries. The
  5184. first entry in the procedure linkage table is reserved. */
  5185. plt_index = get_plt_index (htab->plt_info, h->plt.offset);
  5186. plt_info = htab->plt_info;
  5187. if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
  5188. plt_info = plt_info->short_plt;
  5189. /* Get the offset into the .got table of the entry that
  5190. corresponds to this function. */
  5191. if (htab->fdpic_p)
  5192. /* The offset must be relative to the GOT symbol, twelve bytes
  5193. before the end of .got.plt. Each descriptor is eight
  5194. bytes. */
  5195. got_offset = plt_index * 8 + 12 - sgotplt->size;
  5196. else
  5197. /* Each .got entry is 4 bytes. The first three are
  5198. reserved. */
  5199. got_offset = (plt_index + 3) * 4;
  5200. #ifdef GOT_BIAS
  5201. if (bfd_link_pic (info))
  5202. got_offset -= GOT_BIAS;
  5203. #endif
  5204. /* Fill in the entry in the procedure linkage table. */
  5205. memcpy (splt->contents + h->plt.offset,
  5206. plt_info->symbol_entry,
  5207. plt_info->symbol_entry_size);
  5208. if (bfd_link_pic (info) || htab->fdpic_p)
  5209. {
  5210. if (plt_info->symbol_fields.got20)
  5211. {
  5212. bfd_reloc_status_type r;
  5213. r = install_movi20_field (output_bfd, got_offset,
  5214. splt->owner, splt, splt->contents,
  5215. h->plt.offset
  5216. + plt_info->symbol_fields.got_entry);
  5217. BFD_ASSERT (r == bfd_reloc_ok);
  5218. }
  5219. else
  5220. install_plt_field (output_bfd, false, got_offset,
  5221. (splt->contents
  5222. + h->plt.offset
  5223. + plt_info->symbol_fields.got_entry));
  5224. }
  5225. else
  5226. {
  5227. BFD_ASSERT (!plt_info->symbol_fields.got20);
  5228. install_plt_field (output_bfd, false,
  5229. (sgotplt->output_section->vma
  5230. + sgotplt->output_offset
  5231. + got_offset),
  5232. (splt->contents
  5233. + h->plt.offset
  5234. + plt_info->symbol_fields.got_entry));
  5235. if (htab->root.target_os == is_vxworks)
  5236. {
  5237. unsigned int reachable_plts, plts_per_4k;
  5238. int distance;
  5239. /* Divide the PLT into groups. The first group contains
  5240. REACHABLE_PLTS entries and the other groups contain
  5241. PLTS_PER_4K entries. Entries in the first group can
  5242. branch directly to .plt; those in later groups branch
  5243. to the last element of the previous group. */
  5244. /* ??? It would be better to create multiple copies of
  5245. the common resolver stub. */
  5246. reachable_plts = ((4096
  5247. - plt_info->plt0_entry_size
  5248. - (plt_info->symbol_fields.plt + 4))
  5249. / plt_info->symbol_entry_size) + 1;
  5250. plts_per_4k = (4096 / plt_info->symbol_entry_size);
  5251. if (plt_index < reachable_plts)
  5252. distance = -(h->plt.offset
  5253. + plt_info->symbol_fields.plt);
  5254. else
  5255. distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
  5256. * plt_info->symbol_entry_size);
  5257. /* Install the 'bra' with this offset. */
  5258. bfd_put_16 (output_bfd,
  5259. 0xa000 | (0x0fff & ((distance - 4) / 2)),
  5260. (splt->contents
  5261. + h->plt.offset
  5262. + plt_info->symbol_fields.plt));
  5263. }
  5264. else
  5265. install_plt_field (output_bfd, true,
  5266. splt->output_section->vma + splt->output_offset,
  5267. (splt->contents
  5268. + h->plt.offset
  5269. + plt_info->symbol_fields.plt));
  5270. }
  5271. /* Make got_offset relative to the start of .got.plt. */
  5272. #ifdef GOT_BIAS
  5273. if (bfd_link_pic (info))
  5274. got_offset += GOT_BIAS;
  5275. #endif
  5276. if (htab->fdpic_p)
  5277. got_offset = plt_index * 8;
  5278. if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
  5279. install_plt_field (output_bfd, false,
  5280. plt_index * sizeof (Elf32_External_Rela),
  5281. (splt->contents
  5282. + h->plt.offset
  5283. + plt_info->symbol_fields.reloc_offset));
  5284. /* Fill in the entry in the global offset table. */
  5285. bfd_put_32 (output_bfd,
  5286. (splt->output_section->vma
  5287. + splt->output_offset
  5288. + h->plt.offset
  5289. + plt_info->symbol_resolve_offset),
  5290. sgotplt->contents + got_offset);
  5291. if (htab->fdpic_p)
  5292. bfd_put_32 (output_bfd,
  5293. sh_elf_osec_to_segment (output_bfd, splt->output_section),
  5294. sgotplt->contents + got_offset + 4);
  5295. /* Fill in the entry in the .rela.plt section. */
  5296. rel.r_offset = (sgotplt->output_section->vma
  5297. + sgotplt->output_offset
  5298. + got_offset);
  5299. if (htab->fdpic_p)
  5300. rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
  5301. else
  5302. rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
  5303. rel.r_addend = 0;
  5304. #ifdef GOT_BIAS
  5305. rel.r_addend = GOT_BIAS;
  5306. #endif
  5307. loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
  5308. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  5309. if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
  5310. {
  5311. /* Create the .rela.plt.unloaded relocations for this PLT entry.
  5312. Begin by pointing LOC to the first such relocation. */
  5313. loc = (htab->srelplt2->contents
  5314. + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
  5315. /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
  5316. for the PLT entry's pointer to the .got.plt entry. */
  5317. rel.r_offset = (splt->output_section->vma
  5318. + splt->output_offset
  5319. + h->plt.offset
  5320. + plt_info->symbol_fields.got_entry);
  5321. rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
  5322. rel.r_addend = got_offset;
  5323. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  5324. loc += sizeof (Elf32_External_Rela);
  5325. /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
  5326. the .got.plt entry, which initially points to .plt. */
  5327. rel.r_offset = (sgotplt->output_section->vma
  5328. + sgotplt->output_offset
  5329. + got_offset);
  5330. rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
  5331. rel.r_addend = 0;
  5332. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  5333. }
  5334. if (!h->def_regular)
  5335. {
  5336. /* Mark the symbol as undefined, rather than as defined in
  5337. the .plt section. Leave the value alone. */
  5338. sym->st_shndx = SHN_UNDEF;
  5339. }
  5340. }
  5341. if (h->got.offset != (bfd_vma) -1
  5342. && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
  5343. && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
  5344. && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
  5345. {
  5346. asection *sgot;
  5347. asection *srelgot;
  5348. Elf_Internal_Rela rel;
  5349. bfd_byte *loc;
  5350. /* This symbol has an entry in the global offset table. Set it
  5351. up. */
  5352. sgot = htab->root.sgot;
  5353. srelgot = htab->root.srelgot;
  5354. BFD_ASSERT (sgot != NULL && srelgot != NULL);
  5355. rel.r_offset = (sgot->output_section->vma
  5356. + sgot->output_offset
  5357. + (h->got.offset &~ (bfd_vma) 1));
  5358. /* If this is a static link, or it is a -Bsymbolic link and the
  5359. symbol is defined locally or was forced to be local because
  5360. of a version file, we just want to emit a RELATIVE reloc.
  5361. The entry in the global offset table will already have been
  5362. initialized in the relocate_section function. */
  5363. if (bfd_link_pic (info)
  5364. && SYMBOL_REFERENCES_LOCAL (info, h))
  5365. {
  5366. if (htab->fdpic_p)
  5367. {
  5368. asection *sec = h->root.u.def.section;
  5369. int dynindx
  5370. = elf_section_data (sec->output_section)->dynindx;
  5371. rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
  5372. rel.r_addend = (h->root.u.def.value
  5373. + h->root.u.def.section->output_offset);
  5374. }
  5375. else
  5376. {
  5377. rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
  5378. rel.r_addend = (h->root.u.def.value
  5379. + h->root.u.def.section->output_section->vma
  5380. + h->root.u.def.section->output_offset);
  5381. }
  5382. }
  5383. else
  5384. {
  5385. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
  5386. rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
  5387. rel.r_addend = 0;
  5388. }
  5389. loc = srelgot->contents;
  5390. loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  5391. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  5392. }
  5393. if (h->needs_copy)
  5394. {
  5395. asection *s;
  5396. Elf_Internal_Rela rel;
  5397. bfd_byte *loc;
  5398. /* This symbol needs a copy reloc. Set it up. */
  5399. BFD_ASSERT (h->dynindx != -1
  5400. && (h->root.type == bfd_link_hash_defined
  5401. || h->root.type == bfd_link_hash_defweak));
  5402. s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
  5403. BFD_ASSERT (s != NULL);
  5404. rel.r_offset = (h->root.u.def.value
  5405. + h->root.u.def.section->output_section->vma
  5406. + h->root.u.def.section->output_offset);
  5407. rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
  5408. rel.r_addend = 0;
  5409. loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
  5410. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  5411. }
  5412. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
  5413. _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
  5414. ".got" section. */
  5415. if (h == htab->root.hdynamic
  5416. || (htab->root.target_os != is_vxworks && h == htab->root.hgot))
  5417. sym->st_shndx = SHN_ABS;
  5418. return true;
  5419. }
  5420. /* Finish up the dynamic sections. */
  5421. static bool
  5422. sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
  5423. {
  5424. struct elf_sh_link_hash_table *htab;
  5425. asection *sgotplt;
  5426. asection *sdyn;
  5427. htab = sh_elf_hash_table (info);
  5428. if (htab == NULL)
  5429. return false;
  5430. sgotplt = htab->root.sgotplt;
  5431. sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
  5432. if (htab->root.dynamic_sections_created)
  5433. {
  5434. asection *splt;
  5435. Elf32_External_Dyn *dyncon, *dynconend;
  5436. BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
  5437. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  5438. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  5439. for (; dyncon < dynconend; dyncon++)
  5440. {
  5441. Elf_Internal_Dyn dyn;
  5442. asection *s;
  5443. bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
  5444. switch (dyn.d_tag)
  5445. {
  5446. default:
  5447. if (htab->root.target_os == is_vxworks
  5448. && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
  5449. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5450. break;
  5451. case DT_PLTGOT:
  5452. BFD_ASSERT (htab->root.hgot != NULL);
  5453. s = htab->root.hgot->root.u.def.section;
  5454. dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
  5455. + s->output_section->vma + s->output_offset;
  5456. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5457. break;
  5458. case DT_JMPREL:
  5459. s = htab->root.srelplt->output_section;
  5460. BFD_ASSERT (s != NULL);
  5461. dyn.d_un.d_ptr = s->vma;
  5462. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5463. break;
  5464. case DT_PLTRELSZ:
  5465. s = htab->root.srelplt->output_section;
  5466. BFD_ASSERT (s != NULL);
  5467. dyn.d_un.d_val = s->size;
  5468. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5469. break;
  5470. }
  5471. }
  5472. /* Fill in the first entry in the procedure linkage table. */
  5473. splt = htab->root.splt;
  5474. if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
  5475. {
  5476. unsigned int i;
  5477. memcpy (splt->contents,
  5478. htab->plt_info->plt0_entry,
  5479. htab->plt_info->plt0_entry_size);
  5480. for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
  5481. if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
  5482. install_plt_field (output_bfd, false,
  5483. (sgotplt->output_section->vma
  5484. + sgotplt->output_offset
  5485. + (i * 4)),
  5486. (splt->contents
  5487. + htab->plt_info->plt0_got_fields[i]));
  5488. if (htab->root.target_os == is_vxworks)
  5489. {
  5490. /* Finalize the .rela.plt.unloaded contents. */
  5491. Elf_Internal_Rela rel;
  5492. bfd_byte *loc;
  5493. /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
  5494. first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
  5495. loc = htab->srelplt2->contents;
  5496. rel.r_offset = (splt->output_section->vma
  5497. + splt->output_offset
  5498. + htab->plt_info->plt0_got_fields[2]);
  5499. rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
  5500. rel.r_addend = 8;
  5501. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  5502. loc += sizeof (Elf32_External_Rela);
  5503. /* Fix up the remaining .rela.plt.unloaded relocations.
  5504. They may have the wrong symbol index for _G_O_T_ or
  5505. _P_L_T_ depending on the order in which symbols were
  5506. output. */
  5507. while (loc < htab->srelplt2->contents + htab->srelplt2->size)
  5508. {
  5509. /* The PLT entry's pointer to the .got.plt slot. */
  5510. bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
  5511. rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
  5512. R_SH_DIR32);
  5513. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  5514. loc += sizeof (Elf32_External_Rela);
  5515. /* The .got.plt slot's pointer to .plt. */
  5516. bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
  5517. rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
  5518. R_SH_DIR32);
  5519. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  5520. loc += sizeof (Elf32_External_Rela);
  5521. }
  5522. }
  5523. /* UnixWare sets the entsize of .plt to 4, although that doesn't
  5524. really seem like the right value. */
  5525. elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
  5526. }
  5527. }
  5528. /* Fill in the first three entries in the global offset table. */
  5529. if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
  5530. {
  5531. if (sdyn == NULL)
  5532. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
  5533. else
  5534. bfd_put_32 (output_bfd,
  5535. sdyn->output_section->vma + sdyn->output_offset,
  5536. sgotplt->contents);
  5537. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
  5538. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
  5539. }
  5540. if (sgotplt && sgotplt->size > 0)
  5541. elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
  5542. /* At the very end of the .rofixup section is a pointer to the GOT. */
  5543. if (htab->fdpic_p && htab->srofixup != NULL)
  5544. {
  5545. struct elf_link_hash_entry *hgot = htab->root.hgot;
  5546. bfd_vma got_value = hgot->root.u.def.value
  5547. + hgot->root.u.def.section->output_section->vma
  5548. + hgot->root.u.def.section->output_offset;
  5549. sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
  5550. /* Make sure we allocated and generated the same number of fixups. */
  5551. BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
  5552. }
  5553. if (htab->srelfuncdesc)
  5554. BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
  5555. == htab->srelfuncdesc->size);
  5556. if (htab->root.srelgot)
  5557. BFD_ASSERT (htab->root.srelgot->reloc_count * sizeof (Elf32_External_Rela)
  5558. == htab->root.srelgot->size);
  5559. return true;
  5560. }
  5561. static enum elf_reloc_type_class
  5562. sh_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  5563. const asection *rel_sec ATTRIBUTE_UNUSED,
  5564. const Elf_Internal_Rela *rela)
  5565. {
  5566. switch ((int) ELF32_R_TYPE (rela->r_info))
  5567. {
  5568. case R_SH_RELATIVE:
  5569. return reloc_class_relative;
  5570. case R_SH_JMP_SLOT:
  5571. return reloc_class_plt;
  5572. case R_SH_COPY:
  5573. return reloc_class_copy;
  5574. default:
  5575. return reloc_class_normal;
  5576. }
  5577. }
  5578. #if !defined SH_TARGET_ALREADY_DEFINED
  5579. /* Support for Linux core dump NOTE sections. */
  5580. static bool
  5581. elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  5582. {
  5583. int offset;
  5584. unsigned int size;
  5585. switch (note->descsz)
  5586. {
  5587. default:
  5588. return false;
  5589. case 168: /* Linux/SH */
  5590. /* pr_cursig */
  5591. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  5592. /* pr_pid */
  5593. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  5594. /* pr_reg */
  5595. offset = 72;
  5596. size = 92;
  5597. break;
  5598. }
  5599. /* Make a ".reg/999" section. */
  5600. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  5601. size, note->descpos + offset);
  5602. }
  5603. static bool
  5604. elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  5605. {
  5606. switch (note->descsz)
  5607. {
  5608. default:
  5609. return false;
  5610. case 124: /* Linux/SH elf_prpsinfo */
  5611. elf_tdata (abfd)->core->program
  5612. = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  5613. elf_tdata (abfd)->core->command
  5614. = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  5615. }
  5616. /* Note that for some reason, a spurious space is tacked
  5617. onto the end of the args in some (at least one anyway)
  5618. implementations, so strip it off if it exists. */
  5619. {
  5620. char *command = elf_tdata (abfd)->core->command;
  5621. int n = strlen (command);
  5622. if (0 < n && command[n - 1] == ' ')
  5623. command[n - 1] = '\0';
  5624. }
  5625. return true;
  5626. }
  5627. #endif /* not SH_TARGET_ALREADY_DEFINED */
  5628. /* Return address for Ith PLT stub in section PLT, for relocation REL
  5629. or (bfd_vma) -1 if it should not be included. */
  5630. static bfd_vma
  5631. sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
  5632. const arelent *rel ATTRIBUTE_UNUSED)
  5633. {
  5634. const struct elf_sh_plt_info *plt_info;
  5635. plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
  5636. return plt->vma + get_plt_offset (plt_info, i);
  5637. }
  5638. /* Decide whether to attempt to turn absptr or lsda encodings in
  5639. shared libraries into pcrel within the given input section. */
  5640. static bool
  5641. sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
  5642. struct bfd_link_info *info,
  5643. asection *eh_frame_section ATTRIBUTE_UNUSED)
  5644. {
  5645. struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
  5646. /* We can't use PC-relative encodings in FDPIC binaries, in general. */
  5647. if (htab->fdpic_p)
  5648. return false;
  5649. return true;
  5650. }
  5651. /* Adjust the contents of an eh_frame_hdr section before they're output. */
  5652. static bfd_byte
  5653. sh_elf_encode_eh_address (bfd *abfd,
  5654. struct bfd_link_info *info,
  5655. asection *osec, bfd_vma offset,
  5656. asection *loc_sec, bfd_vma loc_offset,
  5657. bfd_vma *encoded)
  5658. {
  5659. struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
  5660. struct elf_link_hash_entry *h;
  5661. if (!htab->fdpic_p)
  5662. return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
  5663. loc_offset, encoded);
  5664. h = htab->root.hgot;
  5665. BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
  5666. if (! h || (sh_elf_osec_to_segment (abfd, osec)
  5667. == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
  5668. return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
  5669. loc_sec, loc_offset, encoded);
  5670. BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
  5671. == (sh_elf_osec_to_segment
  5672. (abfd, h->root.u.def.section->output_section)));
  5673. *encoded = osec->vma + offset
  5674. - (h->root.u.def.value
  5675. + h->root.u.def.section->output_section->vma
  5676. + h->root.u.def.section->output_offset);
  5677. return DW_EH_PE_datarel | DW_EH_PE_sdata4;
  5678. }
  5679. #if !defined SH_TARGET_ALREADY_DEFINED
  5680. #define TARGET_BIG_SYM sh_elf32_vec
  5681. #define TARGET_BIG_NAME "elf32-sh"
  5682. #define TARGET_LITTLE_SYM sh_elf32_le_vec
  5683. #define TARGET_LITTLE_NAME "elf32-shl"
  5684. #endif
  5685. #define ELF_ARCH bfd_arch_sh
  5686. #define ELF_TARGET_ID SH_ELF_DATA
  5687. #define ELF_MACHINE_CODE EM_SH
  5688. #ifdef __QNXTARGET__
  5689. #define ELF_MAXPAGESIZE 0x1000
  5690. #else
  5691. #define ELF_MAXPAGESIZE 0x80
  5692. #endif
  5693. #define elf_symbol_leading_char '_'
  5694. #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
  5695. #define bfd_elf32_bfd_reloc_name_lookup \
  5696. sh_elf_reloc_name_lookup
  5697. #define elf_info_to_howto sh_elf_info_to_howto
  5698. #define bfd_elf32_bfd_relax_section sh_elf_relax_section
  5699. #define elf_backend_relocate_section sh_elf_relocate_section
  5700. #define bfd_elf32_bfd_get_relocated_section_contents \
  5701. sh_elf_get_relocated_section_contents
  5702. #define bfd_elf32_mkobject sh_elf_mkobject
  5703. #define elf_backend_object_p sh_elf_object_p
  5704. #define bfd_elf32_bfd_copy_private_bfd_data \
  5705. sh_elf_copy_private_data
  5706. #define bfd_elf32_bfd_merge_private_bfd_data \
  5707. sh_elf_merge_private_data
  5708. #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
  5709. #define elf_backend_check_relocs sh_elf_check_relocs
  5710. #define elf_backend_copy_indirect_symbol \
  5711. sh_elf_copy_indirect_symbol
  5712. #define elf_backend_create_dynamic_sections \
  5713. sh_elf_create_dynamic_sections
  5714. #define bfd_elf32_bfd_link_hash_table_create \
  5715. sh_elf_link_hash_table_create
  5716. #define elf_backend_adjust_dynamic_symbol \
  5717. sh_elf_adjust_dynamic_symbol
  5718. #define elf_backend_always_size_sections \
  5719. sh_elf_always_size_sections
  5720. #define elf_backend_size_dynamic_sections \
  5721. sh_elf_size_dynamic_sections
  5722. #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
  5723. #define elf_backend_finish_dynamic_symbol \
  5724. sh_elf_finish_dynamic_symbol
  5725. #define elf_backend_finish_dynamic_sections \
  5726. sh_elf_finish_dynamic_sections
  5727. #define elf_backend_reloc_type_class sh_elf_reloc_type_class
  5728. #define elf_backend_plt_sym_val sh_elf_plt_sym_val
  5729. #define elf_backend_can_make_relative_eh_frame \
  5730. sh_elf_use_relative_eh_frame
  5731. #define elf_backend_can_make_lsda_relative_eh_frame \
  5732. sh_elf_use_relative_eh_frame
  5733. #define elf_backend_encode_eh_address \
  5734. sh_elf_encode_eh_address
  5735. #define elf_backend_stack_align 8
  5736. #define elf_backend_can_gc_sections 1
  5737. #define elf_backend_can_refcount 1
  5738. #define elf_backend_want_got_plt 1
  5739. #define elf_backend_plt_readonly 1
  5740. #define elf_backend_want_plt_sym 0
  5741. #define elf_backend_got_header_size 12
  5742. #define elf_backend_dtrel_excludes_plt 1
  5743. #define elf_backend_linux_prpsinfo32_ugid16 true
  5744. #if !defined SH_TARGET_ALREADY_DEFINED
  5745. #include "elf32-target.h"
  5746. /* NetBSD support. */
  5747. #undef TARGET_BIG_SYM
  5748. #define TARGET_BIG_SYM sh_elf32_nbsd_vec
  5749. #undef TARGET_BIG_NAME
  5750. #define TARGET_BIG_NAME "elf32-sh-nbsd"
  5751. #undef TARGET_LITTLE_SYM
  5752. #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
  5753. #undef TARGET_LITTLE_NAME
  5754. #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
  5755. #undef ELF_MAXPAGESIZE
  5756. #define ELF_MAXPAGESIZE 0x10000
  5757. #undef ELF_COMMONPAGESIZE
  5758. #undef elf_symbol_leading_char
  5759. #define elf_symbol_leading_char 0
  5760. #undef elf32_bed
  5761. #define elf32_bed elf32_sh_nbsd_bed
  5762. #include "elf32-target.h"
  5763. /* Linux support. */
  5764. #undef TARGET_BIG_SYM
  5765. #define TARGET_BIG_SYM sh_elf32_linux_be_vec
  5766. #undef TARGET_BIG_NAME
  5767. #define TARGET_BIG_NAME "elf32-shbig-linux"
  5768. #undef TARGET_LITTLE_SYM
  5769. #define TARGET_LITTLE_SYM sh_elf32_linux_vec
  5770. #undef TARGET_LITTLE_NAME
  5771. #define TARGET_LITTLE_NAME "elf32-sh-linux"
  5772. #undef ELF_COMMONPAGESIZE
  5773. #define ELF_COMMONPAGESIZE 0x1000
  5774. #undef elf_backend_grok_prstatus
  5775. #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
  5776. #undef elf_backend_grok_psinfo
  5777. #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
  5778. #undef elf32_bed
  5779. #define elf32_bed elf32_sh_lin_bed
  5780. #include "elf32-target.h"
  5781. /* FDPIC support. */
  5782. #undef TARGET_BIG_SYM
  5783. #define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
  5784. #undef TARGET_BIG_NAME
  5785. #define TARGET_BIG_NAME "elf32-shbig-fdpic"
  5786. #undef TARGET_LITTLE_SYM
  5787. #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
  5788. #undef TARGET_LITTLE_NAME
  5789. #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
  5790. #undef elf32_bed
  5791. #define elf32_bed elf32_sh_fd_bed
  5792. #include "elf32-target.h"
  5793. /* VxWorks support. */
  5794. #undef TARGET_BIG_SYM
  5795. #define TARGET_BIG_SYM sh_elf32_vxworks_vec
  5796. #undef TARGET_BIG_NAME
  5797. #define TARGET_BIG_NAME "elf32-sh-vxworks"
  5798. #undef TARGET_LITTLE_SYM
  5799. #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
  5800. #undef TARGET_LITTLE_NAME
  5801. #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
  5802. #undef elf32_bed
  5803. #define elf32_bed elf32_sh_vxworks_bed
  5804. #undef elf_backend_want_plt_sym
  5805. #define elf_backend_want_plt_sym 1
  5806. #undef elf_symbol_leading_char
  5807. #define elf_symbol_leading_char '_'
  5808. #define elf_backend_want_got_underscore 1
  5809. #undef elf_backend_grok_prstatus
  5810. #undef elf_backend_grok_psinfo
  5811. #undef elf_backend_add_symbol_hook
  5812. #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
  5813. #undef elf_backend_link_output_symbol_hook
  5814. #define elf_backend_link_output_symbol_hook \
  5815. elf_vxworks_link_output_symbol_hook
  5816. #undef elf_backend_emit_relocs
  5817. #define elf_backend_emit_relocs elf_vxworks_emit_relocs
  5818. #undef elf_backend_final_write_processing
  5819. #define elf_backend_final_write_processing \
  5820. elf_vxworks_final_write_processing
  5821. #undef ELF_MAXPAGESIZE
  5822. #define ELF_MAXPAGESIZE 0x1000
  5823. #undef ELF_COMMONPAGESIZE
  5824. #undef ELF_TARGET_OS
  5825. #define ELF_TARGET_OS is_vxworks
  5826. #include "elf32-target.h"
  5827. #endif /* not SH_TARGET_ALREADY_DEFINED */