elf32-xtensa.c 325 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561
  1. /* Xtensa-specific support for 32-bit ELF.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 3 of the
  7. License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include <stdarg.h>
  19. #include <strings.h>
  20. #include "bfdlink.h"
  21. #include "libbfd.h"
  22. #include "elf-bfd.h"
  23. #include "elf/xtensa.h"
  24. #include "splay-tree.h"
  25. #include "xtensa-isa.h"
  26. #include "xtensa-config.h"
  27. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  28. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  29. #define XTENSA_NO_NOP_REMOVAL 0
  30. #ifndef XSHAL_ABI
  31. #define XSHAL_ABI 0
  32. #endif
  33. #ifndef XTHAL_ABI_UNDEFINED
  34. #define XTHAL_ABI_UNDEFINED -1
  35. #endif
  36. #ifndef XTHAL_ABI_WINDOWED
  37. #define XTHAL_ABI_WINDOWED 0
  38. #endif
  39. #ifndef XTHAL_ABI_CALL0
  40. #define XTHAL_ABI_CALL0 1
  41. #endif
  42. /* Local helper functions. */
  43. static bool add_extra_plt_sections (struct bfd_link_info *, int);
  44. static char *vsprint_msg (const char *, const char *, int, ...) ATTRIBUTE_PRINTF(2,4);
  45. static bfd_reloc_status_type bfd_elf_xtensa_reloc
  46. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  47. static bool do_fix_for_relocatable_link
  48. (Elf_Internal_Rela *, bfd *, asection *, bfd_byte *);
  49. static void do_fix_for_final_link
  50. (Elf_Internal_Rela *, bfd *, asection *, bfd_byte *, bfd_vma *);
  51. /* Local functions to handle Xtensa configurability. */
  52. static bool is_indirect_call_opcode (xtensa_opcode);
  53. static bool is_direct_call_opcode (xtensa_opcode);
  54. static bool is_windowed_call_opcode (xtensa_opcode);
  55. static xtensa_opcode get_const16_opcode (void);
  56. static xtensa_opcode get_l32r_opcode (void);
  57. static bfd_vma l32r_offset (bfd_vma, bfd_vma);
  58. static int get_relocation_opnd (xtensa_opcode, int);
  59. static int get_relocation_slot (int);
  60. static xtensa_opcode get_relocation_opcode
  61. (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
  62. static bool is_l32r_relocation
  63. (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
  64. static bool is_alt_relocation (int);
  65. static bool is_operand_relocation (int);
  66. static bfd_size_type insn_decode_len
  67. (bfd_byte *, bfd_size_type, bfd_size_type);
  68. static int insn_num_slots
  69. (bfd_byte *, bfd_size_type, bfd_size_type);
  70. static xtensa_opcode insn_decode_opcode
  71. (bfd_byte *, bfd_size_type, bfd_size_type, int);
  72. static bool check_branch_target_aligned
  73. (bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
  74. static bool check_loop_aligned
  75. (bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
  76. static bool check_branch_target_aligned_address (bfd_vma, int);
  77. static bfd_size_type get_asm_simplify_size
  78. (bfd_byte *, bfd_size_type, bfd_size_type);
  79. /* Functions for link-time code simplifications. */
  80. static bfd_reloc_status_type elf_xtensa_do_asm_simplify
  81. (bfd_byte *, bfd_vma, bfd_vma, char **);
  82. static bfd_reloc_status_type contract_asm_expansion
  83. (bfd_byte *, bfd_vma, Elf_Internal_Rela *, char **);
  84. static xtensa_opcode swap_callx_for_call_opcode (xtensa_opcode);
  85. static xtensa_opcode get_expanded_call_opcode (bfd_byte *, int, bool *);
  86. /* Access to internal relocations, section contents and symbols. */
  87. static Elf_Internal_Rela *retrieve_internal_relocs
  88. (bfd *, asection *, bool);
  89. static void pin_internal_relocs (asection *, Elf_Internal_Rela *);
  90. static void release_internal_relocs (asection *, Elf_Internal_Rela *);
  91. static bfd_byte *retrieve_contents (bfd *, asection *, bool);
  92. static void pin_contents (asection *, bfd_byte *);
  93. static void release_contents (asection *, bfd_byte *);
  94. static Elf_Internal_Sym *retrieve_local_syms (bfd *);
  95. /* Miscellaneous utility functions. */
  96. static asection *elf_xtensa_get_plt_section (struct bfd_link_info *, int);
  97. static asection *elf_xtensa_get_gotplt_section (struct bfd_link_info *, int);
  98. static asection *get_elf_r_symndx_section (bfd *, unsigned long);
  99. static struct elf_link_hash_entry *get_elf_r_symndx_hash_entry
  100. (bfd *, unsigned long);
  101. static bfd_vma get_elf_r_symndx_offset (bfd *, unsigned long);
  102. static bool is_reloc_sym_weak (bfd *, Elf_Internal_Rela *);
  103. static bool pcrel_reloc_fits (xtensa_opcode, int, bfd_vma, bfd_vma);
  104. static bool xtensa_is_property_section (asection *);
  105. static bool xtensa_is_insntable_section (asection *);
  106. static bool xtensa_is_littable_section (asection *);
  107. static bool xtensa_is_proptable_section (asection *);
  108. static int internal_reloc_compare (const void *, const void *);
  109. static int internal_reloc_matches (const void *, const void *);
  110. static asection *xtensa_get_property_section (asection *, const char *);
  111. static flagword xtensa_get_property_predef_flags (asection *);
  112. /* Other functions called directly by the linker. */
  113. typedef void (*deps_callback_t)
  114. (asection *, bfd_vma, asection *, bfd_vma, void *);
  115. extern bool xtensa_callback_required_dependence
  116. (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
  117. /* Globally visible flag for choosing size optimization of NOP removal
  118. instead of branch-target-aware minimization for NOP removal.
  119. When nonzero, narrow all instructions and remove all NOPs possible
  120. around longcall expansions. */
  121. int elf32xtensa_size_opt;
  122. /* The "new_section_hook" is used to set up a per-section
  123. "xtensa_relax_info" data structure with additional information used
  124. during relaxation. */
  125. typedef struct xtensa_relax_info_struct xtensa_relax_info;
  126. /* The GNU tools do not easily allow extending interfaces to pass around
  127. the pointer to the Xtensa ISA information, so instead we add a global
  128. variable here (in BFD) that can be used by any of the tools that need
  129. this information. */
  130. xtensa_isa xtensa_default_isa;
  131. /* When this is true, relocations may have been modified to refer to
  132. symbols from other input files. The per-section list of "fix"
  133. records needs to be checked when resolving relocations. */
  134. static bool relaxing_section = false;
  135. /* When this is true, during final links, literals that cannot be
  136. coalesced and their relocations may be moved to other sections. */
  137. int elf32xtensa_no_literal_movement = 1;
  138. /* Place property records for a section into individual property section
  139. with xt.prop. prefix. */
  140. bool elf32xtensa_separate_props = false;
  141. /* Xtensa ABI. It affects PLT entry code. */
  142. int elf32xtensa_abi = XTHAL_ABI_UNDEFINED;
  143. /* Rename one of the generic section flags to better document how it
  144. is used here. */
  145. /* Whether relocations have been processed. */
  146. #define reloc_done sec_flg0
  147. static reloc_howto_type elf_howto_table[] =
  148. {
  149. HOWTO (R_XTENSA_NONE, 0, 3, 0, false, 0, complain_overflow_dont,
  150. bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
  151. false, 0, 0, false),
  152. HOWTO (R_XTENSA_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
  153. bfd_elf_xtensa_reloc, "R_XTENSA_32",
  154. true, 0xffffffff, 0xffffffff, false),
  155. /* Replace a 32-bit value with a value from the runtime linker (only
  156. used by linker-generated stub functions). The r_addend value is
  157. special: 1 means to substitute a pointer to the runtime linker's
  158. dynamic resolver function; 2 means to substitute the link map for
  159. the shared object. */
  160. HOWTO (R_XTENSA_RTLD, 0, 2, 32, false, 0, complain_overflow_dont,
  161. NULL, "R_XTENSA_RTLD", false, 0, 0, false),
  162. HOWTO (R_XTENSA_GLOB_DAT, 0, 2, 32, false, 0, complain_overflow_bitfield,
  163. bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
  164. false, 0, 0xffffffff, false),
  165. HOWTO (R_XTENSA_JMP_SLOT, 0, 2, 32, false, 0, complain_overflow_bitfield,
  166. bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
  167. false, 0, 0xffffffff, false),
  168. HOWTO (R_XTENSA_RELATIVE, 0, 2, 32, false, 0, complain_overflow_bitfield,
  169. bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
  170. false, 0, 0xffffffff, false),
  171. HOWTO (R_XTENSA_PLT, 0, 2, 32, false, 0, complain_overflow_bitfield,
  172. bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
  173. false, 0, 0xffffffff, false),
  174. EMPTY_HOWTO (7),
  175. /* Old relocations for backward compatibility. */
  176. HOWTO (R_XTENSA_OP0, 0, 0, 0, true, 0, complain_overflow_dont,
  177. bfd_elf_xtensa_reloc, "R_XTENSA_OP0", false, 0, 0, true),
  178. HOWTO (R_XTENSA_OP1, 0, 0, 0, true, 0, complain_overflow_dont,
  179. bfd_elf_xtensa_reloc, "R_XTENSA_OP1", false, 0, 0, true),
  180. HOWTO (R_XTENSA_OP2, 0, 0, 0, true, 0, complain_overflow_dont,
  181. bfd_elf_xtensa_reloc, "R_XTENSA_OP2", false, 0, 0, true),
  182. /* Assembly auto-expansion. */
  183. HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, true, 0, complain_overflow_dont,
  184. bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND", false, 0, 0, true),
  185. /* Relax assembly auto-expansion. */
  186. HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, true, 0, complain_overflow_dont,
  187. bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY", false, 0, 0, true),
  188. EMPTY_HOWTO (13),
  189. HOWTO (R_XTENSA_32_PCREL, 0, 2, 32, true, 0, complain_overflow_bitfield,
  190. bfd_elf_xtensa_reloc, "R_XTENSA_32_PCREL",
  191. false, 0, 0xffffffff, true),
  192. /* GNU extension to record C++ vtable hierarchy. */
  193. HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 2, 0, false, 0, complain_overflow_dont,
  194. NULL, "R_XTENSA_GNU_VTINHERIT",
  195. false, 0, 0, false),
  196. /* GNU extension to record C++ vtable member usage. */
  197. HOWTO (R_XTENSA_GNU_VTENTRY, 0, 2, 0, false, 0, complain_overflow_dont,
  198. _bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
  199. false, 0, 0, false),
  200. /* Relocations for supporting difference of symbols. */
  201. HOWTO (R_XTENSA_DIFF8, 0, 0, 8, false, 0, complain_overflow_signed,
  202. bfd_elf_xtensa_reloc, "R_XTENSA_DIFF8", false, 0, 0xff, false),
  203. HOWTO (R_XTENSA_DIFF16, 0, 1, 16, false, 0, complain_overflow_signed,
  204. bfd_elf_xtensa_reloc, "R_XTENSA_DIFF16", false, 0, 0xffff, false),
  205. HOWTO (R_XTENSA_DIFF32, 0, 2, 32, false, 0, complain_overflow_signed,
  206. bfd_elf_xtensa_reloc, "R_XTENSA_DIFF32", false, 0, 0xffffffff, false),
  207. /* General immediate operand relocations. */
  208. HOWTO (R_XTENSA_SLOT0_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  209. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_OP", false, 0, 0, true),
  210. HOWTO (R_XTENSA_SLOT1_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  211. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_OP", false, 0, 0, true),
  212. HOWTO (R_XTENSA_SLOT2_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  213. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_OP", false, 0, 0, true),
  214. HOWTO (R_XTENSA_SLOT3_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  215. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_OP", false, 0, 0, true),
  216. HOWTO (R_XTENSA_SLOT4_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  217. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_OP", false, 0, 0, true),
  218. HOWTO (R_XTENSA_SLOT5_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  219. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_OP", false, 0, 0, true),
  220. HOWTO (R_XTENSA_SLOT6_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  221. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_OP", false, 0, 0, true),
  222. HOWTO (R_XTENSA_SLOT7_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  223. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_OP", false, 0, 0, true),
  224. HOWTO (R_XTENSA_SLOT8_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  225. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_OP", false, 0, 0, true),
  226. HOWTO (R_XTENSA_SLOT9_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  227. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_OP", false, 0, 0, true),
  228. HOWTO (R_XTENSA_SLOT10_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  229. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_OP", false, 0, 0, true),
  230. HOWTO (R_XTENSA_SLOT11_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  231. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_OP", false, 0, 0, true),
  232. HOWTO (R_XTENSA_SLOT12_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  233. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_OP", false, 0, 0, true),
  234. HOWTO (R_XTENSA_SLOT13_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  235. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_OP", false, 0, 0, true),
  236. HOWTO (R_XTENSA_SLOT14_OP, 0, 0, 0, true, 0, complain_overflow_dont,
  237. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_OP", false, 0, 0, true),
  238. /* "Alternate" relocations. The meaning of these is opcode-specific. */
  239. HOWTO (R_XTENSA_SLOT0_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  240. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_ALT", false, 0, 0, true),
  241. HOWTO (R_XTENSA_SLOT1_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  242. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_ALT", false, 0, 0, true),
  243. HOWTO (R_XTENSA_SLOT2_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  244. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_ALT", false, 0, 0, true),
  245. HOWTO (R_XTENSA_SLOT3_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  246. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_ALT", false, 0, 0, true),
  247. HOWTO (R_XTENSA_SLOT4_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  248. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_ALT", false, 0, 0, true),
  249. HOWTO (R_XTENSA_SLOT5_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  250. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_ALT", false, 0, 0, true),
  251. HOWTO (R_XTENSA_SLOT6_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  252. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_ALT", false, 0, 0, true),
  253. HOWTO (R_XTENSA_SLOT7_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  254. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_ALT", false, 0, 0, true),
  255. HOWTO (R_XTENSA_SLOT8_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  256. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_ALT", false, 0, 0, true),
  257. HOWTO (R_XTENSA_SLOT9_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  258. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_ALT", false, 0, 0, true),
  259. HOWTO (R_XTENSA_SLOT10_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  260. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_ALT", false, 0, 0, true),
  261. HOWTO (R_XTENSA_SLOT11_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  262. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_ALT", false, 0, 0, true),
  263. HOWTO (R_XTENSA_SLOT12_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  264. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_ALT", false, 0, 0, true),
  265. HOWTO (R_XTENSA_SLOT13_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  266. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_ALT", false, 0, 0, true),
  267. HOWTO (R_XTENSA_SLOT14_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
  268. bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_ALT", false, 0, 0, true),
  269. /* TLS relocations. */
  270. HOWTO (R_XTENSA_TLSDESC_FN, 0, 2, 32, false, 0, complain_overflow_dont,
  271. bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_FN",
  272. false, 0, 0xffffffff, false),
  273. HOWTO (R_XTENSA_TLSDESC_ARG, 0, 2, 32, false, 0, complain_overflow_dont,
  274. bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_ARG",
  275. false, 0, 0xffffffff, false),
  276. HOWTO (R_XTENSA_TLS_DTPOFF, 0, 2, 32, false, 0, complain_overflow_dont,
  277. bfd_elf_xtensa_reloc, "R_XTENSA_TLS_DTPOFF",
  278. false, 0, 0xffffffff, false),
  279. HOWTO (R_XTENSA_TLS_TPOFF, 0, 2, 32, false, 0, complain_overflow_dont,
  280. bfd_elf_xtensa_reloc, "R_XTENSA_TLS_TPOFF",
  281. false, 0, 0xffffffff, false),
  282. HOWTO (R_XTENSA_TLS_FUNC, 0, 0, 0, false, 0, complain_overflow_dont,
  283. bfd_elf_xtensa_reloc, "R_XTENSA_TLS_FUNC",
  284. false, 0, 0, false),
  285. HOWTO (R_XTENSA_TLS_ARG, 0, 0, 0, false, 0, complain_overflow_dont,
  286. bfd_elf_xtensa_reloc, "R_XTENSA_TLS_ARG",
  287. false, 0, 0, false),
  288. HOWTO (R_XTENSA_TLS_CALL, 0, 0, 0, false, 0, complain_overflow_dont,
  289. bfd_elf_xtensa_reloc, "R_XTENSA_TLS_CALL",
  290. false, 0, 0, false),
  291. HOWTO (R_XTENSA_PDIFF8, 0, 0, 8, false, 0, complain_overflow_bitfield,
  292. bfd_elf_xtensa_reloc, "R_XTENSA_PDIFF8", false, 0, 0xff, false),
  293. HOWTO (R_XTENSA_PDIFF16, 0, 1, 16, false, 0, complain_overflow_bitfield,
  294. bfd_elf_xtensa_reloc, "R_XTENSA_PDIFF16", false, 0, 0xffff, false),
  295. HOWTO (R_XTENSA_PDIFF32, 0, 2, 32, false, 0, complain_overflow_bitfield,
  296. bfd_elf_xtensa_reloc, "R_XTENSA_PDIFF32", false, 0, 0xffffffff, false),
  297. HOWTO (R_XTENSA_NDIFF8, 0, 0, 8, false, 0, complain_overflow_bitfield,
  298. bfd_elf_xtensa_reloc, "R_XTENSA_NDIFF8", false, 0, 0xff, false),
  299. HOWTO (R_XTENSA_NDIFF16, 0, 1, 16, false, 0, complain_overflow_bitfield,
  300. bfd_elf_xtensa_reloc, "R_XTENSA_NDIFF16", false, 0, 0xffff, false),
  301. HOWTO (R_XTENSA_NDIFF32, 0, 2, 32, false, 0, complain_overflow_bitfield,
  302. bfd_elf_xtensa_reloc, "R_XTENSA_NDIFF32", false, 0, 0xffffffff, false),
  303. };
  304. #if DEBUG_GEN_RELOC
  305. #define TRACE(str) \
  306. fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
  307. #else
  308. #define TRACE(str)
  309. #endif
  310. static reloc_howto_type *
  311. elf_xtensa_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  312. bfd_reloc_code_real_type code)
  313. {
  314. switch (code)
  315. {
  316. case BFD_RELOC_NONE:
  317. TRACE ("BFD_RELOC_NONE");
  318. return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
  319. case BFD_RELOC_32:
  320. TRACE ("BFD_RELOC_32");
  321. return &elf_howto_table[(unsigned) R_XTENSA_32 ];
  322. case BFD_RELOC_32_PCREL:
  323. TRACE ("BFD_RELOC_32_PCREL");
  324. return &elf_howto_table[(unsigned) R_XTENSA_32_PCREL ];
  325. case BFD_RELOC_XTENSA_DIFF8:
  326. TRACE ("BFD_RELOC_XTENSA_DIFF8");
  327. return &elf_howto_table[(unsigned) R_XTENSA_DIFF8 ];
  328. case BFD_RELOC_XTENSA_DIFF16:
  329. TRACE ("BFD_RELOC_XTENSA_DIFF16");
  330. return &elf_howto_table[(unsigned) R_XTENSA_DIFF16 ];
  331. case BFD_RELOC_XTENSA_DIFF32:
  332. TRACE ("BFD_RELOC_XTENSA_DIFF32");
  333. return &elf_howto_table[(unsigned) R_XTENSA_DIFF32 ];
  334. case BFD_RELOC_XTENSA_PDIFF8:
  335. TRACE ("BFD_RELOC_XTENSA_PDIFF8");
  336. return &elf_howto_table[(unsigned) R_XTENSA_PDIFF8 ];
  337. case BFD_RELOC_XTENSA_PDIFF16:
  338. TRACE ("BFD_RELOC_XTENSA_PDIFF16");
  339. return &elf_howto_table[(unsigned) R_XTENSA_PDIFF16 ];
  340. case BFD_RELOC_XTENSA_PDIFF32:
  341. TRACE ("BFD_RELOC_XTENSA_PDIFF32");
  342. return &elf_howto_table[(unsigned) R_XTENSA_PDIFF32 ];
  343. case BFD_RELOC_XTENSA_NDIFF8:
  344. TRACE ("BFD_RELOC_XTENSA_NDIFF8");
  345. return &elf_howto_table[(unsigned) R_XTENSA_NDIFF8 ];
  346. case BFD_RELOC_XTENSA_NDIFF16:
  347. TRACE ("BFD_RELOC_XTENSA_NDIFF16");
  348. return &elf_howto_table[(unsigned) R_XTENSA_NDIFF16 ];
  349. case BFD_RELOC_XTENSA_NDIFF32:
  350. TRACE ("BFD_RELOC_XTENSA_NDIFF32");
  351. return &elf_howto_table[(unsigned) R_XTENSA_NDIFF32 ];
  352. case BFD_RELOC_XTENSA_RTLD:
  353. TRACE ("BFD_RELOC_XTENSA_RTLD");
  354. return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
  355. case BFD_RELOC_XTENSA_GLOB_DAT:
  356. TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
  357. return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
  358. case BFD_RELOC_XTENSA_JMP_SLOT:
  359. TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
  360. return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
  361. case BFD_RELOC_XTENSA_RELATIVE:
  362. TRACE ("BFD_RELOC_XTENSA_RELATIVE");
  363. return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
  364. case BFD_RELOC_XTENSA_PLT:
  365. TRACE ("BFD_RELOC_XTENSA_PLT");
  366. return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
  367. case BFD_RELOC_XTENSA_OP0:
  368. TRACE ("BFD_RELOC_XTENSA_OP0");
  369. return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
  370. case BFD_RELOC_XTENSA_OP1:
  371. TRACE ("BFD_RELOC_XTENSA_OP1");
  372. return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
  373. case BFD_RELOC_XTENSA_OP2:
  374. TRACE ("BFD_RELOC_XTENSA_OP2");
  375. return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
  376. case BFD_RELOC_XTENSA_ASM_EXPAND:
  377. TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
  378. return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
  379. case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
  380. TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
  381. return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
  382. case BFD_RELOC_VTABLE_INHERIT:
  383. TRACE ("BFD_RELOC_VTABLE_INHERIT");
  384. return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
  385. case BFD_RELOC_VTABLE_ENTRY:
  386. TRACE ("BFD_RELOC_VTABLE_ENTRY");
  387. return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
  388. case BFD_RELOC_XTENSA_TLSDESC_FN:
  389. TRACE ("BFD_RELOC_XTENSA_TLSDESC_FN");
  390. return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_FN ];
  391. case BFD_RELOC_XTENSA_TLSDESC_ARG:
  392. TRACE ("BFD_RELOC_XTENSA_TLSDESC_ARG");
  393. return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_ARG ];
  394. case BFD_RELOC_XTENSA_TLS_DTPOFF:
  395. TRACE ("BFD_RELOC_XTENSA_TLS_DTPOFF");
  396. return &elf_howto_table[(unsigned) R_XTENSA_TLS_DTPOFF ];
  397. case BFD_RELOC_XTENSA_TLS_TPOFF:
  398. TRACE ("BFD_RELOC_XTENSA_TLS_TPOFF");
  399. return &elf_howto_table[(unsigned) R_XTENSA_TLS_TPOFF ];
  400. case BFD_RELOC_XTENSA_TLS_FUNC:
  401. TRACE ("BFD_RELOC_XTENSA_TLS_FUNC");
  402. return &elf_howto_table[(unsigned) R_XTENSA_TLS_FUNC ];
  403. case BFD_RELOC_XTENSA_TLS_ARG:
  404. TRACE ("BFD_RELOC_XTENSA_TLS_ARG");
  405. return &elf_howto_table[(unsigned) R_XTENSA_TLS_ARG ];
  406. case BFD_RELOC_XTENSA_TLS_CALL:
  407. TRACE ("BFD_RELOC_XTENSA_TLS_CALL");
  408. return &elf_howto_table[(unsigned) R_XTENSA_TLS_CALL ];
  409. default:
  410. if (code >= BFD_RELOC_XTENSA_SLOT0_OP
  411. && code <= BFD_RELOC_XTENSA_SLOT14_OP)
  412. {
  413. unsigned n = (R_XTENSA_SLOT0_OP +
  414. (code - BFD_RELOC_XTENSA_SLOT0_OP));
  415. return &elf_howto_table[n];
  416. }
  417. if (code >= BFD_RELOC_XTENSA_SLOT0_ALT
  418. && code <= BFD_RELOC_XTENSA_SLOT14_ALT)
  419. {
  420. unsigned n = (R_XTENSA_SLOT0_ALT +
  421. (code - BFD_RELOC_XTENSA_SLOT0_ALT));
  422. return &elf_howto_table[n];
  423. }
  424. break;
  425. }
  426. /* xgettext:c-format */
  427. _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
  428. bfd_set_error (bfd_error_bad_value);
  429. TRACE ("Unknown");
  430. return NULL;
  431. }
  432. static reloc_howto_type *
  433. elf_xtensa_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  434. const char *r_name)
  435. {
  436. unsigned int i;
  437. for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
  438. if (elf_howto_table[i].name != NULL
  439. && strcasecmp (elf_howto_table[i].name, r_name) == 0)
  440. return &elf_howto_table[i];
  441. return NULL;
  442. }
  443. /* Given an ELF "rela" relocation, find the corresponding howto and record
  444. it in the BFD internal arelent representation of the relocation. */
  445. static bool
  446. elf_xtensa_info_to_howto_rela (bfd *abfd,
  447. arelent *cache_ptr,
  448. Elf_Internal_Rela *dst)
  449. {
  450. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  451. if (r_type >= (unsigned int) R_XTENSA_max)
  452. {
  453. /* xgettext:c-format */
  454. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  455. abfd, r_type);
  456. bfd_set_error (bfd_error_bad_value);
  457. return false;
  458. }
  459. cache_ptr->howto = &elf_howto_table[r_type];
  460. return true;
  461. }
  462. /* Functions for the Xtensa ELF linker. */
  463. /* The name of the dynamic interpreter. This is put in the .interp
  464. section. */
  465. #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
  466. /* The size in bytes of an entry in the procedure linkage table.
  467. (This does _not_ include the space for the literals associated with
  468. the PLT entry.) */
  469. #define PLT_ENTRY_SIZE 16
  470. /* For _really_ large PLTs, we may need to alternate between literals
  471. and code to keep the literals within the 256K range of the L32R
  472. instructions in the code. It's unlikely that anyone would ever need
  473. such a big PLT, but an arbitrary limit on the PLT size would be bad.
  474. Thus, we split the PLT into chunks. Since there's very little
  475. overhead (2 extra literals) for each chunk, the chunk size is kept
  476. small so that the code for handling multiple chunks get used and
  477. tested regularly. With 254 entries, there are 1K of literals for
  478. each chunk, and that seems like a nice round number. */
  479. #define PLT_ENTRIES_PER_CHUNK 254
  480. /* PLT entries are actually used as stub functions for lazy symbol
  481. resolution. Once the symbol is resolved, the stub function is never
  482. invoked. Note: the 32-byte frame size used here cannot be changed
  483. without a corresponding change in the runtime linker. */
  484. static const bfd_byte elf_xtensa_be_plt_entry[][PLT_ENTRY_SIZE] =
  485. {
  486. {
  487. 0x6c, 0x10, 0x04, /* entry sp, 32 */
  488. 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
  489. 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
  490. 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
  491. 0x0a, 0x80, 0x00, /* jx a8 */
  492. 0 /* unused */
  493. },
  494. {
  495. 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
  496. 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
  497. 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
  498. 0x0a, 0x80, 0x00, /* jx a8 */
  499. 0 /* unused */
  500. }
  501. };
  502. static const bfd_byte elf_xtensa_le_plt_entry[][PLT_ENTRY_SIZE] =
  503. {
  504. {
  505. 0x36, 0x41, 0x00, /* entry sp, 32 */
  506. 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
  507. 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
  508. 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
  509. 0xa0, 0x08, 0x00, /* jx a8 */
  510. 0 /* unused */
  511. },
  512. {
  513. 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
  514. 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
  515. 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
  516. 0xa0, 0x08, 0x00, /* jx a8 */
  517. 0 /* unused */
  518. }
  519. };
  520. /* The size of the thread control block. */
  521. #define TCB_SIZE 8
  522. struct elf_xtensa_link_hash_entry
  523. {
  524. struct elf_link_hash_entry elf;
  525. bfd_signed_vma tlsfunc_refcount;
  526. #define GOT_UNKNOWN 0
  527. #define GOT_NORMAL 1
  528. #define GOT_TLS_GD 2 /* global or local dynamic */
  529. #define GOT_TLS_IE 4 /* initial or local exec */
  530. #define GOT_TLS_ANY (GOT_TLS_GD | GOT_TLS_IE)
  531. unsigned char tls_type;
  532. };
  533. #define elf_xtensa_hash_entry(ent) ((struct elf_xtensa_link_hash_entry *)(ent))
  534. struct elf_xtensa_obj_tdata
  535. {
  536. struct elf_obj_tdata root;
  537. /* tls_type for each local got entry. */
  538. char *local_got_tls_type;
  539. bfd_signed_vma *local_tlsfunc_refcounts;
  540. };
  541. #define elf_xtensa_tdata(abfd) \
  542. ((struct elf_xtensa_obj_tdata *) (abfd)->tdata.any)
  543. #define elf_xtensa_local_got_tls_type(abfd) \
  544. (elf_xtensa_tdata (abfd)->local_got_tls_type)
  545. #define elf_xtensa_local_tlsfunc_refcounts(abfd) \
  546. (elf_xtensa_tdata (abfd)->local_tlsfunc_refcounts)
  547. #define is_xtensa_elf(bfd) \
  548. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  549. && elf_tdata (bfd) != NULL \
  550. && elf_object_id (bfd) == XTENSA_ELF_DATA)
  551. static bool
  552. elf_xtensa_mkobject (bfd *abfd)
  553. {
  554. return bfd_elf_allocate_object (abfd, sizeof (struct elf_xtensa_obj_tdata),
  555. XTENSA_ELF_DATA);
  556. }
  557. /* Xtensa ELF linker hash table. */
  558. struct elf_xtensa_link_hash_table
  559. {
  560. struct elf_link_hash_table elf;
  561. /* Short-cuts to get to dynamic linker sections. */
  562. asection *sgotloc;
  563. asection *spltlittbl;
  564. /* Total count of PLT relocations seen during check_relocs.
  565. The actual PLT code must be split into multiple sections and all
  566. the sections have to be created before size_dynamic_sections,
  567. where we figure out the exact number of PLT entries that will be
  568. needed. It is OK if this count is an overestimate, e.g., some
  569. relocations may be removed by GC. */
  570. int plt_reloc_count;
  571. struct elf_xtensa_link_hash_entry *tlsbase;
  572. };
  573. /* Get the Xtensa ELF linker hash table from a link_info structure. */
  574. #define elf_xtensa_hash_table(p) \
  575. ((is_elf_hash_table ((p)->hash) \
  576. && elf_hash_table_id (elf_hash_table (p)) == XTENSA_ELF_DATA) \
  577. ? (struct elf_xtensa_link_hash_table *) (p)->hash : NULL)
  578. /* Create an entry in an Xtensa ELF linker hash table. */
  579. static struct bfd_hash_entry *
  580. elf_xtensa_link_hash_newfunc (struct bfd_hash_entry *entry,
  581. struct bfd_hash_table *table,
  582. const char *string)
  583. {
  584. /* Allocate the structure if it has not already been allocated by a
  585. subclass. */
  586. if (entry == NULL)
  587. {
  588. entry = bfd_hash_allocate (table,
  589. sizeof (struct elf_xtensa_link_hash_entry));
  590. if (entry == NULL)
  591. return entry;
  592. }
  593. /* Call the allocation method of the superclass. */
  594. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  595. if (entry != NULL)
  596. {
  597. struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (entry);
  598. eh->tlsfunc_refcount = 0;
  599. eh->tls_type = GOT_UNKNOWN;
  600. }
  601. return entry;
  602. }
  603. /* Create an Xtensa ELF linker hash table. */
  604. static struct bfd_link_hash_table *
  605. elf_xtensa_link_hash_table_create (bfd *abfd)
  606. {
  607. struct elf_link_hash_entry *tlsbase;
  608. struct elf_xtensa_link_hash_table *ret;
  609. size_t amt = sizeof (struct elf_xtensa_link_hash_table);
  610. ret = bfd_zmalloc (amt);
  611. if (ret == NULL)
  612. return NULL;
  613. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
  614. elf_xtensa_link_hash_newfunc,
  615. sizeof (struct elf_xtensa_link_hash_entry),
  616. XTENSA_ELF_DATA))
  617. {
  618. free (ret);
  619. return NULL;
  620. }
  621. /* Create a hash entry for "_TLS_MODULE_BASE_" to speed up checking
  622. for it later. */
  623. tlsbase = elf_link_hash_lookup (&ret->elf, "_TLS_MODULE_BASE_",
  624. true, false, false);
  625. tlsbase->root.type = bfd_link_hash_new;
  626. tlsbase->root.u.undef.abfd = NULL;
  627. tlsbase->non_elf = 0;
  628. ret->elf.dt_pltgot_required = true;
  629. ret->tlsbase = elf_xtensa_hash_entry (tlsbase);
  630. ret->tlsbase->tls_type = GOT_UNKNOWN;
  631. return &ret->elf.root;
  632. }
  633. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  634. static void
  635. elf_xtensa_copy_indirect_symbol (struct bfd_link_info *info,
  636. struct elf_link_hash_entry *dir,
  637. struct elf_link_hash_entry *ind)
  638. {
  639. struct elf_xtensa_link_hash_entry *edir, *eind;
  640. edir = elf_xtensa_hash_entry (dir);
  641. eind = elf_xtensa_hash_entry (ind);
  642. if (ind->root.type == bfd_link_hash_indirect)
  643. {
  644. edir->tlsfunc_refcount += eind->tlsfunc_refcount;
  645. eind->tlsfunc_refcount = 0;
  646. if (dir->got.refcount <= 0)
  647. {
  648. edir->tls_type = eind->tls_type;
  649. eind->tls_type = GOT_UNKNOWN;
  650. }
  651. }
  652. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  653. }
  654. static inline bool
  655. elf_xtensa_dynamic_symbol_p (struct elf_link_hash_entry *h,
  656. struct bfd_link_info *info)
  657. {
  658. /* Check if we should do dynamic things to this symbol. The
  659. "ignore_protected" argument need not be set, because Xtensa code
  660. does not require special handling of STV_PROTECTED to make function
  661. pointer comparisons work properly. The PLT addresses are never
  662. used for function pointers. */
  663. return _bfd_elf_dynamic_symbol_p (h, info, 0);
  664. }
  665. static int
  666. property_table_compare (const void *ap, const void *bp)
  667. {
  668. const property_table_entry *a = (const property_table_entry *) ap;
  669. const property_table_entry *b = (const property_table_entry *) bp;
  670. if (a->address == b->address)
  671. {
  672. if (a->size != b->size)
  673. return (a->size - b->size);
  674. if ((a->flags & XTENSA_PROP_ALIGN) != (b->flags & XTENSA_PROP_ALIGN))
  675. return ((b->flags & XTENSA_PROP_ALIGN)
  676. - (a->flags & XTENSA_PROP_ALIGN));
  677. if ((a->flags & XTENSA_PROP_ALIGN)
  678. && (GET_XTENSA_PROP_ALIGNMENT (a->flags)
  679. != GET_XTENSA_PROP_ALIGNMENT (b->flags)))
  680. return (GET_XTENSA_PROP_ALIGNMENT (a->flags)
  681. - GET_XTENSA_PROP_ALIGNMENT (b->flags));
  682. if ((a->flags & XTENSA_PROP_UNREACHABLE)
  683. != (b->flags & XTENSA_PROP_UNREACHABLE))
  684. return ((b->flags & XTENSA_PROP_UNREACHABLE)
  685. - (a->flags & XTENSA_PROP_UNREACHABLE));
  686. return (a->flags - b->flags);
  687. }
  688. return (a->address - b->address);
  689. }
  690. static int
  691. property_table_matches (const void *ap, const void *bp)
  692. {
  693. const property_table_entry *a = (const property_table_entry *) ap;
  694. const property_table_entry *b = (const property_table_entry *) bp;
  695. /* Check if one entry overlaps with the other. */
  696. if ((b->address >= a->address && b->address < (a->address + a->size))
  697. || (a->address >= b->address && a->address < (b->address + b->size)))
  698. return 0;
  699. return (a->address - b->address);
  700. }
  701. /* Get the literal table or property table entries for the given
  702. section. Sets TABLE_P and returns the number of entries. On
  703. error, returns a negative value. */
  704. int
  705. xtensa_read_table_entries (bfd *abfd,
  706. asection *section,
  707. property_table_entry **table_p,
  708. const char *sec_name,
  709. bool output_addr)
  710. {
  711. asection *table_section;
  712. bfd_size_type table_size = 0;
  713. bfd_byte *table_data;
  714. property_table_entry *blocks;
  715. int blk, block_count;
  716. bfd_size_type num_records;
  717. Elf_Internal_Rela *internal_relocs, *irel, *rel_end;
  718. bfd_vma section_addr, off;
  719. flagword predef_flags;
  720. bfd_size_type table_entry_size, section_limit;
  721. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
  722. || !section
  723. || !(section->flags & SEC_ALLOC)
  724. || (section->flags & SEC_DEBUGGING))
  725. {
  726. *table_p = NULL;
  727. return 0;
  728. }
  729. table_section = xtensa_get_property_section (section, sec_name);
  730. if (table_section)
  731. table_size = table_section->size;
  732. if (table_size == 0)
  733. {
  734. *table_p = NULL;
  735. return 0;
  736. }
  737. predef_flags = xtensa_get_property_predef_flags (table_section);
  738. table_entry_size = 12;
  739. if (predef_flags)
  740. table_entry_size -= 4;
  741. num_records = table_size / table_entry_size;
  742. table_data = retrieve_contents (abfd, table_section, true);
  743. if (table_data == NULL)
  744. {
  745. *table_p = NULL;
  746. return 0;
  747. }
  748. blocks = (property_table_entry *)
  749. bfd_malloc (num_records * sizeof (property_table_entry));
  750. block_count = 0;
  751. if (output_addr)
  752. section_addr = section->output_section->vma + section->output_offset;
  753. else
  754. section_addr = section->vma;
  755. internal_relocs = retrieve_internal_relocs (abfd, table_section, true);
  756. if (internal_relocs && !table_section->reloc_done)
  757. {
  758. qsort (internal_relocs, table_section->reloc_count,
  759. sizeof (Elf_Internal_Rela), internal_reloc_compare);
  760. irel = internal_relocs;
  761. }
  762. else
  763. irel = NULL;
  764. section_limit = bfd_get_section_limit (abfd, section);
  765. rel_end = internal_relocs + table_section->reloc_count;
  766. for (off = 0; off < table_size; off += table_entry_size)
  767. {
  768. bfd_vma address = bfd_get_32 (abfd, table_data + off);
  769. /* Skip any relocations before the current offset. This should help
  770. avoid confusion caused by unexpected relocations for the preceding
  771. table entry. */
  772. while (irel &&
  773. (irel->r_offset < off
  774. || (irel->r_offset == off
  775. && ELF32_R_TYPE (irel->r_info) == R_XTENSA_NONE)))
  776. {
  777. irel += 1;
  778. if (irel >= rel_end)
  779. irel = 0;
  780. }
  781. if (irel && irel->r_offset == off)
  782. {
  783. bfd_vma sym_off;
  784. unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
  785. BFD_ASSERT (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32);
  786. if (get_elf_r_symndx_section (abfd, r_symndx) != section)
  787. continue;
  788. sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
  789. BFD_ASSERT (sym_off == 0);
  790. address += (section_addr + sym_off + irel->r_addend);
  791. }
  792. else
  793. {
  794. if (address < section_addr
  795. || address >= section_addr + section_limit)
  796. continue;
  797. }
  798. blocks[block_count].address = address;
  799. blocks[block_count].size = bfd_get_32 (abfd, table_data + off + 4);
  800. if (predef_flags)
  801. blocks[block_count].flags = predef_flags;
  802. else
  803. blocks[block_count].flags = bfd_get_32 (abfd, table_data + off + 8);
  804. block_count++;
  805. }
  806. release_contents (table_section, table_data);
  807. release_internal_relocs (table_section, internal_relocs);
  808. if (block_count > 0)
  809. {
  810. /* Now sort them into address order for easy reference. */
  811. qsort (blocks, block_count, sizeof (property_table_entry),
  812. property_table_compare);
  813. /* Check that the table contents are valid. Problems may occur,
  814. for example, if an unrelocated object file is stripped. */
  815. for (blk = 1; blk < block_count; blk++)
  816. {
  817. /* The only circumstance where two entries may legitimately
  818. have the same address is when one of them is a zero-size
  819. placeholder to mark a place where fill can be inserted.
  820. The zero-size entry should come first. */
  821. if (blocks[blk - 1].address == blocks[blk].address &&
  822. blocks[blk - 1].size != 0)
  823. {
  824. /* xgettext:c-format */
  825. _bfd_error_handler (_("%pB(%pA): invalid property table"),
  826. abfd, section);
  827. bfd_set_error (bfd_error_bad_value);
  828. free (blocks);
  829. return -1;
  830. }
  831. }
  832. }
  833. *table_p = blocks;
  834. return block_count;
  835. }
  836. static property_table_entry *
  837. elf_xtensa_find_property_entry (property_table_entry *property_table,
  838. int property_table_size,
  839. bfd_vma addr)
  840. {
  841. property_table_entry entry;
  842. property_table_entry *rv;
  843. if (property_table_size == 0)
  844. return NULL;
  845. entry.address = addr;
  846. entry.size = 1;
  847. entry.flags = 0;
  848. rv = bsearch (&entry, property_table, property_table_size,
  849. sizeof (property_table_entry), property_table_matches);
  850. return rv;
  851. }
  852. static bool
  853. elf_xtensa_in_literal_pool (property_table_entry *lit_table,
  854. int lit_table_size,
  855. bfd_vma addr)
  856. {
  857. if (elf_xtensa_find_property_entry (lit_table, lit_table_size, addr))
  858. return true;
  859. return false;
  860. }
  861. /* Look through the relocs for a section during the first phase, and
  862. calculate needed space in the dynamic reloc sections. */
  863. static bool
  864. elf_xtensa_check_relocs (bfd *abfd,
  865. struct bfd_link_info *info,
  866. asection *sec,
  867. const Elf_Internal_Rela *relocs)
  868. {
  869. struct elf_xtensa_link_hash_table *htab;
  870. Elf_Internal_Shdr *symtab_hdr;
  871. struct elf_link_hash_entry **sym_hashes;
  872. const Elf_Internal_Rela *rel;
  873. const Elf_Internal_Rela *rel_end;
  874. if (bfd_link_relocatable (info))
  875. return true;
  876. BFD_ASSERT (is_xtensa_elf (abfd));
  877. htab = elf_xtensa_hash_table (info);
  878. if (htab == NULL)
  879. return false;
  880. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  881. sym_hashes = elf_sym_hashes (abfd);
  882. rel_end = relocs + sec->reloc_count;
  883. for (rel = relocs; rel < rel_end; rel++)
  884. {
  885. unsigned int r_type;
  886. unsigned r_symndx;
  887. struct elf_link_hash_entry *h = NULL;
  888. struct elf_xtensa_link_hash_entry *eh;
  889. int tls_type, old_tls_type;
  890. bool is_got = false;
  891. bool is_plt = false;
  892. bool is_tlsfunc = false;
  893. r_symndx = ELF32_R_SYM (rel->r_info);
  894. r_type = ELF32_R_TYPE (rel->r_info);
  895. if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
  896. {
  897. /* xgettext:c-format */
  898. _bfd_error_handler (_("%pB: bad symbol index: %d"),
  899. abfd, r_symndx);
  900. return false;
  901. }
  902. if (r_symndx >= symtab_hdr->sh_info)
  903. {
  904. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  905. while (h->root.type == bfd_link_hash_indirect
  906. || h->root.type == bfd_link_hash_warning)
  907. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  908. }
  909. eh = elf_xtensa_hash_entry (h);
  910. switch (r_type)
  911. {
  912. case R_XTENSA_TLSDESC_FN:
  913. if (bfd_link_pic (info))
  914. {
  915. tls_type = GOT_TLS_GD;
  916. is_got = true;
  917. is_tlsfunc = true;
  918. }
  919. else
  920. tls_type = GOT_TLS_IE;
  921. break;
  922. case R_XTENSA_TLSDESC_ARG:
  923. if (bfd_link_pic (info))
  924. {
  925. tls_type = GOT_TLS_GD;
  926. is_got = true;
  927. }
  928. else
  929. {
  930. tls_type = GOT_TLS_IE;
  931. if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
  932. is_got = true;
  933. }
  934. break;
  935. case R_XTENSA_TLS_DTPOFF:
  936. if (bfd_link_pic (info))
  937. tls_type = GOT_TLS_GD;
  938. else
  939. tls_type = GOT_TLS_IE;
  940. break;
  941. case R_XTENSA_TLS_TPOFF:
  942. tls_type = GOT_TLS_IE;
  943. if (bfd_link_pic (info))
  944. info->flags |= DF_STATIC_TLS;
  945. if (bfd_link_pic (info) || h)
  946. is_got = true;
  947. break;
  948. case R_XTENSA_32:
  949. tls_type = GOT_NORMAL;
  950. is_got = true;
  951. break;
  952. case R_XTENSA_PLT:
  953. tls_type = GOT_NORMAL;
  954. is_plt = true;
  955. break;
  956. case R_XTENSA_GNU_VTINHERIT:
  957. /* This relocation describes the C++ object vtable hierarchy.
  958. Reconstruct it for later use during GC. */
  959. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  960. return false;
  961. continue;
  962. case R_XTENSA_GNU_VTENTRY:
  963. /* This relocation describes which C++ vtable entries are actually
  964. used. Record for later use during GC. */
  965. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  966. return false;
  967. continue;
  968. default:
  969. /* Nothing to do for any other relocations. */
  970. continue;
  971. }
  972. if (h)
  973. {
  974. if (is_plt)
  975. {
  976. if (h->plt.refcount <= 0)
  977. {
  978. h->needs_plt = 1;
  979. h->plt.refcount = 1;
  980. }
  981. else
  982. h->plt.refcount += 1;
  983. /* Keep track of the total PLT relocation count even if we
  984. don't yet know whether the dynamic sections will be
  985. created. */
  986. htab->plt_reloc_count += 1;
  987. if (elf_hash_table (info)->dynamic_sections_created)
  988. {
  989. if (! add_extra_plt_sections (info, htab->plt_reloc_count))
  990. return false;
  991. }
  992. }
  993. else if (is_got)
  994. {
  995. if (h->got.refcount <= 0)
  996. h->got.refcount = 1;
  997. else
  998. h->got.refcount += 1;
  999. }
  1000. if (is_tlsfunc)
  1001. eh->tlsfunc_refcount += 1;
  1002. old_tls_type = eh->tls_type;
  1003. }
  1004. else
  1005. {
  1006. /* Allocate storage the first time. */
  1007. if (elf_local_got_refcounts (abfd) == NULL)
  1008. {
  1009. bfd_size_type size = symtab_hdr->sh_info;
  1010. void *mem;
  1011. mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
  1012. if (mem == NULL)
  1013. return false;
  1014. elf_local_got_refcounts (abfd) = (bfd_signed_vma *) mem;
  1015. mem = bfd_zalloc (abfd, size);
  1016. if (mem == NULL)
  1017. return false;
  1018. elf_xtensa_local_got_tls_type (abfd) = (char *) mem;
  1019. mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
  1020. if (mem == NULL)
  1021. return false;
  1022. elf_xtensa_local_tlsfunc_refcounts (abfd)
  1023. = (bfd_signed_vma *) mem;
  1024. }
  1025. /* This is a global offset table entry for a local symbol. */
  1026. if (is_got || is_plt)
  1027. elf_local_got_refcounts (abfd) [r_symndx] += 1;
  1028. if (is_tlsfunc)
  1029. elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx] += 1;
  1030. old_tls_type = elf_xtensa_local_got_tls_type (abfd) [r_symndx];
  1031. }
  1032. if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
  1033. tls_type |= old_tls_type;
  1034. /* If a TLS symbol is accessed using IE at least once,
  1035. there is no point to use a dynamic model for it. */
  1036. else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
  1037. && ((old_tls_type & GOT_TLS_GD) == 0
  1038. || (tls_type & GOT_TLS_IE) == 0))
  1039. {
  1040. if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GD))
  1041. tls_type = old_tls_type;
  1042. else if ((old_tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_GD))
  1043. tls_type |= old_tls_type;
  1044. else
  1045. {
  1046. _bfd_error_handler
  1047. /* xgettext:c-format */
  1048. (_("%pB: `%s' accessed both as normal and thread local symbol"),
  1049. abfd,
  1050. h ? h->root.root.string : "<local>");
  1051. return false;
  1052. }
  1053. }
  1054. if (old_tls_type != tls_type)
  1055. {
  1056. if (eh)
  1057. eh->tls_type = tls_type;
  1058. else
  1059. elf_xtensa_local_got_tls_type (abfd) [r_symndx] = tls_type;
  1060. }
  1061. }
  1062. return true;
  1063. }
  1064. static void
  1065. elf_xtensa_make_sym_local (struct bfd_link_info *info,
  1066. struct elf_link_hash_entry *h)
  1067. {
  1068. if (bfd_link_pic (info))
  1069. {
  1070. if (h->plt.refcount > 0)
  1071. {
  1072. /* For shared objects, there's no need for PLT entries for local
  1073. symbols (use RELATIVE relocs instead of JMP_SLOT relocs). */
  1074. if (h->got.refcount < 0)
  1075. h->got.refcount = 0;
  1076. h->got.refcount += h->plt.refcount;
  1077. h->plt.refcount = 0;
  1078. }
  1079. }
  1080. else
  1081. {
  1082. /* Don't need any dynamic relocations at all. */
  1083. h->plt.refcount = 0;
  1084. h->got.refcount = 0;
  1085. }
  1086. }
  1087. static void
  1088. elf_xtensa_hide_symbol (struct bfd_link_info *info,
  1089. struct elf_link_hash_entry *h,
  1090. bool force_local)
  1091. {
  1092. /* For a shared link, move the plt refcount to the got refcount to leave
  1093. space for RELATIVE relocs. */
  1094. elf_xtensa_make_sym_local (info, h);
  1095. _bfd_elf_link_hash_hide_symbol (info, h, force_local);
  1096. }
  1097. /* Return the section that should be marked against GC for a given
  1098. relocation. */
  1099. static asection *
  1100. elf_xtensa_gc_mark_hook (asection *sec,
  1101. struct bfd_link_info *info,
  1102. Elf_Internal_Rela *rel,
  1103. struct elf_link_hash_entry *h,
  1104. Elf_Internal_Sym *sym)
  1105. {
  1106. /* Property sections are marked "KEEP" in the linker scripts, but they
  1107. should not cause other sections to be marked. (This approach relies
  1108. on elf_xtensa_discard_info to remove property table entries that
  1109. describe discarded sections. Alternatively, it might be more
  1110. efficient to avoid using "KEEP" in the linker scripts and instead use
  1111. the gc_mark_extra_sections hook to mark only the property sections
  1112. that describe marked sections. That alternative does not work well
  1113. with the current property table sections, which do not correspond
  1114. one-to-one with the sections they describe, but that should be fixed
  1115. someday.) */
  1116. if (xtensa_is_property_section (sec))
  1117. return NULL;
  1118. if (h != NULL)
  1119. switch (ELF32_R_TYPE (rel->r_info))
  1120. {
  1121. case R_XTENSA_GNU_VTINHERIT:
  1122. case R_XTENSA_GNU_VTENTRY:
  1123. return NULL;
  1124. }
  1125. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1126. }
  1127. /* Create all the dynamic sections. */
  1128. static bool
  1129. elf_xtensa_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
  1130. {
  1131. struct elf_xtensa_link_hash_table *htab;
  1132. flagword flags, noalloc_flags;
  1133. htab = elf_xtensa_hash_table (info);
  1134. if (htab == NULL)
  1135. return false;
  1136. /* First do all the standard stuff. */
  1137. if (! _bfd_elf_create_dynamic_sections (dynobj, info))
  1138. return false;
  1139. /* Create any extra PLT sections in case check_relocs has already
  1140. been called on all the non-dynamic input files. */
  1141. if (! add_extra_plt_sections (info, htab->plt_reloc_count))
  1142. return false;
  1143. noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1144. | SEC_LINKER_CREATED | SEC_READONLY);
  1145. flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
  1146. /* Mark the ".got.plt" section READONLY. */
  1147. if (htab->elf.sgotplt == NULL
  1148. || !bfd_set_section_flags (htab->elf.sgotplt, flags))
  1149. return false;
  1150. /* Create ".got.loc" (literal tables for use by dynamic linker). */
  1151. htab->sgotloc = bfd_make_section_anyway_with_flags (dynobj, ".got.loc",
  1152. flags);
  1153. if (htab->sgotloc == NULL
  1154. || !bfd_set_section_alignment (htab->sgotloc, 2))
  1155. return false;
  1156. /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
  1157. htab->spltlittbl = bfd_make_section_anyway_with_flags (dynobj, ".xt.lit.plt",
  1158. noalloc_flags);
  1159. if (htab->spltlittbl == NULL
  1160. || !bfd_set_section_alignment (htab->spltlittbl, 2))
  1161. return false;
  1162. return true;
  1163. }
  1164. static bool
  1165. add_extra_plt_sections (struct bfd_link_info *info, int count)
  1166. {
  1167. bfd *dynobj = elf_hash_table (info)->dynobj;
  1168. int chunk;
  1169. /* Iterate over all chunks except 0 which uses the standard ".plt" and
  1170. ".got.plt" sections. */
  1171. for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
  1172. {
  1173. char *sname;
  1174. flagword flags;
  1175. asection *s;
  1176. /* Stop when we find a section has already been created. */
  1177. if (elf_xtensa_get_plt_section (info, chunk))
  1178. break;
  1179. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1180. | SEC_LINKER_CREATED | SEC_READONLY);
  1181. sname = (char *) bfd_malloc (10);
  1182. sprintf (sname, ".plt.%u", chunk);
  1183. s = bfd_make_section_anyway_with_flags (dynobj, sname, flags | SEC_CODE);
  1184. if (s == NULL
  1185. || !bfd_set_section_alignment (s, 2))
  1186. return false;
  1187. sname = (char *) bfd_malloc (14);
  1188. sprintf (sname, ".got.plt.%u", chunk);
  1189. s = bfd_make_section_anyway_with_flags (dynobj, sname, flags);
  1190. if (s == NULL
  1191. || !bfd_set_section_alignment (s, 2))
  1192. return false;
  1193. }
  1194. return true;
  1195. }
  1196. /* Adjust a symbol defined by a dynamic object and referenced by a
  1197. regular object. The current definition is in some section of the
  1198. dynamic object, but we're not including those sections. We have to
  1199. change the definition to something the rest of the link can
  1200. understand. */
  1201. static bool
  1202. elf_xtensa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1203. struct elf_link_hash_entry *h)
  1204. {
  1205. /* If this is a weak symbol, and there is a real definition, the
  1206. processor independent code will have arranged for us to see the
  1207. real definition first, and we can just use the same value. */
  1208. if (h->is_weakalias)
  1209. {
  1210. struct elf_link_hash_entry *def = weakdef (h);
  1211. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  1212. h->root.u.def.section = def->root.u.def.section;
  1213. h->root.u.def.value = def->root.u.def.value;
  1214. return true;
  1215. }
  1216. /* This is a reference to a symbol defined by a dynamic object. The
  1217. reference must go through the GOT, so there's no need for COPY relocs,
  1218. .dynbss, etc. */
  1219. return true;
  1220. }
  1221. static bool
  1222. elf_xtensa_allocate_dynrelocs (struct elf_link_hash_entry *h, void *arg)
  1223. {
  1224. struct bfd_link_info *info;
  1225. struct elf_xtensa_link_hash_table *htab;
  1226. struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (h);
  1227. if (h->root.type == bfd_link_hash_indirect)
  1228. return true;
  1229. info = (struct bfd_link_info *) arg;
  1230. htab = elf_xtensa_hash_table (info);
  1231. if (htab == NULL)
  1232. return false;
  1233. /* If we saw any use of an IE model for this symbol, we can then optimize
  1234. away GOT entries for any TLSDESC_FN relocs. */
  1235. if ((eh->tls_type & GOT_TLS_IE) != 0)
  1236. {
  1237. BFD_ASSERT (h->got.refcount >= eh->tlsfunc_refcount);
  1238. h->got.refcount -= eh->tlsfunc_refcount;
  1239. }
  1240. if (! elf_xtensa_dynamic_symbol_p (h, info))
  1241. elf_xtensa_make_sym_local (info, h);
  1242. if (! elf_xtensa_dynamic_symbol_p (h, info)
  1243. && h->root.type == bfd_link_hash_undefweak)
  1244. return true;
  1245. if (h->plt.refcount > 0)
  1246. htab->elf.srelplt->size += (h->plt.refcount * sizeof (Elf32_External_Rela));
  1247. if (h->got.refcount > 0)
  1248. htab->elf.srelgot->size += (h->got.refcount * sizeof (Elf32_External_Rela));
  1249. return true;
  1250. }
  1251. static void
  1252. elf_xtensa_allocate_local_got_size (struct bfd_link_info *info)
  1253. {
  1254. struct elf_xtensa_link_hash_table *htab;
  1255. bfd *i;
  1256. htab = elf_xtensa_hash_table (info);
  1257. if (htab == NULL)
  1258. return;
  1259. for (i = info->input_bfds; i; i = i->link.next)
  1260. {
  1261. bfd_signed_vma *local_got_refcounts;
  1262. bfd_size_type j, cnt;
  1263. Elf_Internal_Shdr *symtab_hdr;
  1264. local_got_refcounts = elf_local_got_refcounts (i);
  1265. if (!local_got_refcounts)
  1266. continue;
  1267. symtab_hdr = &elf_tdata (i)->symtab_hdr;
  1268. cnt = symtab_hdr->sh_info;
  1269. for (j = 0; j < cnt; ++j)
  1270. {
  1271. /* If we saw any use of an IE model for this symbol, we can
  1272. then optimize away GOT entries for any TLSDESC_FN relocs. */
  1273. if ((elf_xtensa_local_got_tls_type (i) [j] & GOT_TLS_IE) != 0)
  1274. {
  1275. bfd_signed_vma *tlsfunc_refcount
  1276. = &elf_xtensa_local_tlsfunc_refcounts (i) [j];
  1277. BFD_ASSERT (local_got_refcounts[j] >= *tlsfunc_refcount);
  1278. local_got_refcounts[j] -= *tlsfunc_refcount;
  1279. }
  1280. if (local_got_refcounts[j] > 0)
  1281. htab->elf.srelgot->size += (local_got_refcounts[j]
  1282. * sizeof (Elf32_External_Rela));
  1283. }
  1284. }
  1285. }
  1286. /* Set the sizes of the dynamic sections. */
  1287. static bool
  1288. elf_xtensa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  1289. struct bfd_link_info *info)
  1290. {
  1291. struct elf_xtensa_link_hash_table *htab;
  1292. bfd *dynobj, *abfd;
  1293. asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
  1294. bool relplt, relgot;
  1295. int plt_entries, plt_chunks, chunk;
  1296. plt_entries = 0;
  1297. plt_chunks = 0;
  1298. htab = elf_xtensa_hash_table (info);
  1299. if (htab == NULL)
  1300. return false;
  1301. dynobj = elf_hash_table (info)->dynobj;
  1302. if (dynobj == NULL)
  1303. abort ();
  1304. srelgot = htab->elf.srelgot;
  1305. srelplt = htab->elf.srelplt;
  1306. if (elf_hash_table (info)->dynamic_sections_created)
  1307. {
  1308. BFD_ASSERT (htab->elf.srelgot != NULL
  1309. && htab->elf.srelplt != NULL
  1310. && htab->elf.sgot != NULL
  1311. && htab->spltlittbl != NULL
  1312. && htab->sgotloc != NULL);
  1313. /* Set the contents of the .interp section to the interpreter. */
  1314. if (bfd_link_executable (info) && !info->nointerp)
  1315. {
  1316. s = bfd_get_linker_section (dynobj, ".interp");
  1317. if (s == NULL)
  1318. abort ();
  1319. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  1320. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  1321. }
  1322. /* Allocate room for one word in ".got". */
  1323. htab->elf.sgot->size = 4;
  1324. /* Allocate space in ".rela.got" for literals that reference global
  1325. symbols and space in ".rela.plt" for literals that have PLT
  1326. entries. */
  1327. elf_link_hash_traverse (elf_hash_table (info),
  1328. elf_xtensa_allocate_dynrelocs,
  1329. (void *) info);
  1330. /* If we are generating a shared object, we also need space in
  1331. ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
  1332. reference local symbols. */
  1333. if (bfd_link_pic (info))
  1334. elf_xtensa_allocate_local_got_size (info);
  1335. /* Allocate space in ".plt" to match the size of ".rela.plt". For
  1336. each PLT entry, we need the PLT code plus a 4-byte literal.
  1337. For each chunk of ".plt", we also need two more 4-byte
  1338. literals, two corresponding entries in ".rela.got", and an
  1339. 8-byte entry in ".xt.lit.plt". */
  1340. spltlittbl = htab->spltlittbl;
  1341. plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
  1342. plt_chunks =
  1343. (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
  1344. /* Iterate over all the PLT chunks, including any extra sections
  1345. created earlier because the initial count of PLT relocations
  1346. was an overestimate. */
  1347. for (chunk = 0;
  1348. (splt = elf_xtensa_get_plt_section (info, chunk)) != NULL;
  1349. chunk++)
  1350. {
  1351. int chunk_entries;
  1352. sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
  1353. BFD_ASSERT (sgotplt != NULL);
  1354. if (chunk < plt_chunks - 1)
  1355. chunk_entries = PLT_ENTRIES_PER_CHUNK;
  1356. else if (chunk == plt_chunks - 1)
  1357. chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
  1358. else
  1359. chunk_entries = 0;
  1360. if (chunk_entries != 0)
  1361. {
  1362. sgotplt->size = 4 * (chunk_entries + 2);
  1363. splt->size = PLT_ENTRY_SIZE * chunk_entries;
  1364. srelgot->size += 2 * sizeof (Elf32_External_Rela);
  1365. spltlittbl->size += 8;
  1366. }
  1367. else
  1368. {
  1369. sgotplt->size = 0;
  1370. splt->size = 0;
  1371. }
  1372. }
  1373. /* Allocate space in ".got.loc" to match the total size of all the
  1374. literal tables. */
  1375. sgotloc = htab->sgotloc;
  1376. sgotloc->size = spltlittbl->size;
  1377. for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  1378. {
  1379. if (abfd->flags & DYNAMIC)
  1380. continue;
  1381. for (s = abfd->sections; s != NULL; s = s->next)
  1382. {
  1383. if (! discarded_section (s)
  1384. && xtensa_is_littable_section (s)
  1385. && s != spltlittbl)
  1386. sgotloc->size += s->size;
  1387. }
  1388. }
  1389. }
  1390. /* Allocate memory for dynamic sections. */
  1391. relplt = false;
  1392. relgot = false;
  1393. for (s = dynobj->sections; s != NULL; s = s->next)
  1394. {
  1395. const char *name;
  1396. if ((s->flags & SEC_LINKER_CREATED) == 0)
  1397. continue;
  1398. /* It's OK to base decisions on the section name, because none
  1399. of the dynobj section names depend upon the input files. */
  1400. name = bfd_section_name (s);
  1401. if (startswith (name, ".rela"))
  1402. {
  1403. if (s->size != 0)
  1404. {
  1405. if (strcmp (name, ".rela.plt") == 0)
  1406. relplt = true;
  1407. else if (strcmp (name, ".rela.got") == 0)
  1408. relgot = true;
  1409. /* We use the reloc_count field as a counter if we need
  1410. to copy relocs into the output file. */
  1411. s->reloc_count = 0;
  1412. }
  1413. }
  1414. else if (! startswith (name, ".plt.")
  1415. && ! startswith (name, ".got.plt.")
  1416. && strcmp (name, ".got") != 0
  1417. && strcmp (name, ".plt") != 0
  1418. && strcmp (name, ".got.plt") != 0
  1419. && strcmp (name, ".xt.lit.plt") != 0
  1420. && strcmp (name, ".got.loc") != 0)
  1421. {
  1422. /* It's not one of our sections, so don't allocate space. */
  1423. continue;
  1424. }
  1425. if (s->size == 0)
  1426. {
  1427. /* If we don't need this section, strip it from the output
  1428. file. We must create the ".plt*" and ".got.plt*"
  1429. sections in create_dynamic_sections and/or check_relocs
  1430. based on a conservative estimate of the PLT relocation
  1431. count, because the sections must be created before the
  1432. linker maps input sections to output sections. The
  1433. linker does that before size_dynamic_sections, where we
  1434. compute the exact size of the PLT, so there may be more
  1435. of these sections than are actually needed. */
  1436. s->flags |= SEC_EXCLUDE;
  1437. }
  1438. else if ((s->flags & SEC_HAS_CONTENTS) != 0)
  1439. {
  1440. /* Allocate memory for the section contents. */
  1441. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  1442. if (s->contents == NULL)
  1443. return false;
  1444. }
  1445. }
  1446. if (elf_hash_table (info)->dynamic_sections_created)
  1447. {
  1448. /* Add the special XTENSA_RTLD relocations now. The offsets won't be
  1449. known until finish_dynamic_sections, but we need to get the relocs
  1450. in place before they are sorted. */
  1451. for (chunk = 0; chunk < plt_chunks; chunk++)
  1452. {
  1453. Elf_Internal_Rela irela;
  1454. bfd_byte *loc;
  1455. irela.r_offset = 0;
  1456. irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
  1457. irela.r_addend = 0;
  1458. loc = (srelgot->contents
  1459. + srelgot->reloc_count * sizeof (Elf32_External_Rela));
  1460. bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
  1461. bfd_elf32_swap_reloca_out (output_bfd, &irela,
  1462. loc + sizeof (Elf32_External_Rela));
  1463. srelgot->reloc_count += 2;
  1464. }
  1465. /* Add some entries to the .dynamic section. We fill in the
  1466. values later, in elf_xtensa_finish_dynamic_sections, but we
  1467. must add the entries now so that we get the correct size for
  1468. the .dynamic section. The DT_DEBUG entry is filled in by the
  1469. dynamic linker and used by the debugger. */
  1470. #define add_dynamic_entry(TAG, VAL) \
  1471. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  1472. if (!_bfd_elf_add_dynamic_tags (output_bfd, info,
  1473. relplt || relgot))
  1474. return false;
  1475. if (!add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
  1476. || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
  1477. return false;
  1478. }
  1479. #undef add_dynamic_entry
  1480. return true;
  1481. }
  1482. static bool
  1483. elf_xtensa_always_size_sections (bfd *output_bfd,
  1484. struct bfd_link_info *info)
  1485. {
  1486. struct elf_xtensa_link_hash_table *htab;
  1487. asection *tls_sec;
  1488. htab = elf_xtensa_hash_table (info);
  1489. if (htab == NULL)
  1490. return false;
  1491. tls_sec = htab->elf.tls_sec;
  1492. if (tls_sec && (htab->tlsbase->tls_type & GOT_TLS_ANY) != 0)
  1493. {
  1494. struct elf_link_hash_entry *tlsbase = &htab->tlsbase->elf;
  1495. struct bfd_link_hash_entry *bh = &tlsbase->root;
  1496. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  1497. tlsbase->type = STT_TLS;
  1498. if (!(_bfd_generic_link_add_one_symbol
  1499. (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
  1500. tls_sec, 0, NULL, false,
  1501. bed->collect, &bh)))
  1502. return false;
  1503. tlsbase->def_regular = 1;
  1504. tlsbase->other = STV_HIDDEN;
  1505. (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
  1506. }
  1507. return true;
  1508. }
  1509. /* Return the base VMA address which should be subtracted from real addresses
  1510. when resolving @dtpoff relocation.
  1511. This is PT_TLS segment p_vaddr. */
  1512. static bfd_vma
  1513. dtpoff_base (struct bfd_link_info *info)
  1514. {
  1515. /* If tls_sec is NULL, we should have signalled an error already. */
  1516. if (elf_hash_table (info)->tls_sec == NULL)
  1517. return 0;
  1518. return elf_hash_table (info)->tls_sec->vma;
  1519. }
  1520. /* Return the relocation value for @tpoff relocation
  1521. if STT_TLS virtual address is ADDRESS. */
  1522. static bfd_vma
  1523. tpoff (struct bfd_link_info *info, bfd_vma address)
  1524. {
  1525. struct elf_link_hash_table *htab = elf_hash_table (info);
  1526. bfd_vma base;
  1527. /* If tls_sec is NULL, we should have signalled an error already. */
  1528. if (htab->tls_sec == NULL)
  1529. return 0;
  1530. base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
  1531. return address - htab->tls_sec->vma + base;
  1532. }
  1533. /* Perform the specified relocation. The instruction at (contents + address)
  1534. is modified to set one operand to represent the value in "relocation". The
  1535. operand position is determined by the relocation type recorded in the
  1536. howto. */
  1537. #define CALL_SEGMENT_BITS (30)
  1538. #define CALL_SEGMENT_SIZE (1 << CALL_SEGMENT_BITS)
  1539. static bfd_reloc_status_type
  1540. elf_xtensa_do_reloc (reloc_howto_type *howto,
  1541. bfd *abfd,
  1542. asection *input_section,
  1543. bfd_vma relocation,
  1544. bfd_byte *contents,
  1545. bfd_vma address,
  1546. bool is_weak_undef,
  1547. char **error_message)
  1548. {
  1549. xtensa_format fmt;
  1550. xtensa_opcode opcode;
  1551. xtensa_isa isa = xtensa_default_isa;
  1552. static xtensa_insnbuf ibuff = NULL;
  1553. static xtensa_insnbuf sbuff = NULL;
  1554. bfd_vma self_address;
  1555. bfd_size_type input_size;
  1556. int opnd, slot;
  1557. uint32 newval;
  1558. if (!ibuff)
  1559. {
  1560. ibuff = xtensa_insnbuf_alloc (isa);
  1561. sbuff = xtensa_insnbuf_alloc (isa);
  1562. }
  1563. input_size = bfd_get_section_limit (abfd, input_section);
  1564. /* Calculate the PC address for this instruction. */
  1565. self_address = (input_section->output_section->vma
  1566. + input_section->output_offset
  1567. + address);
  1568. switch (howto->type)
  1569. {
  1570. case R_XTENSA_NONE:
  1571. case R_XTENSA_DIFF8:
  1572. case R_XTENSA_DIFF16:
  1573. case R_XTENSA_DIFF32:
  1574. case R_XTENSA_PDIFF8:
  1575. case R_XTENSA_PDIFF16:
  1576. case R_XTENSA_PDIFF32:
  1577. case R_XTENSA_NDIFF8:
  1578. case R_XTENSA_NDIFF16:
  1579. case R_XTENSA_NDIFF32:
  1580. case R_XTENSA_TLS_FUNC:
  1581. case R_XTENSA_TLS_ARG:
  1582. case R_XTENSA_TLS_CALL:
  1583. return bfd_reloc_ok;
  1584. case R_XTENSA_ASM_EXPAND:
  1585. if (!is_weak_undef)
  1586. {
  1587. /* Check for windowed CALL across a 1GB boundary. */
  1588. opcode = get_expanded_call_opcode (contents + address,
  1589. input_size - address, 0);
  1590. if (is_windowed_call_opcode (opcode))
  1591. {
  1592. if ((self_address >> CALL_SEGMENT_BITS)
  1593. != (relocation >> CALL_SEGMENT_BITS))
  1594. {
  1595. *error_message = "windowed longcall crosses 1GB boundary; "
  1596. "return may fail";
  1597. return bfd_reloc_dangerous;
  1598. }
  1599. }
  1600. }
  1601. return bfd_reloc_ok;
  1602. case R_XTENSA_ASM_SIMPLIFY:
  1603. {
  1604. /* Convert the L32R/CALLX to CALL. */
  1605. bfd_reloc_status_type retval =
  1606. elf_xtensa_do_asm_simplify (contents, address, input_size,
  1607. error_message);
  1608. if (retval != bfd_reloc_ok)
  1609. return bfd_reloc_dangerous;
  1610. /* The CALL needs to be relocated. Continue below for that part. */
  1611. address += 3;
  1612. self_address += 3;
  1613. howto = &elf_howto_table[(unsigned) R_XTENSA_SLOT0_OP ];
  1614. }
  1615. break;
  1616. case R_XTENSA_32:
  1617. {
  1618. bfd_vma x;
  1619. x = bfd_get_32 (abfd, contents + address);
  1620. x = x + relocation;
  1621. bfd_put_32 (abfd, x, contents + address);
  1622. }
  1623. return bfd_reloc_ok;
  1624. case R_XTENSA_32_PCREL:
  1625. bfd_put_32 (abfd, relocation - self_address, contents + address);
  1626. return bfd_reloc_ok;
  1627. case R_XTENSA_PLT:
  1628. case R_XTENSA_TLSDESC_FN:
  1629. case R_XTENSA_TLSDESC_ARG:
  1630. case R_XTENSA_TLS_DTPOFF:
  1631. case R_XTENSA_TLS_TPOFF:
  1632. bfd_put_32 (abfd, relocation, contents + address);
  1633. return bfd_reloc_ok;
  1634. }
  1635. /* Only instruction slot-specific relocations handled below.... */
  1636. slot = get_relocation_slot (howto->type);
  1637. if (slot == XTENSA_UNDEFINED)
  1638. {
  1639. *error_message = "unexpected relocation";
  1640. return bfd_reloc_dangerous;
  1641. }
  1642. if (input_size <= address)
  1643. return bfd_reloc_outofrange;
  1644. /* Read the instruction into a buffer and decode the opcode. */
  1645. xtensa_insnbuf_from_chars (isa, ibuff, contents + address,
  1646. input_size - address);
  1647. fmt = xtensa_format_decode (isa, ibuff);
  1648. if (fmt == XTENSA_UNDEFINED)
  1649. {
  1650. *error_message = "cannot decode instruction format";
  1651. return bfd_reloc_dangerous;
  1652. }
  1653. xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
  1654. opcode = xtensa_opcode_decode (isa, fmt, slot, sbuff);
  1655. if (opcode == XTENSA_UNDEFINED)
  1656. {
  1657. *error_message = "cannot decode instruction opcode";
  1658. return bfd_reloc_dangerous;
  1659. }
  1660. /* Check for opcode-specific "alternate" relocations. */
  1661. if (is_alt_relocation (howto->type))
  1662. {
  1663. if (opcode == get_l32r_opcode ())
  1664. {
  1665. /* Handle the special-case of non-PC-relative L32R instructions. */
  1666. bfd *output_bfd = input_section->output_section->owner;
  1667. asection *lit4_sec = bfd_get_section_by_name (output_bfd, ".lit4");
  1668. if (!lit4_sec)
  1669. {
  1670. *error_message = "relocation references missing .lit4 section";
  1671. return bfd_reloc_dangerous;
  1672. }
  1673. self_address = ((lit4_sec->vma & ~0xfff)
  1674. + 0x40000 - 3); /* -3 to compensate for do_reloc */
  1675. newval = relocation;
  1676. opnd = 1;
  1677. }
  1678. else if (opcode == get_const16_opcode ())
  1679. {
  1680. /* ALT used for high 16 bits.
  1681. Ignore 32-bit overflow. */
  1682. newval = (relocation >> 16) & 0xffff;
  1683. opnd = 1;
  1684. }
  1685. else
  1686. {
  1687. /* No other "alternate" relocations currently defined. */
  1688. *error_message = "unexpected relocation";
  1689. return bfd_reloc_dangerous;
  1690. }
  1691. }
  1692. else /* Not an "alternate" relocation.... */
  1693. {
  1694. if (opcode == get_const16_opcode ())
  1695. {
  1696. newval = relocation & 0xffff;
  1697. opnd = 1;
  1698. }
  1699. else
  1700. {
  1701. /* ...normal PC-relative relocation.... */
  1702. /* Determine which operand is being relocated. */
  1703. opnd = get_relocation_opnd (opcode, howto->type);
  1704. if (opnd == XTENSA_UNDEFINED)
  1705. {
  1706. *error_message = "unexpected relocation";
  1707. return bfd_reloc_dangerous;
  1708. }
  1709. if (!howto->pc_relative)
  1710. {
  1711. *error_message = "expected PC-relative relocation";
  1712. return bfd_reloc_dangerous;
  1713. }
  1714. newval = relocation;
  1715. }
  1716. }
  1717. /* Apply the relocation. */
  1718. if (xtensa_operand_do_reloc (isa, opcode, opnd, &newval, self_address)
  1719. || xtensa_operand_encode (isa, opcode, opnd, &newval)
  1720. || xtensa_operand_set_field (isa, opcode, opnd, fmt, slot,
  1721. sbuff, newval))
  1722. {
  1723. const char *opname = xtensa_opcode_name (isa, opcode);
  1724. const char *msg;
  1725. msg = "cannot encode";
  1726. if (is_direct_call_opcode (opcode))
  1727. {
  1728. if ((relocation & 0x3) != 0)
  1729. msg = "misaligned call target";
  1730. else
  1731. msg = "call target out of range";
  1732. }
  1733. else if (opcode == get_l32r_opcode ())
  1734. {
  1735. if ((relocation & 0x3) != 0)
  1736. msg = "misaligned literal target";
  1737. else if (is_alt_relocation (howto->type))
  1738. msg = "literal target out of range (too many literals)";
  1739. else if (self_address > relocation)
  1740. msg = "literal target out of range (try using text-section-literals)";
  1741. else
  1742. msg = "literal placed after use";
  1743. }
  1744. *error_message = vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
  1745. return bfd_reloc_dangerous;
  1746. }
  1747. /* Check for calls across 1GB boundaries. */
  1748. if (is_direct_call_opcode (opcode)
  1749. && is_windowed_call_opcode (opcode))
  1750. {
  1751. if ((self_address >> CALL_SEGMENT_BITS)
  1752. != (relocation >> CALL_SEGMENT_BITS))
  1753. {
  1754. *error_message =
  1755. "windowed call crosses 1GB boundary; return may fail";
  1756. return bfd_reloc_dangerous;
  1757. }
  1758. }
  1759. /* Write the modified instruction back out of the buffer. */
  1760. xtensa_format_set_slot (isa, fmt, slot, ibuff, sbuff);
  1761. xtensa_insnbuf_to_chars (isa, ibuff, contents + address,
  1762. input_size - address);
  1763. return bfd_reloc_ok;
  1764. }
  1765. static char *
  1766. vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
  1767. {
  1768. /* To reduce the size of the memory leak,
  1769. we only use a single message buffer. */
  1770. static bfd_size_type alloc_size = 0;
  1771. static char *message = NULL;
  1772. bfd_size_type orig_len, len = 0;
  1773. bool is_append;
  1774. va_list ap;
  1775. va_start (ap, arglen);
  1776. is_append = (origmsg == message);
  1777. orig_len = strlen (origmsg);
  1778. len = orig_len + strlen (fmt) + arglen + 20;
  1779. if (len > alloc_size)
  1780. {
  1781. message = (char *) bfd_realloc_or_free (message, len);
  1782. alloc_size = len;
  1783. }
  1784. if (message != NULL)
  1785. {
  1786. if (!is_append)
  1787. memcpy (message, origmsg, orig_len);
  1788. vsprintf (message + orig_len, fmt, ap);
  1789. }
  1790. va_end (ap);
  1791. return message;
  1792. }
  1793. /* This function is registered as the "special_function" in the
  1794. Xtensa howto for handling simplify operations.
  1795. bfd_perform_relocation / bfd_install_relocation use it to
  1796. perform (install) the specified relocation. Since this replaces the code
  1797. in bfd_perform_relocation, it is basically an Xtensa-specific,
  1798. stripped-down version of bfd_perform_relocation. */
  1799. static bfd_reloc_status_type
  1800. bfd_elf_xtensa_reloc (bfd *abfd,
  1801. arelent *reloc_entry,
  1802. asymbol *symbol,
  1803. void *data,
  1804. asection *input_section,
  1805. bfd *output_bfd,
  1806. char **error_message)
  1807. {
  1808. bfd_vma relocation;
  1809. bfd_reloc_status_type flag;
  1810. bfd_size_type octets = (reloc_entry->address
  1811. * OCTETS_PER_BYTE (abfd, input_section));
  1812. bfd_vma output_base = 0;
  1813. reloc_howto_type *howto = reloc_entry->howto;
  1814. asection *reloc_target_output_section;
  1815. bool is_weak_undef;
  1816. if (!xtensa_default_isa)
  1817. xtensa_default_isa = xtensa_isa_init (0, 0);
  1818. /* ELF relocs are against symbols. If we are producing relocatable
  1819. output, and the reloc is against an external symbol, the resulting
  1820. reloc will also be against the same symbol. In such a case, we
  1821. don't want to change anything about the way the reloc is handled,
  1822. since it will all be done at final link time. This test is similar
  1823. to what bfd_elf_generic_reloc does except that it lets relocs with
  1824. howto->partial_inplace go through even if the addend is non-zero.
  1825. (The real problem is that partial_inplace is set for XTENSA_32
  1826. relocs to begin with, but that's a long story and there's little we
  1827. can do about it now....) */
  1828. if (output_bfd && (symbol->flags & BSF_SECTION_SYM) == 0)
  1829. {
  1830. reloc_entry->address += input_section->output_offset;
  1831. return bfd_reloc_ok;
  1832. }
  1833. /* Is the address of the relocation really within the section? */
  1834. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  1835. return bfd_reloc_outofrange;
  1836. /* Work out which section the relocation is targeted at and the
  1837. initial relocation command value. */
  1838. /* Get symbol value. (Common symbols are special.) */
  1839. if (bfd_is_com_section (symbol->section))
  1840. relocation = 0;
  1841. else
  1842. relocation = symbol->value;
  1843. reloc_target_output_section = symbol->section->output_section;
  1844. /* Convert input-section-relative symbol value to absolute. */
  1845. if ((output_bfd && !howto->partial_inplace)
  1846. || reloc_target_output_section == NULL)
  1847. output_base = 0;
  1848. else
  1849. output_base = reloc_target_output_section->vma;
  1850. relocation += output_base + symbol->section->output_offset;
  1851. /* Add in supplied addend. */
  1852. relocation += reloc_entry->addend;
  1853. /* Here the variable relocation holds the final address of the
  1854. symbol we are relocating against, plus any addend. */
  1855. if (output_bfd)
  1856. {
  1857. if (!howto->partial_inplace)
  1858. {
  1859. /* This is a partial relocation, and we want to apply the relocation
  1860. to the reloc entry rather than the raw data. Everything except
  1861. relocations against section symbols has already been handled
  1862. above. */
  1863. BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
  1864. reloc_entry->addend = relocation;
  1865. reloc_entry->address += input_section->output_offset;
  1866. return bfd_reloc_ok;
  1867. }
  1868. else
  1869. {
  1870. reloc_entry->address += input_section->output_offset;
  1871. reloc_entry->addend = 0;
  1872. }
  1873. }
  1874. is_weak_undef = (bfd_is_und_section (symbol->section)
  1875. && (symbol->flags & BSF_WEAK) != 0);
  1876. flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
  1877. (bfd_byte *) data, (bfd_vma) octets,
  1878. is_weak_undef, error_message);
  1879. if (flag == bfd_reloc_dangerous)
  1880. {
  1881. /* Add the symbol name to the error message. */
  1882. if (! *error_message)
  1883. *error_message = "";
  1884. *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
  1885. strlen (symbol->name) + 17,
  1886. symbol->name,
  1887. (unsigned long) reloc_entry->addend);
  1888. }
  1889. return flag;
  1890. }
  1891. int xtensa_abi_choice (void)
  1892. {
  1893. if (elf32xtensa_abi == XTHAL_ABI_UNDEFINED)
  1894. return XSHAL_ABI;
  1895. else
  1896. return elf32xtensa_abi;
  1897. }
  1898. /* Set up an entry in the procedure linkage table. */
  1899. static bfd_vma
  1900. elf_xtensa_create_plt_entry (struct bfd_link_info *info,
  1901. bfd *output_bfd,
  1902. unsigned reloc_index)
  1903. {
  1904. asection *splt, *sgotplt;
  1905. bfd_vma plt_base, got_base;
  1906. bfd_vma code_offset, lit_offset, abi_offset;
  1907. int chunk;
  1908. int abi = xtensa_abi_choice ();
  1909. chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
  1910. splt = elf_xtensa_get_plt_section (info, chunk);
  1911. sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
  1912. BFD_ASSERT (splt != NULL && sgotplt != NULL);
  1913. plt_base = splt->output_section->vma + splt->output_offset;
  1914. got_base = sgotplt->output_section->vma + sgotplt->output_offset;
  1915. lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
  1916. code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
  1917. /* Fill in the literal entry. This is the offset of the dynamic
  1918. relocation entry. */
  1919. bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
  1920. sgotplt->contents + lit_offset);
  1921. /* Fill in the entry in the procedure linkage table. */
  1922. memcpy (splt->contents + code_offset,
  1923. (bfd_big_endian (output_bfd)
  1924. ? elf_xtensa_be_plt_entry[abi != XTHAL_ABI_WINDOWED]
  1925. : elf_xtensa_le_plt_entry[abi != XTHAL_ABI_WINDOWED]),
  1926. PLT_ENTRY_SIZE);
  1927. abi_offset = abi == XTHAL_ABI_WINDOWED ? 3 : 0;
  1928. bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
  1929. plt_base + code_offset + abi_offset),
  1930. splt->contents + code_offset + abi_offset + 1);
  1931. bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
  1932. plt_base + code_offset + abi_offset + 3),
  1933. splt->contents + code_offset + abi_offset + 4);
  1934. bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
  1935. plt_base + code_offset + abi_offset + 6),
  1936. splt->contents + code_offset + abi_offset + 7);
  1937. return plt_base + code_offset;
  1938. }
  1939. static bool get_indirect_call_dest_reg (xtensa_opcode, unsigned *);
  1940. static bool
  1941. replace_tls_insn (Elf_Internal_Rela *rel,
  1942. bfd *abfd,
  1943. asection *input_section,
  1944. bfd_byte *contents,
  1945. bool is_ld_model,
  1946. char **error_message)
  1947. {
  1948. static xtensa_insnbuf ibuff = NULL;
  1949. static xtensa_insnbuf sbuff = NULL;
  1950. xtensa_isa isa = xtensa_default_isa;
  1951. xtensa_format fmt;
  1952. xtensa_opcode old_op, new_op;
  1953. bfd_size_type input_size;
  1954. int r_type;
  1955. unsigned dest_reg, src_reg;
  1956. if (ibuff == NULL)
  1957. {
  1958. ibuff = xtensa_insnbuf_alloc (isa);
  1959. sbuff = xtensa_insnbuf_alloc (isa);
  1960. }
  1961. input_size = bfd_get_section_limit (abfd, input_section);
  1962. /* Read the instruction into a buffer and decode the opcode. */
  1963. xtensa_insnbuf_from_chars (isa, ibuff, contents + rel->r_offset,
  1964. input_size - rel->r_offset);
  1965. fmt = xtensa_format_decode (isa, ibuff);
  1966. if (fmt == XTENSA_UNDEFINED)
  1967. {
  1968. *error_message = "cannot decode instruction format";
  1969. return false;
  1970. }
  1971. BFD_ASSERT (xtensa_format_num_slots (isa, fmt) == 1);
  1972. xtensa_format_get_slot (isa, fmt, 0, ibuff, sbuff);
  1973. old_op = xtensa_opcode_decode (isa, fmt, 0, sbuff);
  1974. if (old_op == XTENSA_UNDEFINED)
  1975. {
  1976. *error_message = "cannot decode instruction opcode";
  1977. return false;
  1978. }
  1979. r_type = ELF32_R_TYPE (rel->r_info);
  1980. switch (r_type)
  1981. {
  1982. case R_XTENSA_TLS_FUNC:
  1983. case R_XTENSA_TLS_ARG:
  1984. if (old_op != get_l32r_opcode ()
  1985. || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
  1986. sbuff, &dest_reg) != 0)
  1987. {
  1988. *error_message = "cannot extract L32R destination for TLS access";
  1989. return false;
  1990. }
  1991. break;
  1992. case R_XTENSA_TLS_CALL:
  1993. if (! get_indirect_call_dest_reg (old_op, &dest_reg)
  1994. || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
  1995. sbuff, &src_reg) != 0)
  1996. {
  1997. *error_message = "cannot extract CALLXn operands for TLS access";
  1998. return false;
  1999. }
  2000. break;
  2001. default:
  2002. abort ();
  2003. }
  2004. if (is_ld_model)
  2005. {
  2006. switch (r_type)
  2007. {
  2008. case R_XTENSA_TLS_FUNC:
  2009. case R_XTENSA_TLS_ARG:
  2010. /* Change the instruction to a NOP (or "OR a1, a1, a1" for older
  2011. versions of Xtensa). */
  2012. new_op = xtensa_opcode_lookup (isa, "nop");
  2013. if (new_op == XTENSA_UNDEFINED)
  2014. {
  2015. new_op = xtensa_opcode_lookup (isa, "or");
  2016. if (new_op == XTENSA_UNDEFINED
  2017. || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
  2018. || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
  2019. sbuff, 1) != 0
  2020. || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
  2021. sbuff, 1) != 0
  2022. || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
  2023. sbuff, 1) != 0)
  2024. {
  2025. *error_message = "cannot encode OR for TLS access";
  2026. return false;
  2027. }
  2028. }
  2029. else
  2030. {
  2031. if (xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0)
  2032. {
  2033. *error_message = "cannot encode NOP for TLS access";
  2034. return false;
  2035. }
  2036. }
  2037. break;
  2038. case R_XTENSA_TLS_CALL:
  2039. /* Read THREADPTR into the CALLX's return value register. */
  2040. new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
  2041. if (new_op == XTENSA_UNDEFINED
  2042. || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
  2043. || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
  2044. sbuff, dest_reg + 2) != 0)
  2045. {
  2046. *error_message = "cannot encode RUR.THREADPTR for TLS access";
  2047. return false;
  2048. }
  2049. break;
  2050. }
  2051. }
  2052. else
  2053. {
  2054. switch (r_type)
  2055. {
  2056. case R_XTENSA_TLS_FUNC:
  2057. new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
  2058. if (new_op == XTENSA_UNDEFINED
  2059. || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
  2060. || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
  2061. sbuff, dest_reg) != 0)
  2062. {
  2063. *error_message = "cannot encode RUR.THREADPTR for TLS access";
  2064. return false;
  2065. }
  2066. break;
  2067. case R_XTENSA_TLS_ARG:
  2068. /* Nothing to do. Keep the original L32R instruction. */
  2069. return true;
  2070. case R_XTENSA_TLS_CALL:
  2071. /* Add the CALLX's src register (holding the THREADPTR value)
  2072. to the first argument register (holding the offset) and put
  2073. the result in the CALLX's return value register. */
  2074. new_op = xtensa_opcode_lookup (isa, "add");
  2075. if (new_op == XTENSA_UNDEFINED
  2076. || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
  2077. || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
  2078. sbuff, dest_reg + 2) != 0
  2079. || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
  2080. sbuff, dest_reg + 2) != 0
  2081. || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
  2082. sbuff, src_reg) != 0)
  2083. {
  2084. *error_message = "cannot encode ADD for TLS access";
  2085. return false;
  2086. }
  2087. break;
  2088. }
  2089. }
  2090. xtensa_format_set_slot (isa, fmt, 0, ibuff, sbuff);
  2091. xtensa_insnbuf_to_chars (isa, ibuff, contents + rel->r_offset,
  2092. input_size - rel->r_offset);
  2093. return true;
  2094. }
  2095. #define IS_XTENSA_TLS_RELOC(R_TYPE) \
  2096. ((R_TYPE) == R_XTENSA_TLSDESC_FN \
  2097. || (R_TYPE) == R_XTENSA_TLSDESC_ARG \
  2098. || (R_TYPE) == R_XTENSA_TLS_DTPOFF \
  2099. || (R_TYPE) == R_XTENSA_TLS_TPOFF \
  2100. || (R_TYPE) == R_XTENSA_TLS_FUNC \
  2101. || (R_TYPE) == R_XTENSA_TLS_ARG \
  2102. || (R_TYPE) == R_XTENSA_TLS_CALL)
  2103. /* Relocate an Xtensa ELF section. This is invoked by the linker for
  2104. both relocatable and final links. */
  2105. static int
  2106. elf_xtensa_relocate_section (bfd *output_bfd,
  2107. struct bfd_link_info *info,
  2108. bfd *input_bfd,
  2109. asection *input_section,
  2110. bfd_byte *contents,
  2111. Elf_Internal_Rela *relocs,
  2112. Elf_Internal_Sym *local_syms,
  2113. asection **local_sections)
  2114. {
  2115. struct elf_xtensa_link_hash_table *htab;
  2116. Elf_Internal_Shdr *symtab_hdr;
  2117. Elf_Internal_Rela *rel;
  2118. Elf_Internal_Rela *relend;
  2119. struct elf_link_hash_entry **sym_hashes;
  2120. property_table_entry *lit_table = 0;
  2121. int ltblsize = 0;
  2122. char *local_got_tls_types;
  2123. char *error_message = NULL;
  2124. bfd_size_type input_size;
  2125. int tls_type;
  2126. if (!xtensa_default_isa)
  2127. xtensa_default_isa = xtensa_isa_init (0, 0);
  2128. if (!is_xtensa_elf (input_bfd))
  2129. {
  2130. bfd_set_error (bfd_error_wrong_format);
  2131. return false;
  2132. }
  2133. htab = elf_xtensa_hash_table (info);
  2134. if (htab == NULL)
  2135. return false;
  2136. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2137. sym_hashes = elf_sym_hashes (input_bfd);
  2138. local_got_tls_types = elf_xtensa_local_got_tls_type (input_bfd);
  2139. if (elf_hash_table (info)->dynamic_sections_created)
  2140. {
  2141. ltblsize = xtensa_read_table_entries (input_bfd, input_section,
  2142. &lit_table, XTENSA_LIT_SEC_NAME,
  2143. true);
  2144. if (ltblsize < 0)
  2145. return false;
  2146. }
  2147. input_size = bfd_get_section_limit (input_bfd, input_section);
  2148. rel = relocs;
  2149. relend = relocs + input_section->reloc_count;
  2150. for (; rel < relend; rel++)
  2151. {
  2152. int r_type;
  2153. reloc_howto_type *howto;
  2154. unsigned long r_symndx;
  2155. struct elf_link_hash_entry *h;
  2156. Elf_Internal_Sym *sym;
  2157. char sym_type;
  2158. const char *name;
  2159. asection *sec;
  2160. bfd_vma relocation;
  2161. bfd_reloc_status_type r;
  2162. bool is_weak_undef;
  2163. bool unresolved_reloc;
  2164. bool warned;
  2165. bool dynamic_symbol;
  2166. r_type = ELF32_R_TYPE (rel->r_info);
  2167. if (r_type == (int) R_XTENSA_GNU_VTINHERIT
  2168. || r_type == (int) R_XTENSA_GNU_VTENTRY)
  2169. continue;
  2170. if (r_type < 0 || r_type >= (int) R_XTENSA_max)
  2171. {
  2172. bfd_set_error (bfd_error_bad_value);
  2173. return false;
  2174. }
  2175. howto = &elf_howto_table[r_type];
  2176. r_symndx = ELF32_R_SYM (rel->r_info);
  2177. h = NULL;
  2178. sym = NULL;
  2179. sec = NULL;
  2180. is_weak_undef = false;
  2181. unresolved_reloc = false;
  2182. warned = false;
  2183. if (howto->partial_inplace && !bfd_link_relocatable (info))
  2184. {
  2185. /* Because R_XTENSA_32 was made partial_inplace to fix some
  2186. problems with DWARF info in partial links, there may be
  2187. an addend stored in the contents. Take it out of there
  2188. and move it back into the addend field of the reloc. */
  2189. rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
  2190. bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
  2191. }
  2192. if (r_symndx < symtab_hdr->sh_info)
  2193. {
  2194. sym = local_syms + r_symndx;
  2195. sym_type = ELF32_ST_TYPE (sym->st_info);
  2196. sec = local_sections[r_symndx];
  2197. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  2198. }
  2199. else
  2200. {
  2201. bool ignored;
  2202. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  2203. r_symndx, symtab_hdr, sym_hashes,
  2204. h, sec, relocation,
  2205. unresolved_reloc, warned, ignored);
  2206. if (relocation == 0
  2207. && !unresolved_reloc
  2208. && h->root.type == bfd_link_hash_undefweak)
  2209. is_weak_undef = true;
  2210. sym_type = h->type;
  2211. }
  2212. if (sec != NULL && discarded_section (sec))
  2213. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  2214. rel, 1, relend, howto, 0, contents);
  2215. if (bfd_link_relocatable (info))
  2216. {
  2217. bfd_vma dest_addr;
  2218. asection * sym_sec = get_elf_r_symndx_section (input_bfd, r_symndx);
  2219. /* This is a relocatable link.
  2220. 1) If the reloc is against a section symbol, adjust
  2221. according to the output section.
  2222. 2) If there is a new target for this relocation,
  2223. the new target will be in the same output section.
  2224. We adjust the relocation by the output section
  2225. difference. */
  2226. if (relaxing_section)
  2227. {
  2228. /* Check if this references a section in another input file. */
  2229. if (!do_fix_for_relocatable_link (rel, input_bfd, input_section,
  2230. contents))
  2231. return false;
  2232. }
  2233. dest_addr = sym_sec->output_section->vma + sym_sec->output_offset
  2234. + get_elf_r_symndx_offset (input_bfd, r_symndx) + rel->r_addend;
  2235. if (r_type == R_XTENSA_ASM_SIMPLIFY)
  2236. {
  2237. error_message = NULL;
  2238. /* Convert ASM_SIMPLIFY into the simpler relocation
  2239. so that they never escape a relaxing link. */
  2240. r = contract_asm_expansion (contents, input_size, rel,
  2241. &error_message);
  2242. if (r != bfd_reloc_ok)
  2243. (*info->callbacks->reloc_dangerous)
  2244. (info, error_message,
  2245. input_bfd, input_section, rel->r_offset);
  2246. r_type = ELF32_R_TYPE (rel->r_info);
  2247. }
  2248. /* This is a relocatable link, so we don't have to change
  2249. anything unless the reloc is against a section symbol,
  2250. in which case we have to adjust according to where the
  2251. section symbol winds up in the output section. */
  2252. if (r_symndx < symtab_hdr->sh_info)
  2253. {
  2254. sym = local_syms + r_symndx;
  2255. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  2256. {
  2257. sec = local_sections[r_symndx];
  2258. rel->r_addend += sec->output_offset + sym->st_value;
  2259. }
  2260. }
  2261. /* If there is an addend with a partial_inplace howto,
  2262. then move the addend to the contents. This is a hack
  2263. to work around problems with DWARF in relocatable links
  2264. with some previous version of BFD. Now we can't easily get
  2265. rid of the hack without breaking backward compatibility.... */
  2266. r = bfd_reloc_ok;
  2267. howto = &elf_howto_table[r_type];
  2268. if (howto->partial_inplace && rel->r_addend)
  2269. {
  2270. r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
  2271. rel->r_addend, contents,
  2272. rel->r_offset, false,
  2273. &error_message);
  2274. rel->r_addend = 0;
  2275. }
  2276. else
  2277. {
  2278. /* Put the correct bits in the target instruction, even
  2279. though the relocation will still be present in the output
  2280. file. This makes disassembly clearer, as well as
  2281. allowing loadable kernel modules to work without needing
  2282. relocations on anything other than calls and l32r's. */
  2283. /* If it is not in the same section, there is nothing we can do. */
  2284. if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP &&
  2285. sym_sec->output_section == input_section->output_section)
  2286. {
  2287. r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
  2288. dest_addr, contents,
  2289. rel->r_offset, false,
  2290. &error_message);
  2291. }
  2292. }
  2293. if (r != bfd_reloc_ok)
  2294. (*info->callbacks->reloc_dangerous)
  2295. (info, error_message,
  2296. input_bfd, input_section, rel->r_offset);
  2297. /* Done with work for relocatable link; continue with next reloc. */
  2298. continue;
  2299. }
  2300. /* This is a final link. */
  2301. if (relaxing_section)
  2302. {
  2303. /* Check if this references a section in another input file. */
  2304. do_fix_for_final_link (rel, input_bfd, input_section, contents,
  2305. &relocation);
  2306. }
  2307. /* Sanity check the address. */
  2308. if (rel->r_offset >= input_size
  2309. && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
  2310. {
  2311. _bfd_error_handler
  2312. /* xgettext:c-format */
  2313. (_("%pB(%pA+%#" PRIx64 "): "
  2314. "relocation offset out of range (size=%#" PRIx64 ")"),
  2315. input_bfd, input_section, (uint64_t) rel->r_offset,
  2316. (uint64_t) input_size);
  2317. bfd_set_error (bfd_error_bad_value);
  2318. return false;
  2319. }
  2320. if (h != NULL)
  2321. name = h->root.root.string;
  2322. else
  2323. {
  2324. name = (bfd_elf_string_from_elf_section
  2325. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  2326. if (name == NULL || *name == '\0')
  2327. name = bfd_section_name (sec);
  2328. }
  2329. if (r_symndx != STN_UNDEF
  2330. && r_type != R_XTENSA_NONE
  2331. && (h == NULL
  2332. || h->root.type == bfd_link_hash_defined
  2333. || h->root.type == bfd_link_hash_defweak)
  2334. && IS_XTENSA_TLS_RELOC (r_type) != (sym_type == STT_TLS))
  2335. {
  2336. _bfd_error_handler
  2337. ((sym_type == STT_TLS
  2338. /* xgettext:c-format */
  2339. ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
  2340. /* xgettext:c-format */
  2341. : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
  2342. input_bfd,
  2343. input_section,
  2344. (uint64_t) rel->r_offset,
  2345. howto->name,
  2346. name);
  2347. }
  2348. dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
  2349. tls_type = GOT_UNKNOWN;
  2350. if (h)
  2351. tls_type = elf_xtensa_hash_entry (h)->tls_type;
  2352. else if (local_got_tls_types)
  2353. tls_type = local_got_tls_types [r_symndx];
  2354. switch (r_type)
  2355. {
  2356. case R_XTENSA_32:
  2357. case R_XTENSA_PLT:
  2358. if (elf_hash_table (info)->dynamic_sections_created
  2359. && (input_section->flags & SEC_ALLOC) != 0
  2360. && (dynamic_symbol || bfd_link_pic (info)))
  2361. {
  2362. Elf_Internal_Rela outrel;
  2363. bfd_byte *loc;
  2364. asection *srel;
  2365. if (dynamic_symbol && r_type == R_XTENSA_PLT)
  2366. srel = htab->elf.srelplt;
  2367. else
  2368. srel = htab->elf.srelgot;
  2369. BFD_ASSERT (srel != NULL);
  2370. outrel.r_offset =
  2371. _bfd_elf_section_offset (output_bfd, info,
  2372. input_section, rel->r_offset);
  2373. if ((outrel.r_offset | 1) == (bfd_vma) -1)
  2374. memset (&outrel, 0, sizeof outrel);
  2375. else
  2376. {
  2377. outrel.r_offset += (input_section->output_section->vma
  2378. + input_section->output_offset);
  2379. /* Complain if the relocation is in a read-only section
  2380. and not in a literal pool. */
  2381. if ((input_section->flags & SEC_READONLY) != 0
  2382. && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
  2383. outrel.r_offset))
  2384. {
  2385. error_message =
  2386. _("dynamic relocation in read-only section");
  2387. (*info->callbacks->reloc_dangerous)
  2388. (info, error_message,
  2389. input_bfd, input_section, rel->r_offset);
  2390. }
  2391. if (dynamic_symbol)
  2392. {
  2393. outrel.r_addend = rel->r_addend;
  2394. rel->r_addend = 0;
  2395. if (r_type == R_XTENSA_32)
  2396. {
  2397. outrel.r_info =
  2398. ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
  2399. relocation = 0;
  2400. }
  2401. else /* r_type == R_XTENSA_PLT */
  2402. {
  2403. outrel.r_info =
  2404. ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
  2405. /* Create the PLT entry and set the initial
  2406. contents of the literal entry to the address of
  2407. the PLT entry. */
  2408. relocation =
  2409. elf_xtensa_create_plt_entry (info, output_bfd,
  2410. srel->reloc_count);
  2411. }
  2412. unresolved_reloc = false;
  2413. }
  2414. else if (!is_weak_undef)
  2415. {
  2416. /* Generate a RELATIVE relocation. */
  2417. outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
  2418. outrel.r_addend = 0;
  2419. }
  2420. else
  2421. {
  2422. continue;
  2423. }
  2424. }
  2425. loc = (srel->contents
  2426. + srel->reloc_count++ * sizeof (Elf32_External_Rela));
  2427. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  2428. BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
  2429. <= srel->size);
  2430. }
  2431. else if (r_type == R_XTENSA_ASM_EXPAND && dynamic_symbol)
  2432. {
  2433. /* This should only happen for non-PIC code, which is not
  2434. supposed to be used on systems with dynamic linking.
  2435. Just ignore these relocations. */
  2436. continue;
  2437. }
  2438. break;
  2439. case R_XTENSA_TLS_TPOFF:
  2440. /* Switch to LE model for local symbols in an executable. */
  2441. if (! bfd_link_pic (info) && ! dynamic_symbol)
  2442. {
  2443. relocation = tpoff (info, relocation);
  2444. break;
  2445. }
  2446. /* fall through */
  2447. case R_XTENSA_TLSDESC_FN:
  2448. case R_XTENSA_TLSDESC_ARG:
  2449. {
  2450. if (r_type == R_XTENSA_TLSDESC_FN)
  2451. {
  2452. if (! bfd_link_pic (info) || (tls_type & GOT_TLS_IE) != 0)
  2453. r_type = R_XTENSA_NONE;
  2454. }
  2455. else if (r_type == R_XTENSA_TLSDESC_ARG)
  2456. {
  2457. if (bfd_link_pic (info))
  2458. {
  2459. if ((tls_type & GOT_TLS_IE) != 0)
  2460. r_type = R_XTENSA_TLS_TPOFF;
  2461. }
  2462. else
  2463. {
  2464. r_type = R_XTENSA_TLS_TPOFF;
  2465. if (! dynamic_symbol)
  2466. {
  2467. relocation = tpoff (info, relocation);
  2468. break;
  2469. }
  2470. }
  2471. }
  2472. if (r_type == R_XTENSA_NONE)
  2473. /* Nothing to do here; skip to the next reloc. */
  2474. continue;
  2475. if (! elf_hash_table (info)->dynamic_sections_created)
  2476. {
  2477. error_message =
  2478. _("TLS relocation invalid without dynamic sections");
  2479. (*info->callbacks->reloc_dangerous)
  2480. (info, error_message,
  2481. input_bfd, input_section, rel->r_offset);
  2482. }
  2483. else
  2484. {
  2485. Elf_Internal_Rela outrel;
  2486. bfd_byte *loc;
  2487. asection *srel = htab->elf.srelgot;
  2488. int indx;
  2489. outrel.r_offset = (input_section->output_section->vma
  2490. + input_section->output_offset
  2491. + rel->r_offset);
  2492. /* Complain if the relocation is in a read-only section
  2493. and not in a literal pool. */
  2494. if ((input_section->flags & SEC_READONLY) != 0
  2495. && ! elf_xtensa_in_literal_pool (lit_table, ltblsize,
  2496. outrel.r_offset))
  2497. {
  2498. error_message =
  2499. _("dynamic relocation in read-only section");
  2500. (*info->callbacks->reloc_dangerous)
  2501. (info, error_message,
  2502. input_bfd, input_section, rel->r_offset);
  2503. }
  2504. indx = h && h->dynindx != -1 ? h->dynindx : 0;
  2505. if (indx == 0)
  2506. outrel.r_addend = relocation - dtpoff_base (info);
  2507. else
  2508. outrel.r_addend = 0;
  2509. rel->r_addend = 0;
  2510. outrel.r_info = ELF32_R_INFO (indx, r_type);
  2511. relocation = 0;
  2512. unresolved_reloc = false;
  2513. BFD_ASSERT (srel);
  2514. loc = (srel->contents
  2515. + srel->reloc_count++ * sizeof (Elf32_External_Rela));
  2516. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  2517. BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
  2518. <= srel->size);
  2519. }
  2520. }
  2521. break;
  2522. case R_XTENSA_TLS_DTPOFF:
  2523. if (! bfd_link_pic (info))
  2524. /* Switch from LD model to LE model. */
  2525. relocation = tpoff (info, relocation);
  2526. else
  2527. relocation -= dtpoff_base (info);
  2528. break;
  2529. case R_XTENSA_TLS_FUNC:
  2530. case R_XTENSA_TLS_ARG:
  2531. case R_XTENSA_TLS_CALL:
  2532. /* Check if optimizing to IE or LE model. */
  2533. if ((tls_type & GOT_TLS_IE) != 0)
  2534. {
  2535. bool is_ld_model =
  2536. (h && elf_xtensa_hash_entry (h) == htab->tlsbase);
  2537. if (! replace_tls_insn (rel, input_bfd, input_section, contents,
  2538. is_ld_model, &error_message))
  2539. (*info->callbacks->reloc_dangerous)
  2540. (info, error_message,
  2541. input_bfd, input_section, rel->r_offset);
  2542. if (r_type != R_XTENSA_TLS_ARG || is_ld_model)
  2543. {
  2544. /* Skip subsequent relocations on the same instruction. */
  2545. while (rel + 1 < relend && rel[1].r_offset == rel->r_offset)
  2546. rel++;
  2547. }
  2548. }
  2549. continue;
  2550. default:
  2551. if (elf_hash_table (info)->dynamic_sections_created
  2552. && dynamic_symbol && (is_operand_relocation (r_type)
  2553. || r_type == R_XTENSA_32_PCREL))
  2554. {
  2555. error_message =
  2556. vsprint_msg ("invalid relocation for dynamic symbol", ": %s",
  2557. strlen (name) + 2, name);
  2558. (*info->callbacks->reloc_dangerous)
  2559. (info, error_message, input_bfd, input_section, rel->r_offset);
  2560. continue;
  2561. }
  2562. break;
  2563. }
  2564. /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
  2565. because such sections are not SEC_ALLOC and thus ld.so will
  2566. not process them. */
  2567. if (unresolved_reloc
  2568. && !((input_section->flags & SEC_DEBUGGING) != 0
  2569. && h->def_dynamic)
  2570. && _bfd_elf_section_offset (output_bfd, info, input_section,
  2571. rel->r_offset) != (bfd_vma) -1)
  2572. {
  2573. _bfd_error_handler
  2574. /* xgettext:c-format */
  2575. (_("%pB(%pA+%#" PRIx64 "): "
  2576. "unresolvable %s relocation against symbol `%s'"),
  2577. input_bfd,
  2578. input_section,
  2579. (uint64_t) rel->r_offset,
  2580. howto->name,
  2581. name);
  2582. return false;
  2583. }
  2584. /* TLS optimizations may have changed r_type; update "howto". */
  2585. howto = &elf_howto_table[r_type];
  2586. /* There's no point in calling bfd_perform_relocation here.
  2587. Just go directly to our "special function". */
  2588. r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
  2589. relocation + rel->r_addend,
  2590. contents, rel->r_offset, is_weak_undef,
  2591. &error_message);
  2592. if (r != bfd_reloc_ok && !warned)
  2593. {
  2594. BFD_ASSERT (r == bfd_reloc_dangerous || r == bfd_reloc_other);
  2595. BFD_ASSERT (error_message != NULL);
  2596. if (rel->r_addend == 0)
  2597. error_message = vsprint_msg (error_message, ": %s",
  2598. strlen (name) + 2, name);
  2599. else
  2600. error_message = vsprint_msg (error_message, ": (%s+0x%x)",
  2601. strlen (name) + 22,
  2602. name, (int) rel->r_addend);
  2603. (*info->callbacks->reloc_dangerous)
  2604. (info, error_message, input_bfd, input_section, rel->r_offset);
  2605. }
  2606. }
  2607. free (lit_table);
  2608. input_section->reloc_done = true;
  2609. return true;
  2610. }
  2611. /* Finish up dynamic symbol handling. There's not much to do here since
  2612. the PLT and GOT entries are all set up by relocate_section. */
  2613. static bool
  2614. elf_xtensa_finish_dynamic_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
  2615. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2616. struct elf_link_hash_entry *h,
  2617. Elf_Internal_Sym *sym)
  2618. {
  2619. if (h->needs_plt && !h->def_regular)
  2620. {
  2621. /* Mark the symbol as undefined, rather than as defined in
  2622. the .plt section. Leave the value alone. */
  2623. sym->st_shndx = SHN_UNDEF;
  2624. /* If the symbol is weak, we do need to clear the value.
  2625. Otherwise, the PLT entry would provide a definition for
  2626. the symbol even if the symbol wasn't defined anywhere,
  2627. and so the symbol would never be NULL. */
  2628. if (!h->ref_regular_nonweak)
  2629. sym->st_value = 0;
  2630. }
  2631. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  2632. if (h == elf_hash_table (info)->hdynamic
  2633. || h == elf_hash_table (info)->hgot)
  2634. sym->st_shndx = SHN_ABS;
  2635. return true;
  2636. }
  2637. /* Combine adjacent literal table entries in the output. Adjacent
  2638. entries within each input section may have been removed during
  2639. relaxation, but we repeat the process here, even though it's too late
  2640. to shrink the output section, because it's important to minimize the
  2641. number of literal table entries to reduce the start-up work for the
  2642. runtime linker. Returns the number of remaining table entries or -1
  2643. on error. */
  2644. static int
  2645. elf_xtensa_combine_prop_entries (bfd *output_bfd,
  2646. asection *sxtlit,
  2647. asection *sgotloc)
  2648. {
  2649. bfd_byte *contents;
  2650. property_table_entry *table;
  2651. bfd_size_type section_size, sgotloc_size;
  2652. bfd_vma offset;
  2653. int n, m, num;
  2654. section_size = sxtlit->size;
  2655. if (section_size == 0)
  2656. return 0;
  2657. BFD_ASSERT (section_size % 8 == 0);
  2658. num = section_size / 8;
  2659. sgotloc_size = sgotloc->size;
  2660. if (sgotloc_size != section_size)
  2661. {
  2662. _bfd_error_handler
  2663. (_("internal inconsistency in size of .got.loc section"));
  2664. return -1;
  2665. }
  2666. table = bfd_malloc (num * sizeof (property_table_entry));
  2667. if (table == 0)
  2668. return -1;
  2669. /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
  2670. propagates to the output section, where it doesn't really apply and
  2671. where it breaks the following call to bfd_malloc_and_get_section. */
  2672. sxtlit->flags &= ~SEC_IN_MEMORY;
  2673. if (!bfd_malloc_and_get_section (output_bfd, sxtlit, &contents))
  2674. {
  2675. free (contents);
  2676. free (table);
  2677. return -1;
  2678. }
  2679. /* There should never be any relocations left at this point, so this
  2680. is quite a bit easier than what is done during relaxation. */
  2681. /* Copy the raw contents into a property table array and sort it. */
  2682. offset = 0;
  2683. for (n = 0; n < num; n++)
  2684. {
  2685. table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
  2686. table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
  2687. offset += 8;
  2688. }
  2689. qsort (table, num, sizeof (property_table_entry), property_table_compare);
  2690. for (n = 0; n < num; n++)
  2691. {
  2692. bool remove_entry = false;
  2693. if (table[n].size == 0)
  2694. remove_entry = true;
  2695. else if (n > 0
  2696. && (table[n-1].address + table[n-1].size == table[n].address))
  2697. {
  2698. table[n-1].size += table[n].size;
  2699. remove_entry = true;
  2700. }
  2701. if (remove_entry)
  2702. {
  2703. for (m = n; m < num - 1; m++)
  2704. {
  2705. table[m].address = table[m+1].address;
  2706. table[m].size = table[m+1].size;
  2707. }
  2708. n--;
  2709. num--;
  2710. }
  2711. }
  2712. /* Copy the data back to the raw contents. */
  2713. offset = 0;
  2714. for (n = 0; n < num; n++)
  2715. {
  2716. bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
  2717. bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
  2718. offset += 8;
  2719. }
  2720. /* Clear the removed bytes. */
  2721. if ((bfd_size_type) (num * 8) < section_size)
  2722. memset (&contents[num * 8], 0, section_size - num * 8);
  2723. if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
  2724. section_size))
  2725. return -1;
  2726. /* Copy the contents to ".got.loc". */
  2727. memcpy (sgotloc->contents, contents, section_size);
  2728. free (contents);
  2729. free (table);
  2730. return num;
  2731. }
  2732. /* Finish up the dynamic sections. */
  2733. static bool
  2734. elf_xtensa_finish_dynamic_sections (bfd *output_bfd,
  2735. struct bfd_link_info *info)
  2736. {
  2737. struct elf_xtensa_link_hash_table *htab;
  2738. bfd *dynobj;
  2739. asection *sdyn, *srelplt, *srelgot, *sgot, *sxtlit, *sgotloc;
  2740. Elf32_External_Dyn *dyncon, *dynconend;
  2741. int num_xtlit_entries = 0;
  2742. if (! elf_hash_table (info)->dynamic_sections_created)
  2743. return true;
  2744. htab = elf_xtensa_hash_table (info);
  2745. if (htab == NULL)
  2746. return false;
  2747. dynobj = elf_hash_table (info)->dynobj;
  2748. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2749. BFD_ASSERT (sdyn != NULL);
  2750. /* Set the first entry in the global offset table to the address of
  2751. the dynamic section. */
  2752. sgot = htab->elf.sgot;
  2753. if (sgot)
  2754. {
  2755. BFD_ASSERT (sgot->size == 4);
  2756. if (sdyn == NULL)
  2757. bfd_put_32 (output_bfd, 0, sgot->contents);
  2758. else
  2759. bfd_put_32 (output_bfd,
  2760. sdyn->output_section->vma + sdyn->output_offset,
  2761. sgot->contents);
  2762. }
  2763. srelplt = htab->elf.srelplt;
  2764. srelgot = htab->elf.srelgot;
  2765. if (srelplt && srelplt->size != 0)
  2766. {
  2767. asection *sgotplt, *spltlittbl;
  2768. int chunk, plt_chunks, plt_entries;
  2769. Elf_Internal_Rela irela;
  2770. bfd_byte *loc;
  2771. unsigned rtld_reloc;
  2772. spltlittbl = htab->spltlittbl;
  2773. BFD_ASSERT (srelgot != NULL && spltlittbl != NULL);
  2774. /* Find the first XTENSA_RTLD relocation. Presumably the rest
  2775. of them follow immediately after.... */
  2776. for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
  2777. {
  2778. loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
  2779. bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
  2780. if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
  2781. break;
  2782. }
  2783. BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
  2784. plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
  2785. plt_chunks =
  2786. (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
  2787. for (chunk = 0; chunk < plt_chunks; chunk++)
  2788. {
  2789. int chunk_entries = 0;
  2790. sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
  2791. BFD_ASSERT (sgotplt != NULL);
  2792. /* Emit special RTLD relocations for the first two entries in
  2793. each chunk of the .got.plt section. */
  2794. loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
  2795. bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
  2796. BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
  2797. irela.r_offset = (sgotplt->output_section->vma
  2798. + sgotplt->output_offset);
  2799. irela.r_addend = 1; /* tell rtld to set value to resolver function */
  2800. bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
  2801. rtld_reloc += 1;
  2802. BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
  2803. /* Next literal immediately follows the first. */
  2804. loc += sizeof (Elf32_External_Rela);
  2805. bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
  2806. BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
  2807. irela.r_offset = (sgotplt->output_section->vma
  2808. + sgotplt->output_offset + 4);
  2809. /* Tell rtld to set value to object's link map. */
  2810. irela.r_addend = 2;
  2811. bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
  2812. rtld_reloc += 1;
  2813. BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
  2814. /* Fill in the literal table. */
  2815. if (chunk < plt_chunks - 1)
  2816. chunk_entries = PLT_ENTRIES_PER_CHUNK;
  2817. else
  2818. chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
  2819. BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->size);
  2820. bfd_put_32 (output_bfd,
  2821. sgotplt->output_section->vma + sgotplt->output_offset,
  2822. spltlittbl->contents + (chunk * 8) + 0);
  2823. bfd_put_32 (output_bfd,
  2824. 8 + (chunk_entries * 4),
  2825. spltlittbl->contents + (chunk * 8) + 4);
  2826. }
  2827. /* The .xt.lit.plt section has just been modified. This must
  2828. happen before the code below which combines adjacent literal
  2829. table entries, and the .xt.lit.plt contents have to be forced to
  2830. the output here. */
  2831. if (! bfd_set_section_contents (output_bfd,
  2832. spltlittbl->output_section,
  2833. spltlittbl->contents,
  2834. spltlittbl->output_offset,
  2835. spltlittbl->size))
  2836. return false;
  2837. /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
  2838. spltlittbl->flags &= ~SEC_HAS_CONTENTS;
  2839. }
  2840. /* All the dynamic relocations have been emitted at this point.
  2841. Make sure the relocation sections are the correct size. */
  2842. if ((srelgot && srelgot->size != (sizeof (Elf32_External_Rela)
  2843. * srelgot->reloc_count))
  2844. || (srelplt && srelplt->size != (sizeof (Elf32_External_Rela)
  2845. * srelplt->reloc_count)))
  2846. abort ();
  2847. /* Combine adjacent literal table entries. */
  2848. BFD_ASSERT (! bfd_link_relocatable (info));
  2849. sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
  2850. sgotloc = htab->sgotloc;
  2851. BFD_ASSERT (sgotloc);
  2852. if (sxtlit)
  2853. {
  2854. num_xtlit_entries =
  2855. elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
  2856. if (num_xtlit_entries < 0)
  2857. return false;
  2858. }
  2859. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  2860. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  2861. for (; dyncon < dynconend; dyncon++)
  2862. {
  2863. Elf_Internal_Dyn dyn;
  2864. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  2865. switch (dyn.d_tag)
  2866. {
  2867. default:
  2868. break;
  2869. case DT_XTENSA_GOT_LOC_SZ:
  2870. dyn.d_un.d_val = num_xtlit_entries;
  2871. break;
  2872. case DT_XTENSA_GOT_LOC_OFF:
  2873. dyn.d_un.d_ptr = (htab->sgotloc->output_section->vma
  2874. + htab->sgotloc->output_offset);
  2875. break;
  2876. case DT_PLTGOT:
  2877. dyn.d_un.d_ptr = (htab->elf.sgot->output_section->vma
  2878. + htab->elf.sgot->output_offset);
  2879. break;
  2880. case DT_JMPREL:
  2881. dyn.d_un.d_ptr = (htab->elf.srelplt->output_section->vma
  2882. + htab->elf.srelplt->output_offset);
  2883. break;
  2884. case DT_PLTRELSZ:
  2885. dyn.d_un.d_val = htab->elf.srelplt->size;
  2886. break;
  2887. }
  2888. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2889. }
  2890. return true;
  2891. }
  2892. /* Functions for dealing with the e_flags field. */
  2893. /* Merge backend specific data from an object file to the output
  2894. object file when linking. */
  2895. static bool
  2896. elf_xtensa_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  2897. {
  2898. bfd *obfd = info->output_bfd;
  2899. unsigned out_mach, in_mach;
  2900. flagword out_flag, in_flag;
  2901. /* Check if we have the same endianness. */
  2902. if (!_bfd_generic_verify_endian_match (ibfd, info))
  2903. return false;
  2904. /* Don't even pretend to support mixed-format linking. */
  2905. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  2906. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  2907. return false;
  2908. out_flag = elf_elfheader (obfd)->e_flags;
  2909. in_flag = elf_elfheader (ibfd)->e_flags;
  2910. out_mach = out_flag & EF_XTENSA_MACH;
  2911. in_mach = in_flag & EF_XTENSA_MACH;
  2912. if (out_mach != in_mach)
  2913. {
  2914. _bfd_error_handler
  2915. /* xgettext:c-format */
  2916. (_("%pB: incompatible machine type; output is 0x%x; input is 0x%x"),
  2917. ibfd, out_mach, in_mach);
  2918. bfd_set_error (bfd_error_wrong_format);
  2919. return false;
  2920. }
  2921. if (! elf_flags_init (obfd))
  2922. {
  2923. elf_flags_init (obfd) = true;
  2924. elf_elfheader (obfd)->e_flags = in_flag;
  2925. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  2926. && bfd_get_arch_info (obfd)->the_default)
  2927. return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  2928. bfd_get_mach (ibfd));
  2929. return true;
  2930. }
  2931. if ((out_flag & EF_XTENSA_XT_INSN) != (in_flag & EF_XTENSA_XT_INSN))
  2932. elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
  2933. if ((out_flag & EF_XTENSA_XT_LIT) != (in_flag & EF_XTENSA_XT_LIT))
  2934. elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
  2935. return true;
  2936. }
  2937. static bool
  2938. elf_xtensa_set_private_flags (bfd *abfd, flagword flags)
  2939. {
  2940. BFD_ASSERT (!elf_flags_init (abfd)
  2941. || elf_elfheader (abfd)->e_flags == flags);
  2942. elf_elfheader (abfd)->e_flags |= flags;
  2943. elf_flags_init (abfd) = true;
  2944. return true;
  2945. }
  2946. static bool
  2947. elf_xtensa_print_private_bfd_data (bfd *abfd, void *farg)
  2948. {
  2949. FILE *f = (FILE *) farg;
  2950. flagword e_flags = elf_elfheader (abfd)->e_flags;
  2951. fprintf (f, "\nXtensa header:\n");
  2952. if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
  2953. fprintf (f, "\nMachine = Base\n");
  2954. else
  2955. fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
  2956. fprintf (f, "Insn tables = %s\n",
  2957. (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
  2958. fprintf (f, "Literal tables = %s\n",
  2959. (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
  2960. return _bfd_elf_print_private_bfd_data (abfd, farg);
  2961. }
  2962. /* Set the right machine number for an Xtensa ELF file. */
  2963. static bool
  2964. elf_xtensa_object_p (bfd *abfd)
  2965. {
  2966. int mach;
  2967. unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
  2968. switch (arch)
  2969. {
  2970. case E_XTENSA_MACH:
  2971. mach = bfd_mach_xtensa;
  2972. break;
  2973. default:
  2974. return false;
  2975. }
  2976. (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
  2977. return true;
  2978. }
  2979. /* The final processing done just before writing out an Xtensa ELF object
  2980. file. This gets the Xtensa architecture right based on the machine
  2981. number. */
  2982. static bool
  2983. elf_xtensa_final_write_processing (bfd *abfd)
  2984. {
  2985. int mach;
  2986. unsigned long val = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
  2987. switch (mach = bfd_get_mach (abfd))
  2988. {
  2989. case bfd_mach_xtensa:
  2990. val = E_XTENSA_MACH;
  2991. break;
  2992. default:
  2993. break;
  2994. }
  2995. elf_elfheader (abfd)->e_flags &= ~EF_XTENSA_MACH;
  2996. elf_elfheader (abfd)->e_flags |= val;
  2997. return _bfd_elf_final_write_processing (abfd);
  2998. }
  2999. static enum elf_reloc_type_class
  3000. elf_xtensa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3001. const asection *rel_sec ATTRIBUTE_UNUSED,
  3002. const Elf_Internal_Rela *rela)
  3003. {
  3004. switch ((int) ELF32_R_TYPE (rela->r_info))
  3005. {
  3006. case R_XTENSA_RELATIVE:
  3007. return reloc_class_relative;
  3008. case R_XTENSA_JMP_SLOT:
  3009. return reloc_class_plt;
  3010. default:
  3011. return reloc_class_normal;
  3012. }
  3013. }
  3014. static bool
  3015. elf_xtensa_discard_info_for_section (bfd *abfd,
  3016. struct elf_reloc_cookie *cookie,
  3017. struct bfd_link_info *info,
  3018. asection *sec)
  3019. {
  3020. bfd_byte *contents;
  3021. bfd_vma offset, actual_offset;
  3022. bfd_size_type removed_bytes = 0;
  3023. bfd_size_type entry_size;
  3024. if (sec->output_section
  3025. && bfd_is_abs_section (sec->output_section))
  3026. return false;
  3027. if (xtensa_is_proptable_section (sec))
  3028. entry_size = 12;
  3029. else
  3030. entry_size = 8;
  3031. if (sec->size == 0 || sec->size % entry_size != 0)
  3032. return false;
  3033. contents = retrieve_contents (abfd, sec, info->keep_memory);
  3034. if (!contents)
  3035. return false;
  3036. cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
  3037. if (!cookie->rels)
  3038. {
  3039. release_contents (sec, contents);
  3040. return false;
  3041. }
  3042. /* Sort the relocations. They should already be in order when
  3043. relaxation is enabled, but it might not be. */
  3044. qsort (cookie->rels, sec->reloc_count, sizeof (Elf_Internal_Rela),
  3045. internal_reloc_compare);
  3046. cookie->rel = cookie->rels;
  3047. cookie->relend = cookie->rels + sec->reloc_count;
  3048. for (offset = 0; offset < sec->size; offset += entry_size)
  3049. {
  3050. actual_offset = offset - removed_bytes;
  3051. /* The ...symbol_deleted_p function will skip over relocs but it
  3052. won't adjust their offsets, so do that here. */
  3053. while (cookie->rel < cookie->relend
  3054. && cookie->rel->r_offset < offset)
  3055. {
  3056. cookie->rel->r_offset -= removed_bytes;
  3057. cookie->rel++;
  3058. }
  3059. while (cookie->rel < cookie->relend
  3060. && cookie->rel->r_offset == offset)
  3061. {
  3062. if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
  3063. {
  3064. /* Remove the table entry. (If the reloc type is NONE, then
  3065. the entry has already been merged with another and deleted
  3066. during relaxation.) */
  3067. if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
  3068. {
  3069. /* Shift the contents up. */
  3070. if (offset + entry_size < sec->size)
  3071. memmove (&contents[actual_offset],
  3072. &contents[actual_offset + entry_size],
  3073. sec->size - offset - entry_size);
  3074. removed_bytes += entry_size;
  3075. }
  3076. /* Remove this relocation. */
  3077. cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
  3078. }
  3079. /* Adjust the relocation offset for previous removals. This
  3080. should not be done before calling ...symbol_deleted_p
  3081. because it might mess up the offset comparisons there.
  3082. Make sure the offset doesn't underflow in the case where
  3083. the first entry is removed. */
  3084. if (cookie->rel->r_offset >= removed_bytes)
  3085. cookie->rel->r_offset -= removed_bytes;
  3086. else
  3087. cookie->rel->r_offset = 0;
  3088. cookie->rel++;
  3089. }
  3090. }
  3091. if (removed_bytes != 0)
  3092. {
  3093. /* Adjust any remaining relocs (shouldn't be any). */
  3094. for (; cookie->rel < cookie->relend; cookie->rel++)
  3095. {
  3096. if (cookie->rel->r_offset >= removed_bytes)
  3097. cookie->rel->r_offset -= removed_bytes;
  3098. else
  3099. cookie->rel->r_offset = 0;
  3100. }
  3101. /* Clear the removed bytes. */
  3102. memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
  3103. pin_contents (sec, contents);
  3104. pin_internal_relocs (sec, cookie->rels);
  3105. /* Shrink size. */
  3106. if (sec->rawsize == 0)
  3107. sec->rawsize = sec->size;
  3108. sec->size -= removed_bytes;
  3109. if (xtensa_is_littable_section (sec))
  3110. {
  3111. asection *sgotloc = elf_xtensa_hash_table (info)->sgotloc;
  3112. if (sgotloc)
  3113. sgotloc->size -= removed_bytes;
  3114. }
  3115. }
  3116. else
  3117. {
  3118. release_contents (sec, contents);
  3119. release_internal_relocs (sec, cookie->rels);
  3120. }
  3121. return (removed_bytes != 0);
  3122. }
  3123. static bool
  3124. elf_xtensa_discard_info (bfd *abfd,
  3125. struct elf_reloc_cookie *cookie,
  3126. struct bfd_link_info *info)
  3127. {
  3128. asection *sec;
  3129. bool changed = false;
  3130. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  3131. {
  3132. if (xtensa_is_property_section (sec))
  3133. {
  3134. if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
  3135. changed = true;
  3136. }
  3137. }
  3138. return changed;
  3139. }
  3140. static bool
  3141. elf_xtensa_ignore_discarded_relocs (asection *sec)
  3142. {
  3143. return xtensa_is_property_section (sec);
  3144. }
  3145. static unsigned int
  3146. elf_xtensa_action_discarded (asection *sec)
  3147. {
  3148. if (strcmp (".xt_except_table", sec->name) == 0)
  3149. return 0;
  3150. if (strcmp (".xt_except_desc", sec->name) == 0)
  3151. return 0;
  3152. return _bfd_elf_default_action_discarded (sec);
  3153. }
  3154. /* Support for core dump NOTE sections. */
  3155. static bool
  3156. elf_xtensa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  3157. {
  3158. int offset;
  3159. unsigned int size;
  3160. if (elf_tdata (abfd) == NULL
  3161. || elf_tdata (abfd)->core == NULL)
  3162. return false;
  3163. /* The size for Xtensa is variable, so don't try to recognize the format
  3164. based on the size. Just assume this is GNU/Linux. */
  3165. if (note == NULL || note->descsz < 28)
  3166. return false;
  3167. /* pr_cursig */
  3168. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  3169. /* pr_pid */
  3170. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  3171. /* pr_reg */
  3172. offset = 72;
  3173. size = note->descsz - offset - 4;
  3174. /* Make a ".reg/999" section. */
  3175. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  3176. size, note->descpos + offset);
  3177. }
  3178. static bool
  3179. elf_xtensa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  3180. {
  3181. switch (note->descsz)
  3182. {
  3183. default:
  3184. return false;
  3185. case 128: /* GNU/Linux elf_prpsinfo */
  3186. elf_tdata (abfd)->core->program
  3187. = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
  3188. elf_tdata (abfd)->core->command
  3189. = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
  3190. }
  3191. /* Note that for some reason, a spurious space is tacked
  3192. onto the end of the args in some (at least one anyway)
  3193. implementations, so strip it off if it exists. */
  3194. {
  3195. char *command = elf_tdata (abfd)->core->command;
  3196. int n = strlen (command);
  3197. if (0 < n && command[n - 1] == ' ')
  3198. command[n - 1] = '\0';
  3199. }
  3200. return true;
  3201. }
  3202. /* Generic Xtensa configurability stuff. */
  3203. static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
  3204. static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
  3205. static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
  3206. static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
  3207. static xtensa_opcode call0_op = XTENSA_UNDEFINED;
  3208. static xtensa_opcode call4_op = XTENSA_UNDEFINED;
  3209. static xtensa_opcode call8_op = XTENSA_UNDEFINED;
  3210. static xtensa_opcode call12_op = XTENSA_UNDEFINED;
  3211. static void
  3212. init_call_opcodes (void)
  3213. {
  3214. if (callx0_op == XTENSA_UNDEFINED)
  3215. {
  3216. callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
  3217. callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
  3218. callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
  3219. callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
  3220. call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
  3221. call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
  3222. call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
  3223. call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
  3224. }
  3225. }
  3226. static bool
  3227. is_indirect_call_opcode (xtensa_opcode opcode)
  3228. {
  3229. init_call_opcodes ();
  3230. return (opcode == callx0_op
  3231. || opcode == callx4_op
  3232. || opcode == callx8_op
  3233. || opcode == callx12_op);
  3234. }
  3235. static bool
  3236. is_direct_call_opcode (xtensa_opcode opcode)
  3237. {
  3238. init_call_opcodes ();
  3239. return (opcode == call0_op
  3240. || opcode == call4_op
  3241. || opcode == call8_op
  3242. || opcode == call12_op);
  3243. }
  3244. static bool
  3245. is_windowed_call_opcode (xtensa_opcode opcode)
  3246. {
  3247. init_call_opcodes ();
  3248. return (opcode == call4_op
  3249. || opcode == call8_op
  3250. || opcode == call12_op
  3251. || opcode == callx4_op
  3252. || opcode == callx8_op
  3253. || opcode == callx12_op);
  3254. }
  3255. static bool
  3256. get_indirect_call_dest_reg (xtensa_opcode opcode, unsigned *pdst)
  3257. {
  3258. unsigned dst = (unsigned) -1;
  3259. init_call_opcodes ();
  3260. if (opcode == callx0_op)
  3261. dst = 0;
  3262. else if (opcode == callx4_op)
  3263. dst = 4;
  3264. else if (opcode == callx8_op)
  3265. dst = 8;
  3266. else if (opcode == callx12_op)
  3267. dst = 12;
  3268. if (dst == (unsigned) -1)
  3269. return false;
  3270. *pdst = dst;
  3271. return true;
  3272. }
  3273. static xtensa_opcode
  3274. get_const16_opcode (void)
  3275. {
  3276. static bool done_lookup = false;
  3277. static xtensa_opcode const16_opcode = XTENSA_UNDEFINED;
  3278. if (!done_lookup)
  3279. {
  3280. const16_opcode = xtensa_opcode_lookup (xtensa_default_isa, "const16");
  3281. done_lookup = true;
  3282. }
  3283. return const16_opcode;
  3284. }
  3285. static xtensa_opcode
  3286. get_l32r_opcode (void)
  3287. {
  3288. static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
  3289. static bool done_lookup = false;
  3290. if (!done_lookup)
  3291. {
  3292. l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
  3293. done_lookup = true;
  3294. }
  3295. return l32r_opcode;
  3296. }
  3297. static bfd_vma
  3298. l32r_offset (bfd_vma addr, bfd_vma pc)
  3299. {
  3300. bfd_vma offset;
  3301. offset = addr - ((pc+3) & -4);
  3302. BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
  3303. offset = (signed int) offset >> 2;
  3304. BFD_ASSERT ((signed int) offset >> 16 == -1);
  3305. return offset;
  3306. }
  3307. static xtensa_opcode
  3308. get_rsr_lend_opcode (void)
  3309. {
  3310. static xtensa_opcode rsr_lend_opcode = XTENSA_UNDEFINED;
  3311. static bool done_lookup = false;
  3312. if (!done_lookup)
  3313. {
  3314. rsr_lend_opcode = xtensa_opcode_lookup (xtensa_default_isa, "rsr.lend");
  3315. done_lookup = true;
  3316. }
  3317. return rsr_lend_opcode;
  3318. }
  3319. static xtensa_opcode
  3320. get_wsr_lbeg_opcode (void)
  3321. {
  3322. static xtensa_opcode wsr_lbeg_opcode = XTENSA_UNDEFINED;
  3323. static bool done_lookup = false;
  3324. if (!done_lookup)
  3325. {
  3326. wsr_lbeg_opcode = xtensa_opcode_lookup (xtensa_default_isa, "wsr.lbeg");
  3327. done_lookup = true;
  3328. }
  3329. return wsr_lbeg_opcode;
  3330. }
  3331. static int
  3332. get_relocation_opnd (xtensa_opcode opcode, int r_type)
  3333. {
  3334. xtensa_isa isa = xtensa_default_isa;
  3335. int last_immed, last_opnd, opi;
  3336. if (opcode == XTENSA_UNDEFINED)
  3337. return XTENSA_UNDEFINED;
  3338. /* Find the last visible PC-relative immediate operand for the opcode.
  3339. If there are no PC-relative immediates, then choose the last visible
  3340. immediate; otherwise, fail and return XTENSA_UNDEFINED. */
  3341. last_immed = XTENSA_UNDEFINED;
  3342. last_opnd = xtensa_opcode_num_operands (isa, opcode);
  3343. for (opi = last_opnd - 1; opi >= 0; opi--)
  3344. {
  3345. if (xtensa_operand_is_visible (isa, opcode, opi) == 0)
  3346. continue;
  3347. if (xtensa_operand_is_PCrelative (isa, opcode, opi) == 1)
  3348. {
  3349. last_immed = opi;
  3350. break;
  3351. }
  3352. if (last_immed == XTENSA_UNDEFINED
  3353. && xtensa_operand_is_register (isa, opcode, opi) == 0)
  3354. last_immed = opi;
  3355. }
  3356. if (last_immed < 0)
  3357. return XTENSA_UNDEFINED;
  3358. /* If the operand number was specified in an old-style relocation,
  3359. check for consistency with the operand computed above. */
  3360. if (r_type >= R_XTENSA_OP0 && r_type <= R_XTENSA_OP2)
  3361. {
  3362. int reloc_opnd = r_type - R_XTENSA_OP0;
  3363. if (reloc_opnd != last_immed)
  3364. return XTENSA_UNDEFINED;
  3365. }
  3366. return last_immed;
  3367. }
  3368. int
  3369. get_relocation_slot (int r_type)
  3370. {
  3371. switch (r_type)
  3372. {
  3373. case R_XTENSA_OP0:
  3374. case R_XTENSA_OP1:
  3375. case R_XTENSA_OP2:
  3376. return 0;
  3377. default:
  3378. if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
  3379. return r_type - R_XTENSA_SLOT0_OP;
  3380. if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
  3381. return r_type - R_XTENSA_SLOT0_ALT;
  3382. break;
  3383. }
  3384. return XTENSA_UNDEFINED;
  3385. }
  3386. /* Get the opcode for a relocation. */
  3387. static xtensa_opcode
  3388. get_relocation_opcode (bfd *abfd,
  3389. asection *sec,
  3390. bfd_byte *contents,
  3391. Elf_Internal_Rela *irel)
  3392. {
  3393. static xtensa_insnbuf ibuff = NULL;
  3394. static xtensa_insnbuf sbuff = NULL;
  3395. xtensa_isa isa = xtensa_default_isa;
  3396. xtensa_format fmt;
  3397. int slot;
  3398. if (contents == NULL)
  3399. return XTENSA_UNDEFINED;
  3400. if (bfd_get_section_limit (abfd, sec) <= irel->r_offset)
  3401. return XTENSA_UNDEFINED;
  3402. if (ibuff == NULL)
  3403. {
  3404. ibuff = xtensa_insnbuf_alloc (isa);
  3405. sbuff = xtensa_insnbuf_alloc (isa);
  3406. }
  3407. /* Decode the instruction. */
  3408. xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset],
  3409. sec->size - irel->r_offset);
  3410. fmt = xtensa_format_decode (isa, ibuff);
  3411. slot = get_relocation_slot (ELF32_R_TYPE (irel->r_info));
  3412. if (slot == XTENSA_UNDEFINED)
  3413. return XTENSA_UNDEFINED;
  3414. xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
  3415. return xtensa_opcode_decode (isa, fmt, slot, sbuff);
  3416. }
  3417. bool
  3418. is_l32r_relocation (bfd *abfd,
  3419. asection *sec,
  3420. bfd_byte *contents,
  3421. Elf_Internal_Rela *irel)
  3422. {
  3423. xtensa_opcode opcode;
  3424. if (!is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
  3425. return false;
  3426. opcode = get_relocation_opcode (abfd, sec, contents, irel);
  3427. return (opcode == get_l32r_opcode ());
  3428. }
  3429. static bfd_size_type
  3430. get_asm_simplify_size (bfd_byte *contents,
  3431. bfd_size_type content_len,
  3432. bfd_size_type offset)
  3433. {
  3434. bfd_size_type insnlen, size = 0;
  3435. /* Decode the size of the next two instructions. */
  3436. insnlen = insn_decode_len (contents, content_len, offset);
  3437. if (insnlen == 0)
  3438. return 0;
  3439. size += insnlen;
  3440. insnlen = insn_decode_len (contents, content_len, offset + size);
  3441. if (insnlen == 0)
  3442. return 0;
  3443. size += insnlen;
  3444. return size;
  3445. }
  3446. bool
  3447. is_alt_relocation (int r_type)
  3448. {
  3449. return (r_type >= R_XTENSA_SLOT0_ALT
  3450. && r_type <= R_XTENSA_SLOT14_ALT);
  3451. }
  3452. bool
  3453. is_operand_relocation (int r_type)
  3454. {
  3455. switch (r_type)
  3456. {
  3457. case R_XTENSA_OP0:
  3458. case R_XTENSA_OP1:
  3459. case R_XTENSA_OP2:
  3460. return true;
  3461. default:
  3462. if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
  3463. return true;
  3464. if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
  3465. return true;
  3466. break;
  3467. }
  3468. return false;
  3469. }
  3470. #define MIN_INSN_LENGTH 2
  3471. /* Return 0 if it fails to decode. */
  3472. bfd_size_type
  3473. insn_decode_len (bfd_byte *contents,
  3474. bfd_size_type content_len,
  3475. bfd_size_type offset)
  3476. {
  3477. int insn_len;
  3478. xtensa_isa isa = xtensa_default_isa;
  3479. xtensa_format fmt;
  3480. static xtensa_insnbuf ibuff = NULL;
  3481. if (offset + MIN_INSN_LENGTH > content_len)
  3482. return 0;
  3483. if (ibuff == NULL)
  3484. ibuff = xtensa_insnbuf_alloc (isa);
  3485. xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
  3486. content_len - offset);
  3487. fmt = xtensa_format_decode (isa, ibuff);
  3488. if (fmt == XTENSA_UNDEFINED)
  3489. return 0;
  3490. insn_len = xtensa_format_length (isa, fmt);
  3491. if (insn_len == XTENSA_UNDEFINED)
  3492. return 0;
  3493. return insn_len;
  3494. }
  3495. int
  3496. insn_num_slots (bfd_byte *contents,
  3497. bfd_size_type content_len,
  3498. bfd_size_type offset)
  3499. {
  3500. xtensa_isa isa = xtensa_default_isa;
  3501. xtensa_format fmt;
  3502. static xtensa_insnbuf ibuff = NULL;
  3503. if (offset + MIN_INSN_LENGTH > content_len)
  3504. return XTENSA_UNDEFINED;
  3505. if (ibuff == NULL)
  3506. ibuff = xtensa_insnbuf_alloc (isa);
  3507. xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
  3508. content_len - offset);
  3509. fmt = xtensa_format_decode (isa, ibuff);
  3510. if (fmt == XTENSA_UNDEFINED)
  3511. return XTENSA_UNDEFINED;
  3512. return xtensa_format_num_slots (isa, fmt);
  3513. }
  3514. /* Decode the opcode for a single slot instruction.
  3515. Return 0 if it fails to decode or the instruction is multi-slot. */
  3516. xtensa_opcode
  3517. insn_decode_opcode (bfd_byte *contents,
  3518. bfd_size_type content_len,
  3519. bfd_size_type offset,
  3520. int slot)
  3521. {
  3522. xtensa_isa isa = xtensa_default_isa;
  3523. xtensa_format fmt;
  3524. static xtensa_insnbuf insnbuf = NULL;
  3525. static xtensa_insnbuf slotbuf = NULL;
  3526. if (offset + MIN_INSN_LENGTH > content_len)
  3527. return XTENSA_UNDEFINED;
  3528. if (insnbuf == NULL)
  3529. {
  3530. insnbuf = xtensa_insnbuf_alloc (isa);
  3531. slotbuf = xtensa_insnbuf_alloc (isa);
  3532. }
  3533. xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
  3534. content_len - offset);
  3535. fmt = xtensa_format_decode (isa, insnbuf);
  3536. if (fmt == XTENSA_UNDEFINED)
  3537. return XTENSA_UNDEFINED;
  3538. if (slot >= xtensa_format_num_slots (isa, fmt))
  3539. return XTENSA_UNDEFINED;
  3540. xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
  3541. return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
  3542. }
  3543. /* The offset is the offset in the contents.
  3544. The address is the address of that offset. */
  3545. static bool
  3546. check_branch_target_aligned (bfd_byte *contents,
  3547. bfd_size_type content_length,
  3548. bfd_vma offset,
  3549. bfd_vma address)
  3550. {
  3551. bfd_size_type insn_len = insn_decode_len (contents, content_length, offset);
  3552. if (insn_len == 0)
  3553. return false;
  3554. return check_branch_target_aligned_address (address, insn_len);
  3555. }
  3556. static bool
  3557. check_loop_aligned (bfd_byte *contents,
  3558. bfd_size_type content_length,
  3559. bfd_vma offset,
  3560. bfd_vma address)
  3561. {
  3562. bfd_size_type loop_len, insn_len;
  3563. xtensa_opcode opcode;
  3564. opcode = insn_decode_opcode (contents, content_length, offset, 0);
  3565. if (opcode == XTENSA_UNDEFINED
  3566. || xtensa_opcode_is_loop (xtensa_default_isa, opcode) != 1)
  3567. {
  3568. BFD_ASSERT (false);
  3569. return false;
  3570. }
  3571. loop_len = insn_decode_len (contents, content_length, offset);
  3572. insn_len = insn_decode_len (contents, content_length, offset + loop_len);
  3573. if (loop_len == 0 || insn_len == 0)
  3574. {
  3575. BFD_ASSERT (false);
  3576. return false;
  3577. }
  3578. /* If this is relaxed loop, analyze first instruction of the actual loop
  3579. body. It must be at offset 27 from the loop instruction address. */
  3580. if (insn_len == 3
  3581. && insn_num_slots (contents, content_length, offset + loop_len) == 1
  3582. && insn_decode_opcode (contents, content_length,
  3583. offset + loop_len, 0) == get_rsr_lend_opcode()
  3584. && insn_decode_len (contents, content_length, offset + loop_len + 3) == 3
  3585. && insn_num_slots (contents, content_length, offset + loop_len + 3) == 1
  3586. && insn_decode_opcode (contents, content_length,
  3587. offset + loop_len + 3, 0) == get_wsr_lbeg_opcode())
  3588. {
  3589. loop_len = 27;
  3590. insn_len = insn_decode_len (contents, content_length, offset + loop_len);
  3591. }
  3592. return check_branch_target_aligned_address (address + loop_len, insn_len);
  3593. }
  3594. static bool
  3595. check_branch_target_aligned_address (bfd_vma addr, int len)
  3596. {
  3597. if (len == 8)
  3598. return (addr % 8 == 0);
  3599. return ((addr >> 2) == ((addr + len - 1) >> 2));
  3600. }
  3601. /* Instruction widening and narrowing. */
  3602. /* When FLIX is available we need to access certain instructions only
  3603. when they are 16-bit or 24-bit instructions. This table caches
  3604. information about such instructions by walking through all the
  3605. opcodes and finding the smallest single-slot format into which each
  3606. can be encoded. */
  3607. static xtensa_format *op_single_fmt_table = NULL;
  3608. static void
  3609. init_op_single_format_table (void)
  3610. {
  3611. xtensa_isa isa = xtensa_default_isa;
  3612. xtensa_insnbuf ibuf;
  3613. xtensa_opcode opcode;
  3614. xtensa_format fmt;
  3615. int num_opcodes;
  3616. if (op_single_fmt_table)
  3617. return;
  3618. ibuf = xtensa_insnbuf_alloc (isa);
  3619. num_opcodes = xtensa_isa_num_opcodes (isa);
  3620. op_single_fmt_table = (xtensa_format *)
  3621. bfd_malloc (sizeof (xtensa_format) * num_opcodes);
  3622. for (opcode = 0; opcode < num_opcodes; opcode++)
  3623. {
  3624. op_single_fmt_table[opcode] = XTENSA_UNDEFINED;
  3625. for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
  3626. {
  3627. if (xtensa_format_num_slots (isa, fmt) == 1
  3628. && xtensa_opcode_encode (isa, fmt, 0, ibuf, opcode) == 0)
  3629. {
  3630. xtensa_opcode old_fmt = op_single_fmt_table[opcode];
  3631. int fmt_length = xtensa_format_length (isa, fmt);
  3632. if (old_fmt == XTENSA_UNDEFINED
  3633. || fmt_length < xtensa_format_length (isa, old_fmt))
  3634. op_single_fmt_table[opcode] = fmt;
  3635. }
  3636. }
  3637. }
  3638. xtensa_insnbuf_free (isa, ibuf);
  3639. }
  3640. static xtensa_format
  3641. get_single_format (xtensa_opcode opcode)
  3642. {
  3643. init_op_single_format_table ();
  3644. return op_single_fmt_table[opcode];
  3645. }
  3646. /* For the set of narrowable instructions we do NOT include the
  3647. narrowings beqz -> beqz.n or bnez -> bnez.n because of complexities
  3648. involved during linker relaxation that may require these to
  3649. re-expand in some conditions. Also, the narrowing "or" -> mov.n
  3650. requires special case code to ensure it only works when op1 == op2. */
  3651. struct string_pair
  3652. {
  3653. const char *wide;
  3654. const char *narrow;
  3655. };
  3656. const struct string_pair narrowable[] =
  3657. {
  3658. { "add", "add.n" },
  3659. { "addi", "addi.n" },
  3660. { "addmi", "addi.n" },
  3661. { "l32i", "l32i.n" },
  3662. { "movi", "movi.n" },
  3663. { "ret", "ret.n" },
  3664. { "retw", "retw.n" },
  3665. { "s32i", "s32i.n" },
  3666. { "or", "mov.n" } /* special case only when op1 == op2 */
  3667. };
  3668. const struct string_pair widenable[] =
  3669. {
  3670. { "add", "add.n" },
  3671. { "addi", "addi.n" },
  3672. { "addmi", "addi.n" },
  3673. { "beqz", "beqz.n" },
  3674. { "bnez", "bnez.n" },
  3675. { "l32i", "l32i.n" },
  3676. { "movi", "movi.n" },
  3677. { "ret", "ret.n" },
  3678. { "retw", "retw.n" },
  3679. { "s32i", "s32i.n" },
  3680. { "or", "mov.n" } /* special case only when op1 == op2 */
  3681. };
  3682. /* Check if an instruction can be "narrowed", i.e., changed from a standard
  3683. 3-byte instruction to a 2-byte "density" instruction. If it is valid,
  3684. return the instruction buffer holding the narrow instruction. Otherwise,
  3685. return 0. The set of valid narrowing are specified by a string table
  3686. but require some special case operand checks in some cases. */
  3687. static xtensa_insnbuf
  3688. can_narrow_instruction (xtensa_insnbuf slotbuf,
  3689. xtensa_format fmt,
  3690. xtensa_opcode opcode)
  3691. {
  3692. xtensa_isa isa = xtensa_default_isa;
  3693. xtensa_format o_fmt;
  3694. unsigned opi;
  3695. static xtensa_insnbuf o_insnbuf = NULL;
  3696. static xtensa_insnbuf o_slotbuf = NULL;
  3697. if (o_insnbuf == NULL)
  3698. {
  3699. o_insnbuf = xtensa_insnbuf_alloc (isa);
  3700. o_slotbuf = xtensa_insnbuf_alloc (isa);
  3701. }
  3702. for (opi = 0; opi < (sizeof (narrowable)/sizeof (struct string_pair)); opi++)
  3703. {
  3704. bool is_or = (strcmp ("or", narrowable[opi].wide) == 0);
  3705. if (opcode == xtensa_opcode_lookup (isa, narrowable[opi].wide))
  3706. {
  3707. uint32 value, newval;
  3708. int i, operand_count, o_operand_count;
  3709. xtensa_opcode o_opcode;
  3710. /* Address does not matter in this case. We might need to
  3711. fix it to handle branches/jumps. */
  3712. bfd_vma self_address = 0;
  3713. o_opcode = xtensa_opcode_lookup (isa, narrowable[opi].narrow);
  3714. if (o_opcode == XTENSA_UNDEFINED)
  3715. return 0;
  3716. o_fmt = get_single_format (o_opcode);
  3717. if (o_fmt == XTENSA_UNDEFINED)
  3718. return 0;
  3719. if (xtensa_format_length (isa, fmt) != 3
  3720. || xtensa_format_length (isa, o_fmt) != 2)
  3721. return 0;
  3722. xtensa_format_encode (isa, o_fmt, o_insnbuf);
  3723. operand_count = xtensa_opcode_num_operands (isa, opcode);
  3724. o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
  3725. if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
  3726. return 0;
  3727. if (!is_or)
  3728. {
  3729. if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
  3730. return 0;
  3731. }
  3732. else
  3733. {
  3734. uint32 rawval0, rawval1, rawval2;
  3735. if (o_operand_count + 1 != operand_count
  3736. || xtensa_operand_get_field (isa, opcode, 0,
  3737. fmt, 0, slotbuf, &rawval0) != 0
  3738. || xtensa_operand_get_field (isa, opcode, 1,
  3739. fmt, 0, slotbuf, &rawval1) != 0
  3740. || xtensa_operand_get_field (isa, opcode, 2,
  3741. fmt, 0, slotbuf, &rawval2) != 0
  3742. || rawval1 != rawval2
  3743. || rawval0 == rawval1 /* it is a nop */)
  3744. return 0;
  3745. }
  3746. for (i = 0; i < o_operand_count; ++i)
  3747. {
  3748. if (xtensa_operand_get_field (isa, opcode, i, fmt, 0,
  3749. slotbuf, &value)
  3750. || xtensa_operand_decode (isa, opcode, i, &value))
  3751. return 0;
  3752. /* PC-relative branches need adjustment, but
  3753. the PC-rel operand will always have a relocation. */
  3754. newval = value;
  3755. if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
  3756. self_address)
  3757. || xtensa_operand_encode (isa, o_opcode, i, &newval)
  3758. || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
  3759. o_slotbuf, newval))
  3760. return 0;
  3761. }
  3762. if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
  3763. return 0;
  3764. return o_insnbuf;
  3765. }
  3766. }
  3767. return 0;
  3768. }
  3769. /* Attempt to narrow an instruction. If the narrowing is valid, perform
  3770. the action in-place directly into the contents and return TRUE. Otherwise,
  3771. the return value is FALSE and the contents are not modified. */
  3772. static bool
  3773. narrow_instruction (bfd_byte *contents,
  3774. bfd_size_type content_length,
  3775. bfd_size_type offset)
  3776. {
  3777. xtensa_opcode opcode;
  3778. bfd_size_type insn_len;
  3779. xtensa_isa isa = xtensa_default_isa;
  3780. xtensa_format fmt;
  3781. xtensa_insnbuf o_insnbuf;
  3782. static xtensa_insnbuf insnbuf = NULL;
  3783. static xtensa_insnbuf slotbuf = NULL;
  3784. if (insnbuf == NULL)
  3785. {
  3786. insnbuf = xtensa_insnbuf_alloc (isa);
  3787. slotbuf = xtensa_insnbuf_alloc (isa);
  3788. }
  3789. BFD_ASSERT (offset < content_length);
  3790. if (content_length < 2)
  3791. return false;
  3792. /* We will hand-code a few of these for a little while.
  3793. These have all been specified in the assembler aleady. */
  3794. xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
  3795. content_length - offset);
  3796. fmt = xtensa_format_decode (isa, insnbuf);
  3797. if (xtensa_format_num_slots (isa, fmt) != 1)
  3798. return false;
  3799. if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
  3800. return false;
  3801. opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
  3802. if (opcode == XTENSA_UNDEFINED)
  3803. return false;
  3804. insn_len = xtensa_format_length (isa, fmt);
  3805. if (insn_len > content_length)
  3806. return false;
  3807. o_insnbuf = can_narrow_instruction (slotbuf, fmt, opcode);
  3808. if (o_insnbuf)
  3809. {
  3810. xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
  3811. content_length - offset);
  3812. return true;
  3813. }
  3814. return false;
  3815. }
  3816. /* Check if an instruction can be "widened", i.e., changed from a 2-byte
  3817. "density" instruction to a standard 3-byte instruction. If it is valid,
  3818. return the instruction buffer holding the wide instruction. Otherwise,
  3819. return 0. The set of valid widenings are specified by a string table
  3820. but require some special case operand checks in some cases. */
  3821. static xtensa_insnbuf
  3822. can_widen_instruction (xtensa_insnbuf slotbuf,
  3823. xtensa_format fmt,
  3824. xtensa_opcode opcode)
  3825. {
  3826. xtensa_isa isa = xtensa_default_isa;
  3827. xtensa_format o_fmt;
  3828. unsigned opi;
  3829. static xtensa_insnbuf o_insnbuf = NULL;
  3830. static xtensa_insnbuf o_slotbuf = NULL;
  3831. if (o_insnbuf == NULL)
  3832. {
  3833. o_insnbuf = xtensa_insnbuf_alloc (isa);
  3834. o_slotbuf = xtensa_insnbuf_alloc (isa);
  3835. }
  3836. for (opi = 0; opi < (sizeof (widenable)/sizeof (struct string_pair)); opi++)
  3837. {
  3838. bool is_or = (strcmp ("or", widenable[opi].wide) == 0);
  3839. bool is_branch = (strcmp ("beqz", widenable[opi].wide) == 0
  3840. || strcmp ("bnez", widenable[opi].wide) == 0);
  3841. if (opcode == xtensa_opcode_lookup (isa, widenable[opi].narrow))
  3842. {
  3843. uint32 value, newval;
  3844. int i, operand_count, o_operand_count, check_operand_count;
  3845. xtensa_opcode o_opcode;
  3846. /* Address does not matter in this case. We might need to fix it
  3847. to handle branches/jumps. */
  3848. bfd_vma self_address = 0;
  3849. o_opcode = xtensa_opcode_lookup (isa, widenable[opi].wide);
  3850. if (o_opcode == XTENSA_UNDEFINED)
  3851. return 0;
  3852. o_fmt = get_single_format (o_opcode);
  3853. if (o_fmt == XTENSA_UNDEFINED)
  3854. return 0;
  3855. if (xtensa_format_length (isa, fmt) != 2
  3856. || xtensa_format_length (isa, o_fmt) != 3)
  3857. return 0;
  3858. xtensa_format_encode (isa, o_fmt, o_insnbuf);
  3859. operand_count = xtensa_opcode_num_operands (isa, opcode);
  3860. o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
  3861. check_operand_count = o_operand_count;
  3862. if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
  3863. return 0;
  3864. if (!is_or)
  3865. {
  3866. if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
  3867. return 0;
  3868. }
  3869. else
  3870. {
  3871. uint32 rawval0, rawval1;
  3872. if (o_operand_count != operand_count + 1
  3873. || xtensa_operand_get_field (isa, opcode, 0,
  3874. fmt, 0, slotbuf, &rawval0) != 0
  3875. || xtensa_operand_get_field (isa, opcode, 1,
  3876. fmt, 0, slotbuf, &rawval1) != 0
  3877. || rawval0 == rawval1 /* it is a nop */)
  3878. return 0;
  3879. }
  3880. if (is_branch)
  3881. check_operand_count--;
  3882. for (i = 0; i < check_operand_count; i++)
  3883. {
  3884. int new_i = i;
  3885. if (is_or && i == o_operand_count - 1)
  3886. new_i = i - 1;
  3887. if (xtensa_operand_get_field (isa, opcode, new_i, fmt, 0,
  3888. slotbuf, &value)
  3889. || xtensa_operand_decode (isa, opcode, new_i, &value))
  3890. return 0;
  3891. /* PC-relative branches need adjustment, but
  3892. the PC-rel operand will always have a relocation. */
  3893. newval = value;
  3894. if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
  3895. self_address)
  3896. || xtensa_operand_encode (isa, o_opcode, i, &newval)
  3897. || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
  3898. o_slotbuf, newval))
  3899. return 0;
  3900. }
  3901. if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
  3902. return 0;
  3903. return o_insnbuf;
  3904. }
  3905. }
  3906. return 0;
  3907. }
  3908. /* Attempt to widen an instruction. If the widening is valid, perform
  3909. the action in-place directly into the contents and return TRUE. Otherwise,
  3910. the return value is FALSE and the contents are not modified. */
  3911. static bool
  3912. widen_instruction (bfd_byte *contents,
  3913. bfd_size_type content_length,
  3914. bfd_size_type offset)
  3915. {
  3916. xtensa_opcode opcode;
  3917. bfd_size_type insn_len;
  3918. xtensa_isa isa = xtensa_default_isa;
  3919. xtensa_format fmt;
  3920. xtensa_insnbuf o_insnbuf;
  3921. static xtensa_insnbuf insnbuf = NULL;
  3922. static xtensa_insnbuf slotbuf = NULL;
  3923. if (insnbuf == NULL)
  3924. {
  3925. insnbuf = xtensa_insnbuf_alloc (isa);
  3926. slotbuf = xtensa_insnbuf_alloc (isa);
  3927. }
  3928. BFD_ASSERT (offset < content_length);
  3929. if (content_length < 2)
  3930. return false;
  3931. /* We will hand-code a few of these for a little while.
  3932. These have all been specified in the assembler aleady. */
  3933. xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
  3934. content_length - offset);
  3935. fmt = xtensa_format_decode (isa, insnbuf);
  3936. if (xtensa_format_num_slots (isa, fmt) != 1)
  3937. return false;
  3938. if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
  3939. return false;
  3940. opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
  3941. if (opcode == XTENSA_UNDEFINED)
  3942. return false;
  3943. insn_len = xtensa_format_length (isa, fmt);
  3944. if (insn_len > content_length)
  3945. return false;
  3946. o_insnbuf = can_widen_instruction (slotbuf, fmt, opcode);
  3947. if (o_insnbuf)
  3948. {
  3949. xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
  3950. content_length - offset);
  3951. return true;
  3952. }
  3953. return false;
  3954. }
  3955. /* Code for transforming CALLs at link-time. */
  3956. static bfd_reloc_status_type
  3957. elf_xtensa_do_asm_simplify (bfd_byte *contents,
  3958. bfd_vma address,
  3959. bfd_vma content_length,
  3960. char **error_message)
  3961. {
  3962. static xtensa_insnbuf insnbuf = NULL;
  3963. static xtensa_insnbuf slotbuf = NULL;
  3964. xtensa_format core_format = XTENSA_UNDEFINED;
  3965. xtensa_opcode opcode;
  3966. xtensa_opcode direct_call_opcode;
  3967. xtensa_isa isa = xtensa_default_isa;
  3968. bfd_byte *chbuf = contents + address;
  3969. int opn;
  3970. if (insnbuf == NULL)
  3971. {
  3972. insnbuf = xtensa_insnbuf_alloc (isa);
  3973. slotbuf = xtensa_insnbuf_alloc (isa);
  3974. }
  3975. if (content_length < address)
  3976. {
  3977. *error_message = _("attempt to convert L32R/CALLX to CALL failed");
  3978. return bfd_reloc_other;
  3979. }
  3980. opcode = get_expanded_call_opcode (chbuf, content_length - address, 0);
  3981. direct_call_opcode = swap_callx_for_call_opcode (opcode);
  3982. if (direct_call_opcode == XTENSA_UNDEFINED)
  3983. {
  3984. *error_message = _("attempt to convert L32R/CALLX to CALL failed");
  3985. return bfd_reloc_other;
  3986. }
  3987. /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
  3988. core_format = xtensa_format_lookup (isa, "x24");
  3989. opcode = xtensa_opcode_lookup (isa, "or");
  3990. xtensa_opcode_encode (isa, core_format, 0, slotbuf, opcode);
  3991. for (opn = 0; opn < 3; opn++)
  3992. {
  3993. uint32 regno = 1;
  3994. xtensa_operand_encode (isa, opcode, opn, &regno);
  3995. xtensa_operand_set_field (isa, opcode, opn, core_format, 0,
  3996. slotbuf, regno);
  3997. }
  3998. xtensa_format_encode (isa, core_format, insnbuf);
  3999. xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
  4000. xtensa_insnbuf_to_chars (isa, insnbuf, chbuf, content_length - address);
  4001. /* Assemble a CALL ("callN 0") into the 3 byte offset. */
  4002. xtensa_opcode_encode (isa, core_format, 0, slotbuf, direct_call_opcode);
  4003. xtensa_operand_set_field (isa, opcode, 0, core_format, 0, slotbuf, 0);
  4004. xtensa_format_encode (isa, core_format, insnbuf);
  4005. xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
  4006. xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3,
  4007. content_length - address - 3);
  4008. return bfd_reloc_ok;
  4009. }
  4010. static bfd_reloc_status_type
  4011. contract_asm_expansion (bfd_byte *contents,
  4012. bfd_vma content_length,
  4013. Elf_Internal_Rela *irel,
  4014. char **error_message)
  4015. {
  4016. bfd_reloc_status_type retval =
  4017. elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length,
  4018. error_message);
  4019. if (retval != bfd_reloc_ok)
  4020. return bfd_reloc_dangerous;
  4021. /* Update the irel->r_offset field so that the right immediate and
  4022. the right instruction are modified during the relocation. */
  4023. irel->r_offset += 3;
  4024. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_SLOT0_OP);
  4025. return bfd_reloc_ok;
  4026. }
  4027. static xtensa_opcode
  4028. swap_callx_for_call_opcode (xtensa_opcode opcode)
  4029. {
  4030. init_call_opcodes ();
  4031. if (opcode == callx0_op) return call0_op;
  4032. if (opcode == callx4_op) return call4_op;
  4033. if (opcode == callx8_op) return call8_op;
  4034. if (opcode == callx12_op) return call12_op;
  4035. /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
  4036. return XTENSA_UNDEFINED;
  4037. }
  4038. /* Check if "buf" is pointing to a "L32R aN; CALLX aN" or "CONST16 aN;
  4039. CONST16 aN; CALLX aN" sequence, and if so, return the CALLX opcode.
  4040. If not, return XTENSA_UNDEFINED. */
  4041. #define L32R_TARGET_REG_OPERAND 0
  4042. #define CONST16_TARGET_REG_OPERAND 0
  4043. #define CALLN_SOURCE_OPERAND 0
  4044. static xtensa_opcode
  4045. get_expanded_call_opcode (bfd_byte *buf, int bufsize, bool *p_uses_l32r)
  4046. {
  4047. static xtensa_insnbuf insnbuf = NULL;
  4048. static xtensa_insnbuf slotbuf = NULL;
  4049. xtensa_format fmt;
  4050. xtensa_opcode opcode;
  4051. xtensa_isa isa = xtensa_default_isa;
  4052. uint32 regno, const16_regno, call_regno;
  4053. int offset = 0;
  4054. if (insnbuf == NULL)
  4055. {
  4056. insnbuf = xtensa_insnbuf_alloc (isa);
  4057. slotbuf = xtensa_insnbuf_alloc (isa);
  4058. }
  4059. xtensa_insnbuf_from_chars (isa, insnbuf, buf, bufsize);
  4060. fmt = xtensa_format_decode (isa, insnbuf);
  4061. if (fmt == XTENSA_UNDEFINED
  4062. || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
  4063. return XTENSA_UNDEFINED;
  4064. opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
  4065. if (opcode == XTENSA_UNDEFINED)
  4066. return XTENSA_UNDEFINED;
  4067. if (opcode == get_l32r_opcode ())
  4068. {
  4069. if (p_uses_l32r)
  4070. *p_uses_l32r = true;
  4071. if (xtensa_operand_get_field (isa, opcode, L32R_TARGET_REG_OPERAND,
  4072. fmt, 0, slotbuf, &regno)
  4073. || xtensa_operand_decode (isa, opcode, L32R_TARGET_REG_OPERAND,
  4074. &regno))
  4075. return XTENSA_UNDEFINED;
  4076. }
  4077. else if (opcode == get_const16_opcode ())
  4078. {
  4079. if (p_uses_l32r)
  4080. *p_uses_l32r = false;
  4081. if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
  4082. fmt, 0, slotbuf, &regno)
  4083. || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
  4084. &regno))
  4085. return XTENSA_UNDEFINED;
  4086. /* Check that the next instruction is also CONST16. */
  4087. offset += xtensa_format_length (isa, fmt);
  4088. xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
  4089. fmt = xtensa_format_decode (isa, insnbuf);
  4090. if (fmt == XTENSA_UNDEFINED
  4091. || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
  4092. return XTENSA_UNDEFINED;
  4093. opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
  4094. if (opcode != get_const16_opcode ())
  4095. return XTENSA_UNDEFINED;
  4096. if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
  4097. fmt, 0, slotbuf, &const16_regno)
  4098. || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
  4099. &const16_regno)
  4100. || const16_regno != regno)
  4101. return XTENSA_UNDEFINED;
  4102. }
  4103. else
  4104. return XTENSA_UNDEFINED;
  4105. /* Next instruction should be an CALLXn with operand 0 == regno. */
  4106. offset += xtensa_format_length (isa, fmt);
  4107. xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
  4108. fmt = xtensa_format_decode (isa, insnbuf);
  4109. if (fmt == XTENSA_UNDEFINED
  4110. || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
  4111. return XTENSA_UNDEFINED;
  4112. opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
  4113. if (opcode == XTENSA_UNDEFINED
  4114. || !is_indirect_call_opcode (opcode))
  4115. return XTENSA_UNDEFINED;
  4116. if (xtensa_operand_get_field (isa, opcode, CALLN_SOURCE_OPERAND,
  4117. fmt, 0, slotbuf, &call_regno)
  4118. || xtensa_operand_decode (isa, opcode, CALLN_SOURCE_OPERAND,
  4119. &call_regno))
  4120. return XTENSA_UNDEFINED;
  4121. if (call_regno != regno)
  4122. return XTENSA_UNDEFINED;
  4123. return opcode;
  4124. }
  4125. /* Data structures used during relaxation. */
  4126. /* r_reloc: relocation values. */
  4127. /* Through the relaxation process, we need to keep track of the values
  4128. that will result from evaluating relocations. The standard ELF
  4129. relocation structure is not sufficient for this purpose because we're
  4130. operating on multiple input files at once, so we need to know which
  4131. input file a relocation refers to. The r_reloc structure thus
  4132. records both the input file (bfd) and ELF relocation.
  4133. For efficiency, an r_reloc also contains a "target_offset" field to
  4134. cache the target-section-relative offset value that is represented by
  4135. the relocation.
  4136. The r_reloc also contains a virtual offset that allows multiple
  4137. inserted literals to be placed at the same "address" with
  4138. different offsets. */
  4139. typedef struct r_reloc_struct r_reloc;
  4140. struct r_reloc_struct
  4141. {
  4142. bfd *abfd;
  4143. Elf_Internal_Rela rela;
  4144. bfd_vma target_offset;
  4145. bfd_vma virtual_offset;
  4146. };
  4147. /* The r_reloc structure is included by value in literal_value, but not
  4148. every literal_value has an associated relocation -- some are simple
  4149. constants. In such cases, we set all the fields in the r_reloc
  4150. struct to zero. The r_reloc_is_const function should be used to
  4151. detect this case. */
  4152. static bool
  4153. r_reloc_is_const (const r_reloc *r_rel)
  4154. {
  4155. return (r_rel->abfd == NULL);
  4156. }
  4157. static bfd_vma
  4158. r_reloc_get_target_offset (const r_reloc *r_rel)
  4159. {
  4160. bfd_vma target_offset;
  4161. unsigned long r_symndx;
  4162. BFD_ASSERT (!r_reloc_is_const (r_rel));
  4163. r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
  4164. target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
  4165. return (target_offset + r_rel->rela.r_addend);
  4166. }
  4167. static struct elf_link_hash_entry *
  4168. r_reloc_get_hash_entry (const r_reloc *r_rel)
  4169. {
  4170. unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
  4171. return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
  4172. }
  4173. static asection *
  4174. r_reloc_get_section (const r_reloc *r_rel)
  4175. {
  4176. unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
  4177. return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
  4178. }
  4179. static bool
  4180. r_reloc_is_defined (const r_reloc *r_rel)
  4181. {
  4182. asection *sec;
  4183. if (r_rel == NULL)
  4184. return false;
  4185. sec = r_reloc_get_section (r_rel);
  4186. if (sec == bfd_abs_section_ptr
  4187. || sec == bfd_com_section_ptr
  4188. || sec == bfd_und_section_ptr)
  4189. return false;
  4190. return true;
  4191. }
  4192. static void
  4193. r_reloc_init (r_reloc *r_rel,
  4194. bfd *abfd,
  4195. Elf_Internal_Rela *irel,
  4196. bfd_byte *contents,
  4197. bfd_size_type content_length)
  4198. {
  4199. int r_type;
  4200. reloc_howto_type *howto;
  4201. if (irel)
  4202. {
  4203. r_rel->rela = *irel;
  4204. r_rel->abfd = abfd;
  4205. r_rel->target_offset = r_reloc_get_target_offset (r_rel);
  4206. r_rel->virtual_offset = 0;
  4207. r_type = ELF32_R_TYPE (r_rel->rela.r_info);
  4208. howto = &elf_howto_table[r_type];
  4209. if (howto->partial_inplace)
  4210. {
  4211. bfd_vma inplace_val;
  4212. BFD_ASSERT (r_rel->rela.r_offset < content_length);
  4213. inplace_val = bfd_get_32 (abfd, &contents[r_rel->rela.r_offset]);
  4214. r_rel->target_offset += inplace_val;
  4215. }
  4216. }
  4217. else
  4218. memset (r_rel, 0, sizeof (r_reloc));
  4219. }
  4220. #if DEBUG
  4221. static void
  4222. print_r_reloc (FILE *fp, const r_reloc *r_rel)
  4223. {
  4224. if (r_reloc_is_defined (r_rel))
  4225. {
  4226. asection *sec = r_reloc_get_section (r_rel);
  4227. fprintf (fp, " %s(%s + ", sec->owner->filename, sec->name);
  4228. }
  4229. else if (r_reloc_get_hash_entry (r_rel))
  4230. fprintf (fp, " %s + ", r_reloc_get_hash_entry (r_rel)->root.root.string);
  4231. else
  4232. fprintf (fp, " ?? + ");
  4233. fprintf_vma (fp, r_rel->target_offset);
  4234. if (r_rel->virtual_offset)
  4235. {
  4236. fprintf (fp, " + ");
  4237. fprintf_vma (fp, r_rel->virtual_offset);
  4238. }
  4239. fprintf (fp, ")");
  4240. }
  4241. #endif /* DEBUG */
  4242. /* source_reloc: relocations that reference literals. */
  4243. /* To determine whether literals can be coalesced, we need to first
  4244. record all the relocations that reference the literals. The
  4245. source_reloc structure below is used for this purpose. The
  4246. source_reloc entries are kept in a per-literal-section array, sorted
  4247. by offset within the literal section (i.e., target offset).
  4248. The source_sec and r_rel.rela.r_offset fields identify the source of
  4249. the relocation. The r_rel field records the relocation value, i.e.,
  4250. the offset of the literal being referenced. The opnd field is needed
  4251. to determine the range of the immediate field to which the relocation
  4252. applies, so we can determine whether another literal with the same
  4253. value is within range. The is_null field is true when the relocation
  4254. is being removed (e.g., when an L32R is being removed due to a CALLX
  4255. that is converted to a direct CALL). */
  4256. typedef struct source_reloc_struct source_reloc;
  4257. struct source_reloc_struct
  4258. {
  4259. asection *source_sec;
  4260. r_reloc r_rel;
  4261. xtensa_opcode opcode;
  4262. int opnd;
  4263. bool is_null;
  4264. bool is_abs_literal;
  4265. };
  4266. static void
  4267. init_source_reloc (source_reloc *reloc,
  4268. asection *source_sec,
  4269. const r_reloc *r_rel,
  4270. xtensa_opcode opcode,
  4271. int opnd,
  4272. bool is_abs_literal)
  4273. {
  4274. reloc->source_sec = source_sec;
  4275. reloc->r_rel = *r_rel;
  4276. reloc->opcode = opcode;
  4277. reloc->opnd = opnd;
  4278. reloc->is_null = false;
  4279. reloc->is_abs_literal = is_abs_literal;
  4280. }
  4281. /* Find the source_reloc for a particular source offset and relocation
  4282. type. Note that the array is sorted by _target_ offset, so this is
  4283. just a linear search. */
  4284. static source_reloc *
  4285. find_source_reloc (source_reloc *src_relocs,
  4286. int src_count,
  4287. asection *sec,
  4288. Elf_Internal_Rela *irel)
  4289. {
  4290. int i;
  4291. for (i = 0; i < src_count; i++)
  4292. {
  4293. if (src_relocs[i].source_sec == sec
  4294. && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
  4295. && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
  4296. == ELF32_R_TYPE (irel->r_info)))
  4297. return &src_relocs[i];
  4298. }
  4299. return NULL;
  4300. }
  4301. static int
  4302. source_reloc_compare (const void *ap, const void *bp)
  4303. {
  4304. const source_reloc *a = (const source_reloc *) ap;
  4305. const source_reloc *b = (const source_reloc *) bp;
  4306. if (a->r_rel.target_offset != b->r_rel.target_offset)
  4307. return (a->r_rel.target_offset - b->r_rel.target_offset);
  4308. /* We don't need to sort on these criteria for correctness,
  4309. but enforcing a more strict ordering prevents unstable qsort
  4310. from behaving differently with different implementations.
  4311. Without the code below we get correct but different results
  4312. on Solaris 2.7 and 2.8. We would like to always produce the
  4313. same results no matter the host. */
  4314. if ((!a->is_null) - (!b->is_null))
  4315. return ((!a->is_null) - (!b->is_null));
  4316. return internal_reloc_compare (&a->r_rel.rela, &b->r_rel.rela);
  4317. }
  4318. /* Literal values and value hash tables. */
  4319. /* Literals with the same value can be coalesced. The literal_value
  4320. structure records the value of a literal: the "r_rel" field holds the
  4321. information from the relocation on the literal (if there is one) and
  4322. the "value" field holds the contents of the literal word itself.
  4323. The value_map structure records a literal value along with the
  4324. location of a literal holding that value. The value_map hash table
  4325. is indexed by the literal value, so that we can quickly check if a
  4326. particular literal value has been seen before and is thus a candidate
  4327. for coalescing. */
  4328. typedef struct literal_value_struct literal_value;
  4329. typedef struct value_map_struct value_map;
  4330. typedef struct value_map_hash_table_struct value_map_hash_table;
  4331. struct literal_value_struct
  4332. {
  4333. r_reloc r_rel;
  4334. unsigned long value;
  4335. bool is_abs_literal;
  4336. };
  4337. struct value_map_struct
  4338. {
  4339. literal_value val; /* The literal value. */
  4340. r_reloc loc; /* Location of the literal. */
  4341. value_map *next;
  4342. };
  4343. struct value_map_hash_table_struct
  4344. {
  4345. unsigned bucket_count;
  4346. value_map **buckets;
  4347. unsigned count;
  4348. bool has_last_loc;
  4349. r_reloc last_loc;
  4350. };
  4351. static void
  4352. init_literal_value (literal_value *lit,
  4353. const r_reloc *r_rel,
  4354. unsigned long value,
  4355. bool is_abs_literal)
  4356. {
  4357. lit->r_rel = *r_rel;
  4358. lit->value = value;
  4359. lit->is_abs_literal = is_abs_literal;
  4360. }
  4361. static bool
  4362. literal_value_equal (const literal_value *src1,
  4363. const literal_value *src2,
  4364. bool final_static_link)
  4365. {
  4366. struct elf_link_hash_entry *h1, *h2;
  4367. if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
  4368. return false;
  4369. if (r_reloc_is_const (&src1->r_rel))
  4370. return (src1->value == src2->value);
  4371. if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
  4372. != ELF32_R_TYPE (src2->r_rel.rela.r_info))
  4373. return false;
  4374. if (src1->r_rel.target_offset != src2->r_rel.target_offset)
  4375. return false;
  4376. if (src1->r_rel.virtual_offset != src2->r_rel.virtual_offset)
  4377. return false;
  4378. if (src1->value != src2->value)
  4379. return false;
  4380. /* Now check for the same section (if defined) or the same elf_hash
  4381. (if undefined or weak). */
  4382. h1 = r_reloc_get_hash_entry (&src1->r_rel);
  4383. h2 = r_reloc_get_hash_entry (&src2->r_rel);
  4384. if (r_reloc_is_defined (&src1->r_rel)
  4385. && (final_static_link
  4386. || ((!h1 || h1->root.type != bfd_link_hash_defweak)
  4387. && (!h2 || h2->root.type != bfd_link_hash_defweak))))
  4388. {
  4389. if (r_reloc_get_section (&src1->r_rel)
  4390. != r_reloc_get_section (&src2->r_rel))
  4391. return false;
  4392. }
  4393. else
  4394. {
  4395. /* Require that the hash entries (i.e., symbols) be identical. */
  4396. if (h1 != h2 || h1 == 0)
  4397. return false;
  4398. }
  4399. if (src1->is_abs_literal != src2->is_abs_literal)
  4400. return false;
  4401. return true;
  4402. }
  4403. /* Must be power of 2. */
  4404. #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
  4405. static value_map_hash_table *
  4406. value_map_hash_table_init (void)
  4407. {
  4408. value_map_hash_table *values;
  4409. values = (value_map_hash_table *)
  4410. bfd_zmalloc (sizeof (value_map_hash_table));
  4411. values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
  4412. values->count = 0;
  4413. values->buckets = (value_map **)
  4414. bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
  4415. if (values->buckets == NULL)
  4416. {
  4417. free (values);
  4418. return NULL;
  4419. }
  4420. values->has_last_loc = false;
  4421. return values;
  4422. }
  4423. static void
  4424. value_map_hash_table_delete (value_map_hash_table *table)
  4425. {
  4426. free (table->buckets);
  4427. free (table);
  4428. }
  4429. static unsigned
  4430. hash_bfd_vma (bfd_vma val)
  4431. {
  4432. return (val >> 2) + (val >> 10);
  4433. }
  4434. static unsigned
  4435. literal_value_hash (const literal_value *src)
  4436. {
  4437. unsigned hash_val;
  4438. hash_val = hash_bfd_vma (src->value);
  4439. if (!r_reloc_is_const (&src->r_rel))
  4440. {
  4441. void *sec_or_hash;
  4442. hash_val += hash_bfd_vma (src->is_abs_literal * 1000);
  4443. hash_val += hash_bfd_vma (src->r_rel.target_offset);
  4444. hash_val += hash_bfd_vma (src->r_rel.virtual_offset);
  4445. /* Now check for the same section and the same elf_hash. */
  4446. if (r_reloc_is_defined (&src->r_rel))
  4447. sec_or_hash = r_reloc_get_section (&src->r_rel);
  4448. else
  4449. sec_or_hash = r_reloc_get_hash_entry (&src->r_rel);
  4450. hash_val += hash_bfd_vma ((bfd_vma) (size_t) sec_or_hash);
  4451. }
  4452. return hash_val;
  4453. }
  4454. /* Check if the specified literal_value has been seen before. */
  4455. static value_map *
  4456. value_map_get_cached_value (value_map_hash_table *map,
  4457. const literal_value *val,
  4458. bool final_static_link)
  4459. {
  4460. value_map *map_e;
  4461. value_map *bucket;
  4462. unsigned idx;
  4463. idx = literal_value_hash (val);
  4464. idx = idx & (map->bucket_count - 1);
  4465. bucket = map->buckets[idx];
  4466. for (map_e = bucket; map_e; map_e = map_e->next)
  4467. {
  4468. if (literal_value_equal (&map_e->val, val, final_static_link))
  4469. return map_e;
  4470. }
  4471. return NULL;
  4472. }
  4473. /* Record a new literal value. It is illegal to call this if VALUE
  4474. already has an entry here. */
  4475. static value_map *
  4476. add_value_map (value_map_hash_table *map,
  4477. const literal_value *val,
  4478. const r_reloc *loc,
  4479. bool final_static_link)
  4480. {
  4481. value_map **bucket_p;
  4482. unsigned idx;
  4483. value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
  4484. if (val_e == NULL)
  4485. {
  4486. bfd_set_error (bfd_error_no_memory);
  4487. return NULL;
  4488. }
  4489. BFD_ASSERT (!value_map_get_cached_value (map, val, final_static_link));
  4490. val_e->val = *val;
  4491. val_e->loc = *loc;
  4492. idx = literal_value_hash (val);
  4493. idx = idx & (map->bucket_count - 1);
  4494. bucket_p = &map->buckets[idx];
  4495. val_e->next = *bucket_p;
  4496. *bucket_p = val_e;
  4497. map->count++;
  4498. /* FIXME: Consider resizing the hash table if we get too many entries. */
  4499. return val_e;
  4500. }
  4501. /* Lists of text actions (ta_) for narrowing, widening, longcall
  4502. conversion, space fill, code & literal removal, etc. */
  4503. /* The following text actions are generated:
  4504. "ta_remove_insn" remove an instruction or instructions
  4505. "ta_remove_longcall" convert longcall to call
  4506. "ta_convert_longcall" convert longcall to nop/call
  4507. "ta_narrow_insn" narrow a wide instruction
  4508. "ta_widen" widen a narrow instruction
  4509. "ta_fill" add fill or remove fill
  4510. removed < 0 is a fill; branches to the fill address will be
  4511. changed to address + fill size (e.g., address - removed)
  4512. removed >= 0 branches to the fill address will stay unchanged
  4513. "ta_remove_literal" remove a literal; this action is
  4514. indicated when a literal is removed
  4515. or replaced.
  4516. "ta_add_literal" insert a new literal; this action is
  4517. indicated when a literal has been moved.
  4518. It may use a virtual_offset because
  4519. multiple literals can be placed at the
  4520. same location.
  4521. For each of these text actions, we also record the number of bytes
  4522. removed by performing the text action. In the case of a "ta_widen"
  4523. or a "ta_fill" that adds space, the removed_bytes will be negative. */
  4524. typedef struct text_action_struct text_action;
  4525. typedef struct text_action_list_struct text_action_list;
  4526. typedef enum text_action_enum_t text_action_t;
  4527. enum text_action_enum_t
  4528. {
  4529. ta_none,
  4530. ta_remove_insn, /* removed = -size */
  4531. ta_remove_longcall, /* removed = -size */
  4532. ta_convert_longcall, /* removed = 0 */
  4533. ta_narrow_insn, /* removed = -1 */
  4534. ta_widen_insn, /* removed = +1 */
  4535. ta_fill, /* removed = +size */
  4536. ta_remove_literal,
  4537. ta_add_literal
  4538. };
  4539. /* Structure for a text action record. */
  4540. struct text_action_struct
  4541. {
  4542. text_action_t action;
  4543. asection *sec; /* Optional */
  4544. bfd_vma offset;
  4545. bfd_vma virtual_offset; /* Zero except for adding literals. */
  4546. int removed_bytes;
  4547. literal_value value; /* Only valid when adding literals. */
  4548. };
  4549. struct removal_by_action_entry_struct
  4550. {
  4551. bfd_vma offset;
  4552. int removed;
  4553. int eq_removed;
  4554. int eq_removed_before_fill;
  4555. };
  4556. typedef struct removal_by_action_entry_struct removal_by_action_entry;
  4557. struct removal_by_action_map_struct
  4558. {
  4559. unsigned n_entries;
  4560. removal_by_action_entry *entry;
  4561. };
  4562. typedef struct removal_by_action_map_struct removal_by_action_map;
  4563. /* List of all of the actions taken on a text section. */
  4564. struct text_action_list_struct
  4565. {
  4566. unsigned count;
  4567. splay_tree tree;
  4568. removal_by_action_map map;
  4569. };
  4570. static text_action *
  4571. find_fill_action (text_action_list *l, asection *sec, bfd_vma offset)
  4572. {
  4573. text_action a;
  4574. /* It is not necessary to fill at the end of a section. */
  4575. if (sec->size == offset)
  4576. return NULL;
  4577. a.offset = offset;
  4578. a.action = ta_fill;
  4579. splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
  4580. if (node)
  4581. return (text_action *)node->value;
  4582. return NULL;
  4583. }
  4584. static int
  4585. compute_removed_action_diff (const text_action *ta,
  4586. asection *sec,
  4587. bfd_vma offset,
  4588. int removed,
  4589. int removable_space)
  4590. {
  4591. int new_removed;
  4592. int current_removed = 0;
  4593. if (ta)
  4594. current_removed = ta->removed_bytes;
  4595. BFD_ASSERT (ta == NULL || ta->offset == offset);
  4596. BFD_ASSERT (ta == NULL || ta->action == ta_fill);
  4597. /* It is not necessary to fill at the end of a section. Clean this up. */
  4598. if (sec->size == offset)
  4599. new_removed = removable_space - 0;
  4600. else
  4601. {
  4602. int space;
  4603. int added = -removed - current_removed;
  4604. /* Ignore multiples of the section alignment. */
  4605. added = ((1 << sec->alignment_power) - 1) & added;
  4606. new_removed = (-added);
  4607. /* Modify for removable. */
  4608. space = removable_space - new_removed;
  4609. new_removed = (removable_space
  4610. - (((1 << sec->alignment_power) - 1) & space));
  4611. }
  4612. return (new_removed - current_removed);
  4613. }
  4614. static void
  4615. adjust_fill_action (text_action *ta, int fill_diff)
  4616. {
  4617. ta->removed_bytes += fill_diff;
  4618. }
  4619. static int
  4620. text_action_compare (splay_tree_key a, splay_tree_key b)
  4621. {
  4622. text_action *pa = (text_action *)a;
  4623. text_action *pb = (text_action *)b;
  4624. static const int action_priority[] =
  4625. {
  4626. [ta_fill] = 0,
  4627. [ta_none] = 1,
  4628. [ta_convert_longcall] = 2,
  4629. [ta_narrow_insn] = 3,
  4630. [ta_remove_insn] = 4,
  4631. [ta_remove_longcall] = 5,
  4632. [ta_remove_literal] = 6,
  4633. [ta_widen_insn] = 7,
  4634. [ta_add_literal] = 8,
  4635. };
  4636. if (pa->offset == pb->offset)
  4637. {
  4638. if (pa->action == pb->action)
  4639. return 0;
  4640. return action_priority[pa->action] - action_priority[pb->action];
  4641. }
  4642. else
  4643. return pa->offset < pb->offset ? -1 : 1;
  4644. }
  4645. static text_action *
  4646. action_first (text_action_list *action_list)
  4647. {
  4648. splay_tree_node node = splay_tree_min (action_list->tree);
  4649. return node ? (text_action *)node->value : NULL;
  4650. }
  4651. static text_action *
  4652. action_next (text_action_list *action_list, text_action *action)
  4653. {
  4654. splay_tree_node node = splay_tree_successor (action_list->tree,
  4655. (splay_tree_key)action);
  4656. return node ? (text_action *)node->value : NULL;
  4657. }
  4658. /* Add a modification action to the text. For the case of adding or
  4659. removing space, modify any current fill and assume that
  4660. "unreachable_space" bytes can be freely contracted. Note that a
  4661. negative removed value is a fill. */
  4662. static void
  4663. text_action_add (text_action_list *l,
  4664. text_action_t action,
  4665. asection *sec,
  4666. bfd_vma offset,
  4667. int removed)
  4668. {
  4669. text_action *ta;
  4670. text_action a;
  4671. /* It is not necessary to fill at the end of a section. */
  4672. if (action == ta_fill && sec->size == offset)
  4673. return;
  4674. /* It is not necessary to fill 0 bytes. */
  4675. if (action == ta_fill && removed == 0)
  4676. return;
  4677. a.action = action;
  4678. a.offset = offset;
  4679. if (action == ta_fill)
  4680. {
  4681. splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
  4682. if (node)
  4683. {
  4684. ta = (text_action *)node->value;
  4685. ta->removed_bytes += removed;
  4686. return;
  4687. }
  4688. }
  4689. else
  4690. BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)&a) == NULL);
  4691. ta = (text_action *) bfd_zmalloc (sizeof (text_action));
  4692. ta->action = action;
  4693. ta->sec = sec;
  4694. ta->offset = offset;
  4695. ta->removed_bytes = removed;
  4696. splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
  4697. ++l->count;
  4698. }
  4699. static void
  4700. text_action_add_literal (text_action_list *l,
  4701. text_action_t action,
  4702. const r_reloc *loc,
  4703. const literal_value *value,
  4704. int removed)
  4705. {
  4706. text_action *ta;
  4707. asection *sec = r_reloc_get_section (loc);
  4708. bfd_vma offset = loc->target_offset;
  4709. bfd_vma virtual_offset = loc->virtual_offset;
  4710. BFD_ASSERT (action == ta_add_literal);
  4711. /* Create a new record and fill it up. */
  4712. ta = (text_action *) bfd_zmalloc (sizeof (text_action));
  4713. ta->action = action;
  4714. ta->sec = sec;
  4715. ta->offset = offset;
  4716. ta->virtual_offset = virtual_offset;
  4717. ta->value = *value;
  4718. ta->removed_bytes = removed;
  4719. BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)ta) == NULL);
  4720. splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
  4721. ++l->count;
  4722. }
  4723. /* Find the total offset adjustment for the relaxations specified by
  4724. text_actions, beginning from a particular starting action. This is
  4725. typically used from offset_with_removed_text to search an entire list of
  4726. actions, but it may also be called directly when adjusting adjacent offsets
  4727. so that each search may begin where the previous one left off. */
  4728. static int
  4729. removed_by_actions (text_action_list *action_list,
  4730. text_action **p_start_action,
  4731. bfd_vma offset,
  4732. bool before_fill)
  4733. {
  4734. text_action *r;
  4735. int removed = 0;
  4736. r = *p_start_action;
  4737. if (r)
  4738. {
  4739. splay_tree_node node = splay_tree_lookup (action_list->tree,
  4740. (splay_tree_key)r);
  4741. BFD_ASSERT (node != NULL && r == (text_action *)node->value);
  4742. }
  4743. while (r)
  4744. {
  4745. if (r->offset > offset)
  4746. break;
  4747. if (r->offset == offset
  4748. && (before_fill || r->action != ta_fill || r->removed_bytes >= 0))
  4749. break;
  4750. removed += r->removed_bytes;
  4751. r = action_next (action_list, r);
  4752. }
  4753. *p_start_action = r;
  4754. return removed;
  4755. }
  4756. static bfd_vma
  4757. offset_with_removed_text (text_action_list *action_list, bfd_vma offset)
  4758. {
  4759. text_action *r = action_first (action_list);
  4760. return offset - removed_by_actions (action_list, &r, offset, false);
  4761. }
  4762. static unsigned
  4763. action_list_count (text_action_list *action_list)
  4764. {
  4765. return action_list->count;
  4766. }
  4767. typedef struct map_action_fn_context_struct map_action_fn_context;
  4768. struct map_action_fn_context_struct
  4769. {
  4770. int removed;
  4771. removal_by_action_map map;
  4772. bool eq_complete;
  4773. };
  4774. static int
  4775. map_action_fn (splay_tree_node node, void *p)
  4776. {
  4777. map_action_fn_context *ctx = p;
  4778. text_action *r = (text_action *)node->value;
  4779. removal_by_action_entry *ientry = ctx->map.entry + ctx->map.n_entries;
  4780. if (ctx->map.n_entries && (ientry - 1)->offset == r->offset)
  4781. {
  4782. --ientry;
  4783. }
  4784. else
  4785. {
  4786. ++ctx->map.n_entries;
  4787. ctx->eq_complete = false;
  4788. ientry->offset = r->offset;
  4789. ientry->eq_removed_before_fill = ctx->removed;
  4790. }
  4791. if (!ctx->eq_complete)
  4792. {
  4793. if (r->action != ta_fill || r->removed_bytes >= 0)
  4794. {
  4795. ientry->eq_removed = ctx->removed;
  4796. ctx->eq_complete = true;
  4797. }
  4798. else
  4799. ientry->eq_removed = ctx->removed + r->removed_bytes;
  4800. }
  4801. ctx->removed += r->removed_bytes;
  4802. ientry->removed = ctx->removed;
  4803. return 0;
  4804. }
  4805. static void
  4806. map_removal_by_action (text_action_list *action_list)
  4807. {
  4808. map_action_fn_context ctx;
  4809. ctx.removed = 0;
  4810. ctx.map.n_entries = 0;
  4811. ctx.map.entry = bfd_malloc (action_list_count (action_list) *
  4812. sizeof (removal_by_action_entry));
  4813. ctx.eq_complete = false;
  4814. splay_tree_foreach (action_list->tree, map_action_fn, &ctx);
  4815. action_list->map = ctx.map;
  4816. }
  4817. static int
  4818. removed_by_actions_map (text_action_list *action_list, bfd_vma offset,
  4819. bool before_fill)
  4820. {
  4821. unsigned a, b;
  4822. if (!action_list->map.entry)
  4823. map_removal_by_action (action_list);
  4824. if (!action_list->map.n_entries)
  4825. return 0;
  4826. a = 0;
  4827. b = action_list->map.n_entries;
  4828. while (b - a > 1)
  4829. {
  4830. unsigned c = (a + b) / 2;
  4831. if (action_list->map.entry[c].offset <= offset)
  4832. a = c;
  4833. else
  4834. b = c;
  4835. }
  4836. if (action_list->map.entry[a].offset < offset)
  4837. {
  4838. return action_list->map.entry[a].removed;
  4839. }
  4840. else if (action_list->map.entry[a].offset == offset)
  4841. {
  4842. return before_fill ?
  4843. action_list->map.entry[a].eq_removed_before_fill :
  4844. action_list->map.entry[a].eq_removed;
  4845. }
  4846. else
  4847. {
  4848. return 0;
  4849. }
  4850. }
  4851. static bfd_vma
  4852. offset_with_removed_text_map (text_action_list *action_list, bfd_vma offset)
  4853. {
  4854. int removed = removed_by_actions_map (action_list, offset, false);
  4855. return offset - removed;
  4856. }
  4857. /* The find_insn_action routine will only find non-fill actions. */
  4858. static text_action *
  4859. find_insn_action (text_action_list *action_list, bfd_vma offset)
  4860. {
  4861. static const text_action_t action[] =
  4862. {
  4863. ta_convert_longcall,
  4864. ta_remove_longcall,
  4865. ta_widen_insn,
  4866. ta_narrow_insn,
  4867. ta_remove_insn,
  4868. };
  4869. text_action a;
  4870. unsigned i;
  4871. a.offset = offset;
  4872. for (i = 0; i < sizeof (action) / sizeof (*action); ++i)
  4873. {
  4874. splay_tree_node node;
  4875. a.action = action[i];
  4876. node = splay_tree_lookup (action_list->tree, (splay_tree_key)&a);
  4877. if (node)
  4878. return (text_action *)node->value;
  4879. }
  4880. return NULL;
  4881. }
  4882. #if DEBUG
  4883. static void
  4884. print_action (FILE *fp, text_action *r)
  4885. {
  4886. const char *t = "unknown";
  4887. switch (r->action)
  4888. {
  4889. case ta_remove_insn:
  4890. t = "remove_insn"; break;
  4891. case ta_remove_longcall:
  4892. t = "remove_longcall"; break;
  4893. case ta_convert_longcall:
  4894. t = "convert_longcall"; break;
  4895. case ta_narrow_insn:
  4896. t = "narrow_insn"; break;
  4897. case ta_widen_insn:
  4898. t = "widen_insn"; break;
  4899. case ta_fill:
  4900. t = "fill"; break;
  4901. case ta_none:
  4902. t = "none"; break;
  4903. case ta_remove_literal:
  4904. t = "remove_literal"; break;
  4905. case ta_add_literal:
  4906. t = "add_literal"; break;
  4907. }
  4908. fprintf (fp, "%s: %s[0x%lx] \"%s\" %d\n",
  4909. r->sec->owner->filename,
  4910. r->sec->name, (unsigned long) r->offset, t, r->removed_bytes);
  4911. }
  4912. static int
  4913. print_action_list_fn (splay_tree_node node, void *p)
  4914. {
  4915. text_action *r = (text_action *)node->value;
  4916. print_action (p, r);
  4917. return 0;
  4918. }
  4919. static void
  4920. print_action_list (FILE *fp, text_action_list *action_list)
  4921. {
  4922. fprintf (fp, "Text Action\n");
  4923. splay_tree_foreach (action_list->tree, print_action_list_fn, fp);
  4924. }
  4925. #endif /* DEBUG */
  4926. /* Lists of literals being coalesced or removed. */
  4927. /* In the usual case, the literal identified by "from" is being
  4928. coalesced with another literal identified by "to". If the literal is
  4929. unused and is being removed altogether, "to.abfd" will be NULL.
  4930. The removed_literal entries are kept on a per-section list, sorted
  4931. by the "from" offset field. */
  4932. typedef struct removed_literal_struct removed_literal;
  4933. typedef struct removed_literal_map_entry_struct removed_literal_map_entry;
  4934. typedef struct removed_literal_list_struct removed_literal_list;
  4935. struct removed_literal_struct
  4936. {
  4937. r_reloc from;
  4938. r_reloc to;
  4939. removed_literal *next;
  4940. };
  4941. struct removed_literal_map_entry_struct
  4942. {
  4943. bfd_vma addr;
  4944. removed_literal *literal;
  4945. };
  4946. struct removed_literal_list_struct
  4947. {
  4948. removed_literal *head;
  4949. removed_literal *tail;
  4950. unsigned n_map;
  4951. removed_literal_map_entry *map;
  4952. };
  4953. /* Record that the literal at "from" is being removed. If "to" is not
  4954. NULL, the "from" literal is being coalesced with the "to" literal. */
  4955. static void
  4956. add_removed_literal (removed_literal_list *removed_list,
  4957. const r_reloc *from,
  4958. const r_reloc *to)
  4959. {
  4960. removed_literal *r, *new_r, *next_r;
  4961. new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
  4962. new_r->from = *from;
  4963. if (to)
  4964. new_r->to = *to;
  4965. else
  4966. new_r->to.abfd = NULL;
  4967. new_r->next = NULL;
  4968. r = removed_list->head;
  4969. if (r == NULL)
  4970. {
  4971. removed_list->head = new_r;
  4972. removed_list->tail = new_r;
  4973. }
  4974. /* Special check for common case of append. */
  4975. else if (removed_list->tail->from.target_offset < from->target_offset)
  4976. {
  4977. removed_list->tail->next = new_r;
  4978. removed_list->tail = new_r;
  4979. }
  4980. else
  4981. {
  4982. while (r->from.target_offset < from->target_offset && r->next)
  4983. {
  4984. r = r->next;
  4985. }
  4986. next_r = r->next;
  4987. r->next = new_r;
  4988. new_r->next = next_r;
  4989. if (next_r == NULL)
  4990. removed_list->tail = new_r;
  4991. }
  4992. }
  4993. static void
  4994. map_removed_literal (removed_literal_list *removed_list)
  4995. {
  4996. unsigned n_map = 0;
  4997. unsigned i;
  4998. removed_literal_map_entry *map = NULL;
  4999. removed_literal *r = removed_list->head;
  5000. for (i = 0; r; ++i, r = r->next)
  5001. {
  5002. if (i == n_map)
  5003. {
  5004. n_map = (n_map * 2) + 2;
  5005. map = bfd_realloc (map, n_map * sizeof (*map));
  5006. }
  5007. map[i].addr = r->from.target_offset;
  5008. map[i].literal = r;
  5009. }
  5010. removed_list->map = map;
  5011. removed_list->n_map = i;
  5012. }
  5013. static int
  5014. removed_literal_compare (const void *a, const void *b)
  5015. {
  5016. const bfd_vma *key = a;
  5017. const removed_literal_map_entry *memb = b;
  5018. if (*key == memb->addr)
  5019. return 0;
  5020. else
  5021. return *key < memb->addr ? -1 : 1;
  5022. }
  5023. /* Check if the list of removed literals contains an entry for the
  5024. given address. Return the entry if found. */
  5025. static removed_literal *
  5026. find_removed_literal (removed_literal_list *removed_list, bfd_vma addr)
  5027. {
  5028. removed_literal_map_entry *p;
  5029. removed_literal *r = NULL;
  5030. if (removed_list->map == NULL)
  5031. map_removed_literal (removed_list);
  5032. if (removed_list->map != NULL)
  5033. {
  5034. p = bsearch (&addr, removed_list->map, removed_list->n_map,
  5035. sizeof (*removed_list->map), removed_literal_compare);
  5036. if (p)
  5037. {
  5038. while (p != removed_list->map && (p - 1)->addr == addr)
  5039. --p;
  5040. r = p->literal;
  5041. }
  5042. }
  5043. return r;
  5044. }
  5045. #if DEBUG
  5046. static void
  5047. print_removed_literals (FILE *fp, removed_literal_list *removed_list)
  5048. {
  5049. removed_literal *r;
  5050. r = removed_list->head;
  5051. if (r)
  5052. fprintf (fp, "Removed Literals\n");
  5053. for (; r != NULL; r = r->next)
  5054. {
  5055. print_r_reloc (fp, &r->from);
  5056. fprintf (fp, " => ");
  5057. if (r->to.abfd == NULL)
  5058. fprintf (fp, "REMOVED");
  5059. else
  5060. print_r_reloc (fp, &r->to);
  5061. fprintf (fp, "\n");
  5062. }
  5063. }
  5064. #endif /* DEBUG */
  5065. /* Per-section data for relaxation. */
  5066. typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
  5067. struct xtensa_relax_info_struct
  5068. {
  5069. bool is_relaxable_literal_section;
  5070. bool is_relaxable_asm_section;
  5071. int visited; /* Number of times visited. */
  5072. source_reloc *src_relocs; /* Array[src_count]. */
  5073. int src_count;
  5074. int src_next; /* Next src_relocs entry to assign. */
  5075. removed_literal_list removed_list;
  5076. text_action_list action_list;
  5077. reloc_bfd_fix *fix_list;
  5078. reloc_bfd_fix *fix_array;
  5079. unsigned fix_array_count;
  5080. /* Support for expanding the reloc array that is stored
  5081. in the section structure. If the relocations have been
  5082. reallocated, the newly allocated relocations will be referenced
  5083. here along with the actual size allocated. The relocation
  5084. count will always be found in the section structure. */
  5085. Elf_Internal_Rela *allocated_relocs;
  5086. unsigned relocs_count;
  5087. unsigned allocated_relocs_count;
  5088. };
  5089. struct elf_xtensa_section_data
  5090. {
  5091. struct bfd_elf_section_data elf;
  5092. xtensa_relax_info relax_info;
  5093. };
  5094. static bool
  5095. elf_xtensa_new_section_hook (bfd *abfd, asection *sec)
  5096. {
  5097. if (!sec->used_by_bfd)
  5098. {
  5099. struct elf_xtensa_section_data *sdata;
  5100. size_t amt = sizeof (*sdata);
  5101. sdata = bfd_zalloc (abfd, amt);
  5102. if (sdata == NULL)
  5103. return false;
  5104. sec->used_by_bfd = sdata;
  5105. }
  5106. return _bfd_elf_new_section_hook (abfd, sec);
  5107. }
  5108. static xtensa_relax_info *
  5109. get_xtensa_relax_info (asection *sec)
  5110. {
  5111. struct elf_xtensa_section_data *section_data;
  5112. /* No info available if no section or if it is an output section. */
  5113. if (!sec || sec == sec->output_section)
  5114. return NULL;
  5115. section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
  5116. return &section_data->relax_info;
  5117. }
  5118. static void
  5119. init_xtensa_relax_info (asection *sec)
  5120. {
  5121. xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
  5122. relax_info->is_relaxable_literal_section = false;
  5123. relax_info->is_relaxable_asm_section = false;
  5124. relax_info->visited = 0;
  5125. relax_info->src_relocs = NULL;
  5126. relax_info->src_count = 0;
  5127. relax_info->src_next = 0;
  5128. relax_info->removed_list.head = NULL;
  5129. relax_info->removed_list.tail = NULL;
  5130. relax_info->action_list.tree = splay_tree_new (text_action_compare,
  5131. NULL, NULL);
  5132. relax_info->action_list.map.n_entries = 0;
  5133. relax_info->action_list.map.entry = NULL;
  5134. relax_info->fix_list = NULL;
  5135. relax_info->fix_array = NULL;
  5136. relax_info->fix_array_count = 0;
  5137. relax_info->allocated_relocs = NULL;
  5138. relax_info->relocs_count = 0;
  5139. relax_info->allocated_relocs_count = 0;
  5140. }
  5141. /* Coalescing literals may require a relocation to refer to a section in
  5142. a different input file, but the standard relocation information
  5143. cannot express that. Instead, the reloc_bfd_fix structures are used
  5144. to "fix" the relocations that refer to sections in other input files.
  5145. These structures are kept on per-section lists. The "src_type" field
  5146. records the relocation type in case there are multiple relocations on
  5147. the same location. FIXME: This is ugly; an alternative might be to
  5148. add new symbols with the "owner" field to some other input file. */
  5149. struct reloc_bfd_fix_struct
  5150. {
  5151. asection *src_sec;
  5152. bfd_vma src_offset;
  5153. unsigned src_type; /* Relocation type. */
  5154. asection *target_sec;
  5155. bfd_vma target_offset;
  5156. bool translated;
  5157. reloc_bfd_fix *next;
  5158. };
  5159. static reloc_bfd_fix *
  5160. reloc_bfd_fix_init (asection *src_sec,
  5161. bfd_vma src_offset,
  5162. unsigned src_type,
  5163. asection *target_sec,
  5164. bfd_vma target_offset,
  5165. bool translated)
  5166. {
  5167. reloc_bfd_fix *fix;
  5168. fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
  5169. fix->src_sec = src_sec;
  5170. fix->src_offset = src_offset;
  5171. fix->src_type = src_type;
  5172. fix->target_sec = target_sec;
  5173. fix->target_offset = target_offset;
  5174. fix->translated = translated;
  5175. return fix;
  5176. }
  5177. static void
  5178. add_fix (asection *src_sec, reloc_bfd_fix *fix)
  5179. {
  5180. xtensa_relax_info *relax_info;
  5181. relax_info = get_xtensa_relax_info (src_sec);
  5182. fix->next = relax_info->fix_list;
  5183. relax_info->fix_list = fix;
  5184. }
  5185. static int
  5186. fix_compare (const void *ap, const void *bp)
  5187. {
  5188. const reloc_bfd_fix *a = (const reloc_bfd_fix *) ap;
  5189. const reloc_bfd_fix *b = (const reloc_bfd_fix *) bp;
  5190. if (a->src_offset != b->src_offset)
  5191. return (a->src_offset - b->src_offset);
  5192. return (a->src_type - b->src_type);
  5193. }
  5194. static void
  5195. cache_fix_array (asection *sec)
  5196. {
  5197. unsigned i, count = 0;
  5198. reloc_bfd_fix *r;
  5199. xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
  5200. if (relax_info == NULL)
  5201. return;
  5202. if (relax_info->fix_list == NULL)
  5203. return;
  5204. for (r = relax_info->fix_list; r != NULL; r = r->next)
  5205. count++;
  5206. relax_info->fix_array =
  5207. (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix) * count);
  5208. relax_info->fix_array_count = count;
  5209. r = relax_info->fix_list;
  5210. for (i = 0; i < count; i++, r = r->next)
  5211. {
  5212. relax_info->fix_array[count - 1 - i] = *r;
  5213. relax_info->fix_array[count - 1 - i].next = NULL;
  5214. }
  5215. qsort (relax_info->fix_array, relax_info->fix_array_count,
  5216. sizeof (reloc_bfd_fix), fix_compare);
  5217. }
  5218. static reloc_bfd_fix *
  5219. get_bfd_fix (asection *sec, bfd_vma offset, unsigned type)
  5220. {
  5221. xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
  5222. reloc_bfd_fix *rv;
  5223. reloc_bfd_fix key;
  5224. if (relax_info == NULL)
  5225. return NULL;
  5226. if (relax_info->fix_list == NULL)
  5227. return NULL;
  5228. if (relax_info->fix_array == NULL)
  5229. cache_fix_array (sec);
  5230. key.src_offset = offset;
  5231. key.src_type = type;
  5232. rv = bsearch (&key, relax_info->fix_array, relax_info->fix_array_count,
  5233. sizeof (reloc_bfd_fix), fix_compare);
  5234. return rv;
  5235. }
  5236. /* Section caching. */
  5237. typedef struct section_cache_struct section_cache_t;
  5238. struct section_cache_struct
  5239. {
  5240. asection *sec;
  5241. bfd_byte *contents; /* Cache of the section contents. */
  5242. bfd_size_type content_length;
  5243. property_table_entry *ptbl; /* Cache of the section property table. */
  5244. unsigned pte_count;
  5245. Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
  5246. unsigned reloc_count;
  5247. };
  5248. static void
  5249. init_section_cache (section_cache_t *sec_cache)
  5250. {
  5251. memset (sec_cache, 0, sizeof (*sec_cache));
  5252. }
  5253. static void
  5254. free_section_cache (section_cache_t *sec_cache)
  5255. {
  5256. if (sec_cache->sec)
  5257. {
  5258. release_contents (sec_cache->sec, sec_cache->contents);
  5259. release_internal_relocs (sec_cache->sec, sec_cache->relocs);
  5260. free (sec_cache->ptbl);
  5261. }
  5262. }
  5263. static bool
  5264. section_cache_section (section_cache_t *sec_cache,
  5265. asection *sec,
  5266. struct bfd_link_info *link_info)
  5267. {
  5268. bfd *abfd;
  5269. property_table_entry *prop_table = NULL;
  5270. int ptblsize = 0;
  5271. bfd_byte *contents = NULL;
  5272. Elf_Internal_Rela *internal_relocs = NULL;
  5273. bfd_size_type sec_size;
  5274. if (sec == NULL)
  5275. return false;
  5276. if (sec == sec_cache->sec)
  5277. return true;
  5278. abfd = sec->owner;
  5279. sec_size = bfd_get_section_limit (abfd, sec);
  5280. /* Get the contents. */
  5281. contents = retrieve_contents (abfd, sec, link_info->keep_memory);
  5282. if (contents == NULL && sec_size != 0)
  5283. goto err;
  5284. /* Get the relocations. */
  5285. internal_relocs = retrieve_internal_relocs (abfd, sec,
  5286. link_info->keep_memory);
  5287. /* Get the entry table. */
  5288. ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
  5289. XTENSA_PROP_SEC_NAME, false);
  5290. if (ptblsize < 0)
  5291. goto err;
  5292. /* Fill in the new section cache. */
  5293. free_section_cache (sec_cache);
  5294. init_section_cache (sec_cache);
  5295. sec_cache->sec = sec;
  5296. sec_cache->contents = contents;
  5297. sec_cache->content_length = sec_size;
  5298. sec_cache->relocs = internal_relocs;
  5299. sec_cache->reloc_count = sec->reloc_count;
  5300. sec_cache->pte_count = ptblsize;
  5301. sec_cache->ptbl = prop_table;
  5302. return true;
  5303. err:
  5304. release_contents (sec, contents);
  5305. release_internal_relocs (sec, internal_relocs);
  5306. free (prop_table);
  5307. return false;
  5308. }
  5309. /* Extended basic blocks. */
  5310. /* An ebb_struct represents an Extended Basic Block. Within this
  5311. range, we guarantee that all instructions are decodable, the
  5312. property table entries are contiguous, and no property table
  5313. specifies a segment that cannot have instructions moved. This
  5314. structure contains caches of the contents, property table and
  5315. relocations for the specified section for easy use. The range is
  5316. specified by ranges of indices for the byte offset, property table
  5317. offsets and relocation offsets. These must be consistent. */
  5318. typedef struct ebb_struct ebb_t;
  5319. struct ebb_struct
  5320. {
  5321. asection *sec;
  5322. bfd_byte *contents; /* Cache of the section contents. */
  5323. bfd_size_type content_length;
  5324. property_table_entry *ptbl; /* Cache of the section property table. */
  5325. unsigned pte_count;
  5326. Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
  5327. unsigned reloc_count;
  5328. bfd_vma start_offset; /* Offset in section. */
  5329. unsigned start_ptbl_idx; /* Offset in the property table. */
  5330. unsigned start_reloc_idx; /* Offset in the relocations. */
  5331. bfd_vma end_offset;
  5332. unsigned end_ptbl_idx;
  5333. unsigned end_reloc_idx;
  5334. bool ends_section; /* Is this the last ebb in a section? */
  5335. /* The unreachable property table at the end of this set of blocks;
  5336. NULL if the end is not an unreachable block. */
  5337. property_table_entry *ends_unreachable;
  5338. };
  5339. enum ebb_target_enum
  5340. {
  5341. EBB_NO_ALIGN = 0,
  5342. EBB_DESIRE_TGT_ALIGN,
  5343. EBB_REQUIRE_TGT_ALIGN,
  5344. EBB_REQUIRE_LOOP_ALIGN,
  5345. EBB_REQUIRE_ALIGN
  5346. };
  5347. /* proposed_action_struct is similar to the text_action_struct except
  5348. that is represents a potential transformation, not one that will
  5349. occur. We build a list of these for an extended basic block
  5350. and use them to compute the actual actions desired. We must be
  5351. careful that the entire set of actual actions we perform do not
  5352. break any relocations that would fit if the actions were not
  5353. performed. */
  5354. typedef struct proposed_action_struct proposed_action;
  5355. struct proposed_action_struct
  5356. {
  5357. enum ebb_target_enum align_type; /* for the target alignment */
  5358. bfd_vma alignment_pow;
  5359. text_action_t action;
  5360. bfd_vma offset;
  5361. int removed_bytes;
  5362. bool do_action; /* If false, then we will not perform the action. */
  5363. };
  5364. /* The ebb_constraint_struct keeps a set of proposed actions for an
  5365. extended basic block. */
  5366. typedef struct ebb_constraint_struct ebb_constraint;
  5367. struct ebb_constraint_struct
  5368. {
  5369. ebb_t ebb;
  5370. bool start_movable;
  5371. /* Bytes of extra space at the beginning if movable. */
  5372. int start_extra_space;
  5373. enum ebb_target_enum start_align;
  5374. bool end_movable;
  5375. /* Bytes of extra space at the end if movable. */
  5376. int end_extra_space;
  5377. unsigned action_count;
  5378. unsigned action_allocated;
  5379. /* Array of proposed actions. */
  5380. proposed_action *actions;
  5381. /* Action alignments -- one for each proposed action. */
  5382. enum ebb_target_enum *action_aligns;
  5383. };
  5384. static void
  5385. init_ebb_constraint (ebb_constraint *c)
  5386. {
  5387. memset (c, 0, sizeof (ebb_constraint));
  5388. }
  5389. static void
  5390. free_ebb_constraint (ebb_constraint *c)
  5391. {
  5392. free (c->actions);
  5393. }
  5394. static void
  5395. init_ebb (ebb_t *ebb,
  5396. asection *sec,
  5397. bfd_byte *contents,
  5398. bfd_size_type content_length,
  5399. property_table_entry *prop_table,
  5400. unsigned ptblsize,
  5401. Elf_Internal_Rela *internal_relocs,
  5402. unsigned reloc_count)
  5403. {
  5404. memset (ebb, 0, sizeof (ebb_t));
  5405. ebb->sec = sec;
  5406. ebb->contents = contents;
  5407. ebb->content_length = content_length;
  5408. ebb->ptbl = prop_table;
  5409. ebb->pte_count = ptblsize;
  5410. ebb->relocs = internal_relocs;
  5411. ebb->reloc_count = reloc_count;
  5412. ebb->start_offset = 0;
  5413. ebb->end_offset = ebb->content_length - 1;
  5414. ebb->start_ptbl_idx = 0;
  5415. ebb->end_ptbl_idx = ptblsize;
  5416. ebb->start_reloc_idx = 0;
  5417. ebb->end_reloc_idx = reloc_count;
  5418. }
  5419. /* Extend the ebb to all decodable contiguous sections. The algorithm
  5420. for building a basic block around an instruction is to push it
  5421. forward until we hit the end of a section, an unreachable block or
  5422. a block that cannot be transformed. Then we push it backwards
  5423. searching for similar conditions. */
  5424. static bool extend_ebb_bounds_forward (ebb_t *);
  5425. static bool extend_ebb_bounds_backward (ebb_t *);
  5426. static bfd_size_type insn_block_decodable_len
  5427. (bfd_byte *, bfd_size_type, bfd_vma, bfd_size_type);
  5428. static bool
  5429. extend_ebb_bounds (ebb_t *ebb)
  5430. {
  5431. if (!extend_ebb_bounds_forward (ebb))
  5432. return false;
  5433. if (!extend_ebb_bounds_backward (ebb))
  5434. return false;
  5435. return true;
  5436. }
  5437. static bool
  5438. extend_ebb_bounds_forward (ebb_t *ebb)
  5439. {
  5440. property_table_entry *the_entry, *new_entry;
  5441. the_entry = &ebb->ptbl[ebb->end_ptbl_idx];
  5442. /* Stop when (1) we cannot decode an instruction, (2) we are at
  5443. the end of the property tables, (3) we hit a non-contiguous property
  5444. table entry, (4) we hit a NO_TRANSFORM region. */
  5445. while (1)
  5446. {
  5447. bfd_vma entry_end;
  5448. bfd_size_type insn_block_len;
  5449. entry_end = the_entry->address - ebb->sec->vma + the_entry->size;
  5450. insn_block_len =
  5451. insn_block_decodable_len (ebb->contents, ebb->content_length,
  5452. ebb->end_offset,
  5453. entry_end - ebb->end_offset);
  5454. if (insn_block_len != (entry_end - ebb->end_offset))
  5455. {
  5456. _bfd_error_handler
  5457. /* xgettext:c-format */
  5458. (_("%pB(%pA+%#" PRIx64 "): could not decode instruction; "
  5459. "possible configuration mismatch"),
  5460. ebb->sec->owner, ebb->sec,
  5461. (uint64_t) (ebb->end_offset + insn_block_len));
  5462. return false;
  5463. }
  5464. ebb->end_offset += insn_block_len;
  5465. if (ebb->end_offset == ebb->sec->size)
  5466. ebb->ends_section = true;
  5467. /* Update the reloc counter. */
  5468. while (ebb->end_reloc_idx + 1 < ebb->reloc_count
  5469. && (ebb->relocs[ebb->end_reloc_idx + 1].r_offset
  5470. < ebb->end_offset))
  5471. {
  5472. ebb->end_reloc_idx++;
  5473. }
  5474. if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
  5475. return true;
  5476. new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
  5477. if (((new_entry->flags & XTENSA_PROP_INSN) == 0)
  5478. || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
  5479. || ((the_entry->flags & XTENSA_PROP_ALIGN) != 0))
  5480. break;
  5481. if (the_entry->address + the_entry->size != new_entry->address)
  5482. break;
  5483. the_entry = new_entry;
  5484. ebb->end_ptbl_idx++;
  5485. }
  5486. /* Quick check for an unreachable or end of file just at the end. */
  5487. if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
  5488. {
  5489. if (ebb->end_offset == ebb->content_length)
  5490. ebb->ends_section = true;
  5491. }
  5492. else
  5493. {
  5494. new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
  5495. if ((new_entry->flags & XTENSA_PROP_UNREACHABLE) != 0
  5496. && the_entry->address + the_entry->size == new_entry->address)
  5497. ebb->ends_unreachable = new_entry;
  5498. }
  5499. /* Any other ending requires exact alignment. */
  5500. return true;
  5501. }
  5502. static bool
  5503. extend_ebb_bounds_backward (ebb_t *ebb)
  5504. {
  5505. property_table_entry *the_entry, *new_entry;
  5506. the_entry = &ebb->ptbl[ebb->start_ptbl_idx];
  5507. /* Stop when (1) we cannot decode the instructions in the current entry.
  5508. (2) we are at the beginning of the property tables, (3) we hit a
  5509. non-contiguous property table entry, (4) we hit a NO_TRANSFORM region. */
  5510. while (1)
  5511. {
  5512. bfd_vma block_begin;
  5513. bfd_size_type insn_block_len;
  5514. block_begin = the_entry->address - ebb->sec->vma;
  5515. insn_block_len =
  5516. insn_block_decodable_len (ebb->contents, ebb->content_length,
  5517. block_begin,
  5518. ebb->start_offset - block_begin);
  5519. if (insn_block_len != ebb->start_offset - block_begin)
  5520. {
  5521. _bfd_error_handler
  5522. /* xgettext:c-format */
  5523. (_("%pB(%pA+%#" PRIx64 "): could not decode instruction; "
  5524. "possible configuration mismatch"),
  5525. ebb->sec->owner, ebb->sec,
  5526. (uint64_t) (ebb->end_offset + insn_block_len));
  5527. return false;
  5528. }
  5529. ebb->start_offset -= insn_block_len;
  5530. /* Update the reloc counter. */
  5531. while (ebb->start_reloc_idx > 0
  5532. && (ebb->relocs[ebb->start_reloc_idx - 1].r_offset
  5533. >= ebb->start_offset))
  5534. {
  5535. ebb->start_reloc_idx--;
  5536. }
  5537. if (ebb->start_ptbl_idx == 0)
  5538. return true;
  5539. new_entry = &ebb->ptbl[ebb->start_ptbl_idx - 1];
  5540. if ((new_entry->flags & XTENSA_PROP_INSN) == 0
  5541. || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
  5542. || ((new_entry->flags & XTENSA_PROP_ALIGN) != 0))
  5543. return true;
  5544. if (new_entry->address + new_entry->size != the_entry->address)
  5545. return true;
  5546. the_entry = new_entry;
  5547. ebb->start_ptbl_idx--;
  5548. }
  5549. return true;
  5550. }
  5551. static bfd_size_type
  5552. insn_block_decodable_len (bfd_byte *contents,
  5553. bfd_size_type content_len,
  5554. bfd_vma block_offset,
  5555. bfd_size_type block_len)
  5556. {
  5557. bfd_vma offset = block_offset;
  5558. while (offset < block_offset + block_len)
  5559. {
  5560. bfd_size_type insn_len = 0;
  5561. insn_len = insn_decode_len (contents, content_len, offset);
  5562. if (insn_len == 0)
  5563. return (offset - block_offset);
  5564. offset += insn_len;
  5565. }
  5566. return (offset - block_offset);
  5567. }
  5568. static void
  5569. ebb_propose_action (ebb_constraint *c,
  5570. enum ebb_target_enum align_type,
  5571. bfd_vma alignment_pow,
  5572. text_action_t action,
  5573. bfd_vma offset,
  5574. int removed_bytes,
  5575. bool do_action)
  5576. {
  5577. proposed_action *act;
  5578. if (c->action_allocated <= c->action_count)
  5579. {
  5580. unsigned new_allocated, i;
  5581. proposed_action *new_actions;
  5582. new_allocated = (c->action_count + 2) * 2;
  5583. new_actions = (proposed_action *)
  5584. bfd_zmalloc (sizeof (proposed_action) * new_allocated);
  5585. for (i = 0; i < c->action_count; i++)
  5586. new_actions[i] = c->actions[i];
  5587. free (c->actions);
  5588. c->actions = new_actions;
  5589. c->action_allocated = new_allocated;
  5590. }
  5591. act = &c->actions[c->action_count];
  5592. act->align_type = align_type;
  5593. act->alignment_pow = alignment_pow;
  5594. act->action = action;
  5595. act->offset = offset;
  5596. act->removed_bytes = removed_bytes;
  5597. act->do_action = do_action;
  5598. c->action_count++;
  5599. }
  5600. /* Access to internal relocations, section contents and symbols. */
  5601. /* During relaxation, we need to modify relocations, section contents,
  5602. and symbol definitions, and we need to keep the original values from
  5603. being reloaded from the input files, i.e., we need to "pin" the
  5604. modified values in memory. We also want to continue to observe the
  5605. setting of the "keep-memory" flag. The following functions wrap the
  5606. standard BFD functions to take care of this for us. */
  5607. static Elf_Internal_Rela *
  5608. retrieve_internal_relocs (bfd *abfd, asection *sec, bool keep_memory)
  5609. {
  5610. Elf_Internal_Rela *internal_relocs;
  5611. if ((sec->flags & SEC_LINKER_CREATED) != 0)
  5612. return NULL;
  5613. internal_relocs = elf_section_data (sec)->relocs;
  5614. if (internal_relocs == NULL)
  5615. internal_relocs = (_bfd_elf_link_read_relocs
  5616. (abfd, sec, NULL, NULL, keep_memory));
  5617. return internal_relocs;
  5618. }
  5619. static void
  5620. pin_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
  5621. {
  5622. elf_section_data (sec)->relocs = internal_relocs;
  5623. }
  5624. static void
  5625. release_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
  5626. {
  5627. if (elf_section_data (sec)->relocs != internal_relocs)
  5628. free (internal_relocs);
  5629. }
  5630. static bfd_byte *
  5631. retrieve_contents (bfd *abfd, asection *sec, bool keep_memory)
  5632. {
  5633. bfd_byte *contents;
  5634. bfd_size_type sec_size;
  5635. sec_size = bfd_get_section_limit (abfd, sec);
  5636. contents = elf_section_data (sec)->this_hdr.contents;
  5637. if (contents == NULL && sec_size != 0)
  5638. {
  5639. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  5640. {
  5641. free (contents);
  5642. return NULL;
  5643. }
  5644. if (keep_memory)
  5645. elf_section_data (sec)->this_hdr.contents = contents;
  5646. }
  5647. return contents;
  5648. }
  5649. static void
  5650. pin_contents (asection *sec, bfd_byte *contents)
  5651. {
  5652. elf_section_data (sec)->this_hdr.contents = contents;
  5653. }
  5654. static void
  5655. release_contents (asection *sec, bfd_byte *contents)
  5656. {
  5657. if (elf_section_data (sec)->this_hdr.contents != contents)
  5658. free (contents);
  5659. }
  5660. static Elf_Internal_Sym *
  5661. retrieve_local_syms (bfd *input_bfd)
  5662. {
  5663. Elf_Internal_Shdr *symtab_hdr;
  5664. Elf_Internal_Sym *isymbuf;
  5665. size_t locsymcount;
  5666. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  5667. locsymcount = symtab_hdr->sh_info;
  5668. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  5669. if (isymbuf == NULL && locsymcount != 0)
  5670. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
  5671. NULL, NULL, NULL);
  5672. /* Save the symbols for this input file so they won't be read again. */
  5673. if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
  5674. symtab_hdr->contents = (unsigned char *) isymbuf;
  5675. return isymbuf;
  5676. }
  5677. /* Code for link-time relaxation. */
  5678. /* Initialization for relaxation: */
  5679. static bool analyze_relocations (struct bfd_link_info *);
  5680. static bool find_relaxable_sections
  5681. (bfd *, asection *, struct bfd_link_info *, bool *);
  5682. static bool collect_source_relocs
  5683. (bfd *, asection *, struct bfd_link_info *);
  5684. static bool is_resolvable_asm_expansion
  5685. (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, struct bfd_link_info *,
  5686. bool *);
  5687. static Elf_Internal_Rela *find_associated_l32r_irel
  5688. (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Rela *);
  5689. static bool compute_text_actions
  5690. (bfd *, asection *, struct bfd_link_info *);
  5691. static bool compute_ebb_proposed_actions (ebb_constraint *);
  5692. static bool compute_ebb_actions (ebb_constraint *);
  5693. typedef struct reloc_range_list_struct reloc_range_list;
  5694. static bool check_section_ebb_pcrels_fit
  5695. (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
  5696. reloc_range_list *, const ebb_constraint *,
  5697. const xtensa_opcode *);
  5698. static bool check_section_ebb_reduces (const ebb_constraint *);
  5699. static void text_action_add_proposed
  5700. (text_action_list *, const ebb_constraint *, asection *);
  5701. /* First pass: */
  5702. static bool compute_removed_literals
  5703. (bfd *, asection *, struct bfd_link_info *, value_map_hash_table *);
  5704. static Elf_Internal_Rela *get_irel_at_offset
  5705. (asection *, Elf_Internal_Rela *, bfd_vma);
  5706. static bool is_removable_literal
  5707. (const source_reloc *, int, const source_reloc *, int, asection *,
  5708. property_table_entry *, int);
  5709. static bool remove_dead_literal
  5710. (bfd *, asection *, struct bfd_link_info *, Elf_Internal_Rela *,
  5711. Elf_Internal_Rela *, source_reloc *, property_table_entry *, int);
  5712. static bool identify_literal_placement
  5713. (bfd *, asection *, bfd_byte *, struct bfd_link_info *,
  5714. value_map_hash_table *, bool *, Elf_Internal_Rela *, int,
  5715. source_reloc *, property_table_entry *, int, section_cache_t *,
  5716. bool);
  5717. static bool relocations_reach (source_reloc *, int, const r_reloc *);
  5718. static bool coalesce_shared_literal
  5719. (asection *, source_reloc *, property_table_entry *, int, value_map *);
  5720. static bool move_shared_literal
  5721. (asection *, struct bfd_link_info *, source_reloc *, property_table_entry *,
  5722. int, const r_reloc *, const literal_value *, section_cache_t *);
  5723. /* Second pass: */
  5724. static bool relax_section (bfd *, asection *, struct bfd_link_info *);
  5725. static bool translate_section_fixes (asection *);
  5726. static bool translate_reloc_bfd_fix (reloc_bfd_fix *);
  5727. static asection *translate_reloc (const r_reloc *, r_reloc *, asection *);
  5728. static void shrink_dynamic_reloc_sections
  5729. (struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *);
  5730. static bool move_literal
  5731. (bfd *, struct bfd_link_info *, asection *, bfd_vma, bfd_byte *,
  5732. xtensa_relax_info *, Elf_Internal_Rela **, const literal_value *);
  5733. static bool relax_property_section
  5734. (bfd *, asection *, struct bfd_link_info *);
  5735. /* Third pass: */
  5736. static bool relax_section_symbols (bfd *, asection *);
  5737. static bool
  5738. elf_xtensa_relax_section (bfd *abfd,
  5739. asection *sec,
  5740. struct bfd_link_info *link_info,
  5741. bool *again)
  5742. {
  5743. static value_map_hash_table *values = NULL;
  5744. static bool relocations_analyzed = false;
  5745. xtensa_relax_info *relax_info;
  5746. if (!relocations_analyzed)
  5747. {
  5748. /* Do some overall initialization for relaxation. */
  5749. values = value_map_hash_table_init ();
  5750. if (values == NULL)
  5751. return false;
  5752. relaxing_section = true;
  5753. if (!analyze_relocations (link_info))
  5754. return false;
  5755. relocations_analyzed = true;
  5756. }
  5757. *again = false;
  5758. /* Don't mess with linker-created sections. */
  5759. if ((sec->flags & SEC_LINKER_CREATED) != 0)
  5760. return true;
  5761. relax_info = get_xtensa_relax_info (sec);
  5762. BFD_ASSERT (relax_info != NULL);
  5763. switch (relax_info->visited)
  5764. {
  5765. case 0:
  5766. /* Note: It would be nice to fold this pass into
  5767. analyze_relocations, but it is important for this step that the
  5768. sections be examined in link order. */
  5769. if (!compute_removed_literals (abfd, sec, link_info, values))
  5770. return false;
  5771. *again = true;
  5772. break;
  5773. case 1:
  5774. if (values)
  5775. value_map_hash_table_delete (values);
  5776. values = NULL;
  5777. if (!relax_section (abfd, sec, link_info))
  5778. return false;
  5779. *again = true;
  5780. break;
  5781. case 2:
  5782. if (!relax_section_symbols (abfd, sec))
  5783. return false;
  5784. break;
  5785. }
  5786. relax_info->visited++;
  5787. return true;
  5788. }
  5789. /* Initialization for relaxation. */
  5790. /* This function is called once at the start of relaxation. It scans
  5791. all the input sections and marks the ones that are relaxable (i.e.,
  5792. literal sections with L32R relocations against them), and then
  5793. collects source_reloc information for all the relocations against
  5794. those relaxable sections. During this process, it also detects
  5795. longcalls, i.e., calls relaxed by the assembler into indirect
  5796. calls, that can be optimized back into direct calls. Within each
  5797. extended basic block (ebb) containing an optimized longcall, it
  5798. computes a set of "text actions" that can be performed to remove
  5799. the L32R associated with the longcall while optionally preserving
  5800. branch target alignments. */
  5801. static bool
  5802. analyze_relocations (struct bfd_link_info *link_info)
  5803. {
  5804. bfd *abfd;
  5805. asection *sec;
  5806. bool is_relaxable = false;
  5807. /* Initialize the per-section relaxation info. */
  5808. for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  5809. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  5810. {
  5811. init_xtensa_relax_info (sec);
  5812. }
  5813. /* Mark relaxable sections (and count relocations against each one). */
  5814. for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  5815. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  5816. {
  5817. if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
  5818. return false;
  5819. }
  5820. /* Bail out if there are no relaxable sections. */
  5821. if (!is_relaxable)
  5822. return true;
  5823. /* Allocate space for source_relocs. */
  5824. for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  5825. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  5826. {
  5827. xtensa_relax_info *relax_info;
  5828. relax_info = get_xtensa_relax_info (sec);
  5829. if (relax_info->is_relaxable_literal_section
  5830. || relax_info->is_relaxable_asm_section)
  5831. {
  5832. relax_info->src_relocs = (source_reloc *)
  5833. bfd_malloc (relax_info->src_count * sizeof (source_reloc));
  5834. }
  5835. else
  5836. relax_info->src_count = 0;
  5837. }
  5838. /* Collect info on relocations against each relaxable section. */
  5839. for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  5840. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  5841. {
  5842. if (!collect_source_relocs (abfd, sec, link_info))
  5843. return false;
  5844. }
  5845. /* Compute the text actions. */
  5846. for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  5847. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  5848. {
  5849. if (!compute_text_actions (abfd, sec, link_info))
  5850. return false;
  5851. }
  5852. return true;
  5853. }
  5854. /* Find all the sections that might be relaxed. The motivation for
  5855. this pass is that collect_source_relocs() needs to record _all_ the
  5856. relocations that target each relaxable section. That is expensive
  5857. and unnecessary unless the target section is actually going to be
  5858. relaxed. This pass identifies all such sections by checking if
  5859. they have L32Rs pointing to them. In the process, the total number
  5860. of relocations targeting each section is also counted so that we
  5861. know how much space to allocate for source_relocs against each
  5862. relaxable literal section. */
  5863. static bool
  5864. find_relaxable_sections (bfd *abfd,
  5865. asection *sec,
  5866. struct bfd_link_info *link_info,
  5867. bool *is_relaxable_p)
  5868. {
  5869. Elf_Internal_Rela *internal_relocs;
  5870. bfd_byte *contents;
  5871. bool ok = true;
  5872. unsigned i;
  5873. xtensa_relax_info *source_relax_info;
  5874. bool is_l32r_reloc;
  5875. internal_relocs = retrieve_internal_relocs (abfd, sec,
  5876. link_info->keep_memory);
  5877. if (internal_relocs == NULL)
  5878. return ok;
  5879. contents = retrieve_contents (abfd, sec, link_info->keep_memory);
  5880. if (contents == NULL && sec->size != 0)
  5881. {
  5882. ok = false;
  5883. goto error_return;
  5884. }
  5885. source_relax_info = get_xtensa_relax_info (sec);
  5886. for (i = 0; i < sec->reloc_count; i++)
  5887. {
  5888. Elf_Internal_Rela *irel = &internal_relocs[i];
  5889. r_reloc r_rel;
  5890. asection *target_sec;
  5891. xtensa_relax_info *target_relax_info;
  5892. /* If this section has not already been marked as "relaxable", and
  5893. if it contains any ASM_EXPAND relocations (marking expanded
  5894. longcalls) that can be optimized into direct calls, then mark
  5895. the section as "relaxable". */
  5896. if (source_relax_info
  5897. && !source_relax_info->is_relaxable_asm_section
  5898. && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_EXPAND)
  5899. {
  5900. bool is_reachable = false;
  5901. if (is_resolvable_asm_expansion (abfd, sec, contents, irel,
  5902. link_info, &is_reachable)
  5903. && is_reachable)
  5904. {
  5905. source_relax_info->is_relaxable_asm_section = true;
  5906. *is_relaxable_p = true;
  5907. }
  5908. }
  5909. r_reloc_init (&r_rel, abfd, irel, contents,
  5910. bfd_get_section_limit (abfd, sec));
  5911. target_sec = r_reloc_get_section (&r_rel);
  5912. target_relax_info = get_xtensa_relax_info (target_sec);
  5913. if (!target_relax_info)
  5914. continue;
  5915. /* Count PC-relative operand relocations against the target section.
  5916. Note: The conditions tested here must match the conditions under
  5917. which init_source_reloc is called in collect_source_relocs(). */
  5918. is_l32r_reloc = false;
  5919. if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
  5920. {
  5921. xtensa_opcode opcode =
  5922. get_relocation_opcode (abfd, sec, contents, irel);
  5923. if (opcode != XTENSA_UNDEFINED)
  5924. {
  5925. is_l32r_reloc = (opcode == get_l32r_opcode ());
  5926. if (!is_alt_relocation (ELF32_R_TYPE (irel->r_info))
  5927. || is_l32r_reloc)
  5928. target_relax_info->src_count++;
  5929. }
  5930. }
  5931. if (is_l32r_reloc && r_reloc_is_defined (&r_rel))
  5932. {
  5933. /* Mark the target section as relaxable. */
  5934. target_relax_info->is_relaxable_literal_section = true;
  5935. *is_relaxable_p = true;
  5936. }
  5937. }
  5938. error_return:
  5939. release_contents (sec, contents);
  5940. release_internal_relocs (sec, internal_relocs);
  5941. return ok;
  5942. }
  5943. /* Record _all_ the relocations that point to relaxable sections, and
  5944. get rid of ASM_EXPAND relocs by either converting them to
  5945. ASM_SIMPLIFY or by removing them. */
  5946. static bool
  5947. collect_source_relocs (bfd *abfd,
  5948. asection *sec,
  5949. struct bfd_link_info *link_info)
  5950. {
  5951. Elf_Internal_Rela *internal_relocs;
  5952. bfd_byte *contents;
  5953. bool ok = true;
  5954. unsigned i;
  5955. bfd_size_type sec_size;
  5956. internal_relocs = retrieve_internal_relocs (abfd, sec,
  5957. link_info->keep_memory);
  5958. if (internal_relocs == NULL)
  5959. return ok;
  5960. sec_size = bfd_get_section_limit (abfd, sec);
  5961. contents = retrieve_contents (abfd, sec, link_info->keep_memory);
  5962. if (contents == NULL && sec_size != 0)
  5963. {
  5964. ok = false;
  5965. goto error_return;
  5966. }
  5967. /* Record relocations against relaxable literal sections. */
  5968. for (i = 0; i < sec->reloc_count; i++)
  5969. {
  5970. Elf_Internal_Rela *irel = &internal_relocs[i];
  5971. r_reloc r_rel;
  5972. asection *target_sec;
  5973. xtensa_relax_info *target_relax_info;
  5974. r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
  5975. target_sec = r_reloc_get_section (&r_rel);
  5976. target_relax_info = get_xtensa_relax_info (target_sec);
  5977. if (target_relax_info
  5978. && (target_relax_info->is_relaxable_literal_section
  5979. || target_relax_info->is_relaxable_asm_section))
  5980. {
  5981. xtensa_opcode opcode = XTENSA_UNDEFINED;
  5982. int opnd = -1;
  5983. bool is_abs_literal = false;
  5984. if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
  5985. {
  5986. /* None of the current alternate relocs are PC-relative,
  5987. and only PC-relative relocs matter here. However, we
  5988. still need to record the opcode for literal
  5989. coalescing. */
  5990. opcode = get_relocation_opcode (abfd, sec, contents, irel);
  5991. if (opcode == get_l32r_opcode ())
  5992. {
  5993. is_abs_literal = true;
  5994. opnd = 1;
  5995. }
  5996. else
  5997. opcode = XTENSA_UNDEFINED;
  5998. }
  5999. else if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
  6000. {
  6001. opcode = get_relocation_opcode (abfd, sec, contents, irel);
  6002. opnd = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
  6003. }
  6004. if (opcode != XTENSA_UNDEFINED)
  6005. {
  6006. int src_next = target_relax_info->src_next++;
  6007. source_reloc *s_reloc = &target_relax_info->src_relocs[src_next];
  6008. init_source_reloc (s_reloc, sec, &r_rel, opcode, opnd,
  6009. is_abs_literal);
  6010. }
  6011. }
  6012. }
  6013. /* Now get rid of ASM_EXPAND relocations. At this point, the
  6014. src_relocs array for the target literal section may still be
  6015. incomplete, but it must at least contain the entries for the L32R
  6016. relocations associated with ASM_EXPANDs because they were just
  6017. added in the preceding loop over the relocations. */
  6018. for (i = 0; i < sec->reloc_count; i++)
  6019. {
  6020. Elf_Internal_Rela *irel = &internal_relocs[i];
  6021. bool is_reachable;
  6022. if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
  6023. &is_reachable))
  6024. continue;
  6025. if (is_reachable)
  6026. {
  6027. Elf_Internal_Rela *l32r_irel;
  6028. r_reloc r_rel;
  6029. asection *target_sec;
  6030. xtensa_relax_info *target_relax_info;
  6031. /* Mark the source_reloc for the L32R so that it will be
  6032. removed in compute_removed_literals(), along with the
  6033. associated literal. */
  6034. l32r_irel = find_associated_l32r_irel (abfd, sec, contents,
  6035. irel, internal_relocs);
  6036. if (l32r_irel == NULL)
  6037. continue;
  6038. r_reloc_init (&r_rel, abfd, l32r_irel, contents, sec_size);
  6039. target_sec = r_reloc_get_section (&r_rel);
  6040. target_relax_info = get_xtensa_relax_info (target_sec);
  6041. if (target_relax_info
  6042. && (target_relax_info->is_relaxable_literal_section
  6043. || target_relax_info->is_relaxable_asm_section))
  6044. {
  6045. source_reloc *s_reloc;
  6046. /* Search the source_relocs for the entry corresponding to
  6047. the l32r_irel. Note: The src_relocs array is not yet
  6048. sorted, but it wouldn't matter anyway because we're
  6049. searching by source offset instead of target offset. */
  6050. s_reloc = find_source_reloc (target_relax_info->src_relocs,
  6051. target_relax_info->src_next,
  6052. sec, l32r_irel);
  6053. BFD_ASSERT (s_reloc);
  6054. s_reloc->is_null = true;
  6055. }
  6056. /* Convert this reloc to ASM_SIMPLIFY. */
  6057. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  6058. R_XTENSA_ASM_SIMPLIFY);
  6059. l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
  6060. pin_internal_relocs (sec, internal_relocs);
  6061. }
  6062. else
  6063. {
  6064. /* It is resolvable but doesn't reach. We resolve now
  6065. by eliminating the relocation -- the call will remain
  6066. expanded into L32R/CALLX. */
  6067. irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
  6068. pin_internal_relocs (sec, internal_relocs);
  6069. }
  6070. }
  6071. error_return:
  6072. release_contents (sec, contents);
  6073. release_internal_relocs (sec, internal_relocs);
  6074. return ok;
  6075. }
  6076. /* Return TRUE if the asm expansion can be resolved. Generally it can
  6077. be resolved on a final link or when a partial link locates it in the
  6078. same section as the target. Set "is_reachable" flag if the target of
  6079. the call is within the range of a direct call, given the current VMA
  6080. for this section and the target section. */
  6081. bool
  6082. is_resolvable_asm_expansion (bfd *abfd,
  6083. asection *sec,
  6084. bfd_byte *contents,
  6085. Elf_Internal_Rela *irel,
  6086. struct bfd_link_info *link_info,
  6087. bool *is_reachable_p)
  6088. {
  6089. asection *target_sec;
  6090. asection *s;
  6091. bfd_vma first_vma;
  6092. bfd_vma last_vma;
  6093. unsigned int first_align;
  6094. unsigned int adjust;
  6095. bfd_vma target_offset;
  6096. r_reloc r_rel;
  6097. xtensa_opcode opcode, direct_call_opcode;
  6098. bfd_vma self_address;
  6099. bfd_vma dest_address;
  6100. bool uses_l32r;
  6101. bfd_size_type sec_size;
  6102. *is_reachable_p = false;
  6103. if (contents == NULL)
  6104. return false;
  6105. if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
  6106. return false;
  6107. sec_size = bfd_get_section_limit (abfd, sec);
  6108. opcode = get_expanded_call_opcode (contents + irel->r_offset,
  6109. sec_size - irel->r_offset, &uses_l32r);
  6110. /* Optimization of longcalls that use CONST16 is not yet implemented. */
  6111. if (!uses_l32r)
  6112. return false;
  6113. direct_call_opcode = swap_callx_for_call_opcode (opcode);
  6114. if (direct_call_opcode == XTENSA_UNDEFINED)
  6115. return false;
  6116. /* Check and see that the target resolves. */
  6117. r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
  6118. if (!r_reloc_is_defined (&r_rel))
  6119. return false;
  6120. target_sec = r_reloc_get_section (&r_rel);
  6121. target_offset = r_rel.target_offset;
  6122. /* If the target is in a shared library, then it doesn't reach. This
  6123. isn't supposed to come up because the compiler should never generate
  6124. non-PIC calls on systems that use shared libraries, but the linker
  6125. shouldn't crash regardless. */
  6126. if (!target_sec->output_section)
  6127. return false;
  6128. /* For relocatable sections, we can only simplify when the output
  6129. section of the target is the same as the output section of the
  6130. source. */
  6131. if (bfd_link_relocatable (link_info)
  6132. && (target_sec->output_section != sec->output_section
  6133. || is_reloc_sym_weak (abfd, irel)))
  6134. return false;
  6135. if (target_sec->output_section != sec->output_section)
  6136. {
  6137. /* If the two sections are sufficiently far away that relaxation
  6138. might take the call out of range, we can't simplify. For
  6139. example, a positive displacement call into another memory
  6140. could get moved to a lower address due to literal removal,
  6141. but the destination won't move, and so the displacment might
  6142. get larger.
  6143. If the displacement is negative, assume the destination could
  6144. move as far back as the start of the output section. The
  6145. self_address will be at least as far into the output section
  6146. as it is prior to relaxation.
  6147. If the displacement is postive, assume the destination will be in
  6148. it's pre-relaxed location (because relaxation only makes sections
  6149. smaller). The self_address could go all the way to the beginning
  6150. of the output section. */
  6151. dest_address = target_sec->output_section->vma;
  6152. self_address = sec->output_section->vma;
  6153. if (sec->output_section->vma > target_sec->output_section->vma)
  6154. self_address += sec->output_offset + irel->r_offset + 3;
  6155. else
  6156. dest_address += bfd_get_section_limit (abfd, target_sec->output_section);
  6157. /* Call targets should be four-byte aligned. */
  6158. dest_address = (dest_address + 3) & ~3;
  6159. }
  6160. else
  6161. {
  6162. self_address = (sec->output_section->vma
  6163. + sec->output_offset + irel->r_offset + 3);
  6164. dest_address = (target_sec->output_section->vma
  6165. + target_sec->output_offset + target_offset);
  6166. }
  6167. /* Adjust addresses with alignments for the worst case to see if call insn
  6168. can fit. Don't relax l32r + callx to call if the target can be out of
  6169. range due to alignment.
  6170. Caller and target addresses are highest and lowest address.
  6171. Search all sections between caller and target, looking for max alignment.
  6172. The adjustment is max alignment bytes. If the alignment at the lowest
  6173. address is less than the adjustment, apply the adjustment to highest
  6174. address. */
  6175. /* Start from lowest address.
  6176. Lowest address aligmnet is from input section.
  6177. Initial alignment (adjust) is from input section. */
  6178. if (dest_address > self_address)
  6179. {
  6180. s = sec->output_section;
  6181. last_vma = dest_address;
  6182. first_align = sec->alignment_power;
  6183. adjust = target_sec->alignment_power;
  6184. }
  6185. else
  6186. {
  6187. s = target_sec->output_section;
  6188. last_vma = self_address;
  6189. first_align = target_sec->alignment_power;
  6190. adjust = sec->alignment_power;
  6191. }
  6192. first_vma = s->vma;
  6193. /* Find the largest alignment in output section list. */
  6194. for (; s && s->vma >= first_vma && s->vma <= last_vma ; s = s->next)
  6195. {
  6196. if (s->alignment_power > adjust)
  6197. adjust = s->alignment_power;
  6198. }
  6199. if (adjust > first_align)
  6200. {
  6201. /* Alignment may enlarge the range, adjust highest address. */
  6202. adjust = 1 << adjust;
  6203. if (dest_address > self_address)
  6204. {
  6205. dest_address += adjust;
  6206. }
  6207. else
  6208. {
  6209. self_address += adjust;
  6210. }
  6211. }
  6212. *is_reachable_p = pcrel_reloc_fits (direct_call_opcode, 0,
  6213. self_address, dest_address);
  6214. if ((self_address >> CALL_SEGMENT_BITS) !=
  6215. (dest_address >> CALL_SEGMENT_BITS))
  6216. return false;
  6217. return true;
  6218. }
  6219. static Elf_Internal_Rela *
  6220. find_associated_l32r_irel (bfd *abfd,
  6221. asection *sec,
  6222. bfd_byte *contents,
  6223. Elf_Internal_Rela *other_irel,
  6224. Elf_Internal_Rela *internal_relocs)
  6225. {
  6226. unsigned i;
  6227. for (i = 0; i < sec->reloc_count; i++)
  6228. {
  6229. Elf_Internal_Rela *irel = &internal_relocs[i];
  6230. if (irel == other_irel)
  6231. continue;
  6232. if (irel->r_offset != other_irel->r_offset)
  6233. continue;
  6234. if (is_l32r_relocation (abfd, sec, contents, irel))
  6235. return irel;
  6236. }
  6237. return NULL;
  6238. }
  6239. static xtensa_opcode *
  6240. build_reloc_opcodes (bfd *abfd,
  6241. asection *sec,
  6242. bfd_byte *contents,
  6243. Elf_Internal_Rela *internal_relocs)
  6244. {
  6245. unsigned i;
  6246. xtensa_opcode *reloc_opcodes =
  6247. (xtensa_opcode *) bfd_malloc (sizeof (xtensa_opcode) * sec->reloc_count);
  6248. for (i = 0; i < sec->reloc_count; i++)
  6249. {
  6250. Elf_Internal_Rela *irel = &internal_relocs[i];
  6251. reloc_opcodes[i] = get_relocation_opcode (abfd, sec, contents, irel);
  6252. }
  6253. return reloc_opcodes;
  6254. }
  6255. struct reloc_range_struct
  6256. {
  6257. bfd_vma addr;
  6258. bool add; /* TRUE if start of a range, FALSE otherwise. */
  6259. /* Original irel index in the array of relocations for a section. */
  6260. unsigned irel_index;
  6261. };
  6262. typedef struct reloc_range_struct reloc_range;
  6263. typedef struct reloc_range_list_entry_struct reloc_range_list_entry;
  6264. struct reloc_range_list_entry_struct
  6265. {
  6266. reloc_range_list_entry *next;
  6267. reloc_range_list_entry *prev;
  6268. Elf_Internal_Rela *irel;
  6269. xtensa_opcode opcode;
  6270. int opnum;
  6271. };
  6272. struct reloc_range_list_struct
  6273. {
  6274. /* The rest of the structure is only meaningful when ok is TRUE. */
  6275. bool ok;
  6276. unsigned n_range; /* Number of range markers. */
  6277. reloc_range *range; /* Sorted range markers. */
  6278. unsigned first; /* Index of a first range element in the list. */
  6279. unsigned last; /* One past index of a last range element in the list. */
  6280. unsigned n_list; /* Number of list elements. */
  6281. reloc_range_list_entry *reloc; /* */
  6282. reloc_range_list_entry list_root;
  6283. };
  6284. static int
  6285. reloc_range_compare (const void *a, const void *b)
  6286. {
  6287. const reloc_range *ra = a;
  6288. const reloc_range *rb = b;
  6289. if (ra->addr != rb->addr)
  6290. return ra->addr < rb->addr ? -1 : 1;
  6291. if (ra->add != rb->add)
  6292. return ra->add ? -1 : 1;
  6293. return 0;
  6294. }
  6295. static void
  6296. build_reloc_ranges (bfd *abfd, asection *sec,
  6297. bfd_byte *contents,
  6298. Elf_Internal_Rela *internal_relocs,
  6299. xtensa_opcode *reloc_opcodes,
  6300. reloc_range_list *list)
  6301. {
  6302. unsigned i;
  6303. size_t n = 0;
  6304. size_t max_n = 0;
  6305. reloc_range *ranges = NULL;
  6306. reloc_range_list_entry *reloc =
  6307. bfd_malloc (sec->reloc_count * sizeof (*reloc));
  6308. memset (list, 0, sizeof (*list));
  6309. list->ok = true;
  6310. for (i = 0; i < sec->reloc_count; i++)
  6311. {
  6312. Elf_Internal_Rela *irel = &internal_relocs[i];
  6313. int r_type = ELF32_R_TYPE (irel->r_info);
  6314. reloc_howto_type *howto = &elf_howto_table[r_type];
  6315. r_reloc r_rel;
  6316. if (r_type == R_XTENSA_ASM_SIMPLIFY
  6317. || r_type == R_XTENSA_32_PCREL
  6318. || !howto->pc_relative)
  6319. continue;
  6320. r_reloc_init (&r_rel, abfd, irel, contents,
  6321. bfd_get_section_limit (abfd, sec));
  6322. if (r_reloc_get_section (&r_rel) != sec)
  6323. continue;
  6324. if (n + 2 > max_n)
  6325. {
  6326. max_n = (max_n + 2) * 2;
  6327. ranges = bfd_realloc (ranges, max_n * sizeof (*ranges));
  6328. }
  6329. ranges[n].addr = irel->r_offset;
  6330. ranges[n + 1].addr = r_rel.target_offset;
  6331. ranges[n].add = ranges[n].addr < ranges[n + 1].addr;
  6332. ranges[n + 1].add = !ranges[n].add;
  6333. ranges[n].irel_index = i;
  6334. ranges[n + 1].irel_index = i;
  6335. n += 2;
  6336. reloc[i].irel = irel;
  6337. /* Every relocation won't possibly be checked in the optimized version of
  6338. check_section_ebb_pcrels_fit, so this needs to be done here. */
  6339. if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
  6340. {
  6341. /* None of the current alternate relocs are PC-relative,
  6342. and only PC-relative relocs matter here. */
  6343. }
  6344. else
  6345. {
  6346. xtensa_opcode opcode;
  6347. int opnum;
  6348. if (reloc_opcodes)
  6349. opcode = reloc_opcodes[i];
  6350. else
  6351. opcode = get_relocation_opcode (abfd, sec, contents, irel);
  6352. if (opcode == XTENSA_UNDEFINED)
  6353. {
  6354. list->ok = false;
  6355. break;
  6356. }
  6357. opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
  6358. if (opnum == XTENSA_UNDEFINED)
  6359. {
  6360. list->ok = false;
  6361. break;
  6362. }
  6363. /* Record relocation opcode and opnum as we've calculated them
  6364. anyway and they won't change. */
  6365. reloc[i].opcode = opcode;
  6366. reloc[i].opnum = opnum;
  6367. }
  6368. }
  6369. if (list->ok)
  6370. {
  6371. ranges = bfd_realloc (ranges, n * sizeof (*ranges));
  6372. qsort (ranges, n, sizeof (*ranges), reloc_range_compare);
  6373. list->n_range = n;
  6374. list->range = ranges;
  6375. list->reloc = reloc;
  6376. list->list_root.prev = &list->list_root;
  6377. list->list_root.next = &list->list_root;
  6378. }
  6379. else
  6380. {
  6381. free (ranges);
  6382. free (reloc);
  6383. }
  6384. }
  6385. static void reloc_range_list_append (reloc_range_list *list,
  6386. unsigned irel_index)
  6387. {
  6388. reloc_range_list_entry *entry = list->reloc + irel_index;
  6389. entry->prev = list->list_root.prev;
  6390. entry->next = &list->list_root;
  6391. entry->prev->next = entry;
  6392. entry->next->prev = entry;
  6393. ++list->n_list;
  6394. }
  6395. static void reloc_range_list_remove (reloc_range_list *list,
  6396. unsigned irel_index)
  6397. {
  6398. reloc_range_list_entry *entry = list->reloc + irel_index;
  6399. entry->next->prev = entry->prev;
  6400. entry->prev->next = entry->next;
  6401. --list->n_list;
  6402. }
  6403. /* Update relocation list object so that it lists all relocations that cross
  6404. [first; last] range. Range bounds should not decrease with successive
  6405. invocations. */
  6406. static void reloc_range_list_update_range (reloc_range_list *list,
  6407. bfd_vma first, bfd_vma last)
  6408. {
  6409. /* This should not happen: EBBs are iterated from lower addresses to higher.
  6410. But even if that happens there's no need to break: just flush current list
  6411. and start from scratch. */
  6412. if ((list->last > 0 && list->range[list->last - 1].addr > last) ||
  6413. (list->first > 0 && list->range[list->first - 1].addr >= first))
  6414. {
  6415. list->first = 0;
  6416. list->last = 0;
  6417. list->n_list = 0;
  6418. list->list_root.next = &list->list_root;
  6419. list->list_root.prev = &list->list_root;
  6420. fprintf (stderr, "%s: move backwards requested\n", __func__);
  6421. }
  6422. for (; list->last < list->n_range &&
  6423. list->range[list->last].addr <= last; ++list->last)
  6424. if (list->range[list->last].add)
  6425. reloc_range_list_append (list, list->range[list->last].irel_index);
  6426. for (; list->first < list->n_range &&
  6427. list->range[list->first].addr < first; ++list->first)
  6428. if (!list->range[list->first].add)
  6429. reloc_range_list_remove (list, list->range[list->first].irel_index);
  6430. }
  6431. static void free_reloc_range_list (reloc_range_list *list)
  6432. {
  6433. free (list->range);
  6434. free (list->reloc);
  6435. }
  6436. /* The compute_text_actions function will build a list of potential
  6437. transformation actions for code in the extended basic block of each
  6438. longcall that is optimized to a direct call. From this list we
  6439. generate a set of actions to actually perform that optimizes for
  6440. space and, if not using size_opt, maintains branch target
  6441. alignments.
  6442. These actions to be performed are placed on a per-section list.
  6443. The actual changes are performed by relax_section() in the second
  6444. pass. */
  6445. bool
  6446. compute_text_actions (bfd *abfd,
  6447. asection *sec,
  6448. struct bfd_link_info *link_info)
  6449. {
  6450. xtensa_opcode *reloc_opcodes = NULL;
  6451. xtensa_relax_info *relax_info;
  6452. bfd_byte *contents;
  6453. Elf_Internal_Rela *internal_relocs;
  6454. bool ok = true;
  6455. unsigned i;
  6456. property_table_entry *prop_table = 0;
  6457. int ptblsize = 0;
  6458. bfd_size_type sec_size;
  6459. reloc_range_list relevant_relocs;
  6460. relax_info = get_xtensa_relax_info (sec);
  6461. BFD_ASSERT (relax_info);
  6462. BFD_ASSERT (relax_info->src_next == relax_info->src_count);
  6463. /* Do nothing if the section contains no optimized longcalls. */
  6464. if (!relax_info->is_relaxable_asm_section)
  6465. return ok;
  6466. internal_relocs = retrieve_internal_relocs (abfd, sec,
  6467. link_info->keep_memory);
  6468. if (internal_relocs)
  6469. qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
  6470. internal_reloc_compare);
  6471. sec_size = bfd_get_section_limit (abfd, sec);
  6472. contents = retrieve_contents (abfd, sec, link_info->keep_memory);
  6473. if (contents == NULL && sec_size != 0)
  6474. {
  6475. ok = false;
  6476. goto error_return;
  6477. }
  6478. ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
  6479. XTENSA_PROP_SEC_NAME, false);
  6480. if (ptblsize < 0)
  6481. {
  6482. ok = false;
  6483. goto error_return;
  6484. }
  6485. /* Precompute the opcode for each relocation. */
  6486. reloc_opcodes = build_reloc_opcodes (abfd, sec, contents, internal_relocs);
  6487. build_reloc_ranges (abfd, sec, contents, internal_relocs, reloc_opcodes,
  6488. &relevant_relocs);
  6489. for (i = 0; i < sec->reloc_count; i++)
  6490. {
  6491. Elf_Internal_Rela *irel = &internal_relocs[i];
  6492. bfd_vma r_offset;
  6493. property_table_entry *the_entry;
  6494. int ptbl_idx;
  6495. ebb_t *ebb;
  6496. ebb_constraint ebb_table;
  6497. bfd_size_type simplify_size;
  6498. if (irel && ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_SIMPLIFY)
  6499. continue;
  6500. r_offset = irel->r_offset;
  6501. simplify_size = get_asm_simplify_size (contents, sec_size, r_offset);
  6502. if (simplify_size == 0)
  6503. {
  6504. _bfd_error_handler
  6505. /* xgettext:c-format */
  6506. (_("%pB(%pA+%#" PRIx64 "): could not decode instruction for "
  6507. "XTENSA_ASM_SIMPLIFY relocation; "
  6508. "possible configuration mismatch"),
  6509. sec->owner, sec, (uint64_t) r_offset);
  6510. continue;
  6511. }
  6512. /* If the instruction table is not around, then don't do this
  6513. relaxation. */
  6514. the_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
  6515. sec->vma + irel->r_offset);
  6516. if (the_entry == NULL || XTENSA_NO_NOP_REMOVAL)
  6517. {
  6518. text_action_add (&relax_info->action_list,
  6519. ta_convert_longcall, sec, r_offset,
  6520. 0);
  6521. continue;
  6522. }
  6523. /* If the next longcall happens to be at the same address as an
  6524. unreachable section of size 0, then skip forward. */
  6525. ptbl_idx = the_entry - prop_table;
  6526. while ((the_entry->flags & XTENSA_PROP_UNREACHABLE)
  6527. && the_entry->size == 0
  6528. && ptbl_idx + 1 < ptblsize
  6529. && (prop_table[ptbl_idx + 1].address
  6530. == prop_table[ptbl_idx].address))
  6531. {
  6532. ptbl_idx++;
  6533. the_entry++;
  6534. }
  6535. if (the_entry->flags & XTENSA_PROP_NO_TRANSFORM)
  6536. /* NO_REORDER is OK */
  6537. continue;
  6538. init_ebb_constraint (&ebb_table);
  6539. ebb = &ebb_table.ebb;
  6540. init_ebb (ebb, sec, contents, sec_size, prop_table, ptblsize,
  6541. internal_relocs, sec->reloc_count);
  6542. ebb->start_offset = r_offset + simplify_size;
  6543. ebb->end_offset = r_offset + simplify_size;
  6544. ebb->start_ptbl_idx = ptbl_idx;
  6545. ebb->end_ptbl_idx = ptbl_idx;
  6546. ebb->start_reloc_idx = i;
  6547. ebb->end_reloc_idx = i;
  6548. if (!extend_ebb_bounds (ebb)
  6549. || !compute_ebb_proposed_actions (&ebb_table)
  6550. || !compute_ebb_actions (&ebb_table)
  6551. || !check_section_ebb_pcrels_fit (abfd, sec, contents,
  6552. internal_relocs,
  6553. &relevant_relocs,
  6554. &ebb_table, reloc_opcodes)
  6555. || !check_section_ebb_reduces (&ebb_table))
  6556. {
  6557. /* If anything goes wrong or we get unlucky and something does
  6558. not fit, with our plan because of expansion between
  6559. critical branches, just convert to a NOP. */
  6560. text_action_add (&relax_info->action_list,
  6561. ta_convert_longcall, sec, r_offset, 0);
  6562. i = ebb_table.ebb.end_reloc_idx;
  6563. free_ebb_constraint (&ebb_table);
  6564. continue;
  6565. }
  6566. text_action_add_proposed (&relax_info->action_list, &ebb_table, sec);
  6567. /* Update the index so we do not go looking at the relocations
  6568. we have already processed. */
  6569. i = ebb_table.ebb.end_reloc_idx;
  6570. free_ebb_constraint (&ebb_table);
  6571. }
  6572. free_reloc_range_list (&relevant_relocs);
  6573. #if DEBUG
  6574. if (action_list_count (&relax_info->action_list))
  6575. print_action_list (stderr, &relax_info->action_list);
  6576. #endif
  6577. error_return:
  6578. release_contents (sec, contents);
  6579. release_internal_relocs (sec, internal_relocs);
  6580. free (prop_table);
  6581. free (reloc_opcodes);
  6582. return ok;
  6583. }
  6584. /* Do not widen an instruction if it is preceeded by a
  6585. loop opcode. It might cause misalignment. */
  6586. static bool
  6587. prev_instr_is_a_loop (bfd_byte *contents,
  6588. bfd_size_type content_length,
  6589. bfd_size_type offset)
  6590. {
  6591. xtensa_opcode prev_opcode;
  6592. if (offset < 3)
  6593. return false;
  6594. prev_opcode = insn_decode_opcode (contents, content_length, offset-3, 0);
  6595. return (xtensa_opcode_is_loop (xtensa_default_isa, prev_opcode) == 1);
  6596. }
  6597. /* Find all of the possible actions for an extended basic block. */
  6598. bool
  6599. compute_ebb_proposed_actions (ebb_constraint *ebb_table)
  6600. {
  6601. const ebb_t *ebb = &ebb_table->ebb;
  6602. unsigned rel_idx = ebb->start_reloc_idx;
  6603. property_table_entry *entry, *start_entry, *end_entry;
  6604. bfd_vma offset = 0;
  6605. xtensa_isa isa = xtensa_default_isa;
  6606. xtensa_format fmt;
  6607. static xtensa_insnbuf insnbuf = NULL;
  6608. static xtensa_insnbuf slotbuf = NULL;
  6609. if (insnbuf == NULL)
  6610. {
  6611. insnbuf = xtensa_insnbuf_alloc (isa);
  6612. slotbuf = xtensa_insnbuf_alloc (isa);
  6613. }
  6614. start_entry = &ebb->ptbl[ebb->start_ptbl_idx];
  6615. end_entry = &ebb->ptbl[ebb->end_ptbl_idx];
  6616. for (entry = start_entry; entry <= end_entry; entry++)
  6617. {
  6618. bfd_vma start_offset, end_offset;
  6619. bfd_size_type insn_len;
  6620. start_offset = entry->address - ebb->sec->vma;
  6621. end_offset = entry->address + entry->size - ebb->sec->vma;
  6622. if (entry == start_entry)
  6623. start_offset = ebb->start_offset;
  6624. if (entry == end_entry)
  6625. end_offset = ebb->end_offset;
  6626. offset = start_offset;
  6627. if (offset == entry->address - ebb->sec->vma
  6628. && (entry->flags & XTENSA_PROP_INSN_BRANCH_TARGET) != 0)
  6629. {
  6630. enum ebb_target_enum align_type = EBB_DESIRE_TGT_ALIGN;
  6631. BFD_ASSERT (offset != end_offset);
  6632. if (offset == end_offset)
  6633. return false;
  6634. insn_len = insn_decode_len (ebb->contents, ebb->content_length,
  6635. offset);
  6636. if (insn_len == 0)
  6637. goto decode_error;
  6638. if (check_branch_target_aligned_address (offset, insn_len))
  6639. align_type = EBB_REQUIRE_TGT_ALIGN;
  6640. ebb_propose_action (ebb_table, align_type, 0,
  6641. ta_none, offset, 0, true);
  6642. }
  6643. while (offset != end_offset)
  6644. {
  6645. Elf_Internal_Rela *irel;
  6646. xtensa_opcode opcode;
  6647. while (rel_idx < ebb->end_reloc_idx
  6648. && (ebb->relocs[rel_idx].r_offset < offset
  6649. || (ebb->relocs[rel_idx].r_offset == offset
  6650. && (ELF32_R_TYPE (ebb->relocs[rel_idx].r_info)
  6651. != R_XTENSA_ASM_SIMPLIFY))))
  6652. rel_idx++;
  6653. /* Check for longcall. */
  6654. irel = &ebb->relocs[rel_idx];
  6655. if (irel->r_offset == offset
  6656. && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_SIMPLIFY)
  6657. {
  6658. bfd_size_type simplify_size;
  6659. simplify_size = get_asm_simplify_size (ebb->contents,
  6660. ebb->content_length,
  6661. irel->r_offset);
  6662. if (simplify_size == 0)
  6663. goto decode_error;
  6664. ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
  6665. ta_convert_longcall, offset, 0, true);
  6666. offset += simplify_size;
  6667. continue;
  6668. }
  6669. if (offset + MIN_INSN_LENGTH > ebb->content_length)
  6670. goto decode_error;
  6671. xtensa_insnbuf_from_chars (isa, insnbuf, &ebb->contents[offset],
  6672. ebb->content_length - offset);
  6673. fmt = xtensa_format_decode (isa, insnbuf);
  6674. if (fmt == XTENSA_UNDEFINED)
  6675. goto decode_error;
  6676. insn_len = xtensa_format_length (isa, fmt);
  6677. if (insn_len == (bfd_size_type) XTENSA_UNDEFINED)
  6678. goto decode_error;
  6679. if (xtensa_format_num_slots (isa, fmt) != 1)
  6680. {
  6681. offset += insn_len;
  6682. continue;
  6683. }
  6684. xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
  6685. opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
  6686. if (opcode == XTENSA_UNDEFINED)
  6687. goto decode_error;
  6688. if ((entry->flags & XTENSA_PROP_INSN_NO_DENSITY) == 0
  6689. && (entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
  6690. && can_narrow_instruction (slotbuf, fmt, opcode) != 0)
  6691. {
  6692. /* Add an instruction narrow action. */
  6693. ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
  6694. ta_narrow_insn, offset, 0, false);
  6695. }
  6696. else if ((entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
  6697. && can_widen_instruction (slotbuf, fmt, opcode) != 0
  6698. && ! prev_instr_is_a_loop (ebb->contents,
  6699. ebb->content_length, offset))
  6700. {
  6701. /* Add an instruction widen action. */
  6702. ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
  6703. ta_widen_insn, offset, 0, false);
  6704. }
  6705. else if (xtensa_opcode_is_loop (xtensa_default_isa, opcode) == 1)
  6706. {
  6707. /* Check for branch targets. */
  6708. ebb_propose_action (ebb_table, EBB_REQUIRE_LOOP_ALIGN, 0,
  6709. ta_none, offset, 0, true);
  6710. }
  6711. offset += insn_len;
  6712. }
  6713. }
  6714. if (ebb->ends_unreachable)
  6715. {
  6716. ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
  6717. ta_fill, ebb->end_offset, 0, true);
  6718. }
  6719. return true;
  6720. decode_error:
  6721. _bfd_error_handler
  6722. /* xgettext:c-format */
  6723. (_("%pB(%pA+%#" PRIx64 "): could not decode instruction; "
  6724. "possible configuration mismatch"),
  6725. ebb->sec->owner, ebb->sec, (uint64_t) offset);
  6726. return false;
  6727. }
  6728. /* After all of the information has collected about the
  6729. transformations possible in an EBB, compute the appropriate actions
  6730. here in compute_ebb_actions. We still must check later to make
  6731. sure that the actions do not break any relocations. The algorithm
  6732. used here is pretty greedy. Basically, it removes as many no-ops
  6733. as possible so that the end of the EBB has the same alignment
  6734. characteristics as the original. First, it uses narrowing, then
  6735. fill space at the end of the EBB, and finally widenings. If that
  6736. does not work, it tries again with one fewer no-op removed. The
  6737. optimization will only be performed if all of the branch targets
  6738. that were aligned before transformation are also aligned after the
  6739. transformation.
  6740. When the size_opt flag is set, ignore the branch target alignments,
  6741. narrow all wide instructions, and remove all no-ops unless the end
  6742. of the EBB prevents it. */
  6743. bool
  6744. compute_ebb_actions (ebb_constraint *ebb_table)
  6745. {
  6746. unsigned i = 0;
  6747. unsigned j;
  6748. int removed_bytes = 0;
  6749. ebb_t *ebb = &ebb_table->ebb;
  6750. unsigned seg_idx_start = 0;
  6751. unsigned seg_idx_end = 0;
  6752. /* We perform this like the assembler relaxation algorithm: Start by
  6753. assuming all instructions are narrow and all no-ops removed; then
  6754. walk through.... */
  6755. /* For each segment of this that has a solid constraint, check to
  6756. see if there are any combinations that will keep the constraint.
  6757. If so, use it. */
  6758. for (seg_idx_end = 0; seg_idx_end < ebb_table->action_count; seg_idx_end++)
  6759. {
  6760. bool requires_text_end_align = false;
  6761. unsigned longcall_count = 0;
  6762. unsigned longcall_convert_count = 0;
  6763. unsigned narrowable_count = 0;
  6764. unsigned narrowable_convert_count = 0;
  6765. unsigned widenable_count = 0;
  6766. unsigned widenable_convert_count = 0;
  6767. proposed_action *action = NULL;
  6768. int align = (1 << ebb_table->ebb.sec->alignment_power);
  6769. seg_idx_start = seg_idx_end;
  6770. for (i = seg_idx_start; i < ebb_table->action_count; i++)
  6771. {
  6772. action = &ebb_table->actions[i];
  6773. if (action->action == ta_convert_longcall)
  6774. longcall_count++;
  6775. if (action->action == ta_narrow_insn)
  6776. narrowable_count++;
  6777. if (action->action == ta_widen_insn)
  6778. widenable_count++;
  6779. if (action->action == ta_fill)
  6780. break;
  6781. if (action->align_type == EBB_REQUIRE_LOOP_ALIGN)
  6782. break;
  6783. if (action->align_type == EBB_REQUIRE_TGT_ALIGN
  6784. && !elf32xtensa_size_opt)
  6785. break;
  6786. }
  6787. seg_idx_end = i;
  6788. if (seg_idx_end == ebb_table->action_count && !ebb->ends_unreachable)
  6789. requires_text_end_align = true;
  6790. if (elf32xtensa_size_opt && !requires_text_end_align
  6791. && action->align_type != EBB_REQUIRE_LOOP_ALIGN
  6792. && action->align_type != EBB_REQUIRE_TGT_ALIGN)
  6793. {
  6794. longcall_convert_count = longcall_count;
  6795. narrowable_convert_count = narrowable_count;
  6796. widenable_convert_count = 0;
  6797. }
  6798. else
  6799. {
  6800. /* There is a constraint. Convert the max number of longcalls. */
  6801. narrowable_convert_count = 0;
  6802. longcall_convert_count = 0;
  6803. widenable_convert_count = 0;
  6804. for (j = 0; j < longcall_count; j++)
  6805. {
  6806. int removed = (longcall_count - j) * 3 & (align - 1);
  6807. unsigned desire_narrow = (align - removed) & (align - 1);
  6808. unsigned desire_widen = removed;
  6809. if (desire_narrow <= narrowable_count)
  6810. {
  6811. narrowable_convert_count = desire_narrow;
  6812. narrowable_convert_count +=
  6813. (align * ((narrowable_count - narrowable_convert_count)
  6814. / align));
  6815. longcall_convert_count = (longcall_count - j);
  6816. widenable_convert_count = 0;
  6817. break;
  6818. }
  6819. if (desire_widen <= widenable_count && !elf32xtensa_size_opt)
  6820. {
  6821. narrowable_convert_count = 0;
  6822. longcall_convert_count = longcall_count - j;
  6823. widenable_convert_count = desire_widen;
  6824. break;
  6825. }
  6826. }
  6827. }
  6828. /* Now the number of conversions are saved. Do them. */
  6829. for (i = seg_idx_start; i < seg_idx_end; i++)
  6830. {
  6831. action = &ebb_table->actions[i];
  6832. switch (action->action)
  6833. {
  6834. case ta_convert_longcall:
  6835. if (longcall_convert_count != 0)
  6836. {
  6837. action->action = ta_remove_longcall;
  6838. action->do_action = true;
  6839. action->removed_bytes += 3;
  6840. longcall_convert_count--;
  6841. }
  6842. break;
  6843. case ta_narrow_insn:
  6844. if (narrowable_convert_count != 0)
  6845. {
  6846. action->do_action = true;
  6847. action->removed_bytes += 1;
  6848. narrowable_convert_count--;
  6849. }
  6850. break;
  6851. case ta_widen_insn:
  6852. if (widenable_convert_count != 0)
  6853. {
  6854. action->do_action = true;
  6855. action->removed_bytes -= 1;
  6856. widenable_convert_count--;
  6857. }
  6858. break;
  6859. default:
  6860. break;
  6861. }
  6862. }
  6863. }
  6864. /* Now we move on to some local opts. Try to remove each of the
  6865. remaining longcalls. */
  6866. if (ebb_table->ebb.ends_section || ebb_table->ebb.ends_unreachable)
  6867. {
  6868. removed_bytes = 0;
  6869. for (i = 0; i < ebb_table->action_count; i++)
  6870. {
  6871. int old_removed_bytes = removed_bytes;
  6872. proposed_action *action = &ebb_table->actions[i];
  6873. if (action->do_action && action->action == ta_convert_longcall)
  6874. {
  6875. bool bad_alignment = false;
  6876. removed_bytes += 3;
  6877. for (j = i + 1; j < ebb_table->action_count; j++)
  6878. {
  6879. proposed_action *new_action = &ebb_table->actions[j];
  6880. bfd_vma offset = new_action->offset;
  6881. if (new_action->align_type == EBB_REQUIRE_TGT_ALIGN)
  6882. {
  6883. if (!check_branch_target_aligned
  6884. (ebb_table->ebb.contents,
  6885. ebb_table->ebb.content_length,
  6886. offset, offset - removed_bytes))
  6887. {
  6888. bad_alignment = true;
  6889. break;
  6890. }
  6891. }
  6892. if (new_action->align_type == EBB_REQUIRE_LOOP_ALIGN)
  6893. {
  6894. if (!check_loop_aligned (ebb_table->ebb.contents,
  6895. ebb_table->ebb.content_length,
  6896. offset,
  6897. offset - removed_bytes))
  6898. {
  6899. bad_alignment = true;
  6900. break;
  6901. }
  6902. }
  6903. if (new_action->action == ta_narrow_insn
  6904. && !new_action->do_action
  6905. && ebb_table->ebb.sec->alignment_power == 2)
  6906. {
  6907. /* Narrow an instruction and we are done. */
  6908. new_action->do_action = true;
  6909. new_action->removed_bytes += 1;
  6910. bad_alignment = false;
  6911. break;
  6912. }
  6913. if (new_action->action == ta_widen_insn
  6914. && new_action->do_action
  6915. && ebb_table->ebb.sec->alignment_power == 2)
  6916. {
  6917. /* Narrow an instruction and we are done. */
  6918. new_action->do_action = false;
  6919. new_action->removed_bytes += 1;
  6920. bad_alignment = false;
  6921. break;
  6922. }
  6923. if (new_action->do_action)
  6924. removed_bytes += new_action->removed_bytes;
  6925. }
  6926. if (!bad_alignment)
  6927. {
  6928. action->removed_bytes += 3;
  6929. action->action = ta_remove_longcall;
  6930. action->do_action = true;
  6931. }
  6932. }
  6933. removed_bytes = old_removed_bytes;
  6934. if (action->do_action)
  6935. removed_bytes += action->removed_bytes;
  6936. }
  6937. }
  6938. removed_bytes = 0;
  6939. for (i = 0; i < ebb_table->action_count; ++i)
  6940. {
  6941. proposed_action *action = &ebb_table->actions[i];
  6942. if (action->do_action)
  6943. removed_bytes += action->removed_bytes;
  6944. }
  6945. if ((removed_bytes % (1 << ebb_table->ebb.sec->alignment_power)) != 0
  6946. && ebb->ends_unreachable)
  6947. {
  6948. proposed_action *action;
  6949. int br;
  6950. int extra_space;
  6951. BFD_ASSERT (ebb_table->action_count != 0);
  6952. action = &ebb_table->actions[ebb_table->action_count - 1];
  6953. BFD_ASSERT (action->action == ta_fill);
  6954. BFD_ASSERT (ebb->ends_unreachable->flags & XTENSA_PROP_UNREACHABLE);
  6955. extra_space = xtensa_compute_fill_extra_space (ebb->ends_unreachable);
  6956. br = action->removed_bytes + removed_bytes + extra_space;
  6957. br = br & ((1 << ebb->sec->alignment_power ) - 1);
  6958. action->removed_bytes = extra_space - br;
  6959. }
  6960. return true;
  6961. }
  6962. /* The xlate_map is a sorted array of address mappings designed to
  6963. answer the offset_with_removed_text() query with a binary search instead
  6964. of a linear search through the section's action_list. */
  6965. typedef struct xlate_map_entry xlate_map_entry_t;
  6966. typedef struct xlate_map xlate_map_t;
  6967. struct xlate_map_entry
  6968. {
  6969. bfd_vma orig_address;
  6970. bfd_vma new_address;
  6971. unsigned size;
  6972. };
  6973. struct xlate_map
  6974. {
  6975. unsigned entry_count;
  6976. xlate_map_entry_t *entry;
  6977. };
  6978. static int
  6979. xlate_compare (const void *a_v, const void *b_v)
  6980. {
  6981. const xlate_map_entry_t *a = (const xlate_map_entry_t *) a_v;
  6982. const xlate_map_entry_t *b = (const xlate_map_entry_t *) b_v;
  6983. if (a->orig_address < b->orig_address)
  6984. return -1;
  6985. if (a->orig_address > (b->orig_address + b->size - 1))
  6986. return 1;
  6987. return 0;
  6988. }
  6989. static bfd_vma
  6990. xlate_offset_with_removed_text (const xlate_map_t *map,
  6991. text_action_list *action_list,
  6992. bfd_vma offset)
  6993. {
  6994. void *r;
  6995. xlate_map_entry_t *e;
  6996. struct xlate_map_entry se;
  6997. if (map == NULL)
  6998. return offset_with_removed_text (action_list, offset);
  6999. if (map->entry_count == 0)
  7000. return offset;
  7001. se.orig_address = offset;
  7002. r = bsearch (&se, map->entry, map->entry_count,
  7003. sizeof (xlate_map_entry_t), &xlate_compare);
  7004. e = (xlate_map_entry_t *) r;
  7005. /* There could be a jump past the end of the section,
  7006. allow it using the last xlate map entry to translate its address. */
  7007. if (e == NULL)
  7008. {
  7009. e = map->entry + map->entry_count - 1;
  7010. if (xlate_compare (&se, e) <= 0)
  7011. e = NULL;
  7012. }
  7013. BFD_ASSERT (e != NULL);
  7014. if (e == NULL)
  7015. return offset;
  7016. return e->new_address - e->orig_address + offset;
  7017. }
  7018. typedef struct xlate_map_context_struct xlate_map_context;
  7019. struct xlate_map_context_struct
  7020. {
  7021. xlate_map_t *map;
  7022. xlate_map_entry_t *current_entry;
  7023. int removed;
  7024. };
  7025. static int
  7026. xlate_map_fn (splay_tree_node node, void *p)
  7027. {
  7028. text_action *r = (text_action *)node->value;
  7029. xlate_map_context *ctx = p;
  7030. unsigned orig_size = 0;
  7031. switch (r->action)
  7032. {
  7033. case ta_none:
  7034. case ta_remove_insn:
  7035. case ta_convert_longcall:
  7036. case ta_remove_literal:
  7037. case ta_add_literal:
  7038. break;
  7039. case ta_remove_longcall:
  7040. orig_size = 6;
  7041. break;
  7042. case ta_narrow_insn:
  7043. orig_size = 3;
  7044. break;
  7045. case ta_widen_insn:
  7046. orig_size = 2;
  7047. break;
  7048. case ta_fill:
  7049. break;
  7050. }
  7051. ctx->current_entry->size =
  7052. r->offset + orig_size - ctx->current_entry->orig_address;
  7053. if (ctx->current_entry->size != 0)
  7054. {
  7055. ctx->current_entry++;
  7056. ctx->map->entry_count++;
  7057. }
  7058. ctx->current_entry->orig_address = r->offset + orig_size;
  7059. ctx->removed += r->removed_bytes;
  7060. ctx->current_entry->new_address = r->offset + orig_size - ctx->removed;
  7061. ctx->current_entry->size = 0;
  7062. return 0;
  7063. }
  7064. /* Build a binary searchable offset translation map from a section's
  7065. action list. */
  7066. static xlate_map_t *
  7067. build_xlate_map (asection *sec, xtensa_relax_info *relax_info)
  7068. {
  7069. text_action_list *action_list = &relax_info->action_list;
  7070. unsigned num_actions = 0;
  7071. xlate_map_context ctx;
  7072. ctx.map = (xlate_map_t *) bfd_malloc (sizeof (xlate_map_t));
  7073. if (ctx.map == NULL)
  7074. return NULL;
  7075. num_actions = action_list_count (action_list);
  7076. ctx.map->entry = (xlate_map_entry_t *)
  7077. bfd_malloc (sizeof (xlate_map_entry_t) * (num_actions + 1));
  7078. if (ctx.map->entry == NULL)
  7079. {
  7080. free (ctx.map);
  7081. return NULL;
  7082. }
  7083. ctx.map->entry_count = 0;
  7084. ctx.removed = 0;
  7085. ctx.current_entry = &ctx.map->entry[0];
  7086. ctx.current_entry->orig_address = 0;
  7087. ctx.current_entry->new_address = 0;
  7088. ctx.current_entry->size = 0;
  7089. splay_tree_foreach (action_list->tree, xlate_map_fn, &ctx);
  7090. ctx.current_entry->size = (bfd_get_section_limit (sec->owner, sec)
  7091. - ctx.current_entry->orig_address);
  7092. if (ctx.current_entry->size != 0)
  7093. ctx.map->entry_count++;
  7094. return ctx.map;
  7095. }
  7096. /* Free an offset translation map. */
  7097. static void
  7098. free_xlate_map (xlate_map_t *map)
  7099. {
  7100. if (map)
  7101. {
  7102. free (map->entry);
  7103. free (map);
  7104. }
  7105. }
  7106. /* Use check_section_ebb_pcrels_fit to make sure that all of the
  7107. relocations in a section will fit if a proposed set of actions
  7108. are performed. */
  7109. static bool
  7110. check_section_ebb_pcrels_fit (bfd *abfd,
  7111. asection *sec,
  7112. bfd_byte *contents,
  7113. Elf_Internal_Rela *internal_relocs,
  7114. reloc_range_list *relevant_relocs,
  7115. const ebb_constraint *constraint,
  7116. const xtensa_opcode *reloc_opcodes)
  7117. {
  7118. unsigned i, j;
  7119. unsigned n = sec->reloc_count;
  7120. Elf_Internal_Rela *irel;
  7121. xlate_map_t *xmap = NULL;
  7122. bool ok = true;
  7123. xtensa_relax_info *relax_info;
  7124. reloc_range_list_entry *entry = NULL;
  7125. relax_info = get_xtensa_relax_info (sec);
  7126. if (relax_info && sec->reloc_count > 100)
  7127. {
  7128. xmap = build_xlate_map (sec, relax_info);
  7129. /* NULL indicates out of memory, but the slow version
  7130. can still be used. */
  7131. }
  7132. if (relevant_relocs && constraint->action_count)
  7133. {
  7134. if (!relevant_relocs->ok)
  7135. {
  7136. ok = false;
  7137. n = 0;
  7138. }
  7139. else
  7140. {
  7141. bfd_vma min_offset, max_offset;
  7142. min_offset = max_offset = constraint->actions[0].offset;
  7143. for (i = 1; i < constraint->action_count; ++i)
  7144. {
  7145. proposed_action *action = &constraint->actions[i];
  7146. bfd_vma offset = action->offset;
  7147. if (offset < min_offset)
  7148. min_offset = offset;
  7149. if (offset > max_offset)
  7150. max_offset = offset;
  7151. }
  7152. reloc_range_list_update_range (relevant_relocs, min_offset,
  7153. max_offset);
  7154. n = relevant_relocs->n_list;
  7155. entry = &relevant_relocs->list_root;
  7156. }
  7157. }
  7158. else
  7159. {
  7160. relevant_relocs = NULL;
  7161. }
  7162. for (i = 0; i < n; i++)
  7163. {
  7164. r_reloc r_rel;
  7165. bfd_vma orig_self_offset, orig_target_offset;
  7166. bfd_vma self_offset, target_offset;
  7167. int r_type;
  7168. reloc_howto_type *howto;
  7169. int self_removed_bytes, target_removed_bytes;
  7170. if (relevant_relocs)
  7171. {
  7172. entry = entry->next;
  7173. irel = entry->irel;
  7174. }
  7175. else
  7176. {
  7177. irel = internal_relocs + i;
  7178. }
  7179. r_type = ELF32_R_TYPE (irel->r_info);
  7180. howto = &elf_howto_table[r_type];
  7181. /* We maintain the required invariant: PC-relative relocations
  7182. that fit before linking must fit after linking. Thus we only
  7183. need to deal with relocations to the same section that are
  7184. PC-relative. */
  7185. if (r_type == R_XTENSA_ASM_SIMPLIFY
  7186. || r_type == R_XTENSA_32_PCREL
  7187. || !howto->pc_relative)
  7188. continue;
  7189. r_reloc_init (&r_rel, abfd, irel, contents,
  7190. bfd_get_section_limit (abfd, sec));
  7191. if (r_reloc_get_section (&r_rel) != sec)
  7192. continue;
  7193. orig_self_offset = irel->r_offset;
  7194. orig_target_offset = r_rel.target_offset;
  7195. self_offset = orig_self_offset;
  7196. target_offset = orig_target_offset;
  7197. if (relax_info)
  7198. {
  7199. self_offset =
  7200. xlate_offset_with_removed_text (xmap, &relax_info->action_list,
  7201. orig_self_offset);
  7202. target_offset =
  7203. xlate_offset_with_removed_text (xmap, &relax_info->action_list,
  7204. orig_target_offset);
  7205. }
  7206. self_removed_bytes = 0;
  7207. target_removed_bytes = 0;
  7208. for (j = 0; j < constraint->action_count; ++j)
  7209. {
  7210. proposed_action *action = &constraint->actions[j];
  7211. bfd_vma offset = action->offset;
  7212. int removed_bytes = action->removed_bytes;
  7213. if (offset < orig_self_offset
  7214. || (offset == orig_self_offset && action->action == ta_fill
  7215. && action->removed_bytes < 0))
  7216. self_removed_bytes += removed_bytes;
  7217. if (offset < orig_target_offset
  7218. || (offset == orig_target_offset && action->action == ta_fill
  7219. && action->removed_bytes < 0))
  7220. target_removed_bytes += removed_bytes;
  7221. }
  7222. self_offset -= self_removed_bytes;
  7223. target_offset -= target_removed_bytes;
  7224. /* Try to encode it. Get the operand and check. */
  7225. if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
  7226. {
  7227. /* None of the current alternate relocs are PC-relative,
  7228. and only PC-relative relocs matter here. */
  7229. }
  7230. else
  7231. {
  7232. xtensa_opcode opcode;
  7233. int opnum;
  7234. if (relevant_relocs)
  7235. {
  7236. opcode = entry->opcode;
  7237. opnum = entry->opnum;
  7238. }
  7239. else
  7240. {
  7241. if (reloc_opcodes)
  7242. opcode = reloc_opcodes[relevant_relocs ?
  7243. (unsigned)(entry - relevant_relocs->reloc) : i];
  7244. else
  7245. opcode = get_relocation_opcode (abfd, sec, contents, irel);
  7246. if (opcode == XTENSA_UNDEFINED)
  7247. {
  7248. ok = false;
  7249. break;
  7250. }
  7251. opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
  7252. if (opnum == XTENSA_UNDEFINED)
  7253. {
  7254. ok = false;
  7255. break;
  7256. }
  7257. }
  7258. if (!pcrel_reloc_fits (opcode, opnum, self_offset, target_offset))
  7259. {
  7260. ok = false;
  7261. break;
  7262. }
  7263. }
  7264. }
  7265. free_xlate_map (xmap);
  7266. return ok;
  7267. }
  7268. static bool
  7269. check_section_ebb_reduces (const ebb_constraint *constraint)
  7270. {
  7271. int removed = 0;
  7272. unsigned i;
  7273. for (i = 0; i < constraint->action_count; i++)
  7274. {
  7275. const proposed_action *action = &constraint->actions[i];
  7276. if (action->do_action)
  7277. removed += action->removed_bytes;
  7278. }
  7279. if (removed < 0)
  7280. return false;
  7281. return true;
  7282. }
  7283. void
  7284. text_action_add_proposed (text_action_list *l,
  7285. const ebb_constraint *ebb_table,
  7286. asection *sec)
  7287. {
  7288. unsigned i;
  7289. for (i = 0; i < ebb_table->action_count; i++)
  7290. {
  7291. proposed_action *action = &ebb_table->actions[i];
  7292. if (!action->do_action)
  7293. continue;
  7294. switch (action->action)
  7295. {
  7296. case ta_remove_insn:
  7297. case ta_remove_longcall:
  7298. case ta_convert_longcall:
  7299. case ta_narrow_insn:
  7300. case ta_widen_insn:
  7301. case ta_fill:
  7302. case ta_remove_literal:
  7303. text_action_add (l, action->action, sec, action->offset,
  7304. action->removed_bytes);
  7305. break;
  7306. case ta_none:
  7307. break;
  7308. default:
  7309. BFD_ASSERT (0);
  7310. break;
  7311. }
  7312. }
  7313. }
  7314. int
  7315. xtensa_compute_fill_extra_space (property_table_entry *entry)
  7316. {
  7317. int fill_extra_space;
  7318. if (!entry)
  7319. return 0;
  7320. if ((entry->flags & XTENSA_PROP_UNREACHABLE) == 0)
  7321. return 0;
  7322. fill_extra_space = entry->size;
  7323. if ((entry->flags & XTENSA_PROP_ALIGN) != 0)
  7324. {
  7325. /* Fill bytes for alignment:
  7326. (2**n)-1 - (addr + (2**n)-1) & (2**n -1) */
  7327. int pow = GET_XTENSA_PROP_ALIGNMENT (entry->flags);
  7328. int nsm = (1 << pow) - 1;
  7329. bfd_vma addr = entry->address + entry->size;
  7330. bfd_vma align_fill = nsm - ((addr + nsm) & nsm);
  7331. fill_extra_space += align_fill;
  7332. }
  7333. return fill_extra_space;
  7334. }
  7335. /* First relaxation pass. */
  7336. /* If the section contains relaxable literals, check each literal to
  7337. see if it has the same value as another literal that has already
  7338. been seen, either in the current section or a previous one. If so,
  7339. add an entry to the per-section list of removed literals. The
  7340. actual changes are deferred until the next pass. */
  7341. static bool
  7342. compute_removed_literals (bfd *abfd,
  7343. asection *sec,
  7344. struct bfd_link_info *link_info,
  7345. value_map_hash_table *values)
  7346. {
  7347. xtensa_relax_info *relax_info;
  7348. bfd_byte *contents;
  7349. Elf_Internal_Rela *internal_relocs;
  7350. source_reloc *src_relocs, *rel;
  7351. bool ok = true;
  7352. property_table_entry *prop_table = NULL;
  7353. int ptblsize;
  7354. int i, prev_i;
  7355. bool last_loc_is_prev = false;
  7356. bfd_vma last_target_offset = 0;
  7357. section_cache_t target_sec_cache;
  7358. bfd_size_type sec_size;
  7359. init_section_cache (&target_sec_cache);
  7360. /* Do nothing if it is not a relaxable literal section. */
  7361. relax_info = get_xtensa_relax_info (sec);
  7362. BFD_ASSERT (relax_info);
  7363. if (!relax_info->is_relaxable_literal_section)
  7364. return ok;
  7365. internal_relocs = retrieve_internal_relocs (abfd, sec,
  7366. link_info->keep_memory);
  7367. sec_size = bfd_get_section_limit (abfd, sec);
  7368. contents = retrieve_contents (abfd, sec, link_info->keep_memory);
  7369. if (contents == NULL && sec_size != 0)
  7370. {
  7371. ok = false;
  7372. goto error_return;
  7373. }
  7374. /* Sort the source_relocs by target offset. */
  7375. src_relocs = relax_info->src_relocs;
  7376. qsort (src_relocs, relax_info->src_count,
  7377. sizeof (source_reloc), source_reloc_compare);
  7378. qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
  7379. internal_reloc_compare);
  7380. ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
  7381. XTENSA_PROP_SEC_NAME, false);
  7382. if (ptblsize < 0)
  7383. {
  7384. ok = false;
  7385. goto error_return;
  7386. }
  7387. prev_i = -1;
  7388. for (i = 0; i < relax_info->src_count; i++)
  7389. {
  7390. Elf_Internal_Rela *irel = NULL;
  7391. rel = &src_relocs[i];
  7392. if (get_l32r_opcode () != rel->opcode)
  7393. continue;
  7394. irel = get_irel_at_offset (sec, internal_relocs,
  7395. rel->r_rel.target_offset);
  7396. /* If the relocation on this is not a simple R_XTENSA_32 or
  7397. R_XTENSA_PLT then do not consider it. This may happen when
  7398. the difference of two symbols is used in a literal. */
  7399. if (irel && (ELF32_R_TYPE (irel->r_info) != R_XTENSA_32
  7400. && ELF32_R_TYPE (irel->r_info) != R_XTENSA_PLT))
  7401. continue;
  7402. /* If the target_offset for this relocation is the same as the
  7403. previous relocation, then we've already considered whether the
  7404. literal can be coalesced. Skip to the next one.... */
  7405. if (i != 0 && prev_i != -1
  7406. && src_relocs[i-1].r_rel.target_offset == rel->r_rel.target_offset)
  7407. continue;
  7408. prev_i = i;
  7409. if (last_loc_is_prev &&
  7410. last_target_offset + 4 != rel->r_rel.target_offset)
  7411. last_loc_is_prev = false;
  7412. /* Check if the relocation was from an L32R that is being removed
  7413. because a CALLX was converted to a direct CALL, and check if
  7414. there are no other relocations to the literal. */
  7415. if (is_removable_literal (rel, i, src_relocs, relax_info->src_count,
  7416. sec, prop_table, ptblsize))
  7417. {
  7418. if (!remove_dead_literal (abfd, sec, link_info, internal_relocs,
  7419. irel, rel, prop_table, ptblsize))
  7420. {
  7421. ok = false;
  7422. goto error_return;
  7423. }
  7424. last_target_offset = rel->r_rel.target_offset;
  7425. continue;
  7426. }
  7427. if (!identify_literal_placement (abfd, sec, contents, link_info,
  7428. values,
  7429. &last_loc_is_prev, irel,
  7430. relax_info->src_count - i, rel,
  7431. prop_table, ptblsize,
  7432. &target_sec_cache, rel->is_abs_literal))
  7433. {
  7434. ok = false;
  7435. goto error_return;
  7436. }
  7437. last_target_offset = rel->r_rel.target_offset;
  7438. }
  7439. #if DEBUG
  7440. print_removed_literals (stderr, &relax_info->removed_list);
  7441. print_action_list (stderr, &relax_info->action_list);
  7442. #endif /* DEBUG */
  7443. error_return:
  7444. free (prop_table);
  7445. free_section_cache (&target_sec_cache);
  7446. release_contents (sec, contents);
  7447. release_internal_relocs (sec, internal_relocs);
  7448. return ok;
  7449. }
  7450. static Elf_Internal_Rela *
  7451. get_irel_at_offset (asection *sec,
  7452. Elf_Internal_Rela *internal_relocs,
  7453. bfd_vma offset)
  7454. {
  7455. unsigned i;
  7456. Elf_Internal_Rela *irel;
  7457. unsigned r_type;
  7458. Elf_Internal_Rela key;
  7459. if (!internal_relocs)
  7460. return NULL;
  7461. key.r_offset = offset;
  7462. irel = bsearch (&key, internal_relocs, sec->reloc_count,
  7463. sizeof (Elf_Internal_Rela), internal_reloc_matches);
  7464. if (!irel)
  7465. return NULL;
  7466. /* bsearch does not guarantee which will be returned if there are
  7467. multiple matches. We need the first that is not an alignment. */
  7468. i = irel - internal_relocs;
  7469. while (i > 0)
  7470. {
  7471. if (internal_relocs[i-1].r_offset != offset)
  7472. break;
  7473. i--;
  7474. }
  7475. for ( ; i < sec->reloc_count; i++)
  7476. {
  7477. irel = &internal_relocs[i];
  7478. r_type = ELF32_R_TYPE (irel->r_info);
  7479. if (irel->r_offset == offset && r_type != R_XTENSA_NONE)
  7480. return irel;
  7481. }
  7482. return NULL;
  7483. }
  7484. bool
  7485. is_removable_literal (const source_reloc *rel,
  7486. int i,
  7487. const source_reloc *src_relocs,
  7488. int src_count,
  7489. asection *sec,
  7490. property_table_entry *prop_table,
  7491. int ptblsize)
  7492. {
  7493. const source_reloc *curr_rel;
  7494. property_table_entry *entry;
  7495. if (!rel->is_null)
  7496. return false;
  7497. entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
  7498. sec->vma + rel->r_rel.target_offset);
  7499. if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
  7500. return false;
  7501. for (++i; i < src_count; ++i)
  7502. {
  7503. curr_rel = &src_relocs[i];
  7504. /* If all others have the same target offset.... */
  7505. if (curr_rel->r_rel.target_offset != rel->r_rel.target_offset)
  7506. return true;
  7507. if (!curr_rel->is_null
  7508. && !xtensa_is_property_section (curr_rel->source_sec)
  7509. && !(curr_rel->source_sec->flags & SEC_DEBUGGING))
  7510. return false;
  7511. }
  7512. return true;
  7513. }
  7514. bool
  7515. remove_dead_literal (bfd *abfd,
  7516. asection *sec,
  7517. struct bfd_link_info *link_info,
  7518. Elf_Internal_Rela *internal_relocs,
  7519. Elf_Internal_Rela *irel,
  7520. source_reloc *rel,
  7521. property_table_entry *prop_table,
  7522. int ptblsize)
  7523. {
  7524. property_table_entry *entry;
  7525. xtensa_relax_info *relax_info;
  7526. relax_info = get_xtensa_relax_info (sec);
  7527. if (!relax_info)
  7528. return false;
  7529. entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
  7530. sec->vma + rel->r_rel.target_offset);
  7531. /* Mark the unused literal so that it will be removed. */
  7532. add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
  7533. text_action_add (&relax_info->action_list,
  7534. ta_remove_literal, sec, rel->r_rel.target_offset, 4);
  7535. /* If the section is 4-byte aligned, do not add fill. */
  7536. if (sec->alignment_power > 2)
  7537. {
  7538. int fill_extra_space;
  7539. bfd_vma entry_sec_offset;
  7540. text_action *fa;
  7541. property_table_entry *the_add_entry;
  7542. int removed_diff;
  7543. if (entry)
  7544. entry_sec_offset = entry->address - sec->vma + entry->size;
  7545. else
  7546. entry_sec_offset = rel->r_rel.target_offset + 4;
  7547. /* If the literal range is at the end of the section,
  7548. do not add fill. */
  7549. the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
  7550. entry_sec_offset);
  7551. fill_extra_space = xtensa_compute_fill_extra_space (the_add_entry);
  7552. fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
  7553. removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
  7554. -4, fill_extra_space);
  7555. if (fa)
  7556. adjust_fill_action (fa, removed_diff);
  7557. else
  7558. text_action_add (&relax_info->action_list,
  7559. ta_fill, sec, entry_sec_offset, removed_diff);
  7560. }
  7561. /* Zero out the relocation on this literal location. */
  7562. if (irel)
  7563. {
  7564. if (elf_hash_table (link_info)->dynamic_sections_created)
  7565. shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
  7566. irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
  7567. pin_internal_relocs (sec, internal_relocs);
  7568. }
  7569. /* Do not modify "last_loc_is_prev". */
  7570. return true;
  7571. }
  7572. bool
  7573. identify_literal_placement (bfd *abfd,
  7574. asection *sec,
  7575. bfd_byte *contents,
  7576. struct bfd_link_info *link_info,
  7577. value_map_hash_table *values,
  7578. bool *last_loc_is_prev_p,
  7579. Elf_Internal_Rela *irel,
  7580. int remaining_src_rels,
  7581. source_reloc *rel,
  7582. property_table_entry *prop_table,
  7583. int ptblsize,
  7584. section_cache_t *target_sec_cache,
  7585. bool is_abs_literal)
  7586. {
  7587. literal_value val;
  7588. value_map *val_map;
  7589. xtensa_relax_info *relax_info;
  7590. bool literal_placed = false;
  7591. r_reloc r_rel;
  7592. unsigned long value;
  7593. bool final_static_link;
  7594. bfd_size_type sec_size;
  7595. relax_info = get_xtensa_relax_info (sec);
  7596. if (!relax_info)
  7597. return false;
  7598. sec_size = bfd_get_section_limit (abfd, sec);
  7599. final_static_link =
  7600. (!bfd_link_relocatable (link_info)
  7601. && !elf_hash_table (link_info)->dynamic_sections_created);
  7602. /* The placement algorithm first checks to see if the literal is
  7603. already in the value map. If so and the value map is reachable
  7604. from all uses, then the literal is moved to that location. If
  7605. not, then we identify the last location where a fresh literal was
  7606. placed. If the literal can be safely moved there, then we do so.
  7607. If not, then we assume that the literal is not to move and leave
  7608. the literal where it is, marking it as the last literal
  7609. location. */
  7610. /* Find the literal value. */
  7611. value = 0;
  7612. r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
  7613. if (!irel)
  7614. {
  7615. BFD_ASSERT (rel->r_rel.target_offset < sec_size);
  7616. value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
  7617. }
  7618. init_literal_value (&val, &r_rel, value, is_abs_literal);
  7619. /* Check if we've seen another literal with the same value that
  7620. is in the same output section. */
  7621. val_map = value_map_get_cached_value (values, &val, final_static_link);
  7622. if (val_map
  7623. && (r_reloc_get_section (&val_map->loc)->output_section
  7624. == sec->output_section)
  7625. && relocations_reach (rel, remaining_src_rels, &val_map->loc)
  7626. && coalesce_shared_literal (sec, rel, prop_table, ptblsize, val_map))
  7627. {
  7628. /* No change to last_loc_is_prev. */
  7629. literal_placed = true;
  7630. }
  7631. /* For relocatable links, do not try to move literals. To do it
  7632. correctly might increase the number of relocations in an input
  7633. section making the default relocatable linking fail. */
  7634. if (!bfd_link_relocatable (link_info) && !literal_placed
  7635. && values->has_last_loc && !(*last_loc_is_prev_p))
  7636. {
  7637. asection *target_sec = r_reloc_get_section (&values->last_loc);
  7638. if (target_sec && target_sec->output_section == sec->output_section)
  7639. {
  7640. /* Increment the virtual offset. */
  7641. r_reloc try_loc = values->last_loc;
  7642. try_loc.virtual_offset += 4;
  7643. /* There is a last loc that was in the same output section. */
  7644. if (relocations_reach (rel, remaining_src_rels, &try_loc)
  7645. && move_shared_literal (sec, link_info, rel,
  7646. prop_table, ptblsize,
  7647. &try_loc, &val, target_sec_cache))
  7648. {
  7649. values->last_loc.virtual_offset += 4;
  7650. literal_placed = true;
  7651. if (!val_map)
  7652. val_map = add_value_map (values, &val, &try_loc,
  7653. final_static_link);
  7654. else
  7655. val_map->loc = try_loc;
  7656. }
  7657. }
  7658. }
  7659. if (!literal_placed)
  7660. {
  7661. /* Nothing worked, leave the literal alone but update the last loc. */
  7662. values->has_last_loc = true;
  7663. values->last_loc = rel->r_rel;
  7664. if (!val_map)
  7665. val_map = add_value_map (values, &val, &rel->r_rel, final_static_link);
  7666. else
  7667. val_map->loc = rel->r_rel;
  7668. *last_loc_is_prev_p = true;
  7669. }
  7670. return true;
  7671. }
  7672. /* Check if the original relocations (presumably on L32R instructions)
  7673. identified by reloc[0..N] can be changed to reference the literal
  7674. identified by r_rel. If r_rel is out of range for any of the
  7675. original relocations, then we don't want to coalesce the original
  7676. literal with the one at r_rel. We only check reloc[0..N], where the
  7677. offsets are all the same as for reloc[0] (i.e., they're all
  7678. referencing the same literal) and where N is also bounded by the
  7679. number of remaining entries in the "reloc" array. The "reloc" array
  7680. is sorted by target offset so we know all the entries for the same
  7681. literal will be contiguous. */
  7682. static bool
  7683. relocations_reach (source_reloc *reloc,
  7684. int remaining_relocs,
  7685. const r_reloc *r_rel)
  7686. {
  7687. bfd_vma from_offset, source_address, dest_address;
  7688. asection *sec;
  7689. int i;
  7690. if (!r_reloc_is_defined (r_rel))
  7691. return false;
  7692. sec = r_reloc_get_section (r_rel);
  7693. from_offset = reloc[0].r_rel.target_offset;
  7694. for (i = 0; i < remaining_relocs; i++)
  7695. {
  7696. if (reloc[i].r_rel.target_offset != from_offset)
  7697. break;
  7698. /* Ignore relocations that have been removed. */
  7699. if (reloc[i].is_null)
  7700. continue;
  7701. /* The original and new output section for these must be the same
  7702. in order to coalesce. */
  7703. if (r_reloc_get_section (&reloc[i].r_rel)->output_section
  7704. != sec->output_section)
  7705. return false;
  7706. /* Absolute literals in the same output section can always be
  7707. combined. */
  7708. if (reloc[i].is_abs_literal)
  7709. continue;
  7710. /* A literal with no PC-relative relocations can be moved anywhere. */
  7711. if (reloc[i].opnd != -1)
  7712. {
  7713. /* Otherwise, check to see that it fits. */
  7714. source_address = (reloc[i].source_sec->output_section->vma
  7715. + reloc[i].source_sec->output_offset
  7716. + reloc[i].r_rel.rela.r_offset);
  7717. dest_address = (sec->output_section->vma
  7718. + sec->output_offset
  7719. + r_rel->target_offset);
  7720. if (!pcrel_reloc_fits (reloc[i].opcode, reloc[i].opnd,
  7721. source_address, dest_address))
  7722. return false;
  7723. }
  7724. }
  7725. return true;
  7726. }
  7727. /* Move a literal to another literal location because it is
  7728. the same as the other literal value. */
  7729. static bool
  7730. coalesce_shared_literal (asection *sec,
  7731. source_reloc *rel,
  7732. property_table_entry *prop_table,
  7733. int ptblsize,
  7734. value_map *val_map)
  7735. {
  7736. property_table_entry *entry;
  7737. text_action *fa;
  7738. property_table_entry *the_add_entry;
  7739. int removed_diff;
  7740. xtensa_relax_info *relax_info;
  7741. relax_info = get_xtensa_relax_info (sec);
  7742. if (!relax_info)
  7743. return false;
  7744. entry = elf_xtensa_find_property_entry
  7745. (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
  7746. if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
  7747. return true;
  7748. /* Mark that the literal will be coalesced. */
  7749. add_removed_literal (&relax_info->removed_list, &rel->r_rel, &val_map->loc);
  7750. text_action_add (&relax_info->action_list,
  7751. ta_remove_literal, sec, rel->r_rel.target_offset, 4);
  7752. /* If the section is 4-byte aligned, do not add fill. */
  7753. if (sec->alignment_power > 2)
  7754. {
  7755. int fill_extra_space;
  7756. bfd_vma entry_sec_offset;
  7757. if (entry)
  7758. entry_sec_offset = entry->address - sec->vma + entry->size;
  7759. else
  7760. entry_sec_offset = rel->r_rel.target_offset + 4;
  7761. /* If the literal range is at the end of the section,
  7762. do not add fill. */
  7763. fill_extra_space = 0;
  7764. the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
  7765. entry_sec_offset);
  7766. if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
  7767. fill_extra_space = the_add_entry->size;
  7768. fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
  7769. removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
  7770. -4, fill_extra_space);
  7771. if (fa)
  7772. adjust_fill_action (fa, removed_diff);
  7773. else
  7774. text_action_add (&relax_info->action_list,
  7775. ta_fill, sec, entry_sec_offset, removed_diff);
  7776. }
  7777. return true;
  7778. }
  7779. /* Move a literal to another location. This may actually increase the
  7780. total amount of space used because of alignments so we need to do
  7781. this carefully. Also, it may make a branch go out of range. */
  7782. static bool
  7783. move_shared_literal (asection *sec,
  7784. struct bfd_link_info *link_info,
  7785. source_reloc *rel,
  7786. property_table_entry *prop_table,
  7787. int ptblsize,
  7788. const r_reloc *target_loc,
  7789. const literal_value *lit_value,
  7790. section_cache_t *target_sec_cache)
  7791. {
  7792. property_table_entry *the_add_entry, *src_entry, *target_entry = NULL;
  7793. text_action *fa, *target_fa;
  7794. int removed_diff;
  7795. xtensa_relax_info *relax_info, *target_relax_info;
  7796. asection *target_sec;
  7797. ebb_t *ebb;
  7798. ebb_constraint ebb_table;
  7799. bool relocs_fit;
  7800. /* If this routine always returns FALSE, the literals that cannot be
  7801. coalesced will not be moved. */
  7802. if (elf32xtensa_no_literal_movement)
  7803. return false;
  7804. relax_info = get_xtensa_relax_info (sec);
  7805. if (!relax_info)
  7806. return false;
  7807. target_sec = r_reloc_get_section (target_loc);
  7808. target_relax_info = get_xtensa_relax_info (target_sec);
  7809. /* Literals to undefined sections may not be moved because they
  7810. must report an error. */
  7811. if (bfd_is_und_section (target_sec))
  7812. return false;
  7813. src_entry = elf_xtensa_find_property_entry
  7814. (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
  7815. if (!section_cache_section (target_sec_cache, target_sec, link_info))
  7816. return false;
  7817. target_entry = elf_xtensa_find_property_entry
  7818. (target_sec_cache->ptbl, target_sec_cache->pte_count,
  7819. target_sec->vma + target_loc->target_offset);
  7820. if (!target_entry)
  7821. return false;
  7822. /* Make sure that we have not broken any branches. */
  7823. relocs_fit = false;
  7824. init_ebb_constraint (&ebb_table);
  7825. ebb = &ebb_table.ebb;
  7826. init_ebb (ebb, target_sec_cache->sec, target_sec_cache->contents,
  7827. target_sec_cache->content_length,
  7828. target_sec_cache->ptbl, target_sec_cache->pte_count,
  7829. target_sec_cache->relocs, target_sec_cache->reloc_count);
  7830. /* Propose to add 4 bytes + worst-case alignment size increase to
  7831. destination. */
  7832. ebb_propose_action (&ebb_table, EBB_NO_ALIGN, 0,
  7833. ta_fill, target_loc->target_offset,
  7834. -4 - (1 << target_sec->alignment_power), true);
  7835. /* Check all of the PC-relative relocations to make sure they still fit. */
  7836. relocs_fit = check_section_ebb_pcrels_fit (target_sec->owner, target_sec,
  7837. target_sec_cache->contents,
  7838. target_sec_cache->relocs, NULL,
  7839. &ebb_table, NULL);
  7840. if (!relocs_fit)
  7841. return false;
  7842. text_action_add_literal (&target_relax_info->action_list,
  7843. ta_add_literal, target_loc, lit_value, -4);
  7844. if (target_sec->alignment_power > 2 && target_entry != src_entry)
  7845. {
  7846. /* May need to add or remove some fill to maintain alignment. */
  7847. int fill_extra_space;
  7848. bfd_vma entry_sec_offset;
  7849. entry_sec_offset =
  7850. target_entry->address - target_sec->vma + target_entry->size;
  7851. /* If the literal range is at the end of the section,
  7852. do not add fill. */
  7853. fill_extra_space = 0;
  7854. the_add_entry =
  7855. elf_xtensa_find_property_entry (target_sec_cache->ptbl,
  7856. target_sec_cache->pte_count,
  7857. entry_sec_offset);
  7858. if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
  7859. fill_extra_space = the_add_entry->size;
  7860. target_fa = find_fill_action (&target_relax_info->action_list,
  7861. target_sec, entry_sec_offset);
  7862. removed_diff = compute_removed_action_diff (target_fa, target_sec,
  7863. entry_sec_offset, 4,
  7864. fill_extra_space);
  7865. if (target_fa)
  7866. adjust_fill_action (target_fa, removed_diff);
  7867. else
  7868. text_action_add (&target_relax_info->action_list,
  7869. ta_fill, target_sec, entry_sec_offset, removed_diff);
  7870. }
  7871. /* Mark that the literal will be moved to the new location. */
  7872. add_removed_literal (&relax_info->removed_list, &rel->r_rel, target_loc);
  7873. /* Remove the literal. */
  7874. text_action_add (&relax_info->action_list,
  7875. ta_remove_literal, sec, rel->r_rel.target_offset, 4);
  7876. /* If the section is 4-byte aligned, do not add fill. */
  7877. if (sec->alignment_power > 2 && target_entry != src_entry)
  7878. {
  7879. int fill_extra_space;
  7880. bfd_vma entry_sec_offset;
  7881. if (src_entry)
  7882. entry_sec_offset = src_entry->address - sec->vma + src_entry->size;
  7883. else
  7884. entry_sec_offset = rel->r_rel.target_offset+4;
  7885. /* If the literal range is at the end of the section,
  7886. do not add fill. */
  7887. fill_extra_space = 0;
  7888. the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
  7889. entry_sec_offset);
  7890. if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
  7891. fill_extra_space = the_add_entry->size;
  7892. fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
  7893. removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
  7894. -4, fill_extra_space);
  7895. if (fa)
  7896. adjust_fill_action (fa, removed_diff);
  7897. else
  7898. text_action_add (&relax_info->action_list,
  7899. ta_fill, sec, entry_sec_offset, removed_diff);
  7900. }
  7901. return true;
  7902. }
  7903. /* Second relaxation pass. */
  7904. static int
  7905. action_remove_bytes_fn (splay_tree_node node, void *p)
  7906. {
  7907. bfd_size_type *final_size = p;
  7908. text_action *action = (text_action *)node->value;
  7909. *final_size -= action->removed_bytes;
  7910. return 0;
  7911. }
  7912. /* Modify all of the relocations to point to the right spot, and if this
  7913. is a relaxable section, delete the unwanted literals and fix the
  7914. section size. */
  7915. bool
  7916. relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
  7917. {
  7918. Elf_Internal_Rela *internal_relocs;
  7919. xtensa_relax_info *relax_info;
  7920. bfd_byte *contents;
  7921. bool ok = true;
  7922. unsigned i;
  7923. bool rv = false;
  7924. bool virtual_action;
  7925. bfd_size_type sec_size;
  7926. sec_size = bfd_get_section_limit (abfd, sec);
  7927. relax_info = get_xtensa_relax_info (sec);
  7928. BFD_ASSERT (relax_info);
  7929. /* First translate any of the fixes that have been added already. */
  7930. translate_section_fixes (sec);
  7931. /* Handle property sections (e.g., literal tables) specially. */
  7932. if (xtensa_is_property_section (sec))
  7933. {
  7934. BFD_ASSERT (!relax_info->is_relaxable_literal_section);
  7935. return relax_property_section (abfd, sec, link_info);
  7936. }
  7937. internal_relocs = retrieve_internal_relocs (abfd, sec,
  7938. link_info->keep_memory);
  7939. if (!internal_relocs && !action_list_count (&relax_info->action_list))
  7940. return true;
  7941. contents = retrieve_contents (abfd, sec, link_info->keep_memory);
  7942. if (contents == NULL && sec_size != 0)
  7943. {
  7944. ok = false;
  7945. goto error_return;
  7946. }
  7947. if (internal_relocs)
  7948. {
  7949. for (i = 0; i < sec->reloc_count; i++)
  7950. {
  7951. Elf_Internal_Rela *irel;
  7952. xtensa_relax_info *target_relax_info;
  7953. bfd_vma source_offset, old_source_offset;
  7954. r_reloc r_rel;
  7955. unsigned r_type;
  7956. asection *target_sec;
  7957. /* Locally change the source address.
  7958. Translate the target to the new target address.
  7959. If it points to this section and has been removed,
  7960. NULLify it.
  7961. Write it back. */
  7962. irel = &internal_relocs[i];
  7963. source_offset = irel->r_offset;
  7964. old_source_offset = source_offset;
  7965. r_type = ELF32_R_TYPE (irel->r_info);
  7966. r_reloc_init (&r_rel, abfd, irel, contents,
  7967. bfd_get_section_limit (abfd, sec));
  7968. /* If this section could have changed then we may need to
  7969. change the relocation's offset. */
  7970. if (relax_info->is_relaxable_literal_section
  7971. || relax_info->is_relaxable_asm_section)
  7972. {
  7973. pin_internal_relocs (sec, internal_relocs);
  7974. if (r_type != R_XTENSA_NONE
  7975. && find_removed_literal (&relax_info->removed_list,
  7976. irel->r_offset))
  7977. {
  7978. /* Remove this relocation. */
  7979. if (elf_hash_table (link_info)->dynamic_sections_created)
  7980. shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
  7981. irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
  7982. irel->r_offset = offset_with_removed_text_map
  7983. (&relax_info->action_list, irel->r_offset);
  7984. continue;
  7985. }
  7986. if (r_type == R_XTENSA_ASM_SIMPLIFY)
  7987. {
  7988. text_action *action =
  7989. find_insn_action (&relax_info->action_list,
  7990. irel->r_offset);
  7991. if (action && (action->action == ta_convert_longcall
  7992. || action->action == ta_remove_longcall))
  7993. {
  7994. bfd_reloc_status_type retval;
  7995. char *error_message = NULL;
  7996. retval = contract_asm_expansion (contents, sec_size,
  7997. irel, &error_message);
  7998. if (retval != bfd_reloc_ok)
  7999. {
  8000. (*link_info->callbacks->reloc_dangerous)
  8001. (link_info, error_message, abfd, sec,
  8002. irel->r_offset);
  8003. goto error_return;
  8004. }
  8005. /* Update the action so that the code that moves
  8006. the contents will do the right thing. */
  8007. /* ta_remove_longcall and ta_remove_insn actions are
  8008. grouped together in the tree as well as
  8009. ta_convert_longcall and ta_none, so that changes below
  8010. can be done w/o removing and reinserting action into
  8011. the tree. */
  8012. if (action->action == ta_remove_longcall)
  8013. action->action = ta_remove_insn;
  8014. else
  8015. action->action = ta_none;
  8016. /* Refresh the info in the r_rel. */
  8017. r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
  8018. r_type = ELF32_R_TYPE (irel->r_info);
  8019. }
  8020. }
  8021. source_offset = offset_with_removed_text_map
  8022. (&relax_info->action_list, irel->r_offset);
  8023. irel->r_offset = source_offset;
  8024. }
  8025. /* If the target section could have changed then
  8026. we may need to change the relocation's target offset. */
  8027. target_sec = r_reloc_get_section (&r_rel);
  8028. /* For a reference to a discarded section from a DWARF section,
  8029. i.e., where action_discarded is PRETEND, the symbol will
  8030. eventually be modified to refer to the kept section (at least if
  8031. the kept and discarded sections are the same size). Anticipate
  8032. that here and adjust things accordingly. */
  8033. if (! elf_xtensa_ignore_discarded_relocs (sec)
  8034. && elf_xtensa_action_discarded (sec) == PRETEND
  8035. && sec->sec_info_type != SEC_INFO_TYPE_STABS
  8036. && target_sec != NULL
  8037. && discarded_section (target_sec))
  8038. {
  8039. /* It would be natural to call _bfd_elf_check_kept_section
  8040. here, but it's not exported from elflink.c. It's also a
  8041. fairly expensive check. Adjusting the relocations to the
  8042. discarded section is fairly harmless; it will only adjust
  8043. some addends and difference values. If it turns out that
  8044. _bfd_elf_check_kept_section fails later, it won't matter,
  8045. so just compare the section names to find the right group
  8046. member. */
  8047. asection *kept = target_sec->kept_section;
  8048. if (kept != NULL)
  8049. {
  8050. if ((kept->flags & SEC_GROUP) != 0)
  8051. {
  8052. asection *first = elf_next_in_group (kept);
  8053. asection *s = first;
  8054. kept = NULL;
  8055. while (s != NULL)
  8056. {
  8057. if (strcmp (s->name, target_sec->name) == 0)
  8058. {
  8059. kept = s;
  8060. break;
  8061. }
  8062. s = elf_next_in_group (s);
  8063. if (s == first)
  8064. break;
  8065. }
  8066. }
  8067. }
  8068. if (kept != NULL
  8069. && ((target_sec->rawsize != 0
  8070. ? target_sec->rawsize : target_sec->size)
  8071. == (kept->rawsize != 0 ? kept->rawsize : kept->size)))
  8072. target_sec = kept;
  8073. }
  8074. target_relax_info = get_xtensa_relax_info (target_sec);
  8075. if (target_relax_info
  8076. && (target_relax_info->is_relaxable_literal_section
  8077. || target_relax_info->is_relaxable_asm_section))
  8078. {
  8079. r_reloc new_reloc;
  8080. target_sec = translate_reloc (&r_rel, &new_reloc, target_sec);
  8081. if (r_type == R_XTENSA_DIFF8
  8082. || r_type == R_XTENSA_DIFF16
  8083. || r_type == R_XTENSA_DIFF32
  8084. || r_type == R_XTENSA_PDIFF8
  8085. || r_type == R_XTENSA_PDIFF16
  8086. || r_type == R_XTENSA_PDIFF32
  8087. || r_type == R_XTENSA_NDIFF8
  8088. || r_type == R_XTENSA_NDIFF16
  8089. || r_type == R_XTENSA_NDIFF32)
  8090. {
  8091. bfd_signed_vma diff_value = 0;
  8092. bfd_vma new_end_offset, diff_mask = 0;
  8093. if (bfd_get_section_limit (abfd, sec) < old_source_offset)
  8094. {
  8095. (*link_info->callbacks->reloc_dangerous)
  8096. (link_info, _("invalid relocation address"),
  8097. abfd, sec, old_source_offset);
  8098. goto error_return;
  8099. }
  8100. switch (r_type)
  8101. {
  8102. case R_XTENSA_DIFF8:
  8103. diff_mask = 0x7f;
  8104. diff_value =
  8105. bfd_get_signed_8 (abfd, &contents[old_source_offset]);
  8106. break;
  8107. case R_XTENSA_DIFF16:
  8108. diff_mask = 0x7fff;
  8109. diff_value =
  8110. bfd_get_signed_16 (abfd, &contents[old_source_offset]);
  8111. break;
  8112. case R_XTENSA_DIFF32:
  8113. diff_mask = 0x7fffffff;
  8114. diff_value =
  8115. bfd_get_signed_32 (abfd, &contents[old_source_offset]);
  8116. break;
  8117. case R_XTENSA_PDIFF8:
  8118. case R_XTENSA_NDIFF8:
  8119. diff_mask = 0xff;
  8120. diff_value =
  8121. bfd_get_8 (abfd, &contents[old_source_offset]);
  8122. break;
  8123. case R_XTENSA_PDIFF16:
  8124. case R_XTENSA_NDIFF16:
  8125. diff_mask = 0xffff;
  8126. diff_value =
  8127. bfd_get_16 (abfd, &contents[old_source_offset]);
  8128. break;
  8129. case R_XTENSA_PDIFF32:
  8130. case R_XTENSA_NDIFF32:
  8131. diff_mask = 0xffffffff;
  8132. diff_value =
  8133. bfd_get_32 (abfd, &contents[old_source_offset]);
  8134. break;
  8135. }
  8136. if (r_type >= R_XTENSA_NDIFF8
  8137. && r_type <= R_XTENSA_NDIFF32
  8138. && diff_value)
  8139. diff_value |= ~diff_mask;
  8140. new_end_offset = offset_with_removed_text_map
  8141. (&target_relax_info->action_list,
  8142. r_rel.target_offset + diff_value);
  8143. diff_value = new_end_offset - new_reloc.target_offset;
  8144. switch (r_type)
  8145. {
  8146. case R_XTENSA_DIFF8:
  8147. bfd_put_signed_8 (abfd, diff_value,
  8148. &contents[old_source_offset]);
  8149. break;
  8150. case R_XTENSA_DIFF16:
  8151. bfd_put_signed_16 (abfd, diff_value,
  8152. &contents[old_source_offset]);
  8153. break;
  8154. case R_XTENSA_DIFF32:
  8155. bfd_put_signed_32 (abfd, diff_value,
  8156. &contents[old_source_offset]);
  8157. break;
  8158. case R_XTENSA_PDIFF8:
  8159. case R_XTENSA_NDIFF8:
  8160. bfd_put_8 (abfd, diff_value,
  8161. &contents[old_source_offset]);
  8162. break;
  8163. case R_XTENSA_PDIFF16:
  8164. case R_XTENSA_NDIFF16:
  8165. bfd_put_16 (abfd, diff_value,
  8166. &contents[old_source_offset]);
  8167. break;
  8168. case R_XTENSA_PDIFF32:
  8169. case R_XTENSA_NDIFF32:
  8170. bfd_put_32 (abfd, diff_value,
  8171. &contents[old_source_offset]);
  8172. break;
  8173. }
  8174. /* Check for overflow. Sign bits must be all zeroes or
  8175. all ones. When sign bits are all ones diff_value
  8176. may not be zero. */
  8177. if (((diff_value & ~diff_mask) != 0
  8178. && (diff_value & ~diff_mask) != ~diff_mask)
  8179. || (diff_value && (bfd_vma) diff_value == ~diff_mask))
  8180. {
  8181. (*link_info->callbacks->reloc_dangerous)
  8182. (link_info, _("overflow after relaxation"),
  8183. abfd, sec, old_source_offset);
  8184. goto error_return;
  8185. }
  8186. pin_contents (sec, contents);
  8187. }
  8188. /* If the relocation still references a section in the same
  8189. input file, modify the relocation directly instead of
  8190. adding a "fix" record. */
  8191. if (target_sec->owner == abfd)
  8192. {
  8193. unsigned r_symndx = ELF32_R_SYM (new_reloc.rela.r_info);
  8194. irel->r_info = ELF32_R_INFO (r_symndx, r_type);
  8195. irel->r_addend = new_reloc.rela.r_addend;
  8196. pin_internal_relocs (sec, internal_relocs);
  8197. }
  8198. else
  8199. {
  8200. bfd_vma addend_displacement;
  8201. reloc_bfd_fix *fix;
  8202. addend_displacement =
  8203. new_reloc.target_offset + new_reloc.virtual_offset;
  8204. fix = reloc_bfd_fix_init (sec, source_offset, r_type,
  8205. target_sec,
  8206. addend_displacement, true);
  8207. add_fix (sec, fix);
  8208. }
  8209. }
  8210. }
  8211. }
  8212. if ((relax_info->is_relaxable_literal_section
  8213. || relax_info->is_relaxable_asm_section)
  8214. && action_list_count (&relax_info->action_list))
  8215. {
  8216. /* Walk through the planned actions and build up a table
  8217. of move, copy and fill records. Use the move, copy and
  8218. fill records to perform the actions once. */
  8219. bfd_size_type final_size, copy_size, orig_insn_size;
  8220. bfd_byte *scratch = NULL;
  8221. bfd_byte *dup_contents = NULL;
  8222. bfd_size_type orig_size = sec->size;
  8223. bfd_vma orig_dot = 0;
  8224. bfd_vma orig_dot_copied = 0; /* Byte copied already from
  8225. orig dot in physical memory. */
  8226. bfd_vma orig_dot_vo = 0; /* Virtual offset from orig_dot. */
  8227. bfd_vma dup_dot = 0;
  8228. text_action *action;
  8229. final_size = sec->size;
  8230. splay_tree_foreach (relax_info->action_list.tree,
  8231. action_remove_bytes_fn, &final_size);
  8232. scratch = (bfd_byte *) bfd_zmalloc (final_size);
  8233. dup_contents = (bfd_byte *) bfd_zmalloc (final_size);
  8234. /* The dot is the current fill location. */
  8235. #if DEBUG
  8236. print_action_list (stderr, &relax_info->action_list);
  8237. #endif
  8238. for (action = action_first (&relax_info->action_list); action;
  8239. action = action_next (&relax_info->action_list, action))
  8240. {
  8241. virtual_action = false;
  8242. if (action->offset > orig_dot)
  8243. {
  8244. orig_dot += orig_dot_copied;
  8245. orig_dot_copied = 0;
  8246. orig_dot_vo = 0;
  8247. /* Out of the virtual world. */
  8248. }
  8249. if (action->offset > orig_dot)
  8250. {
  8251. copy_size = action->offset - orig_dot;
  8252. memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
  8253. orig_dot += copy_size;
  8254. dup_dot += copy_size;
  8255. BFD_ASSERT (action->offset == orig_dot);
  8256. }
  8257. else if (action->offset < orig_dot)
  8258. {
  8259. if (action->action == ta_fill
  8260. && action->offset - action->removed_bytes == orig_dot)
  8261. {
  8262. /* This is OK because the fill only effects the dup_dot. */
  8263. }
  8264. else if (action->action == ta_add_literal)
  8265. {
  8266. /* TBD. Might need to handle this. */
  8267. }
  8268. }
  8269. if (action->offset == orig_dot)
  8270. {
  8271. if (action->virtual_offset > orig_dot_vo)
  8272. {
  8273. if (orig_dot_vo == 0)
  8274. {
  8275. /* Need to copy virtual_offset bytes. Probably four. */
  8276. copy_size = action->virtual_offset - orig_dot_vo;
  8277. memmove (&dup_contents[dup_dot],
  8278. &contents[orig_dot], copy_size);
  8279. orig_dot_copied = copy_size;
  8280. dup_dot += copy_size;
  8281. }
  8282. virtual_action = true;
  8283. }
  8284. else
  8285. BFD_ASSERT (action->virtual_offset <= orig_dot_vo);
  8286. }
  8287. switch (action->action)
  8288. {
  8289. case ta_remove_literal:
  8290. case ta_remove_insn:
  8291. BFD_ASSERT (action->removed_bytes >= 0);
  8292. orig_dot += action->removed_bytes;
  8293. break;
  8294. case ta_narrow_insn:
  8295. orig_insn_size = 3;
  8296. copy_size = 2;
  8297. memmove (scratch, &contents[orig_dot], orig_insn_size);
  8298. BFD_ASSERT (action->removed_bytes == 1);
  8299. rv = narrow_instruction (scratch, final_size, 0);
  8300. BFD_ASSERT (rv);
  8301. memmove (&dup_contents[dup_dot], scratch, copy_size);
  8302. orig_dot += orig_insn_size;
  8303. dup_dot += copy_size;
  8304. break;
  8305. case ta_fill:
  8306. if (action->removed_bytes >= 0)
  8307. orig_dot += action->removed_bytes;
  8308. else
  8309. {
  8310. /* Already zeroed in dup_contents. Just bump the
  8311. counters. */
  8312. dup_dot += (-action->removed_bytes);
  8313. }
  8314. break;
  8315. case ta_none:
  8316. BFD_ASSERT (action->removed_bytes == 0);
  8317. break;
  8318. case ta_convert_longcall:
  8319. case ta_remove_longcall:
  8320. /* These will be removed or converted before we get here. */
  8321. BFD_ASSERT (0);
  8322. break;
  8323. case ta_widen_insn:
  8324. orig_insn_size = 2;
  8325. copy_size = 3;
  8326. memmove (scratch, &contents[orig_dot], orig_insn_size);
  8327. BFD_ASSERT (action->removed_bytes == -1);
  8328. rv = widen_instruction (scratch, final_size, 0);
  8329. BFD_ASSERT (rv);
  8330. memmove (&dup_contents[dup_dot], scratch, copy_size);
  8331. orig_dot += orig_insn_size;
  8332. dup_dot += copy_size;
  8333. break;
  8334. case ta_add_literal:
  8335. orig_insn_size = 0;
  8336. copy_size = 4;
  8337. BFD_ASSERT (action->removed_bytes == -4);
  8338. /* TBD -- place the literal value here and insert
  8339. into the table. */
  8340. memset (&dup_contents[dup_dot], 0, 4);
  8341. pin_internal_relocs (sec, internal_relocs);
  8342. pin_contents (sec, contents);
  8343. if (!move_literal (abfd, link_info, sec, dup_dot, dup_contents,
  8344. relax_info, &internal_relocs, &action->value))
  8345. goto error_return;
  8346. if (virtual_action)
  8347. orig_dot_vo += copy_size;
  8348. orig_dot += orig_insn_size;
  8349. dup_dot += copy_size;
  8350. break;
  8351. default:
  8352. /* Not implemented yet. */
  8353. BFD_ASSERT (0);
  8354. break;
  8355. }
  8356. BFD_ASSERT (dup_dot <= final_size);
  8357. BFD_ASSERT (orig_dot <= orig_size);
  8358. }
  8359. orig_dot += orig_dot_copied;
  8360. orig_dot_copied = 0;
  8361. if (orig_dot != orig_size)
  8362. {
  8363. copy_size = orig_size - orig_dot;
  8364. BFD_ASSERT (orig_size > orig_dot);
  8365. BFD_ASSERT (dup_dot + copy_size == final_size);
  8366. memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
  8367. orig_dot += copy_size;
  8368. dup_dot += copy_size;
  8369. }
  8370. BFD_ASSERT (orig_size == orig_dot);
  8371. BFD_ASSERT (final_size == dup_dot);
  8372. /* Move the dup_contents back. */
  8373. if (final_size > orig_size)
  8374. {
  8375. /* Contents need to be reallocated. Swap the dup_contents into
  8376. contents. */
  8377. sec->contents = dup_contents;
  8378. free (contents);
  8379. contents = dup_contents;
  8380. pin_contents (sec, contents);
  8381. }
  8382. else
  8383. {
  8384. BFD_ASSERT (final_size <= orig_size);
  8385. memset (contents, 0, orig_size);
  8386. memcpy (contents, dup_contents, final_size);
  8387. free (dup_contents);
  8388. }
  8389. free (scratch);
  8390. pin_contents (sec, contents);
  8391. if (sec->rawsize == 0)
  8392. sec->rawsize = sec->size;
  8393. sec->size = final_size;
  8394. }
  8395. error_return:
  8396. release_internal_relocs (sec, internal_relocs);
  8397. release_contents (sec, contents);
  8398. return ok;
  8399. }
  8400. static bool
  8401. translate_section_fixes (asection *sec)
  8402. {
  8403. xtensa_relax_info *relax_info;
  8404. reloc_bfd_fix *r;
  8405. relax_info = get_xtensa_relax_info (sec);
  8406. if (!relax_info)
  8407. return true;
  8408. for (r = relax_info->fix_list; r != NULL; r = r->next)
  8409. if (!translate_reloc_bfd_fix (r))
  8410. return false;
  8411. return true;
  8412. }
  8413. /* Translate a fix given the mapping in the relax info for the target
  8414. section. If it has already been translated, no work is required. */
  8415. static bool
  8416. translate_reloc_bfd_fix (reloc_bfd_fix *fix)
  8417. {
  8418. reloc_bfd_fix new_fix;
  8419. asection *sec;
  8420. xtensa_relax_info *relax_info;
  8421. removed_literal *removed;
  8422. bfd_vma new_offset, target_offset;
  8423. if (fix->translated)
  8424. return true;
  8425. sec = fix->target_sec;
  8426. target_offset = fix->target_offset;
  8427. relax_info = get_xtensa_relax_info (sec);
  8428. if (!relax_info)
  8429. {
  8430. fix->translated = true;
  8431. return true;
  8432. }
  8433. new_fix = *fix;
  8434. /* The fix does not need to be translated if the section cannot change. */
  8435. if (!relax_info->is_relaxable_literal_section
  8436. && !relax_info->is_relaxable_asm_section)
  8437. {
  8438. fix->translated = true;
  8439. return true;
  8440. }
  8441. /* If the literal has been moved and this relocation was on an
  8442. opcode, then the relocation should move to the new literal
  8443. location. Otherwise, the relocation should move within the
  8444. section. */
  8445. removed = false;
  8446. if (is_operand_relocation (fix->src_type))
  8447. {
  8448. /* Check if the original relocation is against a literal being
  8449. removed. */
  8450. removed = find_removed_literal (&relax_info->removed_list,
  8451. target_offset);
  8452. }
  8453. if (removed)
  8454. {
  8455. asection *new_sec;
  8456. /* The fact that there is still a relocation to this literal indicates
  8457. that the literal is being coalesced, not simply removed. */
  8458. BFD_ASSERT (removed->to.abfd != NULL);
  8459. /* This was moved to some other address (possibly another section). */
  8460. new_sec = r_reloc_get_section (&removed->to);
  8461. if (new_sec != sec)
  8462. {
  8463. sec = new_sec;
  8464. relax_info = get_xtensa_relax_info (sec);
  8465. if (!relax_info ||
  8466. (!relax_info->is_relaxable_literal_section
  8467. && !relax_info->is_relaxable_asm_section))
  8468. {
  8469. target_offset = removed->to.target_offset;
  8470. new_fix.target_sec = new_sec;
  8471. new_fix.target_offset = target_offset;
  8472. new_fix.translated = true;
  8473. *fix = new_fix;
  8474. return true;
  8475. }
  8476. }
  8477. target_offset = removed->to.target_offset;
  8478. new_fix.target_sec = new_sec;
  8479. }
  8480. /* The target address may have been moved within its section. */
  8481. new_offset = offset_with_removed_text (&relax_info->action_list,
  8482. target_offset);
  8483. new_fix.target_offset = new_offset;
  8484. new_fix.target_offset = new_offset;
  8485. new_fix.translated = true;
  8486. *fix = new_fix;
  8487. return true;
  8488. }
  8489. /* Fix up a relocation to take account of removed literals. */
  8490. static asection *
  8491. translate_reloc (const r_reloc *orig_rel, r_reloc *new_rel, asection *sec)
  8492. {
  8493. xtensa_relax_info *relax_info;
  8494. removed_literal *removed;
  8495. bfd_vma target_offset, base_offset;
  8496. *new_rel = *orig_rel;
  8497. if (!r_reloc_is_defined (orig_rel))
  8498. return sec ;
  8499. relax_info = get_xtensa_relax_info (sec);
  8500. BFD_ASSERT (relax_info && (relax_info->is_relaxable_literal_section
  8501. || relax_info->is_relaxable_asm_section));
  8502. target_offset = orig_rel->target_offset;
  8503. removed = false;
  8504. if (is_operand_relocation (ELF32_R_TYPE (orig_rel->rela.r_info)))
  8505. {
  8506. /* Check if the original relocation is against a literal being
  8507. removed. */
  8508. removed = find_removed_literal (&relax_info->removed_list,
  8509. target_offset);
  8510. }
  8511. if (removed && removed->to.abfd)
  8512. {
  8513. asection *new_sec;
  8514. /* The fact that there is still a relocation to this literal indicates
  8515. that the literal is being coalesced, not simply removed. */
  8516. BFD_ASSERT (removed->to.abfd != NULL);
  8517. /* This was moved to some other address
  8518. (possibly in another section). */
  8519. *new_rel = removed->to;
  8520. new_sec = r_reloc_get_section (new_rel);
  8521. if (new_sec != sec)
  8522. {
  8523. sec = new_sec;
  8524. relax_info = get_xtensa_relax_info (sec);
  8525. if (!relax_info
  8526. || (!relax_info->is_relaxable_literal_section
  8527. && !relax_info->is_relaxable_asm_section))
  8528. return sec;
  8529. }
  8530. target_offset = new_rel->target_offset;
  8531. }
  8532. /* Find the base offset of the reloc symbol, excluding any addend from the
  8533. reloc or from the section contents (for a partial_inplace reloc). Then
  8534. find the adjusted values of the offsets due to relaxation. The base
  8535. offset is needed to determine the change to the reloc's addend; the reloc
  8536. addend should not be adjusted due to relaxations located before the base
  8537. offset. */
  8538. base_offset = r_reloc_get_target_offset (new_rel) - new_rel->rela.r_addend;
  8539. if (base_offset <= target_offset)
  8540. {
  8541. int base_removed = removed_by_actions_map (&relax_info->action_list,
  8542. base_offset, false);
  8543. int addend_removed = removed_by_actions_map (&relax_info->action_list,
  8544. target_offset, false) -
  8545. base_removed;
  8546. new_rel->target_offset = target_offset - base_removed - addend_removed;
  8547. new_rel->rela.r_addend -= addend_removed;
  8548. }
  8549. else
  8550. {
  8551. /* Handle a negative addend. The base offset comes first. */
  8552. int tgt_removed = removed_by_actions_map (&relax_info->action_list,
  8553. target_offset, false);
  8554. int addend_removed = removed_by_actions_map (&relax_info->action_list,
  8555. base_offset, false) -
  8556. tgt_removed;
  8557. new_rel->target_offset = target_offset - tgt_removed;
  8558. new_rel->rela.r_addend += addend_removed;
  8559. }
  8560. return sec;
  8561. }
  8562. /* For dynamic links, there may be a dynamic relocation for each
  8563. literal. The number of dynamic relocations must be computed in
  8564. size_dynamic_sections, which occurs before relaxation. When a
  8565. literal is removed, this function checks if there is a corresponding
  8566. dynamic relocation and shrinks the size of the appropriate dynamic
  8567. relocation section accordingly. At this point, the contents of the
  8568. dynamic relocation sections have not yet been filled in, so there's
  8569. nothing else that needs to be done. */
  8570. static void
  8571. shrink_dynamic_reloc_sections (struct bfd_link_info *info,
  8572. bfd *abfd,
  8573. asection *input_section,
  8574. Elf_Internal_Rela *rel)
  8575. {
  8576. struct elf_xtensa_link_hash_table *htab;
  8577. Elf_Internal_Shdr *symtab_hdr;
  8578. struct elf_link_hash_entry **sym_hashes;
  8579. unsigned long r_symndx;
  8580. int r_type;
  8581. struct elf_link_hash_entry *h;
  8582. bool dynamic_symbol;
  8583. htab = elf_xtensa_hash_table (info);
  8584. if (htab == NULL)
  8585. return;
  8586. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  8587. sym_hashes = elf_sym_hashes (abfd);
  8588. r_type = ELF32_R_TYPE (rel->r_info);
  8589. r_symndx = ELF32_R_SYM (rel->r_info);
  8590. if (r_symndx < symtab_hdr->sh_info)
  8591. h = NULL;
  8592. else
  8593. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  8594. dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
  8595. if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
  8596. && (input_section->flags & SEC_ALLOC) != 0
  8597. && (dynamic_symbol
  8598. || (bfd_link_pic (info)
  8599. && (!h || h->root.type != bfd_link_hash_undefweak))))
  8600. {
  8601. asection *srel;
  8602. bool is_plt = false;
  8603. if (dynamic_symbol && r_type == R_XTENSA_PLT)
  8604. {
  8605. srel = htab->elf.srelplt;
  8606. is_plt = true;
  8607. }
  8608. else
  8609. srel = htab->elf.srelgot;
  8610. /* Reduce size of the .rela.* section by one reloc. */
  8611. BFD_ASSERT (srel != NULL);
  8612. BFD_ASSERT (srel->size >= sizeof (Elf32_External_Rela));
  8613. srel->size -= sizeof (Elf32_External_Rela);
  8614. if (is_plt)
  8615. {
  8616. asection *splt, *sgotplt, *srelgot;
  8617. int reloc_index, chunk;
  8618. /* Find the PLT reloc index of the entry being removed. This
  8619. is computed from the size of ".rela.plt". It is needed to
  8620. figure out which PLT chunk to resize. Usually "last index
  8621. = size - 1" since the index starts at zero, but in this
  8622. context, the size has just been decremented so there's no
  8623. need to subtract one. */
  8624. reloc_index = srel->size / sizeof (Elf32_External_Rela);
  8625. chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
  8626. splt = elf_xtensa_get_plt_section (info, chunk);
  8627. sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
  8628. BFD_ASSERT (splt != NULL && sgotplt != NULL);
  8629. /* Check if an entire PLT chunk has just been eliminated. */
  8630. if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
  8631. {
  8632. /* The two magic GOT entries for that chunk can go away. */
  8633. srelgot = htab->elf.srelgot;
  8634. BFD_ASSERT (srelgot != NULL);
  8635. srelgot->reloc_count -= 2;
  8636. srelgot->size -= 2 * sizeof (Elf32_External_Rela);
  8637. sgotplt->size -= 8;
  8638. /* There should be only one entry left (and it will be
  8639. removed below). */
  8640. BFD_ASSERT (sgotplt->size == 4);
  8641. BFD_ASSERT (splt->size == PLT_ENTRY_SIZE);
  8642. }
  8643. BFD_ASSERT (sgotplt->size >= 4);
  8644. BFD_ASSERT (splt->size >= PLT_ENTRY_SIZE);
  8645. sgotplt->size -= 4;
  8646. splt->size -= PLT_ENTRY_SIZE;
  8647. }
  8648. }
  8649. }
  8650. /* Take an r_rel and move it to another section. This usually
  8651. requires extending the interal_relocation array and pinning it. If
  8652. the original r_rel is from the same BFD, we can complete this here.
  8653. Otherwise, we add a fix record to let the final link fix the
  8654. appropriate address. Contents and internal relocations for the
  8655. section must be pinned after calling this routine. */
  8656. static bool
  8657. move_literal (bfd *abfd,
  8658. struct bfd_link_info *link_info,
  8659. asection *sec,
  8660. bfd_vma offset,
  8661. bfd_byte *contents,
  8662. xtensa_relax_info *relax_info,
  8663. Elf_Internal_Rela **internal_relocs_p,
  8664. const literal_value *lit)
  8665. {
  8666. Elf_Internal_Rela *new_relocs = NULL;
  8667. size_t new_relocs_count = 0;
  8668. Elf_Internal_Rela this_rela;
  8669. const r_reloc *r_rel;
  8670. r_rel = &lit->r_rel;
  8671. BFD_ASSERT (elf_section_data (sec)->relocs == *internal_relocs_p);
  8672. if (r_reloc_is_const (r_rel))
  8673. bfd_put_32 (abfd, lit->value, contents + offset);
  8674. else
  8675. {
  8676. int r_type;
  8677. unsigned i;
  8678. reloc_bfd_fix *fix;
  8679. unsigned insert_at;
  8680. r_type = ELF32_R_TYPE (r_rel->rela.r_info);
  8681. /* This is the difficult case. We have to create a fix up. */
  8682. this_rela.r_offset = offset;
  8683. this_rela.r_info = ELF32_R_INFO (0, r_type);
  8684. this_rela.r_addend =
  8685. r_rel->target_offset - r_reloc_get_target_offset (r_rel);
  8686. bfd_put_32 (abfd, lit->value, contents + offset);
  8687. /* Currently, we cannot move relocations during a relocatable link. */
  8688. BFD_ASSERT (!bfd_link_relocatable (link_info));
  8689. fix = reloc_bfd_fix_init (sec, offset, r_type,
  8690. r_reloc_get_section (r_rel),
  8691. r_rel->target_offset + r_rel->virtual_offset,
  8692. false);
  8693. /* We also need to mark that relocations are needed here. */
  8694. sec->flags |= SEC_RELOC;
  8695. translate_reloc_bfd_fix (fix);
  8696. /* This fix has not yet been translated. */
  8697. add_fix (sec, fix);
  8698. /* Add the relocation. If we have already allocated our own
  8699. space for the relocations and we have room for more, then use
  8700. it. Otherwise, allocate new space and move the literals. */
  8701. insert_at = sec->reloc_count;
  8702. for (i = 0; i < sec->reloc_count; ++i)
  8703. {
  8704. if (this_rela.r_offset < (*internal_relocs_p)[i].r_offset)
  8705. {
  8706. insert_at = i;
  8707. break;
  8708. }
  8709. }
  8710. if (*internal_relocs_p != relax_info->allocated_relocs
  8711. || sec->reloc_count + 1 > relax_info->allocated_relocs_count)
  8712. {
  8713. BFD_ASSERT (relax_info->allocated_relocs == NULL
  8714. || sec->reloc_count == relax_info->relocs_count);
  8715. if (relax_info->allocated_relocs_count == 0)
  8716. new_relocs_count = (sec->reloc_count + 2) * 2;
  8717. else
  8718. new_relocs_count = (relax_info->allocated_relocs_count + 2) * 2;
  8719. new_relocs = (Elf_Internal_Rela *)
  8720. bfd_zmalloc (sizeof (Elf_Internal_Rela) * (new_relocs_count));
  8721. if (!new_relocs)
  8722. return false;
  8723. /* We could handle this more quickly by finding the split point. */
  8724. if (insert_at != 0)
  8725. memcpy (new_relocs, *internal_relocs_p,
  8726. insert_at * sizeof (Elf_Internal_Rela));
  8727. new_relocs[insert_at] = this_rela;
  8728. if (insert_at != sec->reloc_count)
  8729. memcpy (new_relocs + insert_at + 1,
  8730. (*internal_relocs_p) + insert_at,
  8731. (sec->reloc_count - insert_at)
  8732. * sizeof (Elf_Internal_Rela));
  8733. if (*internal_relocs_p != relax_info->allocated_relocs)
  8734. {
  8735. /* The first time we re-allocate, we can only free the
  8736. old relocs if they were allocated with bfd_malloc.
  8737. This is not true when keep_memory is in effect. */
  8738. if (!link_info->keep_memory)
  8739. free (*internal_relocs_p);
  8740. }
  8741. else
  8742. free (*internal_relocs_p);
  8743. relax_info->allocated_relocs = new_relocs;
  8744. relax_info->allocated_relocs_count = new_relocs_count;
  8745. elf_section_data (sec)->relocs = new_relocs;
  8746. sec->reloc_count++;
  8747. relax_info->relocs_count = sec->reloc_count;
  8748. *internal_relocs_p = new_relocs;
  8749. }
  8750. else
  8751. {
  8752. if (insert_at != sec->reloc_count)
  8753. {
  8754. unsigned idx;
  8755. for (idx = sec->reloc_count; idx > insert_at; idx--)
  8756. (*internal_relocs_p)[idx] = (*internal_relocs_p)[idx-1];
  8757. }
  8758. (*internal_relocs_p)[insert_at] = this_rela;
  8759. sec->reloc_count++;
  8760. if (relax_info->allocated_relocs)
  8761. relax_info->relocs_count = sec->reloc_count;
  8762. }
  8763. }
  8764. return true;
  8765. }
  8766. /* This is similar to relax_section except that when a target is moved,
  8767. we shift addresses up. We also need to modify the size. This
  8768. algorithm does NOT allow for relocations into the middle of the
  8769. property sections. */
  8770. static bool
  8771. relax_property_section (bfd *abfd,
  8772. asection *sec,
  8773. struct bfd_link_info *link_info)
  8774. {
  8775. Elf_Internal_Rela *internal_relocs;
  8776. bfd_byte *contents;
  8777. unsigned i;
  8778. bool ok = true;
  8779. bool is_full_prop_section;
  8780. size_t last_zfill_target_offset = 0;
  8781. asection *last_zfill_target_sec = NULL;
  8782. bfd_size_type sec_size;
  8783. bfd_size_type entry_size;
  8784. sec_size = bfd_get_section_limit (abfd, sec);
  8785. internal_relocs = retrieve_internal_relocs (abfd, sec,
  8786. link_info->keep_memory);
  8787. contents = retrieve_contents (abfd, sec, link_info->keep_memory);
  8788. if (contents == NULL && sec_size != 0)
  8789. {
  8790. ok = false;
  8791. goto error_return;
  8792. }
  8793. is_full_prop_section = xtensa_is_proptable_section (sec);
  8794. if (is_full_prop_section)
  8795. entry_size = 12;
  8796. else
  8797. entry_size = 8;
  8798. if (internal_relocs)
  8799. {
  8800. for (i = 0; i < sec->reloc_count; i++)
  8801. {
  8802. Elf_Internal_Rela *irel;
  8803. xtensa_relax_info *target_relax_info;
  8804. unsigned r_type;
  8805. asection *target_sec;
  8806. literal_value val;
  8807. bfd_byte *size_p, *flags_p;
  8808. /* Locally change the source address.
  8809. Translate the target to the new target address.
  8810. If it points to this section and has been removed, MOVE IT.
  8811. Also, don't forget to modify the associated SIZE at
  8812. (offset + 4). */
  8813. irel = &internal_relocs[i];
  8814. r_type = ELF32_R_TYPE (irel->r_info);
  8815. if (r_type == R_XTENSA_NONE)
  8816. continue;
  8817. /* Find the literal value. */
  8818. r_reloc_init (&val.r_rel, abfd, irel, contents, sec_size);
  8819. size_p = &contents[irel->r_offset + 4];
  8820. flags_p = NULL;
  8821. if (is_full_prop_section)
  8822. flags_p = &contents[irel->r_offset + 8];
  8823. BFD_ASSERT (irel->r_offset + entry_size <= sec_size);
  8824. target_sec = r_reloc_get_section (&val.r_rel);
  8825. target_relax_info = get_xtensa_relax_info (target_sec);
  8826. if (target_relax_info
  8827. && (target_relax_info->is_relaxable_literal_section
  8828. || target_relax_info->is_relaxable_asm_section ))
  8829. {
  8830. /* Translate the relocation's destination. */
  8831. bfd_vma old_offset = val.r_rel.target_offset;
  8832. bfd_vma new_offset;
  8833. long old_size, new_size;
  8834. int removed_by_old_offset =
  8835. removed_by_actions_map (&target_relax_info->action_list,
  8836. old_offset, false);
  8837. new_offset = old_offset - removed_by_old_offset;
  8838. /* Assert that we are not out of bounds. */
  8839. old_size = bfd_get_32 (abfd, size_p);
  8840. new_size = old_size;
  8841. if (old_size == 0)
  8842. {
  8843. /* Only the first zero-sized unreachable entry is
  8844. allowed to expand. In this case the new offset
  8845. should be the offset before the fill and the new
  8846. size is the expansion size. For other zero-sized
  8847. entries the resulting size should be zero with an
  8848. offset before or after the fill address depending
  8849. on whether the expanding unreachable entry
  8850. preceeds it. */
  8851. if (last_zfill_target_sec == 0
  8852. || last_zfill_target_sec != target_sec
  8853. || last_zfill_target_offset != old_offset)
  8854. {
  8855. bfd_vma new_end_offset = new_offset;
  8856. /* Recompute the new_offset, but this time don't
  8857. include any fill inserted by relaxation. */
  8858. removed_by_old_offset =
  8859. removed_by_actions_map (&target_relax_info->action_list,
  8860. old_offset, true);
  8861. new_offset = old_offset - removed_by_old_offset;
  8862. /* If it is not unreachable and we have not yet
  8863. seen an unreachable at this address, place it
  8864. before the fill address. */
  8865. if (flags_p && (bfd_get_32 (abfd, flags_p)
  8866. & XTENSA_PROP_UNREACHABLE) != 0)
  8867. {
  8868. new_size = new_end_offset - new_offset;
  8869. last_zfill_target_sec = target_sec;
  8870. last_zfill_target_offset = old_offset;
  8871. }
  8872. }
  8873. }
  8874. else
  8875. {
  8876. int removed_by_old_offset_size =
  8877. removed_by_actions_map (&target_relax_info->action_list,
  8878. old_offset + old_size, true);
  8879. new_size -= removed_by_old_offset_size - removed_by_old_offset;
  8880. }
  8881. if (new_size != old_size)
  8882. {
  8883. bfd_put_32 (abfd, new_size, size_p);
  8884. pin_contents (sec, contents);
  8885. }
  8886. if (new_offset != old_offset)
  8887. {
  8888. bfd_vma diff = new_offset - old_offset;
  8889. irel->r_addend += diff;
  8890. pin_internal_relocs (sec, internal_relocs);
  8891. }
  8892. }
  8893. }
  8894. }
  8895. /* Combine adjacent property table entries. This is also done in
  8896. finish_dynamic_sections() but at that point it's too late to
  8897. reclaim the space in the output section, so we do this twice. */
  8898. if (internal_relocs && (!bfd_link_relocatable (link_info)
  8899. || xtensa_is_littable_section (sec)))
  8900. {
  8901. Elf_Internal_Rela *last_irel = NULL;
  8902. Elf_Internal_Rela *irel, *next_rel, *rel_end;
  8903. int removed_bytes = 0;
  8904. bfd_vma offset;
  8905. flagword predef_flags;
  8906. predef_flags = xtensa_get_property_predef_flags (sec);
  8907. /* Walk over memory and relocations at the same time.
  8908. This REQUIRES that the internal_relocs be sorted by offset. */
  8909. qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
  8910. internal_reloc_compare);
  8911. pin_internal_relocs (sec, internal_relocs);
  8912. pin_contents (sec, contents);
  8913. next_rel = internal_relocs;
  8914. rel_end = internal_relocs + sec->reloc_count;
  8915. BFD_ASSERT (sec->size % entry_size == 0);
  8916. for (offset = 0; offset < sec->size; offset += entry_size)
  8917. {
  8918. Elf_Internal_Rela *offset_rel, *extra_rel;
  8919. bfd_vma bytes_to_remove, size, actual_offset;
  8920. bool remove_this_rel;
  8921. flagword flags;
  8922. /* Find the first relocation for the entry at the current offset.
  8923. Adjust the offsets of any extra relocations for the previous
  8924. entry. */
  8925. offset_rel = NULL;
  8926. if (next_rel)
  8927. {
  8928. for (irel = next_rel; irel < rel_end; irel++)
  8929. {
  8930. if ((irel->r_offset == offset
  8931. && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
  8932. || irel->r_offset > offset)
  8933. {
  8934. offset_rel = irel;
  8935. break;
  8936. }
  8937. irel->r_offset -= removed_bytes;
  8938. }
  8939. }
  8940. /* Find the next relocation (if there are any left). */
  8941. extra_rel = NULL;
  8942. if (offset_rel)
  8943. {
  8944. for (irel = offset_rel + 1; irel < rel_end; irel++)
  8945. {
  8946. if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
  8947. {
  8948. extra_rel = irel;
  8949. break;
  8950. }
  8951. }
  8952. }
  8953. /* Check if there are relocations on the current entry. There
  8954. should usually be a relocation on the offset field. If there
  8955. are relocations on the size or flags, then we can't optimize
  8956. this entry. Also, find the next relocation to examine on the
  8957. next iteration. */
  8958. if (offset_rel)
  8959. {
  8960. if (offset_rel->r_offset >= offset + entry_size)
  8961. {
  8962. next_rel = offset_rel;
  8963. /* There are no relocations on the current entry, but we
  8964. might still be able to remove it if the size is zero. */
  8965. offset_rel = NULL;
  8966. }
  8967. else if (offset_rel->r_offset > offset
  8968. || (extra_rel
  8969. && extra_rel->r_offset < offset + entry_size))
  8970. {
  8971. /* There is a relocation on the size or flags, so we can't
  8972. do anything with this entry. Continue with the next. */
  8973. next_rel = offset_rel;
  8974. continue;
  8975. }
  8976. else
  8977. {
  8978. BFD_ASSERT (offset_rel->r_offset == offset);
  8979. offset_rel->r_offset -= removed_bytes;
  8980. next_rel = offset_rel + 1;
  8981. }
  8982. }
  8983. else
  8984. next_rel = NULL;
  8985. remove_this_rel = false;
  8986. bytes_to_remove = 0;
  8987. actual_offset = offset - removed_bytes;
  8988. size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
  8989. if (is_full_prop_section)
  8990. flags = bfd_get_32 (abfd, &contents[actual_offset + 8]);
  8991. else
  8992. flags = predef_flags;
  8993. if (size == 0
  8994. && (flags & XTENSA_PROP_ALIGN) == 0
  8995. && (flags & XTENSA_PROP_UNREACHABLE) == 0)
  8996. {
  8997. /* Always remove entries with zero size and no alignment. */
  8998. bytes_to_remove = entry_size;
  8999. if (offset_rel)
  9000. remove_this_rel = true;
  9001. }
  9002. else if (offset_rel
  9003. && ELF32_R_TYPE (offset_rel->r_info) == R_XTENSA_32)
  9004. {
  9005. if (last_irel)
  9006. {
  9007. flagword old_flags;
  9008. bfd_vma old_size =
  9009. bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
  9010. bfd_vma old_address =
  9011. (last_irel->r_addend
  9012. + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
  9013. bfd_vma new_address =
  9014. (offset_rel->r_addend
  9015. + bfd_get_32 (abfd, &contents[actual_offset]));
  9016. if (is_full_prop_section)
  9017. old_flags = bfd_get_32
  9018. (abfd, &contents[last_irel->r_offset + 8]);
  9019. else
  9020. old_flags = predef_flags;
  9021. if ((ELF32_R_SYM (offset_rel->r_info)
  9022. == ELF32_R_SYM (last_irel->r_info))
  9023. && old_address + old_size == new_address
  9024. && old_flags == flags
  9025. && (old_flags & XTENSA_PROP_INSN_BRANCH_TARGET) == 0
  9026. && (old_flags & XTENSA_PROP_INSN_LOOP_TARGET) == 0)
  9027. {
  9028. /* Fix the old size. */
  9029. bfd_put_32 (abfd, old_size + size,
  9030. &contents[last_irel->r_offset + 4]);
  9031. bytes_to_remove = entry_size;
  9032. remove_this_rel = true;
  9033. }
  9034. else
  9035. last_irel = offset_rel;
  9036. }
  9037. else
  9038. last_irel = offset_rel;
  9039. }
  9040. if (remove_this_rel)
  9041. {
  9042. offset_rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
  9043. offset_rel->r_offset = 0;
  9044. }
  9045. if (bytes_to_remove != 0)
  9046. {
  9047. removed_bytes += bytes_to_remove;
  9048. if (offset + bytes_to_remove < sec->size)
  9049. memmove (&contents[actual_offset],
  9050. &contents[actual_offset + bytes_to_remove],
  9051. sec->size - offset - bytes_to_remove);
  9052. }
  9053. }
  9054. if (removed_bytes)
  9055. {
  9056. /* Fix up any extra relocations on the last entry. */
  9057. for (irel = next_rel; irel < rel_end; irel++)
  9058. irel->r_offset -= removed_bytes;
  9059. /* Clear the removed bytes. */
  9060. memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
  9061. if (sec->rawsize == 0)
  9062. sec->rawsize = sec->size;
  9063. sec->size -= removed_bytes;
  9064. if (xtensa_is_littable_section (sec))
  9065. {
  9066. asection *sgotloc = elf_xtensa_hash_table (link_info)->sgotloc;
  9067. if (sgotloc)
  9068. sgotloc->size -= removed_bytes;
  9069. }
  9070. }
  9071. }
  9072. error_return:
  9073. release_internal_relocs (sec, internal_relocs);
  9074. release_contents (sec, contents);
  9075. return ok;
  9076. }
  9077. /* Third relaxation pass. */
  9078. /* Change symbol values to account for removed literals. */
  9079. bool
  9080. relax_section_symbols (bfd *abfd, asection *sec)
  9081. {
  9082. xtensa_relax_info *relax_info;
  9083. unsigned int sec_shndx;
  9084. Elf_Internal_Shdr *symtab_hdr;
  9085. Elf_Internal_Sym *isymbuf;
  9086. unsigned i, num_syms, num_locals;
  9087. relax_info = get_xtensa_relax_info (sec);
  9088. BFD_ASSERT (relax_info);
  9089. if (!relax_info->is_relaxable_literal_section
  9090. && !relax_info->is_relaxable_asm_section)
  9091. return true;
  9092. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  9093. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  9094. isymbuf = retrieve_local_syms (abfd);
  9095. num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
  9096. num_locals = symtab_hdr->sh_info;
  9097. /* Adjust the local symbols defined in this section. */
  9098. for (i = 0; i < num_locals; i++)
  9099. {
  9100. Elf_Internal_Sym *isym = &isymbuf[i];
  9101. if (isym->st_shndx == sec_shndx)
  9102. {
  9103. bfd_vma orig_addr = isym->st_value;
  9104. int removed = removed_by_actions_map (&relax_info->action_list,
  9105. orig_addr, false);
  9106. isym->st_value -= removed;
  9107. if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC)
  9108. isym->st_size -=
  9109. removed_by_actions_map (&relax_info->action_list,
  9110. orig_addr + isym->st_size, false) -
  9111. removed;
  9112. }
  9113. }
  9114. /* Now adjust the global symbols defined in this section. */
  9115. for (i = 0; i < (num_syms - num_locals); i++)
  9116. {
  9117. struct elf_link_hash_entry *sym_hash;
  9118. sym_hash = elf_sym_hashes (abfd)[i];
  9119. if (sym_hash->root.type == bfd_link_hash_warning)
  9120. sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
  9121. if ((sym_hash->root.type == bfd_link_hash_defined
  9122. || sym_hash->root.type == bfd_link_hash_defweak)
  9123. && sym_hash->root.u.def.section == sec)
  9124. {
  9125. bfd_vma orig_addr = sym_hash->root.u.def.value;
  9126. int removed = removed_by_actions_map (&relax_info->action_list,
  9127. orig_addr, false);
  9128. sym_hash->root.u.def.value -= removed;
  9129. if (sym_hash->type == STT_FUNC)
  9130. sym_hash->size -=
  9131. removed_by_actions_map (&relax_info->action_list,
  9132. orig_addr + sym_hash->size, false) -
  9133. removed;
  9134. }
  9135. }
  9136. return true;
  9137. }
  9138. /* "Fix" handling functions, called while performing relocations. */
  9139. static bool
  9140. do_fix_for_relocatable_link (Elf_Internal_Rela *rel,
  9141. bfd *input_bfd,
  9142. asection *input_section,
  9143. bfd_byte *contents)
  9144. {
  9145. r_reloc r_rel;
  9146. asection *sec, *old_sec;
  9147. bfd_vma old_offset;
  9148. int r_type = ELF32_R_TYPE (rel->r_info);
  9149. reloc_bfd_fix *fix;
  9150. if (r_type == R_XTENSA_NONE)
  9151. return true;
  9152. fix = get_bfd_fix (input_section, rel->r_offset, r_type);
  9153. if (!fix)
  9154. return true;
  9155. r_reloc_init (&r_rel, input_bfd, rel, contents,
  9156. bfd_get_section_limit (input_bfd, input_section));
  9157. old_sec = r_reloc_get_section (&r_rel);
  9158. old_offset = r_rel.target_offset;
  9159. if (!old_sec || !r_reloc_is_defined (&r_rel))
  9160. {
  9161. if (r_type != R_XTENSA_ASM_EXPAND)
  9162. {
  9163. _bfd_error_handler
  9164. /* xgettext:c-format */
  9165. (_("%pB(%pA+%#" PRIx64 "): unexpected fix for %s relocation"),
  9166. input_bfd, input_section, (uint64_t) rel->r_offset,
  9167. elf_howto_table[r_type].name);
  9168. return false;
  9169. }
  9170. /* Leave it be. Resolution will happen in a later stage. */
  9171. }
  9172. else
  9173. {
  9174. sec = fix->target_sec;
  9175. rel->r_addend += ((sec->output_offset + fix->target_offset)
  9176. - (old_sec->output_offset + old_offset));
  9177. }
  9178. return true;
  9179. }
  9180. static void
  9181. do_fix_for_final_link (Elf_Internal_Rela *rel,
  9182. bfd *input_bfd,
  9183. asection *input_section,
  9184. bfd_byte *contents,
  9185. bfd_vma *relocationp)
  9186. {
  9187. asection *sec;
  9188. int r_type = ELF32_R_TYPE (rel->r_info);
  9189. reloc_bfd_fix *fix;
  9190. bfd_vma fixup_diff;
  9191. if (r_type == R_XTENSA_NONE)
  9192. return;
  9193. fix = get_bfd_fix (input_section, rel->r_offset, r_type);
  9194. if (!fix)
  9195. return;
  9196. sec = fix->target_sec;
  9197. fixup_diff = rel->r_addend;
  9198. if (elf_howto_table[fix->src_type].partial_inplace)
  9199. {
  9200. bfd_vma inplace_val;
  9201. BFD_ASSERT (fix->src_offset
  9202. < bfd_get_section_limit (input_bfd, input_section));
  9203. inplace_val = bfd_get_32 (input_bfd, &contents[fix->src_offset]);
  9204. fixup_diff += inplace_val;
  9205. }
  9206. *relocationp = (sec->output_section->vma
  9207. + sec->output_offset
  9208. + fix->target_offset - fixup_diff);
  9209. }
  9210. /* Miscellaneous utility functions.... */
  9211. static asection *
  9212. elf_xtensa_get_plt_section (struct bfd_link_info *info, int chunk)
  9213. {
  9214. bfd *dynobj;
  9215. char plt_name[17];
  9216. if (chunk == 0)
  9217. return elf_hash_table (info)->splt;
  9218. dynobj = elf_hash_table (info)->dynobj;
  9219. sprintf (plt_name, ".plt.%u", chunk);
  9220. return bfd_get_linker_section (dynobj, plt_name);
  9221. }
  9222. static asection *
  9223. elf_xtensa_get_gotplt_section (struct bfd_link_info *info, int chunk)
  9224. {
  9225. bfd *dynobj;
  9226. char got_name[21];
  9227. if (chunk == 0)
  9228. return elf_hash_table (info)->sgotplt;
  9229. dynobj = elf_hash_table (info)->dynobj;
  9230. sprintf (got_name, ".got.plt.%u", chunk);
  9231. return bfd_get_linker_section (dynobj, got_name);
  9232. }
  9233. /* Get the input section for a given symbol index.
  9234. If the symbol is:
  9235. . a section symbol, return the section;
  9236. . a common symbol, return the common section;
  9237. . an undefined symbol, return the undefined section;
  9238. . an indirect symbol, follow the links;
  9239. . an absolute value, return the absolute section. */
  9240. static asection *
  9241. get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
  9242. {
  9243. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  9244. asection *target_sec = NULL;
  9245. if (r_symndx < symtab_hdr->sh_info)
  9246. {
  9247. Elf_Internal_Sym *isymbuf;
  9248. unsigned int section_index;
  9249. isymbuf = retrieve_local_syms (abfd);
  9250. section_index = isymbuf[r_symndx].st_shndx;
  9251. if (section_index == SHN_UNDEF)
  9252. target_sec = bfd_und_section_ptr;
  9253. else if (section_index == SHN_ABS)
  9254. target_sec = bfd_abs_section_ptr;
  9255. else if (section_index == SHN_COMMON)
  9256. target_sec = bfd_com_section_ptr;
  9257. else
  9258. target_sec = bfd_section_from_elf_index (abfd, section_index);
  9259. }
  9260. else
  9261. {
  9262. unsigned long indx = r_symndx - symtab_hdr->sh_info;
  9263. struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
  9264. while (h->root.type == bfd_link_hash_indirect
  9265. || h->root.type == bfd_link_hash_warning)
  9266. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9267. switch (h->root.type)
  9268. {
  9269. case bfd_link_hash_defined:
  9270. case bfd_link_hash_defweak:
  9271. target_sec = h->root.u.def.section;
  9272. break;
  9273. case bfd_link_hash_common:
  9274. target_sec = bfd_com_section_ptr;
  9275. break;
  9276. case bfd_link_hash_undefined:
  9277. case bfd_link_hash_undefweak:
  9278. target_sec = bfd_und_section_ptr;
  9279. break;
  9280. default: /* New indirect warning. */
  9281. target_sec = bfd_und_section_ptr;
  9282. break;
  9283. }
  9284. }
  9285. return target_sec;
  9286. }
  9287. static struct elf_link_hash_entry *
  9288. get_elf_r_symndx_hash_entry (bfd *abfd, unsigned long r_symndx)
  9289. {
  9290. unsigned long indx;
  9291. struct elf_link_hash_entry *h;
  9292. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  9293. if (r_symndx < symtab_hdr->sh_info)
  9294. return NULL;
  9295. indx = r_symndx - symtab_hdr->sh_info;
  9296. h = elf_sym_hashes (abfd)[indx];
  9297. while (h->root.type == bfd_link_hash_indirect
  9298. || h->root.type == bfd_link_hash_warning)
  9299. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9300. return h;
  9301. }
  9302. /* Get the section-relative offset for a symbol number. */
  9303. static bfd_vma
  9304. get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
  9305. {
  9306. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  9307. bfd_vma offset = 0;
  9308. if (r_symndx < symtab_hdr->sh_info)
  9309. {
  9310. Elf_Internal_Sym *isymbuf;
  9311. isymbuf = retrieve_local_syms (abfd);
  9312. offset = isymbuf[r_symndx].st_value;
  9313. }
  9314. else
  9315. {
  9316. unsigned long indx = r_symndx - symtab_hdr->sh_info;
  9317. struct elf_link_hash_entry *h =
  9318. elf_sym_hashes (abfd)[indx];
  9319. while (h->root.type == bfd_link_hash_indirect
  9320. || h->root.type == bfd_link_hash_warning)
  9321. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9322. if (h->root.type == bfd_link_hash_defined
  9323. || h->root.type == bfd_link_hash_defweak)
  9324. offset = h->root.u.def.value;
  9325. }
  9326. return offset;
  9327. }
  9328. static bool
  9329. is_reloc_sym_weak (bfd *abfd, Elf_Internal_Rela *rel)
  9330. {
  9331. unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
  9332. struct elf_link_hash_entry *h;
  9333. h = get_elf_r_symndx_hash_entry (abfd, r_symndx);
  9334. if (h && h->root.type == bfd_link_hash_defweak)
  9335. return true;
  9336. return false;
  9337. }
  9338. static bool
  9339. pcrel_reloc_fits (xtensa_opcode opc,
  9340. int opnd,
  9341. bfd_vma self_address,
  9342. bfd_vma dest_address)
  9343. {
  9344. xtensa_isa isa = xtensa_default_isa;
  9345. uint32 valp = dest_address;
  9346. if (xtensa_operand_do_reloc (isa, opc, opnd, &valp, self_address)
  9347. || xtensa_operand_encode (isa, opc, opnd, &valp))
  9348. return false;
  9349. return true;
  9350. }
  9351. static bool
  9352. xtensa_is_property_section (asection *sec)
  9353. {
  9354. if (xtensa_is_insntable_section (sec)
  9355. || xtensa_is_littable_section (sec)
  9356. || xtensa_is_proptable_section (sec))
  9357. return true;
  9358. return false;
  9359. }
  9360. static bool
  9361. xtensa_is_insntable_section (asection *sec)
  9362. {
  9363. if (startswith (sec->name, XTENSA_INSN_SEC_NAME)
  9364. || startswith (sec->name, ".gnu.linkonce.x."))
  9365. return true;
  9366. return false;
  9367. }
  9368. static bool
  9369. xtensa_is_littable_section (asection *sec)
  9370. {
  9371. if (startswith (sec->name, XTENSA_LIT_SEC_NAME)
  9372. || startswith (sec->name, ".gnu.linkonce.p."))
  9373. return true;
  9374. return false;
  9375. }
  9376. static bool
  9377. xtensa_is_proptable_section (asection *sec)
  9378. {
  9379. if (startswith (sec->name, XTENSA_PROP_SEC_NAME)
  9380. || startswith (sec->name, ".gnu.linkonce.prop."))
  9381. return true;
  9382. return false;
  9383. }
  9384. static int
  9385. internal_reloc_compare (const void *ap, const void *bp)
  9386. {
  9387. const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
  9388. const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
  9389. if (a->r_offset != b->r_offset)
  9390. return (a->r_offset - b->r_offset);
  9391. /* We don't need to sort on these criteria for correctness,
  9392. but enforcing a more strict ordering prevents unstable qsort
  9393. from behaving differently with different implementations.
  9394. Without the code below we get correct but different results
  9395. on Solaris 2.7 and 2.8. We would like to always produce the
  9396. same results no matter the host. */
  9397. if (a->r_info != b->r_info)
  9398. return (a->r_info - b->r_info);
  9399. return (a->r_addend - b->r_addend);
  9400. }
  9401. static int
  9402. internal_reloc_matches (const void *ap, const void *bp)
  9403. {
  9404. const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
  9405. const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
  9406. /* Check if one entry overlaps with the other; this shouldn't happen
  9407. except when searching for a match. */
  9408. return (a->r_offset - b->r_offset);
  9409. }
  9410. /* Predicate function used to look up a section in a particular group. */
  9411. static bool
  9412. match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
  9413. {
  9414. const char *gname = inf;
  9415. const char *group_name = elf_group_name (sec);
  9416. return (group_name == gname
  9417. || (group_name != NULL
  9418. && gname != NULL
  9419. && strcmp (group_name, gname) == 0));
  9420. }
  9421. static char *
  9422. xtensa_add_names (const char *base, const char *suffix)
  9423. {
  9424. if (suffix)
  9425. {
  9426. size_t base_len = strlen (base);
  9427. size_t suffix_len = strlen (suffix);
  9428. char *str = bfd_malloc (base_len + suffix_len + 1);
  9429. memcpy (str, base, base_len);
  9430. memcpy (str + base_len, suffix, suffix_len + 1);
  9431. return str;
  9432. }
  9433. else
  9434. {
  9435. return strdup (base);
  9436. }
  9437. }
  9438. static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
  9439. static char *
  9440. xtensa_property_section_name (asection *sec, const char *base_name,
  9441. bool separate_sections)
  9442. {
  9443. const char *suffix, *group_name;
  9444. char *prop_sec_name;
  9445. group_name = elf_group_name (sec);
  9446. if (group_name)
  9447. {
  9448. suffix = strrchr (sec->name, '.');
  9449. if (suffix == sec->name)
  9450. suffix = 0;
  9451. prop_sec_name = xtensa_add_names (base_name, suffix);
  9452. }
  9453. else if (startswith (sec->name, ".gnu.linkonce."))
  9454. {
  9455. char *linkonce_kind = 0;
  9456. if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
  9457. linkonce_kind = "x.";
  9458. else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
  9459. linkonce_kind = "p.";
  9460. else if (strcmp (base_name, XTENSA_PROP_SEC_NAME) == 0)
  9461. linkonce_kind = "prop.";
  9462. else
  9463. abort ();
  9464. prop_sec_name = (char *) bfd_malloc (strlen (sec->name)
  9465. + strlen (linkonce_kind) + 1);
  9466. memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
  9467. strcpy (prop_sec_name + linkonce_len, linkonce_kind);
  9468. suffix = sec->name + linkonce_len;
  9469. /* For backward compatibility, replace "t." instead of inserting
  9470. the new linkonce_kind (but not for "prop" sections). */
  9471. if (startswith (suffix, "t.") && linkonce_kind[1] == '.')
  9472. suffix += 2;
  9473. strcat (prop_sec_name + linkonce_len, suffix);
  9474. }
  9475. else
  9476. {
  9477. prop_sec_name = xtensa_add_names (base_name,
  9478. separate_sections ? sec->name : NULL);
  9479. }
  9480. return prop_sec_name;
  9481. }
  9482. static asection *
  9483. xtensa_get_separate_property_section (asection *sec, const char *base_name,
  9484. bool separate_section)
  9485. {
  9486. char *prop_sec_name;
  9487. asection *prop_sec;
  9488. prop_sec_name = xtensa_property_section_name (sec, base_name,
  9489. separate_section);
  9490. prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
  9491. match_section_group,
  9492. (void *) elf_group_name (sec));
  9493. free (prop_sec_name);
  9494. return prop_sec;
  9495. }
  9496. static asection *
  9497. xtensa_get_property_section (asection *sec, const char *base_name)
  9498. {
  9499. asection *prop_sec;
  9500. /* Try individual property section first. */
  9501. prop_sec = xtensa_get_separate_property_section (sec, base_name, true);
  9502. /* Refer to a common property section if individual is not present. */
  9503. if (!prop_sec)
  9504. prop_sec = xtensa_get_separate_property_section (sec, base_name, false);
  9505. return prop_sec;
  9506. }
  9507. asection *
  9508. xtensa_make_property_section (asection *sec, const char *base_name)
  9509. {
  9510. char *prop_sec_name;
  9511. asection *prop_sec;
  9512. /* Check if the section already exists. */
  9513. prop_sec_name = xtensa_property_section_name (sec, base_name,
  9514. elf32xtensa_separate_props);
  9515. prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
  9516. match_section_group,
  9517. (void *) elf_group_name (sec));
  9518. /* If not, create it. */
  9519. if (! prop_sec)
  9520. {
  9521. flagword flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY);
  9522. flags |= (bfd_section_flags (sec)
  9523. & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES));
  9524. prop_sec = bfd_make_section_anyway_with_flags
  9525. (sec->owner, strdup (prop_sec_name), flags);
  9526. if (! prop_sec)
  9527. return 0;
  9528. elf_group_name (prop_sec) = elf_group_name (sec);
  9529. }
  9530. free (prop_sec_name);
  9531. return prop_sec;
  9532. }
  9533. flagword
  9534. xtensa_get_property_predef_flags (asection *sec)
  9535. {
  9536. if (xtensa_is_insntable_section (sec))
  9537. return (XTENSA_PROP_INSN
  9538. | XTENSA_PROP_NO_TRANSFORM
  9539. | XTENSA_PROP_INSN_NO_REORDER);
  9540. if (xtensa_is_littable_section (sec))
  9541. return (XTENSA_PROP_LITERAL
  9542. | XTENSA_PROP_NO_TRANSFORM
  9543. | XTENSA_PROP_INSN_NO_REORDER);
  9544. return 0;
  9545. }
  9546. /* Other functions called directly by the linker. */
  9547. bool
  9548. xtensa_callback_required_dependence (bfd *abfd,
  9549. asection *sec,
  9550. struct bfd_link_info *link_info,
  9551. deps_callback_t callback,
  9552. void *closure)
  9553. {
  9554. Elf_Internal_Rela *internal_relocs;
  9555. bfd_byte *contents;
  9556. unsigned i;
  9557. bool ok = true;
  9558. bfd_size_type sec_size;
  9559. sec_size = bfd_get_section_limit (abfd, sec);
  9560. /* ".plt*" sections have no explicit relocations but they contain L32R
  9561. instructions that reference the corresponding ".got.plt*" sections. */
  9562. if ((sec->flags & SEC_LINKER_CREATED) != 0
  9563. && startswith (sec->name, ".plt"))
  9564. {
  9565. asection *sgotplt;
  9566. /* Find the corresponding ".got.plt*" section. */
  9567. if (sec->name[4] == '\0')
  9568. sgotplt = elf_hash_table (link_info)->sgotplt;
  9569. else
  9570. {
  9571. char got_name[14];
  9572. int chunk = 0;
  9573. BFD_ASSERT (sec->name[4] == '.');
  9574. chunk = strtol (&sec->name[5], NULL, 10);
  9575. sprintf (got_name, ".got.plt.%u", chunk);
  9576. sgotplt = bfd_get_linker_section (sec->owner, got_name);
  9577. }
  9578. BFD_ASSERT (sgotplt);
  9579. /* Assume worst-case offsets: L32R at the very end of the ".plt"
  9580. section referencing a literal at the very beginning of
  9581. ".got.plt". This is very close to the real dependence, anyway. */
  9582. (*callback) (sec, sec_size, sgotplt, 0, closure);
  9583. }
  9584. /* Only ELF files are supported for Xtensa. Check here to avoid a segfault
  9585. when building uclibc, which runs "ld -b binary /dev/null". */
  9586. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  9587. return ok;
  9588. internal_relocs = retrieve_internal_relocs (abfd, sec,
  9589. link_info->keep_memory);
  9590. if (internal_relocs == NULL
  9591. || sec->reloc_count == 0)
  9592. return ok;
  9593. /* Cache the contents for the duration of this scan. */
  9594. contents = retrieve_contents (abfd, sec, link_info->keep_memory);
  9595. if (contents == NULL && sec_size != 0)
  9596. {
  9597. ok = false;
  9598. goto error_return;
  9599. }
  9600. if (!xtensa_default_isa)
  9601. xtensa_default_isa = xtensa_isa_init (0, 0);
  9602. for (i = 0; i < sec->reloc_count; i++)
  9603. {
  9604. Elf_Internal_Rela *irel = &internal_relocs[i];
  9605. if (is_l32r_relocation (abfd, sec, contents, irel))
  9606. {
  9607. r_reloc l32r_rel;
  9608. asection *target_sec;
  9609. bfd_vma target_offset;
  9610. r_reloc_init (&l32r_rel, abfd, irel, contents, sec_size);
  9611. target_sec = NULL;
  9612. target_offset = 0;
  9613. /* L32Rs must be local to the input file. */
  9614. if (r_reloc_is_defined (&l32r_rel))
  9615. {
  9616. target_sec = r_reloc_get_section (&l32r_rel);
  9617. target_offset = l32r_rel.target_offset;
  9618. }
  9619. (*callback) (sec, irel->r_offset, target_sec, target_offset,
  9620. closure);
  9621. }
  9622. }
  9623. error_return:
  9624. release_internal_relocs (sec, internal_relocs);
  9625. release_contents (sec, contents);
  9626. return ok;
  9627. }
  9628. /* The default literal sections should always be marked as "code" (i.e.,
  9629. SHF_EXECINSTR). This is particularly important for the Linux kernel
  9630. module loader so that the literals are not placed after the text. */
  9631. static const struct bfd_elf_special_section elf_xtensa_special_sections[] =
  9632. {
  9633. { STRING_COMMA_LEN (".fini.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
  9634. { STRING_COMMA_LEN (".init.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
  9635. { STRING_COMMA_LEN (".literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
  9636. { STRING_COMMA_LEN (".xtensa.info"), 0, SHT_NOTE, 0 },
  9637. { NULL, 0, 0, 0, 0 }
  9638. };
  9639. #define ELF_TARGET_ID XTENSA_ELF_DATA
  9640. #ifndef ELF_ARCH
  9641. #define TARGET_LITTLE_SYM xtensa_elf32_le_vec
  9642. #define TARGET_LITTLE_NAME "elf32-xtensa-le"
  9643. #define TARGET_BIG_SYM xtensa_elf32_be_vec
  9644. #define TARGET_BIG_NAME "elf32-xtensa-be"
  9645. #define ELF_ARCH bfd_arch_xtensa
  9646. #define ELF_MACHINE_CODE EM_XTENSA
  9647. #define ELF_MACHINE_ALT1 EM_XTENSA_OLD
  9648. #define ELF_MAXPAGESIZE 0x1000
  9649. #endif /* ELF_ARCH */
  9650. #define elf_backend_can_gc_sections 1
  9651. #define elf_backend_can_refcount 1
  9652. #define elf_backend_plt_readonly 1
  9653. #define elf_backend_got_header_size 4
  9654. #define elf_backend_want_dynbss 0
  9655. #define elf_backend_want_got_plt 1
  9656. #define elf_backend_dtrel_excludes_plt 1
  9657. #define elf_info_to_howto elf_xtensa_info_to_howto_rela
  9658. #define bfd_elf32_mkobject elf_xtensa_mkobject
  9659. #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
  9660. #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
  9661. #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
  9662. #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
  9663. #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
  9664. #define bfd_elf32_bfd_reloc_name_lookup \
  9665. elf_xtensa_reloc_name_lookup
  9666. #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
  9667. #define bfd_elf32_bfd_link_hash_table_create elf_xtensa_link_hash_table_create
  9668. #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
  9669. #define elf_backend_check_relocs elf_xtensa_check_relocs
  9670. #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
  9671. #define elf_backend_discard_info elf_xtensa_discard_info
  9672. #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
  9673. #define elf_backend_final_write_processing elf_xtensa_final_write_processing
  9674. #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
  9675. #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
  9676. #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
  9677. #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
  9678. #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
  9679. #define elf_backend_hide_symbol elf_xtensa_hide_symbol
  9680. #define elf_backend_object_p elf_xtensa_object_p
  9681. #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
  9682. #define elf_backend_relocate_section elf_xtensa_relocate_section
  9683. #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
  9684. #define elf_backend_always_size_sections elf_xtensa_always_size_sections
  9685. #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
  9686. #define elf_backend_special_sections elf_xtensa_special_sections
  9687. #define elf_backend_action_discarded elf_xtensa_action_discarded
  9688. #define elf_backend_copy_indirect_symbol elf_xtensa_copy_indirect_symbol
  9689. #include "elf32-target.h"