elf64-ppc.c 528 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355
  1. /* PowerPC64-specific support for 64-bit ELF.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Linus Nordberg, Swox AB <info@swox.com>,
  4. based on elf32-ppc.c by Ian Lance Taylor.
  5. Largely rewritten by Alan Modra.
  6. This file is part of BFD, the Binary File Descriptor library.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along
  16. with this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  18. /* The 64-bit PowerPC ELF ABI may be found at
  19. http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
  20. http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
  21. /* The assembler should generate a full set of section symbols even
  22. when they appear unused. The linux kernel build tool recordmcount
  23. needs them. */
  24. #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
  25. #include "sysdep.h"
  26. #include <stdarg.h>
  27. #include "bfd.h"
  28. #include "bfdlink.h"
  29. #include "libbfd.h"
  30. #include "elf-bfd.h"
  31. #include "elf/ppc64.h"
  32. #include "elf64-ppc.h"
  33. #include "dwarf2.h"
  34. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  35. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  36. static bfd_reloc_status_type ppc64_elf_ha_reloc
  37. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  38. static bfd_reloc_status_type ppc64_elf_branch_reloc
  39. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  40. static bfd_reloc_status_type ppc64_elf_brtaken_reloc
  41. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  42. static bfd_reloc_status_type ppc64_elf_sectoff_reloc
  43. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  44. static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
  45. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  46. static bfd_reloc_status_type ppc64_elf_toc_reloc
  47. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  48. static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
  49. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  50. static bfd_reloc_status_type ppc64_elf_toc64_reloc
  51. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  52. static bfd_reloc_status_type ppc64_elf_prefix_reloc
  53. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  54. static bfd_reloc_status_type ppc64_elf_unhandled_reloc
  55. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  56. static bfd_vma opd_entry_value
  57. (asection *, bfd_vma, asection **, bfd_vma *, bool);
  58. #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
  59. #define TARGET_LITTLE_NAME "elf64-powerpcle"
  60. #define TARGET_BIG_SYM powerpc_elf64_vec
  61. #define TARGET_BIG_NAME "elf64-powerpc"
  62. #define ELF_ARCH bfd_arch_powerpc
  63. #define ELF_TARGET_ID PPC64_ELF_DATA
  64. #define ELF_MACHINE_CODE EM_PPC64
  65. #define ELF_MAXPAGESIZE 0x10000
  66. #define ELF_COMMONPAGESIZE 0x1000
  67. #define elf_info_to_howto ppc64_elf_info_to_howto
  68. #define elf_backend_want_got_sym 0
  69. #define elf_backend_want_plt_sym 0
  70. #define elf_backend_plt_alignment 3
  71. #define elf_backend_plt_not_loaded 1
  72. #define elf_backend_got_header_size 8
  73. #define elf_backend_want_dynrelro 1
  74. #define elf_backend_can_gc_sections 1
  75. #define elf_backend_can_refcount 1
  76. #define elf_backend_rela_normal 1
  77. #define elf_backend_dtrel_excludes_plt 1
  78. #define elf_backend_default_execstack 0
  79. #define bfd_elf64_mkobject ppc64_elf_mkobject
  80. #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
  81. #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
  82. #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
  83. #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
  84. #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
  85. #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
  86. #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
  87. #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
  88. #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
  89. #define elf_backend_object_p ppc64_elf_object_p
  90. #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
  91. #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
  92. #define elf_backend_write_core_note ppc64_elf_write_core_note
  93. #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
  94. #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
  95. #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
  96. #define elf_backend_check_directives ppc64_elf_before_check_relocs
  97. #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
  98. #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
  99. #define elf_backend_check_relocs ppc64_elf_check_relocs
  100. #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
  101. #define elf_backend_gc_keep ppc64_elf_gc_keep
  102. #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
  103. #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
  104. #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
  105. #define elf_backend_hide_symbol ppc64_elf_hide_symbol
  106. #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
  107. #define elf_backend_always_size_sections ppc64_elf_edit
  108. #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
  109. #define elf_backend_hash_symbol ppc64_elf_hash_symbol
  110. #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
  111. #define elf_backend_action_discarded ppc64_elf_action_discarded
  112. #define elf_backend_relocate_section ppc64_elf_relocate_section
  113. #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
  114. #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
  115. #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
  116. #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
  117. #define elf_backend_special_sections ppc64_elf_special_sections
  118. #define elf_backend_section_flags ppc64_elf_section_flags
  119. #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
  120. #define elf_backend_merge_symbol ppc64_elf_merge_symbol
  121. #define elf_backend_get_reloc_section bfd_get_section_by_name
  122. /* The name of the dynamic interpreter. This is put in the .interp
  123. section. */
  124. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  125. /* The size in bytes of an entry in the procedure linkage table. */
  126. #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
  127. #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
  128. /* The initial size of the plt reserved for the dynamic linker. */
  129. #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
  130. /* Offsets to some stack save slots. */
  131. #define STK_LR 16
  132. #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
  133. /* This one is dodgy. ELFv2 does not have a linker word, so use the
  134. CR save slot. Used only by optimised __tls_get_addr call stub,
  135. relying on __tls_get_addr_opt not saving CR.. */
  136. #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
  137. /* TOC base pointers offset from start of TOC. */
  138. #define TOC_BASE_OFF 0x8000
  139. /* TOC base alignment. */
  140. #define TOC_BASE_ALIGN 256
  141. /* Offset of tp and dtp pointers from start of TLS block. */
  142. #define TP_OFFSET 0x7000
  143. #define DTP_OFFSET 0x8000
  144. /* .plt call stub instructions. The normal stub is like this, but
  145. sometimes the .plt entry crosses a 64k boundary and we need to
  146. insert an addi to adjust r11. */
  147. #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
  148. #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
  149. #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
  150. #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
  151. #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
  152. #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
  153. #define BCTR 0x4e800420 /* bctr */
  154. #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
  155. #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
  156. #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
  157. #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
  158. #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
  159. #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
  160. #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
  161. #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
  162. #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
  163. #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
  164. #define BNECTR 0x4ca20420 /* bnectr+ */
  165. #define BNECTR_P4 0x4ce20420 /* bnectr+ */
  166. #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
  167. #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
  168. #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
  169. #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
  170. #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
  171. #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
  172. #define LI_R11_0 0x39600000 /* li %r11,0 */
  173. #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
  174. #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
  175. #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
  176. #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
  177. #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
  178. #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
  179. #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
  180. #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
  181. #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
  182. #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
  183. #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
  184. #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
  185. #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
  186. #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
  187. #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
  188. #define PADDI_R12_PC 0x0610000039800000ULL
  189. #define PLD_R12_PC 0x04100000e5800000ULL
  190. #define PNOP 0x0700000000000000ULL
  191. /* __glink_PLTresolve stub instructions. We enter with the index in
  192. R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
  193. #define GLINK_PLTRESOLVE_SIZE(htab) \
  194. (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
  195. /* 0: */
  196. /* .quad plt0-1f */
  197. /* __glink: */
  198. #define MFLR_R12 0x7d8802a6 /* mflr %12 */
  199. #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
  200. /* 1: */
  201. #define MFLR_R11 0x7d6802a6 /* mflr %11 */
  202. /* ld %2,(0b-1b)(%11) */
  203. #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
  204. #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
  205. /* ld %12,0(%11) */
  206. /* ld %2,8(%11) */
  207. /* mtctr %12 */
  208. /* ld %11,16(%11) */
  209. /* bctr */
  210. #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
  211. #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
  212. #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
  213. #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
  214. #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
  215. #define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
  216. #define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
  217. /* Pad with this. */
  218. #define NOP 0x60000000
  219. /* Some other nops. */
  220. #define CROR_151515 0x4def7b82
  221. #define CROR_313131 0x4ffffb82
  222. /* .glink entries for the first 32k functions are two instructions. */
  223. #define LI_R0_0 0x38000000 /* li %r0,0 */
  224. #define B_DOT 0x48000000 /* b . */
  225. /* After that, we need two instructions to load the index, followed by
  226. a branch. */
  227. #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
  228. #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
  229. /* Instructions used by the save and restore reg functions. */
  230. #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
  231. #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
  232. #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
  233. #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
  234. #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
  235. #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
  236. #define LI_R12_0 0x39800000 /* li %r12,0 */
  237. #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
  238. #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
  239. #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
  240. #define BLR 0x4e800020 /* blr */
  241. /* Since .opd is an array of descriptors and each entry will end up
  242. with identical R_PPC64_RELATIVE relocs, there is really no need to
  243. propagate .opd relocs; The dynamic linker should be taught to
  244. relocate .opd without reloc entries. */
  245. #ifndef NO_OPD_RELOCS
  246. #define NO_OPD_RELOCS 0
  247. #endif
  248. #ifndef ARRAY_SIZE
  249. #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
  250. #endif
  251. static inline int
  252. abiversion (bfd *abfd)
  253. {
  254. return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
  255. }
  256. static inline void
  257. set_abiversion (bfd *abfd, int ver)
  258. {
  259. elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
  260. elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
  261. }
  262. /* Relocation HOWTO's. */
  263. /* Like other ELF RELA targets that don't apply multiple
  264. field-altering relocations to the same localation, src_mask is
  265. always zero and pcrel_offset is the same as pc_relative.
  266. PowerPC can always use a zero bitpos, even when the field is not at
  267. the LSB. For example, a REL24 could use rightshift=2, bisize=24
  268. and bitpos=2 which matches the ABI description, or as we do here,
  269. rightshift=0, bitsize=26 and bitpos=0. */
  270. #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
  271. complain, special_func) \
  272. HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
  273. complain_overflow_ ## complain, special_func, \
  274. #type, false, 0, mask, pc_relative)
  275. static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
  276. static reloc_howto_type ppc64_elf_howto_raw[] =
  277. {
  278. /* This reloc does nothing. */
  279. HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
  280. bfd_elf_generic_reloc),
  281. /* A standard 32 bit relocation. */
  282. HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
  283. bfd_elf_generic_reloc),
  284. /* An absolute 26 bit branch; the lower two bits must be zero.
  285. FIXME: we don't check that, we just clear them. */
  286. HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
  287. bfd_elf_generic_reloc),
  288. /* A standard 16 bit relocation. */
  289. HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
  290. bfd_elf_generic_reloc),
  291. /* A 16 bit relocation without overflow. */
  292. HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
  293. bfd_elf_generic_reloc),
  294. /* Bits 16-31 of an address. */
  295. HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
  296. bfd_elf_generic_reloc),
  297. /* Bits 16-31 of an address, plus 1 if the contents of the low 16
  298. bits, treated as a signed number, is negative. */
  299. HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
  300. ppc64_elf_ha_reloc),
  301. /* An absolute 16 bit branch; the lower two bits must be zero.
  302. FIXME: we don't check that, we just clear them. */
  303. HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
  304. ppc64_elf_branch_reloc),
  305. /* An absolute 16 bit branch, for which bit 10 should be set to
  306. indicate that the branch is expected to be taken. The lower two
  307. bits must be zero. */
  308. HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
  309. ppc64_elf_brtaken_reloc),
  310. /* An absolute 16 bit branch, for which bit 10 should be set to
  311. indicate that the branch is not expected to be taken. The lower
  312. two bits must be zero. */
  313. HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
  314. ppc64_elf_brtaken_reloc),
  315. /* A relative 26 bit branch; the lower two bits must be zero. */
  316. HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
  317. ppc64_elf_branch_reloc),
  318. /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
  319. HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
  320. ppc64_elf_branch_reloc),
  321. /* Another variant, when p10 insns can't be used on stubs. */
  322. HOW (R_PPC64_REL24_P9NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
  323. ppc64_elf_branch_reloc),
  324. /* A relative 16 bit branch; the lower two bits must be zero. */
  325. HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
  326. ppc64_elf_branch_reloc),
  327. /* A relative 16 bit branch. Bit 10 should be set to indicate that
  328. the branch is expected to be taken. The lower two bits must be
  329. zero. */
  330. HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
  331. ppc64_elf_brtaken_reloc),
  332. /* A relative 16 bit branch. Bit 10 should be set to indicate that
  333. the branch is not expected to be taken. The lower two bits must
  334. be zero. */
  335. HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
  336. ppc64_elf_brtaken_reloc),
  337. /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
  338. symbol. */
  339. HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
  340. ppc64_elf_unhandled_reloc),
  341. /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
  342. the symbol. */
  343. HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
  344. ppc64_elf_unhandled_reloc),
  345. /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
  346. the symbol. */
  347. HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
  348. ppc64_elf_unhandled_reloc),
  349. /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
  350. the symbol. */
  351. HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
  352. ppc64_elf_unhandled_reloc),
  353. /* This is used only by the dynamic linker. The symbol should exist
  354. both in the object being run and in some shared library. The
  355. dynamic linker copies the data addressed by the symbol from the
  356. shared library into the object, because the object being
  357. run has to have the data at some particular address. */
  358. HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
  359. ppc64_elf_unhandled_reloc),
  360. /* Like R_PPC64_ADDR64, but used when setting global offset table
  361. entries. */
  362. HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  363. ppc64_elf_unhandled_reloc),
  364. /* Created by the link editor. Marks a procedure linkage table
  365. entry for a symbol. */
  366. HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
  367. ppc64_elf_unhandled_reloc),
  368. /* Used only by the dynamic linker. When the object is run, this
  369. doubleword64 is set to the load address of the object, plus the
  370. addend. */
  371. HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  372. bfd_elf_generic_reloc),
  373. /* Like R_PPC64_ADDR32, but may be unaligned. */
  374. HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
  375. bfd_elf_generic_reloc),
  376. /* Like R_PPC64_ADDR16, but may be unaligned. */
  377. HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
  378. bfd_elf_generic_reloc),
  379. /* 32-bit PC relative. */
  380. HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
  381. bfd_elf_generic_reloc),
  382. /* 32-bit relocation to the symbol's procedure linkage table. */
  383. HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
  384. ppc64_elf_unhandled_reloc),
  385. /* 32-bit PC relative relocation to the symbol's procedure linkage table.
  386. FIXME: R_PPC64_PLTREL32 not supported. */
  387. HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
  388. ppc64_elf_unhandled_reloc),
  389. /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
  390. the symbol. */
  391. HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
  392. ppc64_elf_unhandled_reloc),
  393. /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
  394. the symbol. */
  395. HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
  396. ppc64_elf_unhandled_reloc),
  397. /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
  398. the symbol. */
  399. HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
  400. ppc64_elf_unhandled_reloc),
  401. /* 16-bit section relative relocation. */
  402. HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
  403. ppc64_elf_sectoff_reloc),
  404. /* Like R_PPC64_SECTOFF, but no overflow warning. */
  405. HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
  406. ppc64_elf_sectoff_reloc),
  407. /* 16-bit upper half section relative relocation. */
  408. HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
  409. ppc64_elf_sectoff_reloc),
  410. /* 16-bit upper half adjusted section relative relocation. */
  411. HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
  412. ppc64_elf_sectoff_ha_reloc),
  413. /* Like R_PPC64_REL24 without touching the two least significant bits. */
  414. HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
  415. bfd_elf_generic_reloc),
  416. /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
  417. /* A standard 64-bit relocation. */
  418. HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  419. bfd_elf_generic_reloc),
  420. /* The bits 32-47 of an address. */
  421. HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
  422. bfd_elf_generic_reloc),
  423. /* The bits 32-47 of an address, plus 1 if the contents of the low
  424. 16 bits, treated as a signed number, is negative. */
  425. HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
  426. ppc64_elf_ha_reloc),
  427. /* The bits 48-63 of an address. */
  428. HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
  429. bfd_elf_generic_reloc),
  430. /* The bits 48-63 of an address, plus 1 if the contents of the low
  431. 16 bits, treated as a signed number, is negative. */
  432. HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
  433. ppc64_elf_ha_reloc),
  434. /* Like ADDR64, but may be unaligned. */
  435. HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  436. bfd_elf_generic_reloc),
  437. /* 64-bit relative relocation. */
  438. HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
  439. bfd_elf_generic_reloc),
  440. /* 64-bit relocation to the symbol's procedure linkage table. */
  441. HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  442. ppc64_elf_unhandled_reloc),
  443. /* 64-bit PC relative relocation to the symbol's procedure linkage
  444. table. */
  445. /* FIXME: R_PPC64_PLTREL64 not supported. */
  446. HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
  447. ppc64_elf_unhandled_reloc),
  448. /* 16 bit TOC-relative relocation. */
  449. /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
  450. HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
  451. ppc64_elf_toc_reloc),
  452. /* 16 bit TOC-relative relocation without overflow. */
  453. /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
  454. HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
  455. ppc64_elf_toc_reloc),
  456. /* 16 bit TOC-relative relocation, high 16 bits. */
  457. /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
  458. HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
  459. ppc64_elf_toc_reloc),
  460. /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
  461. contents of the low 16 bits, treated as a signed number, is
  462. negative. */
  463. /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
  464. HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
  465. ppc64_elf_toc_ha_reloc),
  466. /* 64-bit relocation; insert value of TOC base (.TOC.). */
  467. /* R_PPC64_TOC 51 doubleword64 .TOC. */
  468. HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  469. ppc64_elf_toc64_reloc),
  470. /* Like R_PPC64_GOT16, but also informs the link editor that the
  471. value to relocate may (!) refer to a PLT entry which the link
  472. editor (a) may replace with the symbol value. If the link editor
  473. is unable to fully resolve the symbol, it may (b) create a PLT
  474. entry and store the address to the new PLT entry in the GOT.
  475. This permits lazy resolution of function symbols at run time.
  476. The link editor may also skip all of this and just (c) emit a
  477. R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
  478. /* FIXME: R_PPC64_PLTGOT16 not implemented. */
  479. HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
  480. ppc64_elf_unhandled_reloc),
  481. /* Like R_PPC64_PLTGOT16, but without overflow. */
  482. /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
  483. HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
  484. ppc64_elf_unhandled_reloc),
  485. /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
  486. /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
  487. HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
  488. ppc64_elf_unhandled_reloc),
  489. /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
  490. 1 if the contents of the low 16 bits, treated as a signed number,
  491. is negative. */
  492. /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
  493. HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
  494. ppc64_elf_unhandled_reloc),
  495. /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
  496. HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
  497. bfd_elf_generic_reloc),
  498. /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
  499. HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  500. bfd_elf_generic_reloc),
  501. /* Like R_PPC64_GOT16, but for instructions with a DS field. */
  502. HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
  503. ppc64_elf_unhandled_reloc),
  504. /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
  505. HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  506. ppc64_elf_unhandled_reloc),
  507. /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
  508. HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  509. ppc64_elf_unhandled_reloc),
  510. /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
  511. HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
  512. ppc64_elf_sectoff_reloc),
  513. /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
  514. HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  515. ppc64_elf_sectoff_reloc),
  516. /* Like R_PPC64_TOC16, but for instructions with a DS field. */
  517. HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
  518. ppc64_elf_toc_reloc),
  519. /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
  520. HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  521. ppc64_elf_toc_reloc),
  522. /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
  523. /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
  524. HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
  525. ppc64_elf_unhandled_reloc),
  526. /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
  527. /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
  528. HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  529. ppc64_elf_unhandled_reloc),
  530. /* Marker relocs for TLS. */
  531. HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
  532. bfd_elf_generic_reloc),
  533. HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
  534. bfd_elf_generic_reloc),
  535. HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
  536. bfd_elf_generic_reloc),
  537. /* Marker reloc for optimizing r2 save in prologue rather than on
  538. each plt call stub. */
  539. HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
  540. bfd_elf_generic_reloc),
  541. /* Marker relocs on inline plt call instructions. */
  542. HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
  543. bfd_elf_generic_reloc),
  544. HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
  545. bfd_elf_generic_reloc),
  546. /* Computes the load module index of the load module that contains the
  547. definition of its TLS sym. */
  548. HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  549. ppc64_elf_unhandled_reloc),
  550. /* Computes a dtv-relative displacement, the difference between the value
  551. of sym+add and the base address of the thread-local storage block that
  552. contains the definition of sym, minus 0x8000. */
  553. HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  554. ppc64_elf_unhandled_reloc),
  555. /* A 16 bit dtprel reloc. */
  556. HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
  557. ppc64_elf_unhandled_reloc),
  558. /* Like DTPREL16, but no overflow. */
  559. HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
  560. ppc64_elf_unhandled_reloc),
  561. /* Like DTPREL16_LO, but next higher group of 16 bits. */
  562. HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
  563. ppc64_elf_unhandled_reloc),
  564. /* Like DTPREL16_HI, but adjust for low 16 bits. */
  565. HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
  566. ppc64_elf_unhandled_reloc),
  567. /* Like DTPREL16_HI, but next higher group of 16 bits. */
  568. HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
  569. ppc64_elf_unhandled_reloc),
  570. /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
  571. HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
  572. ppc64_elf_unhandled_reloc),
  573. /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
  574. HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
  575. ppc64_elf_unhandled_reloc),
  576. /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
  577. HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
  578. ppc64_elf_unhandled_reloc),
  579. /* Like DTPREL16, but for insns with a DS field. */
  580. HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
  581. ppc64_elf_unhandled_reloc),
  582. /* Like DTPREL16_DS, but no overflow. */
  583. HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  584. ppc64_elf_unhandled_reloc),
  585. /* Computes a tp-relative displacement, the difference between the value of
  586. sym+add and the value of the thread pointer (r13). */
  587. HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  588. ppc64_elf_unhandled_reloc),
  589. /* A 16 bit tprel reloc. */
  590. HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
  591. ppc64_elf_unhandled_reloc),
  592. /* Like TPREL16, but no overflow. */
  593. HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
  594. ppc64_elf_unhandled_reloc),
  595. /* Like TPREL16_LO, but next higher group of 16 bits. */
  596. HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
  597. ppc64_elf_unhandled_reloc),
  598. /* Like TPREL16_HI, but adjust for low 16 bits. */
  599. HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
  600. ppc64_elf_unhandled_reloc),
  601. /* Like TPREL16_HI, but next higher group of 16 bits. */
  602. HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
  603. ppc64_elf_unhandled_reloc),
  604. /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
  605. HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
  606. ppc64_elf_unhandled_reloc),
  607. /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
  608. HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
  609. ppc64_elf_unhandled_reloc),
  610. /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
  611. HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
  612. ppc64_elf_unhandled_reloc),
  613. /* Like TPREL16, but for insns with a DS field. */
  614. HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
  615. ppc64_elf_unhandled_reloc),
  616. /* Like TPREL16_DS, but no overflow. */
  617. HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  618. ppc64_elf_unhandled_reloc),
  619. /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
  620. with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
  621. to the first entry relative to the TOC base (r2). */
  622. HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
  623. ppc64_elf_unhandled_reloc),
  624. /* Like GOT_TLSGD16, but no overflow. */
  625. HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
  626. ppc64_elf_unhandled_reloc),
  627. /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
  628. HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
  629. ppc64_elf_unhandled_reloc),
  630. /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
  631. HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
  632. ppc64_elf_unhandled_reloc),
  633. /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
  634. with values (sym+add)@dtpmod and zero, and computes the offset to the
  635. first entry relative to the TOC base (r2). */
  636. HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
  637. ppc64_elf_unhandled_reloc),
  638. /* Like GOT_TLSLD16, but no overflow. */
  639. HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
  640. ppc64_elf_unhandled_reloc),
  641. /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
  642. HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
  643. ppc64_elf_unhandled_reloc),
  644. /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
  645. HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
  646. ppc64_elf_unhandled_reloc),
  647. /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
  648. the offset to the entry relative to the TOC base (r2). */
  649. HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
  650. ppc64_elf_unhandled_reloc),
  651. /* Like GOT_DTPREL16_DS, but no overflow. */
  652. HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  653. ppc64_elf_unhandled_reloc),
  654. /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
  655. HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
  656. ppc64_elf_unhandled_reloc),
  657. /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
  658. HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
  659. ppc64_elf_unhandled_reloc),
  660. /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
  661. offset to the entry relative to the TOC base (r2). */
  662. HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
  663. ppc64_elf_unhandled_reloc),
  664. /* Like GOT_TPREL16_DS, but no overflow. */
  665. HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
  666. ppc64_elf_unhandled_reloc),
  667. /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
  668. HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
  669. ppc64_elf_unhandled_reloc),
  670. /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
  671. HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
  672. ppc64_elf_unhandled_reloc),
  673. HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
  674. ppc64_elf_unhandled_reloc),
  675. HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  676. bfd_elf_generic_reloc),
  677. /* A 16 bit relative relocation. */
  678. HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
  679. bfd_elf_generic_reloc),
  680. /* A 16 bit relative relocation without overflow. */
  681. HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
  682. bfd_elf_generic_reloc),
  683. /* The high order 16 bits of a relative address. */
  684. HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
  685. bfd_elf_generic_reloc),
  686. /* The high order 16 bits of a relative address, plus 1 if the contents of
  687. the low 16 bits, treated as a signed number, is negative. */
  688. HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
  689. ppc64_elf_ha_reloc),
  690. HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
  691. bfd_elf_generic_reloc),
  692. HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
  693. ppc64_elf_ha_reloc),
  694. HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
  695. bfd_elf_generic_reloc),
  696. HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
  697. ppc64_elf_ha_reloc),
  698. HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
  699. bfd_elf_generic_reloc),
  700. HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
  701. ppc64_elf_ha_reloc),
  702. /* Like R_PPC64_REL16_HA but for split field in addpcis. */
  703. HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
  704. ppc64_elf_ha_reloc),
  705. /* A split-field reloc for addpcis, non-relative (gas internal use only). */
  706. HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
  707. ppc64_elf_ha_reloc),
  708. /* Like R_PPC64_ADDR16_HI, but no overflow. */
  709. HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
  710. bfd_elf_generic_reloc),
  711. /* Like R_PPC64_ADDR16_HA, but no overflow. */
  712. HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
  713. ppc64_elf_ha_reloc),
  714. /* Like R_PPC64_DTPREL16_HI, but no overflow. */
  715. HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
  716. ppc64_elf_unhandled_reloc),
  717. /* Like R_PPC64_DTPREL16_HA, but no overflow. */
  718. HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
  719. ppc64_elf_unhandled_reloc),
  720. /* Like R_PPC64_TPREL16_HI, but no overflow. */
  721. HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
  722. ppc64_elf_unhandled_reloc),
  723. /* Like R_PPC64_TPREL16_HA, but no overflow. */
  724. HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
  725. ppc64_elf_unhandled_reloc),
  726. /* Marker reloc on ELFv2 large-model function entry. */
  727. HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
  728. bfd_elf_generic_reloc),
  729. /* Like ADDR64, but use local entry point of function. */
  730. HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
  731. bfd_elf_generic_reloc),
  732. HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
  733. bfd_elf_generic_reloc),
  734. HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
  735. bfd_elf_generic_reloc),
  736. HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
  737. bfd_elf_generic_reloc),
  738. HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
  739. ppc64_elf_prefix_reloc),
  740. HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
  741. ppc64_elf_prefix_reloc),
  742. HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
  743. ppc64_elf_prefix_reloc),
  744. HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
  745. ppc64_elf_prefix_reloc),
  746. HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
  747. ppc64_elf_prefix_reloc),
  748. HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
  749. ppc64_elf_unhandled_reloc),
  750. HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
  751. ppc64_elf_unhandled_reloc),
  752. HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
  753. ppc64_elf_unhandled_reloc),
  754. HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
  755. ppc64_elf_unhandled_reloc),
  756. HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
  757. ppc64_elf_unhandled_reloc),
  758. HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
  759. ppc64_elf_unhandled_reloc),
  760. HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
  761. ppc64_elf_unhandled_reloc),
  762. HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
  763. ppc64_elf_unhandled_reloc),
  764. HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
  765. ppc64_elf_unhandled_reloc),
  766. HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
  767. bfd_elf_generic_reloc),
  768. HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
  769. ppc64_elf_ha_reloc),
  770. HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
  771. bfd_elf_generic_reloc),
  772. HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
  773. ppc64_elf_ha_reloc),
  774. HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
  775. bfd_elf_generic_reloc),
  776. HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
  777. ppc64_elf_ha_reloc),
  778. HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
  779. bfd_elf_generic_reloc),
  780. HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
  781. ppc64_elf_ha_reloc),
  782. HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
  783. ppc64_elf_prefix_reloc),
  784. HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
  785. ppc64_elf_prefix_reloc),
  786. /* GNU extension to record C++ vtable hierarchy. */
  787. HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
  788. NULL),
  789. /* GNU extension to record C++ vtable member usage. */
  790. HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
  791. NULL),
  792. };
  793. /* Initialize the ppc64_elf_howto_table, so that linear accesses can
  794. be done. */
  795. static void
  796. ppc_howto_init (void)
  797. {
  798. unsigned int i, type;
  799. for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
  800. {
  801. type = ppc64_elf_howto_raw[i].type;
  802. BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
  803. ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
  804. }
  805. }
  806. static reloc_howto_type *
  807. ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
  808. {
  809. enum elf_ppc64_reloc_type r = R_PPC64_NONE;
  810. if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
  811. /* Initialize howto table if needed. */
  812. ppc_howto_init ();
  813. switch (code)
  814. {
  815. default:
  816. /* xgettext:c-format */
  817. _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
  818. (int) code);
  819. bfd_set_error (bfd_error_bad_value);
  820. return NULL;
  821. case BFD_RELOC_NONE: r = R_PPC64_NONE;
  822. break;
  823. case BFD_RELOC_32: r = R_PPC64_ADDR32;
  824. break;
  825. case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
  826. break;
  827. case BFD_RELOC_16: r = R_PPC64_ADDR16;
  828. break;
  829. case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
  830. break;
  831. case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
  832. break;
  833. case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
  834. break;
  835. case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
  836. break;
  837. case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
  838. break;
  839. case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
  840. break;
  841. case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
  842. break;
  843. case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
  844. break;
  845. case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
  846. break;
  847. case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
  848. break;
  849. case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
  850. break;
  851. case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
  852. break;
  853. case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
  854. break;
  855. case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
  856. break;
  857. case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
  858. break;
  859. case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
  860. break;
  861. case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
  862. break;
  863. case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
  864. break;
  865. case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
  866. break;
  867. case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
  868. break;
  869. case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
  870. break;
  871. case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
  872. break;
  873. case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
  874. break;
  875. case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
  876. break;
  877. case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
  878. break;
  879. case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
  880. break;
  881. case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
  882. break;
  883. case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
  884. break;
  885. case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
  886. break;
  887. case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
  888. break;
  889. case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
  890. break;
  891. case BFD_RELOC_64: r = R_PPC64_ADDR64;
  892. break;
  893. case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
  894. break;
  895. case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
  896. break;
  897. case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
  898. break;
  899. case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
  900. break;
  901. case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
  902. break;
  903. case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
  904. break;
  905. case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
  906. break;
  907. case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
  908. break;
  909. case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
  910. break;
  911. case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
  912. break;
  913. case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
  914. break;
  915. case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
  916. break;
  917. case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
  918. break;
  919. case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
  920. break;
  921. case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
  922. break;
  923. case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
  924. break;
  925. case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
  926. break;
  927. case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
  928. break;
  929. case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
  930. break;
  931. case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
  932. break;
  933. case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
  934. break;
  935. case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
  936. break;
  937. case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
  938. break;
  939. case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
  940. break;
  941. case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
  942. break;
  943. case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
  944. break;
  945. case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
  946. break;
  947. case BFD_RELOC_PPC64_TLS_PCREL:
  948. case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
  949. break;
  950. case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
  951. break;
  952. case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
  953. break;
  954. case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
  955. break;
  956. case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
  957. break;
  958. case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
  959. break;
  960. case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
  961. break;
  962. case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
  963. break;
  964. case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
  965. break;
  966. case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
  967. break;
  968. case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
  969. break;
  970. case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
  971. break;
  972. case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
  973. break;
  974. case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
  975. break;
  976. case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
  977. break;
  978. case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
  979. break;
  980. case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
  981. break;
  982. case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
  983. break;
  984. case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
  985. break;
  986. case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
  987. break;
  988. case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
  989. break;
  990. case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
  991. break;
  992. case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
  993. break;
  994. case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
  995. break;
  996. case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
  997. break;
  998. case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
  999. break;
  1000. case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
  1001. break;
  1002. case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
  1003. break;
  1004. case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
  1005. break;
  1006. case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
  1007. break;
  1008. case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
  1009. break;
  1010. case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
  1011. break;
  1012. case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
  1013. break;
  1014. case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
  1015. break;
  1016. case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
  1017. break;
  1018. case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
  1019. break;
  1020. case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
  1021. break;
  1022. case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
  1023. break;
  1024. case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
  1025. break;
  1026. case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
  1027. break;
  1028. case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
  1029. break;
  1030. case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
  1031. break;
  1032. case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
  1033. break;
  1034. case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
  1035. break;
  1036. case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
  1037. break;
  1038. case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
  1039. break;
  1040. case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
  1041. break;
  1042. case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
  1043. break;
  1044. case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
  1045. break;
  1046. case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
  1047. break;
  1048. case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
  1049. break;
  1050. case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
  1051. break;
  1052. case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
  1053. break;
  1054. case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
  1055. break;
  1056. case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
  1057. break;
  1058. case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
  1059. break;
  1060. case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
  1061. break;
  1062. case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
  1063. break;
  1064. case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
  1065. break;
  1066. case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
  1067. break;
  1068. case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
  1069. break;
  1070. case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
  1071. break;
  1072. case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
  1073. break;
  1074. case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
  1075. break;
  1076. case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
  1077. break;
  1078. case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
  1079. break;
  1080. case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
  1081. break;
  1082. case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
  1083. break;
  1084. case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
  1085. break;
  1086. case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
  1087. break;
  1088. case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
  1089. break;
  1090. case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
  1091. break;
  1092. case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
  1093. break;
  1094. case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
  1095. break;
  1096. case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
  1097. break;
  1098. case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
  1099. break;
  1100. case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
  1101. break;
  1102. case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
  1103. break;
  1104. case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
  1105. break;
  1106. case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
  1107. break;
  1108. case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
  1109. break;
  1110. case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
  1111. break;
  1112. case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
  1113. break;
  1114. case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
  1115. break;
  1116. case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
  1117. break;
  1118. }
  1119. return ppc64_elf_howto_table[r];
  1120. };
  1121. static reloc_howto_type *
  1122. ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
  1123. {
  1124. unsigned int i;
  1125. static char *compat_map[][2] = {
  1126. { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
  1127. { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
  1128. { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
  1129. { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
  1130. };
  1131. for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
  1132. if (ppc64_elf_howto_raw[i].name != NULL
  1133. && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
  1134. return &ppc64_elf_howto_raw[i];
  1135. /* Handle old names of relocations in case they were used by
  1136. .reloc directives.
  1137. FIXME: Remove this soon. Mapping the reloc names is very likely
  1138. completely unnecessary. */
  1139. for (i = 0; i < ARRAY_SIZE (compat_map); i++)
  1140. if (strcasecmp (compat_map[i][0], r_name) == 0)
  1141. {
  1142. _bfd_error_handler (_("warning: %s should be used rather than %s"),
  1143. compat_map[i][1], compat_map[i][0]);
  1144. return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
  1145. }
  1146. return NULL;
  1147. }
  1148. /* Set the howto pointer for a PowerPC ELF reloc. */
  1149. static bool
  1150. ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
  1151. Elf_Internal_Rela *dst)
  1152. {
  1153. unsigned int type;
  1154. /* Initialize howto table if needed. */
  1155. if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
  1156. ppc_howto_init ();
  1157. type = ELF64_R_TYPE (dst->r_info);
  1158. if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
  1159. {
  1160. /* xgettext:c-format */
  1161. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  1162. abfd, type);
  1163. bfd_set_error (bfd_error_bad_value);
  1164. return false;
  1165. }
  1166. cache_ptr->howto = ppc64_elf_howto_table[type];
  1167. if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
  1168. {
  1169. /* xgettext:c-format */
  1170. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  1171. abfd, type);
  1172. bfd_set_error (bfd_error_bad_value);
  1173. return false;
  1174. }
  1175. return true;
  1176. }
  1177. /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
  1178. static bfd_reloc_status_type
  1179. ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1180. void *data, asection *input_section,
  1181. bfd *output_bfd, char **error_message)
  1182. {
  1183. enum elf_ppc64_reloc_type r_type;
  1184. long insn;
  1185. bfd_size_type octets;
  1186. bfd_vma value;
  1187. /* If this is a relocatable link (output_bfd test tells us), just
  1188. call the generic function. Any adjustment will be done at final
  1189. link time. */
  1190. if (output_bfd != NULL)
  1191. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1192. input_section, output_bfd, error_message);
  1193. /* Adjust the addend for sign extension of the low 16 (or 34) bits.
  1194. We won't actually be using the low bits, so trashing them
  1195. doesn't matter. */
  1196. r_type = reloc_entry->howto->type;
  1197. if (r_type == R_PPC64_ADDR16_HIGHERA34
  1198. || r_type == R_PPC64_ADDR16_HIGHESTA34
  1199. || r_type == R_PPC64_REL16_HIGHERA34
  1200. || r_type == R_PPC64_REL16_HIGHESTA34)
  1201. reloc_entry->addend += 1ULL << 33;
  1202. else
  1203. reloc_entry->addend += 1U << 15;
  1204. if (r_type != R_PPC64_REL16DX_HA)
  1205. return bfd_reloc_continue;
  1206. value = 0;
  1207. if (!bfd_is_com_section (symbol->section))
  1208. value = symbol->value;
  1209. value += (reloc_entry->addend
  1210. + symbol->section->output_offset
  1211. + symbol->section->output_section->vma);
  1212. value -= (reloc_entry->address
  1213. + input_section->output_offset
  1214. + input_section->output_section->vma);
  1215. value = (bfd_signed_vma) value >> 16;
  1216. octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
  1217. if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
  1218. input_section, octets))
  1219. return bfd_reloc_outofrange;
  1220. insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
  1221. insn &= ~0x1fffc1;
  1222. insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
  1223. bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
  1224. if (value + 0x8000 > 0xffff)
  1225. return bfd_reloc_overflow;
  1226. return bfd_reloc_ok;
  1227. }
  1228. static bfd_reloc_status_type
  1229. ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1230. void *data, asection *input_section,
  1231. bfd *output_bfd, char **error_message)
  1232. {
  1233. if (output_bfd != NULL)
  1234. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1235. input_section, output_bfd, error_message);
  1236. if (strcmp (symbol->section->name, ".opd") == 0
  1237. && (symbol->section->owner->flags & DYNAMIC) == 0)
  1238. {
  1239. bfd_vma dest = opd_entry_value (symbol->section,
  1240. symbol->value + reloc_entry->addend,
  1241. NULL, NULL, false);
  1242. if (dest != (bfd_vma) -1)
  1243. reloc_entry->addend = dest - (symbol->value
  1244. + symbol->section->output_section->vma
  1245. + symbol->section->output_offset);
  1246. }
  1247. else
  1248. {
  1249. elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
  1250. if (symbol->section->owner != abfd
  1251. && symbol->section->owner != NULL
  1252. && abiversion (symbol->section->owner) >= 2)
  1253. {
  1254. unsigned int i;
  1255. for (i = 0; i < symbol->section->owner->symcount; ++i)
  1256. {
  1257. asymbol *symdef = symbol->section->owner->outsymbols[i];
  1258. if (strcmp (symdef->name, symbol->name) == 0)
  1259. {
  1260. elfsym = (elf_symbol_type *) symdef;
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. reloc_entry->addend
  1266. += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
  1267. }
  1268. return bfd_reloc_continue;
  1269. }
  1270. static bfd_reloc_status_type
  1271. ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1272. void *data, asection *input_section,
  1273. bfd *output_bfd, char **error_message)
  1274. {
  1275. long insn;
  1276. enum elf_ppc64_reloc_type r_type;
  1277. bfd_size_type octets;
  1278. /* Assume 'at' branch hints. */
  1279. bool is_isa_v2 = true;
  1280. /* If this is a relocatable link (output_bfd test tells us), just
  1281. call the generic function. Any adjustment will be done at final
  1282. link time. */
  1283. if (output_bfd != NULL)
  1284. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1285. input_section, output_bfd, error_message);
  1286. octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
  1287. if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
  1288. input_section, octets))
  1289. return bfd_reloc_outofrange;
  1290. insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
  1291. insn &= ~(0x01 << 21);
  1292. r_type = reloc_entry->howto->type;
  1293. if (r_type == R_PPC64_ADDR14_BRTAKEN
  1294. || r_type == R_PPC64_REL14_BRTAKEN)
  1295. insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
  1296. if (is_isa_v2)
  1297. {
  1298. /* Set 'a' bit. This is 0b00010 in BO field for branch
  1299. on CR(BI) insns (BO == 001at or 011at), and 0b01000
  1300. for branch on CTR insns (BO == 1a00t or 1a01t). */
  1301. if ((insn & (0x14 << 21)) == (0x04 << 21))
  1302. insn |= 0x02 << 21;
  1303. else if ((insn & (0x14 << 21)) == (0x10 << 21))
  1304. insn |= 0x08 << 21;
  1305. else
  1306. goto out;
  1307. }
  1308. else
  1309. {
  1310. bfd_vma target = 0;
  1311. bfd_vma from;
  1312. if (!bfd_is_com_section (symbol->section))
  1313. target = symbol->value;
  1314. target += symbol->section->output_section->vma;
  1315. target += symbol->section->output_offset;
  1316. target += reloc_entry->addend;
  1317. from = (reloc_entry->address
  1318. + input_section->output_offset
  1319. + input_section->output_section->vma);
  1320. /* Invert 'y' bit if not the default. */
  1321. if ((bfd_signed_vma) (target - from) < 0)
  1322. insn ^= 0x01 << 21;
  1323. }
  1324. bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
  1325. out:
  1326. return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
  1327. input_section, output_bfd, error_message);
  1328. }
  1329. static bfd_reloc_status_type
  1330. ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1331. void *data, asection *input_section,
  1332. bfd *output_bfd, char **error_message)
  1333. {
  1334. /* If this is a relocatable link (output_bfd test tells us), just
  1335. call the generic function. Any adjustment will be done at final
  1336. link time. */
  1337. if (output_bfd != NULL)
  1338. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1339. input_section, output_bfd, error_message);
  1340. /* Subtract the symbol section base address. */
  1341. reloc_entry->addend -= symbol->section->output_section->vma;
  1342. return bfd_reloc_continue;
  1343. }
  1344. static bfd_reloc_status_type
  1345. ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1346. void *data, asection *input_section,
  1347. bfd *output_bfd, char **error_message)
  1348. {
  1349. /* If this is a relocatable link (output_bfd test tells us), just
  1350. call the generic function. Any adjustment will be done at final
  1351. link time. */
  1352. if (output_bfd != NULL)
  1353. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1354. input_section, output_bfd, error_message);
  1355. /* Subtract the symbol section base address. */
  1356. reloc_entry->addend -= symbol->section->output_section->vma;
  1357. /* Adjust the addend for sign extension of the low 16 bits. */
  1358. reloc_entry->addend += 0x8000;
  1359. return bfd_reloc_continue;
  1360. }
  1361. static bfd_reloc_status_type
  1362. ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1363. void *data, asection *input_section,
  1364. bfd *output_bfd, char **error_message)
  1365. {
  1366. bfd_vma TOCstart;
  1367. /* If this is a relocatable link (output_bfd test tells us), just
  1368. call the generic function. Any adjustment will be done at final
  1369. link time. */
  1370. if (output_bfd != NULL)
  1371. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1372. input_section, output_bfd, error_message);
  1373. TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
  1374. if (TOCstart == 0)
  1375. TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
  1376. /* Subtract the TOC base address. */
  1377. reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
  1378. return bfd_reloc_continue;
  1379. }
  1380. static bfd_reloc_status_type
  1381. ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1382. void *data, asection *input_section,
  1383. bfd *output_bfd, char **error_message)
  1384. {
  1385. bfd_vma TOCstart;
  1386. /* If this is a relocatable link (output_bfd test tells us), just
  1387. call the generic function. Any adjustment will be done at final
  1388. link time. */
  1389. if (output_bfd != NULL)
  1390. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1391. input_section, output_bfd, error_message);
  1392. TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
  1393. if (TOCstart == 0)
  1394. TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
  1395. /* Subtract the TOC base address. */
  1396. reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
  1397. /* Adjust the addend for sign extension of the low 16 bits. */
  1398. reloc_entry->addend += 0x8000;
  1399. return bfd_reloc_continue;
  1400. }
  1401. static bfd_reloc_status_type
  1402. ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1403. void *data, asection *input_section,
  1404. bfd *output_bfd, char **error_message)
  1405. {
  1406. bfd_vma TOCstart;
  1407. bfd_size_type octets;
  1408. /* If this is a relocatable link (output_bfd test tells us), just
  1409. call the generic function. Any adjustment will be done at final
  1410. link time. */
  1411. if (output_bfd != NULL)
  1412. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1413. input_section, output_bfd, error_message);
  1414. octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
  1415. if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
  1416. input_section, octets))
  1417. return bfd_reloc_outofrange;
  1418. TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
  1419. if (TOCstart == 0)
  1420. TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
  1421. bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
  1422. return bfd_reloc_ok;
  1423. }
  1424. static bfd_reloc_status_type
  1425. ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1426. void *data, asection *input_section,
  1427. bfd *output_bfd, char **error_message)
  1428. {
  1429. uint64_t insn;
  1430. bfd_vma targ;
  1431. bfd_size_type octets;
  1432. if (output_bfd != NULL)
  1433. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1434. input_section, output_bfd, error_message);
  1435. octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
  1436. if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
  1437. input_section, octets))
  1438. return bfd_reloc_outofrange;
  1439. insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
  1440. insn <<= 32;
  1441. insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
  1442. targ = (symbol->section->output_section->vma
  1443. + symbol->section->output_offset
  1444. + reloc_entry->addend);
  1445. if (!bfd_is_com_section (symbol->section))
  1446. targ += symbol->value;
  1447. if (reloc_entry->howto->type == R_PPC64_D34_HA30)
  1448. targ += 1ULL << 33;
  1449. if (reloc_entry->howto->pc_relative)
  1450. {
  1451. bfd_vma from = (reloc_entry->address
  1452. + input_section->output_offset
  1453. + input_section->output_section->vma);
  1454. targ -=from;
  1455. }
  1456. targ >>= reloc_entry->howto->rightshift;
  1457. insn &= ~reloc_entry->howto->dst_mask;
  1458. insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
  1459. bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
  1460. bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
  1461. if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
  1462. && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
  1463. >= 1ULL << reloc_entry->howto->bitsize))
  1464. return bfd_reloc_overflow;
  1465. return bfd_reloc_ok;
  1466. }
  1467. static bfd_reloc_status_type
  1468. ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  1469. void *data, asection *input_section,
  1470. bfd *output_bfd, char **error_message)
  1471. {
  1472. /* If this is a relocatable link (output_bfd test tells us), just
  1473. call the generic function. Any adjustment will be done at final
  1474. link time. */
  1475. if (output_bfd != NULL)
  1476. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1477. input_section, output_bfd, error_message);
  1478. if (error_message != NULL)
  1479. {
  1480. static char *message;
  1481. free (message);
  1482. if (asprintf (&message, _("generic linker can't handle %s"),
  1483. reloc_entry->howto->name) < 0)
  1484. message = NULL;
  1485. *error_message = message;
  1486. }
  1487. return bfd_reloc_dangerous;
  1488. }
  1489. /* Track GOT entries needed for a given symbol. We might need more
  1490. than one got entry per symbol. */
  1491. struct got_entry
  1492. {
  1493. struct got_entry *next;
  1494. /* The symbol addend that we'll be placing in the GOT. */
  1495. bfd_vma addend;
  1496. /* Unlike other ELF targets, we use separate GOT entries for the same
  1497. symbol referenced from different input files. This is to support
  1498. automatic multiple TOC/GOT sections, where the TOC base can vary
  1499. from one input file to another. After partitioning into TOC groups
  1500. we merge entries within the group.
  1501. Point to the BFD owning this GOT entry. */
  1502. bfd *owner;
  1503. /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
  1504. TLS_TPREL or TLS_DTPREL for tls entries. */
  1505. unsigned char tls_type;
  1506. /* Non-zero if got.ent points to real entry. */
  1507. unsigned char is_indirect;
  1508. /* Reference count until size_dynamic_sections, GOT offset thereafter. */
  1509. union
  1510. {
  1511. bfd_signed_vma refcount;
  1512. bfd_vma offset;
  1513. struct got_entry *ent;
  1514. } got;
  1515. };
  1516. /* The same for PLT. */
  1517. struct plt_entry
  1518. {
  1519. struct plt_entry *next;
  1520. bfd_vma addend;
  1521. union
  1522. {
  1523. bfd_signed_vma refcount;
  1524. bfd_vma offset;
  1525. } plt;
  1526. };
  1527. struct ppc64_elf_obj_tdata
  1528. {
  1529. struct elf_obj_tdata elf;
  1530. /* Shortcuts to dynamic linker sections. */
  1531. asection *got;
  1532. asection *relgot;
  1533. /* Used during garbage collection. We attach global symbols defined
  1534. on removed .opd entries to this section so that the sym is removed. */
  1535. asection *deleted_section;
  1536. /* TLS local dynamic got entry handling. Support for multiple GOT
  1537. sections means we potentially need one of these for each input bfd. */
  1538. struct got_entry tlsld_got;
  1539. union
  1540. {
  1541. /* A copy of relocs before they are modified for --emit-relocs. */
  1542. Elf_Internal_Rela *relocs;
  1543. /* Section contents. */
  1544. bfd_byte *contents;
  1545. } opd;
  1546. /* Nonzero if this bfd has small toc/got relocs, ie. that expect
  1547. the reloc to be in the range -32768 to 32767. */
  1548. unsigned int has_small_toc_reloc : 1;
  1549. /* Set if toc/got ha relocs detected not using r2, or lo reloc
  1550. instruction not one we handle. */
  1551. unsigned int unexpected_toc_insn : 1;
  1552. /* Set if PLT/GOT/TOC relocs that can be optimised are present in
  1553. this file. */
  1554. unsigned int has_optrel : 1;
  1555. };
  1556. #define ppc64_elf_tdata(bfd) \
  1557. ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
  1558. #define ppc64_tlsld_got(bfd) \
  1559. (&ppc64_elf_tdata (bfd)->tlsld_got)
  1560. #define is_ppc64_elf(bfd) \
  1561. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  1562. && elf_object_id (bfd) == PPC64_ELF_DATA)
  1563. /* Override the generic function because we store some extras. */
  1564. static bool
  1565. ppc64_elf_mkobject (bfd *abfd)
  1566. {
  1567. return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
  1568. PPC64_ELF_DATA);
  1569. }
  1570. /* Fix bad default arch selected for a 64 bit input bfd when the
  1571. default is 32 bit. Also select arch based on apuinfo. */
  1572. static bool
  1573. ppc64_elf_object_p (bfd *abfd)
  1574. {
  1575. if (!abfd->arch_info->the_default)
  1576. return true;
  1577. if (abfd->arch_info->bits_per_word == 32)
  1578. {
  1579. Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
  1580. if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  1581. {
  1582. /* Relies on arch after 32 bit default being 64 bit default. */
  1583. abfd->arch_info = abfd->arch_info->next;
  1584. BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
  1585. }
  1586. }
  1587. return _bfd_elf_ppc_set_arch (abfd);
  1588. }
  1589. /* Support for core dump NOTE sections. */
  1590. static bool
  1591. ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  1592. {
  1593. size_t offset, size;
  1594. if (note->descsz != 504)
  1595. return false;
  1596. /* pr_cursig */
  1597. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  1598. /* pr_pid */
  1599. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
  1600. /* pr_reg */
  1601. offset = 112;
  1602. size = 384;
  1603. /* Make a ".reg/999" section. */
  1604. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  1605. size, note->descpos + offset);
  1606. }
  1607. static bool
  1608. ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  1609. {
  1610. if (note->descsz != 136)
  1611. return false;
  1612. elf_tdata (abfd)->core->pid
  1613. = bfd_get_32 (abfd, note->descdata + 24);
  1614. elf_tdata (abfd)->core->program
  1615. = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
  1616. elf_tdata (abfd)->core->command
  1617. = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
  1618. return true;
  1619. }
  1620. static char *
  1621. ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
  1622. ...)
  1623. {
  1624. switch (note_type)
  1625. {
  1626. default:
  1627. return NULL;
  1628. case NT_PRPSINFO:
  1629. {
  1630. char data[136] ATTRIBUTE_NONSTRING;
  1631. va_list ap;
  1632. va_start (ap, note_type);
  1633. memset (data, 0, sizeof (data));
  1634. strncpy (data + 40, va_arg (ap, const char *), 16);
  1635. #if GCC_VERSION == 8000 || GCC_VERSION == 8001
  1636. DIAGNOSTIC_PUSH;
  1637. /* GCC 8.0 and 8.1 warn about 80 equals destination size with
  1638. -Wstringop-truncation:
  1639. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
  1640. */
  1641. DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
  1642. #endif
  1643. strncpy (data + 56, va_arg (ap, const char *), 80);
  1644. #if GCC_VERSION == 8000 || GCC_VERSION == 8001
  1645. DIAGNOSTIC_POP;
  1646. #endif
  1647. va_end (ap);
  1648. return elfcore_write_note (abfd, buf, bufsiz,
  1649. "CORE", note_type, data, sizeof (data));
  1650. }
  1651. case NT_PRSTATUS:
  1652. {
  1653. char data[504];
  1654. va_list ap;
  1655. long pid;
  1656. int cursig;
  1657. const void *greg;
  1658. va_start (ap, note_type);
  1659. memset (data, 0, 112);
  1660. pid = va_arg (ap, long);
  1661. bfd_put_32 (abfd, pid, data + 32);
  1662. cursig = va_arg (ap, int);
  1663. bfd_put_16 (abfd, cursig, data + 12);
  1664. greg = va_arg (ap, const void *);
  1665. memcpy (data + 112, greg, 384);
  1666. memset (data + 496, 0, 8);
  1667. va_end (ap);
  1668. return elfcore_write_note (abfd, buf, bufsiz,
  1669. "CORE", note_type, data, sizeof (data));
  1670. }
  1671. }
  1672. }
  1673. /* Add extra PPC sections. */
  1674. static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
  1675. {
  1676. { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
  1677. { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  1678. { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  1679. { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  1680. { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  1681. { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  1682. { NULL, 0, 0, 0, 0 }
  1683. };
  1684. enum _ppc64_sec_type {
  1685. sec_normal = 0,
  1686. sec_opd = 1,
  1687. sec_toc = 2
  1688. };
  1689. struct _ppc64_elf_section_data
  1690. {
  1691. struct bfd_elf_section_data elf;
  1692. union
  1693. {
  1694. /* An array with one entry for each opd function descriptor,
  1695. and some spares since opd entries may be either 16 or 24 bytes. */
  1696. #define OPD_NDX(OFF) ((OFF) >> 4)
  1697. struct _opd_sec_data
  1698. {
  1699. /* Points to the function code section for local opd entries. */
  1700. asection **func_sec;
  1701. /* After editing .opd, adjust references to opd local syms. */
  1702. long *adjust;
  1703. } opd;
  1704. /* An array for toc sections, indexed by offset/8. */
  1705. struct _toc_sec_data
  1706. {
  1707. /* Specifies the relocation symbol index used at a given toc offset. */
  1708. unsigned *symndx;
  1709. /* And the relocation addend. */
  1710. bfd_vma *add;
  1711. } toc;
  1712. } u;
  1713. enum _ppc64_sec_type sec_type:2;
  1714. /* Flag set when small branches are detected. Used to
  1715. select suitable defaults for the stub group size. */
  1716. unsigned int has_14bit_branch:1;
  1717. /* Flag set when PLTCALL relocs are detected. */
  1718. unsigned int has_pltcall:1;
  1719. /* Flag set when section has PLT/GOT/TOC relocations that can be
  1720. optimised. */
  1721. unsigned int has_optrel:1;
  1722. };
  1723. #define ppc64_elf_section_data(sec) \
  1724. ((struct _ppc64_elf_section_data *) elf_section_data (sec))
  1725. static bool
  1726. ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
  1727. {
  1728. if (!sec->used_by_bfd)
  1729. {
  1730. struct _ppc64_elf_section_data *sdata;
  1731. size_t amt = sizeof (*sdata);
  1732. sdata = bfd_zalloc (abfd, amt);
  1733. if (sdata == NULL)
  1734. return false;
  1735. sec->used_by_bfd = sdata;
  1736. }
  1737. return _bfd_elf_new_section_hook (abfd, sec);
  1738. }
  1739. static bool
  1740. ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
  1741. {
  1742. const char *name = hdr->bfd_section->name;
  1743. if (startswith (name, ".sbss")
  1744. || startswith (name, ".sdata"))
  1745. hdr->bfd_section->flags |= SEC_SMALL_DATA;
  1746. return true;
  1747. }
  1748. static struct _opd_sec_data *
  1749. get_opd_info (asection * sec)
  1750. {
  1751. if (sec != NULL
  1752. && ppc64_elf_section_data (sec) != NULL
  1753. && ppc64_elf_section_data (sec)->sec_type == sec_opd)
  1754. return &ppc64_elf_section_data (sec)->u.opd;
  1755. return NULL;
  1756. }
  1757. /* Parameters for the qsort hook. */
  1758. static bool synthetic_relocatable;
  1759. static const asection *synthetic_opd;
  1760. /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
  1761. static int
  1762. compare_symbols (const void *ap, const void *bp)
  1763. {
  1764. const asymbol *a = *(const asymbol **) ap;
  1765. const asymbol *b = *(const asymbol **) bp;
  1766. /* Section symbols first. */
  1767. if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
  1768. return -1;
  1769. if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
  1770. return 1;
  1771. /* then .opd symbols. */
  1772. if (synthetic_opd != NULL)
  1773. {
  1774. if (strcmp (a->section->name, ".opd") == 0
  1775. && strcmp (b->section->name, ".opd") != 0)
  1776. return -1;
  1777. if (strcmp (a->section->name, ".opd") != 0
  1778. && strcmp (b->section->name, ".opd") == 0)
  1779. return 1;
  1780. }
  1781. /* then other code symbols. */
  1782. if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
  1783. == (SEC_CODE | SEC_ALLOC))
  1784. && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
  1785. != (SEC_CODE | SEC_ALLOC)))
  1786. return -1;
  1787. if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
  1788. != (SEC_CODE | SEC_ALLOC))
  1789. && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
  1790. == (SEC_CODE | SEC_ALLOC)))
  1791. return 1;
  1792. if (synthetic_relocatable)
  1793. {
  1794. if (a->section->id < b->section->id)
  1795. return -1;
  1796. if (a->section->id > b->section->id)
  1797. return 1;
  1798. }
  1799. if (a->value + a->section->vma < b->value + b->section->vma)
  1800. return -1;
  1801. if (a->value + a->section->vma > b->value + b->section->vma)
  1802. return 1;
  1803. /* For syms with the same value, prefer strong dynamic global function
  1804. syms over other syms. */
  1805. if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
  1806. return -1;
  1807. if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
  1808. return 1;
  1809. if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
  1810. return -1;
  1811. if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
  1812. return 1;
  1813. if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
  1814. return -1;
  1815. if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
  1816. return 1;
  1817. if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
  1818. return -1;
  1819. if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
  1820. return 1;
  1821. /* Finally, sort on where the symbol is in memory. The symbols will
  1822. be in at most two malloc'd blocks, one for static syms, one for
  1823. dynamic syms, and we distinguish the two blocks above by testing
  1824. BSF_DYNAMIC. Since we are sorting the symbol pointers which were
  1825. originally in the same order as the symbols (and we're not
  1826. sorting the symbols themselves), this ensures a stable sort. */
  1827. if (a < b)
  1828. return -1;
  1829. if (a > b)
  1830. return 1;
  1831. return 0;
  1832. }
  1833. /* Search SYMS for a symbol of the given VALUE. */
  1834. static asymbol *
  1835. sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
  1836. bfd_vma value)
  1837. {
  1838. size_t mid;
  1839. if (id == (unsigned) -1)
  1840. {
  1841. while (lo < hi)
  1842. {
  1843. mid = (lo + hi) >> 1;
  1844. if (syms[mid]->value + syms[mid]->section->vma < value)
  1845. lo = mid + 1;
  1846. else if (syms[mid]->value + syms[mid]->section->vma > value)
  1847. hi = mid;
  1848. else
  1849. return syms[mid];
  1850. }
  1851. }
  1852. else
  1853. {
  1854. while (lo < hi)
  1855. {
  1856. mid = (lo + hi) >> 1;
  1857. if (syms[mid]->section->id < id)
  1858. lo = mid + 1;
  1859. else if (syms[mid]->section->id > id)
  1860. hi = mid;
  1861. else if (syms[mid]->value < value)
  1862. lo = mid + 1;
  1863. else if (syms[mid]->value > value)
  1864. hi = mid;
  1865. else
  1866. return syms[mid];
  1867. }
  1868. }
  1869. return NULL;
  1870. }
  1871. static bool
  1872. section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
  1873. {
  1874. bfd_vma vma = *(bfd_vma *) ptr;
  1875. return ((section->flags & SEC_ALLOC) != 0
  1876. && section->vma <= vma
  1877. && vma < section->vma + section->size);
  1878. }
  1879. /* Create synthetic symbols, effectively restoring "dot-symbol" function
  1880. entry syms. Also generate @plt symbols for the glink branch table.
  1881. Returns count of synthetic symbols in RET or -1 on error. */
  1882. static long
  1883. ppc64_elf_get_synthetic_symtab (bfd *abfd,
  1884. long static_count, asymbol **static_syms,
  1885. long dyn_count, asymbol **dyn_syms,
  1886. asymbol **ret)
  1887. {
  1888. asymbol *s;
  1889. size_t i, j, count;
  1890. char *names;
  1891. size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
  1892. asection *opd = NULL;
  1893. bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
  1894. asymbol **syms;
  1895. int abi = abiversion (abfd);
  1896. *ret = NULL;
  1897. if (abi < 2)
  1898. {
  1899. opd = bfd_get_section_by_name (abfd, ".opd");
  1900. if (opd == NULL && abi == 1)
  1901. return 0;
  1902. }
  1903. syms = NULL;
  1904. codesecsym = 0;
  1905. codesecsymend = 0;
  1906. secsymend = 0;
  1907. opdsymend = 0;
  1908. symcount = 0;
  1909. if (opd != NULL)
  1910. {
  1911. symcount = static_count;
  1912. if (!relocatable)
  1913. symcount += dyn_count;
  1914. if (symcount == 0)
  1915. return 0;
  1916. syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
  1917. if (syms == NULL)
  1918. return -1;
  1919. if (!relocatable && static_count != 0 && dyn_count != 0)
  1920. {
  1921. /* Use both symbol tables. */
  1922. memcpy (syms, static_syms, static_count * sizeof (*syms));
  1923. memcpy (syms + static_count, dyn_syms,
  1924. (dyn_count + 1) * sizeof (*syms));
  1925. }
  1926. else if (!relocatable && static_count == 0)
  1927. memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
  1928. else
  1929. memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
  1930. /* Trim uninteresting symbols. Interesting symbols are section,
  1931. function, and notype symbols. */
  1932. for (i = 0, j = 0; i < symcount; ++i)
  1933. if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
  1934. | BSF_RELC | BSF_SRELC)) == 0)
  1935. syms[j++] = syms[i];
  1936. symcount = j;
  1937. synthetic_relocatable = relocatable;
  1938. synthetic_opd = opd;
  1939. qsort (syms, symcount, sizeof (*syms), compare_symbols);
  1940. if (!relocatable && symcount > 1)
  1941. {
  1942. /* Trim duplicate syms, since we may have merged the normal
  1943. and dynamic symbols. Actually, we only care about syms
  1944. that have different values, so trim any with the same
  1945. value. Don't consider ifunc and ifunc resolver symbols
  1946. duplicates however, because GDB wants to know whether a
  1947. text symbol is an ifunc resolver. */
  1948. for (i = 1, j = 1; i < symcount; ++i)
  1949. {
  1950. const asymbol *s0 = syms[i - 1];
  1951. const asymbol *s1 = syms[i];
  1952. if ((s0->value + s0->section->vma
  1953. != s1->value + s1->section->vma)
  1954. || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
  1955. != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
  1956. syms[j++] = syms[i];
  1957. }
  1958. symcount = j;
  1959. }
  1960. i = 0;
  1961. /* Note that here and in compare_symbols we can't compare opd and
  1962. sym->section directly. With separate debug info files, the
  1963. symbols will be extracted from the debug file while abfd passed
  1964. to this function is the real binary. */
  1965. if ((syms[i]->flags & BSF_SECTION_SYM) != 0
  1966. && strcmp (syms[i]->section->name, ".opd") == 0)
  1967. ++i;
  1968. codesecsym = i;
  1969. for (; i < symcount; ++i)
  1970. if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
  1971. | SEC_THREAD_LOCAL))
  1972. != (SEC_CODE | SEC_ALLOC))
  1973. || (syms[i]->flags & BSF_SECTION_SYM) == 0)
  1974. break;
  1975. codesecsymend = i;
  1976. for (; i < symcount; ++i)
  1977. if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
  1978. break;
  1979. secsymend = i;
  1980. for (; i < symcount; ++i)
  1981. if (strcmp (syms[i]->section->name, ".opd") != 0)
  1982. break;
  1983. opdsymend = i;
  1984. for (; i < symcount; ++i)
  1985. if (((syms[i]->section->flags
  1986. & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
  1987. != (SEC_CODE | SEC_ALLOC))
  1988. break;
  1989. symcount = i;
  1990. }
  1991. count = 0;
  1992. if (relocatable)
  1993. {
  1994. bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
  1995. arelent *r;
  1996. size_t size;
  1997. size_t relcount;
  1998. if (opdsymend == secsymend)
  1999. goto done;
  2000. slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
  2001. relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
  2002. if (relcount == 0)
  2003. goto done;
  2004. if (!(*slurp_relocs) (abfd, opd, static_syms, false))
  2005. {
  2006. count = -1;
  2007. goto done;
  2008. }
  2009. size = 0;
  2010. for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
  2011. {
  2012. asymbol *sym;
  2013. while (r < opd->relocation + relcount
  2014. && r->address < syms[i]->value + opd->vma)
  2015. ++r;
  2016. if (r == opd->relocation + relcount)
  2017. break;
  2018. if (r->address != syms[i]->value + opd->vma)
  2019. continue;
  2020. if (r->howto->type != R_PPC64_ADDR64)
  2021. continue;
  2022. sym = *r->sym_ptr_ptr;
  2023. if (!sym_exists_at (syms, opdsymend, symcount,
  2024. sym->section->id, sym->value + r->addend))
  2025. {
  2026. ++count;
  2027. size += sizeof (asymbol);
  2028. size += strlen (syms[i]->name) + 2;
  2029. }
  2030. }
  2031. if (size == 0)
  2032. goto done;
  2033. s = *ret = bfd_malloc (size);
  2034. if (s == NULL)
  2035. {
  2036. count = -1;
  2037. goto done;
  2038. }
  2039. names = (char *) (s + count);
  2040. for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
  2041. {
  2042. asymbol *sym;
  2043. while (r < opd->relocation + relcount
  2044. && r->address < syms[i]->value + opd->vma)
  2045. ++r;
  2046. if (r == opd->relocation + relcount)
  2047. break;
  2048. if (r->address != syms[i]->value + opd->vma)
  2049. continue;
  2050. if (r->howto->type != R_PPC64_ADDR64)
  2051. continue;
  2052. sym = *r->sym_ptr_ptr;
  2053. if (!sym_exists_at (syms, opdsymend, symcount,
  2054. sym->section->id, sym->value + r->addend))
  2055. {
  2056. size_t len;
  2057. *s = *syms[i];
  2058. s->flags |= BSF_SYNTHETIC;
  2059. s->section = sym->section;
  2060. s->value = sym->value + r->addend;
  2061. s->name = names;
  2062. *names++ = '.';
  2063. len = strlen (syms[i]->name);
  2064. memcpy (names, syms[i]->name, len + 1);
  2065. names += len + 1;
  2066. /* Have udata.p point back to the original symbol this
  2067. synthetic symbol was derived from. */
  2068. s->udata.p = syms[i];
  2069. s++;
  2070. }
  2071. }
  2072. }
  2073. else
  2074. {
  2075. bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
  2076. bfd_byte *contents = NULL;
  2077. size_t size;
  2078. size_t plt_count = 0;
  2079. bfd_vma glink_vma = 0, resolv_vma = 0;
  2080. asection *dynamic, *glink = NULL, *relplt = NULL;
  2081. arelent *p;
  2082. if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
  2083. {
  2084. free_contents_and_exit_err:
  2085. count = -1;
  2086. free_contents_and_exit:
  2087. free (contents);
  2088. goto done;
  2089. }
  2090. size = 0;
  2091. for (i = secsymend; i < opdsymend; ++i)
  2092. {
  2093. bfd_vma ent;
  2094. /* Ignore bogus symbols. */
  2095. if (syms[i]->value > opd->size - 8)
  2096. continue;
  2097. ent = bfd_get_64 (abfd, contents + syms[i]->value);
  2098. if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
  2099. {
  2100. ++count;
  2101. size += sizeof (asymbol);
  2102. size += strlen (syms[i]->name) + 2;
  2103. }
  2104. }
  2105. /* Get start of .glink stubs from DT_PPC64_GLINK. */
  2106. if (dyn_count != 0
  2107. && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
  2108. {
  2109. bfd_byte *dynbuf, *extdyn, *extdynend;
  2110. size_t extdynsize;
  2111. void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
  2112. if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
  2113. goto free_contents_and_exit_err;
  2114. extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
  2115. swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
  2116. extdyn = dynbuf;
  2117. extdynend = extdyn + dynamic->size;
  2118. for (; extdyn < extdynend; extdyn += extdynsize)
  2119. {
  2120. Elf_Internal_Dyn dyn;
  2121. (*swap_dyn_in) (abfd, extdyn, &dyn);
  2122. if (dyn.d_tag == DT_NULL)
  2123. break;
  2124. if (dyn.d_tag == DT_PPC64_GLINK)
  2125. {
  2126. /* The first glink stub starts at DT_PPC64_GLINK plus 32.
  2127. See comment in ppc64_elf_finish_dynamic_sections. */
  2128. glink_vma = dyn.d_un.d_val + 8 * 4;
  2129. /* The .glink section usually does not survive the final
  2130. link; search for the section (usually .text) where the
  2131. glink stubs now reside. */
  2132. glink = bfd_sections_find_if (abfd, section_covers_vma,
  2133. &glink_vma);
  2134. break;
  2135. }
  2136. }
  2137. free (dynbuf);
  2138. }
  2139. if (glink != NULL)
  2140. {
  2141. /* Determine __glink trampoline by reading the relative branch
  2142. from the first glink stub. */
  2143. bfd_byte buf[4];
  2144. unsigned int off = 0;
  2145. while (bfd_get_section_contents (abfd, glink, buf,
  2146. glink_vma + off - glink->vma, 4))
  2147. {
  2148. unsigned int insn = bfd_get_32 (abfd, buf);
  2149. insn ^= B_DOT;
  2150. if ((insn & ~0x3fffffc) == 0)
  2151. {
  2152. resolv_vma
  2153. = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
  2154. break;
  2155. }
  2156. off += 4;
  2157. if (off > 4)
  2158. break;
  2159. }
  2160. if (resolv_vma)
  2161. size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
  2162. relplt = bfd_get_section_by_name (abfd, ".rela.plt");
  2163. if (relplt != NULL)
  2164. {
  2165. slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
  2166. if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
  2167. goto free_contents_and_exit_err;
  2168. plt_count = relplt->size / sizeof (Elf64_External_Rela);
  2169. size += plt_count * sizeof (asymbol);
  2170. p = relplt->relocation;
  2171. for (i = 0; i < plt_count; i++, p++)
  2172. {
  2173. size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
  2174. if (p->addend != 0)
  2175. size += sizeof ("+0x") - 1 + 16;
  2176. }
  2177. }
  2178. }
  2179. if (size == 0)
  2180. goto free_contents_and_exit;
  2181. s = *ret = bfd_malloc (size);
  2182. if (s == NULL)
  2183. goto free_contents_and_exit_err;
  2184. names = (char *) (s + count + plt_count + (resolv_vma != 0));
  2185. for (i = secsymend; i < opdsymend; ++i)
  2186. {
  2187. bfd_vma ent;
  2188. if (syms[i]->value > opd->size - 8)
  2189. continue;
  2190. ent = bfd_get_64 (abfd, contents + syms[i]->value);
  2191. if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
  2192. {
  2193. size_t lo, hi;
  2194. size_t len;
  2195. asection *sec = abfd->sections;
  2196. *s = *syms[i];
  2197. lo = codesecsym;
  2198. hi = codesecsymend;
  2199. while (lo < hi)
  2200. {
  2201. size_t mid = (lo + hi) >> 1;
  2202. if (syms[mid]->section->vma < ent)
  2203. lo = mid + 1;
  2204. else if (syms[mid]->section->vma > ent)
  2205. hi = mid;
  2206. else
  2207. {
  2208. sec = syms[mid]->section;
  2209. break;
  2210. }
  2211. }
  2212. if (lo >= hi && lo > codesecsym)
  2213. sec = syms[lo - 1]->section;
  2214. for (; sec != NULL; sec = sec->next)
  2215. {
  2216. if (sec->vma > ent)
  2217. break;
  2218. /* SEC_LOAD may not be set if SEC is from a separate debug
  2219. info file. */
  2220. if ((sec->flags & SEC_ALLOC) == 0)
  2221. break;
  2222. if ((sec->flags & SEC_CODE) != 0)
  2223. s->section = sec;
  2224. }
  2225. s->flags |= BSF_SYNTHETIC;
  2226. s->value = ent - s->section->vma;
  2227. s->name = names;
  2228. *names++ = '.';
  2229. len = strlen (syms[i]->name);
  2230. memcpy (names, syms[i]->name, len + 1);
  2231. names += len + 1;
  2232. /* Have udata.p point back to the original symbol this
  2233. synthetic symbol was derived from. */
  2234. s->udata.p = syms[i];
  2235. s++;
  2236. }
  2237. }
  2238. free (contents);
  2239. if (glink != NULL && relplt != NULL)
  2240. {
  2241. if (resolv_vma)
  2242. {
  2243. /* Add a symbol for the main glink trampoline. */
  2244. memset (s, 0, sizeof *s);
  2245. s->the_bfd = abfd;
  2246. s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
  2247. s->section = glink;
  2248. s->value = resolv_vma - glink->vma;
  2249. s->name = names;
  2250. memcpy (names, "__glink_PLTresolve",
  2251. sizeof ("__glink_PLTresolve"));
  2252. names += sizeof ("__glink_PLTresolve");
  2253. s++;
  2254. count++;
  2255. }
  2256. /* FIXME: It would be very much nicer to put sym@plt on the
  2257. stub rather than on the glink branch table entry. The
  2258. objdump disassembler would then use a sensible symbol
  2259. name on plt calls. The difficulty in doing so is
  2260. a) finding the stubs, and,
  2261. b) matching stubs against plt entries, and,
  2262. c) there can be multiple stubs for a given plt entry.
  2263. Solving (a) could be done by code scanning, but older
  2264. ppc64 binaries used different stubs to current code.
  2265. (b) is the tricky one since you need to known the toc
  2266. pointer for at least one function that uses a pic stub to
  2267. be able to calculate the plt address referenced.
  2268. (c) means gdb would need to set multiple breakpoints (or
  2269. find the glink branch itself) when setting breakpoints
  2270. for pending shared library loads. */
  2271. p = relplt->relocation;
  2272. for (i = 0; i < plt_count; i++, p++)
  2273. {
  2274. size_t len;
  2275. *s = **p->sym_ptr_ptr;
  2276. /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
  2277. we are defining a symbol, ensure one of them is set. */
  2278. if ((s->flags & BSF_LOCAL) == 0)
  2279. s->flags |= BSF_GLOBAL;
  2280. s->flags |= BSF_SYNTHETIC;
  2281. s->section = glink;
  2282. s->value = glink_vma - glink->vma;
  2283. s->name = names;
  2284. s->udata.p = NULL;
  2285. len = strlen ((*p->sym_ptr_ptr)->name);
  2286. memcpy (names, (*p->sym_ptr_ptr)->name, len);
  2287. names += len;
  2288. if (p->addend != 0)
  2289. {
  2290. memcpy (names, "+0x", sizeof ("+0x") - 1);
  2291. names += sizeof ("+0x") - 1;
  2292. bfd_sprintf_vma (abfd, names, p->addend);
  2293. names += strlen (names);
  2294. }
  2295. memcpy (names, "@plt", sizeof ("@plt"));
  2296. names += sizeof ("@plt");
  2297. s++;
  2298. if (abi < 2)
  2299. {
  2300. glink_vma += 8;
  2301. if (i >= 0x8000)
  2302. glink_vma += 4;
  2303. }
  2304. else
  2305. glink_vma += 4;
  2306. }
  2307. count += plt_count;
  2308. }
  2309. }
  2310. done:
  2311. free (syms);
  2312. return count;
  2313. }
  2314. /* The following functions are specific to the ELF linker, while
  2315. functions above are used generally. Those named ppc64_elf_* are
  2316. called by the main ELF linker code. They appear in this file more
  2317. or less in the order in which they are called. eg.
  2318. ppc64_elf_check_relocs is called early in the link process,
  2319. ppc64_elf_finish_dynamic_sections is one of the last functions
  2320. called.
  2321. PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
  2322. functions have both a function code symbol and a function descriptor
  2323. symbol. A call to foo in a relocatable object file looks like:
  2324. . .text
  2325. . x:
  2326. . bl .foo
  2327. . nop
  2328. The function definition in another object file might be:
  2329. . .section .opd
  2330. . foo: .quad .foo
  2331. . .quad .TOC.@tocbase
  2332. . .quad 0
  2333. .
  2334. . .text
  2335. . .foo: blr
  2336. When the linker resolves the call during a static link, the branch
  2337. unsurprisingly just goes to .foo and the .opd information is unused.
  2338. If the function definition is in a shared library, things are a little
  2339. different: The call goes via a plt call stub, the opd information gets
  2340. copied to the plt, and the linker patches the nop.
  2341. . x:
  2342. . bl .foo_stub
  2343. . ld 2,40(1)
  2344. .
  2345. .
  2346. . .foo_stub:
  2347. . std 2,40(1) # in practice, the call stub
  2348. . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
  2349. . addi 11,11,Lfoo@toc@l # this is the general idea
  2350. . ld 12,0(11)
  2351. . ld 2,8(11)
  2352. . mtctr 12
  2353. . ld 11,16(11)
  2354. . bctr
  2355. .
  2356. . .section .plt
  2357. . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
  2358. The "reloc ()" notation is supposed to indicate that the linker emits
  2359. an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
  2360. copying.
  2361. What are the difficulties here? Well, firstly, the relocations
  2362. examined by the linker in check_relocs are against the function code
  2363. sym .foo, while the dynamic relocation in the plt is emitted against
  2364. the function descriptor symbol, foo. Somewhere along the line, we need
  2365. to carefully copy dynamic link information from one symbol to the other.
  2366. Secondly, the generic part of the elf linker will make .foo a dynamic
  2367. symbol as is normal for most other backends. We need foo dynamic
  2368. instead, at least for an application final link. However, when
  2369. creating a shared library containing foo, we need to have both symbols
  2370. dynamic so that references to .foo are satisfied during the early
  2371. stages of linking. Otherwise the linker might decide to pull in a
  2372. definition from some other object, eg. a static library.
  2373. Update: As of August 2004, we support a new convention. Function
  2374. calls may use the function descriptor symbol, ie. "bl foo". This
  2375. behaves exactly as "bl .foo". */
  2376. /* Of those relocs that might be copied as dynamic relocs, this
  2377. function selects those that must be copied when linking a shared
  2378. library or PIE, even when the symbol is local. */
  2379. static int
  2380. must_be_dyn_reloc (struct bfd_link_info *info,
  2381. enum elf_ppc64_reloc_type r_type)
  2382. {
  2383. switch (r_type)
  2384. {
  2385. default:
  2386. /* Only relative relocs can be resolved when the object load
  2387. address isn't fixed. DTPREL64 is excluded because the
  2388. dynamic linker needs to differentiate global dynamic from
  2389. local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
  2390. return 1;
  2391. case R_PPC64_REL32:
  2392. case R_PPC64_REL64:
  2393. case R_PPC64_REL30:
  2394. case R_PPC64_TOC16:
  2395. case R_PPC64_TOC16_DS:
  2396. case R_PPC64_TOC16_LO:
  2397. case R_PPC64_TOC16_HI:
  2398. case R_PPC64_TOC16_HA:
  2399. case R_PPC64_TOC16_LO_DS:
  2400. return 0;
  2401. case R_PPC64_TPREL16:
  2402. case R_PPC64_TPREL16_LO:
  2403. case R_PPC64_TPREL16_HI:
  2404. case R_PPC64_TPREL16_HA:
  2405. case R_PPC64_TPREL16_DS:
  2406. case R_PPC64_TPREL16_LO_DS:
  2407. case R_PPC64_TPREL16_HIGH:
  2408. case R_PPC64_TPREL16_HIGHA:
  2409. case R_PPC64_TPREL16_HIGHER:
  2410. case R_PPC64_TPREL16_HIGHERA:
  2411. case R_PPC64_TPREL16_HIGHEST:
  2412. case R_PPC64_TPREL16_HIGHESTA:
  2413. case R_PPC64_TPREL64:
  2414. case R_PPC64_TPREL34:
  2415. /* These relocations are relative but in a shared library the
  2416. linker doesn't know the thread pointer base. */
  2417. return bfd_link_dll (info);
  2418. }
  2419. }
  2420. /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
  2421. copying dynamic variables from a shared lib into an app's .dynbss
  2422. section, and instead use a dynamic relocation to point into the
  2423. shared lib. With code that gcc generates it is vital that this be
  2424. enabled; In the PowerPC64 ELFv1 ABI the address of a function is
  2425. actually the address of a function descriptor which resides in the
  2426. .opd section. gcc uses the descriptor directly rather than going
  2427. via the GOT as some other ABIs do, which means that initialized
  2428. function pointers reference the descriptor. Thus, a function
  2429. pointer initialized to the address of a function in a shared
  2430. library will either require a .dynbss copy and a copy reloc, or a
  2431. dynamic reloc. Using a .dynbss copy redefines the function
  2432. descriptor symbol to point to the copy. This presents a problem as
  2433. a PLT entry for that function is also initialized from the function
  2434. descriptor symbol and the copy may not be initialized first. */
  2435. #define ELIMINATE_COPY_RELOCS 1
  2436. /* Section name for stubs is the associated section name plus this
  2437. string. */
  2438. #define STUB_SUFFIX ".stub"
  2439. /* Linker stubs.
  2440. ppc_stub_long_branch:
  2441. Used when a 14 bit branch (or even a 24 bit branch) can't reach its
  2442. destination, but a 24 bit branch in a stub section will reach.
  2443. . b dest
  2444. ppc_stub_plt_branch:
  2445. Similar to the above, but a 24 bit branch in the stub section won't
  2446. reach its destination.
  2447. . addis %r12,%r2,xxx@toc@ha
  2448. . ld %r12,xxx@toc@l(%r12)
  2449. . mtctr %r12
  2450. . bctr
  2451. ppc_stub_plt_call:
  2452. Used to call a function in a shared library. If it so happens that
  2453. the plt entry referenced crosses a 64k boundary, then an extra
  2454. "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
  2455. An r2save variant starts with "std %r2,40(%r1)".
  2456. . addis %r11,%r2,xxx@toc@ha
  2457. . ld %r12,xxx+0@toc@l(%r11)
  2458. . mtctr %r12
  2459. . ld %r2,xxx+8@toc@l(%r11)
  2460. . ld %r11,xxx+16@toc@l(%r11)
  2461. . bctr
  2462. ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
  2463. code to adjust the value and save r2 to support multiple toc sections.
  2464. A ppc_stub_long_branch with an r2 offset looks like:
  2465. . std %r2,40(%r1)
  2466. . addis %r2,%r2,off@ha
  2467. . addi %r2,%r2,off@l
  2468. . b dest
  2469. A ppc_stub_plt_branch with an r2 offset looks like:
  2470. . std %r2,40(%r1)
  2471. . addis %r12,%r2,xxx@toc@ha
  2472. . ld %r12,xxx@toc@l(%r12)
  2473. . addis %r2,%r2,off@ha
  2474. . addi %r2,%r2,off@l
  2475. . mtctr %r12
  2476. . bctr
  2477. All of the above stubs are shown as their ELFv1 variants. ELFv2
  2478. variants exist too, simpler for plt calls since a new toc pointer
  2479. and static chain are not loaded by the stub. In addition, ELFv2
  2480. has some more complex stubs to handle calls marked with NOTOC
  2481. relocs from functions where r2 is not a valid toc pointer.
  2482. ppc_stub_long_branch_p9notoc:
  2483. . mflr %r12
  2484. . bcl 20,31,1f
  2485. . 1:
  2486. . mflr %r11
  2487. . mtlr %r12
  2488. . addis %r12,%r11,dest-1b@ha
  2489. . addi %r12,%r12,dest-1b@l
  2490. . b dest
  2491. ppc_stub_plt_branch_p9notoc:
  2492. . mflr %r12
  2493. . bcl 20,31,1f
  2494. . 1:
  2495. . mflr %r11
  2496. . mtlr %r12
  2497. . lis %r12,xxx-1b@highest
  2498. . ori %r12,%r12,xxx-1b@higher
  2499. . sldi %r12,%r12,32
  2500. . oris %r12,%r12,xxx-1b@high
  2501. . ori %r12,%r12,xxx-1b@l
  2502. . add %r12,%r11,%r12
  2503. . mtctr %r12
  2504. . bctr
  2505. ppc_stub_plt_call_p9notoc:
  2506. . mflr %r12
  2507. . bcl 20,31,1f
  2508. . 1:
  2509. . mflr %r11
  2510. . mtlr %r12
  2511. . lis %r12,xxx-1b@highest
  2512. . ori %r12,%r12,xxx-1b@higher
  2513. . sldi %r12,%r12,32
  2514. . oris %r12,%r12,xxx-1b@high
  2515. . ori %r12,%r12,xxx-1b@l
  2516. . ldx %r12,%r11,%r12
  2517. . mtctr %r12
  2518. . bctr
  2519. There are also ELFv1 power10 variants of these stubs.
  2520. ppc_stub_long_branch_notoc:
  2521. . pla %r12,dest@pcrel
  2522. . b dest
  2523. ppc_stub_plt_branch_notoc:
  2524. . lis %r11,(dest-1f)@highesta34
  2525. . ori %r11,%r11,(dest-1f)@highera34
  2526. . sldi %r11,%r11,34
  2527. . 1: pla %r12,dest@pcrel
  2528. . add %r12,%r11,%r12
  2529. . mtctr %r12
  2530. . bctr
  2531. ppc_stub_plt_call_notoc:
  2532. . lis %r11,(xxx-1f)@highesta34
  2533. . ori %r11,%r11,(xxx-1f)@highera34
  2534. . sldi %r11,%r11,34
  2535. . 1: pla %r12,xxx@pcrel
  2536. . ldx %r12,%r11,%r12
  2537. . mtctr %r12
  2538. . bctr
  2539. In cases where the high instructions would add zero, they are
  2540. omitted and following instructions modified in some cases.
  2541. For example, a power10 ppc_stub_plt_call_notoc might simplify down
  2542. to
  2543. . pld %r12,xxx@pcrel
  2544. . mtctr %r12
  2545. . bctr
  2546. Stub variants may be merged. For example, if printf is called from
  2547. code with the tocsave optimization (ie. r2 saved in function
  2548. prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
  2549. and from other code without the tocsave optimization requiring a
  2550. ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
  2551. type will be created. Calls with the tocsave optimization will
  2552. enter this stub after the instruction saving r2. A similar
  2553. situation exists when calls are marked with R_PPC64_REL24_NOTOC
  2554. relocations. These require a ppc_stub_plt_call_notoc linkage stub
  2555. to call an external function like printf. If other calls to printf
  2556. require a ppc_stub_plt_call linkage stub then a single
  2557. ppc_stub_plt_call_notoc linkage stub may be used for both types of
  2558. call. */
  2559. enum ppc_stub_main_type
  2560. {
  2561. ppc_stub_none,
  2562. ppc_stub_long_branch,
  2563. ppc_stub_plt_branch,
  2564. ppc_stub_plt_call,
  2565. ppc_stub_global_entry,
  2566. ppc_stub_save_res
  2567. };
  2568. /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
  2569. these variations. */
  2570. enum ppc_stub_sub_type
  2571. {
  2572. ppc_stub_toc,
  2573. ppc_stub_notoc,
  2574. ppc_stub_p9notoc
  2575. };
  2576. struct ppc_stub_type
  2577. {
  2578. ENUM_BITFIELD (ppc_stub_main_type) main : 3;
  2579. ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
  2580. unsigned int r2save : 1;
  2581. };
  2582. /* Information on stub grouping. */
  2583. struct map_stub
  2584. {
  2585. /* The stub section. */
  2586. asection *stub_sec;
  2587. /* This is the section to which stubs in the group will be attached. */
  2588. asection *link_sec;
  2589. /* Next group. */
  2590. struct map_stub *next;
  2591. /* Whether to emit a copy of register save/restore functions in this
  2592. group. */
  2593. int needs_save_res;
  2594. /* Current offset within stubs after the insn restoring lr in a
  2595. _notoc or _both stub using bcl for pc-relative addressing, or
  2596. after the insn restoring lr in a __tls_get_addr_opt plt stub. */
  2597. unsigned int lr_restore;
  2598. /* Accumulated size of EH info emitted to describe return address
  2599. if stubs modify lr. Does not include 17 byte FDE header. */
  2600. unsigned int eh_size;
  2601. /* Offset in glink_eh_frame to the start of EH info for this group. */
  2602. unsigned int eh_base;
  2603. };
  2604. struct ppc_stub_hash_entry
  2605. {
  2606. /* Base hash table entry structure. */
  2607. struct bfd_hash_entry root;
  2608. struct ppc_stub_type type;
  2609. /* Group information. */
  2610. struct map_stub *group;
  2611. /* Offset within stub_sec of the beginning of this stub. */
  2612. bfd_vma stub_offset;
  2613. /* Given the symbol's value and its section we can determine its final
  2614. value when building the stubs (so the stub knows where to jump. */
  2615. bfd_vma target_value;
  2616. asection *target_section;
  2617. /* The symbol table entry, if any, that this was derived from. */
  2618. struct ppc_link_hash_entry *h;
  2619. struct plt_entry *plt_ent;
  2620. /* Symbol type. */
  2621. unsigned char symtype;
  2622. /* Symbol st_other. */
  2623. unsigned char other;
  2624. };
  2625. struct ppc_branch_hash_entry
  2626. {
  2627. /* Base hash table entry structure. */
  2628. struct bfd_hash_entry root;
  2629. /* Offset within branch lookup table. */
  2630. unsigned int offset;
  2631. /* Generation marker. */
  2632. unsigned int iter;
  2633. };
  2634. /* Used to track dynamic relocations. */
  2635. struct ppc_dyn_relocs
  2636. {
  2637. struct ppc_dyn_relocs *next;
  2638. /* The input section of the reloc. */
  2639. asection *sec;
  2640. /* Total number of relocs copied for the input section. */
  2641. unsigned int count;
  2642. /* Number of pc-relative relocs copied for the input section. */
  2643. unsigned int pc_count;
  2644. /* Number of relocs that might become R_PPC64_RELATIVE. */
  2645. unsigned int rel_count;
  2646. };
  2647. struct ppc_local_dyn_relocs
  2648. {
  2649. struct ppc_local_dyn_relocs *next;
  2650. /* The input section of the reloc. */
  2651. asection *sec;
  2652. /* Total number of relocs copied for the input section. */
  2653. unsigned int count;
  2654. /* Number of relocs that might become R_PPC64_RELATIVE. */
  2655. unsigned int rel_count : 31;
  2656. /* Whether this entry is for STT_GNU_IFUNC symbols. */
  2657. unsigned int ifunc : 1;
  2658. };
  2659. struct ppc_link_hash_entry
  2660. {
  2661. struct elf_link_hash_entry elf;
  2662. union
  2663. {
  2664. /* A pointer to the most recently used stub hash entry against this
  2665. symbol. */
  2666. struct ppc_stub_hash_entry *stub_cache;
  2667. /* A pointer to the next symbol starting with a '.' */
  2668. struct ppc_link_hash_entry *next_dot_sym;
  2669. } u;
  2670. /* Link between function code and descriptor symbols. */
  2671. struct ppc_link_hash_entry *oh;
  2672. /* Flag function code and descriptor symbols. */
  2673. unsigned int is_func:1;
  2674. unsigned int is_func_descriptor:1;
  2675. unsigned int fake:1;
  2676. /* Whether global opd/toc sym has been adjusted or not.
  2677. After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
  2678. should be set for all globals defined in any opd/toc section. */
  2679. unsigned int adjust_done:1;
  2680. /* Set if this is an out-of-line register save/restore function,
  2681. with non-standard calling convention. */
  2682. unsigned int save_res:1;
  2683. /* Set if a duplicate symbol with non-zero localentry is detected,
  2684. even when the duplicate symbol does not provide a definition. */
  2685. unsigned int non_zero_localentry:1;
  2686. /* Contexts in which symbol is used in the GOT (or TOC).
  2687. Bits are or'd into the mask as the corresponding relocs are
  2688. encountered during check_relocs, with TLS_TLS being set when any
  2689. of the other TLS bits are set. tls_optimize clears bits when
  2690. optimizing to indicate the corresponding GOT entry type is not
  2691. needed. If set, TLS_TLS is never cleared. tls_optimize may also
  2692. set TLS_GDIE when a GD reloc turns into an IE one.
  2693. These flags are also kept for local symbols. */
  2694. #define TLS_TLS 1 /* Any TLS reloc. */
  2695. #define TLS_GD 2 /* GD reloc. */
  2696. #define TLS_LD 4 /* LD reloc. */
  2697. #define TLS_TPREL 8 /* TPREL reloc, => IE. */
  2698. #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
  2699. #define TLS_MARK 32 /* __tls_get_addr call marked. */
  2700. #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
  2701. #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
  2702. unsigned char tls_mask;
  2703. /* The above field is also used to mark function symbols. In which
  2704. case TLS_TLS will be 0. */
  2705. #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
  2706. #define PLT_KEEP 4 /* inline plt call requires plt entry. */
  2707. #define NON_GOT 256 /* local symbol plt, not stored. */
  2708. };
  2709. static inline struct ppc_link_hash_entry *
  2710. ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
  2711. {
  2712. return (struct ppc_link_hash_entry *) ent;
  2713. }
  2714. static inline struct elf_link_hash_entry *
  2715. elf_hash_entry (struct ppc_link_hash_entry *ent)
  2716. {
  2717. return (struct elf_link_hash_entry *) ent;
  2718. }
  2719. /* ppc64 ELF linker hash table. */
  2720. struct ppc_link_hash_table
  2721. {
  2722. struct elf_link_hash_table elf;
  2723. /* The stub hash table. */
  2724. struct bfd_hash_table stub_hash_table;
  2725. /* Another hash table for plt_branch stubs. */
  2726. struct bfd_hash_table branch_hash_table;
  2727. /* Hash table for function prologue tocsave. */
  2728. htab_t tocsave_htab;
  2729. /* Various options and other info passed from the linker. */
  2730. struct ppc64_elf_params *params;
  2731. /* The size of sec_info below. */
  2732. unsigned int sec_info_arr_size;
  2733. /* Per-section array of extra section info. Done this way rather
  2734. than as part of ppc64_elf_section_data so we have the info for
  2735. non-ppc64 sections. */
  2736. struct
  2737. {
  2738. /* Along with elf_gp, specifies the TOC pointer used by this section. */
  2739. bfd_vma toc_off;
  2740. union
  2741. {
  2742. /* The section group that this section belongs to. */
  2743. struct map_stub *group;
  2744. /* A temp section list pointer. */
  2745. asection *list;
  2746. } u;
  2747. } *sec_info;
  2748. /* Linked list of groups. */
  2749. struct map_stub *group;
  2750. /* Temp used when calculating TOC pointers. */
  2751. bfd_vma toc_curr;
  2752. bfd *toc_bfd;
  2753. asection *toc_first_sec;
  2754. /* Used when adding symbols. */
  2755. struct ppc_link_hash_entry *dot_syms;
  2756. /* Shortcuts to get to dynamic linker sections. */
  2757. asection *glink;
  2758. asection *global_entry;
  2759. asection *sfpr;
  2760. asection *pltlocal;
  2761. asection *relpltlocal;
  2762. asection *brlt;
  2763. asection *relbrlt;
  2764. asection *glink_eh_frame;
  2765. /* Shortcut to .__tls_get_addr and __tls_get_addr. */
  2766. struct ppc_link_hash_entry *tls_get_addr;
  2767. struct ppc_link_hash_entry *tls_get_addr_fd;
  2768. struct ppc_link_hash_entry *tga_desc;
  2769. struct ppc_link_hash_entry *tga_desc_fd;
  2770. struct map_stub *tga_group;
  2771. /* The size of reliplt used by got entry relocs. */
  2772. bfd_size_type got_reli_size;
  2773. /* DT_RELR array of section/r_offset. */
  2774. size_t relr_alloc;
  2775. size_t relr_count;
  2776. struct
  2777. {
  2778. asection *sec;
  2779. bfd_vma off;
  2780. } *relr;
  2781. /* Statistics. */
  2782. unsigned long stub_count[ppc_stub_save_res];
  2783. /* Number of stubs against global syms. */
  2784. unsigned long stub_globals;
  2785. /* Set if we're linking code with function descriptors. */
  2786. unsigned int opd_abi:1;
  2787. /* Support for multiple toc sections. */
  2788. unsigned int do_multi_toc:1;
  2789. unsigned int multi_toc_needed:1;
  2790. unsigned int second_toc_pass:1;
  2791. unsigned int do_toc_opt:1;
  2792. /* Set if tls optimization is enabled. */
  2793. unsigned int do_tls_opt:1;
  2794. /* Set if inline plt calls should be converted to direct calls. */
  2795. unsigned int can_convert_all_inline_plt:1;
  2796. /* Set if a stub_offset changed. */
  2797. unsigned int stub_changed:1;
  2798. /* Set on error. */
  2799. unsigned int stub_error:1;
  2800. /* Whether func_desc_adjust needs to be run over symbols. */
  2801. unsigned int need_func_desc_adj:1;
  2802. /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
  2803. unsigned int has_plt_localentry0:1;
  2804. /* Whether calls are made via the PLT from NOTOC functions. */
  2805. unsigned int notoc_plt:1;
  2806. /* Whether any code linked seems to be Power10. */
  2807. unsigned int has_power10_relocs:1;
  2808. /* Incremented every time we size stubs. */
  2809. unsigned int stub_iteration;
  2810. /* After 20 iterations of stub sizing we no longer allow stubs to
  2811. shrink. This is to break out of a pathological case where adding
  2812. stubs or increasing their size on one iteration decreases section
  2813. gaps (perhaps due to alignment), which then results in smaller
  2814. stubs on the next iteration. */
  2815. #define STUB_SHRINK_ITER 20
  2816. };
  2817. /* Rename some of the generic section flags to better document how they
  2818. are used here. */
  2819. /* Nonzero if this section has TLS related relocations. */
  2820. #define has_tls_reloc sec_flg0
  2821. /* Nonzero if this section has a call to __tls_get_addr lacking marker
  2822. relocations. */
  2823. #define nomark_tls_get_addr sec_flg1
  2824. /* Nonzero if this section has any toc or got relocs. */
  2825. #define has_toc_reloc sec_flg2
  2826. /* Nonzero if this section has a call to another section that uses
  2827. the toc or got. */
  2828. #define makes_toc_func_call sec_flg3
  2829. /* Recursion protection when determining above flag. */
  2830. #define call_check_in_progress sec_flg4
  2831. #define call_check_done sec_flg5
  2832. /* Get the ppc64 ELF linker hash table from a link_info structure. */
  2833. #define ppc_hash_table(p) \
  2834. ((is_elf_hash_table ((p)->hash) \
  2835. && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
  2836. ? (struct ppc_link_hash_table *) (p)->hash : NULL)
  2837. #define ppc_stub_hash_lookup(table, string, create, copy) \
  2838. ((struct ppc_stub_hash_entry *) \
  2839. bfd_hash_lookup ((table), (string), (create), (copy)))
  2840. #define ppc_branch_hash_lookup(table, string, create, copy) \
  2841. ((struct ppc_branch_hash_entry *) \
  2842. bfd_hash_lookup ((table), (string), (create), (copy)))
  2843. /* Create an entry in the stub hash table. */
  2844. static struct bfd_hash_entry *
  2845. stub_hash_newfunc (struct bfd_hash_entry *entry,
  2846. struct bfd_hash_table *table,
  2847. const char *string)
  2848. {
  2849. /* Allocate the structure if it has not already been allocated by a
  2850. subclass. */
  2851. if (entry == NULL)
  2852. {
  2853. entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
  2854. if (entry == NULL)
  2855. return entry;
  2856. }
  2857. /* Call the allocation method of the superclass. */
  2858. entry = bfd_hash_newfunc (entry, table, string);
  2859. if (entry != NULL)
  2860. {
  2861. struct ppc_stub_hash_entry *eh;
  2862. /* Initialize the local fields. */
  2863. eh = (struct ppc_stub_hash_entry *) entry;
  2864. eh->type.main = ppc_stub_none;
  2865. eh->type.sub = ppc_stub_toc;
  2866. eh->type.r2save = 0;
  2867. eh->group = NULL;
  2868. eh->stub_offset = 0;
  2869. eh->target_value = 0;
  2870. eh->target_section = NULL;
  2871. eh->h = NULL;
  2872. eh->plt_ent = NULL;
  2873. eh->other = 0;
  2874. }
  2875. return entry;
  2876. }
  2877. /* Create an entry in the branch hash table. */
  2878. static struct bfd_hash_entry *
  2879. branch_hash_newfunc (struct bfd_hash_entry *entry,
  2880. struct bfd_hash_table *table,
  2881. const char *string)
  2882. {
  2883. /* Allocate the structure if it has not already been allocated by a
  2884. subclass. */
  2885. if (entry == NULL)
  2886. {
  2887. entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
  2888. if (entry == NULL)
  2889. return entry;
  2890. }
  2891. /* Call the allocation method of the superclass. */
  2892. entry = bfd_hash_newfunc (entry, table, string);
  2893. if (entry != NULL)
  2894. {
  2895. struct ppc_branch_hash_entry *eh;
  2896. /* Initialize the local fields. */
  2897. eh = (struct ppc_branch_hash_entry *) entry;
  2898. eh->offset = 0;
  2899. eh->iter = 0;
  2900. }
  2901. return entry;
  2902. }
  2903. /* Create an entry in a ppc64 ELF linker hash table. */
  2904. static struct bfd_hash_entry *
  2905. link_hash_newfunc (struct bfd_hash_entry *entry,
  2906. struct bfd_hash_table *table,
  2907. const char *string)
  2908. {
  2909. /* Allocate the structure if it has not already been allocated by a
  2910. subclass. */
  2911. if (entry == NULL)
  2912. {
  2913. entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
  2914. if (entry == NULL)
  2915. return entry;
  2916. }
  2917. /* Call the allocation method of the superclass. */
  2918. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  2919. if (entry != NULL)
  2920. {
  2921. struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
  2922. memset (&eh->u.stub_cache, 0,
  2923. (sizeof (struct ppc_link_hash_entry)
  2924. - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
  2925. /* When making function calls, old ABI code references function entry
  2926. points (dot symbols), while new ABI code references the function
  2927. descriptor symbol. We need to make any combination of reference and
  2928. definition work together, without breaking archive linking.
  2929. For a defined function "foo" and an undefined call to "bar":
  2930. An old object defines "foo" and ".foo", references ".bar" (possibly
  2931. "bar" too).
  2932. A new object defines "foo" and references "bar".
  2933. A new object thus has no problem with its undefined symbols being
  2934. satisfied by definitions in an old object. On the other hand, the
  2935. old object won't have ".bar" satisfied by a new object.
  2936. Keep a list of newly added dot-symbols. */
  2937. if (string[0] == '.')
  2938. {
  2939. struct ppc_link_hash_table *htab;
  2940. htab = (struct ppc_link_hash_table *) table;
  2941. eh->u.next_dot_sym = htab->dot_syms;
  2942. htab->dot_syms = eh;
  2943. }
  2944. }
  2945. return entry;
  2946. }
  2947. struct tocsave_entry
  2948. {
  2949. asection *sec;
  2950. bfd_vma offset;
  2951. };
  2952. static hashval_t
  2953. tocsave_htab_hash (const void *p)
  2954. {
  2955. const struct tocsave_entry *e = (const struct tocsave_entry *) p;
  2956. return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
  2957. }
  2958. static int
  2959. tocsave_htab_eq (const void *p1, const void *p2)
  2960. {
  2961. const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
  2962. const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
  2963. return e1->sec == e2->sec && e1->offset == e2->offset;
  2964. }
  2965. /* Destroy a ppc64 ELF linker hash table. */
  2966. static void
  2967. ppc64_elf_link_hash_table_free (bfd *obfd)
  2968. {
  2969. struct ppc_link_hash_table *htab;
  2970. htab = (struct ppc_link_hash_table *) obfd->link.hash;
  2971. if (htab->tocsave_htab)
  2972. htab_delete (htab->tocsave_htab);
  2973. bfd_hash_table_free (&htab->branch_hash_table);
  2974. bfd_hash_table_free (&htab->stub_hash_table);
  2975. _bfd_elf_link_hash_table_free (obfd);
  2976. }
  2977. /* Create a ppc64 ELF linker hash table. */
  2978. static struct bfd_link_hash_table *
  2979. ppc64_elf_link_hash_table_create (bfd *abfd)
  2980. {
  2981. struct ppc_link_hash_table *htab;
  2982. size_t amt = sizeof (struct ppc_link_hash_table);
  2983. htab = bfd_zmalloc (amt);
  2984. if (htab == NULL)
  2985. return NULL;
  2986. if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
  2987. sizeof (struct ppc_link_hash_entry),
  2988. PPC64_ELF_DATA))
  2989. {
  2990. free (htab);
  2991. return NULL;
  2992. }
  2993. /* Init the stub hash table too. */
  2994. if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
  2995. sizeof (struct ppc_stub_hash_entry)))
  2996. {
  2997. _bfd_elf_link_hash_table_free (abfd);
  2998. return NULL;
  2999. }
  3000. /* And the branch hash table. */
  3001. if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
  3002. sizeof (struct ppc_branch_hash_entry)))
  3003. {
  3004. bfd_hash_table_free (&htab->stub_hash_table);
  3005. _bfd_elf_link_hash_table_free (abfd);
  3006. return NULL;
  3007. }
  3008. htab->tocsave_htab = htab_try_create (1024,
  3009. tocsave_htab_hash,
  3010. tocsave_htab_eq,
  3011. NULL);
  3012. if (htab->tocsave_htab == NULL)
  3013. {
  3014. ppc64_elf_link_hash_table_free (abfd);
  3015. return NULL;
  3016. }
  3017. htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
  3018. /* Initializing two fields of the union is just cosmetic. We really
  3019. only care about glist, but when compiled on a 32-bit host the
  3020. bfd_vma fields are larger. Setting the bfd_vma to zero makes
  3021. debugger inspection of these fields look nicer. */
  3022. htab->elf.init_got_refcount.refcount = 0;
  3023. htab->elf.init_got_refcount.glist = NULL;
  3024. htab->elf.init_plt_refcount.refcount = 0;
  3025. htab->elf.init_plt_refcount.glist = NULL;
  3026. htab->elf.init_got_offset.offset = 0;
  3027. htab->elf.init_got_offset.glist = NULL;
  3028. htab->elf.init_plt_offset.offset = 0;
  3029. htab->elf.init_plt_offset.glist = NULL;
  3030. return &htab->elf.root;
  3031. }
  3032. /* Create sections for linker generated code. */
  3033. static bool
  3034. create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
  3035. {
  3036. struct ppc_link_hash_table *htab;
  3037. flagword flags;
  3038. htab = ppc_hash_table (info);
  3039. flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
  3040. | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  3041. if (htab->params->save_restore_funcs)
  3042. {
  3043. /* Create .sfpr for code to save and restore fp regs. */
  3044. htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
  3045. flags);
  3046. if (htab->sfpr == NULL
  3047. || !bfd_set_section_alignment (htab->sfpr, 2))
  3048. return false;
  3049. }
  3050. if (bfd_link_relocatable (info))
  3051. return true;
  3052. /* Create .glink for lazy dynamic linking support. */
  3053. htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
  3054. flags);
  3055. if (htab->glink == NULL
  3056. || !bfd_set_section_alignment (htab->glink, 3))
  3057. return false;
  3058. /* The part of .glink used by global entry stubs, separate so that
  3059. it can be aligned appropriately without affecting htab->glink. */
  3060. htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
  3061. flags);
  3062. if (htab->global_entry == NULL
  3063. || !bfd_set_section_alignment (htab->global_entry, 2))
  3064. return false;
  3065. if (!info->no_ld_generated_unwind_info)
  3066. {
  3067. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
  3068. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  3069. htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
  3070. ".eh_frame",
  3071. flags);
  3072. if (htab->glink_eh_frame == NULL
  3073. || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
  3074. return false;
  3075. }
  3076. flags = SEC_ALLOC | SEC_LINKER_CREATED;
  3077. htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
  3078. if (htab->elf.iplt == NULL
  3079. || !bfd_set_section_alignment (htab->elf.iplt, 3))
  3080. return false;
  3081. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
  3082. | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  3083. htab->elf.irelplt
  3084. = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
  3085. if (htab->elf.irelplt == NULL
  3086. || !bfd_set_section_alignment (htab->elf.irelplt, 3))
  3087. return false;
  3088. /* Create branch lookup table for plt_branch stubs. */
  3089. flags = (SEC_ALLOC | SEC_LOAD
  3090. | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  3091. htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
  3092. flags);
  3093. if (htab->brlt == NULL
  3094. || !bfd_set_section_alignment (htab->brlt, 3))
  3095. return false;
  3096. /* Local plt entries, put in .branch_lt but a separate section for
  3097. convenience. */
  3098. htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
  3099. flags);
  3100. if (htab->pltlocal == NULL
  3101. || !bfd_set_section_alignment (htab->pltlocal, 3))
  3102. return false;
  3103. if (!bfd_link_pic (info))
  3104. return true;
  3105. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
  3106. | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  3107. htab->relbrlt
  3108. = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
  3109. if (htab->relbrlt == NULL
  3110. || !bfd_set_section_alignment (htab->relbrlt, 3))
  3111. return false;
  3112. htab->relpltlocal
  3113. = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
  3114. if (htab->relpltlocal == NULL
  3115. || !bfd_set_section_alignment (htab->relpltlocal, 3))
  3116. return false;
  3117. return true;
  3118. }
  3119. /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
  3120. bool
  3121. ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
  3122. struct ppc64_elf_params *params)
  3123. {
  3124. struct ppc_link_hash_table *htab;
  3125. elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
  3126. /* Always hook our dynamic sections into the first bfd, which is the
  3127. linker created stub bfd. This ensures that the GOT header is at
  3128. the start of the output TOC section. */
  3129. htab = ppc_hash_table (info);
  3130. htab->elf.dynobj = params->stub_bfd;
  3131. htab->params = params;
  3132. return create_linkage_sections (htab->elf.dynobj, info);
  3133. }
  3134. /* Build a name for an entry in the stub hash table. */
  3135. static char *
  3136. ppc_stub_name (const asection *input_section,
  3137. const asection *sym_sec,
  3138. const struct ppc_link_hash_entry *h,
  3139. const Elf_Internal_Rela *rel)
  3140. {
  3141. char *stub_name;
  3142. ssize_t len;
  3143. /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
  3144. offsets from a sym as a branch target? In fact, we could
  3145. probably assume the addend is always zero. */
  3146. BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
  3147. if (h)
  3148. {
  3149. len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
  3150. stub_name = bfd_malloc (len);
  3151. if (stub_name == NULL)
  3152. return stub_name;
  3153. len = sprintf (stub_name, "%08x.%s+%x",
  3154. input_section->id & 0xffffffff,
  3155. h->elf.root.root.string,
  3156. (int) rel->r_addend & 0xffffffff);
  3157. }
  3158. else
  3159. {
  3160. len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
  3161. stub_name = bfd_malloc (len);
  3162. if (stub_name == NULL)
  3163. return stub_name;
  3164. len = sprintf (stub_name, "%08x.%x:%x+%x",
  3165. input_section->id & 0xffffffff,
  3166. sym_sec->id & 0xffffffff,
  3167. (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
  3168. (int) rel->r_addend & 0xffffffff);
  3169. }
  3170. if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
  3171. stub_name[len - 2] = 0;
  3172. return stub_name;
  3173. }
  3174. /* If mixing power10 with non-power10 code and --power10-stubs is not
  3175. specified (or is auto) then there may be multiple stub types for any
  3176. given symbol. Up to three classes of stubs are stored in separate
  3177. stub_hash_table entries having the same key string. The entries
  3178. will always be adjacent on entry->root.next chain, even if hash
  3179. table resizing occurs. This function selects the correct entry to
  3180. use. */
  3181. static struct ppc_stub_hash_entry *
  3182. select_alt_stub (struct ppc_stub_hash_entry *entry,
  3183. enum elf_ppc64_reloc_type r_type)
  3184. {
  3185. enum ppc_stub_sub_type subt;
  3186. switch (r_type)
  3187. {
  3188. case R_PPC64_REL24_NOTOC:
  3189. subt = ppc_stub_notoc;
  3190. break;
  3191. case R_PPC64_REL24_P9NOTOC:
  3192. subt = ppc_stub_p9notoc;
  3193. break;
  3194. default:
  3195. subt = ppc_stub_toc;
  3196. break;
  3197. }
  3198. while (entry != NULL && entry->type.sub != subt)
  3199. {
  3200. const char *stub_name = entry->root.string;
  3201. entry = (struct ppc_stub_hash_entry *) entry->root.next;
  3202. if (entry != NULL
  3203. && entry->root.string != stub_name)
  3204. entry = NULL;
  3205. }
  3206. return entry;
  3207. }
  3208. /* Look up an entry in the stub hash. Stub entries are cached because
  3209. creating the stub name takes a bit of time. */
  3210. static struct ppc_stub_hash_entry *
  3211. ppc_get_stub_entry (const asection *input_section,
  3212. const asection *sym_sec,
  3213. struct ppc_link_hash_entry *h,
  3214. const Elf_Internal_Rela *rel,
  3215. struct ppc_link_hash_table *htab)
  3216. {
  3217. struct ppc_stub_hash_entry *stub_entry;
  3218. struct map_stub *group;
  3219. /* If this input section is part of a group of sections sharing one
  3220. stub section, then use the id of the first section in the group.
  3221. Stub names need to include a section id, as there may well be
  3222. more than one stub used to reach say, printf, and we need to
  3223. distinguish between them. */
  3224. group = htab->sec_info[input_section->id].u.group;
  3225. if (group == NULL)
  3226. return NULL;
  3227. if (h != NULL && h->u.stub_cache != NULL
  3228. && h->u.stub_cache->h == h
  3229. && h->u.stub_cache->group == group)
  3230. {
  3231. stub_entry = h->u.stub_cache;
  3232. }
  3233. else
  3234. {
  3235. char *stub_name;
  3236. stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
  3237. if (stub_name == NULL)
  3238. return NULL;
  3239. stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
  3240. stub_name, false, false);
  3241. if (h != NULL)
  3242. h->u.stub_cache = stub_entry;
  3243. free (stub_name);
  3244. }
  3245. if (stub_entry != NULL && htab->params->power10_stubs == -1)
  3246. stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
  3247. return stub_entry;
  3248. }
  3249. /* Add a new stub entry to the stub hash. Not all fields of the new
  3250. stub entry are initialised. */
  3251. static struct ppc_stub_hash_entry *
  3252. ppc_add_stub (const char *stub_name,
  3253. asection *section,
  3254. struct bfd_link_info *info)
  3255. {
  3256. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  3257. struct map_stub *group;
  3258. asection *link_sec;
  3259. asection *stub_sec;
  3260. struct ppc_stub_hash_entry *stub_entry;
  3261. group = htab->sec_info[section->id].u.group;
  3262. link_sec = group->link_sec;
  3263. stub_sec = group->stub_sec;
  3264. if (stub_sec == NULL)
  3265. {
  3266. size_t namelen;
  3267. bfd_size_type len;
  3268. char *s_name;
  3269. namelen = strlen (link_sec->name);
  3270. len = namelen + sizeof (STUB_SUFFIX);
  3271. s_name = bfd_alloc (htab->params->stub_bfd, len);
  3272. if (s_name == NULL)
  3273. return NULL;
  3274. memcpy (s_name, link_sec->name, namelen);
  3275. memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
  3276. stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
  3277. if (stub_sec == NULL)
  3278. return NULL;
  3279. group->stub_sec = stub_sec;
  3280. }
  3281. /* Enter this entry into the linker stub hash table. */
  3282. stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
  3283. true, false);
  3284. if (stub_entry == NULL)
  3285. {
  3286. /* xgettext:c-format */
  3287. _bfd_error_handler (_("%pB: cannot create stub entry %s"),
  3288. section->owner, stub_name);
  3289. return NULL;
  3290. }
  3291. stub_entry->group = group;
  3292. stub_entry->stub_offset = 0;
  3293. return stub_entry;
  3294. }
  3295. /* A stub has already been created, but it may not be the required
  3296. type. We shouldn't be transitioning from plt_call to long_branch
  3297. stubs or vice versa, but we might be upgrading from plt_call to
  3298. plt_call with r2save for example. */
  3299. static bool
  3300. ppc_merge_stub (struct ppc_link_hash_table *htab,
  3301. struct ppc_stub_hash_entry *stub_entry,
  3302. struct ppc_stub_type stub_type,
  3303. enum elf_ppc64_reloc_type r_type)
  3304. {
  3305. struct ppc_stub_type old_type = stub_entry->type;
  3306. if (old_type.main == ppc_stub_save_res)
  3307. return true;
  3308. if (htab->params->power10_stubs == -1)
  3309. {
  3310. /* For --power10-stubs=auto, don't merge _notoc and other
  3311. varieties of stubs. */
  3312. struct ppc_stub_hash_entry *alt_stub;
  3313. alt_stub = select_alt_stub (stub_entry, r_type);
  3314. if (alt_stub == NULL)
  3315. {
  3316. alt_stub = ((struct ppc_stub_hash_entry *)
  3317. stub_hash_newfunc (NULL,
  3318. &htab->stub_hash_table,
  3319. stub_entry->root.string));
  3320. if (alt_stub == NULL)
  3321. return false;
  3322. *alt_stub = *stub_entry;
  3323. stub_entry->root.next = &alt_stub->root;
  3324. /* Sort notoc stubs first, then toc stubs, then p9notoc.
  3325. Not that it matters, this just puts smaller stubs first. */
  3326. if (stub_type.sub == ppc_stub_notoc)
  3327. alt_stub = stub_entry;
  3328. else if (stub_type.sub == ppc_stub_p9notoc
  3329. && alt_stub->root.next
  3330. && alt_stub->root.next->string == alt_stub->root.string)
  3331. {
  3332. struct ppc_stub_hash_entry *next
  3333. = (struct ppc_stub_hash_entry *) alt_stub->root.next;
  3334. alt_stub->type = next->type;
  3335. alt_stub = next;
  3336. }
  3337. alt_stub->type = stub_type;
  3338. return true;
  3339. }
  3340. stub_entry = alt_stub;
  3341. }
  3342. old_type = stub_entry->type;
  3343. if (old_type.main == ppc_stub_plt_branch)
  3344. old_type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
  3345. if (old_type.main != stub_type.main
  3346. || (old_type.sub != stub_type.sub
  3347. && old_type.sub != ppc_stub_toc
  3348. && stub_type.sub != ppc_stub_toc))
  3349. abort ();
  3350. stub_entry->type.sub |= stub_type.sub;
  3351. stub_entry->type.r2save |= stub_type.r2save;
  3352. return true;
  3353. }
  3354. /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
  3355. not already done. */
  3356. static bool
  3357. create_got_section (bfd *abfd, struct bfd_link_info *info)
  3358. {
  3359. asection *got, *relgot;
  3360. flagword flags;
  3361. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  3362. if (!is_ppc64_elf (abfd))
  3363. return false;
  3364. if (htab == NULL)
  3365. return false;
  3366. if (!htab->elf.sgot
  3367. && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
  3368. return false;
  3369. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  3370. | SEC_LINKER_CREATED);
  3371. got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  3372. if (!got
  3373. || !bfd_set_section_alignment (got, 3))
  3374. return false;
  3375. relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
  3376. flags | SEC_READONLY);
  3377. if (!relgot
  3378. || !bfd_set_section_alignment (relgot, 3))
  3379. return false;
  3380. ppc64_elf_tdata (abfd)->got = got;
  3381. ppc64_elf_tdata (abfd)->relgot = relgot;
  3382. return true;
  3383. }
  3384. /* Follow indirect and warning symbol links. */
  3385. static inline struct bfd_link_hash_entry *
  3386. follow_link (struct bfd_link_hash_entry *h)
  3387. {
  3388. while (h->type == bfd_link_hash_indirect
  3389. || h->type == bfd_link_hash_warning)
  3390. h = h->u.i.link;
  3391. return h;
  3392. }
  3393. static inline struct elf_link_hash_entry *
  3394. elf_follow_link (struct elf_link_hash_entry *h)
  3395. {
  3396. return (struct elf_link_hash_entry *) follow_link (&h->root);
  3397. }
  3398. static inline struct ppc_link_hash_entry *
  3399. ppc_follow_link (struct ppc_link_hash_entry *h)
  3400. {
  3401. return ppc_elf_hash_entry (elf_follow_link (&h->elf));
  3402. }
  3403. /* Merge PLT info on FROM with that on TO. */
  3404. static void
  3405. move_plt_plist (struct ppc_link_hash_entry *from,
  3406. struct ppc_link_hash_entry *to)
  3407. {
  3408. if (from->elf.plt.plist != NULL)
  3409. {
  3410. if (to->elf.plt.plist != NULL)
  3411. {
  3412. struct plt_entry **entp;
  3413. struct plt_entry *ent;
  3414. for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
  3415. {
  3416. struct plt_entry *dent;
  3417. for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
  3418. if (dent->addend == ent->addend)
  3419. {
  3420. dent->plt.refcount += ent->plt.refcount;
  3421. *entp = ent->next;
  3422. break;
  3423. }
  3424. if (dent == NULL)
  3425. entp = &ent->next;
  3426. }
  3427. *entp = to->elf.plt.plist;
  3428. }
  3429. to->elf.plt.plist = from->elf.plt.plist;
  3430. from->elf.plt.plist = NULL;
  3431. }
  3432. }
  3433. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  3434. static void
  3435. ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
  3436. struct elf_link_hash_entry *dir,
  3437. struct elf_link_hash_entry *ind)
  3438. {
  3439. struct ppc_link_hash_entry *edir, *eind;
  3440. edir = ppc_elf_hash_entry (dir);
  3441. eind = ppc_elf_hash_entry (ind);
  3442. edir->is_func |= eind->is_func;
  3443. edir->is_func_descriptor |= eind->is_func_descriptor;
  3444. edir->tls_mask |= eind->tls_mask;
  3445. if (eind->oh != NULL)
  3446. edir->oh = ppc_follow_link (eind->oh);
  3447. if (edir->elf.versioned != versioned_hidden)
  3448. edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
  3449. edir->elf.ref_regular |= eind->elf.ref_regular;
  3450. edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
  3451. edir->elf.non_got_ref |= eind->elf.non_got_ref;
  3452. edir->elf.needs_plt |= eind->elf.needs_plt;
  3453. edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
  3454. /* If we were called to copy over info for a weak sym, don't copy
  3455. dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
  3456. in order to simplify readonly_dynrelocs and save a field in the
  3457. symbol hash entry, but that means dyn_relocs can't be used in any
  3458. tests about a specific symbol, or affect other symbol flags which
  3459. are then tested. */
  3460. if (eind->elf.root.type != bfd_link_hash_indirect)
  3461. return;
  3462. /* Copy over any dynamic relocs we may have on the indirect sym. */
  3463. if (ind->dyn_relocs != NULL)
  3464. {
  3465. if (dir->dyn_relocs != NULL)
  3466. {
  3467. struct ppc_dyn_relocs **pp;
  3468. struct ppc_dyn_relocs *p;
  3469. /* Add reloc counts against the indirect sym to the direct sym
  3470. list. Merge any entries against the same section. */
  3471. for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
  3472. (p = *pp) != NULL;
  3473. )
  3474. {
  3475. struct ppc_dyn_relocs *q;
  3476. for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
  3477. q != NULL;
  3478. q = q->next)
  3479. if (q->sec == p->sec)
  3480. {
  3481. q->count += p->count;
  3482. q->pc_count += p->pc_count;
  3483. q->rel_count += p->rel_count;
  3484. *pp = p->next;
  3485. break;
  3486. }
  3487. if (q == NULL)
  3488. pp = &p->next;
  3489. }
  3490. *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
  3491. }
  3492. dir->dyn_relocs = ind->dyn_relocs;
  3493. ind->dyn_relocs = NULL;
  3494. }
  3495. /* Copy over got entries that we may have already seen to the
  3496. symbol which just became indirect. */
  3497. if (eind->elf.got.glist != NULL)
  3498. {
  3499. if (edir->elf.got.glist != NULL)
  3500. {
  3501. struct got_entry **entp;
  3502. struct got_entry *ent;
  3503. for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
  3504. {
  3505. struct got_entry *dent;
  3506. for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
  3507. if (dent->addend == ent->addend
  3508. && dent->owner == ent->owner
  3509. && dent->tls_type == ent->tls_type)
  3510. {
  3511. dent->got.refcount += ent->got.refcount;
  3512. *entp = ent->next;
  3513. break;
  3514. }
  3515. if (dent == NULL)
  3516. entp = &ent->next;
  3517. }
  3518. *entp = edir->elf.got.glist;
  3519. }
  3520. edir->elf.got.glist = eind->elf.got.glist;
  3521. eind->elf.got.glist = NULL;
  3522. }
  3523. /* And plt entries. */
  3524. move_plt_plist (eind, edir);
  3525. if (eind->elf.dynindx != -1)
  3526. {
  3527. if (edir->elf.dynindx != -1)
  3528. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  3529. edir->elf.dynstr_index);
  3530. edir->elf.dynindx = eind->elf.dynindx;
  3531. edir->elf.dynstr_index = eind->elf.dynstr_index;
  3532. eind->elf.dynindx = -1;
  3533. eind->elf.dynstr_index = 0;
  3534. }
  3535. }
  3536. /* Find the function descriptor hash entry from the given function code
  3537. hash entry FH. Link the entries via their OH fields. */
  3538. static struct ppc_link_hash_entry *
  3539. lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
  3540. {
  3541. struct ppc_link_hash_entry *fdh = fh->oh;
  3542. if (fdh == NULL)
  3543. {
  3544. const char *fd_name = fh->elf.root.root.string + 1;
  3545. fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
  3546. false, false, false));
  3547. if (fdh == NULL)
  3548. return fdh;
  3549. fdh->is_func_descriptor = 1;
  3550. fdh->oh = fh;
  3551. fh->is_func = 1;
  3552. fh->oh = fdh;
  3553. }
  3554. fdh = ppc_follow_link (fdh);
  3555. fdh->is_func_descriptor = 1;
  3556. fdh->oh = fh;
  3557. return fdh;
  3558. }
  3559. /* Make a fake function descriptor sym for the undefined code sym FH. */
  3560. static struct ppc_link_hash_entry *
  3561. make_fdh (struct bfd_link_info *info,
  3562. struct ppc_link_hash_entry *fh)
  3563. {
  3564. bfd *abfd = fh->elf.root.u.undef.abfd;
  3565. struct bfd_link_hash_entry *bh = NULL;
  3566. struct ppc_link_hash_entry *fdh;
  3567. flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
  3568. ? BSF_WEAK
  3569. : BSF_GLOBAL);
  3570. if (!_bfd_generic_link_add_one_symbol (info, abfd,
  3571. fh->elf.root.root.string + 1,
  3572. flags, bfd_und_section_ptr, 0,
  3573. NULL, false, false, &bh))
  3574. return NULL;
  3575. fdh = (struct ppc_link_hash_entry *) bh;
  3576. fdh->elf.non_elf = 0;
  3577. fdh->fake = 1;
  3578. fdh->is_func_descriptor = 1;
  3579. fdh->oh = fh;
  3580. fh->is_func = 1;
  3581. fh->oh = fdh;
  3582. return fdh;
  3583. }
  3584. /* Fix function descriptor symbols defined in .opd sections to be
  3585. function type. */
  3586. static bool
  3587. ppc64_elf_add_symbol_hook (bfd *ibfd,
  3588. struct bfd_link_info *info,
  3589. Elf_Internal_Sym *isym,
  3590. const char **name,
  3591. flagword *flags ATTRIBUTE_UNUSED,
  3592. asection **sec,
  3593. bfd_vma *value)
  3594. {
  3595. if (*sec != NULL
  3596. && strcmp ((*sec)->name, ".opd") == 0)
  3597. {
  3598. asection *code_sec;
  3599. if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
  3600. || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
  3601. isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
  3602. /* If the symbol is a function defined in .opd, and the function
  3603. code is in a discarded group, let it appear to be undefined. */
  3604. if (!bfd_link_relocatable (info)
  3605. && (*sec)->reloc_count != 0
  3606. && opd_entry_value (*sec, *value, &code_sec, NULL,
  3607. false) != (bfd_vma) -1
  3608. && discarded_section (code_sec))
  3609. {
  3610. *sec = bfd_und_section_ptr;
  3611. isym->st_shndx = SHN_UNDEF;
  3612. }
  3613. }
  3614. else if (*sec != NULL
  3615. && strcmp ((*sec)->name, ".toc") == 0
  3616. && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
  3617. {
  3618. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  3619. if (htab != NULL)
  3620. htab->params->object_in_toc = 1;
  3621. }
  3622. if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
  3623. {
  3624. if (abiversion (ibfd) == 0)
  3625. set_abiversion (ibfd, 2);
  3626. else if (abiversion (ibfd) == 1)
  3627. {
  3628. _bfd_error_handler (_("symbol '%s' has invalid st_other"
  3629. " for ABI version 1"), *name);
  3630. bfd_set_error (bfd_error_bad_value);
  3631. return false;
  3632. }
  3633. }
  3634. return true;
  3635. }
  3636. /* Merge non-visibility st_other attributes: local entry point. */
  3637. static void
  3638. ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
  3639. unsigned int st_other,
  3640. bool definition,
  3641. bool dynamic)
  3642. {
  3643. if (definition && (!dynamic || !h->def_regular))
  3644. h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
  3645. | ELF_ST_VISIBILITY (h->other));
  3646. }
  3647. /* Hook called on merging a symbol. We use this to clear "fake" since
  3648. we now have a real symbol. */
  3649. static bool
  3650. ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
  3651. const Elf_Internal_Sym *isym,
  3652. asection **psec ATTRIBUTE_UNUSED,
  3653. bool newdef ATTRIBUTE_UNUSED,
  3654. bool olddef ATTRIBUTE_UNUSED,
  3655. bfd *oldbfd ATTRIBUTE_UNUSED,
  3656. const asection *oldsec ATTRIBUTE_UNUSED)
  3657. {
  3658. ppc_elf_hash_entry (h)->fake = 0;
  3659. if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
  3660. ppc_elf_hash_entry (h)->non_zero_localentry = 1;
  3661. return true;
  3662. }
  3663. /* This function makes an old ABI object reference to ".bar" cause the
  3664. inclusion of a new ABI object archive that defines "bar".
  3665. NAME is a symbol defined in an archive. Return a symbol in the hash
  3666. table that might be satisfied by the archive symbols. */
  3667. static struct bfd_link_hash_entry *
  3668. ppc64_elf_archive_symbol_lookup (bfd *abfd,
  3669. struct bfd_link_info *info,
  3670. const char *name)
  3671. {
  3672. struct bfd_link_hash_entry *h;
  3673. char *dot_name;
  3674. size_t len;
  3675. h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
  3676. if (h != NULL
  3677. && ppc_hash_table (info) != NULL
  3678. /* Don't return this sym if it is a fake function descriptor
  3679. created by add_symbol_adjust. */
  3680. && !((struct ppc_link_hash_entry *) h)->fake)
  3681. return h;
  3682. if (name[0] == '.')
  3683. return h;
  3684. len = strlen (name);
  3685. dot_name = bfd_alloc (abfd, len + 2);
  3686. if (dot_name == NULL)
  3687. return (struct bfd_link_hash_entry *) -1;
  3688. dot_name[0] = '.';
  3689. memcpy (dot_name + 1, name, len + 1);
  3690. h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
  3691. bfd_release (abfd, dot_name);
  3692. if (h != NULL)
  3693. return h;
  3694. if (strcmp (name, "__tls_get_addr_opt") == 0)
  3695. h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
  3696. return h;
  3697. }
  3698. /* This function satisfies all old ABI object references to ".bar" if a
  3699. new ABI object defines "bar". Well, at least, undefined dot symbols
  3700. are made weak. This stops later archive searches from including an
  3701. object if we already have a function descriptor definition. It also
  3702. prevents the linker complaining about undefined symbols.
  3703. We also check and correct mismatched symbol visibility here. The
  3704. most restrictive visibility of the function descriptor and the
  3705. function entry symbol is used. */
  3706. static bool
  3707. add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
  3708. {
  3709. struct ppc_link_hash_table *htab;
  3710. struct ppc_link_hash_entry *fdh;
  3711. if (eh->elf.root.type == bfd_link_hash_warning)
  3712. eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
  3713. if (eh->elf.root.type == bfd_link_hash_indirect)
  3714. return true;
  3715. if (eh->elf.root.root.string[0] != '.')
  3716. abort ();
  3717. htab = ppc_hash_table (info);
  3718. if (htab == NULL)
  3719. return false;
  3720. fdh = lookup_fdh (eh, htab);
  3721. if (fdh == NULL
  3722. && !bfd_link_relocatable (info)
  3723. && (eh->elf.root.type == bfd_link_hash_undefined
  3724. || eh->elf.root.type == bfd_link_hash_undefweak)
  3725. && eh->elf.ref_regular)
  3726. {
  3727. /* Make an undefined function descriptor sym, in order to
  3728. pull in an --as-needed shared lib. Archives are handled
  3729. elsewhere. */
  3730. fdh = make_fdh (info, eh);
  3731. if (fdh == NULL)
  3732. return false;
  3733. }
  3734. if (fdh != NULL)
  3735. {
  3736. unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
  3737. unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
  3738. /* Make both descriptor and entry symbol have the most
  3739. constraining visibility of either symbol. */
  3740. if (entry_vis < descr_vis)
  3741. fdh->elf.other += entry_vis - descr_vis;
  3742. else if (entry_vis > descr_vis)
  3743. eh->elf.other += descr_vis - entry_vis;
  3744. /* Propagate reference flags from entry symbol to function
  3745. descriptor symbol. */
  3746. fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
  3747. fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
  3748. fdh->elf.ref_regular |= eh->elf.ref_regular;
  3749. fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
  3750. if (!fdh->elf.forced_local
  3751. && fdh->elf.dynindx == -1
  3752. && fdh->elf.versioned != versioned_hidden
  3753. && (bfd_link_dll (info)
  3754. || fdh->elf.def_dynamic
  3755. || fdh->elf.ref_dynamic)
  3756. && (eh->elf.ref_regular
  3757. || eh->elf.def_regular))
  3758. {
  3759. if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
  3760. return false;
  3761. }
  3762. }
  3763. return true;
  3764. }
  3765. /* Set up opd section info and abiversion for IBFD, and process list
  3766. of dot-symbols we made in link_hash_newfunc. */
  3767. static bool
  3768. ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
  3769. {
  3770. struct ppc_link_hash_table *htab;
  3771. struct ppc_link_hash_entry **p, *eh;
  3772. asection *opd = bfd_get_section_by_name (ibfd, ".opd");
  3773. if (opd != NULL && opd->size != 0)
  3774. {
  3775. BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
  3776. ppc64_elf_section_data (opd)->sec_type = sec_opd;
  3777. if (abiversion (ibfd) == 0)
  3778. set_abiversion (ibfd, 1);
  3779. else if (abiversion (ibfd) >= 2)
  3780. {
  3781. /* xgettext:c-format */
  3782. _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
  3783. ibfd, abiversion (ibfd));
  3784. bfd_set_error (bfd_error_bad_value);
  3785. return false;
  3786. }
  3787. }
  3788. if (is_ppc64_elf (info->output_bfd))
  3789. {
  3790. /* For input files without an explicit abiversion in e_flags
  3791. we should have flagged any with symbol st_other bits set
  3792. as ELFv1 and above flagged those with .opd as ELFv2.
  3793. Set the output abiversion if not yet set, and for any input
  3794. still ambiguous, take its abiversion from the output.
  3795. Differences in ABI are reported later. */
  3796. if (abiversion (info->output_bfd) == 0)
  3797. set_abiversion (info->output_bfd, abiversion (ibfd));
  3798. else if (abiversion (ibfd) == 0)
  3799. set_abiversion (ibfd, abiversion (info->output_bfd));
  3800. }
  3801. htab = ppc_hash_table (info);
  3802. if (htab == NULL)
  3803. return true;
  3804. if (opd != NULL && opd->size != 0
  3805. && (ibfd->flags & DYNAMIC) == 0
  3806. && (opd->flags & SEC_RELOC) != 0
  3807. && opd->reloc_count != 0
  3808. && !bfd_is_abs_section (opd->output_section)
  3809. && info->gc_sections)
  3810. {
  3811. /* Garbage collection needs some extra help with .opd sections.
  3812. We don't want to necessarily keep everything referenced by
  3813. relocs in .opd, as that would keep all functions. Instead,
  3814. if we reference an .opd symbol (a function descriptor), we
  3815. want to keep the function code symbol's section. This is
  3816. easy for global symbols, but for local syms we need to keep
  3817. information about the associated function section. */
  3818. bfd_size_type amt;
  3819. asection **opd_sym_map;
  3820. Elf_Internal_Shdr *symtab_hdr;
  3821. Elf_Internal_Rela *relocs, *rel_end, *rel;
  3822. amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
  3823. opd_sym_map = bfd_zalloc (ibfd, amt);
  3824. if (opd_sym_map == NULL)
  3825. return false;
  3826. ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
  3827. relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
  3828. info->keep_memory);
  3829. if (relocs == NULL)
  3830. return false;
  3831. symtab_hdr = &elf_symtab_hdr (ibfd);
  3832. rel_end = relocs + opd->reloc_count - 1;
  3833. for (rel = relocs; rel < rel_end; rel++)
  3834. {
  3835. enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
  3836. unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
  3837. if (r_type == R_PPC64_ADDR64
  3838. && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
  3839. && r_symndx < symtab_hdr->sh_info)
  3840. {
  3841. Elf_Internal_Sym *isym;
  3842. asection *s;
  3843. isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
  3844. r_symndx);
  3845. if (isym == NULL)
  3846. {
  3847. if (elf_section_data (opd)->relocs != relocs)
  3848. free (relocs);
  3849. return false;
  3850. }
  3851. s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
  3852. if (s != NULL && s != opd)
  3853. opd_sym_map[OPD_NDX (rel->r_offset)] = s;
  3854. }
  3855. }
  3856. if (elf_section_data (opd)->relocs != relocs)
  3857. free (relocs);
  3858. }
  3859. p = &htab->dot_syms;
  3860. while ((eh = *p) != NULL)
  3861. {
  3862. *p = NULL;
  3863. if (&eh->elf == htab->elf.hgot)
  3864. ;
  3865. else if (htab->elf.hgot == NULL
  3866. && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
  3867. htab->elf.hgot = &eh->elf;
  3868. else if (abiversion (ibfd) <= 1)
  3869. {
  3870. htab->need_func_desc_adj = 1;
  3871. if (!add_symbol_adjust (eh, info))
  3872. return false;
  3873. }
  3874. p = &eh->u.next_dot_sym;
  3875. }
  3876. return true;
  3877. }
  3878. /* Undo hash table changes when an --as-needed input file is determined
  3879. not to be needed. */
  3880. static bool
  3881. ppc64_elf_notice_as_needed (bfd *ibfd,
  3882. struct bfd_link_info *info,
  3883. enum notice_asneeded_action act)
  3884. {
  3885. if (act == notice_not_needed)
  3886. {
  3887. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  3888. if (htab == NULL)
  3889. return false;
  3890. htab->dot_syms = NULL;
  3891. }
  3892. return _bfd_elf_notice_as_needed (ibfd, info, act);
  3893. }
  3894. /* If --just-symbols against a final linked binary, then assume we need
  3895. toc adjusting stubs when calling functions defined there. */
  3896. static void
  3897. ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
  3898. {
  3899. if ((sec->flags & SEC_CODE) != 0
  3900. && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
  3901. && is_ppc64_elf (sec->owner))
  3902. {
  3903. if (abiversion (sec->owner) >= 2
  3904. || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
  3905. sec->has_toc_reloc = 1;
  3906. }
  3907. _bfd_elf_link_just_syms (sec, info);
  3908. }
  3909. static struct plt_entry **
  3910. update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
  3911. unsigned long r_symndx, bfd_vma r_addend, int tls_type)
  3912. {
  3913. struct got_entry **local_got_ents = elf_local_got_ents (abfd);
  3914. struct plt_entry **local_plt;
  3915. unsigned char *local_got_tls_masks;
  3916. if (local_got_ents == NULL)
  3917. {
  3918. bfd_size_type size = symtab_hdr->sh_info;
  3919. size *= (sizeof (*local_got_ents)
  3920. + sizeof (*local_plt)
  3921. + sizeof (*local_got_tls_masks));
  3922. local_got_ents = bfd_zalloc (abfd, size);
  3923. if (local_got_ents == NULL)
  3924. return NULL;
  3925. elf_local_got_ents (abfd) = local_got_ents;
  3926. }
  3927. if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
  3928. {
  3929. struct got_entry *ent;
  3930. for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
  3931. if (ent->addend == r_addend
  3932. && ent->owner == abfd
  3933. && ent->tls_type == tls_type)
  3934. break;
  3935. if (ent == NULL)
  3936. {
  3937. size_t amt = sizeof (*ent);
  3938. ent = bfd_alloc (abfd, amt);
  3939. if (ent == NULL)
  3940. return false;
  3941. ent->next = local_got_ents[r_symndx];
  3942. ent->addend = r_addend;
  3943. ent->owner = abfd;
  3944. ent->tls_type = tls_type;
  3945. ent->is_indirect = false;
  3946. ent->got.refcount = 0;
  3947. local_got_ents[r_symndx] = ent;
  3948. }
  3949. ent->got.refcount += 1;
  3950. }
  3951. local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
  3952. local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
  3953. local_got_tls_masks[r_symndx] |= tls_type & 0xff;
  3954. return local_plt + r_symndx;
  3955. }
  3956. static bool
  3957. update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
  3958. {
  3959. struct plt_entry *ent;
  3960. for (ent = *plist; ent != NULL; ent = ent->next)
  3961. if (ent->addend == addend)
  3962. break;
  3963. if (ent == NULL)
  3964. {
  3965. size_t amt = sizeof (*ent);
  3966. ent = bfd_alloc (abfd, amt);
  3967. if (ent == NULL)
  3968. return false;
  3969. ent->next = *plist;
  3970. ent->addend = addend;
  3971. ent->plt.refcount = 0;
  3972. *plist = ent;
  3973. }
  3974. ent->plt.refcount += 1;
  3975. return true;
  3976. }
  3977. static bool
  3978. is_branch_reloc (enum elf_ppc64_reloc_type r_type)
  3979. {
  3980. return (r_type == R_PPC64_REL24
  3981. || r_type == R_PPC64_REL24_NOTOC
  3982. || r_type == R_PPC64_REL24_P9NOTOC
  3983. || r_type == R_PPC64_REL14
  3984. || r_type == R_PPC64_REL14_BRTAKEN
  3985. || r_type == R_PPC64_REL14_BRNTAKEN
  3986. || r_type == R_PPC64_ADDR24
  3987. || r_type == R_PPC64_ADDR14
  3988. || r_type == R_PPC64_ADDR14_BRTAKEN
  3989. || r_type == R_PPC64_ADDR14_BRNTAKEN
  3990. || r_type == R_PPC64_PLTCALL
  3991. || r_type == R_PPC64_PLTCALL_NOTOC);
  3992. }
  3993. /* Relocs on inline plt call sequence insns prior to the call. */
  3994. static bool
  3995. is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
  3996. {
  3997. return (r_type == R_PPC64_PLT16_HA
  3998. || r_type == R_PPC64_PLT16_HI
  3999. || r_type == R_PPC64_PLT16_LO
  4000. || r_type == R_PPC64_PLT16_LO_DS
  4001. || r_type == R_PPC64_PLT_PCREL34
  4002. || r_type == R_PPC64_PLT_PCREL34_NOTOC
  4003. || r_type == R_PPC64_PLTSEQ
  4004. || r_type == R_PPC64_PLTSEQ_NOTOC);
  4005. }
  4006. /* Of relocs which might appear paired with TLSGD and TLSLD marker
  4007. relocs, return true for those that operate on a dword. */
  4008. static bool
  4009. is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
  4010. {
  4011. return (r_type == R_PPC64_PLT_PCREL34
  4012. || r_type == R_PPC64_PLT_PCREL34_NOTOC
  4013. || r_type == R_PPC64_PLTCALL);
  4014. }
  4015. /* Like bfd_reloc_offset_in_range but without a howto. Return true
  4016. iff a field of SIZE bytes at OFFSET is within SEC limits. */
  4017. static bool
  4018. offset_in_range (asection *sec, bfd_vma offset, size_t size)
  4019. {
  4020. return offset <= sec->size && size <= sec->size - offset;
  4021. }
  4022. /* Look through the relocs for a section during the first phase, and
  4023. calculate needed space in the global offset table, procedure
  4024. linkage table, and dynamic reloc sections. */
  4025. static bool
  4026. ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
  4027. asection *sec, const Elf_Internal_Rela *relocs)
  4028. {
  4029. struct ppc_link_hash_table *htab;
  4030. Elf_Internal_Shdr *symtab_hdr;
  4031. struct elf_link_hash_entry **sym_hashes;
  4032. const Elf_Internal_Rela *rel;
  4033. const Elf_Internal_Rela *rel_end;
  4034. asection *sreloc;
  4035. struct elf_link_hash_entry *tga, *dottga;
  4036. bool is_opd;
  4037. if (bfd_link_relocatable (info))
  4038. return true;
  4039. BFD_ASSERT (is_ppc64_elf (abfd));
  4040. htab = ppc_hash_table (info);
  4041. if (htab == NULL)
  4042. return false;
  4043. tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
  4044. false, false, true);
  4045. dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
  4046. false, false, true);
  4047. symtab_hdr = &elf_symtab_hdr (abfd);
  4048. sym_hashes = elf_sym_hashes (abfd);
  4049. sreloc = NULL;
  4050. is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
  4051. rel_end = relocs + sec->reloc_count;
  4052. for (rel = relocs; rel < rel_end; rel++)
  4053. {
  4054. unsigned long r_symndx;
  4055. struct elf_link_hash_entry *h;
  4056. Elf_Internal_Sym *isym;
  4057. enum elf_ppc64_reloc_type r_type;
  4058. int tls_type;
  4059. struct _ppc64_elf_section_data *ppc64_sec;
  4060. struct plt_entry **ifunc, **plt_list;
  4061. r_symndx = ELF64_R_SYM (rel->r_info);
  4062. if (r_symndx < symtab_hdr->sh_info)
  4063. {
  4064. h = NULL;
  4065. isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
  4066. if (isym == NULL)
  4067. return false;
  4068. }
  4069. else
  4070. {
  4071. isym = NULL;
  4072. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  4073. h = elf_follow_link (h);
  4074. if (h == htab->elf.hgot)
  4075. sec->has_toc_reloc = 1;
  4076. }
  4077. r_type = ELF64_R_TYPE (rel->r_info);
  4078. switch (r_type)
  4079. {
  4080. case R_PPC64_D34:
  4081. case R_PPC64_D34_LO:
  4082. case R_PPC64_D34_HI30:
  4083. case R_PPC64_D34_HA30:
  4084. case R_PPC64_D28:
  4085. case R_PPC64_TPREL34:
  4086. case R_PPC64_DTPREL34:
  4087. case R_PPC64_PCREL34:
  4088. case R_PPC64_GOT_PCREL34:
  4089. case R_PPC64_GOT_TLSGD_PCREL34:
  4090. case R_PPC64_GOT_TLSLD_PCREL34:
  4091. case R_PPC64_GOT_TPREL_PCREL34:
  4092. case R_PPC64_GOT_DTPREL_PCREL34:
  4093. case R_PPC64_PLT_PCREL34:
  4094. case R_PPC64_PLT_PCREL34_NOTOC:
  4095. case R_PPC64_PCREL28:
  4096. htab->has_power10_relocs = 1;
  4097. break;
  4098. default:
  4099. break;
  4100. }
  4101. switch (r_type)
  4102. {
  4103. case R_PPC64_PLT16_HA:
  4104. case R_PPC64_GOT_TLSLD16_HA:
  4105. case R_PPC64_GOT_TLSGD16_HA:
  4106. case R_PPC64_GOT_TPREL16_HA:
  4107. case R_PPC64_GOT_DTPREL16_HA:
  4108. case R_PPC64_GOT16_HA:
  4109. case R_PPC64_TOC16_HA:
  4110. case R_PPC64_PLT16_LO:
  4111. case R_PPC64_PLT16_LO_DS:
  4112. case R_PPC64_GOT_TLSLD16_LO:
  4113. case R_PPC64_GOT_TLSGD16_LO:
  4114. case R_PPC64_GOT_TPREL16_LO_DS:
  4115. case R_PPC64_GOT_DTPREL16_LO_DS:
  4116. case R_PPC64_GOT16_LO:
  4117. case R_PPC64_GOT16_LO_DS:
  4118. case R_PPC64_TOC16_LO:
  4119. case R_PPC64_TOC16_LO_DS:
  4120. case R_PPC64_GOT_PCREL34:
  4121. ppc64_elf_tdata (abfd)->has_optrel = 1;
  4122. ppc64_elf_section_data (sec)->has_optrel = 1;
  4123. break;
  4124. default:
  4125. break;
  4126. }
  4127. ifunc = NULL;
  4128. if (h != NULL)
  4129. {
  4130. if (h->type == STT_GNU_IFUNC)
  4131. {
  4132. h->needs_plt = 1;
  4133. ifunc = &h->plt.plist;
  4134. }
  4135. }
  4136. else
  4137. {
  4138. if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  4139. {
  4140. ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
  4141. rel->r_addend,
  4142. NON_GOT | PLT_IFUNC);
  4143. if (ifunc == NULL)
  4144. return false;
  4145. }
  4146. }
  4147. tls_type = 0;
  4148. switch (r_type)
  4149. {
  4150. case R_PPC64_TLSGD:
  4151. case R_PPC64_TLSLD:
  4152. /* These special tls relocs tie a call to __tls_get_addr with
  4153. its parameter symbol. */
  4154. if (h != NULL)
  4155. ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
  4156. else
  4157. if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
  4158. rel->r_addend,
  4159. NON_GOT | TLS_TLS | TLS_MARK))
  4160. return false;
  4161. sec->has_tls_reloc = 1;
  4162. break;
  4163. case R_PPC64_GOT_TLSLD16:
  4164. case R_PPC64_GOT_TLSLD16_LO:
  4165. case R_PPC64_GOT_TLSLD16_HI:
  4166. case R_PPC64_GOT_TLSLD16_HA:
  4167. case R_PPC64_GOT_TLSLD_PCREL34:
  4168. tls_type = TLS_TLS | TLS_LD;
  4169. goto dogottls;
  4170. case R_PPC64_GOT_TLSGD16:
  4171. case R_PPC64_GOT_TLSGD16_LO:
  4172. case R_PPC64_GOT_TLSGD16_HI:
  4173. case R_PPC64_GOT_TLSGD16_HA:
  4174. case R_PPC64_GOT_TLSGD_PCREL34:
  4175. tls_type = TLS_TLS | TLS_GD;
  4176. goto dogottls;
  4177. case R_PPC64_GOT_TPREL16_DS:
  4178. case R_PPC64_GOT_TPREL16_LO_DS:
  4179. case R_PPC64_GOT_TPREL16_HI:
  4180. case R_PPC64_GOT_TPREL16_HA:
  4181. case R_PPC64_GOT_TPREL_PCREL34:
  4182. if (bfd_link_dll (info))
  4183. info->flags |= DF_STATIC_TLS;
  4184. tls_type = TLS_TLS | TLS_TPREL;
  4185. goto dogottls;
  4186. case R_PPC64_GOT_DTPREL16_DS:
  4187. case R_PPC64_GOT_DTPREL16_LO_DS:
  4188. case R_PPC64_GOT_DTPREL16_HI:
  4189. case R_PPC64_GOT_DTPREL16_HA:
  4190. case R_PPC64_GOT_DTPREL_PCREL34:
  4191. tls_type = TLS_TLS | TLS_DTPREL;
  4192. dogottls:
  4193. sec->has_tls_reloc = 1;
  4194. goto dogot;
  4195. case R_PPC64_GOT16:
  4196. case R_PPC64_GOT16_LO:
  4197. case R_PPC64_GOT16_HI:
  4198. case R_PPC64_GOT16_HA:
  4199. case R_PPC64_GOT16_DS:
  4200. case R_PPC64_GOT16_LO_DS:
  4201. case R_PPC64_GOT_PCREL34:
  4202. dogot:
  4203. /* This symbol requires a global offset table entry. */
  4204. sec->has_toc_reloc = 1;
  4205. if (r_type == R_PPC64_GOT_TLSLD16
  4206. || r_type == R_PPC64_GOT_TLSGD16
  4207. || r_type == R_PPC64_GOT_TPREL16_DS
  4208. || r_type == R_PPC64_GOT_DTPREL16_DS
  4209. || r_type == R_PPC64_GOT16
  4210. || r_type == R_PPC64_GOT16_DS)
  4211. {
  4212. htab->do_multi_toc = 1;
  4213. ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
  4214. }
  4215. if (ppc64_elf_tdata (abfd)->got == NULL
  4216. && !create_got_section (abfd, info))
  4217. return false;
  4218. if (h != NULL)
  4219. {
  4220. struct ppc_link_hash_entry *eh;
  4221. struct got_entry *ent;
  4222. eh = ppc_elf_hash_entry (h);
  4223. for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
  4224. if (ent->addend == rel->r_addend
  4225. && ent->owner == abfd
  4226. && ent->tls_type == tls_type)
  4227. break;
  4228. if (ent == NULL)
  4229. {
  4230. size_t amt = sizeof (*ent);
  4231. ent = bfd_alloc (abfd, amt);
  4232. if (ent == NULL)
  4233. return false;
  4234. ent->next = eh->elf.got.glist;
  4235. ent->addend = rel->r_addend;
  4236. ent->owner = abfd;
  4237. ent->tls_type = tls_type;
  4238. ent->is_indirect = false;
  4239. ent->got.refcount = 0;
  4240. eh->elf.got.glist = ent;
  4241. }
  4242. ent->got.refcount += 1;
  4243. eh->tls_mask |= tls_type;
  4244. }
  4245. else
  4246. /* This is a global offset table entry for a local symbol. */
  4247. if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
  4248. rel->r_addend, tls_type))
  4249. return false;
  4250. break;
  4251. case R_PPC64_PLT16_HA:
  4252. case R_PPC64_PLT16_HI:
  4253. case R_PPC64_PLT16_LO:
  4254. case R_PPC64_PLT16_LO_DS:
  4255. case R_PPC64_PLT_PCREL34:
  4256. case R_PPC64_PLT_PCREL34_NOTOC:
  4257. case R_PPC64_PLT32:
  4258. case R_PPC64_PLT64:
  4259. /* This symbol requires a procedure linkage table entry. */
  4260. plt_list = ifunc;
  4261. if (h != NULL)
  4262. {
  4263. h->needs_plt = 1;
  4264. if (h->root.root.string[0] == '.'
  4265. && h->root.root.string[1] != '\0')
  4266. ppc_elf_hash_entry (h)->is_func = 1;
  4267. ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
  4268. plt_list = &h->plt.plist;
  4269. }
  4270. if (plt_list == NULL)
  4271. plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
  4272. rel->r_addend,
  4273. NON_GOT | PLT_KEEP);
  4274. if (!update_plt_info (abfd, plt_list, rel->r_addend))
  4275. return false;
  4276. break;
  4277. /* The following relocations don't need to propagate the
  4278. relocation if linking a shared object since they are
  4279. section relative. */
  4280. case R_PPC64_SECTOFF:
  4281. case R_PPC64_SECTOFF_LO:
  4282. case R_PPC64_SECTOFF_HI:
  4283. case R_PPC64_SECTOFF_HA:
  4284. case R_PPC64_SECTOFF_DS:
  4285. case R_PPC64_SECTOFF_LO_DS:
  4286. case R_PPC64_DTPREL16:
  4287. case R_PPC64_DTPREL16_LO:
  4288. case R_PPC64_DTPREL16_HI:
  4289. case R_PPC64_DTPREL16_HA:
  4290. case R_PPC64_DTPREL16_DS:
  4291. case R_PPC64_DTPREL16_LO_DS:
  4292. case R_PPC64_DTPREL16_HIGH:
  4293. case R_PPC64_DTPREL16_HIGHA:
  4294. case R_PPC64_DTPREL16_HIGHER:
  4295. case R_PPC64_DTPREL16_HIGHERA:
  4296. case R_PPC64_DTPREL16_HIGHEST:
  4297. case R_PPC64_DTPREL16_HIGHESTA:
  4298. break;
  4299. /* Nor do these. */
  4300. case R_PPC64_REL16:
  4301. case R_PPC64_REL16_LO:
  4302. case R_PPC64_REL16_HI:
  4303. case R_PPC64_REL16_HA:
  4304. case R_PPC64_REL16_HIGH:
  4305. case R_PPC64_REL16_HIGHA:
  4306. case R_PPC64_REL16_HIGHER:
  4307. case R_PPC64_REL16_HIGHERA:
  4308. case R_PPC64_REL16_HIGHEST:
  4309. case R_PPC64_REL16_HIGHESTA:
  4310. case R_PPC64_REL16_HIGHER34:
  4311. case R_PPC64_REL16_HIGHERA34:
  4312. case R_PPC64_REL16_HIGHEST34:
  4313. case R_PPC64_REL16_HIGHESTA34:
  4314. case R_PPC64_REL16DX_HA:
  4315. break;
  4316. /* Not supported as a dynamic relocation. */
  4317. case R_PPC64_ADDR64_LOCAL:
  4318. if (bfd_link_pic (info))
  4319. {
  4320. if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
  4321. ppc_howto_init ();
  4322. /* xgettext:c-format */
  4323. info->callbacks->einfo (_("%H: %s reloc unsupported "
  4324. "in shared libraries and PIEs\n"),
  4325. abfd, sec, rel->r_offset,
  4326. ppc64_elf_howto_table[r_type]->name);
  4327. bfd_set_error (bfd_error_bad_value);
  4328. return false;
  4329. }
  4330. break;
  4331. case R_PPC64_TOC16:
  4332. case R_PPC64_TOC16_DS:
  4333. htab->do_multi_toc = 1;
  4334. ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
  4335. /* Fall through. */
  4336. case R_PPC64_TOC16_LO:
  4337. case R_PPC64_TOC16_HI:
  4338. case R_PPC64_TOC16_HA:
  4339. case R_PPC64_TOC16_LO_DS:
  4340. sec->has_toc_reloc = 1;
  4341. if (h != NULL && bfd_link_executable (info))
  4342. {
  4343. /* We may need a copy reloc. */
  4344. h->non_got_ref = 1;
  4345. /* Strongly prefer a copy reloc over a dynamic reloc.
  4346. glibc ld.so as of 2019-08 will error out if one of
  4347. these relocations is emitted. */
  4348. h->needs_copy = 1;
  4349. goto dodyn;
  4350. }
  4351. break;
  4352. /* Marker reloc. */
  4353. case R_PPC64_ENTRY:
  4354. break;
  4355. /* This relocation describes the C++ object vtable hierarchy.
  4356. Reconstruct it for later use during GC. */
  4357. case R_PPC64_GNU_VTINHERIT:
  4358. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  4359. return false;
  4360. break;
  4361. /* This relocation describes which C++ vtable entries are actually
  4362. used. Record for later use during GC. */
  4363. case R_PPC64_GNU_VTENTRY:
  4364. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  4365. return false;
  4366. break;
  4367. case R_PPC64_REL14:
  4368. case R_PPC64_REL14_BRTAKEN:
  4369. case R_PPC64_REL14_BRNTAKEN:
  4370. {
  4371. asection *dest = NULL;
  4372. /* Heuristic: If jumping outside our section, chances are
  4373. we are going to need a stub. */
  4374. if (h != NULL)
  4375. {
  4376. /* If the sym is weak it may be overridden later, so
  4377. don't assume we know where a weak sym lives. */
  4378. if (h->root.type == bfd_link_hash_defined)
  4379. dest = h->root.u.def.section;
  4380. }
  4381. else
  4382. dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
  4383. if (dest != sec)
  4384. ppc64_elf_section_data (sec)->has_14bit_branch = 1;
  4385. }
  4386. goto rel24;
  4387. case R_PPC64_PLTCALL:
  4388. case R_PPC64_PLTCALL_NOTOC:
  4389. ppc64_elf_section_data (sec)->has_pltcall = 1;
  4390. /* Fall through. */
  4391. case R_PPC64_REL24:
  4392. case R_PPC64_REL24_NOTOC:
  4393. case R_PPC64_REL24_P9NOTOC:
  4394. rel24:
  4395. plt_list = ifunc;
  4396. if (h != NULL)
  4397. {
  4398. h->needs_plt = 1;
  4399. if (h->root.root.string[0] == '.'
  4400. && h->root.root.string[1] != '\0')
  4401. ppc_elf_hash_entry (h)->is_func = 1;
  4402. if (h == tga || h == dottga)
  4403. {
  4404. sec->has_tls_reloc = 1;
  4405. if (rel != relocs
  4406. && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
  4407. || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
  4408. /* We have a new-style __tls_get_addr call with
  4409. a marker reloc. */
  4410. ;
  4411. else
  4412. /* Mark this section as having an old-style call. */
  4413. sec->nomark_tls_get_addr = 1;
  4414. }
  4415. plt_list = &h->plt.plist;
  4416. }
  4417. /* We may need a .plt entry if the function this reloc
  4418. refers to is in a shared lib. */
  4419. if (plt_list
  4420. && !update_plt_info (abfd, plt_list, rel->r_addend))
  4421. return false;
  4422. break;
  4423. case R_PPC64_ADDR14:
  4424. case R_PPC64_ADDR14_BRNTAKEN:
  4425. case R_PPC64_ADDR14_BRTAKEN:
  4426. case R_PPC64_ADDR24:
  4427. goto dodyn;
  4428. case R_PPC64_TPREL64:
  4429. tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
  4430. if (bfd_link_dll (info))
  4431. info->flags |= DF_STATIC_TLS;
  4432. goto dotlstoc;
  4433. case R_PPC64_DTPMOD64:
  4434. if (rel + 1 < rel_end
  4435. && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
  4436. && rel[1].r_offset == rel->r_offset + 8)
  4437. tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
  4438. else
  4439. tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
  4440. goto dotlstoc;
  4441. case R_PPC64_DTPREL64:
  4442. tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
  4443. if (rel != relocs
  4444. && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
  4445. && rel[-1].r_offset == rel->r_offset - 8)
  4446. /* This is the second reloc of a dtpmod, dtprel pair.
  4447. Don't mark with TLS_DTPREL. */
  4448. goto dodyn;
  4449. dotlstoc:
  4450. sec->has_tls_reloc = 1;
  4451. if (h != NULL)
  4452. ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
  4453. else
  4454. if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
  4455. rel->r_addend, tls_type))
  4456. return false;
  4457. ppc64_sec = ppc64_elf_section_data (sec);
  4458. if (ppc64_sec->sec_type != sec_toc)
  4459. {
  4460. bfd_size_type amt;
  4461. /* One extra to simplify get_tls_mask. */
  4462. amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
  4463. ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
  4464. if (ppc64_sec->u.toc.symndx == NULL)
  4465. return false;
  4466. amt = sec->size * sizeof (bfd_vma) / 8;
  4467. ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
  4468. if (ppc64_sec->u.toc.add == NULL)
  4469. return false;
  4470. BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
  4471. ppc64_sec->sec_type = sec_toc;
  4472. }
  4473. BFD_ASSERT (rel->r_offset % 8 == 0);
  4474. ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
  4475. ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
  4476. /* Mark the second slot of a GD or LD entry.
  4477. -1 to indicate GD and -2 to indicate LD. */
  4478. if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
  4479. ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
  4480. else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
  4481. ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
  4482. goto dodyn;
  4483. case R_PPC64_TPREL16_HI:
  4484. case R_PPC64_TPREL16_HA:
  4485. case R_PPC64_TPREL16_HIGH:
  4486. case R_PPC64_TPREL16_HIGHA:
  4487. case R_PPC64_TPREL16_HIGHER:
  4488. case R_PPC64_TPREL16_HIGHERA:
  4489. case R_PPC64_TPREL16_HIGHEST:
  4490. case R_PPC64_TPREL16_HIGHESTA:
  4491. sec->has_tls_reloc = 1;
  4492. /* Fall through. */
  4493. case R_PPC64_TPREL34:
  4494. case R_PPC64_TPREL16:
  4495. case R_PPC64_TPREL16_DS:
  4496. case R_PPC64_TPREL16_LO:
  4497. case R_PPC64_TPREL16_LO_DS:
  4498. if (bfd_link_dll (info))
  4499. info->flags |= DF_STATIC_TLS;
  4500. goto dodyn;
  4501. case R_PPC64_ADDR64:
  4502. if (is_opd
  4503. && rel + 1 < rel_end
  4504. && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
  4505. {
  4506. if (h != NULL)
  4507. ppc_elf_hash_entry (h)->is_func = 1;
  4508. }
  4509. /* Fall through. */
  4510. case R_PPC64_ADDR16:
  4511. case R_PPC64_ADDR16_DS:
  4512. case R_PPC64_ADDR16_HA:
  4513. case R_PPC64_ADDR16_HI:
  4514. case R_PPC64_ADDR16_HIGH:
  4515. case R_PPC64_ADDR16_HIGHA:
  4516. case R_PPC64_ADDR16_HIGHER:
  4517. case R_PPC64_ADDR16_HIGHERA:
  4518. case R_PPC64_ADDR16_HIGHEST:
  4519. case R_PPC64_ADDR16_HIGHESTA:
  4520. case R_PPC64_ADDR16_LO:
  4521. case R_PPC64_ADDR16_LO_DS:
  4522. case R_PPC64_D34:
  4523. case R_PPC64_D34_LO:
  4524. case R_PPC64_D34_HI30:
  4525. case R_PPC64_D34_HA30:
  4526. case R_PPC64_ADDR16_HIGHER34:
  4527. case R_PPC64_ADDR16_HIGHERA34:
  4528. case R_PPC64_ADDR16_HIGHEST34:
  4529. case R_PPC64_ADDR16_HIGHESTA34:
  4530. case R_PPC64_D28:
  4531. if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
  4532. && rel->r_addend == 0)
  4533. {
  4534. /* We may need a .plt entry if this reloc refers to a
  4535. function in a shared lib. */
  4536. if (!update_plt_info (abfd, &h->plt.plist, 0))
  4537. return false;
  4538. h->pointer_equality_needed = 1;
  4539. }
  4540. /* Fall through. */
  4541. case R_PPC64_REL30:
  4542. case R_PPC64_REL32:
  4543. case R_PPC64_REL64:
  4544. case R_PPC64_ADDR32:
  4545. case R_PPC64_UADDR16:
  4546. case R_PPC64_UADDR32:
  4547. case R_PPC64_UADDR64:
  4548. case R_PPC64_TOC:
  4549. if (h != NULL && bfd_link_executable (info))
  4550. /* We may need a copy reloc. */
  4551. h->non_got_ref = 1;
  4552. /* Don't propagate .opd relocs. */
  4553. if (NO_OPD_RELOCS && is_opd)
  4554. break;
  4555. /* If we are creating a shared library, and this is a reloc
  4556. against a global symbol, or a non PC relative reloc
  4557. against a local symbol, then we need to copy the reloc
  4558. into the shared library. However, if we are linking with
  4559. -Bsymbolic, we do not need to copy a reloc against a
  4560. global symbol which is defined in an object we are
  4561. including in the link (i.e., DEF_REGULAR is set). At
  4562. this point we have not seen all the input files, so it is
  4563. possible that DEF_REGULAR is not set now but will be set
  4564. later (it is never cleared). In case of a weak definition,
  4565. DEF_REGULAR may be cleared later by a strong definition in
  4566. a shared library. We account for that possibility below by
  4567. storing information in the dyn_relocs field of the hash
  4568. table entry. A similar situation occurs when creating
  4569. shared libraries and symbol visibility changes render the
  4570. symbol local.
  4571. If on the other hand, we are creating an executable, we
  4572. may need to keep relocations for symbols satisfied by a
  4573. dynamic library if we manage to avoid copy relocs for the
  4574. symbol. */
  4575. dodyn:
  4576. if ((h != NULL
  4577. && (h->root.type == bfd_link_hash_defweak
  4578. || (!h->def_regular && !h->root.ldscript_def)))
  4579. || (h != NULL
  4580. && !SYMBOL_REFERENCES_LOCAL (info, h))
  4581. || (bfd_link_pic (info)
  4582. && (h != NULL
  4583. ? !bfd_is_abs_symbol (&h->root)
  4584. : isym->st_shndx != SHN_ABS)
  4585. && must_be_dyn_reloc (info, r_type))
  4586. || (!bfd_link_pic (info)
  4587. && ifunc != NULL))
  4588. {
  4589. /* We must copy these reloc types into the output file.
  4590. Create a reloc section in dynobj and make room for
  4591. this reloc. */
  4592. if (sreloc == NULL)
  4593. {
  4594. sreloc = _bfd_elf_make_dynamic_reloc_section
  4595. (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
  4596. if (sreloc == NULL)
  4597. return false;
  4598. }
  4599. /* If this is a global symbol, we count the number of
  4600. relocations we need for this symbol. */
  4601. if (h != NULL)
  4602. {
  4603. struct ppc_dyn_relocs *p;
  4604. struct ppc_dyn_relocs **head;
  4605. head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
  4606. p = *head;
  4607. if (p == NULL || p->sec != sec)
  4608. {
  4609. p = bfd_alloc (htab->elf.dynobj, sizeof *p);
  4610. if (p == NULL)
  4611. return false;
  4612. p->next = *head;
  4613. *head = p;
  4614. p->sec = sec;
  4615. p->count = 0;
  4616. p->pc_count = 0;
  4617. p->rel_count = 0;
  4618. }
  4619. p->count += 1;
  4620. if (!must_be_dyn_reloc (info, r_type))
  4621. p->pc_count += 1;
  4622. if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
  4623. && rel->r_offset % 2 == 0
  4624. && sec->alignment_power != 0)
  4625. p->rel_count += 1;
  4626. }
  4627. else
  4628. {
  4629. /* Track dynamic relocs needed for local syms too. */
  4630. struct ppc_local_dyn_relocs *p;
  4631. struct ppc_local_dyn_relocs **head;
  4632. bool is_ifunc;
  4633. asection *s;
  4634. void *vpp;
  4635. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  4636. if (s == NULL)
  4637. s = sec;
  4638. vpp = &elf_section_data (s)->local_dynrel;
  4639. head = (struct ppc_local_dyn_relocs **) vpp;
  4640. is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
  4641. p = *head;
  4642. if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
  4643. p = p->next;
  4644. if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
  4645. {
  4646. p = bfd_alloc (htab->elf.dynobj, sizeof *p);
  4647. if (p == NULL)
  4648. return false;
  4649. p->next = *head;
  4650. *head = p;
  4651. p->sec = sec;
  4652. p->count = 0;
  4653. p->rel_count = 0;
  4654. p->ifunc = is_ifunc;
  4655. }
  4656. p->count += 1;
  4657. if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
  4658. && rel->r_offset % 2 == 0
  4659. && sec->alignment_power != 0)
  4660. p->rel_count += 1;
  4661. }
  4662. }
  4663. break;
  4664. default:
  4665. break;
  4666. }
  4667. }
  4668. return true;
  4669. }
  4670. /* Merge backend specific data from an object file to the output
  4671. object file when linking. */
  4672. static bool
  4673. ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  4674. {
  4675. bfd *obfd = info->output_bfd;
  4676. unsigned long iflags, oflags;
  4677. if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
  4678. return true;
  4679. if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
  4680. return true;
  4681. if (!_bfd_generic_verify_endian_match (ibfd, info))
  4682. return false;
  4683. iflags = elf_elfheader (ibfd)->e_flags;
  4684. oflags = elf_elfheader (obfd)->e_flags;
  4685. if (iflags & ~EF_PPC64_ABI)
  4686. {
  4687. _bfd_error_handler
  4688. /* xgettext:c-format */
  4689. (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
  4690. bfd_set_error (bfd_error_bad_value);
  4691. return false;
  4692. }
  4693. else if (iflags != oflags && iflags != 0)
  4694. {
  4695. _bfd_error_handler
  4696. /* xgettext:c-format */
  4697. (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
  4698. ibfd, iflags, oflags);
  4699. bfd_set_error (bfd_error_bad_value);
  4700. return false;
  4701. }
  4702. if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
  4703. return false;
  4704. /* Merge Tag_compatibility attributes and any common GNU ones. */
  4705. return _bfd_elf_merge_object_attributes (ibfd, info);
  4706. }
  4707. static bool
  4708. ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
  4709. {
  4710. /* Print normal ELF private data. */
  4711. _bfd_elf_print_private_bfd_data (abfd, ptr);
  4712. if (elf_elfheader (abfd)->e_flags != 0)
  4713. {
  4714. FILE *file = ptr;
  4715. fprintf (file, _("private flags = 0x%lx:"),
  4716. elf_elfheader (abfd)->e_flags);
  4717. if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
  4718. fprintf (file, _(" [abiv%ld]"),
  4719. elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
  4720. fputc ('\n', file);
  4721. }
  4722. return true;
  4723. }
  4724. /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
  4725. of the code entry point, and its section, which must be in the same
  4726. object as OPD_SEC. Returns (bfd_vma) -1 on error. */
  4727. static bfd_vma
  4728. opd_entry_value (asection *opd_sec,
  4729. bfd_vma offset,
  4730. asection **code_sec,
  4731. bfd_vma *code_off,
  4732. bool in_code_sec)
  4733. {
  4734. bfd *opd_bfd = opd_sec->owner;
  4735. Elf_Internal_Rela *relocs;
  4736. Elf_Internal_Rela *lo, *hi, *look;
  4737. bfd_vma val;
  4738. /* No relocs implies we are linking a --just-symbols object, or looking
  4739. at a final linked executable with addr2line or somesuch. */
  4740. if (opd_sec->reloc_count == 0)
  4741. {
  4742. bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
  4743. if (contents == NULL)
  4744. {
  4745. if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
  4746. return (bfd_vma) -1;
  4747. ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
  4748. }
  4749. /* PR 17512: file: 64b9dfbb. */
  4750. if (offset + 7 >= opd_sec->size || offset + 7 < offset)
  4751. return (bfd_vma) -1;
  4752. val = bfd_get_64 (opd_bfd, contents + offset);
  4753. if (code_sec != NULL)
  4754. {
  4755. asection *sec, *likely = NULL;
  4756. if (in_code_sec)
  4757. {
  4758. sec = *code_sec;
  4759. if (sec->vma <= val
  4760. && val < sec->vma + sec->size)
  4761. likely = sec;
  4762. else
  4763. val = -1;
  4764. }
  4765. else
  4766. for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
  4767. if (sec->vma <= val
  4768. && (sec->flags & SEC_LOAD) != 0
  4769. && (sec->flags & SEC_ALLOC) != 0)
  4770. likely = sec;
  4771. if (likely != NULL)
  4772. {
  4773. *code_sec = likely;
  4774. if (code_off != NULL)
  4775. *code_off = val - likely->vma;
  4776. }
  4777. }
  4778. return val;
  4779. }
  4780. BFD_ASSERT (is_ppc64_elf (opd_bfd));
  4781. relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
  4782. if (relocs == NULL)
  4783. relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
  4784. /* PR 17512: file: df8e1fd6. */
  4785. if (relocs == NULL)
  4786. return (bfd_vma) -1;
  4787. /* Go find the opd reloc at the sym address. */
  4788. lo = relocs;
  4789. hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
  4790. val = (bfd_vma) -1;
  4791. while (lo < hi)
  4792. {
  4793. look = lo + (hi - lo) / 2;
  4794. if (look->r_offset < offset)
  4795. lo = look + 1;
  4796. else if (look->r_offset > offset)
  4797. hi = look;
  4798. else
  4799. {
  4800. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
  4801. if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
  4802. && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
  4803. {
  4804. unsigned long symndx = ELF64_R_SYM (look->r_info);
  4805. asection *sec = NULL;
  4806. if (symndx >= symtab_hdr->sh_info
  4807. && elf_sym_hashes (opd_bfd) != NULL)
  4808. {
  4809. struct elf_link_hash_entry **sym_hashes;
  4810. struct elf_link_hash_entry *rh;
  4811. sym_hashes = elf_sym_hashes (opd_bfd);
  4812. rh = sym_hashes[symndx - symtab_hdr->sh_info];
  4813. if (rh != NULL)
  4814. {
  4815. rh = elf_follow_link (rh);
  4816. if (rh->root.type != bfd_link_hash_defined
  4817. && rh->root.type != bfd_link_hash_defweak)
  4818. break;
  4819. if (rh->root.u.def.section->owner == opd_bfd)
  4820. {
  4821. val = rh->root.u.def.value;
  4822. sec = rh->root.u.def.section;
  4823. }
  4824. }
  4825. }
  4826. if (sec == NULL)
  4827. {
  4828. Elf_Internal_Sym *sym;
  4829. if (symndx < symtab_hdr->sh_info)
  4830. {
  4831. sym = (Elf_Internal_Sym *) symtab_hdr->contents;
  4832. if (sym == NULL)
  4833. {
  4834. size_t symcnt = symtab_hdr->sh_info;
  4835. sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
  4836. symcnt, 0,
  4837. NULL, NULL, NULL);
  4838. if (sym == NULL)
  4839. break;
  4840. symtab_hdr->contents = (bfd_byte *) sym;
  4841. }
  4842. sym += symndx;
  4843. }
  4844. else
  4845. {
  4846. sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
  4847. 1, symndx,
  4848. NULL, NULL, NULL);
  4849. if (sym == NULL)
  4850. break;
  4851. }
  4852. sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
  4853. if (sec == NULL)
  4854. break;
  4855. BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
  4856. val = sym->st_value;
  4857. }
  4858. val += look->r_addend;
  4859. if (code_off != NULL)
  4860. *code_off = val;
  4861. if (code_sec != NULL)
  4862. {
  4863. if (in_code_sec && *code_sec != sec)
  4864. return -1;
  4865. else
  4866. *code_sec = sec;
  4867. }
  4868. if (sec->output_section != NULL)
  4869. val += sec->output_section->vma + sec->output_offset;
  4870. }
  4871. break;
  4872. }
  4873. }
  4874. return val;
  4875. }
  4876. /* If the ELF symbol SYM might be a function in SEC, return the
  4877. function size and set *CODE_OFF to the function's entry point,
  4878. otherwise return zero. */
  4879. static bfd_size_type
  4880. ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
  4881. bfd_vma *code_off)
  4882. {
  4883. bfd_size_type size;
  4884. elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
  4885. if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
  4886. | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
  4887. return 0;
  4888. size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
  4889. /* In theory we should check that the symbol's type satisfies
  4890. _bfd_elf_is_function_type(), but there are some function-like
  4891. symbols which would fail this test. (eg _start). Instead
  4892. we check for hidden, local, notype symbols with zero size.
  4893. This type of symbol is generated by the annobin plugin for gcc
  4894. and clang, and should not be considered to be a function symbol. */
  4895. if (size == 0
  4896. && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
  4897. && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
  4898. && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
  4899. return 0;
  4900. if (strcmp (sym->section->name, ".opd") == 0)
  4901. {
  4902. struct _opd_sec_data *opd = get_opd_info (sym->section);
  4903. bfd_vma symval = sym->value;
  4904. if (opd != NULL
  4905. && opd->adjust != NULL
  4906. && elf_section_data (sym->section)->relocs != NULL)
  4907. {
  4908. /* opd_entry_value will use cached relocs that have been
  4909. adjusted, but with raw symbols. That means both local
  4910. and global symbols need adjusting. */
  4911. long adjust = opd->adjust[OPD_NDX (symval)];
  4912. if (adjust == -1)
  4913. return 0;
  4914. symval += adjust;
  4915. }
  4916. if (opd_entry_value (sym->section, symval,
  4917. &sec, code_off, true) == (bfd_vma) -1)
  4918. return 0;
  4919. /* An old ABI binary with dot-syms has a size of 24 on the .opd
  4920. symbol. This size has nothing to do with the code size of the
  4921. function, which is what we're supposed to return, but the
  4922. code size isn't available without looking up the dot-sym.
  4923. However, doing that would be a waste of time particularly
  4924. since elf_find_function will look at the dot-sym anyway.
  4925. Now, elf_find_function will keep the largest size of any
  4926. function sym found at the code address of interest, so return
  4927. 1 here to avoid it incorrectly caching a larger function size
  4928. for a small function. This does mean we return the wrong
  4929. size for a new-ABI function of size 24, but all that does is
  4930. disable caching for such functions. */
  4931. if (size == 24)
  4932. size = 1;
  4933. }
  4934. else
  4935. {
  4936. if (sym->section != sec)
  4937. return 0;
  4938. *code_off = sym->value;
  4939. }
  4940. /* Do not return 0 for the function's size. */
  4941. return size ? size : 1;
  4942. }
  4943. /* Return true if symbol is a strong function defined in an ELFv2
  4944. object with st_other localentry bits of zero, ie. its local entry
  4945. point coincides with its global entry point. */
  4946. static bool
  4947. is_elfv2_localentry0 (struct elf_link_hash_entry *h)
  4948. {
  4949. return (h != NULL
  4950. && h->type == STT_FUNC
  4951. && h->root.type == bfd_link_hash_defined
  4952. && (STO_PPC64_LOCAL_MASK & h->other) == 0
  4953. && !ppc_elf_hash_entry (h)->non_zero_localentry
  4954. && is_ppc64_elf (h->root.u.def.section->owner)
  4955. && abiversion (h->root.u.def.section->owner) >= 2);
  4956. }
  4957. /* Return true if symbol is defined in a regular object file. */
  4958. static bool
  4959. is_static_defined (struct elf_link_hash_entry *h)
  4960. {
  4961. return ((h->root.type == bfd_link_hash_defined
  4962. || h->root.type == bfd_link_hash_defweak)
  4963. && h->root.u.def.section != NULL
  4964. && h->root.u.def.section->output_section != NULL);
  4965. }
  4966. /* If FDH is a function descriptor symbol, return the associated code
  4967. entry symbol if it is defined. Return NULL otherwise. */
  4968. static struct ppc_link_hash_entry *
  4969. defined_code_entry (struct ppc_link_hash_entry *fdh)
  4970. {
  4971. if (fdh->is_func_descriptor)
  4972. {
  4973. struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
  4974. if (fh->elf.root.type == bfd_link_hash_defined
  4975. || fh->elf.root.type == bfd_link_hash_defweak)
  4976. return fh;
  4977. }
  4978. return NULL;
  4979. }
  4980. /* If FH is a function code entry symbol, return the associated
  4981. function descriptor symbol if it is defined. Return NULL otherwise. */
  4982. static struct ppc_link_hash_entry *
  4983. defined_func_desc (struct ppc_link_hash_entry *fh)
  4984. {
  4985. if (fh->oh != NULL
  4986. && fh->oh->is_func_descriptor)
  4987. {
  4988. struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
  4989. if (fdh->elf.root.type == bfd_link_hash_defined
  4990. || fdh->elf.root.type == bfd_link_hash_defweak)
  4991. return fdh;
  4992. }
  4993. return NULL;
  4994. }
  4995. /* Given H is a symbol that satisfies is_static_defined, return the
  4996. value in the output file. */
  4997. static bfd_vma
  4998. defined_sym_val (struct elf_link_hash_entry *h)
  4999. {
  5000. return (h->root.u.def.section->output_section->vma
  5001. + h->root.u.def.section->output_offset
  5002. + h->root.u.def.value);
  5003. }
  5004. /* Return true if H matches __tls_get_addr or one of its variants. */
  5005. static bool
  5006. is_tls_get_addr (struct elf_link_hash_entry *h,
  5007. struct ppc_link_hash_table *htab)
  5008. {
  5009. return (h == elf_hash_entry (htab->tls_get_addr_fd)
  5010. || h == elf_hash_entry (htab->tga_desc_fd)
  5011. || h == elf_hash_entry (htab->tls_get_addr)
  5012. || h == elf_hash_entry (htab->tga_desc));
  5013. }
  5014. static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
  5015. /* Garbage collect sections, after first dealing with dot-symbols. */
  5016. static bool
  5017. ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
  5018. {
  5019. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  5020. if (htab != NULL && htab->need_func_desc_adj)
  5021. {
  5022. elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
  5023. htab->need_func_desc_adj = 0;
  5024. }
  5025. return bfd_elf_gc_sections (abfd, info);
  5026. }
  5027. /* Mark all our entry sym sections, both opd and code section. */
  5028. static void
  5029. ppc64_elf_gc_keep (struct bfd_link_info *info)
  5030. {
  5031. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  5032. struct bfd_sym_chain *sym;
  5033. if (htab == NULL)
  5034. return;
  5035. for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
  5036. {
  5037. struct ppc_link_hash_entry *eh, *fh;
  5038. asection *sec;
  5039. eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
  5040. false, false, true));
  5041. if (eh == NULL)
  5042. continue;
  5043. if (eh->elf.root.type != bfd_link_hash_defined
  5044. && eh->elf.root.type != bfd_link_hash_defweak)
  5045. continue;
  5046. fh = defined_code_entry (eh);
  5047. if (fh != NULL)
  5048. {
  5049. sec = fh->elf.root.u.def.section;
  5050. sec->flags |= SEC_KEEP;
  5051. }
  5052. else if (get_opd_info (eh->elf.root.u.def.section) != NULL
  5053. && opd_entry_value (eh->elf.root.u.def.section,
  5054. eh->elf.root.u.def.value,
  5055. &sec, NULL, false) != (bfd_vma) -1)
  5056. sec->flags |= SEC_KEEP;
  5057. sec = eh->elf.root.u.def.section;
  5058. sec->flags |= SEC_KEEP;
  5059. }
  5060. }
  5061. /* Mark sections containing dynamically referenced symbols. When
  5062. building shared libraries, we must assume that any visible symbol is
  5063. referenced. */
  5064. static bool
  5065. ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
  5066. {
  5067. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  5068. struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
  5069. struct ppc_link_hash_entry *fdh;
  5070. struct bfd_elf_dynamic_list *d = info->dynamic_list;
  5071. /* Dynamic linking info is on the func descriptor sym. */
  5072. fdh = defined_func_desc (eh);
  5073. if (fdh != NULL)
  5074. eh = fdh;
  5075. if ((eh->elf.root.type == bfd_link_hash_defined
  5076. || eh->elf.root.type == bfd_link_hash_defweak)
  5077. && (!eh->elf.start_stop
  5078. || eh->elf.root.ldscript_def
  5079. || !info->start_stop_gc)
  5080. && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
  5081. || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
  5082. && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
  5083. && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
  5084. && (!bfd_link_executable (info)
  5085. || info->gc_keep_exported
  5086. || info->export_dynamic
  5087. || (eh->elf.dynamic
  5088. && d != NULL
  5089. && (*d->match) (&d->head, NULL,
  5090. eh->elf.root.root.string)))
  5091. && (eh->elf.versioned >= versioned
  5092. || !bfd_hide_sym_by_version (info->version_info,
  5093. eh->elf.root.root.string)))))
  5094. {
  5095. asection *code_sec;
  5096. struct ppc_link_hash_entry *fh;
  5097. eh->elf.root.u.def.section->flags |= SEC_KEEP;
  5098. /* Function descriptor syms cause the associated
  5099. function code sym section to be marked. */
  5100. fh = defined_code_entry (eh);
  5101. if (fh != NULL)
  5102. {
  5103. code_sec = fh->elf.root.u.def.section;
  5104. code_sec->flags |= SEC_KEEP;
  5105. }
  5106. else if (get_opd_info (eh->elf.root.u.def.section) != NULL
  5107. && opd_entry_value (eh->elf.root.u.def.section,
  5108. eh->elf.root.u.def.value,
  5109. &code_sec, NULL, false) != (bfd_vma) -1)
  5110. code_sec->flags |= SEC_KEEP;
  5111. }
  5112. return true;
  5113. }
  5114. /* Return the section that should be marked against GC for a given
  5115. relocation. */
  5116. static asection *
  5117. ppc64_elf_gc_mark_hook (asection *sec,
  5118. struct bfd_link_info *info,
  5119. Elf_Internal_Rela *rel,
  5120. struct elf_link_hash_entry *h,
  5121. Elf_Internal_Sym *sym)
  5122. {
  5123. asection *rsec;
  5124. /* Syms return NULL if we're marking .opd, so we avoid marking all
  5125. function sections, as all functions are referenced in .opd. */
  5126. rsec = NULL;
  5127. if (get_opd_info (sec) != NULL)
  5128. return rsec;
  5129. if (h != NULL)
  5130. {
  5131. enum elf_ppc64_reloc_type r_type;
  5132. struct ppc_link_hash_entry *eh, *fh, *fdh;
  5133. r_type = ELF64_R_TYPE (rel->r_info);
  5134. switch (r_type)
  5135. {
  5136. case R_PPC64_GNU_VTINHERIT:
  5137. case R_PPC64_GNU_VTENTRY:
  5138. break;
  5139. default:
  5140. switch (h->root.type)
  5141. {
  5142. case bfd_link_hash_defined:
  5143. case bfd_link_hash_defweak:
  5144. eh = ppc_elf_hash_entry (h);
  5145. fdh = defined_func_desc (eh);
  5146. if (fdh != NULL)
  5147. {
  5148. /* -mcall-aixdesc code references the dot-symbol on
  5149. a call reloc. Mark the function descriptor too
  5150. against garbage collection. */
  5151. fdh->elf.mark = 1;
  5152. if (fdh->elf.is_weakalias)
  5153. weakdef (&fdh->elf)->mark = 1;
  5154. eh = fdh;
  5155. }
  5156. /* Function descriptor syms cause the associated
  5157. function code sym section to be marked. */
  5158. fh = defined_code_entry (eh);
  5159. if (fh != NULL)
  5160. {
  5161. /* They also mark their opd section. */
  5162. eh->elf.root.u.def.section->gc_mark = 1;
  5163. rsec = fh->elf.root.u.def.section;
  5164. }
  5165. else if (get_opd_info (eh->elf.root.u.def.section) != NULL
  5166. && opd_entry_value (eh->elf.root.u.def.section,
  5167. eh->elf.root.u.def.value,
  5168. &rsec, NULL, false) != (bfd_vma) -1)
  5169. eh->elf.root.u.def.section->gc_mark = 1;
  5170. else
  5171. rsec = h->root.u.def.section;
  5172. break;
  5173. case bfd_link_hash_common:
  5174. rsec = h->root.u.c.p->section;
  5175. break;
  5176. default:
  5177. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  5178. }
  5179. }
  5180. }
  5181. else
  5182. {
  5183. struct _opd_sec_data *opd;
  5184. rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
  5185. opd = get_opd_info (rsec);
  5186. if (opd != NULL && opd->func_sec != NULL)
  5187. {
  5188. rsec->gc_mark = 1;
  5189. rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
  5190. }
  5191. }
  5192. return rsec;
  5193. }
  5194. /* The maximum size of .sfpr. */
  5195. #define SFPR_MAX (218*4)
  5196. struct sfpr_def_parms
  5197. {
  5198. const char name[12];
  5199. unsigned char lo, hi;
  5200. bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
  5201. bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
  5202. };
  5203. /* Auto-generate _save*, _rest* functions in .sfpr.
  5204. If STUB_SEC is non-null, define alias symbols in STUB_SEC
  5205. instead. */
  5206. static bool
  5207. sfpr_define (struct bfd_link_info *info,
  5208. const struct sfpr_def_parms *parm,
  5209. asection *stub_sec)
  5210. {
  5211. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  5212. unsigned int i;
  5213. size_t len = strlen (parm->name);
  5214. bool writing = false;
  5215. char sym[16];
  5216. if (htab == NULL)
  5217. return false;
  5218. memcpy (sym, parm->name, len);
  5219. sym[len + 2] = 0;
  5220. for (i = parm->lo; i <= parm->hi; i++)
  5221. {
  5222. struct ppc_link_hash_entry *h;
  5223. sym[len + 0] = i / 10 + '0';
  5224. sym[len + 1] = i % 10 + '0';
  5225. h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
  5226. writing, true, true));
  5227. if (stub_sec != NULL)
  5228. {
  5229. if (h != NULL
  5230. && h->elf.root.type == bfd_link_hash_defined
  5231. && h->elf.root.u.def.section == htab->sfpr)
  5232. {
  5233. struct elf_link_hash_entry *s;
  5234. char buf[32];
  5235. sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
  5236. s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
  5237. if (s == NULL)
  5238. return false;
  5239. if (s->root.type == bfd_link_hash_new)
  5240. {
  5241. s->root.type = bfd_link_hash_defined;
  5242. s->root.u.def.section = stub_sec;
  5243. s->root.u.def.value = (stub_sec->size - htab->sfpr->size
  5244. + h->elf.root.u.def.value);
  5245. s->ref_regular = 1;
  5246. s->def_regular = 1;
  5247. s->ref_regular_nonweak = 1;
  5248. s->forced_local = 1;
  5249. s->non_elf = 0;
  5250. s->root.linker_def = 1;
  5251. }
  5252. }
  5253. continue;
  5254. }
  5255. if (h != NULL)
  5256. {
  5257. h->save_res = 1;
  5258. if (!h->elf.def_regular)
  5259. {
  5260. h->elf.root.type = bfd_link_hash_defined;
  5261. h->elf.root.u.def.section = htab->sfpr;
  5262. h->elf.root.u.def.value = htab->sfpr->size;
  5263. h->elf.type = STT_FUNC;
  5264. h->elf.def_regular = 1;
  5265. h->elf.non_elf = 0;
  5266. _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
  5267. writing = true;
  5268. if (htab->sfpr->contents == NULL)
  5269. {
  5270. htab->sfpr->contents
  5271. = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
  5272. if (htab->sfpr->contents == NULL)
  5273. return false;
  5274. }
  5275. }
  5276. }
  5277. if (writing)
  5278. {
  5279. bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
  5280. if (i != parm->hi)
  5281. p = (*parm->write_ent) (htab->elf.dynobj, p, i);
  5282. else
  5283. p = (*parm->write_tail) (htab->elf.dynobj, p, i);
  5284. htab->sfpr->size = p - htab->sfpr->contents;
  5285. }
  5286. }
  5287. return true;
  5288. }
  5289. static bfd_byte *
  5290. savegpr0 (bfd *abfd, bfd_byte *p, int r)
  5291. {
  5292. bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
  5293. return p + 4;
  5294. }
  5295. static bfd_byte *
  5296. savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
  5297. {
  5298. p = savegpr0 (abfd, p, r);
  5299. bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
  5300. p = p + 4;
  5301. bfd_put_32 (abfd, BLR, p);
  5302. return p + 4;
  5303. }
  5304. static bfd_byte *
  5305. restgpr0 (bfd *abfd, bfd_byte *p, int r)
  5306. {
  5307. bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
  5308. return p + 4;
  5309. }
  5310. static bfd_byte *
  5311. restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
  5312. {
  5313. bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
  5314. p = p + 4;
  5315. p = restgpr0 (abfd, p, r);
  5316. bfd_put_32 (abfd, MTLR_R0, p);
  5317. p = p + 4;
  5318. if (r == 29)
  5319. {
  5320. p = restgpr0 (abfd, p, 30);
  5321. p = restgpr0 (abfd, p, 31);
  5322. }
  5323. bfd_put_32 (abfd, BLR, p);
  5324. return p + 4;
  5325. }
  5326. static bfd_byte *
  5327. savegpr1 (bfd *abfd, bfd_byte *p, int r)
  5328. {
  5329. bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
  5330. return p + 4;
  5331. }
  5332. static bfd_byte *
  5333. savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
  5334. {
  5335. p = savegpr1 (abfd, p, r);
  5336. bfd_put_32 (abfd, BLR, p);
  5337. return p + 4;
  5338. }
  5339. static bfd_byte *
  5340. restgpr1 (bfd *abfd, bfd_byte *p, int r)
  5341. {
  5342. bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
  5343. return p + 4;
  5344. }
  5345. static bfd_byte *
  5346. restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
  5347. {
  5348. p = restgpr1 (abfd, p, r);
  5349. bfd_put_32 (abfd, BLR, p);
  5350. return p + 4;
  5351. }
  5352. static bfd_byte *
  5353. savefpr (bfd *abfd, bfd_byte *p, int r)
  5354. {
  5355. bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
  5356. return p + 4;
  5357. }
  5358. static bfd_byte *
  5359. savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
  5360. {
  5361. p = savefpr (abfd, p, r);
  5362. bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
  5363. p = p + 4;
  5364. bfd_put_32 (abfd, BLR, p);
  5365. return p + 4;
  5366. }
  5367. static bfd_byte *
  5368. restfpr (bfd *abfd, bfd_byte *p, int r)
  5369. {
  5370. bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
  5371. return p + 4;
  5372. }
  5373. static bfd_byte *
  5374. restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
  5375. {
  5376. bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
  5377. p = p + 4;
  5378. p = restfpr (abfd, p, r);
  5379. bfd_put_32 (abfd, MTLR_R0, p);
  5380. p = p + 4;
  5381. if (r == 29)
  5382. {
  5383. p = restfpr (abfd, p, 30);
  5384. p = restfpr (abfd, p, 31);
  5385. }
  5386. bfd_put_32 (abfd, BLR, p);
  5387. return p + 4;
  5388. }
  5389. static bfd_byte *
  5390. savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
  5391. {
  5392. p = savefpr (abfd, p, r);
  5393. bfd_put_32 (abfd, BLR, p);
  5394. return p + 4;
  5395. }
  5396. static bfd_byte *
  5397. restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
  5398. {
  5399. p = restfpr (abfd, p, r);
  5400. bfd_put_32 (abfd, BLR, p);
  5401. return p + 4;
  5402. }
  5403. static bfd_byte *
  5404. savevr (bfd *abfd, bfd_byte *p, int r)
  5405. {
  5406. bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
  5407. p = p + 4;
  5408. bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
  5409. return p + 4;
  5410. }
  5411. static bfd_byte *
  5412. savevr_tail (bfd *abfd, bfd_byte *p, int r)
  5413. {
  5414. p = savevr (abfd, p, r);
  5415. bfd_put_32 (abfd, BLR, p);
  5416. return p + 4;
  5417. }
  5418. static bfd_byte *
  5419. restvr (bfd *abfd, bfd_byte *p, int r)
  5420. {
  5421. bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
  5422. p = p + 4;
  5423. bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
  5424. return p + 4;
  5425. }
  5426. static bfd_byte *
  5427. restvr_tail (bfd *abfd, bfd_byte *p, int r)
  5428. {
  5429. p = restvr (abfd, p, r);
  5430. bfd_put_32 (abfd, BLR, p);
  5431. return p + 4;
  5432. }
  5433. #define STDU_R1_0R1 0xf8210001
  5434. #define ADDI_R1_R1 0x38210000
  5435. /* Emit prologue of wrapper preserving regs around a call to
  5436. __tls_get_addr_opt. */
  5437. static bfd_byte *
  5438. tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
  5439. {
  5440. unsigned int i;
  5441. bfd_put_32 (obfd, MFLR_R0, p);
  5442. p += 4;
  5443. bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
  5444. p += 4;
  5445. if (htab->opd_abi)
  5446. {
  5447. for (i = 4; i < 12; i++)
  5448. {
  5449. bfd_put_32 (obfd,
  5450. STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
  5451. p += 4;
  5452. }
  5453. bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
  5454. p += 4;
  5455. }
  5456. else
  5457. {
  5458. for (i = 4; i < 12; i++)
  5459. {
  5460. bfd_put_32 (obfd,
  5461. STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
  5462. p += 4;
  5463. }
  5464. bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
  5465. p += 4;
  5466. }
  5467. return p;
  5468. }
  5469. /* Emit epilogue of wrapper preserving regs around a call to
  5470. __tls_get_addr_opt. */
  5471. static bfd_byte *
  5472. tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
  5473. {
  5474. unsigned int i;
  5475. if (htab->opd_abi)
  5476. {
  5477. for (i = 4; i < 12; i++)
  5478. {
  5479. bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
  5480. p += 4;
  5481. }
  5482. bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
  5483. p += 4;
  5484. }
  5485. else
  5486. {
  5487. for (i = 4; i < 12; i++)
  5488. {
  5489. bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
  5490. p += 4;
  5491. }
  5492. bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
  5493. p += 4;
  5494. }
  5495. bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
  5496. p += 4;
  5497. bfd_put_32 (obfd, MTLR_R0, p);
  5498. p += 4;
  5499. bfd_put_32 (obfd, BLR, p);
  5500. p += 4;
  5501. return p;
  5502. }
  5503. /* Called via elf_link_hash_traverse to transfer dynamic linking
  5504. information on function code symbol entries to their corresponding
  5505. function descriptor symbol entries. Must not be called twice for
  5506. any given code symbol. */
  5507. static bool
  5508. func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
  5509. {
  5510. struct bfd_link_info *info;
  5511. struct ppc_link_hash_table *htab;
  5512. struct ppc_link_hash_entry *fh;
  5513. struct ppc_link_hash_entry *fdh;
  5514. bool force_local;
  5515. fh = ppc_elf_hash_entry (h);
  5516. if (fh->elf.root.type == bfd_link_hash_indirect)
  5517. return true;
  5518. if (!fh->is_func)
  5519. return true;
  5520. if (fh->elf.root.root.string[0] != '.'
  5521. || fh->elf.root.root.string[1] == '\0')
  5522. return true;
  5523. info = inf;
  5524. htab = ppc_hash_table (info);
  5525. if (htab == NULL)
  5526. return false;
  5527. /* Find the corresponding function descriptor symbol. */
  5528. fdh = lookup_fdh (fh, htab);
  5529. /* Resolve undefined references to dot-symbols as the value
  5530. in the function descriptor, if we have one in a regular object.
  5531. This is to satisfy cases like ".quad .foo". Calls to functions
  5532. in dynamic objects are handled elsewhere. */
  5533. if ((fh->elf.root.type == bfd_link_hash_undefined
  5534. || fh->elf.root.type == bfd_link_hash_undefweak)
  5535. && (fdh->elf.root.type == bfd_link_hash_defined
  5536. || fdh->elf.root.type == bfd_link_hash_defweak)
  5537. && get_opd_info (fdh->elf.root.u.def.section) != NULL
  5538. && opd_entry_value (fdh->elf.root.u.def.section,
  5539. fdh->elf.root.u.def.value,
  5540. &fh->elf.root.u.def.section,
  5541. &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
  5542. {
  5543. fh->elf.root.type = fdh->elf.root.type;
  5544. fh->elf.forced_local = 1;
  5545. fh->elf.def_regular = fdh->elf.def_regular;
  5546. fh->elf.def_dynamic = fdh->elf.def_dynamic;
  5547. }
  5548. if (!fh->elf.dynamic)
  5549. {
  5550. struct plt_entry *ent;
  5551. for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
  5552. if (ent->plt.refcount > 0)
  5553. break;
  5554. if (ent == NULL)
  5555. {
  5556. if (fdh != NULL && fdh->fake)
  5557. _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
  5558. return true;
  5559. }
  5560. }
  5561. /* Create a descriptor as undefined if necessary. */
  5562. if (fdh == NULL
  5563. && !bfd_link_executable (info)
  5564. && (fh->elf.root.type == bfd_link_hash_undefined
  5565. || fh->elf.root.type == bfd_link_hash_undefweak))
  5566. {
  5567. fdh = make_fdh (info, fh);
  5568. if (fdh == NULL)
  5569. return false;
  5570. }
  5571. /* We can't support overriding of symbols on a fake descriptor. */
  5572. if (fdh != NULL
  5573. && fdh->fake
  5574. && (fh->elf.root.type == bfd_link_hash_defined
  5575. || fh->elf.root.type == bfd_link_hash_defweak))
  5576. _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
  5577. /* Transfer dynamic linking information to the function descriptor. */
  5578. if (fdh != NULL)
  5579. {
  5580. fdh->elf.ref_regular |= fh->elf.ref_regular;
  5581. fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
  5582. fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
  5583. fdh->elf.non_got_ref |= fh->elf.non_got_ref;
  5584. fdh->elf.dynamic |= fh->elf.dynamic;
  5585. fdh->elf.needs_plt |= (fh->elf.needs_plt
  5586. || fh->elf.type == STT_FUNC
  5587. || fh->elf.type == STT_GNU_IFUNC);
  5588. move_plt_plist (fh, fdh);
  5589. if (!fdh->elf.forced_local
  5590. && fh->elf.dynindx != -1)
  5591. if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
  5592. return false;
  5593. }
  5594. /* Now that the info is on the function descriptor, clear the
  5595. function code sym info. Any function code syms for which we
  5596. don't have a definition in a regular file, we force local.
  5597. This prevents a shared library from exporting syms that have
  5598. been imported from another library. Function code syms that
  5599. are really in the library we must leave global to prevent the
  5600. linker dragging in a definition from a static library. */
  5601. force_local = (!fh->elf.def_regular
  5602. || fdh == NULL
  5603. || !fdh->elf.def_regular
  5604. || fdh->elf.forced_local);
  5605. _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
  5606. return true;
  5607. }
  5608. static const struct sfpr_def_parms save_res_funcs[] =
  5609. {
  5610. { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
  5611. { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
  5612. { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
  5613. { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
  5614. { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
  5615. { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
  5616. { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
  5617. { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
  5618. { "._savef", 14, 31, savefpr, savefpr1_tail },
  5619. { "._restf", 14, 31, restfpr, restfpr1_tail },
  5620. { "_savevr_", 20, 31, savevr, savevr_tail },
  5621. { "_restvr_", 20, 31, restvr, restvr_tail }
  5622. };
  5623. /* Called near the start of bfd_elf_size_dynamic_sections. We use
  5624. this hook to a) run the edit functions in this file, b) provide
  5625. some gcc support functions, and c) transfer dynamic linking
  5626. information gathered so far on function code symbol entries, to
  5627. their corresponding function descriptor symbol entries. */
  5628. static bool
  5629. ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
  5630. {
  5631. struct ppc_link_hash_table *htab;
  5632. htab = ppc_hash_table (info);
  5633. if (htab == NULL)
  5634. return false;
  5635. /* Call back into the linker, which then runs the edit functions. */
  5636. htab->params->edit ();
  5637. /* Provide any missing _save* and _rest* functions. */
  5638. if (htab->sfpr != NULL)
  5639. {
  5640. unsigned int i;
  5641. htab->sfpr->size = 0;
  5642. for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
  5643. if (!sfpr_define (info, &save_res_funcs[i], NULL))
  5644. return false;
  5645. if (htab->sfpr->size == 0)
  5646. htab->sfpr->flags |= SEC_EXCLUDE;
  5647. }
  5648. if (bfd_link_relocatable (info))
  5649. return true;
  5650. if (htab->elf.hgot != NULL)
  5651. {
  5652. _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
  5653. /* Make .TOC. defined so as to prevent it being made dynamic.
  5654. The wrong value here is fixed later in ppc64_elf_set_toc. */
  5655. if (!htab->elf.hgot->def_regular
  5656. || htab->elf.hgot->root.type != bfd_link_hash_defined)
  5657. {
  5658. htab->elf.hgot->root.type = bfd_link_hash_defined;
  5659. htab->elf.hgot->root.u.def.value = 0;
  5660. htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
  5661. htab->elf.hgot->def_regular = 1;
  5662. htab->elf.hgot->root.linker_def = 1;
  5663. }
  5664. htab->elf.hgot->type = STT_OBJECT;
  5665. htab->elf.hgot->other
  5666. = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  5667. }
  5668. return true;
  5669. }
  5670. /* Return true if we have dynamic relocs against H or any of its weak
  5671. aliases, that apply to read-only sections. Cannot be used after
  5672. size_dynamic_sections. */
  5673. static bool
  5674. alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
  5675. {
  5676. struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
  5677. do
  5678. {
  5679. if (_bfd_elf_readonly_dynrelocs (&eh->elf))
  5680. return true;
  5681. eh = ppc_elf_hash_entry (eh->elf.u.alias);
  5682. }
  5683. while (eh != NULL && &eh->elf != h);
  5684. return false;
  5685. }
  5686. /* Return whether EH has pc-relative dynamic relocs. */
  5687. static bool
  5688. pc_dynrelocs (struct ppc_link_hash_entry *eh)
  5689. {
  5690. struct ppc_dyn_relocs *p;
  5691. for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
  5692. if (p->pc_count != 0)
  5693. return true;
  5694. return false;
  5695. }
  5696. /* Return true if a global entry stub will be created for H. Valid
  5697. for ELFv2 before plt entries have been allocated. */
  5698. static bool
  5699. global_entry_stub (struct elf_link_hash_entry *h)
  5700. {
  5701. struct plt_entry *pent;
  5702. if (!h->pointer_equality_needed
  5703. || h->def_regular)
  5704. return false;
  5705. for (pent = h->plt.plist; pent != NULL; pent = pent->next)
  5706. if (pent->plt.refcount > 0
  5707. && pent->addend == 0)
  5708. return true;
  5709. return false;
  5710. }
  5711. /* Adjust a symbol defined by a dynamic object and referenced by a
  5712. regular object. The current definition is in some section of the
  5713. dynamic object, but we're not including those sections. We have to
  5714. change the definition to something the rest of the link can
  5715. understand. */
  5716. static bool
  5717. ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  5718. struct elf_link_hash_entry *h)
  5719. {
  5720. struct ppc_link_hash_table *htab;
  5721. asection *s, *srel;
  5722. htab = ppc_hash_table (info);
  5723. if (htab == NULL)
  5724. return false;
  5725. /* Deal with function syms. */
  5726. if (h->type == STT_FUNC
  5727. || h->type == STT_GNU_IFUNC
  5728. || h->needs_plt)
  5729. {
  5730. bool local = (ppc_elf_hash_entry (h)->save_res
  5731. || SYMBOL_CALLS_LOCAL (info, h)
  5732. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
  5733. /* Discard dyn_relocs when non-pic if we've decided that a
  5734. function symbol is local and not an ifunc. We keep dynamic
  5735. relocs for ifuncs when local rather than always emitting a
  5736. plt call stub for them and defining the symbol on the call
  5737. stub. We can't do that for ELFv1 anyway (a function symbol
  5738. is defined on a descriptor, not code) and it can be faster at
  5739. run-time due to not needing to bounce through a stub. The
  5740. dyn_relocs for ifuncs will be applied even in a static
  5741. executable. */
  5742. if (!bfd_link_pic (info)
  5743. && h->type != STT_GNU_IFUNC
  5744. && local)
  5745. h->dyn_relocs = NULL;
  5746. /* Clear procedure linkage table information for any symbol that
  5747. won't need a .plt entry. */
  5748. struct plt_entry *ent;
  5749. for (ent = h->plt.plist; ent != NULL; ent = ent->next)
  5750. if (ent->plt.refcount > 0)
  5751. break;
  5752. if (ent == NULL
  5753. || (h->type != STT_GNU_IFUNC
  5754. && local
  5755. && (htab->can_convert_all_inline_plt
  5756. || (ppc_elf_hash_entry (h)->tls_mask
  5757. & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
  5758. {
  5759. h->plt.plist = NULL;
  5760. h->needs_plt = 0;
  5761. h->pointer_equality_needed = 0;
  5762. }
  5763. else if (abiversion (info->output_bfd) >= 2)
  5764. {
  5765. /* Taking a function's address in a read/write section
  5766. doesn't require us to define the function symbol in the
  5767. executable on a global entry stub. A dynamic reloc can
  5768. be used instead. The reason we prefer a few more dynamic
  5769. relocs is that calling via a global entry stub costs a
  5770. few more instructions, and pointer_equality_needed causes
  5771. extra work in ld.so when resolving these symbols. */
  5772. if (global_entry_stub (h))
  5773. {
  5774. if (!_bfd_elf_readonly_dynrelocs (h))
  5775. {
  5776. h->pointer_equality_needed = 0;
  5777. /* If we haven't seen a branch reloc and the symbol
  5778. isn't an ifunc then we don't need a plt entry. */
  5779. if (!h->needs_plt)
  5780. h->plt.plist = NULL;
  5781. }
  5782. else if (!bfd_link_pic (info))
  5783. /* We are going to be defining the function symbol on the
  5784. plt stub, so no dyn_relocs needed when non-pic. */
  5785. h->dyn_relocs = NULL;
  5786. }
  5787. /* ELFv2 function symbols can't have copy relocs. */
  5788. return true;
  5789. }
  5790. else if (!h->needs_plt
  5791. && !_bfd_elf_readonly_dynrelocs (h))
  5792. {
  5793. /* If we haven't seen a branch reloc and the symbol isn't an
  5794. ifunc then we don't need a plt entry. */
  5795. h->plt.plist = NULL;
  5796. h->pointer_equality_needed = 0;
  5797. return true;
  5798. }
  5799. }
  5800. else
  5801. h->plt.plist = NULL;
  5802. /* If this is a weak symbol, and there is a real definition, the
  5803. processor independent code will have arranged for us to see the
  5804. real definition first, and we can just use the same value. */
  5805. if (h->is_weakalias)
  5806. {
  5807. struct elf_link_hash_entry *def = weakdef (h);
  5808. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  5809. h->root.u.def.section = def->root.u.def.section;
  5810. h->root.u.def.value = def->root.u.def.value;
  5811. if (def->root.u.def.section == htab->elf.sdynbss
  5812. || def->root.u.def.section == htab->elf.sdynrelro)
  5813. h->dyn_relocs = NULL;
  5814. return true;
  5815. }
  5816. /* If we are creating a shared library, we must presume that the
  5817. only references to the symbol are via the global offset table.
  5818. For such cases we need not do anything here; the relocations will
  5819. be handled correctly by relocate_section. */
  5820. if (!bfd_link_executable (info))
  5821. return true;
  5822. /* If there are no references to this symbol that do not use the
  5823. GOT, we don't need to generate a copy reloc. */
  5824. if (!h->non_got_ref)
  5825. return true;
  5826. /* Don't generate a copy reloc for symbols defined in the executable. */
  5827. if (!h->def_dynamic || !h->ref_regular || h->def_regular
  5828. /* If -z nocopyreloc was given, don't generate them either. */
  5829. || info->nocopyreloc
  5830. /* If we don't find any dynamic relocs in read-only sections, then
  5831. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  5832. || (ELIMINATE_COPY_RELOCS
  5833. && !h->needs_copy
  5834. && !alias_readonly_dynrelocs (h))
  5835. /* Protected variables do not work with .dynbss. The copy in
  5836. .dynbss won't be used by the shared library with the protected
  5837. definition for the variable. Text relocations are preferable
  5838. to an incorrect program. */
  5839. || h->protected_def)
  5840. return true;
  5841. if (h->type == STT_FUNC
  5842. || h->type == STT_GNU_IFUNC)
  5843. {
  5844. /* .dynbss copies of function symbols only work if we have
  5845. ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
  5846. use dot-symbols and set the function symbol size to the text
  5847. size of the function rather than the size of the descriptor.
  5848. That's wrong for copying a descriptor. */
  5849. if (ppc_elf_hash_entry (h)->oh == NULL
  5850. || !(h->size == 24 || h->size == 16))
  5851. return true;
  5852. /* We should never get here, but unfortunately there are old
  5853. versions of gcc (circa gcc-3.2) that improperly for the
  5854. ELFv1 ABI put initialized function pointers, vtable refs and
  5855. suchlike in read-only sections. Allow them to proceed, but
  5856. warn that this might break at runtime. */
  5857. info->callbacks->einfo
  5858. (_("%P: copy reloc against `%pT' requires lazy plt linking; "
  5859. "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
  5860. h->root.root.string);
  5861. }
  5862. /* This is a reference to a symbol defined by a dynamic object which
  5863. is not a function. */
  5864. /* We must allocate the symbol in our .dynbss section, which will
  5865. become part of the .bss section of the executable. There will be
  5866. an entry for this symbol in the .dynsym section. The dynamic
  5867. object will contain position independent code, so all references
  5868. from the dynamic object to this symbol will go through the global
  5869. offset table. The dynamic linker will use the .dynsym entry to
  5870. determine the address it must put in the global offset table, so
  5871. both the dynamic object and the regular object will refer to the
  5872. same memory location for the variable. */
  5873. if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  5874. {
  5875. s = htab->elf.sdynrelro;
  5876. srel = htab->elf.sreldynrelro;
  5877. }
  5878. else
  5879. {
  5880. s = htab->elf.sdynbss;
  5881. srel = htab->elf.srelbss;
  5882. }
  5883. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  5884. {
  5885. /* We must generate a R_PPC64_COPY reloc to tell the dynamic
  5886. linker to copy the initial value out of the dynamic object
  5887. and into the runtime process image. */
  5888. srel->size += sizeof (Elf64_External_Rela);
  5889. h->needs_copy = 1;
  5890. }
  5891. /* We no longer want dyn_relocs. */
  5892. h->dyn_relocs = NULL;
  5893. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  5894. }
  5895. /* If given a function descriptor symbol, hide both the function code
  5896. sym and the descriptor. */
  5897. static void
  5898. ppc64_elf_hide_symbol (struct bfd_link_info *info,
  5899. struct elf_link_hash_entry *h,
  5900. bool force_local)
  5901. {
  5902. struct ppc_link_hash_entry *eh;
  5903. _bfd_elf_link_hash_hide_symbol (info, h, force_local);
  5904. if (ppc_hash_table (info) == NULL)
  5905. return;
  5906. eh = ppc_elf_hash_entry (h);
  5907. if (eh->is_func_descriptor)
  5908. {
  5909. struct ppc_link_hash_entry *fh = eh->oh;
  5910. if (fh == NULL)
  5911. {
  5912. const char *p, *q;
  5913. struct elf_link_hash_table *htab = elf_hash_table (info);
  5914. char save;
  5915. /* We aren't supposed to use alloca in BFD because on
  5916. systems which do not have alloca the version in libiberty
  5917. calls xmalloc, which might cause the program to crash
  5918. when it runs out of memory. This function doesn't have a
  5919. return status, so there's no way to gracefully return an
  5920. error. So cheat. We know that string[-1] can be safely
  5921. accessed; It's either a string in an ELF string table,
  5922. or allocated in an objalloc structure. */
  5923. p = eh->elf.root.root.string - 1;
  5924. save = *p;
  5925. *(char *) p = '.';
  5926. fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
  5927. false, false));
  5928. *(char *) p = save;
  5929. /* Unfortunately, if it so happens that the string we were
  5930. looking for was allocated immediately before this string,
  5931. then we overwrote the string terminator. That's the only
  5932. reason the lookup should fail. */
  5933. if (fh == NULL)
  5934. {
  5935. q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
  5936. while (q >= eh->elf.root.root.string && *q == *p)
  5937. --q, --p;
  5938. if (q < eh->elf.root.root.string && *p == '.')
  5939. fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
  5940. false, false));
  5941. }
  5942. if (fh != NULL)
  5943. {
  5944. eh->oh = fh;
  5945. fh->oh = eh;
  5946. }
  5947. }
  5948. if (fh != NULL)
  5949. _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
  5950. }
  5951. }
  5952. static bool
  5953. get_sym_h (struct elf_link_hash_entry **hp,
  5954. Elf_Internal_Sym **symp,
  5955. asection **symsecp,
  5956. unsigned char **tls_maskp,
  5957. Elf_Internal_Sym **locsymsp,
  5958. unsigned long r_symndx,
  5959. bfd *ibfd)
  5960. {
  5961. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
  5962. if (r_symndx >= symtab_hdr->sh_info)
  5963. {
  5964. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
  5965. struct elf_link_hash_entry *h;
  5966. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  5967. h = elf_follow_link (h);
  5968. if (hp != NULL)
  5969. *hp = h;
  5970. if (symp != NULL)
  5971. *symp = NULL;
  5972. if (symsecp != NULL)
  5973. {
  5974. asection *symsec = NULL;
  5975. if (h->root.type == bfd_link_hash_defined
  5976. || h->root.type == bfd_link_hash_defweak)
  5977. symsec = h->root.u.def.section;
  5978. *symsecp = symsec;
  5979. }
  5980. if (tls_maskp != NULL)
  5981. *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
  5982. }
  5983. else
  5984. {
  5985. Elf_Internal_Sym *sym;
  5986. Elf_Internal_Sym *locsyms = *locsymsp;
  5987. if (locsyms == NULL)
  5988. {
  5989. locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
  5990. if (locsyms == NULL)
  5991. locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
  5992. symtab_hdr->sh_info,
  5993. 0, NULL, NULL, NULL);
  5994. if (locsyms == NULL)
  5995. return false;
  5996. *locsymsp = locsyms;
  5997. }
  5998. sym = locsyms + r_symndx;
  5999. if (hp != NULL)
  6000. *hp = NULL;
  6001. if (symp != NULL)
  6002. *symp = sym;
  6003. if (symsecp != NULL)
  6004. *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
  6005. if (tls_maskp != NULL)
  6006. {
  6007. struct got_entry **lgot_ents;
  6008. unsigned char *tls_mask;
  6009. tls_mask = NULL;
  6010. lgot_ents = elf_local_got_ents (ibfd);
  6011. if (lgot_ents != NULL)
  6012. {
  6013. struct plt_entry **local_plt = (struct plt_entry **)
  6014. (lgot_ents + symtab_hdr->sh_info);
  6015. unsigned char *lgot_masks = (unsigned char *)
  6016. (local_plt + symtab_hdr->sh_info);
  6017. tls_mask = &lgot_masks[r_symndx];
  6018. }
  6019. *tls_maskp = tls_mask;
  6020. }
  6021. }
  6022. return true;
  6023. }
  6024. /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
  6025. error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
  6026. type suitable for optimization, and 1 otherwise. */
  6027. static int
  6028. get_tls_mask (unsigned char **tls_maskp,
  6029. unsigned long *toc_symndx,
  6030. bfd_vma *toc_addend,
  6031. Elf_Internal_Sym **locsymsp,
  6032. const Elf_Internal_Rela *rel,
  6033. bfd *ibfd)
  6034. {
  6035. unsigned long r_symndx;
  6036. int next_r;
  6037. struct elf_link_hash_entry *h;
  6038. Elf_Internal_Sym *sym;
  6039. asection *sec;
  6040. bfd_vma off;
  6041. r_symndx = ELF64_R_SYM (rel->r_info);
  6042. if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
  6043. return 0;
  6044. if ((*tls_maskp != NULL
  6045. && (**tls_maskp & TLS_TLS) != 0
  6046. && **tls_maskp != (TLS_TLS | TLS_MARK))
  6047. || sec == NULL
  6048. || ppc64_elf_section_data (sec) == NULL
  6049. || ppc64_elf_section_data (sec)->sec_type != sec_toc)
  6050. return 1;
  6051. /* Look inside a TOC section too. */
  6052. if (h != NULL)
  6053. {
  6054. BFD_ASSERT (h->root.type == bfd_link_hash_defined);
  6055. off = h->root.u.def.value;
  6056. }
  6057. else
  6058. off = sym->st_value;
  6059. off += rel->r_addend;
  6060. BFD_ASSERT (off % 8 == 0);
  6061. r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
  6062. next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
  6063. if (toc_symndx != NULL)
  6064. *toc_symndx = r_symndx;
  6065. if (toc_addend != NULL)
  6066. *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
  6067. if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
  6068. return 0;
  6069. if ((h == NULL || is_static_defined (h))
  6070. && (next_r == -1 || next_r == -2))
  6071. return 1 - next_r;
  6072. return 1;
  6073. }
  6074. /* Find (or create) an entry in the tocsave hash table. */
  6075. static struct tocsave_entry *
  6076. tocsave_find (struct ppc_link_hash_table *htab,
  6077. enum insert_option insert,
  6078. Elf_Internal_Sym **local_syms,
  6079. const Elf_Internal_Rela *irela,
  6080. bfd *ibfd)
  6081. {
  6082. unsigned long r_indx;
  6083. struct elf_link_hash_entry *h;
  6084. Elf_Internal_Sym *sym;
  6085. struct tocsave_entry ent, *p;
  6086. hashval_t hash;
  6087. struct tocsave_entry **slot;
  6088. r_indx = ELF64_R_SYM (irela->r_info);
  6089. if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
  6090. return NULL;
  6091. if (ent.sec == NULL || ent.sec->output_section == NULL)
  6092. {
  6093. _bfd_error_handler
  6094. (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
  6095. return NULL;
  6096. }
  6097. if (h != NULL)
  6098. ent.offset = h->root.u.def.value;
  6099. else
  6100. ent.offset = sym->st_value;
  6101. ent.offset += irela->r_addend;
  6102. hash = tocsave_htab_hash (&ent);
  6103. slot = ((struct tocsave_entry **)
  6104. htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
  6105. if (slot == NULL)
  6106. return NULL;
  6107. if (*slot == NULL)
  6108. {
  6109. p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
  6110. if (p == NULL)
  6111. return NULL;
  6112. *p = ent;
  6113. *slot = p;
  6114. }
  6115. return *slot;
  6116. }
  6117. /* Adjust all global syms defined in opd sections. In gcc generated
  6118. code for the old ABI, these will already have been done. */
  6119. static bool
  6120. adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
  6121. {
  6122. struct ppc_link_hash_entry *eh;
  6123. asection *sym_sec;
  6124. struct _opd_sec_data *opd;
  6125. if (h->root.type == bfd_link_hash_indirect)
  6126. return true;
  6127. if (h->root.type != bfd_link_hash_defined
  6128. && h->root.type != bfd_link_hash_defweak)
  6129. return true;
  6130. eh = ppc_elf_hash_entry (h);
  6131. if (eh->adjust_done)
  6132. return true;
  6133. sym_sec = eh->elf.root.u.def.section;
  6134. opd = get_opd_info (sym_sec);
  6135. if (opd != NULL && opd->adjust != NULL)
  6136. {
  6137. long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
  6138. if (adjust == -1)
  6139. {
  6140. /* This entry has been deleted. */
  6141. asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
  6142. if (dsec == NULL)
  6143. {
  6144. for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
  6145. if (discarded_section (dsec))
  6146. {
  6147. ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
  6148. break;
  6149. }
  6150. }
  6151. eh->elf.root.u.def.value = 0;
  6152. eh->elf.root.u.def.section = dsec;
  6153. }
  6154. else
  6155. eh->elf.root.u.def.value += adjust;
  6156. eh->adjust_done = 1;
  6157. }
  6158. return true;
  6159. }
  6160. /* Handles decrementing dynamic reloc counts for the reloc specified by
  6161. R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
  6162. have already been determined. */
  6163. static bool
  6164. dec_dynrel_count (const Elf_Internal_Rela *rel,
  6165. asection *sec,
  6166. struct bfd_link_info *info,
  6167. Elf_Internal_Sym **local_syms,
  6168. struct elf_link_hash_entry *h,
  6169. Elf_Internal_Sym *sym)
  6170. {
  6171. enum elf_ppc64_reloc_type r_type;
  6172. asection *sym_sec = NULL;
  6173. /* Can this reloc be dynamic? This switch, and later tests here
  6174. should be kept in sync with the code in check_relocs. */
  6175. r_type = ELF64_R_TYPE (rel->r_info);
  6176. switch (r_type)
  6177. {
  6178. default:
  6179. return true;
  6180. case R_PPC64_TOC16:
  6181. case R_PPC64_TOC16_DS:
  6182. case R_PPC64_TOC16_LO:
  6183. case R_PPC64_TOC16_HI:
  6184. case R_PPC64_TOC16_HA:
  6185. case R_PPC64_TOC16_LO_DS:
  6186. if (h == NULL)
  6187. return true;
  6188. break;
  6189. case R_PPC64_TPREL16:
  6190. case R_PPC64_TPREL16_LO:
  6191. case R_PPC64_TPREL16_HI:
  6192. case R_PPC64_TPREL16_HA:
  6193. case R_PPC64_TPREL16_DS:
  6194. case R_PPC64_TPREL16_LO_DS:
  6195. case R_PPC64_TPREL16_HIGH:
  6196. case R_PPC64_TPREL16_HIGHA:
  6197. case R_PPC64_TPREL16_HIGHER:
  6198. case R_PPC64_TPREL16_HIGHERA:
  6199. case R_PPC64_TPREL16_HIGHEST:
  6200. case R_PPC64_TPREL16_HIGHESTA:
  6201. case R_PPC64_TPREL64:
  6202. case R_PPC64_TPREL34:
  6203. case R_PPC64_DTPMOD64:
  6204. case R_PPC64_DTPREL64:
  6205. case R_PPC64_ADDR64:
  6206. case R_PPC64_REL30:
  6207. case R_PPC64_REL32:
  6208. case R_PPC64_REL64:
  6209. case R_PPC64_ADDR14:
  6210. case R_PPC64_ADDR14_BRNTAKEN:
  6211. case R_PPC64_ADDR14_BRTAKEN:
  6212. case R_PPC64_ADDR16:
  6213. case R_PPC64_ADDR16_DS:
  6214. case R_PPC64_ADDR16_HA:
  6215. case R_PPC64_ADDR16_HI:
  6216. case R_PPC64_ADDR16_HIGH:
  6217. case R_PPC64_ADDR16_HIGHA:
  6218. case R_PPC64_ADDR16_HIGHER:
  6219. case R_PPC64_ADDR16_HIGHERA:
  6220. case R_PPC64_ADDR16_HIGHEST:
  6221. case R_PPC64_ADDR16_HIGHESTA:
  6222. case R_PPC64_ADDR16_LO:
  6223. case R_PPC64_ADDR16_LO_DS:
  6224. case R_PPC64_ADDR24:
  6225. case R_PPC64_ADDR32:
  6226. case R_PPC64_UADDR16:
  6227. case R_PPC64_UADDR32:
  6228. case R_PPC64_UADDR64:
  6229. case R_PPC64_TOC:
  6230. case R_PPC64_D34:
  6231. case R_PPC64_D34_LO:
  6232. case R_PPC64_D34_HI30:
  6233. case R_PPC64_D34_HA30:
  6234. case R_PPC64_ADDR16_HIGHER34:
  6235. case R_PPC64_ADDR16_HIGHERA34:
  6236. case R_PPC64_ADDR16_HIGHEST34:
  6237. case R_PPC64_ADDR16_HIGHESTA34:
  6238. case R_PPC64_D28:
  6239. break;
  6240. }
  6241. if (local_syms != NULL)
  6242. {
  6243. unsigned long r_symndx;
  6244. bfd *ibfd = sec->owner;
  6245. r_symndx = ELF64_R_SYM (rel->r_info);
  6246. if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
  6247. return false;
  6248. }
  6249. if ((h != NULL
  6250. && (h->root.type == bfd_link_hash_defweak
  6251. || (!h->def_regular && !h->root.ldscript_def)))
  6252. || (h != NULL
  6253. && !SYMBOL_REFERENCES_LOCAL (info, h))
  6254. || (bfd_link_pic (info)
  6255. && (h != NULL
  6256. ? !bfd_is_abs_symbol (&h->root)
  6257. : sym_sec != bfd_abs_section_ptr)
  6258. && must_be_dyn_reloc (info, r_type))
  6259. || (!bfd_link_pic (info)
  6260. && (h != NULL
  6261. ? h->type == STT_GNU_IFUNC
  6262. : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
  6263. ;
  6264. else
  6265. return true;
  6266. if (h != NULL)
  6267. {
  6268. struct ppc_dyn_relocs *p;
  6269. struct ppc_dyn_relocs **pp;
  6270. pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
  6271. /* elf_gc_sweep may have already removed all dyn relocs associated
  6272. with local syms for a given section. Also, symbol flags are
  6273. changed by elf_gc_sweep_symbol, confusing the test above. Don't
  6274. report a dynreloc miscount. */
  6275. if (*pp == NULL && info->gc_sections)
  6276. return true;
  6277. while ((p = *pp) != NULL)
  6278. {
  6279. if (p->sec == sec)
  6280. {
  6281. if (!must_be_dyn_reloc (info, r_type))
  6282. p->pc_count -= 1;
  6283. if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
  6284. && rel->r_offset % 2 == 0
  6285. && sec->alignment_power != 0)
  6286. p->rel_count -= 1;
  6287. p->count -= 1;
  6288. if (p->count == 0)
  6289. *pp = p->next;
  6290. return true;
  6291. }
  6292. pp = &p->next;
  6293. }
  6294. }
  6295. else
  6296. {
  6297. struct ppc_local_dyn_relocs *p;
  6298. struct ppc_local_dyn_relocs **pp;
  6299. void *vpp;
  6300. bool is_ifunc;
  6301. if (local_syms == NULL)
  6302. sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
  6303. if (sym_sec == NULL)
  6304. sym_sec = sec;
  6305. vpp = &elf_section_data (sym_sec)->local_dynrel;
  6306. pp = (struct ppc_local_dyn_relocs **) vpp;
  6307. if (*pp == NULL && info->gc_sections)
  6308. return true;
  6309. is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
  6310. while ((p = *pp) != NULL)
  6311. {
  6312. if (p->sec == sec && p->ifunc == is_ifunc)
  6313. {
  6314. if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
  6315. && rel->r_offset % 2 == 0
  6316. && sec->alignment_power != 0)
  6317. p->rel_count -= 1;
  6318. p->count -= 1;
  6319. if (p->count == 0)
  6320. *pp = p->next;
  6321. return true;
  6322. }
  6323. pp = &p->next;
  6324. }
  6325. }
  6326. /* xgettext:c-format */
  6327. _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
  6328. sec->owner, sec);
  6329. bfd_set_error (bfd_error_bad_value);
  6330. return false;
  6331. }
  6332. /* Remove unused Official Procedure Descriptor entries. Currently we
  6333. only remove those associated with functions in discarded link-once
  6334. sections, or weakly defined functions that have been overridden. It
  6335. would be possible to remove many more entries for statically linked
  6336. applications. */
  6337. bool
  6338. ppc64_elf_edit_opd (struct bfd_link_info *info)
  6339. {
  6340. bfd *ibfd;
  6341. bool some_edited = false;
  6342. asection *need_pad = NULL;
  6343. struct ppc_link_hash_table *htab;
  6344. htab = ppc_hash_table (info);
  6345. if (htab == NULL)
  6346. return false;
  6347. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  6348. {
  6349. asection *sec;
  6350. Elf_Internal_Rela *relstart, *rel, *relend;
  6351. Elf_Internal_Shdr *symtab_hdr;
  6352. Elf_Internal_Sym *local_syms;
  6353. struct _opd_sec_data *opd;
  6354. bool need_edit, add_aux_fields, broken;
  6355. bfd_size_type cnt_16b = 0;
  6356. if (!is_ppc64_elf (ibfd))
  6357. continue;
  6358. sec = bfd_get_section_by_name (ibfd, ".opd");
  6359. if (sec == NULL || sec->size == 0)
  6360. continue;
  6361. if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  6362. continue;
  6363. if (sec->output_section == bfd_abs_section_ptr)
  6364. continue;
  6365. /* Look through the section relocs. */
  6366. if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
  6367. continue;
  6368. local_syms = NULL;
  6369. symtab_hdr = &elf_symtab_hdr (ibfd);
  6370. /* Read the relocations. */
  6371. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
  6372. info->keep_memory);
  6373. if (relstart == NULL)
  6374. return false;
  6375. /* First run through the relocs to check they are sane, and to
  6376. determine whether we need to edit this opd section. */
  6377. need_edit = false;
  6378. broken = false;
  6379. need_pad = sec;
  6380. relend = relstart + sec->reloc_count;
  6381. for (rel = relstart; rel < relend; )
  6382. {
  6383. enum elf_ppc64_reloc_type r_type;
  6384. unsigned long r_symndx;
  6385. asection *sym_sec;
  6386. struct elf_link_hash_entry *h;
  6387. Elf_Internal_Sym *sym;
  6388. bfd_vma offset;
  6389. /* .opd contains an array of 16 or 24 byte entries. We're
  6390. only interested in the reloc pointing to a function entry
  6391. point. */
  6392. offset = rel->r_offset;
  6393. if (rel + 1 == relend
  6394. || rel[1].r_offset != offset + 8)
  6395. {
  6396. /* If someone messes with .opd alignment then after a
  6397. "ld -r" we might have padding in the middle of .opd.
  6398. Also, there's nothing to prevent someone putting
  6399. something silly in .opd with the assembler. No .opd
  6400. optimization for them! */
  6401. broken_opd:
  6402. _bfd_error_handler
  6403. (_("%pB: .opd is not a regular array of opd entries"), ibfd);
  6404. broken = true;
  6405. break;
  6406. }
  6407. if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
  6408. || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
  6409. {
  6410. _bfd_error_handler
  6411. /* xgettext:c-format */
  6412. (_("%pB: unexpected reloc type %u in .opd section"),
  6413. ibfd, r_type);
  6414. broken = true;
  6415. break;
  6416. }
  6417. r_symndx = ELF64_R_SYM (rel->r_info);
  6418. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  6419. r_symndx, ibfd))
  6420. goto error_ret;
  6421. if (sym_sec == NULL || sym_sec->owner == NULL)
  6422. {
  6423. const char *sym_name;
  6424. if (h != NULL)
  6425. sym_name = h->root.root.string;
  6426. else
  6427. sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
  6428. sym_sec);
  6429. _bfd_error_handler
  6430. /* xgettext:c-format */
  6431. (_("%pB: undefined sym `%s' in .opd section"),
  6432. ibfd, sym_name);
  6433. broken = true;
  6434. break;
  6435. }
  6436. /* opd entries are always for functions defined in the
  6437. current input bfd. If the symbol isn't defined in the
  6438. input bfd, then we won't be using the function in this
  6439. bfd; It must be defined in a linkonce section in another
  6440. bfd, or is weak. It's also possible that we are
  6441. discarding the function due to a linker script /DISCARD/,
  6442. which we test for via the output_section. */
  6443. if (sym_sec->owner != ibfd
  6444. || sym_sec->output_section == bfd_abs_section_ptr)
  6445. need_edit = true;
  6446. rel += 2;
  6447. if (rel + 1 == relend
  6448. || (rel + 2 < relend
  6449. && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
  6450. ++rel;
  6451. if (rel == relend)
  6452. {
  6453. if (sec->size == offset + 24)
  6454. {
  6455. need_pad = NULL;
  6456. break;
  6457. }
  6458. if (sec->size == offset + 16)
  6459. {
  6460. cnt_16b++;
  6461. break;
  6462. }
  6463. goto broken_opd;
  6464. }
  6465. else if (rel + 1 < relend
  6466. && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
  6467. && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
  6468. {
  6469. if (rel[0].r_offset == offset + 16)
  6470. cnt_16b++;
  6471. else if (rel[0].r_offset != offset + 24)
  6472. goto broken_opd;
  6473. }
  6474. else
  6475. goto broken_opd;
  6476. }
  6477. add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
  6478. if (!broken && (need_edit || add_aux_fields))
  6479. {
  6480. Elf_Internal_Rela *write_rel;
  6481. Elf_Internal_Shdr *rel_hdr;
  6482. bfd_byte *rptr, *wptr;
  6483. bfd_byte *new_contents;
  6484. bfd_size_type amt;
  6485. new_contents = NULL;
  6486. amt = OPD_NDX (sec->size) * sizeof (long);
  6487. opd = &ppc64_elf_section_data (sec)->u.opd;
  6488. opd->adjust = bfd_zalloc (sec->owner, amt);
  6489. if (opd->adjust == NULL)
  6490. return false;
  6491. /* This seems a waste of time as input .opd sections are all
  6492. zeros as generated by gcc, but I suppose there's no reason
  6493. this will always be so. We might start putting something in
  6494. the third word of .opd entries. */
  6495. if ((sec->flags & SEC_IN_MEMORY) == 0)
  6496. {
  6497. bfd_byte *loc;
  6498. if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
  6499. {
  6500. free (loc);
  6501. error_ret:
  6502. if (symtab_hdr->contents != (unsigned char *) local_syms)
  6503. free (local_syms);
  6504. if (elf_section_data (sec)->relocs != relstart)
  6505. free (relstart);
  6506. return false;
  6507. }
  6508. sec->contents = loc;
  6509. sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
  6510. }
  6511. elf_section_data (sec)->relocs = relstart;
  6512. new_contents = sec->contents;
  6513. if (add_aux_fields)
  6514. {
  6515. new_contents = bfd_malloc (sec->size + cnt_16b * 8);
  6516. if (new_contents == NULL)
  6517. return false;
  6518. need_pad = NULL;
  6519. }
  6520. wptr = new_contents;
  6521. rptr = sec->contents;
  6522. write_rel = relstart;
  6523. for (rel = relstart; rel < relend; )
  6524. {
  6525. unsigned long r_symndx;
  6526. asection *sym_sec;
  6527. struct elf_link_hash_entry *h;
  6528. struct ppc_link_hash_entry *fdh = NULL;
  6529. Elf_Internal_Sym *sym;
  6530. long opd_ent_size;
  6531. Elf_Internal_Rela *next_rel;
  6532. bool skip;
  6533. r_symndx = ELF64_R_SYM (rel->r_info);
  6534. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  6535. r_symndx, ibfd))
  6536. goto error_ret;
  6537. next_rel = rel + 2;
  6538. if (next_rel + 1 == relend
  6539. || (next_rel + 2 < relend
  6540. && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
  6541. ++next_rel;
  6542. /* See if the .opd entry is full 24 byte or
  6543. 16 byte (with fd_aux entry overlapped with next
  6544. fd_func). */
  6545. opd_ent_size = 24;
  6546. if (next_rel == relend)
  6547. {
  6548. if (sec->size == rel->r_offset + 16)
  6549. opd_ent_size = 16;
  6550. }
  6551. else if (next_rel->r_offset == rel->r_offset + 16)
  6552. opd_ent_size = 16;
  6553. if (h != NULL
  6554. && h->root.root.string[0] == '.')
  6555. {
  6556. fdh = ppc_elf_hash_entry (h)->oh;
  6557. if (fdh != NULL)
  6558. {
  6559. fdh = ppc_follow_link (fdh);
  6560. if (fdh->elf.root.type != bfd_link_hash_defined
  6561. && fdh->elf.root.type != bfd_link_hash_defweak)
  6562. fdh = NULL;
  6563. }
  6564. }
  6565. skip = (sym_sec->owner != ibfd
  6566. || sym_sec->output_section == bfd_abs_section_ptr);
  6567. if (skip)
  6568. {
  6569. if (fdh != NULL && sym_sec->owner == ibfd)
  6570. {
  6571. /* Arrange for the function descriptor sym
  6572. to be dropped. */
  6573. fdh->elf.root.u.def.value = 0;
  6574. fdh->elf.root.u.def.section = sym_sec;
  6575. }
  6576. opd->adjust[OPD_NDX (rel->r_offset)] = -1;
  6577. if (NO_OPD_RELOCS || bfd_link_relocatable (info))
  6578. rel = next_rel;
  6579. else
  6580. while (1)
  6581. {
  6582. if (!dec_dynrel_count (rel, sec, info,
  6583. NULL, h, sym))
  6584. goto error_ret;
  6585. if (++rel == next_rel)
  6586. break;
  6587. r_symndx = ELF64_R_SYM (rel->r_info);
  6588. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  6589. r_symndx, ibfd))
  6590. goto error_ret;
  6591. }
  6592. }
  6593. else
  6594. {
  6595. /* We'll be keeping this opd entry. */
  6596. long adjust;
  6597. if (fdh != NULL)
  6598. {
  6599. /* Redefine the function descriptor symbol to
  6600. this location in the opd section. It is
  6601. necessary to update the value here rather
  6602. than using an array of adjustments as we do
  6603. for local symbols, because various places
  6604. in the generic ELF code use the value
  6605. stored in u.def.value. */
  6606. fdh->elf.root.u.def.value = wptr - new_contents;
  6607. fdh->adjust_done = 1;
  6608. }
  6609. /* Local syms are a bit tricky. We could
  6610. tweak them as they can be cached, but
  6611. we'd need to look through the local syms
  6612. for the function descriptor sym which we
  6613. don't have at the moment. So keep an
  6614. array of adjustments. */
  6615. adjust = (wptr - new_contents) - (rptr - sec->contents);
  6616. opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
  6617. if (wptr != rptr)
  6618. memcpy (wptr, rptr, opd_ent_size);
  6619. wptr += opd_ent_size;
  6620. if (add_aux_fields && opd_ent_size == 16)
  6621. {
  6622. memset (wptr, '\0', 8);
  6623. wptr += 8;
  6624. }
  6625. /* We need to adjust any reloc offsets to point to the
  6626. new opd entries. */
  6627. for ( ; rel != next_rel; ++rel)
  6628. {
  6629. rel->r_offset += adjust;
  6630. if (write_rel != rel)
  6631. memcpy (write_rel, rel, sizeof (*rel));
  6632. ++write_rel;
  6633. }
  6634. }
  6635. rptr += opd_ent_size;
  6636. }
  6637. sec->size = wptr - new_contents;
  6638. sec->reloc_count = write_rel - relstart;
  6639. if (add_aux_fields)
  6640. {
  6641. free (sec->contents);
  6642. sec->contents = new_contents;
  6643. }
  6644. /* Fudge the header size too, as this is used later in
  6645. elf_bfd_final_link if we are emitting relocs. */
  6646. rel_hdr = _bfd_elf_single_rel_hdr (sec);
  6647. rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
  6648. some_edited = true;
  6649. }
  6650. else if (elf_section_data (sec)->relocs != relstart)
  6651. free (relstart);
  6652. if (local_syms != NULL
  6653. && symtab_hdr->contents != (unsigned char *) local_syms)
  6654. {
  6655. if (!info->keep_memory)
  6656. free (local_syms);
  6657. else
  6658. symtab_hdr->contents = (unsigned char *) local_syms;
  6659. }
  6660. }
  6661. if (some_edited)
  6662. elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
  6663. /* If we are doing a final link and the last .opd entry is just 16 byte
  6664. long, add a 8 byte padding after it. */
  6665. if (need_pad != NULL && !bfd_link_relocatable (info))
  6666. {
  6667. bfd_byte *p;
  6668. if ((need_pad->flags & SEC_IN_MEMORY) == 0)
  6669. {
  6670. BFD_ASSERT (need_pad->size > 0);
  6671. p = bfd_malloc (need_pad->size + 8);
  6672. if (p == NULL)
  6673. return false;
  6674. if (!bfd_get_section_contents (need_pad->owner, need_pad,
  6675. p, 0, need_pad->size))
  6676. return false;
  6677. need_pad->contents = p;
  6678. need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
  6679. }
  6680. else
  6681. {
  6682. p = bfd_realloc (need_pad->contents, need_pad->size + 8);
  6683. if (p == NULL)
  6684. return false;
  6685. need_pad->contents = p;
  6686. }
  6687. memset (need_pad->contents + need_pad->size, 0, 8);
  6688. need_pad->size += 8;
  6689. }
  6690. return true;
  6691. }
  6692. /* Analyze inline PLT call relocations to see whether calls to locally
  6693. defined functions can be converted to direct calls. */
  6694. bool
  6695. ppc64_elf_inline_plt (struct bfd_link_info *info)
  6696. {
  6697. struct ppc_link_hash_table *htab;
  6698. bfd *ibfd;
  6699. asection *sec;
  6700. bfd_vma low_vma, high_vma, limit;
  6701. htab = ppc_hash_table (info);
  6702. if (htab == NULL)
  6703. return false;
  6704. /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
  6705. reduced somewhat to cater for possible stubs that might be added
  6706. between the call and its destination. */
  6707. if (htab->params->group_size < 0)
  6708. {
  6709. limit = -htab->params->group_size;
  6710. if (limit == 1)
  6711. limit = 0x1e00000;
  6712. }
  6713. else
  6714. {
  6715. limit = htab->params->group_size;
  6716. if (limit == 1)
  6717. limit = 0x1c00000;
  6718. }
  6719. low_vma = -1;
  6720. high_vma = 0;
  6721. for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
  6722. if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
  6723. {
  6724. if (low_vma > sec->vma)
  6725. low_vma = sec->vma;
  6726. if (high_vma < sec->vma + sec->size)
  6727. high_vma = sec->vma + sec->size;
  6728. }
  6729. /* If a "bl" can reach anywhere in local code sections, then we can
  6730. convert all inline PLT sequences to direct calls when the symbol
  6731. is local. */
  6732. if (high_vma - low_vma < limit)
  6733. {
  6734. htab->can_convert_all_inline_plt = 1;
  6735. return true;
  6736. }
  6737. /* Otherwise, go looking through relocs for cases where a direct
  6738. call won't reach. Mark the symbol on any such reloc to disable
  6739. the optimization and keep the PLT entry as it seems likely that
  6740. this will be better than creating trampolines. Note that this
  6741. will disable the optimization for all inline PLT calls to a
  6742. particular symbol, not just those that won't reach. The
  6743. difficulty in doing a more precise optimization is that the
  6744. linker needs to make a decision depending on whether a
  6745. particular R_PPC64_PLTCALL insn can be turned into a direct
  6746. call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
  6747. the sequence, and there is nothing that ties those relocs
  6748. together except their symbol. */
  6749. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  6750. {
  6751. Elf_Internal_Shdr *symtab_hdr;
  6752. Elf_Internal_Sym *local_syms;
  6753. if (!is_ppc64_elf (ibfd))
  6754. continue;
  6755. local_syms = NULL;
  6756. symtab_hdr = &elf_symtab_hdr (ibfd);
  6757. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  6758. if (ppc64_elf_section_data (sec)->has_pltcall
  6759. && !bfd_is_abs_section (sec->output_section))
  6760. {
  6761. Elf_Internal_Rela *relstart, *rel, *relend;
  6762. /* Read the relocations. */
  6763. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
  6764. info->keep_memory);
  6765. if (relstart == NULL)
  6766. return false;
  6767. relend = relstart + sec->reloc_count;
  6768. for (rel = relstart; rel < relend; rel++)
  6769. {
  6770. enum elf_ppc64_reloc_type r_type;
  6771. unsigned long r_symndx;
  6772. asection *sym_sec;
  6773. struct elf_link_hash_entry *h;
  6774. Elf_Internal_Sym *sym;
  6775. unsigned char *tls_maskp;
  6776. r_type = ELF64_R_TYPE (rel->r_info);
  6777. if (r_type != R_PPC64_PLTCALL
  6778. && r_type != R_PPC64_PLTCALL_NOTOC)
  6779. continue;
  6780. r_symndx = ELF64_R_SYM (rel->r_info);
  6781. if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
  6782. r_symndx, ibfd))
  6783. {
  6784. if (elf_section_data (sec)->relocs != relstart)
  6785. free (relstart);
  6786. if (symtab_hdr->contents != (bfd_byte *) local_syms)
  6787. free (local_syms);
  6788. return false;
  6789. }
  6790. if (sym_sec != NULL && sym_sec->output_section != NULL)
  6791. {
  6792. bfd_vma from, to;
  6793. if (h != NULL)
  6794. to = h->root.u.def.value;
  6795. else
  6796. to = sym->st_value;
  6797. to += (rel->r_addend
  6798. + sym_sec->output_offset
  6799. + sym_sec->output_section->vma);
  6800. from = (rel->r_offset
  6801. + sec->output_offset
  6802. + sec->output_section->vma);
  6803. if (to - from + limit < 2 * limit
  6804. && !(r_type == R_PPC64_PLTCALL_NOTOC
  6805. && (((h ? h->other : sym->st_other)
  6806. & STO_PPC64_LOCAL_MASK)
  6807. > 1 << STO_PPC64_LOCAL_BIT)))
  6808. *tls_maskp &= ~PLT_KEEP;
  6809. }
  6810. }
  6811. if (elf_section_data (sec)->relocs != relstart)
  6812. free (relstart);
  6813. }
  6814. if (local_syms != NULL
  6815. && symtab_hdr->contents != (unsigned char *) local_syms)
  6816. {
  6817. if (!info->keep_memory)
  6818. free (local_syms);
  6819. else
  6820. symtab_hdr->contents = (unsigned char *) local_syms;
  6821. }
  6822. }
  6823. return true;
  6824. }
  6825. /* Set htab->tls_get_addr and various other info specific to TLS.
  6826. This needs to run before dynamic symbols are processed in
  6827. bfd_elf_size_dynamic_sections. */
  6828. bool
  6829. ppc64_elf_tls_setup (struct bfd_link_info *info)
  6830. {
  6831. struct ppc_link_hash_table *htab;
  6832. struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
  6833. htab = ppc_hash_table (info);
  6834. if (htab == NULL)
  6835. return false;
  6836. /* Move dynamic linking info to the function descriptor sym. */
  6837. if (htab->need_func_desc_adj)
  6838. {
  6839. elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
  6840. htab->need_func_desc_adj = 0;
  6841. }
  6842. if (abiversion (info->output_bfd) == 1)
  6843. htab->opd_abi = 1;
  6844. if (htab->params->no_multi_toc)
  6845. htab->do_multi_toc = 0;
  6846. else if (!htab->do_multi_toc)
  6847. htab->params->no_multi_toc = 1;
  6848. /* Default to --no-plt-localentry, as this option can cause problems
  6849. with symbol interposition. For example, glibc libpthread.so and
  6850. libc.so duplicate many pthread symbols, with a fallback
  6851. implementation in libc.so. In some cases the fallback does more
  6852. work than the pthread implementation. __pthread_condattr_destroy
  6853. is one such symbol: the libpthread.so implementation is
  6854. localentry:0 while the libc.so implementation is localentry:8.
  6855. An app that "cleverly" uses dlopen to only load necessary
  6856. libraries at runtime may omit loading libpthread.so when not
  6857. running multi-threaded, which then results in the libc.so
  6858. fallback symbols being used and ld.so complaining. Now there
  6859. are workarounds in ld (see non_zero_localentry) to detect the
  6860. pthread situation, but that may not be the only case where
  6861. --plt-localentry can cause trouble. */
  6862. if (htab->params->plt_localentry0 < 0)
  6863. htab->params->plt_localentry0 = 0;
  6864. if (htab->params->plt_localentry0 && htab->has_power10_relocs)
  6865. {
  6866. /* The issue is that __glink_PLTresolve saves r2, which is done
  6867. because glibc ld.so _dl_runtime_resolve restores r2 to support
  6868. a glibc plt call optimisation where global entry code is
  6869. skipped on calls that resolve to the same binary. The
  6870. __glink_PLTresolve save of r2 is incompatible with code
  6871. making tail calls, because the tail call might go via the
  6872. resolver and thus overwrite the proper saved r2. */
  6873. _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
  6874. "power10 pc-relative code"));
  6875. htab->params->plt_localentry0 = 0;
  6876. }
  6877. if (htab->params->plt_localentry0
  6878. && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
  6879. false, false, false) == NULL)
  6880. _bfd_error_handler
  6881. (_("warning: --plt-localentry is especially dangerous without "
  6882. "ld.so support to detect ABI violations"));
  6883. tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
  6884. false, false, true);
  6885. htab->tls_get_addr = ppc_elf_hash_entry (tga);
  6886. tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
  6887. false, false, true);
  6888. htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
  6889. desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
  6890. false, false, true);
  6891. htab->tga_desc = ppc_elf_hash_entry (desc);
  6892. desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
  6893. false, false, true);
  6894. htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
  6895. if (htab->params->tls_get_addr_opt)
  6896. {
  6897. struct elf_link_hash_entry *opt, *opt_fd;
  6898. opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
  6899. false, false, true);
  6900. opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
  6901. false, false, true);
  6902. if (opt_fd != NULL
  6903. && (opt_fd->root.type == bfd_link_hash_defined
  6904. || opt_fd->root.type == bfd_link_hash_defweak))
  6905. {
  6906. /* If glibc supports an optimized __tls_get_addr call stub,
  6907. signalled by the presence of __tls_get_addr_opt, and we'll
  6908. be calling __tls_get_addr via a plt call stub, then
  6909. make __tls_get_addr point to __tls_get_addr_opt. */
  6910. if (!(htab->elf.dynamic_sections_created
  6911. && tga_fd != NULL
  6912. && (tga_fd->type == STT_FUNC
  6913. || tga_fd->needs_plt)
  6914. && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
  6915. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
  6916. tga_fd = NULL;
  6917. if (!(htab->elf.dynamic_sections_created
  6918. && desc_fd != NULL
  6919. && (desc_fd->type == STT_FUNC
  6920. || desc_fd->needs_plt)
  6921. && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
  6922. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
  6923. desc_fd = NULL;
  6924. if (tga_fd != NULL || desc_fd != NULL)
  6925. {
  6926. struct plt_entry *ent = NULL;
  6927. if (tga_fd != NULL)
  6928. for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
  6929. if (ent->plt.refcount > 0)
  6930. break;
  6931. if (ent == NULL && desc_fd != NULL)
  6932. for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
  6933. if (ent->plt.refcount > 0)
  6934. break;
  6935. if (ent != NULL)
  6936. {
  6937. if (tga_fd != NULL)
  6938. {
  6939. tga_fd->root.type = bfd_link_hash_indirect;
  6940. tga_fd->root.u.i.link = &opt_fd->root;
  6941. tga_fd->root.u.i.warning = NULL;
  6942. ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
  6943. }
  6944. if (desc_fd != NULL)
  6945. {
  6946. desc_fd->root.type = bfd_link_hash_indirect;
  6947. desc_fd->root.u.i.link = &opt_fd->root;
  6948. desc_fd->root.u.i.warning = NULL;
  6949. ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
  6950. }
  6951. opt_fd->mark = 1;
  6952. if (opt_fd->dynindx != -1)
  6953. {
  6954. /* Use __tls_get_addr_opt in dynamic relocations. */
  6955. opt_fd->dynindx = -1;
  6956. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  6957. opt_fd->dynstr_index);
  6958. if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
  6959. return false;
  6960. }
  6961. if (tga_fd != NULL)
  6962. {
  6963. htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
  6964. tga = elf_hash_entry (htab->tls_get_addr);
  6965. if (opt != NULL && tga != NULL)
  6966. {
  6967. tga->root.type = bfd_link_hash_indirect;
  6968. tga->root.u.i.link = &opt->root;
  6969. tga->root.u.i.warning = NULL;
  6970. ppc64_elf_copy_indirect_symbol (info, opt, tga);
  6971. opt->mark = 1;
  6972. _bfd_elf_link_hash_hide_symbol (info, opt,
  6973. tga->forced_local);
  6974. htab->tls_get_addr = ppc_elf_hash_entry (opt);
  6975. }
  6976. htab->tls_get_addr_fd->oh = htab->tls_get_addr;
  6977. htab->tls_get_addr_fd->is_func_descriptor = 1;
  6978. if (htab->tls_get_addr != NULL)
  6979. {
  6980. htab->tls_get_addr->oh = htab->tls_get_addr_fd;
  6981. htab->tls_get_addr->is_func = 1;
  6982. }
  6983. }
  6984. if (desc_fd != NULL)
  6985. {
  6986. htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
  6987. if (opt != NULL && desc != NULL)
  6988. {
  6989. desc->root.type = bfd_link_hash_indirect;
  6990. desc->root.u.i.link = &opt->root;
  6991. desc->root.u.i.warning = NULL;
  6992. ppc64_elf_copy_indirect_symbol (info, opt, desc);
  6993. opt->mark = 1;
  6994. _bfd_elf_link_hash_hide_symbol (info, opt,
  6995. desc->forced_local);
  6996. htab->tga_desc = ppc_elf_hash_entry (opt);
  6997. }
  6998. htab->tga_desc_fd->oh = htab->tga_desc;
  6999. htab->tga_desc_fd->is_func_descriptor = 1;
  7000. if (htab->tga_desc != NULL)
  7001. {
  7002. htab->tga_desc->oh = htab->tga_desc_fd;
  7003. htab->tga_desc->is_func = 1;
  7004. }
  7005. }
  7006. }
  7007. }
  7008. }
  7009. else if (htab->params->tls_get_addr_opt < 0)
  7010. htab->params->tls_get_addr_opt = 0;
  7011. }
  7012. if (htab->tga_desc_fd != NULL
  7013. && htab->params->tls_get_addr_opt
  7014. && htab->params->no_tls_get_addr_regsave == -1)
  7015. htab->params->no_tls_get_addr_regsave = 0;
  7016. return true;
  7017. }
  7018. /* Return TRUE iff REL is a branch reloc with a global symbol matching
  7019. any of HASH1, HASH2, HASH3, or HASH4. */
  7020. static bool
  7021. branch_reloc_hash_match (bfd *ibfd,
  7022. Elf_Internal_Rela *rel,
  7023. struct ppc_link_hash_entry *hash1,
  7024. struct ppc_link_hash_entry *hash2,
  7025. struct ppc_link_hash_entry *hash3,
  7026. struct ppc_link_hash_entry *hash4)
  7027. {
  7028. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
  7029. enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
  7030. unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
  7031. if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
  7032. {
  7033. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
  7034. struct elf_link_hash_entry *h;
  7035. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  7036. h = elf_follow_link (h);
  7037. if (h == elf_hash_entry (hash1)
  7038. || h == elf_hash_entry (hash2)
  7039. || h == elf_hash_entry (hash3)
  7040. || h == elf_hash_entry (hash4))
  7041. return true;
  7042. }
  7043. return false;
  7044. }
  7045. /* Run through all the TLS relocs looking for optimization
  7046. opportunities. The linker has been hacked (see ppc64elf.em) to do
  7047. a preliminary section layout so that we know the TLS segment
  7048. offsets. We can't optimize earlier because some optimizations need
  7049. to know the tp offset, and we need to optimize before allocating
  7050. dynamic relocations. */
  7051. bool
  7052. ppc64_elf_tls_optimize (struct bfd_link_info *info)
  7053. {
  7054. bfd *ibfd;
  7055. asection *sec;
  7056. struct ppc_link_hash_table *htab;
  7057. unsigned char *toc_ref;
  7058. int pass;
  7059. if (!bfd_link_executable (info))
  7060. return true;
  7061. htab = ppc_hash_table (info);
  7062. if (htab == NULL)
  7063. return false;
  7064. htab->do_tls_opt = 1;
  7065. /* Make two passes over the relocs. On the first pass, mark toc
  7066. entries involved with tls relocs, and check that tls relocs
  7067. involved in setting up a tls_get_addr call are indeed followed by
  7068. such a call. If they are not, we can't do any tls optimization.
  7069. On the second pass twiddle tls_mask flags to notify
  7070. relocate_section that optimization can be done, and adjust got
  7071. and plt refcounts. */
  7072. toc_ref = NULL;
  7073. for (pass = 0; pass < 2; ++pass)
  7074. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  7075. {
  7076. Elf_Internal_Sym *locsyms = NULL;
  7077. asection *toc = bfd_get_section_by_name (ibfd, ".toc");
  7078. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  7079. if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
  7080. {
  7081. Elf_Internal_Rela *relstart, *rel, *relend;
  7082. bool found_tls_get_addr_arg = 0;
  7083. /* Read the relocations. */
  7084. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
  7085. info->keep_memory);
  7086. if (relstart == NULL)
  7087. {
  7088. free (toc_ref);
  7089. return false;
  7090. }
  7091. relend = relstart + sec->reloc_count;
  7092. for (rel = relstart; rel < relend; rel++)
  7093. {
  7094. enum elf_ppc64_reloc_type r_type;
  7095. unsigned long r_symndx;
  7096. struct elf_link_hash_entry *h;
  7097. Elf_Internal_Sym *sym;
  7098. asection *sym_sec;
  7099. unsigned char *tls_mask;
  7100. unsigned int tls_set, tls_clear, tls_type = 0;
  7101. bfd_vma value;
  7102. bool ok_tprel, is_local;
  7103. long toc_ref_index = 0;
  7104. int expecting_tls_get_addr = 0;
  7105. bool ret = false;
  7106. r_symndx = ELF64_R_SYM (rel->r_info);
  7107. if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
  7108. r_symndx, ibfd))
  7109. {
  7110. err_free_rel:
  7111. if (elf_section_data (sec)->relocs != relstart)
  7112. free (relstart);
  7113. free (toc_ref);
  7114. if (elf_symtab_hdr (ibfd).contents
  7115. != (unsigned char *) locsyms)
  7116. free (locsyms);
  7117. return ret;
  7118. }
  7119. if (h != NULL)
  7120. {
  7121. if (h->root.type == bfd_link_hash_defined
  7122. || h->root.type == bfd_link_hash_defweak)
  7123. value = h->root.u.def.value;
  7124. else if (h->root.type == bfd_link_hash_undefweak)
  7125. value = 0;
  7126. else
  7127. {
  7128. found_tls_get_addr_arg = 0;
  7129. continue;
  7130. }
  7131. }
  7132. else
  7133. /* Symbols referenced by TLS relocs must be of type
  7134. STT_TLS. So no need for .opd local sym adjust. */
  7135. value = sym->st_value;
  7136. ok_tprel = false;
  7137. is_local = SYMBOL_REFERENCES_LOCAL (info, h);
  7138. if (is_local)
  7139. {
  7140. if (h != NULL
  7141. && h->root.type == bfd_link_hash_undefweak)
  7142. ok_tprel = true;
  7143. else if (sym_sec != NULL
  7144. && sym_sec->output_section != NULL)
  7145. {
  7146. value += sym_sec->output_offset;
  7147. value += sym_sec->output_section->vma;
  7148. value -= htab->elf.tls_sec->vma + TP_OFFSET;
  7149. /* Note that even though the prefix insns
  7150. allow a 1<<33 offset we use the same test
  7151. as for addis;addi. There may be a mix of
  7152. pcrel and non-pcrel code and the decision
  7153. to optimise is per symbol, not per TLS
  7154. sequence. */
  7155. ok_tprel = value + 0x80008000ULL < 1ULL << 32;
  7156. }
  7157. }
  7158. r_type = ELF64_R_TYPE (rel->r_info);
  7159. /* If this section has old-style __tls_get_addr calls
  7160. without marker relocs, then check that each
  7161. __tls_get_addr call reloc is preceded by a reloc
  7162. that conceivably belongs to the __tls_get_addr arg
  7163. setup insn. If we don't find matching arg setup
  7164. relocs, don't do any tls optimization. */
  7165. if (pass == 0
  7166. && sec->nomark_tls_get_addr
  7167. && h != NULL
  7168. && is_tls_get_addr (h, htab)
  7169. && !found_tls_get_addr_arg
  7170. && is_branch_reloc (r_type))
  7171. {
  7172. info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
  7173. "TLS optimization disabled\n"),
  7174. ibfd, sec, rel->r_offset);
  7175. ret = true;
  7176. goto err_free_rel;
  7177. }
  7178. found_tls_get_addr_arg = 0;
  7179. switch (r_type)
  7180. {
  7181. case R_PPC64_GOT_TLSLD16:
  7182. case R_PPC64_GOT_TLSLD16_LO:
  7183. case R_PPC64_GOT_TLSLD_PCREL34:
  7184. expecting_tls_get_addr = 1;
  7185. found_tls_get_addr_arg = 1;
  7186. /* Fall through. */
  7187. case R_PPC64_GOT_TLSLD16_HI:
  7188. case R_PPC64_GOT_TLSLD16_HA:
  7189. /* These relocs should never be against a symbol
  7190. defined in a shared lib. Leave them alone if
  7191. that turns out to be the case. */
  7192. if (!is_local)
  7193. continue;
  7194. /* LD -> LE */
  7195. tls_set = 0;
  7196. tls_clear = TLS_LD;
  7197. tls_type = TLS_TLS | TLS_LD;
  7198. break;
  7199. case R_PPC64_GOT_TLSGD16:
  7200. case R_PPC64_GOT_TLSGD16_LO:
  7201. case R_PPC64_GOT_TLSGD_PCREL34:
  7202. expecting_tls_get_addr = 1;
  7203. found_tls_get_addr_arg = 1;
  7204. /* Fall through. */
  7205. case R_PPC64_GOT_TLSGD16_HI:
  7206. case R_PPC64_GOT_TLSGD16_HA:
  7207. if (ok_tprel)
  7208. /* GD -> LE */
  7209. tls_set = 0;
  7210. else
  7211. /* GD -> IE */
  7212. tls_set = TLS_TLS | TLS_GDIE;
  7213. tls_clear = TLS_GD;
  7214. tls_type = TLS_TLS | TLS_GD;
  7215. break;
  7216. case R_PPC64_GOT_TPREL_PCREL34:
  7217. case R_PPC64_GOT_TPREL16_DS:
  7218. case R_PPC64_GOT_TPREL16_LO_DS:
  7219. case R_PPC64_GOT_TPREL16_HI:
  7220. case R_PPC64_GOT_TPREL16_HA:
  7221. if (ok_tprel)
  7222. {
  7223. /* IE -> LE */
  7224. tls_set = 0;
  7225. tls_clear = TLS_TPREL;
  7226. tls_type = TLS_TLS | TLS_TPREL;
  7227. break;
  7228. }
  7229. continue;
  7230. case R_PPC64_TLSLD:
  7231. if (!is_local)
  7232. continue;
  7233. /* Fall through. */
  7234. case R_PPC64_TLSGD:
  7235. if (rel + 1 < relend
  7236. && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
  7237. {
  7238. if (pass != 0
  7239. && (ELF64_R_TYPE (rel[1].r_info)
  7240. != R_PPC64_PLTSEQ)
  7241. && (ELF64_R_TYPE (rel[1].r_info)
  7242. != R_PPC64_PLTSEQ_NOTOC))
  7243. {
  7244. r_symndx = ELF64_R_SYM (rel[1].r_info);
  7245. if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
  7246. r_symndx, ibfd))
  7247. goto err_free_rel;
  7248. if (h != NULL)
  7249. {
  7250. struct plt_entry *ent = NULL;
  7251. for (ent = h->plt.plist;
  7252. ent != NULL;
  7253. ent = ent->next)
  7254. if (ent->addend == rel[1].r_addend)
  7255. break;
  7256. if (ent != NULL
  7257. && ent->plt.refcount > 0)
  7258. ent->plt.refcount -= 1;
  7259. }
  7260. }
  7261. continue;
  7262. }
  7263. found_tls_get_addr_arg = 1;
  7264. /* Fall through. */
  7265. case R_PPC64_TLS:
  7266. case R_PPC64_TOC16:
  7267. case R_PPC64_TOC16_LO:
  7268. if (sym_sec == NULL || sym_sec != toc)
  7269. continue;
  7270. /* Mark this toc entry as referenced by a TLS
  7271. code sequence. We can do that now in the
  7272. case of R_PPC64_TLS, and after checking for
  7273. tls_get_addr for the TOC16 relocs. */
  7274. if (toc_ref == NULL)
  7275. toc_ref
  7276. = bfd_zmalloc (toc->output_section->rawsize / 8);
  7277. if (toc_ref == NULL)
  7278. goto err_free_rel;
  7279. if (h != NULL)
  7280. value = h->root.u.def.value;
  7281. else
  7282. value = sym->st_value;
  7283. value += rel->r_addend;
  7284. if (value % 8 != 0)
  7285. continue;
  7286. BFD_ASSERT (value < toc->size
  7287. && toc->output_offset % 8 == 0);
  7288. toc_ref_index = (value + toc->output_offset) / 8;
  7289. if (r_type == R_PPC64_TLS
  7290. || r_type == R_PPC64_TLSGD
  7291. || r_type == R_PPC64_TLSLD)
  7292. {
  7293. toc_ref[toc_ref_index] = 1;
  7294. continue;
  7295. }
  7296. if (pass != 0 && toc_ref[toc_ref_index] == 0)
  7297. continue;
  7298. tls_set = 0;
  7299. tls_clear = 0;
  7300. expecting_tls_get_addr = 2;
  7301. break;
  7302. case R_PPC64_TPREL64:
  7303. if (pass == 0
  7304. || sec != toc
  7305. || toc_ref == NULL
  7306. || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
  7307. continue;
  7308. if (ok_tprel)
  7309. {
  7310. /* IE -> LE */
  7311. tls_set = TLS_EXPLICIT;
  7312. tls_clear = TLS_TPREL;
  7313. break;
  7314. }
  7315. continue;
  7316. case R_PPC64_DTPMOD64:
  7317. if (pass == 0
  7318. || sec != toc
  7319. || toc_ref == NULL
  7320. || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
  7321. continue;
  7322. if (rel + 1 < relend
  7323. && (rel[1].r_info
  7324. == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
  7325. && rel[1].r_offset == rel->r_offset + 8)
  7326. {
  7327. if (ok_tprel)
  7328. /* GD -> LE */
  7329. tls_set = TLS_EXPLICIT | TLS_GD;
  7330. else
  7331. /* GD -> IE */
  7332. tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
  7333. tls_clear = TLS_GD;
  7334. }
  7335. else
  7336. {
  7337. if (!is_local)
  7338. continue;
  7339. /* LD -> LE */
  7340. tls_set = TLS_EXPLICIT;
  7341. tls_clear = TLS_LD;
  7342. }
  7343. break;
  7344. case R_PPC64_TPREL16_HA:
  7345. if (pass == 0)
  7346. {
  7347. unsigned char buf[4];
  7348. unsigned int insn;
  7349. bfd_vma off = rel->r_offset & ~3;
  7350. if (!bfd_get_section_contents (ibfd, sec, buf,
  7351. off, 4))
  7352. goto err_free_rel;
  7353. insn = bfd_get_32 (ibfd, buf);
  7354. /* addis rt,13,imm */
  7355. if ((insn & ((0x3fu << 26) | 0x1f << 16))
  7356. != ((15u << 26) | (13 << 16)))
  7357. {
  7358. /* xgettext:c-format */
  7359. info->callbacks->minfo
  7360. (_("%H: warning: %s unexpected insn %#x.\n"),
  7361. ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
  7362. htab->do_tls_opt = 0;
  7363. }
  7364. }
  7365. continue;
  7366. case R_PPC64_TPREL16_HI:
  7367. case R_PPC64_TPREL16_HIGH:
  7368. case R_PPC64_TPREL16_HIGHA:
  7369. case R_PPC64_TPREL16_HIGHER:
  7370. case R_PPC64_TPREL16_HIGHERA:
  7371. case R_PPC64_TPREL16_HIGHEST:
  7372. case R_PPC64_TPREL16_HIGHESTA:
  7373. /* These can all be used in sequences along with
  7374. TPREL16_LO or TPREL16_LO_DS in ways we aren't
  7375. able to verify easily. */
  7376. htab->do_tls_opt = 0;
  7377. continue;
  7378. default:
  7379. continue;
  7380. }
  7381. if (pass == 0)
  7382. {
  7383. if (!expecting_tls_get_addr
  7384. || !sec->nomark_tls_get_addr)
  7385. continue;
  7386. if (rel + 1 < relend
  7387. && branch_reloc_hash_match (ibfd, rel + 1,
  7388. htab->tls_get_addr_fd,
  7389. htab->tga_desc_fd,
  7390. htab->tls_get_addr,
  7391. htab->tga_desc))
  7392. {
  7393. if (expecting_tls_get_addr == 2)
  7394. {
  7395. /* Check for toc tls entries. */
  7396. unsigned char *toc_tls;
  7397. int retval;
  7398. retval = get_tls_mask (&toc_tls, NULL, NULL,
  7399. &locsyms,
  7400. rel, ibfd);
  7401. if (retval == 0)
  7402. goto err_free_rel;
  7403. if (toc_tls != NULL)
  7404. {
  7405. if ((*toc_tls & TLS_TLS) != 0
  7406. && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
  7407. found_tls_get_addr_arg = 1;
  7408. if (retval > 1)
  7409. toc_ref[toc_ref_index] = 1;
  7410. }
  7411. }
  7412. continue;
  7413. }
  7414. /* Uh oh, we didn't find the expected call. We
  7415. could just mark this symbol to exclude it
  7416. from tls optimization but it's safer to skip
  7417. the entire optimization. */
  7418. /* xgettext:c-format */
  7419. info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
  7420. "TLS optimization disabled\n"),
  7421. ibfd, sec, rel->r_offset);
  7422. ret = true;
  7423. goto err_free_rel;
  7424. }
  7425. /* If we don't have old-style __tls_get_addr calls
  7426. without TLSGD/TLSLD marker relocs, and we haven't
  7427. found a new-style __tls_get_addr call with a
  7428. marker for this symbol, then we either have a
  7429. broken object file or an -mlongcall style
  7430. indirect call to __tls_get_addr without a marker.
  7431. Disable optimization in this case. */
  7432. if ((tls_clear & (TLS_GD | TLS_LD)) != 0
  7433. && (tls_set & TLS_EXPLICIT) == 0
  7434. && !sec->nomark_tls_get_addr
  7435. && ((*tls_mask & (TLS_TLS | TLS_MARK))
  7436. != (TLS_TLS | TLS_MARK)))
  7437. continue;
  7438. if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
  7439. {
  7440. struct plt_entry *ent = NULL;
  7441. if (htab->tls_get_addr_fd != NULL)
  7442. for (ent = htab->tls_get_addr_fd->elf.plt.plist;
  7443. ent != NULL;
  7444. ent = ent->next)
  7445. if (ent->addend == 0)
  7446. break;
  7447. if (ent == NULL && htab->tga_desc_fd != NULL)
  7448. for (ent = htab->tga_desc_fd->elf.plt.plist;
  7449. ent != NULL;
  7450. ent = ent->next)
  7451. if (ent->addend == 0)
  7452. break;
  7453. if (ent == NULL && htab->tls_get_addr != NULL)
  7454. for (ent = htab->tls_get_addr->elf.plt.plist;
  7455. ent != NULL;
  7456. ent = ent->next)
  7457. if (ent->addend == 0)
  7458. break;
  7459. if (ent == NULL && htab->tga_desc != NULL)
  7460. for (ent = htab->tga_desc->elf.plt.plist;
  7461. ent != NULL;
  7462. ent = ent->next)
  7463. if (ent->addend == 0)
  7464. break;
  7465. if (ent != NULL
  7466. && ent->plt.refcount > 0)
  7467. ent->plt.refcount -= 1;
  7468. }
  7469. if (tls_clear == 0)
  7470. continue;
  7471. if ((tls_set & TLS_EXPLICIT) == 0)
  7472. {
  7473. struct got_entry *ent;
  7474. /* Adjust got entry for this reloc. */
  7475. if (h != NULL)
  7476. ent = h->got.glist;
  7477. else
  7478. ent = elf_local_got_ents (ibfd)[r_symndx];
  7479. for (; ent != NULL; ent = ent->next)
  7480. if (ent->addend == rel->r_addend
  7481. && ent->owner == ibfd
  7482. && ent->tls_type == tls_type)
  7483. break;
  7484. if (ent == NULL)
  7485. abort ();
  7486. if (tls_set == 0)
  7487. {
  7488. /* We managed to get rid of a got entry. */
  7489. if (ent->got.refcount > 0)
  7490. ent->got.refcount -= 1;
  7491. }
  7492. }
  7493. else
  7494. {
  7495. /* If we got rid of a DTPMOD/DTPREL reloc pair then
  7496. we'll lose one or two dyn relocs. */
  7497. if (!dec_dynrel_count (rel, sec, info,
  7498. NULL, h, sym))
  7499. return false;
  7500. if (tls_set == (TLS_EXPLICIT | TLS_GD))
  7501. {
  7502. if (!dec_dynrel_count (rel + 1, sec, info,
  7503. NULL, h, sym))
  7504. return false;
  7505. }
  7506. }
  7507. *tls_mask |= tls_set & 0xff;
  7508. *tls_mask &= ~tls_clear;
  7509. }
  7510. if (elf_section_data (sec)->relocs != relstart)
  7511. free (relstart);
  7512. }
  7513. if (locsyms != NULL
  7514. && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
  7515. {
  7516. if (!info->keep_memory)
  7517. free (locsyms);
  7518. else
  7519. elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
  7520. }
  7521. }
  7522. free (toc_ref);
  7523. return true;
  7524. }
  7525. /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
  7526. the values of any global symbols in a toc section that has been
  7527. edited. Globals in toc sections should be a rarity, so this function
  7528. sets a flag if any are found in toc sections other than the one just
  7529. edited, so that further hash table traversals can be avoided. */
  7530. struct adjust_toc_info
  7531. {
  7532. asection *toc;
  7533. unsigned long *skip;
  7534. bool global_toc_syms;
  7535. };
  7536. enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
  7537. static bool
  7538. adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
  7539. {
  7540. struct ppc_link_hash_entry *eh;
  7541. struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
  7542. unsigned long i;
  7543. if (h->root.type != bfd_link_hash_defined
  7544. && h->root.type != bfd_link_hash_defweak)
  7545. return true;
  7546. eh = ppc_elf_hash_entry (h);
  7547. if (eh->adjust_done)
  7548. return true;
  7549. if (eh->elf.root.u.def.section == toc_inf->toc)
  7550. {
  7551. if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
  7552. i = toc_inf->toc->rawsize >> 3;
  7553. else
  7554. i = eh->elf.root.u.def.value >> 3;
  7555. if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
  7556. {
  7557. _bfd_error_handler
  7558. (_("%s defined on removed toc entry"), eh->elf.root.root.string);
  7559. do
  7560. ++i;
  7561. while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
  7562. eh->elf.root.u.def.value = (bfd_vma) i << 3;
  7563. }
  7564. eh->elf.root.u.def.value -= toc_inf->skip[i];
  7565. eh->adjust_done = 1;
  7566. }
  7567. else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
  7568. toc_inf->global_toc_syms = true;
  7569. return true;
  7570. }
  7571. /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
  7572. on a _LO variety toc/got reloc. */
  7573. static bool
  7574. ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
  7575. {
  7576. return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
  7577. || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
  7578. || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
  7579. || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
  7580. || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
  7581. || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
  7582. || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
  7583. || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
  7584. || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
  7585. || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
  7586. || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
  7587. || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
  7588. || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
  7589. || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
  7590. || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
  7591. || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
  7592. || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
  7593. /* Exclude lfqu by testing reloc. If relocs are ever
  7594. defined for the reduced D field in psq_lu then those
  7595. will need testing too. */
  7596. && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
  7597. || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
  7598. && (insn & 1) == 0)
  7599. || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
  7600. || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
  7601. /* Exclude stfqu. psq_stu as above for psq_lu. */
  7602. && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
  7603. || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
  7604. && (insn & 1) == 0));
  7605. }
  7606. /* PCREL_OPT in one instance flags to the linker that a pair of insns:
  7607. pld ra,symbol@got@pcrel
  7608. load/store rt,off(ra)
  7609. or
  7610. pla ra,symbol@pcrel
  7611. load/store rt,off(ra)
  7612. may be translated to
  7613. pload/pstore rt,symbol+off@pcrel
  7614. nop.
  7615. This function returns true if the optimization is possible, placing
  7616. the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
  7617. On entry to this function, the linker has already determined that
  7618. the pld can be replaced with pla: *PINSN1 is that pla insn,
  7619. while *PINSN2 is the second instruction. */
  7620. static bool
  7621. xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
  7622. {
  7623. uint64_t insn1 = *pinsn1;
  7624. uint64_t insn2 = *pinsn2;
  7625. bfd_signed_vma off;
  7626. if ((insn2 & (63ULL << 58)) == 1ULL << 58)
  7627. {
  7628. /* Check that regs match. */
  7629. if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
  7630. return false;
  7631. /* P8LS or PMLS form, non-pcrel. */
  7632. if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
  7633. return false;
  7634. *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
  7635. *pinsn2 = PNOP;
  7636. off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
  7637. *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
  7638. return true;
  7639. }
  7640. insn2 >>= 32;
  7641. /* Check that regs match. */
  7642. if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
  7643. return false;
  7644. switch ((insn2 >> 26) & 63)
  7645. {
  7646. default:
  7647. return false;
  7648. case 32: /* lwz */
  7649. case 34: /* lbz */
  7650. case 36: /* stw */
  7651. case 38: /* stb */
  7652. case 40: /* lhz */
  7653. case 42: /* lha */
  7654. case 44: /* sth */
  7655. case 48: /* lfs */
  7656. case 50: /* lfd */
  7657. case 52: /* stfs */
  7658. case 54: /* stfd */
  7659. /* These are the PMLS cases, where we just need to tack a prefix
  7660. on the insn. */
  7661. insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
  7662. | (insn2 & ((63ULL << 26) | (31ULL << 21))));
  7663. off = insn2 & 0xffff;
  7664. break;
  7665. case 58: /* lwa, ld */
  7666. if ((insn2 & 1) != 0)
  7667. return false;
  7668. insn1 = ((1ULL << 58) | (1ULL << 52)
  7669. | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
  7670. | (insn2 & (31ULL << 21)));
  7671. off = insn2 & 0xfffc;
  7672. break;
  7673. case 57: /* lxsd, lxssp */
  7674. if ((insn2 & 3) < 2)
  7675. return false;
  7676. insn1 = ((1ULL << 58) | (1ULL << 52)
  7677. | ((40ULL | (insn2 & 3)) << 26)
  7678. | (insn2 & (31ULL << 21)));
  7679. off = insn2 & 0xfffc;
  7680. break;
  7681. case 61: /* stxsd, stxssp, lxv, stxv */
  7682. if ((insn2 & 3) == 0)
  7683. return false;
  7684. else if ((insn2 & 3) >= 2)
  7685. {
  7686. insn1 = ((1ULL << 58) | (1ULL << 52)
  7687. | ((44ULL | (insn2 & 3)) << 26)
  7688. | (insn2 & (31ULL << 21)));
  7689. off = insn2 & 0xfffc;
  7690. }
  7691. else
  7692. {
  7693. insn1 = ((1ULL << 58) | (1ULL << 52)
  7694. | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
  7695. | (insn2 & (31ULL << 21)));
  7696. off = insn2 & 0xfff0;
  7697. }
  7698. break;
  7699. case 56: /* lq */
  7700. insn1 = ((1ULL << 58) | (1ULL << 52)
  7701. | (insn2 & ((63ULL << 26) | (31ULL << 21))));
  7702. off = insn2 & 0xffff;
  7703. break;
  7704. case 6: /* lxvp, stxvp */
  7705. if ((insn2 & 0xe) != 0)
  7706. return false;
  7707. insn1 = ((1ULL << 58) | (1ULL << 52)
  7708. | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
  7709. | (insn2 & (31ULL << 21)));
  7710. off = insn2 & 0xfff0;
  7711. break;
  7712. case 62: /* std, stq */
  7713. if ((insn2 & 1) != 0)
  7714. return false;
  7715. insn1 = ((1ULL << 58) | (1ULL << 52)
  7716. | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
  7717. | (insn2 & (31ULL << 21)));
  7718. off = insn2 & 0xfffc;
  7719. break;
  7720. }
  7721. *pinsn1 = insn1;
  7722. *pinsn2 = (uint64_t) NOP << 32;
  7723. *poff = (off ^ 0x8000) - 0x8000;
  7724. return true;
  7725. }
  7726. /* Examine all relocs referencing .toc sections in order to remove
  7727. unused .toc entries. */
  7728. bool
  7729. ppc64_elf_edit_toc (struct bfd_link_info *info)
  7730. {
  7731. bfd *ibfd;
  7732. struct adjust_toc_info toc_inf;
  7733. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  7734. htab->do_toc_opt = 1;
  7735. toc_inf.global_toc_syms = true;
  7736. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  7737. {
  7738. asection *toc, *sec;
  7739. Elf_Internal_Shdr *symtab_hdr;
  7740. Elf_Internal_Sym *local_syms;
  7741. Elf_Internal_Rela *relstart, *rel, *toc_relocs;
  7742. unsigned long *skip, *drop;
  7743. unsigned char *used;
  7744. unsigned char *keep, last, some_unused;
  7745. if (!is_ppc64_elf (ibfd))
  7746. continue;
  7747. toc = bfd_get_section_by_name (ibfd, ".toc");
  7748. if (toc == NULL
  7749. || toc->size == 0
  7750. || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
  7751. || discarded_section (toc))
  7752. continue;
  7753. toc_relocs = NULL;
  7754. local_syms = NULL;
  7755. symtab_hdr = &elf_symtab_hdr (ibfd);
  7756. /* Look at sections dropped from the final link. */
  7757. skip = NULL;
  7758. relstart = NULL;
  7759. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  7760. {
  7761. if (sec->reloc_count == 0
  7762. || !discarded_section (sec)
  7763. || get_opd_info (sec)
  7764. || (sec->flags & SEC_ALLOC) == 0
  7765. || (sec->flags & SEC_DEBUGGING) != 0)
  7766. continue;
  7767. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
  7768. if (relstart == NULL)
  7769. goto error_ret;
  7770. /* Run through the relocs to see which toc entries might be
  7771. unused. */
  7772. for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
  7773. {
  7774. enum elf_ppc64_reloc_type r_type;
  7775. unsigned long r_symndx;
  7776. asection *sym_sec;
  7777. struct elf_link_hash_entry *h;
  7778. Elf_Internal_Sym *sym;
  7779. bfd_vma val;
  7780. r_type = ELF64_R_TYPE (rel->r_info);
  7781. switch (r_type)
  7782. {
  7783. default:
  7784. continue;
  7785. case R_PPC64_TOC16:
  7786. case R_PPC64_TOC16_LO:
  7787. case R_PPC64_TOC16_HI:
  7788. case R_PPC64_TOC16_HA:
  7789. case R_PPC64_TOC16_DS:
  7790. case R_PPC64_TOC16_LO_DS:
  7791. break;
  7792. }
  7793. r_symndx = ELF64_R_SYM (rel->r_info);
  7794. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  7795. r_symndx, ibfd))
  7796. goto error_ret;
  7797. if (sym_sec != toc)
  7798. continue;
  7799. if (h != NULL)
  7800. val = h->root.u.def.value;
  7801. else
  7802. val = sym->st_value;
  7803. val += rel->r_addend;
  7804. if (val >= toc->size)
  7805. continue;
  7806. /* Anything in the toc ought to be aligned to 8 bytes.
  7807. If not, don't mark as unused. */
  7808. if (val & 7)
  7809. continue;
  7810. if (skip == NULL)
  7811. {
  7812. skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
  7813. if (skip == NULL)
  7814. goto error_ret;
  7815. }
  7816. skip[val >> 3] = ref_from_discarded;
  7817. }
  7818. if (elf_section_data (sec)->relocs != relstart)
  7819. free (relstart);
  7820. }
  7821. /* For largetoc loads of address constants, we can convert
  7822. . addis rx,2,addr@got@ha
  7823. . ld ry,addr@got@l(rx)
  7824. to
  7825. . addis rx,2,addr@toc@ha
  7826. . addi ry,rx,addr@toc@l
  7827. when addr is within 2G of the toc pointer. This then means
  7828. that the word storing "addr" in the toc is no longer needed. */
  7829. if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
  7830. && toc->output_section->rawsize < (bfd_vma) 1 << 31
  7831. && toc->reloc_count != 0)
  7832. {
  7833. /* Read toc relocs. */
  7834. toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
  7835. info->keep_memory);
  7836. if (toc_relocs == NULL)
  7837. goto error_ret;
  7838. for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
  7839. {
  7840. enum elf_ppc64_reloc_type r_type;
  7841. unsigned long r_symndx;
  7842. asection *sym_sec;
  7843. struct elf_link_hash_entry *h;
  7844. Elf_Internal_Sym *sym;
  7845. bfd_vma val, addr;
  7846. r_type = ELF64_R_TYPE (rel->r_info);
  7847. if (r_type != R_PPC64_ADDR64)
  7848. continue;
  7849. r_symndx = ELF64_R_SYM (rel->r_info);
  7850. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  7851. r_symndx, ibfd))
  7852. goto error_ret;
  7853. if (sym_sec == NULL
  7854. || sym_sec->output_section == NULL
  7855. || discarded_section (sym_sec))
  7856. continue;
  7857. if (!SYMBOL_REFERENCES_LOCAL (info, h)
  7858. || (bfd_link_pic (info)
  7859. && sym_sec == bfd_abs_section_ptr))
  7860. continue;
  7861. if (h != NULL)
  7862. {
  7863. if (h->type == STT_GNU_IFUNC)
  7864. continue;
  7865. val = h->root.u.def.value;
  7866. }
  7867. else
  7868. {
  7869. if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  7870. continue;
  7871. val = sym->st_value;
  7872. }
  7873. val += rel->r_addend;
  7874. val += sym_sec->output_section->vma + sym_sec->output_offset;
  7875. /* We don't yet know the exact toc pointer value, but we
  7876. know it will be somewhere in the toc section. Don't
  7877. optimize if the difference from any possible toc
  7878. pointer is outside [ff..f80008000, 7fff7fff]. */
  7879. addr = toc->output_section->vma + TOC_BASE_OFF;
  7880. if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
  7881. continue;
  7882. addr = toc->output_section->vma + toc->output_section->rawsize;
  7883. if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
  7884. continue;
  7885. if (skip == NULL)
  7886. {
  7887. skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
  7888. if (skip == NULL)
  7889. goto error_ret;
  7890. }
  7891. skip[rel->r_offset >> 3]
  7892. |= can_optimize | ((rel - toc_relocs) << 2);
  7893. }
  7894. }
  7895. if (skip == NULL)
  7896. continue;
  7897. used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
  7898. if (used == NULL)
  7899. {
  7900. error_ret:
  7901. if (symtab_hdr->contents != (unsigned char *) local_syms)
  7902. free (local_syms);
  7903. if (sec != NULL
  7904. && elf_section_data (sec)->relocs != relstart)
  7905. free (relstart);
  7906. if (elf_section_data (toc)->relocs != toc_relocs)
  7907. free (toc_relocs);
  7908. free (skip);
  7909. return false;
  7910. }
  7911. /* Now check all kept sections that might reference the toc.
  7912. Check the toc itself last. */
  7913. for (sec = (ibfd->sections == toc && toc->next ? toc->next
  7914. : ibfd->sections);
  7915. sec != NULL;
  7916. sec = (sec == toc ? NULL
  7917. : sec->next == NULL ? toc
  7918. : sec->next == toc && toc->next ? toc->next
  7919. : sec->next))
  7920. {
  7921. int repeat;
  7922. if (sec->reloc_count == 0
  7923. || discarded_section (sec)
  7924. || get_opd_info (sec)
  7925. || (sec->flags & SEC_ALLOC) == 0
  7926. || (sec->flags & SEC_DEBUGGING) != 0)
  7927. continue;
  7928. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
  7929. info->keep_memory);
  7930. if (relstart == NULL)
  7931. {
  7932. free (used);
  7933. goto error_ret;
  7934. }
  7935. /* Mark toc entries referenced as used. */
  7936. do
  7937. {
  7938. repeat = 0;
  7939. for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
  7940. {
  7941. enum elf_ppc64_reloc_type r_type;
  7942. unsigned long r_symndx;
  7943. asection *sym_sec;
  7944. struct elf_link_hash_entry *h;
  7945. Elf_Internal_Sym *sym;
  7946. bfd_vma val;
  7947. r_type = ELF64_R_TYPE (rel->r_info);
  7948. switch (r_type)
  7949. {
  7950. case R_PPC64_TOC16:
  7951. case R_PPC64_TOC16_LO:
  7952. case R_PPC64_TOC16_HI:
  7953. case R_PPC64_TOC16_HA:
  7954. case R_PPC64_TOC16_DS:
  7955. case R_PPC64_TOC16_LO_DS:
  7956. /* In case we're taking addresses of toc entries. */
  7957. case R_PPC64_ADDR64:
  7958. break;
  7959. default:
  7960. continue;
  7961. }
  7962. r_symndx = ELF64_R_SYM (rel->r_info);
  7963. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  7964. r_symndx, ibfd))
  7965. {
  7966. free (used);
  7967. goto error_ret;
  7968. }
  7969. if (sym_sec != toc)
  7970. continue;
  7971. if (h != NULL)
  7972. val = h->root.u.def.value;
  7973. else
  7974. val = sym->st_value;
  7975. val += rel->r_addend;
  7976. if (val >= toc->size)
  7977. continue;
  7978. if ((skip[val >> 3] & can_optimize) != 0)
  7979. {
  7980. bfd_vma off;
  7981. unsigned char opc;
  7982. switch (r_type)
  7983. {
  7984. case R_PPC64_TOC16_HA:
  7985. break;
  7986. case R_PPC64_TOC16_LO_DS:
  7987. off = rel->r_offset;
  7988. off += (bfd_big_endian (ibfd) ? -2 : 3);
  7989. if (!bfd_get_section_contents (ibfd, sec, &opc,
  7990. off, 1))
  7991. {
  7992. free (used);
  7993. goto error_ret;
  7994. }
  7995. if ((opc & (0x3f << 2)) == (58u << 2))
  7996. break;
  7997. /* Fall through. */
  7998. default:
  7999. /* Wrong sort of reloc, or not a ld. We may
  8000. as well clear ref_from_discarded too. */
  8001. skip[val >> 3] = 0;
  8002. }
  8003. }
  8004. if (sec != toc)
  8005. used[val >> 3] = 1;
  8006. /* For the toc section, we only mark as used if this
  8007. entry itself isn't unused. */
  8008. else if ((used[rel->r_offset >> 3]
  8009. || !(skip[rel->r_offset >> 3] & ref_from_discarded))
  8010. && !used[val >> 3])
  8011. {
  8012. /* Do all the relocs again, to catch reference
  8013. chains. */
  8014. repeat = 1;
  8015. used[val >> 3] = 1;
  8016. }
  8017. }
  8018. }
  8019. while (repeat);
  8020. if (elf_section_data (sec)->relocs != relstart)
  8021. free (relstart);
  8022. }
  8023. /* Merge the used and skip arrays. Assume that TOC
  8024. doublewords not appearing as either used or unused belong
  8025. to an entry more than one doubleword in size. */
  8026. for (drop = skip, keep = used, last = 0, some_unused = 0;
  8027. drop < skip + (toc->size + 7) / 8;
  8028. ++drop, ++keep)
  8029. {
  8030. if (*keep)
  8031. {
  8032. *drop &= ~ref_from_discarded;
  8033. if ((*drop & can_optimize) != 0)
  8034. some_unused = 1;
  8035. last = 0;
  8036. }
  8037. else if ((*drop & ref_from_discarded) != 0)
  8038. {
  8039. some_unused = 1;
  8040. last = ref_from_discarded;
  8041. }
  8042. else
  8043. *drop = last;
  8044. }
  8045. free (used);
  8046. if (some_unused)
  8047. {
  8048. bfd_byte *contents, *src;
  8049. unsigned long off;
  8050. Elf_Internal_Sym *sym;
  8051. bool local_toc_syms = false;
  8052. /* Shuffle the toc contents, and at the same time convert the
  8053. skip array from booleans into offsets. */
  8054. if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
  8055. goto error_ret;
  8056. elf_section_data (toc)->this_hdr.contents = contents;
  8057. for (src = contents, off = 0, drop = skip;
  8058. src < contents + toc->size;
  8059. src += 8, ++drop)
  8060. {
  8061. if ((*drop & (can_optimize | ref_from_discarded)) != 0)
  8062. off += 8;
  8063. else if (off != 0)
  8064. {
  8065. *drop = off;
  8066. memcpy (src - off, src, 8);
  8067. }
  8068. }
  8069. *drop = off;
  8070. toc->rawsize = toc->size;
  8071. toc->size = src - contents - off;
  8072. /* Adjust addends for relocs against the toc section sym,
  8073. and optimize any accesses we can. */
  8074. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  8075. {
  8076. if (sec->reloc_count == 0
  8077. || discarded_section (sec))
  8078. continue;
  8079. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
  8080. info->keep_memory);
  8081. if (relstart == NULL)
  8082. goto error_ret;
  8083. for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
  8084. {
  8085. enum elf_ppc64_reloc_type r_type;
  8086. unsigned long r_symndx;
  8087. asection *sym_sec;
  8088. struct elf_link_hash_entry *h;
  8089. bfd_vma val;
  8090. r_type = ELF64_R_TYPE (rel->r_info);
  8091. switch (r_type)
  8092. {
  8093. default:
  8094. continue;
  8095. case R_PPC64_TOC16:
  8096. case R_PPC64_TOC16_LO:
  8097. case R_PPC64_TOC16_HI:
  8098. case R_PPC64_TOC16_HA:
  8099. case R_PPC64_TOC16_DS:
  8100. case R_PPC64_TOC16_LO_DS:
  8101. case R_PPC64_ADDR64:
  8102. break;
  8103. }
  8104. r_symndx = ELF64_R_SYM (rel->r_info);
  8105. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  8106. r_symndx, ibfd))
  8107. goto error_ret;
  8108. if (sym_sec != toc)
  8109. continue;
  8110. if (h != NULL)
  8111. val = h->root.u.def.value;
  8112. else
  8113. {
  8114. val = sym->st_value;
  8115. if (val != 0)
  8116. local_toc_syms = true;
  8117. }
  8118. val += rel->r_addend;
  8119. if (val > toc->rawsize)
  8120. val = toc->rawsize;
  8121. else if ((skip[val >> 3] & ref_from_discarded) != 0)
  8122. continue;
  8123. else if ((skip[val >> 3] & can_optimize) != 0)
  8124. {
  8125. Elf_Internal_Rela *tocrel
  8126. = toc_relocs + (skip[val >> 3] >> 2);
  8127. unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
  8128. switch (r_type)
  8129. {
  8130. case R_PPC64_TOC16_HA:
  8131. rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
  8132. break;
  8133. case R_PPC64_TOC16_LO_DS:
  8134. rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
  8135. break;
  8136. default:
  8137. if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
  8138. ppc_howto_init ();
  8139. info->callbacks->einfo
  8140. /* xgettext:c-format */
  8141. (_("%H: %s references "
  8142. "optimized away TOC entry\n"),
  8143. ibfd, sec, rel->r_offset,
  8144. ppc64_elf_howto_table[r_type]->name);
  8145. bfd_set_error (bfd_error_bad_value);
  8146. goto error_ret;
  8147. }
  8148. rel->r_addend = tocrel->r_addend;
  8149. elf_section_data (sec)->relocs = relstart;
  8150. continue;
  8151. }
  8152. if (h != NULL || sym->st_value != 0)
  8153. continue;
  8154. rel->r_addend -= skip[val >> 3];
  8155. elf_section_data (sec)->relocs = relstart;
  8156. }
  8157. if (elf_section_data (sec)->relocs != relstart)
  8158. free (relstart);
  8159. }
  8160. /* We shouldn't have local or global symbols defined in the TOC,
  8161. but handle them anyway. */
  8162. if (local_syms != NULL)
  8163. for (sym = local_syms;
  8164. sym < local_syms + symtab_hdr->sh_info;
  8165. ++sym)
  8166. if (sym->st_value != 0
  8167. && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
  8168. {
  8169. unsigned long i;
  8170. if (sym->st_value > toc->rawsize)
  8171. i = toc->rawsize >> 3;
  8172. else
  8173. i = sym->st_value >> 3;
  8174. if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
  8175. {
  8176. if (local_toc_syms)
  8177. _bfd_error_handler
  8178. (_("%s defined on removed toc entry"),
  8179. bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
  8180. do
  8181. ++i;
  8182. while ((skip[i] & (ref_from_discarded | can_optimize)));
  8183. sym->st_value = (bfd_vma) i << 3;
  8184. }
  8185. sym->st_value -= skip[i];
  8186. symtab_hdr->contents = (unsigned char *) local_syms;
  8187. }
  8188. /* Adjust any global syms defined in this toc input section. */
  8189. if (toc_inf.global_toc_syms)
  8190. {
  8191. toc_inf.toc = toc;
  8192. toc_inf.skip = skip;
  8193. toc_inf.global_toc_syms = false;
  8194. elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
  8195. &toc_inf);
  8196. }
  8197. if (toc->reloc_count != 0)
  8198. {
  8199. Elf_Internal_Shdr *rel_hdr;
  8200. Elf_Internal_Rela *wrel;
  8201. bfd_size_type sz;
  8202. /* Remove unused toc relocs, and adjust those we keep. */
  8203. if (toc_relocs == NULL)
  8204. toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
  8205. info->keep_memory);
  8206. if (toc_relocs == NULL)
  8207. goto error_ret;
  8208. wrel = toc_relocs;
  8209. for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
  8210. if ((skip[rel->r_offset >> 3]
  8211. & (ref_from_discarded | can_optimize)) == 0)
  8212. {
  8213. wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
  8214. wrel->r_info = rel->r_info;
  8215. wrel->r_addend = rel->r_addend;
  8216. ++wrel;
  8217. }
  8218. else if (!dec_dynrel_count (rel, toc, info,
  8219. &local_syms, NULL, NULL))
  8220. goto error_ret;
  8221. elf_section_data (toc)->relocs = toc_relocs;
  8222. toc->reloc_count = wrel - toc_relocs;
  8223. rel_hdr = _bfd_elf_single_rel_hdr (toc);
  8224. sz = rel_hdr->sh_entsize;
  8225. rel_hdr->sh_size = toc->reloc_count * sz;
  8226. }
  8227. }
  8228. else if (elf_section_data (toc)->relocs != toc_relocs)
  8229. free (toc_relocs);
  8230. if (local_syms != NULL
  8231. && symtab_hdr->contents != (unsigned char *) local_syms)
  8232. {
  8233. if (!info->keep_memory)
  8234. free (local_syms);
  8235. else
  8236. symtab_hdr->contents = (unsigned char *) local_syms;
  8237. }
  8238. free (skip);
  8239. }
  8240. /* Look for cases where we can change an indirect GOT access to
  8241. a GOT relative or PC relative access, possibly reducing the
  8242. number of GOT entries. */
  8243. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  8244. {
  8245. asection *sec;
  8246. Elf_Internal_Shdr *symtab_hdr;
  8247. Elf_Internal_Sym *local_syms;
  8248. Elf_Internal_Rela *relstart, *rel;
  8249. bfd_vma got;
  8250. if (!is_ppc64_elf (ibfd))
  8251. continue;
  8252. if (!ppc64_elf_tdata (ibfd)->has_optrel)
  8253. continue;
  8254. sec = ppc64_elf_tdata (ibfd)->got;
  8255. got = 0;
  8256. if (sec != NULL)
  8257. got = sec->output_section->vma + sec->output_offset + 0x8000;
  8258. local_syms = NULL;
  8259. symtab_hdr = &elf_symtab_hdr (ibfd);
  8260. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  8261. {
  8262. if (sec->reloc_count == 0
  8263. || !ppc64_elf_section_data (sec)->has_optrel
  8264. || discarded_section (sec))
  8265. continue;
  8266. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
  8267. info->keep_memory);
  8268. if (relstart == NULL)
  8269. {
  8270. got_error_ret:
  8271. if (symtab_hdr->contents != (unsigned char *) local_syms)
  8272. free (local_syms);
  8273. if (sec != NULL
  8274. && elf_section_data (sec)->relocs != relstart)
  8275. free (relstart);
  8276. return false;
  8277. }
  8278. for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
  8279. {
  8280. enum elf_ppc64_reloc_type r_type;
  8281. unsigned long r_symndx;
  8282. Elf_Internal_Sym *sym;
  8283. asection *sym_sec;
  8284. struct elf_link_hash_entry *h;
  8285. struct got_entry *ent;
  8286. bfd_vma val, pc;
  8287. unsigned char buf[8];
  8288. unsigned int insn;
  8289. enum {no_check, check_lo, check_ha} insn_check;
  8290. r_type = ELF64_R_TYPE (rel->r_info);
  8291. switch (r_type)
  8292. {
  8293. default:
  8294. insn_check = no_check;
  8295. break;
  8296. case R_PPC64_PLT16_HA:
  8297. case R_PPC64_GOT_TLSLD16_HA:
  8298. case R_PPC64_GOT_TLSGD16_HA:
  8299. case R_PPC64_GOT_TPREL16_HA:
  8300. case R_PPC64_GOT_DTPREL16_HA:
  8301. case R_PPC64_GOT16_HA:
  8302. case R_PPC64_TOC16_HA:
  8303. insn_check = check_ha;
  8304. break;
  8305. case R_PPC64_PLT16_LO:
  8306. case R_PPC64_PLT16_LO_DS:
  8307. case R_PPC64_GOT_TLSLD16_LO:
  8308. case R_PPC64_GOT_TLSGD16_LO:
  8309. case R_PPC64_GOT_TPREL16_LO_DS:
  8310. case R_PPC64_GOT_DTPREL16_LO_DS:
  8311. case R_PPC64_GOT16_LO:
  8312. case R_PPC64_GOT16_LO_DS:
  8313. case R_PPC64_TOC16_LO:
  8314. case R_PPC64_TOC16_LO_DS:
  8315. insn_check = check_lo;
  8316. break;
  8317. }
  8318. if (insn_check != no_check)
  8319. {
  8320. bfd_vma off = rel->r_offset & ~3;
  8321. if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
  8322. goto got_error_ret;
  8323. insn = bfd_get_32 (ibfd, buf);
  8324. if (insn_check == check_lo
  8325. ? !ok_lo_toc_insn (insn, r_type)
  8326. : ((insn & ((0x3fu << 26) | 0x1f << 16))
  8327. != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
  8328. {
  8329. char str[12];
  8330. ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
  8331. sprintf (str, "%#08x", insn);
  8332. info->callbacks->einfo
  8333. /* xgettext:c-format */
  8334. (_("%H: got/toc optimization is not supported for"
  8335. " %s instruction\n"),
  8336. ibfd, sec, rel->r_offset & ~3, str);
  8337. continue;
  8338. }
  8339. }
  8340. switch (r_type)
  8341. {
  8342. /* Note that we don't delete GOT entries for
  8343. R_PPC64_GOT16_DS since we'd need a lot more
  8344. analysis. For starters, the preliminary layout is
  8345. before the GOT, PLT, dynamic sections and stubs are
  8346. laid out. Then we'd need to allow for changes in
  8347. distance between sections caused by alignment. */
  8348. default:
  8349. continue;
  8350. case R_PPC64_GOT16_HA:
  8351. case R_PPC64_GOT16_LO_DS:
  8352. case R_PPC64_GOT_PCREL34:
  8353. break;
  8354. }
  8355. r_symndx = ELF64_R_SYM (rel->r_info);
  8356. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  8357. r_symndx, ibfd))
  8358. goto got_error_ret;
  8359. if (sym_sec == NULL
  8360. || sym_sec->output_section == NULL
  8361. || discarded_section (sym_sec))
  8362. continue;
  8363. if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
  8364. continue;
  8365. if (!SYMBOL_REFERENCES_LOCAL (info, h)
  8366. || (bfd_link_pic (info)
  8367. && sym_sec == bfd_abs_section_ptr))
  8368. continue;
  8369. if (h != NULL)
  8370. val = h->root.u.def.value;
  8371. else
  8372. val = sym->st_value;
  8373. val += rel->r_addend;
  8374. val += sym_sec->output_section->vma + sym_sec->output_offset;
  8375. /* Fudge factor to allow for the fact that the preliminary layout
  8376. isn't exact. Reduce limits by this factor. */
  8377. #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
  8378. switch (r_type)
  8379. {
  8380. default:
  8381. continue;
  8382. case R_PPC64_GOT16_HA:
  8383. if (val - got + LIMIT_ADJUST (0x80008000ULL)
  8384. >= LIMIT_ADJUST (0x100000000ULL))
  8385. continue;
  8386. if (!bfd_get_section_contents (ibfd, sec, buf,
  8387. rel->r_offset & ~3, 4))
  8388. goto got_error_ret;
  8389. insn = bfd_get_32 (ibfd, buf);
  8390. if (((insn & ((0x3fu << 26) | 0x1f << 16))
  8391. != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
  8392. continue;
  8393. break;
  8394. case R_PPC64_GOT16_LO_DS:
  8395. if (val - got + LIMIT_ADJUST (0x80008000ULL)
  8396. >= LIMIT_ADJUST (0x100000000ULL))
  8397. continue;
  8398. if (!bfd_get_section_contents (ibfd, sec, buf,
  8399. rel->r_offset & ~3, 4))
  8400. goto got_error_ret;
  8401. insn = bfd_get_32 (ibfd, buf);
  8402. if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
  8403. continue;
  8404. break;
  8405. case R_PPC64_GOT_PCREL34:
  8406. pc = rel->r_offset;
  8407. pc += sec->output_section->vma + sec->output_offset;
  8408. if (val - pc + LIMIT_ADJUST (1ULL << 33)
  8409. >= LIMIT_ADJUST (1ULL << 34))
  8410. continue;
  8411. if (!bfd_get_section_contents (ibfd, sec, buf,
  8412. rel->r_offset & ~3, 8))
  8413. goto got_error_ret;
  8414. insn = bfd_get_32 (ibfd, buf);
  8415. if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
  8416. continue;
  8417. insn = bfd_get_32 (ibfd, buf + 4);
  8418. if ((insn & (0x3fu << 26)) != 57u << 26)
  8419. continue;
  8420. break;
  8421. }
  8422. #undef LIMIT_ADJUST
  8423. if (h != NULL)
  8424. ent = h->got.glist;
  8425. else
  8426. {
  8427. struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
  8428. ent = local_got_ents[r_symndx];
  8429. }
  8430. for (; ent != NULL; ent = ent->next)
  8431. if (ent->addend == rel->r_addend
  8432. && ent->owner == ibfd
  8433. && ent->tls_type == 0)
  8434. break;
  8435. BFD_ASSERT (ent && ent->got.refcount > 0);
  8436. ent->got.refcount -= 1;
  8437. }
  8438. if (elf_section_data (sec)->relocs != relstart)
  8439. free (relstart);
  8440. }
  8441. if (local_syms != NULL
  8442. && symtab_hdr->contents != (unsigned char *) local_syms)
  8443. {
  8444. if (!info->keep_memory)
  8445. free (local_syms);
  8446. else
  8447. symtab_hdr->contents = (unsigned char *) local_syms;
  8448. }
  8449. }
  8450. return true;
  8451. }
  8452. /* Return true iff input section I references the TOC using
  8453. instructions limited to +/-32k offsets. */
  8454. bool
  8455. ppc64_elf_has_small_toc_reloc (asection *i)
  8456. {
  8457. return (is_ppc64_elf (i->owner)
  8458. && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
  8459. }
  8460. /* Allocate space for one GOT entry. */
  8461. static void
  8462. allocate_got (struct elf_link_hash_entry *h,
  8463. struct bfd_link_info *info,
  8464. struct got_entry *gent)
  8465. {
  8466. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  8467. struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
  8468. int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
  8469. ? 16 : 8);
  8470. int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
  8471. ? 2 : 1) * sizeof (Elf64_External_Rela);
  8472. asection *got = ppc64_elf_tdata (gent->owner)->got;
  8473. gent->got.offset = got->size;
  8474. got->size += entsize;
  8475. if (h->type == STT_GNU_IFUNC)
  8476. {
  8477. htab->elf.irelplt->size += rentsize;
  8478. htab->got_reli_size += rentsize;
  8479. }
  8480. else if (((bfd_link_pic (info)
  8481. && (gent->tls_type == 0
  8482. ? !info->enable_dt_relr
  8483. : !(bfd_link_executable (info)
  8484. && SYMBOL_REFERENCES_LOCAL (info, h)))
  8485. && !bfd_is_abs_symbol (&h->root))
  8486. || (htab->elf.dynamic_sections_created
  8487. && h->dynindx != -1
  8488. && !SYMBOL_REFERENCES_LOCAL (info, h)))
  8489. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  8490. {
  8491. asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
  8492. relgot->size += rentsize;
  8493. }
  8494. }
  8495. /* This function merges got entries in the same toc group. */
  8496. static void
  8497. merge_got_entries (struct got_entry **pent)
  8498. {
  8499. struct got_entry *ent, *ent2;
  8500. for (ent = *pent; ent != NULL; ent = ent->next)
  8501. if (!ent->is_indirect)
  8502. for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
  8503. if (!ent2->is_indirect
  8504. && ent2->addend == ent->addend
  8505. && ent2->tls_type == ent->tls_type
  8506. && elf_gp (ent2->owner) == elf_gp (ent->owner))
  8507. {
  8508. ent2->is_indirect = true;
  8509. ent2->got.ent = ent;
  8510. }
  8511. }
  8512. /* If H is undefined, make it dynamic if that makes sense. */
  8513. static bool
  8514. ensure_undef_dynamic (struct bfd_link_info *info,
  8515. struct elf_link_hash_entry *h)
  8516. {
  8517. struct elf_link_hash_table *htab = elf_hash_table (info);
  8518. if (htab->dynamic_sections_created
  8519. && ((info->dynamic_undefined_weak != 0
  8520. && h->root.type == bfd_link_hash_undefweak)
  8521. || h->root.type == bfd_link_hash_undefined)
  8522. && h->dynindx == -1
  8523. && !h->forced_local
  8524. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  8525. return bfd_elf_link_record_dynamic_symbol (info, h);
  8526. return true;
  8527. }
  8528. /* Choose whether to use htab->iplt or htab->pltlocal rather than the
  8529. usual htab->elf.splt section for a PLT entry. */
  8530. static inline
  8531. bool use_local_plt (struct bfd_link_info *info,
  8532. struct elf_link_hash_entry *h)
  8533. {
  8534. return (h == NULL
  8535. || h->dynindx == -1
  8536. || !elf_hash_table (info)->dynamic_sections_created);
  8537. }
  8538. /* Allocate space in .plt, .got and associated reloc sections for
  8539. dynamic relocs. */
  8540. static bool
  8541. allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  8542. {
  8543. struct bfd_link_info *info;
  8544. struct ppc_link_hash_table *htab;
  8545. asection *s;
  8546. struct ppc_link_hash_entry *eh;
  8547. struct got_entry **pgent, *gent;
  8548. if (h->root.type == bfd_link_hash_indirect)
  8549. return true;
  8550. info = (struct bfd_link_info *) inf;
  8551. htab = ppc_hash_table (info);
  8552. if (htab == NULL)
  8553. return false;
  8554. eh = ppc_elf_hash_entry (h);
  8555. /* Run through the TLS GD got entries first if we're changing them
  8556. to TPREL. */
  8557. if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
  8558. for (gent = h->got.glist; gent != NULL; gent = gent->next)
  8559. if (gent->got.refcount > 0
  8560. && (gent->tls_type & TLS_GD) != 0)
  8561. {
  8562. /* This was a GD entry that has been converted to TPREL. If
  8563. there happens to be a TPREL entry we can use that one. */
  8564. struct got_entry *ent;
  8565. for (ent = h->got.glist; ent != NULL; ent = ent->next)
  8566. if (ent->got.refcount > 0
  8567. && (ent->tls_type & TLS_TPREL) != 0
  8568. && ent->addend == gent->addend
  8569. && ent->owner == gent->owner)
  8570. {
  8571. gent->got.refcount = 0;
  8572. break;
  8573. }
  8574. /* If not, then we'll be using our own TPREL entry. */
  8575. if (gent->got.refcount != 0)
  8576. gent->tls_type = TLS_TLS | TLS_TPREL;
  8577. }
  8578. /* Remove any list entry that won't generate a word in the GOT before
  8579. we call merge_got_entries. Otherwise we risk merging to empty
  8580. entries. */
  8581. pgent = &h->got.glist;
  8582. while ((gent = *pgent) != NULL)
  8583. if (gent->got.refcount > 0)
  8584. {
  8585. if ((gent->tls_type & TLS_LD) != 0
  8586. && SYMBOL_REFERENCES_LOCAL (info, h))
  8587. {
  8588. ppc64_tlsld_got (gent->owner)->got.refcount += 1;
  8589. *pgent = gent->next;
  8590. }
  8591. else
  8592. pgent = &gent->next;
  8593. }
  8594. else
  8595. *pgent = gent->next;
  8596. if (!htab->do_multi_toc)
  8597. merge_got_entries (&h->got.glist);
  8598. for (gent = h->got.glist; gent != NULL; gent = gent->next)
  8599. if (!gent->is_indirect)
  8600. {
  8601. /* Ensure we catch all the cases where this symbol should
  8602. be made dynamic. */
  8603. if (!ensure_undef_dynamic (info, h))
  8604. return false;
  8605. if (!is_ppc64_elf (gent->owner))
  8606. abort ();
  8607. allocate_got (h, info, gent);
  8608. }
  8609. /* If no dynamic sections we can't have dynamic relocs, except for
  8610. IFUNCs which are handled even in static executables. */
  8611. if (!htab->elf.dynamic_sections_created
  8612. && h->type != STT_GNU_IFUNC)
  8613. h->dyn_relocs = NULL;
  8614. /* Discard relocs on undefined symbols that must be local. */
  8615. else if (h->root.type == bfd_link_hash_undefined
  8616. && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  8617. h->dyn_relocs = NULL;
  8618. /* Also discard relocs on undefined weak syms with non-default
  8619. visibility, or when dynamic_undefined_weak says so. */
  8620. else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  8621. h->dyn_relocs = NULL;
  8622. if (h->dyn_relocs != NULL)
  8623. {
  8624. struct ppc_dyn_relocs *p, **pp;
  8625. /* In the shared -Bsymbolic case, discard space allocated for
  8626. dynamic pc-relative relocs against symbols which turn out to
  8627. be defined in regular objects. For the normal shared case,
  8628. discard space for relocs that have become local due to symbol
  8629. visibility changes. */
  8630. if (bfd_link_pic (info))
  8631. {
  8632. /* Relocs that use pc_count are those that appear on a call
  8633. insn, or certain REL relocs (see must_be_dyn_reloc) that
  8634. can be generated via assembly. We want calls to
  8635. protected symbols to resolve directly to the function
  8636. rather than going via the plt. If people want function
  8637. pointer comparisons to work as expected then they should
  8638. avoid writing weird assembly. */
  8639. if (SYMBOL_CALLS_LOCAL (info, h))
  8640. {
  8641. for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
  8642. (p = *pp) != NULL;
  8643. )
  8644. {
  8645. p->count -= p->pc_count;
  8646. p->pc_count = 0;
  8647. if (p->count == 0)
  8648. *pp = p->next;
  8649. else
  8650. pp = &p->next;
  8651. }
  8652. }
  8653. if (h->dyn_relocs != NULL)
  8654. {
  8655. /* Ensure we catch all the cases where this symbol
  8656. should be made dynamic. */
  8657. if (!ensure_undef_dynamic (info, h))
  8658. return false;
  8659. }
  8660. }
  8661. /* For a fixed position executable, discard space for
  8662. relocs against symbols which are not dynamic. */
  8663. else if (h->type != STT_GNU_IFUNC)
  8664. {
  8665. if ((h->dynamic_adjusted
  8666. || (h->ref_regular
  8667. && h->root.type == bfd_link_hash_undefweak
  8668. && (info->dynamic_undefined_weak > 0
  8669. || !_bfd_elf_readonly_dynrelocs (h))))
  8670. && !h->def_regular
  8671. && !ELF_COMMON_DEF_P (h))
  8672. {
  8673. /* Ensure we catch all the cases where this symbol
  8674. should be made dynamic. */
  8675. if (!ensure_undef_dynamic (info, h))
  8676. return false;
  8677. /* But if that didn't work out, discard dynamic relocs. */
  8678. if (h->dynindx == -1)
  8679. h->dyn_relocs = NULL;
  8680. }
  8681. else
  8682. h->dyn_relocs = NULL;
  8683. }
  8684. /* Finally, allocate space. */
  8685. for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
  8686. {
  8687. unsigned int count;
  8688. asection *sreloc = elf_section_data (p->sec)->sreloc;
  8689. if (eh->elf.type == STT_GNU_IFUNC)
  8690. sreloc = htab->elf.irelplt;
  8691. count = p->count;
  8692. if (info->enable_dt_relr
  8693. && ((!NO_OPD_RELOCS
  8694. && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
  8695. || (eh->elf.type != STT_GNU_IFUNC
  8696. && SYMBOL_REFERENCES_LOCAL (info, h))))
  8697. count -= p->rel_count;
  8698. sreloc->size += count * sizeof (Elf64_External_Rela);
  8699. }
  8700. }
  8701. /* We might need a PLT entry when the symbol
  8702. a) is dynamic, or
  8703. b) is an ifunc, or
  8704. c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
  8705. d) has plt16 relocs and we are linking statically. */
  8706. if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
  8707. || h->type == STT_GNU_IFUNC
  8708. || (h->needs_plt && h->dynamic_adjusted)
  8709. || (h->needs_plt
  8710. && h->def_regular
  8711. && !htab->elf.dynamic_sections_created
  8712. && !htab->can_convert_all_inline_plt
  8713. && (ppc_elf_hash_entry (h)->tls_mask
  8714. & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
  8715. {
  8716. struct plt_entry *pent;
  8717. bool doneone = false;
  8718. for (pent = h->plt.plist; pent != NULL; pent = pent->next)
  8719. if (pent->plt.refcount > 0)
  8720. {
  8721. if (!ensure_undef_dynamic (info, h))
  8722. return false;
  8723. if (use_local_plt (info, h))
  8724. {
  8725. if (h->type == STT_GNU_IFUNC)
  8726. {
  8727. s = htab->elf.iplt;
  8728. pent->plt.offset = s->size;
  8729. s->size += PLT_ENTRY_SIZE (htab);
  8730. s = htab->elf.irelplt;
  8731. }
  8732. else
  8733. {
  8734. s = htab->pltlocal;
  8735. pent->plt.offset = s->size;
  8736. s->size += LOCAL_PLT_ENTRY_SIZE (htab);
  8737. s = NULL;
  8738. if (bfd_link_pic (info)
  8739. && !(info->enable_dt_relr && !htab->opd_abi))
  8740. s = htab->relpltlocal;
  8741. }
  8742. }
  8743. else
  8744. {
  8745. /* If this is the first .plt entry, make room for the special
  8746. first entry. */
  8747. s = htab->elf.splt;
  8748. if (s->size == 0)
  8749. s->size += PLT_INITIAL_ENTRY_SIZE (htab);
  8750. pent->plt.offset = s->size;
  8751. /* Make room for this entry. */
  8752. s->size += PLT_ENTRY_SIZE (htab);
  8753. /* Make room for the .glink code. */
  8754. s = htab->glink;
  8755. if (s->size == 0)
  8756. s->size += GLINK_PLTRESOLVE_SIZE (htab);
  8757. if (htab->opd_abi)
  8758. {
  8759. /* We need bigger stubs past index 32767. */
  8760. if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
  8761. s->size += 4;
  8762. s->size += 2*4;
  8763. }
  8764. else
  8765. s->size += 4;
  8766. /* We also need to make an entry in the .rela.plt section. */
  8767. s = htab->elf.srelplt;
  8768. }
  8769. if (s != NULL)
  8770. s->size += sizeof (Elf64_External_Rela);
  8771. doneone = true;
  8772. }
  8773. else
  8774. pent->plt.offset = (bfd_vma) -1;
  8775. if (!doneone)
  8776. {
  8777. h->plt.plist = NULL;
  8778. h->needs_plt = 0;
  8779. }
  8780. }
  8781. else
  8782. {
  8783. h->plt.plist = NULL;
  8784. h->needs_plt = 0;
  8785. }
  8786. return true;
  8787. }
  8788. #define PPC_LO(v) ((v) & 0xffff)
  8789. #define PPC_HI(v) (((v) >> 16) & 0xffff)
  8790. #define PPC_HA(v) PPC_HI ((v) + 0x8000)
  8791. #define D34(v) \
  8792. ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
  8793. #define HA34(v) ((v + (1ULL << 33)) >> 34)
  8794. /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
  8795. to set up space for global entry stubs. These are put in glink,
  8796. after the branch table. */
  8797. static bool
  8798. size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
  8799. {
  8800. struct bfd_link_info *info;
  8801. struct ppc_link_hash_table *htab;
  8802. struct plt_entry *pent;
  8803. asection *s, *plt;
  8804. if (h->root.type == bfd_link_hash_indirect)
  8805. return true;
  8806. if (!h->pointer_equality_needed)
  8807. return true;
  8808. if (h->def_regular)
  8809. return true;
  8810. info = inf;
  8811. htab = ppc_hash_table (info);
  8812. if (htab == NULL)
  8813. return false;
  8814. s = htab->global_entry;
  8815. plt = htab->elf.splt;
  8816. for (pent = h->plt.plist; pent != NULL; pent = pent->next)
  8817. if (pent->plt.offset != (bfd_vma) -1
  8818. && pent->addend == 0)
  8819. {
  8820. /* For ELFv2, if this symbol is not defined in a regular file
  8821. and we are not generating a shared library or pie, then we
  8822. need to define the symbol in the executable on a call stub.
  8823. This is to avoid text relocations. */
  8824. bfd_vma off, stub_align, stub_off, stub_size;
  8825. unsigned int align_power;
  8826. stub_size = 16;
  8827. stub_off = s->size;
  8828. if (htab->params->plt_stub_align >= 0)
  8829. align_power = htab->params->plt_stub_align;
  8830. else
  8831. align_power = -htab->params->plt_stub_align;
  8832. /* Setting section alignment is delayed until we know it is
  8833. non-empty. Otherwise the .text output section will be
  8834. aligned at least to plt_stub_align even when no global
  8835. entry stubs are needed. */
  8836. if (s->alignment_power < align_power)
  8837. s->alignment_power = align_power;
  8838. stub_align = (bfd_vma) 1 << align_power;
  8839. if (htab->params->plt_stub_align >= 0
  8840. || ((((stub_off + stub_size - 1) & -stub_align)
  8841. - (stub_off & -stub_align))
  8842. > ((stub_size - 1) & -stub_align)))
  8843. stub_off = (stub_off + stub_align - 1) & -stub_align;
  8844. off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
  8845. off -= stub_off + s->output_offset + s->output_section->vma;
  8846. /* Note that for --plt-stub-align negative we have a possible
  8847. dependency between stub offset and size. Break that
  8848. dependency by assuming the max stub size when calculating
  8849. the stub offset. */
  8850. if (PPC_HA (off) == 0)
  8851. stub_size -= 4;
  8852. h->root.type = bfd_link_hash_defined;
  8853. h->root.u.def.section = s;
  8854. h->root.u.def.value = stub_off;
  8855. s->size = stub_off + stub_size;
  8856. break;
  8857. }
  8858. return true;
  8859. }
  8860. /* Set the sizes of the dynamic sections. */
  8861. static bool
  8862. ppc64_elf_size_dynamic_sections (bfd *output_bfd,
  8863. struct bfd_link_info *info)
  8864. {
  8865. struct ppc_link_hash_table *htab;
  8866. bfd *dynobj;
  8867. asection *s;
  8868. bool relocs;
  8869. bfd *ibfd;
  8870. struct got_entry *first_tlsld;
  8871. htab = ppc_hash_table (info);
  8872. if (htab == NULL)
  8873. return false;
  8874. dynobj = htab->elf.dynobj;
  8875. if (dynobj == NULL)
  8876. abort ();
  8877. if (htab->elf.dynamic_sections_created)
  8878. {
  8879. /* Set the contents of the .interp section to the interpreter. */
  8880. if (bfd_link_executable (info) && !info->nointerp)
  8881. {
  8882. s = bfd_get_linker_section (dynobj, ".interp");
  8883. if (s == NULL)
  8884. abort ();
  8885. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  8886. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  8887. }
  8888. }
  8889. /* Set up .got offsets for local syms, and space for local dynamic
  8890. relocs. */
  8891. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  8892. {
  8893. struct got_entry **lgot_ents;
  8894. struct got_entry **end_lgot_ents;
  8895. struct plt_entry **local_plt;
  8896. struct plt_entry **end_local_plt;
  8897. unsigned char *lgot_masks;
  8898. bfd_size_type locsymcount;
  8899. Elf_Internal_Shdr *symtab_hdr;
  8900. Elf_Internal_Sym *local_syms;
  8901. Elf_Internal_Sym *isym;
  8902. if (!is_ppc64_elf (ibfd))
  8903. continue;
  8904. for (s = ibfd->sections; s != NULL; s = s->next)
  8905. {
  8906. struct ppc_local_dyn_relocs *p;
  8907. for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
  8908. {
  8909. if (!bfd_is_abs_section (p->sec)
  8910. && bfd_is_abs_section (p->sec->output_section))
  8911. {
  8912. /* Input section has been discarded, either because
  8913. it is a copy of a linkonce section or due to
  8914. linker script /DISCARD/, so we'll be discarding
  8915. the relocs too. */
  8916. }
  8917. else if (p->count != 0)
  8918. {
  8919. unsigned int count;
  8920. asection *srel;
  8921. count = p->count;
  8922. if (info->enable_dt_relr
  8923. && ((!NO_OPD_RELOCS
  8924. && (ppc64_elf_section_data (p->sec)->sec_type
  8925. == sec_opd))
  8926. || !p->ifunc))
  8927. count -= p->rel_count;
  8928. srel = elf_section_data (p->sec)->sreloc;
  8929. if (p->ifunc)
  8930. srel = htab->elf.irelplt;
  8931. srel->size += count * sizeof (Elf64_External_Rela);
  8932. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  8933. info->flags |= DF_TEXTREL;
  8934. }
  8935. }
  8936. }
  8937. lgot_ents = elf_local_got_ents (ibfd);
  8938. if (!lgot_ents)
  8939. continue;
  8940. symtab_hdr = &elf_symtab_hdr (ibfd);
  8941. locsymcount = symtab_hdr->sh_info;
  8942. end_lgot_ents = lgot_ents + locsymcount;
  8943. local_plt = (struct plt_entry **) end_lgot_ents;
  8944. end_local_plt = local_plt + locsymcount;
  8945. lgot_masks = (unsigned char *) end_local_plt;
  8946. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  8947. if (local_syms == NULL && locsymcount != 0)
  8948. {
  8949. local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
  8950. 0, NULL, NULL, NULL);
  8951. if (local_syms == NULL)
  8952. return false;
  8953. }
  8954. s = ppc64_elf_tdata (ibfd)->got;
  8955. for (isym = local_syms;
  8956. lgot_ents < end_lgot_ents;
  8957. ++lgot_ents, ++lgot_masks, isym++)
  8958. {
  8959. struct got_entry **pent, *ent;
  8960. pent = lgot_ents;
  8961. while ((ent = *pent) != NULL)
  8962. if (ent->got.refcount > 0)
  8963. {
  8964. if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
  8965. {
  8966. ppc64_tlsld_got (ibfd)->got.refcount += 1;
  8967. *pent = ent->next;
  8968. }
  8969. else
  8970. {
  8971. unsigned int ent_size = 8;
  8972. unsigned int rel_size = sizeof (Elf64_External_Rela);
  8973. ent->got.offset = s->size;
  8974. if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
  8975. {
  8976. ent_size *= 2;
  8977. rel_size *= 2;
  8978. }
  8979. s->size += ent_size;
  8980. if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
  8981. {
  8982. htab->elf.irelplt->size += rel_size;
  8983. htab->got_reli_size += rel_size;
  8984. }
  8985. else if (bfd_link_pic (info)
  8986. && (ent->tls_type == 0
  8987. ? !info->enable_dt_relr
  8988. : !bfd_link_executable (info))
  8989. && isym->st_shndx != SHN_ABS)
  8990. {
  8991. asection *srel = ppc64_elf_tdata (ibfd)->relgot;
  8992. srel->size += rel_size;
  8993. }
  8994. pent = &ent->next;
  8995. }
  8996. }
  8997. else
  8998. *pent = ent->next;
  8999. }
  9000. if (local_syms != NULL
  9001. && symtab_hdr->contents != (unsigned char *) local_syms)
  9002. {
  9003. if (!info->keep_memory)
  9004. free (local_syms);
  9005. else
  9006. symtab_hdr->contents = (unsigned char *) local_syms;
  9007. }
  9008. /* Allocate space for plt calls to local syms. */
  9009. lgot_masks = (unsigned char *) end_local_plt;
  9010. for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
  9011. {
  9012. struct plt_entry *ent;
  9013. for (ent = *local_plt; ent != NULL; ent = ent->next)
  9014. if (ent->plt.refcount > 0)
  9015. {
  9016. if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
  9017. {
  9018. s = htab->elf.iplt;
  9019. ent->plt.offset = s->size;
  9020. s->size += PLT_ENTRY_SIZE (htab);
  9021. htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
  9022. }
  9023. else if (htab->can_convert_all_inline_plt
  9024. || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
  9025. ent->plt.offset = (bfd_vma) -1;
  9026. else
  9027. {
  9028. s = htab->pltlocal;
  9029. ent->plt.offset = s->size;
  9030. s->size += LOCAL_PLT_ENTRY_SIZE (htab);
  9031. if (bfd_link_pic (info)
  9032. && !(info->enable_dt_relr && !htab->opd_abi))
  9033. htab->relpltlocal->size += sizeof (Elf64_External_Rela);
  9034. }
  9035. }
  9036. else
  9037. ent->plt.offset = (bfd_vma) -1;
  9038. }
  9039. }
  9040. /* Allocate global sym .plt and .got entries, and space for global
  9041. sym dynamic relocs. */
  9042. elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
  9043. if (!htab->opd_abi && !bfd_link_pic (info))
  9044. elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
  9045. first_tlsld = NULL;
  9046. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  9047. {
  9048. struct got_entry *ent;
  9049. if (!is_ppc64_elf (ibfd))
  9050. continue;
  9051. ent = ppc64_tlsld_got (ibfd);
  9052. if (ent->got.refcount > 0)
  9053. {
  9054. if (!htab->do_multi_toc && first_tlsld != NULL)
  9055. {
  9056. ent->is_indirect = true;
  9057. ent->got.ent = first_tlsld;
  9058. }
  9059. else
  9060. {
  9061. if (first_tlsld == NULL)
  9062. first_tlsld = ent;
  9063. s = ppc64_elf_tdata (ibfd)->got;
  9064. ent->got.offset = s->size;
  9065. ent->owner = ibfd;
  9066. s->size += 16;
  9067. if (bfd_link_dll (info))
  9068. {
  9069. asection *srel = ppc64_elf_tdata (ibfd)->relgot;
  9070. srel->size += sizeof (Elf64_External_Rela);
  9071. }
  9072. }
  9073. }
  9074. else
  9075. ent->got.offset = (bfd_vma) -1;
  9076. }
  9077. /* We now have determined the sizes of the various dynamic sections.
  9078. Allocate memory for them. */
  9079. relocs = false;
  9080. for (s = dynobj->sections; s != NULL; s = s->next)
  9081. {
  9082. if ((s->flags & SEC_LINKER_CREATED) == 0)
  9083. continue;
  9084. if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
  9085. /* These haven't been allocated yet; don't strip. */
  9086. continue;
  9087. else if (s == htab->elf.sgot
  9088. || s == htab->elf.splt
  9089. || s == htab->elf.iplt
  9090. || s == htab->pltlocal
  9091. || s == htab->glink
  9092. || s == htab->global_entry
  9093. || s == htab->elf.sdynbss
  9094. || s == htab->elf.sdynrelro)
  9095. {
  9096. /* Strip this section if we don't need it; see the
  9097. comment below. */
  9098. }
  9099. else if (s == htab->glink_eh_frame)
  9100. {
  9101. if (!bfd_is_abs_section (s->output_section))
  9102. /* Not sized yet. */
  9103. continue;
  9104. }
  9105. else if (startswith (s->name, ".rela"))
  9106. {
  9107. if (s->size != 0)
  9108. {
  9109. if (s != htab->elf.srelplt)
  9110. relocs = true;
  9111. /* We use the reloc_count field as a counter if we need
  9112. to copy relocs into the output file. */
  9113. s->reloc_count = 0;
  9114. }
  9115. }
  9116. else
  9117. {
  9118. /* It's not one of our sections, so don't allocate space. */
  9119. continue;
  9120. }
  9121. if (s->size == 0)
  9122. {
  9123. /* If we don't need this section, strip it from the
  9124. output file. This is mostly to handle .rela.bss and
  9125. .rela.plt. We must create both sections in
  9126. create_dynamic_sections, because they must be created
  9127. before the linker maps input sections to output
  9128. sections. The linker does that before
  9129. adjust_dynamic_symbol is called, and it is that
  9130. function which decides whether anything needs to go
  9131. into these sections. */
  9132. s->flags |= SEC_EXCLUDE;
  9133. continue;
  9134. }
  9135. if (bfd_is_abs_section (s->output_section))
  9136. _bfd_error_handler (_("warning: discarding dynamic section %s"),
  9137. s->name);
  9138. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  9139. continue;
  9140. /* Allocate memory for the section contents. We use bfd_zalloc
  9141. here in case unused entries are not reclaimed before the
  9142. section's contents are written out. This should not happen,
  9143. but this way if it does we get a R_PPC64_NONE reloc in .rela
  9144. sections instead of garbage.
  9145. We also rely on the section contents being zero when writing
  9146. the GOT and .dynrelro. */
  9147. s->contents = bfd_zalloc (dynobj, s->size);
  9148. if (s->contents == NULL)
  9149. return false;
  9150. }
  9151. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  9152. {
  9153. if (!is_ppc64_elf (ibfd))
  9154. continue;
  9155. s = ppc64_elf_tdata (ibfd)->got;
  9156. if (s != NULL && s != htab->elf.sgot)
  9157. {
  9158. if (s->size == 0)
  9159. s->flags |= SEC_EXCLUDE;
  9160. else
  9161. {
  9162. s->contents = bfd_zalloc (ibfd, s->size);
  9163. if (s->contents == NULL)
  9164. return false;
  9165. }
  9166. }
  9167. s = ppc64_elf_tdata (ibfd)->relgot;
  9168. if (s != NULL)
  9169. {
  9170. if (s->size == 0)
  9171. s->flags |= SEC_EXCLUDE;
  9172. else
  9173. {
  9174. s->contents = bfd_zalloc (ibfd, s->size);
  9175. if (s->contents == NULL)
  9176. return false;
  9177. relocs = true;
  9178. s->reloc_count = 0;
  9179. }
  9180. }
  9181. }
  9182. if (htab->elf.dynamic_sections_created)
  9183. {
  9184. bool tls_opt;
  9185. /* Add some entries to the .dynamic section. We fill in the
  9186. values later, in ppc64_elf_finish_dynamic_sections, but we
  9187. must add the entries now so that we get the correct size for
  9188. the .dynamic section. The DT_DEBUG entry is filled in by the
  9189. dynamic linker and used by the debugger. */
  9190. #define add_dynamic_entry(TAG, VAL) \
  9191. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  9192. if (bfd_link_executable (info))
  9193. {
  9194. if (!add_dynamic_entry (DT_DEBUG, 0))
  9195. return false;
  9196. }
  9197. if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
  9198. {
  9199. if (!add_dynamic_entry (DT_PLTGOT, 0)
  9200. || !add_dynamic_entry (DT_PLTRELSZ, 0)
  9201. || !add_dynamic_entry (DT_PLTREL, DT_RELA)
  9202. || !add_dynamic_entry (DT_JMPREL, 0)
  9203. || !add_dynamic_entry (DT_PPC64_GLINK, 0))
  9204. return false;
  9205. }
  9206. if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
  9207. {
  9208. if (!add_dynamic_entry (DT_PPC64_OPD, 0)
  9209. || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
  9210. return false;
  9211. }
  9212. tls_opt = (htab->params->tls_get_addr_opt
  9213. && ((htab->tls_get_addr_fd != NULL
  9214. && htab->tls_get_addr_fd->elf.plt.plist != NULL)
  9215. || (htab->tga_desc_fd != NULL
  9216. && htab->tga_desc_fd->elf.plt.plist != NULL)));
  9217. if (tls_opt || !htab->opd_abi)
  9218. {
  9219. if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
  9220. return false;
  9221. }
  9222. if (relocs)
  9223. {
  9224. if (!add_dynamic_entry (DT_RELA, 0)
  9225. || !add_dynamic_entry (DT_RELASZ, 0)
  9226. || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
  9227. return false;
  9228. /* If any dynamic relocs apply to a read-only section,
  9229. then we need a DT_TEXTREL entry. */
  9230. if ((info->flags & DF_TEXTREL) == 0)
  9231. elf_link_hash_traverse (&htab->elf,
  9232. _bfd_elf_maybe_set_textrel, info);
  9233. if ((info->flags & DF_TEXTREL) != 0)
  9234. {
  9235. if (!add_dynamic_entry (DT_TEXTREL, 0))
  9236. return false;
  9237. }
  9238. }
  9239. }
  9240. #undef add_dynamic_entry
  9241. return true;
  9242. }
  9243. /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
  9244. static bool
  9245. ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
  9246. {
  9247. if (h->plt.plist != NULL
  9248. && !h->def_regular
  9249. && !h->pointer_equality_needed)
  9250. return false;
  9251. return _bfd_elf_hash_symbol (h);
  9252. }
  9253. /* Determine the type of stub needed, if any, for a call. */
  9254. static inline enum ppc_stub_main_type
  9255. ppc_type_of_stub (asection *input_sec,
  9256. const Elf_Internal_Rela *rel,
  9257. struct ppc_link_hash_entry **hash,
  9258. struct plt_entry **plt_ent,
  9259. bfd_vma destination,
  9260. unsigned long local_off)
  9261. {
  9262. struct ppc_link_hash_entry *h = *hash;
  9263. bfd_vma location;
  9264. bfd_vma branch_offset;
  9265. bfd_vma max_branch_offset;
  9266. enum elf_ppc64_reloc_type r_type;
  9267. if (h != NULL)
  9268. {
  9269. struct plt_entry *ent;
  9270. struct ppc_link_hash_entry *fdh = h;
  9271. if (h->oh != NULL
  9272. && h->oh->is_func_descriptor)
  9273. {
  9274. fdh = ppc_follow_link (h->oh);
  9275. *hash = fdh;
  9276. }
  9277. for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
  9278. if (ent->addend == rel->r_addend
  9279. && ent->plt.offset != (bfd_vma) -1)
  9280. {
  9281. *plt_ent = ent;
  9282. return ppc_stub_plt_call;
  9283. }
  9284. /* Here, we know we don't have a plt entry. If we don't have a
  9285. either a defined function descriptor or a defined entry symbol
  9286. in a regular object file, then it is pointless trying to make
  9287. any other type of stub. */
  9288. if (!is_static_defined (&fdh->elf)
  9289. && !is_static_defined (&h->elf))
  9290. return ppc_stub_none;
  9291. }
  9292. else if (elf_local_got_ents (input_sec->owner) != NULL)
  9293. {
  9294. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
  9295. struct plt_entry **local_plt = (struct plt_entry **)
  9296. elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
  9297. unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
  9298. if (local_plt[r_symndx] != NULL)
  9299. {
  9300. struct plt_entry *ent;
  9301. for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
  9302. if (ent->addend == rel->r_addend
  9303. && ent->plt.offset != (bfd_vma) -1)
  9304. {
  9305. *plt_ent = ent;
  9306. return ppc_stub_plt_call;
  9307. }
  9308. }
  9309. }
  9310. /* Determine where the call point is. */
  9311. location = (input_sec->output_offset
  9312. + input_sec->output_section->vma
  9313. + rel->r_offset);
  9314. branch_offset = destination - location;
  9315. r_type = ELF64_R_TYPE (rel->r_info);
  9316. /* Determine if a long branch stub is needed. */
  9317. max_branch_offset = 1 << 25;
  9318. if (r_type == R_PPC64_REL14
  9319. || r_type == R_PPC64_REL14_BRTAKEN
  9320. || r_type == R_PPC64_REL14_BRNTAKEN)
  9321. max_branch_offset = 1 << 15;
  9322. if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
  9323. /* We need a stub. Figure out whether a long_branch or plt_branch
  9324. is needed later. */
  9325. return ppc_stub_long_branch;
  9326. return ppc_stub_none;
  9327. }
  9328. /* Gets the address of a label (1:) in r11 and builds an offset in r12,
  9329. then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
  9330. . mflr %r12
  9331. . bcl 20,31,1f
  9332. .1: mflr %r11
  9333. . mtlr %r12
  9334. . lis %r12,xxx-1b@highest
  9335. . ori %r12,%r12,xxx-1b@higher
  9336. . sldi %r12,%r12,32
  9337. . oris %r12,%r12,xxx-1b@high
  9338. . ori %r12,%r12,xxx-1b@l
  9339. . add/ldx %r12,%r11,%r12 */
  9340. static bfd_byte *
  9341. build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
  9342. {
  9343. bfd_put_32 (abfd, MFLR_R12, p);
  9344. p += 4;
  9345. bfd_put_32 (abfd, BCL_20_31, p);
  9346. p += 4;
  9347. bfd_put_32 (abfd, MFLR_R11, p);
  9348. p += 4;
  9349. bfd_put_32 (abfd, MTLR_R12, p);
  9350. p += 4;
  9351. if (off + 0x8000 < 0x10000)
  9352. {
  9353. if (load)
  9354. bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
  9355. else
  9356. bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
  9357. p += 4;
  9358. }
  9359. else if (off + 0x80008000ULL < 0x100000000ULL)
  9360. {
  9361. bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
  9362. p += 4;
  9363. if (load)
  9364. bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
  9365. else
  9366. bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
  9367. p += 4;
  9368. }
  9369. else
  9370. {
  9371. if (off + 0x800000000000ULL < 0x1000000000000ULL)
  9372. {
  9373. bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
  9374. p += 4;
  9375. }
  9376. else
  9377. {
  9378. bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
  9379. p += 4;
  9380. if (((off >> 32) & 0xffff) != 0)
  9381. {
  9382. bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
  9383. p += 4;
  9384. }
  9385. }
  9386. if (((off >> 32) & 0xffffffffULL) != 0)
  9387. {
  9388. bfd_put_32 (abfd, SLDI_R12_R12_32, p);
  9389. p += 4;
  9390. }
  9391. if (PPC_HI (off) != 0)
  9392. {
  9393. bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
  9394. p += 4;
  9395. }
  9396. if (PPC_LO (off) != 0)
  9397. {
  9398. bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
  9399. p += 4;
  9400. }
  9401. if (load)
  9402. bfd_put_32 (abfd, LDX_R12_R11_R12, p);
  9403. else
  9404. bfd_put_32 (abfd, ADD_R12_R11_R12, p);
  9405. p += 4;
  9406. }
  9407. return p;
  9408. }
  9409. static unsigned int
  9410. size_offset (bfd_vma off)
  9411. {
  9412. unsigned int size;
  9413. if (off + 0x8000 < 0x10000)
  9414. size = 4;
  9415. else if (off + 0x80008000ULL < 0x100000000ULL)
  9416. size = 8;
  9417. else
  9418. {
  9419. if (off + 0x800000000000ULL < 0x1000000000000ULL)
  9420. size = 4;
  9421. else
  9422. {
  9423. size = 4;
  9424. if (((off >> 32) & 0xffff) != 0)
  9425. size += 4;
  9426. }
  9427. if (((off >> 32) & 0xffffffffULL) != 0)
  9428. size += 4;
  9429. if (PPC_HI (off) != 0)
  9430. size += 4;
  9431. if (PPC_LO (off) != 0)
  9432. size += 4;
  9433. size += 4;
  9434. }
  9435. return size + 16;
  9436. }
  9437. static unsigned int
  9438. num_relocs_for_offset (bfd_vma off)
  9439. {
  9440. unsigned int num_rel;
  9441. if (off + 0x8000 < 0x10000)
  9442. num_rel = 1;
  9443. else if (off + 0x80008000ULL < 0x100000000ULL)
  9444. num_rel = 2;
  9445. else
  9446. {
  9447. num_rel = 1;
  9448. if (off + 0x800000000000ULL >= 0x1000000000000ULL
  9449. && ((off >> 32) & 0xffff) != 0)
  9450. num_rel += 1;
  9451. if (PPC_HI (off) != 0)
  9452. num_rel += 1;
  9453. if (PPC_LO (off) != 0)
  9454. num_rel += 1;
  9455. }
  9456. return num_rel;
  9457. }
  9458. static Elf_Internal_Rela *
  9459. emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
  9460. bfd_vma roff, bfd_vma targ, bfd_vma off)
  9461. {
  9462. bfd_vma relative_targ = targ - (roff - 8);
  9463. if (bfd_big_endian (info->output_bfd))
  9464. roff += 2;
  9465. r->r_offset = roff;
  9466. r->r_addend = relative_targ + roff;
  9467. if (off + 0x8000 < 0x10000)
  9468. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
  9469. else if (off + 0x80008000ULL < 0x100000000ULL)
  9470. {
  9471. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
  9472. ++r;
  9473. roff += 4;
  9474. r->r_offset = roff;
  9475. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
  9476. r->r_addend = relative_targ + roff;
  9477. }
  9478. else
  9479. {
  9480. if (off + 0x800000000000ULL < 0x1000000000000ULL)
  9481. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
  9482. else
  9483. {
  9484. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
  9485. if (((off >> 32) & 0xffff) != 0)
  9486. {
  9487. ++r;
  9488. roff += 4;
  9489. r->r_offset = roff;
  9490. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
  9491. r->r_addend = relative_targ + roff;
  9492. }
  9493. }
  9494. if (((off >> 32) & 0xffffffffULL) != 0)
  9495. roff += 4;
  9496. if (PPC_HI (off) != 0)
  9497. {
  9498. ++r;
  9499. roff += 4;
  9500. r->r_offset = roff;
  9501. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
  9502. r->r_addend = relative_targ + roff;
  9503. }
  9504. if (PPC_LO (off) != 0)
  9505. {
  9506. ++r;
  9507. roff += 4;
  9508. r->r_offset = roff;
  9509. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
  9510. r->r_addend = relative_targ + roff;
  9511. }
  9512. }
  9513. return r;
  9514. }
  9515. static bfd_byte *
  9516. build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
  9517. bool load)
  9518. {
  9519. uint64_t insn;
  9520. if (off - odd + (1ULL << 33) < 1ULL << 34)
  9521. {
  9522. off -= odd;
  9523. if (odd)
  9524. {
  9525. bfd_put_32 (abfd, NOP, p);
  9526. p += 4;
  9527. }
  9528. if (load)
  9529. insn = PLD_R12_PC;
  9530. else
  9531. insn = PADDI_R12_PC;
  9532. insn |= D34 (off);
  9533. bfd_put_32 (abfd, insn >> 32, p);
  9534. p += 4;
  9535. bfd_put_32 (abfd, insn, p);
  9536. }
  9537. /* The minimum value for paddi is -0x200000000. The minimum value
  9538. for li is -0x8000, which when shifted by 34 and added gives a
  9539. minimum value of -0x2000200000000. The maximum value is
  9540. 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
  9541. else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
  9542. {
  9543. off -= 8 - odd;
  9544. bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
  9545. p += 4;
  9546. if (!odd)
  9547. {
  9548. bfd_put_32 (abfd, SLDI_R11_R11_34, p);
  9549. p += 4;
  9550. }
  9551. insn = PADDI_R12_PC | D34 (off);
  9552. bfd_put_32 (abfd, insn >> 32, p);
  9553. p += 4;
  9554. bfd_put_32 (abfd, insn, p);
  9555. p += 4;
  9556. if (odd)
  9557. {
  9558. bfd_put_32 (abfd, SLDI_R11_R11_34, p);
  9559. p += 4;
  9560. }
  9561. if (load)
  9562. bfd_put_32 (abfd, LDX_R12_R11_R12, p);
  9563. else
  9564. bfd_put_32 (abfd, ADD_R12_R11_R12, p);
  9565. }
  9566. else
  9567. {
  9568. off -= odd + 8;
  9569. bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
  9570. p += 4;
  9571. bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
  9572. p += 4;
  9573. if (odd)
  9574. {
  9575. bfd_put_32 (abfd, SLDI_R11_R11_34, p);
  9576. p += 4;
  9577. }
  9578. insn = PADDI_R12_PC | D34 (off);
  9579. bfd_put_32 (abfd, insn >> 32, p);
  9580. p += 4;
  9581. bfd_put_32 (abfd, insn, p);
  9582. p += 4;
  9583. if (!odd)
  9584. {
  9585. bfd_put_32 (abfd, SLDI_R11_R11_34, p);
  9586. p += 4;
  9587. }
  9588. if (load)
  9589. bfd_put_32 (abfd, LDX_R12_R11_R12, p);
  9590. else
  9591. bfd_put_32 (abfd, ADD_R12_R11_R12, p);
  9592. }
  9593. p += 4;
  9594. return p;
  9595. }
  9596. static unsigned int
  9597. size_power10_offset (bfd_vma off, int odd)
  9598. {
  9599. if (off - odd + (1ULL << 33) < 1ULL << 34)
  9600. return odd + 8;
  9601. else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
  9602. return 20;
  9603. else
  9604. return 24;
  9605. }
  9606. static unsigned int
  9607. num_relocs_for_power10_offset (bfd_vma off, int odd)
  9608. {
  9609. if (off - odd + (1ULL << 33) < 1ULL << 34)
  9610. return 1;
  9611. else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
  9612. return 2;
  9613. else
  9614. return 3;
  9615. }
  9616. static Elf_Internal_Rela *
  9617. emit_relocs_for_power10_offset (struct bfd_link_info *info,
  9618. Elf_Internal_Rela *r, bfd_vma roff,
  9619. bfd_vma targ, bfd_vma off, int odd)
  9620. {
  9621. if (off - odd + (1ULL << 33) < 1ULL << 34)
  9622. roff += odd;
  9623. else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
  9624. {
  9625. int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
  9626. r->r_offset = roff + d_offset;
  9627. r->r_addend = targ + 8 - odd - d_offset;
  9628. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
  9629. ++r;
  9630. roff += 8 - odd;
  9631. }
  9632. else
  9633. {
  9634. int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
  9635. r->r_offset = roff + d_offset;
  9636. r->r_addend = targ + 8 + odd - d_offset;
  9637. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
  9638. ++r;
  9639. roff += 4;
  9640. r->r_offset = roff + d_offset;
  9641. r->r_addend = targ + 4 + odd - d_offset;
  9642. r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
  9643. ++r;
  9644. roff += 4 + odd;
  9645. }
  9646. r->r_offset = roff;
  9647. r->r_addend = targ;
  9648. r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
  9649. return r;
  9650. }
  9651. /* Emit .eh_frame opcode to advance pc by DELTA. */
  9652. static bfd_byte *
  9653. eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
  9654. {
  9655. delta /= 4;
  9656. if (delta < 64)
  9657. *eh++ = DW_CFA_advance_loc + delta;
  9658. else if (delta < 256)
  9659. {
  9660. *eh++ = DW_CFA_advance_loc1;
  9661. *eh++ = delta;
  9662. }
  9663. else if (delta < 65536)
  9664. {
  9665. *eh++ = DW_CFA_advance_loc2;
  9666. bfd_put_16 (abfd, delta, eh);
  9667. eh += 2;
  9668. }
  9669. else
  9670. {
  9671. *eh++ = DW_CFA_advance_loc4;
  9672. bfd_put_32 (abfd, delta, eh);
  9673. eh += 4;
  9674. }
  9675. return eh;
  9676. }
  9677. /* Size of required .eh_frame opcode to advance pc by DELTA. */
  9678. static unsigned int
  9679. eh_advance_size (unsigned int delta)
  9680. {
  9681. if (delta < 64 * 4)
  9682. /* DW_CFA_advance_loc+[1..63]. */
  9683. return 1;
  9684. if (delta < 256 * 4)
  9685. /* DW_CFA_advance_loc1, byte. */
  9686. return 2;
  9687. if (delta < 65536 * 4)
  9688. /* DW_CFA_advance_loc2, 2 bytes. */
  9689. return 3;
  9690. /* DW_CFA_advance_loc4, 4 bytes. */
  9691. return 5;
  9692. }
  9693. /* With power7 weakly ordered memory model, it is possible for ld.so
  9694. to update a plt entry in one thread and have another thread see a
  9695. stale zero toc entry. To avoid this we need some sort of acquire
  9696. barrier in the call stub. One solution is to make the load of the
  9697. toc word seem to appear to depend on the load of the function entry
  9698. word. Another solution is to test for r2 being zero, and branch to
  9699. the appropriate glink entry if so.
  9700. . fake dep barrier compare
  9701. . ld 12,xxx(2) ld 12,xxx(2)
  9702. . mtctr 12 mtctr 12
  9703. . xor 11,12,12 ld 2,xxx+8(2)
  9704. . add 2,2,11 cmpldi 2,0
  9705. . ld 2,xxx+8(2) bnectr+
  9706. . bctr b <glink_entry>
  9707. The solution involving the compare turns out to be faster, so
  9708. that's what we use unless the branch won't reach. */
  9709. #define ALWAYS_USE_FAKE_DEP 0
  9710. #define ALWAYS_EMIT_R2SAVE 0
  9711. static inline unsigned int
  9712. plt_stub_size (struct ppc_link_hash_table *htab,
  9713. struct ppc_stub_hash_entry *stub_entry,
  9714. bfd_vma off,
  9715. unsigned int odd)
  9716. {
  9717. unsigned size;
  9718. if (stub_entry->type.sub == ppc_stub_notoc)
  9719. {
  9720. size = 8 + size_power10_offset (off, odd);
  9721. if (stub_entry->type.r2save)
  9722. size += 4;
  9723. }
  9724. else if (stub_entry->type.sub == ppc_stub_p9notoc)
  9725. {
  9726. size = 8 + size_offset (off - 8);
  9727. if (stub_entry->type.r2save)
  9728. size += 4;
  9729. }
  9730. else
  9731. {
  9732. size = 12;
  9733. if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
  9734. size += 4;
  9735. if (PPC_HA (off) != 0)
  9736. size += 4;
  9737. if (htab->opd_abi)
  9738. {
  9739. size += 4;
  9740. if (htab->params->plt_static_chain)
  9741. size += 4;
  9742. if (htab->params->plt_thread_safe
  9743. && htab->elf.dynamic_sections_created
  9744. && stub_entry->h != NULL
  9745. && stub_entry->h->elf.dynindx != -1)
  9746. size += 8;
  9747. if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
  9748. != PPC_HA (off))
  9749. size += 4;
  9750. }
  9751. }
  9752. if (stub_entry->h != NULL
  9753. && is_tls_get_addr (&stub_entry->h->elf, htab)
  9754. && htab->params->tls_get_addr_opt)
  9755. {
  9756. if (!htab->params->no_tls_get_addr_regsave)
  9757. {
  9758. size += 30 * 4;
  9759. if (stub_entry->type.r2save)
  9760. size += 4;
  9761. }
  9762. else
  9763. {
  9764. size += 7 * 4;
  9765. if (stub_entry->type.r2save)
  9766. size += 6 * 4;
  9767. }
  9768. }
  9769. return size;
  9770. }
  9771. /* Depending on the sign of plt_stub_align:
  9772. If positive, return the padding to align to a 2**plt_stub_align
  9773. boundary.
  9774. If negative, if this stub would cross fewer 2**plt_stub_align
  9775. boundaries if we align, then return the padding needed to do so. */
  9776. static inline unsigned int
  9777. plt_stub_pad (struct ppc_link_hash_table *htab,
  9778. struct ppc_stub_hash_entry *stub_entry,
  9779. bfd_vma stub_off,
  9780. bfd_vma plt_off,
  9781. unsigned int odd)
  9782. {
  9783. int stub_align;
  9784. unsigned stub_size;
  9785. if (htab->params->plt_stub_align >= 0)
  9786. {
  9787. stub_align = 1 << htab->params->plt_stub_align;
  9788. if ((stub_off & (stub_align - 1)) != 0)
  9789. return stub_align - (stub_off & (stub_align - 1));
  9790. return 0;
  9791. }
  9792. stub_align = 1 << -htab->params->plt_stub_align;
  9793. stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
  9794. if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
  9795. > ((stub_size - 1) & -stub_align))
  9796. return stub_align - (stub_off & (stub_align - 1));
  9797. return 0;
  9798. }
  9799. /* Build a toc using .plt call stub. */
  9800. static inline bfd_byte *
  9801. build_plt_stub (struct ppc_link_hash_table *htab,
  9802. struct ppc_stub_hash_entry *stub_entry,
  9803. bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
  9804. {
  9805. bfd *obfd = htab->params->stub_bfd;
  9806. bool plt_load_toc = htab->opd_abi;
  9807. bool plt_static_chain = htab->params->plt_static_chain;
  9808. bool plt_thread_safe = (htab->params->plt_thread_safe
  9809. && htab->elf.dynamic_sections_created
  9810. && stub_entry->h != NULL
  9811. && stub_entry->h->elf.dynindx != -1);
  9812. bool use_fake_dep = plt_thread_safe;
  9813. bfd_vma cmp_branch_off = 0;
  9814. if (!ALWAYS_USE_FAKE_DEP
  9815. && plt_load_toc
  9816. && plt_thread_safe
  9817. && !(stub_entry->h != NULL
  9818. && is_tls_get_addr (&stub_entry->h->elf, htab)
  9819. && htab->params->tls_get_addr_opt))
  9820. {
  9821. bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
  9822. bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
  9823. / PLT_ENTRY_SIZE (htab));
  9824. bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
  9825. bfd_vma to, from;
  9826. if (pltindex > 32768)
  9827. glinkoff += (pltindex - 32768) * 4;
  9828. to = (glinkoff
  9829. + htab->glink->output_offset
  9830. + htab->glink->output_section->vma);
  9831. from = (p - stub_entry->group->stub_sec->contents
  9832. + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
  9833. + 4 * (PPC_HA (offset) != 0)
  9834. + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
  9835. != PPC_HA (offset))
  9836. + 4 * (plt_static_chain != 0)
  9837. + 20
  9838. + stub_entry->group->stub_sec->output_offset
  9839. + stub_entry->group->stub_sec->output_section->vma);
  9840. cmp_branch_off = to - from;
  9841. use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
  9842. }
  9843. if (PPC_HA (offset) != 0)
  9844. {
  9845. if (r != NULL)
  9846. {
  9847. if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
  9848. r[0].r_offset += 4;
  9849. r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
  9850. r[1].r_offset = r[0].r_offset + 4;
  9851. r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
  9852. r[1].r_addend = r[0].r_addend;
  9853. if (plt_load_toc)
  9854. {
  9855. if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
  9856. {
  9857. r[2].r_offset = r[1].r_offset + 4;
  9858. r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
  9859. r[2].r_addend = r[0].r_addend;
  9860. }
  9861. else
  9862. {
  9863. r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
  9864. r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
  9865. r[2].r_addend = r[0].r_addend + 8;
  9866. if (plt_static_chain)
  9867. {
  9868. r[3].r_offset = r[2].r_offset + 4;
  9869. r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
  9870. r[3].r_addend = r[0].r_addend + 16;
  9871. }
  9872. }
  9873. }
  9874. }
  9875. if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
  9876. bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
  9877. if (plt_load_toc)
  9878. {
  9879. bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
  9880. bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
  9881. }
  9882. else
  9883. {
  9884. bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
  9885. bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
  9886. }
  9887. if (plt_load_toc
  9888. && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
  9889. {
  9890. bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
  9891. offset = 0;
  9892. }
  9893. bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
  9894. if (plt_load_toc)
  9895. {
  9896. if (use_fake_dep)
  9897. {
  9898. bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
  9899. bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
  9900. }
  9901. bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
  9902. if (plt_static_chain)
  9903. bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
  9904. }
  9905. }
  9906. else
  9907. {
  9908. if (r != NULL)
  9909. {
  9910. if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
  9911. r[0].r_offset += 4;
  9912. r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
  9913. if (plt_load_toc)
  9914. {
  9915. if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
  9916. {
  9917. r[1].r_offset = r[0].r_offset + 4;
  9918. r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
  9919. r[1].r_addend = r[0].r_addend;
  9920. }
  9921. else
  9922. {
  9923. r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
  9924. r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
  9925. r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
  9926. if (plt_static_chain)
  9927. {
  9928. r[2].r_offset = r[1].r_offset + 4;
  9929. r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
  9930. r[2].r_addend = r[0].r_addend + 8;
  9931. }
  9932. }
  9933. }
  9934. }
  9935. if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
  9936. bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
  9937. bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
  9938. if (plt_load_toc
  9939. && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
  9940. {
  9941. bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
  9942. offset = 0;
  9943. }
  9944. bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
  9945. if (plt_load_toc)
  9946. {
  9947. if (use_fake_dep)
  9948. {
  9949. bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
  9950. bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
  9951. }
  9952. if (plt_static_chain)
  9953. bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
  9954. bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
  9955. }
  9956. }
  9957. if (plt_load_toc && plt_thread_safe && !use_fake_dep)
  9958. {
  9959. bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
  9960. bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
  9961. bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
  9962. }
  9963. else
  9964. bfd_put_32 (obfd, BCTR, p), p += 4;
  9965. return p;
  9966. }
  9967. /* Build a special .plt call stub for __tls_get_addr. */
  9968. #define LD_R0_0R3 0xe8030000
  9969. #define LD_R12_0R3 0xe9830000
  9970. #define MR_R0_R3 0x7c601b78
  9971. #define CMPDI_R0_0 0x2c200000
  9972. #define ADD_R3_R12_R13 0x7c6c6a14
  9973. #define BEQLR 0x4d820020
  9974. #define MR_R3_R0 0x7c030378
  9975. #define BCTRL 0x4e800421
  9976. static bfd_byte *
  9977. build_tls_get_addr_head (struct ppc_link_hash_table *htab,
  9978. struct ppc_stub_hash_entry *stub_entry,
  9979. bfd_byte *p)
  9980. {
  9981. bfd *obfd = htab->params->stub_bfd;
  9982. bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
  9983. bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
  9984. bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
  9985. bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
  9986. bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
  9987. bfd_put_32 (obfd, BEQLR, p), p += 4;
  9988. bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
  9989. if (!htab->params->no_tls_get_addr_regsave)
  9990. p = tls_get_addr_prologue (obfd, p, htab);
  9991. else if (stub_entry->type.r2save)
  9992. {
  9993. bfd_put_32 (obfd, MFLR_R0, p);
  9994. p += 4;
  9995. bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
  9996. p += 4;
  9997. }
  9998. return p;
  9999. }
  10000. static bfd_byte *
  10001. build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
  10002. struct ppc_stub_hash_entry *stub_entry,
  10003. bfd_byte *p,
  10004. bfd_byte *loc)
  10005. {
  10006. bfd *obfd = htab->params->stub_bfd;
  10007. if (!htab->params->no_tls_get_addr_regsave)
  10008. {
  10009. bfd_put_32 (obfd, BCTRL, p - 4);
  10010. if (stub_entry->type.r2save)
  10011. {
  10012. bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
  10013. p += 4;
  10014. }
  10015. p = tls_get_addr_epilogue (obfd, p, htab);
  10016. }
  10017. else if (stub_entry->type.r2save)
  10018. {
  10019. bfd_put_32 (obfd, BCTRL, p - 4);
  10020. bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
  10021. p += 4;
  10022. bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
  10023. p += 4;
  10024. bfd_put_32 (obfd, MTLR_R0, p);
  10025. p += 4;
  10026. bfd_put_32 (obfd, BLR, p);
  10027. p += 4;
  10028. }
  10029. if (htab->glink_eh_frame != NULL
  10030. && htab->glink_eh_frame->size != 0)
  10031. {
  10032. bfd_byte *base, *eh;
  10033. base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
  10034. eh = base + stub_entry->group->eh_size;
  10035. if (!htab->params->no_tls_get_addr_regsave)
  10036. {
  10037. unsigned int cfa_updt, delta, i;
  10038. /* After the bctrl, lr has been modified so we need to emit
  10039. .eh_frame info saying the return address is on the stack. In
  10040. fact we must put the EH info at or before the call rather
  10041. than after it, because the EH info for a call needs to be
  10042. specified by that point.
  10043. See libgcc/unwind-dw2.c execute_cfa_program.
  10044. Any stack pointer update must be described immediately after
  10045. the instruction making the change, and since the stdu occurs
  10046. after saving regs we put all the reg saves and the cfa
  10047. change there. */
  10048. cfa_updt = stub_entry->stub_offset + 18 * 4;
  10049. delta = cfa_updt - stub_entry->group->lr_restore;
  10050. stub_entry->group->lr_restore
  10051. = stub_entry->stub_offset + (p - loc) - 4;
  10052. eh = eh_advance (htab->elf.dynobj, eh, delta);
  10053. *eh++ = DW_CFA_def_cfa_offset;
  10054. if (htab->opd_abi)
  10055. {
  10056. *eh++ = 128;
  10057. *eh++ = 1;
  10058. }
  10059. else
  10060. *eh++ = 96;
  10061. *eh++ = DW_CFA_offset_extended_sf;
  10062. *eh++ = 65;
  10063. *eh++ = (-16 / 8) & 0x7f;
  10064. for (i = 4; i < 12; i++)
  10065. {
  10066. *eh++ = DW_CFA_offset + i;
  10067. *eh++ = (htab->opd_abi ? 13 : 12) - i;
  10068. }
  10069. *eh++ = (DW_CFA_advance_loc
  10070. + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
  10071. *eh++ = DW_CFA_def_cfa_offset;
  10072. *eh++ = 0;
  10073. for (i = 4; i < 12; i++)
  10074. *eh++ = DW_CFA_restore + i;
  10075. *eh++ = DW_CFA_advance_loc + 2;
  10076. *eh++ = DW_CFA_restore_extended;
  10077. *eh++ = 65;
  10078. stub_entry->group->eh_size = eh - base;
  10079. }
  10080. else if (stub_entry->type.r2save)
  10081. {
  10082. unsigned int lr_used, delta;
  10083. lr_used = stub_entry->stub_offset + (p - 20 - loc);
  10084. delta = lr_used - stub_entry->group->lr_restore;
  10085. stub_entry->group->lr_restore = lr_used + 16;
  10086. eh = eh_advance (htab->elf.dynobj, eh, delta);
  10087. *eh++ = DW_CFA_offset_extended_sf;
  10088. *eh++ = 65;
  10089. *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
  10090. *eh++ = DW_CFA_advance_loc + 4;
  10091. *eh++ = DW_CFA_restore_extended;
  10092. *eh++ = 65;
  10093. stub_entry->group->eh_size = eh - base;
  10094. }
  10095. }
  10096. return p;
  10097. }
  10098. static Elf_Internal_Rela *
  10099. get_relocs (asection *sec, int count)
  10100. {
  10101. Elf_Internal_Rela *relocs;
  10102. struct bfd_elf_section_data *elfsec_data;
  10103. elfsec_data = elf_section_data (sec);
  10104. relocs = elfsec_data->relocs;
  10105. if (relocs == NULL)
  10106. {
  10107. bfd_size_type relsize;
  10108. relsize = sec->reloc_count * sizeof (*relocs);
  10109. relocs = bfd_alloc (sec->owner, relsize);
  10110. if (relocs == NULL)
  10111. return NULL;
  10112. elfsec_data->relocs = relocs;
  10113. elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
  10114. sizeof (Elf_Internal_Shdr));
  10115. if (elfsec_data->rela.hdr == NULL)
  10116. return NULL;
  10117. elfsec_data->rela.hdr->sh_size = (sec->reloc_count
  10118. * sizeof (Elf64_External_Rela));
  10119. elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
  10120. sec->reloc_count = 0;
  10121. }
  10122. relocs += sec->reloc_count;
  10123. sec->reloc_count += count;
  10124. return relocs;
  10125. }
  10126. /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
  10127. forms, to the equivalent relocs against the global symbol given by
  10128. STUB_ENTRY->H. */
  10129. static bool
  10130. use_global_in_relocs (struct ppc_link_hash_table *htab,
  10131. struct ppc_stub_hash_entry *stub_entry,
  10132. Elf_Internal_Rela *r, unsigned int num_rel)
  10133. {
  10134. struct elf_link_hash_entry **hashes;
  10135. unsigned long symndx;
  10136. struct ppc_link_hash_entry *h;
  10137. bfd_vma symval;
  10138. /* Relocs are always against symbols in their own object file. Fake
  10139. up global sym hashes for the stub bfd (which has no symbols). */
  10140. hashes = elf_sym_hashes (htab->params->stub_bfd);
  10141. if (hashes == NULL)
  10142. {
  10143. bfd_size_type hsize;
  10144. /* When called the first time, stub_globals will contain the
  10145. total number of symbols seen during stub sizing. After
  10146. allocating, stub_globals is used as an index to fill the
  10147. hashes array. */
  10148. hsize = (htab->stub_globals + 1) * sizeof (*hashes);
  10149. hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
  10150. if (hashes == NULL)
  10151. return false;
  10152. elf_sym_hashes (htab->params->stub_bfd) = hashes;
  10153. htab->stub_globals = 1;
  10154. }
  10155. symndx = htab->stub_globals++;
  10156. h = stub_entry->h;
  10157. hashes[symndx] = &h->elf;
  10158. if (h->oh != NULL && h->oh->is_func)
  10159. h = ppc_follow_link (h->oh);
  10160. BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
  10161. || h->elf.root.type == bfd_link_hash_defweak);
  10162. symval = defined_sym_val (&h->elf);
  10163. while (num_rel-- != 0)
  10164. {
  10165. r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
  10166. if (h->elf.root.u.def.section != stub_entry->target_section)
  10167. {
  10168. /* H is an opd symbol. The addend must be zero, and the
  10169. branch reloc is the only one we can convert. */
  10170. r->r_addend = 0;
  10171. break;
  10172. }
  10173. else
  10174. r->r_addend -= symval;
  10175. --r;
  10176. }
  10177. return true;
  10178. }
  10179. static bfd_vma
  10180. get_r2off (struct bfd_link_info *info,
  10181. struct ppc_stub_hash_entry *stub_entry)
  10182. {
  10183. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  10184. bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
  10185. if (r2off == 0)
  10186. {
  10187. /* Support linking -R objects. Get the toc pointer from the
  10188. opd entry. */
  10189. char buf[8];
  10190. if (!htab->opd_abi)
  10191. return r2off;
  10192. asection *opd = stub_entry->h->elf.root.u.def.section;
  10193. bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
  10194. if (strcmp (opd->name, ".opd") != 0
  10195. || opd->reloc_count != 0)
  10196. {
  10197. info->callbacks->einfo
  10198. (_("%P: cannot find opd entry toc for `%pT'\n"),
  10199. stub_entry->h->elf.root.root.string);
  10200. bfd_set_error (bfd_error_bad_value);
  10201. return (bfd_vma) -1;
  10202. }
  10203. if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
  10204. return (bfd_vma) -1;
  10205. r2off = bfd_get_64 (opd->owner, buf);
  10206. r2off -= elf_gp (info->output_bfd);
  10207. }
  10208. r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
  10209. return r2off;
  10210. }
  10211. static bool
  10212. ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
  10213. {
  10214. struct ppc_stub_hash_entry *stub_entry;
  10215. struct ppc_branch_hash_entry *br_entry;
  10216. struct bfd_link_info *info;
  10217. struct ppc_link_hash_table *htab;
  10218. bfd *obfd;
  10219. bfd_byte *loc;
  10220. bfd_byte *p, *relp;
  10221. bfd_vma targ, off;
  10222. Elf_Internal_Rela *r;
  10223. asection *plt;
  10224. int num_rel;
  10225. int odd;
  10226. bool is_tga;
  10227. /* Massage our args to the form they really have. */
  10228. stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
  10229. info = in_arg;
  10230. /* Fail if the target section could not be assigned to an output
  10231. section. The user should fix his linker script. */
  10232. if (stub_entry->target_section != NULL
  10233. && stub_entry->target_section->output_section == NULL
  10234. && info->non_contiguous_regions)
  10235. info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
  10236. "Retry without --enable-non-contiguous-regions.\n"),
  10237. stub_entry->target_section);
  10238. /* Same for the group. */
  10239. if (stub_entry->group->stub_sec != NULL
  10240. && stub_entry->group->stub_sec->output_section == NULL
  10241. && info->non_contiguous_regions)
  10242. info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
  10243. "output section. Retry without "
  10244. "--enable-non-contiguous-regions.\n"),
  10245. stub_entry->group->stub_sec,
  10246. stub_entry->target_section);
  10247. htab = ppc_hash_table (info);
  10248. if (htab == NULL)
  10249. return false;
  10250. BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
  10251. loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
  10252. htab->stub_count[stub_entry->type.main - 1] += 1;
  10253. if (stub_entry->type.main == ppc_stub_long_branch
  10254. && stub_entry->type.sub == ppc_stub_toc)
  10255. {
  10256. /* Branches are relative. This is where we are going to. */
  10257. targ = (stub_entry->target_value
  10258. + stub_entry->target_section->output_offset
  10259. + stub_entry->target_section->output_section->vma);
  10260. targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
  10261. /* And this is where we are coming from. */
  10262. off = (stub_entry->stub_offset
  10263. + stub_entry->group->stub_sec->output_offset
  10264. + stub_entry->group->stub_sec->output_section->vma);
  10265. off = targ - off;
  10266. p = loc;
  10267. obfd = htab->params->stub_bfd;
  10268. if (stub_entry->type.r2save)
  10269. {
  10270. bfd_vma r2off = get_r2off (info, stub_entry);
  10271. if (r2off == (bfd_vma) -1)
  10272. {
  10273. htab->stub_error = true;
  10274. return false;
  10275. }
  10276. bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
  10277. p += 4;
  10278. if (PPC_HA (r2off) != 0)
  10279. {
  10280. bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
  10281. p += 4;
  10282. }
  10283. if (PPC_LO (r2off) != 0)
  10284. {
  10285. bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
  10286. p += 4;
  10287. }
  10288. off -= p - loc;
  10289. }
  10290. bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
  10291. p += 4;
  10292. if (off + (1 << 25) >= (bfd_vma) (1 << 26))
  10293. {
  10294. _bfd_error_handler
  10295. (_("long branch stub `%s' offset overflow"),
  10296. stub_entry->root.string);
  10297. htab->stub_error = true;
  10298. return false;
  10299. }
  10300. if (info->emitrelocations)
  10301. {
  10302. r = get_relocs (stub_entry->group->stub_sec, 1);
  10303. if (r == NULL)
  10304. return false;
  10305. r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
  10306. r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
  10307. r->r_addend = targ;
  10308. if (stub_entry->h != NULL
  10309. && !use_global_in_relocs (htab, stub_entry, r, 1))
  10310. return false;
  10311. }
  10312. }
  10313. else if (stub_entry->type.main == ppc_stub_plt_branch
  10314. && stub_entry->type.sub == ppc_stub_toc)
  10315. {
  10316. br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
  10317. stub_entry->root.string + 9,
  10318. false, false);
  10319. if (br_entry == NULL)
  10320. {
  10321. _bfd_error_handler (_("can't find branch stub `%s'"),
  10322. stub_entry->root.string);
  10323. htab->stub_error = true;
  10324. return false;
  10325. }
  10326. targ = (stub_entry->target_value
  10327. + stub_entry->target_section->output_offset
  10328. + stub_entry->target_section->output_section->vma);
  10329. if (!stub_entry->type.r2save)
  10330. targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
  10331. bfd_put_64 (htab->brlt->owner, targ,
  10332. htab->brlt->contents + br_entry->offset);
  10333. if (br_entry->iter == htab->stub_iteration)
  10334. {
  10335. br_entry->iter = 0;
  10336. if (htab->relbrlt != NULL && !info->enable_dt_relr)
  10337. {
  10338. /* Create a reloc for the branch lookup table entry. */
  10339. Elf_Internal_Rela rela;
  10340. bfd_byte *rl;
  10341. rela.r_offset = (br_entry->offset
  10342. + htab->brlt->output_offset
  10343. + htab->brlt->output_section->vma);
  10344. rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
  10345. rela.r_addend = targ;
  10346. rl = htab->relbrlt->contents;
  10347. rl += (htab->relbrlt->reloc_count++
  10348. * sizeof (Elf64_External_Rela));
  10349. bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
  10350. }
  10351. else if (info->emitrelocations)
  10352. {
  10353. r = get_relocs (htab->brlt, 1);
  10354. if (r == NULL)
  10355. return false;
  10356. /* brlt, being SEC_LINKER_CREATED does not go through the
  10357. normal reloc processing. Symbols and offsets are not
  10358. translated from input file to output file form, so
  10359. set up the offset per the output file. */
  10360. r->r_offset = (br_entry->offset
  10361. + htab->brlt->output_offset
  10362. + htab->brlt->output_section->vma);
  10363. r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
  10364. r->r_addend = targ;
  10365. }
  10366. }
  10367. targ = (br_entry->offset
  10368. + htab->brlt->output_offset
  10369. + htab->brlt->output_section->vma);
  10370. off = (elf_gp (info->output_bfd)
  10371. + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
  10372. off = targ - off;
  10373. if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
  10374. {
  10375. info->callbacks->einfo
  10376. (_("%P: linkage table error against `%pT'\n"),
  10377. stub_entry->root.string);
  10378. bfd_set_error (bfd_error_bad_value);
  10379. htab->stub_error = true;
  10380. return false;
  10381. }
  10382. if (info->emitrelocations)
  10383. {
  10384. r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
  10385. if (r == NULL)
  10386. return false;
  10387. r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
  10388. if (bfd_big_endian (info->output_bfd))
  10389. r[0].r_offset += 2;
  10390. if (stub_entry->type.r2save)
  10391. r[0].r_offset += 4;
  10392. r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
  10393. r[0].r_addend = targ;
  10394. if (PPC_HA (off) != 0)
  10395. {
  10396. r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
  10397. r[1].r_offset = r[0].r_offset + 4;
  10398. r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
  10399. r[1].r_addend = r[0].r_addend;
  10400. }
  10401. }
  10402. p = loc;
  10403. obfd = htab->params->stub_bfd;
  10404. if (!stub_entry->type.r2save)
  10405. {
  10406. if (PPC_HA (off) != 0)
  10407. {
  10408. bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
  10409. p += 4;
  10410. bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
  10411. }
  10412. else
  10413. bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
  10414. }
  10415. else
  10416. {
  10417. bfd_vma r2off = get_r2off (info, stub_entry);
  10418. if (r2off == (bfd_vma) -1)
  10419. {
  10420. htab->stub_error = true;
  10421. return false;
  10422. }
  10423. bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
  10424. p += 4;
  10425. if (PPC_HA (off) != 0)
  10426. {
  10427. bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
  10428. p += 4;
  10429. bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
  10430. }
  10431. else
  10432. bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
  10433. if (PPC_HA (r2off) != 0)
  10434. {
  10435. p += 4;
  10436. bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
  10437. }
  10438. if (PPC_LO (r2off) != 0)
  10439. {
  10440. p += 4;
  10441. bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
  10442. }
  10443. }
  10444. p += 4;
  10445. bfd_put_32 (obfd, MTCTR_R12, p);
  10446. p += 4;
  10447. bfd_put_32 (obfd, BCTR, p);
  10448. p += 4;
  10449. }
  10450. else if (stub_entry->type.sub >= ppc_stub_notoc)
  10451. {
  10452. bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
  10453. p = loc;
  10454. off = (stub_entry->stub_offset
  10455. + stub_entry->group->stub_sec->output_offset
  10456. + stub_entry->group->stub_sec->output_section->vma);
  10457. obfd = htab->params->stub_bfd;
  10458. is_tga = (is_plt
  10459. && stub_entry->h != NULL
  10460. && is_tls_get_addr (&stub_entry->h->elf, htab)
  10461. && htab->params->tls_get_addr_opt);
  10462. if (is_tga)
  10463. {
  10464. p = build_tls_get_addr_head (htab, stub_entry, p);
  10465. off += p - loc;
  10466. }
  10467. if (stub_entry->type.r2save)
  10468. {
  10469. off += 4;
  10470. bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
  10471. p += 4;
  10472. }
  10473. if (is_plt)
  10474. {
  10475. targ = stub_entry->plt_ent->plt.offset & ~1;
  10476. if (targ >= (bfd_vma) -2)
  10477. abort ();
  10478. plt = htab->elf.splt;
  10479. if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
  10480. {
  10481. if (stub_entry->symtype == STT_GNU_IFUNC)
  10482. plt = htab->elf.iplt;
  10483. else
  10484. plt = htab->pltlocal;
  10485. }
  10486. targ += plt->output_offset + plt->output_section->vma;
  10487. }
  10488. else
  10489. targ = (stub_entry->target_value
  10490. + stub_entry->target_section->output_offset
  10491. + stub_entry->target_section->output_section->vma);
  10492. odd = off & 4;
  10493. off = targ - off;
  10494. relp = p;
  10495. num_rel = 0;
  10496. if (stub_entry->type.sub == ppc_stub_notoc)
  10497. p = build_power10_offset (obfd, p, off, odd, is_plt);
  10498. else
  10499. {
  10500. if (htab->glink_eh_frame != NULL
  10501. && htab->glink_eh_frame->size != 0)
  10502. {
  10503. bfd_byte *base, *eh;
  10504. unsigned int lr_used, delta;
  10505. base = (htab->glink_eh_frame->contents
  10506. + stub_entry->group->eh_base + 17);
  10507. eh = base + stub_entry->group->eh_size;
  10508. lr_used = stub_entry->stub_offset + (p - loc) + 8;
  10509. delta = lr_used - stub_entry->group->lr_restore;
  10510. stub_entry->group->lr_restore = lr_used + 8;
  10511. eh = eh_advance (htab->elf.dynobj, eh, delta);
  10512. *eh++ = DW_CFA_register;
  10513. *eh++ = 65;
  10514. *eh++ = 12;
  10515. *eh++ = DW_CFA_advance_loc + 2;
  10516. *eh++ = DW_CFA_restore_extended;
  10517. *eh++ = 65;
  10518. stub_entry->group->eh_size = eh - base;
  10519. }
  10520. /* The notoc stubs calculate their target (either a PLT entry or
  10521. the global entry point of a function) relative to the PC
  10522. returned by the "bcl" two instructions past the start of the
  10523. sequence emitted by build_offset. The offset is therefore 8
  10524. less than calculated from the start of the sequence. */
  10525. off -= 8;
  10526. p = build_offset (obfd, p, off, is_plt);
  10527. }
  10528. if (stub_entry->type.main == ppc_stub_long_branch)
  10529. {
  10530. bfd_vma from;
  10531. num_rel = 1;
  10532. from = (stub_entry->stub_offset
  10533. + stub_entry->group->stub_sec->output_offset
  10534. + stub_entry->group->stub_sec->output_section->vma
  10535. + (p - loc));
  10536. bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
  10537. }
  10538. else
  10539. {
  10540. bfd_put_32 (obfd, MTCTR_R12, p);
  10541. p += 4;
  10542. bfd_put_32 (obfd, BCTR, p);
  10543. }
  10544. p += 4;
  10545. if (is_tga)
  10546. p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
  10547. if (info->emitrelocations)
  10548. {
  10549. bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
  10550. if (stub_entry->type.sub == ppc_stub_notoc)
  10551. num_rel += num_relocs_for_power10_offset (off, odd);
  10552. else
  10553. {
  10554. num_rel += num_relocs_for_offset (off);
  10555. roff += 16;
  10556. }
  10557. r = get_relocs (stub_entry->group->stub_sec, num_rel);
  10558. if (r == NULL)
  10559. return false;
  10560. if (stub_entry->type.sub == ppc_stub_notoc)
  10561. r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
  10562. else
  10563. r = emit_relocs_for_offset (info, r, roff, targ, off);
  10564. if (stub_entry->type.main == ppc_stub_long_branch)
  10565. {
  10566. ++r;
  10567. roff = p - 4 - stub_entry->group->stub_sec->contents;
  10568. r->r_offset = roff;
  10569. r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
  10570. r->r_addend = targ;
  10571. if (stub_entry->h != NULL
  10572. && !use_global_in_relocs (htab, stub_entry, r, num_rel))
  10573. return false;
  10574. }
  10575. }
  10576. }
  10577. else if (stub_entry->type.main == ppc_stub_plt_call)
  10578. {
  10579. if (stub_entry->h != NULL
  10580. && stub_entry->h->is_func_descriptor
  10581. && stub_entry->h->oh != NULL)
  10582. {
  10583. struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
  10584. /* If the old-ABI "dot-symbol" is undefined make it weak so
  10585. we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
  10586. if (fh->elf.root.type == bfd_link_hash_undefined
  10587. && (stub_entry->h->elf.root.type == bfd_link_hash_defined
  10588. || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
  10589. fh->elf.root.type = bfd_link_hash_undefweak;
  10590. }
  10591. /* Now build the stub. */
  10592. targ = stub_entry->plt_ent->plt.offset & ~1;
  10593. if (targ >= (bfd_vma) -2)
  10594. abort ();
  10595. plt = htab->elf.splt;
  10596. if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
  10597. {
  10598. if (stub_entry->symtype == STT_GNU_IFUNC)
  10599. plt = htab->elf.iplt;
  10600. else
  10601. plt = htab->pltlocal;
  10602. }
  10603. targ += plt->output_offset + plt->output_section->vma;
  10604. off = (elf_gp (info->output_bfd)
  10605. + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
  10606. off = targ - off;
  10607. if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
  10608. {
  10609. info->callbacks->einfo
  10610. /* xgettext:c-format */
  10611. (_("%P: linkage table error against `%pT'\n"),
  10612. stub_entry->h != NULL
  10613. ? stub_entry->h->elf.root.root.string
  10614. : "<local sym>");
  10615. bfd_set_error (bfd_error_bad_value);
  10616. htab->stub_error = true;
  10617. return false;
  10618. }
  10619. r = NULL;
  10620. if (info->emitrelocations)
  10621. {
  10622. r = get_relocs (stub_entry->group->stub_sec,
  10623. ((PPC_HA (off) != 0)
  10624. + (htab->opd_abi
  10625. ? 2 + (htab->params->plt_static_chain
  10626. && PPC_HA (off + 16) == PPC_HA (off))
  10627. : 1)));
  10628. if (r == NULL)
  10629. return false;
  10630. r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
  10631. if (bfd_big_endian (info->output_bfd))
  10632. r[0].r_offset += 2;
  10633. r[0].r_addend = targ;
  10634. }
  10635. p = loc;
  10636. obfd = htab->params->stub_bfd;
  10637. is_tga = (stub_entry->h != NULL
  10638. && is_tls_get_addr (&stub_entry->h->elf, htab)
  10639. && htab->params->tls_get_addr_opt);
  10640. if (is_tga)
  10641. {
  10642. p = build_tls_get_addr_head (htab, stub_entry, p);
  10643. if (r != NULL)
  10644. r[0].r_offset += p - loc;
  10645. }
  10646. p = build_plt_stub (htab, stub_entry, p, off, r);
  10647. if (is_tga)
  10648. p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
  10649. }
  10650. else if (stub_entry->type.main == ppc_stub_save_res)
  10651. return true;
  10652. else
  10653. {
  10654. BFD_FAIL ();
  10655. return false;
  10656. }
  10657. stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
  10658. if (htab->params->emit_stub_syms)
  10659. {
  10660. struct elf_link_hash_entry *h;
  10661. size_t len1, len2;
  10662. char *name;
  10663. const char *const stub_str[] = { "long_branch",
  10664. "plt_branch",
  10665. "plt_call" };
  10666. len1 = strlen (stub_str[stub_entry->type.main - 1]);
  10667. len2 = strlen (stub_entry->root.string);
  10668. name = bfd_malloc (len1 + len2 + 2);
  10669. if (name == NULL)
  10670. return false;
  10671. memcpy (name, stub_entry->root.string, 9);
  10672. memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
  10673. memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
  10674. h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
  10675. if (h == NULL)
  10676. return false;
  10677. if (h->root.type == bfd_link_hash_new)
  10678. {
  10679. h->root.type = bfd_link_hash_defined;
  10680. h->root.u.def.section = stub_entry->group->stub_sec;
  10681. h->root.u.def.value = stub_entry->stub_offset;
  10682. h->ref_regular = 1;
  10683. h->def_regular = 1;
  10684. h->ref_regular_nonweak = 1;
  10685. h->forced_local = 1;
  10686. h->non_elf = 0;
  10687. h->root.linker_def = 1;
  10688. }
  10689. }
  10690. return true;
  10691. }
  10692. /* As above, but don't actually build the stub. Just bump offset so
  10693. we know stub section sizes, and select plt_branch stubs where
  10694. long_branch stubs won't do. */
  10695. static bool
  10696. ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
  10697. {
  10698. struct ppc_stub_hash_entry *stub_entry;
  10699. struct bfd_link_info *info;
  10700. struct ppc_link_hash_table *htab;
  10701. asection *plt;
  10702. bfd_vma targ, off, r2off;
  10703. unsigned int size, extra, lr_used, delta, odd;
  10704. bfd_vma stub_offset;
  10705. /* Massage our args to the form they really have. */
  10706. stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
  10707. info = in_arg;
  10708. htab = ppc_hash_table (info);
  10709. if (htab == NULL)
  10710. return false;
  10711. /* Fail if the target section could not be assigned to an output
  10712. section. The user should fix his linker script. */
  10713. if (stub_entry->target_section != NULL
  10714. && stub_entry->target_section->output_section == NULL
  10715. && info->non_contiguous_regions)
  10716. info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
  10717. "Retry without --enable-non-contiguous-regions.\n"),
  10718. stub_entry->target_section);
  10719. /* Same for the group. */
  10720. if (stub_entry->group->stub_sec != NULL
  10721. && stub_entry->group->stub_sec->output_section == NULL
  10722. && info->non_contiguous_regions)
  10723. info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
  10724. "output section. Retry without "
  10725. "--enable-non-contiguous-regions.\n"),
  10726. stub_entry->group->stub_sec,
  10727. stub_entry->target_section);
  10728. /* Make a note of the offset within the stubs for this entry. */
  10729. stub_offset = stub_entry->group->stub_sec->size;
  10730. if (htab->stub_iteration > STUB_SHRINK_ITER
  10731. && stub_entry->stub_offset > stub_offset)
  10732. stub_offset = stub_entry->stub_offset;
  10733. if (stub_entry->h != NULL
  10734. && stub_entry->h->save_res
  10735. && stub_entry->h->elf.root.type == bfd_link_hash_defined
  10736. && stub_entry->h->elf.root.u.def.section == htab->sfpr)
  10737. {
  10738. /* Don't make stubs to out-of-line register save/restore
  10739. functions. Instead, emit copies of the functions. */
  10740. stub_entry->group->needs_save_res = 1;
  10741. stub_entry->type.main = ppc_stub_save_res;
  10742. stub_entry->type.sub = ppc_stub_toc;
  10743. stub_entry->type.r2save = 0;
  10744. return true;
  10745. }
  10746. if (stub_entry->type.main == ppc_stub_plt_branch)
  10747. {
  10748. /* Reset the stub type from the plt branch variant in case we now
  10749. can reach with a shorter stub. */
  10750. stub_entry->type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
  10751. }
  10752. if (stub_entry->type.main == ppc_stub_long_branch
  10753. && stub_entry->type.sub == ppc_stub_toc)
  10754. {
  10755. targ = (stub_entry->target_value
  10756. + stub_entry->target_section->output_offset
  10757. + stub_entry->target_section->output_section->vma);
  10758. targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
  10759. off = (stub_offset
  10760. + stub_entry->group->stub_sec->output_offset
  10761. + stub_entry->group->stub_sec->output_section->vma);
  10762. size = 4;
  10763. r2off = 0;
  10764. if (stub_entry->type.r2save)
  10765. {
  10766. r2off = get_r2off (info, stub_entry);
  10767. if (r2off == (bfd_vma) -1)
  10768. {
  10769. htab->stub_error = true;
  10770. return false;
  10771. }
  10772. size = 8;
  10773. if (PPC_HA (r2off) != 0)
  10774. size += 4;
  10775. if (PPC_LO (r2off) != 0)
  10776. size += 4;
  10777. off += size - 4;
  10778. }
  10779. off = targ - off;
  10780. /* If the branch offset is too big, use a ppc_stub_plt_branch.
  10781. Do the same for -R objects without function descriptors. */
  10782. if ((stub_entry->type.r2save
  10783. && r2off == 0
  10784. && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
  10785. || off + (1 << 25) >= (bfd_vma) (1 << 26))
  10786. {
  10787. struct ppc_branch_hash_entry *br_entry;
  10788. br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
  10789. stub_entry->root.string + 9,
  10790. true, false);
  10791. if (br_entry == NULL)
  10792. {
  10793. _bfd_error_handler (_("can't build branch stub `%s'"),
  10794. stub_entry->root.string);
  10795. htab->stub_error = true;
  10796. return false;
  10797. }
  10798. if (br_entry->iter != htab->stub_iteration)
  10799. {
  10800. br_entry->iter = htab->stub_iteration;
  10801. br_entry->offset = htab->brlt->size;
  10802. htab->brlt->size += 8;
  10803. if (htab->relbrlt != NULL && !info->enable_dt_relr)
  10804. htab->relbrlt->size += sizeof (Elf64_External_Rela);
  10805. else if (info->emitrelocations)
  10806. {
  10807. htab->brlt->reloc_count += 1;
  10808. htab->brlt->flags |= SEC_RELOC;
  10809. }
  10810. }
  10811. targ = (br_entry->offset
  10812. + htab->brlt->output_offset
  10813. + htab->brlt->output_section->vma);
  10814. off = (elf_gp (info->output_bfd)
  10815. + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
  10816. off = targ - off;
  10817. if (info->emitrelocations)
  10818. {
  10819. stub_entry->group->stub_sec->reloc_count
  10820. += 1 + (PPC_HA (off) != 0);
  10821. stub_entry->group->stub_sec->flags |= SEC_RELOC;
  10822. }
  10823. stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
  10824. if (!stub_entry->type.r2save)
  10825. {
  10826. size = 12;
  10827. if (PPC_HA (off) != 0)
  10828. size = 16;
  10829. }
  10830. else
  10831. {
  10832. size = 16;
  10833. if (PPC_HA (off) != 0)
  10834. size += 4;
  10835. if (PPC_HA (r2off) != 0)
  10836. size += 4;
  10837. if (PPC_LO (r2off) != 0)
  10838. size += 4;
  10839. }
  10840. }
  10841. else if (info->emitrelocations)
  10842. {
  10843. stub_entry->group->stub_sec->reloc_count += 1;
  10844. stub_entry->group->stub_sec->flags |= SEC_RELOC;
  10845. }
  10846. }
  10847. else if (stub_entry->type.main == ppc_stub_long_branch)
  10848. {
  10849. off = (stub_offset
  10850. + stub_entry->group->stub_sec->output_offset
  10851. + stub_entry->group->stub_sec->output_section->vma);
  10852. size = 0;
  10853. if (stub_entry->type.r2save)
  10854. size = 4;
  10855. off += size;
  10856. targ = (stub_entry->target_value
  10857. + stub_entry->target_section->output_offset
  10858. + stub_entry->target_section->output_section->vma);
  10859. odd = off & 4;
  10860. off = targ - off;
  10861. if (info->emitrelocations)
  10862. {
  10863. unsigned int num_rel;
  10864. if (stub_entry->type.sub == ppc_stub_notoc)
  10865. num_rel = num_relocs_for_power10_offset (off, odd);
  10866. else
  10867. num_rel = num_relocs_for_offset (off - 8);
  10868. stub_entry->group->stub_sec->reloc_count += num_rel;
  10869. stub_entry->group->stub_sec->flags |= SEC_RELOC;
  10870. }
  10871. if (stub_entry->type.sub == ppc_stub_notoc)
  10872. extra = size_power10_offset (off, odd);
  10873. else
  10874. extra = size_offset (off - 8);
  10875. /* Include branch insn plus those in the offset sequence. */
  10876. size += 4 + extra;
  10877. /* The branch insn is at the end, or "extra" bytes along. So
  10878. its offset will be "extra" bytes less that that already
  10879. calculated. */
  10880. off -= extra;
  10881. if (stub_entry->type.sub != ppc_stub_notoc)
  10882. {
  10883. /* After the bcl, lr has been modified so we need to emit
  10884. .eh_frame info saying the return address is in r12. */
  10885. lr_used = stub_offset + 8;
  10886. if (stub_entry->type.r2save)
  10887. lr_used += 4;
  10888. /* The eh_frame info will consist of a DW_CFA_advance_loc or
  10889. variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
  10890. DW_CFA_restore_extended 65. */
  10891. delta = lr_used - stub_entry->group->lr_restore;
  10892. stub_entry->group->eh_size += eh_advance_size (delta) + 6;
  10893. stub_entry->group->lr_restore = lr_used + 8;
  10894. }
  10895. /* If the branch can't reach, use a plt_branch. */
  10896. if (off + (1 << 25) >= (bfd_vma) (1 << 26))
  10897. {
  10898. stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
  10899. size += 4;
  10900. }
  10901. else if (info->emitrelocations)
  10902. stub_entry->group->stub_sec->reloc_count +=1;
  10903. }
  10904. else if (stub_entry->type.sub >= ppc_stub_notoc)
  10905. {
  10906. BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
  10907. lr_used = 0;
  10908. if (stub_entry->h != NULL
  10909. && is_tls_get_addr (&stub_entry->h->elf, htab)
  10910. && htab->params->tls_get_addr_opt)
  10911. {
  10912. lr_used += 7 * 4;
  10913. if (!htab->params->no_tls_get_addr_regsave)
  10914. lr_used += 11 * 4;
  10915. else if (stub_entry->type.r2save)
  10916. lr_used += 2 * 4;
  10917. }
  10918. if (stub_entry->type.r2save)
  10919. lr_used += 4;
  10920. targ = stub_entry->plt_ent->plt.offset & ~1;
  10921. if (targ >= (bfd_vma) -2)
  10922. abort ();
  10923. plt = htab->elf.splt;
  10924. if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
  10925. {
  10926. if (stub_entry->symtype == STT_GNU_IFUNC)
  10927. plt = htab->elf.iplt;
  10928. else
  10929. plt = htab->pltlocal;
  10930. }
  10931. targ += plt->output_offset + plt->output_section->vma;
  10932. off = (stub_offset
  10933. + stub_entry->group->stub_sec->output_offset
  10934. + stub_entry->group->stub_sec->output_section->vma
  10935. + lr_used);
  10936. odd = off & 4;
  10937. off = targ - off;
  10938. if (htab->params->plt_stub_align != 0)
  10939. {
  10940. unsigned pad = plt_stub_pad (htab, stub_entry, stub_offset, off, odd);
  10941. stub_offset += pad;
  10942. off -= pad;
  10943. odd ^= pad & 4;
  10944. }
  10945. if (info->emitrelocations)
  10946. {
  10947. unsigned int num_rel;
  10948. if (stub_entry->type.sub == ppc_stub_notoc)
  10949. num_rel = num_relocs_for_power10_offset (off, odd);
  10950. else
  10951. num_rel = num_relocs_for_offset (off - 8);
  10952. stub_entry->group->stub_sec->reloc_count += num_rel;
  10953. stub_entry->group->stub_sec->flags |= SEC_RELOC;
  10954. }
  10955. size = plt_stub_size (htab, stub_entry, off, odd);
  10956. if (stub_entry->type.sub != ppc_stub_notoc)
  10957. {
  10958. /* After the bcl, lr has been modified so we need to emit
  10959. .eh_frame info saying the return address is in r12. */
  10960. lr_used += stub_offset + 8;
  10961. /* The eh_frame info will consist of a DW_CFA_advance_loc or
  10962. variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
  10963. DW_CFA_restore_extended 65. */
  10964. delta = lr_used - stub_entry->group->lr_restore;
  10965. stub_entry->group->eh_size += eh_advance_size (delta) + 6;
  10966. stub_entry->group->lr_restore = lr_used + 8;
  10967. }
  10968. if (stub_entry->h != NULL
  10969. && is_tls_get_addr (&stub_entry->h->elf, htab)
  10970. && htab->params->tls_get_addr_opt)
  10971. {
  10972. if (!htab->params->no_tls_get_addr_regsave)
  10973. {
  10974. unsigned int cfa_updt = stub_offset + 18 * 4;
  10975. delta = cfa_updt - stub_entry->group->lr_restore;
  10976. stub_entry->group->eh_size += eh_advance_size (delta);
  10977. stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
  10978. stub_entry->group->lr_restore = stub_offset + size - 4;
  10979. }
  10980. else if (stub_entry->type.r2save)
  10981. {
  10982. lr_used = stub_offset + size - 20;
  10983. delta = lr_used - stub_entry->group->lr_restore;
  10984. stub_entry->group->eh_size += eh_advance_size (delta) + 6;
  10985. stub_entry->group->lr_restore = stub_offset + size - 4;
  10986. }
  10987. }
  10988. }
  10989. else if (stub_entry->type.main == ppc_stub_plt_call)
  10990. {
  10991. targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
  10992. if (targ >= (bfd_vma) -2)
  10993. abort ();
  10994. plt = htab->elf.splt;
  10995. if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
  10996. {
  10997. if (stub_entry->symtype == STT_GNU_IFUNC)
  10998. plt = htab->elf.iplt;
  10999. else
  11000. plt = htab->pltlocal;
  11001. }
  11002. targ += plt->output_offset + plt->output_section->vma;
  11003. off = (elf_gp (info->output_bfd)
  11004. + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
  11005. off = targ - off;
  11006. if (htab->params->plt_stub_align != 0)
  11007. {
  11008. unsigned pad = plt_stub_pad (htab, stub_entry, stub_offset, off, 0);
  11009. stub_offset += pad;
  11010. }
  11011. if (info->emitrelocations)
  11012. {
  11013. stub_entry->group->stub_sec->reloc_count
  11014. += ((PPC_HA (off) != 0)
  11015. + (htab->opd_abi
  11016. ? 2 + (htab->params->plt_static_chain
  11017. && PPC_HA (off + 16) == PPC_HA (off))
  11018. : 1));
  11019. stub_entry->group->stub_sec->flags |= SEC_RELOC;
  11020. }
  11021. size = plt_stub_size (htab, stub_entry, off, 0);
  11022. if (stub_entry->h != NULL
  11023. && is_tls_get_addr (&stub_entry->h->elf, htab)
  11024. && htab->params->tls_get_addr_opt
  11025. && stub_entry->type.r2save)
  11026. {
  11027. if (!htab->params->no_tls_get_addr_regsave)
  11028. {
  11029. /* Adjustments to r1 need to be described. */
  11030. unsigned int cfa_updt = stub_offset + 18 * 4;
  11031. delta = cfa_updt - stub_entry->group->lr_restore;
  11032. stub_entry->group->eh_size += eh_advance_size (delta);
  11033. stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
  11034. }
  11035. else
  11036. {
  11037. lr_used = stub_offset + size - 20;
  11038. /* The eh_frame info will consist of a DW_CFA_advance_loc
  11039. or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
  11040. DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
  11041. delta = lr_used - stub_entry->group->lr_restore;
  11042. stub_entry->group->eh_size += eh_advance_size (delta) + 6;
  11043. }
  11044. stub_entry->group->lr_restore = stub_offset + size - 4;
  11045. }
  11046. }
  11047. else
  11048. {
  11049. BFD_FAIL ();
  11050. return false;
  11051. }
  11052. if (stub_entry->stub_offset != stub_offset)
  11053. htab->stub_changed = true;
  11054. stub_entry->stub_offset = stub_offset;
  11055. stub_entry->group->stub_sec->size = stub_offset + size;
  11056. return true;
  11057. }
  11058. /* Set up various things so that we can make a list of input sections
  11059. for each output section included in the link. Returns -1 on error,
  11060. 0 when no stubs will be needed, and 1 on success. */
  11061. int
  11062. ppc64_elf_setup_section_lists (struct bfd_link_info *info)
  11063. {
  11064. unsigned int id;
  11065. size_t amt;
  11066. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11067. if (htab == NULL)
  11068. return -1;
  11069. htab->sec_info_arr_size = _bfd_section_id;
  11070. amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
  11071. htab->sec_info = bfd_zmalloc (amt);
  11072. if (htab->sec_info == NULL)
  11073. return -1;
  11074. /* Set toc_off for com, und, abs and ind sections. */
  11075. for (id = 0; id < 3; id++)
  11076. htab->sec_info[id].toc_off = TOC_BASE_OFF;
  11077. return 1;
  11078. }
  11079. /* Set up for first pass at multitoc partitioning. */
  11080. void
  11081. ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
  11082. {
  11083. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11084. htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
  11085. htab->toc_bfd = NULL;
  11086. htab->toc_first_sec = NULL;
  11087. }
  11088. /* The linker repeatedly calls this function for each TOC input section
  11089. and linker generated GOT section. Group input bfds such that the toc
  11090. within a group is less than 64k in size. */
  11091. bool
  11092. ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
  11093. {
  11094. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11095. bfd_vma addr, off, limit;
  11096. if (htab == NULL)
  11097. return false;
  11098. if (!htab->second_toc_pass)
  11099. {
  11100. /* Keep track of the first .toc or .got section for this input bfd. */
  11101. bool new_bfd = htab->toc_bfd != isec->owner;
  11102. if (new_bfd)
  11103. {
  11104. htab->toc_bfd = isec->owner;
  11105. htab->toc_first_sec = isec;
  11106. }
  11107. addr = isec->output_offset + isec->output_section->vma;
  11108. off = addr - htab->toc_curr;
  11109. limit = 0x80008000;
  11110. if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
  11111. limit = 0x10000;
  11112. if (off + isec->size > limit)
  11113. {
  11114. addr = (htab->toc_first_sec->output_offset
  11115. + htab->toc_first_sec->output_section->vma);
  11116. htab->toc_curr = addr;
  11117. htab->toc_curr &= -TOC_BASE_ALIGN;
  11118. }
  11119. /* toc_curr is the base address of this toc group. Set elf_gp
  11120. for the input section to be the offset relative to the
  11121. output toc base plus 0x8000. Making the input elf_gp an
  11122. offset allows us to move the toc as a whole without
  11123. recalculating input elf_gp. */
  11124. off = htab->toc_curr - elf_gp (info->output_bfd);
  11125. off += TOC_BASE_OFF;
  11126. /* Die if someone uses a linker script that doesn't keep input
  11127. file .toc and .got together. */
  11128. if (new_bfd
  11129. && elf_gp (isec->owner) != 0
  11130. && elf_gp (isec->owner) != off)
  11131. return false;
  11132. elf_gp (isec->owner) = off;
  11133. return true;
  11134. }
  11135. /* During the second pass toc_first_sec points to the start of
  11136. a toc group, and toc_curr is used to track the old elf_gp.
  11137. We use toc_bfd to ensure we only look at each bfd once. */
  11138. if (htab->toc_bfd == isec->owner)
  11139. return true;
  11140. htab->toc_bfd = isec->owner;
  11141. if (htab->toc_first_sec == NULL
  11142. || htab->toc_curr != elf_gp (isec->owner))
  11143. {
  11144. htab->toc_curr = elf_gp (isec->owner);
  11145. htab->toc_first_sec = isec;
  11146. }
  11147. addr = (htab->toc_first_sec->output_offset
  11148. + htab->toc_first_sec->output_section->vma);
  11149. off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
  11150. elf_gp (isec->owner) = off;
  11151. return true;
  11152. }
  11153. /* Called via elf_link_hash_traverse to merge GOT entries for global
  11154. symbol H. */
  11155. static bool
  11156. merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
  11157. {
  11158. if (h->root.type == bfd_link_hash_indirect)
  11159. return true;
  11160. merge_got_entries (&h->got.glist);
  11161. return true;
  11162. }
  11163. /* Called via elf_link_hash_traverse to allocate GOT entries for global
  11164. symbol H. */
  11165. static bool
  11166. reallocate_got (struct elf_link_hash_entry *h, void *inf)
  11167. {
  11168. struct got_entry *gent;
  11169. if (h->root.type == bfd_link_hash_indirect)
  11170. return true;
  11171. for (gent = h->got.glist; gent != NULL; gent = gent->next)
  11172. if (!gent->is_indirect)
  11173. allocate_got (h, (struct bfd_link_info *) inf, gent);
  11174. return true;
  11175. }
  11176. /* Called on the first multitoc pass after the last call to
  11177. ppc64_elf_next_toc_section. This function removes duplicate GOT
  11178. entries. */
  11179. bool
  11180. ppc64_elf_layout_multitoc (struct bfd_link_info *info)
  11181. {
  11182. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11183. struct bfd *ibfd, *ibfd2;
  11184. bool done_something;
  11185. htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
  11186. if (!htab->do_multi_toc)
  11187. return false;
  11188. /* Merge global sym got entries within a toc group. */
  11189. elf_link_hash_traverse (&htab->elf, merge_global_got, info);
  11190. /* And tlsld_got. */
  11191. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  11192. {
  11193. struct got_entry *ent, *ent2;
  11194. if (!is_ppc64_elf (ibfd))
  11195. continue;
  11196. ent = ppc64_tlsld_got (ibfd);
  11197. if (!ent->is_indirect
  11198. && ent->got.offset != (bfd_vma) -1)
  11199. {
  11200. for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
  11201. {
  11202. if (!is_ppc64_elf (ibfd2))
  11203. continue;
  11204. ent2 = ppc64_tlsld_got (ibfd2);
  11205. if (!ent2->is_indirect
  11206. && ent2->got.offset != (bfd_vma) -1
  11207. && elf_gp (ibfd2) == elf_gp (ibfd))
  11208. {
  11209. ent2->is_indirect = true;
  11210. ent2->got.ent = ent;
  11211. }
  11212. }
  11213. }
  11214. }
  11215. /* Zap sizes of got sections. */
  11216. htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
  11217. htab->elf.irelplt->size -= htab->got_reli_size;
  11218. htab->got_reli_size = 0;
  11219. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  11220. {
  11221. asection *got, *relgot;
  11222. if (!is_ppc64_elf (ibfd))
  11223. continue;
  11224. got = ppc64_elf_tdata (ibfd)->got;
  11225. if (got != NULL)
  11226. {
  11227. got->rawsize = got->size;
  11228. got->size = 0;
  11229. relgot = ppc64_elf_tdata (ibfd)->relgot;
  11230. relgot->rawsize = relgot->size;
  11231. relgot->size = 0;
  11232. }
  11233. }
  11234. /* Now reallocate the got, local syms first. We don't need to
  11235. allocate section contents again since we never increase size. */
  11236. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  11237. {
  11238. struct got_entry **lgot_ents;
  11239. struct got_entry **end_lgot_ents;
  11240. struct plt_entry **local_plt;
  11241. struct plt_entry **end_local_plt;
  11242. unsigned char *lgot_masks;
  11243. bfd_size_type locsymcount;
  11244. Elf_Internal_Shdr *symtab_hdr;
  11245. asection *s;
  11246. Elf_Internal_Sym *local_syms;
  11247. Elf_Internal_Sym *isym;
  11248. if (!is_ppc64_elf (ibfd))
  11249. continue;
  11250. lgot_ents = elf_local_got_ents (ibfd);
  11251. if (!lgot_ents)
  11252. continue;
  11253. symtab_hdr = &elf_symtab_hdr (ibfd);
  11254. locsymcount = symtab_hdr->sh_info;
  11255. end_lgot_ents = lgot_ents + locsymcount;
  11256. local_plt = (struct plt_entry **) end_lgot_ents;
  11257. end_local_plt = local_plt + locsymcount;
  11258. lgot_masks = (unsigned char *) end_local_plt;
  11259. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  11260. if (local_syms == NULL && locsymcount != 0)
  11261. {
  11262. local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
  11263. 0, NULL, NULL, NULL);
  11264. if (local_syms == NULL)
  11265. return false;
  11266. }
  11267. s = ppc64_elf_tdata (ibfd)->got;
  11268. for (isym = local_syms;
  11269. lgot_ents < end_lgot_ents;
  11270. ++lgot_ents, ++lgot_masks, isym++)
  11271. {
  11272. struct got_entry *ent;
  11273. for (ent = *lgot_ents; ent != NULL; ent = ent->next)
  11274. {
  11275. unsigned int ent_size = 8;
  11276. unsigned int rel_size = sizeof (Elf64_External_Rela);
  11277. ent->got.offset = s->size;
  11278. if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
  11279. {
  11280. ent_size *= 2;
  11281. rel_size *= 2;
  11282. }
  11283. s->size += ent_size;
  11284. if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
  11285. {
  11286. htab->elf.irelplt->size += rel_size;
  11287. htab->got_reli_size += rel_size;
  11288. }
  11289. else if (bfd_link_pic (info)
  11290. && (ent->tls_type == 0
  11291. ? !info->enable_dt_relr
  11292. : !bfd_link_executable (info))
  11293. && isym->st_shndx != SHN_ABS)
  11294. {
  11295. asection *srel = ppc64_elf_tdata (ibfd)->relgot;
  11296. srel->size += rel_size;
  11297. }
  11298. }
  11299. }
  11300. }
  11301. elf_link_hash_traverse (&htab->elf, reallocate_got, info);
  11302. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  11303. {
  11304. struct got_entry *ent;
  11305. if (!is_ppc64_elf (ibfd))
  11306. continue;
  11307. ent = ppc64_tlsld_got (ibfd);
  11308. if (!ent->is_indirect
  11309. && ent->got.offset != (bfd_vma) -1)
  11310. {
  11311. asection *s = ppc64_elf_tdata (ibfd)->got;
  11312. ent->got.offset = s->size;
  11313. s->size += 16;
  11314. if (bfd_link_dll (info))
  11315. {
  11316. asection *srel = ppc64_elf_tdata (ibfd)->relgot;
  11317. srel->size += sizeof (Elf64_External_Rela);
  11318. }
  11319. }
  11320. }
  11321. done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
  11322. if (!done_something)
  11323. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  11324. {
  11325. asection *got;
  11326. if (!is_ppc64_elf (ibfd))
  11327. continue;
  11328. got = ppc64_elf_tdata (ibfd)->got;
  11329. if (got != NULL)
  11330. {
  11331. done_something = got->rawsize != got->size;
  11332. if (done_something)
  11333. break;
  11334. }
  11335. }
  11336. if (done_something)
  11337. (*htab->params->layout_sections_again) ();
  11338. /* Set up for second pass over toc sections to recalculate elf_gp
  11339. on input sections. */
  11340. htab->toc_bfd = NULL;
  11341. htab->toc_first_sec = NULL;
  11342. htab->second_toc_pass = true;
  11343. return done_something;
  11344. }
  11345. /* Called after second pass of multitoc partitioning. */
  11346. void
  11347. ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
  11348. {
  11349. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11350. /* After the second pass, toc_curr tracks the TOC offset used
  11351. for code sections below in ppc64_elf_next_input_section. */
  11352. htab->toc_curr = TOC_BASE_OFF;
  11353. }
  11354. /* No toc references were found in ISEC. If the code in ISEC makes no
  11355. calls, then there's no need to use toc adjusting stubs when branching
  11356. into ISEC. Actually, indirect calls from ISEC are OK as they will
  11357. load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
  11358. needed, and 2 if a cyclical call-graph was found but no other reason
  11359. for a stub was detected. If called from the top level, a return of
  11360. 2 means the same as a return of 0. */
  11361. static int
  11362. toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
  11363. {
  11364. int ret;
  11365. /* Mark this section as checked. */
  11366. isec->call_check_done = 1;
  11367. /* We know none of our code bearing sections will need toc stubs. */
  11368. if ((isec->flags & SEC_LINKER_CREATED) != 0)
  11369. return 0;
  11370. if (isec->size == 0)
  11371. return 0;
  11372. if (isec->output_section == NULL)
  11373. return 0;
  11374. ret = 0;
  11375. if (isec->reloc_count != 0)
  11376. {
  11377. Elf_Internal_Rela *relstart, *rel;
  11378. Elf_Internal_Sym *local_syms;
  11379. struct ppc_link_hash_table *htab;
  11380. relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
  11381. info->keep_memory);
  11382. if (relstart == NULL)
  11383. return -1;
  11384. /* Look for branches to outside of this section. */
  11385. local_syms = NULL;
  11386. htab = ppc_hash_table (info);
  11387. if (htab == NULL)
  11388. return -1;
  11389. for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
  11390. {
  11391. enum elf_ppc64_reloc_type r_type;
  11392. unsigned long r_symndx;
  11393. struct elf_link_hash_entry *h;
  11394. struct ppc_link_hash_entry *eh;
  11395. Elf_Internal_Sym *sym;
  11396. asection *sym_sec;
  11397. struct _opd_sec_data *opd;
  11398. bfd_vma sym_value;
  11399. bfd_vma dest;
  11400. r_type = ELF64_R_TYPE (rel->r_info);
  11401. if (r_type != R_PPC64_REL24
  11402. && r_type != R_PPC64_REL24_NOTOC
  11403. && r_type != R_PPC64_REL24_P9NOTOC
  11404. && r_type != R_PPC64_REL14
  11405. && r_type != R_PPC64_REL14_BRTAKEN
  11406. && r_type != R_PPC64_REL14_BRNTAKEN
  11407. && r_type != R_PPC64_PLTCALL
  11408. && r_type != R_PPC64_PLTCALL_NOTOC)
  11409. continue;
  11410. r_symndx = ELF64_R_SYM (rel->r_info);
  11411. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
  11412. isec->owner))
  11413. {
  11414. ret = -1;
  11415. break;
  11416. }
  11417. /* Calls to dynamic lib functions go through a plt call stub
  11418. that uses r2. */
  11419. eh = ppc_elf_hash_entry (h);
  11420. if (eh != NULL
  11421. && (eh->elf.plt.plist != NULL
  11422. || (eh->oh != NULL
  11423. && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
  11424. {
  11425. ret = 1;
  11426. break;
  11427. }
  11428. if (sym_sec == NULL)
  11429. /* Ignore other undefined symbols. */
  11430. continue;
  11431. /* Assume branches to other sections not included in the
  11432. link need stubs too, to cover -R and absolute syms. */
  11433. if (sym_sec->output_section == NULL)
  11434. {
  11435. ret = 1;
  11436. break;
  11437. }
  11438. if (h == NULL)
  11439. sym_value = sym->st_value;
  11440. else
  11441. {
  11442. if (h->root.type != bfd_link_hash_defined
  11443. && h->root.type != bfd_link_hash_defweak)
  11444. abort ();
  11445. sym_value = h->root.u.def.value;
  11446. }
  11447. sym_value += rel->r_addend;
  11448. /* If this branch reloc uses an opd sym, find the code section. */
  11449. opd = get_opd_info (sym_sec);
  11450. if (opd != NULL)
  11451. {
  11452. if (h == NULL && opd->adjust != NULL)
  11453. {
  11454. long adjust;
  11455. adjust = opd->adjust[OPD_NDX (sym_value)];
  11456. if (adjust == -1)
  11457. /* Assume deleted functions won't ever be called. */
  11458. continue;
  11459. sym_value += adjust;
  11460. }
  11461. dest = opd_entry_value (sym_sec, sym_value,
  11462. &sym_sec, NULL, false);
  11463. if (dest == (bfd_vma) -1)
  11464. continue;
  11465. }
  11466. else
  11467. dest = (sym_value
  11468. + sym_sec->output_offset
  11469. + sym_sec->output_section->vma);
  11470. /* Ignore branch to self. */
  11471. if (sym_sec == isec)
  11472. continue;
  11473. /* If the called function uses the toc, we need a stub. */
  11474. if (sym_sec->has_toc_reloc
  11475. || sym_sec->makes_toc_func_call)
  11476. {
  11477. ret = 1;
  11478. break;
  11479. }
  11480. /* Assume any branch that needs a long branch stub might in fact
  11481. need a plt_branch stub. A plt_branch stub uses r2. */
  11482. else if (dest - (isec->output_offset
  11483. + isec->output_section->vma
  11484. + rel->r_offset) + (1 << 25)
  11485. >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
  11486. ? h->other
  11487. : sym->st_other))
  11488. {
  11489. ret = 1;
  11490. break;
  11491. }
  11492. /* If calling back to a section in the process of being
  11493. tested, we can't say for sure that no toc adjusting stubs
  11494. are needed, so don't return zero. */
  11495. else if (sym_sec->call_check_in_progress)
  11496. ret = 2;
  11497. /* Branches to another section that itself doesn't have any TOC
  11498. references are OK. Recursively call ourselves to check. */
  11499. else if (!sym_sec->call_check_done)
  11500. {
  11501. int recur;
  11502. /* Mark current section as indeterminate, so that other
  11503. sections that call back to current won't be marked as
  11504. known. */
  11505. isec->call_check_in_progress = 1;
  11506. recur = toc_adjusting_stub_needed (info, sym_sec);
  11507. isec->call_check_in_progress = 0;
  11508. if (recur != 0)
  11509. {
  11510. ret = recur;
  11511. if (recur != 2)
  11512. break;
  11513. }
  11514. }
  11515. }
  11516. if (elf_symtab_hdr (isec->owner).contents
  11517. != (unsigned char *) local_syms)
  11518. free (local_syms);
  11519. if (elf_section_data (isec)->relocs != relstart)
  11520. free (relstart);
  11521. }
  11522. if ((ret & 1) == 0
  11523. && isec->map_head.s != NULL
  11524. && (strcmp (isec->output_section->name, ".init") == 0
  11525. || strcmp (isec->output_section->name, ".fini") == 0))
  11526. {
  11527. if (isec->map_head.s->has_toc_reloc
  11528. || isec->map_head.s->makes_toc_func_call)
  11529. ret = 1;
  11530. else if (!isec->map_head.s->call_check_done)
  11531. {
  11532. int recur;
  11533. isec->call_check_in_progress = 1;
  11534. recur = toc_adjusting_stub_needed (info, isec->map_head.s);
  11535. isec->call_check_in_progress = 0;
  11536. if (recur != 0)
  11537. ret = recur;
  11538. }
  11539. }
  11540. if (ret == 1)
  11541. isec->makes_toc_func_call = 1;
  11542. return ret;
  11543. }
  11544. /* The linker repeatedly calls this function for each input section,
  11545. in the order that input sections are linked into output sections.
  11546. Build lists of input sections to determine groupings between which
  11547. we may insert linker stubs. */
  11548. bool
  11549. ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
  11550. {
  11551. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11552. if (htab == NULL)
  11553. return false;
  11554. if ((isec->output_section->flags & SEC_CODE) != 0
  11555. && isec->output_section->id < htab->sec_info_arr_size)
  11556. {
  11557. /* This happens to make the list in reverse order,
  11558. which is what we want. */
  11559. htab->sec_info[isec->id].u.list
  11560. = htab->sec_info[isec->output_section->id].u.list;
  11561. htab->sec_info[isec->output_section->id].u.list = isec;
  11562. }
  11563. if (htab->multi_toc_needed)
  11564. {
  11565. /* Analyse sections that aren't already flagged as needing a
  11566. valid toc pointer. Exclude .fixup for the linux kernel.
  11567. .fixup contains branches, but only back to the function that
  11568. hit an exception. */
  11569. if (!(isec->has_toc_reloc
  11570. || (isec->flags & SEC_CODE) == 0
  11571. || strcmp (isec->name, ".fixup") == 0
  11572. || isec->call_check_done))
  11573. {
  11574. if (toc_adjusting_stub_needed (info, isec) < 0)
  11575. return false;
  11576. }
  11577. /* Make all sections use the TOC assigned for this object file.
  11578. This will be wrong for pasted sections; We fix that in
  11579. check_pasted_section(). */
  11580. if (elf_gp (isec->owner) != 0)
  11581. htab->toc_curr = elf_gp (isec->owner);
  11582. }
  11583. htab->sec_info[isec->id].toc_off = htab->toc_curr;
  11584. return true;
  11585. }
  11586. /* Check that all .init and .fini sections use the same toc, if they
  11587. have toc relocs. */
  11588. static bool
  11589. check_pasted_section (struct bfd_link_info *info, const char *name)
  11590. {
  11591. asection *o = bfd_get_section_by_name (info->output_bfd, name);
  11592. if (o != NULL)
  11593. {
  11594. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11595. bfd_vma toc_off = 0;
  11596. asection *i;
  11597. for (i = o->map_head.s; i != NULL; i = i->map_head.s)
  11598. if (i->has_toc_reloc)
  11599. {
  11600. if (toc_off == 0)
  11601. toc_off = htab->sec_info[i->id].toc_off;
  11602. else if (toc_off != htab->sec_info[i->id].toc_off)
  11603. return false;
  11604. }
  11605. if (toc_off == 0)
  11606. for (i = o->map_head.s; i != NULL; i = i->map_head.s)
  11607. if (i->makes_toc_func_call)
  11608. {
  11609. toc_off = htab->sec_info[i->id].toc_off;
  11610. break;
  11611. }
  11612. /* Make sure the whole pasted function uses the same toc offset. */
  11613. if (toc_off != 0)
  11614. for (i = o->map_head.s; i != NULL; i = i->map_head.s)
  11615. htab->sec_info[i->id].toc_off = toc_off;
  11616. }
  11617. return true;
  11618. }
  11619. bool
  11620. ppc64_elf_check_init_fini (struct bfd_link_info *info)
  11621. {
  11622. bool ret1 = check_pasted_section (info, ".init");
  11623. bool ret2 = check_pasted_section (info, ".fini");
  11624. return ret1 && ret2;
  11625. }
  11626. /* See whether we can group stub sections together. Grouping stub
  11627. sections may result in fewer stubs. More importantly, we need to
  11628. put all .init* and .fini* stubs at the beginning of the .init or
  11629. .fini output sections respectively, because glibc splits the
  11630. _init and _fini functions into multiple parts. Putting a stub in
  11631. the middle of a function is not a good idea. */
  11632. static bool
  11633. group_sections (struct bfd_link_info *info,
  11634. bfd_size_type stub_group_size,
  11635. bool stubs_always_before_branch)
  11636. {
  11637. struct ppc_link_hash_table *htab;
  11638. asection *osec;
  11639. bool suppress_size_errors;
  11640. htab = ppc_hash_table (info);
  11641. if (htab == NULL)
  11642. return false;
  11643. suppress_size_errors = false;
  11644. if (stub_group_size == 1)
  11645. {
  11646. /* Default values. */
  11647. if (stubs_always_before_branch)
  11648. stub_group_size = 0x1e00000;
  11649. else
  11650. stub_group_size = 0x1c00000;
  11651. suppress_size_errors = true;
  11652. }
  11653. for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
  11654. {
  11655. asection *tail;
  11656. if (osec->id >= htab->sec_info_arr_size)
  11657. continue;
  11658. tail = htab->sec_info[osec->id].u.list;
  11659. while (tail != NULL)
  11660. {
  11661. asection *curr;
  11662. asection *prev;
  11663. bfd_size_type total;
  11664. bool big_sec;
  11665. bfd_vma curr_toc;
  11666. struct map_stub *group;
  11667. bfd_size_type group_size;
  11668. curr = tail;
  11669. total = tail->size;
  11670. group_size = (ppc64_elf_section_data (tail) != NULL
  11671. && ppc64_elf_section_data (tail)->has_14bit_branch
  11672. ? stub_group_size >> 10 : stub_group_size);
  11673. big_sec = total > group_size;
  11674. if (big_sec && !suppress_size_errors)
  11675. /* xgettext:c-format */
  11676. _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
  11677. tail->owner, tail);
  11678. curr_toc = htab->sec_info[tail->id].toc_off;
  11679. while ((prev = htab->sec_info[curr->id].u.list) != NULL
  11680. && ((total += curr->output_offset - prev->output_offset)
  11681. < (ppc64_elf_section_data (prev) != NULL
  11682. && ppc64_elf_section_data (prev)->has_14bit_branch
  11683. ? (group_size = stub_group_size >> 10) : group_size))
  11684. && htab->sec_info[prev->id].toc_off == curr_toc)
  11685. curr = prev;
  11686. /* OK, the size from the start of CURR to the end is less
  11687. than group_size and thus can be handled by one stub
  11688. section. (or the tail section is itself larger than
  11689. group_size, in which case we may be toast.) We should
  11690. really be keeping track of the total size of stubs added
  11691. here, as stubs contribute to the final output section
  11692. size. That's a little tricky, and this way will only
  11693. break if stubs added make the total size more than 2^25,
  11694. ie. for the default stub_group_size, if stubs total more
  11695. than 2097152 bytes, or nearly 75000 plt call stubs. */
  11696. group = bfd_alloc (curr->owner, sizeof (*group));
  11697. if (group == NULL)
  11698. return false;
  11699. group->link_sec = curr;
  11700. group->stub_sec = NULL;
  11701. group->needs_save_res = 0;
  11702. group->lr_restore = 0;
  11703. group->eh_size = 0;
  11704. group->eh_base = 0;
  11705. group->next = htab->group;
  11706. htab->group = group;
  11707. do
  11708. {
  11709. prev = htab->sec_info[tail->id].u.list;
  11710. /* Set up this stub group. */
  11711. htab->sec_info[tail->id].u.group = group;
  11712. }
  11713. while (tail != curr && (tail = prev) != NULL);
  11714. /* But wait, there's more! Input sections up to group_size
  11715. bytes before the stub section can be handled by it too.
  11716. Don't do this if we have a really large section after the
  11717. stubs, as adding more stubs increases the chance that
  11718. branches may not reach into the stub section. */
  11719. if (!stubs_always_before_branch && !big_sec)
  11720. {
  11721. total = 0;
  11722. while (prev != NULL
  11723. && ((total += tail->output_offset - prev->output_offset)
  11724. < (ppc64_elf_section_data (prev) != NULL
  11725. && ppc64_elf_section_data (prev)->has_14bit_branch
  11726. ? (group_size = stub_group_size >> 10)
  11727. : group_size))
  11728. && htab->sec_info[prev->id].toc_off == curr_toc)
  11729. {
  11730. tail = prev;
  11731. prev = htab->sec_info[tail->id].u.list;
  11732. htab->sec_info[tail->id].u.group = group;
  11733. }
  11734. }
  11735. tail = prev;
  11736. }
  11737. }
  11738. return true;
  11739. }
  11740. static const unsigned char glink_eh_frame_cie[] =
  11741. {
  11742. 0, 0, 0, 16, /* length. */
  11743. 0, 0, 0, 0, /* id. */
  11744. 1, /* CIE version. */
  11745. 'z', 'R', 0, /* Augmentation string. */
  11746. 4, /* Code alignment. */
  11747. 0x78, /* Data alignment. */
  11748. 65, /* RA reg. */
  11749. 1, /* Augmentation size. */
  11750. DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
  11751. DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
  11752. };
  11753. /* Stripping output sections is normally done before dynamic section
  11754. symbols have been allocated. This function is called later, and
  11755. handles cases like htab->brlt which is mapped to its own output
  11756. section. */
  11757. static void
  11758. maybe_strip_output (struct bfd_link_info *info, asection *isec)
  11759. {
  11760. if (isec->size == 0
  11761. && isec->output_section->size == 0
  11762. && !(isec->output_section->flags & SEC_KEEP)
  11763. && !bfd_section_removed_from_list (info->output_bfd,
  11764. isec->output_section)
  11765. && elf_section_data (isec->output_section)->dynindx == 0)
  11766. {
  11767. isec->output_section->flags |= SEC_EXCLUDE;
  11768. bfd_section_list_remove (info->output_bfd, isec->output_section);
  11769. info->output_bfd->section_count--;
  11770. }
  11771. }
  11772. /* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
  11773. the array of such relocs. */
  11774. static bool
  11775. append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
  11776. {
  11777. if (htab->relr_count >= htab->relr_alloc)
  11778. {
  11779. if (htab->relr_alloc == 0)
  11780. htab->relr_alloc = 4096;
  11781. else
  11782. htab->relr_alloc *= 2;
  11783. htab->relr = bfd_realloc (htab->relr,
  11784. htab->relr_alloc * sizeof (*htab->relr));
  11785. if (htab->relr == NULL)
  11786. return false;
  11787. }
  11788. htab->relr[htab->relr_count].sec = sec;
  11789. htab->relr[htab->relr_count].off = off;
  11790. htab->relr_count++;
  11791. return true;
  11792. }
  11793. /* qsort comparator for bfd_vma args. */
  11794. static int
  11795. compare_relr_address (const void *arg1, const void *arg2)
  11796. {
  11797. bfd_vma a = *(bfd_vma *) arg1;
  11798. bfd_vma b = *(bfd_vma *) arg2;
  11799. return a < b ? -1 : a > b ? 1 : 0;
  11800. }
  11801. /* Produce a malloc'd sorted array of reloc addresses from the info
  11802. stored by append_relr_off. */
  11803. static bfd_vma *
  11804. sort_relr (struct ppc_link_hash_table *htab)
  11805. {
  11806. bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
  11807. if (addr == NULL)
  11808. return NULL;
  11809. for (size_t i = 0; i < htab->relr_count; i++)
  11810. addr[i] = (htab->relr[i].sec->output_section->vma
  11811. + htab->relr[i].sec->output_offset
  11812. + htab->relr[i].off);
  11813. if (htab->relr_count > 1)
  11814. qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
  11815. return addr;
  11816. }
  11817. /* Look over GOT and PLT entries saved on elf_local_got_ents for all
  11818. input files, stashing info about needed relative relocs. */
  11819. static bool
  11820. got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
  11821. {
  11822. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11823. bfd *ibfd;
  11824. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  11825. {
  11826. struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
  11827. struct plt_entry **local_plt, **lplt, **end_local_plt;
  11828. Elf_Internal_Shdr *symtab_hdr;
  11829. bfd_size_type locsymcount;
  11830. Elf_Internal_Sym *local_syms;
  11831. Elf_Internal_Sym *isym;
  11832. struct plt_entry *pent;
  11833. struct got_entry *gent;
  11834. if (!is_ppc64_elf (ibfd))
  11835. continue;
  11836. lgot_ents = elf_local_got_ents (ibfd);
  11837. if (!lgot_ents)
  11838. continue;
  11839. symtab_hdr = &elf_symtab_hdr (ibfd);
  11840. locsymcount = symtab_hdr->sh_info;
  11841. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  11842. if (local_syms == NULL && locsymcount != 0)
  11843. {
  11844. local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
  11845. 0, NULL, NULL, NULL);
  11846. if (local_syms == NULL)
  11847. return false;
  11848. }
  11849. end_lgot_ents = lgot_ents + locsymcount;
  11850. local_plt = (struct plt_entry **) end_lgot_ents;
  11851. end_local_plt = local_plt + locsymcount;
  11852. for (lgot = lgot_ents, isym = local_syms;
  11853. lgot < end_lgot_ents;
  11854. ++lgot, ++isym)
  11855. for (gent = *lgot; gent != NULL; gent = gent->next)
  11856. if (!gent->is_indirect
  11857. && gent->tls_type == 0
  11858. && gent->got.offset != (bfd_vma) -1
  11859. && isym->st_shndx != SHN_ABS)
  11860. {
  11861. asection *got = ppc64_elf_tdata (gent->owner)->got;
  11862. if (!append_relr_off (htab, got, gent->got.offset))
  11863. {
  11864. htab->stub_error = true;
  11865. return false;
  11866. }
  11867. }
  11868. if (!htab->opd_abi)
  11869. for (lplt = local_plt, isym = local_syms;
  11870. lplt < end_local_plt;
  11871. ++lplt, ++isym)
  11872. for (pent = *lplt; pent != NULL; pent = pent->next)
  11873. if (pent->plt.offset != (bfd_vma) -1
  11874. && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
  11875. {
  11876. if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
  11877. {
  11878. if (symtab_hdr->contents != (unsigned char *) local_syms)
  11879. free (local_syms);
  11880. return false;
  11881. }
  11882. }
  11883. if (local_syms != NULL
  11884. && symtab_hdr->contents != (unsigned char *) local_syms)
  11885. {
  11886. if (!info->keep_memory)
  11887. free (local_syms);
  11888. else
  11889. symtab_hdr->contents = (unsigned char *) local_syms;
  11890. }
  11891. }
  11892. return true;
  11893. }
  11894. /* Stash info about needed GOT and PLT entry relative relocs for
  11895. global symbol H. */
  11896. static bool
  11897. got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
  11898. {
  11899. struct bfd_link_info *info;
  11900. struct ppc_link_hash_table *htab;
  11901. struct plt_entry *pent;
  11902. struct got_entry *gent;
  11903. if (h->root.type == bfd_link_hash_indirect)
  11904. return true;
  11905. info = (struct bfd_link_info *) inf;
  11906. htab = ppc_hash_table (info);
  11907. if (htab == NULL)
  11908. return false;
  11909. if (h->type != STT_GNU_IFUNC
  11910. && h->def_regular
  11911. && (h->root.type == bfd_link_hash_defined
  11912. || h->root.type == bfd_link_hash_defweak))
  11913. {
  11914. if ((!htab->elf.dynamic_sections_created
  11915. || h->dynindx == -1
  11916. || SYMBOL_REFERENCES_LOCAL (info, h))
  11917. && !bfd_is_abs_symbol (&h->root))
  11918. for (gent = h->got.glist; gent != NULL; gent = gent->next)
  11919. if (!gent->is_indirect
  11920. && gent->tls_type == 0
  11921. && gent->got.offset != (bfd_vma) -1)
  11922. {
  11923. asection *got = ppc64_elf_tdata (gent->owner)->got;
  11924. if (!append_relr_off (htab, got, gent->got.offset))
  11925. {
  11926. htab->stub_error = true;
  11927. return false;
  11928. }
  11929. }
  11930. if (!htab->opd_abi
  11931. && use_local_plt (info, h))
  11932. for (pent = h->plt.plist; pent != NULL; pent = pent->next)
  11933. if (pent->plt.offset != (bfd_vma) -1)
  11934. {
  11935. if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
  11936. {
  11937. htab->stub_error = true;
  11938. return false;
  11939. }
  11940. }
  11941. }
  11942. return true;
  11943. }
  11944. /* Determine and set the size of the stub section for a final link.
  11945. The basic idea here is to examine all the relocations looking for
  11946. PC-relative calls to a target that is unreachable with a "bl"
  11947. instruction. */
  11948. bool
  11949. ppc64_elf_size_stubs (struct bfd_link_info *info)
  11950. {
  11951. bfd_size_type stub_group_size;
  11952. bool stubs_always_before_branch;
  11953. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  11954. if (htab == NULL)
  11955. return false;
  11956. if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
  11957. htab->params->power10_stubs = 0;
  11958. if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
  11959. htab->params->plt_thread_safe = 1;
  11960. if (!htab->opd_abi)
  11961. htab->params->plt_thread_safe = 0;
  11962. else if (htab->params->plt_thread_safe == -1)
  11963. {
  11964. static const char *const thread_starter[] =
  11965. {
  11966. "pthread_create",
  11967. /* libstdc++ */
  11968. "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
  11969. /* librt */
  11970. "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
  11971. "mq_notify", "create_timer",
  11972. /* libanl */
  11973. "getaddrinfo_a",
  11974. /* libgomp */
  11975. "GOMP_parallel",
  11976. "GOMP_parallel_start",
  11977. "GOMP_parallel_loop_static",
  11978. "GOMP_parallel_loop_static_start",
  11979. "GOMP_parallel_loop_dynamic",
  11980. "GOMP_parallel_loop_dynamic_start",
  11981. "GOMP_parallel_loop_guided",
  11982. "GOMP_parallel_loop_guided_start",
  11983. "GOMP_parallel_loop_runtime",
  11984. "GOMP_parallel_loop_runtime_start",
  11985. "GOMP_parallel_sections",
  11986. "GOMP_parallel_sections_start",
  11987. /* libgo */
  11988. "__go_go",
  11989. };
  11990. unsigned i;
  11991. for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
  11992. {
  11993. struct elf_link_hash_entry *h;
  11994. h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
  11995. false, false, true);
  11996. htab->params->plt_thread_safe = h != NULL && h->ref_regular;
  11997. if (htab->params->plt_thread_safe)
  11998. break;
  11999. }
  12000. }
  12001. stubs_always_before_branch = htab->params->group_size < 0;
  12002. if (htab->params->group_size < 0)
  12003. stub_group_size = -htab->params->group_size;
  12004. else
  12005. stub_group_size = htab->params->group_size;
  12006. if (!group_sections (info, stub_group_size, stubs_always_before_branch))
  12007. return false;
  12008. htab->tga_group = NULL;
  12009. if (!htab->params->no_tls_get_addr_regsave
  12010. && htab->tga_desc_fd != NULL
  12011. && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
  12012. || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
  12013. && htab->tls_get_addr_fd != NULL
  12014. && is_static_defined (&htab->tls_get_addr_fd->elf))
  12015. {
  12016. asection *sym_sec, *code_sec, *stub_sec;
  12017. bfd_vma sym_value;
  12018. struct _opd_sec_data *opd;
  12019. sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
  12020. sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
  12021. code_sec = sym_sec;
  12022. opd = get_opd_info (sym_sec);
  12023. if (opd != NULL)
  12024. opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
  12025. htab->tga_group = htab->sec_info[code_sec->id].u.group;
  12026. stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
  12027. htab->tga_group->link_sec);
  12028. if (stub_sec == NULL)
  12029. return false;
  12030. htab->tga_group->stub_sec = stub_sec;
  12031. htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
  12032. htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
  12033. htab->tga_desc_fd->elf.root.u.def.value = 0;
  12034. htab->tga_desc_fd->elf.type = STT_FUNC;
  12035. htab->tga_desc_fd->elf.def_regular = 1;
  12036. htab->tga_desc_fd->elf.non_elf = 0;
  12037. _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
  12038. }
  12039. /* Loop until no stubs added. After iteration 20 of this loop we may
  12040. exit on a stub section shrinking. */
  12041. while (1)
  12042. {
  12043. bfd *input_bfd;
  12044. unsigned int bfd_indx;
  12045. struct map_stub *group;
  12046. htab->stub_iteration += 1;
  12047. htab->relr_count = 0;
  12048. for (input_bfd = info->input_bfds, bfd_indx = 0;
  12049. input_bfd != NULL;
  12050. input_bfd = input_bfd->link.next, bfd_indx++)
  12051. {
  12052. Elf_Internal_Shdr *symtab_hdr;
  12053. asection *section;
  12054. Elf_Internal_Sym *local_syms = NULL;
  12055. if (!is_ppc64_elf (input_bfd))
  12056. continue;
  12057. /* We'll need the symbol table in a second. */
  12058. symtab_hdr = &elf_symtab_hdr (input_bfd);
  12059. if (symtab_hdr->sh_info == 0)
  12060. continue;
  12061. /* Walk over each section attached to the input bfd. */
  12062. for (section = input_bfd->sections;
  12063. section != NULL;
  12064. section = section->next)
  12065. {
  12066. Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  12067. bool is_opd;
  12068. /* If there aren't any relocs, then there's nothing more
  12069. to do. */
  12070. if ((section->flags & SEC_RELOC) == 0
  12071. || (section->flags & SEC_ALLOC) == 0
  12072. || (section->flags & SEC_LOAD) == 0
  12073. || section->reloc_count == 0)
  12074. continue;
  12075. if (!info->enable_dt_relr
  12076. && (section->flags & SEC_CODE) == 0)
  12077. continue;
  12078. /* If this section is a link-once section that will be
  12079. discarded, then don't create any stubs. */
  12080. if (section->output_section == NULL
  12081. || section->output_section->owner != info->output_bfd)
  12082. continue;
  12083. /* Get the relocs. */
  12084. internal_relocs
  12085. = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
  12086. info->keep_memory);
  12087. if (internal_relocs == NULL)
  12088. goto error_ret_free_local;
  12089. is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
  12090. /* Now examine each relocation. */
  12091. irela = internal_relocs;
  12092. irelaend = irela + section->reloc_count;
  12093. for (; irela < irelaend; irela++)
  12094. {
  12095. enum elf_ppc64_reloc_type r_type;
  12096. unsigned int r_indx;
  12097. struct ppc_stub_type stub_type;
  12098. struct ppc_stub_hash_entry *stub_entry;
  12099. asection *sym_sec, *code_sec;
  12100. bfd_vma sym_value, code_value;
  12101. bfd_vma destination;
  12102. unsigned long local_off;
  12103. bool ok_dest;
  12104. struct ppc_link_hash_entry *hash;
  12105. struct ppc_link_hash_entry *fdh;
  12106. struct elf_link_hash_entry *h;
  12107. Elf_Internal_Sym *sym;
  12108. char *stub_name;
  12109. const asection *id_sec;
  12110. struct _opd_sec_data *opd;
  12111. struct plt_entry *plt_ent;
  12112. r_type = ELF64_R_TYPE (irela->r_info);
  12113. r_indx = ELF64_R_SYM (irela->r_info);
  12114. if (r_type >= R_PPC64_max)
  12115. {
  12116. bfd_set_error (bfd_error_bad_value);
  12117. goto error_ret_free_internal;
  12118. }
  12119. /* Only look for stubs on branch instructions. */
  12120. switch (r_type)
  12121. {
  12122. default:
  12123. continue;
  12124. case R_PPC64_REL24:
  12125. case R_PPC64_REL24_NOTOC:
  12126. case R_PPC64_REL24_P9NOTOC:
  12127. case R_PPC64_REL14:
  12128. case R_PPC64_REL14_BRTAKEN:
  12129. case R_PPC64_REL14_BRNTAKEN:
  12130. if ((section->flags & SEC_CODE) != 0)
  12131. break;
  12132. continue;
  12133. case R_PPC64_ADDR64:
  12134. case R_PPC64_TOC:
  12135. if (info->enable_dt_relr
  12136. && irela->r_offset % 2 == 0
  12137. && section->alignment_power != 0)
  12138. break;
  12139. continue;
  12140. }
  12141. /* Now determine the call target, its name, value,
  12142. section. */
  12143. if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
  12144. r_indx, input_bfd))
  12145. goto error_ret_free_internal;
  12146. if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
  12147. {
  12148. /* Only locally defined symbols can possibly use
  12149. relative relocations. */
  12150. bfd_vma r_offset;
  12151. if ((sym_sec == NULL
  12152. || sym_sec->output_section == NULL)
  12153. /* No symbol is OK too. */
  12154. && !(sym != NULL && sym->st_shndx == 0)
  12155. /* Hack for __ehdr_start, which is undefined
  12156. at this point. */
  12157. && !(h != NULL && h->root.linker_def))
  12158. continue;
  12159. if (NO_OPD_RELOCS && is_opd)
  12160. continue;
  12161. if (!is_opd
  12162. && r_type == R_PPC64_ADDR64)
  12163. {
  12164. if (h != NULL
  12165. ? h->type == STT_GNU_IFUNC
  12166. : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  12167. continue;
  12168. if (h != NULL
  12169. ? bfd_is_abs_symbol (&h->root)
  12170. : sym->st_shndx == SHN_ABS)
  12171. continue;
  12172. if (h != NULL
  12173. && !SYMBOL_REFERENCES_LOCAL (info, h))
  12174. continue;
  12175. }
  12176. r_offset = _bfd_elf_section_offset (info->output_bfd,
  12177. info,
  12178. section,
  12179. irela->r_offset);
  12180. if (r_offset >= (bfd_vma) -2)
  12181. continue;
  12182. if (!append_relr_off (htab, section, r_offset))
  12183. goto error_ret_free_internal;
  12184. continue;
  12185. }
  12186. hash = ppc_elf_hash_entry (h);
  12187. ok_dest = false;
  12188. fdh = NULL;
  12189. sym_value = 0;
  12190. if (hash == NULL)
  12191. {
  12192. sym_value = sym->st_value;
  12193. if (sym_sec != NULL
  12194. && sym_sec->output_section != NULL)
  12195. ok_dest = true;
  12196. }
  12197. else if (hash->elf.root.type == bfd_link_hash_defined
  12198. || hash->elf.root.type == bfd_link_hash_defweak)
  12199. {
  12200. sym_value = hash->elf.root.u.def.value;
  12201. if (sym_sec->output_section != NULL)
  12202. ok_dest = true;
  12203. }
  12204. else if (hash->elf.root.type == bfd_link_hash_undefweak
  12205. || hash->elf.root.type == bfd_link_hash_undefined)
  12206. {
  12207. /* Recognise an old ABI func code entry sym, and
  12208. use the func descriptor sym instead if it is
  12209. defined. */
  12210. if (hash->elf.root.root.string[0] == '.'
  12211. && hash->oh != NULL)
  12212. {
  12213. fdh = ppc_follow_link (hash->oh);
  12214. if (fdh->elf.root.type == bfd_link_hash_defined
  12215. || fdh->elf.root.type == bfd_link_hash_defweak)
  12216. {
  12217. sym_sec = fdh->elf.root.u.def.section;
  12218. sym_value = fdh->elf.root.u.def.value;
  12219. if (sym_sec->output_section != NULL)
  12220. ok_dest = true;
  12221. }
  12222. else
  12223. fdh = NULL;
  12224. }
  12225. }
  12226. else
  12227. {
  12228. bfd_set_error (bfd_error_bad_value);
  12229. goto error_ret_free_internal;
  12230. }
  12231. destination = 0;
  12232. local_off = 0;
  12233. if (ok_dest)
  12234. {
  12235. sym_value += irela->r_addend;
  12236. destination = (sym_value
  12237. + sym_sec->output_offset
  12238. + sym_sec->output_section->vma);
  12239. local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
  12240. ? hash->elf.other
  12241. : sym->st_other);
  12242. }
  12243. code_sec = sym_sec;
  12244. code_value = sym_value;
  12245. opd = get_opd_info (sym_sec);
  12246. if (opd != NULL)
  12247. {
  12248. bfd_vma dest;
  12249. if (hash == NULL && opd->adjust != NULL)
  12250. {
  12251. long adjust = opd->adjust[OPD_NDX (sym_value)];
  12252. if (adjust == -1)
  12253. continue;
  12254. code_value += adjust;
  12255. sym_value += adjust;
  12256. }
  12257. dest = opd_entry_value (sym_sec, sym_value,
  12258. &code_sec, &code_value, false);
  12259. if (dest != (bfd_vma) -1)
  12260. {
  12261. destination = dest;
  12262. if (fdh != NULL)
  12263. {
  12264. /* Fixup old ABI sym to point at code
  12265. entry. */
  12266. hash->elf.root.type = bfd_link_hash_defweak;
  12267. hash->elf.root.u.def.section = code_sec;
  12268. hash->elf.root.u.def.value = code_value;
  12269. }
  12270. }
  12271. }
  12272. /* Determine what (if any) linker stub is needed. */
  12273. plt_ent = NULL;
  12274. stub_type.main = ppc_type_of_stub (section, irela, &hash,
  12275. &plt_ent, destination,
  12276. local_off);
  12277. stub_type.sub = ppc_stub_toc;
  12278. stub_type.r2save = 0;
  12279. if (r_type == R_PPC64_REL24_NOTOC
  12280. || r_type == R_PPC64_REL24_P9NOTOC)
  12281. {
  12282. enum ppc_stub_sub_type notoc = ppc_stub_notoc;
  12283. if (htab->params->power10_stubs == 0
  12284. || (r_type == R_PPC64_REL24_P9NOTOC
  12285. && htab->params->power10_stubs != 1))
  12286. notoc = ppc_stub_p9notoc;
  12287. if (stub_type.main == ppc_stub_plt_call)
  12288. stub_type.sub = notoc;
  12289. else if (stub_type.main == ppc_stub_long_branch
  12290. || (code_sec != NULL
  12291. && code_sec->output_section != NULL
  12292. && (((hash ? hash->elf.other : sym->st_other)
  12293. & STO_PPC64_LOCAL_MASK)
  12294. > 1 << STO_PPC64_LOCAL_BIT)))
  12295. {
  12296. stub_type.main = ppc_stub_long_branch;
  12297. stub_type.sub = notoc;
  12298. stub_type.r2save = 0;
  12299. }
  12300. }
  12301. else if (stub_type.main != ppc_stub_plt_call)
  12302. {
  12303. /* Check whether we need a TOC adjusting stub.
  12304. Since the linker pastes together pieces from
  12305. different object files when creating the
  12306. _init and _fini functions, it may be that a
  12307. call to what looks like a local sym is in
  12308. fact a call needing a TOC adjustment. */
  12309. if ((code_sec != NULL
  12310. && code_sec->output_section != NULL
  12311. && (code_sec->has_toc_reloc
  12312. || code_sec->makes_toc_func_call)
  12313. && (htab->sec_info[code_sec->id].toc_off
  12314. != htab->sec_info[section->id].toc_off))
  12315. || (((hash ? hash->elf.other : sym->st_other)
  12316. & STO_PPC64_LOCAL_MASK)
  12317. == 1 << STO_PPC64_LOCAL_BIT))
  12318. {
  12319. stub_type.main = ppc_stub_long_branch;
  12320. stub_type.sub = ppc_stub_toc;
  12321. stub_type.r2save = 1;
  12322. }
  12323. }
  12324. if (stub_type.main == ppc_stub_none)
  12325. continue;
  12326. /* __tls_get_addr calls might be eliminated. */
  12327. if (stub_type.main != ppc_stub_plt_call
  12328. && hash != NULL
  12329. && is_tls_get_addr (&hash->elf, htab)
  12330. && section->has_tls_reloc
  12331. && irela != internal_relocs)
  12332. {
  12333. /* Get tls info. */
  12334. unsigned char *tls_mask;
  12335. if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
  12336. irela - 1, input_bfd))
  12337. goto error_ret_free_internal;
  12338. if ((*tls_mask & TLS_TLS) != 0
  12339. && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
  12340. continue;
  12341. }
  12342. if (stub_type.main == ppc_stub_plt_call
  12343. && stub_type.sub == ppc_stub_toc)
  12344. {
  12345. if (!htab->opd_abi
  12346. && htab->params->plt_localentry0 != 0
  12347. && is_elfv2_localentry0 (&hash->elf))
  12348. htab->has_plt_localentry0 = 1;
  12349. else if (irela + 1 < irelaend
  12350. && irela[1].r_offset == irela->r_offset + 4
  12351. && (ELF64_R_TYPE (irela[1].r_info)
  12352. == R_PPC64_TOCSAVE))
  12353. {
  12354. if (!tocsave_find (htab, INSERT,
  12355. &local_syms, irela + 1, input_bfd))
  12356. goto error_ret_free_internal;
  12357. }
  12358. else
  12359. stub_type.r2save = 1;
  12360. }
  12361. /* Support for grouping stub sections. */
  12362. id_sec = htab->sec_info[section->id].u.group->link_sec;
  12363. /* Get the name of this stub. */
  12364. stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
  12365. if (!stub_name)
  12366. goto error_ret_free_internal;
  12367. stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
  12368. stub_name, false, false);
  12369. if (stub_entry != NULL)
  12370. {
  12371. free (stub_name);
  12372. if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
  12373. {
  12374. /* xgettext:c-format */
  12375. _bfd_error_handler
  12376. (_("%pB: cannot create stub entry %s"),
  12377. section->owner, stub_entry->root.string);
  12378. goto error_ret_free_internal;
  12379. }
  12380. continue;
  12381. }
  12382. stub_entry = ppc_add_stub (stub_name, section, info);
  12383. if (stub_entry == NULL)
  12384. {
  12385. free (stub_name);
  12386. error_ret_free_internal:
  12387. if (elf_section_data (section)->relocs == NULL)
  12388. free (internal_relocs);
  12389. error_ret_free_local:
  12390. if (symtab_hdr->contents
  12391. != (unsigned char *) local_syms)
  12392. free (local_syms);
  12393. return false;
  12394. }
  12395. stub_entry->type = stub_type;
  12396. if (stub_type.main == ppc_stub_plt_call)
  12397. {
  12398. stub_entry->target_value = sym_value;
  12399. stub_entry->target_section = sym_sec;
  12400. }
  12401. else
  12402. {
  12403. stub_entry->target_value = code_value;
  12404. stub_entry->target_section = code_sec;
  12405. }
  12406. stub_entry->h = hash;
  12407. stub_entry->plt_ent = plt_ent;
  12408. stub_entry->symtype
  12409. = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
  12410. stub_entry->other = hash ? hash->elf.other : sym->st_other;
  12411. if (hash != NULL
  12412. && (hash->elf.root.type == bfd_link_hash_defined
  12413. || hash->elf.root.type == bfd_link_hash_defweak))
  12414. htab->stub_globals += 1;
  12415. }
  12416. /* We're done with the internal relocs, free them. */
  12417. if (elf_section_data (section)->relocs != internal_relocs)
  12418. free (internal_relocs);
  12419. }
  12420. if (local_syms != NULL
  12421. && symtab_hdr->contents != (unsigned char *) local_syms)
  12422. {
  12423. if (!info->keep_memory)
  12424. free (local_syms);
  12425. else
  12426. symtab_hdr->contents = (unsigned char *) local_syms;
  12427. }
  12428. }
  12429. /* We may have added some stubs. Find out the new size of the
  12430. stub sections. */
  12431. for (group = htab->group; group != NULL; group = group->next)
  12432. {
  12433. group->lr_restore = 0;
  12434. group->eh_size = 0;
  12435. if (group->stub_sec != NULL)
  12436. {
  12437. asection *stub_sec = group->stub_sec;
  12438. stub_sec->rawsize = stub_sec->size;
  12439. stub_sec->size = 0;
  12440. stub_sec->reloc_count = 0;
  12441. stub_sec->flags &= ~SEC_RELOC;
  12442. }
  12443. }
  12444. if (htab->tga_group != NULL)
  12445. {
  12446. /* See emit_tga_desc and emit_tga_desc_eh_frame. */
  12447. htab->tga_group->eh_size
  12448. = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
  12449. htab->tga_group->lr_restore = 23 * 4;
  12450. htab->tga_group->stub_sec->size = 24 * 4;
  12451. }
  12452. htab->brlt->rawsize = htab->brlt->size;
  12453. htab->brlt->size = 0;
  12454. htab->brlt->reloc_count = 0;
  12455. htab->brlt->flags &= ~SEC_RELOC;
  12456. if (htab->relbrlt != NULL)
  12457. htab->relbrlt->size = 0;
  12458. if (htab->elf.srelrdyn != NULL)
  12459. {
  12460. htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
  12461. htab->elf.srelrdyn->size = 0;
  12462. }
  12463. htab->stub_changed = false;
  12464. bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
  12465. for (group = htab->group; group != NULL; group = group->next)
  12466. if (group->needs_save_res)
  12467. group->stub_sec->size += htab->sfpr->size;
  12468. if (info->emitrelocations
  12469. && htab->glink != NULL && htab->glink->size != 0)
  12470. {
  12471. htab->glink->reloc_count = 1;
  12472. htab->glink->flags |= SEC_RELOC;
  12473. }
  12474. if (htab->glink_eh_frame != NULL
  12475. && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
  12476. && htab->glink_eh_frame->output_section->size > 8)
  12477. {
  12478. size_t size = 0, align = 4;
  12479. for (group = htab->group; group != NULL; group = group->next)
  12480. if (group->eh_size != 0)
  12481. size += (group->eh_size + 17 + align - 1) & -align;
  12482. if (htab->glink != NULL && htab->glink->size != 0)
  12483. size += (24 + align - 1) & -align;
  12484. if (size != 0)
  12485. size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
  12486. align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
  12487. size = (size + align - 1) & -align;
  12488. htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
  12489. htab->glink_eh_frame->size = size;
  12490. }
  12491. if (htab->params->plt_stub_align != 0)
  12492. for (group = htab->group; group != NULL; group = group->next)
  12493. if (group->stub_sec != NULL)
  12494. {
  12495. int align = abs (htab->params->plt_stub_align);
  12496. group->stub_sec->size
  12497. = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
  12498. }
  12499. if (htab->elf.srelrdyn != NULL)
  12500. {
  12501. bfd_vma r_offset;
  12502. for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
  12503. if (!append_relr_off (htab, htab->brlt, r_offset))
  12504. return false;
  12505. if (!got_and_plt_relr_for_local_syms (info))
  12506. return false;
  12507. elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
  12508. if (htab->stub_error)
  12509. return false;
  12510. bfd_vma *relr_addr = sort_relr (htab);
  12511. if (htab->relr_count != 0 && relr_addr == NULL)
  12512. return false;
  12513. size_t i = 0;
  12514. while (i < htab->relr_count)
  12515. {
  12516. bfd_vma base = relr_addr[i];
  12517. htab->elf.srelrdyn->size += 8;
  12518. i++;
  12519. /* Handle possible duplicate address. This can happen
  12520. as sections increase in size when adding stubs. */
  12521. while (i < htab->relr_count
  12522. && relr_addr[i] == base)
  12523. i++;
  12524. base += 8;
  12525. while (1)
  12526. {
  12527. size_t start_i = i;
  12528. while (i < htab->relr_count
  12529. && relr_addr[i] - base < 63 * 8
  12530. && (relr_addr[i] - base) % 8 == 0)
  12531. i++;
  12532. if (i == start_i)
  12533. break;
  12534. htab->elf.srelrdyn->size += 8;
  12535. base += 63 * 8;
  12536. }
  12537. }
  12538. free (relr_addr);
  12539. }
  12540. for (group = htab->group; group != NULL; group = group->next)
  12541. if (group->stub_sec != NULL
  12542. && group->stub_sec->rawsize != group->stub_sec->size
  12543. && (htab->stub_iteration <= STUB_SHRINK_ITER
  12544. || group->stub_sec->rawsize < group->stub_sec->size))
  12545. break;
  12546. if (group == NULL
  12547. && (!htab->stub_changed
  12548. || htab->stub_iteration > STUB_SHRINK_ITER)
  12549. && (htab->brlt->rawsize == htab->brlt->size
  12550. || (htab->stub_iteration > STUB_SHRINK_ITER
  12551. && htab->brlt->rawsize > htab->brlt->size))
  12552. && (htab->elf.srelrdyn == NULL
  12553. || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
  12554. || (htab->stub_iteration > STUB_SHRINK_ITER
  12555. && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
  12556. && (htab->glink_eh_frame == NULL
  12557. || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
  12558. && (htab->tga_group == NULL
  12559. || htab->stub_iteration > 1))
  12560. break;
  12561. if (htab->stub_iteration > STUB_SHRINK_ITER)
  12562. {
  12563. for (group = htab->group; group != NULL; group = group->next)
  12564. if (group->stub_sec != NULL
  12565. && group->stub_sec->size < group->stub_sec->rawsize)
  12566. group->stub_sec->size = group->stub_sec->rawsize;
  12567. if (htab->brlt->size < htab->brlt->rawsize)
  12568. htab->brlt->size = htab->brlt->rawsize;
  12569. if (htab->elf.srelrdyn != NULL
  12570. && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
  12571. htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
  12572. }
  12573. /* Ask the linker to do its stuff. */
  12574. (*htab->params->layout_sections_again) ();
  12575. }
  12576. if (htab->glink_eh_frame != NULL
  12577. && htab->glink_eh_frame->size != 0)
  12578. {
  12579. bfd_vma val;
  12580. bfd_byte *p, *last_fde;
  12581. size_t last_fde_len, size, align, pad;
  12582. struct map_stub *group;
  12583. /* It is necessary to at least have a rough outline of the
  12584. linker generated CIEs and FDEs written before
  12585. bfd_elf_discard_info is run, in order for these FDEs to be
  12586. indexed in .eh_frame_hdr. */
  12587. p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
  12588. if (p == NULL)
  12589. return false;
  12590. htab->glink_eh_frame->contents = p;
  12591. last_fde = p;
  12592. align = 4;
  12593. memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
  12594. /* CIE length (rewrite in case little-endian). */
  12595. last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
  12596. bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
  12597. p += last_fde_len + 4;
  12598. for (group = htab->group; group != NULL; group = group->next)
  12599. if (group->eh_size != 0)
  12600. {
  12601. group->eh_base = p - htab->glink_eh_frame->contents;
  12602. last_fde = p;
  12603. last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
  12604. /* FDE length. */
  12605. bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
  12606. p += 4;
  12607. /* CIE pointer. */
  12608. val = p - htab->glink_eh_frame->contents;
  12609. bfd_put_32 (htab->elf.dynobj, val, p);
  12610. p += 4;
  12611. /* Offset to stub section, written later. */
  12612. p += 4;
  12613. /* stub section size. */
  12614. bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
  12615. p += 4;
  12616. /* Augmentation. */
  12617. p += 1;
  12618. /* Make sure we don't have all nops. This is enough for
  12619. elf-eh-frame.c to detect the last non-nop opcode. */
  12620. p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
  12621. p = last_fde + last_fde_len + 4;
  12622. }
  12623. if (htab->glink != NULL && htab->glink->size != 0)
  12624. {
  12625. last_fde = p;
  12626. last_fde_len = ((24 + align - 1) & -align) - 4;
  12627. /* FDE length. */
  12628. bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
  12629. p += 4;
  12630. /* CIE pointer. */
  12631. val = p - htab->glink_eh_frame->contents;
  12632. bfd_put_32 (htab->elf.dynobj, val, p);
  12633. p += 4;
  12634. /* Offset to .glink, written later. */
  12635. p += 4;
  12636. /* .glink size. */
  12637. bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
  12638. p += 4;
  12639. /* Augmentation. */
  12640. p += 1;
  12641. *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
  12642. *p++ = DW_CFA_register;
  12643. *p++ = 65;
  12644. *p++ = htab->opd_abi ? 12 : 0;
  12645. *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
  12646. *p++ = DW_CFA_restore_extended;
  12647. *p++ = 65;
  12648. p += ((24 + align - 1) & -align) - 24;
  12649. }
  12650. /* Subsume any padding into the last FDE if user .eh_frame
  12651. sections are aligned more than glink_eh_frame. Otherwise any
  12652. zero padding will be seen as a terminator. */
  12653. align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
  12654. size = p - htab->glink_eh_frame->contents;
  12655. pad = ((size + align - 1) & -align) - size;
  12656. htab->glink_eh_frame->size = size + pad;
  12657. bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
  12658. }
  12659. maybe_strip_output (info, htab->brlt);
  12660. if (htab->relbrlt != NULL)
  12661. maybe_strip_output (info, htab->relbrlt);
  12662. if (htab->glink_eh_frame != NULL)
  12663. maybe_strip_output (info, htab->glink_eh_frame);
  12664. if (htab->elf.srelrdyn != NULL)
  12665. maybe_strip_output (info, htab->elf.srelrdyn);
  12666. return true;
  12667. }
  12668. /* Called after we have determined section placement. If sections
  12669. move, we'll be called again. Provide a value for TOCstart. */
  12670. bfd_vma
  12671. ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
  12672. {
  12673. asection *s;
  12674. bfd_vma TOCstart, adjust;
  12675. if (info != NULL)
  12676. {
  12677. struct elf_link_hash_entry *h;
  12678. struct elf_link_hash_table *htab = elf_hash_table (info);
  12679. if (is_elf_hash_table (&htab->root)
  12680. && htab->hgot != NULL)
  12681. h = htab->hgot;
  12682. else
  12683. {
  12684. h = (struct elf_link_hash_entry *)
  12685. bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
  12686. if (is_elf_hash_table (&htab->root))
  12687. htab->hgot = h;
  12688. }
  12689. if (h != NULL
  12690. && h->root.type == bfd_link_hash_defined
  12691. && !h->root.linker_def
  12692. && (!is_elf_hash_table (&htab->root)
  12693. || h->def_regular))
  12694. {
  12695. TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
  12696. _bfd_set_gp_value (obfd, TOCstart);
  12697. return TOCstart;
  12698. }
  12699. }
  12700. /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
  12701. order. The TOC starts where the first of these sections starts. */
  12702. s = bfd_get_section_by_name (obfd, ".got");
  12703. if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
  12704. s = bfd_get_section_by_name (obfd, ".toc");
  12705. if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
  12706. s = bfd_get_section_by_name (obfd, ".tocbss");
  12707. if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
  12708. s = bfd_get_section_by_name (obfd, ".plt");
  12709. if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
  12710. {
  12711. /* This may happen for
  12712. o references to TOC base (SYM@toc / TOC[tc0]) without a
  12713. .toc directive
  12714. o bad linker script
  12715. o --gc-sections and empty TOC sections
  12716. FIXME: Warn user? */
  12717. /* Look for a likely section. We probably won't even be
  12718. using TOCstart. */
  12719. for (s = obfd->sections; s != NULL; s = s->next)
  12720. if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
  12721. | SEC_EXCLUDE))
  12722. == (SEC_ALLOC | SEC_SMALL_DATA))
  12723. break;
  12724. if (s == NULL)
  12725. for (s = obfd->sections; s != NULL; s = s->next)
  12726. if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
  12727. == (SEC_ALLOC | SEC_SMALL_DATA))
  12728. break;
  12729. if (s == NULL)
  12730. for (s = obfd->sections; s != NULL; s = s->next)
  12731. if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
  12732. == SEC_ALLOC)
  12733. break;
  12734. if (s == NULL)
  12735. for (s = obfd->sections; s != NULL; s = s->next)
  12736. if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
  12737. break;
  12738. }
  12739. TOCstart = 0;
  12740. if (s != NULL)
  12741. TOCstart = s->output_section->vma + s->output_offset;
  12742. /* Force alignment. */
  12743. adjust = TOCstart & (TOC_BASE_ALIGN - 1);
  12744. TOCstart -= adjust;
  12745. _bfd_set_gp_value (obfd, TOCstart);
  12746. if (info != NULL && s != NULL)
  12747. {
  12748. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  12749. if (htab != NULL)
  12750. {
  12751. if (htab->elf.hgot != NULL)
  12752. {
  12753. htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
  12754. htab->elf.hgot->root.u.def.section = s;
  12755. }
  12756. }
  12757. else
  12758. {
  12759. struct bfd_link_hash_entry *bh = NULL;
  12760. _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
  12761. s, TOC_BASE_OFF - adjust,
  12762. NULL, false, false, &bh);
  12763. }
  12764. }
  12765. return TOCstart;
  12766. }
  12767. /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
  12768. write out any global entry stubs, and PLT relocations. */
  12769. static bool
  12770. build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
  12771. {
  12772. struct bfd_link_info *info;
  12773. struct ppc_link_hash_table *htab;
  12774. struct plt_entry *ent;
  12775. asection *s;
  12776. if (h->root.type == bfd_link_hash_indirect)
  12777. return true;
  12778. info = inf;
  12779. htab = ppc_hash_table (info);
  12780. if (htab == NULL)
  12781. return false;
  12782. for (ent = h->plt.plist; ent != NULL; ent = ent->next)
  12783. if (ent->plt.offset != (bfd_vma) -1)
  12784. {
  12785. /* This symbol has an entry in the procedure linkage
  12786. table. Set it up. */
  12787. Elf_Internal_Rela rela;
  12788. asection *plt, *relplt;
  12789. bfd_byte *loc;
  12790. if (use_local_plt (info, h))
  12791. {
  12792. if (!(h->def_regular
  12793. && (h->root.type == bfd_link_hash_defined
  12794. || h->root.type == bfd_link_hash_defweak)))
  12795. continue;
  12796. if (h->type == STT_GNU_IFUNC)
  12797. {
  12798. plt = htab->elf.iplt;
  12799. relplt = htab->elf.irelplt;
  12800. htab->elf.ifunc_resolvers = true;
  12801. if (htab->opd_abi)
  12802. rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
  12803. else
  12804. rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
  12805. }
  12806. else
  12807. {
  12808. plt = htab->pltlocal;
  12809. relplt = NULL;
  12810. if (bfd_link_pic (info)
  12811. && !(info->enable_dt_relr && !htab->opd_abi))
  12812. {
  12813. relplt = htab->relpltlocal;
  12814. if (htab->opd_abi)
  12815. rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
  12816. else
  12817. rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
  12818. }
  12819. }
  12820. rela.r_addend = defined_sym_val (h) + ent->addend;
  12821. if (relplt == NULL)
  12822. {
  12823. loc = plt->contents + ent->plt.offset;
  12824. bfd_put_64 (info->output_bfd, rela.r_addend, loc);
  12825. if (htab->opd_abi)
  12826. {
  12827. bfd_vma toc = elf_gp (info->output_bfd);
  12828. toc += htab->sec_info[h->root.u.def.section->id].toc_off;
  12829. bfd_put_64 (info->output_bfd, toc, loc + 8);
  12830. }
  12831. }
  12832. else
  12833. {
  12834. rela.r_offset = (plt->output_section->vma
  12835. + plt->output_offset
  12836. + ent->plt.offset);
  12837. loc = relplt->contents + (relplt->reloc_count++
  12838. * sizeof (Elf64_External_Rela));
  12839. bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
  12840. }
  12841. }
  12842. else
  12843. {
  12844. rela.r_offset = (htab->elf.splt->output_section->vma
  12845. + htab->elf.splt->output_offset
  12846. + ent->plt.offset);
  12847. rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
  12848. rela.r_addend = ent->addend;
  12849. loc = (htab->elf.srelplt->contents
  12850. + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
  12851. / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
  12852. if (h->type == STT_GNU_IFUNC && is_static_defined (h))
  12853. htab->elf.ifunc_resolvers = true;
  12854. bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
  12855. }
  12856. }
  12857. if (!h->pointer_equality_needed)
  12858. return true;
  12859. if (h->def_regular)
  12860. return true;
  12861. s = htab->global_entry;
  12862. if (s == NULL || s->size == 0)
  12863. return true;
  12864. for (ent = h->plt.plist; ent != NULL; ent = ent->next)
  12865. if (ent->plt.offset != (bfd_vma) -1
  12866. && ent->addend == 0)
  12867. {
  12868. bfd_byte *p;
  12869. asection *plt;
  12870. bfd_vma off;
  12871. p = s->contents + h->root.u.def.value;
  12872. plt = htab->elf.splt;
  12873. if (use_local_plt (info, h))
  12874. {
  12875. if (h->type == STT_GNU_IFUNC)
  12876. plt = htab->elf.iplt;
  12877. else
  12878. plt = htab->pltlocal;
  12879. }
  12880. off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
  12881. off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
  12882. if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
  12883. {
  12884. info->callbacks->einfo
  12885. (_("%P: linkage table error against `%pT'\n"),
  12886. h->root.root.string);
  12887. bfd_set_error (bfd_error_bad_value);
  12888. htab->stub_error = true;
  12889. }
  12890. htab->stub_count[ppc_stub_global_entry - 1] += 1;
  12891. if (htab->params->emit_stub_syms)
  12892. {
  12893. size_t len = strlen (h->root.root.string);
  12894. char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
  12895. if (name == NULL)
  12896. return false;
  12897. sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
  12898. h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
  12899. if (h == NULL)
  12900. return false;
  12901. if (h->root.type == bfd_link_hash_new)
  12902. {
  12903. h->root.type = bfd_link_hash_defined;
  12904. h->root.u.def.section = s;
  12905. h->root.u.def.value = p - s->contents;
  12906. h->ref_regular = 1;
  12907. h->def_regular = 1;
  12908. h->ref_regular_nonweak = 1;
  12909. h->forced_local = 1;
  12910. h->non_elf = 0;
  12911. h->root.linker_def = 1;
  12912. }
  12913. }
  12914. if (PPC_HA (off) != 0)
  12915. {
  12916. bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
  12917. p += 4;
  12918. }
  12919. bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
  12920. p += 4;
  12921. bfd_put_32 (s->owner, MTCTR_R12, p);
  12922. p += 4;
  12923. bfd_put_32 (s->owner, BCTR, p);
  12924. break;
  12925. }
  12926. return true;
  12927. }
  12928. /* Write PLT relocs for locals. */
  12929. static bool
  12930. write_plt_relocs_for_local_syms (struct bfd_link_info *info)
  12931. {
  12932. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  12933. bfd *ibfd;
  12934. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  12935. {
  12936. struct got_entry **lgot_ents, **end_lgot_ents;
  12937. struct plt_entry **local_plt, **lplt, **end_local_plt;
  12938. Elf_Internal_Shdr *symtab_hdr;
  12939. bfd_size_type locsymcount;
  12940. Elf_Internal_Sym *local_syms = NULL;
  12941. struct plt_entry *ent;
  12942. if (!is_ppc64_elf (ibfd))
  12943. continue;
  12944. lgot_ents = elf_local_got_ents (ibfd);
  12945. if (!lgot_ents)
  12946. continue;
  12947. symtab_hdr = &elf_symtab_hdr (ibfd);
  12948. locsymcount = symtab_hdr->sh_info;
  12949. end_lgot_ents = lgot_ents + locsymcount;
  12950. local_plt = (struct plt_entry **) end_lgot_ents;
  12951. end_local_plt = local_plt + locsymcount;
  12952. for (lplt = local_plt; lplt < end_local_plt; ++lplt)
  12953. for (ent = *lplt; ent != NULL; ent = ent->next)
  12954. if (ent->plt.offset != (bfd_vma) -1)
  12955. {
  12956. Elf_Internal_Sym *sym;
  12957. asection *sym_sec;
  12958. asection *plt, *relplt;
  12959. bfd_byte *loc;
  12960. bfd_vma val;
  12961. if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
  12962. lplt - local_plt, ibfd))
  12963. {
  12964. if (symtab_hdr->contents != (unsigned char *) local_syms)
  12965. free (local_syms);
  12966. return false;
  12967. }
  12968. val = sym->st_value + ent->addend;
  12969. if (sym_sec != NULL && sym_sec->output_section != NULL)
  12970. val += sym_sec->output_offset + sym_sec->output_section->vma;
  12971. if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  12972. {
  12973. htab->elf.ifunc_resolvers = true;
  12974. plt = htab->elf.iplt;
  12975. relplt = htab->elf.irelplt;
  12976. }
  12977. else
  12978. {
  12979. plt = htab->pltlocal;
  12980. relplt = NULL;
  12981. if (bfd_link_pic (info)
  12982. && !(info->enable_dt_relr && !htab->opd_abi))
  12983. relplt = htab->relpltlocal;
  12984. }
  12985. if (relplt == NULL)
  12986. {
  12987. loc = plt->contents + ent->plt.offset;
  12988. bfd_put_64 (info->output_bfd, val, loc);
  12989. if (htab->opd_abi)
  12990. {
  12991. bfd_vma toc = elf_gp (ibfd);
  12992. bfd_put_64 (info->output_bfd, toc, loc + 8);
  12993. }
  12994. }
  12995. else
  12996. {
  12997. Elf_Internal_Rela rela;
  12998. rela.r_offset = (ent->plt.offset
  12999. + plt->output_offset
  13000. + plt->output_section->vma);
  13001. if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  13002. {
  13003. if (htab->opd_abi)
  13004. rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
  13005. else
  13006. rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
  13007. }
  13008. else
  13009. {
  13010. if (htab->opd_abi)
  13011. rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
  13012. else
  13013. rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
  13014. }
  13015. rela.r_addend = val;
  13016. loc = relplt->contents + (relplt->reloc_count++
  13017. * sizeof (Elf64_External_Rela));
  13018. bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
  13019. }
  13020. }
  13021. if (local_syms != NULL
  13022. && symtab_hdr->contents != (unsigned char *) local_syms)
  13023. {
  13024. if (!info->keep_memory)
  13025. free (local_syms);
  13026. else
  13027. symtab_hdr->contents = (unsigned char *) local_syms;
  13028. }
  13029. }
  13030. return true;
  13031. }
  13032. /* Emit the static wrapper function preserving registers around a
  13033. __tls_get_addr_opt call. */
  13034. static bool
  13035. emit_tga_desc (struct ppc_link_hash_table *htab)
  13036. {
  13037. asection *stub_sec = htab->tga_group->stub_sec;
  13038. unsigned int cfa_updt = 11 * 4;
  13039. bfd_byte *p;
  13040. bfd_vma to, from, delta;
  13041. BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
  13042. && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
  13043. && htab->tga_desc_fd->elf.root.u.def.value == 0);
  13044. to = defined_sym_val (&htab->tls_get_addr_fd->elf);
  13045. from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
  13046. delta = to - from;
  13047. if (delta + (1 << 25) >= 1 << 26)
  13048. {
  13049. _bfd_error_handler (_("__tls_get_addr call offset overflow"));
  13050. htab->stub_error = true;
  13051. return false;
  13052. }
  13053. p = stub_sec->contents;
  13054. p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
  13055. bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
  13056. p += 4;
  13057. p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
  13058. return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
  13059. }
  13060. /* Emit eh_frame describing the static wrapper function. */
  13061. static bfd_byte *
  13062. emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
  13063. {
  13064. unsigned int cfa_updt = 11 * 4;
  13065. unsigned int i;
  13066. *p++ = DW_CFA_advance_loc + cfa_updt / 4;
  13067. *p++ = DW_CFA_def_cfa_offset;
  13068. if (htab->opd_abi)
  13069. {
  13070. *p++ = 128;
  13071. *p++ = 1;
  13072. }
  13073. else
  13074. *p++ = 96;
  13075. *p++ = DW_CFA_offset_extended_sf;
  13076. *p++ = 65;
  13077. *p++ = (-16 / 8) & 0x7f;
  13078. for (i = 4; i < 12; i++)
  13079. {
  13080. *p++ = DW_CFA_offset + i;
  13081. *p++ = (htab->opd_abi ? 13 : 12) - i;
  13082. }
  13083. *p++ = DW_CFA_advance_loc + 10;
  13084. *p++ = DW_CFA_def_cfa_offset;
  13085. *p++ = 0;
  13086. for (i = 4; i < 12; i++)
  13087. *p++ = DW_CFA_restore + i;
  13088. *p++ = DW_CFA_advance_loc + 2;
  13089. *p++ = DW_CFA_restore_extended;
  13090. *p++ = 65;
  13091. return p;
  13092. }
  13093. /* Build all the stubs associated with the current output file.
  13094. The stubs are kept in a hash table attached to the main linker
  13095. hash table. This function is called via gldelf64ppc_finish. */
  13096. bool
  13097. ppc64_elf_build_stubs (struct bfd_link_info *info,
  13098. char **stats)
  13099. {
  13100. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  13101. struct map_stub *group;
  13102. asection *stub_sec;
  13103. bfd_byte *p;
  13104. int stub_sec_count = 0;
  13105. if (htab == NULL)
  13106. return false;
  13107. /* Allocate memory to hold the linker stubs. */
  13108. for (group = htab->group; group != NULL; group = group->next)
  13109. {
  13110. group->eh_size = 0;
  13111. group->lr_restore = 0;
  13112. if ((stub_sec = group->stub_sec) != NULL
  13113. && stub_sec->size != 0)
  13114. {
  13115. stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
  13116. stub_sec->size);
  13117. if (stub_sec->contents == NULL)
  13118. return false;
  13119. stub_sec->size = 0;
  13120. }
  13121. }
  13122. if (htab->glink != NULL && htab->glink->size != 0)
  13123. {
  13124. unsigned int indx;
  13125. bfd_vma plt0;
  13126. /* Build the .glink plt call stub. */
  13127. if (htab->params->emit_stub_syms)
  13128. {
  13129. struct elf_link_hash_entry *h;
  13130. h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
  13131. true, false, false);
  13132. if (h == NULL)
  13133. return false;
  13134. if (h->root.type == bfd_link_hash_new)
  13135. {
  13136. h->root.type = bfd_link_hash_defined;
  13137. h->root.u.def.section = htab->glink;
  13138. h->root.u.def.value = 8;
  13139. h->ref_regular = 1;
  13140. h->def_regular = 1;
  13141. h->ref_regular_nonweak = 1;
  13142. h->forced_local = 1;
  13143. h->non_elf = 0;
  13144. h->root.linker_def = 1;
  13145. }
  13146. }
  13147. plt0 = (htab->elf.splt->output_section->vma
  13148. + htab->elf.splt->output_offset
  13149. - 16);
  13150. if (info->emitrelocations)
  13151. {
  13152. Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
  13153. if (r == NULL)
  13154. return false;
  13155. r->r_offset = (htab->glink->output_offset
  13156. + htab->glink->output_section->vma);
  13157. r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
  13158. r->r_addend = plt0;
  13159. }
  13160. p = htab->glink->contents;
  13161. plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
  13162. bfd_put_64 (htab->glink->owner, plt0, p);
  13163. p += 8;
  13164. if (htab->opd_abi)
  13165. {
  13166. bfd_put_32 (htab->glink->owner, MFLR_R12, p);
  13167. p += 4;
  13168. bfd_put_32 (htab->glink->owner, BCL_20_31, p);
  13169. p += 4;
  13170. bfd_put_32 (htab->glink->owner, MFLR_R11, p);
  13171. p += 4;
  13172. bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
  13173. p += 4;
  13174. bfd_put_32 (htab->glink->owner, MTLR_R12, p);
  13175. p += 4;
  13176. bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
  13177. p += 4;
  13178. bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
  13179. p += 4;
  13180. bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
  13181. p += 4;
  13182. bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
  13183. p += 4;
  13184. bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
  13185. p += 4;
  13186. }
  13187. else
  13188. {
  13189. unsigned int insn;
  13190. /* 0:
  13191. . .quad plt0-1f # plt0 entry relative to 1:
  13192. #
  13193. # We get here with r12 initially @ a glink branch
  13194. # Load the address of _dl_runtime_resolve from plt0 and
  13195. # jump to it, with r0 set to the index of the PLT entry
  13196. # to be resolved and r11 the link map.
  13197. __glink_PLTresolve:
  13198. . std %r2,24(%r1) # optional
  13199. . mflr %r0
  13200. . bcl 20,31,1f
  13201. 1:
  13202. . mflr %r11
  13203. . mtlr %r0
  13204. . ld %r0,(0b-1b)(%r11)
  13205. . sub %r12,%r12,%r11
  13206. . add %r11,%r0,%r11
  13207. . addi %r0,%r12,1b-2f
  13208. . ld %r12,0(%r11)
  13209. . srdi %r0,%r0,2
  13210. . mtctr %r12
  13211. . ld %r11,8(%r11)
  13212. . bctr
  13213. 2:
  13214. . b __glink_PLTresolve
  13215. . ...
  13216. . b __glink_PLTresolve */
  13217. if (htab->has_plt_localentry0)
  13218. {
  13219. bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
  13220. p += 4;
  13221. }
  13222. bfd_put_32 (htab->glink->owner, MFLR_R0, p);
  13223. p += 4;
  13224. bfd_put_32 (htab->glink->owner, BCL_20_31, p);
  13225. p += 4;
  13226. bfd_put_32 (htab->glink->owner, MFLR_R11, p);
  13227. p += 4;
  13228. bfd_put_32 (htab->glink->owner, MTLR_R0, p);
  13229. p += 4;
  13230. if (htab->has_plt_localentry0)
  13231. insn = LD_R0_0R11 | (-20 & 0xfffc);
  13232. else
  13233. insn = LD_R0_0R11 | (-16 & 0xfffc);
  13234. bfd_put_32 (htab->glink->owner, insn, p);
  13235. p += 4;
  13236. bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
  13237. p += 4;
  13238. bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
  13239. p += 4;
  13240. bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
  13241. p += 4;
  13242. bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
  13243. p += 4;
  13244. bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
  13245. p += 4;
  13246. bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
  13247. p += 4;
  13248. bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
  13249. p += 4;
  13250. }
  13251. bfd_put_32 (htab->glink->owner, BCTR, p);
  13252. p += 4;
  13253. BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
  13254. /* Build the .glink lazy link call stubs. */
  13255. indx = 0;
  13256. while (p < htab->glink->contents + htab->glink->size)
  13257. {
  13258. if (htab->opd_abi)
  13259. {
  13260. if (indx < 0x8000)
  13261. {
  13262. bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
  13263. p += 4;
  13264. }
  13265. else
  13266. {
  13267. bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
  13268. p += 4;
  13269. bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
  13270. p);
  13271. p += 4;
  13272. }
  13273. }
  13274. bfd_put_32 (htab->glink->owner,
  13275. B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
  13276. indx++;
  13277. p += 4;
  13278. }
  13279. }
  13280. if (htab->tga_group != NULL)
  13281. {
  13282. htab->tga_group->lr_restore = 23 * 4;
  13283. htab->tga_group->stub_sec->size = 24 * 4;
  13284. if (!emit_tga_desc (htab))
  13285. return false;
  13286. if (htab->glink_eh_frame != NULL
  13287. && htab->glink_eh_frame->size != 0)
  13288. {
  13289. size_t align = 4;
  13290. p = htab->glink_eh_frame->contents;
  13291. p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
  13292. p += 17;
  13293. htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
  13294. }
  13295. }
  13296. /* Build .glink global entry stubs, and PLT relocs for globals. */
  13297. elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
  13298. if (!write_plt_relocs_for_local_syms (info))
  13299. return false;
  13300. if (htab->brlt != NULL && htab->brlt->size != 0)
  13301. {
  13302. htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
  13303. htab->brlt->size);
  13304. if (htab->brlt->contents == NULL)
  13305. return false;
  13306. }
  13307. if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
  13308. {
  13309. htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
  13310. htab->relbrlt->size);
  13311. if (htab->relbrlt->contents == NULL)
  13312. return false;
  13313. }
  13314. /* Build the stubs as directed by the stub hash table. */
  13315. bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
  13316. for (group = htab->group; group != NULL; group = group->next)
  13317. if (group->needs_save_res)
  13318. group->stub_sec->size += htab->sfpr->size;
  13319. if (htab->relbrlt != NULL)
  13320. htab->relbrlt->reloc_count = 0;
  13321. if (htab->params->plt_stub_align != 0)
  13322. for (group = htab->group; group != NULL; group = group->next)
  13323. if ((stub_sec = group->stub_sec) != NULL)
  13324. {
  13325. int align = abs (htab->params->plt_stub_align);
  13326. stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
  13327. }
  13328. for (group = htab->group; group != NULL; group = group->next)
  13329. if (group->needs_save_res)
  13330. {
  13331. stub_sec = group->stub_sec;
  13332. memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
  13333. htab->sfpr->contents, htab->sfpr->size);
  13334. if (htab->params->emit_stub_syms)
  13335. {
  13336. unsigned int i;
  13337. for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
  13338. if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
  13339. return false;
  13340. }
  13341. }
  13342. if (htab->glink_eh_frame != NULL
  13343. && htab->glink_eh_frame->size != 0)
  13344. {
  13345. bfd_vma val;
  13346. size_t align = 4;
  13347. p = htab->glink_eh_frame->contents;
  13348. p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
  13349. for (group = htab->group; group != NULL; group = group->next)
  13350. if (group->eh_size != 0)
  13351. {
  13352. /* Offset to stub section. */
  13353. val = (group->stub_sec->output_section->vma
  13354. + group->stub_sec->output_offset);
  13355. val -= (htab->glink_eh_frame->output_section->vma
  13356. + htab->glink_eh_frame->output_offset
  13357. + (p + 8 - htab->glink_eh_frame->contents));
  13358. if (val + 0x80000000 > 0xffffffff)
  13359. {
  13360. _bfd_error_handler
  13361. (_("%s offset too large for .eh_frame sdata4 encoding"),
  13362. group->stub_sec->name);
  13363. return false;
  13364. }
  13365. bfd_put_32 (htab->elf.dynobj, val, p + 8);
  13366. p += (group->eh_size + 17 + 3) & -4;
  13367. }
  13368. if (htab->glink != NULL && htab->glink->size != 0)
  13369. {
  13370. /* Offset to .glink. */
  13371. val = (htab->glink->output_section->vma
  13372. + htab->glink->output_offset
  13373. + 8);
  13374. val -= (htab->glink_eh_frame->output_section->vma
  13375. + htab->glink_eh_frame->output_offset
  13376. + (p + 8 - htab->glink_eh_frame->contents));
  13377. if (val + 0x80000000 > 0xffffffff)
  13378. {
  13379. _bfd_error_handler
  13380. (_("%s offset too large for .eh_frame sdata4 encoding"),
  13381. htab->glink->name);
  13382. return false;
  13383. }
  13384. bfd_put_32 (htab->elf.dynobj, val, p + 8);
  13385. p += (24 + align - 1) & -align;
  13386. }
  13387. }
  13388. if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
  13389. {
  13390. htab->elf.srelrdyn->contents
  13391. = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
  13392. if (htab->elf.srelrdyn->contents == NULL)
  13393. return false;
  13394. bfd_vma *relr_addr = sort_relr (htab);
  13395. if (htab->relr_count != 0 && relr_addr == NULL)
  13396. return false;
  13397. size_t i = 0;
  13398. bfd_byte *loc = htab->elf.srelrdyn->contents;
  13399. while (i < htab->relr_count)
  13400. {
  13401. bfd_vma base = relr_addr[i];
  13402. BFD_ASSERT (base % 2 == 0);
  13403. bfd_put_64 (htab->elf.dynobj, base, loc);
  13404. loc += 8;
  13405. i++;
  13406. while (i < htab->relr_count
  13407. && relr_addr[i] == base)
  13408. {
  13409. htab->stub_error = true;
  13410. i++;
  13411. }
  13412. base += 8;
  13413. while (1)
  13414. {
  13415. bfd_vma bits = 0;
  13416. while (i < htab->relr_count
  13417. && relr_addr[i] - base < 63 * 8
  13418. && (relr_addr[i] - base) % 8 == 0)
  13419. {
  13420. bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
  13421. i++;
  13422. }
  13423. if (bits == 0)
  13424. break;
  13425. bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
  13426. loc += 8;
  13427. base += 63 * 8;
  13428. }
  13429. }
  13430. free (relr_addr);
  13431. /* Pad any excess with 1's, a do-nothing encoding. */
  13432. while ((size_t) (loc - htab->elf.srelrdyn->contents)
  13433. < htab->elf.srelrdyn->size)
  13434. {
  13435. bfd_put_64 (htab->elf.dynobj, 1, loc);
  13436. loc += 8;
  13437. }
  13438. }
  13439. for (group = htab->group; group != NULL; group = group->next)
  13440. if ((stub_sec = group->stub_sec) != NULL)
  13441. {
  13442. stub_sec_count += 1;
  13443. if (stub_sec->rawsize != stub_sec->size
  13444. && (htab->stub_iteration <= STUB_SHRINK_ITER
  13445. || stub_sec->rawsize < stub_sec->size))
  13446. break;
  13447. }
  13448. if (group != NULL)
  13449. htab->stub_error = true;
  13450. if (htab->stub_error)
  13451. {
  13452. _bfd_error_handler (_("stubs don't match calculated size"));
  13453. return false;
  13454. }
  13455. if (stats != NULL)
  13456. {
  13457. char *groupmsg;
  13458. if (asprintf (&groupmsg,
  13459. ngettext ("linker stubs in %u group\n",
  13460. "linker stubs in %u groups\n",
  13461. stub_sec_count),
  13462. stub_sec_count) < 0)
  13463. *stats = NULL;
  13464. else
  13465. {
  13466. if (asprintf (stats, _("%s"
  13467. " branch %lu\n"
  13468. " long branch %lu\n"
  13469. " plt call %lu\n"
  13470. " global entry %lu"),
  13471. groupmsg,
  13472. htab->stub_count[ppc_stub_long_branch - 1],
  13473. htab->stub_count[ppc_stub_plt_branch - 1],
  13474. htab->stub_count[ppc_stub_plt_call - 1],
  13475. htab->stub_count[ppc_stub_global_entry - 1]) < 0)
  13476. *stats = NULL;
  13477. free (groupmsg);
  13478. }
  13479. }
  13480. return true;
  13481. }
  13482. /* What to do when ld finds relocations against symbols defined in
  13483. discarded sections. */
  13484. static unsigned int
  13485. ppc64_elf_action_discarded (asection *sec)
  13486. {
  13487. if (strcmp (".opd", sec->name) == 0)
  13488. return 0;
  13489. if (strcmp (".toc", sec->name) == 0)
  13490. return 0;
  13491. if (strcmp (".toc1", sec->name) == 0)
  13492. return 0;
  13493. return _bfd_elf_default_action_discarded (sec);
  13494. }
  13495. /* These are the dynamic relocations supported by glibc. */
  13496. static bool
  13497. ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
  13498. {
  13499. switch (r_type)
  13500. {
  13501. case R_PPC64_RELATIVE:
  13502. case R_PPC64_NONE:
  13503. case R_PPC64_ADDR64:
  13504. case R_PPC64_GLOB_DAT:
  13505. case R_PPC64_IRELATIVE:
  13506. case R_PPC64_JMP_IREL:
  13507. case R_PPC64_JMP_SLOT:
  13508. case R_PPC64_DTPMOD64:
  13509. case R_PPC64_DTPREL64:
  13510. case R_PPC64_TPREL64:
  13511. case R_PPC64_TPREL16_LO_DS:
  13512. case R_PPC64_TPREL16_DS:
  13513. case R_PPC64_TPREL16:
  13514. case R_PPC64_TPREL16_LO:
  13515. case R_PPC64_TPREL16_HI:
  13516. case R_PPC64_TPREL16_HIGH:
  13517. case R_PPC64_TPREL16_HA:
  13518. case R_PPC64_TPREL16_HIGHA:
  13519. case R_PPC64_TPREL16_HIGHER:
  13520. case R_PPC64_TPREL16_HIGHEST:
  13521. case R_PPC64_TPREL16_HIGHERA:
  13522. case R_PPC64_TPREL16_HIGHESTA:
  13523. case R_PPC64_ADDR16_LO_DS:
  13524. case R_PPC64_ADDR16_LO:
  13525. case R_PPC64_ADDR16_HI:
  13526. case R_PPC64_ADDR16_HIGH:
  13527. case R_PPC64_ADDR16_HA:
  13528. case R_PPC64_ADDR16_HIGHA:
  13529. case R_PPC64_REL30:
  13530. case R_PPC64_COPY:
  13531. case R_PPC64_UADDR64:
  13532. case R_PPC64_UADDR32:
  13533. case R_PPC64_ADDR32:
  13534. case R_PPC64_ADDR24:
  13535. case R_PPC64_ADDR16:
  13536. case R_PPC64_UADDR16:
  13537. case R_PPC64_ADDR16_DS:
  13538. case R_PPC64_ADDR16_HIGHER:
  13539. case R_PPC64_ADDR16_HIGHEST:
  13540. case R_PPC64_ADDR16_HIGHERA:
  13541. case R_PPC64_ADDR16_HIGHESTA:
  13542. case R_PPC64_ADDR14:
  13543. case R_PPC64_ADDR14_BRTAKEN:
  13544. case R_PPC64_ADDR14_BRNTAKEN:
  13545. case R_PPC64_REL32:
  13546. case R_PPC64_REL64:
  13547. return true;
  13548. default:
  13549. return false;
  13550. }
  13551. }
  13552. /* The RELOCATE_SECTION function is called by the ELF backend linker
  13553. to handle the relocations for a section.
  13554. The relocs are always passed as Rela structures; if the section
  13555. actually uses Rel structures, the r_addend field will always be
  13556. zero.
  13557. This function is responsible for adjust the section contents as
  13558. necessary, and (if using Rela relocs and generating a
  13559. relocatable output file) adjusting the reloc addend as
  13560. necessary.
  13561. This function does not have to worry about setting the reloc
  13562. address or the reloc symbol index.
  13563. LOCAL_SYMS is a pointer to the swapped in local symbols.
  13564. LOCAL_SECTIONS is an array giving the section in the input file
  13565. corresponding to the st_shndx field of each local symbol.
  13566. The global hash table entry for the global symbols can be found
  13567. via elf_sym_hashes (input_bfd).
  13568. When generating relocatable output, this function must handle
  13569. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  13570. going to be the section symbol corresponding to the output
  13571. section, which means that the addend must be adjusted
  13572. accordingly. */
  13573. static int
  13574. ppc64_elf_relocate_section (bfd *output_bfd,
  13575. struct bfd_link_info *info,
  13576. bfd *input_bfd,
  13577. asection *input_section,
  13578. bfd_byte *contents,
  13579. Elf_Internal_Rela *relocs,
  13580. Elf_Internal_Sym *local_syms,
  13581. asection **local_sections)
  13582. {
  13583. struct ppc_link_hash_table *htab;
  13584. Elf_Internal_Shdr *symtab_hdr;
  13585. struct elf_link_hash_entry **sym_hashes;
  13586. Elf_Internal_Rela *rel;
  13587. Elf_Internal_Rela *wrel;
  13588. Elf_Internal_Rela *relend;
  13589. Elf_Internal_Rela outrel;
  13590. bfd_byte *loc;
  13591. struct got_entry **local_got_ents;
  13592. bfd_vma TOCstart;
  13593. bool ret = true;
  13594. bool is_opd;
  13595. /* Assume 'at' branch hints. */
  13596. bool is_isa_v2 = true;
  13597. bool warned_dynamic = false;
  13598. bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
  13599. /* Initialize howto table if needed. */
  13600. if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
  13601. ppc_howto_init ();
  13602. htab = ppc_hash_table (info);
  13603. if (htab == NULL)
  13604. return false;
  13605. /* Don't relocate stub sections. */
  13606. if (input_section->owner == htab->params->stub_bfd)
  13607. return true;
  13608. if (!is_ppc64_elf (input_bfd))
  13609. {
  13610. bfd_set_error (bfd_error_wrong_format);
  13611. return false;
  13612. }
  13613. local_got_ents = elf_local_got_ents (input_bfd);
  13614. TOCstart = elf_gp (output_bfd);
  13615. symtab_hdr = &elf_symtab_hdr (input_bfd);
  13616. sym_hashes = elf_sym_hashes (input_bfd);
  13617. is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
  13618. rel = wrel = relocs;
  13619. relend = relocs + input_section->reloc_count;
  13620. for (; rel < relend; wrel++, rel++)
  13621. {
  13622. enum elf_ppc64_reloc_type r_type;
  13623. bfd_vma addend;
  13624. bfd_reloc_status_type r;
  13625. Elf_Internal_Sym *sym;
  13626. asection *sec;
  13627. struct elf_link_hash_entry *h_elf;
  13628. struct ppc_link_hash_entry *h;
  13629. struct ppc_link_hash_entry *fdh;
  13630. const char *sym_name;
  13631. unsigned long r_symndx, toc_symndx;
  13632. bfd_vma toc_addend;
  13633. unsigned char tls_mask, tls_gd, tls_type;
  13634. unsigned char sym_type;
  13635. bfd_vma relocation;
  13636. bool unresolved_reloc, save_unresolved_reloc;
  13637. bool warned;
  13638. enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
  13639. unsigned int insn;
  13640. unsigned int mask;
  13641. struct ppc_stub_hash_entry *stub_entry;
  13642. bfd_vma max_br_offset;
  13643. bfd_vma from;
  13644. Elf_Internal_Rela orig_rel;
  13645. reloc_howto_type *howto;
  13646. struct reloc_howto_struct alt_howto;
  13647. uint64_t pinsn;
  13648. bfd_vma offset;
  13649. again:
  13650. orig_rel = *rel;
  13651. r_type = ELF64_R_TYPE (rel->r_info);
  13652. r_symndx = ELF64_R_SYM (rel->r_info);
  13653. /* For old style R_PPC64_TOC relocs with a zero symbol, use the
  13654. symbol of the previous ADDR64 reloc. The symbol gives us the
  13655. proper TOC base to use. */
  13656. if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
  13657. && wrel != relocs
  13658. && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
  13659. && is_opd)
  13660. r_symndx = ELF64_R_SYM (wrel[-1].r_info);
  13661. sym = NULL;
  13662. sec = NULL;
  13663. h_elf = NULL;
  13664. sym_name = NULL;
  13665. unresolved_reloc = false;
  13666. warned = false;
  13667. if (r_symndx < symtab_hdr->sh_info)
  13668. {
  13669. /* It's a local symbol. */
  13670. struct _opd_sec_data *opd;
  13671. sym = local_syms + r_symndx;
  13672. sec = local_sections[r_symndx];
  13673. sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
  13674. sym_type = ELF64_ST_TYPE (sym->st_info);
  13675. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  13676. opd = get_opd_info (sec);
  13677. if (opd != NULL && opd->adjust != NULL)
  13678. {
  13679. long adjust = opd->adjust[OPD_NDX (sym->st_value
  13680. + rel->r_addend)];
  13681. if (adjust == -1)
  13682. relocation = 0;
  13683. else
  13684. {
  13685. /* If this is a relocation against the opd section sym
  13686. and we have edited .opd, adjust the reloc addend so
  13687. that ld -r and ld --emit-relocs output is correct.
  13688. If it is a reloc against some other .opd symbol,
  13689. then the symbol value will be adjusted later. */
  13690. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  13691. rel->r_addend += adjust;
  13692. else
  13693. relocation += adjust;
  13694. }
  13695. }
  13696. }
  13697. else
  13698. {
  13699. bool ignored;
  13700. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  13701. r_symndx, symtab_hdr, sym_hashes,
  13702. h_elf, sec, relocation,
  13703. unresolved_reloc, warned, ignored);
  13704. sym_name = h_elf->root.root.string;
  13705. sym_type = h_elf->type;
  13706. if (sec != NULL
  13707. && sec->owner == output_bfd
  13708. && strcmp (sec->name, ".opd") == 0)
  13709. {
  13710. /* This is a symbol defined in a linker script. All
  13711. such are defined in output sections, even those
  13712. defined by simple assignment from a symbol defined in
  13713. an input section. Transfer the symbol to an
  13714. appropriate input .opd section, so that a branch to
  13715. this symbol will be mapped to the location specified
  13716. by the opd entry. */
  13717. struct bfd_link_order *lo;
  13718. for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
  13719. if (lo->type == bfd_indirect_link_order)
  13720. {
  13721. asection *isec = lo->u.indirect.section;
  13722. if (h_elf->root.u.def.value >= isec->output_offset
  13723. && h_elf->root.u.def.value < (isec->output_offset
  13724. + isec->size))
  13725. {
  13726. h_elf->root.u.def.value -= isec->output_offset;
  13727. h_elf->root.u.def.section = isec;
  13728. sec = isec;
  13729. break;
  13730. }
  13731. }
  13732. }
  13733. }
  13734. h = ppc_elf_hash_entry (h_elf);
  13735. if (sec != NULL && discarded_section (sec))
  13736. {
  13737. _bfd_clear_contents (ppc64_elf_howto_table[r_type],
  13738. input_bfd, input_section,
  13739. contents, rel->r_offset);
  13740. wrel->r_offset = rel->r_offset;
  13741. wrel->r_info = 0;
  13742. wrel->r_addend = 0;
  13743. /* For ld -r, remove relocations in debug sections against
  13744. symbols defined in discarded sections. Not done for
  13745. non-debug to preserve relocs in .eh_frame which the
  13746. eh_frame editing code expects to be present. */
  13747. if (bfd_link_relocatable (info)
  13748. && (input_section->flags & SEC_DEBUGGING))
  13749. wrel--;
  13750. continue;
  13751. }
  13752. if (bfd_link_relocatable (info))
  13753. goto copy_reloc;
  13754. if (h != NULL && &h->elf == htab->elf.hgot)
  13755. {
  13756. relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
  13757. sec = bfd_abs_section_ptr;
  13758. unresolved_reloc = false;
  13759. }
  13760. /* TLS optimizations. Replace instruction sequences and relocs
  13761. based on information we collected in tls_optimize. We edit
  13762. RELOCS so that --emit-relocs will output something sensible
  13763. for the final instruction stream. */
  13764. tls_mask = 0;
  13765. tls_gd = 0;
  13766. toc_symndx = 0;
  13767. if (h != NULL)
  13768. tls_mask = h->tls_mask;
  13769. else if (local_got_ents != NULL)
  13770. {
  13771. struct plt_entry **local_plt = (struct plt_entry **)
  13772. (local_got_ents + symtab_hdr->sh_info);
  13773. unsigned char *lgot_masks = (unsigned char *)
  13774. (local_plt + symtab_hdr->sh_info);
  13775. tls_mask = lgot_masks[r_symndx];
  13776. }
  13777. if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
  13778. && (r_type == R_PPC64_TLS
  13779. || r_type == R_PPC64_TLSGD
  13780. || r_type == R_PPC64_TLSLD))
  13781. {
  13782. /* Check for toc tls entries. */
  13783. unsigned char *toc_tls;
  13784. if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
  13785. &local_syms, rel, input_bfd))
  13786. return false;
  13787. if (toc_tls)
  13788. tls_mask = *toc_tls;
  13789. }
  13790. /* Check that tls relocs are used with tls syms, and non-tls
  13791. relocs are used with non-tls syms. */
  13792. if (r_symndx != STN_UNDEF
  13793. && r_type != R_PPC64_NONE
  13794. && (h == NULL
  13795. || h->elf.root.type == bfd_link_hash_defined
  13796. || h->elf.root.type == bfd_link_hash_defweak)
  13797. && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
  13798. {
  13799. if ((tls_mask & TLS_TLS) != 0
  13800. && (r_type == R_PPC64_TLS
  13801. || r_type == R_PPC64_TLSGD
  13802. || r_type == R_PPC64_TLSLD))
  13803. /* R_PPC64_TLS is OK against a symbol in the TOC. */
  13804. ;
  13805. else
  13806. info->callbacks->einfo
  13807. (!IS_PPC64_TLS_RELOC (r_type)
  13808. /* xgettext:c-format */
  13809. ? _("%H: %s used with TLS symbol `%pT'\n")
  13810. /* xgettext:c-format */
  13811. : _("%H: %s used with non-TLS symbol `%pT'\n"),
  13812. input_bfd, input_section, rel->r_offset,
  13813. ppc64_elf_howto_table[r_type]->name,
  13814. sym_name);
  13815. }
  13816. /* Ensure reloc mapping code below stays sane. */
  13817. if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
  13818. || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
  13819. || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
  13820. || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
  13821. || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
  13822. || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
  13823. || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
  13824. || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
  13825. || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
  13826. || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
  13827. abort ();
  13828. switch (r_type)
  13829. {
  13830. default:
  13831. break;
  13832. case R_PPC64_LO_DS_OPT:
  13833. if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
  13834. {
  13835. insn = bfd_get_32 (input_bfd,
  13836. contents + rel->r_offset - d_offset);
  13837. if ((insn & (0x3fu << 26)) != 58u << 26)
  13838. abort ();
  13839. insn += (14u << 26) - (58u << 26);
  13840. bfd_put_32 (input_bfd, insn,
  13841. contents + rel->r_offset - d_offset);
  13842. r_type = R_PPC64_TOC16_LO;
  13843. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  13844. }
  13845. break;
  13846. case R_PPC64_TOC16:
  13847. case R_PPC64_TOC16_LO:
  13848. case R_PPC64_TOC16_DS:
  13849. case R_PPC64_TOC16_LO_DS:
  13850. {
  13851. /* Check for toc tls entries. */
  13852. unsigned char *toc_tls;
  13853. int retval;
  13854. retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
  13855. &local_syms, rel, input_bfd);
  13856. if (retval == 0)
  13857. return false;
  13858. if (toc_tls)
  13859. {
  13860. tls_mask = *toc_tls;
  13861. if (r_type == R_PPC64_TOC16_DS
  13862. || r_type == R_PPC64_TOC16_LO_DS)
  13863. {
  13864. if ((tls_mask & TLS_TLS) != 0
  13865. && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
  13866. goto toctprel;
  13867. }
  13868. else
  13869. {
  13870. /* If we found a GD reloc pair, then we might be
  13871. doing a GD->IE transition. */
  13872. if (retval == 2)
  13873. {
  13874. tls_gd = TLS_GDIE;
  13875. if ((tls_mask & TLS_TLS) != 0
  13876. && (tls_mask & TLS_GD) == 0)
  13877. goto tls_ldgd_opt;
  13878. }
  13879. else if (retval == 3)
  13880. {
  13881. if ((tls_mask & TLS_TLS) != 0
  13882. && (tls_mask & TLS_LD) == 0)
  13883. goto tls_ldgd_opt;
  13884. }
  13885. }
  13886. }
  13887. }
  13888. break;
  13889. case R_PPC64_GOT_TPREL16_HI:
  13890. case R_PPC64_GOT_TPREL16_HA:
  13891. if ((tls_mask & TLS_TLS) != 0
  13892. && (tls_mask & TLS_TPREL) == 0
  13893. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  13894. {
  13895. rel->r_offset -= d_offset;
  13896. bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
  13897. r_type = R_PPC64_NONE;
  13898. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  13899. }
  13900. break;
  13901. case R_PPC64_GOT_TPREL16_DS:
  13902. case R_PPC64_GOT_TPREL16_LO_DS:
  13903. if ((tls_mask & TLS_TLS) != 0
  13904. && (tls_mask & TLS_TPREL) == 0
  13905. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  13906. {
  13907. toctprel:
  13908. insn = bfd_get_32 (input_bfd,
  13909. contents + rel->r_offset - d_offset);
  13910. insn &= 31 << 21;
  13911. insn |= 0x3c0d0000; /* addis 0,13,0 */
  13912. bfd_put_32 (input_bfd, insn,
  13913. contents + rel->r_offset - d_offset);
  13914. r_type = R_PPC64_TPREL16_HA;
  13915. if (toc_symndx != 0)
  13916. {
  13917. rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
  13918. rel->r_addend = toc_addend;
  13919. /* We changed the symbol. Start over in order to
  13920. get h, sym, sec etc. right. */
  13921. goto again;
  13922. }
  13923. else
  13924. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  13925. }
  13926. break;
  13927. case R_PPC64_GOT_TPREL_PCREL34:
  13928. if ((tls_mask & TLS_TLS) != 0
  13929. && (tls_mask & TLS_TPREL) == 0
  13930. && offset_in_range (input_section, rel->r_offset, 8))
  13931. {
  13932. /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
  13933. pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  13934. pinsn <<= 32;
  13935. pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
  13936. pinsn += ((2ULL << 56) + (-1ULL << 52)
  13937. + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
  13938. bfd_put_32 (input_bfd, pinsn >> 32,
  13939. contents + rel->r_offset);
  13940. bfd_put_32 (input_bfd, pinsn & 0xffffffff,
  13941. contents + rel->r_offset + 4);
  13942. r_type = R_PPC64_TPREL34;
  13943. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  13944. }
  13945. break;
  13946. case R_PPC64_TLS:
  13947. if ((tls_mask & TLS_TLS) != 0
  13948. && (tls_mask & TLS_TPREL) == 0
  13949. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  13950. {
  13951. insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
  13952. insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
  13953. if (insn == 0)
  13954. break;
  13955. if ((rel->r_offset & 3) == 0)
  13956. {
  13957. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  13958. /* Was PPC64_TLS which sits on insn boundary, now
  13959. PPC64_TPREL16_LO which is at low-order half-word. */
  13960. rel->r_offset += d_offset;
  13961. r_type = R_PPC64_TPREL16_LO;
  13962. if (toc_symndx != 0)
  13963. {
  13964. rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
  13965. rel->r_addend = toc_addend;
  13966. /* We changed the symbol. Start over in order to
  13967. get h, sym, sec etc. right. */
  13968. goto again;
  13969. }
  13970. else
  13971. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  13972. }
  13973. else if ((rel->r_offset & 3) == 1)
  13974. {
  13975. /* For pcrel IE to LE we already have the full
  13976. offset and thus don't need an addi here. A nop
  13977. or mr will do. */
  13978. if ((insn & (0x3fu << 26)) == 14 << 26)
  13979. {
  13980. /* Extract regs from addi rt,ra,si. */
  13981. unsigned int rt = (insn >> 21) & 0x1f;
  13982. unsigned int ra = (insn >> 16) & 0x1f;
  13983. if (rt == ra)
  13984. insn = NOP;
  13985. else
  13986. {
  13987. /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
  13988. insn = (rt << 16) | (ra << 21) | (ra << 11);
  13989. insn |= (31u << 26) | (444u << 1);
  13990. }
  13991. }
  13992. bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
  13993. }
  13994. }
  13995. break;
  13996. case R_PPC64_GOT_TLSGD16_HI:
  13997. case R_PPC64_GOT_TLSGD16_HA:
  13998. tls_gd = TLS_GDIE;
  13999. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
  14000. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  14001. goto tls_gdld_hi;
  14002. break;
  14003. case R_PPC64_GOT_TLSLD16_HI:
  14004. case R_PPC64_GOT_TLSLD16_HA:
  14005. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
  14006. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  14007. {
  14008. tls_gdld_hi:
  14009. if ((tls_mask & tls_gd) != 0)
  14010. r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
  14011. + R_PPC64_GOT_TPREL16_DS);
  14012. else
  14013. {
  14014. rel->r_offset -= d_offset;
  14015. bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
  14016. r_type = R_PPC64_NONE;
  14017. }
  14018. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14019. }
  14020. break;
  14021. case R_PPC64_GOT_TLSGD16:
  14022. case R_PPC64_GOT_TLSGD16_LO:
  14023. tls_gd = TLS_GDIE;
  14024. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
  14025. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  14026. goto tls_ldgd_opt;
  14027. break;
  14028. case R_PPC64_GOT_TLSLD16:
  14029. case R_PPC64_GOT_TLSLD16_LO:
  14030. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
  14031. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  14032. {
  14033. unsigned int insn1, insn2;
  14034. tls_ldgd_opt:
  14035. offset = (bfd_vma) -1;
  14036. /* If not using the newer R_PPC64_TLSGD/LD to mark
  14037. __tls_get_addr calls, we must trust that the call
  14038. stays with its arg setup insns, ie. that the next
  14039. reloc is the __tls_get_addr call associated with
  14040. the current reloc. Edit both insns. */
  14041. if (input_section->nomark_tls_get_addr
  14042. && rel + 1 < relend
  14043. && branch_reloc_hash_match (input_bfd, rel + 1,
  14044. htab->tls_get_addr_fd,
  14045. htab->tga_desc_fd,
  14046. htab->tls_get_addr,
  14047. htab->tga_desc))
  14048. offset = rel[1].r_offset;
  14049. /* We read the low GOT_TLS (or TOC16) insn because we
  14050. need to keep the destination reg. It may be
  14051. something other than the usual r3, and moved to r3
  14052. before the call by intervening code. */
  14053. insn1 = bfd_get_32 (input_bfd,
  14054. contents + rel->r_offset - d_offset);
  14055. if ((tls_mask & tls_gd) != 0)
  14056. {
  14057. /* IE */
  14058. insn1 &= (0x1f << 21) | (0x1f << 16);
  14059. insn1 |= 58u << 26; /* ld */
  14060. insn2 = 0x7c636a14; /* add 3,3,13 */
  14061. if (offset != (bfd_vma) -1)
  14062. rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
  14063. if (r_type == R_PPC64_TOC16
  14064. || r_type == R_PPC64_TOC16_LO)
  14065. r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
  14066. else
  14067. r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
  14068. + R_PPC64_GOT_TPREL16_DS);
  14069. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14070. }
  14071. else
  14072. {
  14073. /* LE */
  14074. insn1 &= 0x1f << 21;
  14075. insn1 |= 0x3c0d0000; /* addis r,13,0 */
  14076. insn2 = 0x38630000; /* addi 3,3,0 */
  14077. if (tls_gd == 0)
  14078. {
  14079. /* Was an LD reloc. */
  14080. r_symndx = STN_UNDEF;
  14081. rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
  14082. }
  14083. else if (toc_symndx != 0)
  14084. {
  14085. r_symndx = toc_symndx;
  14086. rel->r_addend = toc_addend;
  14087. }
  14088. r_type = R_PPC64_TPREL16_HA;
  14089. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14090. if (offset != (bfd_vma) -1)
  14091. {
  14092. rel[1].r_info = ELF64_R_INFO (r_symndx,
  14093. R_PPC64_TPREL16_LO);
  14094. rel[1].r_offset = offset + d_offset;
  14095. rel[1].r_addend = rel->r_addend;
  14096. }
  14097. }
  14098. bfd_put_32 (input_bfd, insn1,
  14099. contents + rel->r_offset - d_offset);
  14100. if (offset != (bfd_vma) -1
  14101. && offset_in_range (input_section, offset, 4))
  14102. {
  14103. bfd_put_32 (input_bfd, insn2, contents + offset);
  14104. if (offset_in_range (input_section, offset + 4, 4))
  14105. {
  14106. insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
  14107. if (insn2 == LD_R2_0R1 + STK_TOC (htab))
  14108. bfd_put_32 (input_bfd, NOP, contents + offset + 4);
  14109. }
  14110. }
  14111. if ((tls_mask & tls_gd) == 0
  14112. && (tls_gd == 0 || toc_symndx != 0))
  14113. {
  14114. /* We changed the symbol. Start over in order
  14115. to get h, sym, sec etc. right. */
  14116. goto again;
  14117. }
  14118. }
  14119. break;
  14120. case R_PPC64_GOT_TLSGD_PCREL34:
  14121. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
  14122. && offset_in_range (input_section, rel->r_offset, 8))
  14123. {
  14124. pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  14125. pinsn <<= 32;
  14126. pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
  14127. if ((tls_mask & TLS_GDIE) != 0)
  14128. {
  14129. /* IE, pla -> pld */
  14130. pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
  14131. r_type = R_PPC64_GOT_TPREL_PCREL34;
  14132. }
  14133. else
  14134. {
  14135. /* LE, pla pcrel -> paddi r13 */
  14136. pinsn += (-1ULL << 52) + (13ULL << 16);
  14137. r_type = R_PPC64_TPREL34;
  14138. }
  14139. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14140. bfd_put_32 (input_bfd, pinsn >> 32,
  14141. contents + rel->r_offset);
  14142. bfd_put_32 (input_bfd, pinsn & 0xffffffff,
  14143. contents + rel->r_offset + 4);
  14144. }
  14145. break;
  14146. case R_PPC64_GOT_TLSLD_PCREL34:
  14147. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
  14148. && offset_in_range (input_section, rel->r_offset, 8))
  14149. {
  14150. pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  14151. pinsn <<= 32;
  14152. pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
  14153. pinsn += (-1ULL << 52) + (13ULL << 16);
  14154. bfd_put_32 (input_bfd, pinsn >> 32,
  14155. contents + rel->r_offset);
  14156. bfd_put_32 (input_bfd, pinsn & 0xffffffff,
  14157. contents + rel->r_offset + 4);
  14158. rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
  14159. r_symndx = STN_UNDEF;
  14160. r_type = R_PPC64_TPREL34;
  14161. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14162. goto again;
  14163. }
  14164. break;
  14165. case R_PPC64_TLSGD:
  14166. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
  14167. && rel + 1 < relend
  14168. && offset_in_range (input_section, rel->r_offset,
  14169. is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
  14170. ? 8 : 4))
  14171. {
  14172. unsigned int insn2;
  14173. enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
  14174. offset = rel->r_offset;
  14175. if (is_plt_seq_reloc (r_type1))
  14176. {
  14177. bfd_put_32 (output_bfd, NOP, contents + offset);
  14178. if (r_type1 == R_PPC64_PLT_PCREL34
  14179. || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
  14180. bfd_put_32 (output_bfd, NOP, contents + offset + 4);
  14181. rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
  14182. break;
  14183. }
  14184. if (r_type1 == R_PPC64_PLTCALL)
  14185. bfd_put_32 (output_bfd, NOP, contents + offset + 4);
  14186. if ((tls_mask & TLS_GDIE) != 0)
  14187. {
  14188. /* IE */
  14189. r_type = R_PPC64_NONE;
  14190. insn2 = 0x7c636a14; /* add 3,3,13 */
  14191. }
  14192. else
  14193. {
  14194. /* LE */
  14195. if (toc_symndx != 0)
  14196. {
  14197. r_symndx = toc_symndx;
  14198. rel->r_addend = toc_addend;
  14199. }
  14200. if (r_type1 == R_PPC64_REL24_NOTOC
  14201. || r_type1 == R_PPC64_REL24_P9NOTOC
  14202. || r_type1 == R_PPC64_PLTCALL_NOTOC)
  14203. {
  14204. r_type = R_PPC64_NONE;
  14205. insn2 = NOP;
  14206. }
  14207. else
  14208. {
  14209. rel->r_offset = offset + d_offset;
  14210. r_type = R_PPC64_TPREL16_LO;
  14211. insn2 = 0x38630000; /* addi 3,3,0 */
  14212. }
  14213. }
  14214. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14215. /* Zap the reloc on the _tls_get_addr call too. */
  14216. BFD_ASSERT (offset == rel[1].r_offset);
  14217. rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
  14218. bfd_put_32 (input_bfd, insn2, contents + offset);
  14219. if ((tls_mask & TLS_GDIE) == 0
  14220. && toc_symndx != 0
  14221. && r_type != R_PPC64_NONE)
  14222. goto again;
  14223. }
  14224. break;
  14225. case R_PPC64_TLSLD:
  14226. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
  14227. && rel + 1 < relend
  14228. && offset_in_range (input_section, rel->r_offset,
  14229. is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
  14230. ? 8 : 4))
  14231. {
  14232. unsigned int insn2;
  14233. enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
  14234. offset = rel->r_offset;
  14235. if (is_plt_seq_reloc (r_type1))
  14236. {
  14237. bfd_put_32 (output_bfd, NOP, contents + offset);
  14238. if (r_type1 == R_PPC64_PLT_PCREL34
  14239. || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
  14240. bfd_put_32 (output_bfd, NOP, contents + offset + 4);
  14241. rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
  14242. break;
  14243. }
  14244. if (r_type1 == R_PPC64_PLTCALL)
  14245. bfd_put_32 (output_bfd, NOP, contents + offset + 4);
  14246. if (r_type1 == R_PPC64_REL24_NOTOC
  14247. || r_type1 == R_PPC64_REL24_P9NOTOC
  14248. || r_type1 == R_PPC64_PLTCALL_NOTOC)
  14249. {
  14250. r_type = R_PPC64_NONE;
  14251. insn2 = NOP;
  14252. }
  14253. else
  14254. {
  14255. rel->r_offset = offset + d_offset;
  14256. r_symndx = STN_UNDEF;
  14257. r_type = R_PPC64_TPREL16_LO;
  14258. rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
  14259. insn2 = 0x38630000; /* addi 3,3,0 */
  14260. }
  14261. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14262. /* Zap the reloc on the _tls_get_addr call too. */
  14263. BFD_ASSERT (offset == rel[1].r_offset);
  14264. rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
  14265. bfd_put_32 (input_bfd, insn2, contents + offset);
  14266. if (r_type != R_PPC64_NONE)
  14267. goto again;
  14268. }
  14269. break;
  14270. case R_PPC64_DTPMOD64:
  14271. if (rel + 1 < relend
  14272. && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
  14273. && rel[1].r_offset == rel->r_offset + 8)
  14274. {
  14275. if ((tls_mask & TLS_GD) == 0
  14276. && offset_in_range (input_section, rel->r_offset, 8))
  14277. {
  14278. rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
  14279. if ((tls_mask & TLS_GDIE) != 0)
  14280. r_type = R_PPC64_TPREL64;
  14281. else
  14282. {
  14283. bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
  14284. r_type = R_PPC64_NONE;
  14285. }
  14286. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14287. }
  14288. }
  14289. else
  14290. {
  14291. if ((tls_mask & TLS_LD) == 0
  14292. && offset_in_range (input_section, rel->r_offset, 8))
  14293. {
  14294. bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
  14295. r_type = R_PPC64_NONE;
  14296. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14297. }
  14298. }
  14299. break;
  14300. case R_PPC64_TPREL64:
  14301. if ((tls_mask & TLS_TPREL) == 0)
  14302. {
  14303. r_type = R_PPC64_NONE;
  14304. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14305. }
  14306. break;
  14307. case R_PPC64_ENTRY:
  14308. relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
  14309. if (!bfd_link_pic (info)
  14310. && !info->traditional_format
  14311. && relocation + 0x80008000 <= 0xffffffff
  14312. && offset_in_range (input_section, rel->r_offset, 8))
  14313. {
  14314. unsigned int insn1, insn2;
  14315. insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
  14316. insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
  14317. if ((insn1 & ~0xfffc) == LD_R2_0R12
  14318. && insn2 == ADD_R2_R2_R12)
  14319. {
  14320. bfd_put_32 (input_bfd,
  14321. LIS_R2 + PPC_HA (relocation),
  14322. contents + rel->r_offset);
  14323. bfd_put_32 (input_bfd,
  14324. ADDI_R2_R2 + PPC_LO (relocation),
  14325. contents + rel->r_offset + 4);
  14326. }
  14327. }
  14328. else
  14329. {
  14330. relocation -= (rel->r_offset
  14331. + input_section->output_offset
  14332. + input_section->output_section->vma);
  14333. if (relocation + 0x80008000 <= 0xffffffff
  14334. && offset_in_range (input_section, rel->r_offset, 8))
  14335. {
  14336. unsigned int insn1, insn2;
  14337. insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
  14338. insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
  14339. if ((insn1 & ~0xfffc) == LD_R2_0R12
  14340. && insn2 == ADD_R2_R2_R12)
  14341. {
  14342. bfd_put_32 (input_bfd,
  14343. ADDIS_R2_R12 + PPC_HA (relocation),
  14344. contents + rel->r_offset);
  14345. bfd_put_32 (input_bfd,
  14346. ADDI_R2_R2 + PPC_LO (relocation),
  14347. contents + rel->r_offset + 4);
  14348. }
  14349. }
  14350. }
  14351. break;
  14352. case R_PPC64_REL16_HA:
  14353. /* If we are generating a non-PIC executable, edit
  14354. . 0: addis 2,12,.TOC.-0b@ha
  14355. . addi 2,2,.TOC.-0b@l
  14356. used by ELFv2 global entry points to set up r2, to
  14357. . lis 2,.TOC.@ha
  14358. . addi 2,2,.TOC.@l
  14359. if .TOC. is in range. */
  14360. if (!bfd_link_pic (info)
  14361. && !info->traditional_format
  14362. && !htab->opd_abi
  14363. && rel->r_addend == d_offset
  14364. && h != NULL && &h->elf == htab->elf.hgot
  14365. && rel + 1 < relend
  14366. && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
  14367. && rel[1].r_offset == rel->r_offset + 4
  14368. && rel[1].r_addend == rel->r_addend + 4
  14369. && relocation + 0x80008000 <= 0xffffffff
  14370. && offset_in_range (input_section, rel->r_offset - d_offset, 8))
  14371. {
  14372. unsigned int insn1, insn2;
  14373. offset = rel->r_offset - d_offset;
  14374. insn1 = bfd_get_32 (input_bfd, contents + offset);
  14375. insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
  14376. if ((insn1 & 0xffff0000) == ADDIS_R2_R12
  14377. && (insn2 & 0xffff0000) == ADDI_R2_R2)
  14378. {
  14379. r_type = R_PPC64_ADDR16_HA;
  14380. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14381. rel->r_addend -= d_offset;
  14382. rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
  14383. rel[1].r_addend -= d_offset + 4;
  14384. bfd_put_32 (input_bfd, LIS_R2, contents + offset);
  14385. }
  14386. }
  14387. break;
  14388. }
  14389. /* Handle other relocations that tweak non-addend part of insn. */
  14390. insn = 0;
  14391. max_br_offset = 1 << 25;
  14392. addend = rel->r_addend;
  14393. reloc_dest = DEST_NORMAL;
  14394. switch (r_type)
  14395. {
  14396. default:
  14397. break;
  14398. case R_PPC64_TOCSAVE:
  14399. if (relocation + addend == (rel->r_offset
  14400. + input_section->output_offset
  14401. + input_section->output_section->vma)
  14402. && tocsave_find (htab, NO_INSERT,
  14403. &local_syms, rel, input_bfd)
  14404. && offset_in_range (input_section, rel->r_offset, 4))
  14405. {
  14406. insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  14407. if (insn == NOP
  14408. || insn == CROR_151515 || insn == CROR_313131)
  14409. bfd_put_32 (input_bfd,
  14410. STD_R2_0R1 + STK_TOC (htab),
  14411. contents + rel->r_offset);
  14412. }
  14413. break;
  14414. /* Branch taken prediction relocations. */
  14415. case R_PPC64_ADDR14_BRTAKEN:
  14416. case R_PPC64_REL14_BRTAKEN:
  14417. insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
  14418. /* Fall through. */
  14419. /* Branch not taken prediction relocations. */
  14420. case R_PPC64_ADDR14_BRNTAKEN:
  14421. case R_PPC64_REL14_BRNTAKEN:
  14422. if (!offset_in_range (input_section, rel->r_offset, 4))
  14423. break;
  14424. insn |= bfd_get_32 (input_bfd,
  14425. contents + rel->r_offset) & ~(0x01 << 21);
  14426. /* Fall through. */
  14427. case R_PPC64_REL14:
  14428. max_br_offset = 1 << 15;
  14429. /* Fall through. */
  14430. case R_PPC64_REL24:
  14431. case R_PPC64_REL24_NOTOC:
  14432. case R_PPC64_REL24_P9NOTOC:
  14433. case R_PPC64_PLTCALL:
  14434. case R_PPC64_PLTCALL_NOTOC:
  14435. /* Calls to functions with a different TOC, such as calls to
  14436. shared objects, need to alter the TOC pointer. This is
  14437. done using a linkage stub. A REL24 branching to these
  14438. linkage stubs needs to be followed by a nop, as the nop
  14439. will be replaced with an instruction to restore the TOC
  14440. base pointer. */
  14441. fdh = h;
  14442. if (h != NULL
  14443. && h->oh != NULL
  14444. && h->oh->is_func_descriptor)
  14445. fdh = ppc_follow_link (h->oh);
  14446. stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
  14447. htab);
  14448. if ((r_type == R_PPC64_PLTCALL
  14449. || r_type == R_PPC64_PLTCALL_NOTOC)
  14450. && stub_entry != NULL
  14451. && stub_entry->type.main == ppc_stub_plt_call)
  14452. stub_entry = NULL;
  14453. if (stub_entry != NULL
  14454. && (stub_entry->type.main == ppc_stub_plt_call
  14455. || stub_entry->type.r2save))
  14456. {
  14457. bool can_plt_call = false;
  14458. if (r_type == R_PPC64_REL24_NOTOC
  14459. || r_type == R_PPC64_REL24_P9NOTOC)
  14460. {
  14461. /* NOTOC calls don't need to restore r2. */
  14462. can_plt_call = true;
  14463. }
  14464. else if (stub_entry->type.main == ppc_stub_plt_call
  14465. && !htab->opd_abi
  14466. && htab->params->plt_localentry0 != 0
  14467. && h != NULL
  14468. && is_elfv2_localentry0 (&h->elf))
  14469. {
  14470. /* The function doesn't use or change r2. */
  14471. can_plt_call = true;
  14472. }
  14473. /* All of these stubs may modify r2, so there must be a
  14474. branch and link followed by a nop. The nop is
  14475. replaced by an insn to restore r2. */
  14476. else if (offset_in_range (input_section, rel->r_offset, 8))
  14477. {
  14478. unsigned long br;
  14479. br = bfd_get_32 (input_bfd,
  14480. contents + rel->r_offset);
  14481. if ((br & 1) != 0)
  14482. {
  14483. unsigned long nop;
  14484. nop = bfd_get_32 (input_bfd,
  14485. contents + rel->r_offset + 4);
  14486. if (nop == LD_R2_0R1 + STK_TOC (htab))
  14487. can_plt_call = true;
  14488. else if (nop == NOP
  14489. || nop == CROR_151515
  14490. || nop == CROR_313131)
  14491. {
  14492. if (h != NULL
  14493. && is_tls_get_addr (&h->elf, htab)
  14494. && htab->params->tls_get_addr_opt)
  14495. {
  14496. /* Special stub used, leave nop alone. */
  14497. }
  14498. else
  14499. bfd_put_32 (input_bfd,
  14500. LD_R2_0R1 + STK_TOC (htab),
  14501. contents + rel->r_offset + 4);
  14502. can_plt_call = true;
  14503. }
  14504. }
  14505. }
  14506. if (!can_plt_call && h != NULL)
  14507. {
  14508. const char *name = h->elf.root.root.string;
  14509. if (*name == '.')
  14510. ++name;
  14511. if (startswith (name, "__libc_start_main")
  14512. && (name[17] == 0 || name[17] == '@'))
  14513. {
  14514. /* Allow crt1 branch to go via a toc adjusting
  14515. stub. Other calls that never return could do
  14516. the same, if we could detect such. */
  14517. can_plt_call = true;
  14518. }
  14519. }
  14520. if (!can_plt_call)
  14521. {
  14522. /* g++ as of 20130507 emits self-calls without a
  14523. following nop. This is arguably wrong since we
  14524. have conflicting information. On the one hand a
  14525. global symbol and on the other a local call
  14526. sequence, but don't error for this special case.
  14527. It isn't possible to cheaply verify we have
  14528. exactly such a call. Allow all calls to the same
  14529. section. */
  14530. asection *code_sec = sec;
  14531. if (get_opd_info (sec) != NULL)
  14532. {
  14533. bfd_vma off = (relocation + addend
  14534. - sec->output_section->vma
  14535. - sec->output_offset);
  14536. opd_entry_value (sec, off, &code_sec, NULL, false);
  14537. }
  14538. if (code_sec == input_section)
  14539. can_plt_call = true;
  14540. }
  14541. if (!can_plt_call)
  14542. {
  14543. if (stub_entry->type.main == ppc_stub_plt_call)
  14544. info->callbacks->einfo
  14545. /* xgettext:c-format */
  14546. (_("%H: call to `%pT' lacks nop, can't restore toc; "
  14547. "(plt call stub)\n"),
  14548. input_bfd, input_section, rel->r_offset, sym_name);
  14549. else
  14550. info->callbacks->einfo
  14551. /* xgettext:c-format */
  14552. (_("%H: call to `%pT' lacks nop, can't restore toc; "
  14553. "(toc save/adjust stub)\n"),
  14554. input_bfd, input_section, rel->r_offset, sym_name);
  14555. bfd_set_error (bfd_error_bad_value);
  14556. ret = false;
  14557. }
  14558. if (can_plt_call
  14559. && stub_entry->type.main == ppc_stub_plt_call)
  14560. unresolved_reloc = false;
  14561. }
  14562. if ((stub_entry == NULL
  14563. || stub_entry->type.main == ppc_stub_long_branch
  14564. || stub_entry->type.main == ppc_stub_plt_branch)
  14565. && get_opd_info (sec) != NULL)
  14566. {
  14567. /* The branch destination is the value of the opd entry. */
  14568. bfd_vma off = (relocation + addend
  14569. - sec->output_section->vma
  14570. - sec->output_offset);
  14571. bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
  14572. if (dest != (bfd_vma) -1)
  14573. {
  14574. relocation = dest;
  14575. addend = 0;
  14576. reloc_dest = DEST_OPD;
  14577. }
  14578. }
  14579. /* If the branch is out of reach we ought to have a long
  14580. branch stub. */
  14581. from = (rel->r_offset
  14582. + input_section->output_offset
  14583. + input_section->output_section->vma);
  14584. relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
  14585. ? fdh->elf.other
  14586. : sym->st_other);
  14587. if (stub_entry != NULL
  14588. && (stub_entry->type.main == ppc_stub_long_branch
  14589. || stub_entry->type.main == ppc_stub_plt_branch))
  14590. {
  14591. if (stub_entry->type.sub == ppc_stub_toc
  14592. && !stub_entry->type.r2save
  14593. && (r_type == R_PPC64_ADDR14_BRTAKEN
  14594. || r_type == R_PPC64_ADDR14_BRNTAKEN
  14595. || (relocation + addend - from + max_br_offset
  14596. < 2 * max_br_offset)))
  14597. /* Don't use the stub if this branch is in range. */
  14598. stub_entry = NULL;
  14599. if (stub_entry != NULL
  14600. && stub_entry->type.sub >= ppc_stub_notoc
  14601. && ((r_type != R_PPC64_REL24_NOTOC
  14602. && r_type != R_PPC64_REL24_P9NOTOC)
  14603. || ((fdh ? fdh->elf.other : sym->st_other)
  14604. & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
  14605. && (relocation + addend - from + max_br_offset
  14606. < 2 * max_br_offset))
  14607. stub_entry = NULL;
  14608. if (stub_entry != NULL
  14609. && stub_entry->type.r2save
  14610. && (r_type == R_PPC64_REL24_NOTOC
  14611. || r_type == R_PPC64_REL24_P9NOTOC)
  14612. && (relocation + addend - from + max_br_offset
  14613. < 2 * max_br_offset))
  14614. stub_entry = NULL;
  14615. }
  14616. if (stub_entry != NULL)
  14617. {
  14618. /* Munge up the value and addend so that we call the stub
  14619. rather than the procedure directly. */
  14620. asection *stub_sec = stub_entry->group->stub_sec;
  14621. if (stub_entry->type.main == ppc_stub_save_res)
  14622. relocation += (stub_sec->output_offset
  14623. + stub_sec->output_section->vma
  14624. + stub_sec->size - htab->sfpr->size
  14625. - htab->sfpr->output_offset
  14626. - htab->sfpr->output_section->vma);
  14627. else
  14628. relocation = (stub_entry->stub_offset
  14629. + stub_sec->output_offset
  14630. + stub_sec->output_section->vma);
  14631. addend = 0;
  14632. reloc_dest = DEST_STUB;
  14633. if (((stub_entry->type.r2save
  14634. && (r_type == R_PPC64_REL24_NOTOC
  14635. || r_type == R_PPC64_REL24_P9NOTOC))
  14636. || ((stub_entry->type.main == ppc_stub_plt_call
  14637. && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
  14638. && rel + 1 < relend
  14639. && rel[1].r_offset == rel->r_offset + 4
  14640. && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
  14641. && !(stub_entry->type.main == ppc_stub_plt_call
  14642. && htab->params->tls_get_addr_opt
  14643. && h != NULL
  14644. && is_tls_get_addr (&h->elf, htab)))
  14645. {
  14646. /* Skip over the r2 store at the start of the stub. */
  14647. relocation += 4;
  14648. }
  14649. if ((r_type == R_PPC64_REL24_NOTOC
  14650. || r_type == R_PPC64_REL24_P9NOTOC)
  14651. && stub_entry->type.main == ppc_stub_plt_call
  14652. && stub_entry->type.sub >= ppc_stub_notoc)
  14653. htab->notoc_plt = 1;
  14654. }
  14655. if (insn != 0)
  14656. {
  14657. if (is_isa_v2)
  14658. {
  14659. /* Set 'a' bit. This is 0b00010 in BO field for branch
  14660. on CR(BI) insns (BO == 001at or 011at), and 0b01000
  14661. for branch on CTR insns (BO == 1a00t or 1a01t). */
  14662. if ((insn & (0x14 << 21)) == (0x04 << 21))
  14663. insn |= 0x02 << 21;
  14664. else if ((insn & (0x14 << 21)) == (0x10 << 21))
  14665. insn |= 0x08 << 21;
  14666. else
  14667. break;
  14668. }
  14669. else
  14670. {
  14671. /* Invert 'y' bit if not the default. */
  14672. if ((bfd_signed_vma) (relocation + addend - from) < 0)
  14673. insn ^= 0x01 << 21;
  14674. }
  14675. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  14676. }
  14677. /* NOP out calls to undefined weak functions.
  14678. We can thus call a weak function without first
  14679. checking whether the function is defined. */
  14680. else if (h != NULL
  14681. && h->elf.root.type == bfd_link_hash_undefweak
  14682. && h->elf.dynindx == -1
  14683. && (r_type == R_PPC64_REL24
  14684. || r_type == R_PPC64_REL24_NOTOC
  14685. || r_type == R_PPC64_REL24_P9NOTOC)
  14686. && relocation == 0
  14687. && addend == 0
  14688. && offset_in_range (input_section, rel->r_offset, 4))
  14689. {
  14690. bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
  14691. goto copy_reloc;
  14692. }
  14693. break;
  14694. case R_PPC64_GOT16_DS:
  14695. if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
  14696. || (bfd_link_pic (info)
  14697. && sec == bfd_abs_section_ptr)
  14698. || !htab->do_toc_opt)
  14699. break;
  14700. from = TOCstart + htab->sec_info[input_section->id].toc_off;
  14701. if (relocation + addend - from + 0x8000 < 0x10000
  14702. && sec != NULL
  14703. && sec->output_section != NULL
  14704. && !discarded_section (sec)
  14705. && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
  14706. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  14707. {
  14708. insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
  14709. if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
  14710. {
  14711. insn += (14u << 26) - (58u << 26);
  14712. bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
  14713. r_type = R_PPC64_TOC16;
  14714. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14715. }
  14716. }
  14717. break;
  14718. case R_PPC64_GOT16_LO_DS:
  14719. case R_PPC64_GOT16_HA:
  14720. if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
  14721. || (bfd_link_pic (info)
  14722. && sec == bfd_abs_section_ptr)
  14723. || !htab->do_toc_opt)
  14724. break;
  14725. from = TOCstart + htab->sec_info[input_section->id].toc_off;
  14726. if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
  14727. && sec != NULL
  14728. && sec->output_section != NULL
  14729. && !discarded_section (sec)
  14730. && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
  14731. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  14732. {
  14733. insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
  14734. if (r_type == R_PPC64_GOT16_LO_DS
  14735. && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
  14736. {
  14737. insn += (14u << 26) - (58u << 26);
  14738. bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
  14739. r_type = R_PPC64_TOC16_LO;
  14740. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14741. }
  14742. else if (r_type == R_PPC64_GOT16_HA
  14743. && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
  14744. {
  14745. r_type = R_PPC64_TOC16_HA;
  14746. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14747. }
  14748. }
  14749. break;
  14750. case R_PPC64_GOT_PCREL34:
  14751. if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
  14752. || (bfd_link_pic (info)
  14753. && sec == bfd_abs_section_ptr)
  14754. || !htab->do_toc_opt)
  14755. break;
  14756. from = (rel->r_offset
  14757. + input_section->output_section->vma
  14758. + input_section->output_offset);
  14759. if (!(relocation - from + (1ULL << 33) < 1ULL << 34
  14760. && sec != NULL
  14761. && sec->output_section != NULL
  14762. && !discarded_section (sec)
  14763. && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
  14764. && offset_in_range (input_section, rel->r_offset, 8)))
  14765. break;
  14766. offset = rel->r_offset;
  14767. pinsn = bfd_get_32 (input_bfd, contents + offset);
  14768. pinsn <<= 32;
  14769. pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
  14770. if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
  14771. != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
  14772. break;
  14773. /* Replace with paddi. */
  14774. pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
  14775. r_type = R_PPC64_PCREL34;
  14776. rel->r_info = ELF64_R_INFO (r_symndx, r_type);
  14777. bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
  14778. bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
  14779. /* Fall through. */
  14780. case R_PPC64_PCREL34:
  14781. if (!htab->params->no_pcrel_opt
  14782. && rel + 1 < relend
  14783. && rel[1].r_offset == rel->r_offset
  14784. && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
  14785. && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
  14786. && offset_in_range (input_section, rel->r_offset, 8))
  14787. {
  14788. offset = rel->r_offset;
  14789. pinsn = bfd_get_32 (input_bfd, contents + offset);
  14790. pinsn <<= 32;
  14791. pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
  14792. if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
  14793. == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
  14794. | (14ULL << 26) /* paddi */))
  14795. {
  14796. bfd_vma off2 = rel[1].r_addend;
  14797. if (off2 == 0)
  14798. /* zero means next insn. */
  14799. off2 = 8;
  14800. off2 += offset;
  14801. if (offset_in_range (input_section, off2, 4))
  14802. {
  14803. uint64_t pinsn2;
  14804. bfd_signed_vma addend_off;
  14805. pinsn2 = bfd_get_32 (input_bfd, contents + off2);
  14806. pinsn2 <<= 32;
  14807. if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
  14808. {
  14809. if (!offset_in_range (input_section, off2, 8))
  14810. break;
  14811. pinsn2 |= bfd_get_32 (input_bfd,
  14812. contents + off2 + 4);
  14813. }
  14814. if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
  14815. {
  14816. addend += addend_off;
  14817. rel->r_addend = addend;
  14818. bfd_put_32 (input_bfd, pinsn >> 32,
  14819. contents + offset);
  14820. bfd_put_32 (input_bfd, pinsn,
  14821. contents + offset + 4);
  14822. bfd_put_32 (input_bfd, pinsn2 >> 32,
  14823. contents + off2);
  14824. if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
  14825. bfd_put_32 (input_bfd, pinsn2,
  14826. contents + off2 + 4);
  14827. }
  14828. }
  14829. }
  14830. }
  14831. break;
  14832. }
  14833. tls_type = 0;
  14834. save_unresolved_reloc = unresolved_reloc;
  14835. switch (r_type)
  14836. {
  14837. default:
  14838. /* xgettext:c-format */
  14839. _bfd_error_handler (_("%pB: %s unsupported"),
  14840. input_bfd, ppc64_elf_howto_table[r_type]->name);
  14841. bfd_set_error (bfd_error_bad_value);
  14842. ret = false;
  14843. goto copy_reloc;
  14844. case R_PPC64_NONE:
  14845. case R_PPC64_TLS:
  14846. case R_PPC64_TLSGD:
  14847. case R_PPC64_TLSLD:
  14848. case R_PPC64_TOCSAVE:
  14849. case R_PPC64_GNU_VTINHERIT:
  14850. case R_PPC64_GNU_VTENTRY:
  14851. case R_PPC64_ENTRY:
  14852. case R_PPC64_PCREL_OPT:
  14853. goto copy_reloc;
  14854. /* GOT16 relocations. Like an ADDR16 using the symbol's
  14855. address in the GOT as relocation value instead of the
  14856. symbol's value itself. Also, create a GOT entry for the
  14857. symbol and put the symbol value there. */
  14858. case R_PPC64_GOT_TLSGD16:
  14859. case R_PPC64_GOT_TLSGD16_LO:
  14860. case R_PPC64_GOT_TLSGD16_HI:
  14861. case R_PPC64_GOT_TLSGD16_HA:
  14862. case R_PPC64_GOT_TLSGD_PCREL34:
  14863. tls_type = TLS_TLS | TLS_GD;
  14864. goto dogot;
  14865. case R_PPC64_GOT_TLSLD16:
  14866. case R_PPC64_GOT_TLSLD16_LO:
  14867. case R_PPC64_GOT_TLSLD16_HI:
  14868. case R_PPC64_GOT_TLSLD16_HA:
  14869. case R_PPC64_GOT_TLSLD_PCREL34:
  14870. tls_type = TLS_TLS | TLS_LD;
  14871. goto dogot;
  14872. case R_PPC64_GOT_TPREL16_DS:
  14873. case R_PPC64_GOT_TPREL16_LO_DS:
  14874. case R_PPC64_GOT_TPREL16_HI:
  14875. case R_PPC64_GOT_TPREL16_HA:
  14876. case R_PPC64_GOT_TPREL_PCREL34:
  14877. tls_type = TLS_TLS | TLS_TPREL;
  14878. goto dogot;
  14879. case R_PPC64_GOT_DTPREL16_DS:
  14880. case R_PPC64_GOT_DTPREL16_LO_DS:
  14881. case R_PPC64_GOT_DTPREL16_HI:
  14882. case R_PPC64_GOT_DTPREL16_HA:
  14883. case R_PPC64_GOT_DTPREL_PCREL34:
  14884. tls_type = TLS_TLS | TLS_DTPREL;
  14885. goto dogot;
  14886. case R_PPC64_GOT16:
  14887. case R_PPC64_GOT16_LO:
  14888. case R_PPC64_GOT16_HI:
  14889. case R_PPC64_GOT16_HA:
  14890. case R_PPC64_GOT16_DS:
  14891. case R_PPC64_GOT16_LO_DS:
  14892. case R_PPC64_GOT_PCREL34:
  14893. dogot:
  14894. {
  14895. /* Relocation is to the entry for this symbol in the global
  14896. offset table. */
  14897. asection *got;
  14898. bfd_vma *offp;
  14899. bfd_vma off;
  14900. unsigned long indx = 0;
  14901. struct got_entry *ent;
  14902. if (tls_type == (TLS_TLS | TLS_LD)
  14903. && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
  14904. ent = ppc64_tlsld_got (input_bfd);
  14905. else
  14906. {
  14907. if (h != NULL)
  14908. {
  14909. if (!htab->elf.dynamic_sections_created
  14910. || h->elf.dynindx == -1
  14911. || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
  14912. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
  14913. /* This is actually a static link, or it is a
  14914. -Bsymbolic link and the symbol is defined
  14915. locally, or the symbol was forced to be local
  14916. because of a version file. */
  14917. ;
  14918. else
  14919. {
  14920. indx = h->elf.dynindx;
  14921. unresolved_reloc = false;
  14922. }
  14923. ent = h->elf.got.glist;
  14924. }
  14925. else
  14926. {
  14927. if (local_got_ents == NULL)
  14928. abort ();
  14929. ent = local_got_ents[r_symndx];
  14930. }
  14931. for (; ent != NULL; ent = ent->next)
  14932. if (ent->addend == orig_rel.r_addend
  14933. && ent->owner == input_bfd
  14934. && ent->tls_type == tls_type)
  14935. break;
  14936. }
  14937. if (ent == NULL)
  14938. abort ();
  14939. if (ent->is_indirect)
  14940. ent = ent->got.ent;
  14941. offp = &ent->got.offset;
  14942. got = ppc64_elf_tdata (ent->owner)->got;
  14943. if (got == NULL)
  14944. abort ();
  14945. /* The offset must always be a multiple of 8. We use the
  14946. least significant bit to record whether we have already
  14947. processed this entry. */
  14948. off = *offp;
  14949. if ((off & 1) != 0)
  14950. off &= ~1;
  14951. else
  14952. {
  14953. /* Generate relocs for the dynamic linker, except in
  14954. the case of TLSLD where we'll use one entry per
  14955. module. */
  14956. asection *relgot;
  14957. bool ifunc;
  14958. *offp = off | 1;
  14959. relgot = NULL;
  14960. ifunc = (h != NULL
  14961. ? h->elf.type == STT_GNU_IFUNC
  14962. : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
  14963. if (ifunc)
  14964. {
  14965. relgot = htab->elf.irelplt;
  14966. if (indx == 0 || is_static_defined (&h->elf))
  14967. htab->elf.ifunc_resolvers = true;
  14968. }
  14969. else if (indx != 0
  14970. || (bfd_link_pic (info)
  14971. && (h == NULL
  14972. || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
  14973. && !(tls_type != 0
  14974. && bfd_link_executable (info)
  14975. && (h == NULL
  14976. || SYMBOL_REFERENCES_LOCAL (info,
  14977. &h->elf)))
  14978. && (h != NULL
  14979. ? !bfd_is_abs_symbol (&h->elf.root)
  14980. : sym->st_shndx != SHN_ABS)))
  14981. relgot = ppc64_elf_tdata (ent->owner)->relgot;
  14982. if (relgot != NULL)
  14983. {
  14984. outrel.r_offset = (got->output_section->vma
  14985. + got->output_offset
  14986. + off);
  14987. outrel.r_addend = orig_rel.r_addend;
  14988. if (tls_type & (TLS_LD | TLS_GD))
  14989. {
  14990. outrel.r_addend = 0;
  14991. outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
  14992. if (tls_type == (TLS_TLS | TLS_GD))
  14993. {
  14994. loc = relgot->contents;
  14995. loc += (relgot->reloc_count++
  14996. * sizeof (Elf64_External_Rela));
  14997. bfd_elf64_swap_reloca_out (output_bfd,
  14998. &outrel, loc);
  14999. outrel.r_offset += 8;
  15000. outrel.r_addend = orig_rel.r_addend;
  15001. outrel.r_info
  15002. = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
  15003. }
  15004. }
  15005. else if (tls_type == (TLS_TLS | TLS_DTPREL))
  15006. outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
  15007. else if (tls_type == (TLS_TLS | TLS_TPREL))
  15008. outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
  15009. else if (indx != 0)
  15010. outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
  15011. else
  15012. {
  15013. if (ifunc)
  15014. outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
  15015. else
  15016. outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
  15017. /* Write the .got section contents for the sake
  15018. of prelink. */
  15019. loc = got->contents + off;
  15020. bfd_put_64 (output_bfd, outrel.r_addend + relocation,
  15021. loc);
  15022. }
  15023. if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
  15024. {
  15025. outrel.r_addend += relocation;
  15026. if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
  15027. {
  15028. if (htab->elf.tls_sec == NULL)
  15029. outrel.r_addend = 0;
  15030. else
  15031. outrel.r_addend -= htab->elf.tls_sec->vma;
  15032. }
  15033. }
  15034. if (!(info->enable_dt_relr
  15035. && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
  15036. {
  15037. loc = relgot->contents;
  15038. loc += (relgot->reloc_count++
  15039. * sizeof (Elf64_External_Rela));
  15040. bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
  15041. }
  15042. }
  15043. /* Init the .got section contents here if we're not
  15044. emitting a reloc. */
  15045. else
  15046. {
  15047. relocation += orig_rel.r_addend;
  15048. if (tls_type != 0)
  15049. {
  15050. if (htab->elf.tls_sec == NULL)
  15051. relocation = 0;
  15052. else
  15053. {
  15054. if (tls_type & TLS_LD)
  15055. relocation = 0;
  15056. else
  15057. relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
  15058. if (tls_type & TLS_TPREL)
  15059. relocation += DTP_OFFSET - TP_OFFSET;
  15060. }
  15061. if (tls_type & (TLS_GD | TLS_LD))
  15062. {
  15063. bfd_put_64 (output_bfd, relocation,
  15064. got->contents + off + 8);
  15065. relocation = 1;
  15066. }
  15067. }
  15068. bfd_put_64 (output_bfd, relocation,
  15069. got->contents + off);
  15070. }
  15071. }
  15072. if (off >= (bfd_vma) -2)
  15073. abort ();
  15074. relocation = got->output_section->vma + got->output_offset + off;
  15075. addend = 0;
  15076. if (!(r_type == R_PPC64_GOT_PCREL34
  15077. || r_type == R_PPC64_GOT_TLSGD_PCREL34
  15078. || r_type == R_PPC64_GOT_TLSLD_PCREL34
  15079. || r_type == R_PPC64_GOT_TPREL_PCREL34
  15080. || r_type == R_PPC64_GOT_DTPREL_PCREL34))
  15081. addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
  15082. }
  15083. break;
  15084. case R_PPC64_PLT16_HA:
  15085. case R_PPC64_PLT16_HI:
  15086. case R_PPC64_PLT16_LO:
  15087. case R_PPC64_PLT16_LO_DS:
  15088. case R_PPC64_PLT_PCREL34:
  15089. case R_PPC64_PLT_PCREL34_NOTOC:
  15090. case R_PPC64_PLT32:
  15091. case R_PPC64_PLT64:
  15092. case R_PPC64_PLTSEQ:
  15093. case R_PPC64_PLTSEQ_NOTOC:
  15094. case R_PPC64_PLTCALL:
  15095. case R_PPC64_PLTCALL_NOTOC:
  15096. /* Relocation is to the entry for this symbol in the
  15097. procedure linkage table. */
  15098. unresolved_reloc = true;
  15099. {
  15100. struct plt_entry **plt_list = NULL;
  15101. if (h != NULL)
  15102. plt_list = &h->elf.plt.plist;
  15103. else if (local_got_ents != NULL)
  15104. {
  15105. struct plt_entry **local_plt = (struct plt_entry **)
  15106. (local_got_ents + symtab_hdr->sh_info);
  15107. plt_list = local_plt + r_symndx;
  15108. }
  15109. if (plt_list)
  15110. {
  15111. struct plt_entry *ent;
  15112. for (ent = *plt_list; ent != NULL; ent = ent->next)
  15113. if (ent->plt.offset != (bfd_vma) -1
  15114. && ent->addend == orig_rel.r_addend)
  15115. {
  15116. asection *plt;
  15117. bfd_vma got;
  15118. plt = htab->elf.splt;
  15119. if (use_local_plt (info, elf_hash_entry (h)))
  15120. {
  15121. if (h != NULL
  15122. ? h->elf.type == STT_GNU_IFUNC
  15123. : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  15124. plt = htab->elf.iplt;
  15125. else
  15126. plt = htab->pltlocal;
  15127. }
  15128. relocation = (plt->output_section->vma
  15129. + plt->output_offset
  15130. + ent->plt.offset);
  15131. if (r_type == R_PPC64_PLT16_HA
  15132. || r_type == R_PPC64_PLT16_HI
  15133. || r_type == R_PPC64_PLT16_LO
  15134. || r_type == R_PPC64_PLT16_LO_DS)
  15135. {
  15136. got = (elf_gp (output_bfd)
  15137. + htab->sec_info[input_section->id].toc_off);
  15138. relocation -= got;
  15139. }
  15140. addend = 0;
  15141. unresolved_reloc = false;
  15142. break;
  15143. }
  15144. }
  15145. }
  15146. break;
  15147. case R_PPC64_TOC:
  15148. /* Relocation value is TOC base. */
  15149. relocation = TOCstart;
  15150. if (r_symndx == STN_UNDEF)
  15151. relocation += htab->sec_info[input_section->id].toc_off;
  15152. else if (unresolved_reloc)
  15153. ;
  15154. else if (sec != NULL && sec->id < htab->sec_info_arr_size)
  15155. relocation += htab->sec_info[sec->id].toc_off;
  15156. else
  15157. unresolved_reloc = true;
  15158. if (unresolved_reloc
  15159. || (!is_opd
  15160. && h != NULL
  15161. && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
  15162. info->callbacks->einfo
  15163. /* xgettext:c-format */
  15164. (_("%H: %s against %pT is not supported\n"),
  15165. input_bfd, input_section, rel->r_offset,
  15166. ppc64_elf_howto_table[r_type]->name, sym_name);
  15167. goto dodyn;
  15168. /* TOC16 relocs. We want the offset relative to the TOC base,
  15169. which is the address of the start of the TOC plus 0x8000.
  15170. The TOC consists of sections .got, .toc, .tocbss, and .plt,
  15171. in this order. */
  15172. case R_PPC64_TOC16:
  15173. case R_PPC64_TOC16_LO:
  15174. case R_PPC64_TOC16_HI:
  15175. case R_PPC64_TOC16_DS:
  15176. case R_PPC64_TOC16_LO_DS:
  15177. case R_PPC64_TOC16_HA:
  15178. addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
  15179. if (h != NULL)
  15180. goto dodyn;
  15181. break;
  15182. /* Relocate against the beginning of the section. */
  15183. case R_PPC64_SECTOFF:
  15184. case R_PPC64_SECTOFF_LO:
  15185. case R_PPC64_SECTOFF_HI:
  15186. case R_PPC64_SECTOFF_DS:
  15187. case R_PPC64_SECTOFF_LO_DS:
  15188. case R_PPC64_SECTOFF_HA:
  15189. if (sec != NULL)
  15190. addend -= sec->output_section->vma;
  15191. break;
  15192. case R_PPC64_REL16:
  15193. case R_PPC64_REL16_LO:
  15194. case R_PPC64_REL16_HI:
  15195. case R_PPC64_REL16_HA:
  15196. case R_PPC64_REL16_HIGH:
  15197. case R_PPC64_REL16_HIGHA:
  15198. case R_PPC64_REL16_HIGHER:
  15199. case R_PPC64_REL16_HIGHERA:
  15200. case R_PPC64_REL16_HIGHEST:
  15201. case R_PPC64_REL16_HIGHESTA:
  15202. case R_PPC64_REL16_HIGHER34:
  15203. case R_PPC64_REL16_HIGHERA34:
  15204. case R_PPC64_REL16_HIGHEST34:
  15205. case R_PPC64_REL16_HIGHESTA34:
  15206. case R_PPC64_REL16DX_HA:
  15207. case R_PPC64_REL14:
  15208. case R_PPC64_REL14_BRNTAKEN:
  15209. case R_PPC64_REL14_BRTAKEN:
  15210. case R_PPC64_REL24:
  15211. case R_PPC64_REL24_NOTOC:
  15212. case R_PPC64_REL24_P9NOTOC:
  15213. case R_PPC64_PCREL34:
  15214. case R_PPC64_PCREL28:
  15215. break;
  15216. case R_PPC64_TPREL16:
  15217. case R_PPC64_TPREL16_LO:
  15218. case R_PPC64_TPREL16_HI:
  15219. case R_PPC64_TPREL16_HA:
  15220. case R_PPC64_TPREL16_DS:
  15221. case R_PPC64_TPREL16_LO_DS:
  15222. case R_PPC64_TPREL16_HIGH:
  15223. case R_PPC64_TPREL16_HIGHA:
  15224. case R_PPC64_TPREL16_HIGHER:
  15225. case R_PPC64_TPREL16_HIGHERA:
  15226. case R_PPC64_TPREL16_HIGHEST:
  15227. case R_PPC64_TPREL16_HIGHESTA:
  15228. if (h != NULL
  15229. && h->elf.root.type == bfd_link_hash_undefweak
  15230. && h->elf.dynindx == -1
  15231. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  15232. {
  15233. /* Make this relocation against an undefined weak symbol
  15234. resolve to zero. This is really just a tweak, since
  15235. code using weak externs ought to check that they are
  15236. defined before using them. */
  15237. bfd_byte *p = contents + rel->r_offset - d_offset;
  15238. insn = bfd_get_32 (input_bfd, p);
  15239. insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
  15240. if (insn != 0)
  15241. bfd_put_32 (input_bfd, insn, p);
  15242. break;
  15243. }
  15244. /* Fall through. */
  15245. case R_PPC64_TPREL34:
  15246. if (htab->elf.tls_sec != NULL)
  15247. addend -= htab->elf.tls_sec->vma + TP_OFFSET;
  15248. /* The TPREL16 relocs shouldn't really be used in shared
  15249. libs or with non-local symbols as that will result in
  15250. DT_TEXTREL being set, but support them anyway. */
  15251. goto dodyn;
  15252. case R_PPC64_DTPREL16:
  15253. case R_PPC64_DTPREL16_LO:
  15254. case R_PPC64_DTPREL16_HI:
  15255. case R_PPC64_DTPREL16_HA:
  15256. case R_PPC64_DTPREL16_DS:
  15257. case R_PPC64_DTPREL16_LO_DS:
  15258. case R_PPC64_DTPREL16_HIGH:
  15259. case R_PPC64_DTPREL16_HIGHA:
  15260. case R_PPC64_DTPREL16_HIGHER:
  15261. case R_PPC64_DTPREL16_HIGHERA:
  15262. case R_PPC64_DTPREL16_HIGHEST:
  15263. case R_PPC64_DTPREL16_HIGHESTA:
  15264. case R_PPC64_DTPREL34:
  15265. if (htab->elf.tls_sec != NULL)
  15266. addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
  15267. break;
  15268. case R_PPC64_ADDR64_LOCAL:
  15269. addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
  15270. ? h->elf.other
  15271. : sym->st_other);
  15272. break;
  15273. case R_PPC64_DTPMOD64:
  15274. relocation = 1;
  15275. addend = 0;
  15276. goto dodyn;
  15277. case R_PPC64_TPREL64:
  15278. if (htab->elf.tls_sec != NULL)
  15279. addend -= htab->elf.tls_sec->vma + TP_OFFSET;
  15280. goto dodyn;
  15281. case R_PPC64_DTPREL64:
  15282. if (htab->elf.tls_sec != NULL)
  15283. addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
  15284. /* Fall through. */
  15285. /* Relocations that may need to be propagated if this is a
  15286. dynamic object. */
  15287. case R_PPC64_REL30:
  15288. case R_PPC64_REL32:
  15289. case R_PPC64_REL64:
  15290. case R_PPC64_ADDR14:
  15291. case R_PPC64_ADDR14_BRNTAKEN:
  15292. case R_PPC64_ADDR14_BRTAKEN:
  15293. case R_PPC64_ADDR16:
  15294. case R_PPC64_ADDR16_DS:
  15295. case R_PPC64_ADDR16_HA:
  15296. case R_PPC64_ADDR16_HI:
  15297. case R_PPC64_ADDR16_HIGH:
  15298. case R_PPC64_ADDR16_HIGHA:
  15299. case R_PPC64_ADDR16_HIGHER:
  15300. case R_PPC64_ADDR16_HIGHERA:
  15301. case R_PPC64_ADDR16_HIGHEST:
  15302. case R_PPC64_ADDR16_HIGHESTA:
  15303. case R_PPC64_ADDR16_LO:
  15304. case R_PPC64_ADDR16_LO_DS:
  15305. case R_PPC64_ADDR16_HIGHER34:
  15306. case R_PPC64_ADDR16_HIGHERA34:
  15307. case R_PPC64_ADDR16_HIGHEST34:
  15308. case R_PPC64_ADDR16_HIGHESTA34:
  15309. case R_PPC64_ADDR24:
  15310. case R_PPC64_ADDR32:
  15311. case R_PPC64_ADDR64:
  15312. case R_PPC64_UADDR16:
  15313. case R_PPC64_UADDR32:
  15314. case R_PPC64_UADDR64:
  15315. case R_PPC64_D34:
  15316. case R_PPC64_D34_LO:
  15317. case R_PPC64_D34_HI30:
  15318. case R_PPC64_D34_HA30:
  15319. case R_PPC64_D28:
  15320. dodyn:
  15321. if ((input_section->flags & SEC_ALLOC) == 0)
  15322. break;
  15323. if (NO_OPD_RELOCS && is_opd)
  15324. break;
  15325. if (bfd_link_pic (info)
  15326. ? ((h == NULL
  15327. || h->elf.dyn_relocs != NULL)
  15328. && ((h != NULL && pc_dynrelocs (h))
  15329. || must_be_dyn_reloc (info, r_type)))
  15330. : (h != NULL
  15331. ? h->elf.dyn_relocs != NULL
  15332. : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
  15333. {
  15334. bool skip, relocate;
  15335. asection *sreloc;
  15336. bfd_vma out_off;
  15337. long indx = 0;
  15338. /* When generating a dynamic object, these relocations
  15339. are copied into the output file to be resolved at run
  15340. time. */
  15341. skip = false;
  15342. relocate = false;
  15343. out_off = _bfd_elf_section_offset (output_bfd, info,
  15344. input_section, rel->r_offset);
  15345. if (out_off == (bfd_vma) -1)
  15346. skip = true;
  15347. else if (out_off == (bfd_vma) -2)
  15348. skip = true, relocate = true;
  15349. out_off += (input_section->output_section->vma
  15350. + input_section->output_offset);
  15351. outrel.r_offset = out_off;
  15352. outrel.r_addend = rel->r_addend;
  15353. /* Optimize unaligned reloc use. */
  15354. if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
  15355. || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
  15356. r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
  15357. else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
  15358. || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
  15359. r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
  15360. else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
  15361. || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
  15362. r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
  15363. if (skip)
  15364. memset (&outrel, 0, sizeof outrel);
  15365. else if (h != NULL
  15366. && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
  15367. && !is_opd
  15368. && r_type != R_PPC64_TOC)
  15369. {
  15370. indx = h->elf.dynindx;
  15371. BFD_ASSERT (indx != -1);
  15372. outrel.r_info = ELF64_R_INFO (indx, r_type);
  15373. }
  15374. else
  15375. {
  15376. /* This symbol is local, or marked to become local,
  15377. or this is an opd section reloc which must point
  15378. at a local function. */
  15379. outrel.r_addend += relocation;
  15380. if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
  15381. {
  15382. if (is_opd && h != NULL)
  15383. {
  15384. /* Lie about opd entries. This case occurs
  15385. when building shared libraries and we
  15386. reference a function in another shared
  15387. lib. The same thing happens for a weak
  15388. definition in an application that's
  15389. overridden by a strong definition in a
  15390. shared lib. (I believe this is a generic
  15391. bug in binutils handling of weak syms.)
  15392. In these cases we won't use the opd
  15393. entry in this lib. */
  15394. unresolved_reloc = false;
  15395. }
  15396. if (!is_opd
  15397. && r_type == R_PPC64_ADDR64
  15398. && (h != NULL
  15399. ? h->elf.type == STT_GNU_IFUNC
  15400. : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
  15401. outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
  15402. else
  15403. {
  15404. outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
  15405. /* We need to relocate .opd contents for ld.so.
  15406. Prelink also wants simple and consistent rules
  15407. for relocs. This make all RELATIVE relocs have
  15408. *r_offset equal to r_addend. */
  15409. relocate = true;
  15410. }
  15411. }
  15412. else
  15413. {
  15414. if (h != NULL
  15415. ? h->elf.type == STT_GNU_IFUNC
  15416. : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  15417. {
  15418. info->callbacks->einfo
  15419. /* xgettext:c-format */
  15420. (_("%H: %s for indirect "
  15421. "function `%pT' unsupported\n"),
  15422. input_bfd, input_section, rel->r_offset,
  15423. ppc64_elf_howto_table[r_type]->name,
  15424. sym_name);
  15425. ret = false;
  15426. }
  15427. else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
  15428. ;
  15429. else if (sec == NULL || sec->owner == NULL)
  15430. {
  15431. bfd_set_error (bfd_error_bad_value);
  15432. return false;
  15433. }
  15434. else
  15435. {
  15436. asection *osec = sec->output_section;
  15437. if ((osec->flags & SEC_THREAD_LOCAL) != 0)
  15438. {
  15439. /* TLS symbol values are relative to the
  15440. TLS segment. Dynamic relocations for
  15441. local TLS symbols therefore can't be
  15442. reduced to a relocation against their
  15443. section symbol because it holds the
  15444. address of the section, not a value
  15445. relative to the TLS segment. We could
  15446. change the .tdata dynamic section symbol
  15447. to be zero value but STN_UNDEF works
  15448. and is used elsewhere, eg. for TPREL64
  15449. GOT relocs against local TLS symbols. */
  15450. osec = htab->elf.tls_sec;
  15451. indx = 0;
  15452. }
  15453. else
  15454. {
  15455. indx = elf_section_data (osec)->dynindx;
  15456. if (indx == 0)
  15457. {
  15458. if ((osec->flags & SEC_READONLY) == 0
  15459. && htab->elf.data_index_section != NULL)
  15460. osec = htab->elf.data_index_section;
  15461. else
  15462. osec = htab->elf.text_index_section;
  15463. indx = elf_section_data (osec)->dynindx;
  15464. }
  15465. BFD_ASSERT (indx != 0);
  15466. }
  15467. /* We are turning this relocation into one
  15468. against a section symbol, so subtract out
  15469. the output section's address but not the
  15470. offset of the input section in the output
  15471. section. */
  15472. outrel.r_addend -= osec->vma;
  15473. }
  15474. outrel.r_info = ELF64_R_INFO (indx, r_type);
  15475. }
  15476. }
  15477. if (!(info->enable_dt_relr
  15478. && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
  15479. && rel->r_offset % 2 == 0
  15480. && input_section->alignment_power != 0
  15481. && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
  15482. {
  15483. sreloc = elf_section_data (input_section)->sreloc;
  15484. if (h != NULL
  15485. ? h->elf.type == STT_GNU_IFUNC
  15486. : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  15487. {
  15488. sreloc = htab->elf.irelplt;
  15489. if (indx == 0 || is_static_defined (&h->elf))
  15490. htab->elf.ifunc_resolvers = true;
  15491. }
  15492. if (sreloc == NULL)
  15493. abort ();
  15494. if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
  15495. >= sreloc->size)
  15496. abort ();
  15497. loc = sreloc->contents;
  15498. loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
  15499. bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
  15500. }
  15501. if (!warned_dynamic
  15502. && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
  15503. {
  15504. info->callbacks->einfo
  15505. /* xgettext:c-format */
  15506. (_("%X%P: %pB: %s against %pT "
  15507. "is not supported by glibc as a dynamic relocation\n"),
  15508. input_bfd,
  15509. ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
  15510. sym_name);
  15511. warned_dynamic = true;
  15512. }
  15513. /* If this reloc is against an external symbol, it will
  15514. be computed at runtime, so there's no need to do
  15515. anything now. However, for the sake of prelink ensure
  15516. that the section contents are a known value. */
  15517. if (!relocate)
  15518. {
  15519. unresolved_reloc = false;
  15520. /* The value chosen here is quite arbitrary as ld.so
  15521. ignores section contents except for the special
  15522. case of .opd where the contents might be accessed
  15523. before relocation. Choose zero, as that won't
  15524. cause reloc overflow. */
  15525. relocation = 0;
  15526. addend = 0;
  15527. /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
  15528. to improve backward compatibility with older
  15529. versions of ld. */
  15530. if (r_type == R_PPC64_ADDR64)
  15531. addend = outrel.r_addend;
  15532. /* Adjust pc_relative relocs to have zero in *r_offset. */
  15533. else if (ppc64_elf_howto_table[r_type]->pc_relative)
  15534. addend = outrel.r_offset;
  15535. }
  15536. }
  15537. break;
  15538. case R_PPC64_COPY:
  15539. case R_PPC64_GLOB_DAT:
  15540. case R_PPC64_JMP_SLOT:
  15541. case R_PPC64_JMP_IREL:
  15542. case R_PPC64_RELATIVE:
  15543. /* We shouldn't ever see these dynamic relocs in relocatable
  15544. files. */
  15545. /* Fall through. */
  15546. case R_PPC64_PLTGOT16:
  15547. case R_PPC64_PLTGOT16_DS:
  15548. case R_PPC64_PLTGOT16_HA:
  15549. case R_PPC64_PLTGOT16_HI:
  15550. case R_PPC64_PLTGOT16_LO:
  15551. case R_PPC64_PLTGOT16_LO_DS:
  15552. case R_PPC64_PLTREL32:
  15553. case R_PPC64_PLTREL64:
  15554. /* These ones haven't been implemented yet. */
  15555. info->callbacks->einfo
  15556. /* xgettext:c-format */
  15557. (_("%P: %pB: %s is not supported for `%pT'\n"),
  15558. input_bfd,
  15559. ppc64_elf_howto_table[r_type]->name, sym_name);
  15560. bfd_set_error (bfd_error_invalid_operation);
  15561. ret = false;
  15562. goto copy_reloc;
  15563. }
  15564. /* Multi-instruction sequences that access the TOC can be
  15565. optimized, eg. addis ra,r2,0; addi rb,ra,x;
  15566. to nop; addi rb,r2,x; */
  15567. switch (r_type)
  15568. {
  15569. default:
  15570. break;
  15571. case R_PPC64_GOT_TLSLD16_HI:
  15572. case R_PPC64_GOT_TLSGD16_HI:
  15573. case R_PPC64_GOT_TPREL16_HI:
  15574. case R_PPC64_GOT_DTPREL16_HI:
  15575. case R_PPC64_GOT16_HI:
  15576. case R_PPC64_TOC16_HI:
  15577. /* These relocs would only be useful if building up an
  15578. offset to later add to r2, perhaps in an indexed
  15579. addressing mode instruction. Don't try to optimize.
  15580. Unfortunately, the possibility of someone building up an
  15581. offset like this or even with the HA relocs, means that
  15582. we need to check the high insn when optimizing the low
  15583. insn. */
  15584. break;
  15585. case R_PPC64_PLTCALL_NOTOC:
  15586. if (!unresolved_reloc)
  15587. htab->notoc_plt = 1;
  15588. /* Fall through. */
  15589. case R_PPC64_PLTCALL:
  15590. if (unresolved_reloc
  15591. && offset_in_range (input_section, rel->r_offset,
  15592. r_type == R_PPC64_PLTCALL ? 8 : 4))
  15593. {
  15594. /* No plt entry. Make this into a direct call. */
  15595. bfd_byte *p = contents + rel->r_offset;
  15596. insn = bfd_get_32 (input_bfd, p);
  15597. insn &= 1;
  15598. bfd_put_32 (input_bfd, B_DOT | insn, p);
  15599. if (r_type == R_PPC64_PLTCALL)
  15600. bfd_put_32 (input_bfd, NOP, p + 4);
  15601. unresolved_reloc = save_unresolved_reloc;
  15602. r_type = R_PPC64_REL24;
  15603. }
  15604. break;
  15605. case R_PPC64_PLTSEQ_NOTOC:
  15606. case R_PPC64_PLTSEQ:
  15607. if (unresolved_reloc)
  15608. {
  15609. unresolved_reloc = false;
  15610. goto nop_it;
  15611. }
  15612. break;
  15613. case R_PPC64_PLT_PCREL34_NOTOC:
  15614. if (!unresolved_reloc)
  15615. htab->notoc_plt = 1;
  15616. /* Fall through. */
  15617. case R_PPC64_PLT_PCREL34:
  15618. if (unresolved_reloc
  15619. && offset_in_range (input_section, rel->r_offset, 8))
  15620. {
  15621. bfd_byte *p = contents + rel->r_offset;
  15622. bfd_put_32 (input_bfd, PNOP >> 32, p);
  15623. bfd_put_32 (input_bfd, PNOP, p + 4);
  15624. unresolved_reloc = false;
  15625. goto copy_reloc;
  15626. }
  15627. break;
  15628. case R_PPC64_PLT16_HA:
  15629. if (unresolved_reloc)
  15630. {
  15631. unresolved_reloc = false;
  15632. goto nop_it;
  15633. }
  15634. /* Fall through. */
  15635. case R_PPC64_GOT_TLSLD16_HA:
  15636. case R_PPC64_GOT_TLSGD16_HA:
  15637. case R_PPC64_GOT_TPREL16_HA:
  15638. case R_PPC64_GOT_DTPREL16_HA:
  15639. case R_PPC64_GOT16_HA:
  15640. case R_PPC64_TOC16_HA:
  15641. if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
  15642. && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
  15643. && !(bfd_link_pic (info)
  15644. && (h != NULL
  15645. ? bfd_is_abs_symbol (&h->elf.root)
  15646. : sec == bfd_abs_section_ptr)))
  15647. {
  15648. bfd_byte *p;
  15649. nop_it:
  15650. if (offset_in_range (input_section, rel->r_offset & ~3, 4))
  15651. {
  15652. p = contents + (rel->r_offset & ~3);
  15653. bfd_put_32 (input_bfd, NOP, p);
  15654. goto copy_reloc;
  15655. }
  15656. }
  15657. break;
  15658. case R_PPC64_PLT16_LO:
  15659. case R_PPC64_PLT16_LO_DS:
  15660. if (unresolved_reloc)
  15661. {
  15662. unresolved_reloc = false;
  15663. goto nop_it;
  15664. }
  15665. /* Fall through. */
  15666. case R_PPC64_GOT_TLSLD16_LO:
  15667. case R_PPC64_GOT_TLSGD16_LO:
  15668. case R_PPC64_GOT_TPREL16_LO_DS:
  15669. case R_PPC64_GOT_DTPREL16_LO_DS:
  15670. case R_PPC64_GOT16_LO:
  15671. case R_PPC64_GOT16_LO_DS:
  15672. case R_PPC64_TOC16_LO:
  15673. case R_PPC64_TOC16_LO_DS:
  15674. if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
  15675. && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
  15676. && !(bfd_link_pic (info)
  15677. && (h != NULL
  15678. ? bfd_is_abs_symbol (&h->elf.root)
  15679. : sec == bfd_abs_section_ptr))
  15680. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  15681. {
  15682. bfd_byte *p = contents + (rel->r_offset & ~3);
  15683. insn = bfd_get_32 (input_bfd, p);
  15684. if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
  15685. {
  15686. /* Transform addic to addi when we change reg. */
  15687. insn &= ~((0x3fu << 26) | (0x1f << 16));
  15688. insn |= (14u << 26) | (2 << 16);
  15689. }
  15690. else
  15691. {
  15692. insn &= ~(0x1f << 16);
  15693. insn |= 2 << 16;
  15694. }
  15695. bfd_put_32 (input_bfd, insn, p);
  15696. }
  15697. break;
  15698. case R_PPC64_TPREL16_HA:
  15699. if (htab->do_tls_opt
  15700. && relocation + addend + 0x8000 < 0x10000
  15701. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  15702. {
  15703. bfd_byte *p = contents + (rel->r_offset & ~3);
  15704. bfd_put_32 (input_bfd, NOP, p);
  15705. goto copy_reloc;
  15706. }
  15707. break;
  15708. case R_PPC64_TPREL16_LO:
  15709. case R_PPC64_TPREL16_LO_DS:
  15710. if (htab->do_tls_opt
  15711. && relocation + addend + 0x8000 < 0x10000
  15712. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  15713. {
  15714. bfd_byte *p = contents + (rel->r_offset & ~3);
  15715. insn = bfd_get_32 (input_bfd, p);
  15716. insn &= ~(0x1f << 16);
  15717. insn |= 13 << 16;
  15718. bfd_put_32 (input_bfd, insn, p);
  15719. }
  15720. break;
  15721. }
  15722. /* Do any further special processing. */
  15723. switch (r_type)
  15724. {
  15725. default:
  15726. break;
  15727. case R_PPC64_REL16_HA:
  15728. case R_PPC64_REL16_HIGHA:
  15729. case R_PPC64_REL16_HIGHERA:
  15730. case R_PPC64_REL16_HIGHESTA:
  15731. case R_PPC64_REL16DX_HA:
  15732. case R_PPC64_ADDR16_HA:
  15733. case R_PPC64_ADDR16_HIGHA:
  15734. case R_PPC64_ADDR16_HIGHERA:
  15735. case R_PPC64_ADDR16_HIGHESTA:
  15736. case R_PPC64_TOC16_HA:
  15737. case R_PPC64_SECTOFF_HA:
  15738. case R_PPC64_TPREL16_HA:
  15739. case R_PPC64_TPREL16_HIGHA:
  15740. case R_PPC64_TPREL16_HIGHERA:
  15741. case R_PPC64_TPREL16_HIGHESTA:
  15742. case R_PPC64_DTPREL16_HA:
  15743. case R_PPC64_DTPREL16_HIGHA:
  15744. case R_PPC64_DTPREL16_HIGHERA:
  15745. case R_PPC64_DTPREL16_HIGHESTA:
  15746. /* It's just possible that this symbol is a weak symbol
  15747. that's not actually defined anywhere. In that case,
  15748. 'sec' would be NULL, and we should leave the symbol
  15749. alone (it will be set to zero elsewhere in the link). */
  15750. if (sec == NULL)
  15751. break;
  15752. /* Fall through. */
  15753. case R_PPC64_GOT16_HA:
  15754. case R_PPC64_PLTGOT16_HA:
  15755. case R_PPC64_PLT16_HA:
  15756. case R_PPC64_GOT_TLSGD16_HA:
  15757. case R_PPC64_GOT_TLSLD16_HA:
  15758. case R_PPC64_GOT_TPREL16_HA:
  15759. case R_PPC64_GOT_DTPREL16_HA:
  15760. /* Add 0x10000 if sign bit in 0:15 is set.
  15761. Bits 0:15 are not used. */
  15762. addend += 0x8000;
  15763. break;
  15764. case R_PPC64_D34_HA30:
  15765. case R_PPC64_ADDR16_HIGHERA34:
  15766. case R_PPC64_ADDR16_HIGHESTA34:
  15767. case R_PPC64_REL16_HIGHERA34:
  15768. case R_PPC64_REL16_HIGHESTA34:
  15769. if (sec != NULL)
  15770. addend += 1ULL << 33;
  15771. break;
  15772. case R_PPC64_ADDR16_DS:
  15773. case R_PPC64_ADDR16_LO_DS:
  15774. case R_PPC64_GOT16_DS:
  15775. case R_PPC64_GOT16_LO_DS:
  15776. case R_PPC64_PLT16_LO_DS:
  15777. case R_PPC64_SECTOFF_DS:
  15778. case R_PPC64_SECTOFF_LO_DS:
  15779. case R_PPC64_TOC16_DS:
  15780. case R_PPC64_TOC16_LO_DS:
  15781. case R_PPC64_PLTGOT16_DS:
  15782. case R_PPC64_PLTGOT16_LO_DS:
  15783. case R_PPC64_GOT_TPREL16_DS:
  15784. case R_PPC64_GOT_TPREL16_LO_DS:
  15785. case R_PPC64_GOT_DTPREL16_DS:
  15786. case R_PPC64_GOT_DTPREL16_LO_DS:
  15787. case R_PPC64_TPREL16_DS:
  15788. case R_PPC64_TPREL16_LO_DS:
  15789. case R_PPC64_DTPREL16_DS:
  15790. case R_PPC64_DTPREL16_LO_DS:
  15791. if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
  15792. break;
  15793. insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
  15794. mask = 3;
  15795. /* If this reloc is against an lq, lxv, or stxv insn, then
  15796. the value must be a multiple of 16. This is somewhat of
  15797. a hack, but the "correct" way to do this by defining _DQ
  15798. forms of all the _DS relocs bloats all reloc switches in
  15799. this file. It doesn't make much sense to use these
  15800. relocs in data, so testing the insn should be safe. */
  15801. if ((insn & (0x3fu << 26)) == (56u << 26)
  15802. || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
  15803. mask = 15;
  15804. relocation += addend;
  15805. addend = insn & (mask ^ 3);
  15806. if ((relocation & mask) != 0)
  15807. {
  15808. relocation ^= relocation & mask;
  15809. info->callbacks->einfo
  15810. /* xgettext:c-format */
  15811. (_("%H: error: %s not a multiple of %u\n"),
  15812. input_bfd, input_section, rel->r_offset,
  15813. ppc64_elf_howto_table[r_type]->name,
  15814. mask + 1);
  15815. bfd_set_error (bfd_error_bad_value);
  15816. ret = false;
  15817. goto copy_reloc;
  15818. }
  15819. break;
  15820. }
  15821. /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
  15822. because such sections are not SEC_ALLOC and thus ld.so will
  15823. not process them. */
  15824. howto = ppc64_elf_howto_table[(int) r_type];
  15825. if (unresolved_reloc
  15826. && !((input_section->flags & SEC_DEBUGGING) != 0
  15827. && h->elf.def_dynamic)
  15828. && _bfd_elf_section_offset (output_bfd, info, input_section,
  15829. rel->r_offset) != (bfd_vma) -1)
  15830. {
  15831. info->callbacks->einfo
  15832. /* xgettext:c-format */
  15833. (_("%H: unresolvable %s against `%pT'\n"),
  15834. input_bfd, input_section, rel->r_offset,
  15835. howto->name,
  15836. h->elf.root.root.string);
  15837. ret = false;
  15838. }
  15839. /* 16-bit fields in insns mostly have signed values, but a
  15840. few insns have 16-bit unsigned values. Really, we should
  15841. have different reloc types. */
  15842. if (howto->complain_on_overflow != complain_overflow_dont
  15843. && howto->dst_mask == 0xffff
  15844. && (input_section->flags & SEC_CODE) != 0
  15845. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  15846. {
  15847. enum complain_overflow complain = complain_overflow_signed;
  15848. insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
  15849. if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
  15850. complain = complain_overflow_bitfield;
  15851. else if (howto->rightshift == 0
  15852. ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
  15853. || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
  15854. || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
  15855. : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
  15856. || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
  15857. || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
  15858. complain = complain_overflow_unsigned;
  15859. if (howto->complain_on_overflow != complain)
  15860. {
  15861. alt_howto = *howto;
  15862. alt_howto.complain_on_overflow = complain;
  15863. howto = &alt_howto;
  15864. }
  15865. }
  15866. switch (r_type)
  15867. {
  15868. /* Split field relocs aren't handled by _bfd_final_link_relocate. */
  15869. case R_PPC64_D34:
  15870. case R_PPC64_D34_LO:
  15871. case R_PPC64_D34_HI30:
  15872. case R_PPC64_D34_HA30:
  15873. case R_PPC64_PCREL34:
  15874. case R_PPC64_GOT_PCREL34:
  15875. case R_PPC64_TPREL34:
  15876. case R_PPC64_DTPREL34:
  15877. case R_PPC64_GOT_TLSGD_PCREL34:
  15878. case R_PPC64_GOT_TLSLD_PCREL34:
  15879. case R_PPC64_GOT_TPREL_PCREL34:
  15880. case R_PPC64_GOT_DTPREL_PCREL34:
  15881. case R_PPC64_PLT_PCREL34:
  15882. case R_PPC64_PLT_PCREL34_NOTOC:
  15883. case R_PPC64_D28:
  15884. case R_PPC64_PCREL28:
  15885. if (!offset_in_range (input_section, rel->r_offset, 8))
  15886. r = bfd_reloc_outofrange;
  15887. else
  15888. {
  15889. relocation += addend;
  15890. if (howto->pc_relative)
  15891. relocation -= (rel->r_offset
  15892. + input_section->output_offset
  15893. + input_section->output_section->vma);
  15894. relocation >>= howto->rightshift;
  15895. pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  15896. pinsn <<= 32;
  15897. pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
  15898. pinsn &= ~howto->dst_mask;
  15899. pinsn |= (((relocation << 16) | (relocation & 0xffff))
  15900. & howto->dst_mask);
  15901. bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
  15902. bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
  15903. r = bfd_reloc_ok;
  15904. if (howto->complain_on_overflow == complain_overflow_signed
  15905. && (relocation + (1ULL << (howto->bitsize - 1))
  15906. >= 1ULL << howto->bitsize))
  15907. r = bfd_reloc_overflow;
  15908. }
  15909. break;
  15910. case R_PPC64_REL16DX_HA:
  15911. if (!offset_in_range (input_section, rel->r_offset, 4))
  15912. r = bfd_reloc_outofrange;
  15913. else
  15914. {
  15915. relocation += addend;
  15916. relocation -= (rel->r_offset
  15917. + input_section->output_offset
  15918. + input_section->output_section->vma);
  15919. relocation = (bfd_signed_vma) relocation >> 16;
  15920. insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  15921. insn &= ~0x1fffc1;
  15922. insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
  15923. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  15924. r = bfd_reloc_ok;
  15925. if (relocation + 0x8000 > 0xffff)
  15926. r = bfd_reloc_overflow;
  15927. }
  15928. break;
  15929. default:
  15930. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  15931. contents, rel->r_offset,
  15932. relocation, addend);
  15933. }
  15934. if (r != bfd_reloc_ok)
  15935. {
  15936. char *more_info = NULL;
  15937. const char *reloc_name = howto->name;
  15938. if (reloc_dest != DEST_NORMAL)
  15939. {
  15940. more_info = bfd_malloc (strlen (reloc_name) + 8);
  15941. if (more_info != NULL)
  15942. {
  15943. strcpy (more_info, reloc_name);
  15944. strcat (more_info, (reloc_dest == DEST_OPD
  15945. ? " (OPD)" : " (stub)"));
  15946. reloc_name = more_info;
  15947. }
  15948. }
  15949. if (r == bfd_reloc_overflow)
  15950. {
  15951. /* On code like "if (foo) foo();" don't report overflow
  15952. on a branch to zero when foo is undefined. */
  15953. if (!warned
  15954. && (reloc_dest == DEST_STUB
  15955. || !(h != NULL
  15956. && (h->elf.root.type == bfd_link_hash_undefweak
  15957. || h->elf.root.type == bfd_link_hash_undefined)
  15958. && is_branch_reloc (r_type))))
  15959. info->callbacks->reloc_overflow
  15960. (info, (struct bfd_link_hash_entry *) h, sym_name,
  15961. reloc_name, orig_rel.r_addend, input_bfd, input_section,
  15962. rel->r_offset);
  15963. }
  15964. else
  15965. {
  15966. info->callbacks->einfo
  15967. /* xgettext:c-format */
  15968. (_("%H: %s against `%pT': error %d\n"),
  15969. input_bfd, input_section, rel->r_offset,
  15970. reloc_name, sym_name, (int) r);
  15971. ret = false;
  15972. }
  15973. free (more_info);
  15974. }
  15975. copy_reloc:
  15976. if (wrel != rel)
  15977. *wrel = *rel;
  15978. }
  15979. if (wrel != rel)
  15980. {
  15981. Elf_Internal_Shdr *rel_hdr;
  15982. size_t deleted = rel - wrel;
  15983. rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
  15984. rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
  15985. if (rel_hdr->sh_size == 0)
  15986. {
  15987. /* It is too late to remove an empty reloc section. Leave
  15988. one NONE reloc.
  15989. ??? What is wrong with an empty section??? */
  15990. rel_hdr->sh_size = rel_hdr->sh_entsize;
  15991. deleted -= 1;
  15992. }
  15993. rel_hdr = _bfd_elf_single_rel_hdr (input_section);
  15994. rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
  15995. input_section->reloc_count -= deleted;
  15996. }
  15997. /* If we're emitting relocations, then shortly after this function
  15998. returns, reloc offsets and addends for this section will be
  15999. adjusted. Worse, reloc symbol indices will be for the output
  16000. file rather than the input. Save a copy of the relocs for
  16001. opd_entry_value. */
  16002. if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
  16003. {
  16004. bfd_size_type amt;
  16005. amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
  16006. rel = bfd_alloc (input_bfd, amt);
  16007. BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
  16008. ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
  16009. if (rel == NULL)
  16010. return false;
  16011. memcpy (rel, relocs, amt);
  16012. }
  16013. return ret;
  16014. }
  16015. /* Adjust the value of any local symbols in opd sections. */
  16016. static int
  16017. ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
  16018. const char *name ATTRIBUTE_UNUSED,
  16019. Elf_Internal_Sym *elfsym,
  16020. asection *input_sec,
  16021. struct elf_link_hash_entry *h)
  16022. {
  16023. struct _opd_sec_data *opd;
  16024. long adjust;
  16025. bfd_vma value;
  16026. if (h != NULL)
  16027. return 1;
  16028. opd = get_opd_info (input_sec);
  16029. if (opd == NULL || opd->adjust == NULL)
  16030. return 1;
  16031. value = elfsym->st_value - input_sec->output_offset;
  16032. if (!bfd_link_relocatable (info))
  16033. value -= input_sec->output_section->vma;
  16034. adjust = opd->adjust[OPD_NDX (value)];
  16035. if (adjust == -1)
  16036. return 2;
  16037. elfsym->st_value += adjust;
  16038. return 1;
  16039. }
  16040. /* Finish up dynamic symbol handling. We set the contents of various
  16041. dynamic sections here. */
  16042. static bool
  16043. ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
  16044. struct bfd_link_info *info,
  16045. struct elf_link_hash_entry *h,
  16046. Elf_Internal_Sym *sym)
  16047. {
  16048. struct ppc_link_hash_table *htab;
  16049. struct plt_entry *ent;
  16050. htab = ppc_hash_table (info);
  16051. if (htab == NULL)
  16052. return false;
  16053. if (!htab->opd_abi && !h->def_regular)
  16054. for (ent = h->plt.plist; ent != NULL; ent = ent->next)
  16055. if (ent->plt.offset != (bfd_vma) -1)
  16056. {
  16057. /* Mark the symbol as undefined, rather than as
  16058. defined in glink. Leave the value if there were
  16059. any relocations where pointer equality matters
  16060. (this is a clue for the dynamic linker, to make
  16061. function pointer comparisons work between an
  16062. application and shared library), otherwise set it
  16063. to zero. */
  16064. sym->st_shndx = SHN_UNDEF;
  16065. if (!h->pointer_equality_needed)
  16066. sym->st_value = 0;
  16067. else if (!h->ref_regular_nonweak)
  16068. {
  16069. /* This breaks function pointer comparisons, but
  16070. that is better than breaking tests for a NULL
  16071. function pointer. */
  16072. sym->st_value = 0;
  16073. }
  16074. break;
  16075. }
  16076. if (h->needs_copy
  16077. && (h->root.type == bfd_link_hash_defined
  16078. || h->root.type == bfd_link_hash_defweak)
  16079. && (h->root.u.def.section == htab->elf.sdynbss
  16080. || h->root.u.def.section == htab->elf.sdynrelro))
  16081. {
  16082. /* This symbol needs a copy reloc. Set it up. */
  16083. Elf_Internal_Rela rela;
  16084. asection *srel;
  16085. bfd_byte *loc;
  16086. if (h->dynindx == -1)
  16087. abort ();
  16088. rela.r_offset = defined_sym_val (h);
  16089. rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
  16090. rela.r_addend = 0;
  16091. if (h->root.u.def.section == htab->elf.sdynrelro)
  16092. srel = htab->elf.sreldynrelro;
  16093. else
  16094. srel = htab->elf.srelbss;
  16095. loc = srel->contents;
  16096. loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
  16097. bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
  16098. }
  16099. return true;
  16100. }
  16101. /* Used to decide how to sort relocs in an optimal manner for the
  16102. dynamic linker, before writing them out. */
  16103. static enum elf_reloc_type_class
  16104. ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
  16105. const asection *rel_sec,
  16106. const Elf_Internal_Rela *rela)
  16107. {
  16108. enum elf_ppc64_reloc_type r_type;
  16109. struct ppc_link_hash_table *htab = ppc_hash_table (info);
  16110. if (rel_sec == htab->elf.irelplt)
  16111. return reloc_class_ifunc;
  16112. r_type = ELF64_R_TYPE (rela->r_info);
  16113. switch (r_type)
  16114. {
  16115. case R_PPC64_RELATIVE:
  16116. return reloc_class_relative;
  16117. case R_PPC64_JMP_SLOT:
  16118. return reloc_class_plt;
  16119. case R_PPC64_COPY:
  16120. return reloc_class_copy;
  16121. default:
  16122. return reloc_class_normal;
  16123. }
  16124. }
  16125. /* Finish up the dynamic sections. */
  16126. static bool
  16127. ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
  16128. struct bfd_link_info *info)
  16129. {
  16130. struct ppc_link_hash_table *htab;
  16131. bfd *dynobj;
  16132. asection *sdyn;
  16133. htab = ppc_hash_table (info);
  16134. if (htab == NULL)
  16135. return false;
  16136. dynobj = htab->elf.dynobj;
  16137. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  16138. if (htab->elf.dynamic_sections_created)
  16139. {
  16140. Elf64_External_Dyn *dyncon, *dynconend;
  16141. if (sdyn == NULL || htab->elf.sgot == NULL)
  16142. abort ();
  16143. dyncon = (Elf64_External_Dyn *) sdyn->contents;
  16144. dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
  16145. for (; dyncon < dynconend; dyncon++)
  16146. {
  16147. Elf_Internal_Dyn dyn;
  16148. asection *s;
  16149. bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
  16150. switch (dyn.d_tag)
  16151. {
  16152. default:
  16153. continue;
  16154. case DT_PPC64_GLINK:
  16155. s = htab->glink;
  16156. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  16157. /* We stupidly defined DT_PPC64_GLINK to be the start
  16158. of glink rather than the first entry point, which is
  16159. what ld.so needs, and now have a bigger stub to
  16160. support automatic multiple TOCs. */
  16161. dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
  16162. break;
  16163. case DT_PPC64_OPD:
  16164. s = bfd_get_section_by_name (output_bfd, ".opd");
  16165. if (s == NULL)
  16166. continue;
  16167. dyn.d_un.d_ptr = s->vma;
  16168. break;
  16169. case DT_PPC64_OPT:
  16170. if ((htab->do_multi_toc && htab->multi_toc_needed)
  16171. || htab->notoc_plt)
  16172. dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
  16173. if (htab->has_plt_localentry0)
  16174. dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
  16175. break;
  16176. case DT_PPC64_OPDSZ:
  16177. s = bfd_get_section_by_name (output_bfd, ".opd");
  16178. if (s == NULL)
  16179. continue;
  16180. dyn.d_un.d_val = s->size;
  16181. break;
  16182. case DT_PLTGOT:
  16183. s = htab->elf.splt;
  16184. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  16185. break;
  16186. case DT_JMPREL:
  16187. s = htab->elf.srelplt;
  16188. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  16189. break;
  16190. case DT_PLTRELSZ:
  16191. dyn.d_un.d_val = htab->elf.srelplt->size;
  16192. break;
  16193. case DT_TEXTREL:
  16194. if (htab->elf.ifunc_resolvers)
  16195. info->callbacks->einfo
  16196. (_("%P: warning: text relocations and GNU indirect "
  16197. "functions may result in a segfault at runtime\n"));
  16198. continue;
  16199. }
  16200. bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  16201. }
  16202. }
  16203. if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
  16204. && htab->elf.sgot->output_section != bfd_abs_section_ptr)
  16205. {
  16206. /* Fill in the first entry in the global offset table.
  16207. We use it to hold the link-time TOCbase. */
  16208. bfd_put_64 (output_bfd,
  16209. elf_gp (output_bfd) + TOC_BASE_OFF,
  16210. htab->elf.sgot->contents);
  16211. /* Set .got entry size. */
  16212. elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
  16213. = 8;
  16214. }
  16215. if (htab->elf.splt != NULL && htab->elf.splt->size != 0
  16216. && htab->elf.splt->output_section != bfd_abs_section_ptr)
  16217. {
  16218. /* Set .plt entry size. */
  16219. elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
  16220. = PLT_ENTRY_SIZE (htab);
  16221. }
  16222. /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
  16223. brlt ourselves if emitrelocations. */
  16224. if (htab->brlt != NULL
  16225. && htab->brlt->reloc_count != 0
  16226. && !_bfd_elf_link_output_relocs (output_bfd,
  16227. htab->brlt,
  16228. elf_section_data (htab->brlt)->rela.hdr,
  16229. elf_section_data (htab->brlt)->relocs,
  16230. NULL))
  16231. return false;
  16232. if (htab->glink != NULL
  16233. && htab->glink->reloc_count != 0
  16234. && !_bfd_elf_link_output_relocs (output_bfd,
  16235. htab->glink,
  16236. elf_section_data (htab->glink)->rela.hdr,
  16237. elf_section_data (htab->glink)->relocs,
  16238. NULL))
  16239. return false;
  16240. if (htab->glink_eh_frame != NULL
  16241. && htab->glink_eh_frame->size != 0
  16242. && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
  16243. && !_bfd_elf_write_section_eh_frame (output_bfd, info,
  16244. htab->glink_eh_frame,
  16245. htab->glink_eh_frame->contents))
  16246. return false;
  16247. /* We need to handle writing out multiple GOT sections ourselves,
  16248. since we didn't add them to DYNOBJ. We know dynobj is the first
  16249. bfd. */
  16250. while ((dynobj = dynobj->link.next) != NULL)
  16251. {
  16252. asection *s;
  16253. if (!is_ppc64_elf (dynobj))
  16254. continue;
  16255. s = ppc64_elf_tdata (dynobj)->got;
  16256. if (s != NULL
  16257. && s->size != 0
  16258. && s->output_section != bfd_abs_section_ptr
  16259. && !bfd_set_section_contents (output_bfd, s->output_section,
  16260. s->contents, s->output_offset,
  16261. s->size))
  16262. return false;
  16263. s = ppc64_elf_tdata (dynobj)->relgot;
  16264. if (s != NULL
  16265. && s->size != 0
  16266. && s->output_section != bfd_abs_section_ptr
  16267. && !bfd_set_section_contents (output_bfd, s->output_section,
  16268. s->contents, s->output_offset,
  16269. s->size))
  16270. return false;
  16271. }
  16272. return true;
  16273. }
  16274. #include "elf64-target.h"
  16275. /* FreeBSD support */
  16276. #undef TARGET_LITTLE_SYM
  16277. #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
  16278. #undef TARGET_LITTLE_NAME
  16279. #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
  16280. #undef TARGET_BIG_SYM
  16281. #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
  16282. #undef TARGET_BIG_NAME
  16283. #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
  16284. #undef ELF_OSABI
  16285. #define ELF_OSABI ELFOSABI_FREEBSD
  16286. #undef elf64_bed
  16287. #define elf64_bed elf64_powerpc_fbsd_bed
  16288. #include "elf64-target.h"