powerpc.cc 253 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576
  1. // powerpc.cc -- powerpc target support for gold.
  2. // Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. // Written by David S. Miller <davem@davemloft.net>
  4. // and David Edelsohn <edelsohn@gnu.org>
  5. // This file is part of gold.
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 3 of the License, or
  9. // (at your option) any later version.
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. // MA 02110-1301, USA.
  18. #include "gold.h"
  19. #include <set>
  20. #include <algorithm>
  21. #include "elfcpp.h"
  22. #include "dwarf.h"
  23. #include "parameters.h"
  24. #include "reloc.h"
  25. #include "powerpc.h"
  26. #include "object.h"
  27. #include "symtab.h"
  28. #include "layout.h"
  29. #include "output.h"
  30. #include "copy-relocs.h"
  31. #include "target.h"
  32. #include "target-reloc.h"
  33. #include "target-select.h"
  34. #include "tls.h"
  35. #include "errors.h"
  36. #include "gc.h"
  37. namespace
  38. {
  39. using namespace gold;
  40. template<int size, bool big_endian>
  41. class Output_data_plt_powerpc;
  42. template<int size, bool big_endian>
  43. class Output_data_brlt_powerpc;
  44. template<int size, bool big_endian>
  45. class Output_data_got_powerpc;
  46. template<int size, bool big_endian>
  47. class Output_data_glink;
  48. template<int size, bool big_endian>
  49. class Stub_table;
  50. template<int size, bool big_endian>
  51. class Output_data_save_res;
  52. template<int size, bool big_endian>
  53. class Target_powerpc;
  54. struct Stub_table_owner
  55. {
  56. Output_section* output_section;
  57. const Output_section::Input_section* owner;
  58. };
  59. inline bool
  60. is_branch_reloc(unsigned int r_type);
  61. template<int size, bool big_endian>
  62. class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
  63. {
  64. public:
  65. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  66. typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
  67. typedef Unordered_map<Address, Section_refs> Access_from;
  68. Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
  69. const typename elfcpp::Ehdr<size, big_endian>& ehdr)
  70. : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
  71. special_(0), has_small_toc_reloc_(false), opd_valid_(false),
  72. opd_ent_(), access_from_map_(), has14_(), stub_table_index_(),
  73. e_flags_(ehdr.get_e_flags()), st_other_()
  74. {
  75. this->set_abiversion(0);
  76. }
  77. ~Powerpc_relobj()
  78. { }
  79. // Read the symbols then set up st_other vector.
  80. void
  81. do_read_symbols(Read_symbols_data*);
  82. // The .got2 section shndx.
  83. unsigned int
  84. got2_shndx() const
  85. {
  86. if (size == 32)
  87. return this->special_;
  88. else
  89. return 0;
  90. }
  91. // The .opd section shndx.
  92. unsigned int
  93. opd_shndx() const
  94. {
  95. if (size == 32)
  96. return 0;
  97. else
  98. return this->special_;
  99. }
  100. // Init OPD entry arrays.
  101. void
  102. init_opd(size_t opd_size)
  103. {
  104. size_t count = this->opd_ent_ndx(opd_size);
  105. this->opd_ent_.resize(count);
  106. }
  107. // Return section and offset of function entry for .opd + R_OFF.
  108. unsigned int
  109. get_opd_ent(Address r_off, Address* value = NULL) const
  110. {
  111. size_t ndx = this->opd_ent_ndx(r_off);
  112. gold_assert(ndx < this->opd_ent_.size());
  113. gold_assert(this->opd_ent_[ndx].shndx != 0);
  114. if (value != NULL)
  115. *value = this->opd_ent_[ndx].off;
  116. return this->opd_ent_[ndx].shndx;
  117. }
  118. // Set section and offset of function entry for .opd + R_OFF.
  119. void
  120. set_opd_ent(Address r_off, unsigned int shndx, Address value)
  121. {
  122. size_t ndx = this->opd_ent_ndx(r_off);
  123. gold_assert(ndx < this->opd_ent_.size());
  124. this->opd_ent_[ndx].shndx = shndx;
  125. this->opd_ent_[ndx].off = value;
  126. }
  127. // Return discard flag for .opd + R_OFF.
  128. bool
  129. get_opd_discard(Address r_off) const
  130. {
  131. size_t ndx = this->opd_ent_ndx(r_off);
  132. gold_assert(ndx < this->opd_ent_.size());
  133. return this->opd_ent_[ndx].discard;
  134. }
  135. // Set discard flag for .opd + R_OFF.
  136. void
  137. set_opd_discard(Address r_off)
  138. {
  139. size_t ndx = this->opd_ent_ndx(r_off);
  140. gold_assert(ndx < this->opd_ent_.size());
  141. this->opd_ent_[ndx].discard = true;
  142. }
  143. bool
  144. opd_valid() const
  145. { return this->opd_valid_; }
  146. void
  147. set_opd_valid()
  148. { this->opd_valid_ = true; }
  149. // Examine .rela.opd to build info about function entry points.
  150. void
  151. scan_opd_relocs(size_t reloc_count,
  152. const unsigned char* prelocs,
  153. const unsigned char* plocal_syms);
  154. // Perform the Sized_relobj_file method, then set up opd info from
  155. // .opd relocs.
  156. void
  157. do_read_relocs(Read_relocs_data*);
  158. bool
  159. do_find_special_sections(Read_symbols_data* sd);
  160. // Adjust this local symbol value. Return false if the symbol
  161. // should be discarded from the output file.
  162. bool
  163. do_adjust_local_symbol(Symbol_value<size>* lv) const
  164. {
  165. if (size == 64 && this->opd_shndx() != 0)
  166. {
  167. bool is_ordinary;
  168. if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
  169. return true;
  170. if (this->get_opd_discard(lv->input_value()))
  171. return false;
  172. }
  173. return true;
  174. }
  175. Access_from*
  176. access_from_map()
  177. { return &this->access_from_map_; }
  178. // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
  179. // section at DST_OFF.
  180. void
  181. add_reference(Relobj* src_obj,
  182. unsigned int src_indx,
  183. typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
  184. {
  185. Section_id src_id(src_obj, src_indx);
  186. this->access_from_map_[dst_off].insert(src_id);
  187. }
  188. // Add a reference to the code section specified by the .opd entry
  189. // at DST_OFF
  190. void
  191. add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
  192. {
  193. size_t ndx = this->opd_ent_ndx(dst_off);
  194. if (ndx >= this->opd_ent_.size())
  195. this->opd_ent_.resize(ndx + 1);
  196. this->opd_ent_[ndx].gc_mark = true;
  197. }
  198. void
  199. process_gc_mark(Symbol_table* symtab)
  200. {
  201. for (size_t i = 0; i < this->opd_ent_.size(); i++)
  202. if (this->opd_ent_[i].gc_mark)
  203. {
  204. unsigned int shndx = this->opd_ent_[i].shndx;
  205. symtab->gc()->worklist().push_back(Section_id(this, shndx));
  206. }
  207. }
  208. // Return offset in output GOT section that this object will use
  209. // as a TOC pointer. Won't be just a constant with multi-toc support.
  210. Address
  211. toc_base_offset() const
  212. { return 0x8000; }
  213. void
  214. set_has_small_toc_reloc()
  215. { has_small_toc_reloc_ = true; }
  216. bool
  217. has_small_toc_reloc() const
  218. { return has_small_toc_reloc_; }
  219. void
  220. set_has_14bit_branch(unsigned int shndx)
  221. {
  222. if (shndx >= this->has14_.size())
  223. this->has14_.resize(shndx + 1);
  224. this->has14_[shndx] = true;
  225. }
  226. bool
  227. has_14bit_branch(unsigned int shndx) const
  228. { return shndx < this->has14_.size() && this->has14_[shndx]; }
  229. void
  230. set_stub_table(unsigned int shndx, unsigned int stub_index)
  231. {
  232. if (shndx >= this->stub_table_index_.size())
  233. this->stub_table_index_.resize(shndx + 1);
  234. this->stub_table_index_[shndx] = stub_index;
  235. }
  236. Stub_table<size, big_endian>*
  237. stub_table(unsigned int shndx)
  238. {
  239. if (shndx < this->stub_table_index_.size())
  240. {
  241. Target_powerpc<size, big_endian>* target
  242. = static_cast<Target_powerpc<size, big_endian>*>(
  243. parameters->sized_target<size, big_endian>());
  244. unsigned int indx = this->stub_table_index_[shndx];
  245. gold_assert(indx < target->stub_tables().size());
  246. return target->stub_tables()[indx];
  247. }
  248. return NULL;
  249. }
  250. void
  251. clear_stub_table()
  252. {
  253. this->stub_table_index_.clear();
  254. }
  255. int
  256. abiversion() const
  257. { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
  258. // Set ABI version for input and output
  259. void
  260. set_abiversion(int ver);
  261. unsigned int
  262. ppc64_local_entry_offset(const Symbol* sym) const
  263. { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
  264. unsigned int
  265. ppc64_local_entry_offset(unsigned int symndx) const
  266. { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
  267. private:
  268. struct Opd_ent
  269. {
  270. unsigned int shndx;
  271. bool discard : 1;
  272. bool gc_mark : 1;
  273. Address off;
  274. };
  275. // Return index into opd_ent_ array for .opd entry at OFF.
  276. // .opd entries are 24 bytes long, but they can be spaced 16 bytes
  277. // apart when the language doesn't use the last 8-byte word, the
  278. // environment pointer. Thus dividing the entry section offset by
  279. // 16 will give an index into opd_ent_ that works for either layout
  280. // of .opd. (It leaves some elements of the vector unused when .opd
  281. // entries are spaced 24 bytes apart, but we don't know the spacing
  282. // until relocations are processed, and in any case it is possible
  283. // for an object to have some entries spaced 16 bytes apart and
  284. // others 24 bytes apart.)
  285. size_t
  286. opd_ent_ndx(size_t off) const
  287. { return off >> 4;}
  288. // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
  289. unsigned int special_;
  290. // For 64-bit, whether this object uses small model relocs to access
  291. // the toc.
  292. bool has_small_toc_reloc_;
  293. // Set at the start of gc_process_relocs, when we know opd_ent_
  294. // vector is valid. The flag could be made atomic and set in
  295. // do_read_relocs with memory_order_release and then tested with
  296. // memory_order_acquire, potentially resulting in fewer entries in
  297. // access_from_map_.
  298. bool opd_valid_;
  299. // The first 8-byte word of an OPD entry gives the address of the
  300. // entry point of the function. Relocatable object files have a
  301. // relocation on this word. The following vector records the
  302. // section and offset specified by these relocations.
  303. std::vector<Opd_ent> opd_ent_;
  304. // References made to this object's .opd section when running
  305. // gc_process_relocs for another object, before the opd_ent_ vector
  306. // is valid for this object.
  307. Access_from access_from_map_;
  308. // Whether input section has a 14-bit branch reloc.
  309. std::vector<bool> has14_;
  310. // The stub table to use for a given input section.
  311. std::vector<unsigned int> stub_table_index_;
  312. // Header e_flags
  313. elfcpp::Elf_Word e_flags_;
  314. // ELF st_other field for local symbols.
  315. std::vector<unsigned char> st_other_;
  316. };
  317. template<int size, bool big_endian>
  318. class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
  319. {
  320. public:
  321. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  322. Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
  323. const typename elfcpp::Ehdr<size, big_endian>& ehdr)
  324. : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
  325. opd_shndx_(0), opd_ent_(), e_flags_(ehdr.get_e_flags())
  326. {
  327. this->set_abiversion(0);
  328. }
  329. ~Powerpc_dynobj()
  330. { }
  331. // Call Sized_dynobj::do_read_symbols to read the symbols then
  332. // read .opd from a dynamic object, filling in opd_ent_ vector,
  333. void
  334. do_read_symbols(Read_symbols_data*);
  335. // The .opd section shndx.
  336. unsigned int
  337. opd_shndx() const
  338. {
  339. return this->opd_shndx_;
  340. }
  341. // The .opd section address.
  342. Address
  343. opd_address() const
  344. {
  345. return this->opd_address_;
  346. }
  347. // Init OPD entry arrays.
  348. void
  349. init_opd(size_t opd_size)
  350. {
  351. size_t count = this->opd_ent_ndx(opd_size);
  352. this->opd_ent_.resize(count);
  353. }
  354. // Return section and offset of function entry for .opd + R_OFF.
  355. unsigned int
  356. get_opd_ent(Address r_off, Address* value = NULL) const
  357. {
  358. size_t ndx = this->opd_ent_ndx(r_off);
  359. gold_assert(ndx < this->opd_ent_.size());
  360. gold_assert(this->opd_ent_[ndx].shndx != 0);
  361. if (value != NULL)
  362. *value = this->opd_ent_[ndx].off;
  363. return this->opd_ent_[ndx].shndx;
  364. }
  365. // Set section and offset of function entry for .opd + R_OFF.
  366. void
  367. set_opd_ent(Address r_off, unsigned int shndx, Address value)
  368. {
  369. size_t ndx = this->opd_ent_ndx(r_off);
  370. gold_assert(ndx < this->opd_ent_.size());
  371. this->opd_ent_[ndx].shndx = shndx;
  372. this->opd_ent_[ndx].off = value;
  373. }
  374. int
  375. abiversion() const
  376. { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
  377. // Set ABI version for input and output.
  378. void
  379. set_abiversion(int ver);
  380. private:
  381. // Used to specify extent of executable sections.
  382. struct Sec_info
  383. {
  384. Sec_info(Address start_, Address len_, unsigned int shndx_)
  385. : start(start_), len(len_), shndx(shndx_)
  386. { }
  387. bool
  388. operator<(const Sec_info& that) const
  389. { return this->start < that.start; }
  390. Address start;
  391. Address len;
  392. unsigned int shndx;
  393. };
  394. struct Opd_ent
  395. {
  396. unsigned int shndx;
  397. Address off;
  398. };
  399. // Return index into opd_ent_ array for .opd entry at OFF.
  400. size_t
  401. opd_ent_ndx(size_t off) const
  402. { return off >> 4;}
  403. // For 64-bit the .opd section shndx and address.
  404. unsigned int opd_shndx_;
  405. Address opd_address_;
  406. // The first 8-byte word of an OPD entry gives the address of the
  407. // entry point of the function. Records the section and offset
  408. // corresponding to the address. Note that in dynamic objects,
  409. // offset is *not* relative to the section.
  410. std::vector<Opd_ent> opd_ent_;
  411. // Header e_flags
  412. elfcpp::Elf_Word e_flags_;
  413. };
  414. template<int size, bool big_endian>
  415. class Target_powerpc : public Sized_target<size, big_endian>
  416. {
  417. public:
  418. typedef
  419. Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
  420. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  421. typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
  422. static const Address invalid_address = static_cast<Address>(0) - 1;
  423. // Offset of tp and dtp pointers from start of TLS block.
  424. static const Address tp_offset = 0x7000;
  425. static const Address dtp_offset = 0x8000;
  426. Target_powerpc()
  427. : Sized_target<size, big_endian>(&powerpc_info),
  428. got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
  429. glink_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_POWERPC_COPY),
  430. tlsld_got_offset_(-1U),
  431. stub_tables_(), branch_lookup_table_(), branch_info_(),
  432. plt_thread_safe_(false), relax_failed_(false), relax_fail_count_(0),
  433. stub_group_size_(0), savres_section_(0)
  434. {
  435. }
  436. // Process the relocations to determine unreferenced sections for
  437. // garbage collection.
  438. void
  439. gc_process_relocs(Symbol_table* symtab,
  440. Layout* layout,
  441. Sized_relobj_file<size, big_endian>* object,
  442. unsigned int data_shndx,
  443. unsigned int sh_type,
  444. const unsigned char* prelocs,
  445. size_t reloc_count,
  446. Output_section* output_section,
  447. bool needs_special_offset_handling,
  448. size_t local_symbol_count,
  449. const unsigned char* plocal_symbols);
  450. // Scan the relocations to look for symbol adjustments.
  451. void
  452. scan_relocs(Symbol_table* symtab,
  453. Layout* layout,
  454. Sized_relobj_file<size, big_endian>* object,
  455. unsigned int data_shndx,
  456. unsigned int sh_type,
  457. const unsigned char* prelocs,
  458. size_t reloc_count,
  459. Output_section* output_section,
  460. bool needs_special_offset_handling,
  461. size_t local_symbol_count,
  462. const unsigned char* plocal_symbols);
  463. // Map input .toc section to output .got section.
  464. const char*
  465. do_output_section_name(const Relobj*, const char* name, size_t* plen) const
  466. {
  467. if (size == 64 && strcmp(name, ".toc") == 0)
  468. {
  469. *plen = 4;
  470. return ".got";
  471. }
  472. return NULL;
  473. }
  474. // Provide linker defined save/restore functions.
  475. void
  476. define_save_restore_funcs(Layout*, Symbol_table*);
  477. // No stubs unless a final link.
  478. bool
  479. do_may_relax() const
  480. { return !parameters->options().relocatable(); }
  481. bool
  482. do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
  483. void
  484. do_plt_fde_location(const Output_data*, unsigned char*,
  485. uint64_t*, off_t*) const;
  486. // Stash info about branches, for stub generation.
  487. void
  488. push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
  489. unsigned int data_shndx, Address r_offset,
  490. unsigned int r_type, unsigned int r_sym, Address addend)
  491. {
  492. Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
  493. this->branch_info_.push_back(info);
  494. if (r_type == elfcpp::R_POWERPC_REL14
  495. || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
  496. || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
  497. ppc_object->set_has_14bit_branch(data_shndx);
  498. }
  499. void
  500. do_define_standard_symbols(Symbol_table*, Layout*);
  501. // Finalize the sections.
  502. void
  503. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  504. // Return the value to use for a dynamic which requires special
  505. // treatment.
  506. uint64_t
  507. do_dynsym_value(const Symbol*) const;
  508. // Return the PLT address to use for a local symbol.
  509. uint64_t
  510. do_plt_address_for_local(const Relobj*, unsigned int) const;
  511. // Return the PLT address to use for a global symbol.
  512. uint64_t
  513. do_plt_address_for_global(const Symbol*) const;
  514. // Return the offset to use for the GOT_INDX'th got entry which is
  515. // for a local tls symbol specified by OBJECT, SYMNDX.
  516. int64_t
  517. do_tls_offset_for_local(const Relobj* object,
  518. unsigned int symndx,
  519. unsigned int got_indx) const;
  520. // Return the offset to use for the GOT_INDX'th got entry which is
  521. // for global tls symbol GSYM.
  522. int64_t
  523. do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
  524. void
  525. do_function_location(Symbol_location*) const;
  526. bool
  527. do_can_check_for_function_pointers() const
  528. { return true; }
  529. // Adjust -fsplit-stack code which calls non-split-stack code.
  530. void
  531. do_calls_non_split(Relobj* object, unsigned int shndx,
  532. section_offset_type fnoffset, section_size_type fnsize,
  533. unsigned char* view, section_size_type view_size,
  534. std::string* from, std::string* to) const;
  535. // Relocate a section.
  536. void
  537. relocate_section(const Relocate_info<size, big_endian>*,
  538. unsigned int sh_type,
  539. const unsigned char* prelocs,
  540. size_t reloc_count,
  541. Output_section* output_section,
  542. bool needs_special_offset_handling,
  543. unsigned char* view,
  544. Address view_address,
  545. section_size_type view_size,
  546. const Reloc_symbol_changes*);
  547. // Scan the relocs during a relocatable link.
  548. void
  549. scan_relocatable_relocs(Symbol_table* symtab,
  550. Layout* layout,
  551. Sized_relobj_file<size, big_endian>* object,
  552. unsigned int data_shndx,
  553. unsigned int sh_type,
  554. const unsigned char* prelocs,
  555. size_t reloc_count,
  556. Output_section* output_section,
  557. bool needs_special_offset_handling,
  558. size_t local_symbol_count,
  559. const unsigned char* plocal_symbols,
  560. Relocatable_relocs*);
  561. // Emit relocations for a section.
  562. void
  563. relocate_relocs(const Relocate_info<size, big_endian>*,
  564. unsigned int sh_type,
  565. const unsigned char* prelocs,
  566. size_t reloc_count,
  567. Output_section* output_section,
  568. typename elfcpp::Elf_types<size>::Elf_Off
  569. offset_in_output_section,
  570. const Relocatable_relocs*,
  571. unsigned char*,
  572. Address view_address,
  573. section_size_type,
  574. unsigned char* reloc_view,
  575. section_size_type reloc_view_size);
  576. // Return whether SYM is defined by the ABI.
  577. bool
  578. do_is_defined_by_abi(const Symbol* sym) const
  579. {
  580. return strcmp(sym->name(), "__tls_get_addr") == 0;
  581. }
  582. // Return the size of the GOT section.
  583. section_size_type
  584. got_size() const
  585. {
  586. gold_assert(this->got_ != NULL);
  587. return this->got_->data_size();
  588. }
  589. // Get the PLT section.
  590. const Output_data_plt_powerpc<size, big_endian>*
  591. plt_section() const
  592. {
  593. gold_assert(this->plt_ != NULL);
  594. return this->plt_;
  595. }
  596. // Get the IPLT section.
  597. const Output_data_plt_powerpc<size, big_endian>*
  598. iplt_section() const
  599. {
  600. gold_assert(this->iplt_ != NULL);
  601. return this->iplt_;
  602. }
  603. // Get the .glink section.
  604. const Output_data_glink<size, big_endian>*
  605. glink_section() const
  606. {
  607. gold_assert(this->glink_ != NULL);
  608. return this->glink_;
  609. }
  610. Output_data_glink<size, big_endian>*
  611. glink_section()
  612. {
  613. gold_assert(this->glink_ != NULL);
  614. return this->glink_;
  615. }
  616. bool has_glink() const
  617. { return this->glink_ != NULL; }
  618. // Get the GOT section.
  619. const Output_data_got_powerpc<size, big_endian>*
  620. got_section() const
  621. {
  622. gold_assert(this->got_ != NULL);
  623. return this->got_;
  624. }
  625. // Get the GOT section, creating it if necessary.
  626. Output_data_got_powerpc<size, big_endian>*
  627. got_section(Symbol_table*, Layout*);
  628. Object*
  629. do_make_elf_object(const std::string&, Input_file*, off_t,
  630. const elfcpp::Ehdr<size, big_endian>&);
  631. // Return the number of entries in the GOT.
  632. unsigned int
  633. got_entry_count() const
  634. {
  635. if (this->got_ == NULL)
  636. return 0;
  637. return this->got_size() / (size / 8);
  638. }
  639. // Return the number of entries in the PLT.
  640. unsigned int
  641. plt_entry_count() const;
  642. // Return the offset of the first non-reserved PLT entry.
  643. unsigned int
  644. first_plt_entry_offset() const
  645. {
  646. if (size == 32)
  647. return 0;
  648. if (this->abiversion() >= 2)
  649. return 16;
  650. return 24;
  651. }
  652. // Return the size of each PLT entry.
  653. unsigned int
  654. plt_entry_size() const
  655. {
  656. if (size == 32)
  657. return 4;
  658. if (this->abiversion() >= 2)
  659. return 8;
  660. return 24;
  661. }
  662. Output_data_save_res<size, big_endian>*
  663. savres_section() const
  664. {
  665. return this->savres_section_;
  666. }
  667. // Add any special sections for this symbol to the gc work list.
  668. // For powerpc64, this adds the code section of a function
  669. // descriptor.
  670. void
  671. do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
  672. // Handle target specific gc actions when adding a gc reference from
  673. // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
  674. // and DST_OFF. For powerpc64, this adds a referenc to the code
  675. // section of a function descriptor.
  676. void
  677. do_gc_add_reference(Symbol_table* symtab,
  678. Relobj* src_obj,
  679. unsigned int src_shndx,
  680. Relobj* dst_obj,
  681. unsigned int dst_shndx,
  682. Address dst_off) const;
  683. typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
  684. const Stub_tables&
  685. stub_tables() const
  686. { return this->stub_tables_; }
  687. const Output_data_brlt_powerpc<size, big_endian>*
  688. brlt_section() const
  689. { return this->brlt_section_; }
  690. void
  691. add_branch_lookup_table(Address to)
  692. {
  693. unsigned int off = this->branch_lookup_table_.size() * (size / 8);
  694. this->branch_lookup_table_.insert(std::make_pair(to, off));
  695. }
  696. Address
  697. find_branch_lookup_table(Address to)
  698. {
  699. typename Branch_lookup_table::const_iterator p
  700. = this->branch_lookup_table_.find(to);
  701. return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
  702. }
  703. void
  704. write_branch_lookup_table(unsigned char *oview)
  705. {
  706. for (typename Branch_lookup_table::const_iterator p
  707. = this->branch_lookup_table_.begin();
  708. p != this->branch_lookup_table_.end();
  709. ++p)
  710. {
  711. elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
  712. }
  713. }
  714. bool
  715. plt_thread_safe() const
  716. { return this->plt_thread_safe_; }
  717. int
  718. abiversion () const
  719. { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
  720. void
  721. set_abiversion (int ver)
  722. {
  723. elfcpp::Elf_Word flags = this->processor_specific_flags();
  724. flags &= ~elfcpp::EF_PPC64_ABI;
  725. flags |= ver & elfcpp::EF_PPC64_ABI;
  726. this->set_processor_specific_flags(flags);
  727. }
  728. // Offset to to save stack slot
  729. int
  730. stk_toc () const
  731. { return this->abiversion() < 2 ? 40 : 24; }
  732. private:
  733. class Track_tls
  734. {
  735. public:
  736. enum Tls_get_addr
  737. {
  738. NOT_EXPECTED = 0,
  739. EXPECTED = 1,
  740. SKIP = 2,
  741. NORMAL = 3
  742. };
  743. Track_tls()
  744. : tls_get_addr_(NOT_EXPECTED),
  745. relinfo_(NULL), relnum_(0), r_offset_(0)
  746. { }
  747. ~Track_tls()
  748. {
  749. if (this->tls_get_addr_ != NOT_EXPECTED)
  750. this->missing();
  751. }
  752. void
  753. missing(void)
  754. {
  755. if (this->relinfo_ != NULL)
  756. gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
  757. _("missing expected __tls_get_addr call"));
  758. }
  759. void
  760. expect_tls_get_addr_call(
  761. const Relocate_info<size, big_endian>* relinfo,
  762. size_t relnum,
  763. Address r_offset)
  764. {
  765. this->tls_get_addr_ = EXPECTED;
  766. this->relinfo_ = relinfo;
  767. this->relnum_ = relnum;
  768. this->r_offset_ = r_offset;
  769. }
  770. void
  771. expect_tls_get_addr_call()
  772. { this->tls_get_addr_ = EXPECTED; }
  773. void
  774. skip_next_tls_get_addr_call()
  775. {this->tls_get_addr_ = SKIP; }
  776. Tls_get_addr
  777. maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
  778. {
  779. bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
  780. || r_type == elfcpp::R_PPC_PLTREL24)
  781. && gsym != NULL
  782. && strcmp(gsym->name(), "__tls_get_addr") == 0);
  783. Tls_get_addr last_tls = this->tls_get_addr_;
  784. this->tls_get_addr_ = NOT_EXPECTED;
  785. if (is_tls_call && last_tls != EXPECTED)
  786. return last_tls;
  787. else if (!is_tls_call && last_tls != NOT_EXPECTED)
  788. {
  789. this->missing();
  790. return EXPECTED;
  791. }
  792. return NORMAL;
  793. }
  794. private:
  795. // What we're up to regarding calls to __tls_get_addr.
  796. // On powerpc, the branch and link insn making a call to
  797. // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
  798. // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
  799. // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
  800. // The marker relocation always comes first, and has the same
  801. // symbol as the reloc on the insn setting up the __tls_get_addr
  802. // argument. This ties the arg setup insn with the call insn,
  803. // allowing ld to safely optimize away the call. We check that
  804. // every call to __tls_get_addr has a marker relocation, and that
  805. // every marker relocation is on a call to __tls_get_addr.
  806. Tls_get_addr tls_get_addr_;
  807. // Info about the last reloc for error message.
  808. const Relocate_info<size, big_endian>* relinfo_;
  809. size_t relnum_;
  810. Address r_offset_;
  811. };
  812. // The class which scans relocations.
  813. class Scan : protected Track_tls
  814. {
  815. public:
  816. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  817. Scan()
  818. : Track_tls(), issued_non_pic_error_(false)
  819. { }
  820. static inline int
  821. get_reference_flags(unsigned int r_type, const Target_powerpc* target);
  822. inline void
  823. local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
  824. Sized_relobj_file<size, big_endian>* object,
  825. unsigned int data_shndx,
  826. Output_section* output_section,
  827. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  828. const elfcpp::Sym<size, big_endian>& lsym,
  829. bool is_discarded);
  830. inline void
  831. global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
  832. Sized_relobj_file<size, big_endian>* object,
  833. unsigned int data_shndx,
  834. Output_section* output_section,
  835. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  836. Symbol* gsym);
  837. inline bool
  838. local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
  839. Target_powerpc* ,
  840. Sized_relobj_file<size, big_endian>* relobj,
  841. unsigned int ,
  842. Output_section* ,
  843. const elfcpp::Rela<size, big_endian>& ,
  844. unsigned int r_type,
  845. const elfcpp::Sym<size, big_endian>&)
  846. {
  847. // PowerPC64 .opd is not folded, so any identical function text
  848. // may be folded and we'll still keep function addresses distinct.
  849. // That means no reloc is of concern here.
  850. if (size == 64)
  851. {
  852. Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  853. <Powerpc_relobj<size, big_endian>*>(relobj);
  854. if (ppcobj->abiversion() == 1)
  855. return false;
  856. }
  857. // For 32-bit and ELFv2, conservatively assume anything but calls to
  858. // function code might be taking the address of the function.
  859. return !is_branch_reloc(r_type);
  860. }
  861. inline bool
  862. global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
  863. Target_powerpc* ,
  864. Sized_relobj_file<size, big_endian>* relobj,
  865. unsigned int ,
  866. Output_section* ,
  867. const elfcpp::Rela<size, big_endian>& ,
  868. unsigned int r_type,
  869. Symbol*)
  870. {
  871. // As above.
  872. if (size == 64)
  873. {
  874. Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  875. <Powerpc_relobj<size, big_endian>*>(relobj);
  876. if (ppcobj->abiversion() == 1)
  877. return false;
  878. }
  879. return !is_branch_reloc(r_type);
  880. }
  881. static bool
  882. reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
  883. Sized_relobj_file<size, big_endian>* object,
  884. unsigned int r_type, bool report_err);
  885. private:
  886. static void
  887. unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
  888. unsigned int r_type);
  889. static void
  890. unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
  891. unsigned int r_type, Symbol*);
  892. static void
  893. generate_tls_call(Symbol_table* symtab, Layout* layout,
  894. Target_powerpc* target);
  895. void
  896. check_non_pic(Relobj*, unsigned int r_type);
  897. // Whether we have issued an error about a non-PIC compilation.
  898. bool issued_non_pic_error_;
  899. };
  900. bool
  901. symval_for_branch(const Symbol_table* symtab,
  902. const Sized_symbol<size>* gsym,
  903. Powerpc_relobj<size, big_endian>* object,
  904. Address *value, unsigned int *dest_shndx);
  905. // The class which implements relocation.
  906. class Relocate : protected Track_tls
  907. {
  908. public:
  909. // Use 'at' branch hints when true, 'y' when false.
  910. // FIXME maybe: set this with an option.
  911. static const bool is_isa_v2 = true;
  912. Relocate()
  913. : Track_tls()
  914. { }
  915. // Do a relocation. Return false if the caller should not issue
  916. // any warnings about this relocation.
  917. inline bool
  918. relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
  919. Output_section*, size_t relnum,
  920. const elfcpp::Rela<size, big_endian>&,
  921. unsigned int r_type, const Sized_symbol<size>*,
  922. const Symbol_value<size>*,
  923. unsigned char*,
  924. typename elfcpp::Elf_types<size>::Elf_Addr,
  925. section_size_type);
  926. };
  927. class Relocate_comdat_behavior
  928. {
  929. public:
  930. // Decide what the linker should do for relocations that refer to
  931. // discarded comdat sections.
  932. inline Comdat_behavior
  933. get(const char* name)
  934. {
  935. gold::Default_comdat_behavior default_behavior;
  936. Comdat_behavior ret = default_behavior.get(name);
  937. if (ret == CB_WARNING)
  938. {
  939. if (size == 32
  940. && (strcmp(name, ".fixup") == 0
  941. || strcmp(name, ".got2") == 0))
  942. ret = CB_IGNORE;
  943. if (size == 64
  944. && (strcmp(name, ".opd") == 0
  945. || strcmp(name, ".toc") == 0
  946. || strcmp(name, ".toc1") == 0))
  947. ret = CB_IGNORE;
  948. }
  949. return ret;
  950. }
  951. };
  952. // A class which returns the size required for a relocation type,
  953. // used while scanning relocs during a relocatable link.
  954. class Relocatable_size_for_reloc
  955. {
  956. public:
  957. unsigned int
  958. get_size_for_reloc(unsigned int, Relobj*)
  959. {
  960. gold_unreachable();
  961. return 0;
  962. }
  963. };
  964. // Optimize the TLS relocation type based on what we know about the
  965. // symbol. IS_FINAL is true if the final address of this symbol is
  966. // known at link time.
  967. tls::Tls_optimization
  968. optimize_tls_gd(bool is_final)
  969. {
  970. // If we are generating a shared library, then we can't do anything
  971. // in the linker.
  972. if (parameters->options().shared())
  973. return tls::TLSOPT_NONE;
  974. if (!is_final)
  975. return tls::TLSOPT_TO_IE;
  976. return tls::TLSOPT_TO_LE;
  977. }
  978. tls::Tls_optimization
  979. optimize_tls_ld()
  980. {
  981. if (parameters->options().shared())
  982. return tls::TLSOPT_NONE;
  983. return tls::TLSOPT_TO_LE;
  984. }
  985. tls::Tls_optimization
  986. optimize_tls_ie(bool is_final)
  987. {
  988. if (!is_final || parameters->options().shared())
  989. return tls::TLSOPT_NONE;
  990. return tls::TLSOPT_TO_LE;
  991. }
  992. // Create glink.
  993. void
  994. make_glink_section(Layout*);
  995. // Create the PLT section.
  996. void
  997. make_plt_section(Symbol_table*, Layout*);
  998. void
  999. make_iplt_section(Symbol_table*, Layout*);
  1000. void
  1001. make_brlt_section(Layout*);
  1002. // Create a PLT entry for a global symbol.
  1003. void
  1004. make_plt_entry(Symbol_table*, Layout*, Symbol*);
  1005. // Create a PLT entry for a local IFUNC symbol.
  1006. void
  1007. make_local_ifunc_plt_entry(Symbol_table*, Layout*,
  1008. Sized_relobj_file<size, big_endian>*,
  1009. unsigned int);
  1010. // Create a GOT entry for local dynamic __tls_get_addr.
  1011. unsigned int
  1012. tlsld_got_offset(Symbol_table* symtab, Layout* layout,
  1013. Sized_relobj_file<size, big_endian>* object);
  1014. unsigned int
  1015. tlsld_got_offset() const
  1016. {
  1017. return this->tlsld_got_offset_;
  1018. }
  1019. // Get the dynamic reloc section, creating it if necessary.
  1020. Reloc_section*
  1021. rela_dyn_section(Layout*);
  1022. // Similarly, but for ifunc symbols get the one for ifunc.
  1023. Reloc_section*
  1024. rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
  1025. // Copy a relocation against a global symbol.
  1026. void
  1027. copy_reloc(Symbol_table* symtab, Layout* layout,
  1028. Sized_relobj_file<size, big_endian>* object,
  1029. unsigned int shndx, Output_section* output_section,
  1030. Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
  1031. {
  1032. this->copy_relocs_.copy_reloc(symtab, layout,
  1033. symtab->get_sized_symbol<size>(sym),
  1034. object, shndx, output_section,
  1035. reloc, this->rela_dyn_section(layout));
  1036. }
  1037. // Look over all the input sections, deciding where to place stubs.
  1038. void
  1039. group_sections(Layout*, const Task*, bool);
  1040. // Sort output sections by address.
  1041. struct Sort_sections
  1042. {
  1043. bool
  1044. operator()(const Output_section* sec1, const Output_section* sec2)
  1045. { return sec1->address() < sec2->address(); }
  1046. };
  1047. class Branch_info
  1048. {
  1049. public:
  1050. Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
  1051. unsigned int data_shndx,
  1052. Address r_offset,
  1053. unsigned int r_type,
  1054. unsigned int r_sym,
  1055. Address addend)
  1056. : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
  1057. r_type_(r_type), r_sym_(r_sym), addend_(addend)
  1058. { }
  1059. ~Branch_info()
  1060. { }
  1061. // If this branch needs a plt call stub, or a long branch stub, make one.
  1062. bool
  1063. make_stub(Stub_table<size, big_endian>*,
  1064. Stub_table<size, big_endian>*,
  1065. Symbol_table*) const;
  1066. private:
  1067. // The branch location..
  1068. Powerpc_relobj<size, big_endian>* object_;
  1069. unsigned int shndx_;
  1070. Address offset_;
  1071. // ..and the branch type and destination.
  1072. unsigned int r_type_;
  1073. unsigned int r_sym_;
  1074. Address addend_;
  1075. };
  1076. // Information about this specific target which we pass to the
  1077. // general Target structure.
  1078. static Target::Target_info powerpc_info;
  1079. // The types of GOT entries needed for this platform.
  1080. // These values are exposed to the ABI in an incremental link.
  1081. // Do not renumber existing values without changing the version
  1082. // number of the .gnu_incremental_inputs section.
  1083. enum Got_type
  1084. {
  1085. GOT_TYPE_STANDARD,
  1086. GOT_TYPE_TLSGD, // double entry for @got@tlsgd
  1087. GOT_TYPE_DTPREL, // entry for @got@dtprel
  1088. GOT_TYPE_TPREL // entry for @got@tprel
  1089. };
  1090. // The GOT section.
  1091. Output_data_got_powerpc<size, big_endian>* got_;
  1092. // The PLT section. This is a container for a table of addresses,
  1093. // and their relocations. Each address in the PLT has a dynamic
  1094. // relocation (R_*_JMP_SLOT) and each address will have a
  1095. // corresponding entry in .glink for lazy resolution of the PLT.
  1096. // ppc32 initialises the PLT to point at the .glink entry, while
  1097. // ppc64 leaves this to ld.so. To make a call via the PLT, the
  1098. // linker adds a stub that loads the PLT entry into ctr then
  1099. // branches to ctr. There may be more than one stub for each PLT
  1100. // entry. DT_JMPREL points at the first PLT dynamic relocation and
  1101. // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
  1102. Output_data_plt_powerpc<size, big_endian>* plt_;
  1103. // The IPLT section. Like plt_, this is a container for a table of
  1104. // addresses and their relocations, specifically for STT_GNU_IFUNC
  1105. // functions that resolve locally (STT_GNU_IFUNC functions that
  1106. // don't resolve locally go in PLT). Unlike plt_, these have no
  1107. // entry in .glink for lazy resolution, and the relocation section
  1108. // does not have a 1-1 correspondence with IPLT addresses. In fact,
  1109. // the relocation section may contain relocations against
  1110. // STT_GNU_IFUNC symbols at locations outside of IPLT. The
  1111. // relocation section will appear at the end of other dynamic
  1112. // relocations, so that ld.so applies these relocations after other
  1113. // dynamic relocations. In a static executable, the relocation
  1114. // section is emitted and marked with __rela_iplt_start and
  1115. // __rela_iplt_end symbols.
  1116. Output_data_plt_powerpc<size, big_endian>* iplt_;
  1117. // Section holding long branch destinations.
  1118. Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
  1119. // The .glink section.
  1120. Output_data_glink<size, big_endian>* glink_;
  1121. // The dynamic reloc section.
  1122. Reloc_section* rela_dyn_;
  1123. // Relocs saved to avoid a COPY reloc.
  1124. Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
  1125. // Offset of the GOT entry for local dynamic __tls_get_addr calls.
  1126. unsigned int tlsld_got_offset_;
  1127. Stub_tables stub_tables_;
  1128. typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
  1129. Branch_lookup_table branch_lookup_table_;
  1130. typedef std::vector<Branch_info> Branches;
  1131. Branches branch_info_;
  1132. bool plt_thread_safe_;
  1133. bool relax_failed_;
  1134. int relax_fail_count_;
  1135. int32_t stub_group_size_;
  1136. Output_data_save_res<size, big_endian> *savres_section_;
  1137. };
  1138. template<>
  1139. Target::Target_info Target_powerpc<32, true>::powerpc_info =
  1140. {
  1141. 32, // size
  1142. true, // is_big_endian
  1143. elfcpp::EM_PPC, // machine_code
  1144. false, // has_make_symbol
  1145. false, // has_resolve
  1146. false, // has_code_fill
  1147. true, // is_default_stack_executable
  1148. false, // can_icf_inline_merge_sections
  1149. '\0', // wrap_char
  1150. "/usr/lib/ld.so.1", // dynamic_linker
  1151. 0x10000000, // default_text_segment_address
  1152. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  1153. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  1154. false, // isolate_execinstr
  1155. 0, // rosegment_gap
  1156. elfcpp::SHN_UNDEF, // small_common_shndx
  1157. elfcpp::SHN_UNDEF, // large_common_shndx
  1158. 0, // small_common_section_flags
  1159. 0, // large_common_section_flags
  1160. NULL, // attributes_section
  1161. NULL, // attributes_vendor
  1162. "_start" // entry_symbol_name
  1163. };
  1164. template<>
  1165. Target::Target_info Target_powerpc<32, false>::powerpc_info =
  1166. {
  1167. 32, // size
  1168. false, // is_big_endian
  1169. elfcpp::EM_PPC, // machine_code
  1170. false, // has_make_symbol
  1171. false, // has_resolve
  1172. false, // has_code_fill
  1173. true, // is_default_stack_executable
  1174. false, // can_icf_inline_merge_sections
  1175. '\0', // wrap_char
  1176. "/usr/lib/ld.so.1", // dynamic_linker
  1177. 0x10000000, // default_text_segment_address
  1178. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  1179. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  1180. false, // isolate_execinstr
  1181. 0, // rosegment_gap
  1182. elfcpp::SHN_UNDEF, // small_common_shndx
  1183. elfcpp::SHN_UNDEF, // large_common_shndx
  1184. 0, // small_common_section_flags
  1185. 0, // large_common_section_flags
  1186. NULL, // attributes_section
  1187. NULL, // attributes_vendor
  1188. "_start" // entry_symbol_name
  1189. };
  1190. template<>
  1191. Target::Target_info Target_powerpc<64, true>::powerpc_info =
  1192. {
  1193. 64, // size
  1194. true, // is_big_endian
  1195. elfcpp::EM_PPC64, // machine_code
  1196. false, // has_make_symbol
  1197. false, // has_resolve
  1198. false, // has_code_fill
  1199. true, // is_default_stack_executable
  1200. false, // can_icf_inline_merge_sections
  1201. '\0', // wrap_char
  1202. "/usr/lib/ld.so.1", // dynamic_linker
  1203. 0x10000000, // default_text_segment_address
  1204. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  1205. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  1206. false, // isolate_execinstr
  1207. 0, // rosegment_gap
  1208. elfcpp::SHN_UNDEF, // small_common_shndx
  1209. elfcpp::SHN_UNDEF, // large_common_shndx
  1210. 0, // small_common_section_flags
  1211. 0, // large_common_section_flags
  1212. NULL, // attributes_section
  1213. NULL, // attributes_vendor
  1214. "_start" // entry_symbol_name
  1215. };
  1216. template<>
  1217. Target::Target_info Target_powerpc<64, false>::powerpc_info =
  1218. {
  1219. 64, // size
  1220. false, // is_big_endian
  1221. elfcpp::EM_PPC64, // machine_code
  1222. false, // has_make_symbol
  1223. false, // has_resolve
  1224. false, // has_code_fill
  1225. true, // is_default_stack_executable
  1226. false, // can_icf_inline_merge_sections
  1227. '\0', // wrap_char
  1228. "/usr/lib/ld.so.1", // dynamic_linker
  1229. 0x10000000, // default_text_segment_address
  1230. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  1231. 4 * 1024, // common_pagesize (overridable by -z common-page-size)
  1232. false, // isolate_execinstr
  1233. 0, // rosegment_gap
  1234. elfcpp::SHN_UNDEF, // small_common_shndx
  1235. elfcpp::SHN_UNDEF, // large_common_shndx
  1236. 0, // small_common_section_flags
  1237. 0, // large_common_section_flags
  1238. NULL, // attributes_section
  1239. NULL, // attributes_vendor
  1240. "_start" // entry_symbol_name
  1241. };
  1242. inline bool
  1243. is_branch_reloc(unsigned int r_type)
  1244. {
  1245. return (r_type == elfcpp::R_POWERPC_REL24
  1246. || r_type == elfcpp::R_PPC_PLTREL24
  1247. || r_type == elfcpp::R_PPC_LOCAL24PC
  1248. || r_type == elfcpp::R_POWERPC_REL14
  1249. || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
  1250. || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
  1251. || r_type == elfcpp::R_POWERPC_ADDR24
  1252. || r_type == elfcpp::R_POWERPC_ADDR14
  1253. || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
  1254. || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
  1255. }
  1256. // If INSN is an opcode that may be used with an @tls operand, return
  1257. // the transformed insn for TLS optimisation, otherwise return 0. If
  1258. // REG is non-zero only match an insn with RB or RA equal to REG.
  1259. uint32_t
  1260. at_tls_transform(uint32_t insn, unsigned int reg)
  1261. {
  1262. if ((insn & (0x3f << 26)) != 31 << 26)
  1263. return 0;
  1264. unsigned int rtra;
  1265. if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
  1266. rtra = insn & ((1 << 26) - (1 << 16));
  1267. else if (((insn >> 16) & 0x1f) == reg)
  1268. rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
  1269. else
  1270. return 0;
  1271. if ((insn & (0x3ff << 1)) == 266 << 1)
  1272. // add -> addi
  1273. insn = 14 << 26;
  1274. else if ((insn & (0x1f << 1)) == 23 << 1
  1275. && ((insn & (0x1f << 6)) < 14 << 6
  1276. || ((insn & (0x1f << 6)) >= 16 << 6
  1277. && (insn & (0x1f << 6)) < 24 << 6)))
  1278. // load and store indexed -> dform
  1279. insn = (32 | ((insn >> 6) & 0x1f)) << 26;
  1280. else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
  1281. // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
  1282. insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
  1283. else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
  1284. // lwax -> lwa
  1285. insn = (58 << 26) | 2;
  1286. else
  1287. return 0;
  1288. insn |= rtra;
  1289. return insn;
  1290. }
  1291. template<int size, bool big_endian>
  1292. class Powerpc_relocate_functions
  1293. {
  1294. public:
  1295. enum Overflow_check
  1296. {
  1297. CHECK_NONE,
  1298. CHECK_SIGNED,
  1299. CHECK_UNSIGNED,
  1300. CHECK_BITFIELD,
  1301. CHECK_LOW_INSN,
  1302. CHECK_HIGH_INSN
  1303. };
  1304. enum Status
  1305. {
  1306. STATUS_OK,
  1307. STATUS_OVERFLOW
  1308. };
  1309. private:
  1310. typedef Powerpc_relocate_functions<size, big_endian> This;
  1311. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1312. template<int valsize>
  1313. static inline bool
  1314. has_overflow_signed(Address value)
  1315. {
  1316. // limit = 1 << (valsize - 1) without shift count exceeding size of type
  1317. Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
  1318. limit <<= ((valsize - 1) >> 1);
  1319. limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
  1320. return value + limit > (limit << 1) - 1;
  1321. }
  1322. template<int valsize>
  1323. static inline bool
  1324. has_overflow_unsigned(Address value)
  1325. {
  1326. Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
  1327. limit <<= ((valsize - 1) >> 1);
  1328. limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
  1329. return value > (limit << 1) - 1;
  1330. }
  1331. template<int valsize>
  1332. static inline bool
  1333. has_overflow_bitfield(Address value)
  1334. {
  1335. return (has_overflow_unsigned<valsize>(value)
  1336. && has_overflow_signed<valsize>(value));
  1337. }
  1338. template<int valsize>
  1339. static inline Status
  1340. overflowed(Address value, Overflow_check overflow)
  1341. {
  1342. if (overflow == CHECK_SIGNED)
  1343. {
  1344. if (has_overflow_signed<valsize>(value))
  1345. return STATUS_OVERFLOW;
  1346. }
  1347. else if (overflow == CHECK_UNSIGNED)
  1348. {
  1349. if (has_overflow_unsigned<valsize>(value))
  1350. return STATUS_OVERFLOW;
  1351. }
  1352. else if (overflow == CHECK_BITFIELD)
  1353. {
  1354. if (has_overflow_bitfield<valsize>(value))
  1355. return STATUS_OVERFLOW;
  1356. }
  1357. return STATUS_OK;
  1358. }
  1359. // Do a simple RELA relocation
  1360. template<int fieldsize, int valsize>
  1361. static inline Status
  1362. rela(unsigned char* view, Address value, Overflow_check overflow)
  1363. {
  1364. typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
  1365. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1366. elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
  1367. return overflowed<valsize>(value, overflow);
  1368. }
  1369. template<int fieldsize, int valsize>
  1370. static inline Status
  1371. rela(unsigned char* view,
  1372. unsigned int right_shift,
  1373. typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
  1374. Address value,
  1375. Overflow_check overflow)
  1376. {
  1377. typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
  1378. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1379. Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
  1380. Valtype reloc = value >> right_shift;
  1381. val &= ~dst_mask;
  1382. reloc &= dst_mask;
  1383. elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
  1384. return overflowed<valsize>(value >> right_shift, overflow);
  1385. }
  1386. // Do a simple RELA relocation, unaligned.
  1387. template<int fieldsize, int valsize>
  1388. static inline Status
  1389. rela_ua(unsigned char* view, Address value, Overflow_check overflow)
  1390. {
  1391. elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
  1392. return overflowed<valsize>(value, overflow);
  1393. }
  1394. template<int fieldsize, int valsize>
  1395. static inline Status
  1396. rela_ua(unsigned char* view,
  1397. unsigned int right_shift,
  1398. typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
  1399. Address value,
  1400. Overflow_check overflow)
  1401. {
  1402. typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
  1403. Valtype;
  1404. Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
  1405. Valtype reloc = value >> right_shift;
  1406. val &= ~dst_mask;
  1407. reloc &= dst_mask;
  1408. elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
  1409. return overflowed<valsize>(value >> right_shift, overflow);
  1410. }
  1411. public:
  1412. // R_PPC64_ADDR64: (Symbol + Addend)
  1413. static inline void
  1414. addr64(unsigned char* view, Address value)
  1415. { This::template rela<64,64>(view, value, CHECK_NONE); }
  1416. // R_PPC64_UADDR64: (Symbol + Addend) unaligned
  1417. static inline void
  1418. addr64_u(unsigned char* view, Address value)
  1419. { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
  1420. // R_POWERPC_ADDR32: (Symbol + Addend)
  1421. static inline Status
  1422. addr32(unsigned char* view, Address value, Overflow_check overflow)
  1423. { return This::template rela<32,32>(view, value, overflow); }
  1424. // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
  1425. static inline Status
  1426. addr32_u(unsigned char* view, Address value, Overflow_check overflow)
  1427. { return This::template rela_ua<32,32>(view, value, overflow); }
  1428. // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
  1429. static inline Status
  1430. addr24(unsigned char* view, Address value, Overflow_check overflow)
  1431. {
  1432. Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
  1433. value, overflow);
  1434. if (overflow != CHECK_NONE && (value & 3) != 0)
  1435. stat = STATUS_OVERFLOW;
  1436. return stat;
  1437. }
  1438. // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
  1439. static inline Status
  1440. addr16(unsigned char* view, Address value, Overflow_check overflow)
  1441. { return This::template rela<16,16>(view, value, overflow); }
  1442. // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
  1443. static inline Status
  1444. addr16_u(unsigned char* view, Address value, Overflow_check overflow)
  1445. { return This::template rela_ua<16,16>(view, value, overflow); }
  1446. // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
  1447. static inline Status
  1448. addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
  1449. {
  1450. Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
  1451. if ((value & 3) != 0)
  1452. stat = STATUS_OVERFLOW;
  1453. return stat;
  1454. }
  1455. // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
  1456. static inline void
  1457. addr16_hi(unsigned char* view, Address value)
  1458. { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
  1459. // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
  1460. static inline void
  1461. addr16_ha(unsigned char* view, Address value)
  1462. { This::addr16_hi(view, value + 0x8000); }
  1463. // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
  1464. static inline void
  1465. addr16_hi2(unsigned char* view, Address value)
  1466. { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
  1467. // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
  1468. static inline void
  1469. addr16_ha2(unsigned char* view, Address value)
  1470. { This::addr16_hi2(view, value + 0x8000); }
  1471. // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
  1472. static inline void
  1473. addr16_hi3(unsigned char* view, Address value)
  1474. { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
  1475. // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
  1476. static inline void
  1477. addr16_ha3(unsigned char* view, Address value)
  1478. { This::addr16_hi3(view, value + 0x8000); }
  1479. // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
  1480. static inline Status
  1481. addr14(unsigned char* view, Address value, Overflow_check overflow)
  1482. {
  1483. Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
  1484. if (overflow != CHECK_NONE && (value & 3) != 0)
  1485. stat = STATUS_OVERFLOW;
  1486. return stat;
  1487. }
  1488. };
  1489. // Set ABI version for input and output.
  1490. template<int size, bool big_endian>
  1491. void
  1492. Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
  1493. {
  1494. this->e_flags_ |= ver;
  1495. if (this->abiversion() != 0)
  1496. {
  1497. Target_powerpc<size, big_endian>* target =
  1498. static_cast<Target_powerpc<size, big_endian>*>(
  1499. parameters->sized_target<size, big_endian>());
  1500. if (target->abiversion() == 0)
  1501. target->set_abiversion(this->abiversion());
  1502. else if (target->abiversion() != this->abiversion())
  1503. gold_error(_("%s: ABI version %d is not compatible "
  1504. "with ABI version %d output"),
  1505. this->name().c_str(),
  1506. this->abiversion(), target->abiversion());
  1507. }
  1508. }
  1509. // Stash away the index of .got2 or .opd in a relocatable object, if
  1510. // such a section exists.
  1511. template<int size, bool big_endian>
  1512. bool
  1513. Powerpc_relobj<size, big_endian>::do_find_special_sections(
  1514. Read_symbols_data* sd)
  1515. {
  1516. const unsigned char* const pshdrs = sd->section_headers->data();
  1517. const unsigned char* namesu = sd->section_names->data();
  1518. const char* names = reinterpret_cast<const char*>(namesu);
  1519. section_size_type names_size = sd->section_names_size;
  1520. const unsigned char* s;
  1521. s = this->template find_shdr<size, big_endian>(pshdrs,
  1522. size == 32 ? ".got2" : ".opd",
  1523. names, names_size, NULL);
  1524. if (s != NULL)
  1525. {
  1526. unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
  1527. this->special_ = ndx;
  1528. if (size == 64)
  1529. {
  1530. if (this->abiversion() == 0)
  1531. this->set_abiversion(1);
  1532. else if (this->abiversion() > 1)
  1533. gold_error(_("%s: .opd invalid in abiv%d"),
  1534. this->name().c_str(), this->abiversion());
  1535. }
  1536. }
  1537. return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
  1538. }
  1539. // Examine .rela.opd to build info about function entry points.
  1540. template<int size, bool big_endian>
  1541. void
  1542. Powerpc_relobj<size, big_endian>::scan_opd_relocs(
  1543. size_t reloc_count,
  1544. const unsigned char* prelocs,
  1545. const unsigned char* plocal_syms)
  1546. {
  1547. if (size == 64)
  1548. {
  1549. typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
  1550. Reltype;
  1551. const int reloc_size
  1552. = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
  1553. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1554. Address expected_off = 0;
  1555. bool regular = true;
  1556. unsigned int opd_ent_size = 0;
  1557. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  1558. {
  1559. Reltype reloc(prelocs);
  1560. typename elfcpp::Elf_types<size>::Elf_WXword r_info
  1561. = reloc.get_r_info();
  1562. unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  1563. if (r_type == elfcpp::R_PPC64_ADDR64)
  1564. {
  1565. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  1566. typename elfcpp::Elf_types<size>::Elf_Addr value;
  1567. bool is_ordinary;
  1568. unsigned int shndx;
  1569. if (r_sym < this->local_symbol_count())
  1570. {
  1571. typename elfcpp::Sym<size, big_endian>
  1572. lsym(plocal_syms + r_sym * sym_size);
  1573. shndx = lsym.get_st_shndx();
  1574. shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  1575. value = lsym.get_st_value();
  1576. }
  1577. else
  1578. shndx = this->symbol_section_and_value(r_sym, &value,
  1579. &is_ordinary);
  1580. this->set_opd_ent(reloc.get_r_offset(), shndx,
  1581. value + reloc.get_r_addend());
  1582. if (i == 2)
  1583. {
  1584. expected_off = reloc.get_r_offset();
  1585. opd_ent_size = expected_off;
  1586. }
  1587. else if (expected_off != reloc.get_r_offset())
  1588. regular = false;
  1589. expected_off += opd_ent_size;
  1590. }
  1591. else if (r_type == elfcpp::R_PPC64_TOC)
  1592. {
  1593. if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
  1594. regular = false;
  1595. }
  1596. else
  1597. {
  1598. gold_warning(_("%s: unexpected reloc type %u in .opd section"),
  1599. this->name().c_str(), r_type);
  1600. regular = false;
  1601. }
  1602. }
  1603. if (reloc_count <= 2)
  1604. opd_ent_size = this->section_size(this->opd_shndx());
  1605. if (opd_ent_size != 24 && opd_ent_size != 16)
  1606. regular = false;
  1607. if (!regular)
  1608. {
  1609. gold_warning(_("%s: .opd is not a regular array of opd entries"),
  1610. this->name().c_str());
  1611. opd_ent_size = 0;
  1612. }
  1613. }
  1614. }
  1615. template<int size, bool big_endian>
  1616. void
  1617. Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
  1618. {
  1619. Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
  1620. if (size == 64)
  1621. {
  1622. for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
  1623. p != rd->relocs.end();
  1624. ++p)
  1625. {
  1626. if (p->data_shndx == this->opd_shndx())
  1627. {
  1628. uint64_t opd_size = this->section_size(this->opd_shndx());
  1629. gold_assert(opd_size == static_cast<size_t>(opd_size));
  1630. if (opd_size != 0)
  1631. {
  1632. this->init_opd(opd_size);
  1633. this->scan_opd_relocs(p->reloc_count, p->contents->data(),
  1634. rd->local_symbols->data());
  1635. }
  1636. break;
  1637. }
  1638. }
  1639. }
  1640. }
  1641. // Read the symbols then set up st_other vector.
  1642. template<int size, bool big_endian>
  1643. void
  1644. Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
  1645. {
  1646. this->base_read_symbols(sd);
  1647. if (size == 64)
  1648. {
  1649. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  1650. const unsigned char* const pshdrs = sd->section_headers->data();
  1651. const unsigned int loccount = this->do_local_symbol_count();
  1652. if (loccount != 0)
  1653. {
  1654. this->st_other_.resize(loccount);
  1655. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1656. off_t locsize = loccount * sym_size;
  1657. const unsigned int symtab_shndx = this->symtab_shndx();
  1658. const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
  1659. typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
  1660. const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
  1661. locsize, true, false);
  1662. psyms += sym_size;
  1663. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  1664. {
  1665. elfcpp::Sym<size, big_endian> sym(psyms);
  1666. unsigned char st_other = sym.get_st_other();
  1667. this->st_other_[i] = st_other;
  1668. if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
  1669. {
  1670. if (this->abiversion() == 0)
  1671. this->set_abiversion(2);
  1672. else if (this->abiversion() < 2)
  1673. gold_error(_("%s: local symbol %d has invalid st_other"
  1674. " for ABI version 1"),
  1675. this->name().c_str(), i);
  1676. }
  1677. }
  1678. }
  1679. }
  1680. }
  1681. template<int size, bool big_endian>
  1682. void
  1683. Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
  1684. {
  1685. this->e_flags_ |= ver;
  1686. if (this->abiversion() != 0)
  1687. {
  1688. Target_powerpc<size, big_endian>* target =
  1689. static_cast<Target_powerpc<size, big_endian>*>(
  1690. parameters->sized_target<size, big_endian>());
  1691. if (target->abiversion() == 0)
  1692. target->set_abiversion(this->abiversion());
  1693. else if (target->abiversion() != this->abiversion())
  1694. gold_error(_("%s: ABI version %d is not compatible "
  1695. "with ABI version %d output"),
  1696. this->name().c_str(),
  1697. this->abiversion(), target->abiversion());
  1698. }
  1699. }
  1700. // Call Sized_dynobj::base_read_symbols to read the symbols then
  1701. // read .opd from a dynamic object, filling in opd_ent_ vector,
  1702. template<int size, bool big_endian>
  1703. void
  1704. Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
  1705. {
  1706. this->base_read_symbols(sd);
  1707. if (size == 64)
  1708. {
  1709. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  1710. const unsigned char* const pshdrs = sd->section_headers->data();
  1711. const unsigned char* namesu = sd->section_names->data();
  1712. const char* names = reinterpret_cast<const char*>(namesu);
  1713. const unsigned char* s = NULL;
  1714. const unsigned char* opd;
  1715. section_size_type opd_size;
  1716. // Find and read .opd section.
  1717. while (1)
  1718. {
  1719. s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
  1720. sd->section_names_size,
  1721. s);
  1722. if (s == NULL)
  1723. return;
  1724. typename elfcpp::Shdr<size, big_endian> shdr(s);
  1725. if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
  1726. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
  1727. {
  1728. if (this->abiversion() == 0)
  1729. this->set_abiversion(1);
  1730. else if (this->abiversion() > 1)
  1731. gold_error(_("%s: .opd invalid in abiv%d"),
  1732. this->name().c_str(), this->abiversion());
  1733. this->opd_shndx_ = (s - pshdrs) / shdr_size;
  1734. this->opd_address_ = shdr.get_sh_addr();
  1735. opd_size = convert_to_section_size_type(shdr.get_sh_size());
  1736. opd = this->get_view(shdr.get_sh_offset(), opd_size,
  1737. true, false);
  1738. break;
  1739. }
  1740. }
  1741. // Build set of executable sections.
  1742. // Using a set is probably overkill. There is likely to be only
  1743. // a few executable sections, typically .init, .text and .fini,
  1744. // and they are generally grouped together.
  1745. typedef std::set<Sec_info> Exec_sections;
  1746. Exec_sections exec_sections;
  1747. s = pshdrs;
  1748. for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
  1749. {
  1750. typename elfcpp::Shdr<size, big_endian> shdr(s);
  1751. if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
  1752. && ((shdr.get_sh_flags()
  1753. & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
  1754. == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
  1755. && shdr.get_sh_size() != 0)
  1756. {
  1757. exec_sections.insert(Sec_info(shdr.get_sh_addr(),
  1758. shdr.get_sh_size(), i));
  1759. }
  1760. }
  1761. if (exec_sections.empty())
  1762. return;
  1763. // Look over the OPD entries. This is complicated by the fact
  1764. // that some binaries will use two-word entries while others
  1765. // will use the standard three-word entries. In most cases
  1766. // the third word (the environment pointer for languages like
  1767. // Pascal) is unused and will be zero. If the third word is
  1768. // used it should not be pointing into executable sections,
  1769. // I think.
  1770. this->init_opd(opd_size);
  1771. for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
  1772. {
  1773. typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
  1774. const Valtype* valp = reinterpret_cast<const Valtype*>(p);
  1775. Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
  1776. if (val == 0)
  1777. // Chances are that this is the third word of an OPD entry.
  1778. continue;
  1779. typename Exec_sections::const_iterator e
  1780. = exec_sections.upper_bound(Sec_info(val, 0, 0));
  1781. if (e != exec_sections.begin())
  1782. {
  1783. --e;
  1784. if (e->start <= val && val < e->start + e->len)
  1785. {
  1786. // We have an address in an executable section.
  1787. // VAL ought to be the function entry, set it up.
  1788. this->set_opd_ent(p - opd, e->shndx, val);
  1789. // Skip second word of OPD entry, the TOC pointer.
  1790. p += 8;
  1791. }
  1792. }
  1793. // If we didn't match any executable sections, we likely
  1794. // have a non-zero third word in the OPD entry.
  1795. }
  1796. }
  1797. }
  1798. // Set up some symbols.
  1799. template<int size, bool big_endian>
  1800. void
  1801. Target_powerpc<size, big_endian>::do_define_standard_symbols(
  1802. Symbol_table* symtab,
  1803. Layout* layout)
  1804. {
  1805. if (size == 32)
  1806. {
  1807. // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
  1808. // undefined when scanning relocs (and thus requires
  1809. // non-relative dynamic relocs). The proper value will be
  1810. // updated later.
  1811. Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
  1812. if (gotsym != NULL && gotsym->is_undefined())
  1813. {
  1814. Target_powerpc<size, big_endian>* target =
  1815. static_cast<Target_powerpc<size, big_endian>*>(
  1816. parameters->sized_target<size, big_endian>());
  1817. Output_data_got_powerpc<size, big_endian>* got
  1818. = target->got_section(symtab, layout);
  1819. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  1820. Symbol_table::PREDEFINED,
  1821. got, 0, 0,
  1822. elfcpp::STT_OBJECT,
  1823. elfcpp::STB_LOCAL,
  1824. elfcpp::STV_HIDDEN, 0,
  1825. false, false);
  1826. }
  1827. // Define _SDA_BASE_ at the start of the .sdata section + 32768.
  1828. Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
  1829. if (sdasym != NULL && sdasym->is_undefined())
  1830. {
  1831. Output_data_space* sdata = new Output_data_space(4, "** sdata");
  1832. Output_section* os
  1833. = layout->add_output_section_data(".sdata", 0,
  1834. elfcpp::SHF_ALLOC
  1835. | elfcpp::SHF_WRITE,
  1836. sdata, ORDER_SMALL_DATA, false);
  1837. symtab->define_in_output_data("_SDA_BASE_", NULL,
  1838. Symbol_table::PREDEFINED,
  1839. os, 32768, 0, elfcpp::STT_OBJECT,
  1840. elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
  1841. 0, false, false);
  1842. }
  1843. }
  1844. else
  1845. {
  1846. // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
  1847. Symbol *gotsym = symtab->lookup(".TOC.", NULL);
  1848. if (gotsym != NULL && gotsym->is_undefined())
  1849. {
  1850. Target_powerpc<size, big_endian>* target =
  1851. static_cast<Target_powerpc<size, big_endian>*>(
  1852. parameters->sized_target<size, big_endian>());
  1853. Output_data_got_powerpc<size, big_endian>* got
  1854. = target->got_section(symtab, layout);
  1855. symtab->define_in_output_data(".TOC.", NULL,
  1856. Symbol_table::PREDEFINED,
  1857. got, 0x8000, 0,
  1858. elfcpp::STT_OBJECT,
  1859. elfcpp::STB_LOCAL,
  1860. elfcpp::STV_HIDDEN, 0,
  1861. false, false);
  1862. }
  1863. }
  1864. }
  1865. // Set up PowerPC target specific relobj.
  1866. template<int size, bool big_endian>
  1867. Object*
  1868. Target_powerpc<size, big_endian>::do_make_elf_object(
  1869. const std::string& name,
  1870. Input_file* input_file,
  1871. off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
  1872. {
  1873. int et = ehdr.get_e_type();
  1874. // ET_EXEC files are valid input for --just-symbols/-R,
  1875. // and we treat them as relocatable objects.
  1876. if (et == elfcpp::ET_REL
  1877. || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
  1878. {
  1879. Powerpc_relobj<size, big_endian>* obj =
  1880. new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
  1881. obj->setup();
  1882. return obj;
  1883. }
  1884. else if (et == elfcpp::ET_DYN)
  1885. {
  1886. Powerpc_dynobj<size, big_endian>* obj =
  1887. new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
  1888. obj->setup();
  1889. return obj;
  1890. }
  1891. else
  1892. {
  1893. gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
  1894. return NULL;
  1895. }
  1896. }
  1897. template<int size, bool big_endian>
  1898. class Output_data_got_powerpc : public Output_data_got<size, big_endian>
  1899. {
  1900. public:
  1901. typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
  1902. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
  1903. Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
  1904. : Output_data_got<size, big_endian>(),
  1905. symtab_(symtab), layout_(layout),
  1906. header_ent_cnt_(size == 32 ? 3 : 1),
  1907. header_index_(size == 32 ? 0x2000 : 0)
  1908. { }
  1909. // Override all the Output_data_got methods we use so as to first call
  1910. // reserve_ent().
  1911. bool
  1912. add_global(Symbol* gsym, unsigned int got_type)
  1913. {
  1914. this->reserve_ent();
  1915. return Output_data_got<size, big_endian>::add_global(gsym, got_type);
  1916. }
  1917. bool
  1918. add_global_plt(Symbol* gsym, unsigned int got_type)
  1919. {
  1920. this->reserve_ent();
  1921. return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
  1922. }
  1923. bool
  1924. add_global_tls(Symbol* gsym, unsigned int got_type)
  1925. { return this->add_global_plt(gsym, got_type); }
  1926. void
  1927. add_global_with_rel(Symbol* gsym, unsigned int got_type,
  1928. Output_data_reloc_generic* rel_dyn, unsigned int r_type)
  1929. {
  1930. this->reserve_ent();
  1931. Output_data_got<size, big_endian>::
  1932. add_global_with_rel(gsym, got_type, rel_dyn, r_type);
  1933. }
  1934. void
  1935. add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
  1936. Output_data_reloc_generic* rel_dyn,
  1937. unsigned int r_type_1, unsigned int r_type_2)
  1938. {
  1939. this->reserve_ent(2);
  1940. Output_data_got<size, big_endian>::
  1941. add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
  1942. }
  1943. bool
  1944. add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
  1945. {
  1946. this->reserve_ent();
  1947. return Output_data_got<size, big_endian>::add_local(object, sym_index,
  1948. got_type);
  1949. }
  1950. bool
  1951. add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
  1952. {
  1953. this->reserve_ent();
  1954. return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
  1955. got_type);
  1956. }
  1957. bool
  1958. add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
  1959. { return this->add_local_plt(object, sym_index, got_type); }
  1960. void
  1961. add_local_tls_pair(Relobj* object, unsigned int sym_index,
  1962. unsigned int got_type,
  1963. Output_data_reloc_generic* rel_dyn,
  1964. unsigned int r_type)
  1965. {
  1966. this->reserve_ent(2);
  1967. Output_data_got<size, big_endian>::
  1968. add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
  1969. }
  1970. unsigned int
  1971. add_constant(Valtype constant)
  1972. {
  1973. this->reserve_ent();
  1974. return Output_data_got<size, big_endian>::add_constant(constant);
  1975. }
  1976. unsigned int
  1977. add_constant_pair(Valtype c1, Valtype c2)
  1978. {
  1979. this->reserve_ent(2);
  1980. return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
  1981. }
  1982. // Offset of _GLOBAL_OFFSET_TABLE_.
  1983. unsigned int
  1984. g_o_t() const
  1985. {
  1986. return this->got_offset(this->header_index_);
  1987. }
  1988. // Offset of base used to access the GOT/TOC.
  1989. // The got/toc pointer reg will be set to this value.
  1990. Valtype
  1991. got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
  1992. {
  1993. if (size == 32)
  1994. return this->g_o_t();
  1995. else
  1996. return (this->output_section()->address()
  1997. + object->toc_base_offset()
  1998. - this->address());
  1999. }
  2000. // Ensure our GOT has a header.
  2001. void
  2002. set_final_data_size()
  2003. {
  2004. if (this->header_ent_cnt_ != 0)
  2005. this->make_header();
  2006. Output_data_got<size, big_endian>::set_final_data_size();
  2007. }
  2008. // First word of GOT header needs some values that are not
  2009. // handled by Output_data_got so poke them in here.
  2010. // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
  2011. void
  2012. do_write(Output_file* of)
  2013. {
  2014. Valtype val = 0;
  2015. if (size == 32 && this->layout_->dynamic_data() != NULL)
  2016. val = this->layout_->dynamic_section()->address();
  2017. if (size == 64)
  2018. val = this->output_section()->address() + 0x8000;
  2019. this->replace_constant(this->header_index_, val);
  2020. Output_data_got<size, big_endian>::do_write(of);
  2021. }
  2022. private:
  2023. void
  2024. reserve_ent(unsigned int cnt = 1)
  2025. {
  2026. if (this->header_ent_cnt_ == 0)
  2027. return;
  2028. if (this->num_entries() + cnt > this->header_index_)
  2029. this->make_header();
  2030. }
  2031. void
  2032. make_header()
  2033. {
  2034. this->header_ent_cnt_ = 0;
  2035. this->header_index_ = this->num_entries();
  2036. if (size == 32)
  2037. {
  2038. Output_data_got<size, big_endian>::add_constant(0);
  2039. Output_data_got<size, big_endian>::add_constant(0);
  2040. Output_data_got<size, big_endian>::add_constant(0);
  2041. // Define _GLOBAL_OFFSET_TABLE_ at the header
  2042. Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
  2043. if (gotsym != NULL)
  2044. {
  2045. Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
  2046. sym->set_value(this->g_o_t());
  2047. }
  2048. else
  2049. this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  2050. Symbol_table::PREDEFINED,
  2051. this, this->g_o_t(), 0,
  2052. elfcpp::STT_OBJECT,
  2053. elfcpp::STB_LOCAL,
  2054. elfcpp::STV_HIDDEN, 0,
  2055. false, false);
  2056. }
  2057. else
  2058. Output_data_got<size, big_endian>::add_constant(0);
  2059. }
  2060. // Stashed pointers.
  2061. Symbol_table* symtab_;
  2062. Layout* layout_;
  2063. // GOT header size.
  2064. unsigned int header_ent_cnt_;
  2065. // GOT header index.
  2066. unsigned int header_index_;
  2067. };
  2068. // Get the GOT section, creating it if necessary.
  2069. template<int size, bool big_endian>
  2070. Output_data_got_powerpc<size, big_endian>*
  2071. Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
  2072. Layout* layout)
  2073. {
  2074. if (this->got_ == NULL)
  2075. {
  2076. gold_assert(symtab != NULL && layout != NULL);
  2077. this->got_
  2078. = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
  2079. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  2080. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  2081. this->got_, ORDER_DATA, false);
  2082. }
  2083. return this->got_;
  2084. }
  2085. // Get the dynamic reloc section, creating it if necessary.
  2086. template<int size, bool big_endian>
  2087. typename Target_powerpc<size, big_endian>::Reloc_section*
  2088. Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
  2089. {
  2090. if (this->rela_dyn_ == NULL)
  2091. {
  2092. gold_assert(layout != NULL);
  2093. this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
  2094. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  2095. elfcpp::SHF_ALLOC, this->rela_dyn_,
  2096. ORDER_DYNAMIC_RELOCS, false);
  2097. }
  2098. return this->rela_dyn_;
  2099. }
  2100. // Similarly, but for ifunc symbols get the one for ifunc.
  2101. template<int size, bool big_endian>
  2102. typename Target_powerpc<size, big_endian>::Reloc_section*
  2103. Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
  2104. Layout* layout,
  2105. bool for_ifunc)
  2106. {
  2107. if (!for_ifunc)
  2108. return this->rela_dyn_section(layout);
  2109. if (this->iplt_ == NULL)
  2110. this->make_iplt_section(symtab, layout);
  2111. return this->iplt_->rel_plt();
  2112. }
  2113. class Stub_control
  2114. {
  2115. public:
  2116. // Determine the stub group size. The group size is the absolute
  2117. // value of the parameter --stub-group-size. If --stub-group-size
  2118. // is passed a negative value, we restrict stubs to be always before
  2119. // the stubbed branches.
  2120. Stub_control(int32_t size, bool no_size_errors)
  2121. : state_(NO_GROUP), stub_group_size_(abs(size)),
  2122. stub14_group_size_(abs(size) >> 10),
  2123. stubs_always_before_branch_(size < 0),
  2124. suppress_size_errors_(no_size_errors),
  2125. group_end_addr_(0), owner_(NULL), output_section_(NULL)
  2126. {
  2127. }
  2128. // Return true iff input section can be handled by current stub
  2129. // group.
  2130. bool
  2131. can_add_to_stub_group(Output_section* o,
  2132. const Output_section::Input_section* i,
  2133. bool has14);
  2134. const Output_section::Input_section*
  2135. owner()
  2136. { return owner_; }
  2137. Output_section*
  2138. output_section()
  2139. { return output_section_; }
  2140. void
  2141. set_output_and_owner(Output_section* o,
  2142. const Output_section::Input_section* i)
  2143. {
  2144. this->output_section_ = o;
  2145. this->owner_ = i;
  2146. }
  2147. private:
  2148. typedef enum
  2149. {
  2150. NO_GROUP,
  2151. FINDING_STUB_SECTION,
  2152. HAS_STUB_SECTION
  2153. } State;
  2154. State state_;
  2155. uint32_t stub_group_size_;
  2156. uint32_t stub14_group_size_;
  2157. bool stubs_always_before_branch_;
  2158. bool suppress_size_errors_;
  2159. uint64_t group_end_addr_;
  2160. const Output_section::Input_section* owner_;
  2161. Output_section* output_section_;
  2162. };
  2163. // Return true iff input section can be handled by current stub
  2164. // group.
  2165. bool
  2166. Stub_control::can_add_to_stub_group(Output_section* o,
  2167. const Output_section::Input_section* i,
  2168. bool has14)
  2169. {
  2170. uint32_t group_size
  2171. = has14 ? this->stub14_group_size_ : this->stub_group_size_;
  2172. bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
  2173. uint64_t this_size;
  2174. uint64_t start_addr = o->address();
  2175. if (whole_sec)
  2176. // .init and .fini sections are pasted together to form a single
  2177. // function. We can't be adding stubs in the middle of the function.
  2178. this_size = o->data_size();
  2179. else
  2180. {
  2181. start_addr += i->relobj()->output_section_offset(i->shndx());
  2182. this_size = i->data_size();
  2183. }
  2184. uint64_t end_addr = start_addr + this_size;
  2185. bool toobig = this_size > group_size;
  2186. if (toobig && !this->suppress_size_errors_)
  2187. gold_warning(_("%s:%s exceeds group size"),
  2188. i->relobj()->name().c_str(),
  2189. i->relobj()->section_name(i->shndx()).c_str());
  2190. if (this->state_ != HAS_STUB_SECTION
  2191. && (!whole_sec || this->output_section_ != o)
  2192. && (this->state_ == NO_GROUP
  2193. || this->group_end_addr_ - end_addr < group_size))
  2194. {
  2195. this->owner_ = i;
  2196. this->output_section_ = o;
  2197. }
  2198. if (this->state_ == NO_GROUP)
  2199. {
  2200. this->state_ = FINDING_STUB_SECTION;
  2201. this->group_end_addr_ = end_addr;
  2202. }
  2203. else if (this->group_end_addr_ - start_addr < group_size)
  2204. ;
  2205. // Adding this section would make the group larger than GROUP_SIZE.
  2206. else if (this->state_ == FINDING_STUB_SECTION
  2207. && !this->stubs_always_before_branch_
  2208. && !toobig)
  2209. {
  2210. // But wait, there's more! Input sections up to GROUP_SIZE
  2211. // bytes before the stub table can be handled by it too.
  2212. this->state_ = HAS_STUB_SECTION;
  2213. this->group_end_addr_ = end_addr;
  2214. }
  2215. else
  2216. {
  2217. this->state_ = NO_GROUP;
  2218. return false;
  2219. }
  2220. return true;
  2221. }
  2222. // Look over all the input sections, deciding where to place stubs.
  2223. template<int size, bool big_endian>
  2224. void
  2225. Target_powerpc<size, big_endian>::group_sections(Layout* layout,
  2226. const Task*,
  2227. bool no_size_errors)
  2228. {
  2229. Stub_control stub_control(this->stub_group_size_, no_size_errors);
  2230. // Group input sections and insert stub table
  2231. Stub_table_owner* table_owner = NULL;
  2232. std::vector<Stub_table_owner*> tables;
  2233. Layout::Section_list section_list;
  2234. layout->get_executable_sections(&section_list);
  2235. std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
  2236. for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
  2237. o != section_list.rend();
  2238. ++o)
  2239. {
  2240. typedef Output_section::Input_section_list Input_section_list;
  2241. for (Input_section_list::const_reverse_iterator i
  2242. = (*o)->input_sections().rbegin();
  2243. i != (*o)->input_sections().rend();
  2244. ++i)
  2245. {
  2246. if (i->is_input_section()
  2247. || i->is_relaxed_input_section())
  2248. {
  2249. Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  2250. <Powerpc_relobj<size, big_endian>*>(i->relobj());
  2251. bool has14 = ppcobj->has_14bit_branch(i->shndx());
  2252. if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
  2253. {
  2254. table_owner->output_section = stub_control.output_section();
  2255. table_owner->owner = stub_control.owner();
  2256. stub_control.set_output_and_owner(*o, &*i);
  2257. table_owner = NULL;
  2258. }
  2259. if (table_owner == NULL)
  2260. {
  2261. table_owner = new Stub_table_owner;
  2262. tables.push_back(table_owner);
  2263. }
  2264. ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
  2265. }
  2266. }
  2267. }
  2268. if (table_owner != NULL)
  2269. {
  2270. const Output_section::Input_section* i = stub_control.owner();
  2271. if (tables.size() >= 2 && tables[tables.size() - 2]->owner == i)
  2272. {
  2273. // Corner case. A new stub group was made for the first
  2274. // section (last one looked at here) for some reason, but
  2275. // the first section is already being used as the owner for
  2276. // a stub table for following sections. Force it into that
  2277. // stub group.
  2278. tables.pop_back();
  2279. delete table_owner;
  2280. Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  2281. <Powerpc_relobj<size, big_endian>*>(i->relobj());
  2282. ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
  2283. }
  2284. else
  2285. {
  2286. table_owner->output_section = stub_control.output_section();
  2287. table_owner->owner = i;
  2288. }
  2289. }
  2290. for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
  2291. t != tables.end();
  2292. ++t)
  2293. {
  2294. Stub_table<size, big_endian>* stub_table;
  2295. if ((*t)->owner->is_input_section())
  2296. stub_table = new Stub_table<size, big_endian>(this,
  2297. (*t)->output_section,
  2298. (*t)->owner);
  2299. else if ((*t)->owner->is_relaxed_input_section())
  2300. stub_table = static_cast<Stub_table<size, big_endian>*>(
  2301. (*t)->owner->relaxed_input_section());
  2302. else
  2303. gold_unreachable();
  2304. this->stub_tables_.push_back(stub_table);
  2305. delete *t;
  2306. }
  2307. }
  2308. static unsigned long
  2309. max_branch_delta (unsigned int r_type)
  2310. {
  2311. if (r_type == elfcpp::R_POWERPC_REL14
  2312. || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
  2313. || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
  2314. return 1L << 15;
  2315. if (r_type == elfcpp::R_POWERPC_REL24
  2316. || r_type == elfcpp::R_PPC_PLTREL24
  2317. || r_type == elfcpp::R_PPC_LOCAL24PC)
  2318. return 1L << 25;
  2319. return 0;
  2320. }
  2321. // If this branch needs a plt call stub, or a long branch stub, make one.
  2322. template<int size, bool big_endian>
  2323. bool
  2324. Target_powerpc<size, big_endian>::Branch_info::make_stub(
  2325. Stub_table<size, big_endian>* stub_table,
  2326. Stub_table<size, big_endian>* ifunc_stub_table,
  2327. Symbol_table* symtab) const
  2328. {
  2329. Symbol* sym = this->object_->global_symbol(this->r_sym_);
  2330. if (sym != NULL && sym->is_forwarder())
  2331. sym = symtab->resolve_forwards(sym);
  2332. const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
  2333. Target_powerpc<size, big_endian>* target =
  2334. static_cast<Target_powerpc<size, big_endian>*>(
  2335. parameters->sized_target<size, big_endian>());
  2336. if (gsym != NULL
  2337. ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
  2338. : this->object_->local_has_plt_offset(this->r_sym_))
  2339. {
  2340. if (size == 64
  2341. && gsym != NULL
  2342. && target->abiversion() >= 2
  2343. && !parameters->options().output_is_position_independent()
  2344. && !is_branch_reloc(this->r_type_))
  2345. target->glink_section()->add_global_entry(gsym);
  2346. else
  2347. {
  2348. if (stub_table == NULL)
  2349. stub_table = this->object_->stub_table(this->shndx_);
  2350. if (stub_table == NULL)
  2351. {
  2352. // This is a ref from a data section to an ifunc symbol.
  2353. stub_table = ifunc_stub_table;
  2354. }
  2355. gold_assert(stub_table != NULL);
  2356. Address from = this->object_->get_output_section_offset(this->shndx_);
  2357. if (from != invalid_address)
  2358. from += (this->object_->output_section(this->shndx_)->address()
  2359. + this->offset_);
  2360. if (gsym != NULL)
  2361. return stub_table->add_plt_call_entry(from,
  2362. this->object_, gsym,
  2363. this->r_type_, this->addend_);
  2364. else
  2365. return stub_table->add_plt_call_entry(from,
  2366. this->object_, this->r_sym_,
  2367. this->r_type_, this->addend_);
  2368. }
  2369. }
  2370. else
  2371. {
  2372. Address max_branch_offset = max_branch_delta(this->r_type_);
  2373. if (max_branch_offset == 0)
  2374. return true;
  2375. Address from = this->object_->get_output_section_offset(this->shndx_);
  2376. gold_assert(from != invalid_address);
  2377. from += (this->object_->output_section(this->shndx_)->address()
  2378. + this->offset_);
  2379. Address to;
  2380. if (gsym != NULL)
  2381. {
  2382. switch (gsym->source())
  2383. {
  2384. case Symbol::FROM_OBJECT:
  2385. {
  2386. Object* symobj = gsym->object();
  2387. if (symobj->is_dynamic()
  2388. || symobj->pluginobj() != NULL)
  2389. return true;
  2390. bool is_ordinary;
  2391. unsigned int shndx = gsym->shndx(&is_ordinary);
  2392. if (shndx == elfcpp::SHN_UNDEF)
  2393. return true;
  2394. }
  2395. break;
  2396. case Symbol::IS_UNDEFINED:
  2397. return true;
  2398. default:
  2399. break;
  2400. }
  2401. Symbol_table::Compute_final_value_status status;
  2402. to = symtab->compute_final_value<size>(gsym, &status);
  2403. if (status != Symbol_table::CFVS_OK)
  2404. return true;
  2405. if (size == 64)
  2406. to += this->object_->ppc64_local_entry_offset(gsym);
  2407. }
  2408. else
  2409. {
  2410. const Symbol_value<size>* psymval
  2411. = this->object_->local_symbol(this->r_sym_);
  2412. Symbol_value<size> symval;
  2413. typedef Sized_relobj_file<size, big_endian> ObjType;
  2414. typename ObjType::Compute_final_local_value_status status
  2415. = this->object_->compute_final_local_value(this->r_sym_, psymval,
  2416. &symval, symtab);
  2417. if (status != ObjType::CFLV_OK
  2418. || !symval.has_output_value())
  2419. return true;
  2420. to = symval.value(this->object_, 0);
  2421. if (size == 64)
  2422. to += this->object_->ppc64_local_entry_offset(this->r_sym_);
  2423. }
  2424. if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
  2425. to += this->addend_;
  2426. if (stub_table == NULL)
  2427. stub_table = this->object_->stub_table(this->shndx_);
  2428. if (size == 64 && target->abiversion() < 2)
  2429. {
  2430. unsigned int dest_shndx;
  2431. if (!target->symval_for_branch(symtab, gsym, this->object_,
  2432. &to, &dest_shndx))
  2433. return true;
  2434. }
  2435. Address delta = to - from;
  2436. if (delta + max_branch_offset >= 2 * max_branch_offset)
  2437. {
  2438. if (stub_table == NULL)
  2439. {
  2440. gold_warning(_("%s:%s: branch in non-executable section,"
  2441. " no long branch stub for you"),
  2442. this->object_->name().c_str(),
  2443. this->object_->section_name(this->shndx_).c_str());
  2444. return true;
  2445. }
  2446. bool save_res = (size == 64
  2447. && gsym != NULL
  2448. && gsym->source() == Symbol::IN_OUTPUT_DATA
  2449. && gsym->output_data() == target->savres_section());
  2450. return stub_table->add_long_branch_entry(this->object_,
  2451. this->r_type_,
  2452. from, to, save_res);
  2453. }
  2454. }
  2455. return true;
  2456. }
  2457. // Relaxation hook. This is where we do stub generation.
  2458. template<int size, bool big_endian>
  2459. bool
  2460. Target_powerpc<size, big_endian>::do_relax(int pass,
  2461. const Input_objects*,
  2462. Symbol_table* symtab,
  2463. Layout* layout,
  2464. const Task* task)
  2465. {
  2466. unsigned int prev_brlt_size = 0;
  2467. if (pass == 1)
  2468. {
  2469. bool thread_safe
  2470. = this->abiversion() < 2 && parameters->options().plt_thread_safe();
  2471. if (size == 64
  2472. && this->abiversion() < 2
  2473. && !thread_safe
  2474. && !parameters->options().user_set_plt_thread_safe())
  2475. {
  2476. static const char* const thread_starter[] =
  2477. {
  2478. "pthread_create",
  2479. /* libstdc++ */
  2480. "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
  2481. /* librt */
  2482. "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
  2483. "mq_notify", "create_timer",
  2484. /* libanl */
  2485. "getaddrinfo_a",
  2486. /* libgomp */
  2487. "GOMP_parallel",
  2488. "GOMP_parallel_start",
  2489. "GOMP_parallel_loop_static",
  2490. "GOMP_parallel_loop_static_start",
  2491. "GOMP_parallel_loop_dynamic",
  2492. "GOMP_parallel_loop_dynamic_start",
  2493. "GOMP_parallel_loop_guided",
  2494. "GOMP_parallel_loop_guided_start",
  2495. "GOMP_parallel_loop_runtime",
  2496. "GOMP_parallel_loop_runtime_start",
  2497. "GOMP_parallel_sections",
  2498. "GOMP_parallel_sections_start",
  2499. /* libgo */
  2500. "__go_go",
  2501. };
  2502. if (parameters->options().shared())
  2503. thread_safe = true;
  2504. else
  2505. {
  2506. for (unsigned int i = 0;
  2507. i < sizeof(thread_starter) / sizeof(thread_starter[0]);
  2508. i++)
  2509. {
  2510. Symbol* sym = symtab->lookup(thread_starter[i], NULL);
  2511. thread_safe = (sym != NULL
  2512. && sym->in_reg()
  2513. && sym->in_real_elf());
  2514. if (thread_safe)
  2515. break;
  2516. }
  2517. }
  2518. }
  2519. this->plt_thread_safe_ = thread_safe;
  2520. }
  2521. if (pass == 1)
  2522. {
  2523. this->stub_group_size_ = parameters->options().stub_group_size();
  2524. bool no_size_errors = true;
  2525. if (this->stub_group_size_ == 1)
  2526. this->stub_group_size_ = 0x1c00000;
  2527. else if (this->stub_group_size_ == -1)
  2528. this->stub_group_size_ = -0x1e00000;
  2529. else
  2530. no_size_errors = false;
  2531. this->group_sections(layout, task, no_size_errors);
  2532. }
  2533. else if (this->relax_failed_ && this->relax_fail_count_ < 3)
  2534. {
  2535. this->branch_lookup_table_.clear();
  2536. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  2537. p != this->stub_tables_.end();
  2538. ++p)
  2539. {
  2540. (*p)->clear_stubs(true);
  2541. }
  2542. this->stub_tables_.clear();
  2543. this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
  2544. gold_info(_("%s: stub group size is too large; retrying with %d"),
  2545. program_name, this->stub_group_size_);
  2546. this->group_sections(layout, task, true);
  2547. }
  2548. // We need address of stub tables valid for make_stub.
  2549. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  2550. p != this->stub_tables_.end();
  2551. ++p)
  2552. {
  2553. const Powerpc_relobj<size, big_endian>* object
  2554. = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
  2555. Address off = object->get_output_section_offset((*p)->shndx());
  2556. gold_assert(off != invalid_address);
  2557. Output_section* os = (*p)->output_section();
  2558. (*p)->set_address_and_size(os, off);
  2559. }
  2560. if (pass != 1)
  2561. {
  2562. // Clear plt call stubs, long branch stubs and branch lookup table.
  2563. prev_brlt_size = this->branch_lookup_table_.size();
  2564. this->branch_lookup_table_.clear();
  2565. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  2566. p != this->stub_tables_.end();
  2567. ++p)
  2568. {
  2569. (*p)->clear_stubs(false);
  2570. }
  2571. }
  2572. // Build all the stubs.
  2573. this->relax_failed_ = false;
  2574. Stub_table<size, big_endian>* ifunc_stub_table
  2575. = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
  2576. Stub_table<size, big_endian>* one_stub_table
  2577. = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
  2578. for (typename Branches::const_iterator b = this->branch_info_.begin();
  2579. b != this->branch_info_.end();
  2580. b++)
  2581. {
  2582. if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
  2583. && !this->relax_failed_)
  2584. {
  2585. this->relax_failed_ = true;
  2586. this->relax_fail_count_++;
  2587. if (this->relax_fail_count_ < 3)
  2588. return true;
  2589. }
  2590. }
  2591. // Did anything change size?
  2592. unsigned int num_huge_branches = this->branch_lookup_table_.size();
  2593. bool again = num_huge_branches != prev_brlt_size;
  2594. if (size == 64 && num_huge_branches != 0)
  2595. this->make_brlt_section(layout);
  2596. if (size == 64 && again)
  2597. this->brlt_section_->set_current_size(num_huge_branches);
  2598. typedef Unordered_set<Output_section*> Output_sections;
  2599. Output_sections os_need_update;
  2600. for (typename Stub_tables::iterator p = this->stub_tables_.begin();
  2601. p != this->stub_tables_.end();
  2602. ++p)
  2603. {
  2604. if ((*p)->size_update())
  2605. {
  2606. again = true;
  2607. (*p)->add_eh_frame(layout);
  2608. os_need_update.insert((*p)->output_section());
  2609. }
  2610. }
  2611. // Set output section offsets for all input sections in an output
  2612. // section that just changed size. Anything past the stubs will
  2613. // need updating.
  2614. for (typename Output_sections::iterator p = os_need_update.begin();
  2615. p != os_need_update.end();
  2616. p++)
  2617. {
  2618. Output_section* os = *p;
  2619. Address off = 0;
  2620. typedef Output_section::Input_section_list Input_section_list;
  2621. for (Input_section_list::const_iterator i = os->input_sections().begin();
  2622. i != os->input_sections().end();
  2623. ++i)
  2624. {
  2625. off = align_address(off, i->addralign());
  2626. if (i->is_input_section() || i->is_relaxed_input_section())
  2627. i->relobj()->set_section_offset(i->shndx(), off);
  2628. if (i->is_relaxed_input_section())
  2629. {
  2630. Stub_table<size, big_endian>* stub_table
  2631. = static_cast<Stub_table<size, big_endian>*>(
  2632. i->relaxed_input_section());
  2633. off += stub_table->set_address_and_size(os, off);
  2634. }
  2635. else
  2636. off += i->data_size();
  2637. }
  2638. // If .branch_lt is part of this output section, then we have
  2639. // just done the offset adjustment.
  2640. os->clear_section_offsets_need_adjustment();
  2641. }
  2642. if (size == 64
  2643. && !again
  2644. && num_huge_branches != 0
  2645. && parameters->options().output_is_position_independent())
  2646. {
  2647. // Fill in the BRLT relocs.
  2648. this->brlt_section_->reset_brlt_sizes();
  2649. for (typename Branch_lookup_table::const_iterator p
  2650. = this->branch_lookup_table_.begin();
  2651. p != this->branch_lookup_table_.end();
  2652. ++p)
  2653. {
  2654. this->brlt_section_->add_reloc(p->first, p->second);
  2655. }
  2656. this->brlt_section_->finalize_brlt_sizes();
  2657. }
  2658. return again;
  2659. }
  2660. template<int size, bool big_endian>
  2661. void
  2662. Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
  2663. unsigned char* oview,
  2664. uint64_t* paddress,
  2665. off_t* plen) const
  2666. {
  2667. uint64_t address = plt->address();
  2668. off_t len = plt->data_size();
  2669. if (plt == this->glink_)
  2670. {
  2671. // See Output_data_glink::do_write() for glink contents.
  2672. if (len == 0)
  2673. {
  2674. gold_assert(parameters->doing_static_link());
  2675. // Static linking may need stubs, to support ifunc and long
  2676. // branches. We need to create an output section for
  2677. // .eh_frame early in the link process, to have a place to
  2678. // attach stub .eh_frame info. We also need to have
  2679. // registered a CIE that matches the stub CIE. Both of
  2680. // these requirements are satisfied by creating an FDE and
  2681. // CIE for .glink, even though static linking will leave
  2682. // .glink zero length.
  2683. // ??? Hopefully generating an FDE with a zero address range
  2684. // won't confuse anything that consumes .eh_frame info.
  2685. }
  2686. else if (size == 64)
  2687. {
  2688. // There is one word before __glink_PLTresolve
  2689. address += 8;
  2690. len -= 8;
  2691. }
  2692. else if (parameters->options().output_is_position_independent())
  2693. {
  2694. // There are two FDEs for a position independent glink.
  2695. // The first covers the branch table, the second
  2696. // __glink_PLTresolve at the end of glink.
  2697. off_t resolve_size = this->glink_->pltresolve_size;
  2698. if (oview[9] == elfcpp::DW_CFA_nop)
  2699. len -= resolve_size;
  2700. else
  2701. {
  2702. address += len - resolve_size;
  2703. len = resolve_size;
  2704. }
  2705. }
  2706. }
  2707. else
  2708. {
  2709. // Must be a stub table.
  2710. const Stub_table<size, big_endian>* stub_table
  2711. = static_cast<const Stub_table<size, big_endian>*>(plt);
  2712. uint64_t stub_address = stub_table->stub_address();
  2713. len -= stub_address - address;
  2714. address = stub_address;
  2715. }
  2716. *paddress = address;
  2717. *plen = len;
  2718. }
  2719. // A class to handle the PLT data.
  2720. template<int size, bool big_endian>
  2721. class Output_data_plt_powerpc : public Output_section_data_build
  2722. {
  2723. public:
  2724. typedef Output_data_reloc<elfcpp::SHT_RELA, true,
  2725. size, big_endian> Reloc_section;
  2726. Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
  2727. Reloc_section* plt_rel,
  2728. const char* name)
  2729. : Output_section_data_build(size == 32 ? 4 : 8),
  2730. rel_(plt_rel),
  2731. targ_(targ),
  2732. name_(name)
  2733. { }
  2734. // Add an entry to the PLT.
  2735. void
  2736. add_entry(Symbol*);
  2737. void
  2738. add_ifunc_entry(Symbol*);
  2739. void
  2740. add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
  2741. // Return the .rela.plt section data.
  2742. Reloc_section*
  2743. rel_plt() const
  2744. {
  2745. return this->rel_;
  2746. }
  2747. // Return the number of PLT entries.
  2748. unsigned int
  2749. entry_count() const
  2750. {
  2751. if (this->current_data_size() == 0)
  2752. return 0;
  2753. return ((this->current_data_size() - this->first_plt_entry_offset())
  2754. / this->plt_entry_size());
  2755. }
  2756. protected:
  2757. void
  2758. do_adjust_output_section(Output_section* os)
  2759. {
  2760. os->set_entsize(0);
  2761. }
  2762. // Write to a map file.
  2763. void
  2764. do_print_to_mapfile(Mapfile* mapfile) const
  2765. { mapfile->print_output_data(this, this->name_); }
  2766. private:
  2767. // Return the offset of the first non-reserved PLT entry.
  2768. unsigned int
  2769. first_plt_entry_offset() const
  2770. {
  2771. // IPLT has no reserved entry.
  2772. if (this->name_[3] == 'I')
  2773. return 0;
  2774. return this->targ_->first_plt_entry_offset();
  2775. }
  2776. // Return the size of each PLT entry.
  2777. unsigned int
  2778. plt_entry_size() const
  2779. {
  2780. return this->targ_->plt_entry_size();
  2781. }
  2782. // Write out the PLT data.
  2783. void
  2784. do_write(Output_file*);
  2785. // The reloc section.
  2786. Reloc_section* rel_;
  2787. // Allows access to .glink for do_write.
  2788. Target_powerpc<size, big_endian>* targ_;
  2789. // What to report in map file.
  2790. const char *name_;
  2791. };
  2792. // Add an entry to the PLT.
  2793. template<int size, bool big_endian>
  2794. void
  2795. Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
  2796. {
  2797. if (!gsym->has_plt_offset())
  2798. {
  2799. section_size_type off = this->current_data_size();
  2800. if (off == 0)
  2801. off += this->first_plt_entry_offset();
  2802. gsym->set_plt_offset(off);
  2803. gsym->set_needs_dynsym_entry();
  2804. unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
  2805. this->rel_->add_global(gsym, dynrel, this, off, 0);
  2806. off += this->plt_entry_size();
  2807. this->set_current_data_size(off);
  2808. }
  2809. }
  2810. // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
  2811. template<int size, bool big_endian>
  2812. void
  2813. Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
  2814. {
  2815. if (!gsym->has_plt_offset())
  2816. {
  2817. section_size_type off = this->current_data_size();
  2818. gsym->set_plt_offset(off);
  2819. unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
  2820. if (size == 64 && this->targ_->abiversion() < 2)
  2821. dynrel = elfcpp::R_PPC64_JMP_IREL;
  2822. this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
  2823. off += this->plt_entry_size();
  2824. this->set_current_data_size(off);
  2825. }
  2826. }
  2827. // Add an entry for a local ifunc symbol to the IPLT.
  2828. template<int size, bool big_endian>
  2829. void
  2830. Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
  2831. Sized_relobj_file<size, big_endian>* relobj,
  2832. unsigned int local_sym_index)
  2833. {
  2834. if (!relobj->local_has_plt_offset(local_sym_index))
  2835. {
  2836. section_size_type off = this->current_data_size();
  2837. relobj->set_local_plt_offset(local_sym_index, off);
  2838. unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
  2839. if (size == 64 && this->targ_->abiversion() < 2)
  2840. dynrel = elfcpp::R_PPC64_JMP_IREL;
  2841. this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
  2842. this, off, 0);
  2843. off += this->plt_entry_size();
  2844. this->set_current_data_size(off);
  2845. }
  2846. }
  2847. static const uint32_t add_0_11_11 = 0x7c0b5a14;
  2848. static const uint32_t add_2_2_11 = 0x7c425a14;
  2849. static const uint32_t add_3_3_2 = 0x7c631214;
  2850. static const uint32_t add_3_3_13 = 0x7c636a14;
  2851. static const uint32_t add_11_0_11 = 0x7d605a14;
  2852. static const uint32_t add_11_2_11 = 0x7d625a14;
  2853. static const uint32_t add_11_11_2 = 0x7d6b1214;
  2854. static const uint32_t addi_0_12 = 0x380c0000;
  2855. static const uint32_t addi_2_2 = 0x38420000;
  2856. static const uint32_t addi_3_3 = 0x38630000;
  2857. static const uint32_t addi_11_11 = 0x396b0000;
  2858. static const uint32_t addi_12_1 = 0x39810000;
  2859. static const uint32_t addi_12_12 = 0x398c0000;
  2860. static const uint32_t addis_0_2 = 0x3c020000;
  2861. static const uint32_t addis_0_13 = 0x3c0d0000;
  2862. static const uint32_t addis_2_12 = 0x3c4c0000;
  2863. static const uint32_t addis_11_2 = 0x3d620000;
  2864. static const uint32_t addis_11_11 = 0x3d6b0000;
  2865. static const uint32_t addis_11_30 = 0x3d7e0000;
  2866. static const uint32_t addis_12_1 = 0x3d810000;
  2867. static const uint32_t addis_12_2 = 0x3d820000;
  2868. static const uint32_t addis_12_12 = 0x3d8c0000;
  2869. static const uint32_t b = 0x48000000;
  2870. static const uint32_t bcl_20_31 = 0x429f0005;
  2871. static const uint32_t bctr = 0x4e800420;
  2872. static const uint32_t blr = 0x4e800020;
  2873. static const uint32_t bnectr_p4 = 0x4ce20420;
  2874. static const uint32_t cmpld_7_12_0 = 0x7fac0040;
  2875. static const uint32_t cmpldi_2_0 = 0x28220000;
  2876. static const uint32_t cror_15_15_15 = 0x4def7b82;
  2877. static const uint32_t cror_31_31_31 = 0x4ffffb82;
  2878. static const uint32_t ld_0_1 = 0xe8010000;
  2879. static const uint32_t ld_0_12 = 0xe80c0000;
  2880. static const uint32_t ld_2_1 = 0xe8410000;
  2881. static const uint32_t ld_2_2 = 0xe8420000;
  2882. static const uint32_t ld_2_11 = 0xe84b0000;
  2883. static const uint32_t ld_11_2 = 0xe9620000;
  2884. static const uint32_t ld_11_11 = 0xe96b0000;
  2885. static const uint32_t ld_12_2 = 0xe9820000;
  2886. static const uint32_t ld_12_11 = 0xe98b0000;
  2887. static const uint32_t ld_12_12 = 0xe98c0000;
  2888. static const uint32_t lfd_0_1 = 0xc8010000;
  2889. static const uint32_t li_0_0 = 0x38000000;
  2890. static const uint32_t li_12_0 = 0x39800000;
  2891. static const uint32_t lis_0 = 0x3c000000;
  2892. static const uint32_t lis_11 = 0x3d600000;
  2893. static const uint32_t lis_12 = 0x3d800000;
  2894. static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
  2895. static const uint32_t lwz_0_12 = 0x800c0000;
  2896. static const uint32_t lwz_11_11 = 0x816b0000;
  2897. static const uint32_t lwz_11_30 = 0x817e0000;
  2898. static const uint32_t lwz_12_12 = 0x818c0000;
  2899. static const uint32_t lwzu_0_12 = 0x840c0000;
  2900. static const uint32_t mflr_0 = 0x7c0802a6;
  2901. static const uint32_t mflr_11 = 0x7d6802a6;
  2902. static const uint32_t mflr_12 = 0x7d8802a6;
  2903. static const uint32_t mtctr_0 = 0x7c0903a6;
  2904. static const uint32_t mtctr_11 = 0x7d6903a6;
  2905. static const uint32_t mtctr_12 = 0x7d8903a6;
  2906. static const uint32_t mtlr_0 = 0x7c0803a6;
  2907. static const uint32_t mtlr_12 = 0x7d8803a6;
  2908. static const uint32_t nop = 0x60000000;
  2909. static const uint32_t ori_0_0_0 = 0x60000000;
  2910. static const uint32_t srdi_0_0_2 = 0x7800f082;
  2911. static const uint32_t std_0_1 = 0xf8010000;
  2912. static const uint32_t std_0_12 = 0xf80c0000;
  2913. static const uint32_t std_2_1 = 0xf8410000;
  2914. static const uint32_t stfd_0_1 = 0xd8010000;
  2915. static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
  2916. static const uint32_t sub_11_11_12 = 0x7d6c5850;
  2917. static const uint32_t sub_12_12_11 = 0x7d8b6050;
  2918. static const uint32_t xor_2_12_12 = 0x7d826278;
  2919. static const uint32_t xor_11_12_12 = 0x7d8b6278;
  2920. // Write out the PLT.
  2921. template<int size, bool big_endian>
  2922. void
  2923. Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
  2924. {
  2925. if (size == 32 && this->name_[3] != 'I')
  2926. {
  2927. const section_size_type offset = this->offset();
  2928. const section_size_type oview_size
  2929. = convert_to_section_size_type(this->data_size());
  2930. unsigned char* const oview = of->get_output_view(offset, oview_size);
  2931. unsigned char* pov = oview;
  2932. unsigned char* endpov = oview + oview_size;
  2933. // The address of the .glink branch table
  2934. const Output_data_glink<size, big_endian>* glink
  2935. = this->targ_->glink_section();
  2936. elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
  2937. while (pov < endpov)
  2938. {
  2939. elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
  2940. pov += 4;
  2941. branch_tab += 4;
  2942. }
  2943. of->write_output_view(offset, oview_size, oview);
  2944. }
  2945. }
  2946. // Create the PLT section.
  2947. template<int size, bool big_endian>
  2948. void
  2949. Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
  2950. Layout* layout)
  2951. {
  2952. if (this->plt_ == NULL)
  2953. {
  2954. if (this->got_ == NULL)
  2955. this->got_section(symtab, layout);
  2956. if (this->glink_ == NULL)
  2957. make_glink_section(layout);
  2958. // Ensure that .rela.dyn always appears before .rela.plt This is
  2959. // necessary due to how, on PowerPC and some other targets, .rela.dyn
  2960. // needs to include .rela.plt in its range.
  2961. this->rela_dyn_section(layout);
  2962. Reloc_section* plt_rel = new Reloc_section(false);
  2963. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  2964. elfcpp::SHF_ALLOC, plt_rel,
  2965. ORDER_DYNAMIC_PLT_RELOCS, false);
  2966. this->plt_
  2967. = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
  2968. "** PLT");
  2969. layout->add_output_section_data(".plt",
  2970. (size == 32
  2971. ? elfcpp::SHT_PROGBITS
  2972. : elfcpp::SHT_NOBITS),
  2973. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  2974. this->plt_,
  2975. (size == 32
  2976. ? ORDER_SMALL_DATA
  2977. : ORDER_SMALL_BSS),
  2978. false);
  2979. }
  2980. }
  2981. // Create the IPLT section.
  2982. template<int size, bool big_endian>
  2983. void
  2984. Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
  2985. Layout* layout)
  2986. {
  2987. if (this->iplt_ == NULL)
  2988. {
  2989. this->make_plt_section(symtab, layout);
  2990. Reloc_section* iplt_rel = new Reloc_section(false);
  2991. this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
  2992. this->iplt_
  2993. = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
  2994. "** IPLT");
  2995. this->plt_->output_section()->add_output_section_data(this->iplt_);
  2996. }
  2997. }
  2998. // A section for huge long branch addresses, similar to plt section.
  2999. template<int size, bool big_endian>
  3000. class Output_data_brlt_powerpc : public Output_section_data_build
  3001. {
  3002. public:
  3003. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  3004. typedef Output_data_reloc<elfcpp::SHT_RELA, true,
  3005. size, big_endian> Reloc_section;
  3006. Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
  3007. Reloc_section* brlt_rel)
  3008. : Output_section_data_build(size == 32 ? 4 : 8),
  3009. rel_(brlt_rel),
  3010. targ_(targ)
  3011. { }
  3012. void
  3013. reset_brlt_sizes()
  3014. {
  3015. this->reset_data_size();
  3016. this->rel_->reset_data_size();
  3017. }
  3018. void
  3019. finalize_brlt_sizes()
  3020. {
  3021. this->finalize_data_size();
  3022. this->rel_->finalize_data_size();
  3023. }
  3024. // Add a reloc for an entry in the BRLT.
  3025. void
  3026. add_reloc(Address to, unsigned int off)
  3027. { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
  3028. // Update section and reloc section size.
  3029. void
  3030. set_current_size(unsigned int num_branches)
  3031. {
  3032. this->reset_address_and_file_offset();
  3033. this->set_current_data_size(num_branches * 16);
  3034. this->finalize_data_size();
  3035. Output_section* os = this->output_section();
  3036. os->set_section_offsets_need_adjustment();
  3037. if (this->rel_ != NULL)
  3038. {
  3039. unsigned int reloc_size
  3040. = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
  3041. this->rel_->reset_address_and_file_offset();
  3042. this->rel_->set_current_data_size(num_branches * reloc_size);
  3043. this->rel_->finalize_data_size();
  3044. Output_section* os = this->rel_->output_section();
  3045. os->set_section_offsets_need_adjustment();
  3046. }
  3047. }
  3048. protected:
  3049. void
  3050. do_adjust_output_section(Output_section* os)
  3051. {
  3052. os->set_entsize(0);
  3053. }
  3054. // Write to a map file.
  3055. void
  3056. do_print_to_mapfile(Mapfile* mapfile) const
  3057. { mapfile->print_output_data(this, "** BRLT"); }
  3058. private:
  3059. // Write out the BRLT data.
  3060. void
  3061. do_write(Output_file*);
  3062. // The reloc section.
  3063. Reloc_section* rel_;
  3064. Target_powerpc<size, big_endian>* targ_;
  3065. };
  3066. // Make the branch lookup table section.
  3067. template<int size, bool big_endian>
  3068. void
  3069. Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
  3070. {
  3071. if (size == 64 && this->brlt_section_ == NULL)
  3072. {
  3073. Reloc_section* brlt_rel = NULL;
  3074. bool is_pic = parameters->options().output_is_position_independent();
  3075. if (is_pic)
  3076. {
  3077. // When PIC we can't fill in .branch_lt (like .plt it can be
  3078. // a bss style section) but must initialise at runtime via
  3079. // dynamic relocats.
  3080. this->rela_dyn_section(layout);
  3081. brlt_rel = new Reloc_section(false);
  3082. this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
  3083. }
  3084. this->brlt_section_
  3085. = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
  3086. if (this->plt_ && is_pic)
  3087. this->plt_->output_section()
  3088. ->add_output_section_data(this->brlt_section_);
  3089. else
  3090. layout->add_output_section_data(".branch_lt",
  3091. (is_pic ? elfcpp::SHT_NOBITS
  3092. : elfcpp::SHT_PROGBITS),
  3093. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  3094. this->brlt_section_,
  3095. (is_pic ? ORDER_SMALL_BSS
  3096. : ORDER_SMALL_DATA),
  3097. false);
  3098. }
  3099. }
  3100. // Write out .branch_lt when non-PIC.
  3101. template<int size, bool big_endian>
  3102. void
  3103. Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
  3104. {
  3105. if (size == 64 && !parameters->options().output_is_position_independent())
  3106. {
  3107. const section_size_type offset = this->offset();
  3108. const section_size_type oview_size
  3109. = convert_to_section_size_type(this->data_size());
  3110. unsigned char* const oview = of->get_output_view(offset, oview_size);
  3111. this->targ_->write_branch_lookup_table(oview);
  3112. of->write_output_view(offset, oview_size, oview);
  3113. }
  3114. }
  3115. static inline uint32_t
  3116. l(uint32_t a)
  3117. {
  3118. return a & 0xffff;
  3119. }
  3120. static inline uint32_t
  3121. hi(uint32_t a)
  3122. {
  3123. return l(a >> 16);
  3124. }
  3125. static inline uint32_t
  3126. ha(uint32_t a)
  3127. {
  3128. return hi(a + 0x8000);
  3129. }
  3130. template<int size>
  3131. struct Eh_cie
  3132. {
  3133. static const unsigned char eh_frame_cie[12];
  3134. };
  3135. template<int size>
  3136. const unsigned char Eh_cie<size>::eh_frame_cie[] =
  3137. {
  3138. 1, // CIE version.
  3139. 'z', 'R', 0, // Augmentation string.
  3140. 4, // Code alignment.
  3141. 0x80 - size / 8 , // Data alignment.
  3142. 65, // RA reg.
  3143. 1, // Augmentation size.
  3144. (elfcpp::DW_EH_PE_pcrel
  3145. | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
  3146. elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
  3147. };
  3148. // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
  3149. static const unsigned char glink_eh_frame_fde_64v1[] =
  3150. {
  3151. 0, 0, 0, 0, // Replaced with offset to .glink.
  3152. 0, 0, 0, 0, // Replaced with size of .glink.
  3153. 0, // Augmentation size.
  3154. elfcpp::DW_CFA_advance_loc + 1,
  3155. elfcpp::DW_CFA_register, 65, 12,
  3156. elfcpp::DW_CFA_advance_loc + 4,
  3157. elfcpp::DW_CFA_restore_extended, 65
  3158. };
  3159. // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
  3160. static const unsigned char glink_eh_frame_fde_64v2[] =
  3161. {
  3162. 0, 0, 0, 0, // Replaced with offset to .glink.
  3163. 0, 0, 0, 0, // Replaced with size of .glink.
  3164. 0, // Augmentation size.
  3165. elfcpp::DW_CFA_advance_loc + 1,
  3166. elfcpp::DW_CFA_register, 65, 0,
  3167. elfcpp::DW_CFA_advance_loc + 4,
  3168. elfcpp::DW_CFA_restore_extended, 65
  3169. };
  3170. // Describe __glink_PLTresolve use of LR, 32-bit version.
  3171. static const unsigned char glink_eh_frame_fde_32[] =
  3172. {
  3173. 0, 0, 0, 0, // Replaced with offset to .glink.
  3174. 0, 0, 0, 0, // Replaced with size of .glink.
  3175. 0, // Augmentation size.
  3176. elfcpp::DW_CFA_advance_loc + 2,
  3177. elfcpp::DW_CFA_register, 65, 0,
  3178. elfcpp::DW_CFA_advance_loc + 4,
  3179. elfcpp::DW_CFA_restore_extended, 65
  3180. };
  3181. static const unsigned char default_fde[] =
  3182. {
  3183. 0, 0, 0, 0, // Replaced with offset to stubs.
  3184. 0, 0, 0, 0, // Replaced with size of stubs.
  3185. 0, // Augmentation size.
  3186. elfcpp::DW_CFA_nop, // Pad.
  3187. elfcpp::DW_CFA_nop,
  3188. elfcpp::DW_CFA_nop
  3189. };
  3190. template<bool big_endian>
  3191. static inline void
  3192. write_insn(unsigned char* p, uint32_t v)
  3193. {
  3194. elfcpp::Swap<32, big_endian>::writeval(p, v);
  3195. }
  3196. // Stub_table holds information about plt and long branch stubs.
  3197. // Stubs are built in an area following some input section determined
  3198. // by group_sections(). This input section is converted to a relaxed
  3199. // input section allowing it to be resized to accommodate the stubs
  3200. template<int size, bool big_endian>
  3201. class Stub_table : public Output_relaxed_input_section
  3202. {
  3203. public:
  3204. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  3205. static const Address invalid_address = static_cast<Address>(0) - 1;
  3206. Stub_table(Target_powerpc<size, big_endian>* targ,
  3207. Output_section* output_section,
  3208. const Output_section::Input_section* owner)
  3209. : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
  3210. owner->relobj()
  3211. ->section_addralign(owner->shndx())),
  3212. targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
  3213. orig_data_size_(owner->current_data_size()),
  3214. plt_size_(0), last_plt_size_(0),
  3215. branch_size_(0), last_branch_size_(0), eh_frame_added_(false),
  3216. need_save_res_(false)
  3217. {
  3218. this->set_output_section(output_section);
  3219. std::vector<Output_relaxed_input_section*> new_relaxed;
  3220. new_relaxed.push_back(this);
  3221. output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
  3222. }
  3223. // Add a plt call stub.
  3224. bool
  3225. add_plt_call_entry(Address,
  3226. const Sized_relobj_file<size, big_endian>*,
  3227. const Symbol*,
  3228. unsigned int,
  3229. Address);
  3230. bool
  3231. add_plt_call_entry(Address,
  3232. const Sized_relobj_file<size, big_endian>*,
  3233. unsigned int,
  3234. unsigned int,
  3235. Address);
  3236. // Find a given plt call stub.
  3237. Address
  3238. find_plt_call_entry(const Symbol*) const;
  3239. Address
  3240. find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
  3241. unsigned int) const;
  3242. Address
  3243. find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
  3244. const Symbol*,
  3245. unsigned int,
  3246. Address) const;
  3247. Address
  3248. find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
  3249. unsigned int,
  3250. unsigned int,
  3251. Address) const;
  3252. // Add a long branch stub.
  3253. bool
  3254. add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
  3255. unsigned int, Address, Address, bool);
  3256. Address
  3257. find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
  3258. Address) const;
  3259. bool
  3260. can_reach_stub(Address from, unsigned int off, unsigned int r_type)
  3261. {
  3262. Address max_branch_offset = max_branch_delta(r_type);
  3263. if (max_branch_offset == 0)
  3264. return true;
  3265. gold_assert(from != invalid_address);
  3266. Address loc = off + this->stub_address();
  3267. return loc - from + max_branch_offset < 2 * max_branch_offset;
  3268. }
  3269. void
  3270. clear_stubs(bool all)
  3271. {
  3272. this->plt_call_stubs_.clear();
  3273. this->plt_size_ = 0;
  3274. this->long_branch_stubs_.clear();
  3275. this->branch_size_ = 0;
  3276. this->need_save_res_ = false;
  3277. if (all)
  3278. {
  3279. this->last_plt_size_ = 0;
  3280. this->last_branch_size_ = 0;
  3281. }
  3282. }
  3283. Address
  3284. set_address_and_size(const Output_section* os, Address off)
  3285. {
  3286. Address start_off = off;
  3287. off += this->orig_data_size_;
  3288. Address my_size = this->plt_size_ + this->branch_size_;
  3289. if (this->need_save_res_)
  3290. my_size += this->targ_->savres_section()->data_size();
  3291. if (my_size != 0)
  3292. off = align_address(off, this->stub_align());
  3293. // Include original section size and alignment padding in size
  3294. my_size += off - start_off;
  3295. this->reset_address_and_file_offset();
  3296. this->set_current_data_size(my_size);
  3297. this->set_address_and_file_offset(os->address() + start_off,
  3298. os->offset() + start_off);
  3299. return my_size;
  3300. }
  3301. Address
  3302. stub_address() const
  3303. {
  3304. return align_address(this->address() + this->orig_data_size_,
  3305. this->stub_align());
  3306. }
  3307. Address
  3308. stub_offset() const
  3309. {
  3310. return align_address(this->offset() + this->orig_data_size_,
  3311. this->stub_align());
  3312. }
  3313. section_size_type
  3314. plt_size() const
  3315. { return this->plt_size_; }
  3316. bool
  3317. size_update()
  3318. {
  3319. Output_section* os = this->output_section();
  3320. if (os->addralign() < this->stub_align())
  3321. {
  3322. os->set_addralign(this->stub_align());
  3323. // FIXME: get rid of the insane checkpointing.
  3324. // We can't increase alignment of the input section to which
  3325. // stubs are attached; The input section may be .init which
  3326. // is pasted together with other .init sections to form a
  3327. // function. Aligning might insert zero padding resulting in
  3328. // sigill. However we do need to increase alignment of the
  3329. // output section so that the align_address() on offset in
  3330. // set_address_and_size() adds the same padding as the
  3331. // align_address() on address in stub_address().
  3332. // What's more, we need this alignment for the layout done in
  3333. // relaxation_loop_body() so that the output section starts at
  3334. // a suitably aligned address.
  3335. os->checkpoint_set_addralign(this->stub_align());
  3336. }
  3337. if (this->last_plt_size_ != this->plt_size_
  3338. || this->last_branch_size_ != this->branch_size_)
  3339. {
  3340. this->last_plt_size_ = this->plt_size_;
  3341. this->last_branch_size_ = this->branch_size_;
  3342. return true;
  3343. }
  3344. return false;
  3345. }
  3346. // Add .eh_frame info for this stub section. Unlike other linker
  3347. // generated .eh_frame this is added late in the link, because we
  3348. // only want the .eh_frame info if this particular stub section is
  3349. // non-empty.
  3350. void
  3351. add_eh_frame(Layout* layout)
  3352. {
  3353. if (!this->eh_frame_added_)
  3354. {
  3355. if (!parameters->options().ld_generated_unwind_info())
  3356. return;
  3357. // Since we add stub .eh_frame info late, it must be placed
  3358. // after all other linker generated .eh_frame info so that
  3359. // merge mapping need not be updated for input sections.
  3360. // There is no provision to use a different CIE to that used
  3361. // by .glink.
  3362. if (!this->targ_->has_glink())
  3363. return;
  3364. layout->add_eh_frame_for_plt(this,
  3365. Eh_cie<size>::eh_frame_cie,
  3366. sizeof (Eh_cie<size>::eh_frame_cie),
  3367. default_fde,
  3368. sizeof (default_fde));
  3369. this->eh_frame_added_ = true;
  3370. }
  3371. }
  3372. Target_powerpc<size, big_endian>*
  3373. targ() const
  3374. { return targ_; }
  3375. private:
  3376. class Plt_stub_ent;
  3377. class Plt_stub_ent_hash;
  3378. typedef Unordered_map<Plt_stub_ent, unsigned int,
  3379. Plt_stub_ent_hash> Plt_stub_entries;
  3380. // Alignment of stub section.
  3381. unsigned int
  3382. stub_align() const
  3383. {
  3384. if (size == 32)
  3385. return 16;
  3386. unsigned int min_align = 32;
  3387. unsigned int user_align = 1 << parameters->options().plt_align();
  3388. return std::max(user_align, min_align);
  3389. }
  3390. // Return the plt offset for the given call stub.
  3391. Address
  3392. plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
  3393. {
  3394. const Symbol* gsym = p->first.sym_;
  3395. if (gsym != NULL)
  3396. {
  3397. *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
  3398. && gsym->can_use_relative_reloc(false));
  3399. return gsym->plt_offset();
  3400. }
  3401. else
  3402. {
  3403. *is_iplt = true;
  3404. const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
  3405. unsigned int local_sym_index = p->first.locsym_;
  3406. return relobj->local_plt_offset(local_sym_index);
  3407. }
  3408. }
  3409. // Size of a given plt call stub.
  3410. unsigned int
  3411. plt_call_size(typename Plt_stub_entries::const_iterator p) const
  3412. {
  3413. if (size == 32)
  3414. return 16;
  3415. bool is_iplt;
  3416. Address plt_addr = this->plt_off(p, &is_iplt);
  3417. if (is_iplt)
  3418. plt_addr += this->targ_->iplt_section()->address();
  3419. else
  3420. plt_addr += this->targ_->plt_section()->address();
  3421. Address got_addr = this->targ_->got_section()->output_section()->address();
  3422. const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  3423. <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
  3424. got_addr += ppcobj->toc_base_offset();
  3425. Address off = plt_addr - got_addr;
  3426. unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
  3427. if (this->targ_->abiversion() < 2)
  3428. {
  3429. bool static_chain = parameters->options().plt_static_chain();
  3430. bool thread_safe = this->targ_->plt_thread_safe();
  3431. bytes += (4
  3432. + 4 * static_chain
  3433. + 8 * thread_safe
  3434. + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
  3435. }
  3436. unsigned int align = 1 << parameters->options().plt_align();
  3437. if (align > 1)
  3438. bytes = (bytes + align - 1) & -align;
  3439. return bytes;
  3440. }
  3441. // Return long branch stub size.
  3442. unsigned int
  3443. branch_stub_size(Address to)
  3444. {
  3445. Address loc
  3446. = this->stub_address() + this->last_plt_size_ + this->branch_size_;
  3447. if (to - loc + (1 << 25) < 2 << 25)
  3448. return 4;
  3449. if (size == 64 || !parameters->options().output_is_position_independent())
  3450. return 16;
  3451. return 32;
  3452. }
  3453. // Write out stubs.
  3454. void
  3455. do_write(Output_file*);
  3456. // Plt call stub keys.
  3457. class Plt_stub_ent
  3458. {
  3459. public:
  3460. Plt_stub_ent(const Symbol* sym)
  3461. : sym_(sym), object_(0), addend_(0), locsym_(0)
  3462. { }
  3463. Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
  3464. unsigned int locsym_index)
  3465. : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
  3466. { }
  3467. Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
  3468. const Symbol* sym,
  3469. unsigned int r_type,
  3470. Address addend)
  3471. : sym_(sym), object_(0), addend_(0), locsym_(0)
  3472. {
  3473. if (size != 32)
  3474. this->addend_ = addend;
  3475. else if (parameters->options().output_is_position_independent()
  3476. && r_type == elfcpp::R_PPC_PLTREL24)
  3477. {
  3478. this->addend_ = addend;
  3479. if (this->addend_ >= 32768)
  3480. this->object_ = object;
  3481. }
  3482. }
  3483. Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
  3484. unsigned int locsym_index,
  3485. unsigned int r_type,
  3486. Address addend)
  3487. : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
  3488. {
  3489. if (size != 32)
  3490. this->addend_ = addend;
  3491. else if (parameters->options().output_is_position_independent()
  3492. && r_type == elfcpp::R_PPC_PLTREL24)
  3493. this->addend_ = addend;
  3494. }
  3495. bool operator==(const Plt_stub_ent& that) const
  3496. {
  3497. return (this->sym_ == that.sym_
  3498. && this->object_ == that.object_
  3499. && this->addend_ == that.addend_
  3500. && this->locsym_ == that.locsym_);
  3501. }
  3502. const Symbol* sym_;
  3503. const Sized_relobj_file<size, big_endian>* object_;
  3504. typename elfcpp::Elf_types<size>::Elf_Addr addend_;
  3505. unsigned int locsym_;
  3506. };
  3507. class Plt_stub_ent_hash
  3508. {
  3509. public:
  3510. size_t operator()(const Plt_stub_ent& ent) const
  3511. {
  3512. return (reinterpret_cast<uintptr_t>(ent.sym_)
  3513. ^ reinterpret_cast<uintptr_t>(ent.object_)
  3514. ^ ent.addend_
  3515. ^ ent.locsym_);
  3516. }
  3517. };
  3518. // Long branch stub keys.
  3519. class Branch_stub_ent
  3520. {
  3521. public:
  3522. Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
  3523. Address to, bool save_res)
  3524. : dest_(to), toc_base_off_(0), save_res_(save_res)
  3525. {
  3526. if (size == 64)
  3527. toc_base_off_ = obj->toc_base_offset();
  3528. }
  3529. bool operator==(const Branch_stub_ent& that) const
  3530. {
  3531. return (this->dest_ == that.dest_
  3532. && (size == 32
  3533. || this->toc_base_off_ == that.toc_base_off_));
  3534. }
  3535. Address dest_;
  3536. unsigned int toc_base_off_;
  3537. bool save_res_;
  3538. };
  3539. class Branch_stub_ent_hash
  3540. {
  3541. public:
  3542. size_t operator()(const Branch_stub_ent& ent) const
  3543. { return ent.dest_ ^ ent.toc_base_off_; }
  3544. };
  3545. // In a sane world this would be a global.
  3546. Target_powerpc<size, big_endian>* targ_;
  3547. // Map sym/object/addend to stub offset.
  3548. Plt_stub_entries plt_call_stubs_;
  3549. // Map destination address to stub offset.
  3550. typedef Unordered_map<Branch_stub_ent, unsigned int,
  3551. Branch_stub_ent_hash> Branch_stub_entries;
  3552. Branch_stub_entries long_branch_stubs_;
  3553. // size of input section
  3554. section_size_type orig_data_size_;
  3555. // size of stubs
  3556. section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
  3557. // Whether .eh_frame info has been created for this stub section.
  3558. bool eh_frame_added_;
  3559. // Set if this stub group needs a copy of out-of-line register
  3560. // save/restore functions.
  3561. bool need_save_res_;
  3562. };
  3563. // Add a plt call stub, if we do not already have one for this
  3564. // sym/object/addend combo.
  3565. template<int size, bool big_endian>
  3566. bool
  3567. Stub_table<size, big_endian>::add_plt_call_entry(
  3568. Address from,
  3569. const Sized_relobj_file<size, big_endian>* object,
  3570. const Symbol* gsym,
  3571. unsigned int r_type,
  3572. Address addend)
  3573. {
  3574. Plt_stub_ent ent(object, gsym, r_type, addend);
  3575. unsigned int off = this->plt_size_;
  3576. std::pair<typename Plt_stub_entries::iterator, bool> p
  3577. = this->plt_call_stubs_.insert(std::make_pair(ent, off));
  3578. if (p.second)
  3579. this->plt_size_ = off + this->plt_call_size(p.first);
  3580. return this->can_reach_stub(from, off, r_type);
  3581. }
  3582. template<int size, bool big_endian>
  3583. bool
  3584. Stub_table<size, big_endian>::add_plt_call_entry(
  3585. Address from,
  3586. const Sized_relobj_file<size, big_endian>* object,
  3587. unsigned int locsym_index,
  3588. unsigned int r_type,
  3589. Address addend)
  3590. {
  3591. Plt_stub_ent ent(object, locsym_index, r_type, addend);
  3592. unsigned int off = this->plt_size_;
  3593. std::pair<typename Plt_stub_entries::iterator, bool> p
  3594. = this->plt_call_stubs_.insert(std::make_pair(ent, off));
  3595. if (p.second)
  3596. this->plt_size_ = off + this->plt_call_size(p.first);
  3597. return this->can_reach_stub(from, off, r_type);
  3598. }
  3599. // Find a plt call stub.
  3600. template<int size, bool big_endian>
  3601. typename Stub_table<size, big_endian>::Address
  3602. Stub_table<size, big_endian>::find_plt_call_entry(
  3603. const Sized_relobj_file<size, big_endian>* object,
  3604. const Symbol* gsym,
  3605. unsigned int r_type,
  3606. Address addend) const
  3607. {
  3608. Plt_stub_ent ent(object, gsym, r_type, addend);
  3609. typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
  3610. return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
  3611. }
  3612. template<int size, bool big_endian>
  3613. typename Stub_table<size, big_endian>::Address
  3614. Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
  3615. {
  3616. Plt_stub_ent ent(gsym);
  3617. typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
  3618. return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
  3619. }
  3620. template<int size, bool big_endian>
  3621. typename Stub_table<size, big_endian>::Address
  3622. Stub_table<size, big_endian>::find_plt_call_entry(
  3623. const Sized_relobj_file<size, big_endian>* object,
  3624. unsigned int locsym_index,
  3625. unsigned int r_type,
  3626. Address addend) const
  3627. {
  3628. Plt_stub_ent ent(object, locsym_index, r_type, addend);
  3629. typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
  3630. return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
  3631. }
  3632. template<int size, bool big_endian>
  3633. typename Stub_table<size, big_endian>::Address
  3634. Stub_table<size, big_endian>::find_plt_call_entry(
  3635. const Sized_relobj_file<size, big_endian>* object,
  3636. unsigned int locsym_index) const
  3637. {
  3638. Plt_stub_ent ent(object, locsym_index);
  3639. typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
  3640. return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
  3641. }
  3642. // Add a long branch stub if we don't already have one to given
  3643. // destination.
  3644. template<int size, bool big_endian>
  3645. bool
  3646. Stub_table<size, big_endian>::add_long_branch_entry(
  3647. const Powerpc_relobj<size, big_endian>* object,
  3648. unsigned int r_type,
  3649. Address from,
  3650. Address to,
  3651. bool save_res)
  3652. {
  3653. Branch_stub_ent ent(object, to, save_res);
  3654. Address off = this->branch_size_;
  3655. if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
  3656. {
  3657. if (save_res)
  3658. this->need_save_res_ = true;
  3659. else
  3660. {
  3661. unsigned int stub_size = this->branch_stub_size(to);
  3662. this->branch_size_ = off + stub_size;
  3663. if (size == 64 && stub_size != 4)
  3664. this->targ_->add_branch_lookup_table(to);
  3665. }
  3666. }
  3667. return this->can_reach_stub(from, off, r_type);
  3668. }
  3669. // Find long branch stub offset.
  3670. template<int size, bool big_endian>
  3671. typename Stub_table<size, big_endian>::Address
  3672. Stub_table<size, big_endian>::find_long_branch_entry(
  3673. const Powerpc_relobj<size, big_endian>* object,
  3674. Address to) const
  3675. {
  3676. Branch_stub_ent ent(object, to, false);
  3677. typename Branch_stub_entries::const_iterator p
  3678. = this->long_branch_stubs_.find(ent);
  3679. if (p == this->long_branch_stubs_.end())
  3680. return invalid_address;
  3681. if (p->first.save_res_)
  3682. return to - this->targ_->savres_section()->address() + this->branch_size_;
  3683. return p->second;
  3684. }
  3685. // A class to handle .glink.
  3686. template<int size, bool big_endian>
  3687. class Output_data_glink : public Output_section_data
  3688. {
  3689. public:
  3690. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  3691. static const Address invalid_address = static_cast<Address>(0) - 1;
  3692. static const int pltresolve_size = 16*4;
  3693. Output_data_glink(Target_powerpc<size, big_endian>* targ)
  3694. : Output_section_data(16), targ_(targ), global_entry_stubs_(),
  3695. end_branch_table_(), ge_size_(0)
  3696. { }
  3697. void
  3698. add_eh_frame(Layout* layout);
  3699. void
  3700. add_global_entry(const Symbol*);
  3701. Address
  3702. find_global_entry(const Symbol*) const;
  3703. Address
  3704. global_entry_address() const
  3705. {
  3706. gold_assert(this->is_data_size_valid());
  3707. unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
  3708. return this->address() + global_entry_off;
  3709. }
  3710. protected:
  3711. // Write to a map file.
  3712. void
  3713. do_print_to_mapfile(Mapfile* mapfile) const
  3714. { mapfile->print_output_data(this, _("** glink")); }
  3715. private:
  3716. void
  3717. set_final_data_size();
  3718. // Write out .glink
  3719. void
  3720. do_write(Output_file*);
  3721. // Allows access to .got and .plt for do_write.
  3722. Target_powerpc<size, big_endian>* targ_;
  3723. // Map sym to stub offset.
  3724. typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
  3725. Global_entry_stub_entries global_entry_stubs_;
  3726. unsigned int end_branch_table_, ge_size_;
  3727. };
  3728. template<int size, bool big_endian>
  3729. void
  3730. Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
  3731. {
  3732. if (!parameters->options().ld_generated_unwind_info())
  3733. return;
  3734. if (size == 64)
  3735. {
  3736. if (this->targ_->abiversion() < 2)
  3737. layout->add_eh_frame_for_plt(this,
  3738. Eh_cie<64>::eh_frame_cie,
  3739. sizeof (Eh_cie<64>::eh_frame_cie),
  3740. glink_eh_frame_fde_64v1,
  3741. sizeof (glink_eh_frame_fde_64v1));
  3742. else
  3743. layout->add_eh_frame_for_plt(this,
  3744. Eh_cie<64>::eh_frame_cie,
  3745. sizeof (Eh_cie<64>::eh_frame_cie),
  3746. glink_eh_frame_fde_64v2,
  3747. sizeof (glink_eh_frame_fde_64v2));
  3748. }
  3749. else
  3750. {
  3751. // 32-bit .glink can use the default since the CIE return
  3752. // address reg, LR, is valid.
  3753. layout->add_eh_frame_for_plt(this,
  3754. Eh_cie<32>::eh_frame_cie,
  3755. sizeof (Eh_cie<32>::eh_frame_cie),
  3756. default_fde,
  3757. sizeof (default_fde));
  3758. // Except where LR is used in a PIC __glink_PLTresolve.
  3759. if (parameters->options().output_is_position_independent())
  3760. layout->add_eh_frame_for_plt(this,
  3761. Eh_cie<32>::eh_frame_cie,
  3762. sizeof (Eh_cie<32>::eh_frame_cie),
  3763. glink_eh_frame_fde_32,
  3764. sizeof (glink_eh_frame_fde_32));
  3765. }
  3766. }
  3767. template<int size, bool big_endian>
  3768. void
  3769. Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
  3770. {
  3771. std::pair<typename Global_entry_stub_entries::iterator, bool> p
  3772. = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
  3773. if (p.second)
  3774. this->ge_size_ += 16;
  3775. }
  3776. template<int size, bool big_endian>
  3777. typename Output_data_glink<size, big_endian>::Address
  3778. Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
  3779. {
  3780. typename Global_entry_stub_entries::const_iterator p
  3781. = this->global_entry_stubs_.find(gsym);
  3782. return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
  3783. }
  3784. template<int size, bool big_endian>
  3785. void
  3786. Output_data_glink<size, big_endian>::set_final_data_size()
  3787. {
  3788. unsigned int count = this->targ_->plt_entry_count();
  3789. section_size_type total = 0;
  3790. if (count != 0)
  3791. {
  3792. if (size == 32)
  3793. {
  3794. // space for branch table
  3795. total += 4 * (count - 1);
  3796. total += -total & 15;
  3797. total += this->pltresolve_size;
  3798. }
  3799. else
  3800. {
  3801. total += this->pltresolve_size;
  3802. // space for branch table
  3803. total += 4 * count;
  3804. if (this->targ_->abiversion() < 2)
  3805. {
  3806. total += 4 * count;
  3807. if (count > 0x8000)
  3808. total += 4 * (count - 0x8000);
  3809. }
  3810. }
  3811. }
  3812. this->end_branch_table_ = total;
  3813. total = (total + 15) & -16;
  3814. total += this->ge_size_;
  3815. this->set_data_size(total);
  3816. }
  3817. // Write out plt and long branch stub code.
  3818. template<int size, bool big_endian>
  3819. void
  3820. Stub_table<size, big_endian>::do_write(Output_file* of)
  3821. {
  3822. if (this->plt_call_stubs_.empty()
  3823. && this->long_branch_stubs_.empty())
  3824. return;
  3825. const section_size_type start_off = this->offset();
  3826. const section_size_type off = this->stub_offset();
  3827. const section_size_type oview_size =
  3828. convert_to_section_size_type(this->data_size() - (off - start_off));
  3829. unsigned char* const oview = of->get_output_view(off, oview_size);
  3830. unsigned char* p;
  3831. if (size == 64)
  3832. {
  3833. const Output_data_got_powerpc<size, big_endian>* got
  3834. = this->targ_->got_section();
  3835. Address got_os_addr = got->output_section()->address();
  3836. if (!this->plt_call_stubs_.empty())
  3837. {
  3838. // The base address of the .plt section.
  3839. Address plt_base = this->targ_->plt_section()->address();
  3840. Address iplt_base = invalid_address;
  3841. // Write out plt call stubs.
  3842. typename Plt_stub_entries::const_iterator cs;
  3843. for (cs = this->plt_call_stubs_.begin();
  3844. cs != this->plt_call_stubs_.end();
  3845. ++cs)
  3846. {
  3847. bool is_iplt;
  3848. Address pltoff = this->plt_off(cs, &is_iplt);
  3849. Address plt_addr = pltoff;
  3850. if (is_iplt)
  3851. {
  3852. if (iplt_base == invalid_address)
  3853. iplt_base = this->targ_->iplt_section()->address();
  3854. plt_addr += iplt_base;
  3855. }
  3856. else
  3857. plt_addr += plt_base;
  3858. const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
  3859. <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
  3860. Address got_addr = got_os_addr + ppcobj->toc_base_offset();
  3861. Address off = plt_addr - got_addr;
  3862. if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
  3863. gold_error(_("%s: linkage table error against `%s'"),
  3864. cs->first.object_->name().c_str(),
  3865. cs->first.sym_->demangled_name().c_str());
  3866. bool plt_load_toc = this->targ_->abiversion() < 2;
  3867. bool static_chain
  3868. = plt_load_toc && parameters->options().plt_static_chain();
  3869. bool thread_safe
  3870. = plt_load_toc && this->targ_->plt_thread_safe();
  3871. bool use_fake_dep = false;
  3872. Address cmp_branch_off = 0;
  3873. if (thread_safe)
  3874. {
  3875. unsigned int pltindex
  3876. = ((pltoff - this->targ_->first_plt_entry_offset())
  3877. / this->targ_->plt_entry_size());
  3878. Address glinkoff
  3879. = (this->targ_->glink_section()->pltresolve_size
  3880. + pltindex * 8);
  3881. if (pltindex > 32768)
  3882. glinkoff += (pltindex - 32768) * 4;
  3883. Address to
  3884. = this->targ_->glink_section()->address() + glinkoff;
  3885. Address from
  3886. = (this->stub_address() + cs->second + 24
  3887. + 4 * (ha(off) != 0)
  3888. + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
  3889. + 4 * static_chain);
  3890. cmp_branch_off = to - from;
  3891. use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
  3892. }
  3893. p = oview + cs->second;
  3894. if (ha(off) != 0)
  3895. {
  3896. write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
  3897. p += 4;
  3898. if (plt_load_toc)
  3899. {
  3900. write_insn<big_endian>(p, addis_11_2 + ha(off));
  3901. p += 4;
  3902. write_insn<big_endian>(p, ld_12_11 + l(off));
  3903. p += 4;
  3904. }
  3905. else
  3906. {
  3907. write_insn<big_endian>(p, addis_12_2 + ha(off));
  3908. p += 4;
  3909. write_insn<big_endian>(p, ld_12_12 + l(off));
  3910. p += 4;
  3911. }
  3912. if (plt_load_toc
  3913. && ha(off + 8 + 8 * static_chain) != ha(off))
  3914. {
  3915. write_insn<big_endian>(p, addi_11_11 + l(off));
  3916. p += 4;
  3917. off = 0;
  3918. }
  3919. write_insn<big_endian>(p, mtctr_12);
  3920. p += 4;
  3921. if (plt_load_toc)
  3922. {
  3923. if (use_fake_dep)
  3924. {
  3925. write_insn<big_endian>(p, xor_2_12_12);
  3926. p += 4;
  3927. write_insn<big_endian>(p, add_11_11_2);
  3928. p += 4;
  3929. }
  3930. write_insn<big_endian>(p, ld_2_11 + l(off + 8));
  3931. p += 4;
  3932. if (static_chain)
  3933. {
  3934. write_insn<big_endian>(p, ld_11_11 + l(off + 16));
  3935. p += 4;
  3936. }
  3937. }
  3938. }
  3939. else
  3940. {
  3941. write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
  3942. p += 4;
  3943. write_insn<big_endian>(p, ld_12_2 + l(off));
  3944. p += 4;
  3945. if (plt_load_toc
  3946. && ha(off + 8 + 8 * static_chain) != ha(off))
  3947. {
  3948. write_insn<big_endian>(p, addi_2_2 + l(off));
  3949. p += 4;
  3950. off = 0;
  3951. }
  3952. write_insn<big_endian>(p, mtctr_12);
  3953. p += 4;
  3954. if (plt_load_toc)
  3955. {
  3956. if (use_fake_dep)
  3957. {
  3958. write_insn<big_endian>(p, xor_11_12_12);
  3959. p += 4;
  3960. write_insn<big_endian>(p, add_2_2_11);
  3961. p += 4;
  3962. }
  3963. if (static_chain)
  3964. {
  3965. write_insn<big_endian>(p, ld_11_2 + l(off + 16));
  3966. p += 4;
  3967. }
  3968. write_insn<big_endian>(p, ld_2_2 + l(off + 8));
  3969. p += 4;
  3970. }
  3971. }
  3972. if (thread_safe && !use_fake_dep)
  3973. {
  3974. write_insn<big_endian>(p, cmpldi_2_0);
  3975. p += 4;
  3976. write_insn<big_endian>(p, bnectr_p4);
  3977. p += 4;
  3978. write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
  3979. }
  3980. else
  3981. write_insn<big_endian>(p, bctr);
  3982. }
  3983. }
  3984. // Write out long branch stubs.
  3985. typename Branch_stub_entries::const_iterator bs;
  3986. for (bs = this->long_branch_stubs_.begin();
  3987. bs != this->long_branch_stubs_.end();
  3988. ++bs)
  3989. {
  3990. if (bs->first.save_res_)
  3991. continue;
  3992. p = oview + this->plt_size_ + bs->second;
  3993. Address loc = this->stub_address() + this->plt_size_ + bs->second;
  3994. Address delta = bs->first.dest_ - loc;
  3995. if (delta + (1 << 25) < 2 << 25)
  3996. write_insn<big_endian>(p, b | (delta & 0x3fffffc));
  3997. else
  3998. {
  3999. Address brlt_addr
  4000. = this->targ_->find_branch_lookup_table(bs->first.dest_);
  4001. gold_assert(brlt_addr != invalid_address);
  4002. brlt_addr += this->targ_->brlt_section()->address();
  4003. Address got_addr = got_os_addr + bs->first.toc_base_off_;
  4004. Address brltoff = brlt_addr - got_addr;
  4005. if (ha(brltoff) == 0)
  4006. {
  4007. write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
  4008. }
  4009. else
  4010. {
  4011. write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
  4012. write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
  4013. }
  4014. write_insn<big_endian>(p, mtctr_12), p += 4;
  4015. write_insn<big_endian>(p, bctr);
  4016. }
  4017. }
  4018. }
  4019. else
  4020. {
  4021. if (!this->plt_call_stubs_.empty())
  4022. {
  4023. // The base address of the .plt section.
  4024. Address plt_base = this->targ_->plt_section()->address();
  4025. Address iplt_base = invalid_address;
  4026. // The address of _GLOBAL_OFFSET_TABLE_.
  4027. Address g_o_t = invalid_address;
  4028. // Write out plt call stubs.
  4029. typename Plt_stub_entries::const_iterator cs;
  4030. for (cs = this->plt_call_stubs_.begin();
  4031. cs != this->plt_call_stubs_.end();
  4032. ++cs)
  4033. {
  4034. bool is_iplt;
  4035. Address plt_addr = this->plt_off(cs, &is_iplt);
  4036. if (is_iplt)
  4037. {
  4038. if (iplt_base == invalid_address)
  4039. iplt_base = this->targ_->iplt_section()->address();
  4040. plt_addr += iplt_base;
  4041. }
  4042. else
  4043. plt_addr += plt_base;
  4044. p = oview + cs->second;
  4045. if (parameters->options().output_is_position_independent())
  4046. {
  4047. Address got_addr;
  4048. const Powerpc_relobj<size, big_endian>* ppcobj
  4049. = (static_cast<const Powerpc_relobj<size, big_endian>*>
  4050. (cs->first.object_));
  4051. if (ppcobj != NULL && cs->first.addend_ >= 32768)
  4052. {
  4053. unsigned int got2 = ppcobj->got2_shndx();
  4054. got_addr = ppcobj->get_output_section_offset(got2);
  4055. gold_assert(got_addr != invalid_address);
  4056. got_addr += (ppcobj->output_section(got2)->address()
  4057. + cs->first.addend_);
  4058. }
  4059. else
  4060. {
  4061. if (g_o_t == invalid_address)
  4062. {
  4063. const Output_data_got_powerpc<size, big_endian>* got
  4064. = this->targ_->got_section();
  4065. g_o_t = got->address() + got->g_o_t();
  4066. }
  4067. got_addr = g_o_t;
  4068. }
  4069. Address off = plt_addr - got_addr;
  4070. if (ha(off) == 0)
  4071. {
  4072. write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
  4073. write_insn<big_endian>(p + 4, mtctr_11);
  4074. write_insn<big_endian>(p + 8, bctr);
  4075. }
  4076. else
  4077. {
  4078. write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
  4079. write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
  4080. write_insn<big_endian>(p + 8, mtctr_11);
  4081. write_insn<big_endian>(p + 12, bctr);
  4082. }
  4083. }
  4084. else
  4085. {
  4086. write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
  4087. write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
  4088. write_insn<big_endian>(p + 8, mtctr_11);
  4089. write_insn<big_endian>(p + 12, bctr);
  4090. }
  4091. }
  4092. }
  4093. // Write out long branch stubs.
  4094. typename Branch_stub_entries::const_iterator bs;
  4095. for (bs = this->long_branch_stubs_.begin();
  4096. bs != this->long_branch_stubs_.end();
  4097. ++bs)
  4098. {
  4099. if (bs->first.save_res_)
  4100. continue;
  4101. p = oview + this->plt_size_ + bs->second;
  4102. Address loc = this->stub_address() + this->plt_size_ + bs->second;
  4103. Address delta = bs->first.dest_ - loc;
  4104. if (delta + (1 << 25) < 2 << 25)
  4105. write_insn<big_endian>(p, b | (delta & 0x3fffffc));
  4106. else if (!parameters->options().output_is_position_independent())
  4107. {
  4108. write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
  4109. write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
  4110. write_insn<big_endian>(p + 8, mtctr_12);
  4111. write_insn<big_endian>(p + 12, bctr);
  4112. }
  4113. else
  4114. {
  4115. delta -= 8;
  4116. write_insn<big_endian>(p + 0, mflr_0);
  4117. write_insn<big_endian>(p + 4, bcl_20_31);
  4118. write_insn<big_endian>(p + 8, mflr_12);
  4119. write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
  4120. write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
  4121. write_insn<big_endian>(p + 20, mtlr_0);
  4122. write_insn<big_endian>(p + 24, mtctr_12);
  4123. write_insn<big_endian>(p + 28, bctr);
  4124. }
  4125. }
  4126. }
  4127. if (this->need_save_res_)
  4128. {
  4129. p = oview + this->plt_size_ + this->branch_size_;
  4130. memcpy (p, this->targ_->savres_section()->contents(),
  4131. this->targ_->savres_section()->data_size());
  4132. }
  4133. }
  4134. // Write out .glink.
  4135. template<int size, bool big_endian>
  4136. void
  4137. Output_data_glink<size, big_endian>::do_write(Output_file* of)
  4138. {
  4139. const section_size_type off = this->offset();
  4140. const section_size_type oview_size =
  4141. convert_to_section_size_type(this->data_size());
  4142. unsigned char* const oview = of->get_output_view(off, oview_size);
  4143. unsigned char* p;
  4144. // The base address of the .plt section.
  4145. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  4146. Address plt_base = this->targ_->plt_section()->address();
  4147. if (size == 64)
  4148. {
  4149. if (this->end_branch_table_ != 0)
  4150. {
  4151. // Write pltresolve stub.
  4152. p = oview;
  4153. Address after_bcl = this->address() + 16;
  4154. Address pltoff = plt_base - after_bcl;
  4155. elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
  4156. if (this->targ_->abiversion() < 2)
  4157. {
  4158. write_insn<big_endian>(p, mflr_12), p += 4;
  4159. write_insn<big_endian>(p, bcl_20_31), p += 4;
  4160. write_insn<big_endian>(p, mflr_11), p += 4;
  4161. write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
  4162. write_insn<big_endian>(p, mtlr_12), p += 4;
  4163. write_insn<big_endian>(p, add_11_2_11), p += 4;
  4164. write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
  4165. write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
  4166. write_insn<big_endian>(p, mtctr_12), p += 4;
  4167. write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
  4168. }
  4169. else
  4170. {
  4171. write_insn<big_endian>(p, mflr_0), p += 4;
  4172. write_insn<big_endian>(p, bcl_20_31), p += 4;
  4173. write_insn<big_endian>(p, mflr_11), p += 4;
  4174. write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
  4175. write_insn<big_endian>(p, mtlr_0), p += 4;
  4176. write_insn<big_endian>(p, sub_12_12_11), p += 4;
  4177. write_insn<big_endian>(p, add_11_2_11), p += 4;
  4178. write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
  4179. write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
  4180. write_insn<big_endian>(p, srdi_0_0_2), p += 4;
  4181. write_insn<big_endian>(p, mtctr_12), p += 4;
  4182. write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
  4183. }
  4184. write_insn<big_endian>(p, bctr), p += 4;
  4185. while (p < oview + this->pltresolve_size)
  4186. write_insn<big_endian>(p, nop), p += 4;
  4187. // Write lazy link call stubs.
  4188. uint32_t indx = 0;
  4189. while (p < oview + this->end_branch_table_)
  4190. {
  4191. if (this->targ_->abiversion() < 2)
  4192. {
  4193. if (indx < 0x8000)
  4194. {
  4195. write_insn<big_endian>(p, li_0_0 + indx), p += 4;
  4196. }
  4197. else
  4198. {
  4199. write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
  4200. write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
  4201. }
  4202. }
  4203. uint32_t branch_off = 8 - (p - oview);
  4204. write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
  4205. indx++;
  4206. }
  4207. }
  4208. Address plt_base = this->targ_->plt_section()->address();
  4209. Address iplt_base = invalid_address;
  4210. unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
  4211. Address global_entry_base = this->address() + global_entry_off;
  4212. typename Global_entry_stub_entries::const_iterator ge;
  4213. for (ge = this->global_entry_stubs_.begin();
  4214. ge != this->global_entry_stubs_.end();
  4215. ++ge)
  4216. {
  4217. p = oview + global_entry_off + ge->second;
  4218. Address plt_addr = ge->first->plt_offset();
  4219. if (ge->first->type() == elfcpp::STT_GNU_IFUNC
  4220. && ge->first->can_use_relative_reloc(false))
  4221. {
  4222. if (iplt_base == invalid_address)
  4223. iplt_base = this->targ_->iplt_section()->address();
  4224. plt_addr += iplt_base;
  4225. }
  4226. else
  4227. plt_addr += plt_base;
  4228. Address my_addr = global_entry_base + ge->second;
  4229. Address off = plt_addr - my_addr;
  4230. if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
  4231. gold_error(_("%s: linkage table error against `%s'"),
  4232. ge->first->object()->name().c_str(),
  4233. ge->first->demangled_name().c_str());
  4234. write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
  4235. write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
  4236. write_insn<big_endian>(p, mtctr_12), p += 4;
  4237. write_insn<big_endian>(p, bctr);
  4238. }
  4239. }
  4240. else
  4241. {
  4242. const Output_data_got_powerpc<size, big_endian>* got
  4243. = this->targ_->got_section();
  4244. // The address of _GLOBAL_OFFSET_TABLE_.
  4245. Address g_o_t = got->address() + got->g_o_t();
  4246. // Write out pltresolve branch table.
  4247. p = oview;
  4248. unsigned int the_end = oview_size - this->pltresolve_size;
  4249. unsigned char* end_p = oview + the_end;
  4250. while (p < end_p - 8 * 4)
  4251. write_insn<big_endian>(p, b + end_p - p), p += 4;
  4252. while (p < end_p)
  4253. write_insn<big_endian>(p, nop), p += 4;
  4254. // Write out pltresolve call stub.
  4255. if (parameters->options().output_is_position_independent())
  4256. {
  4257. Address res0_off = 0;
  4258. Address after_bcl_off = the_end + 12;
  4259. Address bcl_res0 = after_bcl_off - res0_off;
  4260. write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
  4261. write_insn<big_endian>(p + 4, mflr_0);
  4262. write_insn<big_endian>(p + 8, bcl_20_31);
  4263. write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
  4264. write_insn<big_endian>(p + 16, mflr_12);
  4265. write_insn<big_endian>(p + 20, mtlr_0);
  4266. write_insn<big_endian>(p + 24, sub_11_11_12);
  4267. Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
  4268. write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
  4269. if (ha(got_bcl) == ha(got_bcl + 4))
  4270. {
  4271. write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
  4272. write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
  4273. }
  4274. else
  4275. {
  4276. write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
  4277. write_insn<big_endian>(p + 36, lwz_12_12 + 4);
  4278. }
  4279. write_insn<big_endian>(p + 40, mtctr_0);
  4280. write_insn<big_endian>(p + 44, add_0_11_11);
  4281. write_insn<big_endian>(p + 48, add_11_0_11);
  4282. write_insn<big_endian>(p + 52, bctr);
  4283. write_insn<big_endian>(p + 56, nop);
  4284. write_insn<big_endian>(p + 60, nop);
  4285. }
  4286. else
  4287. {
  4288. Address res0 = this->address();
  4289. write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
  4290. write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
  4291. if (ha(g_o_t + 4) == ha(g_o_t + 8))
  4292. write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
  4293. else
  4294. write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
  4295. write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
  4296. write_insn<big_endian>(p + 16, mtctr_0);
  4297. write_insn<big_endian>(p + 20, add_0_11_11);
  4298. if (ha(g_o_t + 4) == ha(g_o_t + 8))
  4299. write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
  4300. else
  4301. write_insn<big_endian>(p + 24, lwz_12_12 + 4);
  4302. write_insn<big_endian>(p + 28, add_11_0_11);
  4303. write_insn<big_endian>(p + 32, bctr);
  4304. write_insn<big_endian>(p + 36, nop);
  4305. write_insn<big_endian>(p + 40, nop);
  4306. write_insn<big_endian>(p + 44, nop);
  4307. write_insn<big_endian>(p + 48, nop);
  4308. write_insn<big_endian>(p + 52, nop);
  4309. write_insn<big_endian>(p + 56, nop);
  4310. write_insn<big_endian>(p + 60, nop);
  4311. }
  4312. p += 64;
  4313. }
  4314. of->write_output_view(off, oview_size, oview);
  4315. }
  4316. // A class to handle linker generated save/restore functions.
  4317. template<int size, bool big_endian>
  4318. class Output_data_save_res : public Output_section_data_build
  4319. {
  4320. public:
  4321. Output_data_save_res(Symbol_table* symtab);
  4322. const unsigned char*
  4323. contents() const
  4324. {
  4325. return contents_;
  4326. }
  4327. protected:
  4328. // Write to a map file.
  4329. void
  4330. do_print_to_mapfile(Mapfile* mapfile) const
  4331. { mapfile->print_output_data(this, _("** save/restore")); }
  4332. void
  4333. do_write(Output_file*);
  4334. private:
  4335. // The maximum size of save/restore contents.
  4336. static const unsigned int savres_max = 218*4;
  4337. void
  4338. savres_define(Symbol_table* symtab,
  4339. const char *name,
  4340. unsigned int lo, unsigned int hi,
  4341. unsigned char* write_ent(unsigned char*, int),
  4342. unsigned char* write_tail(unsigned char*, int));
  4343. unsigned char *contents_;
  4344. };
  4345. template<bool big_endian>
  4346. static unsigned char*
  4347. savegpr0(unsigned char* p, int r)
  4348. {
  4349. uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
  4350. write_insn<big_endian>(p, insn);
  4351. return p + 4;
  4352. }
  4353. template<bool big_endian>
  4354. static unsigned char*
  4355. savegpr0_tail(unsigned char* p, int r)
  4356. {
  4357. p = savegpr0<big_endian>(p, r);
  4358. uint32_t insn = std_0_1 + 16;
  4359. write_insn<big_endian>(p, insn);
  4360. p = p + 4;
  4361. write_insn<big_endian>(p, blr);
  4362. return p + 4;
  4363. }
  4364. template<bool big_endian>
  4365. static unsigned char*
  4366. restgpr0(unsigned char* p, int r)
  4367. {
  4368. uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
  4369. write_insn<big_endian>(p, insn);
  4370. return p + 4;
  4371. }
  4372. template<bool big_endian>
  4373. static unsigned char*
  4374. restgpr0_tail(unsigned char* p, int r)
  4375. {
  4376. uint32_t insn = ld_0_1 + 16;
  4377. write_insn<big_endian>(p, insn);
  4378. p = p + 4;
  4379. p = restgpr0<big_endian>(p, r);
  4380. write_insn<big_endian>(p, mtlr_0);
  4381. p = p + 4;
  4382. if (r == 29)
  4383. {
  4384. p = restgpr0<big_endian>(p, 30);
  4385. p = restgpr0<big_endian>(p, 31);
  4386. }
  4387. write_insn<big_endian>(p, blr);
  4388. return p + 4;
  4389. }
  4390. template<bool big_endian>
  4391. static unsigned char*
  4392. savegpr1(unsigned char* p, int r)
  4393. {
  4394. uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
  4395. write_insn<big_endian>(p, insn);
  4396. return p + 4;
  4397. }
  4398. template<bool big_endian>
  4399. static unsigned char*
  4400. savegpr1_tail(unsigned char* p, int r)
  4401. {
  4402. p = savegpr1<big_endian>(p, r);
  4403. write_insn<big_endian>(p, blr);
  4404. return p + 4;
  4405. }
  4406. template<bool big_endian>
  4407. static unsigned char*
  4408. restgpr1(unsigned char* p, int r)
  4409. {
  4410. uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
  4411. write_insn<big_endian>(p, insn);
  4412. return p + 4;
  4413. }
  4414. template<bool big_endian>
  4415. static unsigned char*
  4416. restgpr1_tail(unsigned char* p, int r)
  4417. {
  4418. p = restgpr1<big_endian>(p, r);
  4419. write_insn<big_endian>(p, blr);
  4420. return p + 4;
  4421. }
  4422. template<bool big_endian>
  4423. static unsigned char*
  4424. savefpr(unsigned char* p, int r)
  4425. {
  4426. uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
  4427. write_insn<big_endian>(p, insn);
  4428. return p + 4;
  4429. }
  4430. template<bool big_endian>
  4431. static unsigned char*
  4432. savefpr0_tail(unsigned char* p, int r)
  4433. {
  4434. p = savefpr<big_endian>(p, r);
  4435. write_insn<big_endian>(p, std_0_1 + 16);
  4436. p = p + 4;
  4437. write_insn<big_endian>(p, blr);
  4438. return p + 4;
  4439. }
  4440. template<bool big_endian>
  4441. static unsigned char*
  4442. restfpr(unsigned char* p, int r)
  4443. {
  4444. uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
  4445. write_insn<big_endian>(p, insn);
  4446. return p + 4;
  4447. }
  4448. template<bool big_endian>
  4449. static unsigned char*
  4450. restfpr0_tail(unsigned char* p, int r)
  4451. {
  4452. write_insn<big_endian>(p, ld_0_1 + 16);
  4453. p = p + 4;
  4454. p = restfpr<big_endian>(p, r);
  4455. write_insn<big_endian>(p, mtlr_0);
  4456. p = p + 4;
  4457. if (r == 29)
  4458. {
  4459. p = restfpr<big_endian>(p, 30);
  4460. p = restfpr<big_endian>(p, 31);
  4461. }
  4462. write_insn<big_endian>(p, blr);
  4463. return p + 4;
  4464. }
  4465. template<bool big_endian>
  4466. static unsigned char*
  4467. savefpr1_tail(unsigned char* p, int r)
  4468. {
  4469. p = savefpr<big_endian>(p, r);
  4470. write_insn<big_endian>(p, blr);
  4471. return p + 4;
  4472. }
  4473. template<bool big_endian>
  4474. static unsigned char*
  4475. restfpr1_tail(unsigned char* p, int r)
  4476. {
  4477. p = restfpr<big_endian>(p, r);
  4478. write_insn<big_endian>(p, blr);
  4479. return p + 4;
  4480. }
  4481. template<bool big_endian>
  4482. static unsigned char*
  4483. savevr(unsigned char* p, int r)
  4484. {
  4485. uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
  4486. write_insn<big_endian>(p, insn);
  4487. p = p + 4;
  4488. insn = stvx_0_12_0 + (r << 21);
  4489. write_insn<big_endian>(p, insn);
  4490. return p + 4;
  4491. }
  4492. template<bool big_endian>
  4493. static unsigned char*
  4494. savevr_tail(unsigned char* p, int r)
  4495. {
  4496. p = savevr<big_endian>(p, r);
  4497. write_insn<big_endian>(p, blr);
  4498. return p + 4;
  4499. }
  4500. template<bool big_endian>
  4501. static unsigned char*
  4502. restvr(unsigned char* p, int r)
  4503. {
  4504. uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
  4505. write_insn<big_endian>(p, insn);
  4506. p = p + 4;
  4507. insn = lvx_0_12_0 + (r << 21);
  4508. write_insn<big_endian>(p, insn);
  4509. return p + 4;
  4510. }
  4511. template<bool big_endian>
  4512. static unsigned char*
  4513. restvr_tail(unsigned char* p, int r)
  4514. {
  4515. p = restvr<big_endian>(p, r);
  4516. write_insn<big_endian>(p, blr);
  4517. return p + 4;
  4518. }
  4519. template<int size, bool big_endian>
  4520. Output_data_save_res<size, big_endian>::Output_data_save_res(
  4521. Symbol_table* symtab)
  4522. : Output_section_data_build(4),
  4523. contents_(NULL)
  4524. {
  4525. this->savres_define(symtab,
  4526. "_savegpr0_", 14, 31,
  4527. savegpr0<big_endian>, savegpr0_tail<big_endian>);
  4528. this->savres_define(symtab,
  4529. "_restgpr0_", 14, 29,
  4530. restgpr0<big_endian>, restgpr0_tail<big_endian>);
  4531. this->savres_define(symtab,
  4532. "_restgpr0_", 30, 31,
  4533. restgpr0<big_endian>, restgpr0_tail<big_endian>);
  4534. this->savres_define(symtab,
  4535. "_savegpr1_", 14, 31,
  4536. savegpr1<big_endian>, savegpr1_tail<big_endian>);
  4537. this->savres_define(symtab,
  4538. "_restgpr1_", 14, 31,
  4539. restgpr1<big_endian>, restgpr1_tail<big_endian>);
  4540. this->savres_define(symtab,
  4541. "_savefpr_", 14, 31,
  4542. savefpr<big_endian>, savefpr0_tail<big_endian>);
  4543. this->savres_define(symtab,
  4544. "_restfpr_", 14, 29,
  4545. restfpr<big_endian>, restfpr0_tail<big_endian>);
  4546. this->savres_define(symtab,
  4547. "_restfpr_", 30, 31,
  4548. restfpr<big_endian>, restfpr0_tail<big_endian>);
  4549. this->savres_define(symtab,
  4550. "._savef", 14, 31,
  4551. savefpr<big_endian>, savefpr1_tail<big_endian>);
  4552. this->savres_define(symtab,
  4553. "._restf", 14, 31,
  4554. restfpr<big_endian>, restfpr1_tail<big_endian>);
  4555. this->savres_define(symtab,
  4556. "_savevr_", 20, 31,
  4557. savevr<big_endian>, savevr_tail<big_endian>);
  4558. this->savres_define(symtab,
  4559. "_restvr_", 20, 31,
  4560. restvr<big_endian>, restvr_tail<big_endian>);
  4561. }
  4562. template<int size, bool big_endian>
  4563. void
  4564. Output_data_save_res<size, big_endian>::savres_define(
  4565. Symbol_table* symtab,
  4566. const char *name,
  4567. unsigned int lo, unsigned int hi,
  4568. unsigned char* write_ent(unsigned char*, int),
  4569. unsigned char* write_tail(unsigned char*, int))
  4570. {
  4571. size_t len = strlen(name);
  4572. bool writing = false;
  4573. char sym[16];
  4574. memcpy(sym, name, len);
  4575. sym[len + 2] = 0;
  4576. for (unsigned int i = lo; i <= hi; i++)
  4577. {
  4578. sym[len + 0] = i / 10 + '0';
  4579. sym[len + 1] = i % 10 + '0';
  4580. Symbol* gsym = symtab->lookup(sym);
  4581. bool refd = gsym != NULL && gsym->is_undefined();
  4582. writing = writing || refd;
  4583. if (writing)
  4584. {
  4585. if (this->contents_ == NULL)
  4586. this->contents_ = new unsigned char[this->savres_max];
  4587. section_size_type value = this->current_data_size();
  4588. unsigned char* p = this->contents_ + value;
  4589. if (i != hi)
  4590. p = write_ent(p, i);
  4591. else
  4592. p = write_tail(p, i);
  4593. section_size_type cur_size = p - this->contents_;
  4594. this->set_current_data_size(cur_size);
  4595. if (refd)
  4596. symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
  4597. this, value, cur_size - value,
  4598. elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
  4599. elfcpp::STV_HIDDEN, 0, false, false);
  4600. }
  4601. }
  4602. }
  4603. // Write out save/restore.
  4604. template<int size, bool big_endian>
  4605. void
  4606. Output_data_save_res<size, big_endian>::do_write(Output_file* of)
  4607. {
  4608. const section_size_type off = this->offset();
  4609. const section_size_type oview_size =
  4610. convert_to_section_size_type(this->data_size());
  4611. unsigned char* const oview = of->get_output_view(off, oview_size);
  4612. memcpy(oview, this->contents_, oview_size);
  4613. of->write_output_view(off, oview_size, oview);
  4614. }
  4615. // Create the glink section.
  4616. template<int size, bool big_endian>
  4617. void
  4618. Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
  4619. {
  4620. if (this->glink_ == NULL)
  4621. {
  4622. this->glink_ = new Output_data_glink<size, big_endian>(this);
  4623. this->glink_->add_eh_frame(layout);
  4624. layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
  4625. elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
  4626. this->glink_, ORDER_TEXT, false);
  4627. }
  4628. }
  4629. // Create a PLT entry for a global symbol.
  4630. template<int size, bool big_endian>
  4631. void
  4632. Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
  4633. Layout* layout,
  4634. Symbol* gsym)
  4635. {
  4636. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  4637. && gsym->can_use_relative_reloc(false))
  4638. {
  4639. if (this->iplt_ == NULL)
  4640. this->make_iplt_section(symtab, layout);
  4641. this->iplt_->add_ifunc_entry(gsym);
  4642. }
  4643. else
  4644. {
  4645. if (this->plt_ == NULL)
  4646. this->make_plt_section(symtab, layout);
  4647. this->plt_->add_entry(gsym);
  4648. }
  4649. }
  4650. // Make a PLT entry for a local STT_GNU_IFUNC symbol.
  4651. template<int size, bool big_endian>
  4652. void
  4653. Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
  4654. Symbol_table* symtab,
  4655. Layout* layout,
  4656. Sized_relobj_file<size, big_endian>* relobj,
  4657. unsigned int r_sym)
  4658. {
  4659. if (this->iplt_ == NULL)
  4660. this->make_iplt_section(symtab, layout);
  4661. this->iplt_->add_local_ifunc_entry(relobj, r_sym);
  4662. }
  4663. // Return the number of entries in the PLT.
  4664. template<int size, bool big_endian>
  4665. unsigned int
  4666. Target_powerpc<size, big_endian>::plt_entry_count() const
  4667. {
  4668. if (this->plt_ == NULL)
  4669. return 0;
  4670. return this->plt_->entry_count();
  4671. }
  4672. // Create a GOT entry for local dynamic __tls_get_addr calls.
  4673. template<int size, bool big_endian>
  4674. unsigned int
  4675. Target_powerpc<size, big_endian>::tlsld_got_offset(
  4676. Symbol_table* symtab,
  4677. Layout* layout,
  4678. Sized_relobj_file<size, big_endian>* object)
  4679. {
  4680. if (this->tlsld_got_offset_ == -1U)
  4681. {
  4682. gold_assert(symtab != NULL && layout != NULL && object != NULL);
  4683. Reloc_section* rela_dyn = this->rela_dyn_section(layout);
  4684. Output_data_got_powerpc<size, big_endian>* got
  4685. = this->got_section(symtab, layout);
  4686. unsigned int got_offset = got->add_constant_pair(0, 0);
  4687. rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
  4688. got_offset, 0);
  4689. this->tlsld_got_offset_ = got_offset;
  4690. }
  4691. return this->tlsld_got_offset_;
  4692. }
  4693. // Get the Reference_flags for a particular relocation.
  4694. template<int size, bool big_endian>
  4695. int
  4696. Target_powerpc<size, big_endian>::Scan::get_reference_flags(
  4697. unsigned int r_type,
  4698. const Target_powerpc* target)
  4699. {
  4700. int ref = 0;
  4701. switch (r_type)
  4702. {
  4703. case elfcpp::R_POWERPC_NONE:
  4704. case elfcpp::R_POWERPC_GNU_VTINHERIT:
  4705. case elfcpp::R_POWERPC_GNU_VTENTRY:
  4706. case elfcpp::R_PPC64_TOC:
  4707. // No symbol reference.
  4708. break;
  4709. case elfcpp::R_PPC64_ADDR64:
  4710. case elfcpp::R_PPC64_UADDR64:
  4711. case elfcpp::R_POWERPC_ADDR32:
  4712. case elfcpp::R_POWERPC_UADDR32:
  4713. case elfcpp::R_POWERPC_ADDR16:
  4714. case elfcpp::R_POWERPC_UADDR16:
  4715. case elfcpp::R_POWERPC_ADDR16_LO:
  4716. case elfcpp::R_POWERPC_ADDR16_HI:
  4717. case elfcpp::R_POWERPC_ADDR16_HA:
  4718. ref = Symbol::ABSOLUTE_REF;
  4719. break;
  4720. case elfcpp::R_POWERPC_ADDR24:
  4721. case elfcpp::R_POWERPC_ADDR14:
  4722. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  4723. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  4724. ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
  4725. break;
  4726. case elfcpp::R_PPC64_REL64:
  4727. case elfcpp::R_POWERPC_REL32:
  4728. case elfcpp::R_PPC_LOCAL24PC:
  4729. case elfcpp::R_POWERPC_REL16:
  4730. case elfcpp::R_POWERPC_REL16_LO:
  4731. case elfcpp::R_POWERPC_REL16_HI:
  4732. case elfcpp::R_POWERPC_REL16_HA:
  4733. ref = Symbol::RELATIVE_REF;
  4734. break;
  4735. case elfcpp::R_POWERPC_REL24:
  4736. case elfcpp::R_PPC_PLTREL24:
  4737. case elfcpp::R_POWERPC_REL14:
  4738. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  4739. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  4740. ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  4741. break;
  4742. case elfcpp::R_POWERPC_GOT16:
  4743. case elfcpp::R_POWERPC_GOT16_LO:
  4744. case elfcpp::R_POWERPC_GOT16_HI:
  4745. case elfcpp::R_POWERPC_GOT16_HA:
  4746. case elfcpp::R_PPC64_GOT16_DS:
  4747. case elfcpp::R_PPC64_GOT16_LO_DS:
  4748. case elfcpp::R_PPC64_TOC16:
  4749. case elfcpp::R_PPC64_TOC16_LO:
  4750. case elfcpp::R_PPC64_TOC16_HI:
  4751. case elfcpp::R_PPC64_TOC16_HA:
  4752. case elfcpp::R_PPC64_TOC16_DS:
  4753. case elfcpp::R_PPC64_TOC16_LO_DS:
  4754. // Absolute in GOT.
  4755. ref = Symbol::ABSOLUTE_REF;
  4756. break;
  4757. case elfcpp::R_POWERPC_GOT_TPREL16:
  4758. case elfcpp::R_POWERPC_TLS:
  4759. ref = Symbol::TLS_REF;
  4760. break;
  4761. case elfcpp::R_POWERPC_COPY:
  4762. case elfcpp::R_POWERPC_GLOB_DAT:
  4763. case elfcpp::R_POWERPC_JMP_SLOT:
  4764. case elfcpp::R_POWERPC_RELATIVE:
  4765. case elfcpp::R_POWERPC_DTPMOD:
  4766. default:
  4767. // Not expected. We will give an error later.
  4768. break;
  4769. }
  4770. if (size == 64 && target->abiversion() < 2)
  4771. ref |= Symbol::FUNC_DESC_ABI;
  4772. return ref;
  4773. }
  4774. // Report an unsupported relocation against a local symbol.
  4775. template<int size, bool big_endian>
  4776. void
  4777. Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
  4778. Sized_relobj_file<size, big_endian>* object,
  4779. unsigned int r_type)
  4780. {
  4781. gold_error(_("%s: unsupported reloc %u against local symbol"),
  4782. object->name().c_str(), r_type);
  4783. }
  4784. // We are about to emit a dynamic relocation of type R_TYPE. If the
  4785. // dynamic linker does not support it, issue an error.
  4786. template<int size, bool big_endian>
  4787. void
  4788. Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
  4789. unsigned int r_type)
  4790. {
  4791. gold_assert(r_type != elfcpp::R_POWERPC_NONE);
  4792. // These are the relocation types supported by glibc for both 32-bit
  4793. // and 64-bit powerpc.
  4794. switch (r_type)
  4795. {
  4796. case elfcpp::R_POWERPC_NONE:
  4797. case elfcpp::R_POWERPC_RELATIVE:
  4798. case elfcpp::R_POWERPC_GLOB_DAT:
  4799. case elfcpp::R_POWERPC_DTPMOD:
  4800. case elfcpp::R_POWERPC_DTPREL:
  4801. case elfcpp::R_POWERPC_TPREL:
  4802. case elfcpp::R_POWERPC_JMP_SLOT:
  4803. case elfcpp::R_POWERPC_COPY:
  4804. case elfcpp::R_POWERPC_IRELATIVE:
  4805. case elfcpp::R_POWERPC_ADDR32:
  4806. case elfcpp::R_POWERPC_UADDR32:
  4807. case elfcpp::R_POWERPC_ADDR24:
  4808. case elfcpp::R_POWERPC_ADDR16:
  4809. case elfcpp::R_POWERPC_UADDR16:
  4810. case elfcpp::R_POWERPC_ADDR16_LO:
  4811. case elfcpp::R_POWERPC_ADDR16_HI:
  4812. case elfcpp::R_POWERPC_ADDR16_HA:
  4813. case elfcpp::R_POWERPC_ADDR14:
  4814. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  4815. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  4816. case elfcpp::R_POWERPC_REL32:
  4817. case elfcpp::R_POWERPC_REL24:
  4818. case elfcpp::R_POWERPC_TPREL16:
  4819. case elfcpp::R_POWERPC_TPREL16_LO:
  4820. case elfcpp::R_POWERPC_TPREL16_HI:
  4821. case elfcpp::R_POWERPC_TPREL16_HA:
  4822. return;
  4823. default:
  4824. break;
  4825. }
  4826. if (size == 64)
  4827. {
  4828. switch (r_type)
  4829. {
  4830. // These are the relocation types supported only on 64-bit.
  4831. case elfcpp::R_PPC64_ADDR64:
  4832. case elfcpp::R_PPC64_UADDR64:
  4833. case elfcpp::R_PPC64_JMP_IREL:
  4834. case elfcpp::R_PPC64_ADDR16_DS:
  4835. case elfcpp::R_PPC64_ADDR16_LO_DS:
  4836. case elfcpp::R_PPC64_ADDR16_HIGH:
  4837. case elfcpp::R_PPC64_ADDR16_HIGHA:
  4838. case elfcpp::R_PPC64_ADDR16_HIGHER:
  4839. case elfcpp::R_PPC64_ADDR16_HIGHEST:
  4840. case elfcpp::R_PPC64_ADDR16_HIGHERA:
  4841. case elfcpp::R_PPC64_ADDR16_HIGHESTA:
  4842. case elfcpp::R_PPC64_REL64:
  4843. case elfcpp::R_POWERPC_ADDR30:
  4844. case elfcpp::R_PPC64_TPREL16_DS:
  4845. case elfcpp::R_PPC64_TPREL16_LO_DS:
  4846. case elfcpp::R_PPC64_TPREL16_HIGH:
  4847. case elfcpp::R_PPC64_TPREL16_HIGHA:
  4848. case elfcpp::R_PPC64_TPREL16_HIGHER:
  4849. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  4850. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  4851. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  4852. return;
  4853. default:
  4854. break;
  4855. }
  4856. }
  4857. else
  4858. {
  4859. switch (r_type)
  4860. {
  4861. // These are the relocation types supported only on 32-bit.
  4862. // ??? glibc ld.so doesn't need to support these.
  4863. case elfcpp::R_POWERPC_DTPREL16:
  4864. case elfcpp::R_POWERPC_DTPREL16_LO:
  4865. case elfcpp::R_POWERPC_DTPREL16_HI:
  4866. case elfcpp::R_POWERPC_DTPREL16_HA:
  4867. return;
  4868. default:
  4869. break;
  4870. }
  4871. }
  4872. // This prevents us from issuing more than one error per reloc
  4873. // section. But we can still wind up issuing more than one
  4874. // error per object file.
  4875. if (this->issued_non_pic_error_)
  4876. return;
  4877. gold_assert(parameters->options().output_is_position_independent());
  4878. object->error(_("requires unsupported dynamic reloc; "
  4879. "recompile with -fPIC"));
  4880. this->issued_non_pic_error_ = true;
  4881. return;
  4882. }
  4883. // Return whether we need to make a PLT entry for a relocation of the
  4884. // given type against a STT_GNU_IFUNC symbol.
  4885. template<int size, bool big_endian>
  4886. bool
  4887. Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
  4888. Target_powerpc<size, big_endian>* target,
  4889. Sized_relobj_file<size, big_endian>* object,
  4890. unsigned int r_type,
  4891. bool report_err)
  4892. {
  4893. // In non-pic code any reference will resolve to the plt call stub
  4894. // for the ifunc symbol.
  4895. if ((size == 32 || target->abiversion() >= 2)
  4896. && !parameters->options().output_is_position_independent())
  4897. return true;
  4898. switch (r_type)
  4899. {
  4900. // Word size refs from data sections are OK, but don't need a PLT entry.
  4901. case elfcpp::R_POWERPC_ADDR32:
  4902. case elfcpp::R_POWERPC_UADDR32:
  4903. if (size == 32)
  4904. return false;
  4905. break;
  4906. case elfcpp::R_PPC64_ADDR64:
  4907. case elfcpp::R_PPC64_UADDR64:
  4908. if (size == 64)
  4909. return false;
  4910. break;
  4911. // GOT refs are good, but also don't need a PLT entry.
  4912. case elfcpp::R_POWERPC_GOT16:
  4913. case elfcpp::R_POWERPC_GOT16_LO:
  4914. case elfcpp::R_POWERPC_GOT16_HI:
  4915. case elfcpp::R_POWERPC_GOT16_HA:
  4916. case elfcpp::R_PPC64_GOT16_DS:
  4917. case elfcpp::R_PPC64_GOT16_LO_DS:
  4918. return false;
  4919. // Function calls are good, and these do need a PLT entry.
  4920. case elfcpp::R_POWERPC_ADDR24:
  4921. case elfcpp::R_POWERPC_ADDR14:
  4922. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  4923. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  4924. case elfcpp::R_POWERPC_REL24:
  4925. case elfcpp::R_PPC_PLTREL24:
  4926. case elfcpp::R_POWERPC_REL14:
  4927. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  4928. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  4929. return true;
  4930. default:
  4931. break;
  4932. }
  4933. // Anything else is a problem.
  4934. // If we are building a static executable, the libc startup function
  4935. // responsible for applying indirect function relocations is going
  4936. // to complain about the reloc type.
  4937. // If we are building a dynamic executable, we will have a text
  4938. // relocation. The dynamic loader will set the text segment
  4939. // writable and non-executable to apply text relocations. So we'll
  4940. // segfault when trying to run the indirection function to resolve
  4941. // the reloc.
  4942. if (report_err)
  4943. gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
  4944. object->name().c_str(), r_type);
  4945. return false;
  4946. }
  4947. // Scan a relocation for a local symbol.
  4948. template<int size, bool big_endian>
  4949. inline void
  4950. Target_powerpc<size, big_endian>::Scan::local(
  4951. Symbol_table* symtab,
  4952. Layout* layout,
  4953. Target_powerpc<size, big_endian>* target,
  4954. Sized_relobj_file<size, big_endian>* object,
  4955. unsigned int data_shndx,
  4956. Output_section* output_section,
  4957. const elfcpp::Rela<size, big_endian>& reloc,
  4958. unsigned int r_type,
  4959. const elfcpp::Sym<size, big_endian>& lsym,
  4960. bool is_discarded)
  4961. {
  4962. this->maybe_skip_tls_get_addr_call(r_type, NULL);
  4963. if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
  4964. || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
  4965. {
  4966. this->expect_tls_get_addr_call();
  4967. const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
  4968. if (tls_type != tls::TLSOPT_NONE)
  4969. this->skip_next_tls_get_addr_call();
  4970. }
  4971. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
  4972. || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
  4973. {
  4974. this->expect_tls_get_addr_call();
  4975. const tls::Tls_optimization tls_type = target->optimize_tls_ld();
  4976. if (tls_type != tls::TLSOPT_NONE)
  4977. this->skip_next_tls_get_addr_call();
  4978. }
  4979. Powerpc_relobj<size, big_endian>* ppc_object
  4980. = static_cast<Powerpc_relobj<size, big_endian>*>(object);
  4981. if (is_discarded)
  4982. {
  4983. if (size == 64
  4984. && data_shndx == ppc_object->opd_shndx()
  4985. && r_type == elfcpp::R_PPC64_ADDR64)
  4986. ppc_object->set_opd_discard(reloc.get_r_offset());
  4987. return;
  4988. }
  4989. // A local STT_GNU_IFUNC symbol may require a PLT entry.
  4990. bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
  4991. if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
  4992. {
  4993. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  4994. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  4995. r_type, r_sym, reloc.get_r_addend());
  4996. target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
  4997. }
  4998. switch (r_type)
  4999. {
  5000. case elfcpp::R_POWERPC_NONE:
  5001. case elfcpp::R_POWERPC_GNU_VTINHERIT:
  5002. case elfcpp::R_POWERPC_GNU_VTENTRY:
  5003. case elfcpp::R_PPC64_TOCSAVE:
  5004. case elfcpp::R_POWERPC_TLS:
  5005. break;
  5006. case elfcpp::R_PPC64_TOC:
  5007. {
  5008. Output_data_got_powerpc<size, big_endian>* got
  5009. = target->got_section(symtab, layout);
  5010. if (parameters->options().output_is_position_independent())
  5011. {
  5012. Address off = reloc.get_r_offset();
  5013. if (size == 64
  5014. && target->abiversion() < 2
  5015. && data_shndx == ppc_object->opd_shndx()
  5016. && ppc_object->get_opd_discard(off - 8))
  5017. break;
  5018. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5019. Powerpc_relobj<size, big_endian>* symobj = ppc_object;
  5020. rela_dyn->add_output_section_relative(got->output_section(),
  5021. elfcpp::R_POWERPC_RELATIVE,
  5022. output_section,
  5023. object, data_shndx, off,
  5024. symobj->toc_base_offset());
  5025. }
  5026. }
  5027. break;
  5028. case elfcpp::R_PPC64_ADDR64:
  5029. case elfcpp::R_PPC64_UADDR64:
  5030. case elfcpp::R_POWERPC_ADDR32:
  5031. case elfcpp::R_POWERPC_UADDR32:
  5032. case elfcpp::R_POWERPC_ADDR24:
  5033. case elfcpp::R_POWERPC_ADDR16:
  5034. case elfcpp::R_POWERPC_ADDR16_LO:
  5035. case elfcpp::R_POWERPC_ADDR16_HI:
  5036. case elfcpp::R_POWERPC_ADDR16_HA:
  5037. case elfcpp::R_POWERPC_UADDR16:
  5038. case elfcpp::R_PPC64_ADDR16_HIGH:
  5039. case elfcpp::R_PPC64_ADDR16_HIGHA:
  5040. case elfcpp::R_PPC64_ADDR16_HIGHER:
  5041. case elfcpp::R_PPC64_ADDR16_HIGHERA:
  5042. case elfcpp::R_PPC64_ADDR16_HIGHEST:
  5043. case elfcpp::R_PPC64_ADDR16_HIGHESTA:
  5044. case elfcpp::R_PPC64_ADDR16_DS:
  5045. case elfcpp::R_PPC64_ADDR16_LO_DS:
  5046. case elfcpp::R_POWERPC_ADDR14:
  5047. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  5048. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  5049. // If building a shared library (or a position-independent
  5050. // executable), we need to create a dynamic relocation for
  5051. // this location.
  5052. if (parameters->options().output_is_position_independent()
  5053. || (size == 64 && is_ifunc && target->abiversion() < 2))
  5054. {
  5055. Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
  5056. is_ifunc);
  5057. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  5058. if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
  5059. || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
  5060. {
  5061. unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
  5062. : elfcpp::R_POWERPC_RELATIVE);
  5063. rela_dyn->add_local_relative(object, r_sym, dynrel,
  5064. output_section, data_shndx,
  5065. reloc.get_r_offset(),
  5066. reloc.get_r_addend(), false);
  5067. }
  5068. else if (lsym.get_st_type() != elfcpp::STT_SECTION)
  5069. {
  5070. check_non_pic(object, r_type);
  5071. rela_dyn->add_local(object, r_sym, r_type, output_section,
  5072. data_shndx, reloc.get_r_offset(),
  5073. reloc.get_r_addend());
  5074. }
  5075. else
  5076. {
  5077. gold_assert(lsym.get_st_value() == 0);
  5078. unsigned int shndx = lsym.get_st_shndx();
  5079. bool is_ordinary;
  5080. shndx = object->adjust_sym_shndx(r_sym, shndx,
  5081. &is_ordinary);
  5082. if (!is_ordinary)
  5083. object->error(_("section symbol %u has bad shndx %u"),
  5084. r_sym, shndx);
  5085. else
  5086. rela_dyn->add_local_section(object, shndx, r_type,
  5087. output_section, data_shndx,
  5088. reloc.get_r_offset());
  5089. }
  5090. }
  5091. break;
  5092. case elfcpp::R_POWERPC_REL24:
  5093. case elfcpp::R_PPC_PLTREL24:
  5094. case elfcpp::R_PPC_LOCAL24PC:
  5095. case elfcpp::R_POWERPC_REL14:
  5096. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  5097. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  5098. if (!is_ifunc)
  5099. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  5100. r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
  5101. reloc.get_r_addend());
  5102. break;
  5103. case elfcpp::R_PPC64_REL64:
  5104. case elfcpp::R_POWERPC_REL32:
  5105. case elfcpp::R_POWERPC_REL16:
  5106. case elfcpp::R_POWERPC_REL16_LO:
  5107. case elfcpp::R_POWERPC_REL16_HI:
  5108. case elfcpp::R_POWERPC_REL16_HA:
  5109. case elfcpp::R_POWERPC_SECTOFF:
  5110. case elfcpp::R_POWERPC_SECTOFF_LO:
  5111. case elfcpp::R_POWERPC_SECTOFF_HI:
  5112. case elfcpp::R_POWERPC_SECTOFF_HA:
  5113. case elfcpp::R_PPC64_SECTOFF_DS:
  5114. case elfcpp::R_PPC64_SECTOFF_LO_DS:
  5115. case elfcpp::R_POWERPC_TPREL16:
  5116. case elfcpp::R_POWERPC_TPREL16_LO:
  5117. case elfcpp::R_POWERPC_TPREL16_HI:
  5118. case elfcpp::R_POWERPC_TPREL16_HA:
  5119. case elfcpp::R_PPC64_TPREL16_DS:
  5120. case elfcpp::R_PPC64_TPREL16_LO_DS:
  5121. case elfcpp::R_PPC64_TPREL16_HIGH:
  5122. case elfcpp::R_PPC64_TPREL16_HIGHA:
  5123. case elfcpp::R_PPC64_TPREL16_HIGHER:
  5124. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  5125. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  5126. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  5127. case elfcpp::R_POWERPC_DTPREL16:
  5128. case elfcpp::R_POWERPC_DTPREL16_LO:
  5129. case elfcpp::R_POWERPC_DTPREL16_HI:
  5130. case elfcpp::R_POWERPC_DTPREL16_HA:
  5131. case elfcpp::R_PPC64_DTPREL16_DS:
  5132. case elfcpp::R_PPC64_DTPREL16_LO_DS:
  5133. case elfcpp::R_PPC64_DTPREL16_HIGH:
  5134. case elfcpp::R_PPC64_DTPREL16_HIGHA:
  5135. case elfcpp::R_PPC64_DTPREL16_HIGHER:
  5136. case elfcpp::R_PPC64_DTPREL16_HIGHERA:
  5137. case elfcpp::R_PPC64_DTPREL16_HIGHEST:
  5138. case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
  5139. case elfcpp::R_PPC64_TLSGD:
  5140. case elfcpp::R_PPC64_TLSLD:
  5141. case elfcpp::R_PPC64_ADDR64_LOCAL:
  5142. break;
  5143. case elfcpp::R_POWERPC_GOT16:
  5144. case elfcpp::R_POWERPC_GOT16_LO:
  5145. case elfcpp::R_POWERPC_GOT16_HI:
  5146. case elfcpp::R_POWERPC_GOT16_HA:
  5147. case elfcpp::R_PPC64_GOT16_DS:
  5148. case elfcpp::R_PPC64_GOT16_LO_DS:
  5149. {
  5150. // The symbol requires a GOT entry.
  5151. Output_data_got_powerpc<size, big_endian>* got
  5152. = target->got_section(symtab, layout);
  5153. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  5154. if (!parameters->options().output_is_position_independent())
  5155. {
  5156. if (is_ifunc
  5157. && (size == 32 || target->abiversion() >= 2))
  5158. got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
  5159. else
  5160. got->add_local(object, r_sym, GOT_TYPE_STANDARD);
  5161. }
  5162. else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
  5163. {
  5164. // If we are generating a shared object or a pie, this
  5165. // symbol's GOT entry will be set by a dynamic relocation.
  5166. unsigned int off;
  5167. off = got->add_constant(0);
  5168. object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
  5169. Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
  5170. is_ifunc);
  5171. unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
  5172. : elfcpp::R_POWERPC_RELATIVE);
  5173. rela_dyn->add_local_relative(object, r_sym, dynrel,
  5174. got, off, 0, false);
  5175. }
  5176. }
  5177. break;
  5178. case elfcpp::R_PPC64_TOC16:
  5179. case elfcpp::R_PPC64_TOC16_LO:
  5180. case elfcpp::R_PPC64_TOC16_HI:
  5181. case elfcpp::R_PPC64_TOC16_HA:
  5182. case elfcpp::R_PPC64_TOC16_DS:
  5183. case elfcpp::R_PPC64_TOC16_LO_DS:
  5184. // We need a GOT section.
  5185. target->got_section(symtab, layout);
  5186. break;
  5187. case elfcpp::R_POWERPC_GOT_TLSGD16:
  5188. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  5189. case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
  5190. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  5191. {
  5192. const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
  5193. if (tls_type == tls::TLSOPT_NONE)
  5194. {
  5195. Output_data_got_powerpc<size, big_endian>* got
  5196. = target->got_section(symtab, layout);
  5197. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  5198. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5199. got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
  5200. rela_dyn, elfcpp::R_POWERPC_DTPMOD);
  5201. }
  5202. else if (tls_type == tls::TLSOPT_TO_LE)
  5203. {
  5204. // no GOT relocs needed for Local Exec.
  5205. }
  5206. else
  5207. gold_unreachable();
  5208. }
  5209. break;
  5210. case elfcpp::R_POWERPC_GOT_TLSLD16:
  5211. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  5212. case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
  5213. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  5214. {
  5215. const tls::Tls_optimization tls_type = target->optimize_tls_ld();
  5216. if (tls_type == tls::TLSOPT_NONE)
  5217. target->tlsld_got_offset(symtab, layout, object);
  5218. else if (tls_type == tls::TLSOPT_TO_LE)
  5219. {
  5220. // no GOT relocs needed for Local Exec.
  5221. if (parameters->options().emit_relocs())
  5222. {
  5223. Output_section* os = layout->tls_segment()->first_section();
  5224. gold_assert(os != NULL);
  5225. os->set_needs_symtab_index();
  5226. }
  5227. }
  5228. else
  5229. gold_unreachable();
  5230. }
  5231. break;
  5232. case elfcpp::R_POWERPC_GOT_DTPREL16:
  5233. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  5234. case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
  5235. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  5236. {
  5237. Output_data_got_powerpc<size, big_endian>* got
  5238. = target->got_section(symtab, layout);
  5239. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  5240. got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
  5241. }
  5242. break;
  5243. case elfcpp::R_POWERPC_GOT_TPREL16:
  5244. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  5245. case elfcpp::R_POWERPC_GOT_TPREL16_HI:
  5246. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  5247. {
  5248. const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
  5249. if (tls_type == tls::TLSOPT_NONE)
  5250. {
  5251. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  5252. if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
  5253. {
  5254. Output_data_got_powerpc<size, big_endian>* got
  5255. = target->got_section(symtab, layout);
  5256. unsigned int off = got->add_constant(0);
  5257. object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
  5258. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5259. rela_dyn->add_symbolless_local_addend(object, r_sym,
  5260. elfcpp::R_POWERPC_TPREL,
  5261. got, off, 0);
  5262. }
  5263. }
  5264. else if (tls_type == tls::TLSOPT_TO_LE)
  5265. {
  5266. // no GOT relocs needed for Local Exec.
  5267. }
  5268. else
  5269. gold_unreachable();
  5270. }
  5271. break;
  5272. default:
  5273. unsupported_reloc_local(object, r_type);
  5274. break;
  5275. }
  5276. switch (r_type)
  5277. {
  5278. case elfcpp::R_POWERPC_GOT_TLSLD16:
  5279. case elfcpp::R_POWERPC_GOT_TLSGD16:
  5280. case elfcpp::R_POWERPC_GOT_TPREL16:
  5281. case elfcpp::R_POWERPC_GOT_DTPREL16:
  5282. case elfcpp::R_POWERPC_GOT16:
  5283. case elfcpp::R_PPC64_GOT16_DS:
  5284. case elfcpp::R_PPC64_TOC16:
  5285. case elfcpp::R_PPC64_TOC16_DS:
  5286. ppc_object->set_has_small_toc_reloc();
  5287. default:
  5288. break;
  5289. }
  5290. }
  5291. // Report an unsupported relocation against a global symbol.
  5292. template<int size, bool big_endian>
  5293. void
  5294. Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
  5295. Sized_relobj_file<size, big_endian>* object,
  5296. unsigned int r_type,
  5297. Symbol* gsym)
  5298. {
  5299. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  5300. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  5301. }
  5302. // Scan a relocation for a global symbol.
  5303. template<int size, bool big_endian>
  5304. inline void
  5305. Target_powerpc<size, big_endian>::Scan::global(
  5306. Symbol_table* symtab,
  5307. Layout* layout,
  5308. Target_powerpc<size, big_endian>* target,
  5309. Sized_relobj_file<size, big_endian>* object,
  5310. unsigned int data_shndx,
  5311. Output_section* output_section,
  5312. const elfcpp::Rela<size, big_endian>& reloc,
  5313. unsigned int r_type,
  5314. Symbol* gsym)
  5315. {
  5316. if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
  5317. return;
  5318. if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
  5319. || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
  5320. {
  5321. this->expect_tls_get_addr_call();
  5322. const bool final = gsym->final_value_is_known();
  5323. const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
  5324. if (tls_type != tls::TLSOPT_NONE)
  5325. this->skip_next_tls_get_addr_call();
  5326. }
  5327. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
  5328. || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
  5329. {
  5330. this->expect_tls_get_addr_call();
  5331. const tls::Tls_optimization tls_type = target->optimize_tls_ld();
  5332. if (tls_type != tls::TLSOPT_NONE)
  5333. this->skip_next_tls_get_addr_call();
  5334. }
  5335. Powerpc_relobj<size, big_endian>* ppc_object
  5336. = static_cast<Powerpc_relobj<size, big_endian>*>(object);
  5337. // A STT_GNU_IFUNC symbol may require a PLT entry.
  5338. bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
  5339. bool pushed_ifunc = false;
  5340. if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
  5341. {
  5342. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  5343. r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
  5344. reloc.get_r_addend());
  5345. target->make_plt_entry(symtab, layout, gsym);
  5346. pushed_ifunc = true;
  5347. }
  5348. switch (r_type)
  5349. {
  5350. case elfcpp::R_POWERPC_NONE:
  5351. case elfcpp::R_POWERPC_GNU_VTINHERIT:
  5352. case elfcpp::R_POWERPC_GNU_VTENTRY:
  5353. case elfcpp::R_PPC_LOCAL24PC:
  5354. case elfcpp::R_POWERPC_TLS:
  5355. break;
  5356. case elfcpp::R_PPC64_TOC:
  5357. {
  5358. Output_data_got_powerpc<size, big_endian>* got
  5359. = target->got_section(symtab, layout);
  5360. if (parameters->options().output_is_position_independent())
  5361. {
  5362. Address off = reloc.get_r_offset();
  5363. if (size == 64
  5364. && data_shndx == ppc_object->opd_shndx()
  5365. && ppc_object->get_opd_discard(off - 8))
  5366. break;
  5367. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5368. Powerpc_relobj<size, big_endian>* symobj = ppc_object;
  5369. if (data_shndx != ppc_object->opd_shndx())
  5370. symobj = static_cast
  5371. <Powerpc_relobj<size, big_endian>*>(gsym->object());
  5372. rela_dyn->add_output_section_relative(got->output_section(),
  5373. elfcpp::R_POWERPC_RELATIVE,
  5374. output_section,
  5375. object, data_shndx, off,
  5376. symobj->toc_base_offset());
  5377. }
  5378. }
  5379. break;
  5380. case elfcpp::R_PPC64_ADDR64:
  5381. if (size == 64
  5382. && target->abiversion() < 2
  5383. && data_shndx == ppc_object->opd_shndx()
  5384. && (gsym->is_defined_in_discarded_section()
  5385. || gsym->object() != object))
  5386. {
  5387. ppc_object->set_opd_discard(reloc.get_r_offset());
  5388. break;
  5389. }
  5390. // Fall thru
  5391. case elfcpp::R_PPC64_UADDR64:
  5392. case elfcpp::R_POWERPC_ADDR32:
  5393. case elfcpp::R_POWERPC_UADDR32:
  5394. case elfcpp::R_POWERPC_ADDR24:
  5395. case elfcpp::R_POWERPC_ADDR16:
  5396. case elfcpp::R_POWERPC_ADDR16_LO:
  5397. case elfcpp::R_POWERPC_ADDR16_HI:
  5398. case elfcpp::R_POWERPC_ADDR16_HA:
  5399. case elfcpp::R_POWERPC_UADDR16:
  5400. case elfcpp::R_PPC64_ADDR16_HIGH:
  5401. case elfcpp::R_PPC64_ADDR16_HIGHA:
  5402. case elfcpp::R_PPC64_ADDR16_HIGHER:
  5403. case elfcpp::R_PPC64_ADDR16_HIGHERA:
  5404. case elfcpp::R_PPC64_ADDR16_HIGHEST:
  5405. case elfcpp::R_PPC64_ADDR16_HIGHESTA:
  5406. case elfcpp::R_PPC64_ADDR16_DS:
  5407. case elfcpp::R_PPC64_ADDR16_LO_DS:
  5408. case elfcpp::R_POWERPC_ADDR14:
  5409. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  5410. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  5411. {
  5412. // Make a PLT entry if necessary.
  5413. if (gsym->needs_plt_entry())
  5414. {
  5415. // Since this is not a PC-relative relocation, we may be
  5416. // taking the address of a function. In that case we need to
  5417. // set the entry in the dynamic symbol table to the address of
  5418. // the PLT call stub.
  5419. bool need_ifunc_plt = false;
  5420. if ((size == 32 || target->abiversion() >= 2)
  5421. && gsym->is_from_dynobj()
  5422. && !parameters->options().output_is_position_independent())
  5423. {
  5424. gsym->set_needs_dynsym_value();
  5425. need_ifunc_plt = true;
  5426. }
  5427. if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
  5428. {
  5429. target->push_branch(ppc_object, data_shndx,
  5430. reloc.get_r_offset(), r_type,
  5431. elfcpp::elf_r_sym<size>(reloc.get_r_info()),
  5432. reloc.get_r_addend());
  5433. target->make_plt_entry(symtab, layout, gsym);
  5434. }
  5435. }
  5436. // Make a dynamic relocation if necessary.
  5437. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
  5438. || (size == 64 && is_ifunc && target->abiversion() < 2))
  5439. {
  5440. if (!parameters->options().output_is_position_independent()
  5441. && gsym->may_need_copy_reloc())
  5442. {
  5443. target->copy_reloc(symtab, layout, object,
  5444. data_shndx, output_section, gsym, reloc);
  5445. }
  5446. else if ((((size == 32
  5447. && r_type == elfcpp::R_POWERPC_ADDR32)
  5448. || (size == 64
  5449. && r_type == elfcpp::R_PPC64_ADDR64
  5450. && target->abiversion() >= 2))
  5451. && gsym->can_use_relative_reloc(false)
  5452. && !(gsym->visibility() == elfcpp::STV_PROTECTED
  5453. && parameters->options().shared()))
  5454. || (size == 64
  5455. && r_type == elfcpp::R_PPC64_ADDR64
  5456. && target->abiversion() < 2
  5457. && (gsym->can_use_relative_reloc(false)
  5458. || data_shndx == ppc_object->opd_shndx())))
  5459. {
  5460. Reloc_section* rela_dyn
  5461. = target->rela_dyn_section(symtab, layout, is_ifunc);
  5462. unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
  5463. : elfcpp::R_POWERPC_RELATIVE);
  5464. rela_dyn->add_symbolless_global_addend(
  5465. gsym, dynrel, output_section, object, data_shndx,
  5466. reloc.get_r_offset(), reloc.get_r_addend());
  5467. }
  5468. else
  5469. {
  5470. Reloc_section* rela_dyn
  5471. = target->rela_dyn_section(symtab, layout, is_ifunc);
  5472. check_non_pic(object, r_type);
  5473. rela_dyn->add_global(gsym, r_type, output_section,
  5474. object, data_shndx,
  5475. reloc.get_r_offset(),
  5476. reloc.get_r_addend());
  5477. }
  5478. }
  5479. }
  5480. break;
  5481. case elfcpp::R_PPC_PLTREL24:
  5482. case elfcpp::R_POWERPC_REL24:
  5483. if (!is_ifunc)
  5484. {
  5485. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  5486. r_type,
  5487. elfcpp::elf_r_sym<size>(reloc.get_r_info()),
  5488. reloc.get_r_addend());
  5489. if (gsym->needs_plt_entry()
  5490. || (!gsym->final_value_is_known()
  5491. && (gsym->is_undefined()
  5492. || gsym->is_from_dynobj()
  5493. || gsym->is_preemptible())))
  5494. target->make_plt_entry(symtab, layout, gsym);
  5495. }
  5496. // Fall thru
  5497. case elfcpp::R_PPC64_REL64:
  5498. case elfcpp::R_POWERPC_REL32:
  5499. // Make a dynamic relocation if necessary.
  5500. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
  5501. {
  5502. if (!parameters->options().output_is_position_independent()
  5503. && gsym->may_need_copy_reloc())
  5504. {
  5505. target->copy_reloc(symtab, layout, object,
  5506. data_shndx, output_section, gsym,
  5507. reloc);
  5508. }
  5509. else
  5510. {
  5511. Reloc_section* rela_dyn
  5512. = target->rela_dyn_section(symtab, layout, is_ifunc);
  5513. check_non_pic(object, r_type);
  5514. rela_dyn->add_global(gsym, r_type, output_section, object,
  5515. data_shndx, reloc.get_r_offset(),
  5516. reloc.get_r_addend());
  5517. }
  5518. }
  5519. break;
  5520. case elfcpp::R_POWERPC_REL14:
  5521. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  5522. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  5523. if (!is_ifunc)
  5524. target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
  5525. r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
  5526. reloc.get_r_addend());
  5527. break;
  5528. case elfcpp::R_POWERPC_REL16:
  5529. case elfcpp::R_POWERPC_REL16_LO:
  5530. case elfcpp::R_POWERPC_REL16_HI:
  5531. case elfcpp::R_POWERPC_REL16_HA:
  5532. case elfcpp::R_POWERPC_SECTOFF:
  5533. case elfcpp::R_POWERPC_SECTOFF_LO:
  5534. case elfcpp::R_POWERPC_SECTOFF_HI:
  5535. case elfcpp::R_POWERPC_SECTOFF_HA:
  5536. case elfcpp::R_PPC64_SECTOFF_DS:
  5537. case elfcpp::R_PPC64_SECTOFF_LO_DS:
  5538. case elfcpp::R_POWERPC_TPREL16:
  5539. case elfcpp::R_POWERPC_TPREL16_LO:
  5540. case elfcpp::R_POWERPC_TPREL16_HI:
  5541. case elfcpp::R_POWERPC_TPREL16_HA:
  5542. case elfcpp::R_PPC64_TPREL16_DS:
  5543. case elfcpp::R_PPC64_TPREL16_LO_DS:
  5544. case elfcpp::R_PPC64_TPREL16_HIGH:
  5545. case elfcpp::R_PPC64_TPREL16_HIGHA:
  5546. case elfcpp::R_PPC64_TPREL16_HIGHER:
  5547. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  5548. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  5549. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  5550. case elfcpp::R_POWERPC_DTPREL16:
  5551. case elfcpp::R_POWERPC_DTPREL16_LO:
  5552. case elfcpp::R_POWERPC_DTPREL16_HI:
  5553. case elfcpp::R_POWERPC_DTPREL16_HA:
  5554. case elfcpp::R_PPC64_DTPREL16_DS:
  5555. case elfcpp::R_PPC64_DTPREL16_LO_DS:
  5556. case elfcpp::R_PPC64_DTPREL16_HIGH:
  5557. case elfcpp::R_PPC64_DTPREL16_HIGHA:
  5558. case elfcpp::R_PPC64_DTPREL16_HIGHER:
  5559. case elfcpp::R_PPC64_DTPREL16_HIGHERA:
  5560. case elfcpp::R_PPC64_DTPREL16_HIGHEST:
  5561. case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
  5562. case elfcpp::R_PPC64_TLSGD:
  5563. case elfcpp::R_PPC64_TLSLD:
  5564. case elfcpp::R_PPC64_ADDR64_LOCAL:
  5565. break;
  5566. case elfcpp::R_POWERPC_GOT16:
  5567. case elfcpp::R_POWERPC_GOT16_LO:
  5568. case elfcpp::R_POWERPC_GOT16_HI:
  5569. case elfcpp::R_POWERPC_GOT16_HA:
  5570. case elfcpp::R_PPC64_GOT16_DS:
  5571. case elfcpp::R_PPC64_GOT16_LO_DS:
  5572. {
  5573. // The symbol requires a GOT entry.
  5574. Output_data_got_powerpc<size, big_endian>* got;
  5575. got = target->got_section(symtab, layout);
  5576. if (gsym->final_value_is_known())
  5577. {
  5578. if (is_ifunc
  5579. && (size == 32 || target->abiversion() >= 2))
  5580. got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  5581. else
  5582. got->add_global(gsym, GOT_TYPE_STANDARD);
  5583. }
  5584. else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
  5585. {
  5586. // If we are generating a shared object or a pie, this
  5587. // symbol's GOT entry will be set by a dynamic relocation.
  5588. unsigned int off = got->add_constant(0);
  5589. gsym->set_got_offset(GOT_TYPE_STANDARD, off);
  5590. Reloc_section* rela_dyn
  5591. = target->rela_dyn_section(symtab, layout, is_ifunc);
  5592. if (gsym->can_use_relative_reloc(false)
  5593. && !((size == 32
  5594. || target->abiversion() >= 2)
  5595. && gsym->visibility() == elfcpp::STV_PROTECTED
  5596. && parameters->options().shared()))
  5597. {
  5598. unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
  5599. : elfcpp::R_POWERPC_RELATIVE);
  5600. rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
  5601. }
  5602. else
  5603. {
  5604. unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
  5605. rela_dyn->add_global(gsym, dynrel, got, off, 0);
  5606. }
  5607. }
  5608. }
  5609. break;
  5610. case elfcpp::R_PPC64_TOC16:
  5611. case elfcpp::R_PPC64_TOC16_LO:
  5612. case elfcpp::R_PPC64_TOC16_HI:
  5613. case elfcpp::R_PPC64_TOC16_HA:
  5614. case elfcpp::R_PPC64_TOC16_DS:
  5615. case elfcpp::R_PPC64_TOC16_LO_DS:
  5616. // We need a GOT section.
  5617. target->got_section(symtab, layout);
  5618. break;
  5619. case elfcpp::R_POWERPC_GOT_TLSGD16:
  5620. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  5621. case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
  5622. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  5623. {
  5624. const bool final = gsym->final_value_is_known();
  5625. const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
  5626. if (tls_type == tls::TLSOPT_NONE)
  5627. {
  5628. Output_data_got_powerpc<size, big_endian>* got
  5629. = target->got_section(symtab, layout);
  5630. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5631. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
  5632. elfcpp::R_POWERPC_DTPMOD,
  5633. elfcpp::R_POWERPC_DTPREL);
  5634. }
  5635. else if (tls_type == tls::TLSOPT_TO_IE)
  5636. {
  5637. if (!gsym->has_got_offset(GOT_TYPE_TPREL))
  5638. {
  5639. Output_data_got_powerpc<size, big_endian>* got
  5640. = target->got_section(symtab, layout);
  5641. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5642. if (gsym->is_undefined()
  5643. || gsym->is_from_dynobj())
  5644. {
  5645. got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
  5646. elfcpp::R_POWERPC_TPREL);
  5647. }
  5648. else
  5649. {
  5650. unsigned int off = got->add_constant(0);
  5651. gsym->set_got_offset(GOT_TYPE_TPREL, off);
  5652. unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
  5653. rela_dyn->add_symbolless_global_addend(gsym, dynrel,
  5654. got, off, 0);
  5655. }
  5656. }
  5657. }
  5658. else if (tls_type == tls::TLSOPT_TO_LE)
  5659. {
  5660. // no GOT relocs needed for Local Exec.
  5661. }
  5662. else
  5663. gold_unreachable();
  5664. }
  5665. break;
  5666. case elfcpp::R_POWERPC_GOT_TLSLD16:
  5667. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  5668. case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
  5669. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  5670. {
  5671. const tls::Tls_optimization tls_type = target->optimize_tls_ld();
  5672. if (tls_type == tls::TLSOPT_NONE)
  5673. target->tlsld_got_offset(symtab, layout, object);
  5674. else if (tls_type == tls::TLSOPT_TO_LE)
  5675. {
  5676. // no GOT relocs needed for Local Exec.
  5677. if (parameters->options().emit_relocs())
  5678. {
  5679. Output_section* os = layout->tls_segment()->first_section();
  5680. gold_assert(os != NULL);
  5681. os->set_needs_symtab_index();
  5682. }
  5683. }
  5684. else
  5685. gold_unreachable();
  5686. }
  5687. break;
  5688. case elfcpp::R_POWERPC_GOT_DTPREL16:
  5689. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  5690. case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
  5691. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  5692. {
  5693. Output_data_got_powerpc<size, big_endian>* got
  5694. = target->got_section(symtab, layout);
  5695. if (!gsym->final_value_is_known()
  5696. && (gsym->is_from_dynobj()
  5697. || gsym->is_undefined()
  5698. || gsym->is_preemptible()))
  5699. got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
  5700. target->rela_dyn_section(layout),
  5701. elfcpp::R_POWERPC_DTPREL);
  5702. else
  5703. got->add_global_tls(gsym, GOT_TYPE_DTPREL);
  5704. }
  5705. break;
  5706. case elfcpp::R_POWERPC_GOT_TPREL16:
  5707. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  5708. case elfcpp::R_POWERPC_GOT_TPREL16_HI:
  5709. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  5710. {
  5711. const bool final = gsym->final_value_is_known();
  5712. const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
  5713. if (tls_type == tls::TLSOPT_NONE)
  5714. {
  5715. if (!gsym->has_got_offset(GOT_TYPE_TPREL))
  5716. {
  5717. Output_data_got_powerpc<size, big_endian>* got
  5718. = target->got_section(symtab, layout);
  5719. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5720. if (gsym->is_undefined()
  5721. || gsym->is_from_dynobj())
  5722. {
  5723. got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
  5724. elfcpp::R_POWERPC_TPREL);
  5725. }
  5726. else
  5727. {
  5728. unsigned int off = got->add_constant(0);
  5729. gsym->set_got_offset(GOT_TYPE_TPREL, off);
  5730. unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
  5731. rela_dyn->add_symbolless_global_addend(gsym, dynrel,
  5732. got, off, 0);
  5733. }
  5734. }
  5735. }
  5736. else if (tls_type == tls::TLSOPT_TO_LE)
  5737. {
  5738. // no GOT relocs needed for Local Exec.
  5739. }
  5740. else
  5741. gold_unreachable();
  5742. }
  5743. break;
  5744. default:
  5745. unsupported_reloc_global(object, r_type, gsym);
  5746. break;
  5747. }
  5748. switch (r_type)
  5749. {
  5750. case elfcpp::R_POWERPC_GOT_TLSLD16:
  5751. case elfcpp::R_POWERPC_GOT_TLSGD16:
  5752. case elfcpp::R_POWERPC_GOT_TPREL16:
  5753. case elfcpp::R_POWERPC_GOT_DTPREL16:
  5754. case elfcpp::R_POWERPC_GOT16:
  5755. case elfcpp::R_PPC64_GOT16_DS:
  5756. case elfcpp::R_PPC64_TOC16:
  5757. case elfcpp::R_PPC64_TOC16_DS:
  5758. ppc_object->set_has_small_toc_reloc();
  5759. default:
  5760. break;
  5761. }
  5762. }
  5763. // Process relocations for gc.
  5764. template<int size, bool big_endian>
  5765. void
  5766. Target_powerpc<size, big_endian>::gc_process_relocs(
  5767. Symbol_table* symtab,
  5768. Layout* layout,
  5769. Sized_relobj_file<size, big_endian>* object,
  5770. unsigned int data_shndx,
  5771. unsigned int,
  5772. const unsigned char* prelocs,
  5773. size_t reloc_count,
  5774. Output_section* output_section,
  5775. bool needs_special_offset_handling,
  5776. size_t local_symbol_count,
  5777. const unsigned char* plocal_symbols)
  5778. {
  5779. typedef Target_powerpc<size, big_endian> Powerpc;
  5780. typedef typename Target_powerpc<size, big_endian>::Scan Scan;
  5781. Powerpc_relobj<size, big_endian>* ppc_object
  5782. = static_cast<Powerpc_relobj<size, big_endian>*>(object);
  5783. if (size == 64)
  5784. ppc_object->set_opd_valid();
  5785. if (size == 64 && data_shndx == ppc_object->opd_shndx())
  5786. {
  5787. typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
  5788. for (p = ppc_object->access_from_map()->begin();
  5789. p != ppc_object->access_from_map()->end();
  5790. ++p)
  5791. {
  5792. Address dst_off = p->first;
  5793. unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
  5794. typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
  5795. for (s = p->second.begin(); s != p->second.end(); ++s)
  5796. {
  5797. Relobj* src_obj = s->first;
  5798. unsigned int src_indx = s->second;
  5799. symtab->gc()->add_reference(src_obj, src_indx,
  5800. ppc_object, dst_indx);
  5801. }
  5802. p->second.clear();
  5803. }
  5804. ppc_object->access_from_map()->clear();
  5805. ppc_object->process_gc_mark(symtab);
  5806. // Don't look at .opd relocs as .opd will reference everything.
  5807. return;
  5808. }
  5809. gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
  5810. typename Target_powerpc::Relocatable_size_for_reloc>(
  5811. symtab,
  5812. layout,
  5813. this,
  5814. object,
  5815. data_shndx,
  5816. prelocs,
  5817. reloc_count,
  5818. output_section,
  5819. needs_special_offset_handling,
  5820. local_symbol_count,
  5821. plocal_symbols);
  5822. }
  5823. // Handle target specific gc actions when adding a gc reference from
  5824. // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
  5825. // and DST_OFF. For powerpc64, this adds a referenc to the code
  5826. // section of a function descriptor.
  5827. template<int size, bool big_endian>
  5828. void
  5829. Target_powerpc<size, big_endian>::do_gc_add_reference(
  5830. Symbol_table* symtab,
  5831. Relobj* src_obj,
  5832. unsigned int src_shndx,
  5833. Relobj* dst_obj,
  5834. unsigned int dst_shndx,
  5835. Address dst_off) const
  5836. {
  5837. if (size != 64 || dst_obj->is_dynamic())
  5838. return;
  5839. Powerpc_relobj<size, big_endian>* ppc_object
  5840. = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
  5841. if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
  5842. {
  5843. if (ppc_object->opd_valid())
  5844. {
  5845. dst_shndx = ppc_object->get_opd_ent(dst_off);
  5846. symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
  5847. }
  5848. else
  5849. {
  5850. // If we haven't run scan_opd_relocs, we must delay
  5851. // processing this function descriptor reference.
  5852. ppc_object->add_reference(src_obj, src_shndx, dst_off);
  5853. }
  5854. }
  5855. }
  5856. // Add any special sections for this symbol to the gc work list.
  5857. // For powerpc64, this adds the code section of a function
  5858. // descriptor.
  5859. template<int size, bool big_endian>
  5860. void
  5861. Target_powerpc<size, big_endian>::do_gc_mark_symbol(
  5862. Symbol_table* symtab,
  5863. Symbol* sym) const
  5864. {
  5865. if (size == 64)
  5866. {
  5867. Powerpc_relobj<size, big_endian>* ppc_object
  5868. = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
  5869. bool is_ordinary;
  5870. unsigned int shndx = sym->shndx(&is_ordinary);
  5871. if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
  5872. {
  5873. Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
  5874. Address dst_off = gsym->value();
  5875. if (ppc_object->opd_valid())
  5876. {
  5877. unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
  5878. symtab->gc()->worklist().push_back(Section_id(ppc_object,
  5879. dst_indx));
  5880. }
  5881. else
  5882. ppc_object->add_gc_mark(dst_off);
  5883. }
  5884. }
  5885. }
  5886. // For a symbol location in .opd, set LOC to the location of the
  5887. // function entry.
  5888. template<int size, bool big_endian>
  5889. void
  5890. Target_powerpc<size, big_endian>::do_function_location(
  5891. Symbol_location* loc) const
  5892. {
  5893. if (size == 64 && loc->shndx != 0)
  5894. {
  5895. if (loc->object->is_dynamic())
  5896. {
  5897. Powerpc_dynobj<size, big_endian>* ppc_object
  5898. = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
  5899. if (loc->shndx == ppc_object->opd_shndx())
  5900. {
  5901. Address dest_off;
  5902. Address off = loc->offset - ppc_object->opd_address();
  5903. loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
  5904. loc->offset = dest_off;
  5905. }
  5906. }
  5907. else
  5908. {
  5909. const Powerpc_relobj<size, big_endian>* ppc_object
  5910. = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
  5911. if (loc->shndx == ppc_object->opd_shndx())
  5912. {
  5913. Address dest_off;
  5914. loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
  5915. loc->offset = dest_off;
  5916. }
  5917. }
  5918. }
  5919. }
  5920. // FNOFFSET in section SHNDX in OBJECT is the start of a function
  5921. // compiled with -fsplit-stack. The function calls non-split-stack
  5922. // code. Change the function to ensure it has enough stack space to
  5923. // call some random function.
  5924. template<int size, bool big_endian>
  5925. void
  5926. Target_powerpc<size, big_endian>::do_calls_non_split(
  5927. Relobj* object,
  5928. unsigned int shndx,
  5929. section_offset_type fnoffset,
  5930. section_size_type fnsize,
  5931. unsigned char* view,
  5932. section_size_type view_size,
  5933. std::string* from,
  5934. std::string* to) const
  5935. {
  5936. // 32-bit not supported.
  5937. if (size == 32)
  5938. {
  5939. // warn
  5940. Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
  5941. view, view_size, from, to);
  5942. return;
  5943. }
  5944. // The function always starts with
  5945. // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
  5946. // addis %r12,%r1,-allocate@ha
  5947. // addi %r12,%r12,-allocate@l
  5948. // cmpld %r12,%r0
  5949. // but note that the addis or addi may be replaced with a nop
  5950. unsigned char *entry = view + fnoffset;
  5951. uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
  5952. if ((insn & 0xffff0000) == addis_2_12)
  5953. {
  5954. /* Skip ELFv2 global entry code. */
  5955. entry += 8;
  5956. insn = elfcpp::Swap<32, big_endian>::readval(entry);
  5957. }
  5958. unsigned char *pinsn = entry;
  5959. bool ok = false;
  5960. const uint32_t ld_private_ss = 0xe80d8fc0;
  5961. if (insn == ld_private_ss)
  5962. {
  5963. int32_t allocate = 0;
  5964. while (1)
  5965. {
  5966. pinsn += 4;
  5967. insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
  5968. if ((insn & 0xffff0000) == addis_12_1)
  5969. allocate += (insn & 0xffff) << 16;
  5970. else if ((insn & 0xffff0000) == addi_12_1
  5971. || (insn & 0xffff0000) == addi_12_12)
  5972. allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
  5973. else if (insn != nop)
  5974. break;
  5975. }
  5976. if (insn == cmpld_7_12_0 && pinsn == entry + 12)
  5977. {
  5978. int extra = parameters->options().split_stack_adjust_size();
  5979. allocate -= extra;
  5980. if (allocate >= 0 || extra < 0)
  5981. {
  5982. object->error(_("split-stack stack size overflow at "
  5983. "section %u offset %0zx"),
  5984. shndx, static_cast<size_t>(fnoffset));
  5985. return;
  5986. }
  5987. pinsn = entry + 4;
  5988. insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
  5989. if (insn != addis_12_1)
  5990. {
  5991. elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
  5992. pinsn += 4;
  5993. insn = addi_12_12 | (allocate & 0xffff);
  5994. if (insn != addi_12_12)
  5995. {
  5996. elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
  5997. pinsn += 4;
  5998. }
  5999. }
  6000. else
  6001. {
  6002. insn = addi_12_1 | (allocate & 0xffff);
  6003. elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
  6004. pinsn += 4;
  6005. }
  6006. if (pinsn != entry + 12)
  6007. elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
  6008. ok = true;
  6009. }
  6010. }
  6011. if (!ok)
  6012. {
  6013. if (!object->has_no_split_stack())
  6014. object->error(_("failed to match split-stack sequence at "
  6015. "section %u offset %0zx"),
  6016. shndx, static_cast<size_t>(fnoffset));
  6017. }
  6018. }
  6019. // Scan relocations for a section.
  6020. template<int size, bool big_endian>
  6021. void
  6022. Target_powerpc<size, big_endian>::scan_relocs(
  6023. Symbol_table* symtab,
  6024. Layout* layout,
  6025. Sized_relobj_file<size, big_endian>* object,
  6026. unsigned int data_shndx,
  6027. unsigned int sh_type,
  6028. const unsigned char* prelocs,
  6029. size_t reloc_count,
  6030. Output_section* output_section,
  6031. bool needs_special_offset_handling,
  6032. size_t local_symbol_count,
  6033. const unsigned char* plocal_symbols)
  6034. {
  6035. typedef Target_powerpc<size, big_endian> Powerpc;
  6036. typedef typename Target_powerpc<size, big_endian>::Scan Scan;
  6037. if (sh_type == elfcpp::SHT_REL)
  6038. {
  6039. gold_error(_("%s: unsupported REL reloc section"),
  6040. object->name().c_str());
  6041. return;
  6042. }
  6043. gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
  6044. symtab,
  6045. layout,
  6046. this,
  6047. object,
  6048. data_shndx,
  6049. prelocs,
  6050. reloc_count,
  6051. output_section,
  6052. needs_special_offset_handling,
  6053. local_symbol_count,
  6054. plocal_symbols);
  6055. }
  6056. // Functor class for processing the global symbol table.
  6057. // Removes symbols defined on discarded opd entries.
  6058. template<bool big_endian>
  6059. class Global_symbol_visitor_opd
  6060. {
  6061. public:
  6062. Global_symbol_visitor_opd()
  6063. { }
  6064. void
  6065. operator()(Sized_symbol<64>* sym)
  6066. {
  6067. if (sym->has_symtab_index()
  6068. || sym->source() != Symbol::FROM_OBJECT
  6069. || !sym->in_real_elf())
  6070. return;
  6071. if (sym->object()->is_dynamic())
  6072. return;
  6073. Powerpc_relobj<64, big_endian>* symobj
  6074. = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
  6075. if (symobj->opd_shndx() == 0)
  6076. return;
  6077. bool is_ordinary;
  6078. unsigned int shndx = sym->shndx(&is_ordinary);
  6079. if (shndx == symobj->opd_shndx()
  6080. && symobj->get_opd_discard(sym->value()))
  6081. {
  6082. sym->set_undefined();
  6083. sym->set_visibility(elfcpp::STV_DEFAULT);
  6084. sym->set_is_defined_in_discarded_section();
  6085. sym->set_symtab_index(-1U);
  6086. }
  6087. }
  6088. };
  6089. template<int size, bool big_endian>
  6090. void
  6091. Target_powerpc<size, big_endian>::define_save_restore_funcs(
  6092. Layout* layout,
  6093. Symbol_table* symtab)
  6094. {
  6095. if (size == 64)
  6096. {
  6097. Output_data_save_res<size, big_endian>* savres
  6098. = new Output_data_save_res<size, big_endian>(symtab);
  6099. this->savres_section_ = savres;
  6100. layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
  6101. elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
  6102. savres, ORDER_TEXT, false);
  6103. }
  6104. }
  6105. // Sort linker created .got section first (for the header), then input
  6106. // sections belonging to files using small model code.
  6107. template<bool big_endian>
  6108. class Sort_toc_sections
  6109. {
  6110. public:
  6111. bool
  6112. operator()(const Output_section::Input_section& is1,
  6113. const Output_section::Input_section& is2) const
  6114. {
  6115. if (!is1.is_input_section() && is2.is_input_section())
  6116. return true;
  6117. bool small1
  6118. = (is1.is_input_section()
  6119. && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
  6120. ->has_small_toc_reloc()));
  6121. bool small2
  6122. = (is2.is_input_section()
  6123. && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
  6124. ->has_small_toc_reloc()));
  6125. return small1 && !small2;
  6126. }
  6127. };
  6128. // Finalize the sections.
  6129. template<int size, bool big_endian>
  6130. void
  6131. Target_powerpc<size, big_endian>::do_finalize_sections(
  6132. Layout* layout,
  6133. const Input_objects*,
  6134. Symbol_table* symtab)
  6135. {
  6136. if (parameters->doing_static_link())
  6137. {
  6138. // At least some versions of glibc elf-init.o have a strong
  6139. // reference to __rela_iplt marker syms. A weak ref would be
  6140. // better..
  6141. if (this->iplt_ != NULL)
  6142. {
  6143. Reloc_section* rel = this->iplt_->rel_plt();
  6144. symtab->define_in_output_data("__rela_iplt_start", NULL,
  6145. Symbol_table::PREDEFINED, rel, 0, 0,
  6146. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  6147. elfcpp::STV_HIDDEN, 0, false, true);
  6148. symtab->define_in_output_data("__rela_iplt_end", NULL,
  6149. Symbol_table::PREDEFINED, rel, 0, 0,
  6150. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  6151. elfcpp::STV_HIDDEN, 0, true, true);
  6152. }
  6153. else
  6154. {
  6155. symtab->define_as_constant("__rela_iplt_start", NULL,
  6156. Symbol_table::PREDEFINED, 0, 0,
  6157. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  6158. elfcpp::STV_HIDDEN, 0, true, false);
  6159. symtab->define_as_constant("__rela_iplt_end", NULL,
  6160. Symbol_table::PREDEFINED, 0, 0,
  6161. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  6162. elfcpp::STV_HIDDEN, 0, true, false);
  6163. }
  6164. }
  6165. if (size == 64)
  6166. {
  6167. typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
  6168. symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
  6169. if (!parameters->options().relocatable())
  6170. {
  6171. this->define_save_restore_funcs(layout, symtab);
  6172. // Annoyingly, we need to make these sections now whether or
  6173. // not we need them. If we delay until do_relax then we
  6174. // need to mess with the relaxation machinery checkpointing.
  6175. this->got_section(symtab, layout);
  6176. this->make_brlt_section(layout);
  6177. if (parameters->options().toc_sort())
  6178. {
  6179. Output_section* os = this->got_->output_section();
  6180. if (os != NULL && os->input_sections().size() > 1)
  6181. std::stable_sort(os->input_sections().begin(),
  6182. os->input_sections().end(),
  6183. Sort_toc_sections<big_endian>());
  6184. }
  6185. }
  6186. }
  6187. // Fill in some more dynamic tags.
  6188. Output_data_dynamic* odyn = layout->dynamic_data();
  6189. if (odyn != NULL)
  6190. {
  6191. const Reloc_section* rel_plt = (this->plt_ == NULL
  6192. ? NULL
  6193. : this->plt_->rel_plt());
  6194. layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
  6195. this->rela_dyn_, true, size == 32);
  6196. if (size == 32)
  6197. {
  6198. if (this->got_ != NULL)
  6199. {
  6200. this->got_->finalize_data_size();
  6201. odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
  6202. this->got_, this->got_->g_o_t());
  6203. }
  6204. }
  6205. else
  6206. {
  6207. if (this->glink_ != NULL)
  6208. {
  6209. this->glink_->finalize_data_size();
  6210. odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
  6211. this->glink_,
  6212. (this->glink_->pltresolve_size
  6213. - 32));
  6214. }
  6215. }
  6216. }
  6217. // Emit any relocs we saved in an attempt to avoid generating COPY
  6218. // relocs.
  6219. if (this->copy_relocs_.any_saved_relocs())
  6220. this->copy_relocs_.emit(this->rela_dyn_section(layout));
  6221. }
  6222. // Return TRUE iff INSN is one we expect on a _LO variety toc/got
  6223. // reloc.
  6224. static bool
  6225. ok_lo_toc_insn(uint32_t insn)
  6226. {
  6227. return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
  6228. || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
  6229. || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
  6230. || (insn & (0x3f << 26)) == 36u << 26 /* stw */
  6231. || (insn & (0x3f << 26)) == 38u << 26 /* stb */
  6232. || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
  6233. || (insn & (0x3f << 26)) == 42u << 26 /* lha */
  6234. || (insn & (0x3f << 26)) == 44u << 26 /* sth */
  6235. || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
  6236. || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
  6237. || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
  6238. || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
  6239. || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
  6240. || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
  6241. || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
  6242. && (insn & 3) != 1)
  6243. || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
  6244. && ((insn & 3) == 0 || (insn & 3) == 3))
  6245. || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
  6246. }
  6247. // Return the value to use for a branch relocation.
  6248. template<int size, bool big_endian>
  6249. bool
  6250. Target_powerpc<size, big_endian>::symval_for_branch(
  6251. const Symbol_table* symtab,
  6252. const Sized_symbol<size>* gsym,
  6253. Powerpc_relobj<size, big_endian>* object,
  6254. Address *value,
  6255. unsigned int *dest_shndx)
  6256. {
  6257. if (size == 32 || this->abiversion() >= 2)
  6258. gold_unreachable();
  6259. *dest_shndx = 0;
  6260. // If the symbol is defined in an opd section, ie. is a function
  6261. // descriptor, use the function descriptor code entry address
  6262. Powerpc_relobj<size, big_endian>* symobj = object;
  6263. if (gsym != NULL
  6264. && gsym->source() != Symbol::FROM_OBJECT)
  6265. return true;
  6266. if (gsym != NULL)
  6267. symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
  6268. unsigned int shndx = symobj->opd_shndx();
  6269. if (shndx == 0)
  6270. return true;
  6271. Address opd_addr = symobj->get_output_section_offset(shndx);
  6272. if (opd_addr == invalid_address)
  6273. return true;
  6274. opd_addr += symobj->output_section_address(shndx);
  6275. if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
  6276. {
  6277. Address sec_off;
  6278. *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
  6279. if (symtab->is_section_folded(symobj, *dest_shndx))
  6280. {
  6281. Section_id folded
  6282. = symtab->icf()->get_folded_section(symobj, *dest_shndx);
  6283. symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
  6284. *dest_shndx = folded.second;
  6285. }
  6286. Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
  6287. if (sec_addr == invalid_address)
  6288. return false;
  6289. sec_addr += symobj->output_section(*dest_shndx)->address();
  6290. *value = sec_addr + sec_off;
  6291. }
  6292. return true;
  6293. }
  6294. // Perform a relocation.
  6295. template<int size, bool big_endian>
  6296. inline bool
  6297. Target_powerpc<size, big_endian>::Relocate::relocate(
  6298. const Relocate_info<size, big_endian>* relinfo,
  6299. Target_powerpc* target,
  6300. Output_section* os,
  6301. size_t relnum,
  6302. const elfcpp::Rela<size, big_endian>& rela,
  6303. unsigned int r_type,
  6304. const Sized_symbol<size>* gsym,
  6305. const Symbol_value<size>* psymval,
  6306. unsigned char* view,
  6307. Address address,
  6308. section_size_type view_size)
  6309. {
  6310. if (view == NULL)
  6311. return true;
  6312. switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
  6313. {
  6314. case Track_tls::NOT_EXPECTED:
  6315. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6316. _("__tls_get_addr call lacks marker reloc"));
  6317. break;
  6318. case Track_tls::EXPECTED:
  6319. // We have already complained.
  6320. break;
  6321. case Track_tls::SKIP:
  6322. return true;
  6323. case Track_tls::NORMAL:
  6324. break;
  6325. }
  6326. typedef Powerpc_relocate_functions<size, big_endian> Reloc;
  6327. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
  6328. Powerpc_relobj<size, big_endian>* const object
  6329. = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
  6330. Address value = 0;
  6331. bool has_stub_value = false;
  6332. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6333. if ((gsym != NULL
  6334. ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
  6335. : object->local_has_plt_offset(r_sym))
  6336. && (!psymval->is_ifunc_symbol()
  6337. || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
  6338. {
  6339. if (size == 64
  6340. && gsym != NULL
  6341. && target->abiversion() >= 2
  6342. && !parameters->options().output_is_position_independent()
  6343. && !is_branch_reloc(r_type))
  6344. {
  6345. Address off = target->glink_section()->find_global_entry(gsym);
  6346. if (off != invalid_address)
  6347. {
  6348. value = target->glink_section()->global_entry_address() + off;
  6349. has_stub_value = true;
  6350. }
  6351. }
  6352. else
  6353. {
  6354. Stub_table<size, big_endian>* stub_table
  6355. = object->stub_table(relinfo->data_shndx);
  6356. if (stub_table == NULL)
  6357. {
  6358. // This is a ref from a data section to an ifunc symbol.
  6359. if (target->stub_tables().size() != 0)
  6360. stub_table = target->stub_tables()[0];
  6361. }
  6362. if (stub_table != NULL)
  6363. {
  6364. Address off;
  6365. if (gsym != NULL)
  6366. off = stub_table->find_plt_call_entry(object, gsym, r_type,
  6367. rela.get_r_addend());
  6368. else
  6369. off = stub_table->find_plt_call_entry(object, r_sym, r_type,
  6370. rela.get_r_addend());
  6371. if (off != invalid_address)
  6372. {
  6373. value = stub_table->stub_address() + off;
  6374. has_stub_value = true;
  6375. }
  6376. }
  6377. }
  6378. // We don't care too much about bogus debug references to
  6379. // non-local functions, but otherwise there had better be a plt
  6380. // call stub or global entry stub as appropriate.
  6381. gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
  6382. }
  6383. if (r_type == elfcpp::R_POWERPC_GOT16
  6384. || r_type == elfcpp::R_POWERPC_GOT16_LO
  6385. || r_type == elfcpp::R_POWERPC_GOT16_HI
  6386. || r_type == elfcpp::R_POWERPC_GOT16_HA
  6387. || r_type == elfcpp::R_PPC64_GOT16_DS
  6388. || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
  6389. {
  6390. if (gsym != NULL)
  6391. {
  6392. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  6393. value = gsym->got_offset(GOT_TYPE_STANDARD);
  6394. }
  6395. else
  6396. {
  6397. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6398. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
  6399. value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
  6400. }
  6401. value -= target->got_section()->got_base_offset(object);
  6402. }
  6403. else if (r_type == elfcpp::R_PPC64_TOC)
  6404. {
  6405. value = (target->got_section()->output_section()->address()
  6406. + object->toc_base_offset());
  6407. }
  6408. else if (gsym != NULL
  6409. && (r_type == elfcpp::R_POWERPC_REL24
  6410. || r_type == elfcpp::R_PPC_PLTREL24)
  6411. && has_stub_value)
  6412. {
  6413. if (size == 64)
  6414. {
  6415. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  6416. Valtype* wv = reinterpret_cast<Valtype*>(view);
  6417. bool can_plt_call = false;
  6418. if (rela.get_r_offset() + 8 <= view_size)
  6419. {
  6420. Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
  6421. Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
  6422. if ((insn & 1) != 0
  6423. && (insn2 == nop
  6424. || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
  6425. {
  6426. elfcpp::Swap<32, big_endian>::
  6427. writeval(wv + 1, ld_2_1 + target->stk_toc());
  6428. can_plt_call = true;
  6429. }
  6430. }
  6431. if (!can_plt_call)
  6432. {
  6433. // If we don't have a branch and link followed by a nop,
  6434. // we can't go via the plt because there is no place to
  6435. // put a toc restoring instruction.
  6436. // Unless we know we won't be returning.
  6437. if (strcmp(gsym->name(), "__libc_start_main") == 0)
  6438. can_plt_call = true;
  6439. }
  6440. if (!can_plt_call)
  6441. {
  6442. // g++ as of 20130507 emits self-calls without a
  6443. // following nop. This is arguably wrong since we have
  6444. // conflicting information. On the one hand a global
  6445. // symbol and on the other a local call sequence, but
  6446. // don't error for this special case.
  6447. // It isn't possible to cheaply verify we have exactly
  6448. // such a call. Allow all calls to the same section.
  6449. bool ok = false;
  6450. Address code = value;
  6451. if (gsym->source() == Symbol::FROM_OBJECT
  6452. && gsym->object() == object)
  6453. {
  6454. unsigned int dest_shndx = 0;
  6455. if (target->abiversion() < 2)
  6456. {
  6457. Address addend = rela.get_r_addend();
  6458. code = psymval->value(object, addend);
  6459. target->symval_for_branch(relinfo->symtab, gsym, object,
  6460. &code, &dest_shndx);
  6461. }
  6462. bool is_ordinary;
  6463. if (dest_shndx == 0)
  6464. dest_shndx = gsym->shndx(&is_ordinary);
  6465. ok = dest_shndx == relinfo->data_shndx;
  6466. }
  6467. if (!ok)
  6468. {
  6469. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6470. _("call lacks nop, can't restore toc; "
  6471. "recompile with -fPIC"));
  6472. value = code;
  6473. }
  6474. }
  6475. }
  6476. }
  6477. else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  6478. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
  6479. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
  6480. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
  6481. {
  6482. // First instruction of a global dynamic sequence, arg setup insn.
  6483. const bool final = gsym == NULL || gsym->final_value_is_known();
  6484. const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
  6485. enum Got_type got_type = GOT_TYPE_STANDARD;
  6486. if (tls_type == tls::TLSOPT_NONE)
  6487. got_type = GOT_TYPE_TLSGD;
  6488. else if (tls_type == tls::TLSOPT_TO_IE)
  6489. got_type = GOT_TYPE_TPREL;
  6490. if (got_type != GOT_TYPE_STANDARD)
  6491. {
  6492. if (gsym != NULL)
  6493. {
  6494. gold_assert(gsym->has_got_offset(got_type));
  6495. value = gsym->got_offset(got_type);
  6496. }
  6497. else
  6498. {
  6499. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6500. gold_assert(object->local_has_got_offset(r_sym, got_type));
  6501. value = object->local_got_offset(r_sym, got_type);
  6502. }
  6503. value -= target->got_section()->got_base_offset(object);
  6504. }
  6505. if (tls_type == tls::TLSOPT_TO_IE)
  6506. {
  6507. if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  6508. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
  6509. {
  6510. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6511. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  6512. insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
  6513. if (size == 32)
  6514. insn |= 32 << 26; // lwz
  6515. else
  6516. insn |= 58 << 26; // ld
  6517. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6518. }
  6519. r_type += (elfcpp::R_POWERPC_GOT_TPREL16
  6520. - elfcpp::R_POWERPC_GOT_TLSGD16);
  6521. }
  6522. else if (tls_type == tls::TLSOPT_TO_LE)
  6523. {
  6524. if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  6525. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
  6526. {
  6527. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6528. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  6529. insn &= (1 << 26) - (1 << 21); // extract rt
  6530. if (size == 32)
  6531. insn |= addis_0_2;
  6532. else
  6533. insn |= addis_0_13;
  6534. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6535. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  6536. value = psymval->value(object, rela.get_r_addend());
  6537. }
  6538. else
  6539. {
  6540. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6541. Insn insn = nop;
  6542. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6543. r_type = elfcpp::R_POWERPC_NONE;
  6544. }
  6545. }
  6546. }
  6547. else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
  6548. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
  6549. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
  6550. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
  6551. {
  6552. // First instruction of a local dynamic sequence, arg setup insn.
  6553. const tls::Tls_optimization tls_type = target->optimize_tls_ld();
  6554. if (tls_type == tls::TLSOPT_NONE)
  6555. {
  6556. value = target->tlsld_got_offset();
  6557. value -= target->got_section()->got_base_offset(object);
  6558. }
  6559. else
  6560. {
  6561. gold_assert(tls_type == tls::TLSOPT_TO_LE);
  6562. if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
  6563. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
  6564. {
  6565. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6566. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  6567. insn &= (1 << 26) - (1 << 21); // extract rt
  6568. if (size == 32)
  6569. insn |= addis_0_2;
  6570. else
  6571. insn |= addis_0_13;
  6572. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6573. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  6574. value = dtp_offset;
  6575. }
  6576. else
  6577. {
  6578. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6579. Insn insn = nop;
  6580. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6581. r_type = elfcpp::R_POWERPC_NONE;
  6582. }
  6583. }
  6584. }
  6585. else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
  6586. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
  6587. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
  6588. || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
  6589. {
  6590. // Accesses relative to a local dynamic sequence address,
  6591. // no optimisation here.
  6592. if (gsym != NULL)
  6593. {
  6594. gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
  6595. value = gsym->got_offset(GOT_TYPE_DTPREL);
  6596. }
  6597. else
  6598. {
  6599. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6600. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
  6601. value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
  6602. }
  6603. value -= target->got_section()->got_base_offset(object);
  6604. }
  6605. else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
  6606. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
  6607. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
  6608. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
  6609. {
  6610. // First instruction of initial exec sequence.
  6611. const bool final = gsym == NULL || gsym->final_value_is_known();
  6612. const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
  6613. if (tls_type == tls::TLSOPT_NONE)
  6614. {
  6615. if (gsym != NULL)
  6616. {
  6617. gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
  6618. value = gsym->got_offset(GOT_TYPE_TPREL);
  6619. }
  6620. else
  6621. {
  6622. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6623. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
  6624. value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
  6625. }
  6626. value -= target->got_section()->got_base_offset(object);
  6627. }
  6628. else
  6629. {
  6630. gold_assert(tls_type == tls::TLSOPT_TO_LE);
  6631. if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
  6632. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
  6633. {
  6634. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6635. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  6636. insn &= (1 << 26) - (1 << 21); // extract rt from ld
  6637. if (size == 32)
  6638. insn |= addis_0_2;
  6639. else
  6640. insn |= addis_0_13;
  6641. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6642. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  6643. value = psymval->value(object, rela.get_r_addend());
  6644. }
  6645. else
  6646. {
  6647. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6648. Insn insn = nop;
  6649. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6650. r_type = elfcpp::R_POWERPC_NONE;
  6651. }
  6652. }
  6653. }
  6654. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
  6655. || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
  6656. {
  6657. // Second instruction of a global dynamic sequence,
  6658. // the __tls_get_addr call
  6659. this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
  6660. const bool final = gsym == NULL || gsym->final_value_is_known();
  6661. const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
  6662. if (tls_type != tls::TLSOPT_NONE)
  6663. {
  6664. if (tls_type == tls::TLSOPT_TO_IE)
  6665. {
  6666. Insn* iview = reinterpret_cast<Insn*>(view);
  6667. Insn insn = add_3_3_13;
  6668. if (size == 32)
  6669. insn = add_3_3_2;
  6670. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6671. r_type = elfcpp::R_POWERPC_NONE;
  6672. }
  6673. else
  6674. {
  6675. Insn* iview = reinterpret_cast<Insn*>(view);
  6676. Insn insn = addi_3_3;
  6677. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6678. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  6679. view += 2 * big_endian;
  6680. value = psymval->value(object, rela.get_r_addend());
  6681. }
  6682. this->skip_next_tls_get_addr_call();
  6683. }
  6684. }
  6685. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
  6686. || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
  6687. {
  6688. // Second instruction of a local dynamic sequence,
  6689. // the __tls_get_addr call
  6690. this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
  6691. const tls::Tls_optimization tls_type = target->optimize_tls_ld();
  6692. if (tls_type == tls::TLSOPT_TO_LE)
  6693. {
  6694. Insn* iview = reinterpret_cast<Insn*>(view);
  6695. Insn insn = addi_3_3;
  6696. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6697. this->skip_next_tls_get_addr_call();
  6698. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  6699. view += 2 * big_endian;
  6700. value = dtp_offset;
  6701. }
  6702. }
  6703. else if (r_type == elfcpp::R_POWERPC_TLS)
  6704. {
  6705. // Second instruction of an initial exec sequence
  6706. const bool final = gsym == NULL || gsym->final_value_is_known();
  6707. const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
  6708. if (tls_type == tls::TLSOPT_TO_LE)
  6709. {
  6710. Insn* iview = reinterpret_cast<Insn*>(view);
  6711. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  6712. unsigned int reg = size == 32 ? 2 : 13;
  6713. insn = at_tls_transform(insn, reg);
  6714. gold_assert(insn != 0);
  6715. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6716. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  6717. view += 2 * big_endian;
  6718. value = psymval->value(object, rela.get_r_addend());
  6719. }
  6720. }
  6721. else if (!has_stub_value)
  6722. {
  6723. Address addend = 0;
  6724. if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
  6725. addend = rela.get_r_addend();
  6726. value = psymval->value(object, addend);
  6727. if (size == 64 && is_branch_reloc(r_type))
  6728. {
  6729. if (target->abiversion() >= 2)
  6730. {
  6731. if (gsym != NULL)
  6732. value += object->ppc64_local_entry_offset(gsym);
  6733. else
  6734. value += object->ppc64_local_entry_offset(r_sym);
  6735. }
  6736. else
  6737. {
  6738. unsigned int dest_shndx;
  6739. target->symval_for_branch(relinfo->symtab, gsym, object,
  6740. &value, &dest_shndx);
  6741. }
  6742. }
  6743. Address max_branch_offset = max_branch_delta(r_type);
  6744. if (max_branch_offset != 0
  6745. && value - address + max_branch_offset >= 2 * max_branch_offset)
  6746. {
  6747. Stub_table<size, big_endian>* stub_table
  6748. = object->stub_table(relinfo->data_shndx);
  6749. if (stub_table != NULL)
  6750. {
  6751. Address off = stub_table->find_long_branch_entry(object, value);
  6752. if (off != invalid_address)
  6753. {
  6754. value = (stub_table->stub_address() + stub_table->plt_size()
  6755. + off);
  6756. has_stub_value = true;
  6757. }
  6758. }
  6759. }
  6760. }
  6761. switch (r_type)
  6762. {
  6763. case elfcpp::R_PPC64_REL64:
  6764. case elfcpp::R_POWERPC_REL32:
  6765. case elfcpp::R_POWERPC_REL24:
  6766. case elfcpp::R_PPC_PLTREL24:
  6767. case elfcpp::R_PPC_LOCAL24PC:
  6768. case elfcpp::R_POWERPC_REL16:
  6769. case elfcpp::R_POWERPC_REL16_LO:
  6770. case elfcpp::R_POWERPC_REL16_HI:
  6771. case elfcpp::R_POWERPC_REL16_HA:
  6772. case elfcpp::R_POWERPC_REL14:
  6773. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  6774. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  6775. value -= address;
  6776. break;
  6777. case elfcpp::R_PPC64_TOC16:
  6778. case elfcpp::R_PPC64_TOC16_LO:
  6779. case elfcpp::R_PPC64_TOC16_HI:
  6780. case elfcpp::R_PPC64_TOC16_HA:
  6781. case elfcpp::R_PPC64_TOC16_DS:
  6782. case elfcpp::R_PPC64_TOC16_LO_DS:
  6783. // Subtract the TOC base address.
  6784. value -= (target->got_section()->output_section()->address()
  6785. + object->toc_base_offset());
  6786. break;
  6787. case elfcpp::R_POWERPC_SECTOFF:
  6788. case elfcpp::R_POWERPC_SECTOFF_LO:
  6789. case elfcpp::R_POWERPC_SECTOFF_HI:
  6790. case elfcpp::R_POWERPC_SECTOFF_HA:
  6791. case elfcpp::R_PPC64_SECTOFF_DS:
  6792. case elfcpp::R_PPC64_SECTOFF_LO_DS:
  6793. if (os != NULL)
  6794. value -= os->address();
  6795. break;
  6796. case elfcpp::R_PPC64_TPREL16_DS:
  6797. case elfcpp::R_PPC64_TPREL16_LO_DS:
  6798. case elfcpp::R_PPC64_TPREL16_HIGH:
  6799. case elfcpp::R_PPC64_TPREL16_HIGHA:
  6800. if (size != 64)
  6801. // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
  6802. break;
  6803. case elfcpp::R_POWERPC_TPREL16:
  6804. case elfcpp::R_POWERPC_TPREL16_LO:
  6805. case elfcpp::R_POWERPC_TPREL16_HI:
  6806. case elfcpp::R_POWERPC_TPREL16_HA:
  6807. case elfcpp::R_POWERPC_TPREL:
  6808. case elfcpp::R_PPC64_TPREL16_HIGHER:
  6809. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  6810. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  6811. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  6812. // tls symbol values are relative to tls_segment()->vaddr()
  6813. value -= tp_offset;
  6814. break;
  6815. case elfcpp::R_PPC64_DTPREL16_DS:
  6816. case elfcpp::R_PPC64_DTPREL16_LO_DS:
  6817. case elfcpp::R_PPC64_DTPREL16_HIGHER:
  6818. case elfcpp::R_PPC64_DTPREL16_HIGHERA:
  6819. case elfcpp::R_PPC64_DTPREL16_HIGHEST:
  6820. case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
  6821. if (size != 64)
  6822. // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
  6823. // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
  6824. break;
  6825. case elfcpp::R_POWERPC_DTPREL16:
  6826. case elfcpp::R_POWERPC_DTPREL16_LO:
  6827. case elfcpp::R_POWERPC_DTPREL16_HI:
  6828. case elfcpp::R_POWERPC_DTPREL16_HA:
  6829. case elfcpp::R_POWERPC_DTPREL:
  6830. case elfcpp::R_PPC64_DTPREL16_HIGH:
  6831. case elfcpp::R_PPC64_DTPREL16_HIGHA:
  6832. // tls symbol values are relative to tls_segment()->vaddr()
  6833. value -= dtp_offset;
  6834. break;
  6835. case elfcpp::R_PPC64_ADDR64_LOCAL:
  6836. if (gsym != NULL)
  6837. value += object->ppc64_local_entry_offset(gsym);
  6838. else
  6839. value += object->ppc64_local_entry_offset(r_sym);
  6840. break;
  6841. default:
  6842. break;
  6843. }
  6844. Insn branch_bit = 0;
  6845. switch (r_type)
  6846. {
  6847. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  6848. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  6849. branch_bit = 1 << 21;
  6850. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  6851. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  6852. {
  6853. Insn* iview = reinterpret_cast<Insn*>(view);
  6854. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  6855. insn &= ~(1 << 21);
  6856. insn |= branch_bit;
  6857. if (this->is_isa_v2)
  6858. {
  6859. // Set 'a' bit. This is 0b00010 in BO field for branch
  6860. // on CR(BI) insns (BO == 001at or 011at), and 0b01000
  6861. // for branch on CTR insns (BO == 1a00t or 1a01t).
  6862. if ((insn & (0x14 << 21)) == (0x04 << 21))
  6863. insn |= 0x02 << 21;
  6864. else if ((insn & (0x14 << 21)) == (0x10 << 21))
  6865. insn |= 0x08 << 21;
  6866. else
  6867. break;
  6868. }
  6869. else
  6870. {
  6871. // Invert 'y' bit if not the default.
  6872. if (static_cast<Signed_address>(value) < 0)
  6873. insn ^= 1 << 21;
  6874. }
  6875. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6876. }
  6877. break;
  6878. default:
  6879. break;
  6880. }
  6881. if (size == 64)
  6882. {
  6883. // Multi-instruction sequences that access the TOC can be
  6884. // optimized, eg. addis ra,r2,0; addi rb,ra,x;
  6885. // to nop; addi rb,r2,x;
  6886. switch (r_type)
  6887. {
  6888. default:
  6889. break;
  6890. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  6891. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  6892. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  6893. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  6894. case elfcpp::R_POWERPC_GOT16_HA:
  6895. case elfcpp::R_PPC64_TOC16_HA:
  6896. if (parameters->options().toc_optimize())
  6897. {
  6898. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6899. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  6900. if ((insn & ((0x3f << 26) | 0x1f << 16))
  6901. != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
  6902. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6903. _("toc optimization is not supported "
  6904. "for %#08x instruction"), insn);
  6905. else if (value + 0x8000 < 0x10000)
  6906. {
  6907. elfcpp::Swap<32, big_endian>::writeval(iview, nop);
  6908. return true;
  6909. }
  6910. }
  6911. break;
  6912. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  6913. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  6914. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  6915. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  6916. case elfcpp::R_POWERPC_GOT16_LO:
  6917. case elfcpp::R_PPC64_GOT16_LO_DS:
  6918. case elfcpp::R_PPC64_TOC16_LO:
  6919. case elfcpp::R_PPC64_TOC16_LO_DS:
  6920. if (parameters->options().toc_optimize())
  6921. {
  6922. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  6923. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  6924. if (!ok_lo_toc_insn(insn))
  6925. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6926. _("toc optimization is not supported "
  6927. "for %#08x instruction"), insn);
  6928. else if (value + 0x8000 < 0x10000)
  6929. {
  6930. if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
  6931. {
  6932. // Transform addic to addi when we change reg.
  6933. insn &= ~((0x3f << 26) | (0x1f << 16));
  6934. insn |= (14u << 26) | (2 << 16);
  6935. }
  6936. else
  6937. {
  6938. insn &= ~(0x1f << 16);
  6939. insn |= 2 << 16;
  6940. }
  6941. elfcpp::Swap<32, big_endian>::writeval(iview, insn);
  6942. }
  6943. }
  6944. break;
  6945. }
  6946. }
  6947. typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
  6948. elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
  6949. switch (r_type)
  6950. {
  6951. case elfcpp::R_POWERPC_ADDR32:
  6952. case elfcpp::R_POWERPC_UADDR32:
  6953. if (size == 64)
  6954. overflow = Reloc::CHECK_BITFIELD;
  6955. break;
  6956. case elfcpp::R_POWERPC_REL32:
  6957. if (size == 64)
  6958. overflow = Reloc::CHECK_SIGNED;
  6959. break;
  6960. case elfcpp::R_POWERPC_UADDR16:
  6961. overflow = Reloc::CHECK_BITFIELD;
  6962. break;
  6963. case elfcpp::R_POWERPC_ADDR16:
  6964. // We really should have three separate relocations,
  6965. // one for 16-bit data, one for insns with 16-bit signed fields,
  6966. // and one for insns with 16-bit unsigned fields.
  6967. overflow = Reloc::CHECK_BITFIELD;
  6968. if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
  6969. overflow = Reloc::CHECK_LOW_INSN;
  6970. break;
  6971. case elfcpp::R_POWERPC_ADDR16_HI:
  6972. case elfcpp::R_POWERPC_ADDR16_HA:
  6973. case elfcpp::R_POWERPC_GOT16_HI:
  6974. case elfcpp::R_POWERPC_GOT16_HA:
  6975. case elfcpp::R_POWERPC_PLT16_HI:
  6976. case elfcpp::R_POWERPC_PLT16_HA:
  6977. case elfcpp::R_POWERPC_SECTOFF_HI:
  6978. case elfcpp::R_POWERPC_SECTOFF_HA:
  6979. case elfcpp::R_PPC64_TOC16_HI:
  6980. case elfcpp::R_PPC64_TOC16_HA:
  6981. case elfcpp::R_PPC64_PLTGOT16_HI:
  6982. case elfcpp::R_PPC64_PLTGOT16_HA:
  6983. case elfcpp::R_POWERPC_TPREL16_HI:
  6984. case elfcpp::R_POWERPC_TPREL16_HA:
  6985. case elfcpp::R_POWERPC_DTPREL16_HI:
  6986. case elfcpp::R_POWERPC_DTPREL16_HA:
  6987. case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
  6988. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  6989. case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
  6990. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  6991. case elfcpp::R_POWERPC_GOT_TPREL16_HI:
  6992. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  6993. case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
  6994. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  6995. case elfcpp::R_POWERPC_REL16_HI:
  6996. case elfcpp::R_POWERPC_REL16_HA:
  6997. if (size != 32)
  6998. overflow = Reloc::CHECK_HIGH_INSN;
  6999. break;
  7000. case elfcpp::R_POWERPC_REL16:
  7001. case elfcpp::R_PPC64_TOC16:
  7002. case elfcpp::R_POWERPC_GOT16:
  7003. case elfcpp::R_POWERPC_SECTOFF:
  7004. case elfcpp::R_POWERPC_TPREL16:
  7005. case elfcpp::R_POWERPC_DTPREL16:
  7006. case elfcpp::R_POWERPC_GOT_TLSGD16:
  7007. case elfcpp::R_POWERPC_GOT_TLSLD16:
  7008. case elfcpp::R_POWERPC_GOT_TPREL16:
  7009. case elfcpp::R_POWERPC_GOT_DTPREL16:
  7010. overflow = Reloc::CHECK_LOW_INSN;
  7011. break;
  7012. case elfcpp::R_POWERPC_ADDR24:
  7013. case elfcpp::R_POWERPC_ADDR14:
  7014. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  7015. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  7016. case elfcpp::R_PPC64_ADDR16_DS:
  7017. case elfcpp::R_POWERPC_REL24:
  7018. case elfcpp::R_PPC_PLTREL24:
  7019. case elfcpp::R_PPC_LOCAL24PC:
  7020. case elfcpp::R_PPC64_TPREL16_DS:
  7021. case elfcpp::R_PPC64_DTPREL16_DS:
  7022. case elfcpp::R_PPC64_TOC16_DS:
  7023. case elfcpp::R_PPC64_GOT16_DS:
  7024. case elfcpp::R_PPC64_SECTOFF_DS:
  7025. case elfcpp::R_POWERPC_REL14:
  7026. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  7027. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  7028. overflow = Reloc::CHECK_SIGNED;
  7029. break;
  7030. }
  7031. if (overflow == Reloc::CHECK_LOW_INSN
  7032. || overflow == Reloc::CHECK_HIGH_INSN)
  7033. {
  7034. Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
  7035. Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
  7036. if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
  7037. overflow = Reloc::CHECK_BITFIELD;
  7038. else if (overflow == Reloc::CHECK_LOW_INSN
  7039. ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
  7040. || (insn & (0x3f << 26)) == 24u << 26 /* ori */
  7041. || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
  7042. : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
  7043. || (insn & (0x3f << 26)) == 25u << 26 /* oris */
  7044. || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
  7045. overflow = Reloc::CHECK_UNSIGNED;
  7046. else
  7047. overflow = Reloc::CHECK_SIGNED;
  7048. }
  7049. typename Powerpc_relocate_functions<size, big_endian>::Status status
  7050. = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
  7051. switch (r_type)
  7052. {
  7053. case elfcpp::R_POWERPC_NONE:
  7054. case elfcpp::R_POWERPC_TLS:
  7055. case elfcpp::R_POWERPC_GNU_VTINHERIT:
  7056. case elfcpp::R_POWERPC_GNU_VTENTRY:
  7057. break;
  7058. case elfcpp::R_PPC64_ADDR64:
  7059. case elfcpp::R_PPC64_REL64:
  7060. case elfcpp::R_PPC64_TOC:
  7061. case elfcpp::R_PPC64_ADDR64_LOCAL:
  7062. Reloc::addr64(view, value);
  7063. break;
  7064. case elfcpp::R_POWERPC_TPREL:
  7065. case elfcpp::R_POWERPC_DTPREL:
  7066. if (size == 64)
  7067. Reloc::addr64(view, value);
  7068. else
  7069. status = Reloc::addr32(view, value, overflow);
  7070. break;
  7071. case elfcpp::R_PPC64_UADDR64:
  7072. Reloc::addr64_u(view, value);
  7073. break;
  7074. case elfcpp::R_POWERPC_ADDR32:
  7075. status = Reloc::addr32(view, value, overflow);
  7076. break;
  7077. case elfcpp::R_POWERPC_REL32:
  7078. case elfcpp::R_POWERPC_UADDR32:
  7079. status = Reloc::addr32_u(view, value, overflow);
  7080. break;
  7081. case elfcpp::R_POWERPC_ADDR24:
  7082. case elfcpp::R_POWERPC_REL24:
  7083. case elfcpp::R_PPC_PLTREL24:
  7084. case elfcpp::R_PPC_LOCAL24PC:
  7085. status = Reloc::addr24(view, value, overflow);
  7086. break;
  7087. case elfcpp::R_POWERPC_GOT_DTPREL16:
  7088. case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
  7089. case elfcpp::R_POWERPC_GOT_TPREL16:
  7090. case elfcpp::R_POWERPC_GOT_TPREL16_LO:
  7091. if (size == 64)
  7092. {
  7093. // On ppc64 these are all ds form
  7094. status = Reloc::addr16_ds(view, value, overflow);
  7095. break;
  7096. }
  7097. case elfcpp::R_POWERPC_ADDR16:
  7098. case elfcpp::R_POWERPC_REL16:
  7099. case elfcpp::R_PPC64_TOC16:
  7100. case elfcpp::R_POWERPC_GOT16:
  7101. case elfcpp::R_POWERPC_SECTOFF:
  7102. case elfcpp::R_POWERPC_TPREL16:
  7103. case elfcpp::R_POWERPC_DTPREL16:
  7104. case elfcpp::R_POWERPC_GOT_TLSGD16:
  7105. case elfcpp::R_POWERPC_GOT_TLSLD16:
  7106. case elfcpp::R_POWERPC_ADDR16_LO:
  7107. case elfcpp::R_POWERPC_REL16_LO:
  7108. case elfcpp::R_PPC64_TOC16_LO:
  7109. case elfcpp::R_POWERPC_GOT16_LO:
  7110. case elfcpp::R_POWERPC_SECTOFF_LO:
  7111. case elfcpp::R_POWERPC_TPREL16_LO:
  7112. case elfcpp::R_POWERPC_DTPREL16_LO:
  7113. case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
  7114. case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
  7115. status = Reloc::addr16(view, value, overflow);
  7116. break;
  7117. case elfcpp::R_POWERPC_UADDR16:
  7118. status = Reloc::addr16_u(view, value, overflow);
  7119. break;
  7120. case elfcpp::R_PPC64_ADDR16_HIGH:
  7121. case elfcpp::R_PPC64_TPREL16_HIGH:
  7122. case elfcpp::R_PPC64_DTPREL16_HIGH:
  7123. if (size == 32)
  7124. // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
  7125. goto unsupp;
  7126. case elfcpp::R_POWERPC_ADDR16_HI:
  7127. case elfcpp::R_POWERPC_REL16_HI:
  7128. case elfcpp::R_PPC64_TOC16_HI:
  7129. case elfcpp::R_POWERPC_GOT16_HI:
  7130. case elfcpp::R_POWERPC_SECTOFF_HI:
  7131. case elfcpp::R_POWERPC_TPREL16_HI:
  7132. case elfcpp::R_POWERPC_DTPREL16_HI:
  7133. case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
  7134. case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
  7135. case elfcpp::R_POWERPC_GOT_TPREL16_HI:
  7136. case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
  7137. Reloc::addr16_hi(view, value);
  7138. break;
  7139. case elfcpp::R_PPC64_ADDR16_HIGHA:
  7140. case elfcpp::R_PPC64_TPREL16_HIGHA:
  7141. case elfcpp::R_PPC64_DTPREL16_HIGHA:
  7142. if (size == 32)
  7143. // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
  7144. goto unsupp;
  7145. case elfcpp::R_POWERPC_ADDR16_HA:
  7146. case elfcpp::R_POWERPC_REL16_HA:
  7147. case elfcpp::R_PPC64_TOC16_HA:
  7148. case elfcpp::R_POWERPC_GOT16_HA:
  7149. case elfcpp::R_POWERPC_SECTOFF_HA:
  7150. case elfcpp::R_POWERPC_TPREL16_HA:
  7151. case elfcpp::R_POWERPC_DTPREL16_HA:
  7152. case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
  7153. case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
  7154. case elfcpp::R_POWERPC_GOT_TPREL16_HA:
  7155. case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
  7156. Reloc::addr16_ha(view, value);
  7157. break;
  7158. case elfcpp::R_PPC64_DTPREL16_HIGHER:
  7159. if (size == 32)
  7160. // R_PPC_EMB_NADDR16_LO
  7161. goto unsupp;
  7162. case elfcpp::R_PPC64_ADDR16_HIGHER:
  7163. case elfcpp::R_PPC64_TPREL16_HIGHER:
  7164. Reloc::addr16_hi2(view, value);
  7165. break;
  7166. case elfcpp::R_PPC64_DTPREL16_HIGHERA:
  7167. if (size == 32)
  7168. // R_PPC_EMB_NADDR16_HI
  7169. goto unsupp;
  7170. case elfcpp::R_PPC64_ADDR16_HIGHERA:
  7171. case elfcpp::R_PPC64_TPREL16_HIGHERA:
  7172. Reloc::addr16_ha2(view, value);
  7173. break;
  7174. case elfcpp::R_PPC64_DTPREL16_HIGHEST:
  7175. if (size == 32)
  7176. // R_PPC_EMB_NADDR16_HA
  7177. goto unsupp;
  7178. case elfcpp::R_PPC64_ADDR16_HIGHEST:
  7179. case elfcpp::R_PPC64_TPREL16_HIGHEST:
  7180. Reloc::addr16_hi3(view, value);
  7181. break;
  7182. case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
  7183. if (size == 32)
  7184. // R_PPC_EMB_SDAI16
  7185. goto unsupp;
  7186. case elfcpp::R_PPC64_ADDR16_HIGHESTA:
  7187. case elfcpp::R_PPC64_TPREL16_HIGHESTA:
  7188. Reloc::addr16_ha3(view, value);
  7189. break;
  7190. case elfcpp::R_PPC64_DTPREL16_DS:
  7191. case elfcpp::R_PPC64_DTPREL16_LO_DS:
  7192. if (size == 32)
  7193. // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
  7194. goto unsupp;
  7195. case elfcpp::R_PPC64_TPREL16_DS:
  7196. case elfcpp::R_PPC64_TPREL16_LO_DS:
  7197. if (size == 32)
  7198. // R_PPC_TLSGD, R_PPC_TLSLD
  7199. break;
  7200. case elfcpp::R_PPC64_ADDR16_DS:
  7201. case elfcpp::R_PPC64_ADDR16_LO_DS:
  7202. case elfcpp::R_PPC64_TOC16_DS:
  7203. case elfcpp::R_PPC64_TOC16_LO_DS:
  7204. case elfcpp::R_PPC64_GOT16_DS:
  7205. case elfcpp::R_PPC64_GOT16_LO_DS:
  7206. case elfcpp::R_PPC64_SECTOFF_DS:
  7207. case elfcpp::R_PPC64_SECTOFF_LO_DS:
  7208. status = Reloc::addr16_ds(view, value, overflow);
  7209. break;
  7210. case elfcpp::R_POWERPC_ADDR14:
  7211. case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
  7212. case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
  7213. case elfcpp::R_POWERPC_REL14:
  7214. case elfcpp::R_POWERPC_REL14_BRTAKEN:
  7215. case elfcpp::R_POWERPC_REL14_BRNTAKEN:
  7216. status = Reloc::addr14(view, value, overflow);
  7217. break;
  7218. case elfcpp::R_POWERPC_COPY:
  7219. case elfcpp::R_POWERPC_GLOB_DAT:
  7220. case elfcpp::R_POWERPC_JMP_SLOT:
  7221. case elfcpp::R_POWERPC_RELATIVE:
  7222. case elfcpp::R_POWERPC_DTPMOD:
  7223. case elfcpp::R_PPC64_JMP_IREL:
  7224. case elfcpp::R_POWERPC_IRELATIVE:
  7225. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  7226. _("unexpected reloc %u in object file"),
  7227. r_type);
  7228. break;
  7229. case elfcpp::R_PPC_EMB_SDA21:
  7230. if (size == 32)
  7231. goto unsupp;
  7232. else
  7233. {
  7234. // R_PPC64_TOCSAVE. For the time being this can be ignored.
  7235. }
  7236. break;
  7237. case elfcpp::R_PPC_EMB_SDA2I16:
  7238. case elfcpp::R_PPC_EMB_SDA2REL:
  7239. if (size == 32)
  7240. goto unsupp;
  7241. // R_PPC64_TLSGD, R_PPC64_TLSLD
  7242. break;
  7243. case elfcpp::R_POWERPC_PLT32:
  7244. case elfcpp::R_POWERPC_PLTREL32:
  7245. case elfcpp::R_POWERPC_PLT16_LO:
  7246. case elfcpp::R_POWERPC_PLT16_HI:
  7247. case elfcpp::R_POWERPC_PLT16_HA:
  7248. case elfcpp::R_PPC_SDAREL16:
  7249. case elfcpp::R_POWERPC_ADDR30:
  7250. case elfcpp::R_PPC64_PLT64:
  7251. case elfcpp::R_PPC64_PLTREL64:
  7252. case elfcpp::R_PPC64_PLTGOT16:
  7253. case elfcpp::R_PPC64_PLTGOT16_LO:
  7254. case elfcpp::R_PPC64_PLTGOT16_HI:
  7255. case elfcpp::R_PPC64_PLTGOT16_HA:
  7256. case elfcpp::R_PPC64_PLT16_LO_DS:
  7257. case elfcpp::R_PPC64_PLTGOT16_DS:
  7258. case elfcpp::R_PPC64_PLTGOT16_LO_DS:
  7259. case elfcpp::R_PPC_EMB_RELSDA:
  7260. case elfcpp::R_PPC_TOC16:
  7261. default:
  7262. unsupp:
  7263. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  7264. _("unsupported reloc %u"),
  7265. r_type);
  7266. break;
  7267. }
  7268. if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
  7269. && (has_stub_value
  7270. || !(gsym != NULL
  7271. && gsym->is_undefined()
  7272. && is_branch_reloc(r_type))))
  7273. {
  7274. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  7275. _("relocation overflow"));
  7276. if (has_stub_value)
  7277. gold_info(_("try relinking with a smaller --stub-group-size"));
  7278. }
  7279. return true;
  7280. }
  7281. // Relocate section data.
  7282. template<int size, bool big_endian>
  7283. void
  7284. Target_powerpc<size, big_endian>::relocate_section(
  7285. const Relocate_info<size, big_endian>* relinfo,
  7286. unsigned int sh_type,
  7287. const unsigned char* prelocs,
  7288. size_t reloc_count,
  7289. Output_section* output_section,
  7290. bool needs_special_offset_handling,
  7291. unsigned char* view,
  7292. Address address,
  7293. section_size_type view_size,
  7294. const Reloc_symbol_changes* reloc_symbol_changes)
  7295. {
  7296. typedef Target_powerpc<size, big_endian> Powerpc;
  7297. typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
  7298. typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
  7299. Powerpc_comdat_behavior;
  7300. gold_assert(sh_type == elfcpp::SHT_RELA);
  7301. gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
  7302. Powerpc_relocate, Powerpc_comdat_behavior>(
  7303. relinfo,
  7304. this,
  7305. prelocs,
  7306. reloc_count,
  7307. output_section,
  7308. needs_special_offset_handling,
  7309. view,
  7310. address,
  7311. view_size,
  7312. reloc_symbol_changes);
  7313. }
  7314. class Powerpc_scan_relocatable_reloc
  7315. {
  7316. public:
  7317. // Return the strategy to use for a local symbol which is not a
  7318. // section symbol, given the relocation type.
  7319. inline Relocatable_relocs::Reloc_strategy
  7320. local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
  7321. {
  7322. if (r_type == 0 && r_sym == 0)
  7323. return Relocatable_relocs::RELOC_DISCARD;
  7324. return Relocatable_relocs::RELOC_COPY;
  7325. }
  7326. // Return the strategy to use for a local symbol which is a section
  7327. // symbol, given the relocation type.
  7328. inline Relocatable_relocs::Reloc_strategy
  7329. local_section_strategy(unsigned int, Relobj*)
  7330. {
  7331. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  7332. }
  7333. // Return the strategy to use for a global symbol, given the
  7334. // relocation type, the object, and the symbol index.
  7335. inline Relocatable_relocs::Reloc_strategy
  7336. global_strategy(unsigned int r_type, Relobj*, unsigned int)
  7337. {
  7338. if (r_type == elfcpp::R_PPC_PLTREL24)
  7339. return Relocatable_relocs::RELOC_SPECIAL;
  7340. return Relocatable_relocs::RELOC_COPY;
  7341. }
  7342. };
  7343. // Scan the relocs during a relocatable link.
  7344. template<int size, bool big_endian>
  7345. void
  7346. Target_powerpc<size, big_endian>::scan_relocatable_relocs(
  7347. Symbol_table* symtab,
  7348. Layout* layout,
  7349. Sized_relobj_file<size, big_endian>* object,
  7350. unsigned int data_shndx,
  7351. unsigned int sh_type,
  7352. const unsigned char* prelocs,
  7353. size_t reloc_count,
  7354. Output_section* output_section,
  7355. bool needs_special_offset_handling,
  7356. size_t local_symbol_count,
  7357. const unsigned char* plocal_symbols,
  7358. Relocatable_relocs* rr)
  7359. {
  7360. gold_assert(sh_type == elfcpp::SHT_RELA);
  7361. gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
  7362. Powerpc_scan_relocatable_reloc>(
  7363. symtab,
  7364. layout,
  7365. object,
  7366. data_shndx,
  7367. prelocs,
  7368. reloc_count,
  7369. output_section,
  7370. needs_special_offset_handling,
  7371. local_symbol_count,
  7372. plocal_symbols,
  7373. rr);
  7374. }
  7375. // Emit relocations for a section.
  7376. // This is a modified version of the function by the same name in
  7377. // target-reloc.h. Using relocate_special_relocatable for
  7378. // R_PPC_PLTREL24 would require duplication of the entire body of the
  7379. // loop, so we may as well duplicate the whole thing.
  7380. template<int size, bool big_endian>
  7381. void
  7382. Target_powerpc<size, big_endian>::relocate_relocs(
  7383. const Relocate_info<size, big_endian>* relinfo,
  7384. unsigned int sh_type,
  7385. const unsigned char* prelocs,
  7386. size_t reloc_count,
  7387. Output_section* output_section,
  7388. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  7389. const Relocatable_relocs* rr,
  7390. unsigned char*,
  7391. Address view_address,
  7392. section_size_type,
  7393. unsigned char* reloc_view,
  7394. section_size_type reloc_view_size)
  7395. {
  7396. gold_assert(sh_type == elfcpp::SHT_RELA);
  7397. typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
  7398. Reltype;
  7399. typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
  7400. Reltype_write;
  7401. const int reloc_size
  7402. = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
  7403. Powerpc_relobj<size, big_endian>* const object
  7404. = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
  7405. const unsigned int local_count = object->local_symbol_count();
  7406. unsigned int got2_shndx = object->got2_shndx();
  7407. Address got2_addend = 0;
  7408. if (got2_shndx != 0)
  7409. {
  7410. got2_addend = object->get_output_section_offset(got2_shndx);
  7411. gold_assert(got2_addend != invalid_address);
  7412. }
  7413. unsigned char* pwrite = reloc_view;
  7414. bool zap_next = false;
  7415. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  7416. {
  7417. Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
  7418. if (strategy == Relocatable_relocs::RELOC_DISCARD)
  7419. continue;
  7420. Reltype reloc(prelocs);
  7421. Reltype_write reloc_write(pwrite);
  7422. Address offset = reloc.get_r_offset();
  7423. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  7424. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  7425. unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  7426. const unsigned int orig_r_sym = r_sym;
  7427. typename elfcpp::Elf_types<size>::Elf_Swxword addend
  7428. = reloc.get_r_addend();
  7429. const Symbol* gsym = NULL;
  7430. if (zap_next)
  7431. {
  7432. // We could arrange to discard these and other relocs for
  7433. // tls optimised sequences in the strategy methods, but for
  7434. // now do as BFD ld does.
  7435. r_type = elfcpp::R_POWERPC_NONE;
  7436. zap_next = false;
  7437. }
  7438. // Get the new symbol index.
  7439. Output_section* os = NULL;
  7440. if (r_sym < local_count)
  7441. {
  7442. switch (strategy)
  7443. {
  7444. case Relocatable_relocs::RELOC_COPY:
  7445. case Relocatable_relocs::RELOC_SPECIAL:
  7446. if (r_sym != 0)
  7447. {
  7448. r_sym = object->symtab_index(r_sym);
  7449. gold_assert(r_sym != -1U);
  7450. }
  7451. break;
  7452. case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
  7453. {
  7454. // We are adjusting a section symbol. We need to find
  7455. // the symbol table index of the section symbol for
  7456. // the output section corresponding to input section
  7457. // in which this symbol is defined.
  7458. gold_assert(r_sym < local_count);
  7459. bool is_ordinary;
  7460. unsigned int shndx =
  7461. object->local_symbol_input_shndx(r_sym, &is_ordinary);
  7462. gold_assert(is_ordinary);
  7463. os = object->output_section(shndx);
  7464. gold_assert(os != NULL);
  7465. gold_assert(os->needs_symtab_index());
  7466. r_sym = os->symtab_index();
  7467. }
  7468. break;
  7469. default:
  7470. gold_unreachable();
  7471. }
  7472. }
  7473. else
  7474. {
  7475. gsym = object->global_symbol(r_sym);
  7476. gold_assert(gsym != NULL);
  7477. if (gsym->is_forwarder())
  7478. gsym = relinfo->symtab->resolve_forwards(gsym);
  7479. gold_assert(gsym->has_symtab_index());
  7480. r_sym = gsym->symtab_index();
  7481. }
  7482. // Get the new offset--the location in the output section where
  7483. // this relocation should be applied.
  7484. if (static_cast<Address>(offset_in_output_section) != invalid_address)
  7485. offset += offset_in_output_section;
  7486. else
  7487. {
  7488. section_offset_type sot_offset =
  7489. convert_types<section_offset_type, Address>(offset);
  7490. section_offset_type new_sot_offset =
  7491. output_section->output_offset(object, relinfo->data_shndx,
  7492. sot_offset);
  7493. gold_assert(new_sot_offset != -1);
  7494. offset = new_sot_offset;
  7495. }
  7496. // In an object file, r_offset is an offset within the section.
  7497. // In an executable or dynamic object, generated by
  7498. // --emit-relocs, r_offset is an absolute address.
  7499. if (!parameters->options().relocatable())
  7500. {
  7501. offset += view_address;
  7502. if (static_cast<Address>(offset_in_output_section) != invalid_address)
  7503. offset -= offset_in_output_section;
  7504. }
  7505. // Handle the reloc addend based on the strategy.
  7506. if (strategy == Relocatable_relocs::RELOC_COPY)
  7507. ;
  7508. else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
  7509. {
  7510. const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
  7511. gold_assert(os != NULL);
  7512. addend = psymval->value(object, addend) - os->address();
  7513. }
  7514. else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
  7515. {
  7516. if (addend >= 32768)
  7517. addend += got2_addend;
  7518. }
  7519. else
  7520. gold_unreachable();
  7521. if (!parameters->options().relocatable())
  7522. {
  7523. if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  7524. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
  7525. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
  7526. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
  7527. {
  7528. // First instruction of a global dynamic sequence,
  7529. // arg setup insn.
  7530. const bool final = gsym == NULL || gsym->final_value_is_known();
  7531. switch (this->optimize_tls_gd(final))
  7532. {
  7533. case tls::TLSOPT_TO_IE:
  7534. r_type += (elfcpp::R_POWERPC_GOT_TPREL16
  7535. - elfcpp::R_POWERPC_GOT_TLSGD16);
  7536. break;
  7537. case tls::TLSOPT_TO_LE:
  7538. if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
  7539. || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
  7540. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  7541. else
  7542. {
  7543. r_type = elfcpp::R_POWERPC_NONE;
  7544. offset -= 2 * big_endian;
  7545. }
  7546. break;
  7547. default:
  7548. break;
  7549. }
  7550. }
  7551. else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
  7552. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
  7553. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
  7554. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
  7555. {
  7556. // First instruction of a local dynamic sequence,
  7557. // arg setup insn.
  7558. if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
  7559. {
  7560. if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
  7561. || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
  7562. {
  7563. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  7564. const Output_section* os = relinfo->layout->tls_segment()
  7565. ->first_section();
  7566. gold_assert(os != NULL);
  7567. gold_assert(os->needs_symtab_index());
  7568. r_sym = os->symtab_index();
  7569. addend = dtp_offset;
  7570. }
  7571. else
  7572. {
  7573. r_type = elfcpp::R_POWERPC_NONE;
  7574. offset -= 2 * big_endian;
  7575. }
  7576. }
  7577. }
  7578. else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
  7579. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
  7580. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
  7581. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
  7582. {
  7583. // First instruction of initial exec sequence.
  7584. const bool final = gsym == NULL || gsym->final_value_is_known();
  7585. if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
  7586. {
  7587. if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
  7588. || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
  7589. r_type = elfcpp::R_POWERPC_TPREL16_HA;
  7590. else
  7591. {
  7592. r_type = elfcpp::R_POWERPC_NONE;
  7593. offset -= 2 * big_endian;
  7594. }
  7595. }
  7596. }
  7597. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
  7598. || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
  7599. {
  7600. // Second instruction of a global dynamic sequence,
  7601. // the __tls_get_addr call
  7602. const bool final = gsym == NULL || gsym->final_value_is_known();
  7603. switch (this->optimize_tls_gd(final))
  7604. {
  7605. case tls::TLSOPT_TO_IE:
  7606. r_type = elfcpp::R_POWERPC_NONE;
  7607. zap_next = true;
  7608. break;
  7609. case tls::TLSOPT_TO_LE:
  7610. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  7611. offset += 2 * big_endian;
  7612. zap_next = true;
  7613. break;
  7614. default:
  7615. break;
  7616. }
  7617. }
  7618. else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
  7619. || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
  7620. {
  7621. // Second instruction of a local dynamic sequence,
  7622. // the __tls_get_addr call
  7623. if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
  7624. {
  7625. const Output_section* os = relinfo->layout->tls_segment()
  7626. ->first_section();
  7627. gold_assert(os != NULL);
  7628. gold_assert(os->needs_symtab_index());
  7629. r_sym = os->symtab_index();
  7630. addend = dtp_offset;
  7631. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  7632. offset += 2 * big_endian;
  7633. zap_next = true;
  7634. }
  7635. }
  7636. else if (r_type == elfcpp::R_POWERPC_TLS)
  7637. {
  7638. // Second instruction of an initial exec sequence
  7639. const bool final = gsym == NULL || gsym->final_value_is_known();
  7640. if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
  7641. {
  7642. r_type = elfcpp::R_POWERPC_TPREL16_LO;
  7643. offset += 2 * big_endian;
  7644. }
  7645. }
  7646. }
  7647. reloc_write.put_r_offset(offset);
  7648. reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
  7649. reloc_write.put_r_addend(addend);
  7650. pwrite += reloc_size;
  7651. }
  7652. gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
  7653. == reloc_view_size);
  7654. }
  7655. // Return the value to use for a dynamic symbol which requires special
  7656. // treatment. This is how we support equality comparisons of function
  7657. // pointers across shared library boundaries, as described in the
  7658. // processor specific ABI supplement.
  7659. template<int size, bool big_endian>
  7660. uint64_t
  7661. Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
  7662. {
  7663. if (size == 32)
  7664. {
  7665. gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
  7666. for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
  7667. p != this->stub_tables_.end();
  7668. ++p)
  7669. {
  7670. Address off = (*p)->find_plt_call_entry(gsym);
  7671. if (off != invalid_address)
  7672. return (*p)->stub_address() + off;
  7673. }
  7674. }
  7675. else if (this->abiversion() >= 2)
  7676. {
  7677. Address off = this->glink_section()->find_global_entry(gsym);
  7678. if (off != invalid_address)
  7679. return this->glink_section()->global_entry_address() + off;
  7680. }
  7681. gold_unreachable();
  7682. }
  7683. // Return the PLT address to use for a local symbol.
  7684. template<int size, bool big_endian>
  7685. uint64_t
  7686. Target_powerpc<size, big_endian>::do_plt_address_for_local(
  7687. const Relobj* object,
  7688. unsigned int symndx) const
  7689. {
  7690. if (size == 32)
  7691. {
  7692. const Sized_relobj<size, big_endian>* relobj
  7693. = static_cast<const Sized_relobj<size, big_endian>*>(object);
  7694. for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
  7695. p != this->stub_tables_.end();
  7696. ++p)
  7697. {
  7698. Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
  7699. symndx);
  7700. if (off != invalid_address)
  7701. return (*p)->stub_address() + off;
  7702. }
  7703. }
  7704. gold_unreachable();
  7705. }
  7706. // Return the PLT address to use for a global symbol.
  7707. template<int size, bool big_endian>
  7708. uint64_t
  7709. Target_powerpc<size, big_endian>::do_plt_address_for_global(
  7710. const Symbol* gsym) const
  7711. {
  7712. if (size == 32)
  7713. {
  7714. for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
  7715. p != this->stub_tables_.end();
  7716. ++p)
  7717. {
  7718. Address off = (*p)->find_plt_call_entry(gsym);
  7719. if (off != invalid_address)
  7720. return (*p)->stub_address() + off;
  7721. }
  7722. }
  7723. else if (this->abiversion() >= 2)
  7724. {
  7725. Address off = this->glink_section()->find_global_entry(gsym);
  7726. if (off != invalid_address)
  7727. return this->glink_section()->global_entry_address() + off;
  7728. }
  7729. gold_unreachable();
  7730. }
  7731. // Return the offset to use for the GOT_INDX'th got entry which is
  7732. // for a local tls symbol specified by OBJECT, SYMNDX.
  7733. template<int size, bool big_endian>
  7734. int64_t
  7735. Target_powerpc<size, big_endian>::do_tls_offset_for_local(
  7736. const Relobj* object,
  7737. unsigned int symndx,
  7738. unsigned int got_indx) const
  7739. {
  7740. const Powerpc_relobj<size, big_endian>* ppc_object
  7741. = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
  7742. if (ppc_object->local_symbol(symndx)->is_tls_symbol())
  7743. {
  7744. for (Got_type got_type = GOT_TYPE_TLSGD;
  7745. got_type <= GOT_TYPE_TPREL;
  7746. got_type = Got_type(got_type + 1))
  7747. if (ppc_object->local_has_got_offset(symndx, got_type))
  7748. {
  7749. unsigned int off = ppc_object->local_got_offset(symndx, got_type);
  7750. if (got_type == GOT_TYPE_TLSGD)
  7751. off += size / 8;
  7752. if (off == got_indx * (size / 8))
  7753. {
  7754. if (got_type == GOT_TYPE_TPREL)
  7755. return -tp_offset;
  7756. else
  7757. return -dtp_offset;
  7758. }
  7759. }
  7760. }
  7761. gold_unreachable();
  7762. }
  7763. // Return the offset to use for the GOT_INDX'th got entry which is
  7764. // for global tls symbol GSYM.
  7765. template<int size, bool big_endian>
  7766. int64_t
  7767. Target_powerpc<size, big_endian>::do_tls_offset_for_global(
  7768. Symbol* gsym,
  7769. unsigned int got_indx) const
  7770. {
  7771. if (gsym->type() == elfcpp::STT_TLS)
  7772. {
  7773. for (Got_type got_type = GOT_TYPE_TLSGD;
  7774. got_type <= GOT_TYPE_TPREL;
  7775. got_type = Got_type(got_type + 1))
  7776. if (gsym->has_got_offset(got_type))
  7777. {
  7778. unsigned int off = gsym->got_offset(got_type);
  7779. if (got_type == GOT_TYPE_TLSGD)
  7780. off += size / 8;
  7781. if (off == got_indx * (size / 8))
  7782. {
  7783. if (got_type == GOT_TYPE_TPREL)
  7784. return -tp_offset;
  7785. else
  7786. return -dtp_offset;
  7787. }
  7788. }
  7789. }
  7790. gold_unreachable();
  7791. }
  7792. // The selector for powerpc object files.
  7793. template<int size, bool big_endian>
  7794. class Target_selector_powerpc : public Target_selector
  7795. {
  7796. public:
  7797. Target_selector_powerpc()
  7798. : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
  7799. size, big_endian,
  7800. (size == 64
  7801. ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
  7802. : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
  7803. (size == 64
  7804. ? (big_endian ? "elf64ppc" : "elf64lppc")
  7805. : (big_endian ? "elf32ppc" : "elf32lppc")))
  7806. { }
  7807. virtual Target*
  7808. do_instantiate_target()
  7809. { return new Target_powerpc<size, big_endian>(); }
  7810. };
  7811. Target_selector_powerpc<32, true> target_selector_ppc32;
  7812. Target_selector_powerpc<32, false> target_selector_ppc32le;
  7813. Target_selector_powerpc<64, true> target_selector_ppc64;
  7814. Target_selector_powerpc<64, false> target_selector_ppc64le;
  7815. // Instantiate these constants for -O0
  7816. template<int size, bool big_endian>
  7817. const int Output_data_glink<size, big_endian>::pltresolve_size;
  7818. template<int size, bool big_endian>
  7819. const typename Output_data_glink<size, big_endian>::Address
  7820. Output_data_glink<size, big_endian>::invalid_address;
  7821. template<int size, bool big_endian>
  7822. const typename Stub_table<size, big_endian>::Address
  7823. Stub_table<size, big_endian>::invalid_address;
  7824. template<int size, bool big_endian>
  7825. const typename Target_powerpc<size, big_endian>::Address
  7826. Target_powerpc<size, big_endian>::invalid_address;
  7827. } // End anonymous namespace.