elf32-ppc.c 303 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565
  1. /* PowerPC-specific support for 32-bit ELF
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. /* The assembler should generate a full set of section symbols even
  18. when they appear unused. The linux kernel build tool recordmcount
  19. needs them. */
  20. #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
  21. #include "sysdep.h"
  22. #include <stdarg.h>
  23. #include "bfd.h"
  24. #include "bfdlink.h"
  25. #include "libbfd.h"
  26. #include "elf-bfd.h"
  27. #include "elf/ppc.h"
  28. #include "elf32-ppc.h"
  29. #include "elf-vxworks.h"
  30. #include "dwarf2.h"
  31. #include "opcode/ppc.h"
  32. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  33. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  34. typedef enum split16_format_type
  35. {
  36. split16a_type = 0,
  37. split16d_type
  38. }
  39. split16_format_type;
  40. /* RELA relocations are used here. */
  41. static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
  42. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  43. static bfd_reloc_status_type ppc_elf_unhandled_reloc
  44. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  45. /* Branch prediction bit for branch taken relocs. */
  46. #define BRANCH_PREDICT_BIT 0x200000
  47. /* Mask to set RA in memory instructions. */
  48. #define RA_REGISTER_MASK 0x001f0000
  49. /* Value to shift register by to insert RA. */
  50. #define RA_REGISTER_SHIFT 16
  51. /* The name of the dynamic interpreter. This is put in the .interp
  52. section. */
  53. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  54. /* For old-style PLT. */
  55. /* The number of single-slot PLT entries (the rest use two slots). */
  56. #define PLT_NUM_SINGLE_ENTRIES 8192
  57. /* For new-style .glink and .plt. */
  58. #define GLINK_PLTRESOLVE 16*4
  59. #define GLINK_ENTRY_SIZE(htab, h) \
  60. ((4*4 \
  61. + (h != NULL \
  62. && h == htab->tls_get_addr \
  63. && !htab->params->no_tls_get_addr_opt ? 8*4 : 0) \
  64. + (1u << htab->params->plt_stub_align) - 1) \
  65. & -(1u << htab->params->plt_stub_align))
  66. /* VxWorks uses its own plt layout, filled in by the static linker. */
  67. /* The standard VxWorks PLT entry. */
  68. #define VXWORKS_PLT_ENTRY_SIZE 32
  69. static const bfd_vma ppc_elf_vxworks_plt_entry
  70. [VXWORKS_PLT_ENTRY_SIZE / 4] =
  71. {
  72. 0x3d800000, /* lis r12,0 */
  73. 0x818c0000, /* lwz r12,0(r12) */
  74. 0x7d8903a6, /* mtctr r12 */
  75. 0x4e800420, /* bctr */
  76. 0x39600000, /* li r11,0 */
  77. 0x48000000, /* b 14 <.PLT0resolve+0x4> */
  78. 0x60000000, /* nop */
  79. 0x60000000, /* nop */
  80. };
  81. static const bfd_vma ppc_elf_vxworks_pic_plt_entry
  82. [VXWORKS_PLT_ENTRY_SIZE / 4] =
  83. {
  84. 0x3d9e0000, /* addis r12,r30,0 */
  85. 0x818c0000, /* lwz r12,0(r12) */
  86. 0x7d8903a6, /* mtctr r12 */
  87. 0x4e800420, /* bctr */
  88. 0x39600000, /* li r11,0 */
  89. 0x48000000, /* b 14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
  90. 0x60000000, /* nop */
  91. 0x60000000, /* nop */
  92. };
  93. /* The initial VxWorks PLT entry. */
  94. #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
  95. static const bfd_vma ppc_elf_vxworks_plt0_entry
  96. [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
  97. {
  98. 0x3d800000, /* lis r12,0 */
  99. 0x398c0000, /* addi r12,r12,0 */
  100. 0x800c0008, /* lwz r0,8(r12) */
  101. 0x7c0903a6, /* mtctr r0 */
  102. 0x818c0004, /* lwz r12,4(r12) */
  103. 0x4e800420, /* bctr */
  104. 0x60000000, /* nop */
  105. 0x60000000, /* nop */
  106. };
  107. static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
  108. [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
  109. {
  110. 0x819e0008, /* lwz r12,8(r30) */
  111. 0x7d8903a6, /* mtctr r12 */
  112. 0x819e0004, /* lwz r12,4(r30) */
  113. 0x4e800420, /* bctr */
  114. 0x60000000, /* nop */
  115. 0x60000000, /* nop */
  116. 0x60000000, /* nop */
  117. 0x60000000, /* nop */
  118. };
  119. /* For executables, we have some additional relocations in
  120. .rela.plt.unloaded, for the kernel loader. */
  121. /* The number of non-JMP_SLOT relocations per PLT0 slot. */
  122. #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
  123. /* The number of relocations in the PLTResolve slot. */
  124. #define VXWORKS_PLTRESOLVE_RELOCS 2
  125. /* The number of relocations in the PLTResolve slot when creating
  126. a shared library. */
  127. #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
  128. /* Some instructions. */
  129. #define ADDIS_11_11 0x3d6b0000
  130. #define ADDIS_11_30 0x3d7e0000
  131. #define ADDIS_12_12 0x3d8c0000
  132. #define ADDI_11_11 0x396b0000
  133. #define ADD_0_11_11 0x7c0b5a14
  134. #define ADD_3_12_2 0x7c6c1214
  135. #define ADD_11_0_11 0x7d605a14
  136. #define B 0x48000000
  137. #define BA 0x48000002
  138. #define BCL_20_31 0x429f0005
  139. #define BCTR 0x4e800420
  140. #define BEQLR 0x4d820020
  141. #define CMPWI_11_0 0x2c0b0000
  142. #define LIS_11 0x3d600000
  143. #define LIS_12 0x3d800000
  144. #define LWZU_0_12 0x840c0000
  145. #define LWZ_0_12 0x800c0000
  146. #define LWZ_11_3 0x81630000
  147. #define LWZ_11_11 0x816b0000
  148. #define LWZ_11_30 0x817e0000
  149. #define LWZ_12_3 0x81830000
  150. #define LWZ_12_12 0x818c0000
  151. #define MR_0_3 0x7c601b78
  152. #define MR_3_0 0x7c030378
  153. #define MFLR_0 0x7c0802a6
  154. #define MFLR_12 0x7d8802a6
  155. #define MTCTR_0 0x7c0903a6
  156. #define MTCTR_11 0x7d6903a6
  157. #define MTLR_0 0x7c0803a6
  158. #define NOP 0x60000000
  159. #define SUB_11_11_12 0x7d6c5850
  160. /* Offset of tp and dtp pointers from start of TLS block. */
  161. #define TP_OFFSET 0x7000
  162. #define DTP_OFFSET 0x8000
  163. /* The value of a defined global symbol. */
  164. #define SYM_VAL(SYM) \
  165. ((SYM)->root.u.def.section->output_section->vma \
  166. + (SYM)->root.u.def.section->output_offset \
  167. + (SYM)->root.u.def.value)
  168. /* Relocation HOWTO's. */
  169. /* Like other ELF RELA targets that don't apply multiple
  170. field-altering relocations to the same localation, src_mask is
  171. always zero and pcrel_offset is the same as pc_relative.
  172. PowerPC can always use a zero bitpos, even when the field is not at
  173. the LSB. For example, a REL24 could use rightshift=2, bisize=24
  174. and bitpos=2 which matches the ABI description, or as we do here,
  175. rightshift=0, bitsize=26 and bitpos=0. */
  176. #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
  177. complain, special_func) \
  178. HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
  179. complain_overflow_ ## complain, special_func, \
  180. #type, false, 0, mask, pc_relative)
  181. static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
  182. static reloc_howto_type ppc_elf_howto_raw[] = {
  183. /* This reloc does nothing. */
  184. HOW (R_PPC_NONE, 3, 0, 0, 0, false, dont,
  185. bfd_elf_generic_reloc),
  186. /* A standard 32 bit relocation. */
  187. HOW (R_PPC_ADDR32, 2, 32, 0xffffffff, 0, false, dont,
  188. bfd_elf_generic_reloc),
  189. /* An absolute 26 bit branch; the lower two bits must be zero.
  190. FIXME: we don't check that, we just clear them. */
  191. HOW (R_PPC_ADDR24, 2, 26, 0x3fffffc, 0, false, signed,
  192. bfd_elf_generic_reloc),
  193. /* A standard 16 bit relocation. */
  194. HOW (R_PPC_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
  195. bfd_elf_generic_reloc),
  196. /* A 16 bit relocation without overflow. */
  197. HOW (R_PPC_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
  198. bfd_elf_generic_reloc),
  199. /* The high order 16 bits of an address. */
  200. HOW (R_PPC_ADDR16_HI, 1, 16, 0xffff, 16, false, dont,
  201. bfd_elf_generic_reloc),
  202. /* The high order 16 bits of an address, plus 1 if the contents of
  203. the low 16 bits, treated as a signed number, is negative. */
  204. HOW (R_PPC_ADDR16_HA, 1, 16, 0xffff, 16, false, dont,
  205. ppc_elf_addr16_ha_reloc),
  206. /* An absolute 16 bit branch; the lower two bits must be zero.
  207. FIXME: we don't check that, we just clear them. */
  208. HOW (R_PPC_ADDR14, 2, 16, 0xfffc, 0, false, signed,
  209. bfd_elf_generic_reloc),
  210. /* An absolute 16 bit branch, for which bit 10 should be set to
  211. indicate that the branch is expected to be taken. The lower two
  212. bits must be zero. */
  213. HOW (R_PPC_ADDR14_BRTAKEN, 2, 16, 0xfffc, 0, false, signed,
  214. bfd_elf_generic_reloc),
  215. /* An absolute 16 bit branch, for which bit 10 should be set to
  216. indicate that the branch is not expected to be taken. The lower
  217. two bits must be zero. */
  218. HOW (R_PPC_ADDR14_BRNTAKEN, 2, 16, 0xfffc, 0, false, signed,
  219. bfd_elf_generic_reloc),
  220. /* A relative 26 bit branch; the lower two bits must be zero. */
  221. HOW (R_PPC_REL24, 2, 26, 0x3fffffc, 0, true, signed,
  222. bfd_elf_generic_reloc),
  223. /* A relative 16 bit branch; the lower two bits must be zero. */
  224. HOW (R_PPC_REL14, 2, 16, 0xfffc, 0, true, signed,
  225. bfd_elf_generic_reloc),
  226. /* A relative 16 bit branch. Bit 10 should be set to indicate that
  227. the branch is expected to be taken. The lower two bits must be
  228. zero. */
  229. HOW (R_PPC_REL14_BRTAKEN, 2, 16, 0xfffc, 0, true, signed,
  230. bfd_elf_generic_reloc),
  231. /* A relative 16 bit branch. Bit 10 should be set to indicate that
  232. the branch is not expected to be taken. The lower two bits must
  233. be zero. */
  234. HOW (R_PPC_REL14_BRNTAKEN, 2, 16, 0xfffc, 0, true, signed,
  235. bfd_elf_generic_reloc),
  236. /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
  237. symbol. */
  238. HOW (R_PPC_GOT16, 1, 16, 0xffff, 0, false, signed,
  239. ppc_elf_unhandled_reloc),
  240. /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
  241. the symbol. */
  242. HOW (R_PPC_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
  243. ppc_elf_unhandled_reloc),
  244. /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
  245. the symbol. */
  246. HOW (R_PPC_GOT16_HI, 1, 16, 0xffff, 16, false, dont,
  247. ppc_elf_unhandled_reloc),
  248. /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
  249. the symbol. */
  250. HOW (R_PPC_GOT16_HA, 1, 16, 0xffff, 16, false, dont,
  251. ppc_elf_unhandled_reloc),
  252. /* Like R_PPC_REL24, but referring to the procedure linkage table
  253. entry for the symbol. */
  254. HOW (R_PPC_PLTREL24, 2, 26, 0x3fffffc, 0, true, signed,
  255. ppc_elf_unhandled_reloc),
  256. /* This is used only by the dynamic linker. The symbol should exist
  257. both in the object being run and in some shared library. The
  258. dynamic linker copies the data addressed by the symbol from the
  259. shared library into the object, because the object being
  260. run has to have the data at some particular address. */
  261. HOW (R_PPC_COPY, 2, 32, 0, 0, false, dont,
  262. ppc_elf_unhandled_reloc),
  263. /* Like R_PPC_ADDR32, but used when setting global offset table
  264. entries. */
  265. HOW (R_PPC_GLOB_DAT, 2, 32, 0xffffffff, 0, false, dont,
  266. ppc_elf_unhandled_reloc),
  267. /* Marks a procedure linkage table entry for a symbol. */
  268. HOW (R_PPC_JMP_SLOT, 2, 32, 0, 0, false, dont,
  269. ppc_elf_unhandled_reloc),
  270. /* Used only by the dynamic linker. When the object is run, this
  271. longword is set to the load address of the object, plus the
  272. addend. */
  273. HOW (R_PPC_RELATIVE, 2, 32, 0xffffffff, 0, false, dont,
  274. bfd_elf_generic_reloc),
  275. /* Like R_PPC_REL24, but uses the value of the symbol within the
  276. object rather than the final value. Normally used for
  277. _GLOBAL_OFFSET_TABLE_. */
  278. HOW (R_PPC_LOCAL24PC, 2, 26, 0x3fffffc, 0, true, signed,
  279. bfd_elf_generic_reloc),
  280. /* Like R_PPC_ADDR32, but may be unaligned. */
  281. HOW (R_PPC_UADDR32, 2, 32, 0xffffffff, 0, false, dont,
  282. bfd_elf_generic_reloc),
  283. /* Like R_PPC_ADDR16, but may be unaligned. */
  284. HOW (R_PPC_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
  285. bfd_elf_generic_reloc),
  286. /* 32-bit PC relative */
  287. HOW (R_PPC_REL32, 2, 32, 0xffffffff, 0, true, dont,
  288. bfd_elf_generic_reloc),
  289. /* 32-bit relocation to the symbol's procedure linkage table.
  290. FIXME: not supported. */
  291. HOW (R_PPC_PLT32, 2, 32, 0, 0, false, dont,
  292. ppc_elf_unhandled_reloc),
  293. /* 32-bit PC relative relocation to the symbol's procedure linkage table.
  294. FIXME: not supported. */
  295. HOW (R_PPC_PLTREL32, 2, 32, 0, 0, true, dont,
  296. ppc_elf_unhandled_reloc),
  297. /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
  298. the symbol. */
  299. HOW (R_PPC_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
  300. ppc_elf_unhandled_reloc),
  301. /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
  302. the symbol. */
  303. HOW (R_PPC_PLT16_HI, 1, 16, 0xffff, 16, false, dont,
  304. ppc_elf_unhandled_reloc),
  305. /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
  306. the symbol. */
  307. HOW (R_PPC_PLT16_HA, 1, 16, 0xffff, 16, false, dont,
  308. ppc_elf_unhandled_reloc),
  309. /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
  310. small data items. */
  311. HOW (R_PPC_SDAREL16, 1, 16, 0xffff, 0, false, signed,
  312. ppc_elf_unhandled_reloc),
  313. /* 16-bit section relative relocation. */
  314. HOW (R_PPC_SECTOFF, 1, 16, 0xffff, 0, false, signed,
  315. ppc_elf_unhandled_reloc),
  316. /* 16-bit lower half section relative relocation. */
  317. HOW (R_PPC_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
  318. ppc_elf_unhandled_reloc),
  319. /* 16-bit upper half section relative relocation. */
  320. HOW (R_PPC_SECTOFF_HI, 1, 16, 0xffff, 16, false, dont,
  321. ppc_elf_unhandled_reloc),
  322. /* 16-bit upper half adjusted section relative relocation. */
  323. HOW (R_PPC_SECTOFF_HA, 1, 16, 0xffff, 16, false, dont,
  324. ppc_elf_unhandled_reloc),
  325. /* Marker relocs for TLS. */
  326. HOW (R_PPC_TLS, 2, 32, 0, 0, false, dont,
  327. bfd_elf_generic_reloc),
  328. HOW (R_PPC_TLSGD, 2, 32, 0, 0, false, dont,
  329. bfd_elf_generic_reloc),
  330. HOW (R_PPC_TLSLD, 2, 32, 0, 0, false, dont,
  331. bfd_elf_generic_reloc),
  332. /* Marker relocs on inline plt call instructions. */
  333. HOW (R_PPC_PLTSEQ, 2, 32, 0, 0, false, dont,
  334. bfd_elf_generic_reloc),
  335. HOW (R_PPC_PLTCALL, 2, 32, 0, 0, false, dont,
  336. bfd_elf_generic_reloc),
  337. /* Computes the load module index of the load module that contains the
  338. definition of its TLS sym. */
  339. HOW (R_PPC_DTPMOD32, 2, 32, 0xffffffff, 0, false, dont,
  340. ppc_elf_unhandled_reloc),
  341. /* Computes a dtv-relative displacement, the difference between the value
  342. of sym+add and the base address of the thread-local storage block that
  343. contains the definition of sym, minus 0x8000. */
  344. HOW (R_PPC_DTPREL32, 2, 32, 0xffffffff, 0, false, dont,
  345. ppc_elf_unhandled_reloc),
  346. /* A 16 bit dtprel reloc. */
  347. HOW (R_PPC_DTPREL16, 1, 16, 0xffff, 0, false, signed,
  348. ppc_elf_unhandled_reloc),
  349. /* Like DTPREL16, but no overflow. */
  350. HOW (R_PPC_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
  351. ppc_elf_unhandled_reloc),
  352. /* Like DTPREL16_LO, but next higher group of 16 bits. */
  353. HOW (R_PPC_DTPREL16_HI, 1, 16, 0xffff, 16, false, dont,
  354. ppc_elf_unhandled_reloc),
  355. /* Like DTPREL16_HI, but adjust for low 16 bits. */
  356. HOW (R_PPC_DTPREL16_HA, 1, 16, 0xffff, 16, false, dont,
  357. ppc_elf_unhandled_reloc),
  358. /* Computes a tp-relative displacement, the difference between the value of
  359. sym+add and the value of the thread pointer (r13). */
  360. HOW (R_PPC_TPREL32, 2, 32, 0xffffffff, 0, false, dont,
  361. ppc_elf_unhandled_reloc),
  362. /* A 16 bit tprel reloc. */
  363. HOW (R_PPC_TPREL16, 1, 16, 0xffff, 0, false, signed,
  364. ppc_elf_unhandled_reloc),
  365. /* Like TPREL16, but no overflow. */
  366. HOW (R_PPC_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
  367. ppc_elf_unhandled_reloc),
  368. /* Like TPREL16_LO, but next higher group of 16 bits. */
  369. HOW (R_PPC_TPREL16_HI, 1, 16, 0xffff, 16, false, dont,
  370. ppc_elf_unhandled_reloc),
  371. /* Like TPREL16_HI, but adjust for low 16 bits. */
  372. HOW (R_PPC_TPREL16_HA, 1, 16, 0xffff, 16, false, dont,
  373. ppc_elf_unhandled_reloc),
  374. /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
  375. with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
  376. to the first entry. */
  377. HOW (R_PPC_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
  378. ppc_elf_unhandled_reloc),
  379. /* Like GOT_TLSGD16, but no overflow. */
  380. HOW (R_PPC_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
  381. ppc_elf_unhandled_reloc),
  382. /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
  383. HOW (R_PPC_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, dont,
  384. ppc_elf_unhandled_reloc),
  385. /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
  386. HOW (R_PPC_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, dont,
  387. ppc_elf_unhandled_reloc),
  388. /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
  389. with values (sym+add)@dtpmod and zero, and computes the offset to the
  390. first entry. */
  391. HOW (R_PPC_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
  392. ppc_elf_unhandled_reloc),
  393. /* Like GOT_TLSLD16, but no overflow. */
  394. HOW (R_PPC_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
  395. ppc_elf_unhandled_reloc),
  396. /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
  397. HOW (R_PPC_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, dont,
  398. ppc_elf_unhandled_reloc),
  399. /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
  400. HOW (R_PPC_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, dont,
  401. ppc_elf_unhandled_reloc),
  402. /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
  403. the offset to the entry. */
  404. HOW (R_PPC_GOT_DTPREL16, 1, 16, 0xffff, 0, false, signed,
  405. ppc_elf_unhandled_reloc),
  406. /* Like GOT_DTPREL16, but no overflow. */
  407. HOW (R_PPC_GOT_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
  408. ppc_elf_unhandled_reloc),
  409. /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
  410. HOW (R_PPC_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, dont,
  411. ppc_elf_unhandled_reloc),
  412. /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
  413. HOW (R_PPC_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, dont,
  414. ppc_elf_unhandled_reloc),
  415. /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
  416. offset to the entry. */
  417. HOW (R_PPC_GOT_TPREL16, 1, 16, 0xffff, 0, false, signed,
  418. ppc_elf_unhandled_reloc),
  419. /* Like GOT_TPREL16, but no overflow. */
  420. HOW (R_PPC_GOT_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
  421. ppc_elf_unhandled_reloc),
  422. /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
  423. HOW (R_PPC_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, dont,
  424. ppc_elf_unhandled_reloc),
  425. /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
  426. HOW (R_PPC_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, dont,
  427. ppc_elf_unhandled_reloc),
  428. /* The remaining relocs are from the Embedded ELF ABI, and are not
  429. in the SVR4 ELF ABI. */
  430. /* 32 bit value resulting from the addend minus the symbol. */
  431. HOW (R_PPC_EMB_NADDR32, 2, 32, 0xffffffff, 0, false, dont,
  432. ppc_elf_unhandled_reloc),
  433. /* 16 bit value resulting from the addend minus the symbol. */
  434. HOW (R_PPC_EMB_NADDR16, 1, 16, 0xffff, 0, false, signed,
  435. ppc_elf_unhandled_reloc),
  436. /* 16 bit value resulting from the addend minus the symbol. */
  437. HOW (R_PPC_EMB_NADDR16_LO, 1, 16, 0xffff, 0, false, dont,
  438. ppc_elf_unhandled_reloc),
  439. /* The high order 16 bits of the addend minus the symbol. */
  440. HOW (R_PPC_EMB_NADDR16_HI, 1, 16, 0xffff, 16, false, dont,
  441. ppc_elf_unhandled_reloc),
  442. /* The high order 16 bits of the result of the addend minus the address,
  443. plus 1 if the contents of the low 16 bits, treated as a signed number,
  444. is negative. */
  445. HOW (R_PPC_EMB_NADDR16_HA, 1, 16, 0xffff, 16, false, dont,
  446. ppc_elf_unhandled_reloc),
  447. /* 16 bit value resulting from allocating a 4 byte word to hold an
  448. address in the .sdata section, and returning the offset from
  449. _SDA_BASE_ for that relocation. */
  450. HOW (R_PPC_EMB_SDAI16, 1, 16, 0xffff, 0, false, signed,
  451. ppc_elf_unhandled_reloc),
  452. /* 16 bit value resulting from allocating a 4 byte word to hold an
  453. address in the .sdata2 section, and returning the offset from
  454. _SDA2_BASE_ for that relocation. */
  455. HOW (R_PPC_EMB_SDA2I16, 1, 16, 0xffff, 0, false, signed,
  456. ppc_elf_unhandled_reloc),
  457. /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
  458. small data items. */
  459. HOW (R_PPC_EMB_SDA2REL, 1, 16, 0xffff, 0, false, signed,
  460. ppc_elf_unhandled_reloc),
  461. /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
  462. signed offset from the appropriate base, and filling in the register
  463. field with the appropriate register (0, 2, or 13). */
  464. HOW (R_PPC_EMB_SDA21, 2, 16, 0xffff, 0, false, signed,
  465. ppc_elf_unhandled_reloc),
  466. /* Relocation not handled: R_PPC_EMB_MRKREF */
  467. /* Relocation not handled: R_PPC_EMB_RELSEC16 */
  468. /* Relocation not handled: R_PPC_EMB_RELST_LO */
  469. /* Relocation not handled: R_PPC_EMB_RELST_HI */
  470. /* Relocation not handled: R_PPC_EMB_RELST_HA */
  471. /* Relocation not handled: R_PPC_EMB_BIT_FLD */
  472. /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
  473. in the 16 bit signed offset from the appropriate base, and filling in the
  474. register field with the appropriate register (0, 2, or 13). */
  475. HOW (R_PPC_EMB_RELSDA, 1, 16, 0xffff, 0, false, signed,
  476. ppc_elf_unhandled_reloc),
  477. /* A relative 8 bit branch. */
  478. HOW (R_PPC_VLE_REL8, 1, 8, 0xff, 1, true, signed,
  479. bfd_elf_generic_reloc),
  480. /* A relative 15 bit branch. */
  481. HOW (R_PPC_VLE_REL15, 2, 16, 0xfffe, 0, true, signed,
  482. bfd_elf_generic_reloc),
  483. /* A relative 24 bit branch. */
  484. HOW (R_PPC_VLE_REL24, 2, 25, 0x1fffffe, 0, true, signed,
  485. bfd_elf_generic_reloc),
  486. /* The 16 LSBS in split16a format. */
  487. HOW (R_PPC_VLE_LO16A, 2, 16, 0x1f07ff, 0, false, dont,
  488. ppc_elf_unhandled_reloc),
  489. /* The 16 LSBS in split16d format. */
  490. HOW (R_PPC_VLE_LO16D, 2, 16, 0x3e007ff, 0, false, dont,
  491. ppc_elf_unhandled_reloc),
  492. /* Bits 16-31 split16a format. */
  493. HOW (R_PPC_VLE_HI16A, 2, 16, 0x1f07ff, 16, false, dont,
  494. ppc_elf_unhandled_reloc),
  495. /* Bits 16-31 split16d format. */
  496. HOW (R_PPC_VLE_HI16D, 2, 16, 0x3e007ff, 16, false, dont,
  497. ppc_elf_unhandled_reloc),
  498. /* Bits 16-31 (High Adjusted) in split16a format. */
  499. HOW (R_PPC_VLE_HA16A, 2, 16, 0x1f07ff, 16, false, dont,
  500. ppc_elf_unhandled_reloc),
  501. /* Bits 16-31 (High Adjusted) in split16d format. */
  502. HOW (R_PPC_VLE_HA16D, 2, 16, 0x3e007ff, 16, false, dont,
  503. ppc_elf_unhandled_reloc),
  504. /* This reloc is like R_PPC_EMB_SDA21 but only applies to e_add16i
  505. instructions. If the register base is 0 then the linker changes
  506. the e_add16i to an e_li instruction. */
  507. HOW (R_PPC_VLE_SDA21, 2, 16, 0xffff, 0, false, signed,
  508. ppc_elf_unhandled_reloc),
  509. /* Like R_PPC_VLE_SDA21 but ignore overflow. */
  510. HOW (R_PPC_VLE_SDA21_LO, 2, 16, 0xffff, 0, false, dont,
  511. ppc_elf_unhandled_reloc),
  512. /* The 16 LSBS relative to _SDA_BASE_ in split16a format. */
  513. HOW (R_PPC_VLE_SDAREL_LO16A, 2, 16, 0x1f07ff, 0, false, dont,
  514. ppc_elf_unhandled_reloc),
  515. /* The 16 LSBS relative to _SDA_BASE_ in split16d format. */
  516. HOW (R_PPC_VLE_SDAREL_LO16D, 2, 16, 0x3e007ff, 0, false, dont,
  517. ppc_elf_unhandled_reloc),
  518. /* Bits 16-31 relative to _SDA_BASE_ in split16a format. */
  519. HOW (R_PPC_VLE_SDAREL_HI16A, 2, 16, 0x1f07ff, 16, false, dont,
  520. ppc_elf_unhandled_reloc),
  521. /* Bits 16-31 relative to _SDA_BASE_ in split16d format. */
  522. HOW (R_PPC_VLE_SDAREL_HI16D, 2, 16, 0x3e007ff, 16, false, dont,
  523. ppc_elf_unhandled_reloc),
  524. /* Bits 16-31 (HA) relative to _SDA_BASE split16a format. */
  525. HOW (R_PPC_VLE_SDAREL_HA16A, 2, 16, 0x1f07ff, 16, false, dont,
  526. ppc_elf_unhandled_reloc),
  527. /* Bits 16-31 (HA) relative to _SDA_BASE split16d format. */
  528. HOW (R_PPC_VLE_SDAREL_HA16D, 2, 16, 0x3e007ff, 16, false, dont,
  529. ppc_elf_unhandled_reloc),
  530. /* e_li split20 format. */
  531. HOW (R_PPC_VLE_ADDR20, 2, 20, 0x1f7fff, 0, false, dont,
  532. ppc_elf_unhandled_reloc),
  533. HOW (R_PPC_IRELATIVE, 2, 32, 0xffffffff, 0, false, dont,
  534. ppc_elf_unhandled_reloc),
  535. /* A 16 bit relative relocation. */
  536. HOW (R_PPC_REL16, 1, 16, 0xffff, 0, true, signed,
  537. bfd_elf_generic_reloc),
  538. /* A 16 bit relative relocation without overflow. */
  539. HOW (R_PPC_REL16_LO, 1, 16, 0xffff, 0, true, dont,
  540. bfd_elf_generic_reloc),
  541. /* The high order 16 bits of a relative address. */
  542. HOW (R_PPC_REL16_HI, 1, 16, 0xffff, 16, true, dont,
  543. bfd_elf_generic_reloc),
  544. /* The high order 16 bits of a relative address, plus 1 if the contents of
  545. the low 16 bits, treated as a signed number, is negative. */
  546. HOW (R_PPC_REL16_HA, 1, 16, 0xffff, 16, true, dont,
  547. ppc_elf_addr16_ha_reloc),
  548. /* Like R_PPC_REL16_HA but for split field in addpcis. */
  549. HOW (R_PPC_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
  550. ppc_elf_addr16_ha_reloc),
  551. /* A split-field reloc for addpcis, non-relative (gas internal use only). */
  552. HOW (R_PPC_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
  553. ppc_elf_addr16_ha_reloc),
  554. /* GNU extension to record C++ vtable hierarchy. */
  555. HOW (R_PPC_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
  556. NULL),
  557. /* GNU extension to record C++ vtable member usage. */
  558. HOW (R_PPC_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
  559. NULL),
  560. /* Phony reloc to handle AIX style TOC entries. */
  561. HOW (R_PPC_TOC16, 1, 16, 0xffff, 0, false, signed,
  562. ppc_elf_unhandled_reloc),
  563. };
  564. /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
  565. static void
  566. ppc_elf_howto_init (void)
  567. {
  568. unsigned int i, type;
  569. for (i = 0;
  570. i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
  571. i++)
  572. {
  573. type = ppc_elf_howto_raw[i].type;
  574. if (type >= (sizeof (ppc_elf_howto_table)
  575. / sizeof (ppc_elf_howto_table[0])))
  576. abort ();
  577. ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
  578. }
  579. }
  580. static reloc_howto_type *
  581. ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  582. bfd_reloc_code_real_type code)
  583. {
  584. enum elf_ppc_reloc_type r;
  585. /* Initialize howto table if not already done. */
  586. if (!ppc_elf_howto_table[R_PPC_ADDR32])
  587. ppc_elf_howto_init ();
  588. switch (code)
  589. {
  590. default:
  591. return NULL;
  592. case BFD_RELOC_NONE: r = R_PPC_NONE; break;
  593. case BFD_RELOC_32: r = R_PPC_ADDR32; break;
  594. case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
  595. case BFD_RELOC_PPC64_ADDR16_DS:
  596. case BFD_RELOC_16: r = R_PPC_ADDR16; break;
  597. case BFD_RELOC_PPC64_ADDR16_LO_DS:
  598. case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
  599. case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
  600. case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
  601. case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
  602. case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
  603. case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
  604. case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
  605. case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
  606. case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
  607. case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
  608. case BFD_RELOC_PPC64_GOT16_DS:
  609. case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
  610. case BFD_RELOC_PPC64_GOT16_LO_DS:
  611. case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
  612. case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
  613. case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
  614. case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
  615. case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
  616. case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
  617. case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
  618. case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
  619. case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
  620. case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
  621. case BFD_RELOC_PPC64_PLT16_LO_DS:
  622. case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
  623. case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
  624. case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
  625. case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
  626. case BFD_RELOC_PPC64_SECTOFF_DS:
  627. case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
  628. case BFD_RELOC_PPC64_SECTOFF_LO_DS:
  629. case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
  630. case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
  631. case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
  632. case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
  633. case BFD_RELOC_PPC64_TOC16_DS:
  634. case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
  635. case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
  636. case BFD_RELOC_PPC_TLSGD: r = R_PPC_TLSGD; break;
  637. case BFD_RELOC_PPC_TLSLD: r = R_PPC_TLSLD; break;
  638. case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
  639. case BFD_RELOC_PPC64_TPREL16_DS:
  640. case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
  641. case BFD_RELOC_PPC64_TPREL16_LO_DS:
  642. case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
  643. case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
  644. case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
  645. case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
  646. case BFD_RELOC_PPC64_DTPREL16_DS:
  647. case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
  648. case BFD_RELOC_PPC64_DTPREL16_LO_DS:
  649. case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
  650. case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
  651. case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
  652. case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
  653. case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
  654. case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
  655. case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
  656. case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
  657. case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
  658. case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
  659. case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
  660. case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
  661. case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
  662. case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
  663. case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
  664. case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
  665. case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
  666. case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
  667. case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
  668. case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
  669. case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
  670. case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
  671. case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
  672. case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
  673. case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
  674. case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
  675. case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
  676. case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
  677. case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
  678. case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
  679. case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
  680. case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
  681. case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
  682. case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
  683. case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
  684. case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
  685. case BFD_RELOC_PPC_VLE_REL8: r = R_PPC_VLE_REL8; break;
  686. case BFD_RELOC_PPC_VLE_REL15: r = R_PPC_VLE_REL15; break;
  687. case BFD_RELOC_PPC_VLE_REL24: r = R_PPC_VLE_REL24; break;
  688. case BFD_RELOC_PPC_VLE_LO16A: r = R_PPC_VLE_LO16A; break;
  689. case BFD_RELOC_PPC_VLE_LO16D: r = R_PPC_VLE_LO16D; break;
  690. case BFD_RELOC_PPC_VLE_HI16A: r = R_PPC_VLE_HI16A; break;
  691. case BFD_RELOC_PPC_VLE_HI16D: r = R_PPC_VLE_HI16D; break;
  692. case BFD_RELOC_PPC_VLE_HA16A: r = R_PPC_VLE_HA16A; break;
  693. case BFD_RELOC_PPC_VLE_HA16D: r = R_PPC_VLE_HA16D; break;
  694. case BFD_RELOC_PPC_VLE_SDA21: r = R_PPC_VLE_SDA21; break;
  695. case BFD_RELOC_PPC_VLE_SDA21_LO: r = R_PPC_VLE_SDA21_LO; break;
  696. case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
  697. r = R_PPC_VLE_SDAREL_LO16A;
  698. break;
  699. case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
  700. r = R_PPC_VLE_SDAREL_LO16D;
  701. break;
  702. case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
  703. r = R_PPC_VLE_SDAREL_HI16A;
  704. break;
  705. case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
  706. r = R_PPC_VLE_SDAREL_HI16D;
  707. break;
  708. case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
  709. r = R_PPC_VLE_SDAREL_HA16A;
  710. break;
  711. case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
  712. r = R_PPC_VLE_SDAREL_HA16D;
  713. break;
  714. case BFD_RELOC_16_PCREL: r = R_PPC_REL16; break;
  715. case BFD_RELOC_LO16_PCREL: r = R_PPC_REL16_LO; break;
  716. case BFD_RELOC_HI16_PCREL: r = R_PPC_REL16_HI; break;
  717. case BFD_RELOC_HI16_S_PCREL: r = R_PPC_REL16_HA; break;
  718. case BFD_RELOC_PPC_16DX_HA: r = R_PPC_16DX_HA; break;
  719. case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC_REL16DX_HA; break;
  720. case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
  721. case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
  722. }
  723. return ppc_elf_howto_table[r];
  724. };
  725. static reloc_howto_type *
  726. ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  727. const char *r_name)
  728. {
  729. unsigned int i;
  730. for (i = 0;
  731. i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
  732. i++)
  733. if (ppc_elf_howto_raw[i].name != NULL
  734. && strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
  735. return &ppc_elf_howto_raw[i];
  736. return NULL;
  737. }
  738. /* Set the howto pointer for a PowerPC ELF reloc. */
  739. static bool
  740. ppc_elf_info_to_howto (bfd *abfd,
  741. arelent *cache_ptr,
  742. Elf_Internal_Rela *dst)
  743. {
  744. unsigned int r_type;
  745. /* Initialize howto table if not already done. */
  746. if (!ppc_elf_howto_table[R_PPC_ADDR32])
  747. ppc_elf_howto_init ();
  748. r_type = ELF32_R_TYPE (dst->r_info);
  749. if (r_type >= R_PPC_max)
  750. {
  751. /* xgettext:c-format */
  752. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  753. abfd, r_type);
  754. bfd_set_error (bfd_error_bad_value);
  755. return false;
  756. }
  757. cache_ptr->howto = ppc_elf_howto_table[r_type];
  758. /* Just because the above assert didn't trigger doesn't mean that
  759. ELF32_R_TYPE (dst->r_info) is necessarily a valid relocation. */
  760. if (cache_ptr->howto == NULL)
  761. {
  762. /* xgettext:c-format */
  763. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  764. abfd, r_type);
  765. bfd_set_error (bfd_error_bad_value);
  766. return false;
  767. }
  768. return true;
  769. }
  770. /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs. */
  771. static bfd_reloc_status_type
  772. ppc_elf_addr16_ha_reloc (bfd *abfd,
  773. arelent *reloc_entry,
  774. asymbol *symbol,
  775. void *data,
  776. asection *input_section,
  777. bfd *output_bfd,
  778. char **error_message ATTRIBUTE_UNUSED)
  779. {
  780. enum elf_ppc_reloc_type r_type;
  781. long insn;
  782. bfd_size_type octets;
  783. bfd_vma value;
  784. if (output_bfd != NULL)
  785. {
  786. reloc_entry->address += input_section->output_offset;
  787. return bfd_reloc_ok;
  788. }
  789. reloc_entry->addend += 0x8000;
  790. r_type = reloc_entry->howto->type;
  791. if (r_type != R_PPC_REL16DX_HA)
  792. return bfd_reloc_continue;
  793. value = 0;
  794. if (!bfd_is_com_section (symbol->section))
  795. value = symbol->value;
  796. value += (reloc_entry->addend
  797. + symbol->section->output_offset
  798. + symbol->section->output_section->vma);
  799. value -= (reloc_entry->address
  800. + input_section->output_offset
  801. + input_section->output_section->vma);
  802. value >>= 16;
  803. octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
  804. if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
  805. input_section, octets))
  806. return bfd_reloc_outofrange;
  807. insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
  808. insn &= ~0x1fffc1;
  809. insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
  810. bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
  811. return bfd_reloc_ok;
  812. }
  813. static bfd_reloc_status_type
  814. ppc_elf_unhandled_reloc (bfd *abfd,
  815. arelent *reloc_entry,
  816. asymbol *symbol,
  817. void *data,
  818. asection *input_section,
  819. bfd *output_bfd,
  820. char **error_message)
  821. {
  822. /* If this is a relocatable link (output_bfd test tells us), just
  823. call the generic function. Any adjustment will be done at final
  824. link time. */
  825. if (output_bfd != NULL)
  826. return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  827. input_section, output_bfd, error_message);
  828. if (error_message != NULL)
  829. {
  830. static char *message;
  831. free (message);
  832. if (asprintf (&message, _("generic linker can't handle %s"),
  833. reloc_entry->howto->name) < 0)
  834. message = NULL;
  835. *error_message = message;
  836. }
  837. return bfd_reloc_dangerous;
  838. }
  839. /* Sections created by the linker. */
  840. typedef struct elf_linker_section
  841. {
  842. /* Pointer to the bfd section. */
  843. asection *section;
  844. /* Section name. */
  845. const char *name;
  846. /* Associated bss section name. */
  847. const char *bss_name;
  848. /* Associated symbol name. */
  849. const char *sym_name;
  850. /* Associated symbol. */
  851. struct elf_link_hash_entry *sym;
  852. } elf_linker_section_t;
  853. /* Linked list of allocated pointer entries. This hangs off of the
  854. symbol lists, and provides allows us to return different pointers,
  855. based on different addend's. */
  856. typedef struct elf_linker_section_pointers
  857. {
  858. /* next allocated pointer for this symbol */
  859. struct elf_linker_section_pointers *next;
  860. /* offset of pointer from beginning of section */
  861. bfd_vma offset;
  862. /* addend used */
  863. bfd_vma addend;
  864. /* which linker section this is */
  865. elf_linker_section_t *lsect;
  866. } elf_linker_section_pointers_t;
  867. struct ppc_elf_obj_tdata
  868. {
  869. struct elf_obj_tdata elf;
  870. /* A mapping from local symbols to offsets into the various linker
  871. sections added. This is index by the symbol index. */
  872. elf_linker_section_pointers_t **linker_section_pointers;
  873. /* Flags used to auto-detect plt type. */
  874. unsigned int makes_plt_call : 1;
  875. unsigned int has_rel16 : 1;
  876. };
  877. #define ppc_elf_tdata(bfd) \
  878. ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
  879. #define elf_local_ptr_offsets(bfd) \
  880. (ppc_elf_tdata (bfd)->linker_section_pointers)
  881. #define is_ppc_elf(bfd) \
  882. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  883. && elf_object_id (bfd) == PPC32_ELF_DATA)
  884. /* Override the generic function because we store some extras. */
  885. static bool
  886. ppc_elf_mkobject (bfd *abfd)
  887. {
  888. return bfd_elf_allocate_object (abfd, sizeof (struct ppc_elf_obj_tdata),
  889. PPC32_ELF_DATA);
  890. }
  891. /* When defaulting arch/mach, decode apuinfo to find a better match. */
  892. bool
  893. _bfd_elf_ppc_set_arch (bfd *abfd)
  894. {
  895. unsigned long mach = 0;
  896. asection *s;
  897. unsigned char *contents;
  898. if (abfd->arch_info->bits_per_word == 32
  899. && bfd_big_endian (abfd))
  900. {
  901. for (s = abfd->sections; s != NULL; s = s->next)
  902. if ((elf_section_data (s)->this_hdr.sh_flags & SHF_PPC_VLE) != 0)
  903. break;
  904. if (s != NULL)
  905. mach = bfd_mach_ppc_vle;
  906. }
  907. if (mach == 0)
  908. {
  909. s = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
  910. if (s != NULL
  911. && s->size >= 24
  912. && bfd_malloc_and_get_section (abfd, s, &contents))
  913. {
  914. unsigned int apuinfo_size = bfd_get_32 (abfd, contents + 4);
  915. unsigned int i;
  916. for (i = 20; i < apuinfo_size + 20 && i + 4 <= s->size; i += 4)
  917. {
  918. unsigned int val = bfd_get_32 (abfd, contents + i);
  919. switch (val >> 16)
  920. {
  921. case PPC_APUINFO_PMR:
  922. case PPC_APUINFO_RFMCI:
  923. if (mach == 0)
  924. mach = bfd_mach_ppc_titan;
  925. break;
  926. case PPC_APUINFO_ISEL:
  927. case PPC_APUINFO_CACHELCK:
  928. if (mach == bfd_mach_ppc_titan)
  929. mach = bfd_mach_ppc_e500mc;
  930. break;
  931. case PPC_APUINFO_SPE:
  932. case PPC_APUINFO_EFS:
  933. case PPC_APUINFO_BRLOCK:
  934. if (mach != bfd_mach_ppc_vle)
  935. mach = bfd_mach_ppc_e500;
  936. break;
  937. case PPC_APUINFO_VLE:
  938. mach = bfd_mach_ppc_vle;
  939. break;
  940. default:
  941. mach = -1ul;
  942. }
  943. }
  944. free (contents);
  945. }
  946. }
  947. if (mach != 0 && mach != -1ul)
  948. {
  949. const bfd_arch_info_type *arch;
  950. for (arch = abfd->arch_info->next; arch; arch = arch->next)
  951. if (arch->mach == mach)
  952. {
  953. abfd->arch_info = arch;
  954. break;
  955. }
  956. }
  957. return true;
  958. }
  959. /* Fix bad default arch selected for a 32 bit input bfd when the
  960. default is 64 bit. Also select arch based on apuinfo. */
  961. static bool
  962. ppc_elf_object_p (bfd *abfd)
  963. {
  964. if (!abfd->arch_info->the_default)
  965. return true;
  966. if (abfd->arch_info->bits_per_word == 64)
  967. {
  968. Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
  969. if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
  970. {
  971. /* Relies on arch after 64 bit default being 32 bit default. */
  972. abfd->arch_info = abfd->arch_info->next;
  973. BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
  974. }
  975. }
  976. return _bfd_elf_ppc_set_arch (abfd);
  977. }
  978. /* Function to set whether a module needs the -mrelocatable bit set. */
  979. static bool
  980. ppc_elf_set_private_flags (bfd *abfd, flagword flags)
  981. {
  982. BFD_ASSERT (!elf_flags_init (abfd)
  983. || elf_elfheader (abfd)->e_flags == flags);
  984. elf_elfheader (abfd)->e_flags = flags;
  985. elf_flags_init (abfd) = true;
  986. return true;
  987. }
  988. /* Support for core dump NOTE sections. */
  989. static bool
  990. ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  991. {
  992. int offset;
  993. unsigned int size;
  994. switch (note->descsz)
  995. {
  996. default:
  997. return false;
  998. case 268: /* Linux/PPC. */
  999. /* pr_cursig */
  1000. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  1001. /* pr_pid */
  1002. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  1003. /* pr_reg */
  1004. offset = 72;
  1005. size = 192;
  1006. break;
  1007. }
  1008. /* Make a ".reg/999" section. */
  1009. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  1010. size, note->descpos + offset);
  1011. }
  1012. static bool
  1013. ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  1014. {
  1015. switch (note->descsz)
  1016. {
  1017. default:
  1018. return false;
  1019. case 128: /* Linux/PPC elf_prpsinfo. */
  1020. elf_tdata (abfd)->core->pid
  1021. = bfd_get_32 (abfd, note->descdata + 16);
  1022. elf_tdata (abfd)->core->program
  1023. = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
  1024. elf_tdata (abfd)->core->command
  1025. = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
  1026. }
  1027. /* Note that for some reason, a spurious space is tacked
  1028. onto the end of the args in some (at least one anyway)
  1029. implementations, so strip it off if it exists. */
  1030. {
  1031. char *command = elf_tdata (abfd)->core->command;
  1032. int n = strlen (command);
  1033. if (0 < n && command[n - 1] == ' ')
  1034. command[n - 1] = '\0';
  1035. }
  1036. return true;
  1037. }
  1038. static char *
  1039. ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
  1040. {
  1041. switch (note_type)
  1042. {
  1043. default:
  1044. return NULL;
  1045. case NT_PRPSINFO:
  1046. {
  1047. char data[128] ATTRIBUTE_NONSTRING;
  1048. va_list ap;
  1049. va_start (ap, note_type);
  1050. memset (data, 0, sizeof (data));
  1051. strncpy (data + 32, va_arg (ap, const char *), 16);
  1052. #if GCC_VERSION == 8000 || GCC_VERSION == 8001
  1053. DIAGNOSTIC_PUSH;
  1054. /* GCC 8.0 and 8.1 warn about 80 equals destination size with
  1055. -Wstringop-truncation:
  1056. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
  1057. */
  1058. DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
  1059. #endif
  1060. strncpy (data + 48, va_arg (ap, const char *), 80);
  1061. #if GCC_VERSION == 8000 || GCC_VERSION == 8001
  1062. DIAGNOSTIC_POP;
  1063. #endif
  1064. va_end (ap);
  1065. return elfcore_write_note (abfd, buf, bufsiz,
  1066. "CORE", note_type, data, sizeof (data));
  1067. }
  1068. case NT_PRSTATUS:
  1069. {
  1070. char data[268];
  1071. va_list ap;
  1072. long pid;
  1073. int cursig;
  1074. const void *greg;
  1075. va_start (ap, note_type);
  1076. memset (data, 0, 72);
  1077. pid = va_arg (ap, long);
  1078. bfd_put_32 (abfd, pid, data + 24);
  1079. cursig = va_arg (ap, int);
  1080. bfd_put_16 (abfd, cursig, data + 12);
  1081. greg = va_arg (ap, const void *);
  1082. memcpy (data + 72, greg, 192);
  1083. memset (data + 264, 0, 4);
  1084. va_end (ap);
  1085. return elfcore_write_note (abfd, buf, bufsiz,
  1086. "CORE", note_type, data, sizeof (data));
  1087. }
  1088. }
  1089. }
  1090. static flagword
  1091. ppc_elf_lookup_section_flags (char *flag_name)
  1092. {
  1093. if (!strcmp (flag_name, "SHF_PPC_VLE"))
  1094. return SHF_PPC_VLE;
  1095. return 0;
  1096. }
  1097. /* Return address for Ith PLT stub in section PLT, for relocation REL
  1098. or (bfd_vma) -1 if it should not be included. */
  1099. static bfd_vma
  1100. ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
  1101. const asection *plt ATTRIBUTE_UNUSED,
  1102. const arelent *rel)
  1103. {
  1104. return rel->address;
  1105. }
  1106. /* Handle a PowerPC specific section when reading an object file. This
  1107. is called when bfd_section_from_shdr finds a section with an unknown
  1108. type. */
  1109. static bool
  1110. ppc_elf_section_from_shdr (bfd *abfd,
  1111. Elf_Internal_Shdr *hdr,
  1112. const char *name,
  1113. int shindex)
  1114. {
  1115. asection *newsect;
  1116. flagword flags;
  1117. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  1118. return false;
  1119. newsect = hdr->bfd_section;
  1120. flags = 0;
  1121. if (hdr->sh_flags & SHF_EXCLUDE)
  1122. flags |= SEC_EXCLUDE;
  1123. if (hdr->sh_type == SHT_ORDERED)
  1124. flags |= SEC_SORT_ENTRIES;
  1125. if (startswith (name, ".PPC.EMB"))
  1126. name += 8;
  1127. if (startswith (name, ".sbss")
  1128. || startswith (name, ".sdata"))
  1129. flags |= SEC_SMALL_DATA;
  1130. return (flags == 0
  1131. || bfd_set_section_flags (newsect, newsect->flags | flags));
  1132. }
  1133. /* Set up any other section flags and such that may be necessary. */
  1134. static bool
  1135. ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
  1136. Elf_Internal_Shdr *shdr,
  1137. asection *asect)
  1138. {
  1139. if ((asect->flags & SEC_SORT_ENTRIES) != 0)
  1140. shdr->sh_type = SHT_ORDERED;
  1141. return true;
  1142. }
  1143. /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
  1144. need to bump up the number of section headers. */
  1145. static int
  1146. ppc_elf_additional_program_headers (bfd *abfd,
  1147. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  1148. {
  1149. asection *s;
  1150. int ret = 0;
  1151. s = bfd_get_section_by_name (abfd, ".sbss2");
  1152. if (s != NULL && (s->flags & SEC_ALLOC) != 0)
  1153. ++ret;
  1154. s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
  1155. if (s != NULL && (s->flags & SEC_ALLOC) != 0)
  1156. ++ret;
  1157. return ret;
  1158. }
  1159. /* Modify the segment map for VLE executables. */
  1160. bool
  1161. ppc_elf_modify_segment_map (bfd *abfd,
  1162. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  1163. {
  1164. struct elf_segment_map *m;
  1165. /* At this point in the link, output sections have already been sorted by
  1166. LMA and assigned to segments. All that is left to do is to ensure
  1167. there is no mixing of VLE & non-VLE sections in a text segment.
  1168. If we find that case, we split the segment.
  1169. We maintain the original output section order. */
  1170. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  1171. {
  1172. struct elf_segment_map *n;
  1173. size_t amt;
  1174. unsigned int j, k;
  1175. unsigned int p_flags;
  1176. if (m->p_type != PT_LOAD || m->count == 0)
  1177. continue;
  1178. for (p_flags = PF_R, j = 0; j != m->count; ++j)
  1179. {
  1180. if ((m->sections[j]->flags & SEC_READONLY) == 0)
  1181. p_flags |= PF_W;
  1182. if ((m->sections[j]->flags & SEC_CODE) != 0)
  1183. {
  1184. p_flags |= PF_X;
  1185. if ((elf_section_flags (m->sections[j]) & SHF_PPC_VLE) != 0)
  1186. p_flags |= PF_PPC_VLE;
  1187. break;
  1188. }
  1189. }
  1190. if (j != m->count)
  1191. while (++j != m->count)
  1192. {
  1193. unsigned int p_flags1 = PF_R;
  1194. if ((m->sections[j]->flags & SEC_READONLY) == 0)
  1195. p_flags1 |= PF_W;
  1196. if ((m->sections[j]->flags & SEC_CODE) != 0)
  1197. {
  1198. p_flags1 |= PF_X;
  1199. if ((elf_section_flags (m->sections[j]) & SHF_PPC_VLE) != 0)
  1200. p_flags1 |= PF_PPC_VLE;
  1201. if (((p_flags1 ^ p_flags) & PF_PPC_VLE) != 0)
  1202. break;
  1203. }
  1204. p_flags |= p_flags1;
  1205. }
  1206. /* If we're splitting a segment which originally contained rw
  1207. sections then those sections might now only be in one of the
  1208. two parts. So always set p_flags if splitting, even if we
  1209. are being called for objcopy with p_flags_valid set. */
  1210. if (j != m->count || !m->p_flags_valid)
  1211. {
  1212. m->p_flags_valid = 1;
  1213. m->p_flags = p_flags;
  1214. }
  1215. if (j == m->count)
  1216. continue;
  1217. /* Sections 0..j-1 stay in this (current) segment,
  1218. the remainder are put in a new segment.
  1219. The scan resumes with the new segment. */
  1220. amt = sizeof (struct elf_segment_map);
  1221. amt += (m->count - j - 1) * sizeof (asection *);
  1222. n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  1223. if (n == NULL)
  1224. return false;
  1225. n->p_type = PT_LOAD;
  1226. n->count = m->count - j;
  1227. for (k = 0; k < n->count; ++k)
  1228. n->sections[k] = m->sections[j + k];
  1229. m->count = j;
  1230. m->p_size_valid = 0;
  1231. n->next = m->next;
  1232. m->next = n;
  1233. }
  1234. return true;
  1235. }
  1236. /* Add extra PPC sections -- Note, for now, make .sbss2 and
  1237. .PPC.EMB.sbss0 a normal section, and not a bss section so
  1238. that the linker doesn't crater when trying to make more than
  1239. 2 sections. */
  1240. static const struct bfd_elf_special_section ppc_elf_special_sections[] =
  1241. {
  1242. { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
  1243. { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  1244. { STRING_COMMA_LEN (".sbss2"), -2, SHT_PROGBITS, SHF_ALLOC },
  1245. { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  1246. { STRING_COMMA_LEN (".sdata2"), -2, SHT_PROGBITS, SHF_ALLOC },
  1247. { STRING_COMMA_LEN (".tags"), 0, SHT_ORDERED, SHF_ALLOC },
  1248. { STRING_COMMA_LEN (APUINFO_SECTION_NAME), 0, SHT_NOTE, 0 },
  1249. { STRING_COMMA_LEN (".PPC.EMB.sbss0"), 0, SHT_PROGBITS, SHF_ALLOC },
  1250. { STRING_COMMA_LEN (".PPC.EMB.sdata0"), 0, SHT_PROGBITS, SHF_ALLOC },
  1251. { NULL, 0, 0, 0, 0 }
  1252. };
  1253. /* This is what we want for new plt/got. */
  1254. static const struct bfd_elf_special_section ppc_alt_plt =
  1255. { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC };
  1256. static const struct bfd_elf_special_section *
  1257. ppc_elf_get_sec_type_attr (bfd *abfd, asection *sec)
  1258. {
  1259. const struct bfd_elf_special_section *ssect;
  1260. /* See if this is one of the special sections. */
  1261. if (sec->name == NULL)
  1262. return NULL;
  1263. ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
  1264. sec->use_rela_p);
  1265. if (ssect != NULL)
  1266. {
  1267. if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
  1268. ssect = &ppc_alt_plt;
  1269. return ssect;
  1270. }
  1271. return _bfd_elf_get_sec_type_attr (abfd, sec);
  1272. }
  1273. /* Very simple linked list structure for recording apuinfo values. */
  1274. typedef struct apuinfo_list
  1275. {
  1276. struct apuinfo_list *next;
  1277. unsigned long value;
  1278. }
  1279. apuinfo_list;
  1280. static apuinfo_list *head;
  1281. static bool apuinfo_set;
  1282. static void
  1283. apuinfo_list_init (void)
  1284. {
  1285. head = NULL;
  1286. apuinfo_set = false;
  1287. }
  1288. static void
  1289. apuinfo_list_add (unsigned long value)
  1290. {
  1291. apuinfo_list *entry = head;
  1292. while (entry != NULL)
  1293. {
  1294. if (entry->value == value)
  1295. return;
  1296. entry = entry->next;
  1297. }
  1298. entry = bfd_malloc (sizeof (* entry));
  1299. if (entry == NULL)
  1300. return;
  1301. entry->value = value;
  1302. entry->next = head;
  1303. head = entry;
  1304. }
  1305. static unsigned
  1306. apuinfo_list_length (void)
  1307. {
  1308. apuinfo_list *entry;
  1309. unsigned long count;
  1310. for (entry = head, count = 0;
  1311. entry;
  1312. entry = entry->next)
  1313. ++ count;
  1314. return count;
  1315. }
  1316. static inline unsigned long
  1317. apuinfo_list_element (unsigned long number)
  1318. {
  1319. apuinfo_list * entry;
  1320. for (entry = head;
  1321. entry && number --;
  1322. entry = entry->next)
  1323. ;
  1324. return entry ? entry->value : 0;
  1325. }
  1326. static void
  1327. apuinfo_list_finish (void)
  1328. {
  1329. apuinfo_list *entry;
  1330. for (entry = head; entry;)
  1331. {
  1332. apuinfo_list *next = entry->next;
  1333. free (entry);
  1334. entry = next;
  1335. }
  1336. head = NULL;
  1337. }
  1338. /* Scan the input BFDs and create a linked list of
  1339. the APUinfo values that will need to be emitted. */
  1340. static void
  1341. ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
  1342. {
  1343. bfd *ibfd;
  1344. asection *asec;
  1345. char *buffer = NULL;
  1346. bfd_size_type largest_input_size = 0;
  1347. unsigned i;
  1348. unsigned long length;
  1349. const char *error_message = NULL;
  1350. if (link_info == NULL)
  1351. return;
  1352. apuinfo_list_init ();
  1353. /* Read in the input sections contents. */
  1354. for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link.next)
  1355. {
  1356. unsigned long datum;
  1357. asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
  1358. if (asec == NULL)
  1359. continue;
  1360. /* xgettext:c-format */
  1361. error_message = _("corrupt %s section in %pB");
  1362. length = asec->size;
  1363. if (length < 20)
  1364. goto fail;
  1365. apuinfo_set = true;
  1366. if (largest_input_size < asec->size)
  1367. {
  1368. free (buffer);
  1369. largest_input_size = asec->size;
  1370. buffer = bfd_malloc (largest_input_size);
  1371. if (!buffer)
  1372. return;
  1373. }
  1374. if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
  1375. || (bfd_bread (buffer, length, ibfd) != length))
  1376. {
  1377. /* xgettext:c-format */
  1378. error_message = _("unable to read in %s section from %pB");
  1379. goto fail;
  1380. }
  1381. /* Verify the contents of the header. Note - we have to
  1382. extract the values this way in order to allow for a
  1383. host whose endian-ness is different from the target. */
  1384. datum = bfd_get_32 (ibfd, buffer);
  1385. if (datum != sizeof APUINFO_LABEL)
  1386. goto fail;
  1387. datum = bfd_get_32 (ibfd, buffer + 8);
  1388. if (datum != 0x2)
  1389. goto fail;
  1390. if (strcmp (buffer + 12, APUINFO_LABEL) != 0)
  1391. goto fail;
  1392. /* Get the number of bytes used for apuinfo entries. */
  1393. datum = bfd_get_32 (ibfd, buffer + 4);
  1394. if (datum + 20 != length)
  1395. goto fail;
  1396. /* Scan the apuinfo section, building a list of apuinfo numbers. */
  1397. for (i = 0; i < datum; i += 4)
  1398. apuinfo_list_add (bfd_get_32 (ibfd, buffer + 20 + i));
  1399. }
  1400. error_message = NULL;
  1401. if (apuinfo_set)
  1402. {
  1403. /* Compute the size of the output section. */
  1404. unsigned num_entries = apuinfo_list_length ();
  1405. /* Set the output section size, if it exists. */
  1406. asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
  1407. if (asec && !bfd_set_section_size (asec, 20 + num_entries * 4))
  1408. {
  1409. ibfd = abfd;
  1410. /* xgettext:c-format */
  1411. error_message = _("warning: unable to set size of %s section in %pB");
  1412. }
  1413. }
  1414. fail:
  1415. free (buffer);
  1416. if (error_message)
  1417. _bfd_error_handler (error_message, APUINFO_SECTION_NAME, ibfd);
  1418. }
  1419. /* Prevent the output section from accumulating the input sections'
  1420. contents. We have already stored this in our linked list structure. */
  1421. static bool
  1422. ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
  1423. struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
  1424. asection *asec,
  1425. bfd_byte *contents ATTRIBUTE_UNUSED)
  1426. {
  1427. return apuinfo_set && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
  1428. }
  1429. /* Finally we can generate the output section. */
  1430. static void
  1431. ppc_final_write_processing (bfd *abfd)
  1432. {
  1433. bfd_byte *buffer;
  1434. asection *asec;
  1435. unsigned i;
  1436. unsigned num_entries;
  1437. bfd_size_type length;
  1438. asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
  1439. if (asec == NULL)
  1440. return;
  1441. if (!apuinfo_set)
  1442. return;
  1443. length = asec->size;
  1444. if (length < 20)
  1445. return;
  1446. buffer = bfd_malloc (length);
  1447. if (buffer == NULL)
  1448. {
  1449. _bfd_error_handler
  1450. (_("failed to allocate space for new APUinfo section"));
  1451. return;
  1452. }
  1453. /* Create the apuinfo header. */
  1454. num_entries = apuinfo_list_length ();
  1455. bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
  1456. bfd_put_32 (abfd, num_entries * 4, buffer + 4);
  1457. bfd_put_32 (abfd, 0x2, buffer + 8);
  1458. strcpy ((char *) buffer + 12, APUINFO_LABEL);
  1459. length = 20;
  1460. for (i = 0; i < num_entries; i++)
  1461. {
  1462. bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
  1463. length += 4;
  1464. }
  1465. if (length != asec->size)
  1466. _bfd_error_handler (_("failed to compute new APUinfo section"));
  1467. if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
  1468. _bfd_error_handler (_("failed to install new APUinfo section"));
  1469. free (buffer);
  1470. apuinfo_list_finish ();
  1471. }
  1472. static bool
  1473. ppc_elf_final_write_processing (bfd *abfd)
  1474. {
  1475. ppc_final_write_processing (abfd);
  1476. return _bfd_elf_final_write_processing (abfd);
  1477. }
  1478. static bool
  1479. is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
  1480. {
  1481. bfd_byte buf[4 * 4];
  1482. if (!bfd_get_section_contents (abfd, glink, buf, off, sizeof buf))
  1483. return false;
  1484. return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
  1485. && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
  1486. && bfd_get_32 (abfd, buf + 8) == MTCTR_11
  1487. && bfd_get_32 (abfd, buf + 12) == BCTR);
  1488. }
  1489. static bool
  1490. section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
  1491. {
  1492. bfd_vma vma = *(bfd_vma *) ptr;
  1493. return ((section->flags & SEC_ALLOC) != 0
  1494. && section->vma <= vma
  1495. && vma < section->vma + section->size);
  1496. }
  1497. static long
  1498. ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
  1499. long dynsymcount, asymbol **dynsyms,
  1500. asymbol **ret)
  1501. {
  1502. bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
  1503. asection *plt, *relplt, *dynamic, *glink;
  1504. bfd_vma glink_vma = 0;
  1505. bfd_vma resolv_vma = 0;
  1506. bfd_vma stub_off;
  1507. asymbol *s;
  1508. arelent *p;
  1509. size_t count, i, stub_delta;
  1510. size_t size;
  1511. char *names;
  1512. bfd_byte buf[4];
  1513. *ret = NULL;
  1514. if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
  1515. return 0;
  1516. if (dynsymcount <= 0)
  1517. return 0;
  1518. relplt = bfd_get_section_by_name (abfd, ".rela.plt");
  1519. if (relplt == NULL)
  1520. return 0;
  1521. plt = bfd_get_section_by_name (abfd, ".plt");
  1522. if (plt == NULL)
  1523. return 0;
  1524. /* Call common code to handle old-style executable PLTs. */
  1525. if (elf_section_flags (plt) & SHF_EXECINSTR)
  1526. return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
  1527. dynsymcount, dynsyms, ret);
  1528. /* If this object was prelinked, the prelinker stored the address
  1529. of .glink at got[1]. If it wasn't prelinked, got[1] will be zero. */
  1530. dynamic = bfd_get_section_by_name (abfd, ".dynamic");
  1531. if (dynamic != NULL)
  1532. {
  1533. bfd_byte *dynbuf, *extdyn, *extdynend;
  1534. size_t extdynsize;
  1535. void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
  1536. if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
  1537. return -1;
  1538. extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
  1539. swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
  1540. extdyn = dynbuf;
  1541. extdynend = extdyn + dynamic->size;
  1542. for (; extdyn < extdynend; extdyn += extdynsize)
  1543. {
  1544. Elf_Internal_Dyn dyn;
  1545. (*swap_dyn_in) (abfd, extdyn, &dyn);
  1546. if (dyn.d_tag == DT_NULL)
  1547. break;
  1548. if (dyn.d_tag == DT_PPC_GOT)
  1549. {
  1550. unsigned int g_o_t = dyn.d_un.d_val;
  1551. asection *got = bfd_get_section_by_name (abfd, ".got");
  1552. if (got != NULL
  1553. && bfd_get_section_contents (abfd, got, buf,
  1554. g_o_t - got->vma + 4, 4))
  1555. glink_vma = bfd_get_32 (abfd, buf);
  1556. break;
  1557. }
  1558. }
  1559. free (dynbuf);
  1560. }
  1561. /* Otherwise we read the first plt entry. */
  1562. if (glink_vma == 0)
  1563. {
  1564. if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
  1565. glink_vma = bfd_get_32 (abfd, buf);
  1566. }
  1567. if (glink_vma == 0)
  1568. return 0;
  1569. /* The .glink section usually does not survive the final
  1570. link; search for the section (usually .text) where the
  1571. glink stubs now reside. */
  1572. glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
  1573. if (glink == NULL)
  1574. return 0;
  1575. /* Determine glink PLT resolver by reading the relative branch
  1576. from the first glink stub. */
  1577. if (bfd_get_section_contents (abfd, glink, buf,
  1578. glink_vma - glink->vma, 4))
  1579. {
  1580. unsigned int insn = bfd_get_32 (abfd, buf);
  1581. /* The first glink stub may either branch to the resolver ... */
  1582. insn ^= B;
  1583. if ((insn & ~0x3fffffc) == 0)
  1584. resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
  1585. /* ... or fall through a bunch of NOPs. */
  1586. else if ((insn ^ B ^ NOP) == 0)
  1587. for (i = 4;
  1588. bfd_get_section_contents (abfd, glink, buf,
  1589. glink_vma - glink->vma + i, 4);
  1590. i += 4)
  1591. if (bfd_get_32 (abfd, buf) != NOP)
  1592. {
  1593. resolv_vma = glink_vma + i;
  1594. break;
  1595. }
  1596. }
  1597. count = relplt->size / sizeof (Elf32_External_Rela);
  1598. /* If the stubs are those for -shared/-pie then we might have
  1599. multiple stubs for each plt entry. If that is the case then
  1600. there is no way to associate stubs with their plt entries short
  1601. of figuring out the GOT pointer value used in the stub.
  1602. The offsets tested here need to cover all possible values of
  1603. GLINK_ENTRY_SIZE for other than __tls_get_addr_opt. */
  1604. stub_off = glink_vma - glink->vma;
  1605. for (stub_delta = 16; stub_delta <= 32; stub_delta += 8)
  1606. if (is_nonpic_glink_stub (abfd, glink, stub_off - stub_delta))
  1607. break;
  1608. if (stub_delta > 32)
  1609. return 0;
  1610. slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
  1611. if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
  1612. return -1;
  1613. size = count * sizeof (asymbol);
  1614. p = relplt->relocation;
  1615. for (i = 0; i < count; i++, p++)
  1616. {
  1617. size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
  1618. if (p->addend != 0)
  1619. size += sizeof ("+0x") - 1 + 8;
  1620. }
  1621. size += sizeof (asymbol) + sizeof ("__glink");
  1622. if (resolv_vma)
  1623. size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
  1624. s = *ret = bfd_malloc (size);
  1625. if (s == NULL)
  1626. return -1;
  1627. stub_off = glink_vma - glink->vma;
  1628. names = (char *) (s + count + 1 + (resolv_vma != 0));
  1629. p = relplt->relocation + count - 1;
  1630. for (i = 0; i < count; i++)
  1631. {
  1632. size_t len;
  1633. stub_off -= stub_delta;
  1634. if (strcmp ((*p->sym_ptr_ptr)->name, "__tls_get_addr_opt") == 0)
  1635. stub_off -= 32;
  1636. *s = **p->sym_ptr_ptr;
  1637. /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
  1638. we are defining a symbol, ensure one of them is set. */
  1639. if ((s->flags & BSF_LOCAL) == 0)
  1640. s->flags |= BSF_GLOBAL;
  1641. s->flags |= BSF_SYNTHETIC;
  1642. s->section = glink;
  1643. s->value = stub_off;
  1644. s->name = names;
  1645. s->udata.p = NULL;
  1646. len = strlen ((*p->sym_ptr_ptr)->name);
  1647. memcpy (names, (*p->sym_ptr_ptr)->name, len);
  1648. names += len;
  1649. if (p->addend != 0)
  1650. {
  1651. memcpy (names, "+0x", sizeof ("+0x") - 1);
  1652. names += sizeof ("+0x") - 1;
  1653. bfd_sprintf_vma (abfd, names, p->addend);
  1654. names += strlen (names);
  1655. }
  1656. memcpy (names, "@plt", sizeof ("@plt"));
  1657. names += sizeof ("@plt");
  1658. ++s;
  1659. --p;
  1660. }
  1661. /* Add a symbol at the start of the glink branch table. */
  1662. memset (s, 0, sizeof *s);
  1663. s->the_bfd = abfd;
  1664. s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
  1665. s->section = glink;
  1666. s->value = glink_vma - glink->vma;
  1667. s->name = names;
  1668. memcpy (names, "__glink", sizeof ("__glink"));
  1669. names += sizeof ("__glink");
  1670. s++;
  1671. count++;
  1672. if (resolv_vma)
  1673. {
  1674. /* Add a symbol for the glink PLT resolver. */
  1675. memset (s, 0, sizeof *s);
  1676. s->the_bfd = abfd;
  1677. s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
  1678. s->section = glink;
  1679. s->value = resolv_vma - glink->vma;
  1680. s->name = names;
  1681. memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
  1682. names += sizeof ("__glink_PLTresolve");
  1683. s++;
  1684. count++;
  1685. }
  1686. return count;
  1687. }
  1688. /* The following functions are specific to the ELF linker, while
  1689. functions above are used generally. They appear in this file more
  1690. or less in the order in which they are called. eg.
  1691. ppc_elf_check_relocs is called early in the link process,
  1692. ppc_elf_finish_dynamic_sections is one of the last functions
  1693. called. */
  1694. /* Track PLT entries needed for a given symbol. We might need more
  1695. than one glink entry per symbol when generating a pic binary. */
  1696. struct plt_entry
  1697. {
  1698. struct plt_entry *next;
  1699. /* -fPIC uses multiple GOT sections, one per file, called ".got2".
  1700. This field stores the offset into .got2 used to initialise the
  1701. GOT pointer reg. It will always be at least 32768. (Current
  1702. gcc always uses an offset of 32768, but ld -r will pack .got2
  1703. sections together resulting in larger offsets). */
  1704. bfd_vma addend;
  1705. /* The .got2 section. */
  1706. asection *sec;
  1707. /* PLT refcount or offset. */
  1708. union
  1709. {
  1710. bfd_signed_vma refcount;
  1711. bfd_vma offset;
  1712. } plt;
  1713. /* .glink stub offset. */
  1714. bfd_vma glink_offset;
  1715. };
  1716. /* Of those relocs that might be copied as dynamic relocs, this
  1717. function selects those that must be copied when linking a shared
  1718. library or PIE, even when the symbol is local. */
  1719. static int
  1720. must_be_dyn_reloc (struct bfd_link_info *info,
  1721. enum elf_ppc_reloc_type r_type)
  1722. {
  1723. switch (r_type)
  1724. {
  1725. default:
  1726. /* Only relative relocs can be resolved when the object load
  1727. address isn't fixed. DTPREL32 is excluded because the
  1728. dynamic linker needs to differentiate global dynamic from
  1729. local dynamic __tls_index pairs when PPC_OPT_TLS is set. */
  1730. return 1;
  1731. case R_PPC_REL24:
  1732. case R_PPC_REL14:
  1733. case R_PPC_REL14_BRTAKEN:
  1734. case R_PPC_REL14_BRNTAKEN:
  1735. case R_PPC_REL32:
  1736. return 0;
  1737. case R_PPC_TPREL32:
  1738. case R_PPC_TPREL16:
  1739. case R_PPC_TPREL16_LO:
  1740. case R_PPC_TPREL16_HI:
  1741. case R_PPC_TPREL16_HA:
  1742. /* These relocations are relative but in a shared library the
  1743. linker doesn't know the thread pointer base. */
  1744. return bfd_link_dll (info);
  1745. }
  1746. }
  1747. /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
  1748. copying dynamic variables from a shared lib into an app's dynbss
  1749. section, and instead use a dynamic relocation to point into the
  1750. shared lib. */
  1751. #define ELIMINATE_COPY_RELOCS 1
  1752. /* Used to track dynamic relocations for local symbols. */
  1753. struct ppc_dyn_relocs
  1754. {
  1755. struct ppc_dyn_relocs *next;
  1756. /* The input section of the reloc. */
  1757. asection *sec;
  1758. /* Total number of relocs copied for the input section. */
  1759. unsigned int count : 31;
  1760. /* Whether this entry is for STT_GNU_IFUNC symbols. */
  1761. unsigned int ifunc : 1;
  1762. };
  1763. /* PPC ELF linker hash entry. */
  1764. struct ppc_elf_link_hash_entry
  1765. {
  1766. struct elf_link_hash_entry elf;
  1767. /* If this symbol is used in the linker created sections, the processor
  1768. specific backend uses this field to map the field into the offset
  1769. from the beginning of the section. */
  1770. elf_linker_section_pointers_t *linker_section_pointer;
  1771. /* Contexts in which symbol is used in the GOT.
  1772. Bits are or'd into the mask as the corresponding relocs are
  1773. encountered during check_relocs, with TLS_TLS being set when any
  1774. of the other TLS bits are set. tls_optimize clears bits when
  1775. optimizing to indicate the corresponding GOT entry type is not
  1776. needed. If set, TLS_TLS is never cleared. tls_optimize may also
  1777. set TLS_GDIE when a GD reloc turns into an IE one.
  1778. These flags are also kept for local symbols. */
  1779. #define TLS_TLS 1 /* Any TLS reloc. */
  1780. #define TLS_GD 2 /* GD reloc. */
  1781. #define TLS_LD 4 /* LD reloc. */
  1782. #define TLS_TPREL 8 /* TPREL reloc, => IE. */
  1783. #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
  1784. #define TLS_MARK 32 /* __tls_get_addr call marked. */
  1785. #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
  1786. unsigned char tls_mask;
  1787. /* The above field is also used to mark function symbols. In which
  1788. case TLS_TLS will be 0. */
  1789. #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
  1790. #define PLT_KEEP 4 /* inline plt call requires plt entry. */
  1791. #define NON_GOT 256 /* local symbol plt, not stored. */
  1792. /* Nonzero if we have seen a small data relocation referring to this
  1793. symbol. */
  1794. unsigned char has_sda_refs : 1;
  1795. /* Flag use of given relocations. */
  1796. unsigned char has_addr16_ha : 1;
  1797. unsigned char has_addr16_lo : 1;
  1798. };
  1799. #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
  1800. /* PPC ELF linker hash table. */
  1801. struct ppc_elf_link_hash_table
  1802. {
  1803. struct elf_link_hash_table elf;
  1804. /* Various options passed from the linker. */
  1805. struct ppc_elf_params *params;
  1806. /* Short-cuts to get to dynamic linker sections. */
  1807. asection *glink;
  1808. asection *dynsbss;
  1809. asection *relsbss;
  1810. elf_linker_section_t sdata[2];
  1811. asection *sbss;
  1812. asection *glink_eh_frame;
  1813. asection *pltlocal;
  1814. asection *relpltlocal;
  1815. /* The (unloaded but important) .rela.plt.unloaded on VxWorks. */
  1816. asection *srelplt2;
  1817. /* Shortcut to __tls_get_addr. */
  1818. struct elf_link_hash_entry *tls_get_addr;
  1819. /* The bfd that forced an old-style PLT. */
  1820. bfd *old_bfd;
  1821. /* TLS local dynamic got entry handling. */
  1822. union {
  1823. bfd_signed_vma refcount;
  1824. bfd_vma offset;
  1825. } tlsld_got;
  1826. /* Offset of branch table to PltResolve function in glink. */
  1827. bfd_vma glink_pltresolve;
  1828. /* Size of reserved GOT entries. */
  1829. unsigned int got_header_size;
  1830. /* Non-zero if allocating the header left a gap. */
  1831. unsigned int got_gap;
  1832. /* The type of PLT we have chosen to use. */
  1833. enum ppc_elf_plt_type plt_type;
  1834. /* Whether there exist local gnu indirect function resolvers,
  1835. referenced by dynamic relocations. */
  1836. unsigned int local_ifunc_resolver:1;
  1837. unsigned int maybe_local_ifunc_resolver:1;
  1838. /* Set if tls optimization is enabled. */
  1839. unsigned int do_tls_opt:1;
  1840. /* Set if inline plt calls should be converted to direct calls. */
  1841. unsigned int can_convert_all_inline_plt:1;
  1842. /* The size of PLT entries. */
  1843. int plt_entry_size;
  1844. /* The distance between adjacent PLT slots. */
  1845. int plt_slot_size;
  1846. /* The size of the first PLT entry. */
  1847. int plt_initial_entry_size;
  1848. };
  1849. /* Rename some of the generic section flags to better document how they
  1850. are used for ppc32. The flags are only valid for ppc32 elf objects. */
  1851. /* Nonzero if this section has TLS related relocations. */
  1852. #define has_tls_reloc sec_flg0
  1853. /* Nonzero if this section has a call to __tls_get_addr lacking marker
  1854. relocs. */
  1855. #define nomark_tls_get_addr sec_flg1
  1856. /* Flag set when PLTCALL relocs are detected. */
  1857. #define has_pltcall sec_flg2
  1858. /* Get the PPC ELF linker hash table from a link_info structure. */
  1859. #define ppc_elf_hash_table(p) \
  1860. ((is_elf_hash_table ((p)->hash) \
  1861. && elf_hash_table_id (elf_hash_table (p)) == PPC32_ELF_DATA) \
  1862. ? (struct ppc_elf_link_hash_table *) (p)->hash : NULL)
  1863. /* Create an entry in a PPC ELF linker hash table. */
  1864. static struct bfd_hash_entry *
  1865. ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
  1866. struct bfd_hash_table *table,
  1867. const char *string)
  1868. {
  1869. /* Allocate the structure if it has not already been allocated by a
  1870. subclass. */
  1871. if (entry == NULL)
  1872. {
  1873. entry = bfd_hash_allocate (table,
  1874. sizeof (struct ppc_elf_link_hash_entry));
  1875. if (entry == NULL)
  1876. return entry;
  1877. }
  1878. /* Call the allocation method of the superclass. */
  1879. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  1880. if (entry != NULL)
  1881. {
  1882. ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
  1883. ppc_elf_hash_entry (entry)->tls_mask = 0;
  1884. ppc_elf_hash_entry (entry)->has_sda_refs = 0;
  1885. }
  1886. return entry;
  1887. }
  1888. /* Create a PPC ELF linker hash table. */
  1889. static struct bfd_link_hash_table *
  1890. ppc_elf_link_hash_table_create (bfd *abfd)
  1891. {
  1892. struct ppc_elf_link_hash_table *ret;
  1893. static struct ppc_elf_params default_params
  1894. = { PLT_OLD, 0, 0, 1, 0, 0, 12, 0, 0, 0 };
  1895. ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
  1896. if (ret == NULL)
  1897. return NULL;
  1898. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
  1899. ppc_elf_link_hash_newfunc,
  1900. sizeof (struct ppc_elf_link_hash_entry),
  1901. PPC32_ELF_DATA))
  1902. {
  1903. free (ret);
  1904. return NULL;
  1905. }
  1906. ret->elf.init_plt_refcount.refcount = 0;
  1907. ret->elf.init_plt_refcount.glist = NULL;
  1908. ret->elf.init_plt_offset.offset = 0;
  1909. ret->elf.init_plt_offset.glist = NULL;
  1910. ret->params = &default_params;
  1911. ret->sdata[0].name = ".sdata";
  1912. ret->sdata[0].sym_name = "_SDA_BASE_";
  1913. ret->sdata[0].bss_name = ".sbss";
  1914. ret->sdata[1].name = ".sdata2";
  1915. ret->sdata[1].sym_name = "_SDA2_BASE_";
  1916. ret->sdata[1].bss_name = ".sbss2";
  1917. ret->plt_entry_size = 12;
  1918. ret->plt_slot_size = 8;
  1919. ret->plt_initial_entry_size = 72;
  1920. return &ret->elf.root;
  1921. }
  1922. /* Hook linker params into hash table. */
  1923. void
  1924. ppc_elf_link_params (struct bfd_link_info *info, struct ppc_elf_params *params)
  1925. {
  1926. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  1927. if (htab)
  1928. htab->params = params;
  1929. params->pagesize_p2 = bfd_log2 (params->pagesize);
  1930. }
  1931. /* Create .got and the related sections. */
  1932. static bool
  1933. ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
  1934. {
  1935. struct ppc_elf_link_hash_table *htab;
  1936. if (!_bfd_elf_create_got_section (abfd, info))
  1937. return false;
  1938. htab = ppc_elf_hash_table (info);
  1939. if (htab->elf.target_os != is_vxworks)
  1940. {
  1941. /* The powerpc .got has a blrl instruction in it. Mark it
  1942. executable. */
  1943. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
  1944. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  1945. if (!bfd_set_section_flags (htab->elf.sgot, flags))
  1946. return false;
  1947. }
  1948. return true;
  1949. }
  1950. /* Create a special linker section, used for R_PPC_EMB_SDAI16 and
  1951. R_PPC_EMB_SDA2I16 pointers. These sections become part of .sdata
  1952. and .sdata2. Create _SDA_BASE_ and _SDA2_BASE too. */
  1953. static bool
  1954. ppc_elf_create_linker_section (bfd *abfd,
  1955. struct bfd_link_info *info,
  1956. flagword flags,
  1957. elf_linker_section_t *lsect)
  1958. {
  1959. asection *s;
  1960. flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1961. | SEC_LINKER_CREATED);
  1962. s = bfd_make_section_anyway_with_flags (abfd, lsect->name, flags);
  1963. if (s == NULL)
  1964. return false;
  1965. lsect->section = s;
  1966. /* Define the sym on the first section of this name. */
  1967. s = bfd_get_section_by_name (abfd, lsect->name);
  1968. lsect->sym = _bfd_elf_define_linkage_sym (abfd, info, s, lsect->sym_name);
  1969. if (lsect->sym == NULL)
  1970. return false;
  1971. lsect->sym->root.u.def.value = 0x8000;
  1972. return true;
  1973. }
  1974. static bool
  1975. ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
  1976. {
  1977. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  1978. asection *s;
  1979. flagword flags;
  1980. int p2align;
  1981. flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
  1982. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  1983. s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
  1984. htab->glink = s;
  1985. p2align = htab->params->ppc476_workaround ? 6 : 4;
  1986. if (p2align < htab->params->plt_stub_align)
  1987. p2align = htab->params->plt_stub_align;
  1988. if (s == NULL
  1989. || !bfd_set_section_alignment (s, p2align))
  1990. return false;
  1991. if (!info->no_ld_generated_unwind_info)
  1992. {
  1993. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
  1994. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  1995. s = bfd_make_section_anyway_with_flags (abfd, ".eh_frame", flags);
  1996. htab->glink_eh_frame = s;
  1997. if (s == NULL
  1998. || !bfd_set_section_alignment (s, 2))
  1999. return false;
  2000. }
  2001. flags = SEC_ALLOC | SEC_LINKER_CREATED;
  2002. s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
  2003. htab->elf.iplt = s;
  2004. if (s == NULL
  2005. || !bfd_set_section_alignment (s, 4))
  2006. return false;
  2007. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
  2008. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  2009. s = bfd_make_section_anyway_with_flags (abfd, ".rela.iplt", flags);
  2010. htab->elf.irelplt = s;
  2011. if (s == NULL
  2012. || ! bfd_set_section_alignment (s, 2))
  2013. return false;
  2014. /* Local plt entries. */
  2015. flags = (SEC_ALLOC | SEC_LOAD
  2016. | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  2017. htab->pltlocal = bfd_make_section_anyway_with_flags (abfd, ".branch_lt",
  2018. flags);
  2019. if (htab->pltlocal == NULL
  2020. || !bfd_set_section_alignment (htab->pltlocal, 2))
  2021. return false;
  2022. if (bfd_link_pic (info))
  2023. {
  2024. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
  2025. | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  2026. htab->relpltlocal
  2027. = bfd_make_section_anyway_with_flags (abfd, ".rela.branch_lt", flags);
  2028. if (htab->relpltlocal == NULL
  2029. || !bfd_set_section_alignment (htab->relpltlocal, 2))
  2030. return false;
  2031. }
  2032. if (!ppc_elf_create_linker_section (abfd, info, 0,
  2033. &htab->sdata[0]))
  2034. return false;
  2035. if (!ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
  2036. &htab->sdata[1]))
  2037. return false;
  2038. return true;
  2039. }
  2040. /* We have to create .dynsbss and .rela.sbss here so that they get mapped
  2041. to output sections (just like _bfd_elf_create_dynamic_sections has
  2042. to create .dynbss and .rela.bss). */
  2043. static bool
  2044. ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  2045. {
  2046. struct ppc_elf_link_hash_table *htab;
  2047. asection *s;
  2048. flagword flags;
  2049. htab = ppc_elf_hash_table (info);
  2050. if (htab->elf.sgot == NULL
  2051. && !ppc_elf_create_got (abfd, info))
  2052. return false;
  2053. if (!_bfd_elf_create_dynamic_sections (abfd, info))
  2054. return false;
  2055. if (htab->glink == NULL
  2056. && !ppc_elf_create_glink (abfd, info))
  2057. return false;
  2058. s = bfd_make_section_anyway_with_flags (abfd, ".dynsbss",
  2059. SEC_ALLOC | SEC_LINKER_CREATED);
  2060. htab->dynsbss = s;
  2061. if (s == NULL)
  2062. return false;
  2063. if (! bfd_link_pic (info))
  2064. {
  2065. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
  2066. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  2067. s = bfd_make_section_anyway_with_flags (abfd, ".rela.sbss", flags);
  2068. htab->relsbss = s;
  2069. if (s == NULL
  2070. || !bfd_set_section_alignment (s, 2))
  2071. return false;
  2072. }
  2073. if (htab->elf.target_os == is_vxworks
  2074. && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
  2075. return false;
  2076. s = htab->elf.splt;
  2077. flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
  2078. if (htab->plt_type == PLT_VXWORKS)
  2079. /* The VxWorks PLT is a loaded section with contents. */
  2080. flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
  2081. return bfd_set_section_flags (s, flags);
  2082. }
  2083. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  2084. static void
  2085. ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
  2086. struct elf_link_hash_entry *dir,
  2087. struct elf_link_hash_entry *ind)
  2088. {
  2089. struct ppc_elf_link_hash_entry *edir, *eind;
  2090. edir = (struct ppc_elf_link_hash_entry *) dir;
  2091. eind = (struct ppc_elf_link_hash_entry *) ind;
  2092. edir->tls_mask |= eind->tls_mask;
  2093. edir->has_sda_refs |= eind->has_sda_refs;
  2094. if (edir->elf.versioned != versioned_hidden)
  2095. edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
  2096. edir->elf.ref_regular |= eind->elf.ref_regular;
  2097. edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
  2098. edir->elf.non_got_ref |= eind->elf.non_got_ref;
  2099. edir->elf.needs_plt |= eind->elf.needs_plt;
  2100. edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
  2101. /* If we were called to copy over info for a weak sym, that's all. */
  2102. if (eind->elf.root.type != bfd_link_hash_indirect)
  2103. return;
  2104. if (ind->dyn_relocs != NULL)
  2105. {
  2106. if (dir->dyn_relocs != NULL)
  2107. {
  2108. struct elf_dyn_relocs **pp;
  2109. struct elf_dyn_relocs *p;
  2110. /* Add reloc counts against the indirect sym to the direct sym
  2111. list. Merge any entries against the same section. */
  2112. for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
  2113. {
  2114. struct elf_dyn_relocs *q;
  2115. for (q = dir->dyn_relocs; q != NULL; q = q->next)
  2116. if (q->sec == p->sec)
  2117. {
  2118. q->pc_count += p->pc_count;
  2119. q->count += p->count;
  2120. *pp = p->next;
  2121. break;
  2122. }
  2123. if (q == NULL)
  2124. pp = &p->next;
  2125. }
  2126. *pp = dir->dyn_relocs;
  2127. }
  2128. dir->dyn_relocs = ind->dyn_relocs;
  2129. ind->dyn_relocs = NULL;
  2130. }
  2131. /* Copy over the GOT refcount entries that we may have already seen to
  2132. the symbol which just became indirect. */
  2133. edir->elf.got.refcount += eind->elf.got.refcount;
  2134. eind->elf.got.refcount = 0;
  2135. /* And plt entries. */
  2136. if (eind->elf.plt.plist != NULL)
  2137. {
  2138. if (edir->elf.plt.plist != NULL)
  2139. {
  2140. struct plt_entry **entp;
  2141. struct plt_entry *ent;
  2142. for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
  2143. {
  2144. struct plt_entry *dent;
  2145. for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
  2146. if (dent->sec == ent->sec && dent->addend == ent->addend)
  2147. {
  2148. dent->plt.refcount += ent->plt.refcount;
  2149. *entp = ent->next;
  2150. break;
  2151. }
  2152. if (dent == NULL)
  2153. entp = &ent->next;
  2154. }
  2155. *entp = edir->elf.plt.plist;
  2156. }
  2157. edir->elf.plt.plist = eind->elf.plt.plist;
  2158. eind->elf.plt.plist = NULL;
  2159. }
  2160. if (eind->elf.dynindx != -1)
  2161. {
  2162. if (edir->elf.dynindx != -1)
  2163. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  2164. edir->elf.dynstr_index);
  2165. edir->elf.dynindx = eind->elf.dynindx;
  2166. edir->elf.dynstr_index = eind->elf.dynstr_index;
  2167. eind->elf.dynindx = -1;
  2168. eind->elf.dynstr_index = 0;
  2169. }
  2170. }
  2171. /* Hook called by the linker routine which adds symbols from an object
  2172. file. We use it to put .comm items in .sbss, and not .bss. */
  2173. static bool
  2174. ppc_elf_add_symbol_hook (bfd *abfd,
  2175. struct bfd_link_info *info,
  2176. Elf_Internal_Sym *sym,
  2177. const char **namep ATTRIBUTE_UNUSED,
  2178. flagword *flagsp ATTRIBUTE_UNUSED,
  2179. asection **secp,
  2180. bfd_vma *valp)
  2181. {
  2182. if (sym->st_shndx == SHN_COMMON
  2183. && !bfd_link_relocatable (info)
  2184. && is_ppc_elf (info->output_bfd)
  2185. && sym->st_size <= elf_gp_size (abfd))
  2186. {
  2187. /* Common symbols less than or equal to -G nn bytes are automatically
  2188. put into .sbss. */
  2189. struct ppc_elf_link_hash_table *htab;
  2190. htab = ppc_elf_hash_table (info);
  2191. if (htab->sbss == NULL)
  2192. {
  2193. flagword flags = SEC_IS_COMMON | SEC_SMALL_DATA | SEC_LINKER_CREATED;
  2194. if (!htab->elf.dynobj)
  2195. htab->elf.dynobj = abfd;
  2196. htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
  2197. ".sbss",
  2198. flags);
  2199. if (htab->sbss == NULL)
  2200. return false;
  2201. }
  2202. *secp = htab->sbss;
  2203. *valp = sym->st_size;
  2204. }
  2205. return true;
  2206. }
  2207. /* Find a linker generated pointer with a given addend and type. */
  2208. static elf_linker_section_pointers_t *
  2209. elf_find_pointer_linker_section
  2210. (elf_linker_section_pointers_t *linker_pointers,
  2211. bfd_vma addend,
  2212. elf_linker_section_t *lsect)
  2213. {
  2214. for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
  2215. if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
  2216. return linker_pointers;
  2217. return NULL;
  2218. }
  2219. /* Allocate a pointer to live in a linker created section. */
  2220. static bool
  2221. elf_allocate_pointer_linker_section (bfd *abfd,
  2222. elf_linker_section_t *lsect,
  2223. struct elf_link_hash_entry *h,
  2224. const Elf_Internal_Rela *rel)
  2225. {
  2226. elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
  2227. elf_linker_section_pointers_t *linker_section_ptr;
  2228. unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
  2229. bfd_size_type amt;
  2230. BFD_ASSERT (lsect != NULL);
  2231. /* Is this a global symbol? */
  2232. if (h != NULL)
  2233. {
  2234. struct ppc_elf_link_hash_entry *eh;
  2235. /* Has this symbol already been allocated? If so, our work is done. */
  2236. eh = (struct ppc_elf_link_hash_entry *) h;
  2237. if (elf_find_pointer_linker_section (eh->linker_section_pointer,
  2238. rel->r_addend,
  2239. lsect))
  2240. return true;
  2241. ptr_linker_section_ptr = &eh->linker_section_pointer;
  2242. }
  2243. else
  2244. {
  2245. BFD_ASSERT (is_ppc_elf (abfd));
  2246. /* Allocation of a pointer to a local symbol. */
  2247. elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
  2248. /* Allocate a table to hold the local symbols if first time. */
  2249. if (!ptr)
  2250. {
  2251. unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
  2252. amt = num_symbols;
  2253. amt *= sizeof (elf_linker_section_pointers_t *);
  2254. ptr = bfd_zalloc (abfd, amt);
  2255. if (!ptr)
  2256. return false;
  2257. elf_local_ptr_offsets (abfd) = ptr;
  2258. }
  2259. /* Has this symbol already been allocated? If so, our work is done. */
  2260. if (elf_find_pointer_linker_section (ptr[r_symndx],
  2261. rel->r_addend,
  2262. lsect))
  2263. return true;
  2264. ptr_linker_section_ptr = &ptr[r_symndx];
  2265. }
  2266. /* Allocate space for a pointer in the linker section, and allocate
  2267. a new pointer record from internal memory. */
  2268. BFD_ASSERT (ptr_linker_section_ptr != NULL);
  2269. amt = sizeof (elf_linker_section_pointers_t);
  2270. linker_section_ptr = bfd_alloc (abfd, amt);
  2271. if (!linker_section_ptr)
  2272. return false;
  2273. linker_section_ptr->next = *ptr_linker_section_ptr;
  2274. linker_section_ptr->addend = rel->r_addend;
  2275. linker_section_ptr->lsect = lsect;
  2276. *ptr_linker_section_ptr = linker_section_ptr;
  2277. if (!bfd_set_section_alignment (lsect->section, 2))
  2278. return false;
  2279. linker_section_ptr->offset = lsect->section->size;
  2280. lsect->section->size += 4;
  2281. #ifdef DEBUG
  2282. fprintf (stderr,
  2283. "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
  2284. lsect->name, (long) linker_section_ptr->offset,
  2285. (long) lsect->section->size);
  2286. #endif
  2287. return true;
  2288. }
  2289. static struct plt_entry **
  2290. update_local_sym_info (bfd *abfd,
  2291. Elf_Internal_Shdr *symtab_hdr,
  2292. unsigned long r_symndx,
  2293. int tls_type)
  2294. {
  2295. bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
  2296. struct plt_entry **local_plt;
  2297. unsigned char *local_got_tls_masks;
  2298. if (local_got_refcounts == NULL)
  2299. {
  2300. bfd_size_type size = symtab_hdr->sh_info;
  2301. size *= (sizeof (*local_got_refcounts)
  2302. + sizeof (*local_plt)
  2303. + sizeof (*local_got_tls_masks));
  2304. local_got_refcounts = bfd_zalloc (abfd, size);
  2305. if (local_got_refcounts == NULL)
  2306. return NULL;
  2307. elf_local_got_refcounts (abfd) = local_got_refcounts;
  2308. }
  2309. local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
  2310. local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
  2311. local_got_tls_masks[r_symndx] |= tls_type & 0xff;
  2312. if ((tls_type & NON_GOT) == 0)
  2313. local_got_refcounts[r_symndx] += 1;
  2314. return local_plt + r_symndx;
  2315. }
  2316. static bool
  2317. update_plt_info (bfd *abfd, struct plt_entry **plist,
  2318. asection *sec, bfd_vma addend)
  2319. {
  2320. struct plt_entry *ent;
  2321. if (addend < 32768)
  2322. sec = NULL;
  2323. for (ent = *plist; ent != NULL; ent = ent->next)
  2324. if (ent->sec == sec && ent->addend == addend)
  2325. break;
  2326. if (ent == NULL)
  2327. {
  2328. size_t amt = sizeof (*ent);
  2329. ent = bfd_alloc (abfd, amt);
  2330. if (ent == NULL)
  2331. return false;
  2332. ent->next = *plist;
  2333. ent->sec = sec;
  2334. ent->addend = addend;
  2335. ent->plt.refcount = 0;
  2336. *plist = ent;
  2337. }
  2338. ent->plt.refcount += 1;
  2339. return true;
  2340. }
  2341. static struct plt_entry *
  2342. find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
  2343. {
  2344. struct plt_entry *ent;
  2345. if (addend < 32768)
  2346. sec = NULL;
  2347. for (ent = *plist; ent != NULL; ent = ent->next)
  2348. if (ent->sec == sec && ent->addend == addend)
  2349. break;
  2350. return ent;
  2351. }
  2352. static bool
  2353. is_branch_reloc (enum elf_ppc_reloc_type r_type)
  2354. {
  2355. return (r_type == R_PPC_PLTREL24
  2356. || r_type == R_PPC_LOCAL24PC
  2357. || r_type == R_PPC_REL24
  2358. || r_type == R_PPC_REL14
  2359. || r_type == R_PPC_REL14_BRTAKEN
  2360. || r_type == R_PPC_REL14_BRNTAKEN
  2361. || r_type == R_PPC_ADDR24
  2362. || r_type == R_PPC_ADDR14
  2363. || r_type == R_PPC_ADDR14_BRTAKEN
  2364. || r_type == R_PPC_ADDR14_BRNTAKEN
  2365. || r_type == R_PPC_VLE_REL24);
  2366. }
  2367. /* Relocs on inline plt call sequence insns prior to the call. */
  2368. static bool
  2369. is_plt_seq_reloc (enum elf_ppc_reloc_type r_type)
  2370. {
  2371. return (r_type == R_PPC_PLT16_HA
  2372. || r_type == R_PPC_PLT16_HI
  2373. || r_type == R_PPC_PLT16_LO
  2374. || r_type == R_PPC_PLTSEQ);
  2375. }
  2376. /* Like bfd_reloc_offset_in_range but without a howto. Return true
  2377. iff a field of SIZE bytes at OFFSET is within SEC limits. */
  2378. static bool
  2379. offset_in_range (asection *sec, bfd_vma offset, size_t size)
  2380. {
  2381. return offset <= sec->size && size <= sec->size - offset;
  2382. }
  2383. static void
  2384. bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
  2385. {
  2386. _bfd_error_handler
  2387. /* xgettext:c-format */
  2388. (_("%pB: relocation %s cannot be used when making a shared object"),
  2389. abfd,
  2390. ppc_elf_howto_table[r_type]->name);
  2391. bfd_set_error (bfd_error_bad_value);
  2392. }
  2393. /* Look through the relocs for a section during the first phase, and
  2394. allocate space in the global offset table or procedure linkage
  2395. table. */
  2396. static bool
  2397. ppc_elf_check_relocs (bfd *abfd,
  2398. struct bfd_link_info *info,
  2399. asection *sec,
  2400. const Elf_Internal_Rela *relocs)
  2401. {
  2402. struct ppc_elf_link_hash_table *htab;
  2403. Elf_Internal_Shdr *symtab_hdr;
  2404. struct elf_link_hash_entry **sym_hashes;
  2405. const Elf_Internal_Rela *rel;
  2406. const Elf_Internal_Rela *rel_end;
  2407. asection *got2, *sreloc;
  2408. struct elf_link_hash_entry *tga;
  2409. if (bfd_link_relocatable (info))
  2410. return true;
  2411. #ifdef DEBUG
  2412. _bfd_error_handler ("ppc_elf_check_relocs called for section %pA in %pB",
  2413. sec, abfd);
  2414. #endif
  2415. BFD_ASSERT (is_ppc_elf (abfd));
  2416. /* Initialize howto table if not already done. */
  2417. if (!ppc_elf_howto_table[R_PPC_ADDR32])
  2418. ppc_elf_howto_init ();
  2419. htab = ppc_elf_hash_table (info);
  2420. if (htab->glink == NULL)
  2421. {
  2422. if (htab->elf.dynobj == NULL)
  2423. htab->elf.dynobj = abfd;
  2424. if (!ppc_elf_create_glink (htab->elf.dynobj, info))
  2425. return false;
  2426. }
  2427. tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
  2428. false, false, true);
  2429. symtab_hdr = &elf_symtab_hdr (abfd);
  2430. sym_hashes = elf_sym_hashes (abfd);
  2431. got2 = bfd_get_section_by_name (abfd, ".got2");
  2432. sreloc = NULL;
  2433. rel_end = relocs + sec->reloc_count;
  2434. for (rel = relocs; rel < rel_end; rel++)
  2435. {
  2436. unsigned long r_symndx;
  2437. enum elf_ppc_reloc_type r_type;
  2438. struct elf_link_hash_entry *h;
  2439. int tls_type;
  2440. struct plt_entry **ifunc;
  2441. struct plt_entry **pltent;
  2442. bfd_vma addend;
  2443. r_symndx = ELF32_R_SYM (rel->r_info);
  2444. if (r_symndx < symtab_hdr->sh_info)
  2445. h = NULL;
  2446. else
  2447. {
  2448. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  2449. while (h->root.type == bfd_link_hash_indirect
  2450. || h->root.type == bfd_link_hash_warning)
  2451. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2452. }
  2453. /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
  2454. This shows up in particular in an R_PPC_ADDR32 in the eabi
  2455. startup code. */
  2456. if (h != NULL
  2457. && htab->elf.sgot == NULL
  2458. && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  2459. {
  2460. if (htab->elf.dynobj == NULL)
  2461. htab->elf.dynobj = abfd;
  2462. if (!ppc_elf_create_got (htab->elf.dynobj, info))
  2463. return false;
  2464. BFD_ASSERT (h == htab->elf.hgot);
  2465. }
  2466. tls_type = 0;
  2467. r_type = ELF32_R_TYPE (rel->r_info);
  2468. ifunc = NULL;
  2469. if (h == NULL && htab->elf.target_os != is_vxworks)
  2470. {
  2471. Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
  2472. abfd, r_symndx);
  2473. if (isym == NULL)
  2474. return false;
  2475. if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  2476. {
  2477. /* Set PLT_IFUNC flag for this sym, no GOT entry yet. */
  2478. ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
  2479. NON_GOT | PLT_IFUNC);
  2480. if (ifunc == NULL)
  2481. return false;
  2482. /* STT_GNU_IFUNC symbols must have a PLT entry;
  2483. In a non-pie executable even when there are
  2484. no plt calls. */
  2485. if (!bfd_link_pic (info)
  2486. || is_branch_reloc (r_type)
  2487. || r_type == R_PPC_PLT16_LO
  2488. || r_type == R_PPC_PLT16_HI
  2489. || r_type == R_PPC_PLT16_HA)
  2490. {
  2491. addend = 0;
  2492. if (r_type == R_PPC_PLTREL24)
  2493. ppc_elf_tdata (abfd)->makes_plt_call = 1;
  2494. if (bfd_link_pic (info)
  2495. && (r_type == R_PPC_PLTREL24
  2496. || r_type == R_PPC_PLT16_LO
  2497. || r_type == R_PPC_PLT16_HI
  2498. || r_type == R_PPC_PLT16_HA))
  2499. addend = rel->r_addend;
  2500. if (!update_plt_info (abfd, ifunc, got2, addend))
  2501. return false;
  2502. }
  2503. }
  2504. }
  2505. if (htab->elf.target_os != is_vxworks
  2506. && is_branch_reloc (r_type)
  2507. && h != NULL
  2508. && h == tga)
  2509. {
  2510. if (rel != relocs
  2511. && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
  2512. || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
  2513. /* We have a new-style __tls_get_addr call with a marker
  2514. reloc. */
  2515. ;
  2516. else
  2517. /* Mark this section as having an old-style call. */
  2518. sec->nomark_tls_get_addr = 1;
  2519. }
  2520. switch (r_type)
  2521. {
  2522. case R_PPC_TLSGD:
  2523. case R_PPC_TLSLD:
  2524. /* These special tls relocs tie a call to __tls_get_addr with
  2525. its parameter symbol. */
  2526. if (h != NULL)
  2527. ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
  2528. else
  2529. if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
  2530. NON_GOT | TLS_TLS | TLS_MARK))
  2531. return false;
  2532. break;
  2533. case R_PPC_PLTSEQ:
  2534. break;
  2535. case R_PPC_GOT_TLSLD16:
  2536. case R_PPC_GOT_TLSLD16_LO:
  2537. case R_PPC_GOT_TLSLD16_HI:
  2538. case R_PPC_GOT_TLSLD16_HA:
  2539. tls_type = TLS_TLS | TLS_LD;
  2540. goto dogottls;
  2541. case R_PPC_GOT_TLSGD16:
  2542. case R_PPC_GOT_TLSGD16_LO:
  2543. case R_PPC_GOT_TLSGD16_HI:
  2544. case R_PPC_GOT_TLSGD16_HA:
  2545. tls_type = TLS_TLS | TLS_GD;
  2546. goto dogottls;
  2547. case R_PPC_GOT_TPREL16:
  2548. case R_PPC_GOT_TPREL16_LO:
  2549. case R_PPC_GOT_TPREL16_HI:
  2550. case R_PPC_GOT_TPREL16_HA:
  2551. if (bfd_link_dll (info))
  2552. info->flags |= DF_STATIC_TLS;
  2553. tls_type = TLS_TLS | TLS_TPREL;
  2554. goto dogottls;
  2555. case R_PPC_GOT_DTPREL16:
  2556. case R_PPC_GOT_DTPREL16_LO:
  2557. case R_PPC_GOT_DTPREL16_HI:
  2558. case R_PPC_GOT_DTPREL16_HA:
  2559. tls_type = TLS_TLS | TLS_DTPREL;
  2560. dogottls:
  2561. sec->has_tls_reloc = 1;
  2562. /* Fall through. */
  2563. /* GOT16 relocations */
  2564. case R_PPC_GOT16:
  2565. case R_PPC_GOT16_LO:
  2566. case R_PPC_GOT16_HI:
  2567. case R_PPC_GOT16_HA:
  2568. /* This symbol requires a global offset table entry. */
  2569. if (htab->elf.sgot == NULL)
  2570. {
  2571. if (htab->elf.dynobj == NULL)
  2572. htab->elf.dynobj = abfd;
  2573. if (!ppc_elf_create_got (htab->elf.dynobj, info))
  2574. return false;
  2575. }
  2576. if (h != NULL)
  2577. {
  2578. h->got.refcount += 1;
  2579. ppc_elf_hash_entry (h)->tls_mask |= tls_type;
  2580. }
  2581. else
  2582. /* This is a global offset table entry for a local symbol. */
  2583. if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
  2584. return false;
  2585. /* We may also need a plt entry if the symbol turns out to be
  2586. an ifunc. */
  2587. if (h != NULL && !bfd_link_pic (info))
  2588. {
  2589. if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
  2590. return false;
  2591. }
  2592. break;
  2593. /* Indirect .sdata relocation. */
  2594. case R_PPC_EMB_SDAI16:
  2595. htab->sdata[0].sym->ref_regular = 1;
  2596. if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[0],
  2597. h, rel))
  2598. return false;
  2599. if (h != NULL)
  2600. {
  2601. ppc_elf_hash_entry (h)->has_sda_refs = true;
  2602. h->non_got_ref = true;
  2603. }
  2604. break;
  2605. /* Indirect .sdata2 relocation. */
  2606. case R_PPC_EMB_SDA2I16:
  2607. if (!bfd_link_executable (info))
  2608. {
  2609. bad_shared_reloc (abfd, r_type);
  2610. return false;
  2611. }
  2612. htab->sdata[1].sym->ref_regular = 1;
  2613. if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[1],
  2614. h, rel))
  2615. return false;
  2616. if (h != NULL)
  2617. {
  2618. ppc_elf_hash_entry (h)->has_sda_refs = true;
  2619. h->non_got_ref = true;
  2620. }
  2621. break;
  2622. case R_PPC_SDAREL16:
  2623. htab->sdata[0].sym->ref_regular = 1;
  2624. /* Fall through. */
  2625. case R_PPC_VLE_SDAREL_LO16A:
  2626. case R_PPC_VLE_SDAREL_LO16D:
  2627. case R_PPC_VLE_SDAREL_HI16A:
  2628. case R_PPC_VLE_SDAREL_HI16D:
  2629. case R_PPC_VLE_SDAREL_HA16A:
  2630. case R_PPC_VLE_SDAREL_HA16D:
  2631. if (h != NULL)
  2632. {
  2633. ppc_elf_hash_entry (h)->has_sda_refs = true;
  2634. h->non_got_ref = true;
  2635. }
  2636. break;
  2637. case R_PPC_VLE_REL8:
  2638. case R_PPC_VLE_REL15:
  2639. case R_PPC_VLE_REL24:
  2640. case R_PPC_VLE_LO16A:
  2641. case R_PPC_VLE_LO16D:
  2642. case R_PPC_VLE_HI16A:
  2643. case R_PPC_VLE_HI16D:
  2644. case R_PPC_VLE_HA16A:
  2645. case R_PPC_VLE_HA16D:
  2646. case R_PPC_VLE_ADDR20:
  2647. break;
  2648. case R_PPC_EMB_SDA2REL:
  2649. if (!bfd_link_executable (info))
  2650. {
  2651. bad_shared_reloc (abfd, r_type);
  2652. return false;
  2653. }
  2654. htab->sdata[1].sym->ref_regular = 1;
  2655. if (h != NULL)
  2656. {
  2657. ppc_elf_hash_entry (h)->has_sda_refs = true;
  2658. h->non_got_ref = true;
  2659. }
  2660. break;
  2661. case R_PPC_VLE_SDA21_LO:
  2662. case R_PPC_VLE_SDA21:
  2663. case R_PPC_EMB_SDA21:
  2664. case R_PPC_EMB_RELSDA:
  2665. if (h != NULL)
  2666. {
  2667. ppc_elf_hash_entry (h)->has_sda_refs = true;
  2668. h->non_got_ref = true;
  2669. }
  2670. break;
  2671. case R_PPC_EMB_NADDR32:
  2672. case R_PPC_EMB_NADDR16:
  2673. case R_PPC_EMB_NADDR16_LO:
  2674. case R_PPC_EMB_NADDR16_HI:
  2675. case R_PPC_EMB_NADDR16_HA:
  2676. if (h != NULL)
  2677. h->non_got_ref = true;
  2678. break;
  2679. case R_PPC_PLTREL24:
  2680. if (h == NULL)
  2681. break;
  2682. ppc_elf_tdata (abfd)->makes_plt_call = 1;
  2683. goto pltentry;
  2684. case R_PPC_PLTCALL:
  2685. sec->has_pltcall = 1;
  2686. /* Fall through. */
  2687. case R_PPC_PLT32:
  2688. case R_PPC_PLTREL32:
  2689. case R_PPC_PLT16_LO:
  2690. case R_PPC_PLT16_HI:
  2691. case R_PPC_PLT16_HA:
  2692. pltentry:
  2693. #ifdef DEBUG
  2694. fprintf (stderr, "Reloc requires a PLT entry\n");
  2695. #endif
  2696. /* This symbol requires a procedure linkage table entry. */
  2697. if (h == NULL)
  2698. {
  2699. pltent = update_local_sym_info (abfd, symtab_hdr, r_symndx,
  2700. NON_GOT | PLT_KEEP);
  2701. if (pltent == NULL)
  2702. return false;
  2703. }
  2704. else
  2705. {
  2706. if (r_type != R_PPC_PLTREL24)
  2707. ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
  2708. h->needs_plt = 1;
  2709. pltent = &h->plt.plist;
  2710. }
  2711. addend = 0;
  2712. if (bfd_link_pic (info)
  2713. && (r_type == R_PPC_PLTREL24
  2714. || r_type == R_PPC_PLT16_LO
  2715. || r_type == R_PPC_PLT16_HI
  2716. || r_type == R_PPC_PLT16_HA))
  2717. addend = rel->r_addend;
  2718. if (!update_plt_info (abfd, pltent, got2, addend))
  2719. return false;
  2720. break;
  2721. /* The following relocations don't need to propagate the
  2722. relocation if linking a shared object since they are
  2723. section relative. */
  2724. case R_PPC_SECTOFF:
  2725. case R_PPC_SECTOFF_LO:
  2726. case R_PPC_SECTOFF_HI:
  2727. case R_PPC_SECTOFF_HA:
  2728. case R_PPC_DTPREL16:
  2729. case R_PPC_DTPREL16_LO:
  2730. case R_PPC_DTPREL16_HI:
  2731. case R_PPC_DTPREL16_HA:
  2732. case R_PPC_TOC16:
  2733. break;
  2734. case R_PPC_REL16:
  2735. case R_PPC_REL16_LO:
  2736. case R_PPC_REL16_HI:
  2737. case R_PPC_REL16_HA:
  2738. case R_PPC_REL16DX_HA:
  2739. ppc_elf_tdata (abfd)->has_rel16 = 1;
  2740. break;
  2741. /* These are just markers. */
  2742. case R_PPC_TLS:
  2743. case R_PPC_EMB_MRKREF:
  2744. case R_PPC_NONE:
  2745. case R_PPC_max:
  2746. case R_PPC_RELAX:
  2747. case R_PPC_RELAX_PLT:
  2748. case R_PPC_RELAX_PLTREL24:
  2749. case R_PPC_16DX_HA:
  2750. break;
  2751. /* These should only appear in dynamic objects. */
  2752. case R_PPC_COPY:
  2753. case R_PPC_GLOB_DAT:
  2754. case R_PPC_JMP_SLOT:
  2755. case R_PPC_RELATIVE:
  2756. case R_PPC_IRELATIVE:
  2757. break;
  2758. /* These aren't handled yet. We'll report an error later. */
  2759. case R_PPC_ADDR30:
  2760. case R_PPC_EMB_RELSEC16:
  2761. case R_PPC_EMB_RELST_LO:
  2762. case R_PPC_EMB_RELST_HI:
  2763. case R_PPC_EMB_RELST_HA:
  2764. case R_PPC_EMB_BIT_FLD:
  2765. break;
  2766. /* This refers only to functions defined in the shared library. */
  2767. case R_PPC_LOCAL24PC:
  2768. if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
  2769. {
  2770. htab->plt_type = PLT_OLD;
  2771. htab->old_bfd = abfd;
  2772. }
  2773. if (h != NULL && h->type == STT_GNU_IFUNC)
  2774. {
  2775. h->needs_plt = 1;
  2776. if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
  2777. return false;
  2778. }
  2779. break;
  2780. /* This relocation describes the C++ object vtable hierarchy.
  2781. Reconstruct it for later use during GC. */
  2782. case R_PPC_GNU_VTINHERIT:
  2783. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  2784. return false;
  2785. break;
  2786. /* This relocation describes which C++ vtable entries are actually
  2787. used. Record for later use during GC. */
  2788. case R_PPC_GNU_VTENTRY:
  2789. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  2790. return false;
  2791. break;
  2792. case R_PPC_TPREL16_HI:
  2793. case R_PPC_TPREL16_HA:
  2794. sec->has_tls_reloc = 1;
  2795. /* Fall through. */
  2796. /* We shouldn't really be seeing TPREL32. */
  2797. case R_PPC_TPREL32:
  2798. case R_PPC_TPREL16:
  2799. case R_PPC_TPREL16_LO:
  2800. if (bfd_link_dll (info))
  2801. info->flags |= DF_STATIC_TLS;
  2802. goto dodyn;
  2803. /* Nor these. */
  2804. case R_PPC_DTPMOD32:
  2805. case R_PPC_DTPREL32:
  2806. goto dodyn;
  2807. case R_PPC_REL32:
  2808. if (h == NULL
  2809. && got2 != NULL
  2810. && (sec->flags & SEC_CODE) != 0
  2811. && bfd_link_pic (info)
  2812. && htab->plt_type == PLT_UNSET)
  2813. {
  2814. /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
  2815. the start of a function, which assembles to a REL32
  2816. reference to .got2. If we detect one of these, then
  2817. force the old PLT layout because the linker cannot
  2818. reliably deduce the GOT pointer value needed for
  2819. PLT call stubs. */
  2820. asection *s;
  2821. Elf_Internal_Sym *isym;
  2822. isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
  2823. abfd, r_symndx);
  2824. if (isym == NULL)
  2825. return false;
  2826. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  2827. if (s == got2)
  2828. {
  2829. htab->plt_type = PLT_OLD;
  2830. htab->old_bfd = abfd;
  2831. }
  2832. }
  2833. if (h == NULL || h == htab->elf.hgot)
  2834. break;
  2835. /* fall through */
  2836. case R_PPC_ADDR32:
  2837. case R_PPC_ADDR16:
  2838. case R_PPC_ADDR16_LO:
  2839. case R_PPC_ADDR16_HI:
  2840. case R_PPC_ADDR16_HA:
  2841. case R_PPC_UADDR32:
  2842. case R_PPC_UADDR16:
  2843. if (h != NULL && !bfd_link_pic (info))
  2844. {
  2845. /* We may need a plt entry if the symbol turns out to be
  2846. a function defined in a dynamic object. */
  2847. if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
  2848. return false;
  2849. /* We may need a copy reloc too. */
  2850. h->non_got_ref = 1;
  2851. h->pointer_equality_needed = 1;
  2852. if (r_type == R_PPC_ADDR16_HA)
  2853. ppc_elf_hash_entry (h)->has_addr16_ha = 1;
  2854. if (r_type == R_PPC_ADDR16_LO)
  2855. ppc_elf_hash_entry (h)->has_addr16_lo = 1;
  2856. }
  2857. goto dodyn;
  2858. case R_PPC_REL24:
  2859. case R_PPC_REL14:
  2860. case R_PPC_REL14_BRTAKEN:
  2861. case R_PPC_REL14_BRNTAKEN:
  2862. if (h == NULL)
  2863. break;
  2864. if (h == htab->elf.hgot)
  2865. {
  2866. if (htab->plt_type == PLT_UNSET)
  2867. {
  2868. htab->plt_type = PLT_OLD;
  2869. htab->old_bfd = abfd;
  2870. }
  2871. break;
  2872. }
  2873. /* fall through */
  2874. case R_PPC_ADDR24:
  2875. case R_PPC_ADDR14:
  2876. case R_PPC_ADDR14_BRTAKEN:
  2877. case R_PPC_ADDR14_BRNTAKEN:
  2878. if (h != NULL && !bfd_link_pic (info))
  2879. {
  2880. /* We may need a plt entry if the symbol turns out to be
  2881. a function defined in a dynamic object. */
  2882. h->needs_plt = 1;
  2883. if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
  2884. return false;
  2885. break;
  2886. }
  2887. dodyn:
  2888. /* If we are creating a shared library, and this is a reloc
  2889. against a global symbol, or a non PC relative reloc
  2890. against a local symbol, then we need to copy the reloc
  2891. into the shared library. However, if we are linking with
  2892. -Bsymbolic, we do not need to copy a reloc against a
  2893. global symbol which is defined in an object we are
  2894. including in the link (i.e., DEF_REGULAR is set). At
  2895. this point we have not seen all the input files, so it is
  2896. possible that DEF_REGULAR is not set now but will be set
  2897. later (it is never cleared). In case of a weak definition,
  2898. DEF_REGULAR may be cleared later by a strong definition in
  2899. a shared library. We account for that possibility below by
  2900. storing information in the dyn_relocs field of the hash
  2901. table entry. A similar situation occurs when creating
  2902. shared libraries and symbol visibility changes render the
  2903. symbol local.
  2904. If on the other hand, we are creating an executable, we
  2905. may need to keep relocations for symbols satisfied by a
  2906. dynamic library if we manage to avoid copy relocs for the
  2907. symbol. */
  2908. if ((bfd_link_pic (info)
  2909. && (must_be_dyn_reloc (info, r_type)
  2910. || (h != NULL
  2911. && (!SYMBOLIC_BIND (info, h)
  2912. || h->root.type == bfd_link_hash_defweak
  2913. || !h->def_regular))))
  2914. || (ELIMINATE_COPY_RELOCS
  2915. && !bfd_link_pic (info)
  2916. && h != NULL
  2917. && (h->root.type == bfd_link_hash_defweak
  2918. || !h->def_regular)))
  2919. {
  2920. #ifdef DEBUG
  2921. fprintf (stderr,
  2922. "ppc_elf_check_relocs needs to "
  2923. "create relocation for %s\n",
  2924. (h && h->root.root.string
  2925. ? h->root.root.string : "<unknown>"));
  2926. #endif
  2927. if (sreloc == NULL)
  2928. {
  2929. if (htab->elf.dynobj == NULL)
  2930. htab->elf.dynobj = abfd;
  2931. sreloc = _bfd_elf_make_dynamic_reloc_section
  2932. (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ true);
  2933. if (sreloc == NULL)
  2934. return false;
  2935. }
  2936. /* If this is a global symbol, we count the number of
  2937. relocations we need for this symbol. */
  2938. if (h != NULL)
  2939. {
  2940. struct elf_dyn_relocs *p;
  2941. struct elf_dyn_relocs **rel_head;
  2942. rel_head = &h->dyn_relocs;
  2943. p = *rel_head;
  2944. if (p == NULL || p->sec != sec)
  2945. {
  2946. p = bfd_alloc (htab->elf.dynobj, sizeof *p);
  2947. if (p == NULL)
  2948. return false;
  2949. p->next = *rel_head;
  2950. *rel_head = p;
  2951. p->sec = sec;
  2952. p->count = 0;
  2953. p->pc_count = 0;
  2954. }
  2955. p->count += 1;
  2956. if (!must_be_dyn_reloc (info, r_type))
  2957. p->pc_count += 1;
  2958. }
  2959. else
  2960. {
  2961. /* Track dynamic relocs needed for local syms too.
  2962. We really need local syms available to do this
  2963. easily. Oh well. */
  2964. struct ppc_dyn_relocs *p;
  2965. struct ppc_dyn_relocs **rel_head;
  2966. bool is_ifunc;
  2967. asection *s;
  2968. void *vpp;
  2969. Elf_Internal_Sym *isym;
  2970. isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
  2971. abfd, r_symndx);
  2972. if (isym == NULL)
  2973. return false;
  2974. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  2975. if (s == NULL)
  2976. s = sec;
  2977. vpp = &elf_section_data (s)->local_dynrel;
  2978. rel_head = (struct ppc_dyn_relocs **) vpp;
  2979. is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
  2980. p = *rel_head;
  2981. if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
  2982. p = p->next;
  2983. if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
  2984. {
  2985. p = bfd_alloc (htab->elf.dynobj, sizeof *p);
  2986. if (p == NULL)
  2987. return false;
  2988. p->next = *rel_head;
  2989. *rel_head = p;
  2990. p->sec = sec;
  2991. p->ifunc = is_ifunc;
  2992. p->count = 0;
  2993. }
  2994. p->count += 1;
  2995. }
  2996. }
  2997. break;
  2998. }
  2999. }
  3000. return true;
  3001. }
  3002. /* Warn for conflicting Tag_GNU_Power_ABI_FP attributes between IBFD
  3003. and OBFD, and merge non-conflicting ones. */
  3004. bool
  3005. _bfd_elf_ppc_merge_fp_attributes (bfd *ibfd, struct bfd_link_info *info)
  3006. {
  3007. bfd *obfd = info->output_bfd;
  3008. obj_attribute *in_attr, *in_attrs;
  3009. obj_attribute *out_attr, *out_attrs;
  3010. bool ret = true;
  3011. bool warn_only;
  3012. /* We only warn about shared library mismatches, because common
  3013. libraries advertise support for a particular long double variant
  3014. but actually support more than one variant. For example, glibc
  3015. typically supports 128-bit IBM long double in the shared library
  3016. but has a compatibility static archive for 64-bit long double.
  3017. The linker doesn't have the smarts to see that an app using
  3018. object files marked as 64-bit long double call the compatibility
  3019. layer objects and only from there call into the shared library. */
  3020. warn_only = (ibfd->flags & DYNAMIC) != 0;
  3021. in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
  3022. out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
  3023. in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
  3024. out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
  3025. if (in_attr->i != out_attr->i)
  3026. {
  3027. int in_fp = in_attr->i & 3;
  3028. int out_fp = out_attr->i & 3;
  3029. static bfd *last_fp, *last_ld;
  3030. if (in_fp == 0)
  3031. ;
  3032. else if (out_fp == 0)
  3033. {
  3034. if (!warn_only)
  3035. {
  3036. out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
  3037. out_attr->i ^= in_fp;
  3038. last_fp = ibfd;
  3039. }
  3040. }
  3041. else if (out_fp != 2 && in_fp == 2)
  3042. {
  3043. _bfd_error_handler
  3044. /* xgettext:c-format */
  3045. (_("%pB uses hard float, %pB uses soft float"),
  3046. last_fp, ibfd);
  3047. ret = warn_only;
  3048. }
  3049. else if (out_fp == 2 && in_fp != 2)
  3050. {
  3051. _bfd_error_handler
  3052. /* xgettext:c-format */
  3053. (_("%pB uses hard float, %pB uses soft float"),
  3054. ibfd, last_fp);
  3055. ret = warn_only;
  3056. }
  3057. else if (out_fp == 1 && in_fp == 3)
  3058. {
  3059. _bfd_error_handler
  3060. /* xgettext:c-format */
  3061. (_("%pB uses double-precision hard float, "
  3062. "%pB uses single-precision hard float"), last_fp, ibfd);
  3063. ret = warn_only;
  3064. }
  3065. else if (out_fp == 3 && in_fp == 1)
  3066. {
  3067. _bfd_error_handler
  3068. /* xgettext:c-format */
  3069. (_("%pB uses double-precision hard float, "
  3070. "%pB uses single-precision hard float"), ibfd, last_fp);
  3071. ret = warn_only;
  3072. }
  3073. in_fp = in_attr->i & 0xc;
  3074. out_fp = out_attr->i & 0xc;
  3075. if (in_fp == 0)
  3076. ;
  3077. else if (out_fp == 0)
  3078. {
  3079. if (!warn_only)
  3080. {
  3081. out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
  3082. out_attr->i ^= in_fp;
  3083. last_ld = ibfd;
  3084. }
  3085. }
  3086. else if (out_fp != 2 * 4 && in_fp == 2 * 4)
  3087. {
  3088. _bfd_error_handler
  3089. /* xgettext:c-format */
  3090. (_("%pB uses 64-bit long double, "
  3091. "%pB uses 128-bit long double"), ibfd, last_ld);
  3092. ret = warn_only;
  3093. }
  3094. else if (in_fp != 2 * 4 && out_fp == 2 * 4)
  3095. {
  3096. _bfd_error_handler
  3097. /* xgettext:c-format */
  3098. (_("%pB uses 64-bit long double, "
  3099. "%pB uses 128-bit long double"), last_ld, ibfd);
  3100. ret = warn_only;
  3101. }
  3102. else if (out_fp == 1 * 4 && in_fp == 3 * 4)
  3103. {
  3104. _bfd_error_handler
  3105. /* xgettext:c-format */
  3106. (_("%pB uses IBM long double, "
  3107. "%pB uses IEEE long double"), last_ld, ibfd);
  3108. ret = warn_only;
  3109. }
  3110. else if (out_fp == 3 * 4 && in_fp == 1 * 4)
  3111. {
  3112. _bfd_error_handler
  3113. /* xgettext:c-format */
  3114. (_("%pB uses IBM long double, "
  3115. "%pB uses IEEE long double"), ibfd, last_ld);
  3116. ret = warn_only;
  3117. }
  3118. }
  3119. if (!ret)
  3120. {
  3121. out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
  3122. bfd_set_error (bfd_error_bad_value);
  3123. }
  3124. return ret;
  3125. }
  3126. /* Merge object attributes from IBFD into OBFD. Warn if
  3127. there are conflicting attributes. */
  3128. static bool
  3129. ppc_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
  3130. {
  3131. bfd *obfd;
  3132. obj_attribute *in_attr, *in_attrs;
  3133. obj_attribute *out_attr, *out_attrs;
  3134. bool ret;
  3135. if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
  3136. return false;
  3137. obfd = info->output_bfd;
  3138. in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
  3139. out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
  3140. /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
  3141. merge non-conflicting ones. */
  3142. in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
  3143. out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
  3144. ret = true;
  3145. if (in_attr->i != out_attr->i)
  3146. {
  3147. int in_vec = in_attr->i & 3;
  3148. int out_vec = out_attr->i & 3;
  3149. static bfd *last_vec;
  3150. if (in_vec == 0)
  3151. ;
  3152. else if (out_vec == 0)
  3153. {
  3154. out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
  3155. out_attr->i = in_vec;
  3156. last_vec = ibfd;
  3157. }
  3158. /* For now, allow generic to transition to AltiVec or SPE
  3159. without a warning. If GCC marked files with their stack
  3160. alignment and used don't-care markings for files which are
  3161. not affected by the vector ABI, we could warn about this
  3162. case too. */
  3163. else if (in_vec == 1)
  3164. ;
  3165. else if (out_vec == 1)
  3166. {
  3167. out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
  3168. out_attr->i = in_vec;
  3169. last_vec = ibfd;
  3170. }
  3171. else if (out_vec < in_vec)
  3172. {
  3173. _bfd_error_handler
  3174. /* xgettext:c-format */
  3175. (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
  3176. last_vec, ibfd);
  3177. out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
  3178. ret = false;
  3179. }
  3180. else if (out_vec > in_vec)
  3181. {
  3182. _bfd_error_handler
  3183. /* xgettext:c-format */
  3184. (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
  3185. ibfd, last_vec);
  3186. out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
  3187. ret = false;
  3188. }
  3189. }
  3190. /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
  3191. and merge non-conflicting ones. */
  3192. in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
  3193. out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
  3194. if (in_attr->i != out_attr->i)
  3195. {
  3196. int in_struct = in_attr->i & 3;
  3197. int out_struct = out_attr->i & 3;
  3198. static bfd *last_struct;
  3199. if (in_struct == 0 || in_struct == 3)
  3200. ;
  3201. else if (out_struct == 0)
  3202. {
  3203. out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
  3204. out_attr->i = in_struct;
  3205. last_struct = ibfd;
  3206. }
  3207. else if (out_struct < in_struct)
  3208. {
  3209. _bfd_error_handler
  3210. /* xgettext:c-format */
  3211. (_("%pB uses r3/r4 for small structure returns, "
  3212. "%pB uses memory"), last_struct, ibfd);
  3213. out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
  3214. ret = false;
  3215. }
  3216. else if (out_struct > in_struct)
  3217. {
  3218. _bfd_error_handler
  3219. /* xgettext:c-format */
  3220. (_("%pB uses r3/r4 for small structure returns, "
  3221. "%pB uses memory"), ibfd, last_struct);
  3222. out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
  3223. ret = false;
  3224. }
  3225. }
  3226. if (!ret)
  3227. {
  3228. bfd_set_error (bfd_error_bad_value);
  3229. return false;
  3230. }
  3231. /* Merge Tag_compatibility attributes and any common GNU ones. */
  3232. return _bfd_elf_merge_object_attributes (ibfd, info);
  3233. }
  3234. /* Merge backend specific data from an object file to the output
  3235. object file when linking. */
  3236. static bool
  3237. ppc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  3238. {
  3239. bfd *obfd = info->output_bfd;
  3240. flagword old_flags;
  3241. flagword new_flags;
  3242. bool error;
  3243. if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
  3244. return true;
  3245. /* Check if we have the same endianness. */
  3246. if (! _bfd_generic_verify_endian_match (ibfd, info))
  3247. return false;
  3248. if (!ppc_elf_merge_obj_attributes (ibfd, info))
  3249. return false;
  3250. if ((ibfd->flags & DYNAMIC) != 0)
  3251. return true;
  3252. new_flags = elf_elfheader (ibfd)->e_flags;
  3253. old_flags = elf_elfheader (obfd)->e_flags;
  3254. if (!elf_flags_init (obfd))
  3255. {
  3256. /* First call, no flags set. */
  3257. elf_flags_init (obfd) = true;
  3258. elf_elfheader (obfd)->e_flags = new_flags;
  3259. }
  3260. /* Compatible flags are ok. */
  3261. else if (new_flags == old_flags)
  3262. ;
  3263. /* Incompatible flags. */
  3264. else
  3265. {
  3266. /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
  3267. to be linked with either. */
  3268. error = false;
  3269. if ((new_flags & EF_PPC_RELOCATABLE) != 0
  3270. && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
  3271. {
  3272. error = true;
  3273. _bfd_error_handler
  3274. (_("%pB: compiled with -mrelocatable and linked with "
  3275. "modules compiled normally"), ibfd);
  3276. }
  3277. else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
  3278. && (old_flags & EF_PPC_RELOCATABLE) != 0)
  3279. {
  3280. error = true;
  3281. _bfd_error_handler
  3282. (_("%pB: compiled normally and linked with "
  3283. "modules compiled with -mrelocatable"), ibfd);
  3284. }
  3285. /* The output is -mrelocatable-lib iff both the input files are. */
  3286. if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
  3287. elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
  3288. /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
  3289. but each input file is either -mrelocatable or -mrelocatable-lib. */
  3290. if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
  3291. && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
  3292. && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
  3293. elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
  3294. /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
  3295. any module uses it. */
  3296. elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
  3297. new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
  3298. old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
  3299. /* Warn about any other mismatches. */
  3300. if (new_flags != old_flags)
  3301. {
  3302. error = true;
  3303. _bfd_error_handler
  3304. /* xgettext:c-format */
  3305. (_("%pB: uses different e_flags (%#x) fields "
  3306. "than previous modules (%#x)"),
  3307. ibfd, new_flags, old_flags);
  3308. }
  3309. if (error)
  3310. {
  3311. bfd_set_error (bfd_error_bad_value);
  3312. return false;
  3313. }
  3314. }
  3315. return true;
  3316. }
  3317. static bfd_reloc_status_type
  3318. ppc_elf_vle_split16 (bfd *input_bfd,
  3319. asection *input_section,
  3320. unsigned long offset,
  3321. bfd_byte *loc,
  3322. bfd_vma value,
  3323. split16_format_type split16_format,
  3324. bool fixup)
  3325. {
  3326. unsigned int insn, opcode;
  3327. if (!offset_in_range (input_section, offset, 4))
  3328. return bfd_reloc_outofrange;
  3329. insn = bfd_get_32 (input_bfd, loc);
  3330. opcode = insn & E_OPCODE_MASK;
  3331. if (opcode == E_OR2I_INSN
  3332. || opcode == E_AND2I_DOT_INSN
  3333. || opcode == E_OR2IS_INSN
  3334. || opcode == E_LIS_INSN
  3335. || opcode == E_AND2IS_DOT_INSN)
  3336. {
  3337. if (split16_format != split16a_type)
  3338. {
  3339. if (fixup)
  3340. split16_format = split16a_type;
  3341. else
  3342. _bfd_error_handler
  3343. /* xgettext:c-format */
  3344. (_("%pB(%pA+0x%lx): expected 16A style relocation on 0x%08x insn"),
  3345. input_bfd, input_section, offset, opcode);
  3346. }
  3347. }
  3348. else if (opcode == E_ADD2I_DOT_INSN
  3349. || opcode == E_ADD2IS_INSN
  3350. || opcode == E_CMP16I_INSN
  3351. || opcode == E_MULL2I_INSN
  3352. || opcode == E_CMPL16I_INSN
  3353. || opcode == E_CMPH16I_INSN
  3354. || opcode == E_CMPHL16I_INSN)
  3355. {
  3356. if (split16_format != split16d_type)
  3357. {
  3358. if (fixup)
  3359. split16_format = split16d_type;
  3360. else
  3361. _bfd_error_handler
  3362. /* xgettext:c-format */
  3363. (_("%pB(%pA+0x%lx): expected 16D style relocation on 0x%08x insn"),
  3364. input_bfd, input_section, offset, opcode);
  3365. }
  3366. }
  3367. if (split16_format == split16a_type)
  3368. {
  3369. insn &= ~((0xf800 << 5) | 0x7ff);
  3370. insn |= (value & 0xf800) << 5;
  3371. if ((insn & E_LI_MASK) == E_LI_INSN)
  3372. {
  3373. /* Hack for e_li. Extend sign. */
  3374. insn &= ~(0xf0000 >> 5);
  3375. insn |= (-(value & 0x8000) & 0xf0000) >> 5;
  3376. }
  3377. }
  3378. else
  3379. {
  3380. insn &= ~((0xf800 << 10) | 0x7ff);
  3381. insn |= (value & 0xf800) << 10;
  3382. }
  3383. insn |= value & 0x7ff;
  3384. bfd_put_32 (input_bfd, insn, loc);
  3385. return bfd_reloc_ok;
  3386. }
  3387. static void
  3388. ppc_elf_vle_split20 (bfd *output_bfd, bfd_byte *loc, bfd_vma value)
  3389. {
  3390. unsigned int insn;
  3391. insn = bfd_get_32 (output_bfd, loc);
  3392. /* We have an li20 field, bits 17..20, 11..15, 21..31. */
  3393. /* Top 4 bits of value to 17..20. */
  3394. insn |= (value & 0xf0000) >> 5;
  3395. /* Next 5 bits of the value to 11..15. */
  3396. insn |= (value & 0xf800) << 5;
  3397. /* And the final 11 bits of the value to bits 21 to 31. */
  3398. insn |= value & 0x7ff;
  3399. bfd_put_32 (output_bfd, insn, loc);
  3400. }
  3401. /* Choose which PLT scheme to use, and set .plt flags appropriately.
  3402. Returns -1 on error, 0 for old PLT, 1 for new PLT. */
  3403. int
  3404. ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
  3405. struct bfd_link_info *info)
  3406. {
  3407. struct ppc_elf_link_hash_table *htab;
  3408. flagword flags;
  3409. htab = ppc_elf_hash_table (info);
  3410. if (htab->plt_type == PLT_UNSET)
  3411. {
  3412. struct elf_link_hash_entry *h;
  3413. if (htab->params->plt_style == PLT_OLD)
  3414. htab->plt_type = PLT_OLD;
  3415. else if (bfd_link_pic (info)
  3416. && htab->elf.dynamic_sections_created
  3417. && (h = elf_link_hash_lookup (&htab->elf, "_mcount",
  3418. false, false, true)) != NULL
  3419. && (h->type == STT_FUNC
  3420. || h->needs_plt)
  3421. && h->ref_regular
  3422. && !(SYMBOL_CALLS_LOCAL (info, h)
  3423. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
  3424. {
  3425. /* Profiling of shared libs (and pies) is not supported with
  3426. secure plt, because ppc32 does profiling before a
  3427. function prologue and a secure plt pic call stubs needs
  3428. r30 to be set up. */
  3429. htab->plt_type = PLT_OLD;
  3430. }
  3431. else
  3432. {
  3433. bfd *ibfd;
  3434. enum ppc_elf_plt_type plt_type = htab->params->plt_style;
  3435. /* Look through the reloc flags left by ppc_elf_check_relocs.
  3436. Use the old style bss plt if a file makes plt calls
  3437. without using the new relocs, and if ld isn't given
  3438. --secure-plt and we never see REL16 relocs. */
  3439. if (plt_type == PLT_UNSET)
  3440. plt_type = PLT_OLD;
  3441. for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
  3442. if (is_ppc_elf (ibfd))
  3443. {
  3444. if (ppc_elf_tdata (ibfd)->has_rel16)
  3445. plt_type = PLT_NEW;
  3446. else if (ppc_elf_tdata (ibfd)->makes_plt_call)
  3447. {
  3448. plt_type = PLT_OLD;
  3449. htab->old_bfd = ibfd;
  3450. break;
  3451. }
  3452. }
  3453. htab->plt_type = plt_type;
  3454. }
  3455. }
  3456. if (htab->plt_type == PLT_OLD && htab->params->plt_style == PLT_NEW)
  3457. {
  3458. if (htab->old_bfd != NULL)
  3459. _bfd_error_handler (_("bss-plt forced due to %pB"), htab->old_bfd);
  3460. else
  3461. _bfd_error_handler (_("bss-plt forced by profiling"));
  3462. }
  3463. BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
  3464. if (htab->plt_type == PLT_NEW)
  3465. {
  3466. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  3467. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  3468. /* The new PLT is a loaded section. */
  3469. if (htab->elf.splt != NULL
  3470. && !bfd_set_section_flags (htab->elf.splt, flags))
  3471. return -1;
  3472. /* The new GOT is not executable. */
  3473. if (htab->elf.sgot != NULL
  3474. && !bfd_set_section_flags (htab->elf.sgot, flags))
  3475. return -1;
  3476. }
  3477. else
  3478. {
  3479. /* Stop an unused .glink section from affecting .text alignment. */
  3480. if (htab->glink != NULL
  3481. && !bfd_set_section_alignment (htab->glink, 0))
  3482. return -1;
  3483. }
  3484. return htab->plt_type == PLT_NEW;
  3485. }
  3486. /* Return the section that should be marked against GC for a given
  3487. relocation. */
  3488. static asection *
  3489. ppc_elf_gc_mark_hook (asection *sec,
  3490. struct bfd_link_info *info,
  3491. Elf_Internal_Rela *rel,
  3492. struct elf_link_hash_entry *h,
  3493. Elf_Internal_Sym *sym)
  3494. {
  3495. if (h != NULL)
  3496. switch (ELF32_R_TYPE (rel->r_info))
  3497. {
  3498. case R_PPC_GNU_VTINHERIT:
  3499. case R_PPC_GNU_VTENTRY:
  3500. return NULL;
  3501. }
  3502. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  3503. }
  3504. static bool
  3505. get_sym_h (struct elf_link_hash_entry **hp,
  3506. Elf_Internal_Sym **symp,
  3507. asection **symsecp,
  3508. unsigned char **tls_maskp,
  3509. Elf_Internal_Sym **locsymsp,
  3510. unsigned long r_symndx,
  3511. bfd *ibfd)
  3512. {
  3513. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
  3514. if (r_symndx >= symtab_hdr->sh_info)
  3515. {
  3516. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
  3517. struct elf_link_hash_entry *h;
  3518. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  3519. while (h->root.type == bfd_link_hash_indirect
  3520. || h->root.type == bfd_link_hash_warning)
  3521. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  3522. if (hp != NULL)
  3523. *hp = h;
  3524. if (symp != NULL)
  3525. *symp = NULL;
  3526. if (symsecp != NULL)
  3527. {
  3528. asection *symsec = NULL;
  3529. if (h->root.type == bfd_link_hash_defined
  3530. || h->root.type == bfd_link_hash_defweak)
  3531. symsec = h->root.u.def.section;
  3532. *symsecp = symsec;
  3533. }
  3534. if (tls_maskp != NULL)
  3535. *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
  3536. }
  3537. else
  3538. {
  3539. Elf_Internal_Sym *sym;
  3540. Elf_Internal_Sym *locsyms = *locsymsp;
  3541. if (locsyms == NULL)
  3542. {
  3543. locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
  3544. if (locsyms == NULL)
  3545. locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
  3546. symtab_hdr->sh_info,
  3547. 0, NULL, NULL, NULL);
  3548. if (locsyms == NULL)
  3549. return false;
  3550. *locsymsp = locsyms;
  3551. }
  3552. sym = locsyms + r_symndx;
  3553. if (hp != NULL)
  3554. *hp = NULL;
  3555. if (symp != NULL)
  3556. *symp = sym;
  3557. if (symsecp != NULL)
  3558. *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
  3559. if (tls_maskp != NULL)
  3560. {
  3561. bfd_signed_vma *local_got;
  3562. unsigned char *tls_mask;
  3563. tls_mask = NULL;
  3564. local_got = elf_local_got_refcounts (ibfd);
  3565. if (local_got != NULL)
  3566. {
  3567. struct plt_entry **local_plt = (struct plt_entry **)
  3568. (local_got + symtab_hdr->sh_info);
  3569. unsigned char *lgot_masks = (unsigned char *)
  3570. (local_plt + symtab_hdr->sh_info);
  3571. tls_mask = &lgot_masks[r_symndx];
  3572. }
  3573. *tls_maskp = tls_mask;
  3574. }
  3575. }
  3576. return true;
  3577. }
  3578. /* Analyze inline PLT call relocations to see whether calls to locally
  3579. defined functions can be converted to direct calls. */
  3580. bool
  3581. ppc_elf_inline_plt (struct bfd_link_info *info)
  3582. {
  3583. struct ppc_elf_link_hash_table *htab;
  3584. bfd *ibfd;
  3585. asection *sec;
  3586. bfd_vma low_vma, high_vma, limit;
  3587. htab = ppc_elf_hash_table (info);
  3588. if (htab == NULL)
  3589. return false;
  3590. /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
  3591. reduced somewhat to cater for possible stubs that might be added
  3592. between the call and its destination. */
  3593. limit = 0x1e00000;
  3594. low_vma = -1;
  3595. high_vma = 0;
  3596. for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
  3597. if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
  3598. {
  3599. if (low_vma > sec->vma)
  3600. low_vma = sec->vma;
  3601. if (high_vma < sec->vma + sec->size)
  3602. high_vma = sec->vma + sec->size;
  3603. }
  3604. /* If a "bl" can reach anywhere in local code sections, then we can
  3605. convert all inline PLT sequences to direct calls when the symbol
  3606. is local. */
  3607. if (high_vma - low_vma < limit)
  3608. {
  3609. htab->can_convert_all_inline_plt = 1;
  3610. return true;
  3611. }
  3612. /* Otherwise, go looking through relocs for cases where a direct
  3613. call won't reach. Mark the symbol on any such reloc to disable
  3614. the optimization and keep the PLT entry as it seems likely that
  3615. this will be better than creating trampolines. Note that this
  3616. will disable the optimization for all inline PLT calls to a
  3617. particular symbol, not just those that won't reach. The
  3618. difficulty in doing a more precise optimization is that the
  3619. linker needs to make a decision depending on whether a
  3620. particular R_PPC_PLTCALL insn can be turned into a direct
  3621. call, for each of the R_PPC_PLTSEQ and R_PPC_PLT16* insns in
  3622. the sequence, and there is nothing that ties those relocs
  3623. together except their symbol. */
  3624. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  3625. {
  3626. Elf_Internal_Shdr *symtab_hdr;
  3627. Elf_Internal_Sym *local_syms;
  3628. if (!is_ppc_elf (ibfd))
  3629. continue;
  3630. local_syms = NULL;
  3631. symtab_hdr = &elf_symtab_hdr (ibfd);
  3632. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  3633. if (sec->has_pltcall
  3634. && !bfd_is_abs_section (sec->output_section))
  3635. {
  3636. Elf_Internal_Rela *relstart, *rel, *relend;
  3637. /* Read the relocations. */
  3638. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
  3639. info->keep_memory);
  3640. if (relstart == NULL)
  3641. return false;
  3642. relend = relstart + sec->reloc_count;
  3643. for (rel = relstart; rel < relend; rel++)
  3644. {
  3645. enum elf_ppc_reloc_type r_type;
  3646. unsigned long r_symndx;
  3647. asection *sym_sec;
  3648. struct elf_link_hash_entry *h;
  3649. Elf_Internal_Sym *sym;
  3650. unsigned char *tls_maskp;
  3651. r_type = ELF32_R_TYPE (rel->r_info);
  3652. if (r_type != R_PPC_PLTCALL)
  3653. continue;
  3654. r_symndx = ELF32_R_SYM (rel->r_info);
  3655. if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
  3656. r_symndx, ibfd))
  3657. {
  3658. if (elf_section_data (sec)->relocs != relstart)
  3659. free (relstart);
  3660. if (symtab_hdr->contents != (unsigned char *) local_syms)
  3661. free (local_syms);
  3662. return false;
  3663. }
  3664. if (sym_sec != NULL && sym_sec->output_section != NULL)
  3665. {
  3666. bfd_vma from, to;
  3667. if (h != NULL)
  3668. to = h->root.u.def.value;
  3669. else
  3670. to = sym->st_value;
  3671. to += (rel->r_addend
  3672. + sym_sec->output_offset
  3673. + sym_sec->output_section->vma);
  3674. from = (rel->r_offset
  3675. + sec->output_offset
  3676. + sec->output_section->vma);
  3677. if (to - from + limit < 2 * limit)
  3678. *tls_maskp &= ~PLT_KEEP;
  3679. }
  3680. }
  3681. if (elf_section_data (sec)->relocs != relstart)
  3682. free (relstart);
  3683. }
  3684. if (local_syms != NULL
  3685. && symtab_hdr->contents != (unsigned char *) local_syms)
  3686. {
  3687. if (!info->keep_memory)
  3688. free (local_syms);
  3689. else
  3690. symtab_hdr->contents = (unsigned char *) local_syms;
  3691. }
  3692. }
  3693. return true;
  3694. }
  3695. /* Set plt output section type, htab->tls_get_addr, and call the
  3696. generic ELF tls_setup function. */
  3697. asection *
  3698. ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
  3699. {
  3700. struct ppc_elf_link_hash_table *htab;
  3701. htab = ppc_elf_hash_table (info);
  3702. htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
  3703. false, false, true);
  3704. if (htab->plt_type != PLT_NEW)
  3705. htab->params->no_tls_get_addr_opt = true;
  3706. if (!htab->params->no_tls_get_addr_opt)
  3707. {
  3708. struct elf_link_hash_entry *opt, *tga;
  3709. opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
  3710. false, false, true);
  3711. if (opt != NULL
  3712. && (opt->root.type == bfd_link_hash_defined
  3713. || opt->root.type == bfd_link_hash_defweak))
  3714. {
  3715. /* If glibc supports an optimized __tls_get_addr call stub,
  3716. signalled by the presence of __tls_get_addr_opt, and we'll
  3717. be calling __tls_get_addr via a plt call stub, then
  3718. make __tls_get_addr point to __tls_get_addr_opt. */
  3719. tga = htab->tls_get_addr;
  3720. if (htab->elf.dynamic_sections_created
  3721. && tga != NULL
  3722. && (tga->type == STT_FUNC
  3723. || tga->needs_plt)
  3724. && !(SYMBOL_CALLS_LOCAL (info, tga)
  3725. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga)))
  3726. {
  3727. struct plt_entry *ent;
  3728. for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
  3729. if (ent->plt.refcount > 0)
  3730. break;
  3731. if (ent != NULL)
  3732. {
  3733. tga->root.type = bfd_link_hash_indirect;
  3734. tga->root.u.i.link = &opt->root;
  3735. ppc_elf_copy_indirect_symbol (info, opt, tga);
  3736. opt->mark = 1;
  3737. if (opt->dynindx != -1)
  3738. {
  3739. /* Use __tls_get_addr_opt in dynamic relocations. */
  3740. opt->dynindx = -1;
  3741. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  3742. opt->dynstr_index);
  3743. if (!bfd_elf_link_record_dynamic_symbol (info, opt))
  3744. return false;
  3745. }
  3746. htab->tls_get_addr = opt;
  3747. }
  3748. }
  3749. }
  3750. else
  3751. htab->params->no_tls_get_addr_opt = true;
  3752. }
  3753. if (htab->plt_type == PLT_NEW
  3754. && htab->elf.splt != NULL
  3755. && htab->elf.splt->output_section != NULL)
  3756. {
  3757. elf_section_type (htab->elf.splt->output_section) = SHT_PROGBITS;
  3758. elf_section_flags (htab->elf.splt->output_section) = SHF_ALLOC + SHF_WRITE;
  3759. }
  3760. return _bfd_elf_tls_setup (obfd, info);
  3761. }
  3762. /* Return TRUE iff REL is a branch reloc with a global symbol matching
  3763. HASH. */
  3764. static bool
  3765. branch_reloc_hash_match (const bfd *ibfd,
  3766. const Elf_Internal_Rela *rel,
  3767. const struct elf_link_hash_entry *hash)
  3768. {
  3769. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
  3770. enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
  3771. unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
  3772. if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
  3773. {
  3774. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
  3775. struct elf_link_hash_entry *h;
  3776. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  3777. while (h->root.type == bfd_link_hash_indirect
  3778. || h->root.type == bfd_link_hash_warning)
  3779. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  3780. if (h == hash)
  3781. return true;
  3782. }
  3783. return false;
  3784. }
  3785. /* Run through all the TLS relocs looking for optimization
  3786. opportunities. */
  3787. bool
  3788. ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
  3789. struct bfd_link_info *info)
  3790. {
  3791. bfd *ibfd;
  3792. asection *sec;
  3793. struct ppc_elf_link_hash_table *htab;
  3794. int pass;
  3795. if (!bfd_link_executable (info))
  3796. return true;
  3797. htab = ppc_elf_hash_table (info);
  3798. if (htab == NULL)
  3799. return false;
  3800. htab->do_tls_opt = 1;
  3801. /* Make two passes through the relocs. First time check that tls
  3802. relocs involved in setting up a tls_get_addr call are indeed
  3803. followed by such a call. If they are not, don't do any tls
  3804. optimization. On the second pass twiddle tls_mask flags to
  3805. notify relocate_section that optimization can be done, and
  3806. adjust got and plt refcounts. */
  3807. for (pass = 0; pass < 2; ++pass)
  3808. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  3809. {
  3810. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
  3811. asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
  3812. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  3813. if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
  3814. {
  3815. Elf_Internal_Rela *relstart, *rel, *relend;
  3816. int expecting_tls_get_addr = 0;
  3817. /* Read the relocations. */
  3818. relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
  3819. info->keep_memory);
  3820. if (relstart == NULL)
  3821. return false;
  3822. relend = relstart + sec->reloc_count;
  3823. for (rel = relstart; rel < relend; rel++)
  3824. {
  3825. enum elf_ppc_reloc_type r_type;
  3826. unsigned long r_symndx;
  3827. struct elf_link_hash_entry *h = NULL;
  3828. unsigned char *tls_mask;
  3829. unsigned char tls_set, tls_clear;
  3830. bool is_local;
  3831. bfd_signed_vma *got_count;
  3832. r_symndx = ELF32_R_SYM (rel->r_info);
  3833. if (r_symndx >= symtab_hdr->sh_info)
  3834. {
  3835. struct elf_link_hash_entry **sym_hashes;
  3836. sym_hashes = elf_sym_hashes (ibfd);
  3837. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  3838. while (h->root.type == bfd_link_hash_indirect
  3839. || h->root.type == bfd_link_hash_warning)
  3840. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  3841. }
  3842. is_local = SYMBOL_REFERENCES_LOCAL (info, h);
  3843. r_type = ELF32_R_TYPE (rel->r_info);
  3844. /* If this section has old-style __tls_get_addr calls
  3845. without marker relocs, then check that each
  3846. __tls_get_addr call reloc is preceded by a reloc
  3847. that conceivably belongs to the __tls_get_addr arg
  3848. setup insn. If we don't find matching arg setup
  3849. relocs, don't do any tls optimization. */
  3850. if (pass == 0
  3851. && sec->nomark_tls_get_addr
  3852. && h != NULL
  3853. && h == htab->tls_get_addr
  3854. && !expecting_tls_get_addr
  3855. && is_branch_reloc (r_type))
  3856. {
  3857. info->callbacks->minfo ("%H __tls_get_addr lost arg, "
  3858. "TLS optimization disabled\n",
  3859. ibfd, sec, rel->r_offset);
  3860. if (elf_section_data (sec)->relocs != relstart)
  3861. free (relstart);
  3862. return true;
  3863. }
  3864. expecting_tls_get_addr = 0;
  3865. switch (r_type)
  3866. {
  3867. case R_PPC_GOT_TLSLD16:
  3868. case R_PPC_GOT_TLSLD16_LO:
  3869. expecting_tls_get_addr = 1;
  3870. /* Fall through. */
  3871. case R_PPC_GOT_TLSLD16_HI:
  3872. case R_PPC_GOT_TLSLD16_HA:
  3873. /* These relocs should never be against a symbol
  3874. defined in a shared lib. Leave them alone if
  3875. that turns out to be the case. */
  3876. if (!is_local)
  3877. continue;
  3878. /* LD -> LE */
  3879. tls_set = 0;
  3880. tls_clear = TLS_LD;
  3881. break;
  3882. case R_PPC_GOT_TLSGD16:
  3883. case R_PPC_GOT_TLSGD16_LO:
  3884. expecting_tls_get_addr = 1;
  3885. /* Fall through. */
  3886. case R_PPC_GOT_TLSGD16_HI:
  3887. case R_PPC_GOT_TLSGD16_HA:
  3888. if (is_local)
  3889. /* GD -> LE */
  3890. tls_set = 0;
  3891. else
  3892. /* GD -> IE */
  3893. tls_set = TLS_TLS | TLS_GDIE;
  3894. tls_clear = TLS_GD;
  3895. break;
  3896. case R_PPC_GOT_TPREL16:
  3897. case R_PPC_GOT_TPREL16_LO:
  3898. case R_PPC_GOT_TPREL16_HI:
  3899. case R_PPC_GOT_TPREL16_HA:
  3900. if (is_local)
  3901. {
  3902. /* IE -> LE */
  3903. tls_set = 0;
  3904. tls_clear = TLS_TPREL;
  3905. break;
  3906. }
  3907. else
  3908. continue;
  3909. case R_PPC_TLSLD:
  3910. if (!is_local)
  3911. continue;
  3912. /* Fall through. */
  3913. case R_PPC_TLSGD:
  3914. if (rel + 1 < relend
  3915. && is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
  3916. {
  3917. if (pass != 0
  3918. && ELF32_R_TYPE (rel[1].r_info) != R_PPC_PLTSEQ)
  3919. {
  3920. r_type = ELF32_R_TYPE (rel[1].r_info);
  3921. r_symndx = ELF32_R_SYM (rel[1].r_info);
  3922. if (r_symndx >= symtab_hdr->sh_info)
  3923. {
  3924. struct elf_link_hash_entry **sym_hashes;
  3925. sym_hashes = elf_sym_hashes (ibfd);
  3926. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  3927. while (h->root.type == bfd_link_hash_indirect
  3928. || h->root.type == bfd_link_hash_warning)
  3929. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  3930. if (h != NULL)
  3931. {
  3932. struct plt_entry *ent = NULL;
  3933. bfd_vma addend = 0;
  3934. if (bfd_link_pic (info))
  3935. addend = rel->r_addend;
  3936. ent = find_plt_ent (&h->plt.plist,
  3937. got2, addend);
  3938. if (ent != NULL
  3939. && ent->plt.refcount > 0)
  3940. ent->plt.refcount -= 1;
  3941. }
  3942. }
  3943. }
  3944. continue;
  3945. }
  3946. expecting_tls_get_addr = 2;
  3947. tls_set = 0;
  3948. tls_clear = 0;
  3949. break;
  3950. case R_PPC_TPREL16_HA:
  3951. if (pass == 0)
  3952. {
  3953. unsigned char buf[4];
  3954. unsigned int insn;
  3955. bfd_vma off = rel->r_offset & ~3;
  3956. if (!bfd_get_section_contents (ibfd, sec, buf,
  3957. off, 4))
  3958. {
  3959. if (elf_section_data (sec)->relocs != relstart)
  3960. free (relstart);
  3961. return false;
  3962. }
  3963. insn = bfd_get_32 (ibfd, buf);
  3964. /* addis rt,2,imm */
  3965. if ((insn & ((0x3fu << 26) | 0x1f << 16))
  3966. != ((15u << 26) | (2 << 16)))
  3967. {
  3968. /* xgettext:c-format */
  3969. info->callbacks->minfo
  3970. (_("%H: warning: %s unexpected insn %#x.\n"),
  3971. ibfd, sec, off, "R_PPC_TPREL16_HA", insn);
  3972. htab->do_tls_opt = 0;
  3973. }
  3974. }
  3975. continue;
  3976. case R_PPC_TPREL16_HI:
  3977. htab->do_tls_opt = 0;
  3978. continue;
  3979. default:
  3980. continue;
  3981. }
  3982. if (pass == 0)
  3983. {
  3984. if (!expecting_tls_get_addr
  3985. || !sec->nomark_tls_get_addr)
  3986. continue;
  3987. if (rel + 1 < relend
  3988. && branch_reloc_hash_match (ibfd, rel + 1,
  3989. htab->tls_get_addr))
  3990. continue;
  3991. /* Uh oh, we didn't find the expected call. We
  3992. could just mark this symbol to exclude it
  3993. from tls optimization but it's safer to skip
  3994. the entire optimization. */
  3995. info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
  3996. "TLS optimization disabled\n"),
  3997. ibfd, sec, rel->r_offset);
  3998. if (elf_section_data (sec)->relocs != relstart)
  3999. free (relstart);
  4000. return true;
  4001. }
  4002. if (h != NULL)
  4003. {
  4004. tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
  4005. got_count = &h->got.refcount;
  4006. }
  4007. else
  4008. {
  4009. bfd_signed_vma *lgot_refs;
  4010. struct plt_entry **local_plt;
  4011. unsigned char *lgot_masks;
  4012. lgot_refs = elf_local_got_refcounts (ibfd);
  4013. if (lgot_refs == NULL)
  4014. abort ();
  4015. local_plt = (struct plt_entry **)
  4016. (lgot_refs + symtab_hdr->sh_info);
  4017. lgot_masks = (unsigned char *)
  4018. (local_plt + symtab_hdr->sh_info);
  4019. tls_mask = &lgot_masks[r_symndx];
  4020. got_count = &lgot_refs[r_symndx];
  4021. }
  4022. /* If we don't have old-style __tls_get_addr calls
  4023. without TLSGD/TLSLD marker relocs, and we haven't
  4024. found a new-style __tls_get_addr call with a
  4025. marker for this symbol, then we either have a
  4026. broken object file or an -mlongcall style
  4027. indirect call to __tls_get_addr without a marker.
  4028. Disable optimization in this case. */
  4029. if ((tls_clear & (TLS_GD | TLS_LD)) != 0
  4030. && !sec->nomark_tls_get_addr
  4031. && ((*tls_mask & (TLS_TLS | TLS_MARK))
  4032. != (TLS_TLS | TLS_MARK)))
  4033. continue;
  4034. if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
  4035. {
  4036. struct plt_entry *ent;
  4037. bfd_vma addend = 0;
  4038. if (bfd_link_pic (info)
  4039. && (ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24
  4040. || ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTCALL))
  4041. addend = rel[1].r_addend;
  4042. ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
  4043. got2, addend);
  4044. if (ent != NULL && ent->plt.refcount > 0)
  4045. ent->plt.refcount -= 1;
  4046. }
  4047. if (tls_clear == 0)
  4048. continue;
  4049. if (tls_set == 0)
  4050. {
  4051. /* We managed to get rid of a got entry. */
  4052. if (*got_count > 0)
  4053. *got_count -= 1;
  4054. }
  4055. *tls_mask |= tls_set;
  4056. *tls_mask &= ~tls_clear;
  4057. }
  4058. if (elf_section_data (sec)->relocs != relstart)
  4059. free (relstart);
  4060. }
  4061. }
  4062. return true;
  4063. }
  4064. /* Return true if we have dynamic relocs against H or any of its weak
  4065. aliases, that apply to read-only sections. Cannot be used after
  4066. size_dynamic_sections. */
  4067. static bool
  4068. alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
  4069. {
  4070. struct ppc_elf_link_hash_entry *eh = ppc_elf_hash_entry (h);
  4071. do
  4072. {
  4073. if (_bfd_elf_readonly_dynrelocs (&eh->elf))
  4074. return true;
  4075. eh = ppc_elf_hash_entry (eh->elf.u.alias);
  4076. } while (eh != NULL && &eh->elf != h);
  4077. return false;
  4078. }
  4079. /* Return whether H has pc-relative dynamic relocs. */
  4080. static bool
  4081. pc_dynrelocs (struct elf_link_hash_entry *h)
  4082. {
  4083. struct elf_dyn_relocs *p;
  4084. for (p = h->dyn_relocs; p != NULL; p = p->next)
  4085. if (p->pc_count != 0)
  4086. return true;
  4087. return false;
  4088. }
  4089. /* Adjust a symbol defined by a dynamic object and referenced by a
  4090. regular object. The current definition is in some section of the
  4091. dynamic object, but we're not including those sections. We have to
  4092. change the definition to something the rest of the link can
  4093. understand. */
  4094. static bool
  4095. ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  4096. struct elf_link_hash_entry *h)
  4097. {
  4098. struct ppc_elf_link_hash_table *htab;
  4099. asection *s;
  4100. #ifdef DEBUG
  4101. fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
  4102. h->root.root.string);
  4103. #endif
  4104. /* Make sure we know what is going on here. */
  4105. htab = ppc_elf_hash_table (info);
  4106. BFD_ASSERT (htab->elf.dynobj != NULL
  4107. && (h->needs_plt
  4108. || h->type == STT_GNU_IFUNC
  4109. || h->is_weakalias
  4110. || (h->def_dynamic
  4111. && h->ref_regular
  4112. && !h->def_regular)));
  4113. /* Deal with function syms. */
  4114. if (h->type == STT_FUNC
  4115. || h->type == STT_GNU_IFUNC
  4116. || h->needs_plt)
  4117. {
  4118. bool local = (SYMBOL_CALLS_LOCAL (info, h)
  4119. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
  4120. /* Discard dyn_relocs when non-pic if we've decided that a
  4121. function symbol is local. */
  4122. if (!bfd_link_pic (info) && local)
  4123. h->dyn_relocs = NULL;
  4124. /* Clear procedure linkage table information for any symbol that
  4125. won't need a .plt entry. */
  4126. struct plt_entry *ent;
  4127. for (ent = h->plt.plist; ent != NULL; ent = ent->next)
  4128. if (ent->plt.refcount > 0)
  4129. break;
  4130. if (ent == NULL
  4131. || (h->type != STT_GNU_IFUNC
  4132. && local
  4133. && (htab->can_convert_all_inline_plt
  4134. || (ppc_elf_hash_entry (h)->tls_mask
  4135. & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
  4136. {
  4137. /* A PLT entry is not required/allowed when:
  4138. 1. We are not using ld.so; because then the PLT entry
  4139. can't be set up, so we can't use one. In this case,
  4140. ppc_elf_adjust_dynamic_symbol won't even be called.
  4141. 2. GC has rendered the entry unused.
  4142. 3. We know for certain that a call to this symbol
  4143. will go to this object, or will remain undefined. */
  4144. h->plt.plist = NULL;
  4145. h->needs_plt = 0;
  4146. h->pointer_equality_needed = 0;
  4147. }
  4148. else
  4149. {
  4150. /* Taking a function's address in a read/write section
  4151. doesn't require us to define the function symbol in the
  4152. executable on a plt call stub. A dynamic reloc can
  4153. be used instead, giving better runtime performance.
  4154. (Calls via that function pointer don't need to bounce
  4155. through the plt call stub.) Similarly, use a dynamic
  4156. reloc for a weak reference when possible, allowing the
  4157. resolution of the symbol to be set at load time rather
  4158. than link time. */
  4159. if ((h->pointer_equality_needed
  4160. || (h->non_got_ref
  4161. && !h->ref_regular_nonweak
  4162. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
  4163. && htab->elf.target_os != is_vxworks
  4164. && !ppc_elf_hash_entry (h)->has_sda_refs
  4165. && !_bfd_elf_readonly_dynrelocs (h))
  4166. {
  4167. h->pointer_equality_needed = 0;
  4168. /* If we haven't seen a branch reloc and the symbol
  4169. isn't an ifunc then we don't need a plt entry. */
  4170. if (!h->needs_plt && h->type != STT_GNU_IFUNC)
  4171. h->plt.plist = NULL;
  4172. }
  4173. else if (!bfd_link_pic (info))
  4174. /* We are going to be defining the function symbol on the
  4175. plt stub, so no dyn_relocs needed when non-pic. */
  4176. h->dyn_relocs = NULL;
  4177. }
  4178. h->protected_def = 0;
  4179. /* Function symbols can't have copy relocs. */
  4180. return true;
  4181. }
  4182. else
  4183. h->plt.plist = NULL;
  4184. /* If this is a weak symbol, and there is a real definition, the
  4185. processor independent code will have arranged for us to see the
  4186. real definition first, and we can just use the same value. */
  4187. if (h->is_weakalias)
  4188. {
  4189. struct elf_link_hash_entry *def = weakdef (h);
  4190. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  4191. h->root.u.def.section = def->root.u.def.section;
  4192. h->root.u.def.value = def->root.u.def.value;
  4193. if (def->root.u.def.section == htab->elf.sdynbss
  4194. || def->root.u.def.section == htab->elf.sdynrelro
  4195. || def->root.u.def.section == htab->dynsbss)
  4196. h->dyn_relocs = NULL;
  4197. return true;
  4198. }
  4199. /* This is a reference to a symbol defined by a dynamic object which
  4200. is not a function. */
  4201. /* If we are creating a shared library, we must presume that the
  4202. only references to the symbol are via the global offset table.
  4203. For such cases we need not do anything here; the relocations will
  4204. be handled correctly by relocate_section. */
  4205. if (bfd_link_pic (info))
  4206. {
  4207. h->protected_def = 0;
  4208. return true;
  4209. }
  4210. /* If there are no references to this symbol that do not use the
  4211. GOT, we don't need to generate a copy reloc. */
  4212. if (!h->non_got_ref)
  4213. {
  4214. h->protected_def = 0;
  4215. return true;
  4216. }
  4217. /* Protected variables do not work with .dynbss. The copy in
  4218. .dynbss won't be used by the shared library with the protected
  4219. definition for the variable. Editing to PIC, or text relocations
  4220. are preferable to an incorrect program. */
  4221. if (h->protected_def)
  4222. {
  4223. if (ELIMINATE_COPY_RELOCS
  4224. && ppc_elf_hash_entry (h)->has_addr16_ha
  4225. && ppc_elf_hash_entry (h)->has_addr16_lo
  4226. && htab->params->pic_fixup == 0
  4227. && info->disable_target_specific_optimizations <= 1)
  4228. htab->params->pic_fixup = 1;
  4229. return true;
  4230. }
  4231. /* If -z nocopyreloc was given, we won't generate them either. */
  4232. if (info->nocopyreloc)
  4233. return true;
  4234. /* If we don't find any dynamic relocs in read-only sections, then
  4235. we'll be keeping the dynamic relocs and avoiding the copy reloc.
  4236. We can't do this if there are any small data relocations. This
  4237. doesn't work on VxWorks, where we can not have dynamic
  4238. relocations (other than copy and jump slot relocations) in an
  4239. executable. */
  4240. if (ELIMINATE_COPY_RELOCS
  4241. && !ppc_elf_hash_entry (h)->has_sda_refs
  4242. && htab->elf.target_os != is_vxworks
  4243. && !h->def_regular
  4244. && !alias_readonly_dynrelocs (h))
  4245. return true;
  4246. /* We must allocate the symbol in our .dynbss section, which will
  4247. become part of the .bss section of the executable. There will be
  4248. an entry for this symbol in the .dynsym section. The dynamic
  4249. object will contain position independent code, so all references
  4250. from the dynamic object to this symbol will go through the global
  4251. offset table. The dynamic linker will use the .dynsym entry to
  4252. determine the address it must put in the global offset table, so
  4253. both the dynamic object and the regular object will refer to the
  4254. same memory location for the variable.
  4255. Of course, if the symbol is referenced using SDAREL relocs, we
  4256. must instead allocate it in .sbss. */
  4257. if (ppc_elf_hash_entry (h)->has_sda_refs)
  4258. s = htab->dynsbss;
  4259. else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  4260. s = htab->elf.sdynrelro;
  4261. else
  4262. s = htab->elf.sdynbss;
  4263. BFD_ASSERT (s != NULL);
  4264. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  4265. {
  4266. asection *srel;
  4267. /* We must generate a R_PPC_COPY reloc to tell the dynamic
  4268. linker to copy the initial value out of the dynamic object
  4269. and into the runtime process image. */
  4270. if (ppc_elf_hash_entry (h)->has_sda_refs)
  4271. srel = htab->relsbss;
  4272. else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  4273. srel = htab->elf.sreldynrelro;
  4274. else
  4275. srel = htab->elf.srelbss;
  4276. BFD_ASSERT (srel != NULL);
  4277. srel->size += sizeof (Elf32_External_Rela);
  4278. h->needs_copy = 1;
  4279. }
  4280. /* We no longer want dyn_relocs. */
  4281. h->dyn_relocs = NULL;
  4282. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  4283. }
  4284. /* Generate a symbol to mark plt call stubs. For non-PIC code the sym is
  4285. xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
  4286. specifying the addend on the plt relocation. For -fpic code, the sym
  4287. is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
  4288. xxxxxxxx.got2.plt_pic32.<callee>. */
  4289. static bool
  4290. add_stub_sym (struct plt_entry *ent,
  4291. struct elf_link_hash_entry *h,
  4292. struct bfd_link_info *info)
  4293. {
  4294. struct elf_link_hash_entry *sh;
  4295. size_t len1, len2, len3;
  4296. char *name;
  4297. const char *stub;
  4298. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  4299. if (bfd_link_pic (info))
  4300. stub = ".plt_pic32.";
  4301. else
  4302. stub = ".plt_call32.";
  4303. len1 = strlen (h->root.root.string);
  4304. len2 = strlen (stub);
  4305. len3 = 0;
  4306. if (ent->sec)
  4307. len3 = strlen (ent->sec->name);
  4308. name = bfd_malloc (len1 + len2 + len3 + 9);
  4309. if (name == NULL)
  4310. return false;
  4311. sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
  4312. if (ent->sec)
  4313. memcpy (name + 8, ent->sec->name, len3);
  4314. memcpy (name + 8 + len3, stub, len2);
  4315. memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
  4316. sh = elf_link_hash_lookup (&htab->elf, name, true, false, false);
  4317. if (sh == NULL)
  4318. return false;
  4319. if (sh->root.type == bfd_link_hash_new)
  4320. {
  4321. sh->root.type = bfd_link_hash_defined;
  4322. sh->root.u.def.section = htab->glink;
  4323. sh->root.u.def.value = ent->glink_offset;
  4324. sh->ref_regular = 1;
  4325. sh->def_regular = 1;
  4326. sh->ref_regular_nonweak = 1;
  4327. sh->forced_local = 1;
  4328. sh->non_elf = 0;
  4329. sh->root.linker_def = 1;
  4330. }
  4331. return true;
  4332. }
  4333. /* Allocate NEED contiguous space in .got, and return the offset.
  4334. Handles allocation of the got header when crossing 32k. */
  4335. static bfd_vma
  4336. allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
  4337. {
  4338. bfd_vma where;
  4339. unsigned int max_before_header;
  4340. if (htab->plt_type == PLT_VXWORKS)
  4341. {
  4342. where = htab->elf.sgot->size;
  4343. htab->elf.sgot->size += need;
  4344. }
  4345. else
  4346. {
  4347. max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
  4348. if (need <= htab->got_gap)
  4349. {
  4350. where = max_before_header - htab->got_gap;
  4351. htab->got_gap -= need;
  4352. }
  4353. else
  4354. {
  4355. if (htab->elf.sgot->size + need > max_before_header
  4356. && htab->elf.sgot->size <= max_before_header)
  4357. {
  4358. htab->got_gap = max_before_header - htab->elf.sgot->size;
  4359. htab->elf.sgot->size = max_before_header + htab->got_header_size;
  4360. }
  4361. where = htab->elf.sgot->size;
  4362. htab->elf.sgot->size += need;
  4363. }
  4364. }
  4365. return where;
  4366. }
  4367. /* Calculate size of GOT entries for symbol given its TLS_MASK.
  4368. TLS_LD is excluded because those go in a special GOT slot. */
  4369. static inline unsigned int
  4370. got_entries_needed (int tls_mask)
  4371. {
  4372. unsigned int need;
  4373. if ((tls_mask & TLS_TLS) == 0)
  4374. need = 4;
  4375. else
  4376. {
  4377. need = 0;
  4378. if ((tls_mask & TLS_GD) != 0)
  4379. need += 8;
  4380. if ((tls_mask & (TLS_TPREL | TLS_GDIE)) != 0)
  4381. need += 4;
  4382. if ((tls_mask & TLS_DTPREL) != 0)
  4383. need += 4;
  4384. }
  4385. return need;
  4386. }
  4387. /* If H is undefined, make it dynamic if that makes sense. */
  4388. static bool
  4389. ensure_undef_dynamic (struct bfd_link_info *info,
  4390. struct elf_link_hash_entry *h)
  4391. {
  4392. struct elf_link_hash_table *htab = elf_hash_table (info);
  4393. if (htab->dynamic_sections_created
  4394. && ((info->dynamic_undefined_weak != 0
  4395. && h->root.type == bfd_link_hash_undefweak)
  4396. || h->root.type == bfd_link_hash_undefined)
  4397. && h->dynindx == -1
  4398. && !h->forced_local
  4399. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  4400. return bfd_elf_link_record_dynamic_symbol (info, h);
  4401. return true;
  4402. }
  4403. /* Choose whether to use htab->iplt or htab->pltlocal rather than the
  4404. usual htab->elf.splt section for a PLT entry. */
  4405. static inline
  4406. bool use_local_plt (struct bfd_link_info *info,
  4407. struct elf_link_hash_entry *h)
  4408. {
  4409. return (h == NULL
  4410. || h->dynindx == -1
  4411. || !elf_hash_table (info)->dynamic_sections_created);
  4412. }
  4413. /* Allocate space in associated reloc sections for dynamic relocs. */
  4414. static bool
  4415. allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  4416. {
  4417. struct bfd_link_info *info = inf;
  4418. struct ppc_elf_link_hash_entry *eh;
  4419. struct ppc_elf_link_hash_table *htab;
  4420. struct elf_dyn_relocs *p;
  4421. if (h->root.type == bfd_link_hash_indirect)
  4422. return true;
  4423. htab = ppc_elf_hash_table (info);
  4424. eh = (struct ppc_elf_link_hash_entry *) h;
  4425. if (eh->elf.got.refcount > 0
  4426. || (ELIMINATE_COPY_RELOCS
  4427. && !eh->elf.def_regular
  4428. && eh->elf.protected_def
  4429. && eh->has_addr16_ha
  4430. && eh->has_addr16_lo
  4431. && htab->params->pic_fixup > 0))
  4432. {
  4433. unsigned int need;
  4434. /* Make sure this symbol is output as a dynamic symbol. */
  4435. if (!ensure_undef_dynamic (info, &eh->elf))
  4436. return false;
  4437. need = 0;
  4438. if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
  4439. {
  4440. if (SYMBOL_REFERENCES_LOCAL (info, &eh->elf))
  4441. /* We'll just use htab->tlsld_got.offset. This should
  4442. always be the case. It's a little odd if we have
  4443. a local dynamic reloc against a non-local symbol. */
  4444. htab->tlsld_got.refcount += 1;
  4445. else
  4446. need += 8;
  4447. }
  4448. need += got_entries_needed (eh->tls_mask);
  4449. if (need == 0)
  4450. eh->elf.got.offset = (bfd_vma) -1;
  4451. else
  4452. {
  4453. eh->elf.got.offset = allocate_got (htab, need);
  4454. if (((bfd_link_pic (info)
  4455. && !((eh->tls_mask & TLS_TLS) != 0
  4456. && bfd_link_executable (info)
  4457. && SYMBOL_REFERENCES_LOCAL (info, &eh->elf)))
  4458. || (htab->elf.dynamic_sections_created
  4459. && eh->elf.dynindx != -1
  4460. && !SYMBOL_REFERENCES_LOCAL (info, &eh->elf)))
  4461. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &eh->elf))
  4462. {
  4463. asection *rsec;
  4464. need *= sizeof (Elf32_External_Rela) / 4;
  4465. if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
  4466. need -= sizeof (Elf32_External_Rela);
  4467. rsec = htab->elf.srelgot;
  4468. if (eh->elf.type == STT_GNU_IFUNC)
  4469. rsec = htab->elf.irelplt;
  4470. rsec->size += need;
  4471. }
  4472. }
  4473. }
  4474. else
  4475. eh->elf.got.offset = (bfd_vma) -1;
  4476. /* If no dynamic sections we can't have dynamic relocs, except for
  4477. IFUNCs which are handled even in static executables. */
  4478. if (!htab->elf.dynamic_sections_created
  4479. && h->type != STT_GNU_IFUNC)
  4480. h->dyn_relocs = NULL;
  4481. /* Discard relocs on undefined symbols that must be local. */
  4482. else if (h->root.type == bfd_link_hash_undefined
  4483. && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  4484. h->dyn_relocs = NULL;
  4485. /* Also discard relocs on undefined weak syms with non-default
  4486. visibility, or when dynamic_undefined_weak says so. */
  4487. else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  4488. h->dyn_relocs = NULL;
  4489. if (h->dyn_relocs == NULL)
  4490. ;
  4491. /* In the shared -Bsymbolic case, discard space allocated for
  4492. dynamic pc-relative relocs against symbols which turn out to be
  4493. defined in regular objects. For the normal shared case, discard
  4494. space for relocs that have become local due to symbol visibility
  4495. changes. */
  4496. else if (bfd_link_pic (info))
  4497. {
  4498. /* Relocs that use pc_count are those that appear on a call insn,
  4499. or certain REL relocs (see must_be_dyn_reloc) that can be
  4500. generated via assembly. We want calls to protected symbols to
  4501. resolve directly to the function rather than going via the plt.
  4502. If people want function pointer comparisons to work as expected
  4503. then they should avoid writing weird assembly. */
  4504. if (SYMBOL_CALLS_LOCAL (info, h))
  4505. {
  4506. struct elf_dyn_relocs **pp;
  4507. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  4508. {
  4509. p->count -= p->pc_count;
  4510. p->pc_count = 0;
  4511. if (p->count == 0)
  4512. *pp = p->next;
  4513. else
  4514. pp = &p->next;
  4515. }
  4516. }
  4517. if (htab->elf.target_os == is_vxworks)
  4518. {
  4519. struct elf_dyn_relocs **pp;
  4520. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  4521. {
  4522. if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
  4523. *pp = p->next;
  4524. else
  4525. pp = &p->next;
  4526. }
  4527. }
  4528. if (h->dyn_relocs != NULL)
  4529. {
  4530. /* Make sure this symbol is output as a dynamic symbol. */
  4531. if (!ensure_undef_dynamic (info, h))
  4532. return false;
  4533. }
  4534. }
  4535. else if (ELIMINATE_COPY_RELOCS)
  4536. {
  4537. /* For the non-pic case, discard space for relocs against
  4538. symbols which turn out to need copy relocs or are not
  4539. dynamic. */
  4540. if ((h->dynamic_adjusted
  4541. || (h->ref_regular
  4542. && h->root.type == bfd_link_hash_undefweak
  4543. && (info->dynamic_undefined_weak > 0
  4544. || !_bfd_elf_readonly_dynrelocs (h))))
  4545. && !h->def_regular
  4546. && !ELF_COMMON_DEF_P (h)
  4547. && !(h->protected_def
  4548. && eh->has_addr16_ha
  4549. && eh->has_addr16_lo
  4550. && htab->params->pic_fixup > 0))
  4551. {
  4552. /* Make sure this symbol is output as a dynamic symbol. */
  4553. if (!ensure_undef_dynamic (info, h))
  4554. return false;
  4555. if (h->dynindx == -1)
  4556. h->dyn_relocs = NULL;
  4557. }
  4558. else
  4559. h->dyn_relocs = NULL;
  4560. }
  4561. /* Allocate space. */
  4562. for (p = h->dyn_relocs; p != NULL; p = p->next)
  4563. {
  4564. asection *sreloc = elf_section_data (p->sec)->sreloc;
  4565. if (eh->elf.type == STT_GNU_IFUNC)
  4566. sreloc = htab->elf.irelplt;
  4567. sreloc->size += p->count * sizeof (Elf32_External_Rela);
  4568. }
  4569. /* Handle PLT relocs. Done last, after dynindx has settled.
  4570. We might need a PLT entry when the symbol
  4571. a) is dynamic, or
  4572. b) is an ifunc, or
  4573. c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
  4574. d) has plt16 relocs and we are linking statically. */
  4575. if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
  4576. || h->type == STT_GNU_IFUNC
  4577. || (h->needs_plt && h->dynamic_adjusted)
  4578. || (h->needs_plt
  4579. && h->def_regular
  4580. && !htab->elf.dynamic_sections_created
  4581. && !htab->can_convert_all_inline_plt
  4582. && (ppc_elf_hash_entry (h)->tls_mask
  4583. & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
  4584. {
  4585. struct plt_entry *ent;
  4586. bool doneone = false;
  4587. bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
  4588. for (ent = h->plt.plist; ent != NULL; ent = ent->next)
  4589. if (ent->plt.refcount > 0)
  4590. {
  4591. asection *s;
  4592. bool dyn;
  4593. if (!ensure_undef_dynamic (info, h))
  4594. return false;
  4595. dyn = !use_local_plt (info, h);
  4596. s = htab->elf.splt;
  4597. if (!dyn)
  4598. {
  4599. if (h->type == STT_GNU_IFUNC)
  4600. s = htab->elf.iplt;
  4601. else
  4602. s = htab->pltlocal;
  4603. }
  4604. if (htab->plt_type == PLT_NEW || !dyn)
  4605. {
  4606. if (!doneone)
  4607. {
  4608. plt_offset = s->size;
  4609. s->size += 4;
  4610. }
  4611. ent->plt.offset = plt_offset;
  4612. if (s == htab->pltlocal)
  4613. ent->glink_offset = glink_offset;
  4614. else
  4615. {
  4616. s = htab->glink;
  4617. if (!doneone || bfd_link_pic (info))
  4618. {
  4619. glink_offset = s->size;
  4620. s->size += GLINK_ENTRY_SIZE (htab, h);
  4621. }
  4622. if (!doneone
  4623. && !bfd_link_pic (info)
  4624. && h->def_dynamic
  4625. && !h->def_regular)
  4626. {
  4627. h->root.u.def.section = s;
  4628. h->root.u.def.value = glink_offset;
  4629. }
  4630. ent->glink_offset = glink_offset;
  4631. if (htab->params->emit_stub_syms
  4632. && !add_stub_sym (ent, h, info))
  4633. return false;
  4634. }
  4635. }
  4636. else
  4637. {
  4638. if (!doneone)
  4639. {
  4640. /* If this is the first .plt entry, make room
  4641. for the special first entry. */
  4642. if (s->size == 0)
  4643. s->size += htab->plt_initial_entry_size;
  4644. /* The PowerPC PLT is actually composed of two
  4645. parts, the first part is 2 words (for a load
  4646. and a jump), and then there is a remaining
  4647. word available at the end. */
  4648. plt_offset = (htab->plt_initial_entry_size
  4649. + (htab->plt_slot_size
  4650. * ((s->size
  4651. - htab->plt_initial_entry_size)
  4652. / htab->plt_entry_size)));
  4653. /* If this symbol is not defined in a regular
  4654. file, and we are not generating a shared
  4655. library, then set the symbol to this location
  4656. in the .plt. This is to avoid text
  4657. relocations, and is required to make
  4658. function pointers compare as equal between
  4659. the normal executable and the shared library. */
  4660. if (! bfd_link_pic (info)
  4661. && h->def_dynamic
  4662. && !h->def_regular)
  4663. {
  4664. h->root.u.def.section = s;
  4665. h->root.u.def.value = plt_offset;
  4666. }
  4667. /* Make room for this entry. */
  4668. s->size += htab->plt_entry_size;
  4669. /* After the 8192nd entry, room for two entries
  4670. is allocated. */
  4671. if (htab->plt_type == PLT_OLD
  4672. && (s->size - htab->plt_initial_entry_size)
  4673. / htab->plt_entry_size
  4674. > PLT_NUM_SINGLE_ENTRIES)
  4675. s->size += htab->plt_entry_size;
  4676. }
  4677. ent->plt.offset = plt_offset;
  4678. }
  4679. /* We also need to make an entry in the .rela.plt section. */
  4680. if (!doneone)
  4681. {
  4682. if (!dyn)
  4683. {
  4684. if (h->type == STT_GNU_IFUNC)
  4685. {
  4686. s = htab->elf.irelplt;
  4687. s->size += sizeof (Elf32_External_Rela);
  4688. }
  4689. else if (bfd_link_pic (info))
  4690. {
  4691. s = htab->relpltlocal;
  4692. s->size += sizeof (Elf32_External_Rela);
  4693. }
  4694. }
  4695. else
  4696. {
  4697. htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
  4698. if (htab->plt_type == PLT_VXWORKS)
  4699. {
  4700. /* Allocate space for the unloaded relocations. */
  4701. if (!bfd_link_pic (info)
  4702. && htab->elf.dynamic_sections_created)
  4703. {
  4704. if (ent->plt.offset
  4705. == (bfd_vma) htab->plt_initial_entry_size)
  4706. {
  4707. htab->srelplt2->size
  4708. += (sizeof (Elf32_External_Rela)
  4709. * VXWORKS_PLTRESOLVE_RELOCS);
  4710. }
  4711. htab->srelplt2->size
  4712. += (sizeof (Elf32_External_Rela)
  4713. * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
  4714. }
  4715. /* Every PLT entry has an associated GOT entry in
  4716. .got.plt. */
  4717. htab->elf.sgotplt->size += 4;
  4718. }
  4719. }
  4720. doneone = true;
  4721. }
  4722. }
  4723. else
  4724. ent->plt.offset = (bfd_vma) -1;
  4725. if (!doneone)
  4726. {
  4727. h->plt.plist = NULL;
  4728. h->needs_plt = 0;
  4729. }
  4730. }
  4731. else
  4732. {
  4733. h->plt.plist = NULL;
  4734. h->needs_plt = 0;
  4735. }
  4736. return true;
  4737. }
  4738. static const unsigned char glink_eh_frame_cie[] =
  4739. {
  4740. 0, 0, 0, 16, /* length. */
  4741. 0, 0, 0, 0, /* id. */
  4742. 1, /* CIE version. */
  4743. 'z', 'R', 0, /* Augmentation string. */
  4744. 4, /* Code alignment. */
  4745. 0x7c, /* Data alignment. */
  4746. 65, /* RA reg. */
  4747. 1, /* Augmentation size. */
  4748. DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
  4749. DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
  4750. };
  4751. /* Set the sizes of the dynamic sections. */
  4752. static bool
  4753. ppc_elf_size_dynamic_sections (bfd *output_bfd,
  4754. struct bfd_link_info *info)
  4755. {
  4756. struct ppc_elf_link_hash_table *htab;
  4757. asection *s;
  4758. bool relocs;
  4759. bfd *ibfd;
  4760. #ifdef DEBUG
  4761. fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
  4762. #endif
  4763. htab = ppc_elf_hash_table (info);
  4764. BFD_ASSERT (htab->elf.dynobj != NULL);
  4765. if (elf_hash_table (info)->dynamic_sections_created)
  4766. {
  4767. /* Set the contents of the .interp section to the interpreter. */
  4768. if (bfd_link_executable (info) && !info->nointerp)
  4769. {
  4770. s = bfd_get_linker_section (htab->elf.dynobj, ".interp");
  4771. BFD_ASSERT (s != NULL);
  4772. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  4773. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  4774. }
  4775. }
  4776. if (htab->plt_type == PLT_OLD)
  4777. htab->got_header_size = 16;
  4778. else if (htab->plt_type == PLT_NEW)
  4779. htab->got_header_size = 12;
  4780. /* Set up .got offsets for local syms, and space for local dynamic
  4781. relocs. */
  4782. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  4783. {
  4784. bfd_signed_vma *local_got;
  4785. bfd_signed_vma *end_local_got;
  4786. struct plt_entry **local_plt;
  4787. struct plt_entry **end_local_plt;
  4788. char *lgot_masks;
  4789. bfd_size_type locsymcount;
  4790. Elf_Internal_Shdr *symtab_hdr;
  4791. if (!is_ppc_elf (ibfd))
  4792. continue;
  4793. for (s = ibfd->sections; s != NULL; s = s->next)
  4794. {
  4795. struct ppc_dyn_relocs *p;
  4796. for (p = ((struct ppc_dyn_relocs *)
  4797. elf_section_data (s)->local_dynrel);
  4798. p != NULL;
  4799. p = p->next)
  4800. {
  4801. if (!bfd_is_abs_section (p->sec)
  4802. && bfd_is_abs_section (p->sec->output_section))
  4803. {
  4804. /* Input section has been discarded, either because
  4805. it is a copy of a linkonce section or due to
  4806. linker script /DISCARD/, so we'll be discarding
  4807. the relocs too. */
  4808. }
  4809. else if (htab->elf.target_os == is_vxworks
  4810. && strcmp (p->sec->output_section->name,
  4811. ".tls_vars") == 0)
  4812. {
  4813. /* Relocations in vxworks .tls_vars sections are
  4814. handled specially by the loader. */
  4815. }
  4816. else if (p->count != 0)
  4817. {
  4818. asection *sreloc = elf_section_data (p->sec)->sreloc;
  4819. if (p->ifunc)
  4820. sreloc = htab->elf.irelplt;
  4821. sreloc->size += p->count * sizeof (Elf32_External_Rela);
  4822. if ((p->sec->output_section->flags
  4823. & (SEC_READONLY | SEC_ALLOC))
  4824. == (SEC_READONLY | SEC_ALLOC))
  4825. {
  4826. info->flags |= DF_TEXTREL;
  4827. info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
  4828. p->sec->owner, p->sec);
  4829. }
  4830. }
  4831. }
  4832. }
  4833. local_got = elf_local_got_refcounts (ibfd);
  4834. if (!local_got)
  4835. continue;
  4836. symtab_hdr = &elf_symtab_hdr (ibfd);
  4837. locsymcount = symtab_hdr->sh_info;
  4838. end_local_got = local_got + locsymcount;
  4839. local_plt = (struct plt_entry **) end_local_got;
  4840. end_local_plt = local_plt + locsymcount;
  4841. lgot_masks = (char *) end_local_plt;
  4842. for (; local_got < end_local_got; ++local_got, ++lgot_masks)
  4843. if (*local_got > 0)
  4844. {
  4845. unsigned int need;
  4846. if ((*lgot_masks & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
  4847. htab->tlsld_got.refcount += 1;
  4848. need = got_entries_needed (*lgot_masks);
  4849. if (need == 0)
  4850. *local_got = (bfd_vma) -1;
  4851. else
  4852. {
  4853. *local_got = allocate_got (htab, need);
  4854. if (bfd_link_pic (info)
  4855. && !((*lgot_masks & TLS_TLS) != 0
  4856. && bfd_link_executable (info)))
  4857. {
  4858. asection *srel;
  4859. need *= sizeof (Elf32_External_Rela) / 4;
  4860. srel = htab->elf.srelgot;
  4861. if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
  4862. srel = htab->elf.irelplt;
  4863. srel->size += need;
  4864. }
  4865. }
  4866. }
  4867. else
  4868. *local_got = (bfd_vma) -1;
  4869. if (htab->elf.target_os == is_vxworks)
  4870. continue;
  4871. /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
  4872. lgot_masks = (char *) end_local_plt;
  4873. for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
  4874. {
  4875. struct plt_entry *ent;
  4876. bool doneone = false;
  4877. bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
  4878. for (ent = *local_plt; ent != NULL; ent = ent->next)
  4879. if (ent->plt.refcount > 0)
  4880. {
  4881. if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
  4882. s = htab->elf.iplt;
  4883. else if (htab->can_convert_all_inline_plt
  4884. || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
  4885. {
  4886. ent->plt.offset = (bfd_vma) -1;
  4887. continue;
  4888. }
  4889. else
  4890. s = htab->pltlocal;
  4891. if (!doneone)
  4892. {
  4893. plt_offset = s->size;
  4894. s->size += 4;
  4895. }
  4896. ent->plt.offset = plt_offset;
  4897. if (s != htab->pltlocal && (!doneone || bfd_link_pic (info)))
  4898. {
  4899. s = htab->glink;
  4900. glink_offset = s->size;
  4901. s->size += GLINK_ENTRY_SIZE (htab, NULL);
  4902. }
  4903. ent->glink_offset = glink_offset;
  4904. if (!doneone)
  4905. {
  4906. if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
  4907. {
  4908. s = htab->elf.irelplt;
  4909. s->size += sizeof (Elf32_External_Rela);
  4910. }
  4911. else if (bfd_link_pic (info))
  4912. {
  4913. s = htab->relpltlocal;
  4914. s->size += sizeof (Elf32_External_Rela);
  4915. }
  4916. doneone = true;
  4917. }
  4918. }
  4919. else
  4920. ent->plt.offset = (bfd_vma) -1;
  4921. }
  4922. }
  4923. /* Allocate space for global sym dynamic relocs. */
  4924. elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
  4925. if (htab->tlsld_got.refcount > 0)
  4926. {
  4927. htab->tlsld_got.offset = allocate_got (htab, 8);
  4928. if (bfd_link_dll (info))
  4929. htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
  4930. }
  4931. else
  4932. htab->tlsld_got.offset = (bfd_vma) -1;
  4933. if (htab->elf.sgot != NULL && htab->plt_type != PLT_VXWORKS)
  4934. {
  4935. unsigned int g_o_t = 32768;
  4936. /* If we haven't allocated the header, do so now. When we get here,
  4937. for old plt/got the got size will be 0 to 32764 (not allocated),
  4938. or 32780 to 65536 (header allocated). For new plt/got, the
  4939. corresponding ranges are 0 to 32768 and 32780 to 65536. */
  4940. if (htab->elf.sgot->size <= 32768)
  4941. {
  4942. g_o_t = htab->elf.sgot->size;
  4943. if (htab->plt_type == PLT_OLD)
  4944. g_o_t += 4;
  4945. htab->elf.sgot->size += htab->got_header_size;
  4946. }
  4947. htab->elf.hgot->root.u.def.value = g_o_t;
  4948. }
  4949. if (bfd_link_pic (info))
  4950. {
  4951. struct elf_link_hash_entry *sda = htab->sdata[0].sym;
  4952. sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
  4953. sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
  4954. }
  4955. if (info->emitrelocations)
  4956. {
  4957. struct elf_link_hash_entry *sda = htab->sdata[0].sym;
  4958. if (sda != NULL && sda->ref_regular)
  4959. sda->root.u.def.section->flags |= SEC_KEEP;
  4960. sda = htab->sdata[1].sym;
  4961. if (sda != NULL && sda->ref_regular)
  4962. sda->root.u.def.section->flags |= SEC_KEEP;
  4963. }
  4964. if (htab->glink != NULL
  4965. && htab->glink->size != 0
  4966. && htab->elf.dynamic_sections_created)
  4967. {
  4968. htab->glink_pltresolve = htab->glink->size;
  4969. /* Space for the branch table. */
  4970. htab->glink->size
  4971. += htab->elf.srelplt->size / (sizeof (Elf32_External_Rela) / 4) - 4;
  4972. /* Pad out to align the start of PLTresolve. */
  4973. htab->glink->size += -htab->glink->size & (htab->params->ppc476_workaround
  4974. ? 63 : 15);
  4975. htab->glink->size += GLINK_PLTRESOLVE;
  4976. if (htab->params->emit_stub_syms)
  4977. {
  4978. struct elf_link_hash_entry *sh;
  4979. sh = elf_link_hash_lookup (&htab->elf, "__glink",
  4980. true, false, false);
  4981. if (sh == NULL)
  4982. return false;
  4983. if (sh->root.type == bfd_link_hash_new)
  4984. {
  4985. sh->root.type = bfd_link_hash_defined;
  4986. sh->root.u.def.section = htab->glink;
  4987. sh->root.u.def.value = htab->glink_pltresolve;
  4988. sh->ref_regular = 1;
  4989. sh->def_regular = 1;
  4990. sh->ref_regular_nonweak = 1;
  4991. sh->forced_local = 1;
  4992. sh->non_elf = 0;
  4993. sh->root.linker_def = 1;
  4994. }
  4995. sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
  4996. true, false, false);
  4997. if (sh == NULL)
  4998. return false;
  4999. if (sh->root.type == bfd_link_hash_new)
  5000. {
  5001. sh->root.type = bfd_link_hash_defined;
  5002. sh->root.u.def.section = htab->glink;
  5003. sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
  5004. sh->ref_regular = 1;
  5005. sh->def_regular = 1;
  5006. sh->ref_regular_nonweak = 1;
  5007. sh->forced_local = 1;
  5008. sh->non_elf = 0;
  5009. sh->root.linker_def = 1;
  5010. }
  5011. }
  5012. }
  5013. if (htab->glink != NULL
  5014. && htab->glink->size != 0
  5015. && htab->glink_eh_frame != NULL
  5016. && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
  5017. && _bfd_elf_eh_frame_present (info))
  5018. {
  5019. s = htab->glink_eh_frame;
  5020. s->size = sizeof (glink_eh_frame_cie) + 20;
  5021. if (bfd_link_pic (info))
  5022. {
  5023. s->size += 4;
  5024. if (htab->glink->size - GLINK_PLTRESOLVE + 8 >= 256)
  5025. s->size += 4;
  5026. }
  5027. }
  5028. /* We've now determined the sizes of the various dynamic sections.
  5029. Allocate memory for them. */
  5030. relocs = false;
  5031. for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
  5032. {
  5033. bool strip_section = true;
  5034. if ((s->flags & SEC_LINKER_CREATED) == 0)
  5035. continue;
  5036. if (s == htab->elf.splt
  5037. || s == htab->elf.sgot)
  5038. {
  5039. /* We'd like to strip these sections if they aren't needed, but if
  5040. we've exported dynamic symbols from them we must leave them.
  5041. It's too late to tell BFD to get rid of the symbols. */
  5042. if (htab->elf.hplt != NULL)
  5043. strip_section = false;
  5044. /* Strip this section if we don't need it; see the
  5045. comment below. */
  5046. }
  5047. else if (s == htab->elf.iplt
  5048. || s == htab->pltlocal
  5049. || s == htab->glink
  5050. || s == htab->glink_eh_frame
  5051. || s == htab->elf.sgotplt
  5052. || s == htab->sbss
  5053. || s == htab->elf.sdynbss
  5054. || s == htab->elf.sdynrelro
  5055. || s == htab->dynsbss)
  5056. {
  5057. /* Strip these too. */
  5058. }
  5059. else if (s == htab->sdata[0].section
  5060. || s == htab->sdata[1].section)
  5061. {
  5062. strip_section = (s->flags & SEC_KEEP) == 0;
  5063. }
  5064. else if (startswith (bfd_section_name (s), ".rela"))
  5065. {
  5066. if (s->size != 0)
  5067. {
  5068. /* Remember whether there are any relocation sections. */
  5069. relocs = true;
  5070. /* We use the reloc_count field as a counter if we need
  5071. to copy relocs into the output file. */
  5072. s->reloc_count = 0;
  5073. }
  5074. }
  5075. else
  5076. {
  5077. /* It's not one of our sections, so don't allocate space. */
  5078. continue;
  5079. }
  5080. if (s->size == 0 && strip_section)
  5081. {
  5082. /* If we don't need this section, strip it from the
  5083. output file. This is mostly to handle .rela.bss and
  5084. .rela.plt. We must create both sections in
  5085. create_dynamic_sections, because they must be created
  5086. before the linker maps input sections to output
  5087. sections. The linker does that before
  5088. adjust_dynamic_symbol is called, and it is that
  5089. function which decides whether anything needs to go
  5090. into these sections. */
  5091. s->flags |= SEC_EXCLUDE;
  5092. continue;
  5093. }
  5094. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  5095. continue;
  5096. /* Allocate memory for the section contents. */
  5097. s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
  5098. if (s->contents == NULL)
  5099. return false;
  5100. }
  5101. if (htab->elf.dynamic_sections_created)
  5102. {
  5103. /* Add some entries to the .dynamic section. We fill in the
  5104. values later, in ppc_elf_finish_dynamic_sections, but we
  5105. must add the entries now so that we get the correct size for
  5106. the .dynamic section. The DT_DEBUG entry is filled in by the
  5107. dynamic linker and used by the debugger. */
  5108. #define add_dynamic_entry(TAG, VAL) \
  5109. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  5110. if (!_bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
  5111. relocs))
  5112. return false;
  5113. if (htab->plt_type == PLT_NEW
  5114. && htab->glink != NULL
  5115. && htab->glink->size != 0)
  5116. {
  5117. if (!add_dynamic_entry (DT_PPC_GOT, 0))
  5118. return false;
  5119. if (!htab->params->no_tls_get_addr_opt
  5120. && htab->tls_get_addr != NULL
  5121. && htab->tls_get_addr->plt.plist != NULL
  5122. && !add_dynamic_entry (DT_PPC_OPT, PPC_OPT_TLS))
  5123. return false;
  5124. }
  5125. }
  5126. #undef add_dynamic_entry
  5127. if (htab->glink_eh_frame != NULL
  5128. && htab->glink_eh_frame->contents != NULL)
  5129. {
  5130. unsigned char *p = htab->glink_eh_frame->contents;
  5131. bfd_vma val;
  5132. memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
  5133. /* CIE length (rewrite in case little-endian). */
  5134. bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
  5135. p += sizeof (glink_eh_frame_cie);
  5136. /* FDE length. */
  5137. val = htab->glink_eh_frame->size - 4 - sizeof (glink_eh_frame_cie);
  5138. bfd_put_32 (htab->elf.dynobj, val, p);
  5139. p += 4;
  5140. /* CIE pointer. */
  5141. val = p - htab->glink_eh_frame->contents;
  5142. bfd_put_32 (htab->elf.dynobj, val, p);
  5143. p += 4;
  5144. /* Offset to .glink. Set later. */
  5145. p += 4;
  5146. /* .glink size. */
  5147. bfd_put_32 (htab->elf.dynobj, htab->glink->size, p);
  5148. p += 4;
  5149. /* Augmentation. */
  5150. p += 1;
  5151. if (bfd_link_pic (info)
  5152. && htab->elf.dynamic_sections_created)
  5153. {
  5154. bfd_vma adv = (htab->glink->size - GLINK_PLTRESOLVE + 8) >> 2;
  5155. if (adv < 64)
  5156. *p++ = DW_CFA_advance_loc + adv;
  5157. else if (adv < 256)
  5158. {
  5159. *p++ = DW_CFA_advance_loc1;
  5160. *p++ = adv;
  5161. }
  5162. else if (adv < 65536)
  5163. {
  5164. *p++ = DW_CFA_advance_loc2;
  5165. bfd_put_16 (htab->elf.dynobj, adv, p);
  5166. p += 2;
  5167. }
  5168. else
  5169. {
  5170. *p++ = DW_CFA_advance_loc4;
  5171. bfd_put_32 (htab->elf.dynobj, adv, p);
  5172. p += 4;
  5173. }
  5174. *p++ = DW_CFA_register;
  5175. *p++ = 65;
  5176. p++;
  5177. *p++ = DW_CFA_advance_loc + 4;
  5178. *p++ = DW_CFA_restore_extended;
  5179. *p++ = 65;
  5180. }
  5181. BFD_ASSERT ((bfd_vma) ((p + 3 - htab->glink_eh_frame->contents) & -4)
  5182. == htab->glink_eh_frame->size);
  5183. }
  5184. return true;
  5185. }
  5186. /* Arrange to have _SDA_BASE_ or _SDA2_BASE_ stripped from the output
  5187. if it looks like nothing is using them. */
  5188. static void
  5189. maybe_strip_sdasym (bfd *output_bfd, elf_linker_section_t *lsect)
  5190. {
  5191. struct elf_link_hash_entry *sda = lsect->sym;
  5192. if (sda != NULL && !sda->ref_regular && sda->dynindx == -1)
  5193. {
  5194. asection *s;
  5195. s = bfd_get_section_by_name (output_bfd, lsect->name);
  5196. if (s == NULL || bfd_section_removed_from_list (output_bfd, s))
  5197. {
  5198. s = bfd_get_section_by_name (output_bfd, lsect->bss_name);
  5199. if (s == NULL || bfd_section_removed_from_list (output_bfd, s))
  5200. {
  5201. sda->def_regular = 0;
  5202. /* This is somewhat magic. See elf_link_output_extsym. */
  5203. sda->ref_dynamic = 1;
  5204. sda->forced_local = 0;
  5205. }
  5206. }
  5207. }
  5208. }
  5209. void
  5210. ppc_elf_maybe_strip_sdata_syms (struct bfd_link_info *info)
  5211. {
  5212. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  5213. if (htab != NULL)
  5214. {
  5215. maybe_strip_sdasym (info->output_bfd, &htab->sdata[0]);
  5216. maybe_strip_sdasym (info->output_bfd, &htab->sdata[1]);
  5217. }
  5218. }
  5219. /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
  5220. static bool
  5221. ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
  5222. {
  5223. if (h->plt.plist != NULL
  5224. && !h->def_regular
  5225. && (!h->pointer_equality_needed
  5226. || !h->ref_regular_nonweak))
  5227. return false;
  5228. return _bfd_elf_hash_symbol (h);
  5229. }
  5230. #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
  5231. /* Relaxation trampolines. r12 is available for clobbering (r11, is
  5232. used for some functions that are allowed to break the ABI). */
  5233. static const int shared_stub_entry[] =
  5234. {
  5235. 0x7c0802a6, /* mflr 0 */
  5236. 0x429f0005, /* bcl 20, 31, .Lxxx */
  5237. 0x7d8802a6, /* mflr 12 */
  5238. 0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
  5239. 0x398c0000, /* addi 12, 12, (xxx-.Lxxx)@l */
  5240. 0x7c0803a6, /* mtlr 0 */
  5241. 0x7d8903a6, /* mtctr 12 */
  5242. 0x4e800420, /* bctr */
  5243. };
  5244. static const int stub_entry[] =
  5245. {
  5246. 0x3d800000, /* lis 12,xxx@ha */
  5247. 0x398c0000, /* addi 12,12,xxx@l */
  5248. 0x7d8903a6, /* mtctr 12 */
  5249. 0x4e800420, /* bctr */
  5250. };
  5251. struct ppc_elf_relax_info
  5252. {
  5253. unsigned int workaround_size;
  5254. unsigned int picfixup_size;
  5255. };
  5256. /* This function implements long branch trampolines, and the ppc476
  5257. icache bug workaround. Any section needing trampolines or patch
  5258. space for the workaround has its size extended so that we can
  5259. add trampolines at the end of the section. */
  5260. static bool
  5261. ppc_elf_relax_section (bfd *abfd,
  5262. asection *isec,
  5263. struct bfd_link_info *link_info,
  5264. bool *again)
  5265. {
  5266. struct one_branch_fixup
  5267. {
  5268. struct one_branch_fixup *next;
  5269. asection *tsec;
  5270. /* Final link, can use the symbol offset. For a
  5271. relocatable link we use the symbol's index. */
  5272. bfd_vma toff;
  5273. bfd_vma trampoff;
  5274. };
  5275. Elf_Internal_Shdr *symtab_hdr;
  5276. bfd_byte *contents = NULL;
  5277. Elf_Internal_Sym *isymbuf = NULL;
  5278. Elf_Internal_Rela *internal_relocs = NULL;
  5279. Elf_Internal_Rela *irel, *irelend = NULL;
  5280. struct one_branch_fixup *branch_fixups = NULL;
  5281. struct ppc_elf_relax_info *relax_info = NULL;
  5282. unsigned changes = 0;
  5283. bool workaround_change;
  5284. struct ppc_elf_link_hash_table *htab;
  5285. bfd_size_type trampbase, trampoff, newsize, picfixup_size;
  5286. asection *got2;
  5287. bool maybe_pasted;
  5288. *again = false;
  5289. /* No need to do anything with non-alloc or non-code sections. */
  5290. if ((isec->flags & SEC_ALLOC) == 0
  5291. || (isec->flags & SEC_CODE) == 0
  5292. || (isec->flags & SEC_LINKER_CREATED) != 0
  5293. || isec->size < 4)
  5294. return true;
  5295. /* We cannot represent the required PIC relocs in the output, so don't
  5296. do anything. The linker doesn't support mixing -shared and -r
  5297. anyway. */
  5298. if (bfd_link_relocatable (link_info) && bfd_link_pic (link_info))
  5299. return true;
  5300. htab = ppc_elf_hash_table (link_info);
  5301. if (htab == NULL)
  5302. return true;
  5303. isec->size = (isec->size + 3) & -4;
  5304. if (isec->rawsize == 0)
  5305. isec->rawsize = isec->size;
  5306. trampbase = isec->size;
  5307. BFD_ASSERT (isec->sec_info_type == SEC_INFO_TYPE_NONE
  5308. || isec->sec_info_type == SEC_INFO_TYPE_TARGET);
  5309. isec->sec_info_type = SEC_INFO_TYPE_TARGET;
  5310. if (htab->params->ppc476_workaround
  5311. || htab->params->pic_fixup > 0)
  5312. {
  5313. if (elf_section_data (isec)->sec_info == NULL)
  5314. {
  5315. elf_section_data (isec)->sec_info
  5316. = bfd_zalloc (abfd, sizeof (struct ppc_elf_relax_info));
  5317. if (elf_section_data (isec)->sec_info == NULL)
  5318. return false;
  5319. }
  5320. relax_info = elf_section_data (isec)->sec_info;
  5321. trampbase -= relax_info->workaround_size;
  5322. }
  5323. maybe_pasted = (strcmp (isec->output_section->name, ".init") == 0
  5324. || strcmp (isec->output_section->name, ".fini") == 0);
  5325. /* Space for a branch around any trampolines. */
  5326. trampoff = trampbase;
  5327. if (maybe_pasted && trampbase == isec->rawsize)
  5328. trampoff += 4;
  5329. symtab_hdr = &elf_symtab_hdr (abfd);
  5330. picfixup_size = 0;
  5331. if (htab->params->branch_trampolines
  5332. || htab->params->pic_fixup > 0)
  5333. {
  5334. /* Get a copy of the native relocations. */
  5335. if (isec->reloc_count != 0)
  5336. {
  5337. internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
  5338. link_info->keep_memory);
  5339. if (internal_relocs == NULL)
  5340. goto error_return;
  5341. }
  5342. got2 = bfd_get_section_by_name (abfd, ".got2");
  5343. irelend = internal_relocs + isec->reloc_count;
  5344. for (irel = internal_relocs; irel < irelend; irel++)
  5345. {
  5346. unsigned long r_type = ELF32_R_TYPE (irel->r_info);
  5347. bfd_vma toff, roff;
  5348. asection *tsec;
  5349. struct one_branch_fixup *f;
  5350. size_t insn_offset = 0;
  5351. bfd_vma max_branch_offset = 0, val;
  5352. bfd_byte *hit_addr;
  5353. unsigned long t0;
  5354. struct elf_link_hash_entry *h;
  5355. Elf_Internal_Sym *isym;
  5356. struct plt_entry **plist;
  5357. unsigned char sym_type;
  5358. switch (r_type)
  5359. {
  5360. case R_PPC_REL24:
  5361. case R_PPC_LOCAL24PC:
  5362. case R_PPC_PLTREL24:
  5363. case R_PPC_PLTCALL:
  5364. max_branch_offset = 1 << 25;
  5365. break;
  5366. case R_PPC_REL14:
  5367. case R_PPC_REL14_BRTAKEN:
  5368. case R_PPC_REL14_BRNTAKEN:
  5369. max_branch_offset = 1 << 15;
  5370. break;
  5371. case R_PPC_ADDR16_HA:
  5372. if (htab->params->pic_fixup > 0)
  5373. break;
  5374. continue;
  5375. default:
  5376. continue;
  5377. }
  5378. /* Get the value of the symbol referred to by the reloc. */
  5379. if (!get_sym_h (&h, &isym, &tsec, NULL, &isymbuf,
  5380. ELF32_R_SYM (irel->r_info), abfd))
  5381. goto error_return;
  5382. if (isym != NULL)
  5383. {
  5384. if (tsec != NULL)
  5385. ;
  5386. else if (isym->st_shndx == SHN_ABS)
  5387. tsec = bfd_abs_section_ptr;
  5388. else
  5389. continue;
  5390. toff = isym->st_value;
  5391. sym_type = ELF_ST_TYPE (isym->st_info);
  5392. }
  5393. else
  5394. {
  5395. if (tsec != NULL)
  5396. toff = h->root.u.def.value;
  5397. else if (h->root.type == bfd_link_hash_undefined
  5398. || h->root.type == bfd_link_hash_undefweak)
  5399. {
  5400. unsigned long indx;
  5401. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  5402. tsec = bfd_und_section_ptr;
  5403. toff = bfd_link_relocatable (link_info) ? indx : 0;
  5404. }
  5405. else
  5406. continue;
  5407. /* If this branch is to __tls_get_addr then we may later
  5408. optimise away the call. We won't be needing a long-
  5409. branch stub in that case. */
  5410. if (bfd_link_executable (link_info)
  5411. && h == htab->tls_get_addr
  5412. && irel != internal_relocs)
  5413. {
  5414. unsigned long t_symndx = ELF32_R_SYM (irel[-1].r_info);
  5415. unsigned long t_rtype = ELF32_R_TYPE (irel[-1].r_info);
  5416. unsigned int tls_mask = 0;
  5417. /* The previous reloc should be one of R_PPC_TLSGD or
  5418. R_PPC_TLSLD, or for older object files, a reloc
  5419. on the __tls_get_addr arg setup insn. Get tls
  5420. mask bits from the symbol on that reloc. */
  5421. if (t_symndx < symtab_hdr->sh_info)
  5422. {
  5423. bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
  5424. if (local_got_offsets != NULL)
  5425. {
  5426. struct plt_entry **local_plt = (struct plt_entry **)
  5427. (local_got_offsets + symtab_hdr->sh_info);
  5428. char *lgot_masks = (char *)
  5429. (local_plt + symtab_hdr->sh_info);
  5430. tls_mask = lgot_masks[t_symndx];
  5431. }
  5432. }
  5433. else
  5434. {
  5435. struct elf_link_hash_entry *th
  5436. = elf_sym_hashes (abfd)[t_symndx - symtab_hdr->sh_info];
  5437. while (th->root.type == bfd_link_hash_indirect
  5438. || th->root.type == bfd_link_hash_warning)
  5439. th = (struct elf_link_hash_entry *) th->root.u.i.link;
  5440. tls_mask
  5441. = ((struct ppc_elf_link_hash_entry *) th)->tls_mask;
  5442. }
  5443. /* The mask bits tell us if the call will be
  5444. optimised away. */
  5445. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
  5446. && (t_rtype == R_PPC_TLSGD
  5447. || t_rtype == R_PPC_GOT_TLSGD16
  5448. || t_rtype == R_PPC_GOT_TLSGD16_LO))
  5449. continue;
  5450. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
  5451. && (t_rtype == R_PPC_TLSLD
  5452. || t_rtype == R_PPC_GOT_TLSLD16
  5453. || t_rtype == R_PPC_GOT_TLSLD16_LO))
  5454. continue;
  5455. }
  5456. sym_type = h->type;
  5457. }
  5458. if (r_type == R_PPC_ADDR16_HA)
  5459. {
  5460. if (h != NULL
  5461. && !h->def_regular
  5462. && h->protected_def
  5463. && ppc_elf_hash_entry (h)->has_addr16_ha
  5464. && ppc_elf_hash_entry (h)->has_addr16_lo)
  5465. picfixup_size += 12;
  5466. continue;
  5467. }
  5468. /* The condition here under which we call find_plt_ent must
  5469. match that in relocate_section. If we call find_plt_ent here
  5470. but not in relocate_section, or vice versa, then the branch
  5471. destination used here may be incorrect. */
  5472. plist = NULL;
  5473. if (h != NULL)
  5474. {
  5475. /* We know is_branch_reloc (r_type) is true. */
  5476. if (h->type == STT_GNU_IFUNC
  5477. || r_type == R_PPC_PLTREL24)
  5478. plist = &h->plt.plist;
  5479. }
  5480. else if (sym_type == STT_GNU_IFUNC
  5481. && elf_local_got_offsets (abfd) != NULL)
  5482. {
  5483. bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
  5484. struct plt_entry **local_plt = (struct plt_entry **)
  5485. (local_got_offsets + symtab_hdr->sh_info);
  5486. plist = local_plt + ELF32_R_SYM (irel->r_info);
  5487. }
  5488. if (plist != NULL)
  5489. {
  5490. bfd_vma addend = 0;
  5491. struct plt_entry *ent;
  5492. if (r_type == R_PPC_PLTREL24 && bfd_link_pic (link_info))
  5493. addend = irel->r_addend;
  5494. ent = find_plt_ent (plist, got2, addend);
  5495. if (ent != NULL)
  5496. {
  5497. if (htab->plt_type == PLT_NEW
  5498. || h == NULL
  5499. || !htab->elf.dynamic_sections_created
  5500. || h->dynindx == -1)
  5501. {
  5502. tsec = htab->glink;
  5503. toff = ent->glink_offset;
  5504. }
  5505. else
  5506. {
  5507. tsec = htab->elf.splt;
  5508. toff = ent->plt.offset;
  5509. }
  5510. }
  5511. }
  5512. /* If the branch and target are in the same section, you have
  5513. no hope of adding stubs. We'll error out later should the
  5514. branch overflow. */
  5515. if (tsec == isec)
  5516. continue;
  5517. /* toff is used for the symbol index when the symbol is
  5518. undefined and we're doing a relocatable link, so we can't
  5519. support addends. It would be possible to do so by
  5520. putting the addend in one_branch_fixup but addends on
  5521. branches are rare so it hardly seems worth supporting. */
  5522. if (bfd_link_relocatable (link_info)
  5523. && tsec == bfd_und_section_ptr
  5524. && r_type != R_PPC_PLTREL24
  5525. && irel->r_addend != 0)
  5526. continue;
  5527. /* There probably isn't any reason to handle symbols in
  5528. SEC_MERGE sections; SEC_MERGE doesn't seem a likely
  5529. attribute for a code section, and we are only looking at
  5530. branches. However, implement it correctly here as a
  5531. reference for other target relax_section functions. */
  5532. if (0 && tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
  5533. {
  5534. /* At this stage in linking, no SEC_MERGE symbol has been
  5535. adjusted, so all references to such symbols need to be
  5536. passed through _bfd_merged_section_offset. (Later, in
  5537. relocate_section, all SEC_MERGE symbols *except* for
  5538. section symbols have been adjusted.)
  5539. gas may reduce relocations against symbols in SEC_MERGE
  5540. sections to a relocation against the section symbol when
  5541. the original addend was zero. When the reloc is against
  5542. a section symbol we should include the addend in the
  5543. offset passed to _bfd_merged_section_offset, since the
  5544. location of interest is the original symbol. On the
  5545. other hand, an access to "sym+addend" where "sym" is not
  5546. a section symbol should not include the addend; Such an
  5547. access is presumed to be an offset from "sym"; The
  5548. location of interest is just "sym". */
  5549. if (sym_type == STT_SECTION
  5550. && r_type != R_PPC_PLTREL24)
  5551. toff += irel->r_addend;
  5552. toff
  5553. = _bfd_merged_section_offset (abfd, &tsec,
  5554. elf_section_data (tsec)->sec_info,
  5555. toff);
  5556. if (sym_type != STT_SECTION
  5557. && r_type != R_PPC_PLTREL24)
  5558. toff += irel->r_addend;
  5559. }
  5560. /* PLTREL24 addends are special. */
  5561. else if (r_type != R_PPC_PLTREL24)
  5562. toff += irel->r_addend;
  5563. /* Attempted -shared link of non-pic code loses. */
  5564. if ((!bfd_link_relocatable (link_info)
  5565. && tsec == bfd_und_section_ptr)
  5566. || tsec->output_section == NULL
  5567. || (tsec->owner != NULL
  5568. && (tsec->owner->flags & BFD_PLUGIN) != 0))
  5569. continue;
  5570. roff = irel->r_offset;
  5571. /* Avoid creating a lot of unnecessary fixups when
  5572. relocatable if the output section size is such that a
  5573. fixup can be created at final link.
  5574. The max_branch_offset adjustment allows for some number
  5575. of other fixups being needed at final link. */
  5576. if (bfd_link_relocatable (link_info)
  5577. && (isec->output_section->rawsize - (isec->output_offset + roff)
  5578. < max_branch_offset - (max_branch_offset >> 4)))
  5579. continue;
  5580. /* If the branch is in range, no need to do anything. */
  5581. if (tsec != bfd_und_section_ptr
  5582. && (!bfd_link_relocatable (link_info)
  5583. /* A relocatable link may have sections moved during
  5584. final link, so do not presume they remain in range. */
  5585. || tsec->output_section == isec->output_section))
  5586. {
  5587. bfd_vma symaddr, reladdr;
  5588. symaddr = tsec->output_section->vma + tsec->output_offset + toff;
  5589. reladdr = isec->output_section->vma + isec->output_offset + roff;
  5590. if (symaddr - reladdr + max_branch_offset
  5591. < 2 * max_branch_offset)
  5592. continue;
  5593. }
  5594. /* Look for an existing fixup to this address. */
  5595. for (f = branch_fixups; f ; f = f->next)
  5596. if (f->tsec == tsec && f->toff == toff)
  5597. break;
  5598. if (f == NULL)
  5599. {
  5600. size_t size;
  5601. unsigned long stub_rtype;
  5602. val = trampoff - roff;
  5603. if (val >= max_branch_offset)
  5604. /* Oh dear, we can't reach a trampoline. Don't try to add
  5605. one. We'll report an error later. */
  5606. continue;
  5607. if (bfd_link_pic (link_info))
  5608. {
  5609. size = 4 * ARRAY_SIZE (shared_stub_entry);
  5610. insn_offset = 12;
  5611. }
  5612. else
  5613. {
  5614. size = 4 * ARRAY_SIZE (stub_entry);
  5615. insn_offset = 0;
  5616. }
  5617. stub_rtype = R_PPC_RELAX;
  5618. if (tsec == htab->elf.splt
  5619. || tsec == htab->glink)
  5620. {
  5621. stub_rtype = R_PPC_RELAX_PLT;
  5622. if (r_type == R_PPC_PLTREL24)
  5623. stub_rtype = R_PPC_RELAX_PLTREL24;
  5624. }
  5625. /* Hijack the old relocation. Since we need two
  5626. relocations for this use a "composite" reloc. */
  5627. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  5628. stub_rtype);
  5629. irel->r_offset = trampoff + insn_offset;
  5630. if (r_type == R_PPC_PLTREL24
  5631. && stub_rtype != R_PPC_RELAX_PLTREL24)
  5632. irel->r_addend = 0;
  5633. /* Record the fixup so we don't do it again this section. */
  5634. f = bfd_malloc (sizeof (*f));
  5635. f->next = branch_fixups;
  5636. f->tsec = tsec;
  5637. f->toff = toff;
  5638. f->trampoff = trampoff;
  5639. branch_fixups = f;
  5640. trampoff += size;
  5641. changes++;
  5642. }
  5643. else
  5644. {
  5645. val = f->trampoff - roff;
  5646. if (val >= max_branch_offset)
  5647. continue;
  5648. /* Nop out the reloc, since we're finalizing things here. */
  5649. irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
  5650. }
  5651. /* Get the section contents. */
  5652. if (contents == NULL)
  5653. {
  5654. /* Get cached copy if it exists. */
  5655. if (elf_section_data (isec)->this_hdr.contents != NULL)
  5656. contents = elf_section_data (isec)->this_hdr.contents;
  5657. /* Go get them off disk. */
  5658. else if (!bfd_malloc_and_get_section (abfd, isec, &contents))
  5659. goto error_return;
  5660. }
  5661. /* Fix up the existing branch to hit the trampoline. */
  5662. hit_addr = contents + roff;
  5663. switch (r_type)
  5664. {
  5665. case R_PPC_REL24:
  5666. case R_PPC_LOCAL24PC:
  5667. case R_PPC_PLTREL24:
  5668. t0 = bfd_get_32 (abfd, hit_addr);
  5669. t0 &= ~0x3fffffc;
  5670. t0 |= val & 0x3fffffc;
  5671. bfd_put_32 (abfd, t0, hit_addr);
  5672. break;
  5673. case R_PPC_REL14:
  5674. case R_PPC_REL14_BRTAKEN:
  5675. case R_PPC_REL14_BRNTAKEN:
  5676. t0 = bfd_get_32 (abfd, hit_addr);
  5677. t0 &= ~0xfffc;
  5678. t0 |= val & 0xfffc;
  5679. bfd_put_32 (abfd, t0, hit_addr);
  5680. break;
  5681. }
  5682. }
  5683. while (branch_fixups != NULL)
  5684. {
  5685. struct one_branch_fixup *f = branch_fixups;
  5686. branch_fixups = branch_fixups->next;
  5687. free (f);
  5688. }
  5689. }
  5690. workaround_change = false;
  5691. newsize = trampoff;
  5692. if (htab->params->ppc476_workaround
  5693. && (!bfd_link_relocatable (link_info)
  5694. || isec->output_section->alignment_power >= htab->params->pagesize_p2))
  5695. {
  5696. bfd_vma addr, end_addr;
  5697. unsigned int crossings;
  5698. bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
  5699. addr = isec->output_section->vma + isec->output_offset;
  5700. end_addr = addr + trampoff;
  5701. addr &= -pagesize;
  5702. crossings = ((end_addr & -pagesize) - addr) >> htab->params->pagesize_p2;
  5703. if (crossings != 0)
  5704. {
  5705. /* Keep space aligned, to ensure the patch code itself does
  5706. not cross a page. Don't decrease size calculated on a
  5707. previous pass as otherwise we might never settle on a layout. */
  5708. newsize = 15 - ((end_addr - 1) & 15);
  5709. newsize += crossings * 16;
  5710. if (relax_info->workaround_size < newsize)
  5711. {
  5712. relax_info->workaround_size = newsize;
  5713. workaround_change = true;
  5714. }
  5715. /* Ensure relocate_section is called. */
  5716. isec->flags |= SEC_RELOC;
  5717. }
  5718. newsize = trampoff + relax_info->workaround_size;
  5719. }
  5720. if (htab->params->pic_fixup > 0)
  5721. {
  5722. picfixup_size -= relax_info->picfixup_size;
  5723. if (picfixup_size != 0)
  5724. relax_info->picfixup_size += picfixup_size;
  5725. newsize += relax_info->picfixup_size;
  5726. }
  5727. if (changes != 0 || picfixup_size != 0 || workaround_change)
  5728. isec->size = newsize;
  5729. if (isymbuf != NULL
  5730. && symtab_hdr->contents != (unsigned char *) isymbuf)
  5731. {
  5732. if (! link_info->keep_memory)
  5733. free (isymbuf);
  5734. else
  5735. {
  5736. /* Cache the symbols for elf_link_input_bfd. */
  5737. symtab_hdr->contents = (unsigned char *) isymbuf;
  5738. }
  5739. }
  5740. if (contents != NULL
  5741. && elf_section_data (isec)->this_hdr.contents != contents)
  5742. {
  5743. if (!changes && !link_info->keep_memory)
  5744. free (contents);
  5745. else
  5746. {
  5747. /* Cache the section contents for elf_link_input_bfd. */
  5748. elf_section_data (isec)->this_hdr.contents = contents;
  5749. }
  5750. }
  5751. changes += picfixup_size;
  5752. if (changes != 0)
  5753. {
  5754. /* Append sufficient NOP relocs so we can write out relocation
  5755. information for the trampolines. */
  5756. Elf_Internal_Shdr *rel_hdr;
  5757. Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
  5758. * sizeof (*new_relocs));
  5759. unsigned ix;
  5760. if (!new_relocs)
  5761. goto error_return;
  5762. memcpy (new_relocs, internal_relocs,
  5763. isec->reloc_count * sizeof (*new_relocs));
  5764. for (ix = changes; ix--;)
  5765. {
  5766. irel = new_relocs + ix + isec->reloc_count;
  5767. irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
  5768. }
  5769. if (internal_relocs != elf_section_data (isec)->relocs)
  5770. free (internal_relocs);
  5771. elf_section_data (isec)->relocs = new_relocs;
  5772. isec->reloc_count += changes;
  5773. rel_hdr = _bfd_elf_single_rel_hdr (isec);
  5774. rel_hdr->sh_size += changes * rel_hdr->sh_entsize;
  5775. }
  5776. else if (elf_section_data (isec)->relocs != internal_relocs)
  5777. free (internal_relocs);
  5778. *again = changes != 0 || workaround_change;
  5779. return true;
  5780. error_return:
  5781. while (branch_fixups != NULL)
  5782. {
  5783. struct one_branch_fixup *f = branch_fixups;
  5784. branch_fixups = branch_fixups->next;
  5785. free (f);
  5786. }
  5787. if ((unsigned char *) isymbuf != symtab_hdr->contents)
  5788. free (isymbuf);
  5789. if (elf_section_data (isec)->this_hdr.contents != contents)
  5790. free (contents);
  5791. if (elf_section_data (isec)->relocs != internal_relocs)
  5792. free (internal_relocs);
  5793. return false;
  5794. }
  5795. /* What to do when ld finds relocations against symbols defined in
  5796. discarded sections. */
  5797. static unsigned int
  5798. ppc_elf_action_discarded (asection *sec)
  5799. {
  5800. if (strcmp (".fixup", sec->name) == 0)
  5801. return 0;
  5802. if (strcmp (".got2", sec->name) == 0)
  5803. return 0;
  5804. return _bfd_elf_default_action_discarded (sec);
  5805. }
  5806. /* Fill in the address for a pointer generated in a linker section. */
  5807. static bfd_vma
  5808. elf_finish_pointer_linker_section (bfd *input_bfd,
  5809. elf_linker_section_t *lsect,
  5810. struct elf_link_hash_entry *h,
  5811. bfd_vma relocation,
  5812. const Elf_Internal_Rela *rel)
  5813. {
  5814. elf_linker_section_pointers_t *linker_section_ptr;
  5815. BFD_ASSERT (lsect != NULL);
  5816. if (h != NULL)
  5817. {
  5818. /* Handle global symbol. */
  5819. struct ppc_elf_link_hash_entry *eh;
  5820. eh = (struct ppc_elf_link_hash_entry *) h;
  5821. BFD_ASSERT (eh->elf.def_regular);
  5822. linker_section_ptr = eh->linker_section_pointer;
  5823. }
  5824. else
  5825. {
  5826. /* Handle local symbol. */
  5827. unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
  5828. BFD_ASSERT (is_ppc_elf (input_bfd));
  5829. BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
  5830. linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
  5831. }
  5832. linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
  5833. rel->r_addend,
  5834. lsect);
  5835. BFD_ASSERT (linker_section_ptr != NULL);
  5836. /* Offset will always be a multiple of four, so use the bottom bit
  5837. as a "written" flag. */
  5838. if ((linker_section_ptr->offset & 1) == 0)
  5839. {
  5840. bfd_put_32 (lsect->section->owner,
  5841. relocation + linker_section_ptr->addend,
  5842. lsect->section->contents + linker_section_ptr->offset);
  5843. linker_section_ptr->offset += 1;
  5844. }
  5845. relocation = (lsect->section->output_section->vma
  5846. + lsect->section->output_offset
  5847. + linker_section_ptr->offset - 1
  5848. - SYM_VAL (lsect->sym));
  5849. #ifdef DEBUG
  5850. fprintf (stderr,
  5851. "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
  5852. lsect->name, (long) relocation, (long) relocation);
  5853. #endif
  5854. return relocation;
  5855. }
  5856. #define PPC_LO(v) ((v) & 0xffff)
  5857. #define PPC_HI(v) (((v) >> 16) & 0xffff)
  5858. #define PPC_HA(v) PPC_HI ((v) + 0x8000)
  5859. static void
  5860. write_glink_stub (struct elf_link_hash_entry *h, struct plt_entry *ent,
  5861. asection *plt_sec, unsigned char *p,
  5862. struct bfd_link_info *info)
  5863. {
  5864. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  5865. bfd *output_bfd = info->output_bfd;
  5866. bfd_vma plt;
  5867. unsigned char *end = p + GLINK_ENTRY_SIZE (htab, h);
  5868. if (h != NULL
  5869. && h == htab->tls_get_addr
  5870. && !htab->params->no_tls_get_addr_opt)
  5871. {
  5872. bfd_put_32 (output_bfd, LWZ_11_3, p);
  5873. p += 4;
  5874. bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
  5875. p += 4;
  5876. bfd_put_32 (output_bfd, MR_0_3, p);
  5877. p += 4;
  5878. bfd_put_32 (output_bfd, CMPWI_11_0, p);
  5879. p += 4;
  5880. bfd_put_32 (output_bfd, ADD_3_12_2, p);
  5881. p += 4;
  5882. bfd_put_32 (output_bfd, BEQLR, p);
  5883. p += 4;
  5884. bfd_put_32 (output_bfd, MR_3_0, p);
  5885. p += 4;
  5886. bfd_put_32 (output_bfd, NOP, p);
  5887. p += 4;
  5888. }
  5889. plt = ((ent->plt.offset & ~1)
  5890. + plt_sec->output_section->vma
  5891. + plt_sec->output_offset);
  5892. if (bfd_link_pic (info))
  5893. {
  5894. bfd_vma got = 0;
  5895. if (ent->addend >= 32768)
  5896. got = (ent->addend
  5897. + ent->sec->output_section->vma
  5898. + ent->sec->output_offset);
  5899. else if (htab->elf.hgot != NULL)
  5900. got = SYM_VAL (htab->elf.hgot);
  5901. plt -= got;
  5902. if (plt + 0x8000 < 0x10000)
  5903. bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
  5904. else
  5905. {
  5906. bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
  5907. p += 4;
  5908. bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
  5909. }
  5910. }
  5911. else
  5912. {
  5913. bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
  5914. p += 4;
  5915. bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
  5916. }
  5917. p += 4;
  5918. bfd_put_32 (output_bfd, MTCTR_11, p);
  5919. p += 4;
  5920. bfd_put_32 (output_bfd, BCTR, p);
  5921. p += 4;
  5922. while (p < end)
  5923. {
  5924. bfd_put_32 (output_bfd, htab->params->ppc476_workaround ? BA : NOP, p);
  5925. p += 4;
  5926. }
  5927. }
  5928. /* Return true if symbol is defined statically. */
  5929. static bool
  5930. is_static_defined (struct elf_link_hash_entry *h)
  5931. {
  5932. return ((h->root.type == bfd_link_hash_defined
  5933. || h->root.type == bfd_link_hash_defweak)
  5934. && h->root.u.def.section != NULL
  5935. && h->root.u.def.section->output_section != NULL);
  5936. }
  5937. /* If INSN is an opcode that may be used with an @tls operand, return
  5938. the transformed insn for TLS optimisation, otherwise return 0. If
  5939. REG is non-zero only match an insn with RB or RA equal to REG. */
  5940. unsigned int
  5941. _bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
  5942. {
  5943. unsigned int rtra;
  5944. if ((insn & (0x3fu << 26)) != 31 << 26)
  5945. return 0;
  5946. if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
  5947. rtra = insn & ((1 << 26) - (1 << 16));
  5948. else if (((insn >> 16) & 0x1f) == reg)
  5949. rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
  5950. else
  5951. return 0;
  5952. if ((insn & (0x3ff << 1)) == 266 << 1)
  5953. /* add -> addi. */
  5954. insn = 14 << 26;
  5955. else if ((insn & (0x1f << 1)) == 23 << 1
  5956. && ((insn & (0x1f << 6)) < 14 << 6
  5957. || ((insn & (0x1f << 6)) >= 16 << 6
  5958. && (insn & (0x1f << 6)) < 24 << 6)))
  5959. /* load and store indexed -> dform. */
  5960. insn = (32u | ((insn >> 6) & 0x1f)) << 26;
  5961. else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
  5962. /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
  5963. insn = ((58u | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
  5964. else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
  5965. /* lwax -> lwa. */
  5966. insn = (58u << 26) | 2;
  5967. else
  5968. return 0;
  5969. insn |= rtra;
  5970. return insn;
  5971. }
  5972. /* If INSN is an opcode that may be used with an @tprel operand, return
  5973. the transformed insn for an undefined weak symbol, ie. with the
  5974. thread pointer REG operand removed. Otherwise return 0. */
  5975. unsigned int
  5976. _bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
  5977. {
  5978. if ((insn & (0x1f << 16)) == reg << 16
  5979. && ((insn & (0x3fu << 26)) == 14u << 26 /* addi */
  5980. || (insn & (0x3fu << 26)) == 15u << 26 /* addis */
  5981. || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
  5982. || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
  5983. || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
  5984. || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
  5985. || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
  5986. || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
  5987. || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
  5988. || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
  5989. || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
  5990. || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
  5991. || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
  5992. || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
  5993. || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
  5994. || ((insn & (0x3fu << 26)) == 58u << 26 /* lwa,ld,lmd */
  5995. && (insn & 3) != 1)
  5996. || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
  5997. && ((insn & 3) == 0 || (insn & 3) == 3))))
  5998. {
  5999. insn &= ~(0x1f << 16);
  6000. }
  6001. else if ((insn & (0x1f << 21)) == reg << 21
  6002. && ((insn & (0x3eu << 26)) == 24u << 26 /* ori, oris */
  6003. || (insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */
  6004. || (insn & (0x3eu << 26)) == 28u << 26 /* andi,andis */))
  6005. {
  6006. insn &= ~(0x1f << 21);
  6007. insn |= (insn & (0x1f << 16)) << 5;
  6008. if ((insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */)
  6009. insn -= 2 >> 26; /* convert to ori,oris */
  6010. }
  6011. else
  6012. insn = 0;
  6013. return insn;
  6014. }
  6015. static bool
  6016. is_insn_ds_form (unsigned int insn)
  6017. {
  6018. return ((insn & (0x3fu << 26)) == 58u << 26 /* ld,ldu,lwa */
  6019. || (insn & (0x3fu << 26)) == 62u << 26 /* std,stdu,stq */
  6020. || (insn & (0x3fu << 26)) == 57u << 26 /* lfdp */
  6021. || (insn & (0x3fu << 26)) == 61u << 26 /* stfdp */);
  6022. }
  6023. static bool
  6024. is_insn_dq_form (unsigned int insn)
  6025. {
  6026. return ((insn & (0x3fu << 26)) == 56u << 26 /* lq */
  6027. || ((insn & (0x3fu << 26)) == (61u << 26) /* lxv, stxv */
  6028. && (insn & 3) == 1));
  6029. }
  6030. /* The RELOCATE_SECTION function is called by the ELF backend linker
  6031. to handle the relocations for a section.
  6032. The relocs are always passed as Rela structures; if the section
  6033. actually uses Rel structures, the r_addend field will always be
  6034. zero.
  6035. This function is responsible for adjust the section contents as
  6036. necessary, and (if using Rela relocs and generating a
  6037. relocatable output file) adjusting the reloc addend as
  6038. necessary.
  6039. This function does not have to worry about setting the reloc
  6040. address or the reloc symbol index.
  6041. LOCAL_SYMS is a pointer to the swapped in local symbols.
  6042. LOCAL_SECTIONS is an array giving the section in the input file
  6043. corresponding to the st_shndx field of each local symbol.
  6044. The global hash table entry for the global symbols can be found
  6045. via elf_sym_hashes (input_bfd).
  6046. When generating relocatable output, this function must handle
  6047. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  6048. going to be the section symbol corresponding to the output
  6049. section, which means that the addend must be adjusted
  6050. accordingly. */
  6051. static int
  6052. ppc_elf_relocate_section (bfd *output_bfd,
  6053. struct bfd_link_info *info,
  6054. bfd *input_bfd,
  6055. asection *input_section,
  6056. bfd_byte *contents,
  6057. Elf_Internal_Rela *relocs,
  6058. Elf_Internal_Sym *local_syms,
  6059. asection **local_sections)
  6060. {
  6061. Elf_Internal_Shdr *symtab_hdr;
  6062. struct elf_link_hash_entry **sym_hashes;
  6063. struct ppc_elf_link_hash_table *htab;
  6064. Elf_Internal_Rela *rel;
  6065. Elf_Internal_Rela *wrel;
  6066. Elf_Internal_Rela *relend;
  6067. Elf_Internal_Rela outrel;
  6068. asection *got2;
  6069. bfd_vma *local_got_offsets;
  6070. bool ret = true;
  6071. bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
  6072. bool is_vxworks_tls;
  6073. unsigned int picfixup_size = 0;
  6074. struct ppc_elf_relax_info *relax_info = NULL;
  6075. #ifdef DEBUG
  6076. _bfd_error_handler ("ppc_elf_relocate_section called for %pB section %pA, "
  6077. "%ld relocations%s",
  6078. input_bfd, input_section,
  6079. (long) input_section->reloc_count,
  6080. (bfd_link_relocatable (info)) ? " (relocatable)" : "");
  6081. #endif
  6082. if (!is_ppc_elf (input_bfd))
  6083. {
  6084. bfd_set_error (bfd_error_wrong_format);
  6085. return false;
  6086. }
  6087. got2 = bfd_get_section_by_name (input_bfd, ".got2");
  6088. /* Initialize howto table if not already done. */
  6089. if (!ppc_elf_howto_table[R_PPC_ADDR32])
  6090. ppc_elf_howto_init ();
  6091. htab = ppc_elf_hash_table (info);
  6092. local_got_offsets = elf_local_got_offsets (input_bfd);
  6093. symtab_hdr = &elf_symtab_hdr (input_bfd);
  6094. sym_hashes = elf_sym_hashes (input_bfd);
  6095. /* We have to handle relocations in vxworks .tls_vars sections
  6096. specially, because the dynamic loader is 'weird'. */
  6097. is_vxworks_tls = (htab->elf.target_os == is_vxworks && bfd_link_pic (info)
  6098. && !strcmp (input_section->output_section->name,
  6099. ".tls_vars"));
  6100. if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET)
  6101. relax_info = elf_section_data (input_section)->sec_info;
  6102. rel = wrel = relocs;
  6103. relend = relocs + input_section->reloc_count;
  6104. for (; rel < relend; wrel++, rel++)
  6105. {
  6106. enum elf_ppc_reloc_type r_type;
  6107. bfd_vma addend;
  6108. bfd_reloc_status_type r;
  6109. Elf_Internal_Sym *sym;
  6110. asection *sec;
  6111. struct elf_link_hash_entry *h;
  6112. const char *sym_name;
  6113. reloc_howto_type *howto;
  6114. unsigned long r_symndx;
  6115. bfd_vma relocation;
  6116. bfd_vma branch_bit, from;
  6117. bool unresolved_reloc, save_unresolved_reloc;
  6118. bool warned;
  6119. unsigned int tls_type, tls_mask, tls_gd;
  6120. struct plt_entry **ifunc, **plt_list;
  6121. struct reloc_howto_struct alt_howto;
  6122. again:
  6123. r_type = ELF32_R_TYPE (rel->r_info);
  6124. sym = NULL;
  6125. sec = NULL;
  6126. h = NULL;
  6127. unresolved_reloc = false;
  6128. warned = false;
  6129. r_symndx = ELF32_R_SYM (rel->r_info);
  6130. if (r_symndx < symtab_hdr->sh_info)
  6131. {
  6132. sym = local_syms + r_symndx;
  6133. sec = local_sections[r_symndx];
  6134. sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
  6135. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  6136. }
  6137. else
  6138. {
  6139. bool ignored;
  6140. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  6141. r_symndx, symtab_hdr, sym_hashes,
  6142. h, sec, relocation,
  6143. unresolved_reloc, warned, ignored);
  6144. sym_name = h->root.root.string;
  6145. }
  6146. if (sec != NULL && discarded_section (sec))
  6147. {
  6148. /* For relocs against symbols from removed linkonce sections,
  6149. or sections discarded by a linker script, we just want the
  6150. section contents zeroed. Avoid any special processing. */
  6151. howto = NULL;
  6152. if (r_type < R_PPC_max)
  6153. howto = ppc_elf_howto_table[r_type];
  6154. _bfd_clear_contents (howto, input_bfd, input_section,
  6155. contents, rel->r_offset);
  6156. wrel->r_offset = rel->r_offset;
  6157. wrel->r_info = 0;
  6158. wrel->r_addend = 0;
  6159. /* For ld -r, remove relocations in debug sections against
  6160. symbols defined in discarded sections. Not done for
  6161. non-debug to preserve relocs in .eh_frame which the
  6162. eh_frame editing code expects to be present. */
  6163. if (bfd_link_relocatable (info)
  6164. && (input_section->flags & SEC_DEBUGGING))
  6165. wrel--;
  6166. continue;
  6167. }
  6168. if (bfd_link_relocatable (info))
  6169. {
  6170. if (got2 != NULL
  6171. && r_type == R_PPC_PLTREL24
  6172. && rel->r_addend != 0)
  6173. {
  6174. /* R_PPC_PLTREL24 is rather special. If non-zero, the
  6175. addend specifies the GOT pointer offset within .got2. */
  6176. rel->r_addend += got2->output_offset;
  6177. }
  6178. if (r_type != R_PPC_RELAX_PLT
  6179. && r_type != R_PPC_RELAX_PLTREL24
  6180. && r_type != R_PPC_RELAX)
  6181. goto copy_reloc;
  6182. }
  6183. /* TLS optimizations. Replace instruction sequences and relocs
  6184. based on information we collected in tls_optimize. We edit
  6185. RELOCS so that --emit-relocs will output something sensible
  6186. for the final instruction stream. */
  6187. tls_mask = 0;
  6188. tls_gd = 0;
  6189. if (h != NULL)
  6190. tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
  6191. else if (local_got_offsets != NULL)
  6192. {
  6193. struct plt_entry **local_plt;
  6194. char *lgot_masks;
  6195. local_plt
  6196. = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
  6197. lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
  6198. tls_mask = lgot_masks[r_symndx];
  6199. }
  6200. /* Ensure reloc mapping code below stays sane. */
  6201. if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
  6202. || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
  6203. || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
  6204. || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
  6205. || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
  6206. || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
  6207. || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
  6208. || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
  6209. abort ();
  6210. switch (r_type)
  6211. {
  6212. default:
  6213. break;
  6214. case R_PPC_GOT_TPREL16:
  6215. case R_PPC_GOT_TPREL16_LO:
  6216. if ((tls_mask & TLS_TLS) != 0
  6217. && (tls_mask & TLS_TPREL) == 0
  6218. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  6219. {
  6220. bfd_vma insn;
  6221. insn = bfd_get_32 (input_bfd,
  6222. contents + rel->r_offset - d_offset);
  6223. insn &= 31 << 21;
  6224. insn |= 0x3c020000; /* addis 0,2,0 */
  6225. bfd_put_32 (input_bfd, insn,
  6226. contents + rel->r_offset - d_offset);
  6227. r_type = R_PPC_TPREL16_HA;
  6228. rel->r_info = ELF32_R_INFO (r_symndx, r_type);
  6229. }
  6230. break;
  6231. case R_PPC_TLS:
  6232. if ((tls_mask & TLS_TLS) != 0
  6233. && (tls_mask & TLS_TPREL) == 0
  6234. && offset_in_range (input_section, rel->r_offset, 4))
  6235. {
  6236. bfd_vma insn;
  6237. insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  6238. insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
  6239. if (insn == 0)
  6240. abort ();
  6241. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  6242. r_type = R_PPC_TPREL16_LO;
  6243. rel->r_info = ELF32_R_INFO (r_symndx, r_type);
  6244. /* Was PPC_TLS which sits on insn boundary, now
  6245. PPC_TPREL16_LO which is at low-order half-word. */
  6246. rel->r_offset += d_offset;
  6247. }
  6248. break;
  6249. case R_PPC_GOT_TLSGD16_HI:
  6250. case R_PPC_GOT_TLSGD16_HA:
  6251. tls_gd = TLS_GDIE;
  6252. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
  6253. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  6254. goto tls_gdld_hi;
  6255. break;
  6256. case R_PPC_GOT_TLSLD16_HI:
  6257. case R_PPC_GOT_TLSLD16_HA:
  6258. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
  6259. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  6260. {
  6261. tls_gdld_hi:
  6262. if ((tls_mask & tls_gd) != 0)
  6263. r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
  6264. + R_PPC_GOT_TPREL16);
  6265. else
  6266. {
  6267. rel->r_offset -= d_offset;
  6268. bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
  6269. r_type = R_PPC_NONE;
  6270. }
  6271. rel->r_info = ELF32_R_INFO (r_symndx, r_type);
  6272. }
  6273. break;
  6274. case R_PPC_GOT_TLSGD16:
  6275. case R_PPC_GOT_TLSGD16_LO:
  6276. tls_gd = TLS_GDIE;
  6277. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
  6278. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  6279. goto tls_ldgd_opt;
  6280. break;
  6281. case R_PPC_GOT_TLSLD16:
  6282. case R_PPC_GOT_TLSLD16_LO:
  6283. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
  6284. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  6285. {
  6286. unsigned int insn1, insn2;
  6287. bfd_vma offset;
  6288. tls_ldgd_opt:
  6289. offset = (bfd_vma) -1;
  6290. /* If not using the newer R_PPC_TLSGD/LD to mark
  6291. __tls_get_addr calls, we must trust that the call
  6292. stays with its arg setup insns, ie. that the next
  6293. reloc is the __tls_get_addr call associated with
  6294. the current reloc. Edit both insns. */
  6295. if (input_section->nomark_tls_get_addr
  6296. && rel + 1 < relend
  6297. && branch_reloc_hash_match (input_bfd, rel + 1,
  6298. htab->tls_get_addr))
  6299. offset = rel[1].r_offset;
  6300. /* We read the low GOT_TLS insn because we need to keep
  6301. the destination reg. It may be something other than
  6302. the usual r3, and moved to r3 before the call by
  6303. intervening code. */
  6304. insn1 = bfd_get_32 (input_bfd,
  6305. contents + rel->r_offset - d_offset);
  6306. if ((tls_mask & tls_gd) != 0)
  6307. {
  6308. /* IE */
  6309. insn1 &= (0x1f << 21) | (0x1f << 16);
  6310. insn1 |= 32u << 26; /* lwz */
  6311. if (offset != (bfd_vma) -1
  6312. && offset_in_range (input_section, offset, 4))
  6313. {
  6314. rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
  6315. insn2 = 0x7c631214; /* add 3,3,2 */
  6316. bfd_put_32 (input_bfd, insn2, contents + offset);
  6317. }
  6318. r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
  6319. + R_PPC_GOT_TPREL16);
  6320. rel->r_info = ELF32_R_INFO (r_symndx, r_type);
  6321. }
  6322. else
  6323. {
  6324. /* LE */
  6325. insn1 &= 0x1f << 21;
  6326. insn1 |= 0x3c020000; /* addis r,2,0 */
  6327. if (tls_gd == 0)
  6328. {
  6329. /* Was an LD reloc. */
  6330. for (r_symndx = 0;
  6331. r_symndx < symtab_hdr->sh_info;
  6332. r_symndx++)
  6333. if (local_sections[r_symndx] == sec)
  6334. break;
  6335. if (r_symndx >= symtab_hdr->sh_info)
  6336. r_symndx = STN_UNDEF;
  6337. rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
  6338. if (r_symndx != STN_UNDEF)
  6339. rel->r_addend -= (local_syms[r_symndx].st_value
  6340. + sec->output_offset
  6341. + sec->output_section->vma);
  6342. }
  6343. r_type = R_PPC_TPREL16_HA;
  6344. rel->r_info = ELF32_R_INFO (r_symndx, r_type);
  6345. if (offset != (bfd_vma) -1
  6346. && offset_in_range (input_section, offset, 4))
  6347. {
  6348. rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
  6349. rel[1].r_offset = offset + d_offset;
  6350. rel[1].r_addend = rel->r_addend;
  6351. insn2 = 0x38630000; /* addi 3,3,0 */
  6352. bfd_put_32 (input_bfd, insn2, contents + offset);
  6353. }
  6354. }
  6355. bfd_put_32 (input_bfd, insn1,
  6356. contents + rel->r_offset - d_offset);
  6357. if (tls_gd == 0)
  6358. {
  6359. /* We changed the symbol on an LD reloc. Start over
  6360. in order to get h, sym, sec etc. right. */
  6361. goto again;
  6362. }
  6363. }
  6364. break;
  6365. case R_PPC_TLSGD:
  6366. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
  6367. && rel + 1 < relend
  6368. && offset_in_range (input_section, rel->r_offset, 4))
  6369. {
  6370. unsigned int insn2;
  6371. bfd_vma offset = rel->r_offset;
  6372. if (is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
  6373. {
  6374. bfd_put_32 (input_bfd, NOP, contents + offset);
  6375. rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
  6376. break;
  6377. }
  6378. if ((tls_mask & TLS_GDIE) != 0)
  6379. {
  6380. /* IE */
  6381. r_type = R_PPC_NONE;
  6382. insn2 = 0x7c631214; /* add 3,3,2 */
  6383. }
  6384. else
  6385. {
  6386. /* LE */
  6387. r_type = R_PPC_TPREL16_LO;
  6388. rel->r_offset += d_offset;
  6389. insn2 = 0x38630000; /* addi 3,3,0 */
  6390. }
  6391. rel->r_info = ELF32_R_INFO (r_symndx, r_type);
  6392. bfd_put_32 (input_bfd, insn2, contents + offset);
  6393. /* Zap the reloc on the _tls_get_addr call too. */
  6394. BFD_ASSERT (offset == rel[1].r_offset);
  6395. rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
  6396. }
  6397. break;
  6398. case R_PPC_TLSLD:
  6399. if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
  6400. && rel + 1 < relend
  6401. && offset_in_range (input_section, rel->r_offset, 4))
  6402. {
  6403. unsigned int insn2;
  6404. if (is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
  6405. {
  6406. bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
  6407. rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
  6408. break;
  6409. }
  6410. for (r_symndx = 0;
  6411. r_symndx < symtab_hdr->sh_info;
  6412. r_symndx++)
  6413. if (local_sections[r_symndx] == sec)
  6414. break;
  6415. if (r_symndx >= symtab_hdr->sh_info)
  6416. r_symndx = STN_UNDEF;
  6417. rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
  6418. if (r_symndx != STN_UNDEF)
  6419. rel->r_addend -= (local_syms[r_symndx].st_value
  6420. + sec->output_offset
  6421. + sec->output_section->vma);
  6422. rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
  6423. rel->r_offset += d_offset;
  6424. insn2 = 0x38630000; /* addi 3,3,0 */
  6425. bfd_put_32 (input_bfd, insn2,
  6426. contents + rel->r_offset - d_offset);
  6427. /* Zap the reloc on the _tls_get_addr call too. */
  6428. BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
  6429. rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
  6430. goto again;
  6431. }
  6432. break;
  6433. }
  6434. /* Handle other relocations that tweak non-addend part of insn. */
  6435. branch_bit = 0;
  6436. switch (r_type)
  6437. {
  6438. default:
  6439. break;
  6440. /* Branch taken prediction relocations. */
  6441. case R_PPC_ADDR14_BRTAKEN:
  6442. case R_PPC_REL14_BRTAKEN:
  6443. branch_bit = BRANCH_PREDICT_BIT;
  6444. /* Fall through. */
  6445. /* Branch not taken prediction relocations. */
  6446. case R_PPC_ADDR14_BRNTAKEN:
  6447. case R_PPC_REL14_BRNTAKEN:
  6448. if (offset_in_range (input_section, rel->r_offset, 4))
  6449. {
  6450. unsigned int insn;
  6451. insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  6452. insn &= ~BRANCH_PREDICT_BIT;
  6453. insn |= branch_bit;
  6454. from = (rel->r_offset
  6455. + input_section->output_offset
  6456. + input_section->output_section->vma);
  6457. /* Invert 'y' bit if not the default. */
  6458. if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
  6459. insn ^= BRANCH_PREDICT_BIT;
  6460. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  6461. }
  6462. break;
  6463. case R_PPC_PLT16_HA:
  6464. if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
  6465. {
  6466. unsigned int insn;
  6467. insn = bfd_get_32 (input_bfd,
  6468. contents + rel->r_offset - d_offset);
  6469. if ((insn & (0x3fu << 26)) == 15u << 26
  6470. && (insn & (0x1f << 16)) != 0)
  6471. {
  6472. if (!bfd_link_pic (info))
  6473. {
  6474. /* Convert addis to lis. */
  6475. insn &= ~(0x1f << 16);
  6476. bfd_put_32 (input_bfd, insn,
  6477. contents + rel->r_offset - d_offset);
  6478. }
  6479. }
  6480. else if (bfd_link_pic (info))
  6481. info->callbacks->einfo
  6482. (_("%P: %H: error: %s with unexpected instruction %x\n"),
  6483. input_bfd, input_section, rel->r_offset,
  6484. "R_PPC_PLT16_HA", insn);
  6485. }
  6486. break;
  6487. }
  6488. if (ELIMINATE_COPY_RELOCS
  6489. && h != NULL
  6490. && !h->def_regular
  6491. && h->protected_def
  6492. && ppc_elf_hash_entry (h)->has_addr16_ha
  6493. && ppc_elf_hash_entry (h)->has_addr16_lo
  6494. && htab->params->pic_fixup > 0)
  6495. {
  6496. /* Convert lis;addi or lis;load/store accessing a protected
  6497. variable defined in a shared library to PIC. */
  6498. unsigned int insn;
  6499. if (r_type == R_PPC_ADDR16_HA
  6500. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  6501. {
  6502. insn = bfd_get_32 (input_bfd,
  6503. contents + rel->r_offset - d_offset);
  6504. if ((insn & (0x3fu << 26)) == (15u << 26)
  6505. && (insn & (0x1f << 16)) == 0 /* lis */)
  6506. {
  6507. bfd_byte *p;
  6508. bfd_vma off;
  6509. bfd_vma got_addr;
  6510. p = (contents + input_section->size
  6511. - relax_info->workaround_size
  6512. - relax_info->picfixup_size
  6513. + picfixup_size);
  6514. off = (p - contents) - (rel->r_offset - d_offset);
  6515. if (off > 0x1fffffc || (off & 3) != 0)
  6516. info->callbacks->einfo
  6517. (_("%H: fixup branch overflow\n"),
  6518. input_bfd, input_section, rel->r_offset);
  6519. bfd_put_32 (input_bfd, B | off,
  6520. contents + rel->r_offset - d_offset);
  6521. got_addr = (htab->elf.sgot->output_section->vma
  6522. + htab->elf.sgot->output_offset
  6523. + (h->got.offset & ~1));
  6524. wrel->r_offset = (p - contents) + d_offset;
  6525. wrel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_HA);
  6526. wrel->r_addend = got_addr;
  6527. insn &= ~0xffff;
  6528. insn |= ((unsigned int) (got_addr + 0x8000) >> 16) & 0xffff;
  6529. bfd_put_32 (input_bfd, insn, p);
  6530. /* Convert lis to lwz, loading address from GOT. */
  6531. insn &= ~0xffff;
  6532. insn ^= (32u ^ 15u) << 26;
  6533. insn |= (insn & (0x1f << 21)) >> 5;
  6534. insn |= got_addr & 0xffff;
  6535. bfd_put_32 (input_bfd, insn, p + 4);
  6536. bfd_put_32 (input_bfd, B | ((-4 - off) & 0x3ffffff), p + 8);
  6537. picfixup_size += 12;
  6538. /* Use one of the spare relocs, so --emit-relocs
  6539. output is reasonable. */
  6540. memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
  6541. wrel++, rel++;
  6542. rel->r_offset = wrel[-1].r_offset + 4;
  6543. rel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_LO);
  6544. rel->r_addend = wrel[-1].r_addend;
  6545. /* Continue on as if we had a got reloc, to output
  6546. dynamic reloc. */
  6547. r_type = R_PPC_GOT16_LO;
  6548. }
  6549. else
  6550. _bfd_error_handler
  6551. /* xgettext:c-format */
  6552. (_("%pB(%pA+%#" PRIx64 "): error: "
  6553. "%s with unexpected instruction %#x"),
  6554. input_bfd, input_section, (uint64_t) rel->r_offset,
  6555. "R_PPC_ADDR16_HA", insn);
  6556. }
  6557. else if (r_type == R_PPC_ADDR16_LO
  6558. && offset_in_range (input_section,
  6559. rel->r_offset - d_offset, 4))
  6560. {
  6561. insn = bfd_get_32 (input_bfd,
  6562. contents + rel->r_offset - d_offset);
  6563. if ((insn & (0x3fu << 26)) == 14u << 26 /* addi */
  6564. || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
  6565. || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
  6566. || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
  6567. || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
  6568. || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
  6569. || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
  6570. || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
  6571. || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
  6572. || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
  6573. || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
  6574. || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
  6575. || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
  6576. || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
  6577. || ((insn & (0x3fu << 26)) == 58u << 26 /* lwa,ld,lmd */
  6578. && (insn & 3) != 1)
  6579. || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
  6580. && ((insn & 3) == 0 || (insn & 3) == 3)))
  6581. {
  6582. /* Arrange to apply the reloc addend, if any. */
  6583. relocation = 0;
  6584. unresolved_reloc = false;
  6585. rel->r_info = ELF32_R_INFO (0, r_type);
  6586. }
  6587. else
  6588. _bfd_error_handler
  6589. /* xgettext:c-format */
  6590. (_("%pB(%pA+%#" PRIx64 "): error: "
  6591. "%s with unexpected instruction %#x"),
  6592. input_bfd, input_section, (uint64_t) rel->r_offset,
  6593. "R_PPC_ADDR16_LO", insn);
  6594. }
  6595. }
  6596. ifunc = NULL;
  6597. if (htab->elf.target_os != is_vxworks)
  6598. {
  6599. struct plt_entry *ent;
  6600. if (h != NULL)
  6601. {
  6602. if (h->type == STT_GNU_IFUNC)
  6603. ifunc = &h->plt.plist;
  6604. }
  6605. else if (local_got_offsets != NULL
  6606. && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  6607. {
  6608. struct plt_entry **local_plt;
  6609. local_plt = (struct plt_entry **) (local_got_offsets
  6610. + symtab_hdr->sh_info);
  6611. ifunc = local_plt + r_symndx;
  6612. }
  6613. ent = NULL;
  6614. if (ifunc != NULL
  6615. && (!bfd_link_pic (info)
  6616. || is_branch_reloc (r_type)
  6617. || r_type == R_PPC_PLT16_LO
  6618. || r_type == R_PPC_PLT16_HI
  6619. || r_type == R_PPC_PLT16_HA))
  6620. {
  6621. addend = 0;
  6622. if (bfd_link_pic (info)
  6623. && (r_type == R_PPC_PLTREL24
  6624. || r_type == R_PPC_PLT16_LO
  6625. || r_type == R_PPC_PLT16_HI
  6626. || r_type == R_PPC_PLT16_HA))
  6627. addend = rel->r_addend;
  6628. ent = find_plt_ent (ifunc, got2, addend);
  6629. }
  6630. if (ent != NULL)
  6631. {
  6632. if (bfd_link_pic (info)
  6633. && ent->sec != got2
  6634. && htab->plt_type != PLT_NEW
  6635. && (!htab->elf.dynamic_sections_created
  6636. || h == NULL
  6637. || h->dynindx == -1))
  6638. {
  6639. /* Uh oh, we are going to create a pic glink stub
  6640. for an ifunc (here for h == NULL and later in
  6641. finish_dynamic_symbol for h != NULL), and
  6642. apparently are using code compiled with
  6643. -mbss-plt. The difficulty is that -mbss-plt code
  6644. gives no indication via a magic PLTREL24 addend
  6645. whether r30 is equal to _GLOBAL_OFFSET_TABLE_ or
  6646. is pointing into a .got2 section (and how far
  6647. into .got2). */
  6648. info->callbacks->einfo
  6649. /* xgettext:c-format */
  6650. (_("%X%H: unsupported bss-plt -fPIC ifunc %s\n"),
  6651. input_bfd, input_section, rel->r_offset, sym_name);
  6652. }
  6653. unresolved_reloc = false;
  6654. if (htab->plt_type == PLT_NEW
  6655. || !htab->elf.dynamic_sections_created
  6656. || h == NULL
  6657. || h->dynindx == -1)
  6658. relocation = (htab->glink->output_section->vma
  6659. + htab->glink->output_offset
  6660. + (ent->glink_offset & ~1));
  6661. else
  6662. relocation = (htab->elf.splt->output_section->vma
  6663. + htab->elf.splt->output_offset
  6664. + ent->plt.offset);
  6665. }
  6666. }
  6667. addend = rel->r_addend;
  6668. save_unresolved_reloc = unresolved_reloc;
  6669. howto = NULL;
  6670. if (r_type < R_PPC_max)
  6671. howto = ppc_elf_howto_table[r_type];
  6672. tls_type = 0;
  6673. switch (r_type)
  6674. {
  6675. default:
  6676. de_fault:
  6677. if (howto)
  6678. /* xgettext:c-format */
  6679. _bfd_error_handler (_("%pB: %s unsupported"),
  6680. input_bfd, howto->name);
  6681. else
  6682. /* xgettext:c-format */
  6683. _bfd_error_handler (_("%pB: reloc %#x unsupported"),
  6684. input_bfd, r_type);
  6685. bfd_set_error (bfd_error_bad_value);
  6686. ret = false;
  6687. goto copy_reloc;
  6688. case R_PPC_NONE:
  6689. case R_PPC_TLS:
  6690. case R_PPC_TLSGD:
  6691. case R_PPC_TLSLD:
  6692. case R_PPC_EMB_MRKREF:
  6693. case R_PPC_GNU_VTINHERIT:
  6694. case R_PPC_GNU_VTENTRY:
  6695. goto copy_reloc;
  6696. /* GOT16 relocations. Like an ADDR16 using the symbol's
  6697. address in the GOT as relocation value instead of the
  6698. symbol's value itself. Also, create a GOT entry for the
  6699. symbol and put the symbol value there. */
  6700. case R_PPC_GOT_TLSGD16:
  6701. case R_PPC_GOT_TLSGD16_LO:
  6702. case R_PPC_GOT_TLSGD16_HI:
  6703. case R_PPC_GOT_TLSGD16_HA:
  6704. tls_type = TLS_TLS | TLS_GD;
  6705. goto dogot;
  6706. case R_PPC_GOT_TLSLD16:
  6707. case R_PPC_GOT_TLSLD16_LO:
  6708. case R_PPC_GOT_TLSLD16_HI:
  6709. case R_PPC_GOT_TLSLD16_HA:
  6710. tls_type = TLS_TLS | TLS_LD;
  6711. goto dogot;
  6712. case R_PPC_GOT_TPREL16:
  6713. case R_PPC_GOT_TPREL16_LO:
  6714. case R_PPC_GOT_TPREL16_HI:
  6715. case R_PPC_GOT_TPREL16_HA:
  6716. tls_type = TLS_TLS | TLS_TPREL;
  6717. goto dogot;
  6718. case R_PPC_GOT_DTPREL16:
  6719. case R_PPC_GOT_DTPREL16_LO:
  6720. case R_PPC_GOT_DTPREL16_HI:
  6721. case R_PPC_GOT_DTPREL16_HA:
  6722. tls_type = TLS_TLS | TLS_DTPREL;
  6723. goto dogot;
  6724. case R_PPC_GOT16:
  6725. case R_PPC_GOT16_LO:
  6726. case R_PPC_GOT16_HI:
  6727. case R_PPC_GOT16_HA:
  6728. tls_mask = 0;
  6729. dogot:
  6730. {
  6731. /* Relocation is to the entry for this symbol in the global
  6732. offset table. */
  6733. bfd_vma off;
  6734. bfd_vma *offp;
  6735. unsigned long indx;
  6736. if (htab->elf.sgot == NULL)
  6737. abort ();
  6738. indx = 0;
  6739. if (tls_type == (TLS_TLS | TLS_LD)
  6740. && SYMBOL_REFERENCES_LOCAL (info, h))
  6741. offp = &htab->tlsld_got.offset;
  6742. else if (h != NULL)
  6743. {
  6744. if (!htab->elf.dynamic_sections_created
  6745. || h->dynindx == -1
  6746. || SYMBOL_REFERENCES_LOCAL (info, h)
  6747. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  6748. /* This is actually a static link, or it is a
  6749. -Bsymbolic link and the symbol is defined
  6750. locally, or the symbol was forced to be local
  6751. because of a version file. */
  6752. ;
  6753. else
  6754. {
  6755. indx = h->dynindx;
  6756. unresolved_reloc = false;
  6757. }
  6758. offp = &h->got.offset;
  6759. }
  6760. else
  6761. {
  6762. if (local_got_offsets == NULL)
  6763. abort ();
  6764. offp = &local_got_offsets[r_symndx];
  6765. }
  6766. /* The offset must always be a multiple of 4. We use the
  6767. least significant bit to record whether we have already
  6768. processed this entry. */
  6769. off = *offp;
  6770. if ((off & 1) != 0)
  6771. off &= ~1;
  6772. else
  6773. {
  6774. unsigned int tls_m = ((tls_mask & TLS_TLS) != 0
  6775. ? tls_mask & (TLS_LD | TLS_GD | TLS_DTPREL
  6776. | TLS_TPREL | TLS_GDIE)
  6777. : 0);
  6778. if (offp == &htab->tlsld_got.offset)
  6779. tls_m = TLS_LD;
  6780. else if ((tls_m & TLS_LD) != 0
  6781. && SYMBOL_REFERENCES_LOCAL (info, h))
  6782. tls_m &= ~TLS_LD;
  6783. /* We might have multiple got entries for this sym.
  6784. Initialize them all. */
  6785. do
  6786. {
  6787. int tls_ty = 0;
  6788. if ((tls_m & TLS_LD) != 0)
  6789. {
  6790. tls_ty = TLS_TLS | TLS_LD;
  6791. tls_m &= ~TLS_LD;
  6792. }
  6793. else if ((tls_m & TLS_GD) != 0)
  6794. {
  6795. tls_ty = TLS_TLS | TLS_GD;
  6796. tls_m &= ~TLS_GD;
  6797. }
  6798. else if ((tls_m & TLS_DTPREL) != 0)
  6799. {
  6800. tls_ty = TLS_TLS | TLS_DTPREL;
  6801. tls_m &= ~TLS_DTPREL;
  6802. }
  6803. else if ((tls_m & (TLS_TPREL | TLS_GDIE)) != 0)
  6804. {
  6805. tls_ty = TLS_TLS | TLS_TPREL;
  6806. tls_m = 0;
  6807. }
  6808. /* Generate relocs for the dynamic linker. */
  6809. if (indx != 0
  6810. || (bfd_link_pic (info)
  6811. && (h == NULL
  6812. || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  6813. && !(tls_ty != 0
  6814. && bfd_link_executable (info)
  6815. && SYMBOL_REFERENCES_LOCAL (info, h))))
  6816. {
  6817. asection *rsec = htab->elf.srelgot;
  6818. bfd_byte * loc;
  6819. if (ifunc != NULL)
  6820. {
  6821. rsec = htab->elf.irelplt;
  6822. if (indx == 0)
  6823. htab->local_ifunc_resolver = 1;
  6824. else if (is_static_defined (h))
  6825. htab->maybe_local_ifunc_resolver = 1;
  6826. }
  6827. outrel.r_offset = (htab->elf.sgot->output_section->vma
  6828. + htab->elf.sgot->output_offset
  6829. + off);
  6830. outrel.r_addend = 0;
  6831. if (tls_ty & (TLS_LD | TLS_GD))
  6832. {
  6833. outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
  6834. if (tls_ty == (TLS_TLS | TLS_GD))
  6835. {
  6836. loc = rsec->contents;
  6837. loc += (rsec->reloc_count++
  6838. * sizeof (Elf32_External_Rela));
  6839. bfd_elf32_swap_reloca_out (output_bfd,
  6840. &outrel, loc);
  6841. outrel.r_offset += 4;
  6842. outrel.r_info
  6843. = ELF32_R_INFO (indx, R_PPC_DTPREL32);
  6844. }
  6845. }
  6846. else if (tls_ty == (TLS_TLS | TLS_DTPREL))
  6847. outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
  6848. else if (tls_ty == (TLS_TLS | TLS_TPREL))
  6849. outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
  6850. else if (indx != 0)
  6851. outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
  6852. else if (ifunc != NULL)
  6853. outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
  6854. else
  6855. outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
  6856. if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
  6857. {
  6858. outrel.r_addend += relocation;
  6859. if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
  6860. {
  6861. if (htab->elf.tls_sec == NULL)
  6862. outrel.r_addend = 0;
  6863. else
  6864. outrel.r_addend -= htab->elf.tls_sec->vma;
  6865. }
  6866. }
  6867. loc = rsec->contents;
  6868. loc += (rsec->reloc_count++
  6869. * sizeof (Elf32_External_Rela));
  6870. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  6871. }
  6872. /* Init the .got section contents if we're not
  6873. emitting a reloc. */
  6874. else
  6875. {
  6876. bfd_vma value = relocation;
  6877. if (tls_ty != 0)
  6878. {
  6879. if (htab->elf.tls_sec == NULL)
  6880. value = 0;
  6881. else
  6882. {
  6883. if (tls_ty & TLS_LD)
  6884. value = 0;
  6885. else
  6886. value -= htab->elf.tls_sec->vma + DTP_OFFSET;
  6887. if (tls_ty & TLS_TPREL)
  6888. value += DTP_OFFSET - TP_OFFSET;
  6889. }
  6890. if (tls_ty & (TLS_LD | TLS_GD))
  6891. {
  6892. bfd_put_32 (input_bfd, value,
  6893. htab->elf.sgot->contents + off + 4);
  6894. value = 1;
  6895. }
  6896. }
  6897. bfd_put_32 (input_bfd, value,
  6898. htab->elf.sgot->contents + off);
  6899. }
  6900. off += 4;
  6901. if (tls_ty & (TLS_LD | TLS_GD))
  6902. off += 4;
  6903. }
  6904. while (tls_m != 0);
  6905. off = *offp;
  6906. *offp = off | 1;
  6907. }
  6908. if (off >= (bfd_vma) -2)
  6909. abort ();
  6910. if ((tls_type & TLS_TLS) != 0)
  6911. {
  6912. if (tls_type != (TLS_TLS | TLS_LD))
  6913. {
  6914. if ((tls_mask & TLS_LD) != 0
  6915. && !SYMBOL_REFERENCES_LOCAL (info, h))
  6916. off += 8;
  6917. if (tls_type != (TLS_TLS | TLS_GD))
  6918. {
  6919. if ((tls_mask & TLS_GD) != 0)
  6920. off += 8;
  6921. if (tls_type != (TLS_TLS | TLS_DTPREL))
  6922. {
  6923. if ((tls_mask & TLS_DTPREL) != 0)
  6924. off += 4;
  6925. }
  6926. }
  6927. }
  6928. }
  6929. /* If here for a picfixup, we're done. */
  6930. if (r_type != ELF32_R_TYPE (rel->r_info))
  6931. goto copy_reloc;
  6932. relocation = (htab->elf.sgot->output_section->vma
  6933. + htab->elf.sgot->output_offset
  6934. + off
  6935. - SYM_VAL (htab->elf.hgot));
  6936. /* Addends on got relocations don't make much sense.
  6937. x+off@got is actually x@got+off, and since the got is
  6938. generated by a hash table traversal, the value in the
  6939. got at entry m+n bears little relation to the entry m. */
  6940. if (addend != 0)
  6941. info->callbacks->einfo
  6942. /* xgettext:c-format */
  6943. (_("%H: non-zero addend on %s reloc against `%s'\n"),
  6944. input_bfd, input_section, rel->r_offset,
  6945. howto->name,
  6946. sym_name);
  6947. }
  6948. break;
  6949. /* Relocations that need no special processing. */
  6950. case R_PPC_LOCAL24PC:
  6951. /* It makes no sense to point a local relocation
  6952. at a symbol not in this object. */
  6953. if (unresolved_reloc)
  6954. {
  6955. (*info->callbacks->undefined_symbol) (info,
  6956. h->root.root.string,
  6957. input_bfd,
  6958. input_section,
  6959. rel->r_offset,
  6960. true);
  6961. goto copy_reloc;
  6962. }
  6963. if (h != NULL && h->type == STT_GNU_IFUNC && bfd_link_pic (info))
  6964. {
  6965. /* @local on an ifunc does not really make sense since
  6966. the ifunc resolver can take you anywhere. More
  6967. seriously, calls to ifuncs must go through a plt call
  6968. stub, and for pic the plt call stubs uses r30 to
  6969. access the PLT. The problem is that a call that is
  6970. local won't have the +32k reloc addend trick marking
  6971. -fPIC code, so the linker won't know whether r30 is
  6972. _GLOBAL_OFFSET_TABLE_ or pointing into a .got2 section. */
  6973. /* xgettext:c-format */
  6974. info->callbacks->einfo (_("%X%H: @local call to ifunc %s\n"),
  6975. input_bfd, input_section, rel->r_offset,
  6976. h->root.root.string);
  6977. }
  6978. break;
  6979. case R_PPC_DTPREL16:
  6980. case R_PPC_DTPREL16_LO:
  6981. case R_PPC_DTPREL16_HI:
  6982. case R_PPC_DTPREL16_HA:
  6983. if (htab->elf.tls_sec != NULL)
  6984. addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
  6985. break;
  6986. /* Relocations that may need to be propagated if this is a shared
  6987. object. */
  6988. case R_PPC_TPREL16:
  6989. case R_PPC_TPREL16_LO:
  6990. case R_PPC_TPREL16_HI:
  6991. case R_PPC_TPREL16_HA:
  6992. if (h != NULL
  6993. && h->root.type == bfd_link_hash_undefweak
  6994. && h->dynindx == -1
  6995. && offset_in_range (input_section, rel->r_offset - d_offset, 4))
  6996. {
  6997. /* Make this relocation against an undefined weak symbol
  6998. resolve to zero. This is really just a tweak, since
  6999. code using weak externs ought to check that they are
  7000. defined before using them. */
  7001. bfd_byte *p = contents + rel->r_offset - d_offset;
  7002. unsigned int insn = bfd_get_32 (input_bfd, p);
  7003. insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
  7004. if (insn != 0)
  7005. bfd_put_32 (input_bfd, insn, p);
  7006. break;
  7007. }
  7008. if (htab->elf.tls_sec != NULL)
  7009. addend -= htab->elf.tls_sec->vma + TP_OFFSET;
  7010. /* The TPREL16 relocs shouldn't really be used in shared
  7011. libs or with non-local symbols as that will result in
  7012. DT_TEXTREL being set, but support them anyway. */
  7013. goto dodyn;
  7014. case R_PPC_TPREL32:
  7015. if (htab->elf.tls_sec != NULL)
  7016. addend -= htab->elf.tls_sec->vma + TP_OFFSET;
  7017. goto dodyn;
  7018. case R_PPC_DTPREL32:
  7019. if (htab->elf.tls_sec != NULL)
  7020. addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
  7021. goto dodyn;
  7022. case R_PPC_DTPMOD32:
  7023. relocation = 1;
  7024. addend = 0;
  7025. goto dodyn;
  7026. case R_PPC_REL16:
  7027. case R_PPC_REL16_LO:
  7028. case R_PPC_REL16_HI:
  7029. case R_PPC_REL16_HA:
  7030. case R_PPC_REL16DX_HA:
  7031. break;
  7032. case R_PPC_REL32:
  7033. if (h == NULL || h == htab->elf.hgot)
  7034. break;
  7035. /* fall through */
  7036. case R_PPC_ADDR32:
  7037. case R_PPC_ADDR16:
  7038. case R_PPC_ADDR16_LO:
  7039. case R_PPC_ADDR16_HI:
  7040. case R_PPC_ADDR16_HA:
  7041. case R_PPC_UADDR32:
  7042. case R_PPC_UADDR16:
  7043. goto dodyn;
  7044. case R_PPC_VLE_REL8:
  7045. case R_PPC_VLE_REL15:
  7046. case R_PPC_VLE_REL24:
  7047. case R_PPC_REL24:
  7048. case R_PPC_REL14:
  7049. case R_PPC_REL14_BRTAKEN:
  7050. case R_PPC_REL14_BRNTAKEN:
  7051. /* If these relocations are not to a named symbol, they can be
  7052. handled right here, no need to bother the dynamic linker. */
  7053. if (SYMBOL_CALLS_LOCAL (info, h)
  7054. || h == htab->elf.hgot)
  7055. break;
  7056. /* fall through */
  7057. case R_PPC_ADDR24:
  7058. case R_PPC_ADDR14:
  7059. case R_PPC_ADDR14_BRTAKEN:
  7060. case R_PPC_ADDR14_BRNTAKEN:
  7061. if (h != NULL && !bfd_link_pic (info))
  7062. break;
  7063. /* fall through */
  7064. dodyn:
  7065. if ((input_section->flags & SEC_ALLOC) == 0
  7066. || is_vxworks_tls)
  7067. break;
  7068. if (bfd_link_pic (info)
  7069. ? ((h == NULL
  7070. || h->dyn_relocs != NULL)
  7071. && ((h != NULL && pc_dynrelocs (h))
  7072. || must_be_dyn_reloc (info, r_type)))
  7073. : (h != NULL
  7074. && h->dyn_relocs != NULL))
  7075. {
  7076. int skip;
  7077. bfd_byte *loc;
  7078. asection *sreloc;
  7079. long indx = 0;
  7080. #ifdef DEBUG
  7081. fprintf (stderr, "ppc_elf_relocate_section needs to "
  7082. "create relocation for %s\n",
  7083. (h && h->root.root.string
  7084. ? h->root.root.string : "<unknown>"));
  7085. #endif
  7086. /* When generating a shared object, these relocations
  7087. are copied into the output file to be resolved at run
  7088. time. */
  7089. skip = 0;
  7090. outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
  7091. input_section,
  7092. rel->r_offset);
  7093. if (outrel.r_offset == (bfd_vma) -1
  7094. || outrel.r_offset == (bfd_vma) -2)
  7095. skip = (int) outrel.r_offset;
  7096. outrel.r_offset += (input_section->output_section->vma
  7097. + input_section->output_offset);
  7098. /* Optimize unaligned reloc use. */
  7099. if ((r_type == R_PPC_ADDR32 && (outrel.r_offset & 3) != 0)
  7100. || (r_type == R_PPC_UADDR32 && (outrel.r_offset & 3) == 0))
  7101. r_type ^= R_PPC_ADDR32 ^ R_PPC_UADDR32;
  7102. if ((r_type == R_PPC_ADDR16 && (outrel.r_offset & 1) != 0)
  7103. || (r_type == R_PPC_UADDR16 && (outrel.r_offset & 1) == 0))
  7104. r_type ^= R_PPC_ADDR16 ^ R_PPC_UADDR16;
  7105. if (skip)
  7106. memset (&outrel, 0, sizeof outrel);
  7107. else if (!SYMBOL_REFERENCES_LOCAL (info, h))
  7108. {
  7109. indx = h->dynindx;
  7110. BFD_ASSERT (indx != -1);
  7111. unresolved_reloc = false;
  7112. outrel.r_info = ELF32_R_INFO (indx, r_type);
  7113. outrel.r_addend = rel->r_addend;
  7114. }
  7115. else
  7116. {
  7117. outrel.r_addend = relocation + rel->r_addend;
  7118. if (r_type != R_PPC_ADDR32)
  7119. {
  7120. if (ifunc != NULL)
  7121. {
  7122. /* If we get here when building a static
  7123. executable, then the libc startup function
  7124. responsible for applying indirect function
  7125. relocations is going to complain about
  7126. the reloc type.
  7127. If we get here when building a dynamic
  7128. executable, it will be because we have
  7129. a text relocation. The dynamic loader
  7130. will set the text segment writable and
  7131. non-executable to apply text relocations.
  7132. So we'll segfault when trying to run the
  7133. indirection function to resolve the reloc. */
  7134. info->callbacks->einfo
  7135. /* xgettext:c-format */
  7136. (_("%H: relocation %s for indirect "
  7137. "function %s unsupported\n"),
  7138. input_bfd, input_section, rel->r_offset,
  7139. howto->name,
  7140. sym_name);
  7141. ret = false;
  7142. }
  7143. else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
  7144. ;
  7145. else if (sec == NULL || sec->owner == NULL)
  7146. {
  7147. bfd_set_error (bfd_error_bad_value);
  7148. ret = false;
  7149. }
  7150. else
  7151. {
  7152. asection *osec;
  7153. /* We are turning this relocation into one
  7154. against a section symbol. It would be
  7155. proper to subtract the symbol's value,
  7156. osec->vma, from the emitted reloc addend,
  7157. but ld.so expects buggy relocs.
  7158. FIXME: Why not always use a zero index? */
  7159. osec = sec->output_section;
  7160. if ((osec->flags & SEC_THREAD_LOCAL) != 0)
  7161. {
  7162. osec = htab->elf.tls_sec;
  7163. indx = 0;
  7164. }
  7165. else
  7166. {
  7167. indx = elf_section_data (osec)->dynindx;
  7168. if (indx == 0)
  7169. {
  7170. osec = htab->elf.text_index_section;
  7171. indx = elf_section_data (osec)->dynindx;
  7172. }
  7173. BFD_ASSERT (indx != 0);
  7174. }
  7175. /* ld.so doesn't expect buggy TLS relocs.
  7176. Don't leave the symbol value in the
  7177. addend for them. */
  7178. if (IS_PPC_TLS_RELOC (r_type))
  7179. outrel.r_addend -= osec->vma;
  7180. }
  7181. outrel.r_info = ELF32_R_INFO (indx, r_type);
  7182. }
  7183. else if (ifunc != NULL)
  7184. outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
  7185. else
  7186. outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
  7187. }
  7188. sreloc = elf_section_data (input_section)->sreloc;
  7189. if (ifunc)
  7190. {
  7191. sreloc = htab->elf.irelplt;
  7192. if (indx == 0)
  7193. htab->local_ifunc_resolver = 1;
  7194. else if (is_static_defined (h))
  7195. htab->maybe_local_ifunc_resolver = 1;
  7196. }
  7197. if (sreloc == NULL)
  7198. return false;
  7199. loc = sreloc->contents;
  7200. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  7201. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  7202. if (skip == -1)
  7203. goto copy_reloc;
  7204. /* This reloc will be computed at runtime. Clear the memory
  7205. so that it contains a predictable value for prelink. */
  7206. if (!skip)
  7207. {
  7208. relocation = howto->pc_relative ? outrel.r_offset : 0;
  7209. addend = 0;
  7210. break;
  7211. }
  7212. }
  7213. break;
  7214. case R_PPC_RELAX_PLT:
  7215. case R_PPC_RELAX_PLTREL24:
  7216. if (h != NULL)
  7217. {
  7218. struct plt_entry *ent;
  7219. bfd_vma got2_addend = 0;
  7220. if (r_type == R_PPC_RELAX_PLTREL24)
  7221. {
  7222. if (bfd_link_pic (info))
  7223. got2_addend = addend;
  7224. addend = 0;
  7225. }
  7226. ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
  7227. if (htab->plt_type == PLT_NEW)
  7228. relocation = (htab->glink->output_section->vma
  7229. + htab->glink->output_offset
  7230. + ent->glink_offset);
  7231. else
  7232. relocation = (htab->elf.splt->output_section->vma
  7233. + htab->elf.splt->output_offset
  7234. + ent->plt.offset);
  7235. }
  7236. /* Fall through. */
  7237. case R_PPC_RELAX:
  7238. if (bfd_link_pic (info)
  7239. ? offset_in_range (input_section, rel->r_offset - 12,
  7240. ARRAY_SIZE (shared_stub_entry) * 4)
  7241. : offset_in_range (input_section, rel->r_offset,
  7242. ARRAY_SIZE (stub_entry) * 4))
  7243. {
  7244. const int *stub;
  7245. size_t size;
  7246. size_t insn_offset = rel->r_offset;
  7247. unsigned int insn;
  7248. if (bfd_link_pic (info))
  7249. {
  7250. relocation -= (input_section->output_section->vma
  7251. + input_section->output_offset
  7252. + rel->r_offset - 4);
  7253. stub = shared_stub_entry;
  7254. bfd_put_32 (input_bfd, stub[0], contents + insn_offset - 12);
  7255. bfd_put_32 (input_bfd, stub[1], contents + insn_offset - 8);
  7256. bfd_put_32 (input_bfd, stub[2], contents + insn_offset - 4);
  7257. stub += 3;
  7258. size = ARRAY_SIZE (shared_stub_entry) - 3;
  7259. }
  7260. else
  7261. {
  7262. stub = stub_entry;
  7263. size = ARRAY_SIZE (stub_entry);
  7264. }
  7265. relocation += addend;
  7266. if (bfd_link_relocatable (info))
  7267. relocation = 0;
  7268. /* First insn is HA, second is LO. */
  7269. insn = *stub++;
  7270. insn |= ((relocation + 0x8000) >> 16) & 0xffff;
  7271. bfd_put_32 (input_bfd, insn, contents + insn_offset);
  7272. insn_offset += 4;
  7273. insn = *stub++;
  7274. insn |= relocation & 0xffff;
  7275. bfd_put_32 (input_bfd, insn, contents + insn_offset);
  7276. insn_offset += 4;
  7277. size -= 2;
  7278. while (size != 0)
  7279. {
  7280. insn = *stub++;
  7281. --size;
  7282. bfd_put_32 (input_bfd, insn, contents + insn_offset);
  7283. insn_offset += 4;
  7284. }
  7285. /* Rewrite the reloc and convert one of the trailing nop
  7286. relocs to describe this relocation. */
  7287. BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
  7288. /* The relocs are at the bottom 2 bytes */
  7289. wrel->r_offset = rel->r_offset + d_offset;
  7290. wrel->r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
  7291. wrel->r_addend = rel->r_addend;
  7292. memmove (wrel + 1, wrel, (relend - wrel - 1) * sizeof (*wrel));
  7293. wrel++, rel++;
  7294. wrel->r_offset += 4;
  7295. wrel->r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
  7296. }
  7297. else
  7298. goto de_fault;
  7299. continue;
  7300. /* Indirect .sdata relocation. */
  7301. case R_PPC_EMB_SDAI16:
  7302. BFD_ASSERT (htab->sdata[0].section != NULL);
  7303. if (!is_static_defined (htab->sdata[0].sym))
  7304. {
  7305. unresolved_reloc = true;
  7306. break;
  7307. }
  7308. relocation
  7309. = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
  7310. h, relocation, rel);
  7311. addend = 0;
  7312. break;
  7313. /* Indirect .sdata2 relocation. */
  7314. case R_PPC_EMB_SDA2I16:
  7315. BFD_ASSERT (htab->sdata[1].section != NULL);
  7316. if (!is_static_defined (htab->sdata[1].sym))
  7317. {
  7318. unresolved_reloc = true;
  7319. break;
  7320. }
  7321. relocation
  7322. = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
  7323. h, relocation, rel);
  7324. addend = 0;
  7325. break;
  7326. /* Handle the TOC16 reloc. We want to use the offset within the .got
  7327. section, not the actual VMA. This is appropriate when generating
  7328. an embedded ELF object, for which the .got section acts like the
  7329. AIX .toc section. */
  7330. case R_PPC_TOC16: /* phony GOT16 relocations */
  7331. if (sec == NULL || sec->output_section == NULL)
  7332. {
  7333. unresolved_reloc = true;
  7334. break;
  7335. }
  7336. BFD_ASSERT (strcmp (bfd_section_name (sec), ".got") == 0
  7337. || strcmp (bfd_section_name (sec), ".cgot") == 0);
  7338. addend -= sec->output_section->vma + sec->output_offset + 0x8000;
  7339. break;
  7340. case R_PPC_PLTREL24:
  7341. if (h != NULL && ifunc == NULL)
  7342. {
  7343. struct plt_entry *ent;
  7344. ent = find_plt_ent (&h->plt.plist, got2,
  7345. bfd_link_pic (info) ? addend : 0);
  7346. if (ent == NULL
  7347. || htab->elf.splt == NULL)
  7348. {
  7349. /* We didn't make a PLT entry for this symbol. This
  7350. happens when statically linking PIC code, or when
  7351. using -Bsymbolic. */
  7352. }
  7353. else
  7354. {
  7355. /* Relocation is to the entry for this symbol in the
  7356. procedure linkage table. */
  7357. unresolved_reloc = false;
  7358. if (htab->plt_type == PLT_NEW)
  7359. relocation = (htab->glink->output_section->vma
  7360. + htab->glink->output_offset
  7361. + ent->glink_offset);
  7362. else
  7363. relocation = (htab->elf.splt->output_section->vma
  7364. + htab->elf.splt->output_offset
  7365. + ent->plt.offset);
  7366. }
  7367. }
  7368. /* R_PPC_PLTREL24 is rather special. If non-zero, the
  7369. addend specifies the GOT pointer offset within .got2.
  7370. Don't apply it to the relocation field. */
  7371. addend = 0;
  7372. break;
  7373. case R_PPC_PLTSEQ:
  7374. case R_PPC_PLTCALL:
  7375. case R_PPC_PLT16_LO:
  7376. case R_PPC_PLT16_HI:
  7377. case R_PPC_PLT16_HA:
  7378. plt_list = NULL;
  7379. if (h != NULL)
  7380. plt_list = &h->plt.plist;
  7381. else if (ifunc != NULL)
  7382. plt_list = ifunc;
  7383. else if (local_got_offsets != NULL)
  7384. {
  7385. struct plt_entry **local_plt;
  7386. local_plt = (struct plt_entry **) (local_got_offsets
  7387. + symtab_hdr->sh_info);
  7388. plt_list = local_plt + r_symndx;
  7389. }
  7390. unresolved_reloc = true;
  7391. if (plt_list != NULL)
  7392. {
  7393. struct plt_entry *ent;
  7394. ent = find_plt_ent (plt_list, got2,
  7395. bfd_link_pic (info) ? addend : 0);
  7396. if (ent != NULL && ent->plt.offset != (bfd_vma) -1)
  7397. {
  7398. asection *plt;
  7399. unresolved_reloc = false;
  7400. plt = htab->elf.splt;
  7401. if (use_local_plt (info, h))
  7402. {
  7403. if (ifunc != NULL)
  7404. plt = htab->elf.iplt;
  7405. else
  7406. plt = htab->pltlocal;
  7407. }
  7408. relocation = (plt->output_section->vma
  7409. + plt->output_offset
  7410. + ent->plt.offset);
  7411. if (bfd_link_pic (info))
  7412. {
  7413. bfd_vma got = 0;
  7414. if (ent->addend >= 32768)
  7415. got = (ent->addend
  7416. + ent->sec->output_section->vma
  7417. + ent->sec->output_offset);
  7418. else
  7419. got = SYM_VAL (htab->elf.hgot);
  7420. relocation -= got;
  7421. }
  7422. }
  7423. }
  7424. addend = 0;
  7425. break;
  7426. /* Relocate against _SDA_BASE_. */
  7427. case R_PPC_SDAREL16:
  7428. {
  7429. const char *name;
  7430. struct elf_link_hash_entry *sda = htab->sdata[0].sym;
  7431. if (sec == NULL
  7432. || sec->output_section == NULL
  7433. || !is_static_defined (sda))
  7434. {
  7435. unresolved_reloc = true;
  7436. break;
  7437. }
  7438. addend -= SYM_VAL (sda);
  7439. name = bfd_section_name (sec->output_section);
  7440. if (!(strcmp (name, ".sdata") == 0
  7441. || strcmp (name, ".sbss") == 0))
  7442. {
  7443. _bfd_error_handler
  7444. /* xgettext:c-format */
  7445. (_("%pB: the target (%s) of a %s relocation is "
  7446. "in the wrong output section (%s)"),
  7447. input_bfd,
  7448. sym_name,
  7449. howto->name,
  7450. name);
  7451. }
  7452. }
  7453. break;
  7454. /* Relocate against _SDA2_BASE_. */
  7455. case R_PPC_EMB_SDA2REL:
  7456. {
  7457. const char *name;
  7458. struct elf_link_hash_entry *sda = htab->sdata[1].sym;
  7459. if (sec == NULL
  7460. || sec->output_section == NULL
  7461. || !is_static_defined (sda))
  7462. {
  7463. unresolved_reloc = true;
  7464. break;
  7465. }
  7466. addend -= SYM_VAL (sda);
  7467. name = bfd_section_name (sec->output_section);
  7468. if (!(strcmp (name, ".sdata2") == 0
  7469. || strcmp (name, ".sbss2") == 0))
  7470. {
  7471. _bfd_error_handler
  7472. /* xgettext:c-format */
  7473. (_("%pB: the target (%s) of a %s relocation is "
  7474. "in the wrong output section (%s)"),
  7475. input_bfd,
  7476. sym_name,
  7477. howto->name,
  7478. name);
  7479. }
  7480. }
  7481. break;
  7482. case R_PPC_VLE_LO16A:
  7483. relocation = relocation + addend;
  7484. r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
  7485. contents + rel->r_offset, relocation,
  7486. split16a_type,
  7487. htab->params->vle_reloc_fixup);
  7488. goto report_reloc;
  7489. case R_PPC_VLE_LO16D:
  7490. relocation = relocation + addend;
  7491. r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
  7492. contents + rel->r_offset, relocation,
  7493. split16d_type,
  7494. htab->params->vle_reloc_fixup);
  7495. goto report_reloc;
  7496. case R_PPC_VLE_HI16A:
  7497. relocation = (relocation + addend) >> 16;
  7498. r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
  7499. contents + rel->r_offset, relocation,
  7500. split16a_type,
  7501. htab->params->vle_reloc_fixup);
  7502. goto report_reloc;
  7503. case R_PPC_VLE_HI16D:
  7504. relocation = (relocation + addend) >> 16;
  7505. r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
  7506. contents + rel->r_offset, relocation,
  7507. split16d_type,
  7508. htab->params->vle_reloc_fixup);
  7509. goto report_reloc;
  7510. case R_PPC_VLE_HA16A:
  7511. relocation = (relocation + addend + 0x8000) >> 16;
  7512. r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
  7513. contents + rel->r_offset, relocation,
  7514. split16a_type,
  7515. htab->params->vle_reloc_fixup);
  7516. goto report_reloc;
  7517. case R_PPC_VLE_HA16D:
  7518. relocation = (relocation + addend + 0x8000) >> 16;
  7519. r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
  7520. contents + rel->r_offset, relocation,
  7521. split16d_type,
  7522. htab->params->vle_reloc_fixup);
  7523. goto report_reloc;
  7524. /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
  7525. case R_PPC_EMB_SDA21:
  7526. case R_PPC_VLE_SDA21:
  7527. case R_PPC_EMB_RELSDA:
  7528. case R_PPC_VLE_SDA21_LO:
  7529. if (!offset_in_range (input_section, rel->r_offset, 4))
  7530. {
  7531. r = bfd_reloc_outofrange;
  7532. goto report_reloc;
  7533. }
  7534. else
  7535. {
  7536. const char *name;
  7537. int reg;
  7538. unsigned int insn;
  7539. struct elf_link_hash_entry *sda = NULL;
  7540. if (sec == NULL || sec->output_section == NULL)
  7541. {
  7542. unresolved_reloc = true;
  7543. break;
  7544. }
  7545. name = bfd_section_name (sec->output_section);
  7546. if (strcmp (name, ".sdata") == 0
  7547. || strcmp (name, ".sbss") == 0)
  7548. {
  7549. reg = 13;
  7550. sda = htab->sdata[0].sym;
  7551. }
  7552. else if (strcmp (name, ".sdata2") == 0
  7553. || strcmp (name, ".sbss2") == 0)
  7554. {
  7555. reg = 2;
  7556. sda = htab->sdata[1].sym;
  7557. }
  7558. else if (strcmp (name, ".PPC.EMB.sdata0") == 0
  7559. || strcmp (name, ".PPC.EMB.sbss0") == 0)
  7560. {
  7561. reg = 0;
  7562. }
  7563. else
  7564. {
  7565. _bfd_error_handler
  7566. /* xgettext:c-format */
  7567. (_("%pB: the target (%s) of a %s relocation is "
  7568. "in the wrong output section (%s)"),
  7569. input_bfd,
  7570. sym_name,
  7571. howto->name,
  7572. name);
  7573. bfd_set_error (bfd_error_bad_value);
  7574. ret = false;
  7575. goto copy_reloc;
  7576. }
  7577. if (sda != NULL)
  7578. {
  7579. if (!is_static_defined (sda))
  7580. {
  7581. unresolved_reloc = true;
  7582. break;
  7583. }
  7584. addend -= SYM_VAL (sda);
  7585. }
  7586. if (r_type == R_PPC_EMB_RELSDA)
  7587. break;
  7588. /* The PowerPC Embedded Application Binary Interface
  7589. version 1.0 insanely chose to specify R_PPC_EMB_SDA21
  7590. operating on a 24-bit field at r_offset. GNU as and
  7591. GNU ld have always assumed R_PPC_EMB_SDA21 operates on
  7592. a 32-bit bit insn at r_offset. Cope with object file
  7593. producers that possibly comply with the EABI in
  7594. generating an odd r_offset for big-endian objects. */
  7595. if (r_type == R_PPC_EMB_SDA21)
  7596. rel->r_offset &= ~1;
  7597. insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  7598. if (reg == 0
  7599. && (r_type == R_PPC_VLE_SDA21
  7600. || r_type == R_PPC_VLE_SDA21_LO))
  7601. {
  7602. relocation = relocation + addend;
  7603. addend = 0;
  7604. /* Force e_li insn, keeping RT from original insn. */
  7605. insn &= 0x1f << 21;
  7606. insn |= 28u << 26;
  7607. /* We have an li20 field, bits 17..20, 11..15, 21..31. */
  7608. /* Top 4 bits of value to 17..20. */
  7609. insn |= (relocation & 0xf0000) >> 5;
  7610. /* Next 5 bits of the value to 11..15. */
  7611. insn |= (relocation & 0xf800) << 5;
  7612. /* And the final 11 bits of the value to bits 21 to 31. */
  7613. insn |= relocation & 0x7ff;
  7614. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  7615. r = bfd_reloc_ok;
  7616. if (r_type == R_PPC_VLE_SDA21
  7617. && ((relocation + 0x80000) & 0xffffffff) > 0x100000)
  7618. r = bfd_reloc_overflow;
  7619. goto report_reloc;
  7620. }
  7621. /* Fill in register field. */
  7622. insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
  7623. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  7624. }
  7625. break;
  7626. case R_PPC_VLE_SDAREL_LO16A:
  7627. case R_PPC_VLE_SDAREL_LO16D:
  7628. case R_PPC_VLE_SDAREL_HI16A:
  7629. case R_PPC_VLE_SDAREL_HI16D:
  7630. case R_PPC_VLE_SDAREL_HA16A:
  7631. case R_PPC_VLE_SDAREL_HA16D:
  7632. if (!offset_in_range (input_section, rel->r_offset, 4))
  7633. r = bfd_reloc_outofrange;
  7634. else
  7635. {
  7636. bfd_vma value;
  7637. const char *name;
  7638. struct elf_link_hash_entry *sda = NULL;
  7639. if (sec == NULL || sec->output_section == NULL)
  7640. {
  7641. unresolved_reloc = true;
  7642. break;
  7643. }
  7644. name = bfd_section_name (sec->output_section);
  7645. if (strcmp (name, ".sdata") == 0
  7646. || strcmp (name, ".sbss") == 0)
  7647. sda = htab->sdata[0].sym;
  7648. else if (strcmp (name, ".sdata2") == 0
  7649. || strcmp (name, ".sbss2") == 0)
  7650. sda = htab->sdata[1].sym;
  7651. else
  7652. {
  7653. _bfd_error_handler
  7654. /* xgettext:c-format */
  7655. (_("%pB: the target (%s) of a %s relocation is "
  7656. "in the wrong output section (%s)"),
  7657. input_bfd,
  7658. sym_name,
  7659. howto->name,
  7660. name);
  7661. bfd_set_error (bfd_error_bad_value);
  7662. ret = false;
  7663. goto copy_reloc;
  7664. }
  7665. if (sda == NULL || !is_static_defined (sda))
  7666. {
  7667. unresolved_reloc = true;
  7668. break;
  7669. }
  7670. value = relocation + addend - SYM_VAL (sda);
  7671. if (r_type == R_PPC_VLE_SDAREL_LO16A)
  7672. r = ppc_elf_vle_split16 (input_bfd, input_section,
  7673. rel->r_offset,
  7674. contents + rel->r_offset, value,
  7675. split16a_type,
  7676. htab->params->vle_reloc_fixup);
  7677. else if (r_type == R_PPC_VLE_SDAREL_LO16D)
  7678. r = ppc_elf_vle_split16 (input_bfd, input_section,
  7679. rel->r_offset,
  7680. contents + rel->r_offset, value,
  7681. split16d_type,
  7682. htab->params->vle_reloc_fixup);
  7683. else if (r_type == R_PPC_VLE_SDAREL_HI16A)
  7684. {
  7685. value = value >> 16;
  7686. r = ppc_elf_vle_split16 (input_bfd, input_section,
  7687. rel->r_offset,
  7688. contents + rel->r_offset, value,
  7689. split16a_type,
  7690. htab->params->vle_reloc_fixup);
  7691. }
  7692. else if (r_type == R_PPC_VLE_SDAREL_HI16D)
  7693. {
  7694. value = value >> 16;
  7695. r = ppc_elf_vle_split16 (input_bfd, input_section,
  7696. rel->r_offset,
  7697. contents + rel->r_offset, value,
  7698. split16d_type,
  7699. htab->params->vle_reloc_fixup);
  7700. }
  7701. else if (r_type == R_PPC_VLE_SDAREL_HA16A)
  7702. {
  7703. value = (value + 0x8000) >> 16;
  7704. r = ppc_elf_vle_split16 (input_bfd, input_section,
  7705. rel->r_offset,
  7706. contents + rel->r_offset, value,
  7707. split16a_type,
  7708. htab->params->vle_reloc_fixup);
  7709. }
  7710. else if (r_type == R_PPC_VLE_SDAREL_HA16D)
  7711. {
  7712. value = (value + 0x8000) >> 16;
  7713. r = ppc_elf_vle_split16 (input_bfd, input_section,
  7714. rel->r_offset,
  7715. contents + rel->r_offset, value,
  7716. split16d_type,
  7717. htab->params->vle_reloc_fixup);
  7718. }
  7719. else
  7720. abort ();
  7721. }
  7722. goto report_reloc;
  7723. case R_PPC_VLE_ADDR20:
  7724. if (!offset_in_range (input_section, rel->r_offset, 4))
  7725. r = bfd_reloc_outofrange;
  7726. else
  7727. {
  7728. ppc_elf_vle_split20 (output_bfd, contents + rel->r_offset,
  7729. relocation);
  7730. r = bfd_reloc_ok;
  7731. }
  7732. goto report_reloc;
  7733. /* Relocate against the beginning of the section. */
  7734. case R_PPC_SECTOFF:
  7735. case R_PPC_SECTOFF_LO:
  7736. case R_PPC_SECTOFF_HI:
  7737. case R_PPC_SECTOFF_HA:
  7738. if (sec == NULL || sec->output_section == NULL)
  7739. {
  7740. unresolved_reloc = true;
  7741. break;
  7742. }
  7743. addend -= sec->output_section->vma;
  7744. break;
  7745. /* Negative relocations. */
  7746. case R_PPC_EMB_NADDR32:
  7747. case R_PPC_EMB_NADDR16:
  7748. case R_PPC_EMB_NADDR16_LO:
  7749. case R_PPC_EMB_NADDR16_HI:
  7750. case R_PPC_EMB_NADDR16_HA:
  7751. addend -= 2 * relocation;
  7752. break;
  7753. case R_PPC_COPY:
  7754. case R_PPC_GLOB_DAT:
  7755. case R_PPC_JMP_SLOT:
  7756. case R_PPC_RELATIVE:
  7757. case R_PPC_IRELATIVE:
  7758. case R_PPC_PLT32:
  7759. case R_PPC_PLTREL32:
  7760. case R_PPC_ADDR30:
  7761. case R_PPC_EMB_RELSEC16:
  7762. case R_PPC_EMB_RELST_LO:
  7763. case R_PPC_EMB_RELST_HI:
  7764. case R_PPC_EMB_RELST_HA:
  7765. case R_PPC_EMB_BIT_FLD:
  7766. /* xgettext:c-format */
  7767. _bfd_error_handler (_("%pB: %s unsupported"),
  7768. input_bfd, howto->name);
  7769. bfd_set_error (bfd_error_invalid_operation);
  7770. ret = false;
  7771. goto copy_reloc;
  7772. }
  7773. switch (r_type)
  7774. {
  7775. default:
  7776. break;
  7777. case R_PPC_TPREL16_HA:
  7778. if (htab->do_tls_opt
  7779. && relocation + addend + 0x8000 < 0x10000
  7780. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  7781. {
  7782. bfd_byte *p = contents + (rel->r_offset & ~3);
  7783. bfd_put_32 (input_bfd, NOP, p);
  7784. }
  7785. break;
  7786. case R_PPC_TPREL16_LO:
  7787. if (htab->do_tls_opt
  7788. && relocation + addend + 0x8000 < 0x10000
  7789. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  7790. {
  7791. bfd_byte *p = contents + (rel->r_offset & ~3);
  7792. unsigned int insn = bfd_get_32 (input_bfd, p);
  7793. insn &= ~(0x1f << 16);
  7794. insn |= 2 << 16;
  7795. bfd_put_32 (input_bfd, insn, p);
  7796. }
  7797. break;
  7798. }
  7799. switch (r_type)
  7800. {
  7801. default:
  7802. break;
  7803. case R_PPC_PLTCALL:
  7804. if (unresolved_reloc)
  7805. {
  7806. if (offset_in_range (input_section, rel->r_offset, 4))
  7807. {
  7808. bfd_byte *p = contents + rel->r_offset;
  7809. unsigned int insn = bfd_get_32 (input_bfd, p);
  7810. insn &= 1;
  7811. bfd_put_32 (input_bfd, B | insn, p);
  7812. unresolved_reloc = save_unresolved_reloc;
  7813. r_type = R_PPC_REL24;
  7814. howto = ppc_elf_howto_table[r_type];
  7815. }
  7816. }
  7817. else if (htab->plt_type != PLT_NEW)
  7818. info->callbacks->einfo
  7819. (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
  7820. input_bfd, input_section, rel->r_offset,
  7821. howto->name);
  7822. break;
  7823. case R_PPC_PLTSEQ:
  7824. case R_PPC_PLT16_HA:
  7825. case R_PPC_PLT16_LO:
  7826. if (unresolved_reloc)
  7827. {
  7828. if (offset_in_range (input_section, rel->r_offset & ~3, 4))
  7829. {
  7830. bfd_byte *p = contents + (rel->r_offset & ~3);
  7831. bfd_put_32 (input_bfd, NOP, p);
  7832. unresolved_reloc = false;
  7833. r_type = R_PPC_NONE;
  7834. howto = ppc_elf_howto_table[r_type];
  7835. }
  7836. }
  7837. else if (htab->plt_type != PLT_NEW)
  7838. info->callbacks->einfo
  7839. (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
  7840. input_bfd, input_section, rel->r_offset,
  7841. howto->name);
  7842. break;
  7843. }
  7844. /* Do any further special processing. */
  7845. switch (r_type)
  7846. {
  7847. default:
  7848. break;
  7849. case R_PPC_ADDR16_HA:
  7850. case R_PPC_REL16_HA:
  7851. case R_PPC_REL16DX_HA:
  7852. case R_PPC_SECTOFF_HA:
  7853. case R_PPC_TPREL16_HA:
  7854. case R_PPC_DTPREL16_HA:
  7855. case R_PPC_EMB_NADDR16_HA:
  7856. case R_PPC_EMB_RELST_HA:
  7857. /* It's just possible that this symbol is a weak symbol
  7858. that's not actually defined anywhere. In that case,
  7859. 'sec' would be NULL, and we should leave the symbol
  7860. alone (it will be set to zero elsewhere in the link). */
  7861. if (sec == NULL)
  7862. break;
  7863. /* Fall through. */
  7864. case R_PPC_PLT16_HA:
  7865. case R_PPC_GOT16_HA:
  7866. case R_PPC_GOT_TLSGD16_HA:
  7867. case R_PPC_GOT_TLSLD16_HA:
  7868. case R_PPC_GOT_TPREL16_HA:
  7869. case R_PPC_GOT_DTPREL16_HA:
  7870. /* Add 0x10000 if sign bit in 0:15 is set.
  7871. Bits 0:15 are not used. */
  7872. addend += 0x8000;
  7873. break;
  7874. case R_PPC_ADDR16:
  7875. case R_PPC_ADDR16_LO:
  7876. case R_PPC_GOT16:
  7877. case R_PPC_GOT16_LO:
  7878. case R_PPC_SDAREL16:
  7879. case R_PPC_SECTOFF:
  7880. case R_PPC_SECTOFF_LO:
  7881. case R_PPC_DTPREL16:
  7882. case R_PPC_DTPREL16_LO:
  7883. case R_PPC_TPREL16:
  7884. case R_PPC_TPREL16_LO:
  7885. case R_PPC_GOT_TLSGD16:
  7886. case R_PPC_GOT_TLSGD16_LO:
  7887. case R_PPC_GOT_TLSLD16:
  7888. case R_PPC_GOT_TLSLD16_LO:
  7889. case R_PPC_GOT_DTPREL16:
  7890. case R_PPC_GOT_DTPREL16_LO:
  7891. case R_PPC_GOT_TPREL16:
  7892. case R_PPC_GOT_TPREL16_LO:
  7893. if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
  7894. {
  7895. /* The 32-bit ABI lacks proper relocations to deal with
  7896. certain 64-bit instructions. Prevent damage to bits
  7897. that make up part of the insn opcode. */
  7898. unsigned int insn, mask, lobit;
  7899. insn = bfd_get_32 (input_bfd,
  7900. contents + rel->r_offset - d_offset);
  7901. mask = 0;
  7902. if (is_insn_ds_form (insn))
  7903. mask = 3;
  7904. else if (is_insn_dq_form (insn))
  7905. mask = 15;
  7906. else
  7907. break;
  7908. relocation += addend;
  7909. addend = insn & mask;
  7910. lobit = mask & relocation;
  7911. if (lobit != 0)
  7912. {
  7913. relocation ^= lobit;
  7914. info->callbacks->einfo
  7915. /* xgettext:c-format */
  7916. (_("%H: error: %s against `%s' not a multiple of %u\n"),
  7917. input_bfd, input_section, rel->r_offset,
  7918. howto->name, sym_name, mask + 1);
  7919. bfd_set_error (bfd_error_bad_value);
  7920. ret = false;
  7921. }
  7922. }
  7923. break;
  7924. }
  7925. #ifdef DEBUG
  7926. fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
  7927. "offset = %ld, addend = %ld\n",
  7928. howto->name,
  7929. (int) r_type,
  7930. sym_name,
  7931. r_symndx,
  7932. (long) rel->r_offset,
  7933. (long) addend);
  7934. #endif
  7935. if (unresolved_reloc
  7936. && !((input_section->flags & SEC_DEBUGGING) != 0
  7937. && h->def_dynamic)
  7938. && _bfd_elf_section_offset (output_bfd, info, input_section,
  7939. rel->r_offset) != (bfd_vma) -1)
  7940. {
  7941. info->callbacks->einfo
  7942. /* xgettext:c-format */
  7943. (_("%H: unresolvable %s relocation against symbol `%s'\n"),
  7944. input_bfd, input_section, rel->r_offset,
  7945. howto->name,
  7946. sym_name);
  7947. ret = false;
  7948. }
  7949. /* 16-bit fields in insns mostly have signed values, but a
  7950. few insns have 16-bit unsigned values. Really, we should
  7951. have different reloc types. */
  7952. if (howto->complain_on_overflow != complain_overflow_dont
  7953. && howto->dst_mask == 0xffff
  7954. && (input_section->flags & SEC_CODE) != 0
  7955. && offset_in_range (input_section, rel->r_offset & ~3, 4))
  7956. {
  7957. enum complain_overflow complain = complain_overflow_signed;
  7958. if ((elf_section_flags (input_section) & SHF_PPC_VLE) == 0)
  7959. {
  7960. unsigned int insn;
  7961. insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
  7962. if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
  7963. complain = complain_overflow_bitfield;
  7964. else if ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
  7965. || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
  7966. || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
  7967. complain = complain_overflow_unsigned;
  7968. }
  7969. if (howto->complain_on_overflow != complain)
  7970. {
  7971. alt_howto = *howto;
  7972. alt_howto.complain_on_overflow = complain;
  7973. howto = &alt_howto;
  7974. }
  7975. }
  7976. if (r_type == R_PPC_REL16DX_HA)
  7977. {
  7978. /* Split field reloc isn't handled by _bfd_final_link_relocate. */
  7979. if (offset_in_range (input_section, rel->r_offset, 4))
  7980. r = bfd_reloc_outofrange;
  7981. else
  7982. {
  7983. unsigned int insn;
  7984. relocation += addend;
  7985. relocation -= (rel->r_offset
  7986. + input_section->output_offset
  7987. + input_section->output_section->vma);
  7988. relocation >>= 16;
  7989. insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  7990. insn &= ~0x1fffc1;
  7991. insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
  7992. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  7993. r = bfd_reloc_ok;
  7994. }
  7995. }
  7996. else
  7997. r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
  7998. rel->r_offset, relocation, addend);
  7999. report_reloc:
  8000. if (r != bfd_reloc_ok)
  8001. {
  8002. if (r == bfd_reloc_overflow)
  8003. {
  8004. /* On code like "if (foo) foo();" don't report overflow
  8005. on a branch to zero when foo is undefined. */
  8006. if (!warned
  8007. && !(h != NULL
  8008. && (h->root.type == bfd_link_hash_undefweak
  8009. || h->root.type == bfd_link_hash_undefined)
  8010. && is_branch_reloc (r_type)))
  8011. info->callbacks->reloc_overflow
  8012. (info, (h ? &h->root : NULL), sym_name, howto->name,
  8013. rel->r_addend, input_bfd, input_section, rel->r_offset);
  8014. }
  8015. else
  8016. {
  8017. info->callbacks->einfo
  8018. /* xgettext:c-format */
  8019. (_("%H: %s reloc against `%s': error %d\n"),
  8020. input_bfd, input_section, rel->r_offset,
  8021. howto->name, sym_name, (int) r);
  8022. ret = false;
  8023. }
  8024. }
  8025. copy_reloc:
  8026. if (wrel != rel)
  8027. *wrel = *rel;
  8028. }
  8029. if (wrel != rel)
  8030. {
  8031. Elf_Internal_Shdr *rel_hdr;
  8032. size_t deleted = rel - wrel;
  8033. rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
  8034. rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
  8035. if (rel_hdr->sh_size == 0)
  8036. {
  8037. /* It is too late to remove an empty reloc section. Leave
  8038. one NONE reloc.
  8039. ??? What is wrong with an empty section??? */
  8040. rel_hdr->sh_size = rel_hdr->sh_entsize;
  8041. deleted -= 1;
  8042. wrel++;
  8043. }
  8044. relend = wrel;
  8045. rel_hdr = _bfd_elf_single_rel_hdr (input_section);
  8046. rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
  8047. input_section->reloc_count -= deleted;
  8048. }
  8049. #ifdef DEBUG
  8050. fprintf (stderr, "\n");
  8051. #endif
  8052. if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET
  8053. && input_section->size != input_section->rawsize
  8054. && (strcmp (input_section->output_section->name, ".init") == 0
  8055. || strcmp (input_section->output_section->name, ".fini") == 0))
  8056. {
  8057. /* Branch around the trampolines. */
  8058. unsigned int insn = B + input_section->size - input_section->rawsize;
  8059. bfd_put_32 (input_bfd, insn, contents + input_section->rawsize);
  8060. }
  8061. if (htab->params->ppc476_workaround
  8062. && input_section->sec_info_type == SEC_INFO_TYPE_TARGET
  8063. && (!bfd_link_relocatable (info)
  8064. || (input_section->output_section->alignment_power
  8065. >= htab->params->pagesize_p2)))
  8066. {
  8067. bfd_vma start_addr, end_addr, addr;
  8068. bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
  8069. if (relax_info->workaround_size != 0)
  8070. {
  8071. bfd_byte *p;
  8072. unsigned int n;
  8073. bfd_byte fill[4];
  8074. bfd_put_32 (input_bfd, BA, fill);
  8075. p = contents + input_section->size - relax_info->workaround_size;
  8076. n = relax_info->workaround_size >> 2;
  8077. while (n--)
  8078. {
  8079. memcpy (p, fill, 4);
  8080. p += 4;
  8081. }
  8082. }
  8083. /* The idea is: Replace the last instruction on a page with a
  8084. branch to a patch area. Put the insn there followed by a
  8085. branch back to the next page. Complicated a little by
  8086. needing to handle moved conditional branches, and by not
  8087. wanting to touch data-in-text. */
  8088. start_addr = (input_section->output_section->vma
  8089. + input_section->output_offset);
  8090. end_addr = (start_addr + input_section->size
  8091. - relax_info->workaround_size);
  8092. for (addr = ((start_addr & -pagesize) + pagesize - 4);
  8093. addr < end_addr;
  8094. addr += pagesize)
  8095. {
  8096. bfd_vma offset = addr - start_addr;
  8097. Elf_Internal_Rela *lo, *hi;
  8098. bool is_data;
  8099. bfd_vma patch_off, patch_addr;
  8100. unsigned int insn;
  8101. /* Do we have a data reloc at this offset? If so, leave
  8102. the word alone. */
  8103. is_data = false;
  8104. lo = relocs;
  8105. hi = relend;
  8106. rel = NULL;
  8107. while (lo < hi)
  8108. {
  8109. rel = lo + (hi - lo) / 2;
  8110. if (rel->r_offset < offset)
  8111. lo = rel + 1;
  8112. else if (rel->r_offset > offset + 3)
  8113. hi = rel;
  8114. else
  8115. {
  8116. switch (ELF32_R_TYPE (rel->r_info))
  8117. {
  8118. case R_PPC_ADDR32:
  8119. case R_PPC_UADDR32:
  8120. case R_PPC_REL32:
  8121. case R_PPC_ADDR30:
  8122. is_data = true;
  8123. break;
  8124. default:
  8125. break;
  8126. }
  8127. break;
  8128. }
  8129. }
  8130. if (is_data)
  8131. continue;
  8132. /* Some instructions can be left alone too. Unconditional
  8133. branches, except for bcctr with BO=0x14 (bctr, bctrl),
  8134. avoid the icache failure.
  8135. The problem occurs due to prefetch across a page boundary
  8136. where stale instructions can be fetched from the next
  8137. page, and the mechanism for flushing these bad
  8138. instructions fails under certain circumstances. The
  8139. unconditional branches:
  8140. 1) Branch: b, bl, ba, bla,
  8141. 2) Branch Conditional: bc, bca, bcl, bcla,
  8142. 3) Branch Conditional to Link Register: bclr, bclrl,
  8143. where (2) and (3) have BO=0x14 making them unconditional,
  8144. prevent the bad prefetch because the prefetch itself is
  8145. affected by these instructions. This happens even if the
  8146. instruction is not executed.
  8147. A bctr example:
  8148. .
  8149. . lis 9,new_page@ha
  8150. . addi 9,9,new_page@l
  8151. . mtctr 9
  8152. . bctr
  8153. . nop
  8154. . nop
  8155. . new_page:
  8156. .
  8157. The bctr is not predicted taken due to ctr not being
  8158. ready, so prefetch continues on past the bctr into the
  8159. new page which might have stale instructions. If they
  8160. fail to be flushed, then they will be executed after the
  8161. bctr executes. Either of the following modifications
  8162. prevent the bad prefetch from happening in the first
  8163. place:
  8164. .
  8165. . lis 9,new_page@ha lis 9,new_page@ha
  8166. . addi 9,9,new_page@l addi 9,9,new_page@l
  8167. . mtctr 9 mtctr 9
  8168. . bctr bctr
  8169. . nop b somewhere_else
  8170. . b somewhere_else nop
  8171. . new_page: new_page:
  8172. . */
  8173. insn = bfd_get_32 (input_bfd, contents + offset);
  8174. if ((insn & (0x3fu << 26)) == (18u << 26) /* b,bl,ba,bla */
  8175. || ((insn & (0x3fu << 26)) == (16u << 26) /* bc,bcl,bca,bcla*/
  8176. && (insn & (0x14 << 21)) == (0x14 << 21)) /* with BO=0x14 */
  8177. || ((insn & (0x3fu << 26)) == (19u << 26)
  8178. && (insn & (0x3ff << 1)) == (16u << 1) /* bclr,bclrl */
  8179. && (insn & (0x14 << 21)) == (0x14 << 21)))/* with BO=0x14 */
  8180. continue;
  8181. patch_addr = (start_addr + input_section->size
  8182. - relax_info->workaround_size);
  8183. patch_addr = (patch_addr + 15) & -16;
  8184. patch_off = patch_addr - start_addr;
  8185. bfd_put_32 (input_bfd, B + patch_off - offset, contents + offset);
  8186. if (rel != NULL
  8187. && rel->r_offset >= offset
  8188. && rel->r_offset < offset + 4)
  8189. {
  8190. asection *sreloc;
  8191. /* If the insn we are patching had a reloc, adjust the
  8192. reloc r_offset so that the reloc applies to the moved
  8193. location. This matters for -r and --emit-relocs. */
  8194. if (rel + 1 != relend)
  8195. {
  8196. Elf_Internal_Rela tmp = *rel;
  8197. /* Keep the relocs sorted by r_offset. */
  8198. memmove (rel, rel + 1, (relend - (rel + 1)) * sizeof (*rel));
  8199. relend[-1] = tmp;
  8200. }
  8201. relend[-1].r_offset += patch_off - offset;
  8202. /* Adjust REL16 addends too. */
  8203. switch (ELF32_R_TYPE (relend[-1].r_info))
  8204. {
  8205. case R_PPC_REL16:
  8206. case R_PPC_REL16_LO:
  8207. case R_PPC_REL16_HI:
  8208. case R_PPC_REL16_HA:
  8209. relend[-1].r_addend += patch_off - offset;
  8210. break;
  8211. default:
  8212. break;
  8213. }
  8214. /* If we are building a PIE or shared library with
  8215. non-PIC objects, perhaps we had a dynamic reloc too?
  8216. If so, the dynamic reloc must move with the insn. */
  8217. sreloc = elf_section_data (input_section)->sreloc;
  8218. if (sreloc != NULL)
  8219. {
  8220. Elf32_External_Rela *slo, *shi, *srelend;
  8221. bfd_vma soffset;
  8222. slo = (Elf32_External_Rela *) sreloc->contents;
  8223. shi = srelend = slo + sreloc->reloc_count;
  8224. soffset = (offset + input_section->output_section->vma
  8225. + input_section->output_offset);
  8226. while (slo < shi)
  8227. {
  8228. Elf32_External_Rela *srel = slo + (shi - slo) / 2;
  8229. bfd_elf32_swap_reloca_in (output_bfd, (bfd_byte *) srel,
  8230. &outrel);
  8231. if (outrel.r_offset < soffset)
  8232. slo = srel + 1;
  8233. else if (outrel.r_offset > soffset + 3)
  8234. shi = srel;
  8235. else
  8236. {
  8237. if (srel + 1 != srelend)
  8238. {
  8239. memmove (srel, srel + 1,
  8240. (srelend - (srel + 1)) * sizeof (*srel));
  8241. srel = srelend - 1;
  8242. }
  8243. outrel.r_offset += patch_off - offset;
  8244. bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  8245. (bfd_byte *) srel);
  8246. break;
  8247. }
  8248. }
  8249. }
  8250. }
  8251. else
  8252. rel = NULL;
  8253. if ((insn & (0x3fu << 26)) == (16u << 26) /* bc */
  8254. && (insn & 2) == 0 /* relative */)
  8255. {
  8256. bfd_vma delta = ((insn & 0xfffc) ^ 0x8000) - 0x8000;
  8257. delta += offset - patch_off;
  8258. if (bfd_link_relocatable (info) && rel != NULL)
  8259. delta = 0;
  8260. if (!bfd_link_relocatable (info) && rel != NULL)
  8261. {
  8262. enum elf_ppc_reloc_type r_type;
  8263. r_type = ELF32_R_TYPE (relend[-1].r_info);
  8264. if (r_type == R_PPC_REL14_BRTAKEN)
  8265. insn |= BRANCH_PREDICT_BIT;
  8266. else if (r_type == R_PPC_REL14_BRNTAKEN)
  8267. insn &= ~BRANCH_PREDICT_BIT;
  8268. else
  8269. BFD_ASSERT (r_type == R_PPC_REL14);
  8270. if ((r_type == R_PPC_REL14_BRTAKEN
  8271. || r_type == R_PPC_REL14_BRNTAKEN)
  8272. && delta + 0x8000 < 0x10000
  8273. && (bfd_signed_vma) delta < 0)
  8274. insn ^= BRANCH_PREDICT_BIT;
  8275. }
  8276. if (delta + 0x8000 < 0x10000)
  8277. {
  8278. bfd_put_32 (input_bfd,
  8279. (insn & ~0xfffc) | (delta & 0xfffc),
  8280. contents + patch_off);
  8281. patch_off += 4;
  8282. bfd_put_32 (input_bfd,
  8283. B | ((offset + 4 - patch_off) & 0x3fffffc),
  8284. contents + patch_off);
  8285. patch_off += 4;
  8286. }
  8287. else
  8288. {
  8289. if (rel != NULL)
  8290. {
  8291. unsigned int r_sym = ELF32_R_SYM (relend[-1].r_info);
  8292. relend[-1].r_offset += 8;
  8293. relend[-1].r_info = ELF32_R_INFO (r_sym, R_PPC_REL24);
  8294. }
  8295. bfd_put_32 (input_bfd,
  8296. (insn & ~0xfffc) | 8,
  8297. contents + patch_off);
  8298. patch_off += 4;
  8299. bfd_put_32 (input_bfd,
  8300. B | ((offset + 4 - patch_off) & 0x3fffffc),
  8301. contents + patch_off);
  8302. patch_off += 4;
  8303. bfd_put_32 (input_bfd,
  8304. B | ((delta - 8) & 0x3fffffc),
  8305. contents + patch_off);
  8306. patch_off += 4;
  8307. }
  8308. }
  8309. else
  8310. {
  8311. bfd_put_32 (input_bfd, insn, contents + patch_off);
  8312. patch_off += 4;
  8313. bfd_put_32 (input_bfd,
  8314. B | ((offset + 4 - patch_off) & 0x3fffffc),
  8315. contents + patch_off);
  8316. patch_off += 4;
  8317. }
  8318. BFD_ASSERT (patch_off <= input_section->size);
  8319. relax_info->workaround_size = input_section->size - patch_off;
  8320. }
  8321. }
  8322. return ret;
  8323. }
  8324. /* Write out the PLT relocs and entries for H. */
  8325. static bool
  8326. write_global_sym_plt (struct elf_link_hash_entry *h, void *inf)
  8327. {
  8328. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  8329. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  8330. struct plt_entry *ent;
  8331. bool doneone;
  8332. doneone = false;
  8333. for (ent = h->plt.plist; ent != NULL; ent = ent->next)
  8334. if (ent->plt.offset != (bfd_vma) -1)
  8335. {
  8336. bool dyn = !use_local_plt (info, h);
  8337. if (!doneone)
  8338. {
  8339. Elf_Internal_Rela rela;
  8340. bfd_byte *loc;
  8341. bfd_vma reloc_index;
  8342. asection *plt = htab->elf.splt;
  8343. asection *relplt = htab->elf.srelplt;
  8344. if (htab->plt_type == PLT_NEW || !dyn)
  8345. reloc_index = ent->plt.offset / 4;
  8346. else
  8347. {
  8348. reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
  8349. / htab->plt_slot_size);
  8350. if (reloc_index > PLT_NUM_SINGLE_ENTRIES
  8351. && htab->plt_type == PLT_OLD)
  8352. reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
  8353. }
  8354. /* This symbol has an entry in the procedure linkage table.
  8355. Set it up. */
  8356. if (htab->plt_type == PLT_VXWORKS && dyn)
  8357. {
  8358. bfd_vma got_offset;
  8359. const bfd_vma *plt_entry;
  8360. /* The first three entries in .got.plt are reserved. */
  8361. got_offset = (reloc_index + 3) * 4;
  8362. /* Use the right PLT. */
  8363. plt_entry = bfd_link_pic (info) ? ppc_elf_vxworks_pic_plt_entry
  8364. : ppc_elf_vxworks_plt_entry;
  8365. /* Fill in the .plt on VxWorks. */
  8366. if (bfd_link_pic (info))
  8367. {
  8368. bfd_put_32 (info->output_bfd,
  8369. plt_entry[0] | PPC_HA (got_offset),
  8370. plt->contents + ent->plt.offset + 0);
  8371. bfd_put_32 (info->output_bfd,
  8372. plt_entry[1] | PPC_LO (got_offset),
  8373. plt->contents + ent->plt.offset + 4);
  8374. }
  8375. else
  8376. {
  8377. bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
  8378. bfd_put_32 (info->output_bfd,
  8379. plt_entry[0] | PPC_HA (got_loc),
  8380. plt->contents + ent->plt.offset + 0);
  8381. bfd_put_32 (info->output_bfd,
  8382. plt_entry[1] | PPC_LO (got_loc),
  8383. plt->contents + ent->plt.offset + 4);
  8384. }
  8385. bfd_put_32 (info->output_bfd, plt_entry[2],
  8386. plt->contents + ent->plt.offset + 8);
  8387. bfd_put_32 (info->output_bfd, plt_entry[3],
  8388. plt->contents + ent->plt.offset + 12);
  8389. /* This instruction is an immediate load. The value loaded is
  8390. the byte offset of the R_PPC_JMP_SLOT relocation from the
  8391. start of the .rela.plt section. The value is stored in the
  8392. low-order 16 bits of the load instruction. */
  8393. /* NOTE: It appears that this is now an index rather than a
  8394. prescaled offset. */
  8395. bfd_put_32 (info->output_bfd,
  8396. plt_entry[4] | reloc_index,
  8397. plt->contents + ent->plt.offset + 16);
  8398. /* This instruction is a PC-relative branch whose target is
  8399. the start of the PLT section. The address of this branch
  8400. instruction is 20 bytes beyond the start of this PLT entry.
  8401. The address is encoded in bits 6-29, inclusive. The value
  8402. stored is right-shifted by two bits, permitting a 26-bit
  8403. offset. */
  8404. bfd_put_32 (info->output_bfd,
  8405. (plt_entry[5]
  8406. | (-(ent->plt.offset + 20) & 0x03fffffc)),
  8407. plt->contents + ent->plt.offset + 20);
  8408. bfd_put_32 (info->output_bfd, plt_entry[6],
  8409. plt->contents + ent->plt.offset + 24);
  8410. bfd_put_32 (info->output_bfd, plt_entry[7],
  8411. plt->contents + ent->plt.offset + 28);
  8412. /* Fill in the GOT entry corresponding to this PLT slot with
  8413. the address immediately after the "bctr" instruction
  8414. in this PLT entry. */
  8415. bfd_put_32 (info->output_bfd, (plt->output_section->vma
  8416. + plt->output_offset
  8417. + ent->plt.offset + 16),
  8418. htab->elf.sgotplt->contents + got_offset);
  8419. if (!bfd_link_pic (info))
  8420. {
  8421. /* Fill in a couple of entries in .rela.plt.unloaded. */
  8422. loc = htab->srelplt2->contents
  8423. + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
  8424. * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
  8425. * sizeof (Elf32_External_Rela));
  8426. /* Provide the @ha relocation for the first instruction. */
  8427. rela.r_offset = (plt->output_section->vma
  8428. + plt->output_offset
  8429. + ent->plt.offset + 2);
  8430. rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
  8431. R_PPC_ADDR16_HA);
  8432. rela.r_addend = got_offset;
  8433. bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
  8434. loc += sizeof (Elf32_External_Rela);
  8435. /* Provide the @l relocation for the second instruction. */
  8436. rela.r_offset = (plt->output_section->vma
  8437. + plt->output_offset
  8438. + ent->plt.offset + 6);
  8439. rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
  8440. R_PPC_ADDR16_LO);
  8441. rela.r_addend = got_offset;
  8442. bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
  8443. loc += sizeof (Elf32_External_Rela);
  8444. /* Provide a relocation for the GOT entry corresponding to this
  8445. PLT slot. Point it at the middle of the .plt entry. */
  8446. rela.r_offset = (htab->elf.sgotplt->output_section->vma
  8447. + htab->elf.sgotplt->output_offset
  8448. + got_offset);
  8449. rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
  8450. R_PPC_ADDR32);
  8451. rela.r_addend = ent->plt.offset + 16;
  8452. bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
  8453. }
  8454. /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
  8455. In particular, the offset for the relocation is not the
  8456. address of the PLT entry for this function, as specified
  8457. by the ABI. Instead, the offset is set to the address of
  8458. the GOT slot for this function. See EABI 4.4.4.1. */
  8459. rela.r_offset = (htab->elf.sgotplt->output_section->vma
  8460. + htab->elf.sgotplt->output_offset
  8461. + got_offset);
  8462. rela.r_addend = 0;
  8463. }
  8464. else
  8465. {
  8466. rela.r_addend = 0;
  8467. if (!dyn)
  8468. {
  8469. if (h->type == STT_GNU_IFUNC)
  8470. {
  8471. plt = htab->elf.iplt;
  8472. relplt = htab->elf.irelplt;
  8473. }
  8474. else
  8475. {
  8476. plt = htab->pltlocal;
  8477. relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
  8478. }
  8479. if (h->def_regular
  8480. && (h->root.type == bfd_link_hash_defined
  8481. || h->root.type == bfd_link_hash_defweak))
  8482. rela.r_addend = SYM_VAL (h);
  8483. }
  8484. if (relplt == NULL)
  8485. {
  8486. loc = plt->contents + ent->plt.offset;
  8487. bfd_put_32 (info->output_bfd, rela.r_addend, loc);
  8488. }
  8489. else
  8490. {
  8491. rela.r_offset = (plt->output_section->vma
  8492. + plt->output_offset
  8493. + ent->plt.offset);
  8494. if (htab->plt_type == PLT_OLD || !dyn)
  8495. {
  8496. /* We don't need to fill in the .plt. The ppc dynamic
  8497. linker will fill it in. */
  8498. }
  8499. else
  8500. {
  8501. bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
  8502. + htab->glink->output_section->vma
  8503. + htab->glink->output_offset);
  8504. bfd_put_32 (info->output_bfd, val,
  8505. plt->contents + ent->plt.offset);
  8506. }
  8507. }
  8508. }
  8509. if (relplt != NULL)
  8510. {
  8511. /* Fill in the entry in the .rela.plt section. */
  8512. if (!dyn)
  8513. {
  8514. if (h->type == STT_GNU_IFUNC)
  8515. rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
  8516. else
  8517. rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
  8518. loc = relplt->contents + (relplt->reloc_count++
  8519. * sizeof (Elf32_External_Rela));
  8520. htab->local_ifunc_resolver = 1;
  8521. }
  8522. else
  8523. {
  8524. rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
  8525. loc = relplt->contents + (reloc_index
  8526. * sizeof (Elf32_External_Rela));
  8527. if (h->type == STT_GNU_IFUNC && is_static_defined (h))
  8528. htab->maybe_local_ifunc_resolver = 1;
  8529. }
  8530. bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
  8531. }
  8532. doneone = true;
  8533. }
  8534. if (htab->plt_type == PLT_NEW || !dyn)
  8535. {
  8536. unsigned char *p;
  8537. asection *plt = htab->elf.splt;
  8538. if (!dyn)
  8539. {
  8540. if (h->type == STT_GNU_IFUNC)
  8541. plt = htab->elf.iplt;
  8542. else
  8543. break;
  8544. }
  8545. p = (unsigned char *) htab->glink->contents + ent->glink_offset;
  8546. write_glink_stub (h, ent, plt, p, info);
  8547. if (!bfd_link_pic (info))
  8548. /* We only need one non-PIC glink stub. */
  8549. break;
  8550. }
  8551. else
  8552. break;
  8553. }
  8554. return true;
  8555. }
  8556. /* Finish up PLT handling. */
  8557. bool
  8558. ppc_finish_symbols (struct bfd_link_info *info)
  8559. {
  8560. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  8561. bfd *ibfd;
  8562. if (!htab)
  8563. return true;
  8564. elf_link_hash_traverse (&htab->elf, write_global_sym_plt, info);
  8565. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  8566. {
  8567. bfd_vma *local_got, *end_local_got;
  8568. struct plt_entry **local_plt, **lplt, **end_local_plt;
  8569. Elf_Internal_Shdr *symtab_hdr;
  8570. bfd_size_type locsymcount;
  8571. Elf_Internal_Sym *local_syms = NULL;
  8572. struct plt_entry *ent;
  8573. if (!is_ppc_elf (ibfd))
  8574. continue;
  8575. local_got = elf_local_got_offsets (ibfd);
  8576. if (!local_got)
  8577. continue;
  8578. symtab_hdr = &elf_symtab_hdr (ibfd);
  8579. locsymcount = symtab_hdr->sh_info;
  8580. end_local_got = local_got + locsymcount;
  8581. local_plt = (struct plt_entry **) end_local_got;
  8582. end_local_plt = local_plt + locsymcount;
  8583. for (lplt = local_plt; lplt < end_local_plt; ++lplt)
  8584. for (ent = *lplt; ent != NULL; ent = ent->next)
  8585. {
  8586. if (ent->plt.offset != (bfd_vma) -1)
  8587. {
  8588. Elf_Internal_Sym *sym;
  8589. asection *sym_sec;
  8590. asection *plt, *relplt;
  8591. bfd_byte *loc;
  8592. bfd_vma val;
  8593. Elf_Internal_Rela rela;
  8594. unsigned char *p;
  8595. if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
  8596. lplt - local_plt, ibfd))
  8597. {
  8598. if (symtab_hdr->contents != (unsigned char *) local_syms)
  8599. free (local_syms);
  8600. return false;
  8601. }
  8602. val = sym->st_value;
  8603. if (sym_sec != NULL && sym_sec->output_section != NULL)
  8604. val += sym_sec->output_offset + sym_sec->output_section->vma;
  8605. if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  8606. {
  8607. htab->local_ifunc_resolver = 1;
  8608. plt = htab->elf.iplt;
  8609. relplt = htab->elf.irelplt;
  8610. rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
  8611. }
  8612. else
  8613. {
  8614. plt = htab->pltlocal;
  8615. if (bfd_link_pic (info))
  8616. {
  8617. relplt = htab->relpltlocal;
  8618. rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
  8619. }
  8620. else
  8621. {
  8622. loc = plt->contents + ent->plt.offset;
  8623. bfd_put_32 (info->output_bfd, val, loc);
  8624. continue;
  8625. }
  8626. }
  8627. rela.r_offset = (ent->plt.offset
  8628. + plt->output_offset
  8629. + plt->output_section->vma);
  8630. rela.r_addend = val;
  8631. loc = relplt->contents + (relplt->reloc_count++
  8632. * sizeof (Elf32_External_Rela));
  8633. bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
  8634. p = (unsigned char *) htab->glink->contents + ent->glink_offset;
  8635. write_glink_stub (NULL, ent, htab->elf.iplt, p, info);
  8636. }
  8637. }
  8638. if (local_syms != NULL
  8639. && symtab_hdr->contents != (unsigned char *) local_syms)
  8640. {
  8641. if (!info->keep_memory)
  8642. free (local_syms);
  8643. else
  8644. symtab_hdr->contents = (unsigned char *) local_syms;
  8645. }
  8646. }
  8647. return true;
  8648. }
  8649. /* Finish up dynamic symbol handling. We set the contents of various
  8650. dynamic sections here. */
  8651. static bool
  8652. ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
  8653. struct bfd_link_info *info,
  8654. struct elf_link_hash_entry *h,
  8655. Elf_Internal_Sym *sym)
  8656. {
  8657. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  8658. struct plt_entry *ent;
  8659. #ifdef DEBUG
  8660. fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
  8661. h->root.root.string);
  8662. #endif
  8663. if (!h->def_regular
  8664. || (h->type == STT_GNU_IFUNC && !bfd_link_pic (info)))
  8665. for (ent = h->plt.plist; ent != NULL; ent = ent->next)
  8666. if (ent->plt.offset != (bfd_vma) -1)
  8667. {
  8668. if (!h->def_regular)
  8669. {
  8670. /* Mark the symbol as undefined, rather than as
  8671. defined in the .plt section. Leave the value if
  8672. there were any relocations where pointer equality
  8673. matters (this is a clue for the dynamic linker, to
  8674. make function pointer comparisons work between an
  8675. application and shared library), otherwise set it
  8676. to zero. */
  8677. sym->st_shndx = SHN_UNDEF;
  8678. if (!h->pointer_equality_needed)
  8679. sym->st_value = 0;
  8680. else if (!h->ref_regular_nonweak)
  8681. {
  8682. /* This breaks function pointer comparisons, but
  8683. that is better than breaking tests for a NULL
  8684. function pointer. */
  8685. sym->st_value = 0;
  8686. }
  8687. }
  8688. else
  8689. {
  8690. /* Set the value of ifunc symbols in a non-pie
  8691. executable to the glink entry. This is to avoid
  8692. text relocations. We can't do this for ifunc in
  8693. allocate_dynrelocs, as we do for normal dynamic
  8694. function symbols with plt entries, because we need
  8695. to keep the original value around for the ifunc
  8696. relocation. */
  8697. sym->st_shndx
  8698. = (_bfd_elf_section_from_bfd_section
  8699. (info->output_bfd, htab->glink->output_section));
  8700. sym->st_value = (ent->glink_offset
  8701. + htab->glink->output_offset
  8702. + htab->glink->output_section->vma);
  8703. }
  8704. break;
  8705. }
  8706. if (h->needs_copy)
  8707. {
  8708. asection *s;
  8709. Elf_Internal_Rela rela;
  8710. bfd_byte *loc;
  8711. /* This symbols needs a copy reloc. Set it up. */
  8712. #ifdef DEBUG
  8713. fprintf (stderr, ", copy");
  8714. #endif
  8715. BFD_ASSERT (h->dynindx != -1);
  8716. if (ppc_elf_hash_entry (h)->has_sda_refs)
  8717. s = htab->relsbss;
  8718. else if (h->root.u.def.section == htab->elf.sdynrelro)
  8719. s = htab->elf.sreldynrelro;
  8720. else
  8721. s = htab->elf.srelbss;
  8722. BFD_ASSERT (s != NULL);
  8723. rela.r_offset = SYM_VAL (h);
  8724. rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
  8725. rela.r_addend = 0;
  8726. loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
  8727. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  8728. }
  8729. #ifdef DEBUG
  8730. fprintf (stderr, "\n");
  8731. #endif
  8732. return true;
  8733. }
  8734. static enum elf_reloc_type_class
  8735. ppc_elf_reloc_type_class (const struct bfd_link_info *info,
  8736. const asection *rel_sec,
  8737. const Elf_Internal_Rela *rela)
  8738. {
  8739. struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
  8740. if (rel_sec == htab->elf.irelplt)
  8741. return reloc_class_ifunc;
  8742. switch (ELF32_R_TYPE (rela->r_info))
  8743. {
  8744. case R_PPC_RELATIVE:
  8745. return reloc_class_relative;
  8746. case R_PPC_JMP_SLOT:
  8747. return reloc_class_plt;
  8748. case R_PPC_COPY:
  8749. return reloc_class_copy;
  8750. default:
  8751. return reloc_class_normal;
  8752. }
  8753. }
  8754. /* Finish up the dynamic sections. */
  8755. static bool
  8756. ppc_elf_finish_dynamic_sections (bfd *output_bfd,
  8757. struct bfd_link_info *info)
  8758. {
  8759. asection *sdyn;
  8760. struct ppc_elf_link_hash_table *htab;
  8761. bfd_vma got;
  8762. bfd *dynobj;
  8763. bool ret = true;
  8764. #ifdef DEBUG
  8765. fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
  8766. #endif
  8767. htab = ppc_elf_hash_table (info);
  8768. dynobj = htab->elf.dynobj;
  8769. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  8770. got = 0;
  8771. if (htab->elf.hgot != NULL)
  8772. got = SYM_VAL (htab->elf.hgot);
  8773. if (htab->elf.dynamic_sections_created)
  8774. {
  8775. Elf32_External_Dyn *dyncon, *dynconend;
  8776. BFD_ASSERT (htab->elf.splt != NULL && sdyn != NULL);
  8777. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  8778. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  8779. for (; dyncon < dynconend; dyncon++)
  8780. {
  8781. Elf_Internal_Dyn dyn;
  8782. asection *s;
  8783. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  8784. switch (dyn.d_tag)
  8785. {
  8786. case DT_PLTGOT:
  8787. if (htab->elf.target_os == is_vxworks)
  8788. s = htab->elf.sgotplt;
  8789. else
  8790. s = htab->elf.splt;
  8791. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  8792. break;
  8793. case DT_PLTRELSZ:
  8794. dyn.d_un.d_val = htab->elf.srelplt->size;
  8795. break;
  8796. case DT_JMPREL:
  8797. s = htab->elf.srelplt;
  8798. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  8799. break;
  8800. case DT_PPC_GOT:
  8801. dyn.d_un.d_ptr = got;
  8802. break;
  8803. case DT_TEXTREL:
  8804. if (htab->local_ifunc_resolver)
  8805. info->callbacks->einfo
  8806. (_("%X%P: text relocations and GNU indirect "
  8807. "functions will result in a segfault at runtime\n"));
  8808. else if (htab->maybe_local_ifunc_resolver)
  8809. info->callbacks->einfo
  8810. (_("%P: warning: text relocations and GNU indirect "
  8811. "functions may result in a segfault at runtime\n"));
  8812. continue;
  8813. default:
  8814. if (htab->elf.target_os == is_vxworks
  8815. && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
  8816. break;
  8817. continue;
  8818. }
  8819. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  8820. }
  8821. }
  8822. if (htab->elf.sgot != NULL
  8823. && htab->elf.sgot->output_section != bfd_abs_section_ptr)
  8824. {
  8825. if (htab->elf.hgot->root.u.def.section == htab->elf.sgot
  8826. || htab->elf.hgot->root.u.def.section == htab->elf.sgotplt)
  8827. {
  8828. unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
  8829. p += htab->elf.hgot->root.u.def.value;
  8830. if (htab->plt_type == PLT_OLD)
  8831. {
  8832. /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
  8833. so that a function can easily find the address of
  8834. _GLOBAL_OFFSET_TABLE_. */
  8835. BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
  8836. < htab->elf.hgot->root.u.def.section->size);
  8837. bfd_put_32 (output_bfd, 0x4e800021, p - 4);
  8838. }
  8839. if (sdyn != NULL)
  8840. {
  8841. bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
  8842. BFD_ASSERT (htab->elf.hgot->root.u.def.value
  8843. < htab->elf.hgot->root.u.def.section->size);
  8844. bfd_put_32 (output_bfd, val, p);
  8845. }
  8846. }
  8847. else
  8848. {
  8849. /* xgettext:c-format */
  8850. _bfd_error_handler (_("%s not defined in linker created %pA"),
  8851. htab->elf.hgot->root.root.string,
  8852. (htab->elf.sgotplt != NULL
  8853. ? htab->elf.sgotplt : htab->elf.sgot));
  8854. bfd_set_error (bfd_error_bad_value);
  8855. ret = false;
  8856. }
  8857. elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
  8858. }
  8859. /* Fill in the first entry in the VxWorks procedure linkage table. */
  8860. if (htab->elf.target_os == is_vxworks
  8861. && htab->elf.splt != NULL
  8862. && htab->elf.splt->size != 0
  8863. && htab->elf.splt->output_section != bfd_abs_section_ptr)
  8864. {
  8865. asection *splt = htab->elf.splt;
  8866. /* Use the right PLT. */
  8867. const bfd_vma *plt_entry = (bfd_link_pic (info)
  8868. ? ppc_elf_vxworks_pic_plt0_entry
  8869. : ppc_elf_vxworks_plt0_entry);
  8870. if (!bfd_link_pic (info))
  8871. {
  8872. bfd_vma got_value = SYM_VAL (htab->elf.hgot);
  8873. bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
  8874. splt->contents + 0);
  8875. bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
  8876. splt->contents + 4);
  8877. }
  8878. else
  8879. {
  8880. bfd_put_32 (output_bfd, plt_entry[0], splt->contents + 0);
  8881. bfd_put_32 (output_bfd, plt_entry[1], splt->contents + 4);
  8882. }
  8883. bfd_put_32 (output_bfd, plt_entry[2], splt->contents + 8);
  8884. bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
  8885. bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
  8886. bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
  8887. bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
  8888. bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
  8889. if (! bfd_link_pic (info))
  8890. {
  8891. Elf_Internal_Rela rela;
  8892. bfd_byte *loc;
  8893. loc = htab->srelplt2->contents;
  8894. /* Output the @ha relocation for the first instruction. */
  8895. rela.r_offset = (htab->elf.splt->output_section->vma
  8896. + htab->elf.splt->output_offset
  8897. + 2);
  8898. rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
  8899. rela.r_addend = 0;
  8900. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  8901. loc += sizeof (Elf32_External_Rela);
  8902. /* Output the @l relocation for the second instruction. */
  8903. rela.r_offset = (htab->elf.splt->output_section->vma
  8904. + htab->elf.splt->output_offset
  8905. + 6);
  8906. rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
  8907. rela.r_addend = 0;
  8908. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  8909. loc += sizeof (Elf32_External_Rela);
  8910. /* Fix up the remaining relocations. They may have the wrong
  8911. symbol index for _G_O_T_ or _P_L_T_ depending on the order
  8912. in which symbols were output. */
  8913. while (loc < htab->srelplt2->contents + htab->srelplt2->size)
  8914. {
  8915. Elf_Internal_Rela rel;
  8916. bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
  8917. rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
  8918. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  8919. loc += sizeof (Elf32_External_Rela);
  8920. bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
  8921. rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
  8922. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  8923. loc += sizeof (Elf32_External_Rela);
  8924. bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
  8925. rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
  8926. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  8927. loc += sizeof (Elf32_External_Rela);
  8928. }
  8929. }
  8930. }
  8931. if (htab->glink != NULL
  8932. && htab->glink->contents != NULL
  8933. && htab->elf.dynamic_sections_created)
  8934. {
  8935. unsigned char *p;
  8936. unsigned char *endp;
  8937. bfd_vma res0;
  8938. /*
  8939. * PIC glink code is the following:
  8940. *
  8941. * # ith PLT code stub.
  8942. * addis 11,30,(plt+(i-1)*4-got)@ha
  8943. * lwz 11,(plt+(i-1)*4-got)@l(11)
  8944. * mtctr 11
  8945. * bctr
  8946. *
  8947. * # A table of branches, one for each plt entry.
  8948. * # The idea is that the plt call stub loads ctr and r11 with these
  8949. * # addresses, so (r11 - res_0) gives the plt index * 4.
  8950. * res_0: b PLTresolve
  8951. * res_1: b PLTresolve
  8952. * .
  8953. * # Some number of entries towards the end can be nops
  8954. * res_n_m3: nop
  8955. * res_n_m2: nop
  8956. * res_n_m1:
  8957. *
  8958. * PLTresolve:
  8959. * addis 11,11,(1f-res_0)@ha
  8960. * mflr 0
  8961. * bcl 20,31,1f
  8962. * 1: addi 11,11,(1b-res_0)@l
  8963. * mflr 12
  8964. * mtlr 0
  8965. * sub 11,11,12 # r11 = index * 4
  8966. * addis 12,12,(got+4-1b)@ha
  8967. * lwz 0,(got+4-1b)@l(12) # got[1] address of dl_runtime_resolve
  8968. * lwz 12,(got+8-1b)@l(12) # got[2] contains the map address
  8969. * mtctr 0
  8970. * add 0,11,11
  8971. * add 11,0,11 # r11 = index * 12 = reloc offset.
  8972. * bctr
  8973. *
  8974. * Non-PIC glink code is a little simpler.
  8975. *
  8976. * # ith PLT code stub.
  8977. * lis 11,(plt+(i-1)*4)@ha
  8978. * lwz 11,(plt+(i-1)*4)@l(11)
  8979. * mtctr 11
  8980. * bctr
  8981. *
  8982. * The branch table is the same, then comes
  8983. *
  8984. * PLTresolve:
  8985. * lis 12,(got+4)@ha
  8986. * addis 11,11,(-res_0)@ha
  8987. * lwz 0,(got+4)@l(12) # got[1] address of dl_runtime_resolve
  8988. * addi 11,11,(-res_0)@l # r11 = index * 4
  8989. * mtctr 0
  8990. * add 0,11,11
  8991. * lwz 12,(got+8)@l(12) # got[2] contains the map address
  8992. * add 11,0,11 # r11 = index * 12 = reloc offset.
  8993. * bctr
  8994. */
  8995. /* Build the branch table, one for each plt entry (less one),
  8996. and perhaps some padding. */
  8997. p = htab->glink->contents;
  8998. p += htab->glink_pltresolve;
  8999. endp = htab->glink->contents;
  9000. endp += htab->glink->size - GLINK_PLTRESOLVE;
  9001. while (p < endp - (htab->params->ppc476_workaround ? 0 : 8 * 4))
  9002. {
  9003. bfd_put_32 (output_bfd, B + endp - p, p);
  9004. p += 4;
  9005. }
  9006. while (p < endp)
  9007. {
  9008. bfd_put_32 (output_bfd, NOP, p);
  9009. p += 4;
  9010. }
  9011. res0 = (htab->glink_pltresolve
  9012. + htab->glink->output_section->vma
  9013. + htab->glink->output_offset);
  9014. if (htab->params->ppc476_workaround)
  9015. {
  9016. /* Ensure that a call stub at the end of a page doesn't
  9017. result in prefetch over the end of the page into the
  9018. glink branch table. */
  9019. bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
  9020. bfd_vma page_addr;
  9021. bfd_vma glink_start = (htab->glink->output_section->vma
  9022. + htab->glink->output_offset);
  9023. for (page_addr = res0 & -pagesize;
  9024. page_addr > glink_start;
  9025. page_addr -= pagesize)
  9026. {
  9027. /* We have a plt call stub that may need fixing. */
  9028. bfd_byte *loc;
  9029. unsigned int insn;
  9030. loc = htab->glink->contents + page_addr - 4 - glink_start;
  9031. insn = bfd_get_32 (output_bfd, loc);
  9032. if (insn == BCTR)
  9033. {
  9034. /* By alignment, we know that there must be at least
  9035. one other call stub before this one. */
  9036. insn = bfd_get_32 (output_bfd, loc - 16);
  9037. if (insn == BCTR)
  9038. bfd_put_32 (output_bfd, B | (-16 & 0x3fffffc), loc);
  9039. else
  9040. bfd_put_32 (output_bfd, B | (-20 & 0x3fffffc), loc);
  9041. }
  9042. }
  9043. }
  9044. /* Last comes the PLTresolve stub. */
  9045. endp = p + GLINK_PLTRESOLVE;
  9046. if (bfd_link_pic (info))
  9047. {
  9048. bfd_vma bcl;
  9049. bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
  9050. + htab->glink->output_section->vma
  9051. + htab->glink->output_offset);
  9052. bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (bcl - res0), p);
  9053. p += 4;
  9054. bfd_put_32 (output_bfd, MFLR_0, p);
  9055. p += 4;
  9056. bfd_put_32 (output_bfd, BCL_20_31, p);
  9057. p += 4;
  9058. bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (bcl - res0), p);
  9059. p += 4;
  9060. bfd_put_32 (output_bfd, MFLR_12, p);
  9061. p += 4;
  9062. bfd_put_32 (output_bfd, MTLR_0, p);
  9063. p += 4;
  9064. bfd_put_32 (output_bfd, SUB_11_11_12, p);
  9065. p += 4;
  9066. bfd_put_32 (output_bfd, ADDIS_12_12 + PPC_HA (got + 4 - bcl), p);
  9067. p += 4;
  9068. if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
  9069. {
  9070. bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4 - bcl), p);
  9071. p += 4;
  9072. bfd_put_32 (output_bfd, LWZ_12_12 + PPC_LO (got + 8 - bcl), p);
  9073. p += 4;
  9074. }
  9075. else
  9076. {
  9077. bfd_put_32 (output_bfd, LWZU_0_12 + PPC_LO (got + 4 - bcl), p);
  9078. p += 4;
  9079. bfd_put_32 (output_bfd, LWZ_12_12 + 4, p);
  9080. p += 4;
  9081. }
  9082. bfd_put_32 (output_bfd, MTCTR_0, p);
  9083. p += 4;
  9084. bfd_put_32 (output_bfd, ADD_0_11_11, p);
  9085. }
  9086. else
  9087. {
  9088. bfd_put_32 (output_bfd, LIS_12 + PPC_HA (got + 4), p);
  9089. p += 4;
  9090. bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (-res0), p);
  9091. p += 4;
  9092. if (PPC_HA (got + 4) == PPC_HA (got + 8))
  9093. bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4), p);
  9094. else
  9095. bfd_put_32 (output_bfd, LWZU_0_12 + PPC_LO (got + 4), p);
  9096. p += 4;
  9097. bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (-res0), p);
  9098. p += 4;
  9099. bfd_put_32 (output_bfd, MTCTR_0, p);
  9100. p += 4;
  9101. bfd_put_32 (output_bfd, ADD_0_11_11, p);
  9102. p += 4;
  9103. if (PPC_HA (got + 4) == PPC_HA (got + 8))
  9104. bfd_put_32 (output_bfd, LWZ_12_12 + PPC_LO (got + 8), p);
  9105. else
  9106. bfd_put_32 (output_bfd, LWZ_12_12 + 4, p);
  9107. }
  9108. p += 4;
  9109. bfd_put_32 (output_bfd, ADD_11_0_11, p);
  9110. p += 4;
  9111. bfd_put_32 (output_bfd, BCTR, p);
  9112. p += 4;
  9113. while (p < endp)
  9114. {
  9115. bfd_put_32 (output_bfd,
  9116. htab->params->ppc476_workaround ? BA : NOP, p);
  9117. p += 4;
  9118. }
  9119. BFD_ASSERT (p == endp);
  9120. }
  9121. if (htab->glink_eh_frame != NULL
  9122. && htab->glink_eh_frame->contents != NULL)
  9123. {
  9124. unsigned char *p = htab->glink_eh_frame->contents;
  9125. bfd_vma val;
  9126. p += sizeof (glink_eh_frame_cie);
  9127. /* FDE length. */
  9128. p += 4;
  9129. /* CIE pointer. */
  9130. p += 4;
  9131. /* Offset to .glink. */
  9132. val = (htab->glink->output_section->vma
  9133. + htab->glink->output_offset);
  9134. val -= (htab->glink_eh_frame->output_section->vma
  9135. + htab->glink_eh_frame->output_offset);
  9136. val -= p - htab->glink_eh_frame->contents;
  9137. bfd_put_32 (htab->elf.dynobj, val, p);
  9138. if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
  9139. && !_bfd_elf_write_section_eh_frame (output_bfd, info,
  9140. htab->glink_eh_frame,
  9141. htab->glink_eh_frame->contents))
  9142. return false;
  9143. }
  9144. return ret;
  9145. }
  9146. #define TARGET_LITTLE_SYM powerpc_elf32_le_vec
  9147. #define TARGET_LITTLE_NAME "elf32-powerpcle"
  9148. #define TARGET_BIG_SYM powerpc_elf32_vec
  9149. #define TARGET_BIG_NAME "elf32-powerpc"
  9150. #define ELF_ARCH bfd_arch_powerpc
  9151. #define ELF_TARGET_ID PPC32_ELF_DATA
  9152. #define ELF_MACHINE_CODE EM_PPC
  9153. #define ELF_MAXPAGESIZE 0x10000
  9154. #define ELF_COMMONPAGESIZE 0x1000
  9155. #define elf_info_to_howto ppc_elf_info_to_howto
  9156. #ifdef EM_CYGNUS_POWERPC
  9157. #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
  9158. #endif
  9159. #ifdef EM_PPC_OLD
  9160. #define ELF_MACHINE_ALT2 EM_PPC_OLD
  9161. #endif
  9162. #define elf_backend_plt_not_loaded 1
  9163. #define elf_backend_want_dynrelro 1
  9164. #define elf_backend_can_gc_sections 1
  9165. #define elf_backend_can_refcount 1
  9166. #define elf_backend_rela_normal 1
  9167. #define elf_backend_caches_rawsize 1
  9168. #define bfd_elf32_mkobject ppc_elf_mkobject
  9169. #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
  9170. #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
  9171. #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
  9172. #define bfd_elf32_bfd_reloc_name_lookup ppc_elf_reloc_name_lookup
  9173. #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
  9174. #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
  9175. #define bfd_elf32_get_synthetic_symtab ppc_elf_get_synthetic_symtab
  9176. #define elf_backend_object_p ppc_elf_object_p
  9177. #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
  9178. #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
  9179. #define elf_backend_relocate_section ppc_elf_relocate_section
  9180. #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
  9181. #define elf_backend_check_relocs ppc_elf_check_relocs
  9182. #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
  9183. #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
  9184. #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
  9185. #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
  9186. #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
  9187. #define elf_backend_hash_symbol ppc_elf_hash_symbol
  9188. #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
  9189. #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
  9190. #define elf_backend_fake_sections ppc_elf_fake_sections
  9191. #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
  9192. #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
  9193. #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
  9194. #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
  9195. #define elf_backend_write_core_note ppc_elf_write_core_note
  9196. #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
  9197. #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
  9198. #define elf_backend_final_write_processing ppc_elf_final_write_processing
  9199. #define elf_backend_write_section ppc_elf_write_section
  9200. #define elf_backend_get_sec_type_attr ppc_elf_get_sec_type_attr
  9201. #define elf_backend_plt_sym_val ppc_elf_plt_sym_val
  9202. #define elf_backend_action_discarded ppc_elf_action_discarded
  9203. #define elf_backend_init_index_section _bfd_elf_init_1_index_section
  9204. #define elf_backend_lookup_section_flags_hook ppc_elf_lookup_section_flags
  9205. #include "elf32-target.h"
  9206. /* FreeBSD Target */
  9207. #undef TARGET_LITTLE_SYM
  9208. #undef TARGET_LITTLE_NAME
  9209. #undef TARGET_BIG_SYM
  9210. #define TARGET_BIG_SYM powerpc_elf32_fbsd_vec
  9211. #undef TARGET_BIG_NAME
  9212. #define TARGET_BIG_NAME "elf32-powerpc-freebsd"
  9213. #undef ELF_OSABI
  9214. #define ELF_OSABI ELFOSABI_FREEBSD
  9215. #undef elf32_bed
  9216. #define elf32_bed elf32_powerpc_fbsd_bed
  9217. #include "elf32-target.h"
  9218. /* VxWorks Target */
  9219. #undef TARGET_LITTLE_SYM
  9220. #undef TARGET_LITTLE_NAME
  9221. #undef TARGET_BIG_SYM
  9222. #define TARGET_BIG_SYM powerpc_elf32_vxworks_vec
  9223. #undef TARGET_BIG_NAME
  9224. #define TARGET_BIG_NAME "elf32-powerpc-vxworks"
  9225. #undef ELF_OSABI
  9226. #undef ELF_TARGET_OS
  9227. #define ELF_TARGET_OS is_vxworks
  9228. /* VxWorks uses the elf default section flags for .plt. */
  9229. static const struct bfd_elf_special_section *
  9230. ppc_elf_vxworks_get_sec_type_attr (bfd *abfd, asection *sec)
  9231. {
  9232. if (sec->name == NULL)
  9233. return NULL;
  9234. if (strcmp (sec->name, ".plt") == 0)
  9235. return _bfd_elf_get_sec_type_attr (abfd, sec);
  9236. return ppc_elf_get_sec_type_attr (abfd, sec);
  9237. }
  9238. /* Like ppc_elf_link_hash_table_create, but overrides
  9239. appropriately for VxWorks. */
  9240. static struct bfd_link_hash_table *
  9241. ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
  9242. {
  9243. struct bfd_link_hash_table *ret;
  9244. ret = ppc_elf_link_hash_table_create (abfd);
  9245. if (ret)
  9246. {
  9247. struct ppc_elf_link_hash_table *htab
  9248. = (struct ppc_elf_link_hash_table *)ret;
  9249. htab->plt_type = PLT_VXWORKS;
  9250. htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
  9251. htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
  9252. htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
  9253. }
  9254. return ret;
  9255. }
  9256. /* Tweak magic VxWorks symbols as they are loaded. */
  9257. static bool
  9258. ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
  9259. struct bfd_link_info *info,
  9260. Elf_Internal_Sym *sym,
  9261. const char **namep,
  9262. flagword *flagsp,
  9263. asection **secp,
  9264. bfd_vma *valp)
  9265. {
  9266. if (!elf_vxworks_add_symbol_hook (abfd, info, sym, namep, flagsp, secp,
  9267. valp))
  9268. return false;
  9269. return ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp);
  9270. }
  9271. static bool
  9272. ppc_elf_vxworks_final_write_processing (bfd *abfd)
  9273. {
  9274. ppc_final_write_processing (abfd);
  9275. return elf_vxworks_final_write_processing (abfd);
  9276. }
  9277. /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
  9278. define it. */
  9279. #undef elf_backend_want_plt_sym
  9280. #define elf_backend_want_plt_sym 1
  9281. #undef elf_backend_want_got_plt
  9282. #define elf_backend_want_got_plt 1
  9283. #undef elf_backend_got_symbol_offset
  9284. #define elf_backend_got_symbol_offset 0
  9285. #undef elf_backend_plt_not_loaded
  9286. #define elf_backend_plt_not_loaded 0
  9287. #undef elf_backend_plt_readonly
  9288. #define elf_backend_plt_readonly 1
  9289. #undef elf_backend_got_header_size
  9290. #define elf_backend_got_header_size 12
  9291. #undef elf_backend_dtrel_excludes_plt
  9292. #define elf_backend_dtrel_excludes_plt 1
  9293. #undef bfd_elf32_get_synthetic_symtab
  9294. #undef bfd_elf32_bfd_link_hash_table_create
  9295. #define bfd_elf32_bfd_link_hash_table_create \
  9296. ppc_elf_vxworks_link_hash_table_create
  9297. #undef elf_backend_add_symbol_hook
  9298. #define elf_backend_add_symbol_hook \
  9299. ppc_elf_vxworks_add_symbol_hook
  9300. #undef elf_backend_link_output_symbol_hook
  9301. #define elf_backend_link_output_symbol_hook \
  9302. elf_vxworks_link_output_symbol_hook
  9303. #undef elf_backend_final_write_processing
  9304. #define elf_backend_final_write_processing \
  9305. ppc_elf_vxworks_final_write_processing
  9306. #undef elf_backend_get_sec_type_attr
  9307. #define elf_backend_get_sec_type_attr \
  9308. ppc_elf_vxworks_get_sec_type_attr
  9309. #undef elf_backend_emit_relocs
  9310. #define elf_backend_emit_relocs \
  9311. elf_vxworks_emit_relocs
  9312. #undef elf32_bed
  9313. #define elf32_bed ppc_elf_vxworks_bed
  9314. #include "elf32-target.h"