powerpc.cc 384 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071
  1. // powerpc.cc -- powerpc target support for gold.
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. // Written by David S. Miller <davem@davemloft.net>
  4. // and David Edelsohn <edelsohn@gnu.org>
  5. // This file is part of gold.
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 3 of the License, or
  9. // (at your option) any later version.
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. // MA 02110-1301, USA.
  18. #include "gold.h"
  19. #include <set>
  20. #include <algorithm>
  21. #include "elfcpp.h"
  22. #include "dwarf.h"
  23. #include "parameters.h"
  24. #include "reloc.h"
  25. #include "powerpc.h"
  26. #include "object.h"
  27. #include "symtab.h"
  28. #include "layout.h"
  29. #include "output.h"
  30. #include "copy-relocs.h"
  31. #include "target.h"
  32. #include "target-reloc.h"
  33. #include "target-select.h"
  34. #include "tls.h"
  35. #include "errors.h"
  36. #include "gc.h"
  37. #include "attributes.h"
  38. namespace
  39. {
  40. using namespace gold;
  41. template<int size, bool big_endian>
  42. class Output_data_plt_powerpc;
  43. template<int size, bool big_endian>
  44. class Output_data_brlt_powerpc;
  45. template<int size, bool big_endian>
  46. class Output_data_got_powerpc;
  47. template<int size, bool big_endian>
  48. class Output_data_glink;
  49. template<int size, bool big_endian>
  50. class Stub_table;
  51. template<int size, bool big_endian>
  52. class Output_data_save_res;
  53. template<int size, bool big_endian>
  54. class Target_powerpc;
  55. struct Stub_table_owner
  56. {
  57. Stub_table_owner()
  58. : output_section(NULL), owner(NULL)
  59. { }
  60. Output_section* output_section;
  61. const Output_section::Input_section* owner;
  62. };
  63. template<int size>
  64. inline bool is_branch_reloc(unsigned int);
  65. template<int size>
  66. inline bool is_plt16_reloc(unsigned int);
  67. // Counter incremented on every Powerpc_relobj constructed.
  68. static uint32_t object_id = 0;
  69. template<int size, bool big_endian>
  70. class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
  71. {
  72. public:
  73. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  74. typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
  75. typedef Unordered_map<Address, Section_refs> Access_from;
  76. Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
  77. const typename elfcpp::Ehdr<size, big_endian>& ehdr)
  78. : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
  79. uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
  80. has_small_toc_reloc_(false), opd_valid_(false),
  81. e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
  82. access_from_map_(), has14_(), stub_table_index_(), st_other_(),
  83. attributes_section_data_(NULL)
  84. {
  85. this->set_abiversion(0);
  86. }
  87. ~Powerpc_relobj()
  88. { delete this->attributes_section_data_; }
  89. // Read the symbols then set up st_other vector.
  90. void
  91. do_read_symbols(Read_symbols_data*);
  92. // Arrange to always relocate .toc first.
  93. virtual void
  94. do_relocate_sections(
  95. const Symbol_table* symtab, const Layout* layout,
  96. const unsigned char* pshdrs, Output_file* of,
  97. typename Sized_relobj_file<size, big_endian>::Views* pviews);
  98. // The .toc section index.
  99. unsigned int
  100. toc_shndx() const
  101. {
  102. return this->toc_;
  103. }
  104. // Mark .toc entry at OFF as not optimizable.
  105. void
  106. set_no_toc_opt(Address off)
  107. {
  108. if (this->no_toc_opt_.empty())
  109. this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
  110. / (size / 8));
  111. off /= size / 8;
  112. if (off < this->no_toc_opt_.size())
  113. this->no_toc_opt_[off] = true;
  114. }
  115. // Mark the entire .toc as not optimizable.
  116. void
  117. set_no_toc_opt()
  118. {
  119. this->no_toc_opt_.resize(1);
  120. this->no_toc_opt_[0] = true;
  121. }
  122. // Return true if code using the .toc entry at OFF should not be edited.
  123. bool
  124. no_toc_opt(Address off) const
  125. {
  126. if (this->no_toc_opt_.empty())
  127. return false;
  128. off /= size / 8;
  129. if (off >= this->no_toc_opt_.size())
  130. return true;
  131. return this->no_toc_opt_[off];
  132. }
  133. // The .got2 section shndx.
  134. unsigned int
  135. got2_shndx() const
  136. {
  137. if (size == 32)
  138. return this->special_;
  139. else
  140. return 0;
  141. }
  142. // The .opd section shndx.
  143. unsigned int
  144. opd_shndx() const
  145. {
  146. if (size == 32)
  147. return 0;
  148. else
  149. return this->special_;
  150. }
  151. // Init OPD entry arrays.
  152. void
  153. init_opd(size_t opd_size)
  154. {
  155. size_t count = this->opd_ent_ndx(opd_size);
  156. this->opd_ent_.resize(count);
  157. }
  158. // Return section and offset of function entry for .opd + R_OFF.
  159. unsigned int
  160. get_opd_ent(Address r_off, Address* value = NULL) const
  161. {
  162. size_t ndx = this->opd_ent_ndx(r_off);
  163. gold_assert(ndx < this->opd_ent_.size());
  164. gold_assert(this->opd_ent_[ndx].shndx != 0);
  165. if (value != NULL)
  166. *value = this->opd_ent_[ndx].off;
  167. return this->opd_ent_[ndx].shndx;
  168. }
  169. // Set section and offset of function entry for .opd + R_OFF.
  170. void
  171. set_opd_ent(Address r_off, unsigned int shndx, Address value)
  172. {
  173. size_t ndx = this->opd_ent_ndx(r_off);
  174. gold_assert(ndx < this->opd_ent_.size());
  175. this->opd_ent_[ndx].shndx = shndx;
  176. this->opd_ent_[ndx].off = value;
  177. }
  178. // Return discard flag for .opd + R_OFF.
  179. bool
  180. get_opd_discard(Address r_off) const
  181. {
  182. size_t ndx = this->opd_ent_ndx(r_off);
  183. gold_assert(ndx < this->opd_ent_.size());
  184. return this->opd_ent_[ndx].discard;
  185. }
  186. // Set discard flag for .opd + R_OFF.
  187. void
  188. set_opd_discard(Address r_off)
  189. {
  190. size_t ndx = this->opd_ent_ndx(r_off);
  191. gold_assert(ndx < this->opd_ent_.size());
  192. this->opd_ent_[ndx].discard = true;
  193. }
  194. bool
  195. opd_valid() const
  196. { return this->opd_valid_; }
  197. void
  198. set_opd_valid()
  199. { this->opd_valid_ = true; }
  200. // Examine .rela.opd to build info about function entry points.
  201. void
  202. scan_opd_relocs(size_t reloc_count,
  203. const unsigned char* prelocs,
  204. const unsigned char* plocal_syms);
  205. // Returns true if a code sequence loading a TOC entry can be
  206. // converted into code calculating a TOC pointer relative offset.
  207. bool
  208. make_toc_relative(Target_powerpc<size, big_endian>* target,
  209. Address* value);
  210. bool
  211. make_got_relative(Target_powerpc<size, big_endian>* target,
  212. const Symbol_value<size>* psymval,
  213. Address addend,
  214. Address* value);
  215. // Perform the Sized_relobj_file method, then set up opd info from
  216. // .opd relocs.
  217. void
  218. do_read_relocs(Read_relocs_data*);
  219. bool
  220. do_find_special_sections(Read_symbols_data* sd);
  221. // Adjust this local symbol value. Return false if the symbol
  222. // should be discarded from the output file.
  223. bool
  224. do_adjust_local_symbol(Symbol_value<size>* lv) const
  225. {
  226. if (size == 64 && this->opd_shndx() != 0)
  227. {
  228. bool is_ordinary;
  229. if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
  230. return true;
  231. if (this->get_opd_discard(lv->input_value()))
  232. return false;
  233. }
  234. return true;
  235. }
  236. Access_from*
  237. access_from_map()
  238. { return &this->access_from_map_; }
  239. // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
  240. // section at DST_OFF.
  241. void
  242. add_reference(Relobj* src_obj,
  243. unsigned int src_indx,
  244. typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
  245. {
  246. Section_id src_id(src_obj, src_indx);
  247. this->access_from_map_[dst_off].insert(src_id);
  248. }
  249. // Add a reference to the code section specified by the .opd entry
  250. // at DST_OFF
  251. void
  252. add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
  253. {
  254. size_t ndx = this->opd_ent_ndx(dst_off);
  255. if (ndx >= this->opd_ent_.size())
  256. this->opd_ent_.resize(ndx + 1);
  257. this->opd_ent_[ndx].gc_mark = true;
  258. }
  259. void
  260. process_gc_mark(Symbol_table* symtab)
  261. {
  262. for (size_t i = 0; i < this->opd_ent_.size(); i++)
  263. if (this->opd_ent_[i].gc_mark)
  264. {
  265. unsigned int shndx = this->opd_ent_[i].shndx;
  266. symtab->gc()->worklist().push_back(Section_id(this, shndx));
  267. }
  268. }
  269. void
  270. set_has_small_toc_reloc()
  271. { has_small_toc_reloc_ = true; }
  272. bool
  273. has_small_toc_reloc() const
  274. { return has_small_toc_reloc_; }
  275. void
  276. set_has_14bit_branch(unsigned int shndx)
  277. {
  278. if (shndx >= this->has14_.size())
  279. this->has14_.resize(shndx + 1);
  280. this->has14_[shndx] = true;
  281. }
  282. bool
  283. has_14bit_branch(unsigned int shndx) const
  284. { return shndx < this->has14_.size() && this->has14_[shndx]; }
  285. void
  286. set_stub_table(unsigned int shndx, unsigned int stub_index)
  287. {
  288. if (shndx >= this->stub_table_index_.size())
  289. this->stub_table_index_.resize(shndx + 1, -1);
  290. this->stub_table_index_[shndx] = stub_index;
  291. }
  292. Stub_table<size, big_endian>*
  293. stub_table(unsigned int shndx)
  294. {
  295. if (shndx < this->stub_table_index_.size())
  296. {
  297. Target_powerpc<size, big_endian>* target
  298. = static_cast<Target_powerpc<size, big_endian>*>(
  299. parameters->sized_target<size, big_endian>());
  300. unsigned int indx = this->stub_table_index_[shndx];
  301. if (indx < target->stub_tables().size())
  302. return target->stub_tables()[indx];
  303. }
  304. return NULL;
  305. }
  306. void
  307. clear_stub_table()
  308. {
  309. this->stub_table_index_.clear();
  310. }
  311. uint32_t
  312. uniq() const
  313. { return this->uniq_; }
  314. int
  315. abiversion() const
  316. { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
  317. // Set ABI version for input and output
  318. void
  319. set_abiversion(int ver);
  320. unsigned int
  321. st_other (unsigned int symndx) const
  322. {
  323. return this->st_other_[symndx];
  324. }
  325. unsigned int
  326. ppc64_local_entry_offset(const Symbol* sym) const
  327. { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
  328. unsigned int
  329. ppc64_local_entry_offset(unsigned int symndx) const
  330. { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
  331. bool
  332. ppc64_needs_toc(const Symbol* sym) const
  333. { return sym->nonvis() > 1 << 3; }
  334. bool
  335. ppc64_needs_toc(unsigned int symndx) const
  336. { return this->st_other_[symndx] > 1 << 5; }
  337. // The contents of the .gnu.attributes section if there is one.
  338. const Attributes_section_data*
  339. attributes_section_data() const
  340. { return this->attributes_section_data_; }
  341. private:
  342. struct Opd_ent
  343. {
  344. unsigned int shndx;
  345. bool discard : 1;
  346. bool gc_mark : 1;
  347. Address off;
  348. };
  349. // Return index into opd_ent_ array for .opd entry at OFF.
  350. // .opd entries are 24 bytes long, but they can be spaced 16 bytes
  351. // apart when the language doesn't use the last 8-byte word, the
  352. // environment pointer. Thus dividing the entry section offset by
  353. // 16 will give an index into opd_ent_ that works for either layout
  354. // of .opd. (It leaves some elements of the vector unused when .opd
  355. // entries are spaced 24 bytes apart, but we don't know the spacing
  356. // until relocations are processed, and in any case it is possible
  357. // for an object to have some entries spaced 16 bytes apart and
  358. // others 24 bytes apart.)
  359. size_t
  360. opd_ent_ndx(size_t off) const
  361. { return off >> 4;}
  362. // Per object unique identifier
  363. uint32_t uniq_;
  364. // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
  365. unsigned int special_;
  366. // For 64-bit the .rela.toc and .toc section shdnx.
  367. unsigned int relatoc_;
  368. unsigned int toc_;
  369. // For 64-bit, whether this object uses small model relocs to access
  370. // the toc.
  371. bool has_small_toc_reloc_;
  372. // Set at the start of gc_process_relocs, when we know opd_ent_
  373. // vector is valid. The flag could be made atomic and set in
  374. // do_read_relocs with memory_order_release and then tested with
  375. // memory_order_acquire, potentially resulting in fewer entries in
  376. // access_from_map_.
  377. bool opd_valid_;
  378. // Header e_flags
  379. elfcpp::Elf_Word e_flags_;
  380. // For 64-bit, an array with one entry per 64-bit word in the .toc
  381. // section, set if accesses using that word cannot be optimised.
  382. std::vector<bool> no_toc_opt_;
  383. // The first 8-byte word of an OPD entry gives the address of the
  384. // entry point of the function. Relocatable object files have a
  385. // relocation on this word. The following vector records the
  386. // section and offset specified by these relocations.
  387. std::vector<Opd_ent> opd_ent_;
  388. // References made to this object's .opd section when running
  389. // gc_process_relocs for another object, before the opd_ent_ vector
  390. // is valid for this object.
  391. Access_from access_from_map_;
  392. // Whether input section has a 14-bit branch reloc.
  393. std::vector<bool> has14_;
  394. // The stub table to use for a given input section.
  395. std::vector<unsigned int> stub_table_index_;
  396. // ELF st_other field for local symbols.
  397. std::vector<unsigned char> st_other_;
  398. // Object attributes if there is a .gnu.attributes section.
  399. Attributes_section_data* attributes_section_data_;
  400. };
  401. template<int size, bool big_endian>
  402. class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
  403. {
  404. public:
  405. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  406. Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
  407. const typename elfcpp::Ehdr<size, big_endian>& ehdr)
  408. : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
  409. opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
  410. attributes_section_data_(NULL)
  411. {
  412. this->set_abiversion(0);
  413. }
  414. ~Powerpc_dynobj()
  415. { delete this->attributes_section_data_; }
  416. // Call Sized_dynobj::do_read_symbols to read the symbols then
  417. // read .opd from a dynamic object, filling in opd_ent_ vector,
  418. void
  419. do_read_symbols(Read_symbols_data*);
  420. // The .opd section shndx.
  421. unsigned int
  422. opd_shndx() const
  423. {
  424. return this->opd_shndx_;
  425. }
  426. // The .opd section address.
  427. Address
  428. opd_address() const
  429. {
  430. return this->opd_address_;
  431. }
  432. // Init OPD entry arrays.
  433. void
  434. init_opd(size_t opd_size)
  435. {
  436. size_t count = this->opd_ent_ndx(opd_size);
  437. this->opd_ent_.resize(count);
  438. }
  439. // Return section and offset of function entry for .opd + R_OFF.
  440. unsigned int
  441. get_opd_ent(Address r_off, Address* value = NULL) const
  442. {
  443. size_t ndx = this->opd_ent_ndx(r_off);
  444. gold_assert(ndx < this->opd_ent_.size());
  445. gold_assert(this->opd_ent_[ndx].shndx != 0);
  446. if (value != NULL)
  447. *value = this->opd_ent_[ndx].off;
  448. return this->opd_ent_[ndx].shndx;
  449. }
  450. // Set section and offset of function entry for .opd + R_OFF.
  451. void
  452. set_opd_ent(Address r_off, unsigned int shndx, Address value)
  453. {
  454. size_t ndx = this->opd_ent_ndx(r_off);
  455. gold_assert(ndx < this->opd_ent_.size());
  456. this->opd_ent_[ndx].shndx = shndx;
  457. this->opd_ent_[ndx].off = value;
  458. }
  459. int
  460. abiversion() const
  461. { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
  462. // Set ABI version for input and output.
  463. void
  464. set_abiversion(int ver);
  465. // The contents of the .gnu.attributes section if there is one.
  466. const Attributes_section_data*
  467. attributes_section_data() const
  468. { return this->attributes_section_data_; }
  469. private:
  470. // Used to specify extent of executable sections.
  471. struct Sec_info
  472. {
  473. Sec_info(Address start_, Address len_, unsigned int shndx_)
  474. : start(start_), len(len_), shndx(shndx_)
  475. { }
  476. bool
  477. operator<(const Sec_info& that) const
  478. { return this->start < that.start; }
  479. Address start;
  480. Address len;
  481. unsigned int shndx;
  482. };
  483. struct Opd_ent
  484. {
  485. unsigned int shndx;
  486. Address off;
  487. };
  488. // Return index into opd_ent_ array for .opd entry at OFF.
  489. size_t
  490. opd_ent_ndx(size_t off) const
  491. { return off >> 4;}
  492. // For 64-bit the .opd section shndx and address.
  493. unsigned int opd_shndx_;
  494. Address opd_address_;
  495. // Header e_flags
  496. elfcpp::Elf_Word e_flags_;
  497. // The first 8-byte word of an OPD entry gives the address of the
  498. // entry point of the function. Records the section and offset
  499. // corresponding to the address. Note that in dynamic objects,
  500. // offset is *not* relative to the section.
  501. std::vector<Opd_ent> opd_ent_;
  502. // Object attributes if there is a .gnu.attributes section.
  503. Attributes_section_data* attributes_section_data_;
  504. };
  505. // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
  506. // base class will emit.
  507. template<int sh_type, int size, bool big_endian>
  508. class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
  509. {
  510. public:
  511. Powerpc_copy_relocs()
  512. : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
  513. { }
  514. // Emit any saved relocations which turn out to be needed. This is
  515. // called after all the relocs have been scanned.
  516. void
  517. emit(Output_data_reloc<sh_type, true, size, big_endian>*);
  518. };
  519. // The types of GOT entries needed for this platform.
  520. // These values are exposed to the ABI in an incremental link, but
  521. // powerpc does not support incremental linking as yet.
  522. enum Got_type
  523. {
  524. GOT_TYPE_STANDARD = 0,
  525. GOT_TYPE_TLSGD = 1, // double entry for @got@tlsgd
  526. GOT_TYPE_DTPREL = 2, // entry for @got@dtprel
  527. GOT_TYPE_TPREL = 3, // entry for @got@tprel
  528. GOT_TYPE_SMALL = 4,
  529. GOT_TYPE_SMALL_TLSGD = 5,
  530. GOT_TYPE_SMALL_DTPREL = 6,
  531. GOT_TYPE_SMALL_TPREL = 7
  532. };
  533. template<int size, bool big_endian>
  534. class Target_powerpc : public Sized_target<size, big_endian>
  535. {
  536. public:
  537. typedef
  538. Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
  539. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  540. typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
  541. typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
  542. static const Address invalid_address = static_cast<Address>(0) - 1;
  543. // Offset of tp and dtp pointers from start of TLS block.
  544. static const Address tp_offset = 0x7000;
  545. static const Address dtp_offset = 0x8000;
  546. Target_powerpc()
  547. : Sized_target<size, big_endian>(&powerpc_info),
  548. got_(NULL), biggot_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL),
  549. brlt_section_(NULL), glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
  550. tlsld_got_offset_(-1U),
  551. stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
  552. power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
  553. plt_localentry0_init_(false), has_localentry0_(false),
  554. has_tls_get_addr_opt_(false), no_tprel_opt_(false),
  555. relax_failed_(false), relax_fail_count_(0),
  556. stub_group_size_(0), savres_section_(0),
  557. tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
  558. attributes_section_data_(NULL),
  559. last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
  560. {
  561. }
  562. // Process the relocations to determine unreferenced sections for
  563. // garbage collection.
  564. void
  565. gc_process_relocs(Symbol_table* symtab,
  566. Layout* layout,
  567. Sized_relobj_file<size, big_endian>* object,
  568. unsigned int data_shndx,
  569. unsigned int sh_type,
  570. const unsigned char* prelocs,
  571. size_t reloc_count,
  572. Output_section* output_section,
  573. bool needs_special_offset_handling,
  574. size_t local_symbol_count,
  575. const unsigned char* plocal_symbols);
  576. // Scan the relocations to look for symbol adjustments.
  577. void
  578. scan_relocs(Symbol_table* symtab,
  579. Layout* layout,
  580. Sized_relobj_file<size, big_endian>* object,
  581. unsigned int data_shndx,
  582. unsigned int sh_type,
  583. const unsigned char* prelocs,
  584. size_t reloc_count,
  585. Output_section* output_section,
  586. bool needs_special_offset_handling,
  587. size_t local_symbol_count,
  588. const unsigned char* plocal_symbols);
  589. // Map input .toc section to output .got section.
  590. const char*
  591. do_output_section_name(const Relobj*, const char* name, size_t* plen) const
  592. {
  593. if (size == 64 && strcmp(name, ".toc") == 0)
  594. {
  595. *plen = 4;
  596. return ".got";
  597. }
  598. return NULL;
  599. }
  600. // Provide linker defined save/restore functions.
  601. void
  602. define_save_restore_funcs(Layout*, Symbol_table*);
  603. // No stubs unless a final link.
  604. bool
  605. do_may_relax() const
  606. { return !parameters->options().relocatable(); }
  607. bool
  608. do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
  609. void
  610. do_plt_fde_location(const Output_data*, unsigned char*,
  611. uint64_t*, off_t*) const;
  612. // Stash info about branches, for stub generation.
  613. void
  614. push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
  615. unsigned int data_shndx, Address r_offset,
  616. unsigned int r_type, unsigned int r_sym, Address addend)
  617. {
  618. Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
  619. this->branch_info_.push_back(info);
  620. if (r_type == elfcpp::R_POWERPC_REL14
  621. || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
  622. || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
  623. ppc_object->set_has_14bit_branch(data_shndx);
  624. }
  625. // Return whether the last branch is a plt call, and if so, mark the
  626. // branch as having an R_PPC64_TOCSAVE.
  627. bool
  628. mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
  629. unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
  630. {
  631. return (size == 64
  632. && !this->branch_info_.empty()
  633. && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
  634. r_offset, this, symtab));
  635. }
  636. // Say the given location, that of a nop in a function prologue with
  637. // an R_PPC64_TOCSAVE reloc, will be used to save r2.
  638. // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
  639. void
  640. add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
  641. unsigned int shndx, Address offset)
  642. {
  643. Symbol_location loc;
  644. loc.object = ppc_object;
  645. loc.shndx = shndx;
  646. loc.offset = offset;
  647. this->tocsave_loc_.insert(loc);
  648. }
  649. // Accessor
  650. const Tocsave_loc*
  651. tocsave_loc() const
  652. {
  653. return &this->tocsave_loc_;
  654. }
  655. void
  656. do_define_standard_symbols(Symbol_table*, Layout*);
  657. // Finalize the sections.
  658. void
  659. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  660. // Return the value to use for a dynamic which requires special
  661. // treatment.
  662. uint64_t
  663. do_dynsym_value(const Symbol*) const;
  664. // Return the PLT address to use for a local symbol.
  665. uint64_t
  666. do_plt_address_for_local(const Relobj*, unsigned int) const;
  667. // Return the PLT address to use for a global symbol.
  668. uint64_t
  669. do_plt_address_for_global(const Symbol*) const;
  670. // Return the offset to use for the GOT_INDX'th got entry which is
  671. // for a local tls symbol specified by OBJECT, SYMNDX.
  672. int64_t
  673. do_tls_offset_for_local(const Relobj* object,
  674. unsigned int symndx,
  675. Output_data_got_base* got,
  676. unsigned int got_indx,
  677. uint64_t addend) const;
  678. // Return the offset to use for the GOT_INDX'th got entry which is
  679. // for global tls symbol GSYM.
  680. int64_t
  681. do_tls_offset_for_global(Symbol* gsym,
  682. Output_data_got_base* got, unsigned int got_indx,
  683. uint64_t addend) const;
  684. void
  685. do_function_location(Symbol_location*) const;
  686. bool
  687. do_can_check_for_function_pointers() const
  688. { return true; }
  689. // Adjust -fsplit-stack code which calls non-split-stack code.
  690. void
  691. do_calls_non_split(Relobj* object, unsigned int shndx,
  692. section_offset_type fnoffset, section_size_type fnsize,
  693. const unsigned char* prelocs, size_t reloc_count,
  694. unsigned char* view, section_size_type view_size,
  695. std::string* from, std::string* to) const;
  696. // Relocate a section.
  697. void
  698. relocate_section(const Relocate_info<size, big_endian>*,
  699. unsigned int sh_type,
  700. const unsigned char* prelocs,
  701. size_t reloc_count,
  702. Output_section* output_section,
  703. bool needs_special_offset_handling,
  704. unsigned char* view,
  705. Address view_address,
  706. section_size_type view_size,
  707. const Reloc_symbol_changes*);
  708. // Scan the relocs during a relocatable link.
  709. void
  710. scan_relocatable_relocs(Symbol_table* symtab,
  711. Layout* layout,
  712. Sized_relobj_file<size, big_endian>* object,
  713. unsigned int data_shndx,
  714. unsigned int sh_type,
  715. const unsigned char* prelocs,
  716. size_t reloc_count,
  717. Output_section* output_section,
  718. bool needs_special_offset_handling,
  719. size_t local_symbol_count,
  720. const unsigned char* plocal_symbols,
  721. Relocatable_relocs*);
  722. // Scan the relocs for --emit-relocs.
  723. void
  724. emit_relocs_scan(Symbol_table* symtab,
  725. Layout* layout,
  726. Sized_relobj_file<size, big_endian>* object,
  727. unsigned int data_shndx,
  728. unsigned int sh_type,
  729. const unsigned char* prelocs,
  730. size_t reloc_count,
  731. Output_section* output_section,
  732. bool needs_special_offset_handling,
  733. size_t local_symbol_count,
  734. const unsigned char* plocal_syms,
  735. Relocatable_relocs* rr);
  736. // Emit relocations for a section.
  737. void
  738. relocate_relocs(const Relocate_info<size, big_endian>*,
  739. unsigned int sh_type,
  740. const unsigned char* prelocs,
  741. size_t reloc_count,
  742. Output_section* output_section,
  743. typename elfcpp::Elf_types<size>::Elf_Off
  744. offset_in_output_section,
  745. unsigned char*,
  746. Address view_address,
  747. section_size_type,
  748. unsigned char* reloc_view,
  749. section_size_type reloc_view_size);
  750. // Return whether SYM is defined by the ABI.
  751. bool
  752. do_is_defined_by_abi(const Symbol* sym) const
  753. {
  754. return strcmp(sym->name(), "__tls_get_addr") == 0;
  755. }
  756. // Return the size of the GOT section, for incremental linking
  757. section_size_type
  758. got_size() const
  759. {
  760. gold_assert(this->got_ != NULL);
  761. return this->got_->data_size() + (this->biggot_
  762. ? this->biggot_->data_size() : 0);
  763. }
  764. // Get the PLT section.
  765. const Output_data_plt_powerpc<size, big_endian>*
  766. plt_section() const
  767. {
  768. gold_assert(this->plt_ != NULL);
  769. return this->plt_;
  770. }
  771. // Get the IPLT section.
  772. const Output_data_plt_powerpc<size, big_endian>*
  773. iplt_section() const
  774. {
  775. gold_assert(this->iplt_ != NULL);
  776. return this->iplt_;
  777. }
  778. // Get the LPLT section.
  779. const Output_data_plt_powerpc<size, big_endian>*
  780. lplt_section() const
  781. {
  782. return this->lplt_;
  783. }
  784. // Return the plt offset and section for the given global sym.
  785. Address
  786. plt_off(const Symbol* gsym,
  787. const Output_data_plt_powerpc<size, big_endian>** sec) const
  788. {
  789. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  790. && gsym->can_use_relative_reloc(false))
  791. *sec = this->iplt_section();
  792. else if (!parameters->doing_static_link())
  793. *sec = this->plt_section();
  794. else
  795. *sec = this->lplt_section();
  796. return gsym->plt_offset();
  797. }
  798. // Return the plt offset and section for the given local sym.
  799. Address
  800. plt_off(const Sized_relobj_file<size, big_endian>* relobj,
  801. unsigned int local_sym_index,
  802. const Output_data_plt_powerpc<size, big_endian>** sec) const
  803. {
  804. const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
  805. if (lsym->is_ifunc_symbol())
  806. *sec = this->iplt_section();
  807. else
  808. *sec = this->lplt_section();
  809. return relobj->local_plt_offset(local_sym_index);
  810. }
  811. // Get the .glink section.
  812. const Output_data_glink<size, big_endian>*
  813. glink_section() const
  814. {
  815. gold_assert(this->glink_ != NULL);
  816. return this->glink_;
  817. }
  818. Output_data_glink<size, big_endian>*
  819. glink_section()
  820. {
  821. gold_assert(this->glink_ != NULL);
  822. return this->glink_;
  823. }
  824. bool has_glink() const
  825. { return this->glink_ != NULL; }
  826. // Get the GOT section.
  827. const Output_data_got_powerpc<size, big_endian>*
  828. got_section(Got_type got_type) const
  829. {
  830. gold_assert(this->got_ != NULL);
  831. if (size == 32 || (got_type & GOT_TYPE_SMALL))
  832. return this->got_;
  833. gold_assert(this->biggot_ != NULL);
  834. return this->biggot_;
  835. }
  836. // Get the GOT section, creating it if necessary.
  837. Output_data_got_powerpc<size, big_endian>*
  838. got_section(Symbol_table*, Layout*, Got_type);
  839. // The toc/got pointer reg will be set to this value.
  840. Address
  841. toc_pointer() const
  842. {
  843. return this->got_->address() + this->got_->g_o_t();
  844. }
  845. // Offset of base used to access the GOT/TOC relative to the GOT section.
  846. Address
  847. got_base_offset(Got_type got_type) const
  848. {
  849. if (size == 32 || (got_type & GOT_TYPE_SMALL))
  850. return this->got_->g_o_t();
  851. return this->toc_pointer() - this->biggot_->address();
  852. }
  853. Object*
  854. do_make_elf_object(const std::string&, Input_file*, off_t,
  855. const elfcpp::Ehdr<size, big_endian>&);
  856. // Return the number of entries in the GOT.
  857. unsigned int
  858. got_entry_count() const
  859. {
  860. if (this->got_ == NULL)
  861. return 0;
  862. return this->got_size() / (size / 8);
  863. }
  864. // Return the number of entries in the PLT.
  865. unsigned int
  866. plt_entry_count() const;
  867. // Return the offset of the first non-reserved PLT entry.
  868. unsigned int
  869. first_plt_entry_offset() const
  870. {
  871. if (size == 32)
  872. return 0;
  873. if (this->abiversion() >= 2)
  874. return 16;
  875. return 24;
  876. }
  877. // Return the size of each PLT entry.
  878. unsigned int
  879. plt_entry_size() const
  880. {
  881. if (size == 32)
  882. return 4;
  883. if (this->abiversion() >= 2)
  884. return 8;
  885. return 24;
  886. }
  887. Output_data_save_res<size, big_endian>*
  888. savres_section() const
  889. {
  890. return this->savres_section_;
  891. }
  892. // Add any special sections for this symbol to the gc work list.
  893. // For powerpc64, this adds the code section of a function
  894. // descriptor.
  895. void
  896. do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
  897. // Handle target specific gc actions when adding a gc reference from
  898. // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
  899. // and DST_OFF. For powerpc64, this adds a referenc to the code
  900. // section of a function descriptor.
  901. void
  902. do_gc_add_reference(Symbol_table* symtab,
  903. Relobj* src_obj,
  904. unsigned int src_shndx,
  905. Relobj* dst_obj,
  906. unsigned int dst_shndx,
  907. Address dst_off) const;
  908. typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
  909. const Stub_tables&
  910. stub_tables() const
  911. { return this->stub_tables_; }
  912. const Output_data_brlt_powerpc<size, big_endian>*
  913. brlt_section() const
  914. { return this->brlt_section_; }
  915. void
  916. add_branch_lookup_table(Address to)
  917. {
  918. unsigned int off = this->branch_lookup_table_.size() * (size / 8);
  919. this->branch_lookup_table_.insert(std::make_pair(to, off));
  920. }
  921. Address
  922. find_branch_lookup_table(Address to)
  923. {
  924. typename Branch_lookup_table::const_iterator p
  925. = this->branch_lookup_table_.find(to);
  926. return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
  927. }
  928. void
  929. write_branch_lookup_table(unsigned char *oview)
  930. {
  931. for (typename Branch_lookup_table::const_iterator p
  932. = this->branch_lookup_table_.begin();
  933. p != this->branch_lookup_table_.end();
  934. ++p)
  935. {
  936. elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
  937. }
  938. }
  939. // Wrapper used after relax to define a local symbol in output data,
  940. // from the end if value < 0.
  941. void
  942. define_local(Symbol_table* symtab, const char* name,
  943. Output_data* od, Address value, unsigned int symsize)
  944. {
  945. Symbol* sym
  946. = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
  947. od, value, symsize, elfcpp::STT_NOTYPE,
  948. elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
  949. static_cast<Signed_address>(value) < 0,
  950. false);
  951. // We are creating this symbol late, so need to fix up things
  952. // done early in Layout::finalize.
  953. sym->set_dynsym_index(-1U);
  954. }
  955. void
  956. set_power10_relocs()
  957. {
  958. this->power10_relocs_ = true;
  959. }
  960. bool
  961. power10_stubs() const
  962. {
  963. return (this->power10_relocs_
  964. && (parameters->options().power10_stubs_enum()
  965. != General_options::POWER10_STUBS_NO));
  966. }
  967. bool
  968. power10_stubs_auto() const
  969. {
  970. return (parameters->options().power10_stubs_enum()
  971. == General_options::POWER10_STUBS_AUTO);
  972. }
  973. bool
  974. plt_thread_safe() const
  975. { return this->plt_thread_safe_; }
  976. bool
  977. plt_localentry0() const
  978. { return this->plt_localentry0_; }
  979. bool
  980. has_localentry0() const
  981. { return this->has_localentry0_; }
  982. void
  983. set_has_localentry0()
  984. {
  985. this->has_localentry0_ = true;
  986. }
  987. bool
  988. is_elfv2_localentry0(const Symbol* gsym) const
  989. {
  990. return (size == 64
  991. && this->abiversion() >= 2
  992. && this->plt_localentry0()
  993. && gsym->type() == elfcpp::STT_FUNC
  994. && gsym->is_defined()
  995. && gsym->nonvis() >> 3 == 0
  996. && !gsym->non_zero_localentry());
  997. }
  998. bool
  999. is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
  1000. unsigned int r_sym) const
  1001. {
  1002. const Powerpc_relobj<size, big_endian>* ppc_object
  1003. = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
  1004. if (size == 64
  1005. && this->abiversion() >= 2
  1006. && this->plt_localentry0()
  1007. && ppc_object->st_other(r_sym) >> 5 == 0)
  1008. {
  1009. const Symbol_value<size>* psymval = object->local_symbol(r_sym);
  1010. bool is_ordinary;
  1011. if (!psymval->is_ifunc_symbol()
  1012. && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
  1013. && is_ordinary)
  1014. return true;
  1015. }
  1016. return false;
  1017. }
  1018. bool
  1019. tprel_opt() const
  1020. { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
  1021. void
  1022. set_no_tprel_opt()
  1023. { this->no_tprel_opt_ = true; }
  1024. // Remember any symbols seen with non-zero localentry, even those
  1025. // not providing a definition
  1026. bool
  1027. resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
  1028. const char*)
  1029. {
  1030. if (size == 64)
  1031. {
  1032. unsigned char st_other = sym.get_st_other();
  1033. if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
  1034. to->set_non_zero_localentry();
  1035. }
  1036. // We haven't resolved anything, continue normal processing.
  1037. return false;
  1038. }
  1039. int
  1040. abiversion() const
  1041. { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
  1042. void
  1043. set_abiversion(int ver)
  1044. {
  1045. elfcpp::Elf_Word flags = this->processor_specific_flags();
  1046. flags &= ~elfcpp::EF_PPC64_ABI;
  1047. flags |= ver & elfcpp::EF_PPC64_ABI;
  1048. this->set_processor_specific_flags(flags);
  1049. }
  1050. Symbol*
  1051. tls_get_addr_opt() const
  1052. { return this->tls_get_addr_opt_; }
  1053. Symbol*
  1054. tls_get_addr() const
  1055. { return this->tls_get_addr_; }
  1056. // If optimizing __tls_get_addr calls, whether this is the
  1057. // "__tls_get_addr" symbol.
  1058. bool
  1059. is_tls_get_addr_opt(const Symbol* gsym) const
  1060. {
  1061. return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
  1062. || gsym == this->tls_get_addr_opt_);
  1063. }
  1064. bool
  1065. replace_tls_get_addr(const Symbol* gsym) const
  1066. { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
  1067. void
  1068. set_has_tls_get_addr_opt()
  1069. { this->has_tls_get_addr_opt_ = true; }
  1070. // Offset to toc save stack slot
  1071. int
  1072. stk_toc() const
  1073. { return this->abiversion() < 2 ? 40 : 24; }
  1074. // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
  1075. // so use the CR save slot. Used only by __tls_get_addr call stub,
  1076. // relying on __tls_get_addr not saving CR itself.
  1077. int
  1078. stk_linker() const
  1079. { return this->abiversion() < 2 ? 32 : 8; }
  1080. // Merge object attributes from input object with those in the output.
  1081. void
  1082. merge_object_attributes(const Object*, const Attributes_section_data*);
  1083. bool
  1084. symval_for_branch(const Symbol_table* symtab,
  1085. const Sized_symbol<size>* gsym,
  1086. Powerpc_relobj<size, big_endian>* object,
  1087. Address *value, unsigned int *dest_shndx);
  1088. private:
  1089. class Track_tls
  1090. {
  1091. public:
  1092. enum Tls_get_addr
  1093. {
  1094. NOT_EXPECTED = 0,
  1095. EXPECTED = 1,
  1096. SKIP = 2,
  1097. NORMAL = 3
  1098. };
  1099. Track_tls()
  1100. : tls_get_addr_state_(NOT_EXPECTED),
  1101. relinfo_(NULL), relnum_(0), r_offset_(0)
  1102. { }
  1103. ~Track_tls()
  1104. {
  1105. if (this->tls_get_addr_state_ != NOT_EXPECTED)
  1106. this->missing();
  1107. }
  1108. void
  1109. missing(void)
  1110. {
  1111. if (this->relinfo_ != NULL)
  1112. gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
  1113. _("missing expected __tls_get_addr call"));
  1114. }
  1115. void
  1116. expect_tls_get_addr_call(
  1117. const Relocate_info<size, big_endian>* relinfo,
  1118. size_t relnum,
  1119. Address r_offset)
  1120. {
  1121. this->tls_get_addr_state_ = EXPECTED;
  1122. this->relinfo_ = relinfo;
  1123. this->relnum_ = relnum;
  1124. this->r_offset_ = r_offset;
  1125. }
  1126. void
  1127. expect_tls_get_addr_call()
  1128. { this->tls_get_addr_state_ = EXPECTED; }
  1129. void
  1130. skip_next_tls_get_addr_call()
  1131. {this->tls_get_addr_state_ = SKIP; }
  1132. Tls_get_addr
  1133. maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
  1134. unsigned int r_type, const Symbol* gsym)
  1135. {
  1136. bool is_tls_call
  1137. = ((r_type == elfcpp::R_POWERPC_REL24
  1138. || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
  1139. || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
  1140. || r_type == elfcpp::R_PPC_PLTREL24
  1141. || is_plt16_reloc<size>(r_type)
  1142. || r_type == elfcpp::R_PPC64_PLT_PCREL34
  1143. || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
  1144. || r_type == elfcpp::R_POWERPC_PLTSEQ
  1145. || r_type == elfcpp::R_POWERPC_PLTCALL
  1146. || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
  1147. || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
  1148. && gsym != NULL
  1149. && (gsym == target->tls_get_addr()
  1150. || gsym == target->tls_get_addr_opt()));
  1151. Tls_get_addr last_tls = this->tls_get_addr_state_;
  1152. this->tls_get_addr_state_ = NOT_EXPECTED;
  1153. if (is_tls_call && last_tls != EXPECTED)
  1154. return last_tls;
  1155. else if (!is_tls_call && last_tls != NOT_EXPECTED)
  1156. {
  1157. this->missing();
  1158. return EXPECTED;
  1159. }
  1160. return NORMAL;
  1161. }
  1162. private:
  1163. // What we're up to regarding calls to __tls_get_addr.
  1164. // On powerpc, the branch and link insn making a call to
  1165. // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
  1166. // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
  1167. // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
  1168. // The marker relocation always comes first, and has the same
  1169. // symbol as the reloc on the insn setting up the __tls_get_addr
  1170. // argument. This ties the arg setup insn with the call insn,
  1171. // allowing ld to safely optimize away the call. We check that
  1172. // every call to __tls_get_addr has a marker relocation, and that
  1173. // every marker relocation is on a call to __tls_get_addr.
  1174. Tls_get_addr tls_get_addr_state_;
  1175. // Info about the last reloc for error message.
  1176. const Relocate_info<size, big_endian>* relinfo_;
  1177. size_t relnum_;
  1178. Address r_offset_;
  1179. };
  1180. // The class which scans relocations.
  1181. class Scan : protected Track_tls
  1182. {
  1183. public:
  1184. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1185. Scan()
  1186. : Track_tls(), issued_non_pic_error_(false)
  1187. { }
  1188. static inline int
  1189. get_reference_flags(unsigned int r_type, const Target_powerpc* target);
  1190. inline void
  1191. local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
  1192. Sized_relobj_file<size, big_endian>* object,
  1193. unsigned int data_shndx,
  1194. Output_section* output_section,
  1195. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  1196. const elfcpp::Sym<size, big_endian>& lsym,
  1197. bool is_discarded);
  1198. inline void
  1199. global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
  1200. Sized_relobj_file<size, big_endian>* object,
  1201. unsigned int data_shndx,
  1202. Output_section* output_section,
  1203. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  1204. Symbol* gsym);
  1205. inline bool
  1206. local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
  1207. Target_powerpc* ,
  1208. Sized_relobj_file<size, big_endian>* relobj,
  1209. unsigned int ,
  1210. Output_section* ,
  1211. const elfcpp::Rela<size, big_endian>& ,
  1212. unsigned int r_type,
  1213. const elfcpp::Sym<size, big_endian>&)
  1214. {
  1215. // PowerPC64 .opd is not folded, so any identical function text
  1216. // may be folded and we'll still keep function addresses distinct.
  1217. // That means no reloc is of concern here.
  1218. if (size == 64)
  1219. {
  1220. Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  1221. <Powerpc_relobj<size, big_endian>*>(relobj);
  1222. if (ppcobj->abiversion() == 1)
  1223. return false;
  1224. }
  1225. // For 32-bit and ELFv2, conservatively assume anything but calls to
  1226. // function code might be taking the address of the function.
  1227. return !is_branch_reloc<size>(r_type);
  1228. }
  1229. inline bool
  1230. global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
  1231. Target_powerpc* ,
  1232. Sized_relobj_file<size, big_endian>* relobj,
  1233. unsigned int ,
  1234. Output_section* ,
  1235. const elfcpp::Rela<size, big_endian>& ,
  1236. unsigned int r_type,
  1237. Symbol*)
  1238. {
  1239. // As above.
  1240. if (size == 64)
  1241. {
  1242. Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  1243. <Powerpc_relobj<size, big_endian>*>(relobj);
  1244. if (ppcobj->abiversion() == 1)
  1245. return false;
  1246. }
  1247. return !is_branch_reloc<size>(r_type);
  1248. }
  1249. static bool
  1250. reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
  1251. Sized_relobj_file<size, big_endian>* object,
  1252. unsigned int r_type, bool report_err);
  1253. private:
  1254. static void
  1255. unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
  1256. unsigned int r_type);
  1257. static void
  1258. unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
  1259. unsigned int r_type, Symbol*);
  1260. static void
  1261. generate_tls_call(Symbol_table* symtab, Layout* layout,
  1262. Target_powerpc* target);
  1263. void
  1264. check_non_pic(Relobj*, unsigned int r_type);
  1265. // Whether we have issued an error about a non-PIC compilation.
  1266. bool issued_non_pic_error_;
  1267. };
  1268. // The class which implements relocation.
  1269. class Relocate : protected Track_tls
  1270. {
  1271. public:
  1272. // Use 'at' branch hints when true, 'y' when false.
  1273. // FIXME maybe: set this with an option.
  1274. static const bool is_isa_v2 = true;
  1275. Relocate()
  1276. : Track_tls()
  1277. { }
  1278. // Do a relocation. Return false if the caller should not issue
  1279. // any warnings about this relocation.
  1280. inline bool
  1281. relocate(const Relocate_info<size, big_endian>*, unsigned int,
  1282. Target_powerpc*, Output_section*, size_t, const unsigned char*,
  1283. const Sized_symbol<size>*, const Symbol_value<size>*,
  1284. unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
  1285. section_size_type);
  1286. };
  1287. class Relocate_comdat_behavior
  1288. {
  1289. public:
  1290. // Decide what the linker should do for relocations that refer to
  1291. // discarded comdat sections.
  1292. inline Comdat_behavior
  1293. get(const char* name)
  1294. {
  1295. gold::Default_comdat_behavior default_behavior;
  1296. Comdat_behavior ret = default_behavior.get(name);
  1297. if (ret == CB_ERROR)
  1298. {
  1299. if (size == 32
  1300. && (strcmp(name, ".fixup") == 0
  1301. || strcmp(name, ".got2") == 0))
  1302. ret = CB_IGNORE;
  1303. if (size == 64
  1304. && (strcmp(name, ".opd") == 0
  1305. || strcmp(name, ".toc") == 0
  1306. || strcmp(name, ".toc1") == 0))
  1307. ret = CB_IGNORE;
  1308. }
  1309. return ret;
  1310. }
  1311. };
  1312. // Optimize the TLS relocation type based on what we know about the
  1313. // symbol. IS_FINAL is true if the final address of this symbol is
  1314. // known at link time.
  1315. tls::Tls_optimization
  1316. optimize_tls_gd(bool is_final)
  1317. {
  1318. // If we are generating a shared library, then we can't do anything
  1319. // in the linker.
  1320. if (parameters->options().shared()
  1321. || !parameters->options().tls_optimize())
  1322. return tls::TLSOPT_NONE;
  1323. if (!is_final)
  1324. return tls::TLSOPT_TO_IE;
  1325. return tls::TLSOPT_TO_LE;
  1326. }
  1327. tls::Tls_optimization
  1328. optimize_tls_ld()
  1329. {
  1330. if (parameters->options().shared()
  1331. || !parameters->options().tls_optimize())
  1332. return tls::TLSOPT_NONE;
  1333. return tls::TLSOPT_TO_LE;
  1334. }
  1335. tls::Tls_optimization
  1336. optimize_tls_ie(bool is_final)
  1337. {
  1338. if (!is_final
  1339. || parameters->options().shared()
  1340. || !parameters->options().tls_optimize())
  1341. return tls::TLSOPT_NONE;
  1342. return tls::TLSOPT_TO_LE;
  1343. }
  1344. // Create glink.
  1345. void
  1346. make_glink_section(Layout*);
  1347. // Create the PLT section.
  1348. void
  1349. make_plt_section(Symbol_table*, Layout*);
  1350. void
  1351. make_iplt_section(Symbol_table*, Layout*);
  1352. void
  1353. make_lplt_section(Symbol_table*, Layout*);
  1354. void
  1355. make_brlt_section(Layout*);
  1356. // Create a PLT entry for a global symbol.
  1357. void
  1358. make_plt_entry(Symbol_table*, Layout*, Symbol*);
  1359. // Create a PLT entry for a local IFUNC symbol.
  1360. void
  1361. make_local_ifunc_plt_entry(Symbol_table*, Layout*,
  1362. Sized_relobj_file<size, big_endian>*,
  1363. unsigned int);
  1364. // Create a PLT entry for a local non-IFUNC symbol.
  1365. void
  1366. make_local_plt_entry(Symbol_table*, Layout*,
  1367. Sized_relobj_file<size, big_endian>*,
  1368. unsigned int);
  1369. void
  1370. make_local_plt_entry(Symbol_table*, Layout*, Symbol*);
  1371. // Create a GOT entry for local dynamic __tls_get_addr.
  1372. unsigned int
  1373. tlsld_got_offset(Symbol_table* symtab, Layout* layout,
  1374. Sized_relobj_file<size, big_endian>* object);
  1375. unsigned int
  1376. tlsld_got_offset() const
  1377. {
  1378. return this->tlsld_got_offset_;
  1379. }
  1380. // Get the dynamic reloc section, creating it if necessary.
  1381. Reloc_section*
  1382. rela_dyn_section(Layout*);
  1383. // Similarly, but for ifunc symbols get the one for ifunc.
  1384. Reloc_section*
  1385. rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
  1386. // Copy a relocation against a global symbol.
  1387. void
  1388. copy_reloc(Symbol_table* symtab, Layout* layout,
  1389. Sized_relobj_file<size, big_endian>* object,
  1390. unsigned int shndx, Output_section* output_section,
  1391. Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
  1392. {
  1393. unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
  1394. this->copy_relocs_.copy_reloc(symtab, layout,
  1395. symtab->get_sized_symbol<size>(sym),
  1396. object, shndx, output_section,
  1397. r_type, reloc.get_r_offset(),
  1398. reloc.get_r_addend(),
  1399. this->rela_dyn_section(layout));
  1400. }
  1401. // Look over all the input sections, deciding where to place stubs.
  1402. void
  1403. group_sections(Layout*, const Task*, bool);
  1404. // Sort output sections by address.
  1405. struct Sort_sections
  1406. {
  1407. bool
  1408. operator()(const Output_section* sec1, const Output_section* sec2)
  1409. { return sec1->address() < sec2->address(); }
  1410. };
  1411. class Branch_info
  1412. {
  1413. public:
  1414. Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
  1415. unsigned int data_shndx,
  1416. Address r_offset,
  1417. unsigned int r_type,
  1418. unsigned int r_sym,
  1419. Address addend)
  1420. : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
  1421. r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
  1422. { }
  1423. ~Branch_info()
  1424. { }
  1425. // Return whether this branch is going via a plt call stub, and if
  1426. // so, mark it as having an R_PPC64_TOCSAVE.
  1427. bool
  1428. mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
  1429. unsigned int shndx, Address offset,
  1430. Target_powerpc* target, Symbol_table* symtab);
  1431. // If this branch needs a plt call stub, or a long branch stub, make one.
  1432. bool
  1433. make_stub(Stub_table<size, big_endian>*,
  1434. Stub_table<size, big_endian>*,
  1435. Symbol_table*) const;
  1436. private:
  1437. // The branch location..
  1438. Powerpc_relobj<size, big_endian>* object_;
  1439. unsigned int shndx_;
  1440. Address offset_;
  1441. // ..and the branch type and destination.
  1442. unsigned int r_type_ : 31;
  1443. unsigned int tocsave_ : 1;
  1444. unsigned int r_sym_;
  1445. Address addend_;
  1446. };
  1447. // Information about this specific target which we pass to the
  1448. // general Target structure.
  1449. static Target::Target_info powerpc_info;
  1450. // The small GOT section used by ppc32, and by ppc64 for entries that
  1451. // must be addresseed +/-32k from the got pointer.
  1452. Output_data_got_powerpc<size, big_endian>* got_;
  1453. // Another GOT section used for entries that can be addressed +/- 2G
  1454. // from the got pointer.
  1455. Output_data_got_powerpc<size, big_endian>* biggot_;
  1456. // The PLT section. This is a container for a table of addresses,
  1457. // and their relocations. Each address in the PLT has a dynamic
  1458. // relocation (R_*_JMP_SLOT) and each address will have a
  1459. // corresponding entry in .glink for lazy resolution of the PLT.
  1460. // ppc32 initialises the PLT to point at the .glink entry, while
  1461. // ppc64 leaves this to ld.so. To make a call via the PLT, the
  1462. // linker adds a stub that loads the PLT entry into ctr then
  1463. // branches to ctr. There may be more than one stub for each PLT
  1464. // entry. DT_JMPREL points at the first PLT dynamic relocation and
  1465. // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
  1466. Output_data_plt_powerpc<size, big_endian>* plt_;
  1467. // The IPLT section. Like plt_, this is a container for a table of
  1468. // addresses and their relocations, specifically for STT_GNU_IFUNC
  1469. // functions that resolve locally (STT_GNU_IFUNC functions that
  1470. // don't resolve locally go in PLT). Unlike plt_, these have no
  1471. // entry in .glink for lazy resolution, and the relocation section
  1472. // does not have a 1-1 correspondence with IPLT addresses. In fact,
  1473. // the relocation section may contain relocations against
  1474. // STT_GNU_IFUNC symbols at locations outside of IPLT. The
  1475. // relocation section will appear at the end of other dynamic
  1476. // relocations, so that ld.so applies these relocations after other
  1477. // dynamic relocations. In a static executable, the relocation
  1478. // section is emitted and marked with __rela_iplt_start and
  1479. // __rela_iplt_end symbols.
  1480. Output_data_plt_powerpc<size, big_endian>* iplt_;
  1481. // A PLT style section for local, non-ifunc symbols
  1482. Output_data_plt_powerpc<size, big_endian>* lplt_;
  1483. // Section holding long branch destinations.
  1484. Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
  1485. // The .glink section.
  1486. Output_data_glink<size, big_endian>* glink_;
  1487. // The dynamic reloc section.
  1488. Reloc_section* rela_dyn_;
  1489. // Relocs saved to avoid a COPY reloc.
  1490. Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
  1491. // Offset of the GOT entry for local dynamic __tls_get_addr calls.
  1492. unsigned int tlsld_got_offset_;
  1493. Stub_tables stub_tables_;
  1494. typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
  1495. Branch_lookup_table branch_lookup_table_;
  1496. typedef std::vector<Branch_info> Branches;
  1497. Branches branch_info_;
  1498. Tocsave_loc tocsave_loc_;
  1499. bool power10_relocs_;
  1500. bool plt_thread_safe_;
  1501. bool plt_localentry0_;
  1502. bool plt_localentry0_init_;
  1503. bool has_localentry0_;
  1504. bool has_tls_get_addr_opt_;
  1505. bool no_tprel_opt_;
  1506. bool relax_failed_;
  1507. int relax_fail_count_;
  1508. int32_t stub_group_size_;
  1509. Output_data_save_res<size, big_endian> *savres_section_;
  1510. // The "__tls_get_addr" symbol, if present
  1511. Symbol* tls_get_addr_;
  1512. // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
  1513. Symbol* tls_get_addr_opt_;
  1514. // Attributes in output.
  1515. Attributes_section_data* attributes_section_data_;
  1516. // Last input file to change various attribute tags
  1517. const char* last_fp_;
  1518. const char* last_ld_;
  1519. const char* last_vec_;
  1520. const char* last_struct_;
  1521. };
  1522. template<>
  1523. Target::Target_info Target_powerpc<32, true>::powerpc_info =
  1524. {
  1525. 32, // size
  1526. true, // is_big_endian
  1527. elfcpp::EM_PPC, // machine_code
  1528. false, // has_make_symbol
  1529. false, // has_resolve
  1530. false, // has_code_fill
  1531. true, // is_default_stack_executable
  1532. false, // can_icf_inline_merge_sections
  1533. '\0', // wrap_char
  1534. "/usr/lib/ld.so.1", // dynamic_linker
  1535. 0x10000000, // default_text_segment_address
  1536. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  1537. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  1538. false, // isolate_execinstr
  1539. 0, // rosegment_gap
  1540. elfcpp::SHN_UNDEF, // small_common_shndx
  1541. elfcpp::SHN_UNDEF, // large_common_shndx
  1542. 0, // small_common_section_flags
  1543. 0, // large_common_section_flags
  1544. NULL, // attributes_section
  1545. NULL, // attributes_vendor
  1546. "_start", // entry_symbol_name
  1547. 32, // hash_entry_size
  1548. elfcpp::SHT_PROGBITS, // unwind_section_type
  1549. };
  1550. template<>
  1551. Target::Target_info Target_powerpc<32, false>::powerpc_info =
  1552. {
  1553. 32, // size
  1554. false, // is_big_endian
  1555. elfcpp::EM_PPC, // machine_code
  1556. false, // has_make_symbol
  1557. false, // has_resolve
  1558. false, // has_code_fill
  1559. true, // is_default_stack_executable
  1560. false, // can_icf_inline_merge_sections
  1561. '\0', // wrap_char
  1562. "/usr/lib/ld.so.1", // dynamic_linker
  1563. 0x10000000, // default_text_segment_address
  1564. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  1565. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  1566. false, // isolate_execinstr
  1567. 0, // rosegment_gap
  1568. elfcpp::SHN_UNDEF, // small_common_shndx
  1569. elfcpp::SHN_UNDEF, // large_common_shndx
  1570. 0, // small_common_section_flags
  1571. 0, // large_common_section_flags
  1572. NULL, // attributes_section
  1573. NULL, // attributes_vendor
  1574. "_start", // entry_symbol_name
  1575. 32, // hash_entry_size
  1576. elfcpp::SHT_PROGBITS, // unwind_section_type
  1577. };
  1578. template<>
  1579. Target::Target_info Target_powerpc<64, true>::powerpc_info =
  1580. {
  1581. 64, // size
  1582. true, // is_big_endian
  1583. elfcpp::EM_PPC64, // machine_code
  1584. false, // has_make_symbol
  1585. true, // has_resolve
  1586. false, // has_code_fill
  1587. false, // is_default_stack_executable
  1588. false, // can_icf_inline_merge_sections
  1589. '\0', // wrap_char
  1590. "/usr/lib/ld.so.1", // dynamic_linker
  1591. 0x10000000, // default_text_segment_address
  1592. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  1593. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  1594. false, // isolate_execinstr
  1595. 0, // rosegment_gap
  1596. elfcpp::SHN_UNDEF, // small_common_shndx
  1597. elfcpp::SHN_UNDEF, // large_common_shndx
  1598. 0, // small_common_section_flags
  1599. 0, // large_common_section_flags
  1600. NULL, // attributes_section
  1601. NULL, // attributes_vendor
  1602. "_start", // entry_symbol_name
  1603. 32, // hash_entry_size
  1604. elfcpp::SHT_PROGBITS, // unwind_section_type
  1605. };
  1606. template<>
  1607. Target::Target_info Target_powerpc<64, false>::powerpc_info =
  1608. {
  1609. 64, // size
  1610. false, // is_big_endian
  1611. elfcpp::EM_PPC64, // machine_code
  1612. false, // has_make_symbol
  1613. true, // has_resolve
  1614. false, // has_code_fill
  1615. false, // is_default_stack_executable
  1616. false, // can_icf_inline_merge_sections
  1617. '\0', // wrap_char
  1618. "/usr/lib/ld.so.1", // dynamic_linker
  1619. 0x10000000, // default_text_segment_address
  1620. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  1621. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  1622. false, // isolate_execinstr
  1623. 0, // rosegment_gap
  1624. elfcpp::SHN_UNDEF, // small_common_shndx
  1625. elfcpp::SHN_UNDEF, // large_common_shndx
  1626. 0, // small_common_section_flags
  1627. 0, // large_common_section_flags
  1628. NULL, // attributes_section
  1629. NULL, // attributes_vendor
  1630. "_start", // entry_symbol_name
  1631. 32, // hash_entry_size
  1632. elfcpp::SHT_PROGBITS, // unwind_section_type
  1633. };
  1634. template<int size>
  1635. inline bool
  1636. is_branch_reloc(unsigned int r_type)
  1637. {
  1638. return (r_type == elfcpp::R_POWERPC_REL24
  1639. || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
  1640. || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
  1641. || r_type == elfcpp::R_PPC_PLTREL24
  1642. || r_type == elfcpp::R_PPC_LOCAL24PC
  1643. || r_type == elfcpp::R_POWERPC_REL14
  1644. || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
  1645. || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
  1646. || r_type == elfcpp::R_POWERPC_ADDR24
  1647. || r_type == elfcpp::R_POWERPC_ADDR14
  1648. || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
  1649. || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
  1650. }
  1651. // Reloc resolves to plt entry.
  1652. template<int size>
  1653. inline bool
  1654. is_plt16_reloc(unsigned int r_type)
  1655. {
  1656. return (r_type == elfcpp::R_POWERPC_PLT16_LO
  1657. || r_type == elfcpp::R_POWERPC_PLT16_HI
  1658. || r_type == elfcpp::R_POWERPC_PLT16_HA
  1659. || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
  1660. }
  1661. // GOT_TYPE_STANDARD or GOT_TYPE_SMALL (ie. not TLS) GOT relocs
  1662. inline bool
  1663. is_got_reloc(unsigned int r_type)
  1664. {
  1665. return (r_type == elfcpp::R_POWERPC_GOT16
  1666. || r_type == elfcpp::R_POWERPC_GOT16_LO
  1667. || r_type == elfcpp::R_POWERPC_GOT16_HI
  1668. || r_type == elfcpp::R_POWERPC_GOT16_HA
  1669. || r_type == elfcpp::R_PPC64_GOT16_DS
  1670. || r_type == elfcpp::R_PPC64_GOT16_LO_DS
  1671. || r_type == elfcpp::R_PPC64_GOT_PCREL34);
  1672. }
  1673. // If INSN is an opcode that may be used with an @tls operand, return
  1674. // the transformed insn for TLS optimisation, otherwise return 0. If
  1675. // REG is non-zero only match an insn with RB or RA equal to REG.
  1676. uint32_t
  1677. at_tls_transform(uint32_t insn, unsigned int reg)
  1678. {
  1679. if ((insn & (0x3f << 26)) != 31 << 26)
  1680. return 0;
  1681. unsigned int rtra;
  1682. if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
  1683. rtra = insn & ((1 << 26) - (1 << 16));
  1684. else if (((insn >> 16) & 0x1f) == reg)
  1685. rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
  1686. else
  1687. return 0;
  1688. if ((insn & (0x3ff << 1)) == 266 << 1)
  1689. // add -> addi
  1690. insn = 14 << 26;
  1691. else if ((insn & (0x1f << 1)) == 23 << 1
  1692. && ((insn & (0x1f << 6)) < 14 << 6
  1693. || ((insn & (0x1f << 6)) >= 16 << 6
  1694. && (insn & (0x1f << 6)) < 24 << 6)))
  1695. // load and store indexed -> dform
  1696. insn = (32 | ((insn >> 6) & 0x1f)) << 26;
  1697. else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
  1698. // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
  1699. insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
  1700. else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
  1701. // lwax -> lwa
  1702. insn = (58 << 26) | 2;
  1703. else
  1704. return 0;
  1705. insn |= rtra;
  1706. return insn;
  1707. }
  1708. template<int size, bool big_endian>
  1709. class Powerpc_relocate_functions
  1710. {
  1711. public:
  1712. enum Overflow_check
  1713. {
  1714. CHECK_NONE,
  1715. CHECK_SIGNED,
  1716. CHECK_UNSIGNED,
  1717. CHECK_BITFIELD,
  1718. CHECK_LOW_INSN,
  1719. CHECK_HIGH_INSN
  1720. };
  1721. enum Status
  1722. {
  1723. STATUS_OK,
  1724. STATUS_OVERFLOW
  1725. };
  1726. private:
  1727. typedef Powerpc_relocate_functions<size, big_endian> This;
  1728. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1729. typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
  1730. template<int valsize>
  1731. static inline bool
  1732. has_overflow_signed(Address value)
  1733. {
  1734. // limit = 1 << (valsize - 1) without shift count exceeding size of type
  1735. Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
  1736. limit <<= ((valsize - 1) >> 1);
  1737. limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
  1738. return value + limit > (limit << 1) - 1;
  1739. }
  1740. template<int valsize>
  1741. static inline bool
  1742. has_overflow_unsigned(Address value)
  1743. {
  1744. Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
  1745. limit <<= ((valsize - 1) >> 1);
  1746. limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
  1747. return value > (limit << 1) - 1;
  1748. }
  1749. template<int valsize>
  1750. static inline bool
  1751. has_overflow_bitfield(Address value)
  1752. {
  1753. return (has_overflow_unsigned<valsize>(value)
  1754. && has_overflow_signed<valsize>(value));
  1755. }
  1756. template<int valsize>
  1757. static inline Status
  1758. overflowed(Address value, Overflow_check overflow)
  1759. {
  1760. if (overflow == CHECK_SIGNED)
  1761. {
  1762. if (has_overflow_signed<valsize>(value))
  1763. return STATUS_OVERFLOW;
  1764. }
  1765. else if (overflow == CHECK_UNSIGNED)
  1766. {
  1767. if (has_overflow_unsigned<valsize>(value))
  1768. return STATUS_OVERFLOW;
  1769. }
  1770. else if (overflow == CHECK_BITFIELD)
  1771. {
  1772. if (has_overflow_bitfield<valsize>(value))
  1773. return STATUS_OVERFLOW;
  1774. }
  1775. return STATUS_OK;
  1776. }
  1777. // Do a simple RELA relocation
  1778. template<int fieldsize, int valsize>
  1779. static inline Status
  1780. rela(unsigned char* view, Address value, Overflow_check overflow)
  1781. {
  1782. typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
  1783. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1784. elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
  1785. return overflowed<valsize>(value, overflow);
  1786. }
  1787. template<int fieldsize, int valsize>
  1788. static inline Status
  1789. rela(unsigned char* view,
  1790. unsigned int right_shift,
  1791. typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
  1792. Address value,
  1793. Overflow_check overflow)
  1794. {
  1795. typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
  1796. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1797. Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
  1798. if (overflow == CHECK_SIGNED)
  1799. value = static_cast<SignedAddress>(value) >> right_shift;
  1800. else
  1801. value = value >> right_shift;
  1802. Valtype reloc = value;
  1803. val &= ~dst_mask;
  1804. reloc &= dst_mask;
  1805. elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
  1806. return overflowed<valsize>(value, overflow);
  1807. }
  1808. // Do a simple RELA relocation, unaligned.
  1809. template<int fieldsize, int valsize>
  1810. static inline Status
  1811. rela_ua(unsigned char* view, Address value, Overflow_check overflow)
  1812. {
  1813. elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
  1814. return overflowed<valsize>(value, overflow);
  1815. }
  1816. template<int fieldsize, int valsize>
  1817. static inline Status
  1818. rela_ua(unsigned char* view,
  1819. unsigned int right_shift,
  1820. typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
  1821. Address value,
  1822. Overflow_check overflow)
  1823. {
  1824. typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
  1825. Valtype;
  1826. Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
  1827. if (overflow == CHECK_SIGNED)
  1828. value = static_cast<SignedAddress>(value) >> right_shift;
  1829. else
  1830. value = value >> right_shift;
  1831. Valtype reloc = value;
  1832. val &= ~dst_mask;
  1833. reloc &= dst_mask;
  1834. elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
  1835. return overflowed<valsize>(value, overflow);
  1836. }
  1837. public:
  1838. // R_PPC64_ADDR64: (Symbol + Addend)
  1839. static inline void
  1840. addr64(unsigned char* view, Address value)
  1841. { This::template rela<64,64>(view, value, CHECK_NONE); }
  1842. // R_PPC64_UADDR64: (Symbol + Addend) unaligned
  1843. static inline void
  1844. addr64_u(unsigned char* view, Address value)
  1845. { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
  1846. // R_POWERPC_ADDR32: (Symbol + Addend)
  1847. static inline Status
  1848. addr32(unsigned char* view, Address value, Overflow_check overflow)
  1849. { return This::template rela<32,32>(view, value, overflow); }
  1850. // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
  1851. static inline Status
  1852. addr32_u(unsigned char* view, Address value, Overflow_check overflow)
  1853. { return This::template rela_ua<32,32>(view, value, overflow); }
  1854. // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
  1855. static inline Status
  1856. addr24(unsigned char* view, Address value, Overflow_check overflow)
  1857. {
  1858. Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
  1859. value, overflow);
  1860. if (overflow != CHECK_NONE && (value & 3) != 0)
  1861. stat = STATUS_OVERFLOW;
  1862. return stat;
  1863. }
  1864. // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
  1865. static inline Status
  1866. addr16(unsigned char* view, Address value, Overflow_check overflow)
  1867. { return This::template rela<16,16>(view, value, overflow); }
  1868. // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
  1869. static inline Status
  1870. addr16_u(unsigned char* view, Address value, Overflow_check overflow)
  1871. { return This::template rela_ua<16,16>(view, value, overflow); }
  1872. // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
  1873. static inline Status
  1874. addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
  1875. {
  1876. Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
  1877. if ((value & 3) != 0)
  1878. stat = STATUS_OVERFLOW;
  1879. return stat;
  1880. }
  1881. // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
  1882. static inline Status
  1883. addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
  1884. {
  1885. Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
  1886. if ((value & 15) != 0)
  1887. stat = STATUS_OVERFLOW;
  1888. return stat;
  1889. }
  1890. // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
  1891. static inline void
  1892. addr16_hi(unsigned char* view, Address value)
  1893. { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
  1894. // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
  1895. static inline void
  1896. addr16_ha(unsigned char* view, Address value)
  1897. { This::addr16_hi(view, value + 0x8000); }
  1898. // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
  1899. static inline void
  1900. addr16_hi2(unsigned char* view, Address value)
  1901. { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
  1902. // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
  1903. static inline void
  1904. addr16_ha2(unsigned char* view, Address value)
  1905. { This::addr16_hi2(view, value + 0x8000); }
  1906. // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
  1907. static inline void
  1908. addr16_hi3(unsigned char* view, Address value)
  1909. { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
  1910. // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
  1911. static inline void
  1912. addr16_ha3(unsigned char* view, Address value)
  1913. { This::addr16_hi3(view, value + 0x8000); }
  1914. // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
  1915. static inline Status
  1916. addr14(unsigned char* view, Address value, Overflow_check overflow)
  1917. {
  1918. Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
  1919. if (overflow != CHECK_NONE && (value & 3) != 0)
  1920. stat = STATUS_OVERFLOW;
  1921. return stat;
  1922. }
  1923. // R_POWERPC_REL16DX_HA
  1924. static inline Status
  1925. addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
  1926. {
  1927. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  1928. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1929. Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
  1930. value += 0x8000;
  1931. value = static_cast<SignedAddress>(value) >> 16;
  1932. val |= (value & 0xffc1) | ((value & 0x3e) << 15);
  1933. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  1934. return overflowed<16>(value, overflow);
  1935. }
  1936. // R_PPC64_D34
  1937. static inline Status
  1938. addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
  1939. {
  1940. Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
  1941. value, overflow);
  1942. This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
  1943. return stat;
  1944. }
  1945. // R_PPC64_D34_HI30
  1946. static inline void
  1947. addr34_hi(unsigned char *view, uint64_t value)
  1948. { This::addr34(view, value >> 34, CHECK_NONE);}
  1949. // R_PPC64_D34_HA30
  1950. static inline void
  1951. addr34_ha(unsigned char *view, uint64_t value)
  1952. { This::addr34_hi(view, value + (1ULL << 33));}
  1953. // R_PPC64_D28
  1954. static inline Status
  1955. addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
  1956. {
  1957. Status stat = This::template rela<32,12>(view, 16, 0xfff,
  1958. value, overflow);
  1959. This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
  1960. return stat;
  1961. }
  1962. // R_PPC64_ADDR16_HIGHER34
  1963. static inline void
  1964. addr16_higher34(unsigned char* view, uint64_t value)
  1965. { This::addr16(view, value >> 34, CHECK_NONE); }
  1966. // R_PPC64_ADDR16_HIGHERA34
  1967. static inline void
  1968. addr16_highera34(unsigned char* view, uint64_t value)
  1969. { This::addr16_higher34(view, value + (1ULL << 33)); }
  1970. // R_PPC64_ADDR16_HIGHEST34
  1971. static inline void
  1972. addr16_highest34(unsigned char* view, uint64_t value)
  1973. { This::addr16(view, value >> 50, CHECK_NONE); }
  1974. // R_PPC64_ADDR16_HIGHESTA34
  1975. static inline void
  1976. addr16_highesta34(unsigned char* view, uint64_t value)
  1977. { This::addr16_highest34(view, value + (1ULL << 33)); }
  1978. };
  1979. // Set ABI version for input and output.
  1980. template<int size, bool big_endian>
  1981. void
  1982. Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
  1983. {
  1984. this->e_flags_ |= ver;
  1985. if (this->abiversion() != 0)
  1986. {
  1987. Target_powerpc<size, big_endian>* target =
  1988. static_cast<Target_powerpc<size, big_endian>*>(
  1989. parameters->sized_target<size, big_endian>());
  1990. if (target->abiversion() == 0)
  1991. target->set_abiversion(this->abiversion());
  1992. else if (target->abiversion() != this->abiversion())
  1993. gold_error(_("%s: ABI version %d is not compatible "
  1994. "with ABI version %d output"),
  1995. this->name().c_str(),
  1996. this->abiversion(), target->abiversion());
  1997. }
  1998. }
  1999. // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
  2000. // relocatable object, if such sections exists.
  2001. template<int size, bool big_endian>
  2002. bool
  2003. Powerpc_relobj<size, big_endian>::do_find_special_sections(
  2004. Read_symbols_data* sd)
  2005. {
  2006. const unsigned char* const pshdrs = sd->section_headers->data();
  2007. const unsigned char* namesu = sd->section_names->data();
  2008. const char* names = reinterpret_cast<const char*>(namesu);
  2009. section_size_type names_size = sd->section_names_size;
  2010. const unsigned char* s;
  2011. s = this->template find_shdr<size, big_endian>(pshdrs,
  2012. size == 32 ? ".got2" : ".opd",
  2013. names, names_size, NULL);
  2014. if (s != NULL)
  2015. {
  2016. unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
  2017. this->special_ = ndx;
  2018. if (size == 64)
  2019. {
  2020. if (this->abiversion() == 0)
  2021. this->set_abiversion(1);
  2022. else if (this->abiversion() > 1)
  2023. gold_error(_("%s: .opd invalid in abiv%d"),
  2024. this->name().c_str(), this->abiversion());
  2025. }
  2026. }
  2027. if (size == 64)
  2028. {
  2029. s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
  2030. names, names_size, NULL);
  2031. if (s != NULL)
  2032. {
  2033. unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
  2034. this->relatoc_ = ndx;
  2035. typename elfcpp::Shdr<size, big_endian> shdr(s);
  2036. this->toc_ = this->adjust_shndx(shdr.get_sh_info());
  2037. }
  2038. }
  2039. return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
  2040. }
  2041. // Examine .rela.opd to build info about function entry points.
  2042. template<int size, bool big_endian>
  2043. void
  2044. Powerpc_relobj<size, big_endian>::scan_opd_relocs(
  2045. size_t reloc_count,
  2046. const unsigned char* prelocs,
  2047. const unsigned char* plocal_syms)
  2048. {
  2049. if (size == 64)
  2050. {
  2051. typedef typename elfcpp::Rela<size, big_endian> Reltype;
  2052. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  2053. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  2054. Address expected_off = 0;
  2055. bool regular = true;
  2056. unsigned int opd_ent_size = 0;
  2057. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  2058. {
  2059. Reltype reloc(prelocs);
  2060. typename elfcpp::Elf_types<size>::Elf_WXword r_info
  2061. = reloc.get_r_info();
  2062. unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  2063. if (r_type == elfcpp::R_PPC64_ADDR64)
  2064. {
  2065. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  2066. typename elfcpp::Elf_types<size>::Elf_Addr value;
  2067. bool is_ordinary;
  2068. unsigned int shndx;
  2069. if (r_sym < this->local_symbol_count())
  2070. {
  2071. typename elfcpp::Sym<size, big_endian>
  2072. lsym(plocal_syms + r_sym * sym_size);
  2073. shndx = lsym.get_st_shndx();
  2074. shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  2075. value = lsym.get_st_value();
  2076. }
  2077. else
  2078. shndx = this->symbol_section_and_value(r_sym, &value,
  2079. &is_ordinary);
  2080. this->set_opd_ent(reloc.get_r_offset(), shndx,
  2081. value + reloc.get_r_addend());
  2082. if (i == 2)
  2083. {
  2084. expected_off = reloc.get_r_offset();
  2085. opd_ent_size = expected_off;
  2086. }
  2087. else if (expected_off != reloc.get_r_offset())
  2088. regular = false;
  2089. expected_off += opd_ent_size;
  2090. }
  2091. else if (r_type == elfcpp::R_PPC64_TOC)
  2092. {
  2093. if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
  2094. regular = false;
  2095. }
  2096. else
  2097. {
  2098. gold_warning(_("%s: unexpected reloc type %u in .opd section"),
  2099. this->name().c_str(), r_type);
  2100. regular = false;
  2101. }
  2102. }
  2103. if (reloc_count <= 2)
  2104. opd_ent_size = this->section_size(this->opd_shndx());
  2105. if (opd_ent_size != 24 && opd_ent_size != 16)
  2106. regular = false;
  2107. if (!regular)
  2108. {
  2109. gold_warning(_("%s: .opd is not a regular array of opd entries"),
  2110. this->name().c_str());
  2111. opd_ent_size = 0;
  2112. }
  2113. }
  2114. }
  2115. // Returns true if a code sequence loading the TOC entry at VALUE
  2116. // relative to the TOC pointer can be converted into code calculating
  2117. // a TOC pointer relative offset.
  2118. // If so, the TOC pointer relative offset is stored to VALUE.
  2119. template<int size, bool big_endian>
  2120. bool
  2121. Powerpc_relobj<size, big_endian>::make_toc_relative(
  2122. Target_powerpc<size, big_endian>* target,
  2123. Address* value)
  2124. {
  2125. if (size != 64)
  2126. return false;
  2127. // With -mcmodel=medium code it is quite possible to have
  2128. // toc-relative relocs referring to objects outside the TOC.
  2129. // Don't try to look at a non-existent TOC.
  2130. if (this->toc_shndx() == 0
  2131. || this->output_section(this->toc_shndx()) == 0)
  2132. return false;
  2133. // Convert VALUE back to an address by adding got_base (see below),
  2134. // then to an offset in the TOC by subtracting the TOC output
  2135. // section address and the TOC output offset.
  2136. Address off = (*value + target->toc_pointer()
  2137. - this->output_section(this->toc_shndx())->address()
  2138. - this->output_section_offset(this->toc_shndx()));
  2139. // Is this offset in the TOC? -mcmodel=medium code may be using
  2140. // TOC relative access to variables outside the TOC. Those of
  2141. // course can't be optimized. We also don't try to optimize code
  2142. // that is using a different object's TOC.
  2143. if (off >= this->section_size(this->toc_shndx()))
  2144. return false;
  2145. if (this->no_toc_opt(off))
  2146. return false;
  2147. section_size_type vlen;
  2148. unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
  2149. Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
  2150. // The TOC pointer
  2151. Address got_base = target->toc_pointer();
  2152. addr -= got_base;
  2153. if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
  2154. return false;
  2155. *value = addr;
  2156. return true;
  2157. }
  2158. template<int size, bool big_endian>
  2159. bool
  2160. Powerpc_relobj<size, big_endian>::make_got_relative(
  2161. Target_powerpc<size, big_endian>* target,
  2162. const Symbol_value<size>* psymval,
  2163. Address addend,
  2164. Address* value)
  2165. {
  2166. Address addr = psymval->value(this, addend);
  2167. Address got_base = target->toc_pointer();
  2168. addr -= got_base;
  2169. if (addr + 0x80008000 > 0xffffffff)
  2170. return false;
  2171. *value = addr;
  2172. return true;
  2173. }
  2174. // Perform the Sized_relobj_file method, then set up opd info from
  2175. // .opd relocs.
  2176. template<int size, bool big_endian>
  2177. void
  2178. Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
  2179. {
  2180. Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
  2181. if (size == 64)
  2182. {
  2183. for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
  2184. p != rd->relocs.end();
  2185. ++p)
  2186. {
  2187. if (p->data_shndx == this->opd_shndx())
  2188. {
  2189. uint64_t opd_size = this->section_size(this->opd_shndx());
  2190. gold_assert(opd_size == static_cast<size_t>(opd_size));
  2191. if (opd_size != 0)
  2192. {
  2193. this->init_opd(opd_size);
  2194. this->scan_opd_relocs(p->reloc_count, p->contents->data(),
  2195. rd->local_symbols->data());
  2196. }
  2197. break;
  2198. }
  2199. }
  2200. }
  2201. }
  2202. // Read the symbols then set up st_other vector.
  2203. template<int size, bool big_endian>
  2204. void
  2205. Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
  2206. {
  2207. this->base_read_symbols(sd);
  2208. if (this->input_file()->format() != Input_file::FORMAT_ELF)
  2209. return;
  2210. if (size == 64)
  2211. {
  2212. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  2213. const unsigned char* const pshdrs = sd->section_headers->data();
  2214. const unsigned int loccount = this->do_local_symbol_count();
  2215. if (loccount != 0)
  2216. {
  2217. this->st_other_.resize(loccount);
  2218. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  2219. off_t locsize = loccount * sym_size;
  2220. const unsigned int symtab_shndx = this->symtab_shndx();
  2221. const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
  2222. typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
  2223. const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
  2224. locsize, true, false);
  2225. psyms += sym_size;
  2226. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  2227. {
  2228. elfcpp::Sym<size, big_endian> sym(psyms);
  2229. unsigned char st_other = sym.get_st_other();
  2230. this->st_other_[i] = st_other;
  2231. if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
  2232. {
  2233. if (this->abiversion() == 0)
  2234. this->set_abiversion(2);
  2235. else if (this->abiversion() < 2)
  2236. gold_error(_("%s: local symbol %d has invalid st_other"
  2237. " for ABI version 1"),
  2238. this->name().c_str(), i);
  2239. }
  2240. }
  2241. }
  2242. }
  2243. const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  2244. const unsigned char* ps = sd->section_headers->data() + shdr_size;
  2245. bool merge_attributes = false;
  2246. for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
  2247. {
  2248. elfcpp::Shdr<size, big_endian> shdr(ps);
  2249. switch (shdr.get_sh_type())
  2250. {
  2251. case elfcpp::SHT_GNU_ATTRIBUTES:
  2252. {
  2253. gold_assert(this->attributes_section_data_ == NULL);
  2254. section_offset_type section_offset = shdr.get_sh_offset();
  2255. section_size_type section_size =
  2256. convert_to_section_size_type(shdr.get_sh_size());
  2257. const unsigned char* view =
  2258. this->get_view(section_offset, section_size, true, false);
  2259. this->attributes_section_data_ =
  2260. new Attributes_section_data(view, section_size);
  2261. }
  2262. break;
  2263. case elfcpp::SHT_SYMTAB:
  2264. {
  2265. // Sometimes an object has no contents except the section
  2266. // name string table and an empty symbol table with the
  2267. // undefined symbol. We don't want to merge
  2268. // processor-specific flags from such an object.
  2269. const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
  2270. elfcpp::Elf_sizes<size>::sym_size;
  2271. if (shdr.get_sh_size() > sym_size)
  2272. merge_attributes = true;
  2273. }
  2274. break;
  2275. case elfcpp::SHT_STRTAB:
  2276. break;
  2277. default:
  2278. merge_attributes = true;
  2279. break;
  2280. }
  2281. }
  2282. if (!merge_attributes)
  2283. {
  2284. // Should rarely happen.
  2285. delete this->attributes_section_data_;
  2286. this->attributes_section_data_ = NULL;
  2287. }
  2288. }
  2289. template<int size, bool big_endian>
  2290. void
  2291. Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
  2292. {
  2293. this->e_flags_ |= ver;
  2294. if (this->abiversion() != 0)
  2295. {
  2296. Target_powerpc<size, big_endian>* target =
  2297. static_cast<Target_powerpc<size, big_endian>*>(
  2298. parameters->sized_target<size, big_endian>());
  2299. if (target->abiversion() == 0)
  2300. target->set_abiversion(this->abiversion());
  2301. else if (target->abiversion() != this->abiversion())
  2302. gold_error(_("%s: ABI version %d is not compatible "
  2303. "with ABI version %d output"),
  2304. this->name().c_str(),
  2305. this->abiversion(), target->abiversion());
  2306. }
  2307. }
  2308. // Call Sized_dynobj::base_read_symbols to read the symbols then
  2309. // read .opd from a dynamic object, filling in opd_ent_ vector,
  2310. template<int size, bool big_endian>
  2311. void
  2312. Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
  2313. {
  2314. this->base_read_symbols(sd);
  2315. const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  2316. const unsigned char* ps =
  2317. sd->section_headers->data() + shdr_size * (this->shnum() - 1);
  2318. for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
  2319. {
  2320. elfcpp::Shdr<size, big_endian> shdr(ps);
  2321. if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
  2322. {
  2323. section_offset_type section_offset = shdr.get_sh_offset();
  2324. section_size_type section_size =
  2325. convert_to_section_size_type(shdr.get_sh_size());
  2326. const unsigned char* view =
  2327. this->get_view(section_offset, section_size, true, false);
  2328. this->attributes_section_data_ =
  2329. new Attributes_section_data(view, section_size);
  2330. break;
  2331. }
  2332. }
  2333. if (size == 64)
  2334. {
  2335. const unsigned char* const pshdrs = sd->section_headers->data();
  2336. const unsigned char* namesu = sd->section_names->data();
  2337. const char* names = reinterpret_cast<const char*>(namesu);
  2338. const unsigned char* s = NULL;
  2339. const unsigned char* opd;
  2340. section_size_type opd_size;
  2341. // Find and read .opd section.
  2342. while (1)
  2343. {
  2344. s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
  2345. sd->section_names_size,
  2346. s);
  2347. if (s == NULL)
  2348. return;
  2349. typename elfcpp::Shdr<size, big_endian> shdr(s);
  2350. if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
  2351. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
  2352. {
  2353. if (this->abiversion() == 0)
  2354. this->set_abiversion(1);
  2355. else if (this->abiversion() > 1)
  2356. gold_error(_("%s: .opd invalid in abiv%d"),
  2357. this->name().c_str(), this->abiversion());
  2358. this->opd_shndx_ = (s - pshdrs) / shdr_size;
  2359. this->opd_address_ = shdr.get_sh_addr();
  2360. opd_size = convert_to_section_size_type(shdr.get_sh_size());
  2361. opd = this->get_view(shdr.get_sh_offset(), opd_size,
  2362. true, false);
  2363. break;
  2364. }
  2365. }
  2366. // Build set of executable sections.
  2367. // Using a set is probably overkill. There is likely to be only
  2368. // a few executable sections, typically .init, .text and .fini,
  2369. // and they are generally grouped together.
  2370. typedef std::set<Sec_info> Exec_sections;
  2371. Exec_sections exec_sections;
  2372. s = pshdrs;
  2373. for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
  2374. {
  2375. typename elfcpp::Shdr<size, big_endian> shdr(s);
  2376. if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
  2377. && ((shdr.get_sh_flags()
  2378. & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
  2379. == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
  2380. && shdr.get_sh_size() != 0)
  2381. {
  2382. exec_sections.insert(Sec_info(shdr.get_sh_addr(),
  2383. shdr.get_sh_size(), i));
  2384. }
  2385. }
  2386. if (exec_sections.empty())
  2387. return;
  2388. // Look over the OPD entries. This is complicated by the fact
  2389. // that some binaries will use two-word entries while others
  2390. // will use the standard three-word entries. In most cases
  2391. // the third word (the environment pointer for languages like
  2392. // Pascal) is unused and will be zero. If the third word is
  2393. // used it should not be pointing into executable sections,
  2394. // I think.
  2395. this->init_opd(opd_size);
  2396. for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
  2397. {
  2398. typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
  2399. const Valtype* valp = reinterpret_cast<const Valtype*>(p);
  2400. Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
  2401. if (val == 0)
  2402. // Chances are that this is the third word of an OPD entry.
  2403. continue;
  2404. typename Exec_sections::const_iterator e
  2405. = exec_sections.upper_bound(Sec_info(val, 0, 0));
  2406. if (e != exec_sections.begin())
  2407. {
  2408. --e;
  2409. if (e->start <= val && val < e->start + e->len)
  2410. {
  2411. // We have an address in an executable section.
  2412. // VAL ought to be the function entry, set it up.
  2413. this->set_opd_ent(p - opd, e->shndx, val);
  2414. // Skip second word of OPD entry, the TOC pointer.
  2415. p += 8;
  2416. }
  2417. }
  2418. // If we didn't match any executable sections, we likely
  2419. // have a non-zero third word in the OPD entry.
  2420. }
  2421. }
  2422. }
  2423. // Relocate sections.
  2424. template<int size, bool big_endian>
  2425. void
  2426. Powerpc_relobj<size, big_endian>::do_relocate_sections(
  2427. const Symbol_table* symtab, const Layout* layout,
  2428. const unsigned char* pshdrs, Output_file* of,
  2429. typename Sized_relobj_file<size, big_endian>::Views* pviews)
  2430. {
  2431. unsigned int start = 1;
  2432. if (size == 64
  2433. && this->relatoc_ != 0
  2434. && !parameters->options().relocatable())
  2435. {
  2436. // Relocate .toc first.
  2437. this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
  2438. this->relatoc_, this->relatoc_);
  2439. this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
  2440. 1, this->relatoc_ - 1);
  2441. start = this->relatoc_ + 1;
  2442. }
  2443. this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
  2444. start, this->shnum() - 1);
  2445. }
  2446. // Set up some symbols.
  2447. template<int size, bool big_endian>
  2448. void
  2449. Target_powerpc<size, big_endian>::do_define_standard_symbols(
  2450. Symbol_table* symtab,
  2451. Layout* layout)
  2452. {
  2453. if (size == 32)
  2454. {
  2455. // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
  2456. // undefined when scanning relocs (and thus requires
  2457. // non-relative dynamic relocs). The proper value will be
  2458. // updated later.
  2459. Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
  2460. if (gotsym != NULL && gotsym->is_undefined())
  2461. {
  2462. Target_powerpc<size, big_endian>* target =
  2463. static_cast<Target_powerpc<size, big_endian>*>(
  2464. parameters->sized_target<size, big_endian>());
  2465. Output_data_got_powerpc<size, big_endian>* got
  2466. = target->got_section(symtab, layout, GOT_TYPE_SMALL);
  2467. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  2468. Symbol_table::PREDEFINED,
  2469. got, 0, 0,
  2470. elfcpp::STT_OBJECT,
  2471. elfcpp::STB_LOCAL,
  2472. elfcpp::STV_HIDDEN, 0,
  2473. false, false);
  2474. }
  2475. // Define _SDA_BASE_ at the start of the .sdata section + 32768.
  2476. Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
  2477. if (sdasym != NULL && sdasym->is_undefined())
  2478. {
  2479. Output_data_space* sdata = new Output_data_space(4, "** sdata");
  2480. Output_section* os
  2481. = layout->add_output_section_data(".sdata", 0,
  2482. elfcpp::SHF_ALLOC
  2483. | elfcpp::SHF_WRITE,
  2484. sdata, ORDER_SMALL_DATA, false);
  2485. symtab->define_in_output_data("_SDA_BASE_", NULL,
  2486. Symbol_table::PREDEFINED,
  2487. os, 32768, 0, elfcpp::STT_OBJECT,
  2488. elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
  2489. 0, false, false);
  2490. }
  2491. }
  2492. else
  2493. {
  2494. // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
  2495. Symbol *gotsym = symtab->lookup(".TOC.", NULL);
  2496. if (gotsym != NULL && gotsym->is_undefined())
  2497. {
  2498. Target_powerpc<size, big_endian>* target =
  2499. static_cast<Target_powerpc<size, big_endian>*>(
  2500. parameters->sized_target<size, big_endian>());
  2501. Output_data_got_powerpc<size, big_endian>* got
  2502. = target->got_section(symtab, layout, GOT_TYPE_SMALL);
  2503. symtab->define_in_output_data(".TOC.", NULL,
  2504. Symbol_table::PREDEFINED,
  2505. got, 0x8000, 0,
  2506. elfcpp::STT_OBJECT,
  2507. elfcpp::STB_LOCAL,
  2508. elfcpp::STV_HIDDEN, 0,
  2509. false, false);
  2510. }
  2511. }
  2512. this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
  2513. if (parameters->options().tls_get_addr_optimize()
  2514. && this->tls_get_addr_ != NULL
  2515. && this->tls_get_addr_->in_reg())
  2516. this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
  2517. if (this->tls_get_addr_opt_ != NULL)
  2518. {
  2519. if (this->tls_get_addr_->is_undefined()
  2520. || this->tls_get_addr_->is_from_dynobj())
  2521. {
  2522. // Make it seem as if references to __tls_get_addr are
  2523. // really to __tls_get_addr_opt, so the latter symbol is
  2524. // made dynamic, not the former.
  2525. this->tls_get_addr_->clear_in_reg();
  2526. this->tls_get_addr_opt_->set_in_reg();
  2527. }
  2528. // We have a non-dynamic definition for __tls_get_addr.
  2529. // Make __tls_get_addr_opt the same, if it does not already have
  2530. // a non-dynamic definition.
  2531. else if (this->tls_get_addr_opt_->is_undefined()
  2532. || this->tls_get_addr_opt_->is_from_dynobj())
  2533. {
  2534. Sized_symbol<size>* from
  2535. = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
  2536. Sized_symbol<size>* to
  2537. = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
  2538. symtab->clone<size>(to, from);
  2539. }
  2540. }
  2541. }
  2542. // Set up PowerPC target specific relobj.
  2543. template<int size, bool big_endian>
  2544. Object*
  2545. Target_powerpc<size, big_endian>::do_make_elf_object(
  2546. const std::string& name,
  2547. Input_file* input_file,
  2548. off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
  2549. {
  2550. int et = ehdr.get_e_type();
  2551. // ET_EXEC files are valid input for --just-symbols/-R,
  2552. // and we treat them as relocatable objects.
  2553. if (et == elfcpp::ET_REL
  2554. || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
  2555. {
  2556. Powerpc_relobj<size, big_endian>* obj =
  2557. new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
  2558. obj->setup();
  2559. return obj;
  2560. }
  2561. else if (et == elfcpp::ET_DYN)
  2562. {
  2563. Powerpc_dynobj<size, big_endian>* obj =
  2564. new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
  2565. obj->setup();
  2566. return obj;
  2567. }
  2568. else
  2569. {
  2570. gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
  2571. return NULL;
  2572. }
  2573. }
  2574. template<int size, bool big_endian>
  2575. class Output_data_got_powerpc : public Output_data_got<size, big_endian>
  2576. {
  2577. public:
  2578. typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
  2579. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
  2580. Output_data_got_powerpc(Symbol_table* symtab, Layout* layout,
  2581. Got_type got_type)
  2582. : Output_data_got<size, big_endian>(),
  2583. symtab_(symtab), layout_(layout),
  2584. header_ent_cnt_(size == 32 ? 3 : 1),
  2585. header_index_(size == 32 ? 0x2000 : -1u)
  2586. {
  2587. if (size == 64)
  2588. this->set_addralign(256);
  2589. if (size == 64 && (got_type & GOT_TYPE_SMALL))
  2590. this->make_header();
  2591. }
  2592. // Override all the Output_data_got methods we use so as to first call
  2593. // reserve_ent().
  2594. bool
  2595. add_global(Symbol* gsym, unsigned int got_type, uint64_t addend)
  2596. {
  2597. this->reserve_ent();
  2598. return Output_data_got<size, big_endian>::add_global(gsym, got_type,
  2599. addend);
  2600. }
  2601. bool
  2602. add_global_plt(Symbol* gsym, unsigned int got_type, uint64_t addend)
  2603. {
  2604. this->reserve_ent();
  2605. return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type,
  2606. addend);
  2607. }
  2608. bool
  2609. add_global_tls(Symbol* gsym, unsigned int got_type, uint64_t addend)
  2610. { return this->add_global_plt(gsym, got_type, addend); }
  2611. void
  2612. add_global_with_rel(Symbol* gsym, unsigned int got_type,
  2613. Output_data_reloc_generic* rel_dyn,
  2614. unsigned int r_type, uint64_t addend)
  2615. {
  2616. this->reserve_ent();
  2617. Output_data_got<size, big_endian>::
  2618. add_global_with_rel(gsym, got_type, rel_dyn, r_type, addend);
  2619. }
  2620. void
  2621. add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
  2622. Output_data_reloc_generic* rel_dyn,
  2623. unsigned int r_type_1, unsigned int r_type_2,
  2624. uint64_t addend)
  2625. {
  2626. if (gsym->has_got_offset(got_type))
  2627. return;
  2628. this->reserve_ent(2);
  2629. Output_data_got<size, big_endian>::
  2630. add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2,
  2631. addend);
  2632. }
  2633. bool
  2634. add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
  2635. uint64_t addend)
  2636. {
  2637. this->reserve_ent();
  2638. return Output_data_got<size, big_endian>::add_local(object, sym_index,
  2639. got_type, addend);
  2640. }
  2641. bool
  2642. add_local_plt(Relobj* object, unsigned int sym_index,
  2643. unsigned int got_type, uint64_t addend)
  2644. {
  2645. this->reserve_ent();
  2646. return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
  2647. got_type, addend);
  2648. }
  2649. bool
  2650. add_local_tls(Relobj* object, unsigned int sym_index,
  2651. unsigned int got_type, uint64_t addend)
  2652. { return this->add_local_plt(object, sym_index, got_type, addend); }
  2653. void
  2654. add_local_tls_pair(Relobj* object, unsigned int sym_index,
  2655. unsigned int got_type,
  2656. Output_data_reloc_generic* rel_dyn,
  2657. unsigned int r_type, uint64_t addend)
  2658. {
  2659. if (object->local_has_got_offset(sym_index, got_type, addend))
  2660. return;
  2661. this->reserve_ent(2);
  2662. Output_data_got<size, big_endian>::
  2663. add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type, addend);
  2664. }
  2665. unsigned int
  2666. add_constant(Valtype constant)
  2667. {
  2668. this->reserve_ent();
  2669. return Output_data_got<size, big_endian>::add_constant(constant);
  2670. }
  2671. unsigned int
  2672. add_constant_pair(Valtype c1, Valtype c2)
  2673. {
  2674. this->reserve_ent(2);
  2675. return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
  2676. }
  2677. // Offset of _GLOBAL_OFFSET_TABLE_ and .TOC. in this section.
  2678. unsigned int
  2679. g_o_t() const
  2680. {
  2681. if (size == 32)
  2682. return this->got_offset(this->header_index_);
  2683. else if (this->header_index_ != -1u)
  2684. return this->got_offset(this->header_index_) + 0x8000;
  2685. else
  2686. gold_unreachable();
  2687. }
  2688. // Ensure our GOT has a header.
  2689. void
  2690. set_final_data_size()
  2691. {
  2692. if (size == 32 && this->header_ent_cnt_ != 0)
  2693. this->make_header();
  2694. Output_data_got<size, big_endian>::set_final_data_size();
  2695. }
  2696. // First word of GOT header needs some values that are not
  2697. // handled by Output_data_got so poke them in here.
  2698. // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
  2699. void
  2700. do_write(Output_file* of)
  2701. {
  2702. if (this->header_index_ != -1u)
  2703. {
  2704. Valtype val = 0;
  2705. if (size == 32 && this->layout_->dynamic_data() != NULL)
  2706. val = this->layout_->dynamic_section()->address();
  2707. if (size == 64)
  2708. val = this->address() + this->g_o_t();
  2709. this->replace_constant(this->header_index_, val);
  2710. }
  2711. Output_data_got<size, big_endian>::do_write(of);
  2712. }
  2713. private:
  2714. void
  2715. reserve_ent(unsigned int cnt = 1)
  2716. {
  2717. if (size != 32 || this->header_ent_cnt_ == 0)
  2718. return;
  2719. if (this->num_entries() + cnt > this->header_index_)
  2720. this->make_header();
  2721. }
  2722. void
  2723. make_header()
  2724. {
  2725. this->header_ent_cnt_ = 0;
  2726. this->header_index_ = this->num_entries();
  2727. if (size == 32)
  2728. {
  2729. Output_data_got<size, big_endian>::add_constant(0);
  2730. Output_data_got<size, big_endian>::add_constant(0);
  2731. Output_data_got<size, big_endian>::add_constant(0);
  2732. // Define _GLOBAL_OFFSET_TABLE_ at the header
  2733. Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
  2734. if (gotsym != NULL)
  2735. {
  2736. Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
  2737. sym->set_value(this->g_o_t());
  2738. }
  2739. else
  2740. this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  2741. Symbol_table::PREDEFINED,
  2742. this, this->g_o_t(), 0,
  2743. elfcpp::STT_OBJECT,
  2744. elfcpp::STB_LOCAL,
  2745. elfcpp::STV_HIDDEN, 0,
  2746. false, false);
  2747. }
  2748. else
  2749. Output_data_got<size, big_endian>::add_constant(0);
  2750. }
  2751. // Stashed pointers.
  2752. Symbol_table* symtab_;
  2753. Layout* layout_;
  2754. // GOT header size.
  2755. unsigned int header_ent_cnt_;
  2756. // GOT header index.
  2757. unsigned int header_index_;
  2758. };
  2759. // Get the GOT section, creating it if necessary.
  2760. template<int size, bool big_endian>
  2761. Output_data_got_powerpc<size, big_endian>*
  2762. Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
  2763. Layout* layout,
  2764. Got_type got_type)
  2765. {
  2766. if (this->got_ == NULL)
  2767. {
  2768. gold_assert(symtab != NULL && layout != NULL);
  2769. this->got_
  2770. = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
  2771. GOT_TYPE_SMALL);
  2772. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  2773. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  2774. this->got_, ORDER_DATA, false);
  2775. }
  2776. if (size == 32 || (got_type & GOT_TYPE_SMALL))
  2777. return this->got_;
  2778. if (this->biggot_ == NULL)
  2779. {
  2780. this->biggot_
  2781. = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
  2782. GOT_TYPE_STANDARD);
  2783. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  2784. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  2785. this->biggot_, ORDER_DATA, false);
  2786. }
  2787. return this->biggot_;
  2788. }
  2789. // Get the dynamic reloc section, creating it if necessary.
  2790. template<int size, bool big_endian>
  2791. typename Target_powerpc<size, big_endian>::Reloc_section*
  2792. Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
  2793. {
  2794. if (this->rela_dyn_ == NULL)
  2795. {
  2796. gold_assert(layout != NULL);
  2797. this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
  2798. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  2799. elfcpp::SHF_ALLOC, this->rela_dyn_,
  2800. ORDER_DYNAMIC_RELOCS, false);
  2801. }
  2802. return this->rela_dyn_;
  2803. }
  2804. // Similarly, but for ifunc symbols get the one for ifunc.
  2805. template<int size, bool big_endian>
  2806. typename Target_powerpc<size, big_endian>::Reloc_section*
  2807. Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
  2808. Layout* layout,
  2809. bool for_ifunc)
  2810. {
  2811. if (!for_ifunc)
  2812. return this->rela_dyn_section(layout);
  2813. if (this->iplt_ == NULL)
  2814. this->make_iplt_section(symtab, layout);
  2815. return this->iplt_->rel_plt();
  2816. }
  2817. class Stub_control
  2818. {
  2819. public:
  2820. // Determine the stub group size. The group size is the absolute
  2821. // value of the parameter --stub-group-size. If --stub-group-size
  2822. // is passed a negative value, we restrict stubs to be always after
  2823. // the stubbed branches.
  2824. Stub_control(int32_t size, bool no_size_errors, bool multi_os)
  2825. : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
  2826. suppress_size_errors_(no_size_errors), multi_os_(multi_os),
  2827. state_(NO_GROUP), group_size_(0), group_start_addr_(0),
  2828. owner_(NULL), output_section_(NULL)
  2829. {
  2830. }
  2831. // Return true iff input section can be handled by current stub
  2832. // group.
  2833. bool
  2834. can_add_to_stub_group(Output_section* o,
  2835. const Output_section::Input_section* i,
  2836. bool has14);
  2837. const Output_section::Input_section*
  2838. owner()
  2839. { return owner_; }
  2840. Output_section*
  2841. output_section()
  2842. { return output_section_; }
  2843. void
  2844. set_output_and_owner(Output_section* o,
  2845. const Output_section::Input_section* i)
  2846. {
  2847. this->output_section_ = o;
  2848. this->owner_ = i;
  2849. }
  2850. private:
  2851. typedef enum
  2852. {
  2853. // Initial state.
  2854. NO_GROUP,
  2855. // Adding group sections before the stubs.
  2856. FINDING_STUB_SECTION,
  2857. // Adding group sections after the stubs.
  2858. HAS_STUB_SECTION
  2859. } State;
  2860. uint32_t stub_group_size_;
  2861. bool stubs_always_after_branch_;
  2862. bool suppress_size_errors_;
  2863. // True if a stub group can serve multiple output sections.
  2864. bool multi_os_;
  2865. State state_;
  2866. // Current max size of group. Starts at stub_group_size_ but is
  2867. // reduced to stub_group_size_/1024 on seeing a section with
  2868. // external conditional branches.
  2869. uint32_t group_size_;
  2870. uint64_t group_start_addr_;
  2871. // owner_ and output_section_ specify the section to which stubs are
  2872. // attached. The stubs are placed at the end of this section.
  2873. const Output_section::Input_section* owner_;
  2874. Output_section* output_section_;
  2875. };
  2876. // Return true iff input section can be handled by current stub
  2877. // group. Sections are presented to this function in order,
  2878. // so the first section is the head of the group.
  2879. bool
  2880. Stub_control::can_add_to_stub_group(Output_section* o,
  2881. const Output_section::Input_section* i,
  2882. bool has14)
  2883. {
  2884. bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
  2885. uint64_t this_size;
  2886. uint64_t start_addr = o->address();
  2887. if (whole_sec)
  2888. // .init and .fini sections are pasted together to form a single
  2889. // function. We can't be adding stubs in the middle of the function.
  2890. this_size = o->data_size();
  2891. else
  2892. {
  2893. start_addr += i->relobj()->output_section_offset(i->shndx());
  2894. this_size = i->data_size();
  2895. }
  2896. uint64_t end_addr = start_addr + this_size;
  2897. uint32_t group_size = this->stub_group_size_;
  2898. if (has14)
  2899. this->group_size_ = group_size = group_size >> 10;
  2900. if (this_size > group_size && !this->suppress_size_errors_)
  2901. gold_warning(_("%s:%s exceeds group size"),
  2902. i->relobj()->name().c_str(),
  2903. i->relobj()->section_name(i->shndx()).c_str());
  2904. gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
  2905. has14 ? " 14bit" : "",
  2906. i->relobj()->name().c_str(),
  2907. i->relobj()->section_name(i->shndx()).c_str(),
  2908. (long long) this_size,
  2909. (this->state_ == NO_GROUP
  2910. ? this_size
  2911. : (long long) end_addr - this->group_start_addr_));
  2912. if (this->state_ == NO_GROUP)
  2913. {
  2914. // Only here on very first use of Stub_control
  2915. this->owner_ = i;
  2916. this->output_section_ = o;
  2917. this->state_ = FINDING_STUB_SECTION;
  2918. this->group_size_ = group_size;
  2919. this->group_start_addr_ = start_addr;
  2920. return true;
  2921. }
  2922. else if (!this->multi_os_ && this->output_section_ != o)
  2923. ;
  2924. else if (this->state_ == HAS_STUB_SECTION)
  2925. {
  2926. // Can we add this section, which is after the stubs, to the
  2927. // group?
  2928. if (end_addr - this->group_start_addr_ <= this->group_size_)
  2929. return true;
  2930. }
  2931. else if (this->state_ == FINDING_STUB_SECTION)
  2932. {
  2933. if ((whole_sec && this->output_section_ == o)
  2934. || end_addr - this->group_start_addr_ <= this->group_size_)
  2935. {
  2936. // Stubs are added at the end of "owner_".
  2937. this->owner_ = i;
  2938. this->output_section_ = o;
  2939. return true;
  2940. }
  2941. // The group before the stubs has reached maximum size.
  2942. // Now see about adding sections after the stubs to the
  2943. // group. If the current section has a 14-bit branch and
  2944. // the group before the stubs exceeds group_size_ (because
  2945. // they didn't have 14-bit branches), don't add sections
  2946. // after the stubs: The size of stubs for such a large
  2947. // group may exceed the reach of a 14-bit branch.
  2948. if (!this->stubs_always_after_branch_
  2949. && this_size <= this->group_size_
  2950. && start_addr - this->group_start_addr_ <= this->group_size_)
  2951. {
  2952. gold_debug(DEBUG_TARGET, "adding after stubs");
  2953. this->state_ = HAS_STUB_SECTION;
  2954. this->group_start_addr_ = start_addr;
  2955. return true;
  2956. }
  2957. }
  2958. else
  2959. gold_unreachable();
  2960. gold_debug(DEBUG_TARGET,
  2961. !this->multi_os_ && this->output_section_ != o
  2962. ? "nope, new output section\n"
  2963. : "nope, didn't fit\n");
  2964. // The section fails to fit in the current group. Set up a few
  2965. // things for the next group. owner_ and output_section_ will be
  2966. // set later after we've retrieved those values for the current
  2967. // group.
  2968. this->state_ = FINDING_STUB_SECTION;
  2969. this->group_size_ = group_size;
  2970. this->group_start_addr_ = start_addr;
  2971. return false;
  2972. }
  2973. // Look over all the input sections, deciding where to place stubs.
  2974. template<int size, bool big_endian>
  2975. void
  2976. Target_powerpc<size, big_endian>::group_sections(Layout* layout,
  2977. const Task*,
  2978. bool no_size_errors)
  2979. {
  2980. Stub_control stub_control(this->stub_group_size_, no_size_errors,
  2981. parameters->options().stub_group_multi());
  2982. // Group input sections and insert stub table
  2983. Stub_table_owner* table_owner = NULL;
  2984. std::vector<Stub_table_owner*> tables;
  2985. Layout::Section_list section_list;
  2986. layout->get_executable_sections(&section_list);
  2987. std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
  2988. for (Layout::Section_list::iterator o = section_list.begin();
  2989. o != section_list.end();
  2990. ++o)
  2991. {
  2992. typedef Output_section::Input_section_list Input_section_list;
  2993. for (Input_section_list::const_iterator i
  2994. = (*o)->input_sections().begin();
  2995. i != (*o)->input_sections().end();
  2996. ++i)
  2997. {
  2998. if (i->is_input_section()
  2999. || i->is_relaxed_input_section())
  3000. {
  3001. Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  3002. <Powerpc_relobj<size, big_endian>*>(i->relobj());
  3003. bool has14 = ppcobj->has_14bit_branch(i->shndx());
  3004. if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
  3005. {
  3006. table_owner->output_section = stub_control.output_section();
  3007. table_owner->owner = stub_control.owner();
  3008. stub_control.set_output_and_owner(*o, &*i);
  3009. table_owner = NULL;
  3010. }
  3011. if (table_owner == NULL)
  3012. {
  3013. table_owner = new Stub_table_owner;
  3014. tables.push_back(table_owner);
  3015. }
  3016. ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
  3017. }
  3018. }
  3019. }
  3020. if (table_owner != NULL)
  3021. {
  3022. table_owner->output_section = stub_control.output_section();
  3023. table_owner->owner = stub_control.owner();;
  3024. }
  3025. for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
  3026. t != tables.end();
  3027. ++t)
  3028. {
  3029. Stub_table<size, big_endian>* stub_table;
  3030. if ((*t)->owner->is_input_section())
  3031. stub_table = new Stub_table<size, big_endian>(this,
  3032. (*t)->output_section,
  3033. (*t)->owner,
  3034. this->stub_tables_.size());
  3035. else if ((*t)->owner->is_relaxed_input_section())
  3036. stub_table = static_cast<Stub_table<size, big_endian>*>(
  3037. (*t)->owner->relaxed_input_section());
  3038. else
  3039. gold_unreachable();
  3040. this->stub_tables_.push_back(stub_table);
  3041. delete *t;
  3042. }
  3043. }
  3044. template<int size>
  3045. static unsigned long
  3046. max_branch_delta (unsigned int r_type)
  3047. {
  3048. if (r_type == elfcpp::R_POWERPC_REL14
  3049. || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
  3050. || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
  3051. return 1L << 15;
  3052. if (r_type == elfcpp::R_POWERPC_REL24
  3053. || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
  3054. || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
  3055. || r_type == elfcpp::R_PPC_PLTREL24
  3056. || r_type == elfcpp::R_PPC_LOCAL24PC)
  3057. return 1L << 25;
  3058. return 0;
  3059. }
  3060. // Return whether this branch is going via a plt call stub.
  3061. template<int size, bool big_endian>
  3062. bool
  3063. Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
  3064. Powerpc_relobj<size, big_endian>* ppc_object,
  3065. unsigned int shndx,
  3066. Address offset,
  3067. Target_powerpc* target,
  3068. Symbol_table* symtab)
  3069. {
  3070. if (this->object_ != ppc_object
  3071. || this->shndx_ != shndx
  3072. || this->offset_ != offset)
  3073. return false;
  3074. Symbol* sym = this->object_->global_symbol(this->r_sym_);
  3075. if (sym != NULL && sym->is_forwarder())
  3076. sym = symtab->resolve_forwards(sym);
  3077. if (target->replace_tls_get_addr(sym))
  3078. sym = target->tls_get_addr_opt();
  3079. const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
  3080. if (gsym != NULL
  3081. ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
  3082. && !target->is_elfv2_localentry0(gsym))
  3083. : (this->object_->local_has_plt_offset(this->r_sym_)
  3084. && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
  3085. {
  3086. this->tocsave_ = 1;
  3087. return true;
  3088. }
  3089. return false;
  3090. }
  3091. // If this branch needs a plt call stub, or a long branch stub, make one.
  3092. template<int size, bool big_endian>
  3093. bool
  3094. Target_powerpc<size, big_endian>::Branch_info::make_stub(
  3095. Stub_table<size, big_endian>* stub_table,
  3096. Stub_table<size, big_endian>* ifunc_stub_table,
  3097. Symbol_table* symtab) const
  3098. {
  3099. Symbol* sym = this->object_->global_symbol(this->r_sym_);
  3100. Target_powerpc<size, big_endian>* target =
  3101. static_cast<Target_powerpc<size, big_endian>*>(
  3102. parameters->sized_target<size, big_endian>());
  3103. if (sym != NULL && sym->is_forwarder())
  3104. sym = symtab->resolve_forwards(sym);
  3105. if (target->replace_tls_get_addr(sym))
  3106. sym = target->tls_get_addr_opt();
  3107. const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
  3108. bool ok = true;
  3109. if (gsym != NULL
  3110. ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
  3111. : this->object_->local_has_plt_offset(this->r_sym_))
  3112. {
  3113. if (size == 64
  3114. && gsym != NULL
  3115. && target->abiversion() >= 2
  3116. && !parameters->options().output_is_position_independent()
  3117. && !is_branch_reloc<size>(this->r_type_))
  3118. target->glink_section()->add_global_entry(gsym);
  3119. else
  3120. {
  3121. if (stub_table == NULL
  3122. && !(size == 32
  3123. && gsym != NULL
  3124. && !parameters->options().output_is_position_independent()
  3125. && !is_branch_reloc<size>(this->r_type_)))
  3126. stub_table = this->object_->stub_table(this->shndx_);
  3127. if (stub_table == NULL)
  3128. {
  3129. // This is a ref from a data section to an ifunc symbol,
  3130. // or a non-branch reloc for which we always want to use
  3131. // one set of stubs for resolving function addresses.
  3132. stub_table = ifunc_stub_table;
  3133. }
  3134. gold_assert(stub_table != NULL);
  3135. Address from = this->object_->get_output_section_offset(this->shndx_);
  3136. if (from != invalid_address)
  3137. from += (this->object_->output_section(this->shndx_)->address()
  3138. + this->offset_);
  3139. if (gsym != NULL)
  3140. ok = stub_table->add_plt_call_entry(from,
  3141. this->object_, gsym,
  3142. this->r_type_, this->addend_,
  3143. this->tocsave_);
  3144. else
  3145. ok = stub_table->add_plt_call_entry(from,
  3146. this->object_, this->r_sym_,
  3147. this->r_type_, this->addend_,
  3148. this->tocsave_);
  3149. }
  3150. }
  3151. else
  3152. {
  3153. Address max_branch_offset = max_branch_delta<size>(this->r_type_);
  3154. if (max_branch_offset == 0)
  3155. return true;
  3156. Address from = this->object_->get_output_section_offset(this->shndx_);
  3157. gold_assert(from != invalid_address);
  3158. from += (this->object_->output_section(this->shndx_)->address()
  3159. + this->offset_);
  3160. Address to;
  3161. unsigned int other = 0;
  3162. if (gsym != NULL)
  3163. {
  3164. switch (gsym->source())
  3165. {
  3166. case Symbol::FROM_OBJECT:
  3167. {
  3168. Object* symobj = gsym->object();
  3169. if (symobj->is_dynamic()
  3170. || symobj->pluginobj() != NULL)
  3171. return true;
  3172. bool is_ordinary;
  3173. unsigned int shndx = gsym->shndx(&is_ordinary);
  3174. if (shndx == elfcpp::SHN_UNDEF)
  3175. return true;
  3176. }
  3177. break;
  3178. case Symbol::IS_UNDEFINED:
  3179. return true;
  3180. default:
  3181. break;
  3182. }
  3183. Symbol_table::Compute_final_value_status status;
  3184. to = symtab->compute_final_value<size>(gsym, &status);
  3185. if (status != Symbol_table::CFVS_OK)
  3186. return true;
  3187. if (size == 64)
  3188. other = gsym->nonvis() >> 3;
  3189. }
  3190. else
  3191. {
  3192. const Symbol_value<size>* psymval
  3193. = this->object_->local_symbol(this->r_sym_);
  3194. Symbol_value<size> symval;
  3195. if (psymval->is_section_symbol())
  3196. symval.set_is_section_symbol();
  3197. typedef Sized_relobj_file<size, big_endian> ObjType;
  3198. typename ObjType::Compute_final_local_value_status status
  3199. = this->object_->compute_final_local_value(this->r_sym_, psymval,
  3200. &symval, symtab);
  3201. if (status != ObjType::CFLV_OK
  3202. || !symval.has_output_value())
  3203. return true;
  3204. to = symval.value(this->object_, 0);
  3205. if (size == 64)
  3206. other = this->object_->st_other(this->r_sym_) >> 5;
  3207. }
  3208. if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
  3209. to += this->addend_;
  3210. if (stub_table == NULL)
  3211. stub_table = this->object_->stub_table(this->shndx_);
  3212. if (size == 64 && target->abiversion() < 2)
  3213. {
  3214. unsigned int dest_shndx;
  3215. if (!target->symval_for_branch(symtab, gsym, this->object_,
  3216. &to, &dest_shndx))
  3217. return true;
  3218. }
  3219. unsigned int local_ent = 0;
  3220. if (size == 64
  3221. && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC
  3222. && this->r_type_ != elfcpp::R_PPC64_REL24_P9NOTOC)
  3223. local_ent = elfcpp::ppc64_decode_local_entry(other);
  3224. Address delta = to + local_ent - from;
  3225. if (delta + max_branch_offset >= 2 * max_branch_offset
  3226. || (size == 64
  3227. && (this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
  3228. || this->r_type_ == elfcpp::R_PPC64_REL24_P9NOTOC)
  3229. && (gsym != NULL
  3230. ? this->object_->ppc64_needs_toc(gsym)
  3231. : this->object_->ppc64_needs_toc(this->r_sym_))))
  3232. {
  3233. if (stub_table == NULL)
  3234. {
  3235. gold_warning(_("%s:%s: branch in non-executable section,"
  3236. " no long branch stub for you"),
  3237. this->object_->name().c_str(),
  3238. this->object_->section_name(this->shndx_).c_str());
  3239. return true;
  3240. }
  3241. bool save_res = (size == 64
  3242. && gsym != NULL
  3243. && gsym->source() == Symbol::IN_OUTPUT_DATA
  3244. && gsym->output_data() == target->savres_section());
  3245. ok = stub_table->add_long_branch_entry(this->r_type_,
  3246. from, to, other, save_res);
  3247. }
  3248. }
  3249. if (!ok)
  3250. gold_debug(DEBUG_TARGET,
  3251. "branch at %s:%s+%#lx\n"
  3252. "can't reach stub attached to %s:%s",
  3253. this->object_->name().c_str(),
  3254. this->object_->section_name(this->shndx_).c_str(),
  3255. (unsigned long) this->offset_,
  3256. stub_table->relobj()->name().c_str(),
  3257. stub_table->relobj()->section_name(stub_table->shndx()).c_str());
  3258. return ok;
  3259. }
  3260. // Relaxation hook. This is where we do stub generation.
  3261. template<int size, bool big_endian>
  3262. bool
  3263. Target_powerpc<size, big_endian>::do_relax(int pass,
  3264. const Input_objects*,
  3265. Symbol_table* symtab,
  3266. Layout* layout,
  3267. const Task* task)
  3268. {
  3269. unsigned int prev_brlt_size = 0;
  3270. if (pass == 1)
  3271. {
  3272. bool thread_safe
  3273. = this->abiversion() < 2 && parameters->options().plt_thread_safe();
  3274. if (size == 64
  3275. && this->abiversion() < 2
  3276. && !thread_safe
  3277. && !parameters->options().user_set_plt_thread_safe())
  3278. {
  3279. static const char* const thread_starter[] =
  3280. {
  3281. "pthread_create",
  3282. /* libstdc++ */
  3283. "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
  3284. /* librt */
  3285. "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
  3286. "mq_notify", "create_timer",
  3287. /* libanl */
  3288. "getaddrinfo_a",
  3289. /* libgomp */
  3290. "GOMP_parallel",
  3291. "GOMP_parallel_start",
  3292. "GOMP_parallel_loop_static",
  3293. "GOMP_parallel_loop_static_start",
  3294. "GOMP_parallel_loop_dynamic",
  3295. "GOMP_parallel_loop_dynamic_start",
  3296. "GOMP_parallel_loop_guided",
  3297. "GOMP_parallel_loop_guided_start",
  3298. "GOMP_parallel_loop_runtime",
  3299. "GOMP_parallel_loop_runtime_start",
  3300. "GOMP_parallel_sections",
  3301. "GOMP_parallel_sections_start",
  3302. /* libgo */
  3303. "__go_go",
  3304. };
  3305. if (parameters->options().shared())
  3306. thread_safe = true;
  3307. else
  3308. {
  3309. for (unsigned int i = 0;
  3310. i < sizeof(thread_starter) / sizeof(thread_starter[0]);
  3311. i++)
  3312. {
  3313. Symbol* sym = symtab->lookup(thread_starter[i], NULL);
  3314. thread_safe = (sym != NULL
  3315. && sym->in_reg()
  3316. && sym->in_real_elf());
  3317. if (thread_safe)
  3318. break;
  3319. }
  3320. }
  3321. }
  3322. this->plt_thread_safe_ = thread_safe;
  3323. }
  3324. if (pass == 1)
  3325. {
  3326. this->stub_group_size_ = parameters->options().stub_group_size();
  3327. bool no_size_errors = true;
  3328. if (this->stub_group_size_ == 1)
  3329. this->stub_group_size_ = 0x1c00000;
  3330. else if (this->stub_group_size_ == -1)
  3331. this->stub_group_size_ = -0x1e00000;
  3332. else
  3333. no_size_errors = false;
  3334. this->group_sections(layout, task, no_size_errors);
  3335. }
  3336. else if (this->relax_failed_ && this->relax_fail_count_ < 3)
  3337. {
  3338. this->branch_lookup_table_.clear();
  3339. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3340. p != this->stub_tables_.end();
  3341. ++p)
  3342. {
  3343. (*p)->clear_stubs(true);
  3344. }
  3345. this->stub_tables_.clear();
  3346. this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
  3347. gold_info(_("%s: stub group size is too large; retrying with %#x"),
  3348. program_name, this->stub_group_size_);
  3349. this->group_sections(layout, task, true);
  3350. }
  3351. // We need address of stub tables valid for make_stub.
  3352. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3353. p != this->stub_tables_.end();
  3354. ++p)
  3355. {
  3356. const Powerpc_relobj<size, big_endian>* object
  3357. = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
  3358. Address off = object->get_output_section_offset((*p)->shndx());
  3359. gold_assert(off != invalid_address);
  3360. Output_section* os = (*p)->output_section();
  3361. (*p)->set_address_and_size(os, off);
  3362. }
  3363. if (pass != 1)
  3364. {
  3365. // Clear plt call stubs, long branch stubs and branch lookup table.
  3366. prev_brlt_size = this->branch_lookup_table_.size();
  3367. this->branch_lookup_table_.clear();
  3368. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3369. p != this->stub_tables_.end();
  3370. ++p)
  3371. {
  3372. (*p)->clear_stubs(false);
  3373. }
  3374. }
  3375. // Build all the stubs.
  3376. this->relax_failed_ = false;
  3377. Stub_table<size, big_endian>* ifunc_stub_table
  3378. = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
  3379. Stub_table<size, big_endian>* one_stub_table
  3380. = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
  3381. for (typename Branches::const_iterator b = this->branch_info_.begin();
  3382. b != this->branch_info_.end();
  3383. b++)
  3384. {
  3385. if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
  3386. && !this->relax_failed_)
  3387. {
  3388. this->relax_failed_ = true;
  3389. this->relax_fail_count_++;
  3390. if (this->relax_fail_count_ < 3)
  3391. return true;
  3392. }
  3393. }
  3394. bool do_resize = false;
  3395. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3396. p != this->stub_tables_.end();
  3397. ++p)
  3398. if ((*p)->need_resize())
  3399. {
  3400. do_resize = true;
  3401. break;
  3402. }
  3403. if (do_resize)
  3404. {
  3405. this->branch_lookup_table_.clear();
  3406. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3407. p != this->stub_tables_.end();
  3408. ++p)
  3409. (*p)->set_resizing(true);
  3410. for (typename Branches::const_iterator b = this->branch_info_.begin();
  3411. b != this->branch_info_.end();
  3412. b++)
  3413. {
  3414. if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
  3415. && !this->relax_failed_)
  3416. {
  3417. this->relax_failed_ = true;
  3418. this->relax_fail_count_++;
  3419. if (this->relax_fail_count_ < 3)
  3420. return true;
  3421. }
  3422. }
  3423. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3424. p != this->stub_tables_.end();
  3425. ++p)
  3426. (*p)->set_resizing(false);
  3427. }
  3428. // Did anything change size?
  3429. unsigned int num_huge_branches = this->branch_lookup_table_.size();
  3430. bool again = num_huge_branches != prev_brlt_size;
  3431. if (size == 64 && num_huge_branches != 0)
  3432. this->make_brlt_section(layout);
  3433. if (size == 64 && again)
  3434. this->brlt_section_->set_current_size(num_huge_branches);
  3435. for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
  3436. p != this->stub_tables_.rend();
  3437. ++p)
  3438. (*p)->remove_eh_frame(layout);
  3439. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3440. p != this->stub_tables_.end();
  3441. ++p)
  3442. (*p)->add_eh_frame(layout);
  3443. typedef Unordered_set<Output_section*> Output_sections;
  3444. Output_sections os_need_update;
  3445. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3446. p != this->stub_tables_.end();
  3447. ++p)
  3448. {
  3449. if ((*p)->size_update())
  3450. {
  3451. again = true;
  3452. os_need_update.insert((*p)->output_section());
  3453. }
  3454. }
  3455. // Set output section offsets for all input sections in an output
  3456. // section that just changed size. Anything past the stubs will
  3457. // need updating.
  3458. for (typename Output_sections::iterator p = os_need_update.begin();
  3459. p != os_need_update.end();
  3460. p++)
  3461. {
  3462. Output_section* os = *p;
  3463. Address off = 0;
  3464. typedef Output_section::Input_section_list Input_section_list;
  3465. for (Input_section_list::const_iterator i = os->input_sections().begin();
  3466. i != os->input_sections().end();
  3467. ++i)
  3468. {
  3469. off = align_address(off, i->addralign());
  3470. if (i->is_input_section() || i->is_relaxed_input_section())
  3471. i->relobj()->set_section_offset(i->shndx(), off);
  3472. if (i->is_relaxed_input_section())
  3473. {
  3474. Stub_table<size, big_endian>* stub_table
  3475. = static_cast<Stub_table<size, big_endian>*>(
  3476. i->relaxed_input_section());
  3477. Address stub_table_size = stub_table->set_address_and_size(os, off);
  3478. off += stub_table_size;
  3479. // After a few iterations, set current stub table size
  3480. // as min size threshold, so later stub tables can only
  3481. // grow in size.
  3482. if (pass >= 4)
  3483. stub_table->set_min_size_threshold(stub_table_size);
  3484. }
  3485. else
  3486. off += i->data_size();
  3487. }
  3488. // If .branch_lt is part of this output section, then we have
  3489. // just done the offset adjustment.
  3490. os->clear_section_offsets_need_adjustment();
  3491. }
  3492. if (size == 64
  3493. && !again
  3494. && num_huge_branches != 0
  3495. && parameters->options().output_is_position_independent())
  3496. {
  3497. // Fill in the BRLT relocs.
  3498. this->brlt_section_->reset_brlt_sizes();
  3499. for (typename Branch_lookup_table::const_iterator p
  3500. = this->branch_lookup_table_.begin();
  3501. p != this->branch_lookup_table_.end();
  3502. ++p)
  3503. {
  3504. this->brlt_section_->add_reloc(p->first, p->second);
  3505. }
  3506. this->brlt_section_->finalize_brlt_sizes();
  3507. }
  3508. if (!again
  3509. && (parameters->options().user_set_emit_stub_syms()
  3510. ? parameters->options().emit_stub_syms()
  3511. : (size == 64
  3512. || parameters->options().output_is_position_independent()
  3513. || parameters->options().emit_relocs())))
  3514. {
  3515. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  3516. p != this->stub_tables_.end();
  3517. ++p)
  3518. (*p)->define_stub_syms(symtab);
  3519. if (this->glink_ != NULL)
  3520. {
  3521. int stub_size = this->glink_->pltresolve_size();
  3522. Address value = -stub_size;
  3523. if (size == 64)
  3524. {
  3525. value = 8;
  3526. stub_size -= 8;
  3527. }
  3528. this->define_local(symtab, "__glink_PLTresolve",
  3529. this->glink_, value, stub_size);
  3530. if (size != 64)
  3531. this->define_local(symtab, "__glink", this->glink_, 0, 0);
  3532. }
  3533. }
  3534. return again;
  3535. }
  3536. template<int size, bool big_endian>
  3537. void
  3538. Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
  3539. unsigned char* oview,
  3540. uint64_t* paddress,
  3541. off_t* plen) const
  3542. {
  3543. uint64_t address = plt->address();
  3544. off_t len = plt->data_size();
  3545. if (plt == this->glink_)
  3546. {
  3547. // See Output_data_glink::do_write() for glink contents.
  3548. if (len == 0)
  3549. {
  3550. // Static linking may need stubs, to support ifunc and long
  3551. // branches. We need to create an output section for
  3552. // .eh_frame early in the link process, to have a place to
  3553. // attach stub .eh_frame info. We also need to have
  3554. // registered a CIE that matches the stub CIE. Both of
  3555. // these requirements are satisfied by creating an FDE and
  3556. // CIE for .glink, even though static linking will leave
  3557. // .glink zero length.
  3558. // ??? Hopefully generating an FDE with a zero address range
  3559. // won't confuse anything that consumes .eh_frame info.
  3560. }
  3561. else if (size == 64)
  3562. {
  3563. // There is one word before __glink_PLTresolve
  3564. address += 8;
  3565. len -= 8;
  3566. }
  3567. else if (parameters->options().output_is_position_independent())
  3568. {
  3569. // There are two FDEs for a position independent glink.
  3570. // The first covers the branch table, the second
  3571. // __glink_PLTresolve at the end of glink.
  3572. off_t resolve_size = this->glink_->pltresolve_size();
  3573. if (oview[9] == elfcpp::DW_CFA_nop)
  3574. len -= resolve_size;
  3575. else
  3576. {
  3577. address += len - resolve_size;
  3578. len = resolve_size;
  3579. }
  3580. }
  3581. }
  3582. else
  3583. {
  3584. // Must be a stub table.
  3585. const Stub_table<size, big_endian>* stub_table
  3586. = static_cast<const Stub_table<size, big_endian>*>(plt);
  3587. uint64_t stub_address = stub_table->stub_address();
  3588. len -= stub_address - address;
  3589. address = stub_address;
  3590. }
  3591. *paddress = address;
  3592. *plen = len;
  3593. }
  3594. // A class to handle the PLT data.
  3595. template<int size, bool big_endian>
  3596. class Output_data_plt_powerpc : public Output_section_data_build
  3597. {
  3598. public:
  3599. typedef Output_data_reloc<elfcpp::SHT_RELA, true,
  3600. size, big_endian> Reloc_section;
  3601. Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
  3602. Symbol_table* symtab,
  3603. Reloc_section* plt_rel,
  3604. const char* name)
  3605. : Output_section_data_build(size == 32 ? 4 : 8),
  3606. rel_(plt_rel), targ_(targ), symtab_(symtab), name_(name), sym_ents_()
  3607. { }
  3608. // Add an entry to the PLT.
  3609. void
  3610. add_entry(Symbol*, bool = false);
  3611. void
  3612. add_ifunc_entry(Symbol*);
  3613. void
  3614. add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
  3615. void
  3616. add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
  3617. // Return the .rela.plt section data.
  3618. Reloc_section*
  3619. rel_plt() const
  3620. {
  3621. return this->rel_;
  3622. }
  3623. // Return the number of PLT entries.
  3624. unsigned int
  3625. entry_count() const
  3626. {
  3627. if (this->current_data_size() == 0)
  3628. return 0;
  3629. return ((this->current_data_size() - this->first_plt_entry_offset())
  3630. / this->plt_entry_size());
  3631. }
  3632. protected:
  3633. void
  3634. do_adjust_output_section(Output_section* os)
  3635. {
  3636. os->set_entsize(0);
  3637. }
  3638. // Write to a map file.
  3639. void
  3640. do_print_to_mapfile(Mapfile* mapfile) const
  3641. { mapfile->print_output_data(this, this->name_); }
  3642. private:
  3643. struct Local_plt_ent
  3644. {
  3645. Local_plt_ent(Sized_relobj_file<size, big_endian>* obj, unsigned int rsym)
  3646. { rsym_ = rsym; u.obj_ = obj; }
  3647. Local_plt_ent(Symbol* sym)
  3648. { rsym_ = -1u; u.gsym_ = sym; }
  3649. ~Local_plt_ent()
  3650. { }
  3651. unsigned int rsym_;
  3652. union
  3653. {
  3654. Sized_relobj_file<size, big_endian>* obj_;
  3655. Symbol* gsym_;
  3656. } u;
  3657. };
  3658. // Return the offset of the first non-reserved PLT entry.
  3659. unsigned int
  3660. first_plt_entry_offset() const
  3661. {
  3662. // IPLT and LPLT have no reserved entry.
  3663. if (this->name_[3] == 'I' || this->name_[3] == 'L')
  3664. return 0;
  3665. return this->targ_->first_plt_entry_offset();
  3666. }
  3667. // Return the size of each PLT entry.
  3668. unsigned int
  3669. plt_entry_size() const
  3670. {
  3671. return this->targ_->plt_entry_size();
  3672. }
  3673. // Write out the PLT data.
  3674. void
  3675. do_write(Output_file*);
  3676. // The reloc section.
  3677. Reloc_section* rel_;
  3678. // Allows access to .glink for do_write.
  3679. Target_powerpc<size, big_endian>* targ_;
  3680. Symbol_table* symtab_;
  3681. // What to report in map file.
  3682. const char *name_;
  3683. std::vector<Local_plt_ent> sym_ents_;
  3684. };
  3685. // Add an entry to the PLT.
  3686. template<int size, bool big_endian>
  3687. void
  3688. Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym,
  3689. bool stash)
  3690. {
  3691. if (!gsym->has_plt_offset())
  3692. {
  3693. section_size_type off = this->current_data_size();
  3694. if (off == 0)
  3695. off += this->first_plt_entry_offset();
  3696. gsym->set_plt_offset(off);
  3697. if (this->rel_)
  3698. {
  3699. gsym->set_needs_dynsym_entry();
  3700. unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
  3701. this->rel_->add_global(gsym, dynrel, this, off, 0);
  3702. }
  3703. off += this->plt_entry_size();
  3704. this->set_current_data_size(off);
  3705. if (stash)
  3706. {
  3707. Local_plt_ent sym(gsym);
  3708. this->sym_ents_.push_back(sym);
  3709. }
  3710. }
  3711. }
  3712. // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
  3713. template<int size, bool big_endian>
  3714. void
  3715. Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
  3716. {
  3717. if (!gsym->has_plt_offset())
  3718. {
  3719. section_size_type off = this->current_data_size();
  3720. gsym->set_plt_offset(off);
  3721. unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
  3722. if (size == 64 && this->targ_->abiversion() < 2)
  3723. dynrel = elfcpp::R_PPC64_JMP_IREL;
  3724. this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
  3725. off += this->plt_entry_size();
  3726. this->set_current_data_size(off);
  3727. }
  3728. }
  3729. // Add an entry for a local symbol to the PLT.
  3730. template<int size, bool big_endian>
  3731. void
  3732. Output_data_plt_powerpc<size, big_endian>::add_local_entry(
  3733. Sized_relobj_file<size, big_endian>* relobj,
  3734. unsigned int local_sym_index)
  3735. {
  3736. if (!relobj->local_has_plt_offset(local_sym_index))
  3737. {
  3738. section_size_type off = this->current_data_size();
  3739. relobj->set_local_plt_offset(local_sym_index, off);
  3740. if (this->rel_)
  3741. {
  3742. unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
  3743. if (size == 64 && this->targ_->abiversion() < 2)
  3744. dynrel = elfcpp::R_POWERPC_JMP_SLOT;
  3745. this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
  3746. dynrel, this, off, 0);
  3747. }
  3748. off += this->plt_entry_size();
  3749. this->set_current_data_size(off);
  3750. Local_plt_ent sym(relobj, local_sym_index);
  3751. this->sym_ents_.push_back(sym);
  3752. }
  3753. }
  3754. // Add an entry for a local ifunc symbol to the IPLT.
  3755. template<int size, bool big_endian>
  3756. void
  3757. Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
  3758. Sized_relobj_file<size, big_endian>* relobj,
  3759. unsigned int local_sym_index)
  3760. {
  3761. if (!relobj->local_has_plt_offset(local_sym_index))
  3762. {
  3763. section_size_type off = this->current_data_size();
  3764. relobj->set_local_plt_offset(local_sym_index, off);
  3765. unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
  3766. if (size == 64 && this->targ_->abiversion() < 2)
  3767. dynrel = elfcpp::R_PPC64_JMP_IREL;
  3768. this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
  3769. this, off, 0);
  3770. off += this->plt_entry_size();
  3771. this->set_current_data_size(off);
  3772. }
  3773. }
  3774. static const uint32_t add_0_11_11 = 0x7c0b5a14;
  3775. static const uint32_t add_2_2_11 = 0x7c425a14;
  3776. static const uint32_t add_2_2_12 = 0x7c426214;
  3777. static const uint32_t add_3_3_2 = 0x7c631214;
  3778. static const uint32_t add_3_3_13 = 0x7c636a14;
  3779. static const uint32_t add_3_12_2 = 0x7c6c1214;
  3780. static const uint32_t add_3_12_13 = 0x7c6c6a14;
  3781. static const uint32_t add_11_0_11 = 0x7d605a14;
  3782. static const uint32_t add_11_2_11 = 0x7d625a14;
  3783. static const uint32_t add_11_11_2 = 0x7d6b1214;
  3784. static const uint32_t add_12_11_12 = 0x7d8b6214;
  3785. static const uint32_t addi_0_12 = 0x380c0000;
  3786. static const uint32_t addi_2_2 = 0x38420000;
  3787. static const uint32_t addi_3_3 = 0x38630000;
  3788. static const uint32_t addi_11_11 = 0x396b0000;
  3789. static const uint32_t addi_12_1 = 0x39810000;
  3790. static const uint32_t addi_12_11 = 0x398b0000;
  3791. static const uint32_t addi_12_12 = 0x398c0000;
  3792. static const uint32_t addis_0_2 = 0x3c020000;
  3793. static const uint32_t addis_0_13 = 0x3c0d0000;
  3794. static const uint32_t addis_2_12 = 0x3c4c0000;
  3795. static const uint32_t addis_11_2 = 0x3d620000;
  3796. static const uint32_t addis_11_11 = 0x3d6b0000;
  3797. static const uint32_t addis_11_30 = 0x3d7e0000;
  3798. static const uint32_t addis_12_1 = 0x3d810000;
  3799. static const uint32_t addis_12_2 = 0x3d820000;
  3800. static const uint32_t addis_12_11 = 0x3d8b0000;
  3801. static const uint32_t addis_12_12 = 0x3d8c0000;
  3802. static const uint32_t b = 0x48000000;
  3803. static const uint32_t bcl_20_31 = 0x429f0005;
  3804. static const uint32_t bctr = 0x4e800420;
  3805. static const uint32_t bctrl = 0x4e800421;
  3806. static const uint32_t beqlr = 0x4d820020;
  3807. static const uint32_t blr = 0x4e800020;
  3808. static const uint32_t bnectr_p4 = 0x4ce20420;
  3809. static const uint32_t cmpld_7_12_0 = 0x7fac0040;
  3810. static const uint32_t cmpldi_2_0 = 0x28220000;
  3811. static const uint32_t cmpdi_11_0 = 0x2c2b0000;
  3812. static const uint32_t cmpwi_11_0 = 0x2c0b0000;
  3813. static const uint32_t cror_15_15_15 = 0x4def7b82;
  3814. static const uint32_t cror_31_31_31 = 0x4ffffb82;
  3815. static const uint32_t ld_0_1 = 0xe8010000;
  3816. static const uint32_t ld_0_11 = 0xe80b0000;
  3817. static const uint32_t ld_0_12 = 0xe80c0000;
  3818. static const uint32_t ld_2_1 = 0xe8410000;
  3819. static const uint32_t ld_2_2 = 0xe8420000;
  3820. static const uint32_t ld_2_11 = 0xe84b0000;
  3821. static const uint32_t ld_2_12 = 0xe84c0000;
  3822. static const uint32_t ld_11_1 = 0xe9610000;
  3823. static const uint32_t ld_11_2 = 0xe9620000;
  3824. static const uint32_t ld_11_3 = 0xe9630000;
  3825. static const uint32_t ld_11_11 = 0xe96b0000;
  3826. static const uint32_t ld_12_2 = 0xe9820000;
  3827. static const uint32_t ld_12_3 = 0xe9830000;
  3828. static const uint32_t ld_12_11 = 0xe98b0000;
  3829. static const uint32_t ld_12_12 = 0xe98c0000;
  3830. static const uint32_t ldx_12_11_12 = 0x7d8b602a;
  3831. static const uint32_t lfd_0_1 = 0xc8010000;
  3832. static const uint32_t li_0_0 = 0x38000000;
  3833. static const uint32_t li_11_0 = 0x39600000;
  3834. static const uint32_t li_12_0 = 0x39800000;
  3835. static const uint32_t lis_0 = 0x3c000000;
  3836. static const uint32_t lis_2 = 0x3c400000;
  3837. static const uint32_t lis_11 = 0x3d600000;
  3838. static const uint32_t lis_12 = 0x3d800000;
  3839. static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
  3840. static const uint32_t lwz_0_12 = 0x800c0000;
  3841. static const uint32_t lwz_11_3 = 0x81630000;
  3842. static const uint32_t lwz_11_11 = 0x816b0000;
  3843. static const uint32_t lwz_11_30 = 0x817e0000;
  3844. static const uint32_t lwz_12_3 = 0x81830000;
  3845. static const uint32_t lwz_12_12 = 0x818c0000;
  3846. static const uint32_t lwzu_0_12 = 0x840c0000;
  3847. static const uint32_t mflr_0 = 0x7c0802a6;
  3848. static const uint32_t mflr_11 = 0x7d6802a6;
  3849. static const uint32_t mflr_12 = 0x7d8802a6;
  3850. static const uint32_t mr_0_3 = 0x7c601b78;
  3851. static const uint32_t mr_3_0 = 0x7c030378;
  3852. static const uint32_t mtctr_0 = 0x7c0903a6;
  3853. static const uint32_t mtctr_11 = 0x7d6903a6;
  3854. static const uint32_t mtctr_12 = 0x7d8903a6;
  3855. static const uint32_t mtlr_0 = 0x7c0803a6;
  3856. static const uint32_t mtlr_11 = 0x7d6803a6;
  3857. static const uint32_t mtlr_12 = 0x7d8803a6;
  3858. static const uint32_t nop = 0x60000000;
  3859. static const uint32_t ori_0_0_0 = 0x60000000;
  3860. static const uint32_t ori_11_11_0 = 0x616b0000;
  3861. static const uint32_t ori_12_12_0 = 0x618c0000;
  3862. static const uint32_t oris_12_12_0 = 0x658c0000;
  3863. static const uint32_t sldi_11_11_34 = 0x796b1746;
  3864. static const uint32_t sldi_12_12_32 = 0x799c07c6;
  3865. static const uint32_t srdi_0_0_2 = 0x7800f082;
  3866. static const uint32_t std_0_1 = 0xf8010000;
  3867. static const uint32_t std_0_12 = 0xf80c0000;
  3868. static const uint32_t std_2_1 = 0xf8410000;
  3869. static const uint32_t std_11_1 = 0xf9610000;
  3870. static const uint32_t stfd_0_1 = 0xd8010000;
  3871. static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
  3872. static const uint32_t sub_11_11_12 = 0x7d6c5850;
  3873. static const uint32_t sub_12_12_11 = 0x7d8b6050;
  3874. static const uint32_t xor_2_12_12 = 0x7d826278;
  3875. static const uint32_t xor_11_12_12 = 0x7d8b6278;
  3876. static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
  3877. static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
  3878. static const uint64_t pnop = 0x0700000000000000ULL;
  3879. // Write out the PLT.
  3880. template<int size, bool big_endian>
  3881. void
  3882. Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
  3883. {
  3884. if (!this->sym_ents_.empty())
  3885. {
  3886. const section_size_type offset = this->offset();
  3887. const section_size_type oview_size
  3888. = convert_to_section_size_type(this->data_size());
  3889. unsigned char* const oview = of->get_output_view(offset, oview_size);
  3890. unsigned char* pov = oview;
  3891. unsigned char* endpov = oview + oview_size;
  3892. for (typename std::vector<Local_plt_ent>::iterator e
  3893. = this->sym_ents_.begin();
  3894. e != this->sym_ents_.end();
  3895. e++)
  3896. {
  3897. typename elfcpp::Elf_types<size>::Elf_Addr val;
  3898. Sized_symbol<size>* gsym = NULL;
  3899. Powerpc_relobj<size, big_endian>* obj = NULL;
  3900. if (e->rsym_ == -1u)
  3901. {
  3902. gsym = static_cast<Sized_symbol<size>*>(e->u.gsym_);
  3903. val = gsym->value();
  3904. }
  3905. else
  3906. {
  3907. obj = static_cast<Powerpc_relobj<size, big_endian>*>(e->u.obj_);
  3908. val = obj->local_symbol(e->rsym_)->value(obj, 0);
  3909. }
  3910. if (this->targ_->abiversion() >= 2)
  3911. {
  3912. elfcpp::Swap<size, big_endian>::writeval(pov, val);
  3913. pov += size / 8;
  3914. }
  3915. else
  3916. {
  3917. unsigned int shndx;
  3918. this->targ_->symval_for_branch(this->symtab_, gsym, obj,
  3919. &val, &shndx);
  3920. elfcpp::Swap<size, big_endian>::writeval(pov, val);
  3921. pov += size / 8;
  3922. val = this->targ_->toc_pointer();
  3923. elfcpp::Swap<size, big_endian>::writeval(pov, val);
  3924. pov += size / 8;
  3925. if (this->plt_entry_size() > 16)
  3926. {
  3927. elfcpp::Swap<size, big_endian>::writeval(pov, 0);
  3928. pov += size / 8;
  3929. }
  3930. }
  3931. }
  3932. gold_assert(pov == endpov);
  3933. }
  3934. if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
  3935. {
  3936. const section_size_type offset = this->offset();
  3937. const section_size_type oview_size
  3938. = convert_to_section_size_type(this->data_size());
  3939. unsigned char* const oview = of->get_output_view(offset, oview_size);
  3940. unsigned char* pov = oview;
  3941. unsigned char* endpov = oview + oview_size;
  3942. // The address of the .glink branch table
  3943. const Output_data_glink<size, big_endian>* glink
  3944. = this->targ_->glink_section();
  3945. elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
  3946. while (pov < endpov)
  3947. {
  3948. elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
  3949. pov += 4;
  3950. branch_tab += 4;
  3951. }
  3952. of->write_output_view(offset, oview_size, oview);
  3953. }
  3954. }
  3955. // Create the PLT section.
  3956. template<int size, bool big_endian>
  3957. void
  3958. Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
  3959. Layout* layout)
  3960. {
  3961. if (this->plt_ == NULL)
  3962. {
  3963. if (this->got_ == NULL)
  3964. this->got_section(symtab, layout, GOT_TYPE_SMALL);
  3965. if (this->glink_ == NULL)
  3966. make_glink_section(layout);
  3967. // Ensure that .rela.dyn always appears before .rela.plt This is
  3968. // necessary due to how, on PowerPC and some other targets, .rela.dyn
  3969. // needs to include .rela.plt in its range.
  3970. this->rela_dyn_section(layout);
  3971. Reloc_section* plt_rel = new Reloc_section(false);
  3972. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  3973. elfcpp::SHF_ALLOC, plt_rel,
  3974. ORDER_DYNAMIC_PLT_RELOCS, false);
  3975. this->plt_
  3976. = new Output_data_plt_powerpc<size, big_endian>(this, symtab, plt_rel,
  3977. "** PLT");
  3978. layout->add_output_section_data(".plt",
  3979. (size == 32
  3980. ? elfcpp::SHT_PROGBITS
  3981. : elfcpp::SHT_NOBITS),
  3982. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  3983. this->plt_,
  3984. (size == 32
  3985. ? ORDER_SMALL_DATA
  3986. : ORDER_SMALL_BSS),
  3987. false);
  3988. Output_section* rela_plt_os = plt_rel->output_section();
  3989. rela_plt_os->set_info_section(this->plt_->output_section());
  3990. }
  3991. }
  3992. // Create the IPLT section.
  3993. template<int size, bool big_endian>
  3994. void
  3995. Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
  3996. Layout* layout)
  3997. {
  3998. if (this->iplt_ == NULL)
  3999. {
  4000. this->make_plt_section(symtab, layout);
  4001. this->make_lplt_section(symtab, layout);
  4002. Reloc_section* iplt_rel = new Reloc_section(false);
  4003. if (this->rela_dyn_->output_section())
  4004. this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
  4005. this->iplt_
  4006. = new Output_data_plt_powerpc<size, big_endian>(this, symtab, iplt_rel,
  4007. "** IPLT");
  4008. if (this->plt_->output_section())
  4009. this->plt_->output_section()->add_output_section_data(this->iplt_);
  4010. }
  4011. }
  4012. // Create the LPLT section.
  4013. template<int size, bool big_endian>
  4014. void
  4015. Target_powerpc<size, big_endian>::make_lplt_section(Symbol_table* symtab,
  4016. Layout* layout)
  4017. {
  4018. if (this->lplt_ == NULL)
  4019. {
  4020. Reloc_section* lplt_rel = NULL;
  4021. if (parameters->options().output_is_position_independent())
  4022. {
  4023. lplt_rel = new Reloc_section(false);
  4024. this->rela_dyn_section(layout);
  4025. if (this->rela_dyn_->output_section())
  4026. this->rela_dyn_->output_section()
  4027. ->add_output_section_data(lplt_rel);
  4028. }
  4029. this->lplt_
  4030. = new Output_data_plt_powerpc<size, big_endian>(this, symtab, lplt_rel,
  4031. "** LPLT");
  4032. this->make_brlt_section(layout);
  4033. if (this->brlt_section_ && this->brlt_section_->output_section())
  4034. this->brlt_section_->output_section()
  4035. ->add_output_section_data(this->lplt_);
  4036. else
  4037. layout->add_output_section_data(".branch_lt",
  4038. elfcpp::SHT_PROGBITS,
  4039. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  4040. this->lplt_,
  4041. ORDER_RELRO,
  4042. true);
  4043. }
  4044. }
  4045. // A section for huge long branch addresses, similar to plt section.
  4046. template<int size, bool big_endian>
  4047. class Output_data_brlt_powerpc : public Output_section_data_build
  4048. {
  4049. public:
  4050. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  4051. typedef Output_data_reloc<elfcpp::SHT_RELA, true,
  4052. size, big_endian> Reloc_section;
  4053. Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
  4054. Reloc_section* brlt_rel)
  4055. : Output_section_data_build(size == 32 ? 4 : 8),
  4056. rel_(brlt_rel),
  4057. targ_(targ)
  4058. { }
  4059. void
  4060. reset_brlt_sizes()
  4061. {
  4062. this->reset_data_size();
  4063. this->rel_->reset_data_size();
  4064. }
  4065. void
  4066. finalize_brlt_sizes()
  4067. {
  4068. this->finalize_data_size();
  4069. this->rel_->finalize_data_size();
  4070. }
  4071. // Add a reloc for an entry in the BRLT.
  4072. void
  4073. add_reloc(Address to, unsigned int off)
  4074. { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
  4075. // Update section and reloc section size.
  4076. void
  4077. set_current_size(unsigned int num_branches)
  4078. {
  4079. this->reset_address_and_file_offset();
  4080. this->set_current_data_size(num_branches * 16);
  4081. this->finalize_data_size();
  4082. Output_section* os = this->output_section();
  4083. os->set_section_offsets_need_adjustment();
  4084. if (this->rel_ != NULL)
  4085. {
  4086. const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  4087. this->rel_->reset_address_and_file_offset();
  4088. this->rel_->set_current_data_size(num_branches * reloc_size);
  4089. this->rel_->finalize_data_size();
  4090. Output_section* os = this->rel_->output_section();
  4091. os->set_section_offsets_need_adjustment();
  4092. }
  4093. }
  4094. protected:
  4095. void
  4096. do_adjust_output_section(Output_section* os)
  4097. {
  4098. os->set_entsize(0);
  4099. }
  4100. // Write to a map file.
  4101. void
  4102. do_print_to_mapfile(Mapfile* mapfile) const
  4103. { mapfile->print_output_data(this, "** BRLT"); }
  4104. private:
  4105. // Write out the BRLT data.
  4106. void
  4107. do_write(Output_file*);
  4108. // The reloc section.
  4109. Reloc_section* rel_;
  4110. Target_powerpc<size, big_endian>* targ_;
  4111. };
  4112. // Make the branch lookup table section.
  4113. template<int size, bool big_endian>
  4114. void
  4115. Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
  4116. {
  4117. if (size == 64 && this->brlt_section_ == NULL)
  4118. {
  4119. Reloc_section* brlt_rel = NULL;
  4120. bool is_pic = parameters->options().output_is_position_independent();
  4121. if (is_pic)
  4122. {
  4123. // When PIC we can't fill in .branch_lt but must initialise at
  4124. // runtime via dynamic relocations.
  4125. this->rela_dyn_section(layout);
  4126. brlt_rel = new Reloc_section(false);
  4127. if (this->rela_dyn_->output_section())
  4128. this->rela_dyn_->output_section()
  4129. ->add_output_section_data(brlt_rel);
  4130. }
  4131. this->brlt_section_
  4132. = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
  4133. if (this->plt_ && is_pic && this->plt_->output_section())
  4134. this->plt_->output_section()
  4135. ->add_output_section_data(this->brlt_section_);
  4136. else
  4137. layout->add_output_section_data(".branch_lt",
  4138. elfcpp::SHT_PROGBITS,
  4139. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  4140. this->brlt_section_,
  4141. ORDER_RELRO,
  4142. true);
  4143. }
  4144. }
  4145. // Write out .branch_lt when non-PIC.
  4146. template<int size, bool big_endian>
  4147. void
  4148. Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
  4149. {
  4150. if (size == 64 && !parameters->options().output_is_position_independent())
  4151. {
  4152. const section_size_type offset = this->offset();
  4153. const section_size_type oview_size
  4154. = convert_to_section_size_type(this->data_size());
  4155. unsigned char* const oview = of->get_output_view(offset, oview_size);
  4156. this->targ_->write_branch_lookup_table(oview);
  4157. of->write_output_view(offset, oview_size, oview);
  4158. }
  4159. }
  4160. static inline uint32_t
  4161. l(uint32_t a)
  4162. {
  4163. return a & 0xffff;
  4164. }
  4165. static inline uint32_t
  4166. hi(uint32_t a)
  4167. {
  4168. return l(a >> 16);
  4169. }
  4170. static inline uint32_t
  4171. ha(uint32_t a)
  4172. {
  4173. return hi(a + 0x8000);
  4174. }
  4175. static inline uint64_t
  4176. d34(uint64_t v)
  4177. {
  4178. return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
  4179. }
  4180. static inline uint64_t
  4181. ha34(uint64_t v)
  4182. {
  4183. return (v + (1ULL << 33)) >> 34;
  4184. }
  4185. template<int size>
  4186. struct Eh_cie
  4187. {
  4188. static const unsigned char eh_frame_cie[12];
  4189. };
  4190. template<int size>
  4191. const unsigned char Eh_cie<size>::eh_frame_cie[] =
  4192. {
  4193. 1, // CIE version.
  4194. 'z', 'R', 0, // Augmentation string.
  4195. 4, // Code alignment.
  4196. 0x80 - size / 8 , // Data alignment.
  4197. 65, // RA reg.
  4198. 1, // Augmentation size.
  4199. (elfcpp::DW_EH_PE_pcrel
  4200. | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
  4201. elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
  4202. };
  4203. // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
  4204. static const unsigned char glink_eh_frame_fde_64v1[] =
  4205. {
  4206. 0, 0, 0, 0, // Replaced with offset to .glink.
  4207. 0, 0, 0, 0, // Replaced with size of .glink.
  4208. 0, // Augmentation size.
  4209. elfcpp::DW_CFA_advance_loc + 2,
  4210. elfcpp::DW_CFA_register, 65, 12,
  4211. elfcpp::DW_CFA_advance_loc + 4,
  4212. elfcpp::DW_CFA_restore_extended, 65
  4213. };
  4214. // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
  4215. static const unsigned char glink_eh_frame_fde_64v2[] =
  4216. {
  4217. 0, 0, 0, 0, // Replaced with offset to .glink.
  4218. 0, 0, 0, 0, // Replaced with size of .glink.
  4219. 0, // Augmentation size.
  4220. elfcpp::DW_CFA_advance_loc + 2,
  4221. elfcpp::DW_CFA_register, 65, 0,
  4222. elfcpp::DW_CFA_advance_loc + 2,
  4223. elfcpp::DW_CFA_restore_extended, 65
  4224. };
  4225. static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
  4226. {
  4227. 0, 0, 0, 0, // Replaced with offset to .glink.
  4228. 0, 0, 0, 0, // Replaced with size of .glink.
  4229. 0, // Augmentation size.
  4230. elfcpp::DW_CFA_advance_loc + 3,
  4231. elfcpp::DW_CFA_register, 65, 0,
  4232. elfcpp::DW_CFA_advance_loc + 2,
  4233. elfcpp::DW_CFA_restore_extended, 65
  4234. };
  4235. // Describe __glink_PLTresolve use of LR, 32-bit version.
  4236. static const unsigned char glink_eh_frame_fde_32[] =
  4237. {
  4238. 0, 0, 0, 0, // Replaced with offset to .glink.
  4239. 0, 0, 0, 0, // Replaced with size of .glink.
  4240. 0, // Augmentation size.
  4241. elfcpp::DW_CFA_advance_loc + 2,
  4242. elfcpp::DW_CFA_register, 65, 0,
  4243. elfcpp::DW_CFA_advance_loc + 4,
  4244. elfcpp::DW_CFA_restore_extended, 65
  4245. };
  4246. static const unsigned char default_fde[] =
  4247. {
  4248. 0, 0, 0, 0, // Replaced with offset to stubs.
  4249. 0, 0, 0, 0, // Replaced with size of stubs.
  4250. 0, // Augmentation size.
  4251. elfcpp::DW_CFA_nop, // Pad.
  4252. elfcpp::DW_CFA_nop,
  4253. elfcpp::DW_CFA_nop
  4254. };
  4255. template<bool big_endian>
  4256. static inline void
  4257. write_insn(unsigned char* p, uint32_t v)
  4258. {
  4259. elfcpp::Swap<32, big_endian>::writeval(p, v);
  4260. }
  4261. template<int size>
  4262. static inline unsigned int
  4263. param_plt_align()
  4264. {
  4265. if (!parameters->options().user_set_plt_align())
  4266. return size == 64 ? 32 : 8;
  4267. return 1 << parameters->options().plt_align();
  4268. }
  4269. // Stub_table holds information about plt and long branch stubs.
  4270. // Stubs are built in an area following some input section determined
  4271. // by group_sections(). This input section is converted to a relaxed
  4272. // input section allowing it to be resized to accommodate the stubs
  4273. template<int size, bool big_endian>
  4274. class Stub_table : public Output_relaxed_input_section
  4275. {
  4276. public:
  4277. struct Plt_stub_ent
  4278. {
  4279. Plt_stub_ent(unsigned int off, unsigned int indx)
  4280. : off_(off), indx_(indx), tocoff_(0), p9off_(0), tsize_ (0), iter_(0),
  4281. toc_(0), notoc_(0), p9notoc_(0), r2save_(0), localentry0_(0)
  4282. { }
  4283. unsigned int off_;
  4284. unsigned int indx_;
  4285. // off_ points at p10 notoc stub, tocoff_ is offset from there to
  4286. // toc stub, p9off_ is offset to p9notoc stub
  4287. unsigned int tocoff_ : 8;
  4288. unsigned int p9off_ : 8;
  4289. // The size of the toc stub, used to locate blr on tls_get_addr stub.
  4290. unsigned int tsize_ : 8;
  4291. // Stub revision management
  4292. unsigned int iter_ : 1;
  4293. // The three types of stubs.
  4294. unsigned int toc_ : 1;
  4295. unsigned int notoc_ : 1;
  4296. unsigned int p9notoc_ : 1;
  4297. // Each with a possible variant saving r2 first
  4298. unsigned int r2save_ : 1;
  4299. // Handy cached info from symbol
  4300. unsigned int localentry0_ : 1;
  4301. };
  4302. struct Branch_stub_ent
  4303. {
  4304. Branch_stub_ent(unsigned int off)
  4305. : off_(off), tocoff_(0), p9off_(0), iter_(0), toc_(0), notoc_(0),
  4306. p9notoc_(0), save_res_(0), other_(0)
  4307. { }
  4308. unsigned int off_;
  4309. // off_ points at p10 notoc stub, tocoff_ is offset from there to
  4310. // toc stub, p9off_ is offset to p9notoc stub
  4311. unsigned int tocoff_ : 8;
  4312. unsigned int p9off_ : 8;
  4313. // Stub revision management
  4314. unsigned int iter_ : 1;
  4315. // Four types of stubs.
  4316. unsigned int toc_ : 1;
  4317. unsigned int notoc_ : 1;
  4318. unsigned int p9notoc_ : 1;
  4319. unsigned int save_res_ : 1;
  4320. // Handy cached info from symbol
  4321. unsigned int other_ : 3;
  4322. };
  4323. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  4324. static const Address invalid_address = static_cast<Address>(0) - 1;
  4325. Stub_table(Target_powerpc<size, big_endian>* targ,
  4326. Output_section* output_section,
  4327. const Output_section::Input_section* owner,
  4328. uint32_t id)
  4329. : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
  4330. owner->relobj()
  4331. ->section_addralign(owner->shndx())),
  4332. targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
  4333. orig_data_size_(owner->current_data_size()),
  4334. plt_size_(0), last_plt_size_(0),
  4335. branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
  4336. need_save_res_(false), need_resize_(false), resizing_(false),
  4337. uniq_(id)
  4338. {
  4339. this->set_output_section(output_section);
  4340. std::vector<Output_relaxed_input_section*> new_relaxed;
  4341. new_relaxed.push_back(this);
  4342. output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
  4343. }
  4344. // Add a plt call stub.
  4345. bool
  4346. add_plt_call_entry(Address,
  4347. const Sized_relobj_file<size, big_endian>*,
  4348. const Symbol*,
  4349. unsigned int,
  4350. Address,
  4351. bool);
  4352. bool
  4353. add_plt_call_entry(Address,
  4354. const Sized_relobj_file<size, big_endian>*,
  4355. unsigned int,
  4356. unsigned int,
  4357. Address,
  4358. bool);
  4359. // Find a given plt call stub.
  4360. const Plt_stub_ent*
  4361. find_plt_call_entry(const Symbol*) const;
  4362. const Plt_stub_ent*
  4363. find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
  4364. unsigned int) const;
  4365. const Plt_stub_ent*
  4366. find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
  4367. const Symbol*,
  4368. unsigned int,
  4369. Address) const;
  4370. const Plt_stub_ent*
  4371. find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
  4372. unsigned int,
  4373. unsigned int,
  4374. Address) const;
  4375. // Add a long branch stub.
  4376. bool
  4377. add_long_branch_entry(unsigned int, Address, Address, unsigned int, bool);
  4378. const Branch_stub_ent*
  4379. find_long_branch_entry(Address) const;
  4380. bool
  4381. can_reach_stub(Address from, unsigned int off, unsigned int r_type)
  4382. {
  4383. Address max_branch_offset = max_branch_delta<size>(r_type);
  4384. if (max_branch_offset == 0)
  4385. return true;
  4386. gold_assert(from != invalid_address);
  4387. Address loc = off + this->stub_address();
  4388. return loc - from + max_branch_offset < 2 * max_branch_offset;
  4389. }
  4390. void
  4391. clear_stubs(bool all)
  4392. {
  4393. this->plt_call_stubs_.clear();
  4394. this->plt_size_ = 0;
  4395. this->long_branch_stubs_.clear();
  4396. this->branch_size_ = 0;
  4397. this->need_save_res_ = false;
  4398. if (all)
  4399. {
  4400. this->last_plt_size_ = 0;
  4401. this->last_branch_size_ = 0;
  4402. }
  4403. }
  4404. bool
  4405. need_resize() const
  4406. { return need_resize_; }
  4407. void
  4408. set_resizing(bool val)
  4409. {
  4410. this->resizing_ = val;
  4411. if (val)
  4412. {
  4413. this->need_resize_ = false;
  4414. this->plt_size_ = 0;
  4415. this->branch_size_ = 0;
  4416. this->need_save_res_ = false;
  4417. }
  4418. }
  4419. Address
  4420. set_address_and_size(const Output_section* os, Address off)
  4421. {
  4422. Address start_off = off;
  4423. off += this->orig_data_size_;
  4424. Address my_size = this->plt_size_ + this->branch_size_;
  4425. if (this->need_save_res_)
  4426. my_size += this->targ_->savres_section()->data_size();
  4427. if (my_size != 0)
  4428. off = align_address(off, this->stub_align());
  4429. // Include original section size and alignment padding in size
  4430. my_size += off - start_off;
  4431. // Ensure new size is always larger than min size
  4432. // threshold. Alignment requirement is included in "my_size", so
  4433. // increase "my_size" does not invalidate alignment.
  4434. if (my_size < this->min_size_threshold_)
  4435. my_size = this->min_size_threshold_;
  4436. this->reset_address_and_file_offset();
  4437. this->set_current_data_size(my_size);
  4438. this->set_address_and_file_offset(os->address() + start_off,
  4439. os->offset() + start_off);
  4440. return my_size;
  4441. }
  4442. Address
  4443. stub_address() const
  4444. {
  4445. return align_address(this->address() + this->orig_data_size_,
  4446. this->stub_align());
  4447. }
  4448. Address
  4449. stub_offset() const
  4450. {
  4451. return align_address(this->offset() + this->orig_data_size_,
  4452. this->stub_align());
  4453. }
  4454. section_size_type
  4455. plt_size() const
  4456. { return this->plt_size_; }
  4457. section_size_type
  4458. branch_size() const
  4459. { return this->branch_size_; }
  4460. void
  4461. set_min_size_threshold(Address min_size)
  4462. { this->min_size_threshold_ = min_size; }
  4463. void
  4464. define_stub_syms(Symbol_table*);
  4465. bool
  4466. size_update()
  4467. {
  4468. Output_section* os = this->output_section();
  4469. if (os->addralign() < this->stub_align())
  4470. {
  4471. os->set_addralign(this->stub_align());
  4472. // FIXME: get rid of the insane checkpointing.
  4473. // We can't increase alignment of the input section to which
  4474. // stubs are attached; The input section may be .init which
  4475. // is pasted together with other .init sections to form a
  4476. // function. Aligning might insert zero padding resulting in
  4477. // sigill. However we do need to increase alignment of the
  4478. // output section so that the align_address() on offset in
  4479. // set_address_and_size() adds the same padding as the
  4480. // align_address() on address in stub_address().
  4481. // What's more, we need this alignment for the layout done in
  4482. // relaxation_loop_body() so that the output section starts at
  4483. // a suitably aligned address.
  4484. os->checkpoint_set_addralign(this->stub_align());
  4485. }
  4486. if (this->last_plt_size_ != this->plt_size_
  4487. || this->last_branch_size_ != this->branch_size_)
  4488. {
  4489. this->last_plt_size_ = this->plt_size_;
  4490. this->last_branch_size_ = this->branch_size_;
  4491. return true;
  4492. }
  4493. return false;
  4494. }
  4495. // Add .eh_frame info for this stub section.
  4496. void
  4497. add_eh_frame(Layout* layout);
  4498. // Remove .eh_frame info for this stub section.
  4499. void
  4500. remove_eh_frame(Layout* layout);
  4501. Target_powerpc<size, big_endian>*
  4502. targ() const
  4503. { return targ_; }
  4504. private:
  4505. class Plt_stub_key;
  4506. class Plt_stub_key_hash;
  4507. typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
  4508. Plt_stub_key_hash> Plt_stub_entries;
  4509. class Branch_stub_key;
  4510. class Branch_stub_key_hash;
  4511. typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
  4512. Branch_stub_key_hash> Branch_stub_entries;
  4513. // Alignment of stub section.
  4514. unsigned int
  4515. stub_align() const
  4516. {
  4517. unsigned int min_align = size == 64 ? 32 : 16;
  4518. unsigned int user_align = 1 << parameters->options().plt_align();
  4519. return std::max(user_align, min_align);
  4520. }
  4521. // Return the plt offset for the given call stub.
  4522. Address
  4523. plt_off(typename Plt_stub_entries::const_iterator p,
  4524. const Output_data_plt_powerpc<size, big_endian>** sec) const
  4525. {
  4526. const Symbol* gsym = p->first.sym_;
  4527. if (gsym != NULL)
  4528. return this->targ_->plt_off(gsym, sec);
  4529. else
  4530. {
  4531. const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
  4532. unsigned int local_sym_index = p->first.locsym_;
  4533. return this->targ_->plt_off(relobj, local_sym_index, sec);
  4534. }
  4535. }
  4536. // Size of a given plt call stub.
  4537. unsigned int
  4538. plt_call_size(typename Plt_stub_entries::iterator p) const;
  4539. unsigned int
  4540. plt_call_align(unsigned int bytes) const
  4541. {
  4542. unsigned int align = param_plt_align<size>();
  4543. return (bytes + align - 1) & -align;
  4544. }
  4545. // Return long branch stub size.
  4546. unsigned int
  4547. branch_stub_size(typename Branch_stub_entries::iterator p,
  4548. bool* need_lt);
  4549. void
  4550. build_tls_opt_head(unsigned char** pp, bool save_lr);
  4551. void
  4552. build_tls_opt_tail(unsigned char* p);
  4553. void
  4554. plt_error(const Plt_stub_key& p);
  4555. // Write out stubs.
  4556. void
  4557. do_write(Output_file*);
  4558. // Plt call stub keys.
  4559. class Plt_stub_key
  4560. {
  4561. public:
  4562. Plt_stub_key(const Symbol* sym)
  4563. : sym_(sym), object_(0), addend_(0), locsym_(0)
  4564. { }
  4565. Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
  4566. unsigned int locsym_index)
  4567. : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
  4568. { }
  4569. Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
  4570. const Symbol* sym,
  4571. unsigned int r_type,
  4572. Address addend)
  4573. : sym_(sym), object_(0), addend_(0), locsym_(0)
  4574. {
  4575. if (size != 32)
  4576. this->addend_ = addend;
  4577. else if (parameters->options().output_is_position_independent()
  4578. && (r_type == elfcpp::R_PPC_PLTREL24
  4579. || r_type == elfcpp::R_POWERPC_PLTCALL))
  4580. {
  4581. this->addend_ = addend;
  4582. if (this->addend_ >= 32768)
  4583. this->object_ = object;
  4584. }
  4585. }
  4586. Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
  4587. unsigned int locsym_index,
  4588. unsigned int r_type,
  4589. Address addend)
  4590. : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
  4591. {
  4592. if (size != 32)
  4593. this->addend_ = addend;
  4594. else if (parameters->options().output_is_position_independent()
  4595. && (r_type == elfcpp::R_PPC_PLTREL24
  4596. || r_type == elfcpp::R_POWERPC_PLTCALL))
  4597. this->addend_ = addend;
  4598. }
  4599. bool operator==(const Plt_stub_key& that) const
  4600. {
  4601. return (this->sym_ == that.sym_
  4602. && this->object_ == that.object_
  4603. && this->addend_ == that.addend_
  4604. && this->locsym_ == that.locsym_);
  4605. }
  4606. const Symbol* sym_;
  4607. const Sized_relobj_file<size, big_endian>* object_;
  4608. typename elfcpp::Elf_types<size>::Elf_Addr addend_;
  4609. unsigned int locsym_;
  4610. };
  4611. class Plt_stub_key_hash
  4612. {
  4613. public:
  4614. size_t operator()(const Plt_stub_key& ent) const
  4615. {
  4616. return (reinterpret_cast<uintptr_t>(ent.sym_)
  4617. ^ reinterpret_cast<uintptr_t>(ent.object_)
  4618. ^ ent.addend_
  4619. ^ ent.locsym_);
  4620. }
  4621. };
  4622. // Long branch stub keys.
  4623. class Branch_stub_key
  4624. {
  4625. public:
  4626. Branch_stub_key(Address to)
  4627. : dest_(to)
  4628. { }
  4629. bool operator==(const Branch_stub_key& that) const
  4630. {
  4631. return this->dest_ == that.dest_;
  4632. }
  4633. Address dest_;
  4634. };
  4635. class Branch_stub_key_hash
  4636. {
  4637. public:
  4638. size_t operator()(const Branch_stub_key& key) const
  4639. { return key.dest_; }
  4640. };
  4641. // In a sane world this would be a global.
  4642. Target_powerpc<size, big_endian>* targ_;
  4643. // Map sym/object/addend to stub offset.
  4644. Plt_stub_entries plt_call_stubs_;
  4645. // Map destination address to stub offset.
  4646. Branch_stub_entries long_branch_stubs_;
  4647. // size of input section
  4648. section_size_type orig_data_size_;
  4649. // size of stubs
  4650. section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
  4651. // Some rare cases cause (PR/20529) fluctuation in stub table
  4652. // size, which leads to an endless relax loop. This is to be fixed
  4653. // by, after the first few iterations, allowing only increase of
  4654. // stub table size. This variable sets the minimal possible size of
  4655. // a stub table, it is zero for the first few iterations, then
  4656. // increases monotonically.
  4657. Address min_size_threshold_;
  4658. // Set if this stub group needs a copy of out-of-line register
  4659. // save/restore functions.
  4660. bool need_save_res_;
  4661. // Set when notoc_/r2save_ changes after sizing a stub
  4662. bool need_resize_;
  4663. // Set when resizing stubs
  4664. bool resizing_;
  4665. // Per stub table unique identifier.
  4666. uint32_t uniq_;
  4667. };
  4668. // Add a plt call stub, if we do not already have one for this
  4669. // sym/object/addend combo.
  4670. template<int size, bool big_endian>
  4671. bool
  4672. Stub_table<size, big_endian>::add_plt_call_entry(
  4673. Address from,
  4674. const Sized_relobj_file<size, big_endian>* object,
  4675. const Symbol* gsym,
  4676. unsigned int r_type,
  4677. Address addend,
  4678. bool tocsave)
  4679. {
  4680. Plt_stub_key key(object, gsym, r_type, addend);
  4681. Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
  4682. std::pair<typename Plt_stub_entries::iterator, bool> p
  4683. = this->plt_call_stubs_.insert(std::make_pair(key, ent));
  4684. if (size == 64)
  4685. {
  4686. if (p.second
  4687. && this->targ_->is_elfv2_localentry0(gsym))
  4688. {
  4689. p.first->second.localentry0_ = 1;
  4690. this->targ_->set_has_localentry0();
  4691. }
  4692. if (r_type == elfcpp::R_PPC64_REL24_NOTOC
  4693. || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
  4694. {
  4695. if (this->targ_->power10_stubs()
  4696. && (!this->targ_->power10_stubs_auto()
  4697. || r_type == elfcpp::R_PPC64_REL24_NOTOC))
  4698. {
  4699. if (!p.second && !p.first->second.notoc_)
  4700. this->need_resize_ = true;
  4701. p.first->second.notoc_ = 1;
  4702. }
  4703. else
  4704. {
  4705. if (!p.second && !p.first->second.p9notoc_)
  4706. this->need_resize_ = true;
  4707. p.first->second.p9notoc_ = 1;
  4708. }
  4709. }
  4710. else
  4711. {
  4712. if (!p.second && !p.first->second.toc_)
  4713. this->need_resize_ = true;
  4714. p.first->second.toc_ = 1;
  4715. if (!tocsave && !p.first->second.localentry0_)
  4716. {
  4717. if (!p.second && !p.first->second.r2save_)
  4718. this->need_resize_ = true;
  4719. p.first->second.r2save_ = 1;
  4720. }
  4721. }
  4722. }
  4723. if (p.second || (this->resizing_ && !p.first->second.iter_))
  4724. {
  4725. if (this->resizing_)
  4726. {
  4727. p.first->second.iter_ = 1;
  4728. p.first->second.off_ = this->plt_size_;
  4729. }
  4730. this->plt_size_ += this->plt_call_size(p.first);
  4731. if (this->targ_->is_tls_get_addr_opt(gsym))
  4732. this->targ_->set_has_tls_get_addr_opt();
  4733. }
  4734. return this->can_reach_stub(from, p.first->second.off_, r_type);
  4735. }
  4736. template<int size, bool big_endian>
  4737. bool
  4738. Stub_table<size, big_endian>::add_plt_call_entry(
  4739. Address from,
  4740. const Sized_relobj_file<size, big_endian>* object,
  4741. unsigned int locsym_index,
  4742. unsigned int r_type,
  4743. Address addend,
  4744. bool tocsave)
  4745. {
  4746. Plt_stub_key key(object, locsym_index, r_type, addend);
  4747. Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
  4748. std::pair<typename Plt_stub_entries::iterator, bool> p
  4749. = this->plt_call_stubs_.insert(std::make_pair(key, ent));
  4750. if (size == 64)
  4751. {
  4752. if (p.second
  4753. && this->targ_->is_elfv2_localentry0(object, locsym_index))
  4754. {
  4755. p.first->second.localentry0_ = 1;
  4756. this->targ_->set_has_localentry0();
  4757. }
  4758. if (r_type == elfcpp::R_PPC64_REL24_NOTOC
  4759. || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
  4760. {
  4761. if (this->targ_->power10_stubs()
  4762. && (!this->targ_->power10_stubs_auto()
  4763. || r_type == elfcpp::R_PPC64_REL24_NOTOC))
  4764. {
  4765. if (!p.second && !p.first->second.notoc_)
  4766. this->need_resize_ = true;
  4767. p.first->second.notoc_ = 1;
  4768. }
  4769. else
  4770. {
  4771. if (!p.second && !p.first->second.p9notoc_)
  4772. this->need_resize_ = true;
  4773. p.first->second.p9notoc_ = 1;
  4774. }
  4775. }
  4776. else
  4777. {
  4778. if (!p.second && !p.first->second.toc_)
  4779. this->need_resize_ = true;
  4780. p.first->second.toc_ = 1;
  4781. if (!tocsave && !p.first->second.localentry0_)
  4782. {
  4783. if (!p.second && !p.first->second.r2save_)
  4784. this->need_resize_ = true;
  4785. p.first->second.r2save_ = 1;
  4786. }
  4787. }
  4788. }
  4789. if (p.second || (this->resizing_ && !p.first->second.iter_))
  4790. {
  4791. if (this->resizing_)
  4792. {
  4793. p.first->second.iter_ = 1;
  4794. p.first->second.off_ = this->plt_size_;
  4795. }
  4796. this->plt_size_ += this->plt_call_size(p.first);
  4797. }
  4798. return this->can_reach_stub(from, p.first->second.off_, r_type);
  4799. }
  4800. // Find a plt call stub.
  4801. template<int size, bool big_endian>
  4802. const typename Stub_table<size, big_endian>::Plt_stub_ent*
  4803. Stub_table<size, big_endian>::find_plt_call_entry(
  4804. const Sized_relobj_file<size, big_endian>* object,
  4805. const Symbol* gsym,
  4806. unsigned int r_type,
  4807. Address addend) const
  4808. {
  4809. Plt_stub_key key(object, gsym, r_type, addend);
  4810. typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
  4811. if (p == this->plt_call_stubs_.end())
  4812. return NULL;
  4813. return &p->second;
  4814. }
  4815. template<int size, bool big_endian>
  4816. const typename Stub_table<size, big_endian>::Plt_stub_ent*
  4817. Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
  4818. {
  4819. Plt_stub_key key(gsym);
  4820. typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
  4821. if (p == this->plt_call_stubs_.end())
  4822. return NULL;
  4823. return &p->second;
  4824. }
  4825. template<int size, bool big_endian>
  4826. const typename Stub_table<size, big_endian>::Plt_stub_ent*
  4827. Stub_table<size, big_endian>::find_plt_call_entry(
  4828. const Sized_relobj_file<size, big_endian>* object,
  4829. unsigned int locsym_index,
  4830. unsigned int r_type,
  4831. Address addend) const
  4832. {
  4833. Plt_stub_key key(object, locsym_index, r_type, addend);
  4834. typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
  4835. if (p == this->plt_call_stubs_.end())
  4836. return NULL;
  4837. return &p->second;
  4838. }
  4839. template<int size, bool big_endian>
  4840. const typename Stub_table<size, big_endian>::Plt_stub_ent*
  4841. Stub_table<size, big_endian>::find_plt_call_entry(
  4842. const Sized_relobj_file<size, big_endian>* object,
  4843. unsigned int locsym_index) const
  4844. {
  4845. Plt_stub_key key(object, locsym_index);
  4846. typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
  4847. if (p == this->plt_call_stubs_.end())
  4848. return NULL;
  4849. return &p->second;
  4850. }
  4851. // Add a long branch stub if we don't already have one to given
  4852. // destination.
  4853. template<int size, bool big_endian>
  4854. bool
  4855. Stub_table<size, big_endian>::add_long_branch_entry(
  4856. unsigned int r_type,
  4857. Address from,
  4858. Address to,
  4859. unsigned int other,
  4860. bool save_res)
  4861. {
  4862. Branch_stub_key key(to);
  4863. Branch_stub_ent ent(this->branch_size_);
  4864. std::pair<typename Branch_stub_entries::iterator, bool> p
  4865. = this->long_branch_stubs_.insert(std::make_pair(key, ent));
  4866. if (save_res)
  4867. {
  4868. if (!p.second && !p.first->second.save_res_)
  4869. this->need_resize_ = true;
  4870. p.first->second.save_res_ = true;
  4871. }
  4872. else if (size == 64
  4873. && (r_type == elfcpp::R_PPC64_REL24_NOTOC
  4874. || r_type == elfcpp::R_PPC64_REL24_P9NOTOC))
  4875. {
  4876. if (this->targ_->power10_stubs()
  4877. && (!this->targ_->power10_stubs_auto()
  4878. || r_type == elfcpp::R_PPC64_REL24_NOTOC))
  4879. {
  4880. if (!p.second && !p.first->second.notoc_)
  4881. this->need_resize_ = true;
  4882. p.first->second.notoc_ = true;
  4883. }
  4884. else
  4885. {
  4886. if (!p.second && !p.first->second.p9notoc_)
  4887. this->need_resize_ = true;
  4888. p.first->second.p9notoc_ = true;
  4889. }
  4890. }
  4891. else
  4892. {
  4893. if (!p.second && !p.first->second.toc_)
  4894. this->need_resize_ = true;
  4895. p.first->second.toc_ = true;
  4896. }
  4897. if (size == 64 && p.first->second.other_ == 0)
  4898. p.first->second.other_ = other;
  4899. if (p.second || (this->resizing_ && !p.first->second.iter_))
  4900. {
  4901. if (this->resizing_)
  4902. {
  4903. p.first->second.iter_ = 1;
  4904. p.first->second.off_ = this->branch_size_;
  4905. }
  4906. if (save_res)
  4907. this->need_save_res_ = true;
  4908. else
  4909. {
  4910. bool need_lt = false;
  4911. unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
  4912. this->branch_size_ += stub_size;
  4913. if (size == 64 && need_lt)
  4914. this->targ_->add_branch_lookup_table(to);
  4915. }
  4916. }
  4917. return this->can_reach_stub(from, p.first->second.off_, r_type);
  4918. }
  4919. // Find long branch stub offset.
  4920. template<int size, bool big_endian>
  4921. const typename Stub_table<size, big_endian>::Branch_stub_ent*
  4922. Stub_table<size, big_endian>::find_long_branch_entry(Address to) const
  4923. {
  4924. Branch_stub_key key(to);
  4925. typename Branch_stub_entries::const_iterator p
  4926. = this->long_branch_stubs_.find(key);
  4927. if (p == this->long_branch_stubs_.end())
  4928. return NULL;
  4929. return &p->second;
  4930. }
  4931. template<bool big_endian>
  4932. static void
  4933. eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
  4934. {
  4935. delta /= 4;
  4936. if (delta < 64)
  4937. fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
  4938. else if (delta < 256)
  4939. {
  4940. fde.push_back(elfcpp::DW_CFA_advance_loc1);
  4941. fde.push_back(delta);
  4942. }
  4943. else if (delta < 65536)
  4944. {
  4945. fde.resize(fde.size() + 3);
  4946. unsigned char *p = &*fde.end() - 3;
  4947. *p++ = elfcpp::DW_CFA_advance_loc2;
  4948. elfcpp::Swap<16, big_endian>::writeval(p, delta);
  4949. }
  4950. else
  4951. {
  4952. fde.resize(fde.size() + 5);
  4953. unsigned char *p = &*fde.end() - 5;
  4954. *p++ = elfcpp::DW_CFA_advance_loc4;
  4955. elfcpp::Swap<32, big_endian>::writeval(p, delta);
  4956. }
  4957. }
  4958. template<typename T>
  4959. static bool
  4960. stub_sort(T s1, T s2)
  4961. {
  4962. return s1->second.off_ < s2->second.off_;
  4963. }
  4964. // Add .eh_frame info for this stub section. Unlike other linker
  4965. // generated .eh_frame this is added late in the link, because we
  4966. // only want the .eh_frame info if this particular stub section is
  4967. // non-empty.
  4968. template<int size, bool big_endian>
  4969. void
  4970. Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
  4971. {
  4972. if (size != 64
  4973. || !parameters->options().ld_generated_unwind_info())
  4974. return;
  4975. // Since we add stub .eh_frame info late, it must be placed
  4976. // after all other linker generated .eh_frame info so that
  4977. // merge mapping need not be updated for input sections.
  4978. // There is no provision to use a different CIE to that used
  4979. // by .glink.
  4980. if (!this->targ_->has_glink())
  4981. return;
  4982. typedef typename Plt_stub_entries::iterator plt_iter;
  4983. std::vector<plt_iter> calls;
  4984. if (!this->plt_call_stubs_.empty())
  4985. for (plt_iter cs = this->plt_call_stubs_.begin();
  4986. cs != this->plt_call_stubs_.end();
  4987. ++cs)
  4988. if (cs->second.p9notoc_
  4989. || (cs->second.toc_
  4990. && cs->second.r2save_
  4991. && !cs->second.localentry0_
  4992. && this->targ_->is_tls_get_addr_opt(cs->first.sym_)))
  4993. calls.push_back(cs);
  4994. if (calls.size() > 1)
  4995. std::stable_sort(calls.begin(), calls.end(),
  4996. stub_sort<plt_iter>);
  4997. typedef typename Branch_stub_entries::const_iterator branch_iter;
  4998. std::vector<branch_iter> branches;
  4999. if (!this->long_branch_stubs_.empty()
  5000. && !this->targ_->power10_stubs())
  5001. for (branch_iter bs = this->long_branch_stubs_.begin();
  5002. bs != this->long_branch_stubs_.end();
  5003. ++bs)
  5004. if (bs->second.notoc_)
  5005. branches.push_back(bs);
  5006. if (branches.size() > 1)
  5007. std::stable_sort(branches.begin(), branches.end(),
  5008. stub_sort<branch_iter>);
  5009. if (calls.empty() && branches.empty())
  5010. return;
  5011. unsigned int last_eh_loc = 0;
  5012. // offset pcrel sdata4, size udata4, and augmentation size byte.
  5013. std::vector<unsigned char> fde(9, 0);
  5014. for (unsigned int i = 0; i < calls.size(); i++)
  5015. {
  5016. plt_iter cs = calls[i];
  5017. unsigned int off = cs->second.off_;
  5018. // The __tls_get_addr_opt call stub needs to describe where
  5019. // it saves LR, to support exceptions that might be thrown
  5020. // from __tls_get_addr, and to support asynchronous exceptions.
  5021. if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  5022. {
  5023. off += 7 * 4;
  5024. if (cs->second.toc_
  5025. && cs->second.r2save_
  5026. && !cs->second.localentry0_)
  5027. {
  5028. off += cs->second.tocoff_ + 2 * 4;
  5029. eh_advance<big_endian>(fde, off - last_eh_loc);
  5030. fde.resize(fde.size() + 6);
  5031. unsigned char* p = &*fde.end() - 6;
  5032. *p++ = elfcpp::DW_CFA_offset_extended_sf;
  5033. *p++ = 65;
  5034. *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
  5035. unsigned int delta = cs->second.tsize_ - 9 * 4 - 4;
  5036. *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
  5037. *p++ = elfcpp::DW_CFA_restore_extended;
  5038. *p++ = 65;
  5039. last_eh_loc = off + delta;
  5040. off = cs->second.off_ + 7 * 4;
  5041. }
  5042. }
  5043. // notoc stubs also should describe LR changes, to support
  5044. // asynchronous exceptions.
  5045. if (cs->second.p9notoc_)
  5046. {
  5047. off += cs->second.p9off_;
  5048. off += (cs->second.r2save_ ? 4 : 0) + 8;
  5049. eh_advance<big_endian>(fde, off - last_eh_loc);
  5050. fde.resize(fde.size() + 6);
  5051. unsigned char* p = &*fde.end() - 6;
  5052. *p++ = elfcpp::DW_CFA_register;
  5053. *p++ = 65;
  5054. *p++ = 12;
  5055. *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
  5056. *p++ = elfcpp::DW_CFA_restore_extended;
  5057. *p++ = 65;
  5058. last_eh_loc = off + 8;
  5059. }
  5060. }
  5061. for (unsigned int i = 0; i < branches.size(); i++)
  5062. {
  5063. branch_iter bs = branches[i];
  5064. unsigned int off = bs->second.off_ + 8;
  5065. eh_advance<big_endian>(fde, off - last_eh_loc);
  5066. fde.resize(fde.size() + 6);
  5067. unsigned char* p = &*fde.end() - 6;
  5068. *p++ = elfcpp::DW_CFA_register;
  5069. *p++ = 65;
  5070. *p++ = 12;
  5071. *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
  5072. *p++ = elfcpp::DW_CFA_restore_extended;
  5073. *p++ = 65;
  5074. last_eh_loc = off + 8;
  5075. }
  5076. layout->add_eh_frame_for_plt(this,
  5077. Eh_cie<size>::eh_frame_cie,
  5078. sizeof (Eh_cie<size>::eh_frame_cie),
  5079. &*fde.begin(), fde.size());
  5080. }
  5081. template<int size, bool big_endian>
  5082. void
  5083. Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
  5084. {
  5085. if (size == 64
  5086. && parameters->options().ld_generated_unwind_info()
  5087. && this->targ_->has_glink())
  5088. layout->remove_eh_frame_for_plt(this,
  5089. Eh_cie<size>::eh_frame_cie,
  5090. sizeof (Eh_cie<size>::eh_frame_cie));
  5091. }
  5092. // A class to handle .glink.
  5093. template<int size, bool big_endian>
  5094. class Output_data_glink : public Output_section_data
  5095. {
  5096. public:
  5097. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  5098. static const Address invalid_address = static_cast<Address>(0) - 1;
  5099. Output_data_glink(Target_powerpc<size, big_endian>* targ)
  5100. : Output_section_data(16), targ_(targ), global_entry_stubs_(),
  5101. end_branch_table_(), ge_size_(0)
  5102. { }
  5103. void
  5104. add_eh_frame(Layout* layout);
  5105. void
  5106. add_global_entry(const Symbol*);
  5107. Address
  5108. find_global_entry(const Symbol*) const;
  5109. unsigned int
  5110. global_entry_align(unsigned int off) const
  5111. {
  5112. unsigned int align = param_plt_align<size>();
  5113. return (off + align - 1) & -align;
  5114. }
  5115. unsigned int
  5116. global_entry_off() const
  5117. {
  5118. return this->global_entry_align(this->end_branch_table_);
  5119. }
  5120. Address
  5121. global_entry_address() const
  5122. {
  5123. gold_assert(this->is_data_size_valid());
  5124. return this->address() + this->global_entry_off();
  5125. }
  5126. int
  5127. pltresolve_size() const
  5128. {
  5129. if (size == 64)
  5130. return (8
  5131. + (this->targ_->abiversion() < 2 ? 11 * 4
  5132. : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
  5133. return 16 * 4;
  5134. }
  5135. protected:
  5136. // Write to a map file.
  5137. void
  5138. do_print_to_mapfile(Mapfile* mapfile) const
  5139. { mapfile->print_output_data(this, _("** glink")); }
  5140. private:
  5141. void
  5142. set_final_data_size();
  5143. // Write out .glink
  5144. void
  5145. do_write(Output_file*);
  5146. // Allows access to .got and .plt for do_write.
  5147. Target_powerpc<size, big_endian>* targ_;
  5148. // Map sym to stub offset.
  5149. typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
  5150. Global_entry_stub_entries global_entry_stubs_;
  5151. unsigned int end_branch_table_, ge_size_;
  5152. };
  5153. template<int size, bool big_endian>
  5154. void
  5155. Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
  5156. {
  5157. if (!parameters->options().ld_generated_unwind_info())
  5158. return;
  5159. if (size == 64)
  5160. {
  5161. if (this->targ_->abiversion() < 2)
  5162. layout->add_eh_frame_for_plt(this,
  5163. Eh_cie<64>::eh_frame_cie,
  5164. sizeof (Eh_cie<64>::eh_frame_cie),
  5165. glink_eh_frame_fde_64v1,
  5166. sizeof (glink_eh_frame_fde_64v1));
  5167. else if (this->targ_->has_localentry0())
  5168. layout->add_eh_frame_for_plt(this,
  5169. Eh_cie<64>::eh_frame_cie,
  5170. sizeof (Eh_cie<64>::eh_frame_cie),
  5171. glink_eh_frame_fde_64v2_localentry0,
  5172. sizeof (glink_eh_frame_fde_64v2));
  5173. else
  5174. layout->add_eh_frame_for_plt(this,
  5175. Eh_cie<64>::eh_frame_cie,
  5176. sizeof (Eh_cie<64>::eh_frame_cie),
  5177. glink_eh_frame_fde_64v2,
  5178. sizeof (glink_eh_frame_fde_64v2));
  5179. }
  5180. else
  5181. {
  5182. // 32-bit .glink can use the default since the CIE return
  5183. // address reg, LR, is valid.
  5184. layout->add_eh_frame_for_plt(this,
  5185. Eh_cie<32>::eh_frame_cie,
  5186. sizeof (Eh_cie<32>::eh_frame_cie),
  5187. default_fde,
  5188. sizeof (default_fde));
  5189. // Except where LR is used in a PIC __glink_PLTresolve.
  5190. if (parameters->options().output_is_position_independent())
  5191. layout->add_eh_frame_for_plt(this,
  5192. Eh_cie<32>::eh_frame_cie,
  5193. sizeof (Eh_cie<32>::eh_frame_cie),
  5194. glink_eh_frame_fde_32,
  5195. sizeof (glink_eh_frame_fde_32));
  5196. }
  5197. }
  5198. template<int size, bool big_endian>
  5199. void
  5200. Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
  5201. {
  5202. unsigned int off = this->global_entry_align(this->ge_size_);
  5203. std::pair<typename Global_entry_stub_entries::iterator, bool> p
  5204. = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
  5205. if (p.second)
  5206. this->ge_size_ = off + 16;
  5207. }
  5208. template<int size, bool big_endian>
  5209. typename Output_data_glink<size, big_endian>::Address
  5210. Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
  5211. {
  5212. typename Global_entry_stub_entries::const_iterator p
  5213. = this->global_entry_stubs_.find(gsym);
  5214. return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
  5215. }
  5216. template<int size, bool big_endian>
  5217. void
  5218. Output_data_glink<size, big_endian>::set_final_data_size()
  5219. {
  5220. unsigned int count = this->targ_->plt_entry_count();
  5221. section_size_type total = 0;
  5222. if (count != 0)
  5223. {
  5224. if (size == 32)
  5225. {
  5226. // space for branch table
  5227. total += 4 * (count - 1);
  5228. total += -total & 15;
  5229. total += this->pltresolve_size();
  5230. }
  5231. else
  5232. {
  5233. total += this->pltresolve_size();
  5234. // space for branch table
  5235. total += 4 * count;
  5236. if (this->targ_->abiversion() < 2)
  5237. {
  5238. total += 4 * count;
  5239. if (count > 0x8000)
  5240. total += 4 * (count - 0x8000);
  5241. }
  5242. }
  5243. }
  5244. this->end_branch_table_ = total;
  5245. total = this->global_entry_align(total);
  5246. total += this->ge_size_;
  5247. this->set_data_size(total);
  5248. }
  5249. // Define symbols on stubs, identifying the stub.
  5250. template<int size, bool big_endian>
  5251. void
  5252. Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
  5253. {
  5254. if (!this->plt_call_stubs_.empty())
  5255. {
  5256. // The key for the plt call stub hash table includes addresses,
  5257. // therefore traversal order depends on those addresses, which
  5258. // can change between runs if gold is a PIE. Unfortunately the
  5259. // output .symtab ordering depends on the order in which symbols
  5260. // are added to the linker symtab. We want reproducible output
  5261. // so must sort the call stub symbols.
  5262. typedef typename Plt_stub_entries::iterator plt_iter;
  5263. std::vector<plt_iter> sorted;
  5264. sorted.resize(this->plt_call_stubs_.size());
  5265. for (plt_iter cs = this->plt_call_stubs_.begin();
  5266. cs != this->plt_call_stubs_.end();
  5267. ++cs)
  5268. sorted[cs->second.indx_] = cs;
  5269. for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
  5270. {
  5271. plt_iter cs = sorted[i];
  5272. char add[10];
  5273. add[0] = 0;
  5274. if (cs->first.addend_ != 0)
  5275. sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
  5276. char obj[10];
  5277. obj[0] = 0;
  5278. if (cs->first.object_)
  5279. {
  5280. const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  5281. <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
  5282. sprintf(obj, "%x:", ppcobj->uniq());
  5283. }
  5284. char localname[9];
  5285. const char *symname;
  5286. if (cs->first.sym_ == NULL)
  5287. {
  5288. sprintf(localname, "%x", cs->first.locsym_);
  5289. symname = localname;
  5290. }
  5291. else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  5292. symname = this->targ_->tls_get_addr_opt()->name();
  5293. else
  5294. symname = cs->first.sym_->name();
  5295. char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
  5296. sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
  5297. Address value
  5298. = this->stub_address() - this->address() + cs->second.off_;
  5299. unsigned int stub_size = this->plt_call_size(cs);
  5300. this->targ_->define_local(symtab, name, this, value, stub_size);
  5301. }
  5302. }
  5303. typedef typename Branch_stub_entries::iterator branch_iter;
  5304. for (branch_iter bs = this->long_branch_stubs_.begin();
  5305. bs != this->long_branch_stubs_.end();
  5306. ++bs)
  5307. {
  5308. if (bs->second.save_res_)
  5309. continue;
  5310. char* name = new char[8 + 13 + 16 + 1];
  5311. sprintf(name, "%08x.long_branch.%llx", this->uniq_,
  5312. static_cast<unsigned long long>(bs->first.dest_));
  5313. Address value = (this->stub_address() - this->address()
  5314. + this->plt_size_ + bs->second.off_);
  5315. bool need_lt = false;
  5316. unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
  5317. this->targ_->define_local(symtab, name, this, value, stub_size);
  5318. }
  5319. }
  5320. // Emit the start of a __tls_get_addr_opt plt call stub.
  5321. template<int size, bool big_endian>
  5322. void
  5323. Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
  5324. bool save_lr)
  5325. {
  5326. unsigned char* p = *pp;
  5327. if (size == 64)
  5328. {
  5329. write_insn<big_endian>(p, ld_11_3 + 0);
  5330. p += 4;
  5331. write_insn<big_endian>(p, ld_12_3 + 8);
  5332. p += 4;
  5333. write_insn<big_endian>(p, mr_0_3);
  5334. p += 4;
  5335. write_insn<big_endian>(p, cmpdi_11_0);
  5336. p += 4;
  5337. write_insn<big_endian>(p, add_3_12_13);
  5338. p += 4;
  5339. write_insn<big_endian>(p, beqlr);
  5340. p += 4;
  5341. write_insn<big_endian>(p, mr_3_0);
  5342. p += 4;
  5343. if (save_lr)
  5344. {
  5345. write_insn<big_endian>(p, mflr_11);
  5346. p += 4;
  5347. write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
  5348. p += 4;
  5349. }
  5350. }
  5351. else
  5352. {
  5353. write_insn<big_endian>(p, lwz_11_3 + 0);
  5354. p += 4;
  5355. write_insn<big_endian>(p, lwz_12_3 + 4);
  5356. p += 4;
  5357. write_insn<big_endian>(p, mr_0_3);
  5358. p += 4;
  5359. write_insn<big_endian>(p, cmpwi_11_0);
  5360. p += 4;
  5361. write_insn<big_endian>(p, add_3_12_2);
  5362. p += 4;
  5363. write_insn<big_endian>(p, beqlr);
  5364. p += 4;
  5365. write_insn<big_endian>(p, mr_3_0);
  5366. p += 4;
  5367. write_insn<big_endian>(p, nop);
  5368. p += 4;
  5369. }
  5370. *pp = p;
  5371. }
  5372. // Emit the tail of a __tls_get_addr_opt plt call stub.
  5373. template<int size, bool big_endian>
  5374. void
  5375. Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
  5376. {
  5377. write_insn<big_endian>(p, bctrl);
  5378. p += 4;
  5379. write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
  5380. p += 4;
  5381. write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
  5382. p += 4;
  5383. write_insn<big_endian>(p, mtlr_11);
  5384. p += 4;
  5385. write_insn<big_endian>(p, blr);
  5386. }
  5387. // Emit pc-relative plt call stub code.
  5388. template<bool big_endian>
  5389. static unsigned char*
  5390. build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
  5391. {
  5392. uint64_t insn;
  5393. if (off - odd + (1ULL << 33) < 1ULL << 34)
  5394. {
  5395. off -= odd;
  5396. if (odd)
  5397. {
  5398. write_insn<big_endian>(p, nop);
  5399. p += 4;
  5400. }
  5401. if (load)
  5402. insn = pld_12_pc;
  5403. else
  5404. insn = paddi_12_pc;
  5405. insn |= d34(off);
  5406. write_insn<big_endian>(p, insn >> 32);
  5407. p += 4;
  5408. write_insn<big_endian>(p, insn & 0xffffffff);
  5409. }
  5410. else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
  5411. {
  5412. off -= 8 - odd;
  5413. write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
  5414. p += 4;
  5415. if (!odd)
  5416. {
  5417. write_insn<big_endian>(p, sldi_11_11_34);
  5418. p += 4;
  5419. }
  5420. insn = paddi_12_pc | d34(off);
  5421. write_insn<big_endian>(p, insn >> 32);
  5422. p += 4;
  5423. write_insn<big_endian>(p, insn & 0xffffffff);
  5424. p += 4;
  5425. if (odd)
  5426. {
  5427. write_insn<big_endian>(p, sldi_11_11_34);
  5428. p += 4;
  5429. }
  5430. if (load)
  5431. write_insn<big_endian>(p, ldx_12_11_12);
  5432. else
  5433. write_insn<big_endian>(p, add_12_11_12);
  5434. }
  5435. else
  5436. {
  5437. off -= odd + 8;
  5438. write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
  5439. p += 4;
  5440. write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
  5441. p += 4;
  5442. if (odd)
  5443. {
  5444. write_insn<big_endian>(p, sldi_11_11_34);
  5445. p += 4;
  5446. }
  5447. insn = paddi_12_pc | d34(off);
  5448. write_insn<big_endian>(p, insn >> 32);
  5449. p += 4;
  5450. write_insn<big_endian>(p, insn & 0xffffffff);
  5451. p += 4;
  5452. if (!odd)
  5453. {
  5454. write_insn<big_endian>(p, sldi_11_11_34);
  5455. p += 4;
  5456. }
  5457. if (load)
  5458. write_insn<big_endian>(p, ldx_12_11_12);
  5459. else
  5460. write_insn<big_endian>(p, add_12_11_12);
  5461. }
  5462. p += 4;
  5463. return p;
  5464. }
  5465. // Gets the address of a label (1:) in r11 and builds an offset in r12,
  5466. // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
  5467. // mflr %r12
  5468. // bcl 20,31,1f
  5469. // 1: mflr %r11
  5470. // mtlr %r12
  5471. // lis %r12,xxx-1b@highest
  5472. // ori %r12,%r12,xxx-1b@higher
  5473. // sldi %r12,%r12,32
  5474. // oris %r12,%r12,xxx-1b@high
  5475. // ori %r12,%r12,xxx-1b@l
  5476. // add/ldx %r12,%r11,%r12
  5477. template<bool big_endian>
  5478. static unsigned char*
  5479. build_notoc_offset(unsigned char* p, uint64_t off, bool load)
  5480. {
  5481. write_insn<big_endian>(p, mflr_12);
  5482. p += 4;
  5483. write_insn<big_endian>(p, bcl_20_31);
  5484. p += 4;
  5485. write_insn<big_endian>(p, mflr_11);
  5486. p += 4;
  5487. write_insn<big_endian>(p, mtlr_12);
  5488. p += 4;
  5489. if (off + 0x8000 < 0x10000)
  5490. {
  5491. if (load)
  5492. write_insn<big_endian>(p, ld_12_11 + l(off));
  5493. else
  5494. write_insn<big_endian>(p, addi_12_11 + l(off));
  5495. }
  5496. else if (off + 0x80008000ULL < 0x100000000ULL)
  5497. {
  5498. write_insn<big_endian>(p, addis_12_11 + ha(off));
  5499. p += 4;
  5500. if (load)
  5501. write_insn<big_endian>(p, ld_12_12 + l(off));
  5502. else
  5503. write_insn<big_endian>(p, addi_12_12 + l(off));
  5504. }
  5505. else
  5506. {
  5507. if (off + 0x800000000000ULL < 0x1000000000000ULL)
  5508. {
  5509. write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
  5510. p += 4;
  5511. }
  5512. else
  5513. {
  5514. write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
  5515. p += 4;
  5516. if (((off >> 32) & 0xffff) != 0)
  5517. {
  5518. write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
  5519. p += 4;
  5520. }
  5521. }
  5522. if (((off >> 32) & 0xffffffffULL) != 0)
  5523. {
  5524. write_insn<big_endian>(p, sldi_12_12_32);
  5525. p += 4;
  5526. }
  5527. if (hi(off) != 0)
  5528. {
  5529. write_insn<big_endian>(p, oris_12_12_0 + hi(off));
  5530. p += 4;
  5531. }
  5532. if (l(off) != 0)
  5533. {
  5534. write_insn<big_endian>(p, ori_12_12_0 + l(off));
  5535. p += 4;
  5536. }
  5537. if (load)
  5538. write_insn<big_endian>(p, ldx_12_11_12);
  5539. else
  5540. write_insn<big_endian>(p, add_12_11_12);
  5541. }
  5542. p += 4;
  5543. return p;
  5544. }
  5545. // Size of a given plt call stub.
  5546. template<int size, bool big_endian>
  5547. unsigned int
  5548. Stub_table<size, big_endian>::plt_call_size(
  5549. typename Plt_stub_entries::iterator p) const
  5550. {
  5551. if (size == 32)
  5552. {
  5553. unsigned int bytes = 4 * 4;
  5554. if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
  5555. bytes = 12 * 4;
  5556. return this->plt_call_align(bytes);
  5557. }
  5558. const Output_data_plt_powerpc<size, big_endian>* plt;
  5559. uint64_t plt_addr = this->plt_off(p, &plt);
  5560. plt_addr += plt->address();
  5561. if (this->targ_->power10_stubs()
  5562. && this->targ_->power10_stubs_auto())
  5563. {
  5564. unsigned int bytes = 0;
  5565. if (p->second.notoc_)
  5566. {
  5567. if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
  5568. bytes = 7 * 4;
  5569. uint64_t from = this->stub_address() + p->second.off_ + bytes;
  5570. uint64_t odd = from & 4;
  5571. uint64_t off = plt_addr - from;
  5572. if (off - odd + (1ULL << 33) < 1ULL << 34)
  5573. bytes += odd + 4 * 4;
  5574. else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
  5575. bytes += 7 * 4;
  5576. else
  5577. bytes += 8 * 4;
  5578. bytes = this->plt_call_align(bytes);
  5579. }
  5580. if (p->second.toc_)
  5581. {
  5582. p->second.tocoff_ = bytes;
  5583. if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
  5584. {
  5585. bytes += 7 * 4;
  5586. if (p->second.r2save_ && !p->second.localentry0_)
  5587. bytes += 2 * 4 + 4 * 4;
  5588. }
  5589. if (p->second.r2save_)
  5590. bytes += 4;
  5591. uint64_t got_addr = this->targ_->toc_pointer();
  5592. uint64_t off = plt_addr - got_addr;
  5593. bytes += 3 * 4 + 4 * (ha(off) != 0);
  5594. p->second.tsize_ = bytes - p->second.tocoff_;
  5595. bytes = this->plt_call_align(bytes);
  5596. }
  5597. if (p->second.p9notoc_)
  5598. {
  5599. p->second.p9off_ = bytes;
  5600. if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
  5601. bytes += 7 * 4;
  5602. uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
  5603. uint64_t off = plt_addr - from;
  5604. if (off + 0x8000 < 0x10000)
  5605. bytes += 7 * 4;
  5606. else if (off + 0x80008000ULL < 0x100000000ULL)
  5607. bytes += 8 * 4;
  5608. else
  5609. {
  5610. bytes += 8 * 4;
  5611. if (off + 0x800000000000ULL >= 0x1000000000000ULL
  5612. && ((off >> 32) & 0xffff) != 0)
  5613. bytes += 4;
  5614. if (((off >> 32) & 0xffffffffULL) != 0)
  5615. bytes += 4;
  5616. if (hi(off) != 0)
  5617. bytes += 4;
  5618. if (l(off) != 0)
  5619. bytes += 4;
  5620. }
  5621. bytes = this->plt_call_align(bytes);
  5622. }
  5623. return bytes;
  5624. }
  5625. else
  5626. {
  5627. unsigned int bytes = 0;
  5628. unsigned int tail = 0;
  5629. if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
  5630. {
  5631. bytes = 7 * 4;
  5632. if (p->second.r2save_ && !p->second.localentry0_)
  5633. {
  5634. bytes = 9 * 4;
  5635. tail = 4 * 4;
  5636. }
  5637. }
  5638. if (p->second.r2save_)
  5639. bytes += 4;
  5640. if (this->targ_->power10_stubs())
  5641. {
  5642. uint64_t from = this->stub_address() + p->second.off_ + bytes;
  5643. uint64_t odd = from & 4;
  5644. uint64_t off = plt_addr - from;
  5645. if (off - odd + (1ULL << 33) < 1ULL << 34)
  5646. bytes += odd + 4 * 4;
  5647. else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
  5648. bytes += 7 * 4;
  5649. else
  5650. bytes += 8 * 4;
  5651. return this->plt_call_align(bytes + tail);
  5652. }
  5653. if (p->second.p9notoc_)
  5654. {
  5655. uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
  5656. uint64_t off = plt_addr - from;
  5657. if (off + 0x8000 < 0x10000)
  5658. bytes += 7 * 4;
  5659. else if (off + 0x80008000ULL < 0x100000000ULL)
  5660. bytes += 8 * 4;
  5661. else
  5662. {
  5663. bytes += 8 * 4;
  5664. if (off + 0x800000000000ULL >= 0x1000000000000ULL
  5665. && ((off >> 32) & 0xffff) != 0)
  5666. bytes += 4;
  5667. if (((off >> 32) & 0xffffffffULL) != 0)
  5668. bytes += 4;
  5669. if (hi(off) != 0)
  5670. bytes += 4;
  5671. if (l(off) != 0)
  5672. bytes += 4;
  5673. }
  5674. return this->plt_call_align(bytes + tail);
  5675. }
  5676. uint64_t got_addr = this->targ_->toc_pointer();
  5677. uint64_t off = plt_addr - got_addr;
  5678. bytes += 3 * 4 + 4 * (ha(off) != 0);
  5679. if (this->targ_->abiversion() < 2)
  5680. {
  5681. bool static_chain = parameters->options().plt_static_chain();
  5682. bool thread_safe = this->targ_->plt_thread_safe();
  5683. bytes += (4
  5684. + 4 * static_chain
  5685. + 8 * thread_safe
  5686. + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
  5687. }
  5688. return this->plt_call_align(bytes + tail);
  5689. }
  5690. }
  5691. // Return long branch stub size.
  5692. template<int size, bool big_endian>
  5693. unsigned int
  5694. Stub_table<size, big_endian>::branch_stub_size(
  5695. typename Branch_stub_entries::iterator p,
  5696. bool* need_lt)
  5697. {
  5698. Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
  5699. if (size == 32)
  5700. {
  5701. if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
  5702. return 4;
  5703. if (parameters->options().output_is_position_independent())
  5704. return 32;
  5705. return 16;
  5706. }
  5707. uint64_t off = p->first.dest_ - loc;
  5708. unsigned int bytes = 0;
  5709. if (p->second.notoc_)
  5710. {
  5711. if (this->targ_->power10_stubs())
  5712. {
  5713. Address odd = loc & 4;
  5714. if (off + (1 << 25) < 2 << 25)
  5715. bytes = odd + 12;
  5716. else if (off - odd + (1ULL << 33) < 1ULL << 34)
  5717. bytes = odd + 16;
  5718. else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
  5719. bytes = 28;
  5720. else
  5721. bytes = 32;
  5722. if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
  5723. return bytes;
  5724. p->second.tocoff_ = bytes;
  5725. }
  5726. else
  5727. {
  5728. off -= 8;
  5729. if (off + 0x8000 < 0x10000)
  5730. return 24;
  5731. if (off + 0x80008000ULL < 0x100000000ULL)
  5732. {
  5733. if (off + 24 + (1 << 25) < 2 << 25)
  5734. return 28;
  5735. return 32;
  5736. }
  5737. bytes = 32;
  5738. if (off + 0x800000000000ULL >= 0x1000000000000ULL
  5739. && ((off >> 32) & 0xffff) != 0)
  5740. bytes += 4;
  5741. if (((off >> 32) & 0xffffffffULL) != 0)
  5742. bytes += 4;
  5743. if (hi(off) != 0)
  5744. bytes += 4;
  5745. if (l(off) != 0)
  5746. bytes += 4;
  5747. return bytes;
  5748. }
  5749. }
  5750. off += elfcpp::ppc64_decode_local_entry(p->second.other_);
  5751. if (off + (1 << 25) < 2 << 25)
  5752. return bytes + 4;
  5753. if (!this->targ_->power10_stubs()
  5754. || (p->second.toc_ && this->targ_->power10_stubs_auto()))
  5755. *need_lt = true;
  5756. return bytes + 16;
  5757. }
  5758. template<int size, bool big_endian>
  5759. void
  5760. Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
  5761. {
  5762. if (p.sym_)
  5763. gold_error(_("linkage table error against `%s'"),
  5764. p.sym_->demangled_name().c_str());
  5765. else
  5766. gold_error(_("linkage table error against `%s:[local %u]'"),
  5767. p.object_->name().c_str(),
  5768. p.locsym_);
  5769. }
  5770. // Write out plt and long branch stub code.
  5771. template<int size, bool big_endian>
  5772. void
  5773. Stub_table<size, big_endian>::do_write(Output_file* of)
  5774. {
  5775. if (this->plt_call_stubs_.empty()
  5776. && this->long_branch_stubs_.empty())
  5777. return;
  5778. const section_size_type start_off = this->offset();
  5779. const section_size_type off = this->stub_offset();
  5780. const section_size_type oview_size =
  5781. convert_to_section_size_type(this->data_size() - (off - start_off));
  5782. unsigned char* const oview = of->get_output_view(off, oview_size);
  5783. unsigned char* p;
  5784. if (size == 64
  5785. && this->targ_->power10_stubs())
  5786. {
  5787. if (!this->plt_call_stubs_.empty())
  5788. {
  5789. // Write out plt call stubs.
  5790. typename Plt_stub_entries::const_iterator cs;
  5791. for (cs = this->plt_call_stubs_.begin();
  5792. cs != this->plt_call_stubs_.end();
  5793. ++cs)
  5794. {
  5795. p = oview + cs->second.off_;
  5796. const Output_data_plt_powerpc<size, big_endian>* plt;
  5797. Address pltoff = this->plt_off(cs, &plt);
  5798. Address plt_addr = pltoff + plt->address();
  5799. if (this->targ_->power10_stubs_auto())
  5800. {
  5801. if (cs->second.notoc_)
  5802. {
  5803. if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  5804. this->build_tls_opt_head(&p, false);
  5805. Address from = this->stub_address() + (p - oview);
  5806. Address delta = plt_addr - from;
  5807. p = build_power10_offset<big_endian>(p, delta, from & 4,
  5808. true);
  5809. write_insn<big_endian>(p, mtctr_12);
  5810. p += 4;
  5811. write_insn<big_endian>(p, bctr);
  5812. p += 4;
  5813. p = oview + this->plt_call_align(p - oview);
  5814. }
  5815. if (cs->second.toc_)
  5816. {
  5817. if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  5818. {
  5819. bool save_lr
  5820. = cs->second.r2save_ && !cs->second.localentry0_;
  5821. this->build_tls_opt_head(&p, save_lr);
  5822. }
  5823. Address got_addr = this->targ_->toc_pointer();
  5824. Address off = plt_addr - got_addr;
  5825. if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
  5826. this->plt_error(cs->first);
  5827. if (cs->second.r2save_)
  5828. {
  5829. write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
  5830. p += 4;
  5831. }
  5832. if (ha(off) != 0)
  5833. {
  5834. write_insn<big_endian>(p, addis_12_2 + ha(off));
  5835. p += 4;
  5836. write_insn<big_endian>(p, ld_12_12 + l(off));
  5837. p += 4;
  5838. }
  5839. else
  5840. {
  5841. write_insn<big_endian>(p, ld_12_2 + l(off));
  5842. p += 4;
  5843. }
  5844. write_insn<big_endian>(p, mtctr_12);
  5845. p += 4;
  5846. if (cs->second.r2save_
  5847. && !cs->second.localentry0_
  5848. && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  5849. this->build_tls_opt_tail(p);
  5850. else
  5851. write_insn<big_endian>(p, bctr);
  5852. }
  5853. if (cs->second.p9notoc_)
  5854. {
  5855. if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  5856. this->build_tls_opt_head(&p, false);
  5857. Address from = this->stub_address() + (p - oview);
  5858. Address delta = plt_addr - from;
  5859. p = build_notoc_offset<big_endian>(p, delta, true);
  5860. write_insn<big_endian>(p, mtctr_12);
  5861. p += 4;
  5862. write_insn<big_endian>(p, bctr);
  5863. p += 4;
  5864. p = oview + this->plt_call_align(p - oview);
  5865. }
  5866. }
  5867. else
  5868. {
  5869. if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  5870. {
  5871. bool save_lr
  5872. = cs->second.r2save_ && !cs->second.localentry0_;
  5873. this->build_tls_opt_head(&p, save_lr);
  5874. }
  5875. if (cs->second.r2save_)
  5876. {
  5877. write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
  5878. p += 4;
  5879. }
  5880. Address from = this->stub_address() + (p - oview);
  5881. Address delta = plt_addr - from;
  5882. p = build_power10_offset<big_endian>(p, delta, from & 4, true);
  5883. write_insn<big_endian>(p, mtctr_12);
  5884. p += 4;
  5885. if (cs->second.r2save_
  5886. && !cs->second.localentry0_
  5887. && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  5888. this->build_tls_opt_tail(p);
  5889. else
  5890. write_insn<big_endian>(p, bctr);
  5891. }
  5892. }
  5893. }
  5894. // Write out long branch stubs.
  5895. typename Branch_stub_entries::const_iterator bs;
  5896. for (bs = this->long_branch_stubs_.begin();
  5897. bs != this->long_branch_stubs_.end();
  5898. ++bs)
  5899. {
  5900. if (bs->second.save_res_)
  5901. continue;
  5902. Address off = this->plt_size_ + bs->second.off_;
  5903. p = oview + off;
  5904. Address loc = this->stub_address() + off;
  5905. Address delta = bs->first.dest_ - loc;
  5906. if (this->targ_->power10_stubs_auto())
  5907. {
  5908. if (bs->second.notoc_)
  5909. {
  5910. unsigned char* startp = p;
  5911. p = build_power10_offset<big_endian>(p, delta,
  5912. loc & 4, false);
  5913. delta -= p - startp;
  5914. startp = p;
  5915. if (delta + (1 << 25) < 2 << 25)
  5916. write_insn<big_endian>(p, b | (delta & 0x3fffffc));
  5917. else
  5918. {
  5919. write_insn<big_endian>(p, mtctr_12);
  5920. p += 4;
  5921. write_insn<big_endian>(p, bctr);
  5922. }
  5923. p += 4;
  5924. delta -= p - startp;
  5925. }
  5926. if (bs->second.toc_)
  5927. {
  5928. delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
  5929. if (delta + (1 << 25) >= 2 << 25)
  5930. {
  5931. Address brlt_addr
  5932. = this->targ_->find_branch_lookup_table(bs->first.dest_);
  5933. gold_assert(brlt_addr != invalid_address);
  5934. brlt_addr += this->targ_->brlt_section()->address();
  5935. Address got_addr = this->targ_->toc_pointer();
  5936. Address brltoff = brlt_addr - got_addr;
  5937. if (ha(brltoff) == 0)
  5938. {
  5939. write_insn<big_endian>(p, ld_12_2 + l(brltoff));
  5940. p += 4;
  5941. }
  5942. else
  5943. {
  5944. write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
  5945. p += 4;
  5946. write_insn<big_endian>(p, ld_12_12 + l(brltoff));
  5947. p += 4;
  5948. }
  5949. }
  5950. if (delta + (1 << 25) < 2 << 25)
  5951. write_insn<big_endian>(p, b | (delta & 0x3fffffc));
  5952. else
  5953. {
  5954. write_insn<big_endian>(p, mtctr_12);
  5955. p += 4;
  5956. write_insn<big_endian>(p, bctr);
  5957. }
  5958. }
  5959. if (bs->second.p9notoc_)
  5960. {
  5961. unsigned char* startp = p;
  5962. p = build_notoc_offset<big_endian>(p, delta, false);
  5963. delta -= p - startp;
  5964. startp = p;
  5965. if (delta + (1 << 25) < 2 << 25)
  5966. write_insn<big_endian>(p, b | (delta & 0x3fffffc));
  5967. else
  5968. {
  5969. write_insn<big_endian>(p, mtctr_12);
  5970. p += 4;
  5971. write_insn<big_endian>(p, bctr);
  5972. }
  5973. p += 4;
  5974. delta -= p - startp;
  5975. }
  5976. }
  5977. else
  5978. {
  5979. if (!bs->second.notoc_)
  5980. delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
  5981. if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
  5982. {
  5983. unsigned char* startp = p;
  5984. p = build_power10_offset<big_endian>(p, delta,
  5985. loc & 4, false);
  5986. delta -= p - startp;
  5987. }
  5988. if (delta + (1 << 25) < 2 << 25)
  5989. write_insn<big_endian>(p, b | (delta & 0x3fffffc));
  5990. else
  5991. {
  5992. write_insn<big_endian>(p, mtctr_12);
  5993. p += 4;
  5994. write_insn<big_endian>(p, bctr);
  5995. }
  5996. }
  5997. }
  5998. }
  5999. else if (size == 64)
  6000. {
  6001. if (!this->plt_call_stubs_.empty()
  6002. && this->targ_->abiversion() >= 2)
  6003. {
  6004. // Write out plt call stubs for ELFv2.
  6005. typename Plt_stub_entries::const_iterator cs;
  6006. for (cs = this->plt_call_stubs_.begin();
  6007. cs != this->plt_call_stubs_.end();
  6008. ++cs)
  6009. {
  6010. const Output_data_plt_powerpc<size, big_endian>* plt;
  6011. Address pltoff = this->plt_off(cs, &plt);
  6012. Address plt_addr = pltoff + plt->address();
  6013. p = oview + cs->second.off_;
  6014. if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  6015. {
  6016. bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
  6017. this->build_tls_opt_head(&p, save_lr);
  6018. }
  6019. if (cs->second.r2save_)
  6020. {
  6021. write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
  6022. p += 4;
  6023. }
  6024. if (cs->second.p9notoc_)
  6025. {
  6026. Address from = this->stub_address() + (p - oview) + 8;
  6027. Address off = plt_addr - from;
  6028. p = build_notoc_offset<big_endian>(p, off, true);
  6029. }
  6030. else
  6031. {
  6032. Address got_addr = this->targ_->toc_pointer();
  6033. Address off = plt_addr - got_addr;
  6034. if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
  6035. this->plt_error(cs->first);
  6036. if (ha(off) != 0)
  6037. {
  6038. write_insn<big_endian>(p, addis_12_2 + ha(off));
  6039. p += 4;
  6040. write_insn<big_endian>(p, ld_12_12 + l(off));
  6041. p += 4;
  6042. }
  6043. else
  6044. {
  6045. write_insn<big_endian>(p, ld_12_2 + l(off));
  6046. p += 4;
  6047. }
  6048. }
  6049. write_insn<big_endian>(p, mtctr_12);
  6050. p += 4;
  6051. if (cs->second.r2save_
  6052. && !cs->second.localentry0_
  6053. && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  6054. this->build_tls_opt_tail(p);
  6055. else
  6056. write_insn<big_endian>(p, bctr);
  6057. }
  6058. }
  6059. else if (!this->plt_call_stubs_.empty())
  6060. {
  6061. // Write out plt call stubs for ELFv1.
  6062. typename Plt_stub_entries::const_iterator cs;
  6063. for (cs = this->plt_call_stubs_.begin();
  6064. cs != this->plt_call_stubs_.end();
  6065. ++cs)
  6066. {
  6067. const Output_data_plt_powerpc<size, big_endian>* plt;
  6068. Address pltoff = this->plt_off(cs, &plt);
  6069. Address plt_addr = pltoff + plt->address();
  6070. Address got_addr = this->targ_->toc_pointer();
  6071. Address off = plt_addr - got_addr;
  6072. if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
  6073. || cs->second.notoc_)
  6074. this->plt_error(cs->first);
  6075. bool static_chain = parameters->options().plt_static_chain();
  6076. bool thread_safe = this->targ_->plt_thread_safe();
  6077. bool use_fake_dep = false;
  6078. Address cmp_branch_off = 0;
  6079. if (thread_safe)
  6080. {
  6081. unsigned int pltindex
  6082. = ((pltoff - this->targ_->first_plt_entry_offset())
  6083. / this->targ_->plt_entry_size());
  6084. Address glinkoff
  6085. = (this->targ_->glink_section()->pltresolve_size()
  6086. + pltindex * 8);
  6087. if (pltindex > 32768)
  6088. glinkoff += (pltindex - 32768) * 4;
  6089. Address to
  6090. = this->targ_->glink_section()->address() + glinkoff;
  6091. Address from
  6092. = (this->stub_address() + cs->second.off_ + 20
  6093. + 4 * cs->second.r2save_
  6094. + 4 * (ha(off) != 0)
  6095. + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
  6096. + 4 * static_chain);
  6097. cmp_branch_off = to - from;
  6098. use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
  6099. }
  6100. p = oview + cs->second.off_;
  6101. if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  6102. {
  6103. bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
  6104. this->build_tls_opt_head(&p, save_lr);
  6105. use_fake_dep = thread_safe;
  6106. }
  6107. if (cs->second.r2save_)
  6108. {
  6109. write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
  6110. p += 4;
  6111. }
  6112. if (ha(off) != 0)
  6113. {
  6114. write_insn<big_endian>(p, addis_11_2 + ha(off));
  6115. p += 4;
  6116. write_insn<big_endian>(p, ld_12_11 + l(off));
  6117. p += 4;
  6118. if (ha(off + 8 + 8 * static_chain) != ha(off))
  6119. {
  6120. write_insn<big_endian>(p, addi_11_11 + l(off));
  6121. p += 4;
  6122. off = 0;
  6123. }
  6124. write_insn<big_endian>(p, mtctr_12);
  6125. p += 4;
  6126. if (use_fake_dep)
  6127. {
  6128. write_insn<big_endian>(p, xor_2_12_12);
  6129. p += 4;
  6130. write_insn<big_endian>(p, add_11_11_2);
  6131. p += 4;
  6132. }
  6133. write_insn<big_endian>(p, ld_2_11 + l(off + 8));
  6134. p += 4;
  6135. if (static_chain)
  6136. {
  6137. write_insn<big_endian>(p, ld_11_11 + l(off + 16));
  6138. p += 4;
  6139. }
  6140. }
  6141. else
  6142. {
  6143. write_insn<big_endian>(p, ld_12_2 + l(off));
  6144. p += 4;
  6145. if (ha(off + 8 + 8 * static_chain) != ha(off))
  6146. {
  6147. write_insn<big_endian>(p, addi_2_2 + l(off));
  6148. p += 4;
  6149. off = 0;
  6150. }
  6151. write_insn<big_endian>(p, mtctr_12);
  6152. p += 4;
  6153. if (use_fake_dep)
  6154. {
  6155. write_insn<big_endian>(p, xor_11_12_12);
  6156. p += 4;
  6157. write_insn<big_endian>(p, add_2_2_11);
  6158. p += 4;
  6159. }
  6160. if (static_chain)
  6161. {
  6162. write_insn<big_endian>(p, ld_11_2 + l(off + 16));
  6163. p += 4;
  6164. }
  6165. write_insn<big_endian>(p, ld_2_2 + l(off + 8));
  6166. p += 4;
  6167. }
  6168. if (cs->second.r2save_
  6169. && !cs->second.localentry0_
  6170. && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  6171. this->build_tls_opt_tail(p);
  6172. else if (thread_safe && !use_fake_dep)
  6173. {
  6174. write_insn<big_endian>(p, cmpldi_2_0);
  6175. p += 4;
  6176. write_insn<big_endian>(p, bnectr_p4);
  6177. p += 4;
  6178. write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
  6179. }
  6180. else
  6181. write_insn<big_endian>(p, bctr);
  6182. }
  6183. }
  6184. // Write out long branch stubs.
  6185. typename Branch_stub_entries::const_iterator bs;
  6186. for (bs = this->long_branch_stubs_.begin();
  6187. bs != this->long_branch_stubs_.end();
  6188. ++bs)
  6189. {
  6190. if (bs->second.save_res_)
  6191. continue;
  6192. Address off = this->plt_size_ + bs->second.off_;
  6193. p = oview + off;
  6194. Address loc = this->stub_address() + off;
  6195. Address delta = bs->first.dest_ - loc;
  6196. if (!bs->second.p9notoc_)
  6197. delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
  6198. if (bs->second.p9notoc_)
  6199. {
  6200. unsigned char* startp = p;
  6201. p = build_notoc_offset<big_endian>(p, off, false);
  6202. delta -= p - startp;
  6203. }
  6204. else if (delta + (1 << 25) >= 2 << 25)
  6205. {
  6206. Address brlt_addr
  6207. = this->targ_->find_branch_lookup_table(bs->first.dest_);
  6208. gold_assert(brlt_addr != invalid_address);
  6209. brlt_addr += this->targ_->brlt_section()->address();
  6210. Address got_addr = this->targ_->toc_pointer();
  6211. Address brltoff = brlt_addr - got_addr;
  6212. if (ha(brltoff) == 0)
  6213. {
  6214. write_insn<big_endian>(p, ld_12_2 + l(brltoff));
  6215. p += 4;
  6216. }
  6217. else
  6218. {
  6219. write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
  6220. p += 4;
  6221. write_insn<big_endian>(p, ld_12_12 + l(brltoff));
  6222. p += 4;
  6223. }
  6224. }
  6225. if (delta + (1 << 25) < 2 << 25)
  6226. write_insn<big_endian>(p, b | (delta & 0x3fffffc));
  6227. else
  6228. {
  6229. write_insn<big_endian>(p, mtctr_12);
  6230. p += 4;
  6231. write_insn<big_endian>(p, bctr);
  6232. }
  6233. }
  6234. }
  6235. else // size == 32
  6236. {
  6237. if (!this->plt_call_stubs_.empty())
  6238. {
  6239. // The address of _GLOBAL_OFFSET_TABLE_.
  6240. Address g_o_t = invalid_address;
  6241. // Write out plt call stubs.
  6242. typename Plt_stub_entries::const_iterator cs;
  6243. for (cs = this->plt_call_stubs_.begin();
  6244. cs != this->plt_call_stubs_.end();
  6245. ++cs)
  6246. {
  6247. const Output_data_plt_powerpc<size, big_endian>* plt;
  6248. Address plt_addr = this->plt_off(cs, &plt);
  6249. plt_addr += plt->address();
  6250. p = oview + cs->second.off_;
  6251. if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
  6252. this->build_tls_opt_head(&p, false);
  6253. if (parameters->options().output_is_position_independent())
  6254. {
  6255. Address got_addr;
  6256. const Powerpc_relobj<size, big_endian>* ppcobj
  6257. = (static_cast<const Powerpc_relobj<size, big_endian>*>
  6258. (cs->first.object_));
  6259. if (ppcobj != NULL && cs->first.addend_ >= 32768)
  6260. {
  6261. unsigned int got2 = ppcobj->got2_shndx();
  6262. got_addr = ppcobj->get_output_section_offset(got2);
  6263. gold_assert(got_addr != invalid_address);
  6264. got_addr += (ppcobj->output_section(got2)->address()
  6265. + cs->first.addend_);
  6266. }
  6267. else
  6268. {
  6269. if (g_o_t == invalid_address)
  6270. g_o_t = this->targ_->toc_pointer();
  6271. got_addr = g_o_t;
  6272. }
  6273. Address off = plt_addr - got_addr;
  6274. if (ha(off) == 0)
  6275. write_insn<big_endian>(p, lwz_11_30 + l(off));
  6276. else
  6277. {
  6278. write_insn<big_endian>(p, addis_11_30 + ha(off));
  6279. p += 4;
  6280. write_insn<big_endian>(p, lwz_11_11 + l(off));
  6281. }
  6282. }
  6283. else
  6284. {
  6285. write_insn<big_endian>(p, lis_11 + ha(plt_addr));
  6286. p += 4;
  6287. write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
  6288. }
  6289. p += 4;
  6290. write_insn<big_endian>(p, mtctr_11);
  6291. p += 4;
  6292. write_insn<big_endian>(p, bctr);
  6293. }
  6294. }
  6295. // Write out long branch stubs.
  6296. typename Branch_stub_entries::const_iterator bs;
  6297. for (bs = this->long_branch_stubs_.begin();
  6298. bs != this->long_branch_stubs_.end();
  6299. ++bs)
  6300. {
  6301. if (bs->second.save_res_)
  6302. continue;
  6303. Address off = this->plt_size_ + bs->second.off_;
  6304. p = oview + off;
  6305. Address loc = this->stub_address() + off;
  6306. Address delta = bs->first.dest_ - loc;
  6307. if (delta + (1 << 25) < 2 << 25)
  6308. write_insn<big_endian>(p, b | (delta & 0x3fffffc));
  6309. else if (!parameters->options().output_is_position_independent())
  6310. {
  6311. write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
  6312. p += 4;
  6313. write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
  6314. }
  6315. else
  6316. {
  6317. delta -= 8;
  6318. write_insn<big_endian>(p, mflr_0);
  6319. p += 4;
  6320. write_insn<big_endian>(p, bcl_20_31);
  6321. p += 4;
  6322. write_insn<big_endian>(p, mflr_12);
  6323. p += 4;
  6324. write_insn<big_endian>(p, addis_12_12 + ha(delta));
  6325. p += 4;
  6326. write_insn<big_endian>(p, addi_12_12 + l(delta));
  6327. p += 4;
  6328. write_insn<big_endian>(p, mtlr_0);
  6329. }
  6330. p += 4;
  6331. write_insn<big_endian>(p, mtctr_12);
  6332. p += 4;
  6333. write_insn<big_endian>(p, bctr);
  6334. }
  6335. }
  6336. if (this->need_save_res_)
  6337. {
  6338. p = oview + this->plt_size_ + this->branch_size_;
  6339. memcpy (p, this->targ_->savres_section()->contents(),
  6340. this->targ_->savres_section()->data_size());
  6341. }
  6342. }
  6343. // Write out .glink.
  6344. template<int size, bool big_endian>
  6345. void
  6346. Output_data_glink<size, big_endian>::do_write(Output_file* of)
  6347. {
  6348. const section_size_type off = this->offset();
  6349. const section_size_type oview_size =
  6350. convert_to_section_size_type(this->data_size());
  6351. unsigned char* const oview = of->get_output_view(off, oview_size);
  6352. unsigned char* p;
  6353. // The base address of the .plt section.
  6354. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  6355. Address plt_base = this->targ_->plt_section()->address();
  6356. if (size == 64)
  6357. {
  6358. if (this->end_branch_table_ != 0)
  6359. {
  6360. // Write pltresolve stub.
  6361. p = oview;
  6362. Address after_bcl = this->address() + 16;
  6363. Address pltoff = plt_base - after_bcl;
  6364. elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
  6365. if (this->targ_->abiversion() < 2)
  6366. {
  6367. write_insn<big_endian>(p, mflr_12), p += 4;
  6368. write_insn<big_endian>(p, bcl_20_31), p += 4;
  6369. write_insn<big_endian>(p, mflr_11), p += 4;
  6370. write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
  6371. write_insn<big_endian>(p, mtlr_12), p += 4;
  6372. write_insn<big_endian>(p, add_11_2_11), p += 4;
  6373. write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
  6374. write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
  6375. write_insn<big_endian>(p, mtctr_12), p += 4;
  6376. write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
  6377. }
  6378. else
  6379. {
  6380. if (this->targ_->has_localentry0())
  6381. {
  6382. write_insn<big_endian>(p, std_2_1 + 24), p += 4;
  6383. }
  6384. write_insn<big_endian>(p, mflr_0), p += 4;
  6385. write_insn<big_endian>(p, bcl_20_31), p += 4;
  6386. write_insn<big_endian>(p, mflr_11), p += 4;
  6387. write_insn<big_endian>(p, mtlr_0), p += 4;
  6388. if (this->targ_->has_localentry0())
  6389. {
  6390. write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
  6391. }
  6392. else
  6393. {
  6394. write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
  6395. }
  6396. write_insn<big_endian>(p, sub_12_12_11), p += 4;
  6397. write_insn<big_endian>(p, add_11_0_11), p += 4;
  6398. write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
  6399. write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
  6400. write_insn<big_endian>(p, srdi_0_0_2), p += 4;
  6401. write_insn<big_endian>(p, mtctr_12), p += 4;
  6402. write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
  6403. }
  6404. write_insn<big_endian>(p, bctr), p += 4;
  6405. gold_assert(p == oview + this->pltresolve_size());
  6406. // Write lazy link call stubs.
  6407. uint32_t indx = 0;
  6408. while (p < oview + this->end_branch_table_)
  6409. {
  6410. if (this->targ_->abiversion() < 2)
  6411. {
  6412. if (indx < 0x8000)
  6413. {
  6414. write_insn<big_endian>(p, li_0_0 + indx), p += 4;
  6415. }
  6416. else
  6417. {
  6418. write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
  6419. write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
  6420. }
  6421. }
  6422. uint32_t branch_off = 8 - (p - oview);
  6423. write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
  6424. indx++;
  6425. }
  6426. }
  6427. Address plt_base = this->targ_->plt_section()->address();
  6428. Address iplt_base = invalid_address;
  6429. unsigned int global_entry_off = this->global_entry_off();
  6430. Address global_entry_base = this->address() + global_entry_off;
  6431. typename Global_entry_stub_entries::const_iterator ge;
  6432. for (ge = this->global_entry_stubs_.begin();
  6433. ge != this->global_entry_stubs_.end();
  6434. ++ge)
  6435. {
  6436. p = oview + global_entry_off + ge->second;
  6437. Address plt_addr = ge->first->plt_offset();
  6438. if (ge->first->type() == elfcpp::STT_GNU_IFUNC
  6439. && ge->first->can_use_relative_reloc(false))
  6440. {
  6441. if (iplt_base == invalid_address)
  6442. iplt_base = this->targ_->iplt_section()->address();
  6443. plt_addr += iplt_base;
  6444. }
  6445. else
  6446. plt_addr += plt_base;
  6447. Address my_addr = global_entry_base + ge->second;
  6448. Address off = plt_addr - my_addr;
  6449. if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
  6450. gold_error(_("linkage table error against `%s'"),
  6451. ge->first->demangled_name().c_str());
  6452. write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
  6453. write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
  6454. write_insn<big_endian>(p, mtctr_12), p += 4;
  6455. write_insn<big_endian>(p, bctr);
  6456. }
  6457. }
  6458. else
  6459. {
  6460. // The address of _GLOBAL_OFFSET_TABLE_.
  6461. Address g_o_t = this->targ_->toc_pointer();
  6462. // Write out pltresolve branch table.
  6463. p = oview;
  6464. unsigned int the_end = oview_size - this->pltresolve_size();
  6465. unsigned char* end_p = oview + the_end;
  6466. while (p < end_p - 8 * 4)
  6467. write_insn<big_endian>(p, b + end_p - p), p += 4;
  6468. while (p < end_p)
  6469. write_insn<big_endian>(p, nop), p += 4;
  6470. // Write out pltresolve call stub.
  6471. end_p = oview + oview_size;
  6472. if (parameters->options().output_is_position_independent())
  6473. {
  6474. Address res0_off = 0;
  6475. Address after_bcl_off = the_end + 12;
  6476. Address bcl_res0 = after_bcl_off - res0_off;
  6477. write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
  6478. p += 4;
  6479. write_insn<big_endian>(p, mflr_0);
  6480. p += 4;
  6481. write_insn<big_endian>(p, bcl_20_31);
  6482. p += 4;
  6483. write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
  6484. p += 4;
  6485. write_insn<big_endian>(p, mflr_12);
  6486. p += 4;
  6487. write_insn<big_endian>(p, mtlr_0);
  6488. p += 4;
  6489. write_insn<big_endian>(p, sub_11_11_12);
  6490. p += 4;
  6491. Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
  6492. write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
  6493. p += 4;
  6494. if (ha(got_bcl) == ha(got_bcl + 4))
  6495. {
  6496. write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
  6497. p += 4;
  6498. write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
  6499. }
  6500. else
  6501. {
  6502. write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
  6503. p += 4;
  6504. write_insn<big_endian>(p, lwz_12_12 + 4);
  6505. }
  6506. p += 4;
  6507. write_insn<big_endian>(p, mtctr_0);
  6508. p += 4;
  6509. write_insn<big_endian>(p, add_0_11_11);
  6510. p += 4;
  6511. write_insn<big_endian>(p, add_11_0_11);
  6512. }
  6513. else
  6514. {
  6515. Address res0 = this->address();
  6516. write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
  6517. p += 4;
  6518. write_insn<big_endian>(p, addis_11_11 + ha(-res0));
  6519. p += 4;
  6520. if (ha(g_o_t + 4) == ha(g_o_t + 8))
  6521. write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
  6522. else
  6523. write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
  6524. p += 4;
  6525. write_insn<big_endian>(p, addi_11_11 + l(-res0));
  6526. p += 4;
  6527. write_insn<big_endian>(p, mtctr_0);
  6528. p += 4;
  6529. write_insn<big_endian>(p, add_0_11_11);
  6530. p += 4;
  6531. if (ha(g_o_t + 4) == ha(g_o_t + 8))
  6532. write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
  6533. else
  6534. write_insn<big_endian>(p, lwz_12_12 + 4);
  6535. p += 4;
  6536. write_insn<big_endian>(p, add_11_0_11);
  6537. }
  6538. p += 4;
  6539. write_insn<big_endian>(p, bctr);
  6540. p += 4;
  6541. while (p < end_p)
  6542. {
  6543. write_insn<big_endian>(p, nop);
  6544. p += 4;
  6545. }
  6546. }
  6547. of->write_output_view(off, oview_size, oview);
  6548. }
  6549. // A class to handle linker generated save/restore functions.
  6550. template<int size, bool big_endian>
  6551. class Output_data_save_res : public Output_section_data_build
  6552. {
  6553. public:
  6554. Output_data_save_res(Symbol_table* symtab);
  6555. const unsigned char*
  6556. contents() const
  6557. {
  6558. return contents_;
  6559. }
  6560. protected:
  6561. // Write to a map file.
  6562. void
  6563. do_print_to_mapfile(Mapfile* mapfile) const
  6564. { mapfile->print_output_data(this, _("** save/restore")); }
  6565. void
  6566. do_write(Output_file*);
  6567. private:
  6568. // The maximum size of save/restore contents.
  6569. static const unsigned int savres_max = 218*4;
  6570. void
  6571. savres_define(Symbol_table* symtab,
  6572. const char *name,
  6573. unsigned int lo, unsigned int hi,
  6574. unsigned char* write_ent(unsigned char*, int),
  6575. unsigned char* write_tail(unsigned char*, int));
  6576. unsigned char *contents_;
  6577. };
  6578. template<bool big_endian>
  6579. static unsigned char*
  6580. savegpr0(unsigned char* p, int r)
  6581. {
  6582. uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
  6583. write_insn<big_endian>(p, insn);
  6584. return p + 4;
  6585. }
  6586. template<bool big_endian>
  6587. static unsigned char*
  6588. savegpr0_tail(unsigned char* p, int r)
  6589. {
  6590. p = savegpr0<big_endian>(p, r);
  6591. uint32_t insn = std_0_1 + 16;
  6592. write_insn<big_endian>(p, insn);
  6593. p = p + 4;
  6594. write_insn<big_endian>(p, blr);
  6595. return p + 4;
  6596. }
  6597. template<bool big_endian>
  6598. static unsigned char*
  6599. restgpr0(unsigned char* p, int r)
  6600. {
  6601. uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
  6602. write_insn<big_endian>(p, insn);
  6603. return p + 4;
  6604. }
  6605. template<bool big_endian>
  6606. static unsigned char*
  6607. restgpr0_tail(unsigned char* p, int r)
  6608. {
  6609. uint32_t insn = ld_0_1 + 16;
  6610. write_insn<big_endian>(p, insn);
  6611. p = p + 4;
  6612. p = restgpr0<big_endian>(p, r);
  6613. write_insn<big_endian>(p, mtlr_0);
  6614. p = p + 4;
  6615. if (r == 29)
  6616. {
  6617. p = restgpr0<big_endian>(p, 30);
  6618. p = restgpr0<big_endian>(p, 31);
  6619. }
  6620. write_insn<big_endian>(p, blr);
  6621. return p + 4;
  6622. }
  6623. template<bool big_endian>
  6624. static unsigned char*
  6625. savegpr1(unsigned char* p, int r)
  6626. {
  6627. uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
  6628. write_insn<big_endian>(p, insn);
  6629. return p + 4;
  6630. }
  6631. template<bool big_endian>
  6632. static unsigned char*
  6633. savegpr1_tail(unsigned char* p, int r)
  6634. {
  6635. p = savegpr1<big_endian>(p, r);
  6636. write_insn<big_endian>(p, blr);
  6637. return p + 4;
  6638. }
  6639. template<bool big_endian>
  6640. static unsigned char*
  6641. restgpr1(unsigned char* p, int r)
  6642. {
  6643. uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
  6644. write_insn<big_endian>(p, insn);
  6645. return p + 4;
  6646. }
  6647. template<bool big_endian>
  6648. static unsigned char*
  6649. restgpr1_tail(unsigned char* p, int r)
  6650. {
  6651. p = restgpr1<big_endian>(p, r);
  6652. write_insn<big_endian>(p, blr);
  6653. return p + 4;
  6654. }
  6655. template<bool big_endian>
  6656. static unsigned char*
  6657. savefpr(unsigned char* p, int r)
  6658. {
  6659. uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
  6660. write_insn<big_endian>(p, insn);
  6661. return p + 4;
  6662. }
  6663. template<bool big_endian>
  6664. static unsigned char*
  6665. savefpr0_tail(unsigned char* p, int r)
  6666. {
  6667. p = savefpr<big_endian>(p, r);
  6668. write_insn<big_endian>(p, std_0_1 + 16);
  6669. p = p + 4;
  6670. write_insn<big_endian>(p, blr);
  6671. return p + 4;
  6672. }
  6673. template<bool big_endian>
  6674. static unsigned char*
  6675. restfpr(unsigned char* p, int r)
  6676. {
  6677. uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
  6678. write_insn<big_endian>(p, insn);
  6679. return p + 4;
  6680. }
  6681. template<bool big_endian>
  6682. static unsigned char*
  6683. restfpr0_tail(unsigned char* p, int r)
  6684. {
  6685. write_insn<big_endian>(p, ld_0_1 + 16);
  6686. p = p + 4;
  6687. p = restfpr<big_endian>(p, r);
  6688. write_insn<big_endian>(p, mtlr_0);
  6689. p = p + 4;
  6690. if (r == 29)
  6691. {
  6692. p = restfpr<big_endian>(p, 30);
  6693. p = restfpr<big_endian>(p, 31);
  6694. }
  6695. write_insn<big_endian>(p, blr);
  6696. return p + 4;
  6697. }
  6698. template<bool big_endian>
  6699. static unsigned char*
  6700. savefpr1_tail(unsigned char* p, int r)
  6701. {
  6702. p = savefpr<big_endian>(p, r);
  6703. write_insn<big_endian>(p, blr);
  6704. return p + 4;
  6705. }
  6706. template<bool big_endian>
  6707. static unsigned char*
  6708. restfpr1_tail(unsigned char* p, int r)
  6709. {
  6710. p = restfpr<big_endian>(p, r);
  6711. write_insn<big_endian>(p, blr);
  6712. return p + 4;
  6713. }
  6714. template<bool big_endian>
  6715. static unsigned char*
  6716. savevr(unsigned char* p, int r)
  6717. {
  6718. uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
  6719. write_insn<big_endian>(p, insn);
  6720. p = p + 4;
  6721. insn = stvx_0_12_0 + (r << 21);
  6722. write_insn<big_endian>(p, insn);
  6723. return p + 4;
  6724. }
  6725. template<bool big_endian>
  6726. static unsigned char*
  6727. savevr_tail(unsigned char* p, int r)
  6728. {
  6729. p = savevr<big_endian>(p, r);
  6730. write_insn<big_endian>(p, blr);
  6731. return p + 4;
  6732. }
  6733. template<bool big_endian>
  6734. static unsigned char*
  6735. restvr(unsigned char* p, int r)
  6736. {
  6737. uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
  6738. write_insn<big_endian>(p, insn);
  6739. p = p + 4;
  6740. insn = lvx_0_12_0 + (r << 21);
  6741. write_insn<big_endian>(p, insn);
  6742. return p + 4;
  6743. }
  6744. template<bool big_endian>
  6745. static unsigned char*
  6746. restvr_tail(unsigned char* p, int r)
  6747. {
  6748. p = restvr<big_endian>(p, r);
  6749. write_insn<big_endian>(p, blr);
  6750. return p + 4;
  6751. }
  6752. template<int size, bool big_endian>
  6753. Output_data_save_res<size, big_endian>::Output_data_save_res(
  6754. Symbol_table* symtab)
  6755. : Output_section_data_build(4),
  6756. contents_(NULL)
  6757. {
  6758. this->savres_define(symtab,
  6759. "_savegpr0_", 14, 31,
  6760. savegpr0<big_endian>, savegpr0_tail<big_endian>);
  6761. this->savres_define(symtab,
  6762. "_restgpr0_", 14, 29,
  6763. restgpr0<big_endian>, restgpr0_tail<big_endian>);
  6764. this->savres_define(symtab,
  6765. "_restgpr0_", 30, 31,
  6766. restgpr0<big_endian>, restgpr0_tail<big_endian>);
  6767. this->savres_define(symtab,
  6768. "_savegpr1_", 14, 31,
  6769. savegpr1<big_endian>, savegpr1_tail<big_endian>);
  6770. this->savres_define(symtab,
  6771. "_restgpr1_", 14, 31,
  6772. restgpr1<big_endian>, restgpr1_tail<big_endian>);
  6773. this->savres_define(symtab,
  6774. "_savefpr_", 14, 31,
  6775. savefpr<big_endian>, savefpr0_tail<big_endian>);
  6776. this->savres_define(symtab,
  6777. "_restfpr_", 14, 29,
  6778. restfpr<big_endian>, restfpr0_tail<big_endian>);
  6779. this->savres_define(symtab,
  6780. "_restfpr_", 30, 31,
  6781. restfpr<big_endian>, restfpr0_tail<big_endian>);
  6782. this->savres_define(symtab,
  6783. "._savef", 14, 31,
  6784. savefpr<big_endian>, savefpr1_tail<big_endian>);
  6785. this->savres_define(symtab,
  6786. "._restf", 14, 31,
  6787. restfpr<big_endian>, restfpr1_tail<big_endian>);
  6788. this->savres_define(symtab,
  6789. "_savevr_", 20, 31,
  6790. savevr<big_endian>, savevr_tail<big_endian>);
  6791. this->savres_define(symtab,
  6792. "_restvr_", 20, 31,
  6793. restvr<big_endian>, restvr_tail<big_endian>);
  6794. }
  6795. template<int size, bool big_endian>
  6796. void
  6797. Output_data_save_res<size, big_endian>::savres_define(
  6798. Symbol_table* symtab,
  6799. const char *name,
  6800. unsigned int lo, unsigned int hi,
  6801. unsigned char* write_ent(unsigned char*, int),
  6802. unsigned char* write_tail(unsigned char*, int))
  6803. {
  6804. size_t len = strlen(name);
  6805. bool writing = false;
  6806. char sym[16];
  6807. memcpy(sym, name, len);
  6808. sym[len + 2] = 0;
  6809. for (unsigned int i = lo; i <= hi; i++)
  6810. {
  6811. sym[len + 0] = i / 10 + '0';
  6812. sym[len + 1] = i % 10 + '0';
  6813. Symbol* gsym = symtab->lookup(sym);
  6814. bool refd = gsym != NULL && gsym->is_undefined();
  6815. writing = writing || refd;
  6816. if (writing)
  6817. {
  6818. if (this->contents_ == NULL)
  6819. this->contents_ = new unsigned char[this->savres_max];
  6820. section_size_type value = this->current_data_size();
  6821. unsigned char* p = this->contents_ + value;
  6822. if (i != hi)
  6823. p = write_ent(p, i);
  6824. else
  6825. p = write_tail(p, i);
  6826. section_size_type cur_size = p - this->contents_;
  6827. this->set_current_data_size(cur_size);
  6828. if (refd)
  6829. symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
  6830. this, value, cur_size - value,
  6831. elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
  6832. elfcpp::STV_HIDDEN, 0, false, false);
  6833. }
  6834. }
  6835. }
  6836. // Write out save/restore.
  6837. template<int size, bool big_endian>
  6838. void
  6839. Output_data_save_res<size, big_endian>::do_write(Output_file* of)
  6840. {
  6841. const section_size_type off = this->offset();
  6842. const section_size_type oview_size =
  6843. convert_to_section_size_type(this->data_size());
  6844. unsigned char* const oview = of->get_output_view(off, oview_size);
  6845. memcpy(oview, this->contents_, oview_size);
  6846. of->write_output_view(off, oview_size, oview);
  6847. }
  6848. // Create the glink section.
  6849. template<int size, bool big_endian>
  6850. void
  6851. Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
  6852. {
  6853. if (this->glink_ == NULL)
  6854. {
  6855. this->glink_ = new Output_data_glink<size, big_endian>(this);
  6856. this->glink_->add_eh_frame(layout);
  6857. layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
  6858. elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
  6859. this->glink_, ORDER_TEXT, false);
  6860. }
  6861. }
  6862. // Create a PLT entry for a global symbol.
  6863. template<int size, bool big_endian>
  6864. void
  6865. Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
  6866. Layout* layout,
  6867. Symbol* gsym)
  6868. {
  6869. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  6870. && gsym->can_use_relative_reloc(false))
  6871. {
  6872. if (this->iplt_ == NULL)
  6873. this->make_iplt_section(symtab, layout);
  6874. this->iplt_->add_ifunc_entry(gsym);
  6875. }
  6876. else
  6877. {
  6878. if (this->plt_ == NULL)
  6879. this->make_plt_section(symtab, layout);
  6880. this->plt_->add_entry(gsym);
  6881. }
  6882. }
  6883. // Make a PLT entry for a local symbol.
  6884. template<int size, bool big_endian>
  6885. void
  6886. Target_powerpc<size, big_endian>::make_local_plt_entry(
  6887. Symbol_table* symtab,
  6888. Layout* layout,
  6889. Sized_relobj_file<size, big_endian>* relobj,
  6890. unsigned int r_sym)
  6891. {
  6892. if (this->lplt_ == NULL)
  6893. this->make_lplt_section(symtab, layout);
  6894. this->lplt_->add_local_entry(relobj, r_sym);
  6895. }
  6896. template<int size, bool big_endian>
  6897. void
  6898. Target_powerpc<size, big_endian>::make_local_plt_entry(Symbol_table* symtab,
  6899. Layout* layout,
  6900. Symbol* gsym)
  6901. {
  6902. if (this->lplt_ == NULL)
  6903. this->make_lplt_section(symtab, layout);
  6904. this->lplt_->add_entry(gsym, true);
  6905. }
  6906. // Make a PLT entry for a local STT_GNU_IFUNC symbol.
  6907. template<int size, bool big_endian>
  6908. void
  6909. Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
  6910. Symbol_table* symtab,
  6911. Layout* layout,
  6912. Sized_relobj_file<size, big_endian>* relobj,
  6913. unsigned int r_sym)
  6914. {
  6915. if (this->iplt_ == NULL)
  6916. this->make_iplt_section(symtab, layout);
  6917. this->iplt_->add_local_ifunc_entry(relobj, r_sym);
  6918. }
  6919. // Return the number of entries in the PLT.
  6920. template<int size, bool big_endian>
  6921. unsigned int
  6922. Target_powerpc<size, big_endian>::plt_entry_count() const
  6923. {
  6924. if (this->plt_ == NULL)
  6925. return 0;
  6926. return this->plt_->entry_count();
  6927. }
  6928. // Create a GOT entry for local dynamic __tls_get_addr calls.
  6929. template<int size, bool big_endian>
  6930. unsigned int
  6931. Target_powerpc<size, big_endian>::tlsld_got_offset(
  6932. Symbol_table* symtab,
  6933. Layout* layout,
  6934. Sized_relobj_file<size, big_endian>* object)
  6935. {
  6936. if (this->tlsld_got_offset_ == -1U)
  6937. {
  6938. gold_assert(symtab != NULL && layout != NULL && object != NULL);
  6939. Reloc_section* rela_dyn = this->rela_dyn_section(layout);
  6940. Output_data_got_powerpc<size, big_endian>* got
  6941. = this->got_section(symtab, layout, GOT_TYPE_SMALL);
  6942. unsigned int got_offset = got->add_constant_pair(0, 0);
  6943. rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
  6944. got_offset, 0);
  6945. this->tlsld_got_offset_ = got_offset;
  6946. }
  6947. return this->tlsld_got_offset_;
  6948. }
  6949. // Get the Reference_flags for a particular relocation.
  6950. template<int size, bool big_endian>
  6951. int
  6952. Target_powerpc<size, big_endian>::Scan::get_reference_flags(
  6953. unsigned int r_type,
  6954. const Target_powerpc* target)
  6955. {
  6956. int ref = 0;
  6957. switch (r_type)
  6958. {
  6959. case elfcpp::R_POWERPC_NONE:
  6960. case elfcpp::R_POWERPC_GNU_VTINHERIT:
  6961. case elfcpp::R_POWERPC_GNU_VTENTRY:
  6962. case elfcpp::R_PPC64_TOC:
  6963. // No symbol reference.
  6964. break;
  6965. case elfcpp::R_PPC64_ADDR64:
  6966. case elfcpp::R_PPC64_UADDR64:
  6967. case elfcpp::R_POWERPC_ADDR32:
  6968. case elfcpp::R_POWERPC_UADDR32:
  6969. case elfcpp::R_POWERPC_ADDR16:
  6970. case elfcpp::R_POWERPC_UADDR16:
  6971. case elfcpp::R_POWERPC_ADDR16_LO:
  6972. case elfcpp::R_POWERPC_ADDR16_HI:
  6973. case elfcpp::R_POWERPC_ADDR16_HA:
  6974. case elfcpp::R_PPC64_ADDR16_HIGHER34:
  6975. case elfcpp::R_PPC64_ADDR16_HIGHERA34:
  6976. case elfcpp::R_PPC64_ADDR16_HIGHEST34:
  6977. case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
  6978. case elfcpp::R_PPC64_D34:
  6979. case elfcpp::R_PPC64_D34_LO:
  6980. case elfcpp::R_PPC64_D34_HI30:
  6981. case elfcpp::R_PPC64_D34_HA30:
  6982. case elfcpp::R_PPC64_D28:
  6983. ref = Symbol::ABSOLUTE_REF;
  6984. break;
  6985. case elfcpp::R_POWERPC_ADDR24:
  6986. case elfcpp::R_POWERPC_ADDR14:
  6987. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  6988. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  6989. ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
  6990. break;
  6991. case elfcpp::R_PPC64_REL64:
  6992. case elfcpp::R_POWERPC_REL32:
  6993. case elfcpp::R_PPC_LOCAL24PC:
  6994. case elfcpp::R_POWERPC_REL16:
  6995. case elfcpp::R_POWERPC_REL16_LO:
  6996. case elfcpp::R_POWERPC_REL16_HI:
  6997. case elfcpp::R_POWERPC_REL16_HA:
  6998. case elfcpp::R_PPC64_REL16_HIGH:
  6999. case elfcpp::R_PPC64_REL16_HIGHA:
  7000. case elfcpp::R_PPC64_REL16_HIGHER:
  7001. case elfcpp::R_PPC64_REL16_HIGHERA:
  7002. case elfcpp::R_PPC64_REL16_HIGHEST:
  7003. case elfcpp::R_PPC64_REL16_HIGHESTA:
  7004. case elfcpp::R_PPC64_PCREL34:
  7005. case elfcpp::R_PPC64_REL16_HIGHER34:
  7006. case elfcpp::R_PPC64_REL16_HIGHERA34:
  7007. case elfcpp::R_PPC64_REL16_HIGHEST34:
  7008. case elfcpp::R_PPC64_REL16_HIGHESTA34:
  7009. case elfcpp::R_PPC64_PCREL28:
  7010. ref = Symbol::RELATIVE_REF;
  7011. break;
  7012. case elfcpp::R_PPC64_REL24_NOTOC:
  7013. if (size == 32)
  7014. break;
  7015. // Fall through.
  7016. case elfcpp::R_PPC64_REL24_P9NOTOC:
  7017. case elfcpp::R_POWERPC_REL24:
  7018. case elfcpp::R_PPC_PLTREL24:
  7019. case elfcpp::R_POWERPC_REL14:
  7020. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  7021. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  7022. case elfcpp::R_POWERPC_PLT16_LO:
  7023. case elfcpp::R_POWERPC_PLT16_HI:
  7024. case elfcpp::R_POWERPC_PLT16_HA:
  7025. case elfcpp::R_PPC64_PLT16_LO_DS:
  7026. case elfcpp::R_POWERPC_PLTSEQ:
  7027. case elfcpp::R_PPC64_PLTSEQ_NOTOC:
  7028. case elfcpp::R_POWERPC_PLTCALL:
  7029. case elfcpp::R_PPC64_PLTCALL_NOTOC:
  7030. case elfcpp::R_PPC64_PLT_PCREL34:
  7031. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  7032. ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  7033. break;
  7034. case elfcpp::R_POWERPC_GOT16:
  7035. case elfcpp::R_POWERPC_GOT16_LO:
  7036. case elfcpp::R_POWERPC_GOT16_HI:
  7037. case elfcpp::R_POWERPC_GOT16_HA:
  7038. case elfcpp::R_PPC64_GOT16_DS:
  7039. case elfcpp::R_PPC64_GOT16_LO_DS:
  7040. case elfcpp::R_PPC64_GOT_PCREL34:
  7041. case elfcpp::R_PPC64_TOC16:
  7042. case elfcpp::R_PPC64_TOC16_LO:
  7043. case elfcpp::R_PPC64_TOC16_HI:
  7044. case elfcpp::R_PPC64_TOC16_HA:
  7045. case elfcpp::R_PPC64_TOC16_DS:
  7046. case elfcpp::R_PPC64_TOC16_LO_DS:
  7047. ref = Symbol::RELATIVE_REF;
  7048. break;
  7049. case elfcpp::R_POWERPC_GOT_TPREL16:
  7050. case elfcpp::R_POWERPC_TLS:
  7051. case elfcpp::R_PPC64_TLSGD:
  7052. case elfcpp::R_PPC64_TLSLD:
  7053. case elfcpp::R_PPC64_TPREL34:
  7054. case elfcpp::R_PPC64_DTPREL34:
  7055. case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
  7056. case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
  7057. case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
  7058. case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
  7059. ref = Symbol::TLS_REF;
  7060. break;
  7061. case elfcpp::R_POWERPC_COPY:
  7062. case elfcpp::R_POWERPC_GLOB_DAT:
  7063. case elfcpp::R_POWERPC_JMP_SLOT:
  7064. case elfcpp::R_POWERPC_RELATIVE:
  7065. case elfcpp::R_POWERPC_DTPMOD:
  7066. default:
  7067. // Not expected. We will give an error later.
  7068. break;
  7069. }
  7070. if (size == 64 && target->abiversion() < 2)
  7071. ref |= Symbol::FUNC_DESC_ABI;
  7072. return ref;
  7073. }
  7074. // Report an unsupported relocation against a local symbol.
  7075. template<int size, bool big_endian>
  7076. void
  7077. Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
  7078. Sized_relobj_file<size, big_endian>* object,
  7079. unsigned int r_type)
  7080. {
  7081. gold_error(_("%s: unsupported reloc %u against local symbol"),
  7082. object->name().c_str(), r_type);
  7083. }
  7084. // We are about to emit a dynamic relocation of type R_TYPE. If the
  7085. // dynamic linker does not support it, issue an error.
  7086. template<int size, bool big_endian>
  7087. void
  7088. Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
  7089. unsigned int r_type)
  7090. {
  7091. gold_assert(r_type != elfcpp::R_POWERPC_NONE);
  7092. // These are the relocation types supported by glibc for both 32-bit
  7093. // and 64-bit powerpc.
  7094. switch (r_type)
  7095. {
  7096. case elfcpp::R_POWERPC_NONE:
  7097. case elfcpp::R_POWERPC_RELATIVE:
  7098. case elfcpp::R_POWERPC_GLOB_DAT:
  7099. case elfcpp::R_POWERPC_DTPMOD:
  7100. case elfcpp::R_POWERPC_DTPREL:
  7101. case elfcpp::R_POWERPC_TPREL:
  7102. case elfcpp::R_POWERPC_JMP_SLOT:
  7103. case elfcpp::R_POWERPC_COPY:
  7104. case elfcpp::R_POWERPC_IRELATIVE:
  7105. case elfcpp::R_POWERPC_ADDR32:
  7106. case elfcpp::R_POWERPC_UADDR32:
  7107. case elfcpp::R_POWERPC_ADDR24:
  7108. case elfcpp::R_POWERPC_ADDR16:
  7109. case elfcpp::R_POWERPC_UADDR16:
  7110. case elfcpp::R_POWERPC_ADDR16_LO:
  7111. case elfcpp::R_POWERPC_ADDR16_HI:
  7112. case elfcpp::R_POWERPC_ADDR16_HA:
  7113. case elfcpp::R_POWERPC_ADDR14:
  7114. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  7115. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  7116. case elfcpp::R_POWERPC_REL32:
  7117. case elfcpp::R_POWERPC_TPREL16:
  7118. case elfcpp::R_POWERPC_TPREL16_LO:
  7119. case elfcpp::R_POWERPC_TPREL16_HI:
  7120. case elfcpp::R_POWERPC_TPREL16_HA:
  7121. return;
  7122. default:
  7123. break;
  7124. }
  7125. if (size == 64)
  7126. {
  7127. switch (r_type)
  7128. {
  7129. // These are the relocation types supported only on 64-bit.
  7130. case elfcpp::R_PPC64_ADDR64:
  7131. case elfcpp::R_PPC64_UADDR64:
  7132. case elfcpp::R_PPC64_JMP_IREL:
  7133. case elfcpp::R_PPC64_ADDR16_DS:
  7134. case elfcpp::R_PPC64_ADDR16_LO_DS:
  7135. case elfcpp::R_PPC64_ADDR16_HIGH:
  7136. case elfcpp::R_PPC64_ADDR16_HIGHA:
  7137. case elfcpp::R_PPC64_ADDR16_HIGHER:
  7138. case elfcpp::R_PPC64_ADDR16_HIGHEST:
  7139. case elfcpp::R_PPC64_ADDR16_HIGHERA:
  7140. case elfcpp::R_PPC64_ADDR16_HIGHESTA:
  7141. case elfcpp::R_PPC64_REL64:
  7142. case elfcpp::R_POWERPC_ADDR30:
  7143. case elfcpp::R_PPC64_TPREL16_DS:
  7144. case elfcpp::R_PPC64_TPREL16_LO_DS:
  7145. case elfcpp::R_PPC64_TPREL16_HIGH:
  7146. case elfcpp::R_PPC64_TPREL16_HIGHA:
  7147. case elfcpp::R_PPC64_TPREL16_HIGHER:
  7148. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  7149. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  7150. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  7151. return;
  7152. default:
  7153. break;
  7154. }
  7155. }
  7156. else
  7157. {
  7158. switch (r_type)
  7159. {
  7160. // These are the relocation types supported only on 32-bit.
  7161. // ??? glibc ld.so doesn't need to support these.
  7162. case elfcpp::R_POWERPC_REL24:
  7163. case elfcpp::R_POWERPC_DTPREL16:
  7164. case elfcpp::R_POWERPC_DTPREL16_LO:
  7165. case elfcpp::R_POWERPC_DTPREL16_HI:
  7166. case elfcpp::R_POWERPC_DTPREL16_HA:
  7167. return;
  7168. default:
  7169. break;
  7170. }
  7171. }
  7172. // This prevents us from issuing more than one error per reloc
  7173. // section. But we can still wind up issuing more than one
  7174. // error per object file.
  7175. if (this->issued_non_pic_error_)
  7176. return;
  7177. gold_assert(parameters->options().output_is_position_independent());
  7178. object->error(_("requires unsupported dynamic reloc; "
  7179. "recompile with -fPIC"));
  7180. this->issued_non_pic_error_ = true;
  7181. return;
  7182. }
  7183. // Return whether we need to make a PLT entry for a relocation of the
  7184. // given type against a STT_GNU_IFUNC symbol.
  7185. template<int size, bool big_endian>
  7186. bool
  7187. Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
  7188. Target_powerpc<size, big_endian>* target,
  7189. Sized_relobj_file<size, big_endian>* object,
  7190. unsigned int r_type,
  7191. bool report_err)
  7192. {
  7193. // In non-pic code any reference will resolve to the plt call stub
  7194. // for the ifunc symbol.
  7195. if ((size == 32 || target->abiversion() >= 2)
  7196. && !parameters->options().output_is_position_independent())
  7197. return true;
  7198. switch (r_type)
  7199. {
  7200. // Word size refs from data sections are OK, but don't need a PLT entry.
  7201. case elfcpp::R_POWERPC_ADDR32:
  7202. case elfcpp::R_POWERPC_UADDR32:
  7203. if (size == 32)
  7204. return false;
  7205. break;
  7206. case elfcpp::R_PPC64_ADDR64:
  7207. case elfcpp::R_PPC64_UADDR64:
  7208. if (size == 64)
  7209. return false;
  7210. break;
  7211. // GOT refs are good, but also don't need a PLT entry.
  7212. case elfcpp::R_POWERPC_GOT16:
  7213. case elfcpp::R_POWERPC_GOT16_LO:
  7214. case elfcpp::R_POWERPC_GOT16_HI:
  7215. case elfcpp::R_POWERPC_GOT16_HA:
  7216. case elfcpp::R_PPC64_GOT16_DS:
  7217. case elfcpp::R_PPC64_GOT16_LO_DS:
  7218. case elfcpp::R_PPC64_GOT_PCREL34:
  7219. return false;
  7220. // PLT relocs are OK and need a PLT entry.
  7221. case elfcpp::R_POWERPC_PLT16_LO:
  7222. case elfcpp::R_POWERPC_PLT16_HI:
  7223. case elfcpp::R_POWERPC_PLT16_HA:
  7224. case elfcpp::R_PPC64_PLT16_LO_DS:
  7225. case elfcpp::R_POWERPC_PLTSEQ:
  7226. case elfcpp::R_POWERPC_PLTCALL:
  7227. case elfcpp::R_PPC64_PLTSEQ_NOTOC:
  7228. case elfcpp::R_PPC64_PLTCALL_NOTOC:
  7229. case elfcpp::R_PPC64_PLT_PCREL34:
  7230. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  7231. return true;
  7232. break;
  7233. // Function calls are good, and these do need a PLT entry.
  7234. case elfcpp::R_PPC64_REL24_NOTOC:
  7235. if (size == 32)
  7236. break;
  7237. // Fall through.
  7238. case elfcpp::R_PPC64_REL24_P9NOTOC:
  7239. case elfcpp::R_POWERPC_ADDR24:
  7240. case elfcpp::R_POWERPC_ADDR14:
  7241. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  7242. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  7243. case elfcpp::R_POWERPC_REL24:
  7244. case elfcpp::R_PPC_PLTREL24:
  7245. case elfcpp::R_POWERPC_REL14:
  7246. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  7247. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  7248. return true;
  7249. default:
  7250. break;
  7251. }
  7252. // Anything else is a problem.
  7253. // If we are building a static executable, the libc startup function
  7254. // responsible for applying indirect function relocations is going
  7255. // to complain about the reloc type.
  7256. // If we are building a dynamic executable, we will have a text
  7257. // relocation. The dynamic loader will set the text segment
  7258. // writable and non-executable to apply text relocations. So we'll
  7259. // segfault when trying to run the indirection function to resolve
  7260. // the reloc.
  7261. if (report_err)
  7262. gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
  7263. object->name().c_str(), r_type);
  7264. return false;
  7265. }
  7266. // Return TRUE iff INSN is one we expect on a _LO variety toc/got
  7267. // reloc.
  7268. static bool
  7269. ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
  7270. {
  7271. return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
  7272. || (insn & (0x3f << 26)) == 14u << 26 /* addi */
  7273. || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
  7274. || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
  7275. || (insn & (0x3f << 26)) == 36u << 26 /* stw */
  7276. || (insn & (0x3f << 26)) == 38u << 26 /* stb */
  7277. || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
  7278. || (insn & (0x3f << 26)) == 42u << 26 /* lha */
  7279. || (insn & (0x3f << 26)) == 44u << 26 /* sth */
  7280. || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
  7281. || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
  7282. || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
  7283. || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
  7284. || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
  7285. || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
  7286. || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
  7287. || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
  7288. /* Exclude lfqu by testing reloc. If relocs are ever
  7289. defined for the reduced D field in psq_lu then those
  7290. will need testing too. */
  7291. && r_type != elfcpp::R_PPC64_TOC16_LO
  7292. && r_type != elfcpp::R_POWERPC_GOT16_LO)
  7293. || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
  7294. && (insn & 1) == 0)
  7295. || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
  7296. || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
  7297. /* Exclude stfqu. psq_stu as above for psq_lu. */
  7298. && r_type != elfcpp::R_PPC64_TOC16_LO
  7299. && r_type != elfcpp::R_POWERPC_GOT16_LO)
  7300. || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
  7301. && (insn & 1) == 0));
  7302. }
  7303. // Scan a relocation for a local symbol.
  7304. template<int size, bool big_endian>
  7305. inline void
  7306. Target_powerpc<size, big_endian>::Scan::local(
  7307. Symbol_table* symtab,
  7308. Layout* layout,
  7309. Target_powerpc<size, big_endian>* target,
  7310. Sized_relobj_file<size, big_endian>* object,
  7311. unsigned int data_shndx,
  7312. Output_section* output_section,
  7313. const elfcpp::Rela<size, big_endian>& reloc,
  7314. unsigned int r_type,
  7315. const elfcpp::Sym<size, big_endian>& lsym,
  7316. bool is_discarded)
  7317. {
  7318. Powerpc_relobj<size, big_endian>* ppc_object
  7319. = static_cast<Powerpc_relobj<size, big_endian>*>(object);
  7320. this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
  7321. if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
  7322. || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
  7323. {
  7324. this->expect_tls_get_addr_call();
  7325. tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
  7326. if (tls_type != tls::TLSOPT_NONE)
  7327. this->skip_next_tls_get_addr_call();
  7328. }
  7329. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
  7330. || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
  7331. {
  7332. this->expect_tls_get_addr_call();
  7333. tls::Tls_optimization tls_type = target->optimize_tls_ld();
  7334. if (tls_type != tls::TLSOPT_NONE)
  7335. this->skip_next_tls_get_addr_call();
  7336. }
  7337. if (is_discarded)
  7338. {
  7339. if (size == 64
  7340. && data_shndx == ppc_object->opd_shndx()
  7341. && r_type == elfcpp::R_PPC64_ADDR64)
  7342. ppc_object->set_opd_discard(reloc.get_r_offset());
  7343. return;
  7344. }
  7345. // A local STT_GNU_IFUNC symbol may require a PLT entry.
  7346. bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
  7347. if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
  7348. {
  7349. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7350. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  7351. r_type, r_sym, reloc.get_r_addend());
  7352. target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
  7353. }
  7354. switch (r_type)
  7355. {
  7356. case elfcpp::R_POWERPC_NONE:
  7357. case elfcpp::R_POWERPC_GNU_VTINHERIT:
  7358. case elfcpp::R_POWERPC_GNU_VTENTRY:
  7359. case elfcpp::R_POWERPC_TLS:
  7360. case elfcpp::R_PPC64_ENTRY:
  7361. case elfcpp::R_POWERPC_PLTSEQ:
  7362. case elfcpp::R_POWERPC_PLTCALL:
  7363. case elfcpp::R_PPC64_PLTSEQ_NOTOC:
  7364. case elfcpp::R_PPC64_PLTCALL_NOTOC:
  7365. case elfcpp::R_PPC64_PCREL_OPT:
  7366. case elfcpp::R_PPC64_ADDR16_HIGHER34:
  7367. case elfcpp::R_PPC64_ADDR16_HIGHERA34:
  7368. case elfcpp::R_PPC64_ADDR16_HIGHEST34:
  7369. case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
  7370. case elfcpp::R_PPC64_REL16_HIGHER34:
  7371. case elfcpp::R_PPC64_REL16_HIGHERA34:
  7372. case elfcpp::R_PPC64_REL16_HIGHEST34:
  7373. case elfcpp::R_PPC64_REL16_HIGHESTA34:
  7374. case elfcpp::R_PPC64_D34:
  7375. case elfcpp::R_PPC64_D34_LO:
  7376. case elfcpp::R_PPC64_D34_HI30:
  7377. case elfcpp::R_PPC64_D34_HA30:
  7378. case elfcpp::R_PPC64_D28:
  7379. case elfcpp::R_PPC64_PCREL34:
  7380. case elfcpp::R_PPC64_PCREL28:
  7381. case elfcpp::R_PPC64_TPREL34:
  7382. case elfcpp::R_PPC64_DTPREL34:
  7383. break;
  7384. case elfcpp::R_PPC64_TOC:
  7385. {
  7386. Output_data_got_powerpc<size, big_endian>* got
  7387. = target->got_section(symtab, layout, GOT_TYPE_SMALL);
  7388. if (parameters->options().output_is_position_independent())
  7389. {
  7390. Address off = reloc.get_r_offset();
  7391. if (size == 64
  7392. && target->abiversion() < 2
  7393. && data_shndx == ppc_object->opd_shndx()
  7394. && ppc_object->get_opd_discard(off - 8))
  7395. break;
  7396. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  7397. Address got_off = got->g_o_t();
  7398. rela_dyn->add_output_section_relative(got->output_section(),
  7399. elfcpp::R_POWERPC_RELATIVE,
  7400. output_section,
  7401. object, data_shndx, off,
  7402. got_off);
  7403. }
  7404. }
  7405. break;
  7406. case elfcpp::R_PPC64_ADDR64:
  7407. case elfcpp::R_PPC64_UADDR64:
  7408. case elfcpp::R_POWERPC_ADDR32:
  7409. case elfcpp::R_POWERPC_UADDR32:
  7410. case elfcpp::R_POWERPC_ADDR24:
  7411. case elfcpp::R_POWERPC_ADDR16:
  7412. case elfcpp::R_POWERPC_ADDR16_LO:
  7413. case elfcpp::R_POWERPC_ADDR16_HI:
  7414. case elfcpp::R_POWERPC_ADDR16_HA:
  7415. case elfcpp::R_POWERPC_UADDR16:
  7416. case elfcpp::R_PPC64_ADDR16_HIGH:
  7417. case elfcpp::R_PPC64_ADDR16_HIGHA:
  7418. case elfcpp::R_PPC64_ADDR16_HIGHER:
  7419. case elfcpp::R_PPC64_ADDR16_HIGHERA:
  7420. case elfcpp::R_PPC64_ADDR16_HIGHEST:
  7421. case elfcpp::R_PPC64_ADDR16_HIGHESTA:
  7422. case elfcpp::R_PPC64_ADDR16_DS:
  7423. case elfcpp::R_PPC64_ADDR16_LO_DS:
  7424. case elfcpp::R_POWERPC_ADDR14:
  7425. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  7426. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  7427. // If building a shared library (or a position-independent
  7428. // executable), we need to create a dynamic relocation for
  7429. // this location.
  7430. if (parameters->options().output_is_position_independent()
  7431. || (size == 64 && is_ifunc && target->abiversion() < 2))
  7432. {
  7433. Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
  7434. is_ifunc);
  7435. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7436. if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
  7437. || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
  7438. {
  7439. unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
  7440. : elfcpp::R_POWERPC_RELATIVE);
  7441. rela_dyn->add_local_relative(object, r_sym, dynrel,
  7442. output_section, data_shndx,
  7443. reloc.get_r_offset(),
  7444. reloc.get_r_addend(), false);
  7445. }
  7446. else if (lsym.get_st_type() != elfcpp::STT_SECTION)
  7447. {
  7448. check_non_pic(object, r_type);
  7449. rela_dyn->add_local(object, r_sym, r_type, output_section,
  7450. data_shndx, reloc.get_r_offset(),
  7451. reloc.get_r_addend());
  7452. }
  7453. else
  7454. {
  7455. gold_assert(lsym.get_st_value() == 0);
  7456. unsigned int shndx = lsym.get_st_shndx();
  7457. bool is_ordinary;
  7458. shndx = object->adjust_sym_shndx(r_sym, shndx,
  7459. &is_ordinary);
  7460. if (!is_ordinary)
  7461. object->error(_("section symbol %u has bad shndx %u"),
  7462. r_sym, shndx);
  7463. else
  7464. rela_dyn->add_local_section(object, shndx, r_type,
  7465. output_section, data_shndx,
  7466. reloc.get_r_offset());
  7467. }
  7468. }
  7469. break;
  7470. case elfcpp::R_PPC64_PLT_PCREL34:
  7471. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  7472. case elfcpp::R_POWERPC_PLT16_LO:
  7473. case elfcpp::R_POWERPC_PLT16_HI:
  7474. case elfcpp::R_POWERPC_PLT16_HA:
  7475. case elfcpp::R_PPC64_PLT16_LO_DS:
  7476. if (!is_ifunc)
  7477. {
  7478. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7479. target->make_local_plt_entry(symtab, layout, object, r_sym);
  7480. }
  7481. break;
  7482. case elfcpp::R_PPC64_REL24_NOTOC:
  7483. if (size == 32)
  7484. break;
  7485. // Fall through.
  7486. case elfcpp::R_PPC64_REL24_P9NOTOC:
  7487. case elfcpp::R_POWERPC_REL24:
  7488. case elfcpp::R_PPC_PLTREL24:
  7489. case elfcpp::R_PPC_LOCAL24PC:
  7490. case elfcpp::R_POWERPC_REL14:
  7491. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  7492. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  7493. if (!is_ifunc)
  7494. {
  7495. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7496. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  7497. r_type, r_sym, reloc.get_r_addend());
  7498. }
  7499. break;
  7500. case elfcpp::R_PPC64_TOCSAVE:
  7501. // R_PPC64_TOCSAVE follows a call instruction to indicate the
  7502. // caller has already saved r2 and thus a plt call stub need not
  7503. // save r2.
  7504. if (size == 64
  7505. && target->mark_pltcall(ppc_object, data_shndx,
  7506. reloc.get_r_offset() - 4, symtab))
  7507. {
  7508. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7509. unsigned int shndx = lsym.get_st_shndx();
  7510. bool is_ordinary;
  7511. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  7512. if (!is_ordinary)
  7513. object->error(_("tocsave symbol %u has bad shndx %u"),
  7514. r_sym, shndx);
  7515. else
  7516. target->add_tocsave(ppc_object, shndx,
  7517. lsym.get_st_value() + reloc.get_r_addend());
  7518. }
  7519. break;
  7520. case elfcpp::R_PPC64_REL64:
  7521. case elfcpp::R_POWERPC_REL32:
  7522. case elfcpp::R_POWERPC_REL16:
  7523. case elfcpp::R_POWERPC_REL16_LO:
  7524. case elfcpp::R_POWERPC_REL16_HI:
  7525. case elfcpp::R_POWERPC_REL16_HA:
  7526. case elfcpp::R_POWERPC_REL16DX_HA:
  7527. case elfcpp::R_PPC64_REL16_HIGH:
  7528. case elfcpp::R_PPC64_REL16_HIGHA:
  7529. case elfcpp::R_PPC64_REL16_HIGHER:
  7530. case elfcpp::R_PPC64_REL16_HIGHERA:
  7531. case elfcpp::R_PPC64_REL16_HIGHEST:
  7532. case elfcpp::R_PPC64_REL16_HIGHESTA:
  7533. case elfcpp::R_POWERPC_SECTOFF:
  7534. case elfcpp::R_POWERPC_SECTOFF_LO:
  7535. case elfcpp::R_POWERPC_SECTOFF_HI:
  7536. case elfcpp::R_POWERPC_SECTOFF_HA:
  7537. case elfcpp::R_PPC64_SECTOFF_DS:
  7538. case elfcpp::R_PPC64_SECTOFF_LO_DS:
  7539. case elfcpp::R_POWERPC_TPREL16:
  7540. case elfcpp::R_POWERPC_TPREL16_LO:
  7541. case elfcpp::R_POWERPC_TPREL16_HI:
  7542. case elfcpp::R_POWERPC_TPREL16_HA:
  7543. case elfcpp::R_PPC64_TPREL16_DS:
  7544. case elfcpp::R_PPC64_TPREL16_LO_DS:
  7545. case elfcpp::R_PPC64_TPREL16_HIGH:
  7546. case elfcpp::R_PPC64_TPREL16_HIGHA:
  7547. case elfcpp::R_PPC64_TPREL16_HIGHER:
  7548. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  7549. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  7550. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  7551. case elfcpp::R_POWERPC_DTPREL16:
  7552. case elfcpp::R_POWERPC_DTPREL16_LO:
  7553. case elfcpp::R_POWERPC_DTPREL16_HI:
  7554. case elfcpp::R_POWERPC_DTPREL16_HA:
  7555. case elfcpp::R_PPC64_DTPREL16_DS:
  7556. case elfcpp::R_PPC64_DTPREL16_LO_DS:
  7557. case elfcpp::R_PPC64_DTPREL16_HIGH:
  7558. case elfcpp::R_PPC64_DTPREL16_HIGHA:
  7559. case elfcpp::R_PPC64_DTPREL16_HIGHER:
  7560. case elfcpp::R_PPC64_DTPREL16_HIGHERA:
  7561. case elfcpp::R_PPC64_DTPREL16_HIGHEST:
  7562. case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
  7563. case elfcpp::R_PPC64_TLSGD:
  7564. case elfcpp::R_PPC64_TLSLD:
  7565. case elfcpp::R_PPC64_ADDR64_LOCAL:
  7566. break;
  7567. case elfcpp::R_PPC64_GOT_PCREL34:
  7568. case elfcpp::R_POWERPC_GOT16:
  7569. case elfcpp::R_POWERPC_GOT16_LO:
  7570. case elfcpp::R_POWERPC_GOT16_HI:
  7571. case elfcpp::R_POWERPC_GOT16_HA:
  7572. case elfcpp::R_PPC64_GOT16_DS:
  7573. case elfcpp::R_PPC64_GOT16_LO_DS:
  7574. {
  7575. // The symbol requires a GOT entry.
  7576. Got_type got_type = ((size == 32
  7577. || r_type == elfcpp::R_POWERPC_GOT16
  7578. || r_type == elfcpp::R_PPC64_GOT16_DS)
  7579. ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
  7580. Output_data_got_powerpc<size, big_endian>* got
  7581. = target->got_section(symtab, layout, got_type);
  7582. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7583. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  7584. if (!parameters->options().output_is_position_independent())
  7585. {
  7586. if (is_ifunc
  7587. && (size == 32 || target->abiversion() >= 2))
  7588. got->add_local_plt(object, r_sym, got_type, addend);
  7589. else
  7590. got->add_local(object, r_sym, got_type, addend);
  7591. }
  7592. else if (!object->local_has_got_offset(r_sym, got_type, addend))
  7593. {
  7594. // If we are generating a shared object or a pie, this
  7595. // symbol's GOT entry will be set by a dynamic relocation.
  7596. unsigned int off;
  7597. off = got->add_constant(0);
  7598. object->set_local_got_offset(r_sym, got_type, off, addend);
  7599. Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
  7600. is_ifunc);
  7601. unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
  7602. : elfcpp::R_POWERPC_RELATIVE);
  7603. rela_dyn->add_local_relative(object, r_sym, dynrel,
  7604. got, off, addend, false);
  7605. }
  7606. }
  7607. break;
  7608. case elfcpp::R_PPC64_TOC16:
  7609. case elfcpp::R_PPC64_TOC16_LO:
  7610. case elfcpp::R_PPC64_TOC16_HI:
  7611. case elfcpp::R_PPC64_TOC16_HA:
  7612. case elfcpp::R_PPC64_TOC16_DS:
  7613. case elfcpp::R_PPC64_TOC16_LO_DS:
  7614. // We need a GOT section.
  7615. target->got_section(symtab, layout, GOT_TYPE_SMALL);
  7616. break;
  7617. case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
  7618. case elfcpp::R_POWERPC_GOT_TLSGD16:
  7619. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  7620. case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
  7621. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  7622. {
  7623. tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
  7624. if (tls_type == tls::TLSOPT_NONE)
  7625. {
  7626. Got_type got_type = ((size == 32
  7627. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
  7628. ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
  7629. Output_data_got_powerpc<size, big_endian>* got
  7630. = target->got_section(symtab, layout, got_type);
  7631. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7632. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  7633. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  7634. got->add_local_tls_pair(object, r_sym, got_type,
  7635. rela_dyn, elfcpp::R_POWERPC_DTPMOD,
  7636. addend);
  7637. }
  7638. else if (tls_type == tls::TLSOPT_TO_LE)
  7639. {
  7640. // no GOT relocs needed for Local Exec.
  7641. }
  7642. else
  7643. gold_unreachable();
  7644. }
  7645. break;
  7646. case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
  7647. case elfcpp::R_POWERPC_GOT_TLSLD16:
  7648. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  7649. case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
  7650. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  7651. {
  7652. tls::Tls_optimization tls_type = target->optimize_tls_ld();
  7653. if (tls_type == tls::TLSOPT_NONE)
  7654. target->tlsld_got_offset(symtab, layout, object);
  7655. else if (tls_type == tls::TLSOPT_TO_LE)
  7656. {
  7657. // no GOT relocs needed for Local Exec.
  7658. if (parameters->options().emit_relocs())
  7659. {
  7660. Output_section* os = layout->tls_segment()->first_section();
  7661. gold_assert(os != NULL);
  7662. os->set_needs_symtab_index();
  7663. }
  7664. }
  7665. else
  7666. gold_unreachable();
  7667. }
  7668. break;
  7669. case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
  7670. case elfcpp::R_POWERPC_GOT_DTPREL16:
  7671. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  7672. case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
  7673. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  7674. {
  7675. Got_type got_type = ((size == 32
  7676. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
  7677. ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
  7678. Output_data_got_powerpc<size, big_endian>* got
  7679. = target->got_section(symtab, layout, got_type);
  7680. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7681. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  7682. got->add_local_tls(object, r_sym, got_type, addend);
  7683. }
  7684. break;
  7685. case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
  7686. case elfcpp::R_POWERPC_GOT_TPREL16:
  7687. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  7688. case elfcpp::R_POWERPC_GOT_TPREL16_HI:
  7689. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  7690. {
  7691. tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
  7692. if (tls_type == tls::TLSOPT_NONE)
  7693. {
  7694. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7695. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  7696. Got_type got_type = ((size == 32
  7697. || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
  7698. ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
  7699. if (!object->local_has_got_offset(r_sym, got_type, addend))
  7700. {
  7701. Output_data_got_powerpc<size, big_endian>* got
  7702. = target->got_section(symtab, layout, got_type);
  7703. unsigned int off = got->add_constant(0);
  7704. object->set_local_got_offset(r_sym, got_type, off, addend);
  7705. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  7706. rela_dyn->add_symbolless_local_addend(object, r_sym,
  7707. elfcpp::R_POWERPC_TPREL,
  7708. got, off, addend);
  7709. }
  7710. }
  7711. else if (tls_type == tls::TLSOPT_TO_LE)
  7712. {
  7713. // no GOT relocs needed for Local Exec.
  7714. }
  7715. else
  7716. gold_unreachable();
  7717. }
  7718. break;
  7719. default:
  7720. unsupported_reloc_local(object, r_type);
  7721. break;
  7722. }
  7723. if (size == 64
  7724. && parameters->options().toc_optimize())
  7725. {
  7726. if (data_shndx == ppc_object->toc_shndx())
  7727. {
  7728. bool ok = true;
  7729. if (r_type != elfcpp::R_PPC64_ADDR64
  7730. || (is_ifunc && target->abiversion() < 2))
  7731. ok = false;
  7732. else if (parameters->options().output_is_position_independent())
  7733. {
  7734. if (is_ifunc)
  7735. ok = false;
  7736. else
  7737. {
  7738. unsigned int shndx = lsym.get_st_shndx();
  7739. if (shndx >= elfcpp::SHN_LORESERVE
  7740. && shndx != elfcpp::SHN_XINDEX)
  7741. ok = false;
  7742. }
  7743. }
  7744. if (!ok)
  7745. ppc_object->set_no_toc_opt(reloc.get_r_offset());
  7746. }
  7747. enum {no_check, check_lo, check_ha} insn_check;
  7748. switch (r_type)
  7749. {
  7750. default:
  7751. insn_check = no_check;
  7752. break;
  7753. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  7754. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  7755. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  7756. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  7757. case elfcpp::R_POWERPC_GOT16_HA:
  7758. case elfcpp::R_PPC64_TOC16_HA:
  7759. insn_check = check_ha;
  7760. break;
  7761. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  7762. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  7763. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  7764. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  7765. case elfcpp::R_POWERPC_GOT16_LO:
  7766. case elfcpp::R_PPC64_GOT16_LO_DS:
  7767. case elfcpp::R_PPC64_TOC16_LO:
  7768. case elfcpp::R_PPC64_TOC16_LO_DS:
  7769. insn_check = check_lo;
  7770. break;
  7771. }
  7772. section_size_type slen;
  7773. const unsigned char* view = NULL;
  7774. if (insn_check != no_check)
  7775. {
  7776. view = ppc_object->section_contents(data_shndx, &slen, false);
  7777. section_size_type off =
  7778. convert_to_section_size_type(reloc.get_r_offset()) & -4;
  7779. if (off < slen)
  7780. {
  7781. uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
  7782. if (insn_check == check_lo
  7783. ? !ok_lo_toc_insn(insn, r_type)
  7784. : ((insn & ((0x3f << 26) | 0x1f << 16))
  7785. != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
  7786. {
  7787. ppc_object->set_no_toc_opt();
  7788. gold_warning(_("%s: toc optimization is not supported "
  7789. "for %#08x instruction"),
  7790. ppc_object->name().c_str(), insn);
  7791. }
  7792. }
  7793. }
  7794. switch (r_type)
  7795. {
  7796. default:
  7797. break;
  7798. case elfcpp::R_PPC64_TOC16:
  7799. case elfcpp::R_PPC64_TOC16_LO:
  7800. case elfcpp::R_PPC64_TOC16_HI:
  7801. case elfcpp::R_PPC64_TOC16_HA:
  7802. case elfcpp::R_PPC64_TOC16_DS:
  7803. case elfcpp::R_PPC64_TOC16_LO_DS:
  7804. unsigned int shndx = lsym.get_st_shndx();
  7805. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7806. bool is_ordinary;
  7807. shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  7808. if (is_ordinary && shndx == ppc_object->toc_shndx())
  7809. {
  7810. Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
  7811. if (dst_off < ppc_object->section_size(shndx))
  7812. {
  7813. bool ok = false;
  7814. if (r_type == elfcpp::R_PPC64_TOC16_HA)
  7815. ok = true;
  7816. else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
  7817. {
  7818. // Need to check that the insn is a ld
  7819. if (!view)
  7820. view = ppc_object->section_contents(data_shndx,
  7821. &slen,
  7822. false);
  7823. section_size_type off =
  7824. (convert_to_section_size_type(reloc.get_r_offset())
  7825. + (big_endian ? -2 : 3));
  7826. if (off < slen
  7827. && (view[off] & (0x3f << 2)) == 58u << 2)
  7828. ok = true;
  7829. }
  7830. if (!ok)
  7831. ppc_object->set_no_toc_opt(dst_off);
  7832. }
  7833. }
  7834. break;
  7835. }
  7836. }
  7837. if (size == 32)
  7838. {
  7839. switch (r_type)
  7840. {
  7841. case elfcpp::R_POWERPC_REL32:
  7842. if (ppc_object->got2_shndx() != 0
  7843. && parameters->options().output_is_position_independent())
  7844. {
  7845. unsigned int shndx = lsym.get_st_shndx();
  7846. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  7847. bool is_ordinary;
  7848. shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  7849. if (is_ordinary && shndx == ppc_object->got2_shndx()
  7850. && (ppc_object->section_flags(data_shndx)
  7851. & elfcpp::SHF_EXECINSTR) != 0)
  7852. gold_error(_("%s: unsupported -mbss-plt code"),
  7853. ppc_object->name().c_str());
  7854. }
  7855. break;
  7856. default:
  7857. break;
  7858. }
  7859. }
  7860. switch (r_type)
  7861. {
  7862. case elfcpp::R_POWERPC_GOT_TLSLD16:
  7863. case elfcpp::R_POWERPC_GOT_TLSGD16:
  7864. case elfcpp::R_POWERPC_GOT_TPREL16:
  7865. case elfcpp::R_POWERPC_GOT_DTPREL16:
  7866. case elfcpp::R_POWERPC_GOT16:
  7867. case elfcpp::R_PPC64_GOT16_DS:
  7868. case elfcpp::R_PPC64_TOC16:
  7869. case elfcpp::R_PPC64_TOC16_DS:
  7870. ppc_object->set_has_small_toc_reloc();
  7871. break;
  7872. default:
  7873. break;
  7874. }
  7875. switch (r_type)
  7876. {
  7877. case elfcpp::R_PPC64_TPREL16_DS:
  7878. case elfcpp::R_PPC64_TPREL16_LO_DS:
  7879. case elfcpp::R_PPC64_TPREL16_HIGH:
  7880. case elfcpp::R_PPC64_TPREL16_HIGHA:
  7881. case elfcpp::R_PPC64_TPREL16_HIGHER:
  7882. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  7883. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  7884. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  7885. case elfcpp::R_PPC64_TPREL34:
  7886. if (size != 64)
  7887. break;
  7888. // Fall through.
  7889. case elfcpp::R_POWERPC_TPREL16:
  7890. case elfcpp::R_POWERPC_TPREL16_LO:
  7891. case elfcpp::R_POWERPC_TPREL16_HI:
  7892. case elfcpp::R_POWERPC_TPREL16_HA:
  7893. layout->set_has_static_tls();
  7894. break;
  7895. default:
  7896. break;
  7897. }
  7898. switch (r_type)
  7899. {
  7900. case elfcpp::R_POWERPC_TPREL16_HA:
  7901. if (target->tprel_opt())
  7902. {
  7903. section_size_type slen;
  7904. const unsigned char* view = NULL;
  7905. view = ppc_object->section_contents(data_shndx, &slen, false);
  7906. section_size_type off
  7907. = convert_to_section_size_type(reloc.get_r_offset()) & -4;
  7908. if (off < slen)
  7909. {
  7910. uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
  7911. if ((insn & ((0x3fu << 26) | 0x1f << 16))
  7912. != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
  7913. target->set_no_tprel_opt();
  7914. }
  7915. }
  7916. break;
  7917. case elfcpp::R_PPC64_TPREL16_HIGH:
  7918. case elfcpp::R_PPC64_TPREL16_HIGHA:
  7919. case elfcpp::R_PPC64_TPREL16_HIGHER:
  7920. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  7921. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  7922. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  7923. if (size != 64)
  7924. break;
  7925. // Fall through.
  7926. case elfcpp::R_POWERPC_TPREL16_HI:
  7927. target->set_no_tprel_opt();
  7928. break;
  7929. default:
  7930. break;
  7931. }
  7932. switch (r_type)
  7933. {
  7934. case elfcpp::R_PPC64_D34:
  7935. case elfcpp::R_PPC64_D34_LO:
  7936. case elfcpp::R_PPC64_D34_HI30:
  7937. case elfcpp::R_PPC64_D34_HA30:
  7938. case elfcpp::R_PPC64_D28:
  7939. case elfcpp::R_PPC64_PCREL34:
  7940. case elfcpp::R_PPC64_PCREL28:
  7941. case elfcpp::R_PPC64_TPREL34:
  7942. case elfcpp::R_PPC64_DTPREL34:
  7943. case elfcpp::R_PPC64_PLT_PCREL34:
  7944. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  7945. case elfcpp::R_PPC64_GOT_PCREL34:
  7946. case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
  7947. case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
  7948. case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
  7949. case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
  7950. target->set_power10_relocs();
  7951. break;
  7952. default:
  7953. break;
  7954. }
  7955. }
  7956. // Report an unsupported relocation against a global symbol.
  7957. template<int size, bool big_endian>
  7958. void
  7959. Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
  7960. Sized_relobj_file<size, big_endian>* object,
  7961. unsigned int r_type,
  7962. Symbol* gsym)
  7963. {
  7964. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  7965. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  7966. }
  7967. // Scan a relocation for a global symbol.
  7968. template<int size, bool big_endian>
  7969. inline void
  7970. Target_powerpc<size, big_endian>::Scan::global(
  7971. Symbol_table* symtab,
  7972. Layout* layout,
  7973. Target_powerpc<size, big_endian>* target,
  7974. Sized_relobj_file<size, big_endian>* object,
  7975. unsigned int data_shndx,
  7976. Output_section* output_section,
  7977. const elfcpp::Rela<size, big_endian>& reloc,
  7978. unsigned int r_type,
  7979. Symbol* gsym)
  7980. {
  7981. Powerpc_relobj<size, big_endian>* ppc_object
  7982. = static_cast<Powerpc_relobj<size, big_endian>*>(object);
  7983. switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
  7984. {
  7985. case Track_tls::SKIP:
  7986. return;
  7987. default:
  7988. break;
  7989. }
  7990. if (target->replace_tls_get_addr(gsym))
  7991. // Change a __tls_get_addr reference to __tls_get_addr_opt
  7992. // so dynamic relocs are emitted against the latter symbol.
  7993. gsym = target->tls_get_addr_opt();
  7994. if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
  7995. || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
  7996. {
  7997. this->expect_tls_get_addr_call();
  7998. bool final = gsym->final_value_is_known();
  7999. tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
  8000. if (tls_type != tls::TLSOPT_NONE)
  8001. this->skip_next_tls_get_addr_call();
  8002. }
  8003. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
  8004. || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
  8005. {
  8006. this->expect_tls_get_addr_call();
  8007. tls::Tls_optimization tls_type = target->optimize_tls_ld();
  8008. if (tls_type != tls::TLSOPT_NONE)
  8009. this->skip_next_tls_get_addr_call();
  8010. }
  8011. // A STT_GNU_IFUNC symbol may require a PLT entry.
  8012. bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
  8013. bool pushed_ifunc = false;
  8014. if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
  8015. {
  8016. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  8017. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  8018. r_type, r_sym, reloc.get_r_addend());
  8019. target->make_plt_entry(symtab, layout, gsym);
  8020. pushed_ifunc = true;
  8021. }
  8022. switch (r_type)
  8023. {
  8024. case elfcpp::R_POWERPC_NONE:
  8025. case elfcpp::R_POWERPC_GNU_VTINHERIT:
  8026. case elfcpp::R_POWERPC_GNU_VTENTRY:
  8027. case elfcpp::R_PPC_LOCAL24PC:
  8028. case elfcpp::R_POWERPC_TLS:
  8029. case elfcpp::R_PPC64_ENTRY:
  8030. case elfcpp::R_POWERPC_PLTSEQ:
  8031. case elfcpp::R_POWERPC_PLTCALL:
  8032. case elfcpp::R_PPC64_PLTSEQ_NOTOC:
  8033. case elfcpp::R_PPC64_PLTCALL_NOTOC:
  8034. case elfcpp::R_PPC64_PCREL_OPT:
  8035. case elfcpp::R_PPC64_ADDR16_HIGHER34:
  8036. case elfcpp::R_PPC64_ADDR16_HIGHERA34:
  8037. case elfcpp::R_PPC64_ADDR16_HIGHEST34:
  8038. case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
  8039. case elfcpp::R_PPC64_REL16_HIGHER34:
  8040. case elfcpp::R_PPC64_REL16_HIGHERA34:
  8041. case elfcpp::R_PPC64_REL16_HIGHEST34:
  8042. case elfcpp::R_PPC64_REL16_HIGHESTA34:
  8043. case elfcpp::R_PPC64_D34:
  8044. case elfcpp::R_PPC64_D34_LO:
  8045. case elfcpp::R_PPC64_D34_HI30:
  8046. case elfcpp::R_PPC64_D34_HA30:
  8047. case elfcpp::R_PPC64_D28:
  8048. case elfcpp::R_PPC64_PCREL34:
  8049. case elfcpp::R_PPC64_PCREL28:
  8050. case elfcpp::R_PPC64_TPREL34:
  8051. case elfcpp::R_PPC64_DTPREL34:
  8052. break;
  8053. case elfcpp::R_PPC64_TOC:
  8054. {
  8055. Output_data_got_powerpc<size, big_endian>* got
  8056. = target->got_section(symtab, layout, GOT_TYPE_SMALL);
  8057. if (parameters->options().output_is_position_independent())
  8058. {
  8059. Address off = reloc.get_r_offset();
  8060. if (size == 64
  8061. && data_shndx == ppc_object->opd_shndx()
  8062. && ppc_object->get_opd_discard(off - 8))
  8063. break;
  8064. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  8065. Address got_off = got->g_o_t();
  8066. rela_dyn->add_output_section_relative(got->output_section(),
  8067. elfcpp::R_POWERPC_RELATIVE,
  8068. output_section,
  8069. object, data_shndx, off,
  8070. got_off);
  8071. }
  8072. }
  8073. break;
  8074. case elfcpp::R_PPC64_ADDR64:
  8075. if (size == 64
  8076. && target->abiversion() < 2
  8077. && data_shndx == ppc_object->opd_shndx()
  8078. && (gsym->is_defined_in_discarded_section()
  8079. || gsym->object() != object))
  8080. {
  8081. ppc_object->set_opd_discard(reloc.get_r_offset());
  8082. break;
  8083. }
  8084. // Fall through.
  8085. case elfcpp::R_PPC64_UADDR64:
  8086. case elfcpp::R_POWERPC_ADDR32:
  8087. case elfcpp::R_POWERPC_UADDR32:
  8088. case elfcpp::R_POWERPC_ADDR24:
  8089. case elfcpp::R_POWERPC_ADDR16:
  8090. case elfcpp::R_POWERPC_ADDR16_LO:
  8091. case elfcpp::R_POWERPC_ADDR16_HI:
  8092. case elfcpp::R_POWERPC_ADDR16_HA:
  8093. case elfcpp::R_POWERPC_UADDR16:
  8094. case elfcpp::R_PPC64_ADDR16_HIGH:
  8095. case elfcpp::R_PPC64_ADDR16_HIGHA:
  8096. case elfcpp::R_PPC64_ADDR16_HIGHER:
  8097. case elfcpp::R_PPC64_ADDR16_HIGHERA:
  8098. case elfcpp::R_PPC64_ADDR16_HIGHEST:
  8099. case elfcpp::R_PPC64_ADDR16_HIGHESTA:
  8100. case elfcpp::R_PPC64_ADDR16_DS:
  8101. case elfcpp::R_PPC64_ADDR16_LO_DS:
  8102. case elfcpp::R_POWERPC_ADDR14:
  8103. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  8104. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  8105. {
  8106. // Make a PLT entry if necessary.
  8107. if (gsym->needs_plt_entry())
  8108. {
  8109. // Since this is not a PC-relative relocation, we may be
  8110. // taking the address of a function. In that case we need to
  8111. // set the entry in the dynamic symbol table to the address of
  8112. // the PLT call stub.
  8113. bool need_ifunc_plt = false;
  8114. if ((size == 32 || target->abiversion() >= 2)
  8115. && gsym->is_from_dynobj()
  8116. && !parameters->options().output_is_position_independent())
  8117. {
  8118. gsym->set_needs_dynsym_value();
  8119. need_ifunc_plt = true;
  8120. }
  8121. if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
  8122. {
  8123. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  8124. target->push_branch(ppc_object, data_shndx,
  8125. reloc.get_r_offset(), r_type, r_sym,
  8126. reloc.get_r_addend());
  8127. target->make_plt_entry(symtab, layout, gsym);
  8128. }
  8129. }
  8130. // Make a dynamic relocation if necessary.
  8131. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
  8132. || (size == 64 && is_ifunc && target->abiversion() < 2))
  8133. {
  8134. if (!parameters->options().output_is_position_independent()
  8135. && gsym->may_need_copy_reloc())
  8136. {
  8137. target->copy_reloc(symtab, layout, object,
  8138. data_shndx, output_section, gsym, reloc);
  8139. }
  8140. else if ((((size == 32
  8141. && r_type == elfcpp::R_POWERPC_ADDR32)
  8142. || (size == 64
  8143. && r_type == elfcpp::R_PPC64_ADDR64
  8144. && target->abiversion() >= 2))
  8145. && gsym->can_use_relative_reloc(false)
  8146. && !(gsym->visibility() == elfcpp::STV_PROTECTED
  8147. && parameters->options().shared()))
  8148. || (size == 64
  8149. && r_type == elfcpp::R_PPC64_ADDR64
  8150. && target->abiversion() < 2
  8151. && (gsym->can_use_relative_reloc(false)
  8152. || data_shndx == ppc_object->opd_shndx())))
  8153. {
  8154. Reloc_section* rela_dyn
  8155. = target->rela_dyn_section(symtab, layout, is_ifunc);
  8156. unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
  8157. : elfcpp::R_POWERPC_RELATIVE);
  8158. rela_dyn->add_symbolless_global_addend(
  8159. gsym, dynrel, output_section, object, data_shndx,
  8160. reloc.get_r_offset(), reloc.get_r_addend());
  8161. }
  8162. else
  8163. {
  8164. Reloc_section* rela_dyn
  8165. = target->rela_dyn_section(symtab, layout, is_ifunc);
  8166. check_non_pic(object, r_type);
  8167. rela_dyn->add_global(gsym, r_type, output_section,
  8168. object, data_shndx,
  8169. reloc.get_r_offset(),
  8170. reloc.get_r_addend());
  8171. if (size == 64
  8172. && parameters->options().toc_optimize()
  8173. && data_shndx == ppc_object->toc_shndx())
  8174. ppc_object->set_no_toc_opt(reloc.get_r_offset());
  8175. }
  8176. }
  8177. }
  8178. break;
  8179. case elfcpp::R_PPC64_PLT_PCREL34:
  8180. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  8181. case elfcpp::R_POWERPC_PLT16_LO:
  8182. case elfcpp::R_POWERPC_PLT16_HI:
  8183. case elfcpp::R_POWERPC_PLT16_HA:
  8184. case elfcpp::R_PPC64_PLT16_LO_DS:
  8185. if (!pushed_ifunc)
  8186. {
  8187. if (!parameters->doing_static_link())
  8188. target->make_plt_entry(symtab, layout, gsym);
  8189. else
  8190. target->make_local_plt_entry(symtab, layout, gsym);
  8191. }
  8192. break;
  8193. case elfcpp::R_PPC64_REL24_NOTOC:
  8194. if (size == 32)
  8195. break;
  8196. // Fall through.
  8197. case elfcpp::R_PPC64_REL24_P9NOTOC:
  8198. case elfcpp::R_PPC_PLTREL24:
  8199. case elfcpp::R_POWERPC_REL24:
  8200. if (!is_ifunc)
  8201. {
  8202. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  8203. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  8204. r_type, r_sym, reloc.get_r_addend());
  8205. if (gsym->needs_plt_entry()
  8206. || (!gsym->final_value_is_known()
  8207. && (gsym->is_undefined()
  8208. || gsym->is_from_dynobj()
  8209. || gsym->is_preemptible())))
  8210. target->make_plt_entry(symtab, layout, gsym);
  8211. }
  8212. // Fall through.
  8213. case elfcpp::R_PPC64_REL64:
  8214. case elfcpp::R_POWERPC_REL32:
  8215. // Make a dynamic relocation if necessary.
  8216. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
  8217. {
  8218. if (!parameters->options().output_is_position_independent()
  8219. && gsym->may_need_copy_reloc())
  8220. {
  8221. target->copy_reloc(symtab, layout, object,
  8222. data_shndx, output_section, gsym,
  8223. reloc);
  8224. }
  8225. else
  8226. {
  8227. Reloc_section* rela_dyn
  8228. = target->rela_dyn_section(symtab, layout, is_ifunc);
  8229. check_non_pic(object, r_type);
  8230. rela_dyn->add_global(gsym, r_type, output_section, object,
  8231. data_shndx, reloc.get_r_offset(),
  8232. reloc.get_r_addend());
  8233. }
  8234. }
  8235. break;
  8236. case elfcpp::R_POWERPC_REL14:
  8237. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  8238. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  8239. if (!is_ifunc)
  8240. {
  8241. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  8242. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  8243. r_type, r_sym, reloc.get_r_addend());
  8244. }
  8245. break;
  8246. case elfcpp::R_PPC64_TOCSAVE:
  8247. // R_PPC64_TOCSAVE follows a call instruction to indicate the
  8248. // caller has already saved r2 and thus a plt call stub need not
  8249. // save r2.
  8250. if (size == 64
  8251. && target->mark_pltcall(ppc_object, data_shndx,
  8252. reloc.get_r_offset() - 4, symtab))
  8253. {
  8254. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  8255. bool is_ordinary;
  8256. unsigned int shndx = gsym->shndx(&is_ordinary);
  8257. if (!is_ordinary)
  8258. object->error(_("tocsave symbol %u has bad shndx %u"),
  8259. r_sym, shndx);
  8260. else
  8261. {
  8262. Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
  8263. target->add_tocsave(ppc_object, shndx,
  8264. sym->value() + reloc.get_r_addend());
  8265. }
  8266. }
  8267. break;
  8268. case elfcpp::R_POWERPC_REL16:
  8269. case elfcpp::R_POWERPC_REL16_LO:
  8270. case elfcpp::R_POWERPC_REL16_HI:
  8271. case elfcpp::R_POWERPC_REL16_HA:
  8272. case elfcpp::R_POWERPC_REL16DX_HA:
  8273. case elfcpp::R_PPC64_REL16_HIGH:
  8274. case elfcpp::R_PPC64_REL16_HIGHA:
  8275. case elfcpp::R_PPC64_REL16_HIGHER:
  8276. case elfcpp::R_PPC64_REL16_HIGHERA:
  8277. case elfcpp::R_PPC64_REL16_HIGHEST:
  8278. case elfcpp::R_PPC64_REL16_HIGHESTA:
  8279. case elfcpp::R_POWERPC_SECTOFF:
  8280. case elfcpp::R_POWERPC_SECTOFF_LO:
  8281. case elfcpp::R_POWERPC_SECTOFF_HI:
  8282. case elfcpp::R_POWERPC_SECTOFF_HA:
  8283. case elfcpp::R_PPC64_SECTOFF_DS:
  8284. case elfcpp::R_PPC64_SECTOFF_LO_DS:
  8285. case elfcpp::R_POWERPC_TPREL16:
  8286. case elfcpp::R_POWERPC_TPREL16_LO:
  8287. case elfcpp::R_POWERPC_TPREL16_HI:
  8288. case elfcpp::R_POWERPC_TPREL16_HA:
  8289. case elfcpp::R_PPC64_TPREL16_DS:
  8290. case elfcpp::R_PPC64_TPREL16_LO_DS:
  8291. case elfcpp::R_PPC64_TPREL16_HIGH:
  8292. case elfcpp::R_PPC64_TPREL16_HIGHA:
  8293. case elfcpp::R_PPC64_TPREL16_HIGHER:
  8294. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  8295. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  8296. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  8297. case elfcpp::R_POWERPC_DTPREL16:
  8298. case elfcpp::R_POWERPC_DTPREL16_LO:
  8299. case elfcpp::R_POWERPC_DTPREL16_HI:
  8300. case elfcpp::R_POWERPC_DTPREL16_HA:
  8301. case elfcpp::R_PPC64_DTPREL16_DS:
  8302. case elfcpp::R_PPC64_DTPREL16_LO_DS:
  8303. case elfcpp::R_PPC64_DTPREL16_HIGH:
  8304. case elfcpp::R_PPC64_DTPREL16_HIGHA:
  8305. case elfcpp::R_PPC64_DTPREL16_HIGHER:
  8306. case elfcpp::R_PPC64_DTPREL16_HIGHERA:
  8307. case elfcpp::R_PPC64_DTPREL16_HIGHEST:
  8308. case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
  8309. case elfcpp::R_PPC64_TLSGD:
  8310. case elfcpp::R_PPC64_TLSLD:
  8311. case elfcpp::R_PPC64_ADDR64_LOCAL:
  8312. break;
  8313. case elfcpp::R_PPC64_GOT_PCREL34:
  8314. case elfcpp::R_POWERPC_GOT16:
  8315. case elfcpp::R_POWERPC_GOT16_LO:
  8316. case elfcpp::R_POWERPC_GOT16_HI:
  8317. case elfcpp::R_POWERPC_GOT16_HA:
  8318. case elfcpp::R_PPC64_GOT16_DS:
  8319. case elfcpp::R_PPC64_GOT16_LO_DS:
  8320. {
  8321. // The symbol requires a GOT entry.
  8322. Output_data_got_powerpc<size, big_endian>* got;
  8323. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  8324. Got_type got_type = ((size == 32
  8325. || r_type == elfcpp::R_POWERPC_GOT16
  8326. || r_type == elfcpp::R_PPC64_GOT16_DS)
  8327. ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
  8328. got = target->got_section(symtab, layout, got_type);
  8329. if (gsym->final_value_is_known())
  8330. {
  8331. if (is_ifunc
  8332. && (size == 32 || target->abiversion() >= 2))
  8333. got->add_global_plt(gsym, got_type, addend);
  8334. else
  8335. got->add_global(gsym, got_type, addend);
  8336. }
  8337. else if (!gsym->has_got_offset(got_type, addend))
  8338. {
  8339. // If we are generating a shared object or a pie, this
  8340. // symbol's GOT entry will be set by a dynamic relocation.
  8341. unsigned int off = got->add_constant(0);
  8342. gsym->set_got_offset(got_type, off, addend);
  8343. Reloc_section* rela_dyn
  8344. = target->rela_dyn_section(symtab, layout, is_ifunc);
  8345. if (gsym->can_use_relative_reloc(false)
  8346. && !((size == 32
  8347. || target->abiversion() >= 2)
  8348. && gsym->visibility() == elfcpp::STV_PROTECTED
  8349. && parameters->options().shared()))
  8350. {
  8351. unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
  8352. : elfcpp::R_POWERPC_RELATIVE);
  8353. rela_dyn->add_global_relative(gsym, dynrel, got, off,
  8354. addend, false);
  8355. }
  8356. else
  8357. {
  8358. unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
  8359. rela_dyn->add_global(gsym, dynrel, got, off, addend);
  8360. }
  8361. }
  8362. }
  8363. break;
  8364. case elfcpp::R_PPC64_TOC16:
  8365. case elfcpp::R_PPC64_TOC16_LO:
  8366. case elfcpp::R_PPC64_TOC16_HI:
  8367. case elfcpp::R_PPC64_TOC16_HA:
  8368. case elfcpp::R_PPC64_TOC16_DS:
  8369. case elfcpp::R_PPC64_TOC16_LO_DS:
  8370. // We need a GOT section.
  8371. target->got_section(symtab, layout, GOT_TYPE_SMALL);
  8372. break;
  8373. case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
  8374. case elfcpp::R_POWERPC_GOT_TLSGD16:
  8375. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  8376. case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
  8377. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  8378. {
  8379. bool final = gsym->final_value_is_known();
  8380. tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
  8381. if (tls_type == tls::TLSOPT_NONE)
  8382. {
  8383. Got_type got_type = ((size == 32
  8384. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
  8385. ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
  8386. Output_data_got_powerpc<size, big_endian>* got
  8387. = target->got_section(symtab, layout, got_type);
  8388. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  8389. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  8390. got->add_global_pair_with_rel(gsym, got_type, rela_dyn,
  8391. elfcpp::R_POWERPC_DTPMOD,
  8392. elfcpp::R_POWERPC_DTPREL,
  8393. addend);
  8394. }
  8395. else if (tls_type == tls::TLSOPT_TO_IE)
  8396. {
  8397. Got_type got_type = ((size == 32
  8398. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
  8399. ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
  8400. if (!gsym->has_got_offset(got_type))
  8401. {
  8402. Output_data_got_powerpc<size, big_endian>* got
  8403. = target->got_section(symtab, layout, got_type);
  8404. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  8405. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  8406. if (gsym->is_undefined()
  8407. || gsym->is_from_dynobj())
  8408. {
  8409. got->add_global_with_rel(gsym, got_type, rela_dyn,
  8410. elfcpp::R_POWERPC_TPREL, addend);
  8411. }
  8412. else
  8413. {
  8414. unsigned int off = got->add_constant(0);
  8415. gsym->set_got_offset(got_type, off);
  8416. unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
  8417. rela_dyn->add_symbolless_global_addend(gsym, dynrel,
  8418. got, off, addend);
  8419. }
  8420. }
  8421. }
  8422. else if (tls_type == tls::TLSOPT_TO_LE)
  8423. {
  8424. // no GOT relocs needed for Local Exec.
  8425. }
  8426. else
  8427. gold_unreachable();
  8428. }
  8429. break;
  8430. case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
  8431. case elfcpp::R_POWERPC_GOT_TLSLD16:
  8432. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  8433. case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
  8434. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  8435. {
  8436. tls::Tls_optimization tls_type = target->optimize_tls_ld();
  8437. if (tls_type == tls::TLSOPT_NONE)
  8438. target->tlsld_got_offset(symtab, layout, object);
  8439. else if (tls_type == tls::TLSOPT_TO_LE)
  8440. {
  8441. // no GOT relocs needed for Local Exec.
  8442. if (parameters->options().emit_relocs())
  8443. {
  8444. Output_section* os = layout->tls_segment()->first_section();
  8445. gold_assert(os != NULL);
  8446. os->set_needs_symtab_index();
  8447. }
  8448. }
  8449. else
  8450. gold_unreachable();
  8451. }
  8452. break;
  8453. case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
  8454. case elfcpp::R_POWERPC_GOT_DTPREL16:
  8455. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  8456. case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
  8457. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  8458. {
  8459. Got_type got_type = ((size == 32
  8460. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
  8461. ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
  8462. Output_data_got_powerpc<size, big_endian>* got
  8463. = target->got_section(symtab, layout, got_type);
  8464. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  8465. if (!gsym->final_value_is_known()
  8466. && (gsym->is_from_dynobj()
  8467. || gsym->is_undefined()
  8468. || gsym->is_preemptible()))
  8469. got->add_global_with_rel(gsym, got_type,
  8470. target->rela_dyn_section(layout),
  8471. elfcpp::R_POWERPC_DTPREL, addend);
  8472. else
  8473. got->add_global_tls(gsym, got_type, addend);
  8474. }
  8475. break;
  8476. case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
  8477. case elfcpp::R_POWERPC_GOT_TPREL16:
  8478. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  8479. case elfcpp::R_POWERPC_GOT_TPREL16_HI:
  8480. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  8481. {
  8482. bool final = gsym->final_value_is_known();
  8483. tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
  8484. if (tls_type == tls::TLSOPT_NONE)
  8485. {
  8486. Got_type got_type = ((size == 32
  8487. || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
  8488. ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
  8489. if (!gsym->has_got_offset(got_type))
  8490. {
  8491. Output_data_got_powerpc<size, big_endian>* got
  8492. = target->got_section(symtab, layout, got_type);
  8493. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  8494. uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
  8495. if (gsym->is_undefined()
  8496. || gsym->is_from_dynobj())
  8497. {
  8498. got->add_global_with_rel(gsym, got_type, rela_dyn,
  8499. elfcpp::R_POWERPC_TPREL, addend);
  8500. }
  8501. else
  8502. {
  8503. unsigned int off = got->add_constant(0);
  8504. gsym->set_got_offset(got_type, off);
  8505. unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
  8506. rela_dyn->add_symbolless_global_addend(gsym, dynrel,
  8507. got, off, addend);
  8508. }
  8509. }
  8510. }
  8511. else if (tls_type == tls::TLSOPT_TO_LE)
  8512. {
  8513. // no GOT relocs needed for Local Exec.
  8514. }
  8515. else
  8516. gold_unreachable();
  8517. }
  8518. break;
  8519. default:
  8520. unsupported_reloc_global(object, r_type, gsym);
  8521. break;
  8522. }
  8523. if (size == 64
  8524. && parameters->options().toc_optimize())
  8525. {
  8526. if (data_shndx == ppc_object->toc_shndx())
  8527. {
  8528. bool ok = true;
  8529. if (r_type != elfcpp::R_PPC64_ADDR64
  8530. || (is_ifunc && target->abiversion() < 2))
  8531. ok = false;
  8532. else if (parameters->options().output_is_position_independent()
  8533. && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
  8534. ok = false;
  8535. if (!ok)
  8536. ppc_object->set_no_toc_opt(reloc.get_r_offset());
  8537. }
  8538. enum {no_check, check_lo, check_ha} insn_check;
  8539. switch (r_type)
  8540. {
  8541. default:
  8542. insn_check = no_check;
  8543. break;
  8544. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  8545. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  8546. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  8547. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  8548. case elfcpp::R_POWERPC_GOT16_HA:
  8549. case elfcpp::R_PPC64_TOC16_HA:
  8550. insn_check = check_ha;
  8551. break;
  8552. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  8553. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  8554. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  8555. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  8556. case elfcpp::R_POWERPC_GOT16_LO:
  8557. case elfcpp::R_PPC64_GOT16_LO_DS:
  8558. case elfcpp::R_PPC64_TOC16_LO:
  8559. case elfcpp::R_PPC64_TOC16_LO_DS:
  8560. insn_check = check_lo;
  8561. break;
  8562. }
  8563. section_size_type slen;
  8564. const unsigned char* view = NULL;
  8565. if (insn_check != no_check)
  8566. {
  8567. view = ppc_object->section_contents(data_shndx, &slen, false);
  8568. section_size_type off =
  8569. convert_to_section_size_type(reloc.get_r_offset()) & -4;
  8570. if (off < slen)
  8571. {
  8572. uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
  8573. if (insn_check == check_lo
  8574. ? !ok_lo_toc_insn(insn, r_type)
  8575. : ((insn & ((0x3f << 26) | 0x1f << 16))
  8576. != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
  8577. {
  8578. ppc_object->set_no_toc_opt();
  8579. gold_warning(_("%s: toc optimization is not supported "
  8580. "for %#08x instruction"),
  8581. ppc_object->name().c_str(), insn);
  8582. }
  8583. }
  8584. }
  8585. switch (r_type)
  8586. {
  8587. default:
  8588. break;
  8589. case elfcpp::R_PPC64_TOC16:
  8590. case elfcpp::R_PPC64_TOC16_LO:
  8591. case elfcpp::R_PPC64_TOC16_HI:
  8592. case elfcpp::R_PPC64_TOC16_HA:
  8593. case elfcpp::R_PPC64_TOC16_DS:
  8594. case elfcpp::R_PPC64_TOC16_LO_DS:
  8595. if (gsym->source() == Symbol::FROM_OBJECT
  8596. && !gsym->object()->is_dynamic())
  8597. {
  8598. Powerpc_relobj<size, big_endian>* sym_object
  8599. = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
  8600. bool is_ordinary;
  8601. unsigned int shndx = gsym->shndx(&is_ordinary);
  8602. if (shndx == sym_object->toc_shndx())
  8603. {
  8604. Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
  8605. Address dst_off = sym->value() + reloc.get_r_addend();
  8606. if (dst_off < sym_object->section_size(shndx))
  8607. {
  8608. bool ok = false;
  8609. if (r_type == elfcpp::R_PPC64_TOC16_HA)
  8610. ok = true;
  8611. else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
  8612. {
  8613. // Need to check that the insn is a ld
  8614. if (!view)
  8615. view = ppc_object->section_contents(data_shndx,
  8616. &slen,
  8617. false);
  8618. section_size_type off =
  8619. (convert_to_section_size_type(reloc.get_r_offset())
  8620. + (big_endian ? -2 : 3));
  8621. if (off < slen
  8622. && (view[off] & (0x3f << 2)) == (58u << 2))
  8623. ok = true;
  8624. }
  8625. if (!ok)
  8626. sym_object->set_no_toc_opt(dst_off);
  8627. }
  8628. }
  8629. }
  8630. break;
  8631. }
  8632. }
  8633. if (size == 32)
  8634. {
  8635. switch (r_type)
  8636. {
  8637. case elfcpp::R_PPC_LOCAL24PC:
  8638. if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
  8639. gold_error(_("%s: unsupported -mbss-plt code"),
  8640. ppc_object->name().c_str());
  8641. break;
  8642. default:
  8643. break;
  8644. }
  8645. }
  8646. switch (r_type)
  8647. {
  8648. case elfcpp::R_POWERPC_GOT_TLSLD16:
  8649. case elfcpp::R_POWERPC_GOT_TLSGD16:
  8650. case elfcpp::R_POWERPC_GOT_TPREL16:
  8651. case elfcpp::R_POWERPC_GOT_DTPREL16:
  8652. case elfcpp::R_POWERPC_GOT16:
  8653. case elfcpp::R_PPC64_GOT16_DS:
  8654. case elfcpp::R_PPC64_TOC16:
  8655. case elfcpp::R_PPC64_TOC16_DS:
  8656. ppc_object->set_has_small_toc_reloc();
  8657. break;
  8658. default:
  8659. break;
  8660. }
  8661. switch (r_type)
  8662. {
  8663. case elfcpp::R_PPC64_TPREL16_DS:
  8664. case elfcpp::R_PPC64_TPREL16_LO_DS:
  8665. case elfcpp::R_PPC64_TPREL16_HIGH:
  8666. case elfcpp::R_PPC64_TPREL16_HIGHA:
  8667. case elfcpp::R_PPC64_TPREL16_HIGHER:
  8668. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  8669. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  8670. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  8671. case elfcpp::R_PPC64_TPREL34:
  8672. if (size != 64)
  8673. break;
  8674. // Fall through.
  8675. case elfcpp::R_POWERPC_TPREL16:
  8676. case elfcpp::R_POWERPC_TPREL16_LO:
  8677. case elfcpp::R_POWERPC_TPREL16_HI:
  8678. case elfcpp::R_POWERPC_TPREL16_HA:
  8679. layout->set_has_static_tls();
  8680. break;
  8681. default:
  8682. break;
  8683. }
  8684. switch (r_type)
  8685. {
  8686. case elfcpp::R_POWERPC_TPREL16_HA:
  8687. if (target->tprel_opt())
  8688. {
  8689. section_size_type slen;
  8690. const unsigned char* view = NULL;
  8691. view = ppc_object->section_contents(data_shndx, &slen, false);
  8692. section_size_type off
  8693. = convert_to_section_size_type(reloc.get_r_offset()) & -4;
  8694. if (off < slen)
  8695. {
  8696. uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
  8697. if ((insn & ((0x3fu << 26) | 0x1f << 16))
  8698. != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
  8699. target->set_no_tprel_opt();
  8700. }
  8701. }
  8702. break;
  8703. case elfcpp::R_PPC64_TPREL16_HIGH:
  8704. case elfcpp::R_PPC64_TPREL16_HIGHA:
  8705. case elfcpp::R_PPC64_TPREL16_HIGHER:
  8706. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  8707. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  8708. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  8709. if (size != 64)
  8710. break;
  8711. // Fall through.
  8712. case elfcpp::R_POWERPC_TPREL16_HI:
  8713. target->set_no_tprel_opt();
  8714. break;
  8715. default:
  8716. break;
  8717. }
  8718. switch (r_type)
  8719. {
  8720. case elfcpp::R_PPC64_D34:
  8721. case elfcpp::R_PPC64_D34_LO:
  8722. case elfcpp::R_PPC64_D34_HI30:
  8723. case elfcpp::R_PPC64_D34_HA30:
  8724. case elfcpp::R_PPC64_D28:
  8725. case elfcpp::R_PPC64_PCREL34:
  8726. case elfcpp::R_PPC64_PCREL28:
  8727. case elfcpp::R_PPC64_TPREL34:
  8728. case elfcpp::R_PPC64_DTPREL34:
  8729. case elfcpp::R_PPC64_PLT_PCREL34:
  8730. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  8731. case elfcpp::R_PPC64_GOT_PCREL34:
  8732. case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
  8733. case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
  8734. case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
  8735. case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
  8736. target->set_power10_relocs();
  8737. break;
  8738. default:
  8739. break;
  8740. }
  8741. }
  8742. // Process relocations for gc.
  8743. template<int size, bool big_endian>
  8744. void
  8745. Target_powerpc<size, big_endian>::gc_process_relocs(
  8746. Symbol_table* symtab,
  8747. Layout* layout,
  8748. Sized_relobj_file<size, big_endian>* object,
  8749. unsigned int data_shndx,
  8750. unsigned int,
  8751. const unsigned char* prelocs,
  8752. size_t reloc_count,
  8753. Output_section* output_section,
  8754. bool needs_special_offset_handling,
  8755. size_t local_symbol_count,
  8756. const unsigned char* plocal_symbols)
  8757. {
  8758. typedef Target_powerpc<size, big_endian> Powerpc;
  8759. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  8760. Classify_reloc;
  8761. Powerpc_relobj<size, big_endian>* ppc_object
  8762. = static_cast<Powerpc_relobj<size, big_endian>*>(object);
  8763. if (size == 64)
  8764. ppc_object->set_opd_valid();
  8765. if (size == 64 && data_shndx == ppc_object->opd_shndx())
  8766. {
  8767. typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
  8768. for (p = ppc_object->access_from_map()->begin();
  8769. p != ppc_object->access_from_map()->end();
  8770. ++p)
  8771. {
  8772. Address dst_off = p->first;
  8773. unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
  8774. typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
  8775. for (s = p->second.begin(); s != p->second.end(); ++s)
  8776. {
  8777. Relobj* src_obj = s->first;
  8778. unsigned int src_indx = s->second;
  8779. symtab->gc()->add_reference(src_obj, src_indx,
  8780. ppc_object, dst_indx);
  8781. }
  8782. p->second.clear();
  8783. }
  8784. ppc_object->access_from_map()->clear();
  8785. ppc_object->process_gc_mark(symtab);
  8786. // Don't look at .opd relocs as .opd will reference everything.
  8787. return;
  8788. }
  8789. gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
  8790. symtab,
  8791. layout,
  8792. this,
  8793. object,
  8794. data_shndx,
  8795. prelocs,
  8796. reloc_count,
  8797. output_section,
  8798. needs_special_offset_handling,
  8799. local_symbol_count,
  8800. plocal_symbols);
  8801. }
  8802. // Handle target specific gc actions when adding a gc reference from
  8803. // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
  8804. // and DST_OFF. For powerpc64, this adds a referenc to the code
  8805. // section of a function descriptor.
  8806. template<int size, bool big_endian>
  8807. void
  8808. Target_powerpc<size, big_endian>::do_gc_add_reference(
  8809. Symbol_table* symtab,
  8810. Relobj* src_obj,
  8811. unsigned int src_shndx,
  8812. Relobj* dst_obj,
  8813. unsigned int dst_shndx,
  8814. Address dst_off) const
  8815. {
  8816. if (size != 64 || dst_obj->is_dynamic())
  8817. return;
  8818. Powerpc_relobj<size, big_endian>* ppc_object
  8819. = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
  8820. if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
  8821. {
  8822. if (ppc_object->opd_valid())
  8823. {
  8824. dst_shndx = ppc_object->get_opd_ent(dst_off);
  8825. symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
  8826. }
  8827. else
  8828. {
  8829. // If we haven't run scan_opd_relocs, we must delay
  8830. // processing this function descriptor reference.
  8831. ppc_object->add_reference(src_obj, src_shndx, dst_off);
  8832. }
  8833. }
  8834. }
  8835. // Add any special sections for this symbol to the gc work list.
  8836. // For powerpc64, this adds the code section of a function
  8837. // descriptor.
  8838. template<int size, bool big_endian>
  8839. void
  8840. Target_powerpc<size, big_endian>::do_gc_mark_symbol(
  8841. Symbol_table* symtab,
  8842. Symbol* sym) const
  8843. {
  8844. if (size == 64 && sym->object()->pluginobj() == NULL)
  8845. {
  8846. Powerpc_relobj<size, big_endian>* ppc_object
  8847. = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
  8848. bool is_ordinary;
  8849. unsigned int shndx = sym->shndx(&is_ordinary);
  8850. if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
  8851. {
  8852. Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
  8853. Address dst_off = gsym->value();
  8854. if (ppc_object->opd_valid())
  8855. {
  8856. unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
  8857. symtab->gc()->worklist().push_back(Section_id(ppc_object,
  8858. dst_indx));
  8859. }
  8860. else
  8861. ppc_object->add_gc_mark(dst_off);
  8862. }
  8863. }
  8864. }
  8865. // For a symbol location in .opd, set LOC to the location of the
  8866. // function entry.
  8867. template<int size, bool big_endian>
  8868. void
  8869. Target_powerpc<size, big_endian>::do_function_location(
  8870. Symbol_location* loc) const
  8871. {
  8872. if (size == 64 && loc->shndx != 0)
  8873. {
  8874. if (loc->object->is_dynamic())
  8875. {
  8876. Powerpc_dynobj<size, big_endian>* ppc_object
  8877. = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
  8878. if (loc->shndx == ppc_object->opd_shndx())
  8879. {
  8880. Address dest_off;
  8881. Address off = loc->offset - ppc_object->opd_address();
  8882. loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
  8883. loc->offset = dest_off;
  8884. }
  8885. }
  8886. else
  8887. {
  8888. const Powerpc_relobj<size, big_endian>* ppc_object
  8889. = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
  8890. if (loc->shndx == ppc_object->opd_shndx())
  8891. {
  8892. Address dest_off;
  8893. loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
  8894. loc->offset = dest_off;
  8895. }
  8896. }
  8897. }
  8898. }
  8899. // FNOFFSET in section SHNDX in OBJECT is the start of a function
  8900. // compiled with -fsplit-stack. The function calls non-split-stack
  8901. // code. Change the function to ensure it has enough stack space to
  8902. // call some random function.
  8903. template<int size, bool big_endian>
  8904. void
  8905. Target_powerpc<size, big_endian>::do_calls_non_split(
  8906. Relobj* object,
  8907. unsigned int shndx,
  8908. section_offset_type fnoffset,
  8909. section_size_type fnsize,
  8910. const unsigned char* prelocs,
  8911. size_t reloc_count,
  8912. unsigned char* view,
  8913. section_size_type view_size,
  8914. std::string* from,
  8915. std::string* to) const
  8916. {
  8917. // 32-bit not supported.
  8918. if (size == 32)
  8919. {
  8920. // warn
  8921. Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
  8922. prelocs, reloc_count, view, view_size,
  8923. from, to);
  8924. return;
  8925. }
  8926. // The function always starts with
  8927. // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
  8928. // addis %r12,%r1,-allocate@ha
  8929. // addi %r12,%r12,-allocate@l
  8930. // cmpld %r12,%r0
  8931. // but note that the addis or addi may be replaced with a nop
  8932. unsigned char *entry = view + fnoffset;
  8933. uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
  8934. if ((insn & 0xffff0000) == addis_2_12)
  8935. {
  8936. /* Skip ELFv2 global entry code. */
  8937. entry += 8;
  8938. insn = elfcpp::Swap<32, big_endian>::readval(entry);
  8939. }
  8940. unsigned char *pinsn = entry;
  8941. bool ok = false;
  8942. const uint32_t ld_private_ss = 0xe80d8fc0;
  8943. if (insn == ld_private_ss)
  8944. {
  8945. int32_t allocate = 0;
  8946. while (1)
  8947. {
  8948. pinsn += 4;
  8949. insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
  8950. if ((insn & 0xffff0000) == addis_12_1)
  8951. allocate += (insn & 0xffff) << 16;
  8952. else if ((insn & 0xffff0000) == addi_12_1
  8953. || (insn & 0xffff0000) == addi_12_12)
  8954. allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
  8955. else if (insn != nop)
  8956. break;
  8957. }
  8958. if (insn == cmpld_7_12_0 && pinsn == entry + 12)
  8959. {
  8960. int extra = parameters->options().split_stack_adjust_size();
  8961. allocate -= extra;
  8962. if (allocate >= 0 || extra < 0)
  8963. {
  8964. object->error(_("split-stack stack size overflow at "
  8965. "section %u offset %0zx"),
  8966. shndx, static_cast<size_t>(fnoffset));
  8967. return;
  8968. }
  8969. pinsn = entry + 4;
  8970. insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
  8971. if (insn != addis_12_1)
  8972. {
  8973. elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
  8974. pinsn += 4;
  8975. insn = addi_12_12 | (allocate & 0xffff);
  8976. if (insn != addi_12_12)
  8977. {
  8978. elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
  8979. pinsn += 4;
  8980. }
  8981. }
  8982. else
  8983. {
  8984. insn = addi_12_1 | (allocate & 0xffff);
  8985. elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
  8986. pinsn += 4;
  8987. }
  8988. if (pinsn != entry + 12)
  8989. elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
  8990. ok = true;
  8991. }
  8992. }
  8993. if (!ok)
  8994. {
  8995. if (!object->has_no_split_stack())
  8996. object->error(_("failed to match split-stack sequence at "
  8997. "section %u offset %0zx"),
  8998. shndx, static_cast<size_t>(fnoffset));
  8999. }
  9000. }
  9001. // Scan relocations for a section.
  9002. template<int size, bool big_endian>
  9003. void
  9004. Target_powerpc<size, big_endian>::scan_relocs(
  9005. Symbol_table* symtab,
  9006. Layout* layout,
  9007. Sized_relobj_file<size, big_endian>* object,
  9008. unsigned int data_shndx,
  9009. unsigned int sh_type,
  9010. const unsigned char* prelocs,
  9011. size_t reloc_count,
  9012. Output_section* output_section,
  9013. bool needs_special_offset_handling,
  9014. size_t local_symbol_count,
  9015. const unsigned char* plocal_symbols)
  9016. {
  9017. typedef Target_powerpc<size, big_endian> Powerpc;
  9018. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  9019. Classify_reloc;
  9020. if (!this->plt_localentry0_init_)
  9021. {
  9022. bool plt_localentry0 = false;
  9023. if (size == 64
  9024. && this->abiversion() >= 2)
  9025. {
  9026. if (parameters->options().user_set_plt_localentry())
  9027. plt_localentry0 = parameters->options().plt_localentry();
  9028. if (plt_localentry0
  9029. && symtab->lookup("GLIBC_2.26", NULL) == NULL)
  9030. gold_warning(_("--plt-localentry is especially dangerous without "
  9031. "ld.so support to detect ABI violations"));
  9032. }
  9033. this->plt_localentry0_ = plt_localentry0;
  9034. this->plt_localentry0_init_ = true;
  9035. }
  9036. if (sh_type == elfcpp::SHT_REL)
  9037. {
  9038. gold_error(_("%s: unsupported REL reloc section"),
  9039. object->name().c_str());
  9040. return;
  9041. }
  9042. gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
  9043. symtab,
  9044. layout,
  9045. this,
  9046. object,
  9047. data_shndx,
  9048. prelocs,
  9049. reloc_count,
  9050. output_section,
  9051. needs_special_offset_handling,
  9052. local_symbol_count,
  9053. plocal_symbols);
  9054. if (this->plt_localentry0_ && this->power10_relocs_)
  9055. {
  9056. gold_warning(_("--plt-localentry is incompatible with "
  9057. "power10 pc-relative code"));
  9058. this->plt_localentry0_ = false;
  9059. }
  9060. }
  9061. // Functor class for processing the global symbol table.
  9062. // Removes symbols defined on discarded opd entries.
  9063. template<bool big_endian>
  9064. class Global_symbol_visitor_opd
  9065. {
  9066. public:
  9067. Global_symbol_visitor_opd()
  9068. { }
  9069. void
  9070. operator()(Sized_symbol<64>* sym)
  9071. {
  9072. if (sym->has_symtab_index()
  9073. || sym->source() != Symbol::FROM_OBJECT
  9074. || !sym->in_real_elf())
  9075. return;
  9076. if (sym->object()->is_dynamic())
  9077. return;
  9078. Powerpc_relobj<64, big_endian>* symobj
  9079. = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
  9080. if (symobj->opd_shndx() == 0)
  9081. return;
  9082. bool is_ordinary;
  9083. unsigned int shndx = sym->shndx(&is_ordinary);
  9084. if (shndx == symobj->opd_shndx()
  9085. && symobj->get_opd_discard(sym->value()))
  9086. {
  9087. sym->set_undefined();
  9088. sym->set_visibility(elfcpp::STV_DEFAULT);
  9089. sym->set_is_defined_in_discarded_section();
  9090. sym->set_symtab_index(-1U);
  9091. }
  9092. }
  9093. };
  9094. template<int size, bool big_endian>
  9095. void
  9096. Target_powerpc<size, big_endian>::define_save_restore_funcs(
  9097. Layout* layout,
  9098. Symbol_table* symtab)
  9099. {
  9100. if (size == 64)
  9101. {
  9102. Output_data_save_res<size, big_endian>* savres
  9103. = new Output_data_save_res<size, big_endian>(symtab);
  9104. this->savres_section_ = savres;
  9105. layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
  9106. elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
  9107. savres, ORDER_TEXT, false);
  9108. }
  9109. }
  9110. // Sort linker created .got section first (for the header), then input
  9111. // sections belonging to files using small model code.
  9112. template<bool big_endian>
  9113. class Sort_toc_sections
  9114. {
  9115. const Output_section_data*
  9116. small_got_section() const
  9117. {
  9118. return (static_cast<Target_powerpc<64, big_endian>*>(
  9119. parameters->sized_target<64, big_endian>())
  9120. ->got_section(GOT_TYPE_SMALL));
  9121. }
  9122. int
  9123. rank(const Output_section::Input_section& isec) const
  9124. {
  9125. if (!isec.is_input_section())
  9126. {
  9127. if (isec.output_section_data() == this->small_got_section())
  9128. return 0;
  9129. return 2;
  9130. }
  9131. if (static_cast<const Powerpc_relobj<64, big_endian>*>(isec.relobj())
  9132. ->has_small_toc_reloc())
  9133. return 1;
  9134. return 3;
  9135. }
  9136. public:
  9137. bool
  9138. operator()(const Output_section::Input_section& is1,
  9139. const Output_section::Input_section& is2) const
  9140. {
  9141. return rank(is1) < rank(is2);
  9142. }
  9143. };
  9144. // Finalize the sections.
  9145. template<int size, bool big_endian>
  9146. void
  9147. Target_powerpc<size, big_endian>::do_finalize_sections(
  9148. Layout* layout,
  9149. const Input_objects* input_objects,
  9150. Symbol_table* symtab)
  9151. {
  9152. if (parameters->doing_static_link())
  9153. {
  9154. // At least some versions of glibc elf-init.o have a strong
  9155. // reference to __rela_iplt marker syms. A weak ref would be
  9156. // better..
  9157. if (this->iplt_ != NULL)
  9158. {
  9159. Reloc_section* rel = this->iplt_->rel_plt();
  9160. symtab->define_in_output_data("__rela_iplt_start", NULL,
  9161. Symbol_table::PREDEFINED, rel, 0, 0,
  9162. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  9163. elfcpp::STV_HIDDEN, 0, false, true);
  9164. symtab->define_in_output_data("__rela_iplt_end", NULL,
  9165. Symbol_table::PREDEFINED, rel, 0, 0,
  9166. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  9167. elfcpp::STV_HIDDEN, 0, true, true);
  9168. }
  9169. else
  9170. {
  9171. symtab->define_as_constant("__rela_iplt_start", NULL,
  9172. Symbol_table::PREDEFINED, 0, 0,
  9173. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  9174. elfcpp::STV_HIDDEN, 0, true, false);
  9175. symtab->define_as_constant("__rela_iplt_end", NULL,
  9176. Symbol_table::PREDEFINED, 0, 0,
  9177. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  9178. elfcpp::STV_HIDDEN, 0, true, false);
  9179. }
  9180. }
  9181. if (size == 64)
  9182. {
  9183. typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
  9184. symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
  9185. if (!parameters->options().relocatable())
  9186. {
  9187. this->define_save_restore_funcs(layout, symtab);
  9188. // Annoyingly, we need to make these sections now whether or
  9189. // not we need them. If we delay until do_relax then we
  9190. // need to mess with the relaxation machinery checkpointing.
  9191. this->got_section(symtab, layout, GOT_TYPE_STANDARD);
  9192. this->make_brlt_section(layout);
  9193. // FIXME, maybe. Here we could run through all the got
  9194. // entries in the small got section, removing any duplicates
  9195. // found in the big got section and renumbering offsets.
  9196. if (parameters->options().toc_sort())
  9197. {
  9198. Output_section* os = this->got_->output_section();
  9199. if (os != NULL && os->input_sections().size() > 1)
  9200. std::stable_sort(os->input_sections().begin(),
  9201. os->input_sections().end(),
  9202. Sort_toc_sections<big_endian>());
  9203. }
  9204. }
  9205. }
  9206. // Fill in some more dynamic tags.
  9207. Output_data_dynamic* odyn = layout->dynamic_data();
  9208. if (odyn != NULL)
  9209. {
  9210. const Reloc_section* rel_plt = (this->plt_ == NULL
  9211. ? NULL
  9212. : this->plt_->rel_plt());
  9213. layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
  9214. this->rela_dyn_, true, size == 32);
  9215. if (size == 32)
  9216. {
  9217. if (this->got_ != NULL)
  9218. {
  9219. this->got_->finalize_data_size();
  9220. odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
  9221. this->got_, this->got_->g_o_t());
  9222. }
  9223. if (this->has_tls_get_addr_opt_)
  9224. odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
  9225. }
  9226. else
  9227. {
  9228. if (this->glink_ != NULL)
  9229. {
  9230. this->glink_->finalize_data_size();
  9231. odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
  9232. this->glink_,
  9233. (this->glink_->pltresolve_size()
  9234. - 32));
  9235. }
  9236. if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
  9237. odyn->add_constant(elfcpp::DT_PPC64_OPT,
  9238. ((this->has_localentry0_
  9239. ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
  9240. | (this->has_tls_get_addr_opt_
  9241. ? elfcpp::PPC64_OPT_TLS : 0)));
  9242. }
  9243. }
  9244. // Emit any relocs we saved in an attempt to avoid generating COPY
  9245. // relocs.
  9246. if (this->copy_relocs_.any_saved_relocs())
  9247. this->copy_relocs_.emit(this->rela_dyn_section(layout));
  9248. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  9249. p != input_objects->relobj_end();
  9250. ++p)
  9251. {
  9252. Powerpc_relobj<size, big_endian>* ppc_relobj
  9253. = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
  9254. if (ppc_relobj->attributes_section_data())
  9255. this->merge_object_attributes(ppc_relobj,
  9256. ppc_relobj->attributes_section_data());
  9257. }
  9258. for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
  9259. p != input_objects->dynobj_end();
  9260. ++p)
  9261. {
  9262. Powerpc_dynobj<size, big_endian>* ppc_dynobj
  9263. = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
  9264. if (ppc_dynobj->attributes_section_data())
  9265. this->merge_object_attributes(ppc_dynobj,
  9266. ppc_dynobj->attributes_section_data());
  9267. }
  9268. // Create a .gnu.attributes section if we have merged any attributes
  9269. // from inputs.
  9270. if (this->attributes_section_data_ != NULL
  9271. && this->attributes_section_data_->size() != 0)
  9272. {
  9273. Output_attributes_section_data* attributes_section
  9274. = new Output_attributes_section_data(*this->attributes_section_data_);
  9275. layout->add_output_section_data(".gnu.attributes",
  9276. elfcpp::SHT_GNU_ATTRIBUTES, 0,
  9277. attributes_section, ORDER_INVALID, false);
  9278. }
  9279. }
  9280. // Merge object attributes from input file called NAME with those of the
  9281. // output. The input object attributes are in the object pointed by PASD.
  9282. template<int size, bool big_endian>
  9283. void
  9284. Target_powerpc<size, big_endian>::merge_object_attributes(
  9285. const Object* obj,
  9286. const Attributes_section_data* pasd)
  9287. {
  9288. // Return if there is no attributes section data.
  9289. if (pasd == NULL)
  9290. return;
  9291. // Create output object attributes.
  9292. if (this->attributes_section_data_ == NULL)
  9293. this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
  9294. const int vendor = Object_attribute::OBJ_ATTR_GNU;
  9295. const Object_attribute* in_attr = pasd->known_attributes(vendor);
  9296. Object_attribute* out_attr
  9297. = this->attributes_section_data_->known_attributes(vendor);
  9298. const char* name = obj->name().c_str();
  9299. const char* err;
  9300. const char* first;
  9301. const char* second;
  9302. int tag = elfcpp::Tag_GNU_Power_ABI_FP;
  9303. int in_fp = in_attr[tag].int_value() & 0xf;
  9304. int out_fp = out_attr[tag].int_value() & 0xf;
  9305. bool warn_only = obj->is_dynamic();
  9306. if (in_fp != out_fp)
  9307. {
  9308. err = NULL;
  9309. if ((in_fp & 3) == 0)
  9310. ;
  9311. else if ((out_fp & 3) == 0)
  9312. {
  9313. if (!warn_only)
  9314. {
  9315. out_fp |= in_fp & 3;
  9316. out_attr[tag].set_int_value(out_fp);
  9317. out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
  9318. this->last_fp_ = name;
  9319. }
  9320. }
  9321. else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
  9322. {
  9323. err = N_("%s uses hard float, %s uses soft float");
  9324. first = this->last_fp_;
  9325. second = name;
  9326. }
  9327. else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
  9328. {
  9329. err = N_("%s uses hard float, %s uses soft float");
  9330. first = name;
  9331. second = this->last_fp_;
  9332. }
  9333. else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
  9334. {
  9335. err = N_("%s uses double-precision hard float, "
  9336. "%s uses single-precision hard float");
  9337. first = this->last_fp_;
  9338. second = name;
  9339. }
  9340. else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
  9341. {
  9342. err = N_("%s uses double-precision hard float, "
  9343. "%s uses single-precision hard float");
  9344. first = name;
  9345. second = this->last_fp_;
  9346. }
  9347. if (err || (in_fp & 0xc) == 0)
  9348. ;
  9349. else if ((out_fp & 0xc) == 0)
  9350. {
  9351. if (!warn_only)
  9352. {
  9353. out_fp |= in_fp & 0xc;
  9354. out_attr[tag].set_int_value(out_fp);
  9355. out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
  9356. this->last_ld_ = name;
  9357. }
  9358. }
  9359. else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
  9360. {
  9361. err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
  9362. first = name;
  9363. second = this->last_ld_;
  9364. }
  9365. else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
  9366. {
  9367. err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
  9368. first = this->last_ld_;
  9369. second = name;
  9370. }
  9371. else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
  9372. {
  9373. err = N_("%s uses IBM long double, %s uses IEEE long double");
  9374. first = this->last_ld_;
  9375. second = name;
  9376. }
  9377. else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
  9378. {
  9379. err = N_("%s uses IBM long double, %s uses IEEE long double");
  9380. first = name;
  9381. second = this->last_ld_;
  9382. }
  9383. if (err)
  9384. {
  9385. if (parameters->options().warn_mismatch())
  9386. {
  9387. if (warn_only)
  9388. gold_warning(_(err), first, second);
  9389. else
  9390. gold_error(_(err), first, second);
  9391. }
  9392. // Arrange for this attribute to be deleted. It's better to
  9393. // say "don't know" about a file than to wrongly claim compliance.
  9394. if (!warn_only)
  9395. out_attr[tag].set_type(0);
  9396. }
  9397. }
  9398. if (size == 32)
  9399. {
  9400. tag = elfcpp::Tag_GNU_Power_ABI_Vector;
  9401. int in_vec = in_attr[tag].int_value() & 3;
  9402. int out_vec = out_attr[tag].int_value() & 3;
  9403. if (in_vec != out_vec)
  9404. {
  9405. err = NULL;
  9406. if (in_vec == 0)
  9407. ;
  9408. else if (out_vec == 0)
  9409. {
  9410. out_vec = in_vec;
  9411. out_attr[tag].set_int_value(out_vec);
  9412. out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
  9413. this->last_vec_ = name;
  9414. }
  9415. // For now, allow generic to transition to AltiVec or SPE
  9416. // without a warning. If GCC marked files with their stack
  9417. // alignment and used don't-care markings for files which are
  9418. // not affected by the vector ABI, we could warn about this
  9419. // case too. */
  9420. else if (in_vec == 1)
  9421. ;
  9422. else if (out_vec == 1)
  9423. {
  9424. out_vec = in_vec;
  9425. out_attr[tag].set_int_value(out_vec);
  9426. out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
  9427. this->last_vec_ = name;
  9428. }
  9429. else if (out_vec < in_vec)
  9430. {
  9431. err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
  9432. first = this->last_vec_;
  9433. second = name;
  9434. }
  9435. else if (out_vec > in_vec)
  9436. {
  9437. err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
  9438. first = name;
  9439. second = this->last_vec_;
  9440. }
  9441. if (err)
  9442. {
  9443. if (parameters->options().warn_mismatch())
  9444. gold_error(_(err), first, second);
  9445. out_attr[tag].set_type(0);
  9446. }
  9447. }
  9448. tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
  9449. int in_struct = in_attr[tag].int_value() & 3;
  9450. int out_struct = out_attr[tag].int_value() & 3;
  9451. if (in_struct != out_struct)
  9452. {
  9453. err = NULL;
  9454. if (in_struct == 0 || in_struct == 3)
  9455. ;
  9456. else if (out_struct == 0)
  9457. {
  9458. out_struct = in_struct;
  9459. out_attr[tag].set_int_value(out_struct);
  9460. out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
  9461. this->last_struct_ = name;
  9462. }
  9463. else if (out_struct < in_struct)
  9464. {
  9465. err = N_("%s uses r3/r4 for small structure returns, "
  9466. "%s uses memory");
  9467. first = this->last_struct_;
  9468. second = name;
  9469. }
  9470. else if (out_struct > in_struct)
  9471. {
  9472. err = N_("%s uses r3/r4 for small structure returns, "
  9473. "%s uses memory");
  9474. first = name;
  9475. second = this->last_struct_;
  9476. }
  9477. if (err)
  9478. {
  9479. if (parameters->options().warn_mismatch())
  9480. gold_error(_(err), first, second);
  9481. out_attr[tag].set_type(0);
  9482. }
  9483. }
  9484. }
  9485. // Merge Tag_compatibility attributes and any common GNU ones.
  9486. this->attributes_section_data_->merge(name, pasd);
  9487. }
  9488. // Emit any saved relocs, and mark toc entries using any of these
  9489. // relocs as not optimizable.
  9490. template<int sh_type, int size, bool big_endian>
  9491. void
  9492. Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
  9493. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
  9494. {
  9495. if (size == 64
  9496. && parameters->options().toc_optimize())
  9497. {
  9498. for (typename Copy_relocs<sh_type, size, big_endian>::
  9499. Copy_reloc_entries::iterator p = this->entries_.begin();
  9500. p != this->entries_.end();
  9501. ++p)
  9502. {
  9503. typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
  9504. entry = *p;
  9505. // If the symbol is no longer defined in a dynamic object,
  9506. // then we emitted a COPY relocation. If it is still
  9507. // dynamic then we'll need dynamic relocations and thus
  9508. // can't optimize toc entries.
  9509. if (entry.sym_->is_from_dynobj())
  9510. {
  9511. Powerpc_relobj<size, big_endian>* ppc_object
  9512. = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
  9513. if (entry.shndx_ == ppc_object->toc_shndx())
  9514. ppc_object->set_no_toc_opt(entry.address_);
  9515. }
  9516. }
  9517. }
  9518. Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
  9519. }
  9520. // Return the value to use for a branch relocation.
  9521. template<int size, bool big_endian>
  9522. bool
  9523. Target_powerpc<size, big_endian>::symval_for_branch(
  9524. const Symbol_table* symtab,
  9525. const Sized_symbol<size>* gsym,
  9526. Powerpc_relobj<size, big_endian>* object,
  9527. Address *value,
  9528. unsigned int *dest_shndx)
  9529. {
  9530. if (size == 32 || this->abiversion() >= 2)
  9531. gold_unreachable();
  9532. *dest_shndx = 0;
  9533. // If the symbol is defined in an opd section, ie. is a function
  9534. // descriptor, use the function descriptor code entry address
  9535. Powerpc_relobj<size, big_endian>* symobj = object;
  9536. if (gsym != NULL
  9537. && (gsym->source() != Symbol::FROM_OBJECT
  9538. || gsym->object()->is_dynamic()))
  9539. return true;
  9540. if (gsym != NULL)
  9541. symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
  9542. unsigned int shndx = symobj->opd_shndx();
  9543. if (shndx == 0)
  9544. return true;
  9545. Address opd_addr = symobj->get_output_section_offset(shndx);
  9546. if (opd_addr == invalid_address)
  9547. return true;
  9548. opd_addr += symobj->output_section_address(shndx);
  9549. if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
  9550. {
  9551. Address sec_off;
  9552. *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
  9553. if (symtab->is_section_folded(symobj, *dest_shndx))
  9554. {
  9555. Section_id folded
  9556. = symtab->icf()->get_folded_section(symobj, *dest_shndx);
  9557. symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
  9558. *dest_shndx = folded.second;
  9559. }
  9560. Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
  9561. if (sec_addr == invalid_address)
  9562. return false;
  9563. sec_addr += symobj->output_section(*dest_shndx)->address();
  9564. *value = sec_addr + sec_off;
  9565. }
  9566. return true;
  9567. }
  9568. template<int size>
  9569. static bool
  9570. relative_value_is_known(const Sized_symbol<size>* gsym)
  9571. {
  9572. if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  9573. return false;
  9574. if (gsym->is_from_dynobj()
  9575. || gsym->is_undefined()
  9576. || gsym->is_preemptible())
  9577. return false;
  9578. if (gsym->is_absolute())
  9579. return !parameters->options().output_is_position_independent();
  9580. return true;
  9581. }
  9582. template<int size>
  9583. static bool
  9584. relative_value_is_known(const Symbol_value<size>* psymval)
  9585. {
  9586. if (psymval->is_ifunc_symbol())
  9587. return false;
  9588. bool is_ordinary;
  9589. unsigned int shndx = psymval->input_shndx(&is_ordinary);
  9590. return is_ordinary && shndx != elfcpp::SHN_UNDEF;
  9591. }
  9592. // PCREL_OPT in one instance flags to the linker that a pair of insns:
  9593. // pld ra,symbol@got@pcrel
  9594. // load/store rt,0(ra)
  9595. // or
  9596. // pla ra,symbol@pcrel
  9597. // load/store rt,0(ra)
  9598. // may be translated to
  9599. // pload/pstore rt,symbol@pcrel
  9600. // nop.
  9601. // This function returns true if the optimization is possible, placing
  9602. // the prefix insn in *PINSN1 and a NOP in *PINSN2.
  9603. //
  9604. // On entry to this function, the linker has already determined that
  9605. // the pld can be replaced with pla: *PINSN1 is that pla insn,
  9606. // while *PINSN2 is the second instruction.
  9607. inline bool
  9608. xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
  9609. {
  9610. uint32_t insn2 = *pinsn2 >> 32;
  9611. uint64_t i1new;
  9612. // Check that regs match.
  9613. if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
  9614. return false;
  9615. switch ((insn2 >> 26) & 63)
  9616. {
  9617. default:
  9618. return false;
  9619. case 32: // lwz
  9620. case 34: // lbz
  9621. case 36: // stw
  9622. case 38: // stb
  9623. case 40: // lhz
  9624. case 42: // lha
  9625. case 44: // sth
  9626. case 48: // lfs
  9627. case 50: // lfd
  9628. case 52: // stfs
  9629. case 54: // stfd
  9630. // These are the PMLS cases, where we just need to tack a prefix
  9631. // on the insn. Check that the D field is zero.
  9632. if ((insn2 & 0xffff) != 0)
  9633. return false;
  9634. i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
  9635. | (insn2 & ((63ULL << 26) | (31ULL << 21))));
  9636. break;
  9637. case 58: // lwa, ld
  9638. if ((insn2 & 0xfffd) != 0)
  9639. return false;
  9640. i1new = ((1ULL << 58) | (1ULL << 52)
  9641. | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
  9642. | (insn2 & (31ULL << 21)));
  9643. break;
  9644. case 57: // lxsd, lxssp
  9645. if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
  9646. return false;
  9647. i1new = ((1ULL << 58) | (1ULL << 52)
  9648. | ((40ULL | (insn2 & 3)) << 26)
  9649. | (insn2 & (31ULL << 21)));
  9650. break;
  9651. case 61: // stxsd, stxssp, lxv, stxv
  9652. if ((insn2 & 3) == 0)
  9653. return false;
  9654. else if ((insn2 & 3) >= 2)
  9655. {
  9656. if ((insn2 & 0xfffc) != 0)
  9657. return false;
  9658. i1new = ((1ULL << 58) | (1ULL << 52)
  9659. | ((44ULL | (insn2 & 3)) << 26)
  9660. | (insn2 & (31ULL << 21)));
  9661. }
  9662. else
  9663. {
  9664. if ((insn2 & 0xfff0) != 0)
  9665. return false;
  9666. i1new = ((1ULL << 58) | (1ULL << 52)
  9667. | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
  9668. | (insn2 & (31ULL << 21)));
  9669. }
  9670. break;
  9671. case 56: // lq
  9672. if ((insn2 & 0xffff) != 0)
  9673. return false;
  9674. i1new = ((1ULL << 58) | (1ULL << 52)
  9675. | (insn2 & ((63ULL << 26) | (31ULL << 21))));
  9676. break;
  9677. case 62: // std, stq
  9678. if ((insn2 & 0xfffd) != 0)
  9679. return false;
  9680. i1new = ((1ULL << 58) | (1ULL << 52)
  9681. | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
  9682. | (insn2 & (31ULL << 21)));
  9683. break;
  9684. }
  9685. *pinsn1 = i1new;
  9686. *pinsn2 = (uint64_t) nop << 32;
  9687. return true;
  9688. }
  9689. // Perform a relocation.
  9690. template<int size, bool big_endian>
  9691. inline bool
  9692. Target_powerpc<size, big_endian>::Relocate::relocate(
  9693. const Relocate_info<size, big_endian>* relinfo,
  9694. unsigned int,
  9695. Target_powerpc* target,
  9696. Output_section* os,
  9697. size_t relnum,
  9698. const unsigned char* preloc,
  9699. const Sized_symbol<size>* gsym,
  9700. const Symbol_value<size>* psymval,
  9701. unsigned char* view,
  9702. Address address,
  9703. section_size_type view_size)
  9704. {
  9705. typedef Powerpc_relocate_functions<size, big_endian> Reloc;
  9706. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
  9707. typedef typename elfcpp::Rela<size, big_endian> Reltype;
  9708. if (view == NULL)
  9709. return true;
  9710. if (target->replace_tls_get_addr(gsym))
  9711. gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
  9712. const elfcpp::Rela<size, big_endian> rela(preloc);
  9713. unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
  9714. Powerpc_relobj<size, big_endian>* const object
  9715. = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
  9716. switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
  9717. {
  9718. case Track_tls::NOT_EXPECTED:
  9719. // No warning. This will result in really old code without tls
  9720. // marker relocs being mis-optimised, but there shouldn't be too
  9721. // much of that code around. The problem with warning is that
  9722. // glibc and libphobos both construct direct calls to
  9723. // __tls_get_addr in a way that is harmless.
  9724. break;
  9725. case Track_tls::EXPECTED:
  9726. // We have already complained.
  9727. break;
  9728. case Track_tls::SKIP:
  9729. if (is_plt16_reloc<size>(r_type)
  9730. || r_type == elfcpp::R_POWERPC_PLTSEQ
  9731. || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
  9732. {
  9733. Insn* iview = reinterpret_cast<Insn*>(view);
  9734. elfcpp::Swap<32, big_endian>::writeval(iview, nop);
  9735. }
  9736. else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
  9737. {
  9738. Insn* iview = reinterpret_cast<Insn*>(view);
  9739. elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
  9740. }
  9741. else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
  9742. || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
  9743. {
  9744. Insn* iview = reinterpret_cast<Insn*>(view);
  9745. elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
  9746. elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
  9747. }
  9748. return true;
  9749. case Track_tls::NORMAL:
  9750. break;
  9751. }
  9752. // Offset from start of insn to d-field reloc.
  9753. const int d_offset = big_endian ? 2 : 0;
  9754. Address value = 0;
  9755. bool has_stub_value = false;
  9756. bool localentry0 = false;
  9757. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  9758. bool use_plt_offset
  9759. = (gsym != NULL
  9760. ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
  9761. : object->local_has_plt_offset(r_sym));
  9762. if (is_plt16_reloc<size>(r_type)
  9763. || r_type == elfcpp::R_PPC64_PLT_PCREL34
  9764. || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
  9765. || r_type == elfcpp::R_POWERPC_PLTSEQ
  9766. || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
  9767. || r_type == elfcpp::R_POWERPC_PLTCALL
  9768. || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
  9769. {
  9770. // It would be possible to replace inline plt calls with direct
  9771. // calls if the PLTCALL is in range. The only difficulty is
  9772. // that the decision depends on the PLTCALL reloc, and we don't
  9773. // know the address of that instruction when processing others
  9774. // in the sequence. So the decision needs to be made in
  9775. // do_relax(). For now, don't optimise inline plt calls.
  9776. if (gsym)
  9777. use_plt_offset = gsym->has_plt_offset();
  9778. }
  9779. if (use_plt_offset
  9780. && !is_got_reloc(r_type)
  9781. && !is_plt16_reloc<size>(r_type)
  9782. && r_type != elfcpp::R_PPC64_PLT_PCREL34
  9783. && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
  9784. && r_type != elfcpp::R_POWERPC_PLTSEQ
  9785. && r_type != elfcpp::R_POWERPC_PLTCALL
  9786. && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
  9787. && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
  9788. && (!psymval->is_ifunc_symbol()
  9789. || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
  9790. {
  9791. if (size == 64
  9792. && gsym != NULL
  9793. && target->abiversion() >= 2
  9794. && !parameters->options().output_is_position_independent()
  9795. && !is_branch_reloc<size>(r_type))
  9796. {
  9797. Address off = target->glink_section()->find_global_entry(gsym);
  9798. if (off != invalid_address)
  9799. {
  9800. value = target->glink_section()->global_entry_address() + off;
  9801. has_stub_value = true;
  9802. }
  9803. }
  9804. else
  9805. {
  9806. Stub_table<size, big_endian>* stub_table = NULL;
  9807. if (target->stub_tables().size() == 1)
  9808. stub_table = target->stub_tables()[0];
  9809. if (stub_table == NULL
  9810. && !(size == 32
  9811. && gsym != NULL
  9812. && !parameters->options().output_is_position_independent()
  9813. && !is_branch_reloc<size>(r_type)))
  9814. stub_table = object->stub_table(relinfo->data_shndx);
  9815. if (stub_table == NULL)
  9816. {
  9817. // This is a ref from a data section to an ifunc symbol,
  9818. // or a non-branch reloc for which we always want to use
  9819. // one set of stubs for resolving function addresses.
  9820. if (target->stub_tables().size() != 0)
  9821. stub_table = target->stub_tables()[0];
  9822. }
  9823. if (stub_table != NULL)
  9824. {
  9825. const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
  9826. if (gsym != NULL)
  9827. ent = stub_table->find_plt_call_entry(object, gsym, r_type,
  9828. rela.get_r_addend());
  9829. else
  9830. ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
  9831. rela.get_r_addend());
  9832. if (ent != NULL)
  9833. {
  9834. value = stub_table->stub_address() + ent->off_;
  9835. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  9836. elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
  9837. size_t reloc_count = shdr.get_sh_size() / reloc_size;
  9838. if (size == 64)
  9839. {
  9840. if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
  9841. {
  9842. if (!ent->notoc_)
  9843. value += ent->p9off_;
  9844. }
  9845. else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
  9846. value += ent->p9off_;
  9847. else
  9848. value += ent->tocoff_;
  9849. }
  9850. if (size == 64
  9851. && ent->r2save_
  9852. && !(gsym != NULL
  9853. && target->is_tls_get_addr_opt(gsym)))
  9854. {
  9855. if (r_type == elfcpp::R_PPC64_REL24_NOTOC
  9856. || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
  9857. {
  9858. if (!(target->power10_stubs()
  9859. && target->power10_stubs_auto()))
  9860. value += 4;
  9861. }
  9862. else if (relnum < reloc_count - 1)
  9863. {
  9864. Reltype next_rela(preloc + reloc_size);
  9865. if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
  9866. == elfcpp::R_PPC64_TOCSAVE
  9867. && (next_rela.get_r_offset()
  9868. == rela.get_r_offset() + 4))
  9869. value += 4;
  9870. }
  9871. }
  9872. localentry0 = ent->localentry0_;
  9873. has_stub_value = true;
  9874. }
  9875. }
  9876. }
  9877. // We don't care too much about bogus debug references to
  9878. // non-local functions, but otherwise there had better be a plt
  9879. // call stub or global entry stub as appropriate.
  9880. gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
  9881. }
  9882. if (use_plt_offset && (is_plt16_reloc<size>(r_type)
  9883. || r_type == elfcpp::R_PPC64_PLT_PCREL34
  9884. || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
  9885. {
  9886. const Output_data_plt_powerpc<size, big_endian>* plt;
  9887. if (gsym)
  9888. value = target->plt_off(gsym, &plt);
  9889. else
  9890. value = target->plt_off(object, r_sym, &plt);
  9891. value += plt->address();
  9892. if (size == 64)
  9893. {
  9894. if (r_type != elfcpp::R_PPC64_PLT_PCREL34
  9895. && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
  9896. value -= target->toc_pointer();
  9897. }
  9898. else if (parameters->options().output_is_position_independent())
  9899. {
  9900. if (rela.get_r_addend() >= 32768)
  9901. {
  9902. unsigned int got2 = object->got2_shndx();
  9903. value -= (object->get_output_section_offset(got2)
  9904. + object->output_section(got2)->address()
  9905. + rela.get_r_addend());
  9906. }
  9907. else
  9908. value -= target->toc_pointer();
  9909. }
  9910. }
  9911. else if (!use_plt_offset
  9912. && (is_plt16_reloc<size>(r_type)
  9913. || r_type == elfcpp::R_POWERPC_PLTSEQ
  9914. || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
  9915. {
  9916. Insn* iview = reinterpret_cast<Insn*>(view);
  9917. elfcpp::Swap<32, big_endian>::writeval(iview, nop);
  9918. r_type = elfcpp::R_POWERPC_NONE;
  9919. }
  9920. else if (!use_plt_offset
  9921. && (r_type == elfcpp::R_PPC64_PLT_PCREL34
  9922. || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
  9923. {
  9924. Insn* iview = reinterpret_cast<Insn*>(view);
  9925. elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
  9926. elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
  9927. r_type = elfcpp::R_POWERPC_NONE;
  9928. }
  9929. else if (is_got_reloc(r_type))
  9930. {
  9931. uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
  9932. Got_type got_type = ((size == 32
  9933. || r_type == elfcpp::R_POWERPC_GOT16
  9934. || r_type == elfcpp::R_PPC64_GOT16_DS)
  9935. ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
  9936. if (gsym != NULL)
  9937. value = gsym->got_offset(got_type, addend);
  9938. else
  9939. value = object->local_got_offset(r_sym, got_type, addend);
  9940. if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
  9941. value += target->got_section(got_type)->address();
  9942. else
  9943. value -= target->got_base_offset(got_type);
  9944. }
  9945. else if (r_type == elfcpp::R_PPC64_TOC)
  9946. {
  9947. value = target->toc_pointer();
  9948. }
  9949. else if (gsym != NULL
  9950. && (r_type == elfcpp::R_POWERPC_REL24
  9951. || r_type == elfcpp::R_PPC_PLTREL24)
  9952. && has_stub_value)
  9953. {
  9954. if (size == 64)
  9955. {
  9956. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  9957. Valtype* wv = reinterpret_cast<Valtype*>(view);
  9958. bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
  9959. if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
  9960. {
  9961. Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
  9962. Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
  9963. if ((insn & 1) != 0
  9964. && (insn2 == nop
  9965. || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
  9966. {
  9967. elfcpp::Swap<32, big_endian>::
  9968. writeval(wv + 1, ld_2_1 + target->stk_toc());
  9969. can_plt_call = true;
  9970. }
  9971. }
  9972. if (!can_plt_call)
  9973. {
  9974. // If we don't have a branch and link followed by a nop,
  9975. // we can't go via the plt because there is no place to
  9976. // put a toc restoring instruction.
  9977. // Unless we know we won't be returning.
  9978. if (strcmp(gsym->name(), "__libc_start_main") == 0)
  9979. can_plt_call = true;
  9980. }
  9981. if (!can_plt_call)
  9982. {
  9983. // g++ as of 20130507 emits self-calls without a
  9984. // following nop. This is arguably wrong since we have
  9985. // conflicting information. On the one hand a global
  9986. // symbol and on the other a local call sequence, but
  9987. // don't error for this special case.
  9988. // It isn't possible to cheaply verify we have exactly
  9989. // such a call. Allow all calls to the same section.
  9990. bool ok = false;
  9991. Address code = value;
  9992. if (gsym->source() == Symbol::FROM_OBJECT
  9993. && gsym->object() == object)
  9994. {
  9995. unsigned int dest_shndx = 0;
  9996. if (target->abiversion() < 2)
  9997. {
  9998. Address addend = rela.get_r_addend();
  9999. code = psymval->value(object, addend);
  10000. target->symval_for_branch(relinfo->symtab, gsym, object,
  10001. &code, &dest_shndx);
  10002. }
  10003. bool is_ordinary;
  10004. if (dest_shndx == 0)
  10005. dest_shndx = gsym->shndx(&is_ordinary);
  10006. ok = dest_shndx == relinfo->data_shndx;
  10007. }
  10008. if (!ok)
  10009. {
  10010. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  10011. _("call lacks nop, can't restore toc; "
  10012. "recompile with -fPIC"));
  10013. value = code;
  10014. }
  10015. }
  10016. }
  10017. }
  10018. else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  10019. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
  10020. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
  10021. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
  10022. || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
  10023. {
  10024. // First instruction of a global dynamic sequence, arg setup insn.
  10025. bool final = gsym == NULL || gsym->final_value_is_known();
  10026. tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
  10027. Got_type got_type = ((size == 32
  10028. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
  10029. ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
  10030. if (tls_type == tls::TLSOPT_NONE)
  10031. got_type = Got_type(got_type | GOT_TYPE_TLSGD);
  10032. else if (tls_type == tls::TLSOPT_TO_IE)
  10033. got_type = Got_type(got_type | GOT_TYPE_TPREL);
  10034. if ((got_type & ~GOT_TYPE_SMALL) != GOT_TYPE_STANDARD)
  10035. {
  10036. uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
  10037. if (gsym != NULL)
  10038. value = gsym->got_offset(got_type, addend);
  10039. else
  10040. value = object->local_got_offset(r_sym, got_type, addend);
  10041. if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
  10042. value += target->got_section(got_type)->address();
  10043. else
  10044. value -= target->got_base_offset(got_type);
  10045. }
  10046. if (tls_type == tls::TLSOPT_TO_IE)
  10047. {
  10048. if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
  10049. {
  10050. Insn* iview = reinterpret_cast<Insn*>(view);
  10051. uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
  10052. pinsn <<= 32;
  10053. pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
  10054. // pla -> pld
  10055. pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
  10056. elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
  10057. elfcpp::Swap<32, big_endian>::writeval(iview + 1,
  10058. pinsn & 0xffffffff);
  10059. r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
  10060. }
  10061. else
  10062. {
  10063. if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  10064. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
  10065. {
  10066. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10067. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10068. insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
  10069. if (size == 32)
  10070. insn |= 32 << 26; // lwz
  10071. else
  10072. insn |= 58 << 26; // ld
  10073. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10074. }
  10075. r_type += (elfcpp::R_POWERPC_GOT_TPREL16
  10076. - elfcpp::R_POWERPC_GOT_TLSGD16);
  10077. }
  10078. }
  10079. else if (tls_type == tls::TLSOPT_TO_LE)
  10080. {
  10081. if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
  10082. {
  10083. Insn* iview = reinterpret_cast<Insn*>(view);
  10084. uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
  10085. pinsn <<= 32;
  10086. pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
  10087. // pla pcrel -> paddi r13
  10088. pinsn += (-1ULL << 52) + (13ULL << 16);
  10089. elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
  10090. elfcpp::Swap<32, big_endian>::writeval(iview + 1,
  10091. pinsn & 0xffffffff);
  10092. r_type = elfcpp::R_PPC64_TPREL34;
  10093. value = psymval->value(object, rela.get_r_addend());
  10094. }
  10095. else
  10096. {
  10097. if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  10098. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
  10099. {
  10100. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10101. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10102. insn &= (1 << 26) - (1 << 21); // extract rt
  10103. if (size == 32)
  10104. insn |= addis_0_2;
  10105. else
  10106. insn |= addis_0_13;
  10107. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10108. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  10109. value = psymval->value(object, rela.get_r_addend());
  10110. }
  10111. else
  10112. {
  10113. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10114. Insn insn = nop;
  10115. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10116. r_type = elfcpp::R_POWERPC_NONE;
  10117. }
  10118. }
  10119. }
  10120. }
  10121. else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
  10122. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
  10123. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
  10124. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
  10125. || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
  10126. {
  10127. // First instruction of a local dynamic sequence, arg setup insn.
  10128. tls::Tls_optimization tls_type = target->optimize_tls_ld();
  10129. if (tls_type == tls::TLSOPT_NONE)
  10130. {
  10131. value = target->tlsld_got_offset();
  10132. if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
  10133. value += target->got_section(GOT_TYPE_SMALL)->address();
  10134. else
  10135. value -= target->got_base_offset(GOT_TYPE_SMALL);
  10136. }
  10137. else
  10138. {
  10139. gold_assert(tls_type == tls::TLSOPT_TO_LE);
  10140. if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
  10141. {
  10142. Insn* iview = reinterpret_cast<Insn*>(view);
  10143. uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
  10144. pinsn <<= 32;
  10145. pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
  10146. // pla pcrel -> paddi r13
  10147. pinsn += (-1ULL << 52) + (13ULL << 16);
  10148. elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
  10149. elfcpp::Swap<32, big_endian>::writeval(iview + 1,
  10150. pinsn & 0xffffffff);
  10151. r_type = elfcpp::R_PPC64_TPREL34;
  10152. value = dtp_offset;
  10153. }
  10154. else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
  10155. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
  10156. {
  10157. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10158. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10159. insn &= (1 << 26) - (1 << 21); // extract rt
  10160. if (size == 32)
  10161. insn |= addis_0_2;
  10162. else
  10163. insn |= addis_0_13;
  10164. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10165. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  10166. value = dtp_offset;
  10167. }
  10168. else
  10169. {
  10170. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10171. Insn insn = nop;
  10172. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10173. r_type = elfcpp::R_POWERPC_NONE;
  10174. }
  10175. }
  10176. }
  10177. else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
  10178. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
  10179. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
  10180. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
  10181. || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
  10182. {
  10183. // Accesses relative to a local dynamic sequence address,
  10184. // no optimisation here.
  10185. uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
  10186. Got_type got_type = ((size == 32
  10187. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
  10188. ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
  10189. if (gsym != NULL)
  10190. value = gsym->got_offset(got_type, addend);
  10191. else
  10192. value = object->local_got_offset(r_sym, got_type, addend);
  10193. if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
  10194. value += target->got_section(got_type)->address();
  10195. else
  10196. value -= target->got_base_offset(got_type);
  10197. }
  10198. else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
  10199. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
  10200. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
  10201. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
  10202. || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
  10203. {
  10204. // First instruction of initial exec sequence.
  10205. bool final = gsym == NULL || gsym->final_value_is_known();
  10206. tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
  10207. if (tls_type == tls::TLSOPT_NONE)
  10208. {
  10209. uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
  10210. Got_type got_type = ((size == 32
  10211. || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
  10212. ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
  10213. if (gsym != NULL)
  10214. value = gsym->got_offset(got_type, addend);
  10215. else
  10216. value = object->local_got_offset(r_sym, got_type, addend);
  10217. if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
  10218. value += target->got_section(got_type)->address();
  10219. else
  10220. value -= target->got_base_offset(got_type);
  10221. }
  10222. else
  10223. {
  10224. gold_assert(tls_type == tls::TLSOPT_TO_LE);
  10225. if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
  10226. {
  10227. Insn* iview = reinterpret_cast<Insn*>(view);
  10228. uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
  10229. pinsn <<= 32;
  10230. pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
  10231. // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
  10232. pinsn += ((2ULL << 56) + (-1ULL << 52)
  10233. + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
  10234. elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
  10235. elfcpp::Swap<32, big_endian>::writeval(iview + 1,
  10236. pinsn & 0xffffffff);
  10237. r_type = elfcpp::R_PPC64_TPREL34;
  10238. value = psymval->value(object, rela.get_r_addend());
  10239. }
  10240. else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
  10241. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
  10242. {
  10243. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10244. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10245. insn &= (1 << 26) - (1 << 21); // extract rt from ld
  10246. if (size == 32)
  10247. insn |= addis_0_2;
  10248. else
  10249. insn |= addis_0_13;
  10250. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10251. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  10252. value = psymval->value(object, rela.get_r_addend());
  10253. }
  10254. else
  10255. {
  10256. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10257. Insn insn = nop;
  10258. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10259. r_type = elfcpp::R_POWERPC_NONE;
  10260. }
  10261. }
  10262. }
  10263. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
  10264. || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
  10265. {
  10266. // Second instruction of a global dynamic sequence,
  10267. // the __tls_get_addr call
  10268. this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
  10269. bool final = gsym == NULL || gsym->final_value_is_known();
  10270. tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
  10271. if (tls_type != tls::TLSOPT_NONE)
  10272. {
  10273. if (tls_type == tls::TLSOPT_TO_IE)
  10274. {
  10275. Insn* iview = reinterpret_cast<Insn*>(view);
  10276. Insn insn = add_3_3_13;
  10277. if (size == 32)
  10278. insn = add_3_3_2;
  10279. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10280. r_type = elfcpp::R_POWERPC_NONE;
  10281. }
  10282. else
  10283. {
  10284. bool is_pcrel = false;
  10285. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  10286. elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
  10287. size_t reloc_count = shdr.get_sh_size() / reloc_size;
  10288. if (relnum < reloc_count - 1)
  10289. {
  10290. Reltype next_rela(preloc + reloc_size);
  10291. unsigned int r_type2
  10292. = elfcpp::elf_r_type<size>(next_rela.get_r_info());
  10293. if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
  10294. || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
  10295. || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
  10296. && next_rela.get_r_offset() == rela.get_r_offset())
  10297. is_pcrel = true;
  10298. }
  10299. Insn* iview = reinterpret_cast<Insn*>(view);
  10300. if (is_pcrel)
  10301. {
  10302. elfcpp::Swap<32, big_endian>::writeval(iview, nop);
  10303. r_type = elfcpp::R_POWERPC_NONE;
  10304. }
  10305. else
  10306. {
  10307. elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
  10308. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  10309. view += d_offset;
  10310. value = psymval->value(object, rela.get_r_addend());
  10311. }
  10312. }
  10313. this->skip_next_tls_get_addr_call();
  10314. }
  10315. }
  10316. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
  10317. || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
  10318. {
  10319. // Second instruction of a local dynamic sequence,
  10320. // the __tls_get_addr call
  10321. this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
  10322. tls::Tls_optimization tls_type = target->optimize_tls_ld();
  10323. if (tls_type == tls::TLSOPT_TO_LE)
  10324. {
  10325. bool is_pcrel = false;
  10326. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  10327. elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
  10328. size_t reloc_count = shdr.get_sh_size() / reloc_size;
  10329. if (relnum < reloc_count - 1)
  10330. {
  10331. Reltype next_rela(preloc + reloc_size);
  10332. unsigned int r_type2
  10333. = elfcpp::elf_r_type<size>(next_rela.get_r_info());
  10334. if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
  10335. || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
  10336. || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
  10337. && next_rela.get_r_offset() == rela.get_r_offset())
  10338. is_pcrel = true;
  10339. }
  10340. Insn* iview = reinterpret_cast<Insn*>(view);
  10341. if (is_pcrel)
  10342. {
  10343. elfcpp::Swap<32, big_endian>::writeval(iview, nop);
  10344. r_type = elfcpp::R_POWERPC_NONE;
  10345. }
  10346. else
  10347. {
  10348. elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
  10349. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  10350. view += d_offset;
  10351. value = dtp_offset;
  10352. }
  10353. this->skip_next_tls_get_addr_call();
  10354. }
  10355. }
  10356. else if (r_type == elfcpp::R_POWERPC_TLS)
  10357. {
  10358. // Second instruction of an initial exec sequence
  10359. bool final = gsym == NULL || gsym->final_value_is_known();
  10360. tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
  10361. if (tls_type == tls::TLSOPT_TO_LE)
  10362. {
  10363. Address roff = rela.get_r_offset() & 3;
  10364. Insn* iview = reinterpret_cast<Insn*>(view - roff);
  10365. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10366. unsigned int reg = size == 32 ? 2 : 13;
  10367. insn = at_tls_transform(insn, reg);
  10368. gold_assert(insn != 0);
  10369. if (roff == 0)
  10370. {
  10371. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10372. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  10373. view += d_offset;
  10374. value = psymval->value(object, rela.get_r_addend());
  10375. }
  10376. else if (roff == 1)
  10377. {
  10378. // For pcrel IE to LE we already have the full offset
  10379. // and thus don't need an addi here. A nop or mr will do.
  10380. if ((insn & (0x3f << 26)) == 14 << 26)
  10381. {
  10382. // Extract regs from addi rt,ra,si.
  10383. unsigned int rt = (insn >> 21) & 0x1f;
  10384. unsigned int ra = (insn >> 16) & 0x1f;
  10385. if (rt == ra)
  10386. insn = nop;
  10387. else
  10388. {
  10389. // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
  10390. insn = (rt << 16) | (ra << 21) | (ra << 11);
  10391. insn |= (31u << 26) | (444u << 1);
  10392. }
  10393. }
  10394. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10395. r_type = elfcpp::R_POWERPC_NONE;
  10396. }
  10397. }
  10398. }
  10399. else if (!has_stub_value)
  10400. {
  10401. if (!use_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
  10402. || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
  10403. {
  10404. // PLTCALL without plt entry => convert to direct call
  10405. Insn* iview = reinterpret_cast<Insn*>(view);
  10406. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10407. insn = (insn & 1) | b;
  10408. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10409. if (size == 32)
  10410. r_type = elfcpp::R_PPC_PLTREL24;
  10411. else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
  10412. r_type = elfcpp::R_PPC64_REL24_NOTOC;
  10413. else
  10414. r_type = elfcpp::R_POWERPC_REL24;
  10415. }
  10416. Address addend = 0;
  10417. if (!(size == 32
  10418. && (r_type == elfcpp::R_PPC_PLTREL24
  10419. || r_type == elfcpp::R_POWERPC_PLT16_LO
  10420. || r_type == elfcpp::R_POWERPC_PLT16_HI
  10421. || r_type == elfcpp::R_POWERPC_PLT16_HA)))
  10422. addend = rela.get_r_addend();
  10423. value = psymval->value(object, addend);
  10424. unsigned int local_ent = 0;
  10425. if (size == 64 && is_branch_reloc<size>(r_type))
  10426. {
  10427. if (target->abiversion() >= 2)
  10428. {
  10429. if (gsym != NULL)
  10430. local_ent = object->ppc64_local_entry_offset(gsym);
  10431. else
  10432. local_ent = object->ppc64_local_entry_offset(r_sym);
  10433. }
  10434. else
  10435. {
  10436. unsigned int dest_shndx;
  10437. target->symval_for_branch(relinfo->symtab, gsym, object,
  10438. &value, &dest_shndx);
  10439. }
  10440. }
  10441. Address max_branch = max_branch_delta<size>(r_type);
  10442. if (max_branch != 0
  10443. && (value + local_ent - address + max_branch >= 2 * max_branch
  10444. || (size == 64
  10445. && (r_type == elfcpp::R_PPC64_REL24_NOTOC
  10446. || r_type == elfcpp::R_PPC64_REL24_NOTOC)
  10447. && (gsym != NULL
  10448. ? object->ppc64_needs_toc(gsym)
  10449. : object->ppc64_needs_toc(r_sym)))))
  10450. {
  10451. Stub_table<size, big_endian>* stub_table
  10452. = object->stub_table(relinfo->data_shndx);
  10453. if (stub_table != NULL)
  10454. {
  10455. const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
  10456. = stub_table->find_long_branch_entry(value);
  10457. if (ent != NULL)
  10458. {
  10459. if (ent->save_res_)
  10460. value = (value - target->savres_section()->address()
  10461. + stub_table->stub_address()
  10462. + stub_table->plt_size()
  10463. + stub_table->branch_size());
  10464. else
  10465. {
  10466. value = (stub_table->stub_address()
  10467. + stub_table->plt_size()
  10468. + ent->off_);
  10469. if (size == 64)
  10470. {
  10471. if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
  10472. {
  10473. if (!ent->notoc_)
  10474. value += ent->p9off_;
  10475. }
  10476. else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
  10477. value += ent->p9off_;
  10478. else
  10479. value += ent->tocoff_;
  10480. }
  10481. }
  10482. has_stub_value = true;
  10483. }
  10484. }
  10485. }
  10486. if (!has_stub_value)
  10487. value += local_ent;
  10488. }
  10489. switch (r_type)
  10490. {
  10491. case elfcpp::R_PPC64_REL24_NOTOC:
  10492. if (size == 32)
  10493. break;
  10494. // Fall through.
  10495. case elfcpp::R_PPC64_REL24_P9NOTOC:
  10496. case elfcpp::R_PPC64_REL64:
  10497. case elfcpp::R_POWERPC_REL32:
  10498. case elfcpp::R_POWERPC_REL24:
  10499. case elfcpp::R_PPC_PLTREL24:
  10500. case elfcpp::R_PPC_LOCAL24PC:
  10501. case elfcpp::R_POWERPC_REL16:
  10502. case elfcpp::R_POWERPC_REL16_LO:
  10503. case elfcpp::R_POWERPC_REL16_HI:
  10504. case elfcpp::R_POWERPC_REL16_HA:
  10505. case elfcpp::R_POWERPC_REL16DX_HA:
  10506. case elfcpp::R_PPC64_REL16_HIGH:
  10507. case elfcpp::R_PPC64_REL16_HIGHA:
  10508. case elfcpp::R_PPC64_REL16_HIGHER:
  10509. case elfcpp::R_PPC64_REL16_HIGHERA:
  10510. case elfcpp::R_PPC64_REL16_HIGHEST:
  10511. case elfcpp::R_PPC64_REL16_HIGHESTA:
  10512. case elfcpp::R_POWERPC_REL14:
  10513. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  10514. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  10515. case elfcpp::R_PPC64_PCREL34:
  10516. case elfcpp::R_PPC64_GOT_PCREL34:
  10517. case elfcpp::R_PPC64_PLT_PCREL34:
  10518. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  10519. case elfcpp::R_PPC64_PCREL28:
  10520. case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
  10521. case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
  10522. case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
  10523. case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
  10524. case elfcpp::R_PPC64_REL16_HIGHER34:
  10525. case elfcpp::R_PPC64_REL16_HIGHERA34:
  10526. case elfcpp::R_PPC64_REL16_HIGHEST34:
  10527. case elfcpp::R_PPC64_REL16_HIGHESTA34:
  10528. value -= address;
  10529. break;
  10530. case elfcpp::R_PPC64_TOC16:
  10531. case elfcpp::R_PPC64_TOC16_LO:
  10532. case elfcpp::R_PPC64_TOC16_HI:
  10533. case elfcpp::R_PPC64_TOC16_HA:
  10534. case elfcpp::R_PPC64_TOC16_DS:
  10535. case elfcpp::R_PPC64_TOC16_LO_DS:
  10536. // Subtract the TOC base address.
  10537. value -= target->toc_pointer();
  10538. break;
  10539. case elfcpp::R_POWERPC_SECTOFF:
  10540. case elfcpp::R_POWERPC_SECTOFF_LO:
  10541. case elfcpp::R_POWERPC_SECTOFF_HI:
  10542. case elfcpp::R_POWERPC_SECTOFF_HA:
  10543. case elfcpp::R_PPC64_SECTOFF_DS:
  10544. case elfcpp::R_PPC64_SECTOFF_LO_DS:
  10545. if (os != NULL)
  10546. value -= os->address();
  10547. break;
  10548. case elfcpp::R_PPC64_TPREL16_DS:
  10549. case elfcpp::R_PPC64_TPREL16_LO_DS:
  10550. case elfcpp::R_PPC64_TPREL16_HIGH:
  10551. case elfcpp::R_PPC64_TPREL16_HIGHA:
  10552. if (size != 64)
  10553. // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
  10554. break;
  10555. // Fall through.
  10556. case elfcpp::R_POWERPC_TPREL16:
  10557. case elfcpp::R_POWERPC_TPREL16_LO:
  10558. case elfcpp::R_POWERPC_TPREL16_HI:
  10559. case elfcpp::R_POWERPC_TPREL16_HA:
  10560. case elfcpp::R_POWERPC_TPREL:
  10561. case elfcpp::R_PPC64_TPREL16_HIGHER:
  10562. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  10563. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  10564. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  10565. case elfcpp::R_PPC64_TPREL34:
  10566. // tls symbol values are relative to tls_segment()->vaddr()
  10567. value -= tp_offset;
  10568. break;
  10569. case elfcpp::R_PPC64_DTPREL16_DS:
  10570. case elfcpp::R_PPC64_DTPREL16_LO_DS:
  10571. case elfcpp::R_PPC64_DTPREL16_HIGHER:
  10572. case elfcpp::R_PPC64_DTPREL16_HIGHERA:
  10573. case elfcpp::R_PPC64_DTPREL16_HIGHEST:
  10574. case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
  10575. if (size != 64)
  10576. // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
  10577. // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
  10578. break;
  10579. // Fall through.
  10580. case elfcpp::R_POWERPC_DTPREL16:
  10581. case elfcpp::R_POWERPC_DTPREL16_LO:
  10582. case elfcpp::R_POWERPC_DTPREL16_HI:
  10583. case elfcpp::R_POWERPC_DTPREL16_HA:
  10584. case elfcpp::R_POWERPC_DTPREL:
  10585. case elfcpp::R_PPC64_DTPREL16_HIGH:
  10586. case elfcpp::R_PPC64_DTPREL16_HIGHA:
  10587. case elfcpp::R_PPC64_DTPREL34:
  10588. // tls symbol values are relative to tls_segment()->vaddr()
  10589. value -= dtp_offset;
  10590. break;
  10591. case elfcpp::R_PPC64_ADDR64_LOCAL:
  10592. if (gsym != NULL)
  10593. value += object->ppc64_local_entry_offset(gsym);
  10594. else
  10595. value += object->ppc64_local_entry_offset(r_sym);
  10596. break;
  10597. default:
  10598. break;
  10599. }
  10600. Insn branch_bit = 0;
  10601. switch (r_type)
  10602. {
  10603. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  10604. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  10605. branch_bit = 1 << 21;
  10606. // Fall through.
  10607. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  10608. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  10609. {
  10610. Insn* iview = reinterpret_cast<Insn*>(view);
  10611. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10612. insn &= ~(1 << 21);
  10613. insn |= branch_bit;
  10614. if (this->is_isa_v2)
  10615. {
  10616. // Set 'a' bit. This is 0b00010 in BO field for branch
  10617. // on CR(BI) insns (BO == 001at or 011at), and 0b01000
  10618. // for branch on CTR insns (BO == 1a00t or 1a01t).
  10619. if ((insn & (0x14 << 21)) == (0x04 << 21))
  10620. insn |= 0x02 << 21;
  10621. else if ((insn & (0x14 << 21)) == (0x10 << 21))
  10622. insn |= 0x08 << 21;
  10623. else
  10624. break;
  10625. }
  10626. else
  10627. {
  10628. // Invert 'y' bit if not the default.
  10629. if (static_cast<Signed_address>(value) < 0)
  10630. insn ^= 1 << 21;
  10631. }
  10632. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10633. }
  10634. break;
  10635. case elfcpp::R_POWERPC_PLT16_HA:
  10636. if (size == 32
  10637. && !parameters->options().output_is_position_independent())
  10638. {
  10639. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10640. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10641. // Convert addis to lis.
  10642. if ((insn & (0x3f << 26)) == 15u << 26
  10643. && (insn & (0x1f << 16)) != 0)
  10644. {
  10645. insn &= ~(0x1f << 16);
  10646. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10647. }
  10648. }
  10649. break;
  10650. default:
  10651. break;
  10652. }
  10653. if (gsym
  10654. ? relative_value_is_known(gsym)
  10655. : relative_value_is_known(psymval))
  10656. {
  10657. Insn* iview;
  10658. Insn* iview2;
  10659. Insn insn;
  10660. uint64_t pinsn, pinsn2;
  10661. switch (r_type)
  10662. {
  10663. default:
  10664. break;
  10665. // Multi-instruction sequences that access the GOT/TOC can
  10666. // be optimized, eg.
  10667. // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
  10668. // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
  10669. // and
  10670. // addis ra,r2,0; addi rb,ra,x@toc@l;
  10671. // to nop; addi rb,r2,x@toc;
  10672. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  10673. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  10674. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  10675. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  10676. case elfcpp::R_POWERPC_GOT16_HA:
  10677. case elfcpp::R_PPC64_TOC16_HA:
  10678. if (size == 64 && parameters->options().toc_optimize())
  10679. {
  10680. iview = reinterpret_cast<Insn*>(view - d_offset);
  10681. insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10682. if ((r_type == elfcpp::R_PPC64_TOC16_HA
  10683. && object->make_toc_relative(target, &value))
  10684. || (r_type == elfcpp::R_POWERPC_GOT16_HA
  10685. && object->make_got_relative(target, psymval,
  10686. rela.get_r_addend(),
  10687. &value)))
  10688. {
  10689. gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
  10690. == ((15u << 26) | (2 << 16)));
  10691. }
  10692. if (((insn & ((0x3f << 26) | 0x1f << 16))
  10693. == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
  10694. && value + 0x8000 < 0x10000)
  10695. {
  10696. elfcpp::Swap<32, big_endian>::writeval(iview, nop);
  10697. return true;
  10698. }
  10699. }
  10700. break;
  10701. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  10702. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  10703. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  10704. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  10705. case elfcpp::R_POWERPC_GOT16_LO:
  10706. case elfcpp::R_PPC64_GOT16_LO_DS:
  10707. case elfcpp::R_PPC64_TOC16_LO:
  10708. case elfcpp::R_PPC64_TOC16_LO_DS:
  10709. if (size == 64 && parameters->options().toc_optimize())
  10710. {
  10711. iview = reinterpret_cast<Insn*>(view - d_offset);
  10712. insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10713. bool changed = false;
  10714. if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
  10715. && object->make_toc_relative(target, &value))
  10716. || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
  10717. && object->make_got_relative(target, psymval,
  10718. rela.get_r_addend(),
  10719. &value)))
  10720. {
  10721. gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
  10722. insn ^= (14u << 26) ^ (58u << 26);
  10723. r_type = elfcpp::R_PPC64_TOC16_LO;
  10724. changed = true;
  10725. }
  10726. if (ok_lo_toc_insn(insn, r_type)
  10727. && value + 0x8000 < 0x10000)
  10728. {
  10729. if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
  10730. {
  10731. // Transform addic to addi when we change reg.
  10732. insn &= ~((0x3f << 26) | (0x1f << 16));
  10733. insn |= (14u << 26) | (2 << 16);
  10734. }
  10735. else
  10736. {
  10737. insn &= ~(0x1f << 16);
  10738. insn |= 2 << 16;
  10739. }
  10740. changed = true;
  10741. }
  10742. if (changed)
  10743. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10744. }
  10745. break;
  10746. case elfcpp::R_PPC64_GOT_PCREL34:
  10747. if (size == 64 && parameters->options().toc_optimize())
  10748. {
  10749. iview = reinterpret_cast<Insn*>(view);
  10750. pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
  10751. pinsn <<= 32;
  10752. pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
  10753. if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
  10754. != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
  10755. break;
  10756. Address relval = psymval->value(object, rela.get_r_addend());
  10757. relval -= address;
  10758. if (relval + (1ULL << 33) < 1ULL << 34)
  10759. {
  10760. value = relval;
  10761. // Replace with paddi
  10762. pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
  10763. elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
  10764. elfcpp::Swap<32, big_endian>::writeval(iview + 1,
  10765. pinsn & 0xffffffff);
  10766. goto pcrelopt;
  10767. }
  10768. }
  10769. break;
  10770. case elfcpp::R_PPC64_PCREL34:
  10771. if (size == 64)
  10772. {
  10773. iview = reinterpret_cast<Insn*>(view);
  10774. pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
  10775. pinsn <<= 32;
  10776. pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
  10777. if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
  10778. != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
  10779. | (14ULL << 26) /* paddi */))
  10780. break;
  10781. pcrelopt:
  10782. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  10783. elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
  10784. size_t reloc_count = shdr.get_sh_size() / reloc_size;
  10785. if (relnum >= reloc_count - 1)
  10786. break;
  10787. Reltype next_rela(preloc + reloc_size);
  10788. if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
  10789. != elfcpp::R_PPC64_PCREL_OPT)
  10790. || next_rela.get_r_offset() != rela.get_r_offset())
  10791. break;
  10792. Address off = next_rela.get_r_addend();
  10793. if (off == 0)
  10794. off = 8; // zero means next insn.
  10795. if (off + rela.get_r_offset() + 4 > view_size)
  10796. break;
  10797. iview2 = reinterpret_cast<Insn*>(view + off);
  10798. pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
  10799. pinsn2 <<= 32;
  10800. if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
  10801. break;
  10802. if (xlate_pcrel_opt(&pinsn, &pinsn2))
  10803. {
  10804. elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
  10805. elfcpp::Swap<32, big_endian>::writeval(iview + 1,
  10806. pinsn & 0xffffffff);
  10807. elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
  10808. }
  10809. }
  10810. break;
  10811. case elfcpp::R_POWERPC_TPREL16_HA:
  10812. if (target->tprel_opt() && value + 0x8000 < 0x10000)
  10813. {
  10814. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10815. elfcpp::Swap<32, big_endian>::writeval(iview, nop);
  10816. return true;
  10817. }
  10818. break;
  10819. case elfcpp::R_PPC64_TPREL16_LO_DS:
  10820. if (size == 32)
  10821. // R_PPC_TLSGD, R_PPC_TLSLD
  10822. break;
  10823. // Fall through.
  10824. case elfcpp::R_POWERPC_TPREL16_LO:
  10825. if (target->tprel_opt() && value + 0x8000 < 0x10000)
  10826. {
  10827. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10828. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  10829. insn &= ~(0x1f << 16);
  10830. insn |= (size == 32 ? 2 : 13) << 16;
  10831. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  10832. }
  10833. break;
  10834. case elfcpp::R_PPC64_ENTRY:
  10835. if (size == 64)
  10836. {
  10837. value = target->toc_pointer();
  10838. if (value + 0x80008000 <= 0xffffffff
  10839. && !parameters->options().output_is_position_independent())
  10840. {
  10841. Insn* iview = reinterpret_cast<Insn*>(view);
  10842. Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
  10843. Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
  10844. if ((insn1 & ~0xfffc) == ld_2_12
  10845. && insn2 == add_2_2_12)
  10846. {
  10847. insn1 = lis_2 + ha(value);
  10848. elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
  10849. insn2 = addi_2_2 + l(value);
  10850. elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
  10851. return true;
  10852. }
  10853. }
  10854. else
  10855. {
  10856. value -= address;
  10857. if (value + 0x80008000 <= 0xffffffff)
  10858. {
  10859. Insn* iview = reinterpret_cast<Insn*>(view);
  10860. Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
  10861. Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
  10862. if ((insn1 & ~0xfffc) == ld_2_12
  10863. && insn2 == add_2_2_12)
  10864. {
  10865. insn1 = addis_2_12 + ha(value);
  10866. elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
  10867. insn2 = addi_2_2 + l(value);
  10868. elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
  10869. return true;
  10870. }
  10871. }
  10872. }
  10873. }
  10874. break;
  10875. case elfcpp::R_POWERPC_REL16_LO:
  10876. // If we are generating a non-PIC executable, edit
  10877. // 0: addis 2,12,.TOC.-0b@ha
  10878. // addi 2,2,.TOC.-0b@l
  10879. // used by ELFv2 global entry points to set up r2, to
  10880. // lis 2,.TOC.@ha
  10881. // addi 2,2,.TOC.@l
  10882. // if .TOC. is in range. */
  10883. if (size == 64
  10884. && value + address - 4 + 0x80008000 <= 0xffffffff
  10885. && relnum + 1 > 1
  10886. && preloc != NULL
  10887. && target->abiversion() >= 2
  10888. && !parameters->options().output_is_position_independent()
  10889. && rela.get_r_addend() == d_offset + 4
  10890. && gsym != NULL
  10891. && strcmp(gsym->name(), ".TOC.") == 0)
  10892. {
  10893. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  10894. Reltype prev_rela(preloc - reloc_size);
  10895. if ((prev_rela.get_r_info()
  10896. == elfcpp::elf_r_info<size>(r_sym,
  10897. elfcpp::R_POWERPC_REL16_HA))
  10898. && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
  10899. && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
  10900. {
  10901. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  10902. Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
  10903. Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
  10904. if ((insn1 & 0xffff0000) == addis_2_12
  10905. && (insn2 & 0xffff0000) == addi_2_2)
  10906. {
  10907. insn1 = lis_2 + ha(value + address - 4);
  10908. elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
  10909. insn2 = addi_2_2 + l(value + address - 4);
  10910. elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
  10911. if (relinfo->rr)
  10912. {
  10913. relinfo->rr->set_strategy(relnum - 1,
  10914. Relocatable_relocs::RELOC_SPECIAL);
  10915. relinfo->rr->set_strategy(relnum,
  10916. Relocatable_relocs::RELOC_SPECIAL);
  10917. }
  10918. return true;
  10919. }
  10920. }
  10921. }
  10922. break;
  10923. }
  10924. }
  10925. typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
  10926. elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
  10927. switch (r_type)
  10928. {
  10929. case elfcpp::R_POWERPC_ADDR32:
  10930. case elfcpp::R_POWERPC_UADDR32:
  10931. if (size == 64)
  10932. overflow = Reloc::CHECK_BITFIELD;
  10933. break;
  10934. case elfcpp::R_POWERPC_REL32:
  10935. case elfcpp::R_POWERPC_REL16DX_HA:
  10936. if (size == 64)
  10937. overflow = Reloc::CHECK_SIGNED;
  10938. break;
  10939. case elfcpp::R_POWERPC_UADDR16:
  10940. overflow = Reloc::CHECK_BITFIELD;
  10941. break;
  10942. case elfcpp::R_POWERPC_ADDR16:
  10943. // We really should have three separate relocations,
  10944. // one for 16-bit data, one for insns with 16-bit signed fields,
  10945. // and one for insns with 16-bit unsigned fields.
  10946. overflow = Reloc::CHECK_BITFIELD;
  10947. if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
  10948. overflow = Reloc::CHECK_LOW_INSN;
  10949. break;
  10950. case elfcpp::R_POWERPC_ADDR16_HI:
  10951. case elfcpp::R_POWERPC_ADDR16_HA:
  10952. case elfcpp::R_POWERPC_GOT16_HI:
  10953. case elfcpp::R_POWERPC_GOT16_HA:
  10954. case elfcpp::R_POWERPC_PLT16_HI:
  10955. case elfcpp::R_POWERPC_PLT16_HA:
  10956. case elfcpp::R_POWERPC_SECTOFF_HI:
  10957. case elfcpp::R_POWERPC_SECTOFF_HA:
  10958. case elfcpp::R_PPC64_TOC16_HI:
  10959. case elfcpp::R_PPC64_TOC16_HA:
  10960. case elfcpp::R_PPC64_PLTGOT16_HI:
  10961. case elfcpp::R_PPC64_PLTGOT16_HA:
  10962. case elfcpp::R_POWERPC_TPREL16_HI:
  10963. case elfcpp::R_POWERPC_TPREL16_HA:
  10964. case elfcpp::R_POWERPC_DTPREL16_HI:
  10965. case elfcpp::R_POWERPC_DTPREL16_HA:
  10966. case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
  10967. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  10968. case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
  10969. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  10970. case elfcpp::R_POWERPC_GOT_TPREL16_HI:
  10971. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  10972. case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
  10973. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  10974. case elfcpp::R_POWERPC_REL16_HI:
  10975. case elfcpp::R_POWERPC_REL16_HA:
  10976. if (size != 32)
  10977. overflow = Reloc::CHECK_HIGH_INSN;
  10978. break;
  10979. case elfcpp::R_POWERPC_REL16:
  10980. case elfcpp::R_PPC64_TOC16:
  10981. case elfcpp::R_POWERPC_GOT16:
  10982. case elfcpp::R_POWERPC_SECTOFF:
  10983. case elfcpp::R_POWERPC_TPREL16:
  10984. case elfcpp::R_POWERPC_DTPREL16:
  10985. case elfcpp::R_POWERPC_GOT_TLSGD16:
  10986. case elfcpp::R_POWERPC_GOT_TLSLD16:
  10987. case elfcpp::R_POWERPC_GOT_TPREL16:
  10988. case elfcpp::R_POWERPC_GOT_DTPREL16:
  10989. overflow = Reloc::CHECK_LOW_INSN;
  10990. break;
  10991. case elfcpp::R_PPC64_REL24_NOTOC:
  10992. if (size == 32)
  10993. break;
  10994. // Fall through.
  10995. case elfcpp::R_PPC64_REL24_P9NOTOC:
  10996. case elfcpp::R_POWERPC_ADDR24:
  10997. case elfcpp::R_POWERPC_ADDR14:
  10998. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  10999. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  11000. case elfcpp::R_PPC64_ADDR16_DS:
  11001. case elfcpp::R_POWERPC_REL24:
  11002. case elfcpp::R_PPC_PLTREL24:
  11003. case elfcpp::R_PPC_LOCAL24PC:
  11004. case elfcpp::R_PPC64_TPREL16_DS:
  11005. case elfcpp::R_PPC64_DTPREL16_DS:
  11006. case elfcpp::R_PPC64_TOC16_DS:
  11007. case elfcpp::R_PPC64_GOT16_DS:
  11008. case elfcpp::R_PPC64_SECTOFF_DS:
  11009. case elfcpp::R_POWERPC_REL14:
  11010. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  11011. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  11012. case elfcpp::R_PPC64_D34:
  11013. case elfcpp::R_PPC64_PCREL34:
  11014. case elfcpp::R_PPC64_GOT_PCREL34:
  11015. case elfcpp::R_PPC64_PLT_PCREL34:
  11016. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  11017. case elfcpp::R_PPC64_D28:
  11018. case elfcpp::R_PPC64_PCREL28:
  11019. case elfcpp::R_PPC64_TPREL34:
  11020. case elfcpp::R_PPC64_DTPREL34:
  11021. case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
  11022. case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
  11023. case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
  11024. case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
  11025. overflow = Reloc::CHECK_SIGNED;
  11026. break;
  11027. }
  11028. Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
  11029. Insn insn = 0;
  11030. if (overflow == Reloc::CHECK_LOW_INSN
  11031. || overflow == Reloc::CHECK_HIGH_INSN)
  11032. {
  11033. insn = elfcpp::Swap<32, big_endian>::readval(iview);
  11034. if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
  11035. overflow = Reloc::CHECK_BITFIELD;
  11036. else if (overflow == Reloc::CHECK_LOW_INSN
  11037. ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
  11038. || (insn & (0x3f << 26)) == 24u << 26 /* ori */
  11039. || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
  11040. : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
  11041. || (insn & (0x3f << 26)) == 25u << 26 /* oris */
  11042. || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
  11043. overflow = Reloc::CHECK_UNSIGNED;
  11044. else
  11045. overflow = Reloc::CHECK_SIGNED;
  11046. }
  11047. bool maybe_dq_reloc = false;
  11048. typename Powerpc_relocate_functions<size, big_endian>::Status status
  11049. = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
  11050. switch (r_type)
  11051. {
  11052. case elfcpp::R_POWERPC_NONE:
  11053. case elfcpp::R_POWERPC_TLS:
  11054. case elfcpp::R_POWERPC_GNU_VTINHERIT:
  11055. case elfcpp::R_POWERPC_GNU_VTENTRY:
  11056. case elfcpp::R_POWERPC_PLTSEQ:
  11057. case elfcpp::R_POWERPC_PLTCALL:
  11058. case elfcpp::R_PPC64_PLTSEQ_NOTOC:
  11059. case elfcpp::R_PPC64_PLTCALL_NOTOC:
  11060. case elfcpp::R_PPC64_PCREL_OPT:
  11061. break;
  11062. case elfcpp::R_PPC64_ADDR64:
  11063. case elfcpp::R_PPC64_REL64:
  11064. case elfcpp::R_PPC64_TOC:
  11065. case elfcpp::R_PPC64_ADDR64_LOCAL:
  11066. Reloc::addr64(view, value);
  11067. break;
  11068. case elfcpp::R_POWERPC_TPREL:
  11069. case elfcpp::R_POWERPC_DTPREL:
  11070. if (size == 64)
  11071. Reloc::addr64(view, value);
  11072. else
  11073. status = Reloc::addr32(view, value, overflow);
  11074. break;
  11075. case elfcpp::R_PPC64_UADDR64:
  11076. Reloc::addr64_u(view, value);
  11077. break;
  11078. case elfcpp::R_POWERPC_ADDR32:
  11079. status = Reloc::addr32(view, value, overflow);
  11080. break;
  11081. case elfcpp::R_POWERPC_REL32:
  11082. case elfcpp::R_POWERPC_UADDR32:
  11083. status = Reloc::addr32_u(view, value, overflow);
  11084. break;
  11085. case elfcpp::R_PPC64_REL24_NOTOC:
  11086. if (size == 32)
  11087. goto unsupp; // R_PPC_EMB_RELSDA
  11088. // Fall through.
  11089. case elfcpp::R_PPC64_REL24_P9NOTOC:
  11090. case elfcpp::R_POWERPC_ADDR24:
  11091. case elfcpp::R_POWERPC_REL24:
  11092. case elfcpp::R_PPC_PLTREL24:
  11093. case elfcpp::R_PPC_LOCAL24PC:
  11094. status = Reloc::addr24(view, value, overflow);
  11095. break;
  11096. case elfcpp::R_POWERPC_GOT_DTPREL16:
  11097. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  11098. case elfcpp::R_POWERPC_GOT_TPREL16:
  11099. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  11100. if (size == 64)
  11101. {
  11102. // On ppc64 these are all ds form
  11103. maybe_dq_reloc = true;
  11104. break;
  11105. }
  11106. // Fall through.
  11107. case elfcpp::R_POWERPC_ADDR16:
  11108. case elfcpp::R_POWERPC_REL16:
  11109. case elfcpp::R_PPC64_TOC16:
  11110. case elfcpp::R_POWERPC_GOT16:
  11111. case elfcpp::R_POWERPC_SECTOFF:
  11112. case elfcpp::R_POWERPC_TPREL16:
  11113. case elfcpp::R_POWERPC_DTPREL16:
  11114. case elfcpp::R_POWERPC_GOT_TLSGD16:
  11115. case elfcpp::R_POWERPC_GOT_TLSLD16:
  11116. case elfcpp::R_POWERPC_ADDR16_LO:
  11117. case elfcpp::R_POWERPC_REL16_LO:
  11118. case elfcpp::R_PPC64_TOC16_LO:
  11119. case elfcpp::R_POWERPC_GOT16_LO:
  11120. case elfcpp::R_POWERPC_PLT16_LO:
  11121. case elfcpp::R_POWERPC_SECTOFF_LO:
  11122. case elfcpp::R_POWERPC_TPREL16_LO:
  11123. case elfcpp::R_POWERPC_DTPREL16_LO:
  11124. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  11125. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  11126. if (size == 64)
  11127. status = Reloc::addr16(view, value, overflow);
  11128. else
  11129. maybe_dq_reloc = true;
  11130. break;
  11131. case elfcpp::R_POWERPC_UADDR16:
  11132. status = Reloc::addr16_u(view, value, overflow);
  11133. break;
  11134. case elfcpp::R_PPC64_ADDR16_HIGH:
  11135. case elfcpp::R_PPC64_TPREL16_HIGH:
  11136. case elfcpp::R_PPC64_DTPREL16_HIGH:
  11137. if (size == 32)
  11138. // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
  11139. goto unsupp;
  11140. // Fall through.
  11141. case elfcpp::R_POWERPC_ADDR16_HI:
  11142. case elfcpp::R_POWERPC_REL16_HI:
  11143. case elfcpp::R_PPC64_REL16_HIGH:
  11144. case elfcpp::R_PPC64_TOC16_HI:
  11145. case elfcpp::R_POWERPC_GOT16_HI:
  11146. case elfcpp::R_POWERPC_PLT16_HI:
  11147. case elfcpp::R_POWERPC_SECTOFF_HI:
  11148. case elfcpp::R_POWERPC_TPREL16_HI:
  11149. case elfcpp::R_POWERPC_DTPREL16_HI:
  11150. case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
  11151. case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
  11152. case elfcpp::R_POWERPC_GOT_TPREL16_HI:
  11153. case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
  11154. Reloc::addr16_hi(view, value);
  11155. break;
  11156. case elfcpp::R_PPC64_ADDR16_HIGHA:
  11157. case elfcpp::R_PPC64_TPREL16_HIGHA:
  11158. case elfcpp::R_PPC64_DTPREL16_HIGHA:
  11159. if (size == 32)
  11160. // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
  11161. goto unsupp;
  11162. // Fall through.
  11163. case elfcpp::R_POWERPC_ADDR16_HA:
  11164. case elfcpp::R_POWERPC_REL16_HA:
  11165. case elfcpp::R_PPC64_REL16_HIGHA:
  11166. case elfcpp::R_PPC64_TOC16_HA:
  11167. case elfcpp::R_POWERPC_GOT16_HA:
  11168. case elfcpp::R_POWERPC_PLT16_HA:
  11169. case elfcpp::R_POWERPC_SECTOFF_HA:
  11170. case elfcpp::R_POWERPC_TPREL16_HA:
  11171. case elfcpp::R_POWERPC_DTPREL16_HA:
  11172. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  11173. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  11174. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  11175. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  11176. Reloc::addr16_ha(view, value);
  11177. break;
  11178. case elfcpp::R_POWERPC_REL16DX_HA:
  11179. status = Reloc::addr16dx_ha(view, value, overflow);
  11180. break;
  11181. case elfcpp::R_PPC64_DTPREL16_HIGHER:
  11182. if (size == 32)
  11183. // R_PPC_EMB_NADDR16_LO
  11184. goto unsupp;
  11185. // Fall through.
  11186. case elfcpp::R_PPC64_ADDR16_HIGHER:
  11187. case elfcpp::R_PPC64_REL16_HIGHER:
  11188. case elfcpp::R_PPC64_TPREL16_HIGHER:
  11189. Reloc::addr16_hi2(view, value);
  11190. break;
  11191. case elfcpp::R_PPC64_DTPREL16_HIGHERA:
  11192. if (size == 32)
  11193. // R_PPC_EMB_NADDR16_HI
  11194. goto unsupp;
  11195. // Fall through.
  11196. case elfcpp::R_PPC64_ADDR16_HIGHERA:
  11197. case elfcpp::R_PPC64_REL16_HIGHERA:
  11198. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  11199. Reloc::addr16_ha2(view, value);
  11200. break;
  11201. case elfcpp::R_PPC64_DTPREL16_HIGHEST:
  11202. if (size == 32)
  11203. // R_PPC_EMB_NADDR16_HA
  11204. goto unsupp;
  11205. // Fall through.
  11206. case elfcpp::R_PPC64_ADDR16_HIGHEST:
  11207. case elfcpp::R_PPC64_REL16_HIGHEST:
  11208. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  11209. Reloc::addr16_hi3(view, value);
  11210. break;
  11211. case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
  11212. if (size == 32)
  11213. // R_PPC_EMB_SDAI16
  11214. goto unsupp;
  11215. // Fall through.
  11216. case elfcpp::R_PPC64_ADDR16_HIGHESTA:
  11217. case elfcpp::R_PPC64_REL16_HIGHESTA:
  11218. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  11219. Reloc::addr16_ha3(view, value);
  11220. break;
  11221. case elfcpp::R_PPC64_DTPREL16_DS:
  11222. case elfcpp::R_PPC64_DTPREL16_LO_DS:
  11223. if (size == 32)
  11224. // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
  11225. goto unsupp;
  11226. // Fall through.
  11227. case elfcpp::R_PPC64_TPREL16_DS:
  11228. case elfcpp::R_PPC64_TPREL16_LO_DS:
  11229. if (size == 32)
  11230. // R_PPC_TLSGD, R_PPC_TLSLD
  11231. break;
  11232. // Fall through.
  11233. case elfcpp::R_PPC64_ADDR16_DS:
  11234. case elfcpp::R_PPC64_ADDR16_LO_DS:
  11235. case elfcpp::R_PPC64_TOC16_DS:
  11236. case elfcpp::R_PPC64_TOC16_LO_DS:
  11237. case elfcpp::R_PPC64_GOT16_DS:
  11238. case elfcpp::R_PPC64_GOT16_LO_DS:
  11239. case elfcpp::R_PPC64_PLT16_LO_DS:
  11240. case elfcpp::R_PPC64_SECTOFF_DS:
  11241. case elfcpp::R_PPC64_SECTOFF_LO_DS:
  11242. maybe_dq_reloc = true;
  11243. break;
  11244. case elfcpp::R_POWERPC_ADDR14:
  11245. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  11246. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  11247. case elfcpp::R_POWERPC_REL14:
  11248. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  11249. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  11250. status = Reloc::addr14(view, value, overflow);
  11251. break;
  11252. case elfcpp::R_POWERPC_COPY:
  11253. case elfcpp::R_POWERPC_GLOB_DAT:
  11254. case elfcpp::R_POWERPC_JMP_SLOT:
  11255. case elfcpp::R_POWERPC_RELATIVE:
  11256. case elfcpp::R_POWERPC_DTPMOD:
  11257. case elfcpp::R_PPC64_JMP_IREL:
  11258. case elfcpp::R_POWERPC_IRELATIVE:
  11259. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  11260. _("unexpected reloc %u in object file"),
  11261. r_type);
  11262. break;
  11263. case elfcpp::R_PPC64_TOCSAVE:
  11264. if (size == 32)
  11265. // R_PPC_EMB_SDA21
  11266. goto unsupp;
  11267. else
  11268. {
  11269. Symbol_location loc;
  11270. loc.object = relinfo->object;
  11271. loc.shndx = relinfo->data_shndx;
  11272. loc.offset = rela.get_r_offset();
  11273. const Tocsave_loc *tocsave = target->tocsave_loc();
  11274. if (tocsave->find(loc) != tocsave->end())
  11275. {
  11276. // If we've generated plt calls using this tocsave, then
  11277. // the nop needs to be changed to save r2.
  11278. Insn* iview = reinterpret_cast<Insn*>(view);
  11279. if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
  11280. elfcpp::Swap<32, big_endian>::
  11281. writeval(iview, std_2_1 + target->stk_toc());
  11282. }
  11283. }
  11284. break;
  11285. case elfcpp::R_PPC_EMB_SDA2I16:
  11286. case elfcpp::R_PPC_EMB_SDA2REL:
  11287. if (size == 32)
  11288. goto unsupp;
  11289. // R_PPC64_TLSGD, R_PPC64_TLSLD
  11290. break;
  11291. case elfcpp::R_PPC64_D34:
  11292. case elfcpp::R_PPC64_D34_LO:
  11293. case elfcpp::R_PPC64_PCREL34:
  11294. case elfcpp::R_PPC64_GOT_PCREL34:
  11295. case elfcpp::R_PPC64_PLT_PCREL34:
  11296. case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
  11297. case elfcpp::R_PPC64_TPREL34:
  11298. case elfcpp::R_PPC64_DTPREL34:
  11299. case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
  11300. case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
  11301. case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
  11302. case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
  11303. if (size == 32)
  11304. goto unsupp;
  11305. status = Reloc::addr34(view, value, overflow);
  11306. break;
  11307. case elfcpp::R_PPC64_D34_HI30:
  11308. if (size == 32)
  11309. goto unsupp;
  11310. Reloc::addr34_hi(view, value);
  11311. break;
  11312. case elfcpp::R_PPC64_D34_HA30:
  11313. if (size == 32)
  11314. goto unsupp;
  11315. Reloc::addr34_ha(view, value);
  11316. break;
  11317. case elfcpp::R_PPC64_D28:
  11318. case elfcpp::R_PPC64_PCREL28:
  11319. if (size == 32)
  11320. goto unsupp;
  11321. status = Reloc::addr28(view, value, overflow);
  11322. break;
  11323. case elfcpp::R_PPC64_ADDR16_HIGHER34:
  11324. case elfcpp::R_PPC64_REL16_HIGHER34:
  11325. if (size == 32)
  11326. goto unsupp;
  11327. Reloc::addr16_higher34(view, value);
  11328. break;
  11329. case elfcpp::R_PPC64_ADDR16_HIGHERA34:
  11330. case elfcpp::R_PPC64_REL16_HIGHERA34:
  11331. if (size == 32)
  11332. goto unsupp;
  11333. Reloc::addr16_highera34(view, value);
  11334. break;
  11335. case elfcpp::R_PPC64_ADDR16_HIGHEST34:
  11336. case elfcpp::R_PPC64_REL16_HIGHEST34:
  11337. if (size == 32)
  11338. goto unsupp;
  11339. Reloc::addr16_highest34(view, value);
  11340. break;
  11341. case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
  11342. case elfcpp::R_PPC64_REL16_HIGHESTA34:
  11343. if (size == 32)
  11344. goto unsupp;
  11345. Reloc::addr16_highesta34(view, value);
  11346. break;
  11347. case elfcpp::R_POWERPC_PLT32:
  11348. case elfcpp::R_POWERPC_PLTREL32:
  11349. case elfcpp::R_PPC_SDAREL16:
  11350. case elfcpp::R_POWERPC_ADDR30:
  11351. case elfcpp::R_PPC64_PLT64:
  11352. case elfcpp::R_PPC64_PLTREL64:
  11353. case elfcpp::R_PPC64_PLTGOT16:
  11354. case elfcpp::R_PPC64_PLTGOT16_LO:
  11355. case elfcpp::R_PPC64_PLTGOT16_HI:
  11356. case elfcpp::R_PPC64_PLTGOT16_HA:
  11357. case elfcpp::R_PPC64_PLTGOT16_DS:
  11358. case elfcpp::R_PPC64_PLTGOT16_LO_DS:
  11359. case elfcpp::R_PPC_TOC16:
  11360. default:
  11361. unsupp:
  11362. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  11363. _("unsupported reloc %u"),
  11364. r_type);
  11365. break;
  11366. }
  11367. if (maybe_dq_reloc)
  11368. {
  11369. if (insn == 0)
  11370. insn = elfcpp::Swap<32, big_endian>::readval(iview);
  11371. if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
  11372. || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
  11373. && (insn & 3) == 1))
  11374. status = Reloc::addr16_dq(view, value, overflow);
  11375. else if (size == 64
  11376. || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
  11377. || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
  11378. || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
  11379. || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
  11380. status = Reloc::addr16_ds(view, value, overflow);
  11381. else
  11382. status = Reloc::addr16(view, value, overflow);
  11383. }
  11384. if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
  11385. && (has_stub_value
  11386. || !(gsym != NULL
  11387. && gsym->is_undefined()
  11388. && is_branch_reloc<size>(r_type))))
  11389. {
  11390. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  11391. _("relocation overflow"));
  11392. if (has_stub_value)
  11393. gold_info(_("try relinking with a smaller --stub-group-size"));
  11394. }
  11395. return true;
  11396. }
  11397. // Relocate section data.
  11398. template<int size, bool big_endian>
  11399. void
  11400. Target_powerpc<size, big_endian>::relocate_section(
  11401. const Relocate_info<size, big_endian>* relinfo,
  11402. unsigned int sh_type,
  11403. const unsigned char* prelocs,
  11404. size_t reloc_count,
  11405. Output_section* output_section,
  11406. bool needs_special_offset_handling,
  11407. unsigned char* view,
  11408. Address address,
  11409. section_size_type view_size,
  11410. const Reloc_symbol_changes* reloc_symbol_changes)
  11411. {
  11412. typedef Target_powerpc<size, big_endian> Powerpc;
  11413. typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
  11414. typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
  11415. Powerpc_comdat_behavior;
  11416. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  11417. Classify_reloc;
  11418. gold_assert(sh_type == elfcpp::SHT_RELA);
  11419. gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
  11420. Powerpc_comdat_behavior, Classify_reloc>(
  11421. relinfo,
  11422. this,
  11423. prelocs,
  11424. reloc_count,
  11425. output_section,
  11426. needs_special_offset_handling,
  11427. view,
  11428. address,
  11429. view_size,
  11430. reloc_symbol_changes);
  11431. }
  11432. template<int size, bool big_endian>
  11433. class Powerpc_scan_relocatable_reloc
  11434. {
  11435. public:
  11436. typedef typename elfcpp::Rela<size, big_endian> Reltype;
  11437. static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  11438. static const int sh_type = elfcpp::SHT_RELA;
  11439. // Return the symbol referred to by the relocation.
  11440. static inline unsigned int
  11441. get_r_sym(const Reltype* reloc)
  11442. { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
  11443. // Return the type of the relocation.
  11444. static inline unsigned int
  11445. get_r_type(const Reltype* reloc)
  11446. { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
  11447. // Return the strategy to use for a local symbol which is not a
  11448. // section symbol, given the relocation type.
  11449. inline Relocatable_relocs::Reloc_strategy
  11450. local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
  11451. {
  11452. if (r_type == 0 && r_sym == 0)
  11453. return Relocatable_relocs::RELOC_DISCARD;
  11454. return Relocatable_relocs::RELOC_COPY;
  11455. }
  11456. // Return the strategy to use for a local symbol which is a section
  11457. // symbol, given the relocation type.
  11458. inline Relocatable_relocs::Reloc_strategy
  11459. local_section_strategy(unsigned int, Relobj*)
  11460. {
  11461. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  11462. }
  11463. // Return the strategy to use for a global symbol, given the
  11464. // relocation type, the object, and the symbol index.
  11465. inline Relocatable_relocs::Reloc_strategy
  11466. global_strategy(unsigned int r_type, Relobj*, unsigned int)
  11467. {
  11468. if (size == 32
  11469. && (r_type == elfcpp::R_PPC_PLTREL24
  11470. || r_type == elfcpp::R_POWERPC_PLT16_LO
  11471. || r_type == elfcpp::R_POWERPC_PLT16_HI
  11472. || r_type == elfcpp::R_POWERPC_PLT16_HA))
  11473. return Relocatable_relocs::RELOC_SPECIAL;
  11474. return Relocatable_relocs::RELOC_COPY;
  11475. }
  11476. };
  11477. // Scan the relocs during a relocatable link.
  11478. template<int size, bool big_endian>
  11479. void
  11480. Target_powerpc<size, big_endian>::scan_relocatable_relocs(
  11481. Symbol_table* symtab,
  11482. Layout* layout,
  11483. Sized_relobj_file<size, big_endian>* object,
  11484. unsigned int data_shndx,
  11485. unsigned int sh_type,
  11486. const unsigned char* prelocs,
  11487. size_t reloc_count,
  11488. Output_section* output_section,
  11489. bool needs_special_offset_handling,
  11490. size_t local_symbol_count,
  11491. const unsigned char* plocal_symbols,
  11492. Relocatable_relocs* rr)
  11493. {
  11494. typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
  11495. gold_assert(sh_type == elfcpp::SHT_RELA);
  11496. gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
  11497. symtab,
  11498. layout,
  11499. object,
  11500. data_shndx,
  11501. prelocs,
  11502. reloc_count,
  11503. output_section,
  11504. needs_special_offset_handling,
  11505. local_symbol_count,
  11506. plocal_symbols,
  11507. rr);
  11508. }
  11509. // Scan the relocs for --emit-relocs.
  11510. template<int size, bool big_endian>
  11511. void
  11512. Target_powerpc<size, big_endian>::emit_relocs_scan(
  11513. Symbol_table* symtab,
  11514. Layout* layout,
  11515. Sized_relobj_file<size, big_endian>* object,
  11516. unsigned int data_shndx,
  11517. unsigned int sh_type,
  11518. const unsigned char* prelocs,
  11519. size_t reloc_count,
  11520. Output_section* output_section,
  11521. bool needs_special_offset_handling,
  11522. size_t local_symbol_count,
  11523. const unsigned char* plocal_syms,
  11524. Relocatable_relocs* rr)
  11525. {
  11526. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  11527. Classify_reloc;
  11528. typedef gold::Default_emit_relocs_strategy<Classify_reloc>
  11529. Emit_relocs_strategy;
  11530. gold_assert(sh_type == elfcpp::SHT_RELA);
  11531. gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
  11532. symtab,
  11533. layout,
  11534. object,
  11535. data_shndx,
  11536. prelocs,
  11537. reloc_count,
  11538. output_section,
  11539. needs_special_offset_handling,
  11540. local_symbol_count,
  11541. plocal_syms,
  11542. rr);
  11543. }
  11544. // Emit relocations for a section.
  11545. // This is a modified version of the function by the same name in
  11546. // target-reloc.h. Using relocate_special_relocatable for
  11547. // R_PPC_PLTREL24 would require duplication of the entire body of the
  11548. // loop, so we may as well duplicate the whole thing.
  11549. template<int size, bool big_endian>
  11550. void
  11551. Target_powerpc<size, big_endian>::relocate_relocs(
  11552. const Relocate_info<size, big_endian>* relinfo,
  11553. unsigned int sh_type,
  11554. const unsigned char* prelocs,
  11555. size_t reloc_count,
  11556. Output_section* output_section,
  11557. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  11558. unsigned char*,
  11559. Address view_address,
  11560. section_size_type,
  11561. unsigned char* reloc_view,
  11562. section_size_type reloc_view_size)
  11563. {
  11564. gold_assert(sh_type == elfcpp::SHT_RELA);
  11565. typedef typename elfcpp::Rela<size, big_endian> Reltype;
  11566. typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
  11567. const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  11568. // Offset from start of insn to d-field reloc.
  11569. const int d_offset = big_endian ? 2 : 0;
  11570. Powerpc_relobj<size, big_endian>* const object
  11571. = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
  11572. const unsigned int local_count = object->local_symbol_count();
  11573. unsigned int got2_shndx = object->got2_shndx();
  11574. Address got2_addend = 0;
  11575. if (got2_shndx != 0)
  11576. {
  11577. got2_addend = object->get_output_section_offset(got2_shndx);
  11578. gold_assert(got2_addend != invalid_address);
  11579. }
  11580. const bool relocatable = parameters->options().relocatable();
  11581. unsigned char* pwrite = reloc_view;
  11582. bool zap_next = false;
  11583. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  11584. {
  11585. Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
  11586. if (strategy == Relocatable_relocs::RELOC_DISCARD)
  11587. continue;
  11588. Reltype reloc(prelocs);
  11589. Reltype_write reloc_write(pwrite);
  11590. Address offset = reloc.get_r_offset();
  11591. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  11592. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  11593. unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  11594. const unsigned int orig_r_sym = r_sym;
  11595. typename elfcpp::Elf_types<size>::Elf_Swxword addend
  11596. = reloc.get_r_addend();
  11597. const Symbol* gsym = NULL;
  11598. if (zap_next)
  11599. {
  11600. // We could arrange to discard these and other relocs for
  11601. // tls optimised sequences in the strategy methods, but for
  11602. // now do as BFD ld does.
  11603. r_type = elfcpp::R_POWERPC_NONE;
  11604. zap_next = false;
  11605. }
  11606. // Get the new symbol index.
  11607. Output_section* os = NULL;
  11608. if (r_sym < local_count)
  11609. {
  11610. switch (strategy)
  11611. {
  11612. case Relocatable_relocs::RELOC_COPY:
  11613. case Relocatable_relocs::RELOC_SPECIAL:
  11614. if (r_sym != 0)
  11615. {
  11616. r_sym = object->symtab_index(r_sym);
  11617. gold_assert(r_sym != -1U);
  11618. }
  11619. break;
  11620. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
  11621. {
  11622. // We are adjusting a section symbol. We need to find
  11623. // the symbol table index of the section symbol for
  11624. // the output section corresponding to input section
  11625. // in which this symbol is defined.
  11626. gold_assert(r_sym < local_count);
  11627. bool is_ordinary;
  11628. unsigned int shndx =
  11629. object->local_symbol_input_shndx(r_sym, &is_ordinary);
  11630. gold_assert(is_ordinary);
  11631. os = object->output_section(shndx);
  11632. gold_assert(os != NULL);
  11633. gold_assert(os->needs_symtab_index());
  11634. r_sym = os->symtab_index();
  11635. }
  11636. break;
  11637. default:
  11638. gold_unreachable();
  11639. }
  11640. }
  11641. else
  11642. {
  11643. gsym = object->global_symbol(r_sym);
  11644. gold_assert(gsym != NULL);
  11645. if (gsym->is_forwarder())
  11646. gsym = relinfo->symtab->resolve_forwards(gsym);
  11647. gold_assert(gsym->has_symtab_index());
  11648. r_sym = gsym->symtab_index();
  11649. }
  11650. // Get the new offset--the location in the output section where
  11651. // this relocation should be applied.
  11652. if (static_cast<Address>(offset_in_output_section) != invalid_address)
  11653. offset += offset_in_output_section;
  11654. else
  11655. {
  11656. section_offset_type sot_offset =
  11657. convert_types<section_offset_type, Address>(offset);
  11658. section_offset_type new_sot_offset =
  11659. output_section->output_offset(object, relinfo->data_shndx,
  11660. sot_offset);
  11661. gold_assert(new_sot_offset != -1);
  11662. offset = new_sot_offset;
  11663. }
  11664. // In an object file, r_offset is an offset within the section.
  11665. // In an executable or dynamic object, generated by
  11666. // --emit-relocs, r_offset is an absolute address.
  11667. if (!relocatable)
  11668. {
  11669. offset += view_address;
  11670. if (static_cast<Address>(offset_in_output_section) != invalid_address)
  11671. offset -= offset_in_output_section;
  11672. }
  11673. // Handle the reloc addend based on the strategy.
  11674. if (strategy == Relocatable_relocs::RELOC_COPY)
  11675. ;
  11676. else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
  11677. {
  11678. const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
  11679. addend = psymval->value(object, addend);
  11680. // In a relocatable link, the symbol value is relative to
  11681. // the start of the output section. For a non-relocatable
  11682. // link, we need to adjust the addend.
  11683. if (!relocatable)
  11684. {
  11685. gold_assert(os != NULL);
  11686. addend -= os->address();
  11687. }
  11688. }
  11689. else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
  11690. {
  11691. if (size == 32)
  11692. {
  11693. if (addend >= 32768)
  11694. addend += got2_addend;
  11695. }
  11696. else if (r_type == elfcpp::R_POWERPC_REL16_HA)
  11697. {
  11698. r_type = elfcpp::R_POWERPC_ADDR16_HA;
  11699. addend -= d_offset;
  11700. }
  11701. else if (r_type == elfcpp::R_POWERPC_REL16_LO)
  11702. {
  11703. r_type = elfcpp::R_POWERPC_ADDR16_LO;
  11704. addend -= d_offset + 4;
  11705. }
  11706. }
  11707. else
  11708. gold_unreachable();
  11709. if (!relocatable)
  11710. {
  11711. if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  11712. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
  11713. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
  11714. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
  11715. {
  11716. // First instruction of a global dynamic sequence,
  11717. // arg setup insn.
  11718. bool final = gsym == NULL || gsym->final_value_is_known();
  11719. tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
  11720. switch (tls_type)
  11721. {
  11722. case tls::TLSOPT_TO_IE:
  11723. r_type += (elfcpp::R_POWERPC_GOT_TPREL16
  11724. - elfcpp::R_POWERPC_GOT_TLSGD16);
  11725. break;
  11726. case tls::TLSOPT_TO_LE:
  11727. if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  11728. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
  11729. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  11730. else
  11731. {
  11732. r_type = elfcpp::R_POWERPC_NONE;
  11733. offset -= d_offset;
  11734. }
  11735. break;
  11736. default:
  11737. break;
  11738. }
  11739. }
  11740. else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
  11741. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
  11742. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
  11743. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
  11744. {
  11745. // First instruction of a local dynamic sequence,
  11746. // arg setup insn.
  11747. tls::Tls_optimization tls_type = this->optimize_tls_ld();
  11748. if (tls_type == tls::TLSOPT_TO_LE)
  11749. {
  11750. if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
  11751. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
  11752. {
  11753. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  11754. const Output_section* os = relinfo->layout->tls_segment()
  11755. ->first_section();
  11756. gold_assert(os != NULL);
  11757. gold_assert(os->needs_symtab_index());
  11758. r_sym = os->symtab_index();
  11759. addend = dtp_offset;
  11760. }
  11761. else
  11762. {
  11763. r_type = elfcpp::R_POWERPC_NONE;
  11764. offset -= d_offset;
  11765. }
  11766. }
  11767. }
  11768. else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
  11769. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
  11770. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
  11771. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
  11772. {
  11773. // First instruction of initial exec sequence.
  11774. bool final = gsym == NULL || gsym->final_value_is_known();
  11775. if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
  11776. {
  11777. if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
  11778. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
  11779. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  11780. else
  11781. {
  11782. r_type = elfcpp::R_POWERPC_NONE;
  11783. offset -= d_offset;
  11784. }
  11785. }
  11786. }
  11787. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
  11788. || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
  11789. {
  11790. // Second instruction of a global dynamic sequence,
  11791. // the __tls_get_addr call
  11792. bool final = gsym == NULL || gsym->final_value_is_known();
  11793. tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
  11794. switch (tls_type)
  11795. {
  11796. case tls::TLSOPT_TO_IE:
  11797. r_type = elfcpp::R_POWERPC_NONE;
  11798. zap_next = true;
  11799. break;
  11800. case tls::TLSOPT_TO_LE:
  11801. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  11802. offset += d_offset;
  11803. zap_next = true;
  11804. break;
  11805. default:
  11806. break;
  11807. }
  11808. }
  11809. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
  11810. || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
  11811. {
  11812. // Second instruction of a local dynamic sequence,
  11813. // the __tls_get_addr call
  11814. tls::Tls_optimization tls_type = this->optimize_tls_ld();
  11815. if (tls_type == tls::TLSOPT_TO_LE)
  11816. {
  11817. const Output_section* os = relinfo->layout->tls_segment()
  11818. ->first_section();
  11819. gold_assert(os != NULL);
  11820. gold_assert(os->needs_symtab_index());
  11821. r_sym = os->symtab_index();
  11822. addend = dtp_offset;
  11823. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  11824. offset += d_offset;
  11825. zap_next = true;
  11826. }
  11827. }
  11828. else if (r_type == elfcpp::R_POWERPC_TLS)
  11829. {
  11830. // Second instruction of an initial exec sequence
  11831. bool final = gsym == NULL || gsym->final_value_is_known();
  11832. if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
  11833. {
  11834. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  11835. offset += d_offset;
  11836. }
  11837. }
  11838. }
  11839. reloc_write.put_r_offset(offset);
  11840. reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
  11841. reloc_write.put_r_addend(addend);
  11842. pwrite += reloc_size;
  11843. }
  11844. gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
  11845. == reloc_view_size);
  11846. }
  11847. // Return the value to use for a dynamic symbol which requires special
  11848. // treatment. This is how we support equality comparisons of function
  11849. // pointers across shared library boundaries, as described in the
  11850. // processor specific ABI supplement.
  11851. template<int size, bool big_endian>
  11852. uint64_t
  11853. Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
  11854. {
  11855. if (size == 32)
  11856. {
  11857. gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
  11858. for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
  11859. p != this->stub_tables_.end();
  11860. ++p)
  11861. {
  11862. const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
  11863. = (*p)->find_plt_call_entry(gsym);
  11864. if (ent != NULL)
  11865. return (*p)->stub_address() + ent->off_;
  11866. }
  11867. }
  11868. else if (this->abiversion() >= 2)
  11869. {
  11870. Address off = this->glink_section()->find_global_entry(gsym);
  11871. if (off != invalid_address)
  11872. return this->glink_section()->global_entry_address() + off;
  11873. }
  11874. gold_unreachable();
  11875. }
  11876. // Return the PLT address to use for a local symbol.
  11877. template<int size, bool big_endian>
  11878. uint64_t
  11879. Target_powerpc<size, big_endian>::do_plt_address_for_local(
  11880. const Relobj* object,
  11881. unsigned int symndx) const
  11882. {
  11883. if (size == 32)
  11884. {
  11885. const Sized_relobj<size, big_endian>* relobj
  11886. = static_cast<const Sized_relobj<size, big_endian>*>(object);
  11887. for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
  11888. p != this->stub_tables_.end();
  11889. ++p)
  11890. {
  11891. const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
  11892. = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
  11893. if (ent != NULL)
  11894. return (*p)->stub_address() + ent->off_;
  11895. }
  11896. }
  11897. gold_unreachable();
  11898. }
  11899. // Return the PLT address to use for a global symbol.
  11900. template<int size, bool big_endian>
  11901. uint64_t
  11902. Target_powerpc<size, big_endian>::do_plt_address_for_global(
  11903. const Symbol* gsym) const
  11904. {
  11905. if (size == 32)
  11906. {
  11907. for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
  11908. p != this->stub_tables_.end();
  11909. ++p)
  11910. {
  11911. const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
  11912. = (*p)->find_plt_call_entry(gsym);
  11913. if (ent != NULL)
  11914. return (*p)->stub_address() + ent->off_;
  11915. }
  11916. }
  11917. else if (this->abiversion() >= 2)
  11918. {
  11919. Address off = this->glink_section()->find_global_entry(gsym);
  11920. if (off != invalid_address)
  11921. return this->glink_section()->global_entry_address() + off;
  11922. }
  11923. gold_unreachable();
  11924. }
  11925. // Return the offset to use for the GOT_INDX'th got entry which is
  11926. // for a local tls symbol specified by OBJECT, SYMNDX.
  11927. template<int size, bool big_endian>
  11928. int64_t
  11929. Target_powerpc<size, big_endian>::do_tls_offset_for_local(
  11930. const Relobj* object,
  11931. unsigned int symndx,
  11932. Output_data_got_base* got,
  11933. unsigned int got_indx,
  11934. uint64_t addend) const
  11935. {
  11936. const Powerpc_relobj<size, big_endian>* ppc_object
  11937. = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
  11938. if (ppc_object->local_symbol(symndx)->is_tls_symbol())
  11939. {
  11940. for (Got_type got_type = (size == 32
  11941. ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
  11942. got_type <= GOT_TYPE_SMALL_TPREL;
  11943. got_type = Got_type(got_type + 1))
  11944. if (got_type != GOT_TYPE_SMALL
  11945. && ppc_object->local_has_got_offset(symndx, got_type, addend))
  11946. {
  11947. unsigned int off
  11948. = ppc_object->local_got_offset(symndx, got_type, addend);
  11949. if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
  11950. off += size / 8;
  11951. if (off == got_indx * (size / 8)
  11952. && (size == 32 || got == this->got_section(got_type)))
  11953. {
  11954. if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
  11955. return -tp_offset;
  11956. else
  11957. return -dtp_offset;
  11958. }
  11959. }
  11960. }
  11961. gold_unreachable();
  11962. }
  11963. // Return the offset to use for the GOT_INDX'th got entry which is
  11964. // for global tls symbol GSYM.
  11965. template<int size, bool big_endian>
  11966. int64_t
  11967. Target_powerpc<size, big_endian>::do_tls_offset_for_global(
  11968. Symbol* gsym,
  11969. Output_data_got_base* got,
  11970. unsigned int got_indx,
  11971. uint64_t addend) const
  11972. {
  11973. if (gsym->type() == elfcpp::STT_TLS)
  11974. {
  11975. for (Got_type got_type = (size == 32
  11976. ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
  11977. got_type <= GOT_TYPE_SMALL_TPREL;
  11978. got_type = Got_type(got_type + 1))
  11979. if (got_type != GOT_TYPE_SMALL
  11980. && gsym->has_got_offset(got_type, addend))
  11981. {
  11982. unsigned int off = gsym->got_offset(got_type, addend);
  11983. if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
  11984. off += size / 8;
  11985. if (off == got_indx * (size / 8)
  11986. && (size == 32 || got == this->got_section(got_type)))
  11987. {
  11988. if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
  11989. return -tp_offset;
  11990. else
  11991. return -dtp_offset;
  11992. }
  11993. }
  11994. }
  11995. gold_unreachable();
  11996. }
  11997. // The selector for powerpc object files.
  11998. template<int size, bool big_endian>
  11999. class Target_selector_powerpc : public Target_selector
  12000. {
  12001. public:
  12002. Target_selector_powerpc()
  12003. : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
  12004. size, big_endian,
  12005. (size == 64
  12006. ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
  12007. : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
  12008. (size == 64
  12009. ? (big_endian ? "elf64ppc" : "elf64lppc")
  12010. : (big_endian ? "elf32ppc" : "elf32lppc")))
  12011. { }
  12012. virtual Target*
  12013. do_instantiate_target()
  12014. { return new Target_powerpc<size, big_endian>(); }
  12015. };
  12016. Target_selector_powerpc<32, true> target_selector_ppc32;
  12017. Target_selector_powerpc<32, false> target_selector_ppc32le;
  12018. Target_selector_powerpc<64, true> target_selector_ppc64;
  12019. Target_selector_powerpc<64, false> target_selector_ppc64le;
  12020. // Instantiate these constants for -O0
  12021. template<int size, bool big_endian>
  12022. const typename Output_data_glink<size, big_endian>::Address
  12023. Output_data_glink<size, big_endian>::invalid_address;
  12024. template<int size, bool big_endian>
  12025. const typename Stub_table<size, big_endian>::Address
  12026. Stub_table<size, big_endian>::invalid_address;
  12027. template<int size, bool big_endian>
  12028. const typename Target_powerpc<size, big_endian>::Address
  12029. Target_powerpc<size, big_endian>::invalid_address;
  12030. } // End anonymous namespace.