aarch64.cc 269 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605
  1. // aarch64.cc -- aarch64 target support for gold.
  2. // Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. // Written by Jing Yu <jingyu@google.com> and Han Shen <shenhan@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cstring>
  19. #include <map>
  20. #include <set>
  21. #include "elfcpp.h"
  22. #include "dwarf.h"
  23. #include "parameters.h"
  24. #include "reloc.h"
  25. #include "aarch64.h"
  26. #include "object.h"
  27. #include "symtab.h"
  28. #include "layout.h"
  29. #include "output.h"
  30. #include "copy-relocs.h"
  31. #include "target.h"
  32. #include "target-reloc.h"
  33. #include "target-select.h"
  34. #include "tls.h"
  35. #include "freebsd.h"
  36. #include "nacl.h"
  37. #include "gc.h"
  38. #include "icf.h"
  39. #include "aarch64-reloc-property.h"
  40. // The first three .got.plt entries are reserved.
  41. const int32_t AARCH64_GOTPLT_RESERVE_COUNT = 3;
  42. namespace
  43. {
  44. using namespace gold;
  45. template<int size, bool big_endian>
  46. class Output_data_plt_aarch64;
  47. template<int size, bool big_endian>
  48. class Output_data_plt_aarch64_standard;
  49. template<int size, bool big_endian>
  50. class Target_aarch64;
  51. template<int size, bool big_endian>
  52. class AArch64_relocate_functions;
  53. // Utility class dealing with insns. This is ported from macros in
  54. // bfd/elfnn-aarch64.cc, but wrapped inside a class as static members. This
  55. // class is used in erratum sequence scanning.
  56. template<bool big_endian>
  57. class AArch64_insn_utilities
  58. {
  59. public:
  60. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  61. static const int BYTES_PER_INSN;
  62. // Zero register encoding - 31.
  63. static const unsigned int AARCH64_ZR;
  64. static unsigned int
  65. aarch64_bit(Insntype insn, int pos)
  66. { return ((1 << pos) & insn) >> pos; }
  67. static unsigned int
  68. aarch64_bits(Insntype insn, int pos, int l)
  69. { return (insn >> pos) & ((1 << l) - 1); }
  70. // Get the encoding field "op31" of 3-source data processing insns. "op31" is
  71. // the name defined in armv8 insn manual C3.5.9.
  72. static unsigned int
  73. aarch64_op31(Insntype insn)
  74. { return aarch64_bits(insn, 21, 3); }
  75. // Get the encoding field "ra" of 3-source data processing insns. "ra" is the
  76. // third source register. See armv8 insn manual C3.5.9.
  77. static unsigned int
  78. aarch64_ra(Insntype insn)
  79. { return aarch64_bits(insn, 10, 5); }
  80. static bool
  81. is_adr(const Insntype insn)
  82. { return (insn & 0x9F000000) == 0x10000000; }
  83. static bool
  84. is_adrp(const Insntype insn)
  85. { return (insn & 0x9F000000) == 0x90000000; }
  86. static bool
  87. is_mrs_tpidr_el0(const Insntype insn)
  88. { return (insn & 0xFFFFFFE0) == 0xd53bd040; }
  89. static unsigned int
  90. aarch64_rm(const Insntype insn)
  91. { return aarch64_bits(insn, 16, 5); }
  92. static unsigned int
  93. aarch64_rn(const Insntype insn)
  94. { return aarch64_bits(insn, 5, 5); }
  95. static unsigned int
  96. aarch64_rd(const Insntype insn)
  97. { return aarch64_bits(insn, 0, 5); }
  98. static unsigned int
  99. aarch64_rt(const Insntype insn)
  100. { return aarch64_bits(insn, 0, 5); }
  101. static unsigned int
  102. aarch64_rt2(const Insntype insn)
  103. { return aarch64_bits(insn, 10, 5); }
  104. // Encode imm21 into adr. Signed imm21 is in the range of [-1M, 1M).
  105. static Insntype
  106. aarch64_adr_encode_imm(Insntype adr, int imm21)
  107. {
  108. gold_assert(is_adr(adr));
  109. gold_assert(-(1 << 20) <= imm21 && imm21 < (1 << 20));
  110. const int mask19 = (1 << 19) - 1;
  111. const int mask2 = 3;
  112. adr &= ~((mask19 << 5) | (mask2 << 29));
  113. adr |= ((imm21 & mask2) << 29) | (((imm21 >> 2) & mask19) << 5);
  114. return adr;
  115. }
  116. // Retrieve encoded adrp 33-bit signed imm value. This value is obtained by
  117. // 21-bit signed imm encoded in the insn multiplied by 4k (page size) and
  118. // 64-bit sign-extended, resulting in [-4G, 4G) with 12-lsb being 0.
  119. static int64_t
  120. aarch64_adrp_decode_imm(const Insntype adrp)
  121. {
  122. const int mask19 = (1 << 19) - 1;
  123. const int mask2 = 3;
  124. gold_assert(is_adrp(adrp));
  125. // 21-bit imm encoded in adrp.
  126. uint64_t imm = ((adrp >> 29) & mask2) | (((adrp >> 5) & mask19) << 2);
  127. // Retrieve msb of 21-bit-signed imm for sign extension.
  128. uint64_t msbt = (imm >> 20) & 1;
  129. // Real value is imm multiplied by 4k. Value now has 33-bit information.
  130. int64_t value = imm << 12;
  131. // Sign extend to 64-bit by repeating msbt 31 (64-33) times and merge it
  132. // with value.
  133. return ((((uint64_t)(1) << 32) - msbt) << 33) | value;
  134. }
  135. static bool
  136. aarch64_b(const Insntype insn)
  137. { return (insn & 0xFC000000) == 0x14000000; }
  138. static bool
  139. aarch64_bl(const Insntype insn)
  140. { return (insn & 0xFC000000) == 0x94000000; }
  141. static bool
  142. aarch64_blr(const Insntype insn)
  143. { return (insn & 0xFFFFFC1F) == 0xD63F0000; }
  144. static bool
  145. aarch64_br(const Insntype insn)
  146. { return (insn & 0xFFFFFC1F) == 0xD61F0000; }
  147. // All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
  148. // LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.
  149. static bool
  150. aarch64_ld(Insntype insn) { return aarch64_bit(insn, 22) == 1; }
  151. static bool
  152. aarch64_ldst(Insntype insn)
  153. { return (insn & 0x0a000000) == 0x08000000; }
  154. static bool
  155. aarch64_ldst_ex(Insntype insn)
  156. { return (insn & 0x3f000000) == 0x08000000; }
  157. static bool
  158. aarch64_ldst_pcrel(Insntype insn)
  159. { return (insn & 0x3b000000) == 0x18000000; }
  160. static bool
  161. aarch64_ldst_nap(Insntype insn)
  162. { return (insn & 0x3b800000) == 0x28000000; }
  163. static bool
  164. aarch64_ldstp_pi(Insntype insn)
  165. { return (insn & 0x3b800000) == 0x28800000; }
  166. static bool
  167. aarch64_ldstp_o(Insntype insn)
  168. { return (insn & 0x3b800000) == 0x29000000; }
  169. static bool
  170. aarch64_ldstp_pre(Insntype insn)
  171. { return (insn & 0x3b800000) == 0x29800000; }
  172. static bool
  173. aarch64_ldst_ui(Insntype insn)
  174. { return (insn & 0x3b200c00) == 0x38000000; }
  175. static bool
  176. aarch64_ldst_piimm(Insntype insn)
  177. { return (insn & 0x3b200c00) == 0x38000400; }
  178. static bool
  179. aarch64_ldst_u(Insntype insn)
  180. { return (insn & 0x3b200c00) == 0x38000800; }
  181. static bool
  182. aarch64_ldst_preimm(Insntype insn)
  183. { return (insn & 0x3b200c00) == 0x38000c00; }
  184. static bool
  185. aarch64_ldst_ro(Insntype insn)
  186. { return (insn & 0x3b200c00) == 0x38200800; }
  187. static bool
  188. aarch64_ldst_uimm(Insntype insn)
  189. { return (insn & 0x3b000000) == 0x39000000; }
  190. static bool
  191. aarch64_ldst_simd_m(Insntype insn)
  192. { return (insn & 0xbfbf0000) == 0x0c000000; }
  193. static bool
  194. aarch64_ldst_simd_m_pi(Insntype insn)
  195. { return (insn & 0xbfa00000) == 0x0c800000; }
  196. static bool
  197. aarch64_ldst_simd_s(Insntype insn)
  198. { return (insn & 0xbf9f0000) == 0x0d000000; }
  199. static bool
  200. aarch64_ldst_simd_s_pi(Insntype insn)
  201. { return (insn & 0xbf800000) == 0x0d800000; }
  202. // Classify an INSN if it is indeed a load/store. Return true if INSN is a
  203. // LD/ST instruction otherwise return false. For scalar LD/ST instructions
  204. // PAIR is FALSE, RT is returned and RT2 is set equal to RT. For LD/ST pair
  205. // instructions PAIR is TRUE, RT and RT2 are returned.
  206. static bool
  207. aarch64_mem_op_p(Insntype insn, unsigned int *rt, unsigned int *rt2,
  208. bool *pair, bool *load)
  209. {
  210. uint32_t opcode;
  211. unsigned int r;
  212. uint32_t opc = 0;
  213. uint32_t v = 0;
  214. uint32_t opc_v = 0;
  215. /* Bail out quickly if INSN doesn't fall into the load-store
  216. encoding space. */
  217. if (!aarch64_ldst (insn))
  218. return false;
  219. *pair = false;
  220. *load = false;
  221. if (aarch64_ldst_ex (insn))
  222. {
  223. *rt = aarch64_rt (insn);
  224. *rt2 = *rt;
  225. if (aarch64_bit (insn, 21) == 1)
  226. {
  227. *pair = true;
  228. *rt2 = aarch64_rt2 (insn);
  229. }
  230. *load = aarch64_ld (insn);
  231. return true;
  232. }
  233. else if (aarch64_ldst_nap (insn)
  234. || aarch64_ldstp_pi (insn)
  235. || aarch64_ldstp_o (insn)
  236. || aarch64_ldstp_pre (insn))
  237. {
  238. *pair = true;
  239. *rt = aarch64_rt (insn);
  240. *rt2 = aarch64_rt2 (insn);
  241. *load = aarch64_ld (insn);
  242. return true;
  243. }
  244. else if (aarch64_ldst_pcrel (insn)
  245. || aarch64_ldst_ui (insn)
  246. || aarch64_ldst_piimm (insn)
  247. || aarch64_ldst_u (insn)
  248. || aarch64_ldst_preimm (insn)
  249. || aarch64_ldst_ro (insn)
  250. || aarch64_ldst_uimm (insn))
  251. {
  252. *rt = aarch64_rt (insn);
  253. *rt2 = *rt;
  254. if (aarch64_ldst_pcrel (insn))
  255. *load = true;
  256. opc = aarch64_bits (insn, 22, 2);
  257. v = aarch64_bit (insn, 26);
  258. opc_v = opc | (v << 2);
  259. *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
  260. || opc_v == 5 || opc_v == 7);
  261. return true;
  262. }
  263. else if (aarch64_ldst_simd_m (insn)
  264. || aarch64_ldst_simd_m_pi (insn))
  265. {
  266. *rt = aarch64_rt (insn);
  267. *load = aarch64_bit (insn, 22);
  268. opcode = (insn >> 12) & 0xf;
  269. switch (opcode)
  270. {
  271. case 0:
  272. case 2:
  273. *rt2 = *rt + 3;
  274. break;
  275. case 4:
  276. case 6:
  277. *rt2 = *rt + 2;
  278. break;
  279. case 7:
  280. *rt2 = *rt;
  281. break;
  282. case 8:
  283. case 10:
  284. *rt2 = *rt + 1;
  285. break;
  286. default:
  287. return false;
  288. }
  289. return true;
  290. }
  291. else if (aarch64_ldst_simd_s (insn)
  292. || aarch64_ldst_simd_s_pi (insn))
  293. {
  294. *rt = aarch64_rt (insn);
  295. r = (insn >> 21) & 1;
  296. *load = aarch64_bit (insn, 22);
  297. opcode = (insn >> 13) & 0x7;
  298. switch (opcode)
  299. {
  300. case 0:
  301. case 2:
  302. case 4:
  303. *rt2 = *rt + r;
  304. break;
  305. case 1:
  306. case 3:
  307. case 5:
  308. *rt2 = *rt + (r == 0 ? 2 : 3);
  309. break;
  310. case 6:
  311. *rt2 = *rt + r;
  312. break;
  313. case 7:
  314. *rt2 = *rt + (r == 0 ? 2 : 3);
  315. break;
  316. default:
  317. return false;
  318. }
  319. return true;
  320. }
  321. return false;
  322. } // End of "aarch64_mem_op_p".
  323. // Return true if INSN is mac insn.
  324. static bool
  325. aarch64_mac(Insntype insn)
  326. { return (insn & 0xff000000) == 0x9b000000; }
  327. // Return true if INSN is multiply-accumulate.
  328. // (This is similar to implementaton in elfnn-aarch64.c.)
  329. static bool
  330. aarch64_mlxl(Insntype insn)
  331. {
  332. uint32_t op31 = aarch64_op31(insn);
  333. if (aarch64_mac(insn)
  334. && (op31 == 0 || op31 == 1 || op31 == 5)
  335. /* Exclude MUL instructions which are encoded as a multiple-accumulate
  336. with RA = XZR. */
  337. && aarch64_ra(insn) != AARCH64_ZR)
  338. {
  339. return true;
  340. }
  341. return false;
  342. }
  343. }; // End of "AArch64_insn_utilities".
  344. // Insn length in byte.
  345. template<bool big_endian>
  346. const int AArch64_insn_utilities<big_endian>::BYTES_PER_INSN = 4;
  347. // Zero register encoding - 31.
  348. template<bool big_endian>
  349. const unsigned int AArch64_insn_utilities<big_endian>::AARCH64_ZR = 0x1f;
  350. // Output_data_got_aarch64 class.
  351. template<int size, bool big_endian>
  352. class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
  353. {
  354. public:
  355. typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
  356. Output_data_got_aarch64(Symbol_table* symtab, Layout* layout)
  357. : Output_data_got<size, big_endian>(),
  358. symbol_table_(symtab), layout_(layout)
  359. { }
  360. // Add a static entry for the GOT entry at OFFSET. GSYM is a global
  361. // symbol and R_TYPE is the code of a dynamic relocation that needs to be
  362. // applied in a static link.
  363. void
  364. add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
  365. { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
  366. // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
  367. // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
  368. // relocation that needs to be applied in a static link.
  369. void
  370. add_static_reloc(unsigned int got_offset, unsigned int r_type,
  371. Sized_relobj_file<size, big_endian>* relobj,
  372. unsigned int index)
  373. {
  374. this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
  375. index));
  376. }
  377. protected:
  378. // Write out the GOT table.
  379. void
  380. do_write(Output_file* of) {
  381. // The first entry in the GOT is the address of the .dynamic section.
  382. gold_assert(this->data_size() >= size / 8);
  383. Output_section* dynamic = this->layout_->dynamic_section();
  384. Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
  385. this->replace_constant(0, dynamic_addr);
  386. Output_data_got<size, big_endian>::do_write(of);
  387. // Handling static relocs
  388. if (this->static_relocs_.empty())
  389. return;
  390. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  391. gold_assert(parameters->doing_static_link());
  392. const off_t offset = this->offset();
  393. const section_size_type oview_size =
  394. convert_to_section_size_type(this->data_size());
  395. unsigned char* const oview = of->get_output_view(offset, oview_size);
  396. Output_segment* tls_segment = this->layout_->tls_segment();
  397. gold_assert(tls_segment != NULL);
  398. AArch64_address aligned_tcb_address =
  399. align_address(Target_aarch64<size, big_endian>::TCB_SIZE,
  400. tls_segment->maximum_alignment());
  401. for (size_t i = 0; i < this->static_relocs_.size(); ++i)
  402. {
  403. Static_reloc& reloc(this->static_relocs_[i]);
  404. AArch64_address value;
  405. if (!reloc.symbol_is_global())
  406. {
  407. Sized_relobj_file<size, big_endian>* object = reloc.relobj();
  408. const Symbol_value<size>* psymval =
  409. reloc.relobj()->local_symbol(reloc.index());
  410. // We are doing static linking. Issue an error and skip this
  411. // relocation if the symbol is undefined or in a discarded_section.
  412. bool is_ordinary;
  413. unsigned int shndx = psymval->input_shndx(&is_ordinary);
  414. if ((shndx == elfcpp::SHN_UNDEF)
  415. || (is_ordinary
  416. && shndx != elfcpp::SHN_UNDEF
  417. && !object->is_section_included(shndx)
  418. && !this->symbol_table_->is_section_folded(object, shndx)))
  419. {
  420. gold_error(_("undefined or discarded local symbol %u from "
  421. " object %s in GOT"),
  422. reloc.index(), reloc.relobj()->name().c_str());
  423. continue;
  424. }
  425. value = psymval->value(object, 0);
  426. }
  427. else
  428. {
  429. const Symbol* gsym = reloc.symbol();
  430. gold_assert(gsym != NULL);
  431. if (gsym->is_forwarder())
  432. gsym = this->symbol_table_->resolve_forwards(gsym);
  433. // We are doing static linking. Issue an error and skip this
  434. // relocation if the symbol is undefined or in a discarded_section
  435. // unless it is a weakly_undefined symbol.
  436. if ((gsym->is_defined_in_discarded_section()
  437. || gsym->is_undefined())
  438. && !gsym->is_weak_undefined())
  439. {
  440. gold_error(_("undefined or discarded symbol %s in GOT"),
  441. gsym->name());
  442. continue;
  443. }
  444. if (!gsym->is_weak_undefined())
  445. {
  446. const Sized_symbol<size>* sym =
  447. static_cast<const Sized_symbol<size>*>(gsym);
  448. value = sym->value();
  449. }
  450. else
  451. value = 0;
  452. }
  453. unsigned got_offset = reloc.got_offset();
  454. gold_assert(got_offset < oview_size);
  455. typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
  456. Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
  457. Valtype x;
  458. switch (reloc.r_type())
  459. {
  460. case elfcpp::R_AARCH64_TLS_DTPREL64:
  461. x = value;
  462. break;
  463. case elfcpp::R_AARCH64_TLS_TPREL64:
  464. x = value + aligned_tcb_address;
  465. break;
  466. default:
  467. gold_unreachable();
  468. }
  469. elfcpp::Swap<size, big_endian>::writeval(wv, x);
  470. }
  471. of->write_output_view(offset, oview_size, oview);
  472. }
  473. private:
  474. // Symbol table of the output object.
  475. Symbol_table* symbol_table_;
  476. // A pointer to the Layout class, so that we can find the .dynamic
  477. // section when we write out the GOT section.
  478. Layout* layout_;
  479. // This class represent dynamic relocations that need to be applied by
  480. // gold because we are using TLS relocations in a static link.
  481. class Static_reloc
  482. {
  483. public:
  484. Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
  485. : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
  486. { this->u_.global.symbol = gsym; }
  487. Static_reloc(unsigned int got_offset, unsigned int r_type,
  488. Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
  489. : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
  490. {
  491. this->u_.local.relobj = relobj;
  492. this->u_.local.index = index;
  493. }
  494. // Return the GOT offset.
  495. unsigned int
  496. got_offset() const
  497. { return this->got_offset_; }
  498. // Relocation type.
  499. unsigned int
  500. r_type() const
  501. { return this->r_type_; }
  502. // Whether the symbol is global or not.
  503. bool
  504. symbol_is_global() const
  505. { return this->symbol_is_global_; }
  506. // For a relocation against a global symbol, the global symbol.
  507. Symbol*
  508. symbol() const
  509. {
  510. gold_assert(this->symbol_is_global_);
  511. return this->u_.global.symbol;
  512. }
  513. // For a relocation against a local symbol, the defining object.
  514. Sized_relobj_file<size, big_endian>*
  515. relobj() const
  516. {
  517. gold_assert(!this->symbol_is_global_);
  518. return this->u_.local.relobj;
  519. }
  520. // For a relocation against a local symbol, the local symbol index.
  521. unsigned int
  522. index() const
  523. {
  524. gold_assert(!this->symbol_is_global_);
  525. return this->u_.local.index;
  526. }
  527. private:
  528. // GOT offset of the entry to which this relocation is applied.
  529. unsigned int got_offset_;
  530. // Type of relocation.
  531. unsigned int r_type_;
  532. // Whether this relocation is against a global symbol.
  533. bool symbol_is_global_;
  534. // A global or local symbol.
  535. union
  536. {
  537. struct
  538. {
  539. // For a global symbol, the symbol itself.
  540. Symbol* symbol;
  541. } global;
  542. struct
  543. {
  544. // For a local symbol, the object defining the symbol.
  545. Sized_relobj_file<size, big_endian>* relobj;
  546. // For a local symbol, the symbol index.
  547. unsigned int index;
  548. } local;
  549. } u_;
  550. }; // End of inner class Static_reloc
  551. std::vector<Static_reloc> static_relocs_;
  552. }; // End of Output_data_got_aarch64
  553. template<int size, bool big_endian>
  554. class AArch64_input_section;
  555. template<int size, bool big_endian>
  556. class AArch64_output_section;
  557. template<int size, bool big_endian>
  558. class AArch64_relobj;
  559. // Stub type enum constants.
  560. enum
  561. {
  562. ST_NONE = 0,
  563. // Using adrp/add pair, 4 insns (including alignment) without mem access,
  564. // the fastest stub. This has a limited jump distance, which is tested by
  565. // aarch64_valid_for_adrp_p.
  566. ST_ADRP_BRANCH = 1,
  567. // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
  568. // unlimited in jump distance.
  569. ST_LONG_BRANCH_ABS = 2,
  570. // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1
  571. // mem access, slowest one. Only used in position independent executables.
  572. ST_LONG_BRANCH_PCREL = 3,
  573. // Stub for erratum 843419 handling.
  574. ST_E_843419 = 4,
  575. // Stub for erratum 835769 handling.
  576. ST_E_835769 = 5,
  577. // Number of total stub types.
  578. ST_NUMBER = 6
  579. };
  580. // Struct that wraps insns for a particular stub. All stub templates are
  581. // created/initialized as constants by Stub_template_repertoire.
  582. template<bool big_endian>
  583. struct Stub_template
  584. {
  585. const typename AArch64_insn_utilities<big_endian>::Insntype* insns;
  586. const int insn_num;
  587. };
  588. // Simple singleton class that creates/initializes/stores all types of stub
  589. // templates.
  590. template<bool big_endian>
  591. class Stub_template_repertoire
  592. {
  593. public:
  594. typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
  595. // Single static method to get stub template for a given stub type.
  596. static const Stub_template<big_endian>*
  597. get_stub_template(int type)
  598. {
  599. static Stub_template_repertoire<big_endian> singleton;
  600. return singleton.stub_templates_[type];
  601. }
  602. private:
  603. // Constructor - creates/initializes all stub templates.
  604. Stub_template_repertoire();
  605. ~Stub_template_repertoire()
  606. { }
  607. // Disallowing copy ctor and copy assignment operator.
  608. Stub_template_repertoire(Stub_template_repertoire&);
  609. Stub_template_repertoire& operator=(Stub_template_repertoire&);
  610. // Data that stores all insn templates.
  611. const Stub_template<big_endian>* stub_templates_[ST_NUMBER];
  612. }; // End of "class Stub_template_repertoire".
  613. // Constructor - creates/initilizes all stub templates.
  614. template<bool big_endian>
  615. Stub_template_repertoire<big_endian>::Stub_template_repertoire()
  616. {
  617. // Insn array definitions.
  618. const static Insntype ST_NONE_INSNS[] = {};
  619. const static Insntype ST_ADRP_BRANCH_INSNS[] =
  620. {
  621. 0x90000010, /* adrp ip0, X */
  622. /* ADR_PREL_PG_HI21(X) */
  623. 0x91000210, /* add ip0, ip0, :lo12:X */
  624. /* ADD_ABS_LO12_NC(X) */
  625. 0xd61f0200, /* br ip0 */
  626. 0x00000000, /* alignment padding */
  627. };
  628. const static Insntype ST_LONG_BRANCH_ABS_INSNS[] =
  629. {
  630. 0x58000050, /* ldr ip0, 0x8 */
  631. 0xd61f0200, /* br ip0 */
  632. 0x00000000, /* address field */
  633. 0x00000000, /* address fields */
  634. };
  635. const static Insntype ST_LONG_BRANCH_PCREL_INSNS[] =
  636. {
  637. 0x58000090, /* ldr ip0, 0x10 */
  638. 0x10000011, /* adr ip1, #0 */
  639. 0x8b110210, /* add ip0, ip0, ip1 */
  640. 0xd61f0200, /* br ip0 */
  641. 0x00000000, /* address field */
  642. 0x00000000, /* address field */
  643. 0x00000000, /* alignment padding */
  644. 0x00000000, /* alignment padding */
  645. };
  646. const static Insntype ST_E_843419_INSNS[] =
  647. {
  648. 0x00000000, /* Placeholder for erratum insn. */
  649. 0x14000000, /* b <label> */
  650. };
  651. // ST_E_835769 has the same stub template as ST_E_843419
  652. // but we reproduce the array here so that the sizeof
  653. // expressions in install_insn_template will work.
  654. const static Insntype ST_E_835769_INSNS[] =
  655. {
  656. 0x00000000, /* Placeholder for erratum insn. */
  657. 0x14000000, /* b <label> */
  658. };
  659. #define install_insn_template(T) \
  660. const static Stub_template<big_endian> template_##T = { \
  661. T##_INSNS, sizeof(T##_INSNS) / sizeof(T##_INSNS[0]) }; \
  662. this->stub_templates_[T] = &template_##T
  663. install_insn_template(ST_NONE);
  664. install_insn_template(ST_ADRP_BRANCH);
  665. install_insn_template(ST_LONG_BRANCH_ABS);
  666. install_insn_template(ST_LONG_BRANCH_PCREL);
  667. install_insn_template(ST_E_843419);
  668. install_insn_template(ST_E_835769);
  669. #undef install_insn_template
  670. }
  671. // Base class for stubs.
  672. template<int size, bool big_endian>
  673. class Stub_base
  674. {
  675. public:
  676. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  677. typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
  678. static const AArch64_address invalid_address =
  679. static_cast<AArch64_address>(-1);
  680. static const section_offset_type invalid_offset =
  681. static_cast<section_offset_type>(-1);
  682. Stub_base(int type)
  683. : destination_address_(invalid_address),
  684. offset_(invalid_offset),
  685. type_(type)
  686. {}
  687. ~Stub_base()
  688. {}
  689. // Get stub type.
  690. int
  691. type() const
  692. { return this->type_; }
  693. // Get stub template that provides stub insn information.
  694. const Stub_template<big_endian>*
  695. stub_template() const
  696. {
  697. return Stub_template_repertoire<big_endian>::
  698. get_stub_template(this->type());
  699. }
  700. // Get destination address.
  701. AArch64_address
  702. destination_address() const
  703. {
  704. gold_assert(this->destination_address_ != this->invalid_address);
  705. return this->destination_address_;
  706. }
  707. // Set destination address.
  708. void
  709. set_destination_address(AArch64_address address)
  710. {
  711. gold_assert(address != this->invalid_address);
  712. this->destination_address_ = address;
  713. }
  714. // Reset the destination address.
  715. void
  716. reset_destination_address()
  717. { this->destination_address_ = this->invalid_address; }
  718. // Get offset of code stub. For Reloc_stub, it is the offset from the
  719. // beginning of its containing stub table; for Erratum_stub, it is the offset
  720. // from the end of reloc_stubs.
  721. section_offset_type
  722. offset() const
  723. {
  724. gold_assert(this->offset_ != this->invalid_offset);
  725. return this->offset_;
  726. }
  727. // Set stub offset.
  728. void
  729. set_offset(section_offset_type offset)
  730. { this->offset_ = offset; }
  731. // Return the stub insn.
  732. const Insntype*
  733. insns() const
  734. { return this->stub_template()->insns; }
  735. // Return num of stub insns.
  736. unsigned int
  737. insn_num() const
  738. { return this->stub_template()->insn_num; }
  739. // Get size of the stub.
  740. int
  741. stub_size() const
  742. {
  743. return this->insn_num() *
  744. AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
  745. }
  746. // Write stub to output file.
  747. void
  748. write(unsigned char* view, section_size_type view_size)
  749. { this->do_write(view, view_size); }
  750. protected:
  751. // Abstract method to be implemented by sub-classes.
  752. virtual void
  753. do_write(unsigned char*, section_size_type) = 0;
  754. private:
  755. // The last insn of a stub is a jump to destination insn. This field records
  756. // the destination address.
  757. AArch64_address destination_address_;
  758. // The stub offset. Note this has difference interpretations between an
  759. // Reloc_stub and an Erratum_stub. For Reloc_stub this is the offset from the
  760. // beginning of the containing stub_table, whereas for Erratum_stub, this is
  761. // the offset from the end of reloc_stubs.
  762. section_offset_type offset_;
  763. // Stub type.
  764. const int type_;
  765. }; // End of "Stub_base".
  766. // Erratum stub class. An erratum stub differs from a reloc stub in that for
  767. // each erratum occurrence, we generate an erratum stub. We never share erratum
  768. // stubs, whereas for reloc stubs, different branch insns share a single reloc
  769. // stub as long as the branch targets are the same. (More to the point, reloc
  770. // stubs can be shared because they're used to reach a specific target, whereas
  771. // erratum stubs branch back to the original control flow.)
  772. template<int size, bool big_endian>
  773. class Erratum_stub : public Stub_base<size, big_endian>
  774. {
  775. public:
  776. typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
  777. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  778. typedef AArch64_insn_utilities<big_endian> Insn_utilities;
  779. typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
  780. static const int STUB_ADDR_ALIGN;
  781. static const Insntype invalid_insn = static_cast<Insntype>(-1);
  782. Erratum_stub(The_aarch64_relobj* relobj, int type,
  783. unsigned shndx, unsigned int sh_offset)
  784. : Stub_base<size, big_endian>(type), relobj_(relobj),
  785. shndx_(shndx), sh_offset_(sh_offset),
  786. erratum_insn_(invalid_insn),
  787. erratum_address_(this->invalid_address)
  788. {}
  789. ~Erratum_stub() {}
  790. // Return the object that contains the erratum.
  791. The_aarch64_relobj*
  792. relobj()
  793. { return this->relobj_; }
  794. // Get section index of the erratum.
  795. unsigned int
  796. shndx() const
  797. { return this->shndx_; }
  798. // Get section offset of the erratum.
  799. unsigned int
  800. sh_offset() const
  801. { return this->sh_offset_; }
  802. // Get the erratum insn. This is the insn located at erratum_insn_address.
  803. Insntype
  804. erratum_insn() const
  805. {
  806. gold_assert(this->erratum_insn_ != this->invalid_insn);
  807. return this->erratum_insn_;
  808. }
  809. // Set the insn that the erratum happens to.
  810. void
  811. set_erratum_insn(Insntype insn)
  812. { this->erratum_insn_ = insn; }
  813. // For 843419, the erratum insn is ld/st xt, [xn, #uimm], which may be a
  814. // relocation spot, in this case, the erratum_insn_ recorded at scanning phase
  815. // is no longer the one we want to write out to the stub, update erratum_insn_
  816. // with relocated version. Also note that in this case xn must not be "PC", so
  817. // it is safe to move the erratum insn from the origin place to the stub. For
  818. // 835769, the erratum insn is multiply-accumulate insn, which could not be a
  819. // relocation spot (assertion added though).
  820. void
  821. update_erratum_insn(Insntype insn)
  822. {
  823. gold_assert(this->erratum_insn_ != this->invalid_insn);
  824. switch (this->type())
  825. {
  826. case ST_E_843419:
  827. gold_assert(Insn_utilities::aarch64_ldst_uimm(insn));
  828. gold_assert(Insn_utilities::aarch64_ldst_uimm(this->erratum_insn()));
  829. gold_assert(Insn_utilities::aarch64_rd(insn) ==
  830. Insn_utilities::aarch64_rd(this->erratum_insn()));
  831. gold_assert(Insn_utilities::aarch64_rn(insn) ==
  832. Insn_utilities::aarch64_rn(this->erratum_insn()));
  833. // Update plain ld/st insn with relocated insn.
  834. this->erratum_insn_ = insn;
  835. break;
  836. case ST_E_835769:
  837. gold_assert(insn == this->erratum_insn());
  838. break;
  839. default:
  840. gold_unreachable();
  841. }
  842. }
  843. // Return the address where an erratum must be done.
  844. AArch64_address
  845. erratum_address() const
  846. {
  847. gold_assert(this->erratum_address_ != this->invalid_address);
  848. return this->erratum_address_;
  849. }
  850. // Set the address where an erratum must be done.
  851. void
  852. set_erratum_address(AArch64_address addr)
  853. { this->erratum_address_ = addr; }
  854. // Later relaxation passes of may alter the recorded erratum and destination
  855. // address. Given an up to date output section address of shidx_ in
  856. // relobj_ we can derive the erratum_address and destination address.
  857. void
  858. update_erratum_address(AArch64_address output_section_addr)
  859. {
  860. const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
  861. AArch64_address updated_addr = output_section_addr + this->sh_offset_;
  862. this->set_erratum_address(updated_addr);
  863. this->set_destination_address(updated_addr + BPI);
  864. }
  865. // Comparator used to group Erratum_stubs in a set by (obj, shndx,
  866. // sh_offset). We do not include 'type' in the calculation, because there is
  867. // at most one stub type at (obj, shndx, sh_offset).
  868. bool
  869. operator<(const Erratum_stub<size, big_endian>& k) const
  870. {
  871. if (this == &k)
  872. return false;
  873. // We group stubs by relobj.
  874. if (this->relobj_ != k.relobj_)
  875. return this->relobj_ < k.relobj_;
  876. // Then by section index.
  877. if (this->shndx_ != k.shndx_)
  878. return this->shndx_ < k.shndx_;
  879. // Lastly by section offset.
  880. return this->sh_offset_ < k.sh_offset_;
  881. }
  882. void
  883. invalidate_erratum_stub()
  884. {
  885. gold_assert(this->erratum_insn_ != invalid_insn);
  886. this->erratum_insn_ = invalid_insn;
  887. }
  888. bool
  889. is_invalidated_erratum_stub()
  890. { return this->erratum_insn_ == invalid_insn; }
  891. protected:
  892. virtual void
  893. do_write(unsigned char*, section_size_type);
  894. private:
  895. // The object that needs to be fixed.
  896. The_aarch64_relobj* relobj_;
  897. // The shndx in the object that needs to be fixed.
  898. const unsigned int shndx_;
  899. // The section offset in the obejct that needs to be fixed.
  900. const unsigned int sh_offset_;
  901. // The insn to be fixed.
  902. Insntype erratum_insn_;
  903. // The address of the above insn.
  904. AArch64_address erratum_address_;
  905. }; // End of "Erratum_stub".
  906. // Erratum sub class to wrap additional info needed by 843419. In fixing this
  907. // erratum, we may choose to replace 'adrp' with 'adr', in this case, we need
  908. // adrp's code position (two or three insns before erratum insn itself).
  909. template<int size, bool big_endian>
  910. class E843419_stub : public Erratum_stub<size, big_endian>
  911. {
  912. public:
  913. typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
  914. E843419_stub(AArch64_relobj<size, big_endian>* relobj,
  915. unsigned int shndx, unsigned int sh_offset,
  916. unsigned int adrp_sh_offset)
  917. : Erratum_stub<size, big_endian>(relobj, ST_E_843419, shndx, sh_offset),
  918. adrp_sh_offset_(adrp_sh_offset)
  919. {}
  920. unsigned int
  921. adrp_sh_offset() const
  922. { return this->adrp_sh_offset_; }
  923. private:
  924. // Section offset of "adrp". (We do not need a "adrp_shndx_" field, because we
  925. // can obtain it from its parent.)
  926. const unsigned int adrp_sh_offset_;
  927. };
  928. template<int size, bool big_endian>
  929. const int Erratum_stub<size, big_endian>::STUB_ADDR_ALIGN = 4;
  930. // Comparator used in set definition.
  931. template<int size, bool big_endian>
  932. struct Erratum_stub_less
  933. {
  934. bool
  935. operator()(const Erratum_stub<size, big_endian>* s1,
  936. const Erratum_stub<size, big_endian>* s2) const
  937. { return *s1 < *s2; }
  938. };
  939. // Erratum_stub implementation for writing stub to output file.
  940. template<int size, bool big_endian>
  941. void
  942. Erratum_stub<size, big_endian>::do_write(unsigned char* view, section_size_type)
  943. {
  944. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  945. const Insntype* insns = this->insns();
  946. uint32_t num_insns = this->insn_num();
  947. Insntype* ip = reinterpret_cast<Insntype*>(view);
  948. // For current implemented erratum 843419 and 835769, the first insn in the
  949. // stub is always a copy of the problematic insn (in 843419, the mem access
  950. // insn, in 835769, the mac insn), followed by a jump-back.
  951. elfcpp::Swap<32, big_endian>::writeval(ip, this->erratum_insn());
  952. for (uint32_t i = 1; i < num_insns; ++i)
  953. elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
  954. }
  955. // Reloc stub class.
  956. template<int size, bool big_endian>
  957. class Reloc_stub : public Stub_base<size, big_endian>
  958. {
  959. public:
  960. typedef Reloc_stub<size, big_endian> This;
  961. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  962. // Branch range. This is used to calculate the section group size, as well as
  963. // determine whether a stub is needed.
  964. static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
  965. static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
  966. // Constant used to determine if an offset fits in the adrp instruction
  967. // encoding.
  968. static const int MAX_ADRP_IMM = (1 << 20) - 1;
  969. static const int MIN_ADRP_IMM = -(1 << 20);
  970. static const int BYTES_PER_INSN = 4;
  971. static const int STUB_ADDR_ALIGN;
  972. // Determine whether the offset fits in the jump/branch instruction.
  973. static bool
  974. aarch64_valid_branch_offset_p(int64_t offset)
  975. { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
  976. // Determine whether the offset fits in the adrp immediate field.
  977. static bool
  978. aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
  979. {
  980. typedef AArch64_relocate_functions<size, big_endian> Reloc;
  981. int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
  982. return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
  983. }
  984. // Determine the stub type for a certain relocation or ST_NONE, if no stub is
  985. // needed.
  986. static int
  987. stub_type_for_reloc(unsigned int r_type, AArch64_address address,
  988. AArch64_address target);
  989. Reloc_stub(int type)
  990. : Stub_base<size, big_endian>(type)
  991. { }
  992. ~Reloc_stub()
  993. { }
  994. // The key class used to index the stub instance in the stub table's stub map.
  995. class Key
  996. {
  997. public:
  998. Key(int type, const Symbol* symbol, const Relobj* relobj,
  999. unsigned int r_sym, int32_t addend)
  1000. : type_(type), addend_(addend)
  1001. {
  1002. if (symbol != NULL)
  1003. {
  1004. this->r_sym_ = Reloc_stub::invalid_index;
  1005. this->u_.symbol = symbol;
  1006. }
  1007. else
  1008. {
  1009. gold_assert(relobj != NULL && r_sym != invalid_index);
  1010. this->r_sym_ = r_sym;
  1011. this->u_.relobj = relobj;
  1012. }
  1013. }
  1014. ~Key()
  1015. { }
  1016. // Return stub type.
  1017. int
  1018. type() const
  1019. { return this->type_; }
  1020. // Return the local symbol index or invalid_index.
  1021. unsigned int
  1022. r_sym() const
  1023. { return this->r_sym_; }
  1024. // Return the symbol if there is one.
  1025. const Symbol*
  1026. symbol() const
  1027. { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
  1028. // Return the relobj if there is one.
  1029. const Relobj*
  1030. relobj() const
  1031. { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
  1032. // Whether this equals to another key k.
  1033. bool
  1034. eq(const Key& k) const
  1035. {
  1036. return ((this->type_ == k.type_)
  1037. && (this->r_sym_ == k.r_sym_)
  1038. && ((this->r_sym_ != Reloc_stub::invalid_index)
  1039. ? (this->u_.relobj == k.u_.relobj)
  1040. : (this->u_.symbol == k.u_.symbol))
  1041. && (this->addend_ == k.addend_));
  1042. }
  1043. // Return a hash value.
  1044. size_t
  1045. hash_value() const
  1046. {
  1047. size_t name_hash_value = gold::string_hash<char>(
  1048. (this->r_sym_ != Reloc_stub::invalid_index)
  1049. ? this->u_.relobj->name().c_str()
  1050. : this->u_.symbol->name());
  1051. // We only have 4 stub types.
  1052. size_t stub_type_hash_value = 0x03 & this->type_;
  1053. return (name_hash_value
  1054. ^ stub_type_hash_value
  1055. ^ ((this->r_sym_ & 0x3fff) << 2)
  1056. ^ ((this->addend_ & 0xffff) << 16));
  1057. }
  1058. // Functors for STL associative containers.
  1059. struct hash
  1060. {
  1061. size_t
  1062. operator()(const Key& k) const
  1063. { return k.hash_value(); }
  1064. };
  1065. struct equal_to
  1066. {
  1067. bool
  1068. operator()(const Key& k1, const Key& k2) const
  1069. { return k1.eq(k2); }
  1070. };
  1071. private:
  1072. // Stub type.
  1073. const int type_;
  1074. // If this is a local symbol, this is the index in the defining object.
  1075. // Otherwise, it is invalid_index for a global symbol.
  1076. unsigned int r_sym_;
  1077. // If r_sym_ is an invalid index, this points to a global symbol.
  1078. // Otherwise, it points to a relobj. We used the unsized and target
  1079. // independent Symbol and Relobj classes instead of Sized_symbol<32> and
  1080. // Arm_relobj, in order to avoid making the stub class a template
  1081. // as most of the stub machinery is endianness-neutral. However, it
  1082. // may require a bit of casting done by users of this class.
  1083. union
  1084. {
  1085. const Symbol* symbol;
  1086. const Relobj* relobj;
  1087. } u_;
  1088. // Addend associated with a reloc.
  1089. int32_t addend_;
  1090. }; // End of inner class Reloc_stub::Key
  1091. protected:
  1092. // This may be overridden in the child class.
  1093. virtual void
  1094. do_write(unsigned char*, section_size_type);
  1095. private:
  1096. static const unsigned int invalid_index = static_cast<unsigned int>(-1);
  1097. }; // End of Reloc_stub
  1098. template<int size, bool big_endian>
  1099. const int Reloc_stub<size, big_endian>::STUB_ADDR_ALIGN = 4;
  1100. // Write data to output file.
  1101. template<int size, bool big_endian>
  1102. void
  1103. Reloc_stub<size, big_endian>::
  1104. do_write(unsigned char* view, section_size_type)
  1105. {
  1106. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  1107. const uint32_t* insns = this->insns();
  1108. uint32_t num_insns = this->insn_num();
  1109. Insntype* ip = reinterpret_cast<Insntype*>(view);
  1110. for (uint32_t i = 0; i < num_insns; ++i)
  1111. elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
  1112. }
  1113. // Determine the stub type for a certain relocation or ST_NONE, if no stub is
  1114. // needed.
  1115. template<int size, bool big_endian>
  1116. inline int
  1117. Reloc_stub<size, big_endian>::stub_type_for_reloc(
  1118. unsigned int r_type, AArch64_address location, AArch64_address dest)
  1119. {
  1120. int64_t branch_offset = 0;
  1121. switch(r_type)
  1122. {
  1123. case elfcpp::R_AARCH64_CALL26:
  1124. case elfcpp::R_AARCH64_JUMP26:
  1125. branch_offset = dest - location;
  1126. break;
  1127. default:
  1128. gold_unreachable();
  1129. }
  1130. if (aarch64_valid_branch_offset_p(branch_offset))
  1131. return ST_NONE;
  1132. if (aarch64_valid_for_adrp_p(location, dest))
  1133. return ST_ADRP_BRANCH;
  1134. // Always use PC-relative addressing in case of -shared or -pie.
  1135. if (parameters->options().output_is_position_independent())
  1136. return ST_LONG_BRANCH_PCREL;
  1137. // This saves 2 insns per stub, compared to ST_LONG_BRANCH_PCREL.
  1138. // But is only applicable to non-shared or non-pie.
  1139. return ST_LONG_BRANCH_ABS;
  1140. }
  1141. // A class to hold stubs for the ARM target. This contains 2 different types of
  1142. // stubs - reloc stubs and erratum stubs.
  1143. template<int size, bool big_endian>
  1144. class Stub_table : public Output_data
  1145. {
  1146. public:
  1147. typedef Target_aarch64<size, big_endian> The_target_aarch64;
  1148. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  1149. typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
  1150. typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
  1151. typedef Reloc_stub<size, big_endian> The_reloc_stub;
  1152. typedef typename The_reloc_stub::Key The_reloc_stub_key;
  1153. typedef Erratum_stub<size, big_endian> The_erratum_stub;
  1154. typedef Erratum_stub_less<size, big_endian> The_erratum_stub_less;
  1155. typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
  1156. typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
  1157. typedef Stub_table<size, big_endian> The_stub_table;
  1158. typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
  1159. The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
  1160. Reloc_stub_map;
  1161. typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
  1162. typedef Relocate_info<size, big_endian> The_relocate_info;
  1163. typedef std::set<The_erratum_stub*, The_erratum_stub_less> Erratum_stub_set;
  1164. typedef typename Erratum_stub_set::iterator Erratum_stub_set_iter;
  1165. Stub_table(The_aarch64_input_section* owner)
  1166. : Output_data(), owner_(owner), reloc_stubs_size_(0),
  1167. erratum_stubs_size_(0), prev_data_size_(0)
  1168. { }
  1169. ~Stub_table()
  1170. { }
  1171. The_aarch64_input_section*
  1172. owner() const
  1173. { return owner_; }
  1174. // Whether this stub table is empty.
  1175. bool
  1176. empty() const
  1177. { return reloc_stubs_.empty() && erratum_stubs_.empty(); }
  1178. // Return the current data size.
  1179. off_t
  1180. current_data_size() const
  1181. { return this->current_data_size_for_child(); }
  1182. // Add a STUB using KEY. The caller is responsible for avoiding addition
  1183. // if a STUB with the same key has already been added.
  1184. void
  1185. add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
  1186. // Add an erratum stub into the erratum stub set. The set is ordered by
  1187. // (relobj, shndx, sh_offset).
  1188. void
  1189. add_erratum_stub(The_erratum_stub* stub);
  1190. // Find if such erratum exists for any given (obj, shndx, sh_offset).
  1191. The_erratum_stub*
  1192. find_erratum_stub(The_aarch64_relobj* a64relobj,
  1193. unsigned int shndx, unsigned int sh_offset);
  1194. // Find all the erratums for a given input section. The return value is a pair
  1195. // of iterators [begin, end).
  1196. std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
  1197. find_erratum_stubs_for_input_section(The_aarch64_relobj* a64relobj,
  1198. unsigned int shndx);
  1199. // Compute the erratum stub address.
  1200. AArch64_address
  1201. erratum_stub_address(The_erratum_stub* stub) const
  1202. {
  1203. AArch64_address r = align_address(this->address() + this->reloc_stubs_size_,
  1204. The_erratum_stub::STUB_ADDR_ALIGN);
  1205. r += stub->offset();
  1206. return r;
  1207. }
  1208. // Finalize stubs. No-op here, just for completeness.
  1209. void
  1210. finalize_stubs()
  1211. { }
  1212. // Look up a relocation stub using KEY. Return NULL if there is none.
  1213. The_reloc_stub*
  1214. find_reloc_stub(The_reloc_stub_key& key)
  1215. {
  1216. Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
  1217. return (p != this->reloc_stubs_.end()) ? p->second : NULL;
  1218. }
  1219. // Relocate reloc stubs in this stub table. This does not relocate erratum stubs.
  1220. void
  1221. relocate_reloc_stubs(const The_relocate_info*,
  1222. The_target_aarch64*,
  1223. Output_section*,
  1224. unsigned char*,
  1225. AArch64_address,
  1226. section_size_type);
  1227. // Relocate an erratum stub.
  1228. void
  1229. relocate_erratum_stub(The_erratum_stub*, unsigned char*);
  1230. // Update data size at the end of a relaxation pass. Return true if data size
  1231. // is different from that of the previous relaxation pass.
  1232. bool
  1233. update_data_size_changed_p()
  1234. {
  1235. // No addralign changed here.
  1236. off_t s = align_address(this->reloc_stubs_size_,
  1237. The_erratum_stub::STUB_ADDR_ALIGN)
  1238. + this->erratum_stubs_size_;
  1239. bool changed = (s != this->prev_data_size_);
  1240. this->prev_data_size_ = s;
  1241. return changed;
  1242. }
  1243. protected:
  1244. // Write out section contents.
  1245. void
  1246. do_write(Output_file*);
  1247. // Return the required alignment.
  1248. uint64_t
  1249. do_addralign() const
  1250. {
  1251. return std::max(The_reloc_stub::STUB_ADDR_ALIGN,
  1252. The_erratum_stub::STUB_ADDR_ALIGN);
  1253. }
  1254. // Reset address and file offset.
  1255. void
  1256. do_reset_address_and_file_offset()
  1257. { this->set_current_data_size_for_child(this->prev_data_size_); }
  1258. // Set final data size.
  1259. void
  1260. set_final_data_size()
  1261. { this->set_data_size(this->current_data_size()); }
  1262. private:
  1263. // Relocate one reloc stub.
  1264. void
  1265. relocate_reloc_stub(The_reloc_stub*,
  1266. const The_relocate_info*,
  1267. The_target_aarch64*,
  1268. Output_section*,
  1269. unsigned char*,
  1270. AArch64_address,
  1271. section_size_type);
  1272. private:
  1273. // Owner of this stub table.
  1274. The_aarch64_input_section* owner_;
  1275. // The relocation stubs.
  1276. Reloc_stub_map reloc_stubs_;
  1277. // The erratum stubs.
  1278. Erratum_stub_set erratum_stubs_;
  1279. // Size of reloc stubs.
  1280. off_t reloc_stubs_size_;
  1281. // Size of erratum stubs.
  1282. off_t erratum_stubs_size_;
  1283. // data size of this in the previous pass.
  1284. off_t prev_data_size_;
  1285. }; // End of Stub_table
  1286. // Add an erratum stub into the erratum stub set. The set is ordered by
  1287. // (relobj, shndx, sh_offset).
  1288. template<int size, bool big_endian>
  1289. void
  1290. Stub_table<size, big_endian>::add_erratum_stub(The_erratum_stub* stub)
  1291. {
  1292. std::pair<Erratum_stub_set_iter, bool> ret =
  1293. this->erratum_stubs_.insert(stub);
  1294. gold_assert(ret.second);
  1295. this->erratum_stubs_size_ = align_address(
  1296. this->erratum_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
  1297. stub->set_offset(this->erratum_stubs_size_);
  1298. this->erratum_stubs_size_ += stub->stub_size();
  1299. }
  1300. // Find if such erratum exists for given (obj, shndx, sh_offset).
  1301. template<int size, bool big_endian>
  1302. Erratum_stub<size, big_endian>*
  1303. Stub_table<size, big_endian>::find_erratum_stub(
  1304. The_aarch64_relobj* a64relobj, unsigned int shndx, unsigned int sh_offset)
  1305. {
  1306. // A dummy object used as key to search in the set.
  1307. The_erratum_stub key(a64relobj, ST_NONE,
  1308. shndx, sh_offset);
  1309. Erratum_stub_set_iter i = this->erratum_stubs_.find(&key);
  1310. if (i != this->erratum_stubs_.end())
  1311. {
  1312. The_erratum_stub* stub(*i);
  1313. gold_assert(stub->erratum_insn() != 0);
  1314. return stub;
  1315. }
  1316. return NULL;
  1317. }
  1318. // Find all the errata for a given input section. The return value is a pair of
  1319. // iterators [begin, end).
  1320. template<int size, bool big_endian>
  1321. std::pair<typename Stub_table<size, big_endian>::Erratum_stub_set_iter,
  1322. typename Stub_table<size, big_endian>::Erratum_stub_set_iter>
  1323. Stub_table<size, big_endian>::find_erratum_stubs_for_input_section(
  1324. The_aarch64_relobj* a64relobj, unsigned int shndx)
  1325. {
  1326. typedef std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter> Result_pair;
  1327. Erratum_stub_set_iter start, end;
  1328. The_erratum_stub low_key(a64relobj, ST_NONE, shndx, 0);
  1329. start = this->erratum_stubs_.lower_bound(&low_key);
  1330. if (start == this->erratum_stubs_.end())
  1331. return Result_pair(this->erratum_stubs_.end(),
  1332. this->erratum_stubs_.end());
  1333. end = start;
  1334. while (end != this->erratum_stubs_.end() &&
  1335. (*end)->relobj() == a64relobj && (*end)->shndx() == shndx)
  1336. ++end;
  1337. return Result_pair(start, end);
  1338. }
  1339. // Add a STUB using KEY. The caller is responsible for avoiding addition
  1340. // if a STUB with the same key has already been added.
  1341. template<int size, bool big_endian>
  1342. void
  1343. Stub_table<size, big_endian>::add_reloc_stub(
  1344. The_reloc_stub* stub, const The_reloc_stub_key& key)
  1345. {
  1346. gold_assert(stub->type() == key.type());
  1347. this->reloc_stubs_[key] = stub;
  1348. // Assign stub offset early. We can do this because we never remove
  1349. // reloc stubs and they are in the beginning of the stub table.
  1350. this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
  1351. The_reloc_stub::STUB_ADDR_ALIGN);
  1352. stub->set_offset(this->reloc_stubs_size_);
  1353. this->reloc_stubs_size_ += stub->stub_size();
  1354. }
  1355. // Relocate an erratum stub.
  1356. template<int size, bool big_endian>
  1357. void
  1358. Stub_table<size, big_endian>::
  1359. relocate_erratum_stub(The_erratum_stub* estub,
  1360. unsigned char* view)
  1361. {
  1362. // Just for convenience.
  1363. const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
  1364. gold_assert(!estub->is_invalidated_erratum_stub());
  1365. AArch64_address stub_address = this->erratum_stub_address(estub);
  1366. // The address of "b" in the stub that is to be "relocated".
  1367. AArch64_address stub_b_insn_address;
  1368. // Branch offset that is to be filled in "b" insn.
  1369. int b_offset = 0;
  1370. switch (estub->type())
  1371. {
  1372. case ST_E_843419:
  1373. case ST_E_835769:
  1374. // The 1st insn of the erratum could be a relocation spot,
  1375. // in this case we need to fix it with
  1376. // "(*i)->erratum_insn()".
  1377. elfcpp::Swap<32, big_endian>::writeval(
  1378. view + (stub_address - this->address()),
  1379. estub->erratum_insn());
  1380. // For the erratum, the 2nd insn is a b-insn to be patched
  1381. // (relocated).
  1382. stub_b_insn_address = stub_address + 1 * BPI;
  1383. b_offset = estub->destination_address() - stub_b_insn_address;
  1384. AArch64_relocate_functions<size, big_endian>::construct_b(
  1385. view + (stub_b_insn_address - this->address()),
  1386. ((unsigned int)(b_offset)) & 0xfffffff);
  1387. break;
  1388. default:
  1389. gold_unreachable();
  1390. break;
  1391. }
  1392. estub->invalidate_erratum_stub();
  1393. }
  1394. // Relocate only reloc stubs in this stub table. This does not relocate erratum
  1395. // stubs.
  1396. template<int size, bool big_endian>
  1397. void
  1398. Stub_table<size, big_endian>::
  1399. relocate_reloc_stubs(const The_relocate_info* relinfo,
  1400. The_target_aarch64* target_aarch64,
  1401. Output_section* output_section,
  1402. unsigned char* view,
  1403. AArch64_address address,
  1404. section_size_type view_size)
  1405. {
  1406. // "view_size" is the total size of the stub_table.
  1407. gold_assert(address == this->address() &&
  1408. view_size == static_cast<section_size_type>(this->data_size()));
  1409. for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
  1410. p != this->reloc_stubs_.end(); ++p)
  1411. relocate_reloc_stub(p->second, relinfo, target_aarch64, output_section,
  1412. view, address, view_size);
  1413. }
  1414. // Relocate one reloc stub. This is a helper for
  1415. // Stub_table::relocate_reloc_stubs().
  1416. template<int size, bool big_endian>
  1417. void
  1418. Stub_table<size, big_endian>::
  1419. relocate_reloc_stub(The_reloc_stub* stub,
  1420. const The_relocate_info* relinfo,
  1421. The_target_aarch64* target_aarch64,
  1422. Output_section* output_section,
  1423. unsigned char* view,
  1424. AArch64_address address,
  1425. section_size_type view_size)
  1426. {
  1427. // "offset" is the offset from the beginning of the stub_table.
  1428. section_size_type offset = stub->offset();
  1429. section_size_type stub_size = stub->stub_size();
  1430. // "view_size" is the total size of the stub_table.
  1431. gold_assert(offset + stub_size <= view_size);
  1432. target_aarch64->relocate_reloc_stub(stub, relinfo, output_section,
  1433. view + offset, address + offset, view_size);
  1434. }
  1435. // Write out the stubs to file.
  1436. template<int size, bool big_endian>
  1437. void
  1438. Stub_table<size, big_endian>::do_write(Output_file* of)
  1439. {
  1440. off_t offset = this->offset();
  1441. const section_size_type oview_size =
  1442. convert_to_section_size_type(this->data_size());
  1443. unsigned char* const oview = of->get_output_view(offset, oview_size);
  1444. // Write relocation stubs.
  1445. for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
  1446. p != this->reloc_stubs_.end(); ++p)
  1447. {
  1448. The_reloc_stub* stub = p->second;
  1449. AArch64_address address = this->address() + stub->offset();
  1450. gold_assert(address ==
  1451. align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
  1452. stub->write(oview + stub->offset(), stub->stub_size());
  1453. }
  1454. // Write erratum stubs.
  1455. unsigned int erratum_stub_start_offset =
  1456. align_address(this->reloc_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
  1457. for (typename Erratum_stub_set::iterator p = this->erratum_stubs_.begin();
  1458. p != this->erratum_stubs_.end(); ++p)
  1459. {
  1460. The_erratum_stub* stub(*p);
  1461. stub->write(oview + erratum_stub_start_offset + stub->offset(),
  1462. stub->stub_size());
  1463. }
  1464. of->write_output_view(this->offset(), oview_size, oview);
  1465. }
  1466. // AArch64_relobj class.
  1467. template<int size, bool big_endian>
  1468. class AArch64_relobj : public Sized_relobj_file<size, big_endian>
  1469. {
  1470. public:
  1471. typedef AArch64_relobj<size, big_endian> This;
  1472. typedef Target_aarch64<size, big_endian> The_target_aarch64;
  1473. typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
  1474. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  1475. typedef Stub_table<size, big_endian> The_stub_table;
  1476. typedef Erratum_stub<size, big_endian> The_erratum_stub;
  1477. typedef typename The_stub_table::Erratum_stub_set_iter Erratum_stub_set_iter;
  1478. typedef std::vector<The_stub_table*> Stub_table_list;
  1479. static const AArch64_address invalid_address =
  1480. static_cast<AArch64_address>(-1);
  1481. AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
  1482. const typename elfcpp::Ehdr<size, big_endian>& ehdr)
  1483. : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
  1484. stub_tables_()
  1485. { }
  1486. ~AArch64_relobj()
  1487. { }
  1488. // Return the stub table of the SHNDX-th section if there is one.
  1489. The_stub_table*
  1490. stub_table(unsigned int shndx) const
  1491. {
  1492. gold_assert(shndx < this->stub_tables_.size());
  1493. return this->stub_tables_[shndx];
  1494. }
  1495. // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
  1496. void
  1497. set_stub_table(unsigned int shndx, The_stub_table* stub_table)
  1498. {
  1499. gold_assert(shndx < this->stub_tables_.size());
  1500. this->stub_tables_[shndx] = stub_table;
  1501. }
  1502. // Entrance to errata scanning.
  1503. void
  1504. scan_errata(unsigned int shndx,
  1505. const elfcpp::Shdr<size, big_endian>&,
  1506. Output_section*, const Symbol_table*,
  1507. The_target_aarch64*);
  1508. // Scan all relocation sections for stub generation.
  1509. void
  1510. scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
  1511. const Layout*);
  1512. // Whether a section is a scannable text section.
  1513. bool
  1514. text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
  1515. const Output_section*, const Symbol_table*);
  1516. // Convert regular input section with index SHNDX to a relaxed section.
  1517. void
  1518. convert_input_section_to_relaxed_section(unsigned shndx)
  1519. {
  1520. // The stubs have relocations and we need to process them after writing
  1521. // out the stubs. So relocation now must follow section write.
  1522. this->set_section_offset(shndx, -1ULL);
  1523. this->set_relocs_must_follow_section_writes();
  1524. }
  1525. // Structure for mapping symbol position.
  1526. struct Mapping_symbol_position
  1527. {
  1528. Mapping_symbol_position(unsigned int shndx, AArch64_address offset):
  1529. shndx_(shndx), offset_(offset)
  1530. {}
  1531. // "<" comparator used in ordered_map container.
  1532. bool
  1533. operator<(const Mapping_symbol_position& p) const
  1534. {
  1535. return (this->shndx_ < p.shndx_
  1536. || (this->shndx_ == p.shndx_ && this->offset_ < p.offset_));
  1537. }
  1538. // Section index.
  1539. unsigned int shndx_;
  1540. // Section offset.
  1541. AArch64_address offset_;
  1542. };
  1543. typedef std::map<Mapping_symbol_position, char> Mapping_symbol_info;
  1544. protected:
  1545. // Post constructor setup.
  1546. void
  1547. do_setup()
  1548. {
  1549. // Call parent's setup method.
  1550. Sized_relobj_file<size, big_endian>::do_setup();
  1551. // Initialize look-up tables.
  1552. this->stub_tables_.resize(this->shnum());
  1553. }
  1554. virtual void
  1555. do_relocate_sections(
  1556. const Symbol_table* symtab, const Layout* layout,
  1557. const unsigned char* pshdrs, Output_file* of,
  1558. typename Sized_relobj_file<size, big_endian>::Views* pviews);
  1559. // Count local symbols and (optionally) record mapping info.
  1560. virtual void
  1561. do_count_local_symbols(Stringpool_template<char>*,
  1562. Stringpool_template<char>*);
  1563. private:
  1564. // Fix all errata in the object, and for each erratum, relocate corresponding
  1565. // erratum stub.
  1566. void
  1567. fix_errata_and_relocate_erratum_stubs(
  1568. typename Sized_relobj_file<size, big_endian>::Views* pviews);
  1569. // Try to fix erratum 843419 in an optimized way. Return true if patch is
  1570. // applied.
  1571. bool
  1572. try_fix_erratum_843419_optimized(
  1573. The_erratum_stub*, AArch64_address,
  1574. typename Sized_relobj_file<size, big_endian>::View_size&);
  1575. // Whether a section needs to be scanned for relocation stubs.
  1576. bool
  1577. section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
  1578. const Relobj::Output_sections&,
  1579. const Symbol_table*, const unsigned char*);
  1580. // List of stub tables.
  1581. Stub_table_list stub_tables_;
  1582. // Mapping symbol information sorted by (section index, section_offset).
  1583. Mapping_symbol_info mapping_symbol_info_;
  1584. }; // End of AArch64_relobj
  1585. // Override to record mapping symbol information.
  1586. template<int size, bool big_endian>
  1587. void
  1588. AArch64_relobj<size, big_endian>::do_count_local_symbols(
  1589. Stringpool_template<char>* pool, Stringpool_template<char>* dynpool)
  1590. {
  1591. Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
  1592. // Only erratum-fixing work needs mapping symbols, so skip this time consuming
  1593. // processing if not fixing erratum.
  1594. if (!parameters->options().fix_cortex_a53_843419()
  1595. && !parameters->options().fix_cortex_a53_835769())
  1596. return;
  1597. const unsigned int loccount = this->local_symbol_count();
  1598. if (loccount == 0)
  1599. return;
  1600. // Read the symbol table section header.
  1601. const unsigned int symtab_shndx = this->symtab_shndx();
  1602. elfcpp::Shdr<size, big_endian>
  1603. symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
  1604. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  1605. // Read the local symbols.
  1606. const int sym_size =elfcpp::Elf_sizes<size>::sym_size;
  1607. gold_assert(loccount == symtabshdr.get_sh_info());
  1608. off_t locsize = loccount * sym_size;
  1609. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  1610. locsize, true, true);
  1611. // For mapping symbol processing, we need to read the symbol names.
  1612. unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
  1613. if (strtab_shndx >= this->shnum())
  1614. {
  1615. this->error(_("invalid symbol table name index: %u"), strtab_shndx);
  1616. return;
  1617. }
  1618. elfcpp::Shdr<size, big_endian>
  1619. strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
  1620. if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
  1621. {
  1622. this->error(_("symbol table name section has wrong type: %u"),
  1623. static_cast<unsigned int>(strtabshdr.get_sh_type()));
  1624. return;
  1625. }
  1626. const char* pnames =
  1627. reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
  1628. strtabshdr.get_sh_size(),
  1629. false, false));
  1630. // Skip the first dummy symbol.
  1631. psyms += sym_size;
  1632. typename Sized_relobj_file<size, big_endian>::Local_values*
  1633. plocal_values = this->local_values();
  1634. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  1635. {
  1636. elfcpp::Sym<size, big_endian> sym(psyms);
  1637. Symbol_value<size>& lv((*plocal_values)[i]);
  1638. AArch64_address input_value = lv.input_value();
  1639. // Check to see if this is a mapping symbol. AArch64 mapping symbols are
  1640. // defined in "ELF for the ARM 64-bit Architecture", Table 4-4, Mapping
  1641. // symbols.
  1642. // Mapping symbols could be one of the following 4 forms -
  1643. // a) $x
  1644. // b) $x.<any...>
  1645. // c) $d
  1646. // d) $d.<any...>
  1647. const char* sym_name = pnames + sym.get_st_name();
  1648. if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')
  1649. && (sym_name[2] == '\0' || sym_name[2] == '.'))
  1650. {
  1651. bool is_ordinary;
  1652. unsigned int input_shndx =
  1653. this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
  1654. gold_assert(is_ordinary);
  1655. Mapping_symbol_position msp(input_shndx, input_value);
  1656. // Insert mapping_symbol_info into map whose ordering is defined by
  1657. // (shndx, offset_within_section).
  1658. this->mapping_symbol_info_[msp] = sym_name[1];
  1659. }
  1660. }
  1661. }
  1662. // Fix all errata in the object and for each erratum, we relocate the
  1663. // corresponding erratum stub (by calling Stub_table::relocate_erratum_stub).
  1664. template<int size, bool big_endian>
  1665. void
  1666. AArch64_relobj<size, big_endian>::fix_errata_and_relocate_erratum_stubs(
  1667. typename Sized_relobj_file<size, big_endian>::Views* pviews)
  1668. {
  1669. typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
  1670. unsigned int shnum = this->shnum();
  1671. const Relobj::Output_sections& out_sections(this->output_sections());
  1672. for (unsigned int i = 1; i < shnum; ++i)
  1673. {
  1674. The_stub_table* stub_table = this->stub_table(i);
  1675. if (!stub_table)
  1676. continue;
  1677. std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
  1678. ipair(stub_table->find_erratum_stubs_for_input_section(this, i));
  1679. Erratum_stub_set_iter p = ipair.first, end = ipair.second;
  1680. typename Sized_relobj_file<size, big_endian>::View_size&
  1681. pview((*pviews)[i]);
  1682. AArch64_address view_offset = 0;
  1683. if (pview.is_input_output_view)
  1684. {
  1685. // In this case, write_sections has not added the output offset to
  1686. // the view's address, so we must do so. Currently this only happens
  1687. // for a relaxed section.
  1688. unsigned int index = this->adjust_shndx(i);
  1689. const Output_relaxed_input_section* poris =
  1690. out_sections[index]->find_relaxed_input_section(this, index);
  1691. gold_assert(poris != NULL);
  1692. view_offset = poris->address() - pview.address;
  1693. }
  1694. while (p != end)
  1695. {
  1696. The_erratum_stub* stub = *p;
  1697. // Double check data before fix.
  1698. gold_assert(pview.address + view_offset + stub->sh_offset()
  1699. == stub->erratum_address());
  1700. // Update previously recorded erratum insn with relocated
  1701. // version.
  1702. Insntype* ip =
  1703. reinterpret_cast<Insntype*>(
  1704. pview.view + view_offset + stub->sh_offset());
  1705. Insntype insn_to_fix = ip[0];
  1706. stub->update_erratum_insn(insn_to_fix);
  1707. // First try to see if erratum is 843419 and if it can be fixed
  1708. // without using branch-to-stub.
  1709. if (!try_fix_erratum_843419_optimized(stub, view_offset, pview))
  1710. {
  1711. // Replace the erratum insn with a branch-to-stub.
  1712. AArch64_address stub_address =
  1713. stub_table->erratum_stub_address(stub);
  1714. unsigned int b_offset = stub_address - stub->erratum_address();
  1715. AArch64_relocate_functions<size, big_endian>::construct_b(
  1716. pview.view + view_offset + stub->sh_offset(),
  1717. b_offset & 0xfffffff);
  1718. }
  1719. // Erratum fix is done (or skipped), continue to relocate erratum
  1720. // stub. Note, when erratum fix is skipped (either because we
  1721. // proactively change the code sequence or the code sequence is
  1722. // changed by relaxation, etc), we can still safely relocate the
  1723. // erratum stub, ignoring the fact the erratum could never be
  1724. // executed.
  1725. stub_table->relocate_erratum_stub(
  1726. stub,
  1727. pview.view + (stub_table->address() - pview.address));
  1728. // Next erratum stub.
  1729. ++p;
  1730. }
  1731. }
  1732. }
  1733. // This is an optimization for 843419. This erratum requires the sequence begin
  1734. // with 'adrp', when final value calculated by adrp fits in adr, we can just
  1735. // replace 'adrp' with 'adr', so we save 2 jumps per occurrence. (Note, however,
  1736. // in this case, we do not delete the erratum stub (too late to do so), it is
  1737. // merely generated without ever being called.)
  1738. template<int size, bool big_endian>
  1739. bool
  1740. AArch64_relobj<size, big_endian>::try_fix_erratum_843419_optimized(
  1741. The_erratum_stub* stub, AArch64_address view_offset,
  1742. typename Sized_relobj_file<size, big_endian>::View_size& pview)
  1743. {
  1744. if (stub->type() != ST_E_843419)
  1745. return false;
  1746. typedef AArch64_insn_utilities<big_endian> Insn_utilities;
  1747. typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
  1748. E843419_stub<size, big_endian>* e843419_stub =
  1749. reinterpret_cast<E843419_stub<size, big_endian>*>(stub);
  1750. AArch64_address pc =
  1751. pview.address + view_offset + e843419_stub->adrp_sh_offset();
  1752. unsigned int adrp_offset = e843419_stub->adrp_sh_offset ();
  1753. Insntype* adrp_view =
  1754. reinterpret_cast<Insntype*>(pview.view + view_offset + adrp_offset);
  1755. Insntype adrp_insn = adrp_view[0];
  1756. // If the instruction at adrp_sh_offset is "mrs R, tpidr_el0", it may come
  1757. // from IE -> LE relaxation etc. This is a side-effect of TLS relaxation that
  1758. // ADRP has been turned into MRS, there is no erratum risk anymore.
  1759. // Therefore, we return true to avoid doing unnecessary branch-to-stub.
  1760. if (Insn_utilities::is_mrs_tpidr_el0(adrp_insn))
  1761. return true;
  1762. // If the instruction at adrp_sh_offset is not ADRP and the instruction before
  1763. // it is "mrs R, tpidr_el0", it may come from LD -> LE relaxation etc.
  1764. // Like the above case, there is no erratum risk any more, we can safely
  1765. // return true.
  1766. if (!Insn_utilities::is_adrp(adrp_insn) && adrp_offset)
  1767. {
  1768. Insntype* prev_view =
  1769. reinterpret_cast<Insntype*>(
  1770. pview.view + view_offset + adrp_offset - 4);
  1771. Insntype prev_insn = prev_view[0];
  1772. if (Insn_utilities::is_mrs_tpidr_el0(prev_insn))
  1773. return true;
  1774. }
  1775. /* If we reach here, the first instruction must be ADRP. */
  1776. gold_assert(Insn_utilities::is_adrp(adrp_insn));
  1777. // Get adrp 33-bit signed imm value.
  1778. int64_t adrp_imm = Insn_utilities::
  1779. aarch64_adrp_decode_imm(adrp_insn);
  1780. // adrp - final value transferred to target register is calculated as:
  1781. // PC[11:0] = Zeros(12)
  1782. // adrp_dest_value = PC + adrp_imm;
  1783. int64_t adrp_dest_value = (pc & ~((1 << 12) - 1)) + adrp_imm;
  1784. // adr -final value transferred to target register is calucalted as:
  1785. // PC + adr_imm
  1786. // So we have:
  1787. // PC + adr_imm = adrp_dest_value
  1788. // ==>
  1789. // adr_imm = adrp_dest_value - PC
  1790. int64_t adr_imm = adrp_dest_value - pc;
  1791. // Check if imm fits in adr (21-bit signed).
  1792. if (-(1 << 20) <= adr_imm && adr_imm < (1 << 20))
  1793. {
  1794. // Convert 'adrp' into 'adr'.
  1795. Insntype adr_insn = adrp_insn & ((1u << 31) - 1);
  1796. adr_insn = Insn_utilities::
  1797. aarch64_adr_encode_imm(adr_insn, adr_imm);
  1798. elfcpp::Swap<32, big_endian>::writeval(adrp_view, adr_insn);
  1799. return true;
  1800. }
  1801. return false;
  1802. }
  1803. // Relocate sections.
  1804. template<int size, bool big_endian>
  1805. void
  1806. AArch64_relobj<size, big_endian>::do_relocate_sections(
  1807. const Symbol_table* symtab, const Layout* layout,
  1808. const unsigned char* pshdrs, Output_file* of,
  1809. typename Sized_relobj_file<size, big_endian>::Views* pviews)
  1810. {
  1811. // Relocate the section data.
  1812. this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
  1813. 1, this->shnum() - 1);
  1814. // We do not generate stubs if doing a relocatable link.
  1815. if (parameters->options().relocatable())
  1816. return;
  1817. // This part only relocates erratum stubs that belong to input sections of this
  1818. // object file.
  1819. if (parameters->options().fix_cortex_a53_843419()
  1820. || parameters->options().fix_cortex_a53_835769())
  1821. this->fix_errata_and_relocate_erratum_stubs(pviews);
  1822. Relocate_info<size, big_endian> relinfo;
  1823. relinfo.symtab = symtab;
  1824. relinfo.layout = layout;
  1825. relinfo.object = this;
  1826. // This part relocates all reloc stubs that are contained in stub_tables of
  1827. // this object file.
  1828. unsigned int shnum = this->shnum();
  1829. The_target_aarch64* target = The_target_aarch64::current_target();
  1830. for (unsigned int i = 1; i < shnum; ++i)
  1831. {
  1832. The_aarch64_input_section* aarch64_input_section =
  1833. target->find_aarch64_input_section(this, i);
  1834. if (aarch64_input_section != NULL
  1835. && aarch64_input_section->is_stub_table_owner()
  1836. && !aarch64_input_section->stub_table()->empty())
  1837. {
  1838. Output_section* os = this->output_section(i);
  1839. gold_assert(os != NULL);
  1840. relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
  1841. relinfo.reloc_shdr = NULL;
  1842. relinfo.data_shndx = i;
  1843. relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
  1844. typename Sized_relobj_file<size, big_endian>::View_size&
  1845. view_struct = (*pviews)[i];
  1846. gold_assert(view_struct.view != NULL);
  1847. The_stub_table* stub_table = aarch64_input_section->stub_table();
  1848. off_t offset = stub_table->address() - view_struct.address;
  1849. unsigned char* view = view_struct.view + offset;
  1850. AArch64_address address = stub_table->address();
  1851. section_size_type view_size = stub_table->data_size();
  1852. stub_table->relocate_reloc_stubs(&relinfo, target, os, view, address,
  1853. view_size);
  1854. }
  1855. }
  1856. }
  1857. // Determine if an input section is scannable for stub processing. SHDR is
  1858. // the header of the section and SHNDX is the section index. OS is the output
  1859. // section for the input section and SYMTAB is the global symbol table used to
  1860. // look up ICF information.
  1861. template<int size, bool big_endian>
  1862. bool
  1863. AArch64_relobj<size, big_endian>::text_section_is_scannable(
  1864. const elfcpp::Shdr<size, big_endian>& text_shdr,
  1865. unsigned int text_shndx,
  1866. const Output_section* os,
  1867. const Symbol_table* symtab)
  1868. {
  1869. // Skip any empty sections, unallocated sections or sections whose
  1870. // type are not SHT_PROGBITS.
  1871. if (text_shdr.get_sh_size() == 0
  1872. || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
  1873. || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
  1874. return false;
  1875. // Skip any discarded or ICF'ed sections.
  1876. if (os == NULL || symtab->is_section_folded(this, text_shndx))
  1877. return false;
  1878. // Skip exception frame.
  1879. if (strcmp(os->name(), ".eh_frame") == 0)
  1880. return false ;
  1881. gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
  1882. os->find_relaxed_input_section(this, text_shndx) != NULL);
  1883. return true;
  1884. }
  1885. // Determine if we want to scan the SHNDX-th section for relocation stubs.
  1886. // This is a helper for AArch64_relobj::scan_sections_for_stubs().
  1887. template<int size, bool big_endian>
  1888. bool
  1889. AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
  1890. const elfcpp::Shdr<size, big_endian>& shdr,
  1891. const Relobj::Output_sections& out_sections,
  1892. const Symbol_table* symtab,
  1893. const unsigned char* pshdrs)
  1894. {
  1895. unsigned int sh_type = shdr.get_sh_type();
  1896. if (sh_type != elfcpp::SHT_RELA)
  1897. return false;
  1898. // Ignore empty section.
  1899. off_t sh_size = shdr.get_sh_size();
  1900. if (sh_size == 0)
  1901. return false;
  1902. // Ignore reloc section with unexpected symbol table. The
  1903. // error will be reported in the final link.
  1904. if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
  1905. return false;
  1906. gold_assert(sh_type == elfcpp::SHT_RELA);
  1907. unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  1908. // Ignore reloc section with unexpected entsize or uneven size.
  1909. // The error will be reported in the final link.
  1910. if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
  1911. return false;
  1912. // Ignore reloc section with bad info. This error will be
  1913. // reported in the final link.
  1914. unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
  1915. if (text_shndx >= this->shnum())
  1916. return false;
  1917. const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  1918. const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
  1919. text_shndx * shdr_size);
  1920. return this->text_section_is_scannable(text_shdr, text_shndx,
  1921. out_sections[text_shndx], symtab);
  1922. }
  1923. // Scan section SHNDX for erratum 843419 and 835769.
  1924. template<int size, bool big_endian>
  1925. void
  1926. AArch64_relobj<size, big_endian>::scan_errata(
  1927. unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
  1928. Output_section* os, const Symbol_table* symtab,
  1929. The_target_aarch64* target)
  1930. {
  1931. if (shdr.get_sh_size() == 0
  1932. || (shdr.get_sh_flags() &
  1933. (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
  1934. || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
  1935. return;
  1936. if (!os || symtab->is_section_folded(this, shndx)) return;
  1937. AArch64_address output_offset = this->get_output_section_offset(shndx);
  1938. AArch64_address output_address;
  1939. if (output_offset != invalid_address)
  1940. output_address = os->address() + output_offset;
  1941. else
  1942. {
  1943. const Output_relaxed_input_section* poris =
  1944. os->find_relaxed_input_section(this, shndx);
  1945. if (!poris) return;
  1946. output_address = poris->address();
  1947. }
  1948. // Update the addresses in previously generated erratum stubs. Unlike when
  1949. // we scan relocations for stubs, if section addresses have changed due to
  1950. // other relaxations we are unlikely to scan the same erratum instances
  1951. // again.
  1952. The_stub_table* stub_table = this->stub_table(shndx);
  1953. if (stub_table)
  1954. {
  1955. std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
  1956. ipair(stub_table->find_erratum_stubs_for_input_section(this, shndx));
  1957. for (Erratum_stub_set_iter p = ipair.first; p != ipair.second; ++p)
  1958. (*p)->update_erratum_address(output_address);
  1959. }
  1960. section_size_type input_view_size = 0;
  1961. const unsigned char* input_view =
  1962. this->section_contents(shndx, &input_view_size, false);
  1963. Mapping_symbol_position section_start(shndx, 0);
  1964. // Find the first mapping symbol record within section shndx.
  1965. typename Mapping_symbol_info::const_iterator p =
  1966. this->mapping_symbol_info_.lower_bound(section_start);
  1967. while (p != this->mapping_symbol_info_.end() &&
  1968. p->first.shndx_ == shndx)
  1969. {
  1970. typename Mapping_symbol_info::const_iterator prev = p;
  1971. ++p;
  1972. if (prev->second == 'x')
  1973. {
  1974. section_size_type span_start =
  1975. convert_to_section_size_type(prev->first.offset_);
  1976. section_size_type span_end;
  1977. if (p != this->mapping_symbol_info_.end()
  1978. && p->first.shndx_ == shndx)
  1979. span_end = convert_to_section_size_type(p->first.offset_);
  1980. else
  1981. span_end = convert_to_section_size_type(shdr.get_sh_size());
  1982. // Here we do not share the scanning code of both errata. For 843419,
  1983. // only the last few insns of each page are examined, which is fast,
  1984. // whereas, for 835769, every insn pair needs to be checked.
  1985. if (parameters->options().fix_cortex_a53_843419())
  1986. target->scan_erratum_843419_span(
  1987. this, shndx, span_start, span_end,
  1988. const_cast<unsigned char*>(input_view), output_address);
  1989. if (parameters->options().fix_cortex_a53_835769())
  1990. target->scan_erratum_835769_span(
  1991. this, shndx, span_start, span_end,
  1992. const_cast<unsigned char*>(input_view), output_address);
  1993. }
  1994. }
  1995. }
  1996. // Scan relocations for stub generation.
  1997. template<int size, bool big_endian>
  1998. void
  1999. AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
  2000. The_target_aarch64* target,
  2001. const Symbol_table* symtab,
  2002. const Layout* layout)
  2003. {
  2004. unsigned int shnum = this->shnum();
  2005. const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  2006. // Read the section headers.
  2007. const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
  2008. shnum * shdr_size,
  2009. true, true);
  2010. // To speed up processing, we set up hash tables for fast lookup of
  2011. // input offsets to output addresses.
  2012. this->initialize_input_to_output_maps();
  2013. const Relobj::Output_sections& out_sections(this->output_sections());
  2014. Relocate_info<size, big_endian> relinfo;
  2015. relinfo.symtab = symtab;
  2016. relinfo.layout = layout;
  2017. relinfo.object = this;
  2018. // Do relocation stubs scanning.
  2019. const unsigned char* p = pshdrs + shdr_size;
  2020. for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
  2021. {
  2022. const elfcpp::Shdr<size, big_endian> shdr(p);
  2023. if (parameters->options().fix_cortex_a53_843419()
  2024. || parameters->options().fix_cortex_a53_835769())
  2025. scan_errata(i, shdr, out_sections[i], symtab, target);
  2026. if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
  2027. pshdrs))
  2028. {
  2029. unsigned int index = this->adjust_shndx(shdr.get_sh_info());
  2030. AArch64_address output_offset =
  2031. this->get_output_section_offset(index);
  2032. AArch64_address output_address;
  2033. if (output_offset != invalid_address)
  2034. {
  2035. output_address = out_sections[index]->address() + output_offset;
  2036. }
  2037. else
  2038. {
  2039. // Currently this only happens for a relaxed section.
  2040. const Output_relaxed_input_section* poris =
  2041. out_sections[index]->find_relaxed_input_section(this, index);
  2042. gold_assert(poris != NULL);
  2043. output_address = poris->address();
  2044. }
  2045. // Get the relocations.
  2046. const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
  2047. shdr.get_sh_size(),
  2048. true, false);
  2049. // Get the section contents.
  2050. section_size_type input_view_size = 0;
  2051. const unsigned char* input_view =
  2052. this->section_contents(index, &input_view_size, false);
  2053. relinfo.reloc_shndx = i;
  2054. relinfo.data_shndx = index;
  2055. unsigned int sh_type = shdr.get_sh_type();
  2056. unsigned int reloc_size;
  2057. gold_assert (sh_type == elfcpp::SHT_RELA);
  2058. reloc_size = elfcpp::Elf_sizes<size>::rela_size;
  2059. Output_section* os = out_sections[index];
  2060. target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
  2061. shdr.get_sh_size() / reloc_size,
  2062. os,
  2063. output_offset == invalid_address,
  2064. input_view, output_address,
  2065. input_view_size);
  2066. }
  2067. }
  2068. }
  2069. // A class to wrap an ordinary input section containing executable code.
  2070. template<int size, bool big_endian>
  2071. class AArch64_input_section : public Output_relaxed_input_section
  2072. {
  2073. public:
  2074. typedef Stub_table<size, big_endian> The_stub_table;
  2075. AArch64_input_section(Relobj* relobj, unsigned int shndx)
  2076. : Output_relaxed_input_section(relobj, shndx, 1),
  2077. stub_table_(NULL),
  2078. original_contents_(NULL), original_size_(0),
  2079. original_addralign_(1)
  2080. { }
  2081. ~AArch64_input_section()
  2082. { delete[] this->original_contents_; }
  2083. // Initialize.
  2084. void
  2085. init();
  2086. // Set the stub_table.
  2087. void
  2088. set_stub_table(The_stub_table* st)
  2089. { this->stub_table_ = st; }
  2090. // Whether this is a stub table owner.
  2091. bool
  2092. is_stub_table_owner() const
  2093. { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
  2094. // Return the original size of the section.
  2095. uint32_t
  2096. original_size() const
  2097. { return this->original_size_; }
  2098. // Return the stub table.
  2099. The_stub_table*
  2100. stub_table()
  2101. { return stub_table_; }
  2102. protected:
  2103. // Write out this input section.
  2104. void
  2105. do_write(Output_file*);
  2106. // Return required alignment of this.
  2107. uint64_t
  2108. do_addralign() const
  2109. {
  2110. if (this->is_stub_table_owner())
  2111. return std::max(this->stub_table_->addralign(),
  2112. static_cast<uint64_t>(this->original_addralign_));
  2113. else
  2114. return this->original_addralign_;
  2115. }
  2116. // Finalize data size.
  2117. void
  2118. set_final_data_size();
  2119. // Reset address and file offset.
  2120. void
  2121. do_reset_address_and_file_offset();
  2122. // Output offset.
  2123. bool
  2124. do_output_offset(const Relobj* object, unsigned int shndx,
  2125. section_offset_type offset,
  2126. section_offset_type* poutput) const
  2127. {
  2128. if ((object == this->relobj())
  2129. && (shndx == this->shndx())
  2130. && (offset >= 0)
  2131. && (offset <=
  2132. convert_types<section_offset_type, uint32_t>(this->original_size_)))
  2133. {
  2134. *poutput = offset;
  2135. return true;
  2136. }
  2137. else
  2138. return false;
  2139. }
  2140. private:
  2141. // Copying is not allowed.
  2142. AArch64_input_section(const AArch64_input_section&);
  2143. AArch64_input_section& operator=(const AArch64_input_section&);
  2144. // The relocation stubs.
  2145. The_stub_table* stub_table_;
  2146. // Original section contents. We have to make a copy here since the file
  2147. // containing the original section may not be locked when we need to access
  2148. // the contents.
  2149. unsigned char* original_contents_;
  2150. // Section size of the original input section.
  2151. uint32_t original_size_;
  2152. // Address alignment of the original input section.
  2153. uint32_t original_addralign_;
  2154. }; // End of AArch64_input_section
  2155. // Finalize data size.
  2156. template<int size, bool big_endian>
  2157. void
  2158. AArch64_input_section<size, big_endian>::set_final_data_size()
  2159. {
  2160. off_t off = convert_types<off_t, uint64_t>(this->original_size_);
  2161. if (this->is_stub_table_owner())
  2162. {
  2163. this->stub_table_->finalize_data_size();
  2164. off = align_address(off, this->stub_table_->addralign());
  2165. off += this->stub_table_->data_size();
  2166. }
  2167. this->set_data_size(off);
  2168. }
  2169. // Reset address and file offset.
  2170. template<int size, bool big_endian>
  2171. void
  2172. AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
  2173. {
  2174. // Size of the original input section contents.
  2175. off_t off = convert_types<off_t, uint64_t>(this->original_size_);
  2176. // If this is a stub table owner, account for the stub table size.
  2177. if (this->is_stub_table_owner())
  2178. {
  2179. The_stub_table* stub_table = this->stub_table_;
  2180. // Reset the stub table's address and file offset. The
  2181. // current data size for child will be updated after that.
  2182. stub_table_->reset_address_and_file_offset();
  2183. off = align_address(off, stub_table_->addralign());
  2184. off += stub_table->current_data_size();
  2185. }
  2186. this->set_current_data_size(off);
  2187. }
  2188. // Initialize an Arm_input_section.
  2189. template<int size, bool big_endian>
  2190. void
  2191. AArch64_input_section<size, big_endian>::init()
  2192. {
  2193. Relobj* relobj = this->relobj();
  2194. unsigned int shndx = this->shndx();
  2195. // We have to cache original size, alignment and contents to avoid locking
  2196. // the original file.
  2197. this->original_addralign_ =
  2198. convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
  2199. // This is not efficient but we expect only a small number of relaxed
  2200. // input sections for stubs.
  2201. section_size_type section_size;
  2202. const unsigned char* section_contents =
  2203. relobj->section_contents(shndx, &section_size, false);
  2204. this->original_size_ =
  2205. convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
  2206. gold_assert(this->original_contents_ == NULL);
  2207. this->original_contents_ = new unsigned char[section_size];
  2208. memcpy(this->original_contents_, section_contents, section_size);
  2209. // We want to make this look like the original input section after
  2210. // output sections are finalized.
  2211. Output_section* os = relobj->output_section(shndx);
  2212. off_t offset = relobj->output_section_offset(shndx);
  2213. gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
  2214. this->set_address(os->address() + offset);
  2215. this->set_file_offset(os->offset() + offset);
  2216. this->set_current_data_size(this->original_size_);
  2217. this->finalize_data_size();
  2218. }
  2219. // Write data to output file.
  2220. template<int size, bool big_endian>
  2221. void
  2222. AArch64_input_section<size, big_endian>::do_write(Output_file* of)
  2223. {
  2224. // We have to write out the original section content.
  2225. gold_assert(this->original_contents_ != NULL);
  2226. of->write(this->offset(), this->original_contents_,
  2227. this->original_size_);
  2228. // If this owns a stub table and it is not empty, write it.
  2229. if (this->is_stub_table_owner() && !this->stub_table_->empty())
  2230. this->stub_table_->write(of);
  2231. }
  2232. // Arm output section class. This is defined mainly to add a number of stub
  2233. // generation methods.
  2234. template<int size, bool big_endian>
  2235. class AArch64_output_section : public Output_section
  2236. {
  2237. public:
  2238. typedef Target_aarch64<size, big_endian> The_target_aarch64;
  2239. typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
  2240. typedef Stub_table<size, big_endian> The_stub_table;
  2241. typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
  2242. public:
  2243. AArch64_output_section(const char* name, elfcpp::Elf_Word type,
  2244. elfcpp::Elf_Xword flags)
  2245. : Output_section(name, type, flags)
  2246. { }
  2247. ~AArch64_output_section() {}
  2248. // Group input sections for stub generation.
  2249. void
  2250. group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
  2251. const Task*);
  2252. private:
  2253. typedef Output_section::Input_section Input_section;
  2254. typedef Output_section::Input_section_list Input_section_list;
  2255. // Create a stub group.
  2256. void
  2257. create_stub_group(Input_section_list::const_iterator,
  2258. Input_section_list::const_iterator,
  2259. Input_section_list::const_iterator,
  2260. The_target_aarch64*,
  2261. std::vector<Output_relaxed_input_section*>&,
  2262. const Task*);
  2263. }; // End of AArch64_output_section
  2264. // Create a stub group for input sections from FIRST to LAST. OWNER points to
  2265. // the input section that will be the owner of the stub table.
  2266. template<int size, bool big_endian> void
  2267. AArch64_output_section<size, big_endian>::create_stub_group(
  2268. Input_section_list::const_iterator first,
  2269. Input_section_list::const_iterator last,
  2270. Input_section_list::const_iterator owner,
  2271. The_target_aarch64* target,
  2272. std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
  2273. const Task* task)
  2274. {
  2275. // Currently we convert ordinary input sections into relaxed sections only
  2276. // at this point.
  2277. The_aarch64_input_section* input_section;
  2278. if (owner->is_relaxed_input_section())
  2279. gold_unreachable();
  2280. else
  2281. {
  2282. gold_assert(owner->is_input_section());
  2283. // Create a new relaxed input section. We need to lock the original
  2284. // file.
  2285. Task_lock_obj<Object> tl(task, owner->relobj());
  2286. input_section =
  2287. target->new_aarch64_input_section(owner->relobj(), owner->shndx());
  2288. new_relaxed_sections.push_back(input_section);
  2289. }
  2290. // Create a stub table.
  2291. The_stub_table* stub_table =
  2292. target->new_stub_table(input_section);
  2293. input_section->set_stub_table(stub_table);
  2294. Input_section_list::const_iterator p = first;
  2295. // Look for input sections or relaxed input sections in [first ... last].
  2296. do
  2297. {
  2298. if (p->is_input_section() || p->is_relaxed_input_section())
  2299. {
  2300. // The stub table information for input sections live
  2301. // in their objects.
  2302. The_aarch64_relobj* aarch64_relobj =
  2303. static_cast<The_aarch64_relobj*>(p->relobj());
  2304. aarch64_relobj->set_stub_table(p->shndx(), stub_table);
  2305. }
  2306. }
  2307. while (p++ != last);
  2308. }
  2309. // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
  2310. // stub groups. We grow a stub group by adding input section until the size is
  2311. // just below GROUP_SIZE. The last input section will be converted into a stub
  2312. // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
  2313. // after the stub table, effectively doubling the group size.
  2314. //
  2315. // This is similar to the group_sections() function in elf32-arm.c but is
  2316. // implemented differently.
  2317. template<int size, bool big_endian>
  2318. void AArch64_output_section<size, big_endian>::group_sections(
  2319. section_size_type group_size,
  2320. bool stubs_always_after_branch,
  2321. Target_aarch64<size, big_endian>* target,
  2322. const Task* task)
  2323. {
  2324. typedef enum
  2325. {
  2326. NO_GROUP,
  2327. FINDING_STUB_SECTION,
  2328. HAS_STUB_SECTION
  2329. } State;
  2330. std::vector<Output_relaxed_input_section*> new_relaxed_sections;
  2331. State state = NO_GROUP;
  2332. section_size_type off = 0;
  2333. section_size_type group_begin_offset = 0;
  2334. section_size_type group_end_offset = 0;
  2335. section_size_type stub_table_end_offset = 0;
  2336. Input_section_list::const_iterator group_begin =
  2337. this->input_sections().end();
  2338. Input_section_list::const_iterator stub_table =
  2339. this->input_sections().end();
  2340. Input_section_list::const_iterator group_end = this->input_sections().end();
  2341. for (Input_section_list::const_iterator p = this->input_sections().begin();
  2342. p != this->input_sections().end();
  2343. ++p)
  2344. {
  2345. section_size_type section_begin_offset =
  2346. align_address(off, p->addralign());
  2347. section_size_type section_end_offset =
  2348. section_begin_offset + p->data_size();
  2349. // Check to see if we should group the previously seen sections.
  2350. switch (state)
  2351. {
  2352. case NO_GROUP:
  2353. break;
  2354. case FINDING_STUB_SECTION:
  2355. // Adding this section makes the group larger than GROUP_SIZE.
  2356. if (section_end_offset - group_begin_offset >= group_size)
  2357. {
  2358. if (stubs_always_after_branch)
  2359. {
  2360. gold_assert(group_end != this->input_sections().end());
  2361. this->create_stub_group(group_begin, group_end, group_end,
  2362. target, new_relaxed_sections,
  2363. task);
  2364. state = NO_GROUP;
  2365. }
  2366. else
  2367. {
  2368. // Input sections up to stub_group_size bytes after the stub
  2369. // table can be handled by it too.
  2370. state = HAS_STUB_SECTION;
  2371. stub_table = group_end;
  2372. stub_table_end_offset = group_end_offset;
  2373. }
  2374. }
  2375. break;
  2376. case HAS_STUB_SECTION:
  2377. // Adding this section makes the post stub-section group larger
  2378. // than GROUP_SIZE.
  2379. gold_unreachable();
  2380. // NOT SUPPORTED YET. For completeness only.
  2381. if (section_end_offset - stub_table_end_offset >= group_size)
  2382. {
  2383. gold_assert(group_end != this->input_sections().end());
  2384. this->create_stub_group(group_begin, group_end, stub_table,
  2385. target, new_relaxed_sections, task);
  2386. state = NO_GROUP;
  2387. }
  2388. break;
  2389. default:
  2390. gold_unreachable();
  2391. }
  2392. // If we see an input section and currently there is no group, start
  2393. // a new one. Skip any empty sections. We look at the data size
  2394. // instead of calling p->relobj()->section_size() to avoid locking.
  2395. if ((p->is_input_section() || p->is_relaxed_input_section())
  2396. && (p->data_size() != 0))
  2397. {
  2398. if (state == NO_GROUP)
  2399. {
  2400. state = FINDING_STUB_SECTION;
  2401. group_begin = p;
  2402. group_begin_offset = section_begin_offset;
  2403. }
  2404. // Keep track of the last input section seen.
  2405. group_end = p;
  2406. group_end_offset = section_end_offset;
  2407. }
  2408. off = section_end_offset;
  2409. }
  2410. // Create a stub group for any ungrouped sections.
  2411. if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
  2412. {
  2413. gold_assert(group_end != this->input_sections().end());
  2414. this->create_stub_group(group_begin, group_end,
  2415. (state == FINDING_STUB_SECTION
  2416. ? group_end
  2417. : stub_table),
  2418. target, new_relaxed_sections, task);
  2419. }
  2420. if (!new_relaxed_sections.empty())
  2421. this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
  2422. // Update the section offsets
  2423. for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
  2424. {
  2425. The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
  2426. new_relaxed_sections[i]->relobj());
  2427. unsigned int shndx = new_relaxed_sections[i]->shndx();
  2428. // Tell AArch64_relobj that this input section is converted.
  2429. relobj->convert_input_section_to_relaxed_section(shndx);
  2430. }
  2431. } // End of AArch64_output_section::group_sections
  2432. AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
  2433. // The aarch64 target class.
  2434. // See the ABI at
  2435. // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
  2436. template<int size, bool big_endian>
  2437. class Target_aarch64 : public Sized_target<size, big_endian>
  2438. {
  2439. public:
  2440. typedef Target_aarch64<size, big_endian> This;
  2441. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
  2442. Reloc_section;
  2443. typedef Relocate_info<size, big_endian> The_relocate_info;
  2444. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  2445. typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
  2446. typedef Reloc_stub<size, big_endian> The_reloc_stub;
  2447. typedef Erratum_stub<size, big_endian> The_erratum_stub;
  2448. typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
  2449. typedef Stub_table<size, big_endian> The_stub_table;
  2450. typedef std::vector<The_stub_table*> Stub_table_list;
  2451. typedef typename Stub_table_list::iterator Stub_table_iterator;
  2452. typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
  2453. typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
  2454. typedef Unordered_map<Section_id,
  2455. AArch64_input_section<size, big_endian>*,
  2456. Section_id_hash> AArch64_input_section_map;
  2457. typedef AArch64_insn_utilities<big_endian> Insn_utilities;
  2458. const static int TCB_SIZE = size / 8 * 2;
  2459. Target_aarch64(const Target::Target_info* info = &aarch64_info)
  2460. : Sized_target<size, big_endian>(info),
  2461. got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
  2462. got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
  2463. rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
  2464. got_mod_index_offset_(-1U),
  2465. tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
  2466. stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
  2467. { }
  2468. // Scan the relocations to determine unreferenced sections for
  2469. // garbage collection.
  2470. void
  2471. gc_process_relocs(Symbol_table* symtab,
  2472. Layout* layout,
  2473. Sized_relobj_file<size, big_endian>* object,
  2474. unsigned int data_shndx,
  2475. unsigned int sh_type,
  2476. const unsigned char* prelocs,
  2477. size_t reloc_count,
  2478. Output_section* output_section,
  2479. bool needs_special_offset_handling,
  2480. size_t local_symbol_count,
  2481. const unsigned char* plocal_symbols);
  2482. // Scan the relocations to look for symbol adjustments.
  2483. void
  2484. scan_relocs(Symbol_table* symtab,
  2485. Layout* layout,
  2486. Sized_relobj_file<size, big_endian>* object,
  2487. unsigned int data_shndx,
  2488. unsigned int sh_type,
  2489. const unsigned char* prelocs,
  2490. size_t reloc_count,
  2491. Output_section* output_section,
  2492. bool needs_special_offset_handling,
  2493. size_t local_symbol_count,
  2494. const unsigned char* plocal_symbols);
  2495. // Finalize the sections.
  2496. void
  2497. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  2498. // Return the value to use for a dynamic which requires special
  2499. // treatment.
  2500. uint64_t
  2501. do_dynsym_value(const Symbol*) const;
  2502. // Relocate a section.
  2503. void
  2504. relocate_section(const Relocate_info<size, big_endian>*,
  2505. unsigned int sh_type,
  2506. const unsigned char* prelocs,
  2507. size_t reloc_count,
  2508. Output_section* output_section,
  2509. bool needs_special_offset_handling,
  2510. unsigned char* view,
  2511. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  2512. section_size_type view_size,
  2513. const Reloc_symbol_changes*);
  2514. // Scan the relocs during a relocatable link.
  2515. void
  2516. scan_relocatable_relocs(Symbol_table* symtab,
  2517. Layout* layout,
  2518. Sized_relobj_file<size, big_endian>* object,
  2519. unsigned int data_shndx,
  2520. unsigned int sh_type,
  2521. const unsigned char* prelocs,
  2522. size_t reloc_count,
  2523. Output_section* output_section,
  2524. bool needs_special_offset_handling,
  2525. size_t local_symbol_count,
  2526. const unsigned char* plocal_symbols,
  2527. Relocatable_relocs*);
  2528. // Scan the relocs for --emit-relocs.
  2529. void
  2530. emit_relocs_scan(Symbol_table* symtab,
  2531. Layout* layout,
  2532. Sized_relobj_file<size, big_endian>* object,
  2533. unsigned int data_shndx,
  2534. unsigned int sh_type,
  2535. const unsigned char* prelocs,
  2536. size_t reloc_count,
  2537. Output_section* output_section,
  2538. bool needs_special_offset_handling,
  2539. size_t local_symbol_count,
  2540. const unsigned char* plocal_syms,
  2541. Relocatable_relocs* rr);
  2542. // Relocate a section during a relocatable link.
  2543. void
  2544. relocate_relocs(
  2545. const Relocate_info<size, big_endian>*,
  2546. unsigned int sh_type,
  2547. const unsigned char* prelocs,
  2548. size_t reloc_count,
  2549. Output_section* output_section,
  2550. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  2551. unsigned char* view,
  2552. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  2553. section_size_type view_size,
  2554. unsigned char* reloc_view,
  2555. section_size_type reloc_view_size);
  2556. // Return the symbol index to use for a target specific relocation.
  2557. // The only target specific relocation is R_AARCH64_TLSDESC for a
  2558. // local symbol, which is an absolute reloc.
  2559. unsigned int
  2560. do_reloc_symbol_index(void*, unsigned int r_type) const
  2561. {
  2562. gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
  2563. return 0;
  2564. }
  2565. // Return the addend to use for a target specific relocation.
  2566. uint64_t
  2567. do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
  2568. // Return the PLT section.
  2569. uint64_t
  2570. do_plt_address_for_global(const Symbol* gsym) const
  2571. { return this->plt_section()->address_for_global(gsym); }
  2572. uint64_t
  2573. do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
  2574. { return this->plt_section()->address_for_local(relobj, symndx); }
  2575. // This function should be defined in targets that can use relocation
  2576. // types to determine (implemented in local_reloc_may_be_function_pointer
  2577. // and global_reloc_may_be_function_pointer)
  2578. // if a function's pointer is taken. ICF uses this in safe mode to only
  2579. // fold those functions whose pointer is defintely not taken.
  2580. bool
  2581. do_can_check_for_function_pointers() const
  2582. { return true; }
  2583. // Return the number of entries in the PLT.
  2584. unsigned int
  2585. plt_entry_count() const;
  2586. //Return the offset of the first non-reserved PLT entry.
  2587. unsigned int
  2588. first_plt_entry_offset() const;
  2589. // Return the size of each PLT entry.
  2590. unsigned int
  2591. plt_entry_size() const;
  2592. // Create a stub table.
  2593. The_stub_table*
  2594. new_stub_table(The_aarch64_input_section*);
  2595. // Create an aarch64 input section.
  2596. The_aarch64_input_section*
  2597. new_aarch64_input_section(Relobj*, unsigned int);
  2598. // Find an aarch64 input section instance for a given OBJ and SHNDX.
  2599. The_aarch64_input_section*
  2600. find_aarch64_input_section(Relobj*, unsigned int) const;
  2601. // Return the thread control block size.
  2602. unsigned int
  2603. tcb_size() const { return This::TCB_SIZE; }
  2604. // Scan a section for stub generation.
  2605. void
  2606. scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
  2607. const unsigned char*, size_t, Output_section*,
  2608. bool, const unsigned char*,
  2609. Address,
  2610. section_size_type);
  2611. // Scan a relocation section for stub.
  2612. template<int sh_type>
  2613. void
  2614. scan_reloc_section_for_stubs(
  2615. const The_relocate_info* relinfo,
  2616. const unsigned char* prelocs,
  2617. size_t reloc_count,
  2618. Output_section* output_section,
  2619. bool needs_special_offset_handling,
  2620. const unsigned char* view,
  2621. Address view_address,
  2622. section_size_type);
  2623. // Relocate a single reloc stub.
  2624. void
  2625. relocate_reloc_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
  2626. Output_section*, unsigned char*, Address,
  2627. section_size_type);
  2628. // Get the default AArch64 target.
  2629. static This*
  2630. current_target()
  2631. {
  2632. gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
  2633. && parameters->target().get_size() == size
  2634. && parameters->target().is_big_endian() == big_endian);
  2635. return static_cast<This*>(parameters->sized_target<size, big_endian>());
  2636. }
  2637. // Scan erratum 843419 for a part of a section.
  2638. void
  2639. scan_erratum_843419_span(
  2640. AArch64_relobj<size, big_endian>*,
  2641. unsigned int,
  2642. const section_size_type,
  2643. const section_size_type,
  2644. unsigned char*,
  2645. Address);
  2646. // Scan erratum 835769 for a part of a section.
  2647. void
  2648. scan_erratum_835769_span(
  2649. AArch64_relobj<size, big_endian>*,
  2650. unsigned int,
  2651. const section_size_type,
  2652. const section_size_type,
  2653. unsigned char*,
  2654. Address);
  2655. protected:
  2656. void
  2657. do_select_as_default_target()
  2658. {
  2659. gold_assert(aarch64_reloc_property_table == NULL);
  2660. aarch64_reloc_property_table = new AArch64_reloc_property_table();
  2661. }
  2662. // Add a new reloc argument, returning the index in the vector.
  2663. size_t
  2664. add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
  2665. unsigned int r_sym)
  2666. {
  2667. this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
  2668. return this->tlsdesc_reloc_info_.size() - 1;
  2669. }
  2670. virtual Output_data_plt_aarch64<size, big_endian>*
  2671. do_make_data_plt(Layout* layout,
  2672. Output_data_got_aarch64<size, big_endian>* got,
  2673. Output_data_space* got_plt,
  2674. Output_data_space* got_irelative)
  2675. {
  2676. return new Output_data_plt_aarch64_standard<size, big_endian>(
  2677. layout, got, got_plt, got_irelative);
  2678. }
  2679. // do_make_elf_object to override the same function in the base class.
  2680. Object*
  2681. do_make_elf_object(const std::string&, Input_file*, off_t,
  2682. const elfcpp::Ehdr<size, big_endian>&);
  2683. Output_data_plt_aarch64<size, big_endian>*
  2684. make_data_plt(Layout* layout,
  2685. Output_data_got_aarch64<size, big_endian>* got,
  2686. Output_data_space* got_plt,
  2687. Output_data_space* got_irelative)
  2688. {
  2689. return this->do_make_data_plt(layout, got, got_plt, got_irelative);
  2690. }
  2691. // We only need to generate stubs, and hence perform relaxation if we are
  2692. // not doing relocatable linking.
  2693. virtual bool
  2694. do_may_relax() const
  2695. { return !parameters->options().relocatable(); }
  2696. // Relaxation hook. This is where we do stub generation.
  2697. virtual bool
  2698. do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
  2699. void
  2700. group_sections(Layout* layout,
  2701. section_size_type group_size,
  2702. bool stubs_always_after_branch,
  2703. const Task* task);
  2704. void
  2705. scan_reloc_for_stub(const The_relocate_info*, unsigned int,
  2706. const Sized_symbol<size>*, unsigned int,
  2707. const Symbol_value<size>*,
  2708. typename elfcpp::Elf_types<size>::Elf_Swxword,
  2709. Address Elf_Addr);
  2710. // Make an output section.
  2711. Output_section*
  2712. do_make_output_section(const char* name, elfcpp::Elf_Word type,
  2713. elfcpp::Elf_Xword flags)
  2714. { return new The_aarch64_output_section(name, type, flags); }
  2715. private:
  2716. // The class which scans relocations.
  2717. class Scan
  2718. {
  2719. public:
  2720. Scan()
  2721. : issued_non_pic_error_(false)
  2722. { }
  2723. inline void
  2724. local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
  2725. Sized_relobj_file<size, big_endian>* object,
  2726. unsigned int data_shndx,
  2727. Output_section* output_section,
  2728. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  2729. const elfcpp::Sym<size, big_endian>& lsym,
  2730. bool is_discarded);
  2731. inline void
  2732. global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
  2733. Sized_relobj_file<size, big_endian>* object,
  2734. unsigned int data_shndx,
  2735. Output_section* output_section,
  2736. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  2737. Symbol* gsym);
  2738. inline bool
  2739. local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
  2740. Target_aarch64<size, big_endian>* ,
  2741. Sized_relobj_file<size, big_endian>* ,
  2742. unsigned int ,
  2743. Output_section* ,
  2744. const elfcpp::Rela<size, big_endian>& ,
  2745. unsigned int r_type,
  2746. const elfcpp::Sym<size, big_endian>&);
  2747. inline bool
  2748. global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
  2749. Target_aarch64<size, big_endian>* ,
  2750. Sized_relobj_file<size, big_endian>* ,
  2751. unsigned int ,
  2752. Output_section* ,
  2753. const elfcpp::Rela<size, big_endian>& ,
  2754. unsigned int r_type,
  2755. Symbol* gsym);
  2756. private:
  2757. static void
  2758. unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
  2759. unsigned int r_type);
  2760. static void
  2761. unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
  2762. unsigned int r_type, Symbol*);
  2763. inline bool
  2764. possible_function_pointer_reloc(unsigned int r_type);
  2765. void
  2766. check_non_pic(Relobj*, unsigned int r_type);
  2767. bool
  2768. reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
  2769. unsigned int r_type);
  2770. // Whether we have issued an error about a non-PIC compilation.
  2771. bool issued_non_pic_error_;
  2772. };
  2773. // The class which implements relocation.
  2774. class Relocate
  2775. {
  2776. public:
  2777. Relocate()
  2778. : skip_call_tls_get_addr_(false)
  2779. { }
  2780. ~Relocate()
  2781. { }
  2782. // Do a relocation. Return false if the caller should not issue
  2783. // any warnings about this relocation.
  2784. inline bool
  2785. relocate(const Relocate_info<size, big_endian>*, unsigned int,
  2786. Target_aarch64*, Output_section*, size_t, const unsigned char*,
  2787. const Sized_symbol<size>*, const Symbol_value<size>*,
  2788. unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
  2789. section_size_type);
  2790. private:
  2791. inline typename AArch64_relocate_functions<size, big_endian>::Status
  2792. relocate_tls(const Relocate_info<size, big_endian>*,
  2793. Target_aarch64<size, big_endian>*,
  2794. size_t,
  2795. const elfcpp::Rela<size, big_endian>&,
  2796. unsigned int r_type, const Sized_symbol<size>*,
  2797. const Symbol_value<size>*,
  2798. unsigned char*,
  2799. typename elfcpp::Elf_types<size>::Elf_Addr);
  2800. inline typename AArch64_relocate_functions<size, big_endian>::Status
  2801. tls_gd_to_le(
  2802. const Relocate_info<size, big_endian>*,
  2803. Target_aarch64<size, big_endian>*,
  2804. const elfcpp::Rela<size, big_endian>&,
  2805. unsigned int,
  2806. unsigned char*,
  2807. const Symbol_value<size>*);
  2808. inline typename AArch64_relocate_functions<size, big_endian>::Status
  2809. tls_ld_to_le(
  2810. const Relocate_info<size, big_endian>*,
  2811. Target_aarch64<size, big_endian>*,
  2812. const elfcpp::Rela<size, big_endian>&,
  2813. unsigned int,
  2814. unsigned char*,
  2815. const Symbol_value<size>*);
  2816. inline typename AArch64_relocate_functions<size, big_endian>::Status
  2817. tls_ie_to_le(
  2818. const Relocate_info<size, big_endian>*,
  2819. Target_aarch64<size, big_endian>*,
  2820. const elfcpp::Rela<size, big_endian>&,
  2821. unsigned int,
  2822. unsigned char*,
  2823. const Symbol_value<size>*);
  2824. inline typename AArch64_relocate_functions<size, big_endian>::Status
  2825. tls_desc_gd_to_le(
  2826. const Relocate_info<size, big_endian>*,
  2827. Target_aarch64<size, big_endian>*,
  2828. const elfcpp::Rela<size, big_endian>&,
  2829. unsigned int,
  2830. unsigned char*,
  2831. const Symbol_value<size>*);
  2832. inline typename AArch64_relocate_functions<size, big_endian>::Status
  2833. tls_desc_gd_to_ie(
  2834. const Relocate_info<size, big_endian>*,
  2835. Target_aarch64<size, big_endian>*,
  2836. const elfcpp::Rela<size, big_endian>&,
  2837. unsigned int,
  2838. unsigned char*,
  2839. const Symbol_value<size>*,
  2840. typename elfcpp::Elf_types<size>::Elf_Addr,
  2841. typename elfcpp::Elf_types<size>::Elf_Addr);
  2842. bool skip_call_tls_get_addr_;
  2843. }; // End of class Relocate
  2844. // Adjust TLS relocation type based on the options and whether this
  2845. // is a local symbol.
  2846. static tls::Tls_optimization
  2847. optimize_tls_reloc(bool is_final, int r_type);
  2848. // Get the GOT section, creating it if necessary.
  2849. Output_data_got_aarch64<size, big_endian>*
  2850. got_section(Symbol_table*, Layout*);
  2851. // Get the GOT PLT section.
  2852. Output_data_space*
  2853. got_plt_section() const
  2854. {
  2855. gold_assert(this->got_plt_ != NULL);
  2856. return this->got_plt_;
  2857. }
  2858. // Get the GOT section for TLSDESC entries.
  2859. Output_data_got<size, big_endian>*
  2860. got_tlsdesc_section() const
  2861. {
  2862. gold_assert(this->got_tlsdesc_ != NULL);
  2863. return this->got_tlsdesc_;
  2864. }
  2865. // Create the PLT section.
  2866. void
  2867. make_plt_section(Symbol_table* symtab, Layout* layout);
  2868. // Create a PLT entry for a global symbol.
  2869. void
  2870. make_plt_entry(Symbol_table*, Layout*, Symbol*);
  2871. // Create a PLT entry for a local STT_GNU_IFUNC symbol.
  2872. void
  2873. make_local_ifunc_plt_entry(Symbol_table*, Layout*,
  2874. Sized_relobj_file<size, big_endian>* relobj,
  2875. unsigned int local_sym_index);
  2876. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  2877. void
  2878. define_tls_base_symbol(Symbol_table*, Layout*);
  2879. // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
  2880. void
  2881. reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
  2882. // Create a GOT entry for the TLS module index.
  2883. unsigned int
  2884. got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  2885. Sized_relobj_file<size, big_endian>* object);
  2886. // Get the PLT section.
  2887. Output_data_plt_aarch64<size, big_endian>*
  2888. plt_section() const
  2889. {
  2890. gold_assert(this->plt_ != NULL);
  2891. return this->plt_;
  2892. }
  2893. // Helper method to create erratum stubs for ST_E_843419 and ST_E_835769. For
  2894. // ST_E_843419, we need an additional field for adrp offset.
  2895. void create_erratum_stub(
  2896. AArch64_relobj<size, big_endian>* relobj,
  2897. unsigned int shndx,
  2898. section_size_type erratum_insn_offset,
  2899. Address erratum_address,
  2900. typename Insn_utilities::Insntype erratum_insn,
  2901. int erratum_type,
  2902. unsigned int e843419_adrp_offset=0);
  2903. // Return whether this is a 3-insn erratum sequence.
  2904. bool is_erratum_843419_sequence(
  2905. typename elfcpp::Swap<32,big_endian>::Valtype insn1,
  2906. typename elfcpp::Swap<32,big_endian>::Valtype insn2,
  2907. typename elfcpp::Swap<32,big_endian>::Valtype insn3);
  2908. // Return whether this is a 835769 sequence.
  2909. // (Similarly implemented as in elfnn-aarch64.c.)
  2910. bool is_erratum_835769_sequence(
  2911. typename elfcpp::Swap<32,big_endian>::Valtype,
  2912. typename elfcpp::Swap<32,big_endian>::Valtype);
  2913. // Get the dynamic reloc section, creating it if necessary.
  2914. Reloc_section*
  2915. rela_dyn_section(Layout*);
  2916. // Get the section to use for TLSDESC relocations.
  2917. Reloc_section*
  2918. rela_tlsdesc_section(Layout*) const;
  2919. // Get the section to use for IRELATIVE relocations.
  2920. Reloc_section*
  2921. rela_irelative_section(Layout*);
  2922. // Add a potential copy relocation.
  2923. void
  2924. copy_reloc(Symbol_table* symtab, Layout* layout,
  2925. Sized_relobj_file<size, big_endian>* object,
  2926. unsigned int shndx, Output_section* output_section,
  2927. Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
  2928. {
  2929. unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
  2930. this->copy_relocs_.copy_reloc(symtab, layout,
  2931. symtab->get_sized_symbol<size>(sym),
  2932. object, shndx, output_section,
  2933. r_type, reloc.get_r_offset(),
  2934. reloc.get_r_addend(),
  2935. this->rela_dyn_section(layout));
  2936. }
  2937. // Information about this specific target which we pass to the
  2938. // general Target structure.
  2939. static const Target::Target_info aarch64_info;
  2940. // The types of GOT entries needed for this platform.
  2941. // These values are exposed to the ABI in an incremental link.
  2942. // Do not renumber existing values without changing the version
  2943. // number of the .gnu_incremental_inputs section.
  2944. enum Got_type
  2945. {
  2946. GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
  2947. GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
  2948. GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
  2949. GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
  2950. };
  2951. // This type is used as the argument to the target specific
  2952. // relocation routines. The only target specific reloc is
  2953. // R_AARCh64_TLSDESC against a local symbol.
  2954. struct Tlsdesc_info
  2955. {
  2956. Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
  2957. unsigned int a_r_sym)
  2958. : object(a_object), r_sym(a_r_sym)
  2959. { }
  2960. // The object in which the local symbol is defined.
  2961. Sized_relobj_file<size, big_endian>* object;
  2962. // The local symbol index in the object.
  2963. unsigned int r_sym;
  2964. };
  2965. // The GOT section.
  2966. Output_data_got_aarch64<size, big_endian>* got_;
  2967. // The PLT section.
  2968. Output_data_plt_aarch64<size, big_endian>* plt_;
  2969. // The GOT PLT section.
  2970. Output_data_space* got_plt_;
  2971. // The GOT section for IRELATIVE relocations.
  2972. Output_data_space* got_irelative_;
  2973. // The GOT section for TLSDESC relocations.
  2974. Output_data_got<size, big_endian>* got_tlsdesc_;
  2975. // The _GLOBAL_OFFSET_TABLE_ symbol.
  2976. Symbol* global_offset_table_;
  2977. // The dynamic reloc section.
  2978. Reloc_section* rela_dyn_;
  2979. // The section to use for IRELATIVE relocs.
  2980. Reloc_section* rela_irelative_;
  2981. // Relocs saved to avoid a COPY reloc.
  2982. Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
  2983. // Offset of the GOT entry for the TLS module index.
  2984. unsigned int got_mod_index_offset_;
  2985. // We handle R_AARCH64_TLSDESC against a local symbol as a target
  2986. // specific relocation. Here we store the object and local symbol
  2987. // index for the relocation.
  2988. std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
  2989. // True if the _TLS_MODULE_BASE_ symbol has been defined.
  2990. bool tls_base_symbol_defined_;
  2991. // List of stub_tables
  2992. Stub_table_list stub_tables_;
  2993. // Actual stub group size
  2994. section_size_type stub_group_size_;
  2995. AArch64_input_section_map aarch64_input_section_map_;
  2996. }; // End of Target_aarch64
  2997. template<>
  2998. const Target::Target_info Target_aarch64<64, false>::aarch64_info =
  2999. {
  3000. 64, // size
  3001. false, // is_big_endian
  3002. elfcpp::EM_AARCH64, // machine_code
  3003. false, // has_make_symbol
  3004. false, // has_resolve
  3005. false, // has_code_fill
  3006. false, // is_default_stack_executable
  3007. true, // can_icf_inline_merge_sections
  3008. '\0', // wrap_char
  3009. "/lib/ld.so.1", // program interpreter
  3010. 0x400000, // default_text_segment_address
  3011. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  3012. 0x1000, // common_pagesize (overridable by -z common-page-size)
  3013. false, // isolate_execinstr
  3014. 0, // rosegment_gap
  3015. elfcpp::SHN_UNDEF, // small_common_shndx
  3016. elfcpp::SHN_UNDEF, // large_common_shndx
  3017. 0, // small_common_section_flags
  3018. 0, // large_common_section_flags
  3019. NULL, // attributes_section
  3020. NULL, // attributes_vendor
  3021. "_start", // entry_symbol_name
  3022. 32, // hash_entry_size
  3023. elfcpp::SHT_PROGBITS, // unwind_section_type
  3024. };
  3025. template<>
  3026. const Target::Target_info Target_aarch64<32, false>::aarch64_info =
  3027. {
  3028. 32, // size
  3029. false, // is_big_endian
  3030. elfcpp::EM_AARCH64, // machine_code
  3031. false, // has_make_symbol
  3032. false, // has_resolve
  3033. false, // has_code_fill
  3034. false, // is_default_stack_executable
  3035. false, // can_icf_inline_merge_sections
  3036. '\0', // wrap_char
  3037. "/lib/ld.so.1", // program interpreter
  3038. 0x400000, // default_text_segment_address
  3039. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  3040. 0x1000, // common_pagesize (overridable by -z common-page-size)
  3041. false, // isolate_execinstr
  3042. 0, // rosegment_gap
  3043. elfcpp::SHN_UNDEF, // small_common_shndx
  3044. elfcpp::SHN_UNDEF, // large_common_shndx
  3045. 0, // small_common_section_flags
  3046. 0, // large_common_section_flags
  3047. NULL, // attributes_section
  3048. NULL, // attributes_vendor
  3049. "_start", // entry_symbol_name
  3050. 32, // hash_entry_size
  3051. elfcpp::SHT_PROGBITS, // unwind_section_type
  3052. };
  3053. template<>
  3054. const Target::Target_info Target_aarch64<64, true>::aarch64_info =
  3055. {
  3056. 64, // size
  3057. true, // is_big_endian
  3058. elfcpp::EM_AARCH64, // machine_code
  3059. false, // has_make_symbol
  3060. false, // has_resolve
  3061. false, // has_code_fill
  3062. false, // is_default_stack_executable
  3063. true, // can_icf_inline_merge_sections
  3064. '\0', // wrap_char
  3065. "/lib/ld.so.1", // program interpreter
  3066. 0x400000, // default_text_segment_address
  3067. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  3068. 0x1000, // common_pagesize (overridable by -z common-page-size)
  3069. false, // isolate_execinstr
  3070. 0, // rosegment_gap
  3071. elfcpp::SHN_UNDEF, // small_common_shndx
  3072. elfcpp::SHN_UNDEF, // large_common_shndx
  3073. 0, // small_common_section_flags
  3074. 0, // large_common_section_flags
  3075. NULL, // attributes_section
  3076. NULL, // attributes_vendor
  3077. "_start", // entry_symbol_name
  3078. 32, // hash_entry_size
  3079. elfcpp::SHT_PROGBITS, // unwind_section_type
  3080. };
  3081. template<>
  3082. const Target::Target_info Target_aarch64<32, true>::aarch64_info =
  3083. {
  3084. 32, // size
  3085. true, // is_big_endian
  3086. elfcpp::EM_AARCH64, // machine_code
  3087. false, // has_make_symbol
  3088. false, // has_resolve
  3089. false, // has_code_fill
  3090. false, // is_default_stack_executable
  3091. false, // can_icf_inline_merge_sections
  3092. '\0', // wrap_char
  3093. "/lib/ld.so.1", // program interpreter
  3094. 0x400000, // default_text_segment_address
  3095. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  3096. 0x1000, // common_pagesize (overridable by -z common-page-size)
  3097. false, // isolate_execinstr
  3098. 0, // rosegment_gap
  3099. elfcpp::SHN_UNDEF, // small_common_shndx
  3100. elfcpp::SHN_UNDEF, // large_common_shndx
  3101. 0, // small_common_section_flags
  3102. 0, // large_common_section_flags
  3103. NULL, // attributes_section
  3104. NULL, // attributes_vendor
  3105. "_start", // entry_symbol_name
  3106. 32, // hash_entry_size
  3107. elfcpp::SHT_PROGBITS, // unwind_section_type
  3108. };
  3109. // Get the GOT section, creating it if necessary.
  3110. template<int size, bool big_endian>
  3111. Output_data_got_aarch64<size, big_endian>*
  3112. Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
  3113. Layout* layout)
  3114. {
  3115. if (this->got_ == NULL)
  3116. {
  3117. gold_assert(symtab != NULL && layout != NULL);
  3118. // When using -z now, we can treat .got.plt as a relro section.
  3119. // Without -z now, it is modified after program startup by lazy
  3120. // PLT relocations.
  3121. bool is_got_plt_relro = parameters->options().now();
  3122. Output_section_order got_order = (is_got_plt_relro
  3123. ? ORDER_RELRO
  3124. : ORDER_RELRO_LAST);
  3125. Output_section_order got_plt_order = (is_got_plt_relro
  3126. ? ORDER_RELRO
  3127. : ORDER_NON_RELRO_FIRST);
  3128. // Layout of .got and .got.plt sections.
  3129. // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
  3130. // ...
  3131. // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
  3132. // .gotplt[1] reserved for ld.so (resolver)
  3133. // .gotplt[2] reserved
  3134. // Generate .got section.
  3135. this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
  3136. layout);
  3137. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  3138. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
  3139. this->got_, got_order, true);
  3140. // The first word of GOT is reserved for the address of .dynamic.
  3141. // We put 0 here now. The value will be replaced later in
  3142. // Output_data_got_aarch64::do_write.
  3143. this->got_->add_constant(0);
  3144. // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
  3145. // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
  3146. // even if there is a .got.plt section.
  3147. this->global_offset_table_ =
  3148. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  3149. Symbol_table::PREDEFINED,
  3150. this->got_,
  3151. 0, 0, elfcpp::STT_OBJECT,
  3152. elfcpp::STB_LOCAL,
  3153. elfcpp::STV_HIDDEN, 0,
  3154. false, false);
  3155. // Generate .got.plt section.
  3156. this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
  3157. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  3158. (elfcpp::SHF_ALLOC
  3159. | elfcpp::SHF_WRITE),
  3160. this->got_plt_, got_plt_order,
  3161. is_got_plt_relro);
  3162. // The first three entries are reserved.
  3163. this->got_plt_->set_current_data_size(
  3164. AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
  3165. // If there are any IRELATIVE relocations, they get GOT entries
  3166. // in .got.plt after the jump slot entries.
  3167. this->got_irelative_ = new Output_data_space(size / 8,
  3168. "** GOT IRELATIVE PLT");
  3169. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  3170. (elfcpp::SHF_ALLOC
  3171. | elfcpp::SHF_WRITE),
  3172. this->got_irelative_,
  3173. got_plt_order,
  3174. is_got_plt_relro);
  3175. // If there are any TLSDESC relocations, they get GOT entries in
  3176. // .got.plt after the jump slot and IRELATIVE entries.
  3177. this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
  3178. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  3179. (elfcpp::SHF_ALLOC
  3180. | elfcpp::SHF_WRITE),
  3181. this->got_tlsdesc_,
  3182. got_plt_order,
  3183. is_got_plt_relro);
  3184. if (!is_got_plt_relro)
  3185. {
  3186. // Those bytes can go into the relro segment.
  3187. layout->increase_relro(
  3188. AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
  3189. }
  3190. }
  3191. return this->got_;
  3192. }
  3193. // Get the dynamic reloc section, creating it if necessary.
  3194. template<int size, bool big_endian>
  3195. typename Target_aarch64<size, big_endian>::Reloc_section*
  3196. Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
  3197. {
  3198. if (this->rela_dyn_ == NULL)
  3199. {
  3200. gold_assert(layout != NULL);
  3201. this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
  3202. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  3203. elfcpp::SHF_ALLOC, this->rela_dyn_,
  3204. ORDER_DYNAMIC_RELOCS, false);
  3205. }
  3206. return this->rela_dyn_;
  3207. }
  3208. // Get the section to use for IRELATIVE relocs, creating it if
  3209. // necessary. These go in .rela.dyn, but only after all other dynamic
  3210. // relocations. They need to follow the other dynamic relocations so
  3211. // that they can refer to global variables initialized by those
  3212. // relocs.
  3213. template<int size, bool big_endian>
  3214. typename Target_aarch64<size, big_endian>::Reloc_section*
  3215. Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
  3216. {
  3217. if (this->rela_irelative_ == NULL)
  3218. {
  3219. // Make sure we have already created the dynamic reloc section.
  3220. this->rela_dyn_section(layout);
  3221. this->rela_irelative_ = new Reloc_section(false);
  3222. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  3223. elfcpp::SHF_ALLOC, this->rela_irelative_,
  3224. ORDER_DYNAMIC_RELOCS, false);
  3225. gold_assert(this->rela_dyn_->output_section()
  3226. == this->rela_irelative_->output_section());
  3227. }
  3228. return this->rela_irelative_;
  3229. }
  3230. // do_make_elf_object to override the same function in the base class. We need
  3231. // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
  3232. // store backend specific information. Hence we need to have our own ELF object
  3233. // creation.
  3234. template<int size, bool big_endian>
  3235. Object*
  3236. Target_aarch64<size, big_endian>::do_make_elf_object(
  3237. const std::string& name,
  3238. Input_file* input_file,
  3239. off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
  3240. {
  3241. int et = ehdr.get_e_type();
  3242. // ET_EXEC files are valid input for --just-symbols/-R,
  3243. // and we treat them as relocatable objects.
  3244. if (et == elfcpp::ET_EXEC && input_file->just_symbols())
  3245. return Sized_target<size, big_endian>::do_make_elf_object(
  3246. name, input_file, offset, ehdr);
  3247. else if (et == elfcpp::ET_REL)
  3248. {
  3249. AArch64_relobj<size, big_endian>* obj =
  3250. new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
  3251. obj->setup();
  3252. return obj;
  3253. }
  3254. else if (et == elfcpp::ET_DYN)
  3255. {
  3256. // Keep base implementation.
  3257. Sized_dynobj<size, big_endian>* obj =
  3258. new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
  3259. obj->setup();
  3260. return obj;
  3261. }
  3262. else
  3263. {
  3264. gold_error(_("%s: unsupported ELF file type %d"),
  3265. name.c_str(), et);
  3266. return NULL;
  3267. }
  3268. }
  3269. // Scan a relocation for stub generation.
  3270. template<int size, bool big_endian>
  3271. void
  3272. Target_aarch64<size, big_endian>::scan_reloc_for_stub(
  3273. const Relocate_info<size, big_endian>* relinfo,
  3274. unsigned int r_type,
  3275. const Sized_symbol<size>* gsym,
  3276. unsigned int r_sym,
  3277. const Symbol_value<size>* psymval,
  3278. typename elfcpp::Elf_types<size>::Elf_Swxword addend,
  3279. Address address)
  3280. {
  3281. const AArch64_relobj<size, big_endian>* aarch64_relobj =
  3282. static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
  3283. Symbol_value<size> symval;
  3284. if (gsym != NULL)
  3285. {
  3286. const AArch64_reloc_property* arp = aarch64_reloc_property_table->
  3287. get_reloc_property(r_type);
  3288. if (gsym->use_plt_offset(arp->reference_flags()))
  3289. {
  3290. // This uses a PLT, change the symbol value.
  3291. symval.set_output_value(this->plt_address_for_global(gsym));
  3292. psymval = &symval;
  3293. }
  3294. else if (gsym->is_undefined())
  3295. {
  3296. // There is no need to generate a stub symbol if the original symbol
  3297. // is undefined.
  3298. gold_debug(DEBUG_TARGET,
  3299. "stub: not creating a stub for undefined symbol %s in file %s",
  3300. gsym->name(), aarch64_relobj->name().c_str());
  3301. return;
  3302. }
  3303. }
  3304. // Get the symbol value.
  3305. typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
  3306. // Owing to pipelining, the PC relative branches below actually skip
  3307. // two instructions when the branch offset is 0.
  3308. Address destination = static_cast<Address>(-1);
  3309. switch (r_type)
  3310. {
  3311. case elfcpp::R_AARCH64_CALL26:
  3312. case elfcpp::R_AARCH64_JUMP26:
  3313. destination = value + addend;
  3314. break;
  3315. default:
  3316. gold_unreachable();
  3317. }
  3318. int stub_type = The_reloc_stub::
  3319. stub_type_for_reloc(r_type, address, destination);
  3320. if (stub_type == ST_NONE)
  3321. return;
  3322. The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
  3323. gold_assert(stub_table != NULL);
  3324. The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
  3325. The_reloc_stub* stub = stub_table->find_reloc_stub(key);
  3326. if (stub == NULL)
  3327. {
  3328. stub = new The_reloc_stub(stub_type);
  3329. stub_table->add_reloc_stub(stub, key);
  3330. }
  3331. stub->set_destination_address(destination);
  3332. } // End of Target_aarch64::scan_reloc_for_stub
  3333. // This function scans a relocation section for stub generation.
  3334. // The template parameter Relocate must be a class type which provides
  3335. // a single function, relocate(), which implements the machine
  3336. // specific part of a relocation.
  3337. // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
  3338. // SHT_REL or SHT_RELA.
  3339. // PRELOCS points to the relocation data. RELOC_COUNT is the number
  3340. // of relocs. OUTPUT_SECTION is the output section.
  3341. // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
  3342. // mapped to output offsets.
  3343. // VIEW is the section data, VIEW_ADDRESS is its memory address, and
  3344. // VIEW_SIZE is the size. These refer to the input section, unless
  3345. // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
  3346. // the output section.
  3347. template<int size, bool big_endian>
  3348. template<int sh_type>
  3349. void inline
  3350. Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
  3351. const Relocate_info<size, big_endian>* relinfo,
  3352. const unsigned char* prelocs,
  3353. size_t reloc_count,
  3354. Output_section* /*output_section*/,
  3355. bool /*needs_special_offset_handling*/,
  3356. const unsigned char* /*view*/,
  3357. Address view_address,
  3358. section_size_type)
  3359. {
  3360. typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
  3361. const int reloc_size =
  3362. Reloc_types<sh_type,size,big_endian>::reloc_size;
  3363. AArch64_relobj<size, big_endian>* object =
  3364. static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
  3365. unsigned int local_count = object->local_symbol_count();
  3366. gold::Default_comdat_behavior default_comdat_behavior;
  3367. Comdat_behavior comdat_behavior = CB_UNDETERMINED;
  3368. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  3369. {
  3370. Reltype reloc(prelocs);
  3371. typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
  3372. unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
  3373. unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
  3374. if (r_type != elfcpp::R_AARCH64_CALL26
  3375. && r_type != elfcpp::R_AARCH64_JUMP26)
  3376. continue;
  3377. section_offset_type offset =
  3378. convert_to_section_size_type(reloc.get_r_offset());
  3379. // Get the addend.
  3380. typename elfcpp::Elf_types<size>::Elf_Swxword addend =
  3381. reloc.get_r_addend();
  3382. const Sized_symbol<size>* sym;
  3383. Symbol_value<size> symval;
  3384. const Symbol_value<size> *psymval;
  3385. bool is_defined_in_discarded_section;
  3386. unsigned int shndx;
  3387. const Symbol* gsym = NULL;
  3388. if (r_sym < local_count)
  3389. {
  3390. sym = NULL;
  3391. psymval = object->local_symbol(r_sym);
  3392. // If the local symbol belongs to a section we are discarding,
  3393. // and that section is a debug section, try to find the
  3394. // corresponding kept section and map this symbol to its
  3395. // counterpart in the kept section. The symbol must not
  3396. // correspond to a section we are folding.
  3397. bool is_ordinary;
  3398. shndx = psymval->input_shndx(&is_ordinary);
  3399. is_defined_in_discarded_section =
  3400. (is_ordinary
  3401. && shndx != elfcpp::SHN_UNDEF
  3402. && !object->is_section_included(shndx)
  3403. && !relinfo->symtab->is_section_folded(object, shndx));
  3404. // We need to compute the would-be final value of this local
  3405. // symbol.
  3406. if (!is_defined_in_discarded_section)
  3407. {
  3408. typedef Sized_relobj_file<size, big_endian> ObjType;
  3409. if (psymval->is_section_symbol())
  3410. symval.set_is_section_symbol();
  3411. typename ObjType::Compute_final_local_value_status status =
  3412. object->compute_final_local_value(r_sym, psymval, &symval,
  3413. relinfo->symtab);
  3414. if (status == ObjType::CFLV_OK)
  3415. {
  3416. // Currently we cannot handle a branch to a target in
  3417. // a merged section. If this is the case, issue an error
  3418. // and also free the merge symbol value.
  3419. if (!symval.has_output_value())
  3420. {
  3421. const std::string& section_name =
  3422. object->section_name(shndx);
  3423. object->error(_("cannot handle branch to local %u "
  3424. "in a merged section %s"),
  3425. r_sym, section_name.c_str());
  3426. }
  3427. psymval = &symval;
  3428. }
  3429. else
  3430. {
  3431. // We cannot determine the final value.
  3432. continue;
  3433. }
  3434. }
  3435. }
  3436. else
  3437. {
  3438. gsym = object->global_symbol(r_sym);
  3439. gold_assert(gsym != NULL);
  3440. if (gsym->is_forwarder())
  3441. gsym = relinfo->symtab->resolve_forwards(gsym);
  3442. sym = static_cast<const Sized_symbol<size>*>(gsym);
  3443. if (sym->has_symtab_index() && sym->symtab_index() != -1U)
  3444. symval.set_output_symtab_index(sym->symtab_index());
  3445. else
  3446. symval.set_no_output_symtab_entry();
  3447. // We need to compute the would-be final value of this global
  3448. // symbol.
  3449. const Symbol_table* symtab = relinfo->symtab;
  3450. const Sized_symbol<size>* sized_symbol =
  3451. symtab->get_sized_symbol<size>(gsym);
  3452. Symbol_table::Compute_final_value_status status;
  3453. typename elfcpp::Elf_types<size>::Elf_Addr value =
  3454. symtab->compute_final_value<size>(sized_symbol, &status);
  3455. // Skip this if the symbol has not output section.
  3456. if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
  3457. continue;
  3458. symval.set_output_value(value);
  3459. if (gsym->type() == elfcpp::STT_TLS)
  3460. symval.set_is_tls_symbol();
  3461. else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  3462. symval.set_is_ifunc_symbol();
  3463. psymval = &symval;
  3464. is_defined_in_discarded_section =
  3465. (gsym->is_defined_in_discarded_section()
  3466. && gsym->is_undefined());
  3467. shndx = 0;
  3468. }
  3469. Symbol_value<size> symval2;
  3470. if (is_defined_in_discarded_section)
  3471. {
  3472. std::string name = object->section_name(relinfo->data_shndx);
  3473. if (comdat_behavior == CB_UNDETERMINED)
  3474. comdat_behavior = default_comdat_behavior.get(name.c_str());
  3475. if (comdat_behavior == CB_PRETEND)
  3476. {
  3477. bool found;
  3478. typename elfcpp::Elf_types<size>::Elf_Addr value =
  3479. object->map_to_kept_section(shndx, name, &found);
  3480. if (found)
  3481. symval2.set_output_value(value + psymval->input_value());
  3482. else
  3483. symval2.set_output_value(0);
  3484. }
  3485. else
  3486. {
  3487. if (comdat_behavior == CB_ERROR)
  3488. issue_discarded_error(relinfo, i, offset, r_sym, gsym);
  3489. symval2.set_output_value(0);
  3490. }
  3491. symval2.set_no_output_symtab_entry();
  3492. psymval = &symval2;
  3493. }
  3494. this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
  3495. addend, view_address + offset);
  3496. } // End of iterating relocs in a section
  3497. } // End of Target_aarch64::scan_reloc_section_for_stubs
  3498. // Scan an input section for stub generation.
  3499. template<int size, bool big_endian>
  3500. void
  3501. Target_aarch64<size, big_endian>::scan_section_for_stubs(
  3502. const Relocate_info<size, big_endian>* relinfo,
  3503. unsigned int sh_type,
  3504. const unsigned char* prelocs,
  3505. size_t reloc_count,
  3506. Output_section* output_section,
  3507. bool needs_special_offset_handling,
  3508. const unsigned char* view,
  3509. Address view_address,
  3510. section_size_type view_size)
  3511. {
  3512. gold_assert(sh_type == elfcpp::SHT_RELA);
  3513. this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
  3514. relinfo,
  3515. prelocs,
  3516. reloc_count,
  3517. output_section,
  3518. needs_special_offset_handling,
  3519. view,
  3520. view_address,
  3521. view_size);
  3522. }
  3523. // Relocate a single reloc stub.
  3524. template<int size, bool big_endian>
  3525. void Target_aarch64<size, big_endian>::
  3526. relocate_reloc_stub(The_reloc_stub* stub,
  3527. const The_relocate_info*,
  3528. Output_section*,
  3529. unsigned char* view,
  3530. Address address,
  3531. section_size_type)
  3532. {
  3533. typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
  3534. typedef typename The_reloc_functions::Status The_reloc_functions_status;
  3535. typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
  3536. Insntype* ip = reinterpret_cast<Insntype*>(view);
  3537. int insn_number = stub->insn_num();
  3538. const uint32_t* insns = stub->insns();
  3539. // Check the insns are really those stub insns.
  3540. for (int i = 0; i < insn_number; ++i)
  3541. {
  3542. Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
  3543. gold_assert(((uint32_t)insn == insns[i]));
  3544. }
  3545. Address dest = stub->destination_address();
  3546. switch(stub->type())
  3547. {
  3548. case ST_ADRP_BRANCH:
  3549. {
  3550. // 1st reloc is ADR_PREL_PG_HI21
  3551. The_reloc_functions_status status =
  3552. The_reloc_functions::adrp(view, dest, address);
  3553. // An error should never arise in the above step. If so, please
  3554. // check 'aarch64_valid_for_adrp_p'.
  3555. gold_assert(status == The_reloc_functions::STATUS_OKAY);
  3556. // 2nd reloc is ADD_ABS_LO12_NC
  3557. const AArch64_reloc_property* arp =
  3558. aarch64_reloc_property_table->get_reloc_property(
  3559. elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
  3560. gold_assert(arp != NULL);
  3561. status = The_reloc_functions::template
  3562. rela_general<32>(view + 4, dest, 0, arp);
  3563. // An error should never arise, it is an "_NC" relocation.
  3564. gold_assert(status == The_reloc_functions::STATUS_OKAY);
  3565. }
  3566. break;
  3567. case ST_LONG_BRANCH_ABS:
  3568. // 1st reloc is R_AARCH64_PREL64, at offset 8
  3569. elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
  3570. break;
  3571. case ST_LONG_BRANCH_PCREL:
  3572. {
  3573. // "PC" calculation is the 2nd insn in the stub.
  3574. uint64_t offset = dest - (address + 4);
  3575. // Offset is placed at offset 4 and 5.
  3576. elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
  3577. }
  3578. break;
  3579. default:
  3580. gold_unreachable();
  3581. }
  3582. }
  3583. // A class to handle the PLT data.
  3584. // This is an abstract base class that handles most of the linker details
  3585. // but does not know the actual contents of PLT entries. The derived
  3586. // classes below fill in those details.
  3587. template<int size, bool big_endian>
  3588. class Output_data_plt_aarch64 : public Output_section_data
  3589. {
  3590. public:
  3591. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
  3592. Reloc_section;
  3593. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  3594. Output_data_plt_aarch64(Layout* layout,
  3595. uint64_t addralign,
  3596. Output_data_got_aarch64<size, big_endian>* got,
  3597. Output_data_space* got_plt,
  3598. Output_data_space* got_irelative)
  3599. : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
  3600. got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
  3601. count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
  3602. { this->init(layout); }
  3603. // Initialize the PLT section.
  3604. void
  3605. init(Layout* layout);
  3606. // Add an entry to the PLT.
  3607. void
  3608. add_entry(Symbol_table*, Layout*, Symbol* gsym);
  3609. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
  3610. unsigned int
  3611. add_local_ifunc_entry(Symbol_table* symtab, Layout*,
  3612. Sized_relobj_file<size, big_endian>* relobj,
  3613. unsigned int local_sym_index);
  3614. // Add the relocation for a PLT entry.
  3615. void
  3616. add_relocation(Symbol_table*, Layout*, Symbol* gsym,
  3617. unsigned int got_offset);
  3618. // Add the reserved TLSDESC_PLT entry to the PLT.
  3619. void
  3620. reserve_tlsdesc_entry(unsigned int got_offset)
  3621. { this->tlsdesc_got_offset_ = got_offset; }
  3622. // Return true if a TLSDESC_PLT entry has been reserved.
  3623. bool
  3624. has_tlsdesc_entry() const
  3625. { return this->tlsdesc_got_offset_ != -1U; }
  3626. // Return the GOT offset for the reserved TLSDESC_PLT entry.
  3627. unsigned int
  3628. get_tlsdesc_got_offset() const
  3629. { return this->tlsdesc_got_offset_; }
  3630. // Return the PLT offset of the reserved TLSDESC_PLT entry.
  3631. unsigned int
  3632. get_tlsdesc_plt_offset() const
  3633. {
  3634. return (this->first_plt_entry_offset() +
  3635. (this->count_ + this->irelative_count_)
  3636. * this->get_plt_entry_size());
  3637. }
  3638. // Return the .rela.plt section data.
  3639. Reloc_section*
  3640. rela_plt()
  3641. { return this->rel_; }
  3642. // Return where the TLSDESC relocations should go.
  3643. Reloc_section*
  3644. rela_tlsdesc(Layout*);
  3645. // Return where the IRELATIVE relocations should go in the PLT
  3646. // relocations.
  3647. Reloc_section*
  3648. rela_irelative(Symbol_table*, Layout*);
  3649. // Return whether we created a section for IRELATIVE relocations.
  3650. bool
  3651. has_irelative_section() const
  3652. { return this->irelative_rel_ != NULL; }
  3653. // Return the number of PLT entries.
  3654. unsigned int
  3655. entry_count() const
  3656. { return this->count_ + this->irelative_count_; }
  3657. // Return the offset of the first non-reserved PLT entry.
  3658. unsigned int
  3659. first_plt_entry_offset() const
  3660. { return this->do_first_plt_entry_offset(); }
  3661. // Return the size of a PLT entry.
  3662. unsigned int
  3663. get_plt_entry_size() const
  3664. { return this->do_get_plt_entry_size(); }
  3665. // Return the reserved tlsdesc entry size.
  3666. unsigned int
  3667. get_plt_tlsdesc_entry_size() const
  3668. { return this->do_get_plt_tlsdesc_entry_size(); }
  3669. // Return the PLT address to use for a global symbol.
  3670. uint64_t
  3671. address_for_global(const Symbol*);
  3672. // Return the PLT address to use for a local symbol.
  3673. uint64_t
  3674. address_for_local(const Relobj*, unsigned int symndx);
  3675. protected:
  3676. // Fill in the first PLT entry.
  3677. void
  3678. fill_first_plt_entry(unsigned char* pov,
  3679. Address got_address,
  3680. Address plt_address)
  3681. { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
  3682. // Fill in a normal PLT entry.
  3683. void
  3684. fill_plt_entry(unsigned char* pov,
  3685. Address got_address,
  3686. Address plt_address,
  3687. unsigned int got_offset,
  3688. unsigned int plt_offset)
  3689. {
  3690. this->do_fill_plt_entry(pov, got_address, plt_address,
  3691. got_offset, plt_offset);
  3692. }
  3693. // Fill in the reserved TLSDESC PLT entry.
  3694. void
  3695. fill_tlsdesc_entry(unsigned char* pov,
  3696. Address gotplt_address,
  3697. Address plt_address,
  3698. Address got_base,
  3699. unsigned int tlsdesc_got_offset,
  3700. unsigned int plt_offset)
  3701. {
  3702. this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
  3703. tlsdesc_got_offset, plt_offset);
  3704. }
  3705. virtual unsigned int
  3706. do_first_plt_entry_offset() const = 0;
  3707. virtual unsigned int
  3708. do_get_plt_entry_size() const = 0;
  3709. virtual unsigned int
  3710. do_get_plt_tlsdesc_entry_size() const = 0;
  3711. virtual void
  3712. do_fill_first_plt_entry(unsigned char* pov,
  3713. Address got_addr,
  3714. Address plt_addr) = 0;
  3715. virtual void
  3716. do_fill_plt_entry(unsigned char* pov,
  3717. Address got_address,
  3718. Address plt_address,
  3719. unsigned int got_offset,
  3720. unsigned int plt_offset) = 0;
  3721. virtual void
  3722. do_fill_tlsdesc_entry(unsigned char* pov,
  3723. Address gotplt_address,
  3724. Address plt_address,
  3725. Address got_base,
  3726. unsigned int tlsdesc_got_offset,
  3727. unsigned int plt_offset) = 0;
  3728. void
  3729. do_adjust_output_section(Output_section* os);
  3730. // Write to a map file.
  3731. void
  3732. do_print_to_mapfile(Mapfile* mapfile) const
  3733. { mapfile->print_output_data(this, _("** PLT")); }
  3734. private:
  3735. // Set the final size.
  3736. void
  3737. set_final_data_size();
  3738. // Write out the PLT data.
  3739. void
  3740. do_write(Output_file*);
  3741. // The reloc section.
  3742. Reloc_section* rel_;
  3743. // The TLSDESC relocs, if necessary. These must follow the regular
  3744. // PLT relocs.
  3745. Reloc_section* tlsdesc_rel_;
  3746. // The IRELATIVE relocs, if necessary. These must follow the
  3747. // regular PLT relocations.
  3748. Reloc_section* irelative_rel_;
  3749. // The .got section.
  3750. Output_data_got_aarch64<size, big_endian>* got_;
  3751. // The .got.plt section.
  3752. Output_data_space* got_plt_;
  3753. // The part of the .got.plt section used for IRELATIVE relocs.
  3754. Output_data_space* got_irelative_;
  3755. // The number of PLT entries.
  3756. unsigned int count_;
  3757. // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
  3758. // follow the regular PLT entries.
  3759. unsigned int irelative_count_;
  3760. // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
  3761. // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
  3762. // indicates an offset is not allocated.
  3763. unsigned int tlsdesc_got_offset_;
  3764. };
  3765. // Initialize the PLT section.
  3766. template<int size, bool big_endian>
  3767. void
  3768. Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
  3769. {
  3770. this->rel_ = new Reloc_section(false);
  3771. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  3772. elfcpp::SHF_ALLOC, this->rel_,
  3773. ORDER_DYNAMIC_PLT_RELOCS, false);
  3774. }
  3775. template<int size, bool big_endian>
  3776. void
  3777. Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
  3778. Output_section* os)
  3779. {
  3780. os->set_entsize(this->get_plt_entry_size());
  3781. }
  3782. // Add an entry to the PLT.
  3783. template<int size, bool big_endian>
  3784. void
  3785. Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
  3786. Layout* layout, Symbol* gsym)
  3787. {
  3788. gold_assert(!gsym->has_plt_offset());
  3789. unsigned int* pcount;
  3790. unsigned int plt_reserved;
  3791. Output_section_data_build* got;
  3792. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  3793. && gsym->can_use_relative_reloc(false))
  3794. {
  3795. pcount = &this->irelative_count_;
  3796. plt_reserved = 0;
  3797. got = this->got_irelative_;
  3798. }
  3799. else
  3800. {
  3801. pcount = &this->count_;
  3802. plt_reserved = this->first_plt_entry_offset();
  3803. got = this->got_plt_;
  3804. }
  3805. gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
  3806. + plt_reserved);
  3807. ++*pcount;
  3808. section_offset_type got_offset = got->current_data_size();
  3809. // Every PLT entry needs a GOT entry which points back to the PLT
  3810. // entry (this will be changed by the dynamic linker, normally
  3811. // lazily when the function is called).
  3812. got->set_current_data_size(got_offset + size / 8);
  3813. // Every PLT entry needs a reloc.
  3814. this->add_relocation(symtab, layout, gsym, got_offset);
  3815. // Note that we don't need to save the symbol. The contents of the
  3816. // PLT are independent of which symbols are used. The symbols only
  3817. // appear in the relocations.
  3818. }
  3819. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
  3820. // the PLT offset.
  3821. template<int size, bool big_endian>
  3822. unsigned int
  3823. Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
  3824. Symbol_table* symtab,
  3825. Layout* layout,
  3826. Sized_relobj_file<size, big_endian>* relobj,
  3827. unsigned int local_sym_index)
  3828. {
  3829. unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
  3830. ++this->irelative_count_;
  3831. section_offset_type got_offset = this->got_irelative_->current_data_size();
  3832. // Every PLT entry needs a GOT entry which points back to the PLT
  3833. // entry.
  3834. this->got_irelative_->set_current_data_size(got_offset + size / 8);
  3835. // Every PLT entry needs a reloc.
  3836. Reloc_section* rela = this->rela_irelative(symtab, layout);
  3837. rela->add_symbolless_local_addend(relobj, local_sym_index,
  3838. elfcpp::R_AARCH64_IRELATIVE,
  3839. this->got_irelative_, got_offset, 0);
  3840. return plt_offset;
  3841. }
  3842. // Add the relocation for a PLT entry.
  3843. template<int size, bool big_endian>
  3844. void
  3845. Output_data_plt_aarch64<size, big_endian>::add_relocation(
  3846. Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
  3847. {
  3848. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  3849. && gsym->can_use_relative_reloc(false))
  3850. {
  3851. Reloc_section* rela = this->rela_irelative(symtab, layout);
  3852. rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
  3853. this->got_irelative_, got_offset, 0);
  3854. }
  3855. else
  3856. {
  3857. gsym->set_needs_dynsym_entry();
  3858. this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
  3859. got_offset, 0);
  3860. }
  3861. }
  3862. // Return where the TLSDESC relocations should go, creating it if
  3863. // necessary. These follow the JUMP_SLOT relocations.
  3864. template<int size, bool big_endian>
  3865. typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
  3866. Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
  3867. {
  3868. if (this->tlsdesc_rel_ == NULL)
  3869. {
  3870. this->tlsdesc_rel_ = new Reloc_section(false);
  3871. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  3872. elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
  3873. ORDER_DYNAMIC_PLT_RELOCS, false);
  3874. gold_assert(this->tlsdesc_rel_->output_section()
  3875. == this->rel_->output_section());
  3876. }
  3877. return this->tlsdesc_rel_;
  3878. }
  3879. // Return where the IRELATIVE relocations should go in the PLT. These
  3880. // follow the JUMP_SLOT and the TLSDESC relocations.
  3881. template<int size, bool big_endian>
  3882. typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
  3883. Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
  3884. Layout* layout)
  3885. {
  3886. if (this->irelative_rel_ == NULL)
  3887. {
  3888. // Make sure we have a place for the TLSDESC relocations, in
  3889. // case we see any later on.
  3890. this->rela_tlsdesc(layout);
  3891. this->irelative_rel_ = new Reloc_section(false);
  3892. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  3893. elfcpp::SHF_ALLOC, this->irelative_rel_,
  3894. ORDER_DYNAMIC_PLT_RELOCS, false);
  3895. gold_assert(this->irelative_rel_->output_section()
  3896. == this->rel_->output_section());
  3897. if (parameters->doing_static_link())
  3898. {
  3899. // A statically linked executable will only have a .rela.plt
  3900. // section to hold R_AARCH64_IRELATIVE relocs for
  3901. // STT_GNU_IFUNC symbols. The library will use these
  3902. // symbols to locate the IRELATIVE relocs at program startup
  3903. // time.
  3904. symtab->define_in_output_data("__rela_iplt_start", NULL,
  3905. Symbol_table::PREDEFINED,
  3906. this->irelative_rel_, 0, 0,
  3907. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  3908. elfcpp::STV_HIDDEN, 0, false, true);
  3909. symtab->define_in_output_data("__rela_iplt_end", NULL,
  3910. Symbol_table::PREDEFINED,
  3911. this->irelative_rel_, 0, 0,
  3912. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  3913. elfcpp::STV_HIDDEN, 0, true, true);
  3914. }
  3915. }
  3916. return this->irelative_rel_;
  3917. }
  3918. // Return the PLT address to use for a global symbol.
  3919. template<int size, bool big_endian>
  3920. uint64_t
  3921. Output_data_plt_aarch64<size, big_endian>::address_for_global(
  3922. const Symbol* gsym)
  3923. {
  3924. uint64_t offset = 0;
  3925. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  3926. && gsym->can_use_relative_reloc(false))
  3927. offset = (this->first_plt_entry_offset() +
  3928. this->count_ * this->get_plt_entry_size());
  3929. return this->address() + offset + gsym->plt_offset();
  3930. }
  3931. // Return the PLT address to use for a local symbol. These are always
  3932. // IRELATIVE relocs.
  3933. template<int size, bool big_endian>
  3934. uint64_t
  3935. Output_data_plt_aarch64<size, big_endian>::address_for_local(
  3936. const Relobj* object,
  3937. unsigned int r_sym)
  3938. {
  3939. return (this->address()
  3940. + this->first_plt_entry_offset()
  3941. + this->count_ * this->get_plt_entry_size()
  3942. + object->local_plt_offset(r_sym));
  3943. }
  3944. // Set the final size.
  3945. template<int size, bool big_endian>
  3946. void
  3947. Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
  3948. {
  3949. unsigned int count = this->count_ + this->irelative_count_;
  3950. unsigned int extra_size = 0;
  3951. if (this->has_tlsdesc_entry())
  3952. extra_size += this->get_plt_tlsdesc_entry_size();
  3953. this->set_data_size(this->first_plt_entry_offset()
  3954. + count * this->get_plt_entry_size()
  3955. + extra_size);
  3956. }
  3957. template<int size, bool big_endian>
  3958. class Output_data_plt_aarch64_standard :
  3959. public Output_data_plt_aarch64<size, big_endian>
  3960. {
  3961. public:
  3962. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  3963. Output_data_plt_aarch64_standard(
  3964. Layout* layout,
  3965. Output_data_got_aarch64<size, big_endian>* got,
  3966. Output_data_space* got_plt,
  3967. Output_data_space* got_irelative)
  3968. : Output_data_plt_aarch64<size, big_endian>(layout,
  3969. size == 32 ? 4 : 8,
  3970. got, got_plt,
  3971. got_irelative)
  3972. { }
  3973. protected:
  3974. // Return the offset of the first non-reserved PLT entry.
  3975. virtual unsigned int
  3976. do_first_plt_entry_offset() const
  3977. { return this->first_plt_entry_size; }
  3978. // Return the size of a PLT entry
  3979. virtual unsigned int
  3980. do_get_plt_entry_size() const
  3981. { return this->plt_entry_size; }
  3982. // Return the size of a tlsdesc entry
  3983. virtual unsigned int
  3984. do_get_plt_tlsdesc_entry_size() const
  3985. { return this->plt_tlsdesc_entry_size; }
  3986. virtual void
  3987. do_fill_first_plt_entry(unsigned char* pov,
  3988. Address got_address,
  3989. Address plt_address);
  3990. virtual void
  3991. do_fill_plt_entry(unsigned char* pov,
  3992. Address got_address,
  3993. Address plt_address,
  3994. unsigned int got_offset,
  3995. unsigned int plt_offset);
  3996. virtual void
  3997. do_fill_tlsdesc_entry(unsigned char* pov,
  3998. Address gotplt_address,
  3999. Address plt_address,
  4000. Address got_base,
  4001. unsigned int tlsdesc_got_offset,
  4002. unsigned int plt_offset);
  4003. private:
  4004. // The size of the first plt entry size.
  4005. static const int first_plt_entry_size = 32;
  4006. // The size of the plt entry size.
  4007. static const int plt_entry_size = 16;
  4008. // The size of the plt tlsdesc entry size.
  4009. static const int plt_tlsdesc_entry_size = 32;
  4010. // Template for the first PLT entry.
  4011. static const uint32_t first_plt_entry[first_plt_entry_size / 4];
  4012. // Template for subsequent PLT entries.
  4013. static const uint32_t plt_entry[plt_entry_size / 4];
  4014. // The reserved TLSDESC entry in the PLT for an executable.
  4015. static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
  4016. };
  4017. // The first entry in the PLT for an executable.
  4018. template<>
  4019. const uint32_t
  4020. Output_data_plt_aarch64_standard<32, false>::
  4021. first_plt_entry[first_plt_entry_size / 4] =
  4022. {
  4023. 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
  4024. 0x90000010, /* adrp x16, PLT_GOT+0x8 */
  4025. 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
  4026. 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
  4027. 0xd61f0220, /* br x17 */
  4028. 0xd503201f, /* nop */
  4029. 0xd503201f, /* nop */
  4030. 0xd503201f, /* nop */
  4031. };
  4032. template<>
  4033. const uint32_t
  4034. Output_data_plt_aarch64_standard<32, true>::
  4035. first_plt_entry[first_plt_entry_size / 4] =
  4036. {
  4037. 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
  4038. 0x90000010, /* adrp x16, PLT_GOT+0x8 */
  4039. 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
  4040. 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
  4041. 0xd61f0220, /* br x17 */
  4042. 0xd503201f, /* nop */
  4043. 0xd503201f, /* nop */
  4044. 0xd503201f, /* nop */
  4045. };
  4046. template<>
  4047. const uint32_t
  4048. Output_data_plt_aarch64_standard<64, false>::
  4049. first_plt_entry[first_plt_entry_size / 4] =
  4050. {
  4051. 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
  4052. 0x90000010, /* adrp x16, PLT_GOT+16 */
  4053. 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
  4054. 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
  4055. 0xd61f0220, /* br x17 */
  4056. 0xd503201f, /* nop */
  4057. 0xd503201f, /* nop */
  4058. 0xd503201f, /* nop */
  4059. };
  4060. template<>
  4061. const uint32_t
  4062. Output_data_plt_aarch64_standard<64, true>::
  4063. first_plt_entry[first_plt_entry_size / 4] =
  4064. {
  4065. 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
  4066. 0x90000010, /* adrp x16, PLT_GOT+16 */
  4067. 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
  4068. 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
  4069. 0xd61f0220, /* br x17 */
  4070. 0xd503201f, /* nop */
  4071. 0xd503201f, /* nop */
  4072. 0xd503201f, /* nop */
  4073. };
  4074. template<>
  4075. const uint32_t
  4076. Output_data_plt_aarch64_standard<32, false>::
  4077. plt_entry[plt_entry_size / 4] =
  4078. {
  4079. 0x90000010, /* adrp x16, PLTGOT + n * 4 */
  4080. 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
  4081. 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
  4082. 0xd61f0220, /* br x17. */
  4083. };
  4084. template<>
  4085. const uint32_t
  4086. Output_data_plt_aarch64_standard<32, true>::
  4087. plt_entry[plt_entry_size / 4] =
  4088. {
  4089. 0x90000010, /* adrp x16, PLTGOT + n * 4 */
  4090. 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
  4091. 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
  4092. 0xd61f0220, /* br x17. */
  4093. };
  4094. template<>
  4095. const uint32_t
  4096. Output_data_plt_aarch64_standard<64, false>::
  4097. plt_entry[plt_entry_size / 4] =
  4098. {
  4099. 0x90000010, /* adrp x16, PLTGOT + n * 8 */
  4100. 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
  4101. 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
  4102. 0xd61f0220, /* br x17. */
  4103. };
  4104. template<>
  4105. const uint32_t
  4106. Output_data_plt_aarch64_standard<64, true>::
  4107. plt_entry[plt_entry_size / 4] =
  4108. {
  4109. 0x90000010, /* adrp x16, PLTGOT + n * 8 */
  4110. 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
  4111. 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
  4112. 0xd61f0220, /* br x17. */
  4113. };
  4114. template<int size, bool big_endian>
  4115. void
  4116. Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
  4117. unsigned char* pov,
  4118. Address got_address,
  4119. Address plt_address)
  4120. {
  4121. // PLT0 of the small PLT looks like this in ELF64 -
  4122. // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
  4123. // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
  4124. // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
  4125. // symbol resolver
  4126. // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
  4127. // GOTPLT entry for this.
  4128. // br x17
  4129. // PLT0 will be slightly different in ELF32 due to different got entry
  4130. // size.
  4131. memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
  4132. Address gotplt_2nd_ent = got_address + (size / 8) * 2;
  4133. // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
  4134. // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
  4135. // FIXME: This only works for 64bit
  4136. AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
  4137. gotplt_2nd_ent, plt_address + 4);
  4138. // Fill in R_AARCH64_LDST8_LO12
  4139. elfcpp::Swap<32, big_endian>::writeval(
  4140. pov + 8,
  4141. ((this->first_plt_entry[2] & 0xffc003ff)
  4142. | ((gotplt_2nd_ent & 0xff8) << 7)));
  4143. // Fill in R_AARCH64_ADD_ABS_LO12
  4144. elfcpp::Swap<32, big_endian>::writeval(
  4145. pov + 12,
  4146. ((this->first_plt_entry[3] & 0xffc003ff)
  4147. | ((gotplt_2nd_ent & 0xfff) << 10)));
  4148. }
  4149. // Subsequent entries in the PLT for an executable.
  4150. // FIXME: This only works for 64bit
  4151. template<int size, bool big_endian>
  4152. void
  4153. Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
  4154. unsigned char* pov,
  4155. Address got_address,
  4156. Address plt_address,
  4157. unsigned int got_offset,
  4158. unsigned int plt_offset)
  4159. {
  4160. memcpy(pov, this->plt_entry, this->plt_entry_size);
  4161. Address gotplt_entry_address = got_address + got_offset;
  4162. Address plt_entry_address = plt_address + plt_offset;
  4163. // Fill in R_AARCH64_PCREL_ADR_HI21
  4164. AArch64_relocate_functions<size, big_endian>::adrp(
  4165. pov,
  4166. gotplt_entry_address,
  4167. plt_entry_address);
  4168. // Fill in R_AARCH64_LDST64_ABS_LO12
  4169. elfcpp::Swap<32, big_endian>::writeval(
  4170. pov + 4,
  4171. ((this->plt_entry[1] & 0xffc003ff)
  4172. | ((gotplt_entry_address & 0xff8) << 7)));
  4173. // Fill in R_AARCH64_ADD_ABS_LO12
  4174. elfcpp::Swap<32, big_endian>::writeval(
  4175. pov + 8,
  4176. ((this->plt_entry[2] & 0xffc003ff)
  4177. | ((gotplt_entry_address & 0xfff) <<10)));
  4178. }
  4179. template<>
  4180. const uint32_t
  4181. Output_data_plt_aarch64_standard<32, false>::
  4182. tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
  4183. {
  4184. 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
  4185. 0x90000002, /* adrp x2, 0 */
  4186. 0x90000003, /* adrp x3, 0 */
  4187. 0xb9400042, /* ldr w2, [w2, #0] */
  4188. 0x11000063, /* add w3, w3, 0 */
  4189. 0xd61f0040, /* br x2 */
  4190. 0xd503201f, /* nop */
  4191. 0xd503201f, /* nop */
  4192. };
  4193. template<>
  4194. const uint32_t
  4195. Output_data_plt_aarch64_standard<32, true>::
  4196. tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
  4197. {
  4198. 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
  4199. 0x90000002, /* adrp x2, 0 */
  4200. 0x90000003, /* adrp x3, 0 */
  4201. 0xb9400042, /* ldr w2, [w2, #0] */
  4202. 0x11000063, /* add w3, w3, 0 */
  4203. 0xd61f0040, /* br x2 */
  4204. 0xd503201f, /* nop */
  4205. 0xd503201f, /* nop */
  4206. };
  4207. template<>
  4208. const uint32_t
  4209. Output_data_plt_aarch64_standard<64, false>::
  4210. tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
  4211. {
  4212. 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
  4213. 0x90000002, /* adrp x2, 0 */
  4214. 0x90000003, /* adrp x3, 0 */
  4215. 0xf9400042, /* ldr x2, [x2, #0] */
  4216. 0x91000063, /* add x3, x3, 0 */
  4217. 0xd61f0040, /* br x2 */
  4218. 0xd503201f, /* nop */
  4219. 0xd503201f, /* nop */
  4220. };
  4221. template<>
  4222. const uint32_t
  4223. Output_data_plt_aarch64_standard<64, true>::
  4224. tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
  4225. {
  4226. 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
  4227. 0x90000002, /* adrp x2, 0 */
  4228. 0x90000003, /* adrp x3, 0 */
  4229. 0xf9400042, /* ldr x2, [x2, #0] */
  4230. 0x91000063, /* add x3, x3, 0 */
  4231. 0xd61f0040, /* br x2 */
  4232. 0xd503201f, /* nop */
  4233. 0xd503201f, /* nop */
  4234. };
  4235. template<int size, bool big_endian>
  4236. void
  4237. Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
  4238. unsigned char* pov,
  4239. Address gotplt_address,
  4240. Address plt_address,
  4241. Address got_base,
  4242. unsigned int tlsdesc_got_offset,
  4243. unsigned int plt_offset)
  4244. {
  4245. memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
  4246. // move DT_TLSDESC_GOT address into x2
  4247. // move .got.plt address into x3
  4248. Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
  4249. Address plt_entry_address = plt_address + plt_offset;
  4250. // R_AARCH64_ADR_PREL_PG_HI21
  4251. AArch64_relocate_functions<size, big_endian>::adrp(
  4252. pov + 4,
  4253. tlsdesc_got_entry,
  4254. plt_entry_address + 4);
  4255. // R_AARCH64_ADR_PREL_PG_HI21
  4256. AArch64_relocate_functions<size, big_endian>::adrp(
  4257. pov + 8,
  4258. gotplt_address,
  4259. plt_entry_address + 8);
  4260. // R_AARCH64_LDST64_ABS_LO12
  4261. elfcpp::Swap<32, big_endian>::writeval(
  4262. pov + 12,
  4263. ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
  4264. | ((tlsdesc_got_entry & 0xff8) << 7)));
  4265. // R_AARCH64_ADD_ABS_LO12
  4266. elfcpp::Swap<32, big_endian>::writeval(
  4267. pov + 16,
  4268. ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
  4269. | ((gotplt_address & 0xfff) << 10)));
  4270. }
  4271. // Write out the PLT. This uses the hand-coded instructions above,
  4272. // and adjusts them as needed. This is specified by the AMD64 ABI.
  4273. template<int size, bool big_endian>
  4274. void
  4275. Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
  4276. {
  4277. const off_t offset = this->offset();
  4278. const section_size_type oview_size =
  4279. convert_to_section_size_type(this->data_size());
  4280. unsigned char* const oview = of->get_output_view(offset, oview_size);
  4281. const off_t got_file_offset = this->got_plt_->offset();
  4282. gold_assert(got_file_offset + this->got_plt_->data_size()
  4283. == this->got_irelative_->offset());
  4284. const section_size_type got_size =
  4285. convert_to_section_size_type(this->got_plt_->data_size()
  4286. + this->got_irelative_->data_size());
  4287. unsigned char* const got_view = of->get_output_view(got_file_offset,
  4288. got_size);
  4289. unsigned char* pov = oview;
  4290. // The base address of the .plt section.
  4291. typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
  4292. // The base address of the PLT portion of the .got section.
  4293. typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
  4294. = this->got_plt_->address();
  4295. this->fill_first_plt_entry(pov, gotplt_address, plt_address);
  4296. pov += this->first_plt_entry_offset();
  4297. // The first three entries in .got.plt are reserved.
  4298. unsigned char* got_pov = got_view;
  4299. memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
  4300. got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
  4301. unsigned int plt_offset = this->first_plt_entry_offset();
  4302. unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
  4303. const unsigned int count = this->count_ + this->irelative_count_;
  4304. for (unsigned int plt_index = 0;
  4305. plt_index < count;
  4306. ++plt_index,
  4307. pov += this->get_plt_entry_size(),
  4308. got_pov += size / 8,
  4309. plt_offset += this->get_plt_entry_size(),
  4310. got_offset += size / 8)
  4311. {
  4312. // Set and adjust the PLT entry itself.
  4313. this->fill_plt_entry(pov, gotplt_address, plt_address,
  4314. got_offset, plt_offset);
  4315. // Set the entry in the GOT, which points to plt0.
  4316. elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
  4317. }
  4318. if (this->has_tlsdesc_entry())
  4319. {
  4320. // Set and adjust the reserved TLSDESC PLT entry.
  4321. unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
  4322. // The base address of the .base section.
  4323. typename elfcpp::Elf_types<size>::Elf_Addr got_base =
  4324. this->got_->address();
  4325. this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
  4326. tlsdesc_got_offset, plt_offset);
  4327. pov += this->get_plt_tlsdesc_entry_size();
  4328. }
  4329. gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
  4330. gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
  4331. of->write_output_view(offset, oview_size, oview);
  4332. of->write_output_view(got_file_offset, got_size, got_view);
  4333. }
  4334. // Telling how to update the immediate field of an instruction.
  4335. struct AArch64_howto
  4336. {
  4337. // The immediate field mask.
  4338. elfcpp::Elf_Xword dst_mask;
  4339. // The offset to apply relocation immediate
  4340. int doffset;
  4341. // The second part offset, if the immediate field has two parts.
  4342. // -1 if the immediate field has only one part.
  4343. int doffset2;
  4344. };
  4345. static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
  4346. {
  4347. {0, -1, -1}, // DATA
  4348. {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
  4349. {0xffffe0, 5, -1}, // LD [23:5]-imm19
  4350. {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
  4351. {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
  4352. {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
  4353. {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
  4354. {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
  4355. {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
  4356. {0x3ffffff, 0, -1}, // B [25:0]-imm26
  4357. {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
  4358. };
  4359. // AArch64 relocate function class
  4360. template<int size, bool big_endian>
  4361. class AArch64_relocate_functions
  4362. {
  4363. public:
  4364. typedef enum
  4365. {
  4366. STATUS_OKAY, // No error during relocation.
  4367. STATUS_OVERFLOW, // Relocation overflow.
  4368. STATUS_BAD_RELOC, // Relocation cannot be applied.
  4369. } Status;
  4370. typedef AArch64_relocate_functions<size, big_endian> This;
  4371. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  4372. typedef Relocate_info<size, big_endian> The_relocate_info;
  4373. typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
  4374. typedef Reloc_stub<size, big_endian> The_reloc_stub;
  4375. typedef Stub_table<size, big_endian> The_stub_table;
  4376. typedef elfcpp::Rela<size, big_endian> The_rela;
  4377. typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
  4378. // Return the page address of the address.
  4379. // Page(address) = address & ~0xFFF
  4380. static inline AArch64_valtype
  4381. Page(Address address)
  4382. {
  4383. return (address & (~static_cast<Address>(0xFFF)));
  4384. }
  4385. private:
  4386. // Update instruction (pointed by view) with selected bits (immed).
  4387. // val = (val & ~dst_mask) | (immed << doffset)
  4388. template<int valsize>
  4389. static inline void
  4390. update_view(unsigned char* view,
  4391. AArch64_valtype immed,
  4392. elfcpp::Elf_Xword doffset,
  4393. elfcpp::Elf_Xword dst_mask)
  4394. {
  4395. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  4396. Valtype* wv = reinterpret_cast<Valtype*>(view);
  4397. Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
  4398. // Clear immediate fields.
  4399. val &= ~dst_mask;
  4400. elfcpp::Swap<valsize, big_endian>::writeval(wv,
  4401. static_cast<Valtype>(val | (immed << doffset)));
  4402. }
  4403. // Update two parts of an instruction (pointed by view) with selected
  4404. // bits (immed1 and immed2).
  4405. // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
  4406. template<int valsize>
  4407. static inline void
  4408. update_view_two_parts(
  4409. unsigned char* view,
  4410. AArch64_valtype immed1,
  4411. AArch64_valtype immed2,
  4412. elfcpp::Elf_Xword doffset1,
  4413. elfcpp::Elf_Xword doffset2,
  4414. elfcpp::Elf_Xword dst_mask)
  4415. {
  4416. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  4417. Valtype* wv = reinterpret_cast<Valtype*>(view);
  4418. Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
  4419. val &= ~dst_mask;
  4420. elfcpp::Swap<valsize, big_endian>::writeval(wv,
  4421. static_cast<Valtype>(val | (immed1 << doffset1) |
  4422. (immed2 << doffset2)));
  4423. }
  4424. // Update adr or adrp instruction with immed.
  4425. // In adr and adrp: [30:29] immlo [23:5] immhi
  4426. static inline void
  4427. update_adr(unsigned char* view, AArch64_valtype immed)
  4428. {
  4429. elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
  4430. This::template update_view_two_parts<32>(
  4431. view,
  4432. immed & 0x3,
  4433. (immed & 0x1ffffc) >> 2,
  4434. 29,
  4435. 5,
  4436. dst_mask);
  4437. }
  4438. // Update movz/movn instruction with bits immed.
  4439. // Set instruction to movz if is_movz is true, otherwise set instruction
  4440. // to movn.
  4441. static inline void
  4442. update_movnz(unsigned char* view,
  4443. AArch64_valtype immed,
  4444. bool is_movz)
  4445. {
  4446. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  4447. Valtype* wv = reinterpret_cast<Valtype*>(view);
  4448. Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
  4449. const elfcpp::Elf_Xword doffset =
  4450. aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
  4451. const elfcpp::Elf_Xword dst_mask =
  4452. aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
  4453. // Clear immediate fields and opc code.
  4454. val &= ~(dst_mask | (0x3 << 29));
  4455. // Set instruction to movz or movn.
  4456. // movz: [30:29] is 10 movn: [30:29] is 00
  4457. if (is_movz)
  4458. val |= (0x2 << 29);
  4459. elfcpp::Swap<32, big_endian>::writeval(wv,
  4460. static_cast<Valtype>(val | (immed << doffset)));
  4461. }
  4462. public:
  4463. // Update selected bits in text.
  4464. template<int valsize>
  4465. static inline typename This::Status
  4466. reloc_common(unsigned char* view, Address x,
  4467. const AArch64_reloc_property* reloc_property)
  4468. {
  4469. // Select bits from X.
  4470. Address immed = reloc_property->select_x_value(x);
  4471. // Update view.
  4472. const AArch64_reloc_property::Reloc_inst inst =
  4473. reloc_property->reloc_inst();
  4474. // If it is a data relocation or instruction has 2 parts of immediate
  4475. // fields, you should not call pcrela_general.
  4476. gold_assert(aarch64_howto[inst].doffset2 == -1 &&
  4477. aarch64_howto[inst].doffset != -1);
  4478. This::template update_view<valsize>(view, immed,
  4479. aarch64_howto[inst].doffset,
  4480. aarch64_howto[inst].dst_mask);
  4481. // Do check overflow or alignment if needed.
  4482. return (reloc_property->checkup_x_value(x)
  4483. ? This::STATUS_OKAY
  4484. : This::STATUS_OVERFLOW);
  4485. }
  4486. // Construct a B insn. Note, although we group it here with other relocation
  4487. // operation, there is actually no 'relocation' involved here.
  4488. static inline void
  4489. construct_b(unsigned char* view, unsigned int branch_offset)
  4490. {
  4491. update_view_two_parts<32>(view, 0x05, (branch_offset >> 2),
  4492. 26, 0, 0xffffffff);
  4493. }
  4494. // Do a simple rela relocation at unaligned addresses.
  4495. template<int valsize>
  4496. static inline typename This::Status
  4497. rela_ua(unsigned char* view,
  4498. const Sized_relobj_file<size, big_endian>* object,
  4499. const Symbol_value<size>* psymval,
  4500. AArch64_valtype addend,
  4501. const AArch64_reloc_property* reloc_property)
  4502. {
  4503. typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
  4504. Valtype;
  4505. typename elfcpp::Elf_types<size>::Elf_Addr x =
  4506. psymval->value(object, addend);
  4507. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
  4508. static_cast<Valtype>(x));
  4509. return (reloc_property->checkup_x_value(x)
  4510. ? This::STATUS_OKAY
  4511. : This::STATUS_OVERFLOW);
  4512. }
  4513. // Do a simple pc-relative relocation at unaligned addresses.
  4514. template<int valsize>
  4515. static inline typename This::Status
  4516. pcrela_ua(unsigned char* view,
  4517. const Sized_relobj_file<size, big_endian>* object,
  4518. const Symbol_value<size>* psymval,
  4519. AArch64_valtype addend,
  4520. Address address,
  4521. const AArch64_reloc_property* reloc_property)
  4522. {
  4523. typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
  4524. Valtype;
  4525. Address x = psymval->value(object, addend) - address;
  4526. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
  4527. static_cast<Valtype>(x));
  4528. return (reloc_property->checkup_x_value(x)
  4529. ? This::STATUS_OKAY
  4530. : This::STATUS_OVERFLOW);
  4531. }
  4532. // Do a simple rela relocation at aligned addresses.
  4533. template<int valsize>
  4534. static inline typename This::Status
  4535. rela(
  4536. unsigned char* view,
  4537. const Sized_relobj_file<size, big_endian>* object,
  4538. const Symbol_value<size>* psymval,
  4539. AArch64_valtype addend,
  4540. const AArch64_reloc_property* reloc_property)
  4541. {
  4542. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  4543. Valtype* wv = reinterpret_cast<Valtype*>(view);
  4544. Address x = psymval->value(object, addend);
  4545. elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
  4546. return (reloc_property->checkup_x_value(x)
  4547. ? This::STATUS_OKAY
  4548. : This::STATUS_OVERFLOW);
  4549. }
  4550. // Do relocate. Update selected bits in text.
  4551. // new_val = (val & ~dst_mask) | (immed << doffset)
  4552. template<int valsize>
  4553. static inline typename This::Status
  4554. rela_general(unsigned char* view,
  4555. const Sized_relobj_file<size, big_endian>* object,
  4556. const Symbol_value<size>* psymval,
  4557. AArch64_valtype addend,
  4558. const AArch64_reloc_property* reloc_property)
  4559. {
  4560. // Calculate relocation.
  4561. Address x = psymval->value(object, addend);
  4562. return This::template reloc_common<valsize>(view, x, reloc_property);
  4563. }
  4564. // Do relocate. Update selected bits in text.
  4565. // new val = (val & ~dst_mask) | (immed << doffset)
  4566. template<int valsize>
  4567. static inline typename This::Status
  4568. rela_general(
  4569. unsigned char* view,
  4570. AArch64_valtype s,
  4571. AArch64_valtype addend,
  4572. const AArch64_reloc_property* reloc_property)
  4573. {
  4574. // Calculate relocation.
  4575. Address x = s + addend;
  4576. return This::template reloc_common<valsize>(view, x, reloc_property);
  4577. }
  4578. // Do address relative relocate. Update selected bits in text.
  4579. // new val = (val & ~dst_mask) | (immed << doffset)
  4580. template<int valsize>
  4581. static inline typename This::Status
  4582. pcrela_general(
  4583. unsigned char* view,
  4584. const Sized_relobj_file<size, big_endian>* object,
  4585. const Symbol_value<size>* psymval,
  4586. AArch64_valtype addend,
  4587. Address address,
  4588. const AArch64_reloc_property* reloc_property)
  4589. {
  4590. // Calculate relocation.
  4591. Address x = psymval->value(object, addend) - address;
  4592. return This::template reloc_common<valsize>(view, x, reloc_property);
  4593. }
  4594. // Calculate (S + A) - address, update adr instruction.
  4595. static inline typename This::Status
  4596. adr(unsigned char* view,
  4597. const Sized_relobj_file<size, big_endian>* object,
  4598. const Symbol_value<size>* psymval,
  4599. Address addend,
  4600. Address address,
  4601. const AArch64_reloc_property* /* reloc_property */)
  4602. {
  4603. AArch64_valtype x = psymval->value(object, addend) - address;
  4604. // Pick bits [20:0] of X.
  4605. AArch64_valtype immed = x & 0x1fffff;
  4606. update_adr(view, immed);
  4607. // Check -2^20 <= X < 2^20
  4608. return (size == 64 && Bits<21>::has_overflow((x))
  4609. ? This::STATUS_OVERFLOW
  4610. : This::STATUS_OKAY);
  4611. }
  4612. // Calculate PG(S+A) - PG(address), update adrp instruction.
  4613. // R_AARCH64_ADR_PREL_PG_HI21
  4614. static inline typename This::Status
  4615. adrp(
  4616. unsigned char* view,
  4617. Address sa,
  4618. Address address)
  4619. {
  4620. AArch64_valtype x = This::Page(sa) - This::Page(address);
  4621. // Pick [32:12] of X.
  4622. AArch64_valtype immed = (x >> 12) & 0x1fffff;
  4623. update_adr(view, immed);
  4624. // Check -2^32 <= X < 2^32
  4625. return (size == 64 && Bits<33>::has_overflow((x))
  4626. ? This::STATUS_OVERFLOW
  4627. : This::STATUS_OKAY);
  4628. }
  4629. // Calculate PG(S+A) - PG(address), update adrp instruction.
  4630. // R_AARCH64_ADR_PREL_PG_HI21
  4631. static inline typename This::Status
  4632. adrp(unsigned char* view,
  4633. const Sized_relobj_file<size, big_endian>* object,
  4634. const Symbol_value<size>* psymval,
  4635. Address addend,
  4636. Address address,
  4637. const AArch64_reloc_property* reloc_property)
  4638. {
  4639. Address sa = psymval->value(object, addend);
  4640. AArch64_valtype x = This::Page(sa) - This::Page(address);
  4641. // Pick [32:12] of X.
  4642. AArch64_valtype immed = (x >> 12) & 0x1fffff;
  4643. update_adr(view, immed);
  4644. return (reloc_property->checkup_x_value(x)
  4645. ? This::STATUS_OKAY
  4646. : This::STATUS_OVERFLOW);
  4647. }
  4648. // Update mov[n/z] instruction. Check overflow if needed.
  4649. // If X >=0, set the instruction to movz and its immediate value to the
  4650. // selected bits S.
  4651. // If X < 0, set the instruction to movn and its immediate value to
  4652. // NOT (selected bits of).
  4653. static inline typename This::Status
  4654. movnz(unsigned char* view,
  4655. AArch64_valtype x,
  4656. const AArch64_reloc_property* reloc_property)
  4657. {
  4658. // Select bits from X.
  4659. Address immed;
  4660. bool is_movz;
  4661. typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
  4662. if (static_cast<SignedW>(x) >= 0)
  4663. {
  4664. immed = reloc_property->select_x_value(x);
  4665. is_movz = true;
  4666. }
  4667. else
  4668. {
  4669. immed = reloc_property->select_x_value(~x);;
  4670. is_movz = false;
  4671. }
  4672. // Update movnz instruction.
  4673. update_movnz(view, immed, is_movz);
  4674. // Do check overflow or alignment if needed.
  4675. return (reloc_property->checkup_x_value(x)
  4676. ? This::STATUS_OKAY
  4677. : This::STATUS_OVERFLOW);
  4678. }
  4679. static inline bool
  4680. maybe_apply_stub(unsigned int,
  4681. const The_relocate_info*,
  4682. const The_rela&,
  4683. unsigned char*,
  4684. Address,
  4685. const Sized_symbol<size>*,
  4686. const Symbol_value<size>*,
  4687. const Sized_relobj_file<size, big_endian>*,
  4688. section_size_type);
  4689. }; // End of AArch64_relocate_functions
  4690. // For a certain relocation type (usually jump/branch), test to see if the
  4691. // destination needs a stub to fulfil. If so, re-route the destination of the
  4692. // original instruction to the stub, note, at this time, the stub has already
  4693. // been generated.
  4694. template<int size, bool big_endian>
  4695. bool
  4696. AArch64_relocate_functions<size, big_endian>::
  4697. maybe_apply_stub(unsigned int r_type,
  4698. const The_relocate_info* relinfo,
  4699. const The_rela& rela,
  4700. unsigned char* view,
  4701. Address address,
  4702. const Sized_symbol<size>* gsym,
  4703. const Symbol_value<size>* psymval,
  4704. const Sized_relobj_file<size, big_endian>* object,
  4705. section_size_type current_group_size)
  4706. {
  4707. if (parameters->options().relocatable())
  4708. return false;
  4709. typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
  4710. Address branch_target = psymval->value(object, 0) + addend;
  4711. int stub_type =
  4712. The_reloc_stub::stub_type_for_reloc(r_type, address, branch_target);
  4713. if (stub_type == ST_NONE)
  4714. return false;
  4715. const The_aarch64_relobj* aarch64_relobj =
  4716. static_cast<const The_aarch64_relobj*>(object);
  4717. const AArch64_reloc_property* arp =
  4718. aarch64_reloc_property_table->get_reloc_property(r_type);
  4719. gold_assert(arp != NULL);
  4720. // We don't create stubs for undefined symbols, but do for weak.
  4721. if (gsym
  4722. && !gsym->use_plt_offset(arp->reference_flags())
  4723. && gsym->is_undefined())
  4724. {
  4725. gold_debug(DEBUG_TARGET,
  4726. "stub: looking for a stub for undefined symbol %s in file %s",
  4727. gsym->name(), aarch64_relobj->name().c_str());
  4728. return false;
  4729. }
  4730. The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
  4731. gold_assert(stub_table != NULL);
  4732. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  4733. typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
  4734. The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
  4735. gold_assert(stub != NULL);
  4736. Address new_branch_target = stub_table->address() + stub->offset();
  4737. typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
  4738. new_branch_target - address;
  4739. typename This::Status status = This::template
  4740. rela_general<32>(view, branch_offset, 0, arp);
  4741. if (status != This::STATUS_OKAY)
  4742. gold_error(_("Stub is too far away, try a smaller value "
  4743. "for '--stub-group-size'. The current value is 0x%lx."),
  4744. static_cast<unsigned long>(current_group_size));
  4745. return true;
  4746. }
  4747. // Group input sections for stub generation.
  4748. //
  4749. // We group input sections in an output section so that the total size,
  4750. // including any padding space due to alignment is smaller than GROUP_SIZE
  4751. // unless the only input section in group is bigger than GROUP_SIZE already.
  4752. // Then an ARM stub table is created to follow the last input section
  4753. // in group. For each group an ARM stub table is created an is placed
  4754. // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
  4755. // extend the group after the stub table.
  4756. template<int size, bool big_endian>
  4757. void
  4758. Target_aarch64<size, big_endian>::group_sections(
  4759. Layout* layout,
  4760. section_size_type group_size,
  4761. bool stubs_always_after_branch,
  4762. const Task* task)
  4763. {
  4764. // Group input sections and insert stub table
  4765. Layout::Section_list section_list;
  4766. layout->get_executable_sections(&section_list);
  4767. for (Layout::Section_list::const_iterator p = section_list.begin();
  4768. p != section_list.end();
  4769. ++p)
  4770. {
  4771. AArch64_output_section<size, big_endian>* output_section =
  4772. static_cast<AArch64_output_section<size, big_endian>*>(*p);
  4773. output_section->group_sections(group_size, stubs_always_after_branch,
  4774. this, task);
  4775. }
  4776. }
  4777. // Find the AArch64_input_section object corresponding to the SHNDX-th input
  4778. // section of RELOBJ.
  4779. template<int size, bool big_endian>
  4780. AArch64_input_section<size, big_endian>*
  4781. Target_aarch64<size, big_endian>::find_aarch64_input_section(
  4782. Relobj* relobj, unsigned int shndx) const
  4783. {
  4784. Section_id sid(relobj, shndx);
  4785. typename AArch64_input_section_map::const_iterator p =
  4786. this->aarch64_input_section_map_.find(sid);
  4787. return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
  4788. }
  4789. // Make a new AArch64_input_section object.
  4790. template<int size, bool big_endian>
  4791. AArch64_input_section<size, big_endian>*
  4792. Target_aarch64<size, big_endian>::new_aarch64_input_section(
  4793. Relobj* relobj, unsigned int shndx)
  4794. {
  4795. Section_id sid(relobj, shndx);
  4796. AArch64_input_section<size, big_endian>* input_section =
  4797. new AArch64_input_section<size, big_endian>(relobj, shndx);
  4798. input_section->init();
  4799. // Register new AArch64_input_section in map for look-up.
  4800. std::pair<typename AArch64_input_section_map::iterator,bool> ins =
  4801. this->aarch64_input_section_map_.insert(
  4802. std::make_pair(sid, input_section));
  4803. // Make sure that it we have not created another AArch64_input_section
  4804. // for this input section already.
  4805. gold_assert(ins.second);
  4806. return input_section;
  4807. }
  4808. // Relaxation hook. This is where we do stub generation.
  4809. template<int size, bool big_endian>
  4810. bool
  4811. Target_aarch64<size, big_endian>::do_relax(
  4812. int pass,
  4813. const Input_objects* input_objects,
  4814. Symbol_table* symtab,
  4815. Layout* layout ,
  4816. const Task* task)
  4817. {
  4818. gold_assert(!parameters->options().relocatable());
  4819. if (pass == 1)
  4820. {
  4821. // We don't handle negative stub_group_size right now.
  4822. this->stub_group_size_ = abs(parameters->options().stub_group_size());
  4823. if (this->stub_group_size_ == 1)
  4824. {
  4825. // Leave room for 4096 4-byte stub entries. If we exceed that, then we
  4826. // will fail to link. The user will have to relink with an explicit
  4827. // group size option.
  4828. this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
  4829. 4096 * 4;
  4830. }
  4831. group_sections(layout, this->stub_group_size_, true, task);
  4832. }
  4833. else
  4834. {
  4835. // If this is not the first pass, addresses and file offsets have
  4836. // been reset at this point, set them here.
  4837. for (Stub_table_iterator sp = this->stub_tables_.begin();
  4838. sp != this->stub_tables_.end(); ++sp)
  4839. {
  4840. The_stub_table* stt = *sp;
  4841. The_aarch64_input_section* owner = stt->owner();
  4842. off_t off = align_address(owner->original_size(),
  4843. stt->addralign());
  4844. stt->set_address_and_file_offset(owner->address() + off,
  4845. owner->offset() + off);
  4846. }
  4847. }
  4848. // Scan relocs for relocation stubs
  4849. for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
  4850. op != input_objects->relobj_end();
  4851. ++op)
  4852. {
  4853. The_aarch64_relobj* aarch64_relobj =
  4854. static_cast<The_aarch64_relobj*>(*op);
  4855. // Lock the object so we can read from it. This is only called
  4856. // single-threaded from Layout::finalize, so it is OK to lock.
  4857. Task_lock_obj<Object> tl(task, aarch64_relobj);
  4858. aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
  4859. }
  4860. bool any_stub_table_changed = false;
  4861. for (Stub_table_iterator siter = this->stub_tables_.begin();
  4862. siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
  4863. {
  4864. The_stub_table* stub_table = *siter;
  4865. if (stub_table->update_data_size_changed_p())
  4866. {
  4867. The_aarch64_input_section* owner = stub_table->owner();
  4868. uint64_t address = owner->address();
  4869. off_t offset = owner->offset();
  4870. owner->reset_address_and_file_offset();
  4871. owner->set_address_and_file_offset(address, offset);
  4872. any_stub_table_changed = true;
  4873. }
  4874. }
  4875. // Do not continue relaxation.
  4876. bool continue_relaxation = any_stub_table_changed;
  4877. if (!continue_relaxation)
  4878. for (Stub_table_iterator sp = this->stub_tables_.begin();
  4879. (sp != this->stub_tables_.end());
  4880. ++sp)
  4881. (*sp)->finalize_stubs();
  4882. return continue_relaxation;
  4883. }
  4884. // Make a new Stub_table.
  4885. template<int size, bool big_endian>
  4886. Stub_table<size, big_endian>*
  4887. Target_aarch64<size, big_endian>::new_stub_table(
  4888. AArch64_input_section<size, big_endian>* owner)
  4889. {
  4890. Stub_table<size, big_endian>* stub_table =
  4891. new Stub_table<size, big_endian>(owner);
  4892. stub_table->set_address(align_address(
  4893. owner->address() + owner->data_size(), 8));
  4894. stub_table->set_file_offset(owner->offset() + owner->data_size());
  4895. stub_table->finalize_data_size();
  4896. this->stub_tables_.push_back(stub_table);
  4897. return stub_table;
  4898. }
  4899. template<int size, bool big_endian>
  4900. uint64_t
  4901. Target_aarch64<size, big_endian>::do_reloc_addend(
  4902. void* arg, unsigned int r_type, uint64_t) const
  4903. {
  4904. gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
  4905. uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
  4906. gold_assert(intarg < this->tlsdesc_reloc_info_.size());
  4907. const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
  4908. const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
  4909. gold_assert(psymval->is_tls_symbol());
  4910. // The value of a TLS symbol is the offset in the TLS segment.
  4911. return psymval->value(ti.object, 0);
  4912. }
  4913. // Return the number of entries in the PLT.
  4914. template<int size, bool big_endian>
  4915. unsigned int
  4916. Target_aarch64<size, big_endian>::plt_entry_count() const
  4917. {
  4918. if (this->plt_ == NULL)
  4919. return 0;
  4920. return this->plt_->entry_count();
  4921. }
  4922. // Return the offset of the first non-reserved PLT entry.
  4923. template<int size, bool big_endian>
  4924. unsigned int
  4925. Target_aarch64<size, big_endian>::first_plt_entry_offset() const
  4926. {
  4927. return this->plt_->first_plt_entry_offset();
  4928. }
  4929. // Return the size of each PLT entry.
  4930. template<int size, bool big_endian>
  4931. unsigned int
  4932. Target_aarch64<size, big_endian>::plt_entry_size() const
  4933. {
  4934. return this->plt_->get_plt_entry_size();
  4935. }
  4936. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  4937. template<int size, bool big_endian>
  4938. void
  4939. Target_aarch64<size, big_endian>::define_tls_base_symbol(
  4940. Symbol_table* symtab, Layout* layout)
  4941. {
  4942. if (this->tls_base_symbol_defined_)
  4943. return;
  4944. Output_segment* tls_segment = layout->tls_segment();
  4945. if (tls_segment != NULL)
  4946. {
  4947. // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
  4948. symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
  4949. Symbol_table::PREDEFINED,
  4950. tls_segment, 0, 0,
  4951. elfcpp::STT_TLS,
  4952. elfcpp::STB_LOCAL,
  4953. elfcpp::STV_HIDDEN, 0,
  4954. Symbol::SEGMENT_START,
  4955. true);
  4956. }
  4957. this->tls_base_symbol_defined_ = true;
  4958. }
  4959. // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
  4960. template<int size, bool big_endian>
  4961. void
  4962. Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
  4963. Symbol_table* symtab, Layout* layout)
  4964. {
  4965. if (this->plt_ == NULL)
  4966. this->make_plt_section(symtab, layout);
  4967. if (!this->plt_->has_tlsdesc_entry())
  4968. {
  4969. // Allocate the TLSDESC_GOT entry.
  4970. Output_data_got_aarch64<size, big_endian>* got =
  4971. this->got_section(symtab, layout);
  4972. unsigned int got_offset = got->add_constant(0);
  4973. // Allocate the TLSDESC_PLT entry.
  4974. this->plt_->reserve_tlsdesc_entry(got_offset);
  4975. }
  4976. }
  4977. // Create a GOT entry for the TLS module index.
  4978. template<int size, bool big_endian>
  4979. unsigned int
  4980. Target_aarch64<size, big_endian>::got_mod_index_entry(
  4981. Symbol_table* symtab, Layout* layout,
  4982. Sized_relobj_file<size, big_endian>* object)
  4983. {
  4984. if (this->got_mod_index_offset_ == -1U)
  4985. {
  4986. gold_assert(symtab != NULL && layout != NULL && object != NULL);
  4987. Reloc_section* rela_dyn = this->rela_dyn_section(layout);
  4988. Output_data_got_aarch64<size, big_endian>* got =
  4989. this->got_section(symtab, layout);
  4990. unsigned int got_offset = got->add_constant(0);
  4991. rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
  4992. got_offset, 0);
  4993. got->add_constant(0);
  4994. this->got_mod_index_offset_ = got_offset;
  4995. }
  4996. return this->got_mod_index_offset_;
  4997. }
  4998. // Optimize the TLS relocation type based on what we know about the
  4999. // symbol. IS_FINAL is true if the final address of this symbol is
  5000. // known at link time.
  5001. template<int size, bool big_endian>
  5002. tls::Tls_optimization
  5003. Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
  5004. int r_type)
  5005. {
  5006. // If we are generating a shared library, then we can't do anything
  5007. // in the linker
  5008. if (parameters->options().shared())
  5009. return tls::TLSOPT_NONE;
  5010. switch (r_type)
  5011. {
  5012. case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
  5013. case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
  5014. case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
  5015. case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
  5016. case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
  5017. case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
  5018. case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
  5019. case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
  5020. case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
  5021. case elfcpp::R_AARCH64_TLSDESC_LDR:
  5022. case elfcpp::R_AARCH64_TLSDESC_ADD:
  5023. case elfcpp::R_AARCH64_TLSDESC_CALL:
  5024. // These are General-Dynamic which permits fully general TLS
  5025. // access. Since we know that we are generating an executable,
  5026. // we can convert this to Initial-Exec. If we also know that
  5027. // this is a local symbol, we can further switch to Local-Exec.
  5028. if (is_final)
  5029. return tls::TLSOPT_TO_LE;
  5030. return tls::TLSOPT_TO_IE;
  5031. case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
  5032. case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
  5033. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
  5034. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
  5035. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
  5036. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
  5037. // These are Local-Dynamic, which refer to local symbols in the
  5038. // dynamic TLS block. Since we know that we generating an
  5039. // executable, we can switch to Local-Exec.
  5040. return tls::TLSOPT_TO_LE;
  5041. case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
  5042. case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
  5043. case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
  5044. case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
  5045. case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
  5046. // These are Initial-Exec relocs which get the thread offset
  5047. // from the GOT. If we know that we are linking against the
  5048. // local symbol, we can switch to Local-Exec, which links the
  5049. // thread offset into the instruction.
  5050. if (is_final)
  5051. return tls::TLSOPT_TO_LE;
  5052. return tls::TLSOPT_NONE;
  5053. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
  5054. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
  5055. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
  5056. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
  5057. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
  5058. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
  5059. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
  5060. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
  5061. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12:
  5062. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
  5063. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12:
  5064. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
  5065. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12:
  5066. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
  5067. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12:
  5068. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
  5069. // When we already have Local-Exec, there is nothing further we
  5070. // can do.
  5071. return tls::TLSOPT_NONE;
  5072. default:
  5073. gold_unreachable();
  5074. }
  5075. }
  5076. // Returns true if this relocation type could be that of a function pointer.
  5077. template<int size, bool big_endian>
  5078. inline bool
  5079. Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
  5080. unsigned int r_type)
  5081. {
  5082. switch (r_type)
  5083. {
  5084. case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
  5085. case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
  5086. case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
  5087. case elfcpp::R_AARCH64_ADR_GOT_PAGE:
  5088. case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
  5089. {
  5090. return true;
  5091. }
  5092. }
  5093. return false;
  5094. }
  5095. // For safe ICF, scan a relocation for a local symbol to check if it
  5096. // corresponds to a function pointer being taken. In that case mark
  5097. // the function whose pointer was taken as not foldable.
  5098. template<int size, bool big_endian>
  5099. inline bool
  5100. Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
  5101. Symbol_table* ,
  5102. Layout* ,
  5103. Target_aarch64<size, big_endian>* ,
  5104. Sized_relobj_file<size, big_endian>* ,
  5105. unsigned int ,
  5106. Output_section* ,
  5107. const elfcpp::Rela<size, big_endian>& ,
  5108. unsigned int r_type,
  5109. const elfcpp::Sym<size, big_endian>&)
  5110. {
  5111. // When building a shared library, do not fold any local symbols.
  5112. return (parameters->options().shared()
  5113. || possible_function_pointer_reloc(r_type));
  5114. }
  5115. // For safe ICF, scan a relocation for a global symbol to check if it
  5116. // corresponds to a function pointer being taken. In that case mark
  5117. // the function whose pointer was taken as not foldable.
  5118. template<int size, bool big_endian>
  5119. inline bool
  5120. Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
  5121. Symbol_table* ,
  5122. Layout* ,
  5123. Target_aarch64<size, big_endian>* ,
  5124. Sized_relobj_file<size, big_endian>* ,
  5125. unsigned int ,
  5126. Output_section* ,
  5127. const elfcpp::Rela<size, big_endian>& ,
  5128. unsigned int r_type,
  5129. Symbol* gsym)
  5130. {
  5131. // When building a shared library, do not fold symbols whose visibility
  5132. // is hidden, internal or protected.
  5133. return ((parameters->options().shared()
  5134. && (gsym->visibility() == elfcpp::STV_INTERNAL
  5135. || gsym->visibility() == elfcpp::STV_PROTECTED
  5136. || gsym->visibility() == elfcpp::STV_HIDDEN))
  5137. || possible_function_pointer_reloc(r_type));
  5138. }
  5139. // Report an unsupported relocation against a local symbol.
  5140. template<int size, bool big_endian>
  5141. void
  5142. Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
  5143. Sized_relobj_file<size, big_endian>* object,
  5144. unsigned int r_type)
  5145. {
  5146. gold_error(_("%s: unsupported reloc %u against local symbol"),
  5147. object->name().c_str(), r_type);
  5148. }
  5149. // We are about to emit a dynamic relocation of type R_TYPE. If the
  5150. // dynamic linker does not support it, issue an error.
  5151. template<int size, bool big_endian>
  5152. void
  5153. Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
  5154. unsigned int r_type)
  5155. {
  5156. gold_assert(r_type != elfcpp::R_AARCH64_NONE);
  5157. switch (r_type)
  5158. {
  5159. // These are the relocation types supported by glibc for AARCH64.
  5160. case elfcpp::R_AARCH64_NONE:
  5161. case elfcpp::R_AARCH64_COPY:
  5162. case elfcpp::R_AARCH64_GLOB_DAT:
  5163. case elfcpp::R_AARCH64_JUMP_SLOT:
  5164. case elfcpp::R_AARCH64_RELATIVE:
  5165. case elfcpp::R_AARCH64_TLS_DTPREL64:
  5166. case elfcpp::R_AARCH64_TLS_DTPMOD64:
  5167. case elfcpp::R_AARCH64_TLS_TPREL64:
  5168. case elfcpp::R_AARCH64_TLSDESC:
  5169. case elfcpp::R_AARCH64_IRELATIVE:
  5170. case elfcpp::R_AARCH64_ABS32:
  5171. case elfcpp::R_AARCH64_ABS64:
  5172. return;
  5173. default:
  5174. break;
  5175. }
  5176. // This prevents us from issuing more than one error per reloc
  5177. // section. But we can still wind up issuing more than one
  5178. // error per object file.
  5179. if (this->issued_non_pic_error_)
  5180. return;
  5181. gold_assert(parameters->options().output_is_position_independent());
  5182. object->error(_("requires unsupported dynamic reloc; "
  5183. "recompile with -fPIC"));
  5184. this->issued_non_pic_error_ = true;
  5185. return;
  5186. }
  5187. // Return whether we need to make a PLT entry for a relocation of the
  5188. // given type against a STT_GNU_IFUNC symbol.
  5189. template<int size, bool big_endian>
  5190. bool
  5191. Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
  5192. Sized_relobj_file<size, big_endian>* object,
  5193. unsigned int r_type)
  5194. {
  5195. const AArch64_reloc_property* arp =
  5196. aarch64_reloc_property_table->get_reloc_property(r_type);
  5197. gold_assert(arp != NULL);
  5198. int flags = arp->reference_flags();
  5199. if (flags & Symbol::TLS_REF)
  5200. {
  5201. gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
  5202. object->name().c_str(), arp->name().c_str());
  5203. return false;
  5204. }
  5205. return flags != 0;
  5206. }
  5207. // Scan a relocation for a local symbol.
  5208. template<int size, bool big_endian>
  5209. inline void
  5210. Target_aarch64<size, big_endian>::Scan::local(
  5211. Symbol_table* symtab,
  5212. Layout* layout,
  5213. Target_aarch64<size, big_endian>* target,
  5214. Sized_relobj_file<size, big_endian>* object,
  5215. unsigned int data_shndx,
  5216. Output_section* output_section,
  5217. const elfcpp::Rela<size, big_endian>& rela,
  5218. unsigned int r_type,
  5219. const elfcpp::Sym<size, big_endian>& lsym,
  5220. bool is_discarded)
  5221. {
  5222. if (is_discarded)
  5223. return;
  5224. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
  5225. Reloc_section;
  5226. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  5227. // A local STT_GNU_IFUNC symbol may require a PLT entry.
  5228. bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
  5229. if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
  5230. target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
  5231. switch (r_type)
  5232. {
  5233. case elfcpp::R_AARCH64_NONE:
  5234. break;
  5235. case elfcpp::R_AARCH64_ABS32:
  5236. case elfcpp::R_AARCH64_ABS16:
  5237. if (parameters->options().output_is_position_independent())
  5238. {
  5239. gold_error(_("%s: unsupported reloc %u in pos independent link."),
  5240. object->name().c_str(), r_type);
  5241. }
  5242. break;
  5243. case elfcpp::R_AARCH64_ABS64:
  5244. // If building a shared library or pie, we need to mark this as a dynmic
  5245. // reloction, so that the dynamic loader can relocate it.
  5246. if (parameters->options().output_is_position_independent())
  5247. {
  5248. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5249. rela_dyn->add_local_relative(object, r_sym,
  5250. elfcpp::R_AARCH64_RELATIVE,
  5251. output_section,
  5252. data_shndx,
  5253. rela.get_r_offset(),
  5254. rela.get_r_addend(),
  5255. is_ifunc);
  5256. }
  5257. break;
  5258. case elfcpp::R_AARCH64_PREL64:
  5259. case elfcpp::R_AARCH64_PREL32:
  5260. case elfcpp::R_AARCH64_PREL16:
  5261. break;
  5262. case elfcpp::R_AARCH64_ADR_GOT_PAGE:
  5263. case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
  5264. case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
  5265. // The above relocations are used to access GOT entries.
  5266. {
  5267. Output_data_got_aarch64<size, big_endian>* got =
  5268. target->got_section(symtab, layout);
  5269. bool is_new = false;
  5270. // This symbol requires a GOT entry.
  5271. if (is_ifunc)
  5272. is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
  5273. else
  5274. is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
  5275. if (is_new && parameters->options().output_is_position_independent())
  5276. target->rela_dyn_section(layout)->
  5277. add_local_relative(object,
  5278. r_sym,
  5279. elfcpp::R_AARCH64_RELATIVE,
  5280. got,
  5281. object->local_got_offset(r_sym,
  5282. GOT_TYPE_STANDARD),
  5283. 0,
  5284. false);
  5285. }
  5286. break;
  5287. case elfcpp::R_AARCH64_MOVW_UABS_G0: // 263
  5288. case elfcpp::R_AARCH64_MOVW_UABS_G0_NC: // 264
  5289. case elfcpp::R_AARCH64_MOVW_UABS_G1: // 265
  5290. case elfcpp::R_AARCH64_MOVW_UABS_G1_NC: // 266
  5291. case elfcpp::R_AARCH64_MOVW_UABS_G2: // 267
  5292. case elfcpp::R_AARCH64_MOVW_UABS_G2_NC: // 268
  5293. case elfcpp::R_AARCH64_MOVW_UABS_G3: // 269
  5294. case elfcpp::R_AARCH64_MOVW_SABS_G0: // 270
  5295. case elfcpp::R_AARCH64_MOVW_SABS_G1: // 271
  5296. case elfcpp::R_AARCH64_MOVW_SABS_G2: // 272
  5297. if (parameters->options().output_is_position_independent())
  5298. {
  5299. gold_error(_("%s: unsupported reloc %u in pos independent link."),
  5300. object->name().c_str(), r_type);
  5301. }
  5302. break;
  5303. case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
  5304. case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
  5305. case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
  5306. case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
  5307. case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
  5308. case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
  5309. case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
  5310. case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
  5311. case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
  5312. case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
  5313. break;
  5314. // Control flow, pc-relative. We don't need to do anything for a relative
  5315. // addressing relocation against a local symbol if it does not reference
  5316. // the GOT.
  5317. case elfcpp::R_AARCH64_TSTBR14:
  5318. case elfcpp::R_AARCH64_CONDBR19:
  5319. case elfcpp::R_AARCH64_JUMP26:
  5320. case elfcpp::R_AARCH64_CALL26:
  5321. break;
  5322. case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
  5323. case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
  5324. {
  5325. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  5326. optimize_tls_reloc(!parameters->options().shared(), r_type);
  5327. if (tlsopt == tls::TLSOPT_TO_LE)
  5328. break;
  5329. layout->set_has_static_tls();
  5330. // Create a GOT entry for the tp-relative offset.
  5331. if (!parameters->doing_static_link())
  5332. {
  5333. Output_data_got_aarch64<size, big_endian>* got =
  5334. target->got_section(symtab, layout);
  5335. got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
  5336. target->rela_dyn_section(layout),
  5337. elfcpp::R_AARCH64_TLS_TPREL64);
  5338. }
  5339. else if (!object->local_has_got_offset(r_sym,
  5340. GOT_TYPE_TLS_OFFSET))
  5341. {
  5342. Output_data_got_aarch64<size, big_endian>* got =
  5343. target->got_section(symtab, layout);
  5344. got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
  5345. unsigned int got_offset =
  5346. object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
  5347. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  5348. gold_assert(addend == 0);
  5349. got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
  5350. object, r_sym);
  5351. }
  5352. }
  5353. break;
  5354. case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
  5355. case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
  5356. {
  5357. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  5358. optimize_tls_reloc(!parameters->options().shared(), r_type);
  5359. if (tlsopt == tls::TLSOPT_TO_LE)
  5360. {
  5361. layout->set_has_static_tls();
  5362. break;
  5363. }
  5364. gold_assert(tlsopt == tls::TLSOPT_NONE);
  5365. Output_data_got_aarch64<size, big_endian>* got =
  5366. target->got_section(symtab, layout);
  5367. got->add_local_pair_with_rel(object,r_sym, data_shndx,
  5368. GOT_TYPE_TLS_PAIR,
  5369. target->rela_dyn_section(layout),
  5370. elfcpp::R_AARCH64_TLS_DTPMOD64);
  5371. }
  5372. break;
  5373. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
  5374. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
  5375. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
  5376. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
  5377. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
  5378. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
  5379. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
  5380. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
  5381. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12:
  5382. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
  5383. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12:
  5384. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
  5385. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12:
  5386. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
  5387. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12:
  5388. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
  5389. {
  5390. layout->set_has_static_tls();
  5391. bool output_is_shared = parameters->options().shared();
  5392. if (output_is_shared)
  5393. gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
  5394. object->name().c_str(), r_type);
  5395. }
  5396. break;
  5397. case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
  5398. case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
  5399. {
  5400. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  5401. optimize_tls_reloc(!parameters->options().shared(), r_type);
  5402. if (tlsopt == tls::TLSOPT_NONE)
  5403. {
  5404. // Create a GOT entry for the module index.
  5405. target->got_mod_index_entry(symtab, layout, object);
  5406. }
  5407. else if (tlsopt != tls::TLSOPT_TO_LE)
  5408. unsupported_reloc_local(object, r_type);
  5409. }
  5410. break;
  5411. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
  5412. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
  5413. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
  5414. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
  5415. break;
  5416. case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
  5417. case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
  5418. case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
  5419. {
  5420. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  5421. optimize_tls_reloc(!parameters->options().shared(), r_type);
  5422. target->define_tls_base_symbol(symtab, layout);
  5423. if (tlsopt == tls::TLSOPT_NONE)
  5424. {
  5425. // Create reserved PLT and GOT entries for the resolver.
  5426. target->reserve_tlsdesc_entries(symtab, layout);
  5427. // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
  5428. // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
  5429. // entry needs to be in an area in .got.plt, not .got. Call
  5430. // got_section to make sure the section has been created.
  5431. target->got_section(symtab, layout);
  5432. Output_data_got<size, big_endian>* got =
  5433. target->got_tlsdesc_section();
  5434. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  5435. if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
  5436. {
  5437. unsigned int got_offset = got->add_constant(0);
  5438. got->add_constant(0);
  5439. object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
  5440. got_offset);
  5441. Reloc_section* rt = target->rela_tlsdesc_section(layout);
  5442. // We store the arguments we need in a vector, and use
  5443. // the index into the vector as the parameter to pass
  5444. // to the target specific routines.
  5445. uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
  5446. void* arg = reinterpret_cast<void*>(intarg);
  5447. rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
  5448. got, got_offset, 0);
  5449. }
  5450. }
  5451. else if (tlsopt != tls::TLSOPT_TO_LE)
  5452. unsupported_reloc_local(object, r_type);
  5453. }
  5454. break;
  5455. case elfcpp::R_AARCH64_TLSDESC_CALL:
  5456. break;
  5457. default:
  5458. unsupported_reloc_local(object, r_type);
  5459. }
  5460. }
  5461. // Report an unsupported relocation against a global symbol.
  5462. template<int size, bool big_endian>
  5463. void
  5464. Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
  5465. Sized_relobj_file<size, big_endian>* object,
  5466. unsigned int r_type,
  5467. Symbol* gsym)
  5468. {
  5469. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  5470. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  5471. }
  5472. template<int size, bool big_endian>
  5473. inline void
  5474. Target_aarch64<size, big_endian>::Scan::global(
  5475. Symbol_table* symtab,
  5476. Layout* layout,
  5477. Target_aarch64<size, big_endian>* target,
  5478. Sized_relobj_file<size, big_endian> * object,
  5479. unsigned int data_shndx,
  5480. Output_section* output_section,
  5481. const elfcpp::Rela<size, big_endian>& rela,
  5482. unsigned int r_type,
  5483. Symbol* gsym)
  5484. {
  5485. // A STT_GNU_IFUNC symbol may require a PLT entry.
  5486. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  5487. && this->reloc_needs_plt_for_ifunc(object, r_type))
  5488. target->make_plt_entry(symtab, layout, gsym);
  5489. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
  5490. Reloc_section;
  5491. const AArch64_reloc_property* arp =
  5492. aarch64_reloc_property_table->get_reloc_property(r_type);
  5493. gold_assert(arp != NULL);
  5494. switch (r_type)
  5495. {
  5496. case elfcpp::R_AARCH64_NONE:
  5497. break;
  5498. case elfcpp::R_AARCH64_ABS16:
  5499. case elfcpp::R_AARCH64_ABS32:
  5500. case elfcpp::R_AARCH64_ABS64:
  5501. {
  5502. // Make a PLT entry if necessary.
  5503. if (gsym->needs_plt_entry())
  5504. {
  5505. target->make_plt_entry(symtab, layout, gsym);
  5506. // Since this is not a PC-relative relocation, we may be
  5507. // taking the address of a function. In that case we need to
  5508. // set the entry in the dynamic symbol table to the address of
  5509. // the PLT entry.
  5510. if (gsym->is_from_dynobj() && !parameters->options().shared())
  5511. gsym->set_needs_dynsym_value();
  5512. }
  5513. // Make a dynamic relocation if necessary.
  5514. if (gsym->needs_dynamic_reloc(arp->reference_flags()))
  5515. {
  5516. if (!parameters->options().output_is_position_independent()
  5517. && gsym->may_need_copy_reloc())
  5518. {
  5519. target->copy_reloc(symtab, layout, object,
  5520. data_shndx, output_section, gsym, rela);
  5521. }
  5522. else if (r_type == elfcpp::R_AARCH64_ABS64
  5523. && gsym->type() == elfcpp::STT_GNU_IFUNC
  5524. && gsym->can_use_relative_reloc(false)
  5525. && !gsym->is_from_dynobj()
  5526. && !gsym->is_undefined()
  5527. && !gsym->is_preemptible())
  5528. {
  5529. // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
  5530. // symbol. This makes a function address in a PIE executable
  5531. // match the address in a shared library that it links against.
  5532. Reloc_section* rela_dyn =
  5533. target->rela_irelative_section(layout);
  5534. unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
  5535. rela_dyn->add_symbolless_global_addend(gsym, r_type,
  5536. output_section, object,
  5537. data_shndx,
  5538. rela.get_r_offset(),
  5539. rela.get_r_addend());
  5540. }
  5541. else if (r_type == elfcpp::R_AARCH64_ABS64
  5542. && gsym->can_use_relative_reloc(false))
  5543. {
  5544. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5545. rela_dyn->add_global_relative(gsym,
  5546. elfcpp::R_AARCH64_RELATIVE,
  5547. output_section,
  5548. object,
  5549. data_shndx,
  5550. rela.get_r_offset(),
  5551. rela.get_r_addend(),
  5552. false);
  5553. }
  5554. else
  5555. {
  5556. check_non_pic(object, r_type);
  5557. Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
  5558. rela_dyn = target->rela_dyn_section(layout);
  5559. rela_dyn->add_global(
  5560. gsym, r_type, output_section, object,
  5561. data_shndx, rela.get_r_offset(),rela.get_r_addend());
  5562. }
  5563. }
  5564. }
  5565. break;
  5566. case elfcpp::R_AARCH64_PREL16:
  5567. case elfcpp::R_AARCH64_PREL32:
  5568. case elfcpp::R_AARCH64_PREL64:
  5569. // This is used to fill the GOT absolute address.
  5570. if (gsym->needs_plt_entry())
  5571. {
  5572. target->make_plt_entry(symtab, layout, gsym);
  5573. }
  5574. break;
  5575. case elfcpp::R_AARCH64_MOVW_UABS_G0: // 263
  5576. case elfcpp::R_AARCH64_MOVW_UABS_G0_NC: // 264
  5577. case elfcpp::R_AARCH64_MOVW_UABS_G1: // 265
  5578. case elfcpp::R_AARCH64_MOVW_UABS_G1_NC: // 266
  5579. case elfcpp::R_AARCH64_MOVW_UABS_G2: // 267
  5580. case elfcpp::R_AARCH64_MOVW_UABS_G2_NC: // 268
  5581. case elfcpp::R_AARCH64_MOVW_UABS_G3: // 269
  5582. case elfcpp::R_AARCH64_MOVW_SABS_G0: // 270
  5583. case elfcpp::R_AARCH64_MOVW_SABS_G1: // 271
  5584. case elfcpp::R_AARCH64_MOVW_SABS_G2: // 272
  5585. if (parameters->options().output_is_position_independent())
  5586. {
  5587. gold_error(_("%s: unsupported reloc %u in pos independent link."),
  5588. object->name().c_str(), r_type);
  5589. }
  5590. // Make a PLT entry if necessary.
  5591. if (gsym->needs_plt_entry())
  5592. {
  5593. target->make_plt_entry(symtab, layout, gsym);
  5594. // Since this is not a PC-relative relocation, we may be
  5595. // taking the address of a function. In that case we need to
  5596. // set the entry in the dynamic symbol table to the address of
  5597. // the PLT entry.
  5598. if (gsym->is_from_dynobj() && !parameters->options().shared())
  5599. gsym->set_needs_dynsym_value();
  5600. }
  5601. break;
  5602. case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
  5603. case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
  5604. case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
  5605. case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
  5606. case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
  5607. case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
  5608. case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
  5609. case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
  5610. case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
  5611. case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
  5612. {
  5613. if (gsym->needs_plt_entry())
  5614. target->make_plt_entry(symtab, layout, gsym);
  5615. // Make a dynamic relocation if necessary.
  5616. if (gsym->needs_dynamic_reloc(arp->reference_flags()))
  5617. {
  5618. if (parameters->options().output_is_executable()
  5619. && gsym->may_need_copy_reloc())
  5620. {
  5621. target->copy_reloc(symtab, layout, object,
  5622. data_shndx, output_section, gsym, rela);
  5623. }
  5624. }
  5625. break;
  5626. }
  5627. case elfcpp::R_AARCH64_ADR_GOT_PAGE:
  5628. case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
  5629. case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
  5630. {
  5631. // The above relocations are used to access GOT entries.
  5632. // Note a GOT entry is an *address* to a symbol.
  5633. // The symbol requires a GOT entry
  5634. Output_data_got_aarch64<size, big_endian>* got =
  5635. target->got_section(symtab, layout);
  5636. if (gsym->final_value_is_known())
  5637. {
  5638. // For a STT_GNU_IFUNC symbol we want the PLT address.
  5639. if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  5640. got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  5641. else
  5642. got->add_global(gsym, GOT_TYPE_STANDARD);
  5643. }
  5644. else
  5645. {
  5646. // If this symbol is not fully resolved, we need to add a dynamic
  5647. // relocation for it.
  5648. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  5649. // Use a GLOB_DAT rather than a RELATIVE reloc if:
  5650. //
  5651. // 1) The symbol may be defined in some other module.
  5652. // 2) We are building a shared library and this is a protected
  5653. // symbol; using GLOB_DAT means that the dynamic linker can use
  5654. // the address of the PLT in the main executable when appropriate
  5655. // so that function address comparisons work.
  5656. // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
  5657. // again so that function address comparisons work.
  5658. if (gsym->is_from_dynobj()
  5659. || gsym->is_undefined()
  5660. || gsym->is_preemptible()
  5661. || (gsym->visibility() == elfcpp::STV_PROTECTED
  5662. && parameters->options().shared())
  5663. || (gsym->type() == elfcpp::STT_GNU_IFUNC
  5664. && parameters->options().output_is_position_independent()))
  5665. got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
  5666. rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
  5667. else
  5668. {
  5669. // For a STT_GNU_IFUNC symbol we want to write the PLT
  5670. // offset into the GOT, so that function pointer
  5671. // comparisons work correctly.
  5672. bool is_new;
  5673. if (gsym->type() != elfcpp::STT_GNU_IFUNC)
  5674. is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
  5675. else
  5676. {
  5677. is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  5678. // Tell the dynamic linker to use the PLT address
  5679. // when resolving relocations.
  5680. if (gsym->is_from_dynobj()
  5681. && !parameters->options().shared())
  5682. gsym->set_needs_dynsym_value();
  5683. }
  5684. if (is_new)
  5685. {
  5686. rela_dyn->add_global_relative(
  5687. gsym, elfcpp::R_AARCH64_RELATIVE,
  5688. got,
  5689. gsym->got_offset(GOT_TYPE_STANDARD),
  5690. 0,
  5691. false);
  5692. }
  5693. }
  5694. }
  5695. break;
  5696. }
  5697. case elfcpp::R_AARCH64_TSTBR14:
  5698. case elfcpp::R_AARCH64_CONDBR19:
  5699. case elfcpp::R_AARCH64_JUMP26:
  5700. case elfcpp::R_AARCH64_CALL26:
  5701. {
  5702. if (gsym->final_value_is_known())
  5703. break;
  5704. if (gsym->is_defined() &&
  5705. !gsym->is_from_dynobj() &&
  5706. !gsym->is_preemptible())
  5707. break;
  5708. // Make plt entry for function call.
  5709. target->make_plt_entry(symtab, layout, gsym);
  5710. break;
  5711. }
  5712. case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
  5713. case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
  5714. {
  5715. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  5716. optimize_tls_reloc(gsym->final_value_is_known(), r_type);
  5717. if (tlsopt == tls::TLSOPT_TO_LE)
  5718. {
  5719. layout->set_has_static_tls();
  5720. break;
  5721. }
  5722. gold_assert(tlsopt == tls::TLSOPT_NONE);
  5723. // General dynamic.
  5724. Output_data_got_aarch64<size, big_endian>* got =
  5725. target->got_section(symtab, layout);
  5726. // Create 2 consecutive entries for module index and offset.
  5727. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
  5728. target->rela_dyn_section(layout),
  5729. elfcpp::R_AARCH64_TLS_DTPMOD64,
  5730. elfcpp::R_AARCH64_TLS_DTPREL64);
  5731. }
  5732. break;
  5733. case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
  5734. case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
  5735. {
  5736. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  5737. optimize_tls_reloc(!parameters->options().shared(), r_type);
  5738. if (tlsopt == tls::TLSOPT_NONE)
  5739. {
  5740. // Create a GOT entry for the module index.
  5741. target->got_mod_index_entry(symtab, layout, object);
  5742. }
  5743. else if (tlsopt != tls::TLSOPT_TO_LE)
  5744. unsupported_reloc_local(object, r_type);
  5745. }
  5746. break;
  5747. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
  5748. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
  5749. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
  5750. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
  5751. break;
  5752. case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
  5753. case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
  5754. {
  5755. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  5756. optimize_tls_reloc(gsym->final_value_is_known(), r_type);
  5757. if (tlsopt == tls::TLSOPT_TO_LE)
  5758. break;
  5759. layout->set_has_static_tls();
  5760. // Create a GOT entry for the tp-relative offset.
  5761. Output_data_got_aarch64<size, big_endian>* got
  5762. = target->got_section(symtab, layout);
  5763. if (!parameters->doing_static_link())
  5764. {
  5765. got->add_global_with_rel(
  5766. gsym, GOT_TYPE_TLS_OFFSET,
  5767. target->rela_dyn_section(layout),
  5768. elfcpp::R_AARCH64_TLS_TPREL64);
  5769. }
  5770. if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
  5771. {
  5772. got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
  5773. unsigned int got_offset =
  5774. gsym->got_offset(GOT_TYPE_TLS_OFFSET);
  5775. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  5776. gold_assert(addend == 0);
  5777. got->add_static_reloc(got_offset,
  5778. elfcpp::R_AARCH64_TLS_TPREL64, gsym);
  5779. }
  5780. }
  5781. break;
  5782. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
  5783. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
  5784. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
  5785. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
  5786. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
  5787. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
  5788. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
  5789. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
  5790. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12:
  5791. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
  5792. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12:
  5793. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
  5794. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12:
  5795. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
  5796. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12:
  5797. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: // Local executable
  5798. layout->set_has_static_tls();
  5799. if (parameters->options().shared())
  5800. gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
  5801. object->name().c_str(), r_type);
  5802. break;
  5803. case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
  5804. case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
  5805. case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
  5806. {
  5807. target->define_tls_base_symbol(symtab, layout);
  5808. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  5809. optimize_tls_reloc(gsym->final_value_is_known(), r_type);
  5810. if (tlsopt == tls::TLSOPT_NONE)
  5811. {
  5812. // Create reserved PLT and GOT entries for the resolver.
  5813. target->reserve_tlsdesc_entries(symtab, layout);
  5814. // Create a double GOT entry with an R_AARCH64_TLSDESC
  5815. // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
  5816. // entry needs to be in an area in .got.plt, not .got. Call
  5817. // got_section to make sure the section has been created.
  5818. target->got_section(symtab, layout);
  5819. Output_data_got<size, big_endian>* got =
  5820. target->got_tlsdesc_section();
  5821. Reloc_section* rt = target->rela_tlsdesc_section(layout);
  5822. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
  5823. elfcpp::R_AARCH64_TLSDESC, 0);
  5824. }
  5825. else if (tlsopt == tls::TLSOPT_TO_IE)
  5826. {
  5827. // Create a GOT entry for the tp-relative offset.
  5828. Output_data_got<size, big_endian>* got
  5829. = target->got_section(symtab, layout);
  5830. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  5831. target->rela_dyn_section(layout),
  5832. elfcpp::R_AARCH64_TLS_TPREL64);
  5833. }
  5834. else if (tlsopt != tls::TLSOPT_TO_LE)
  5835. unsupported_reloc_global(object, r_type, gsym);
  5836. }
  5837. break;
  5838. case elfcpp::R_AARCH64_TLSDESC_CALL:
  5839. break;
  5840. default:
  5841. gold_error(_("%s: unsupported reloc type in global scan"),
  5842. aarch64_reloc_property_table->
  5843. reloc_name_in_error_message(r_type).c_str());
  5844. }
  5845. return;
  5846. } // End of Scan::global
  5847. // Create the PLT section.
  5848. template<int size, bool big_endian>
  5849. void
  5850. Target_aarch64<size, big_endian>::make_plt_section(
  5851. Symbol_table* symtab, Layout* layout)
  5852. {
  5853. if (this->plt_ == NULL)
  5854. {
  5855. // Create the GOT section first.
  5856. this->got_section(symtab, layout);
  5857. this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
  5858. this->got_irelative_);
  5859. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  5860. (elfcpp::SHF_ALLOC
  5861. | elfcpp::SHF_EXECINSTR),
  5862. this->plt_, ORDER_PLT, false);
  5863. // Make the sh_info field of .rela.plt point to .plt.
  5864. Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
  5865. rela_plt_os->set_info_section(this->plt_->output_section());
  5866. }
  5867. }
  5868. // Return the section for TLSDESC relocations.
  5869. template<int size, bool big_endian>
  5870. typename Target_aarch64<size, big_endian>::Reloc_section*
  5871. Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
  5872. {
  5873. return this->plt_section()->rela_tlsdesc(layout);
  5874. }
  5875. // Create a PLT entry for a global symbol.
  5876. template<int size, bool big_endian>
  5877. void
  5878. Target_aarch64<size, big_endian>::make_plt_entry(
  5879. Symbol_table* symtab,
  5880. Layout* layout,
  5881. Symbol* gsym)
  5882. {
  5883. if (gsym->has_plt_offset())
  5884. return;
  5885. if (this->plt_ == NULL)
  5886. this->make_plt_section(symtab, layout);
  5887. this->plt_->add_entry(symtab, layout, gsym);
  5888. }
  5889. // Make a PLT entry for a local STT_GNU_IFUNC symbol.
  5890. template<int size, bool big_endian>
  5891. void
  5892. Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
  5893. Symbol_table* symtab, Layout* layout,
  5894. Sized_relobj_file<size, big_endian>* relobj,
  5895. unsigned int local_sym_index)
  5896. {
  5897. if (relobj->local_has_plt_offset(local_sym_index))
  5898. return;
  5899. if (this->plt_ == NULL)
  5900. this->make_plt_section(symtab, layout);
  5901. unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
  5902. relobj,
  5903. local_sym_index);
  5904. relobj->set_local_plt_offset(local_sym_index, plt_offset);
  5905. }
  5906. template<int size, bool big_endian>
  5907. void
  5908. Target_aarch64<size, big_endian>::gc_process_relocs(
  5909. Symbol_table* symtab,
  5910. Layout* layout,
  5911. Sized_relobj_file<size, big_endian>* object,
  5912. unsigned int data_shndx,
  5913. unsigned int sh_type,
  5914. const unsigned char* prelocs,
  5915. size_t reloc_count,
  5916. Output_section* output_section,
  5917. bool needs_special_offset_handling,
  5918. size_t local_symbol_count,
  5919. const unsigned char* plocal_symbols)
  5920. {
  5921. typedef Target_aarch64<size, big_endian> Aarch64;
  5922. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  5923. Classify_reloc;
  5924. if (sh_type == elfcpp::SHT_REL)
  5925. {
  5926. return;
  5927. }
  5928. gold::gc_process_relocs<size, big_endian, Aarch64, Scan, Classify_reloc>(
  5929. symtab,
  5930. layout,
  5931. this,
  5932. object,
  5933. data_shndx,
  5934. prelocs,
  5935. reloc_count,
  5936. output_section,
  5937. needs_special_offset_handling,
  5938. local_symbol_count,
  5939. plocal_symbols);
  5940. }
  5941. // Scan relocations for a section.
  5942. template<int size, bool big_endian>
  5943. void
  5944. Target_aarch64<size, big_endian>::scan_relocs(
  5945. Symbol_table* symtab,
  5946. Layout* layout,
  5947. Sized_relobj_file<size, big_endian>* object,
  5948. unsigned int data_shndx,
  5949. unsigned int sh_type,
  5950. const unsigned char* prelocs,
  5951. size_t reloc_count,
  5952. Output_section* output_section,
  5953. bool needs_special_offset_handling,
  5954. size_t local_symbol_count,
  5955. const unsigned char* plocal_symbols)
  5956. {
  5957. typedef Target_aarch64<size, big_endian> Aarch64;
  5958. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  5959. Classify_reloc;
  5960. if (sh_type == elfcpp::SHT_REL)
  5961. {
  5962. gold_error(_("%s: unsupported REL reloc section"),
  5963. object->name().c_str());
  5964. return;
  5965. }
  5966. gold::scan_relocs<size, big_endian, Aarch64, Scan, Classify_reloc>(
  5967. symtab,
  5968. layout,
  5969. this,
  5970. object,
  5971. data_shndx,
  5972. prelocs,
  5973. reloc_count,
  5974. output_section,
  5975. needs_special_offset_handling,
  5976. local_symbol_count,
  5977. plocal_symbols);
  5978. }
  5979. // Return the value to use for a dynamic which requires special
  5980. // treatment. This is how we support equality comparisons of function
  5981. // pointers across shared library boundaries, as described in the
  5982. // processor specific ABI supplement.
  5983. template<int size, bool big_endian>
  5984. uint64_t
  5985. Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
  5986. {
  5987. gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
  5988. return this->plt_address_for_global(gsym);
  5989. }
  5990. // Finalize the sections.
  5991. template<int size, bool big_endian>
  5992. void
  5993. Target_aarch64<size, big_endian>::do_finalize_sections(
  5994. Layout* layout,
  5995. const Input_objects*,
  5996. Symbol_table* symtab)
  5997. {
  5998. const Reloc_section* rel_plt = (this->plt_ == NULL
  5999. ? NULL
  6000. : this->plt_->rela_plt());
  6001. layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
  6002. this->rela_dyn_, true, false);
  6003. // Emit any relocs we saved in an attempt to avoid generating COPY
  6004. // relocs.
  6005. if (this->copy_relocs_.any_saved_relocs())
  6006. this->copy_relocs_.emit(this->rela_dyn_section(layout));
  6007. // Fill in some more dynamic tags.
  6008. Output_data_dynamic* const odyn = layout->dynamic_data();
  6009. if (odyn != NULL)
  6010. {
  6011. if (this->plt_ != NULL
  6012. && this->plt_->output_section() != NULL
  6013. && this->plt_ ->has_tlsdesc_entry())
  6014. {
  6015. unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
  6016. unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
  6017. this->got_->finalize_data_size();
  6018. odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
  6019. this->plt_, plt_offset);
  6020. odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
  6021. this->got_, got_offset);
  6022. }
  6023. }
  6024. // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
  6025. // the .got section.
  6026. Symbol* sym = this->global_offset_table_;
  6027. if (sym != NULL)
  6028. {
  6029. uint64_t data_size = this->got_->current_data_size();
  6030. symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
  6031. // If the .got section is more than 0x8000 bytes, we add
  6032. // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
  6033. // bit relocations have a greater chance of working.
  6034. if (data_size >= 0x8000)
  6035. symtab->get_sized_symbol<size>(sym)->set_value(
  6036. symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
  6037. }
  6038. if (parameters->doing_static_link()
  6039. && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
  6040. {
  6041. // If linking statically, make sure that the __rela_iplt symbols
  6042. // were defined if necessary, even if we didn't create a PLT.
  6043. static const Define_symbol_in_segment syms[] =
  6044. {
  6045. {
  6046. "__rela_iplt_start", // name
  6047. elfcpp::PT_LOAD, // segment_type
  6048. elfcpp::PF_W, // segment_flags_set
  6049. elfcpp::PF(0), // segment_flags_clear
  6050. 0, // value
  6051. 0, // size
  6052. elfcpp::STT_NOTYPE, // type
  6053. elfcpp::STB_GLOBAL, // binding
  6054. elfcpp::STV_HIDDEN, // visibility
  6055. 0, // nonvis
  6056. Symbol::SEGMENT_START, // offset_from_base
  6057. true // only_if_ref
  6058. },
  6059. {
  6060. "__rela_iplt_end", // name
  6061. elfcpp::PT_LOAD, // segment_type
  6062. elfcpp::PF_W, // segment_flags_set
  6063. elfcpp::PF(0), // segment_flags_clear
  6064. 0, // value
  6065. 0, // size
  6066. elfcpp::STT_NOTYPE, // type
  6067. elfcpp::STB_GLOBAL, // binding
  6068. elfcpp::STV_HIDDEN, // visibility
  6069. 0, // nonvis
  6070. Symbol::SEGMENT_START, // offset_from_base
  6071. true // only_if_ref
  6072. }
  6073. };
  6074. symtab->define_symbols(layout, 2, syms,
  6075. layout->script_options()->saw_sections_clause());
  6076. }
  6077. return;
  6078. }
  6079. // Perform a relocation.
  6080. template<int size, bool big_endian>
  6081. inline bool
  6082. Target_aarch64<size, big_endian>::Relocate::relocate(
  6083. const Relocate_info<size, big_endian>* relinfo,
  6084. unsigned int,
  6085. Target_aarch64<size, big_endian>* target,
  6086. Output_section* ,
  6087. size_t relnum,
  6088. const unsigned char* preloc,
  6089. const Sized_symbol<size>* gsym,
  6090. const Symbol_value<size>* psymval,
  6091. unsigned char* view,
  6092. typename elfcpp::Elf_types<size>::Elf_Addr address,
  6093. section_size_type /* view_size */)
  6094. {
  6095. if (view == NULL)
  6096. return true;
  6097. typedef AArch64_relocate_functions<size, big_endian> Reloc;
  6098. const elfcpp::Rela<size, big_endian> rela(preloc);
  6099. unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
  6100. const AArch64_reloc_property* reloc_property =
  6101. aarch64_reloc_property_table->get_reloc_property(r_type);
  6102. if (reloc_property == NULL)
  6103. {
  6104. std::string reloc_name =
  6105. aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
  6106. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6107. _("cannot relocate %s in object file"),
  6108. reloc_name.c_str());
  6109. return true;
  6110. }
  6111. const Sized_relobj_file<size, big_endian>* object = relinfo->object;
  6112. // Pick the value to use for symbols defined in the PLT.
  6113. Symbol_value<size> symval;
  6114. if (gsym != NULL
  6115. && gsym->use_plt_offset(reloc_property->reference_flags()))
  6116. {
  6117. symval.set_output_value(target->plt_address_for_global(gsym));
  6118. psymval = &symval;
  6119. }
  6120. else if (gsym == NULL && psymval->is_ifunc_symbol())
  6121. {
  6122. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6123. if (object->local_has_plt_offset(r_sym))
  6124. {
  6125. symval.set_output_value(target->plt_address_for_local(object, r_sym));
  6126. psymval = &symval;
  6127. }
  6128. }
  6129. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  6130. // Get the GOT offset if needed.
  6131. // For aarch64, the GOT pointer points to the start of the GOT section.
  6132. bool have_got_offset = false;
  6133. int got_offset = 0;
  6134. int got_base = (target->got_ != NULL
  6135. ? (target->got_->current_data_size() >= 0x8000
  6136. ? 0x8000 : 0)
  6137. : 0);
  6138. switch (r_type)
  6139. {
  6140. case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
  6141. case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
  6142. case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
  6143. case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
  6144. case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
  6145. case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
  6146. case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
  6147. case elfcpp::R_AARCH64_GOTREL64:
  6148. case elfcpp::R_AARCH64_GOTREL32:
  6149. case elfcpp::R_AARCH64_GOT_LD_PREL19:
  6150. case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
  6151. case elfcpp::R_AARCH64_ADR_GOT_PAGE:
  6152. case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
  6153. case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
  6154. if (gsym != NULL)
  6155. {
  6156. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  6157. got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
  6158. }
  6159. else
  6160. {
  6161. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6162. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
  6163. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
  6164. - got_base);
  6165. }
  6166. have_got_offset = true;
  6167. break;
  6168. default:
  6169. break;
  6170. }
  6171. typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
  6172. typename elfcpp::Elf_types<size>::Elf_Addr value;
  6173. switch (r_type)
  6174. {
  6175. case elfcpp::R_AARCH64_NONE:
  6176. break;
  6177. case elfcpp::R_AARCH64_ABS64:
  6178. if (!parameters->options().apply_dynamic_relocs()
  6179. && parameters->options().output_is_position_independent()
  6180. && gsym != NULL
  6181. && gsym->needs_dynamic_reloc(reloc_property->reference_flags())
  6182. && !gsym->can_use_relative_reloc(false))
  6183. // We have generated an absolute dynamic relocation, so do not
  6184. // apply the relocation statically. (Works around bugs in older
  6185. // Android dynamic linkers.)
  6186. break;
  6187. reloc_status = Reloc::template rela_ua<64>(
  6188. view, object, psymval, addend, reloc_property);
  6189. break;
  6190. case elfcpp::R_AARCH64_ABS32:
  6191. if (!parameters->options().apply_dynamic_relocs()
  6192. && parameters->options().output_is_position_independent()
  6193. && gsym != NULL
  6194. && gsym->needs_dynamic_reloc(reloc_property->reference_flags()))
  6195. // We have generated an absolute dynamic relocation, so do not
  6196. // apply the relocation statically. (Works around bugs in older
  6197. // Android dynamic linkers.)
  6198. break;
  6199. reloc_status = Reloc::template rela_ua<32>(
  6200. view, object, psymval, addend, reloc_property);
  6201. break;
  6202. case elfcpp::R_AARCH64_ABS16:
  6203. if (!parameters->options().apply_dynamic_relocs()
  6204. && parameters->options().output_is_position_independent()
  6205. && gsym != NULL
  6206. && gsym->needs_dynamic_reloc(reloc_property->reference_flags()))
  6207. // We have generated an absolute dynamic relocation, so do not
  6208. // apply the relocation statically. (Works around bugs in older
  6209. // Android dynamic linkers.)
  6210. break;
  6211. reloc_status = Reloc::template rela_ua<16>(
  6212. view, object, psymval, addend, reloc_property);
  6213. break;
  6214. case elfcpp::R_AARCH64_PREL64:
  6215. reloc_status = Reloc::template pcrela_ua<64>(
  6216. view, object, psymval, addend, address, reloc_property);
  6217. break;
  6218. case elfcpp::R_AARCH64_PREL32:
  6219. reloc_status = Reloc::template pcrela_ua<32>(
  6220. view, object, psymval, addend, address, reloc_property);
  6221. break;
  6222. case elfcpp::R_AARCH64_PREL16:
  6223. reloc_status = Reloc::template pcrela_ua<16>(
  6224. view, object, psymval, addend, address, reloc_property);
  6225. break;
  6226. case elfcpp::R_AARCH64_MOVW_UABS_G0:
  6227. case elfcpp::R_AARCH64_MOVW_UABS_G0_NC:
  6228. case elfcpp::R_AARCH64_MOVW_UABS_G1:
  6229. case elfcpp::R_AARCH64_MOVW_UABS_G1_NC:
  6230. case elfcpp::R_AARCH64_MOVW_UABS_G2:
  6231. case elfcpp::R_AARCH64_MOVW_UABS_G2_NC:
  6232. case elfcpp::R_AARCH64_MOVW_UABS_G3:
  6233. reloc_status = Reloc::template rela_general<32>(
  6234. view, object, psymval, addend, reloc_property);
  6235. break;
  6236. case elfcpp::R_AARCH64_MOVW_SABS_G0:
  6237. case elfcpp::R_AARCH64_MOVW_SABS_G1:
  6238. case elfcpp::R_AARCH64_MOVW_SABS_G2:
  6239. reloc_status = Reloc::movnz(view, psymval->value(object, addend),
  6240. reloc_property);
  6241. break;
  6242. case elfcpp::R_AARCH64_LD_PREL_LO19:
  6243. reloc_status = Reloc::template pcrela_general<32>(
  6244. view, object, psymval, addend, address, reloc_property);
  6245. break;
  6246. case elfcpp::R_AARCH64_ADR_PREL_LO21:
  6247. reloc_status = Reloc::adr(view, object, psymval, addend,
  6248. address, reloc_property);
  6249. break;
  6250. case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
  6251. case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
  6252. reloc_status = Reloc::adrp(view, object, psymval, addend, address,
  6253. reloc_property);
  6254. break;
  6255. case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
  6256. case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
  6257. case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
  6258. case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
  6259. case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
  6260. case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
  6261. reloc_status = Reloc::template rela_general<32>(
  6262. view, object, psymval, addend, reloc_property);
  6263. break;
  6264. case elfcpp::R_AARCH64_CALL26:
  6265. if (this->skip_call_tls_get_addr_)
  6266. {
  6267. // Double check that the TLSGD insn has been optimized away.
  6268. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  6269. Insntype insn = elfcpp::Swap<32, big_endian>::readval(
  6270. reinterpret_cast<Insntype*>(view));
  6271. gold_assert((insn & 0xff000000) == 0x91000000);
  6272. reloc_status = Reloc::STATUS_OKAY;
  6273. this->skip_call_tls_get_addr_ = false;
  6274. // Return false to stop further processing this reloc.
  6275. return false;
  6276. }
  6277. // Fall through.
  6278. case elfcpp::R_AARCH64_JUMP26:
  6279. if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
  6280. gsym, psymval, object,
  6281. target->stub_group_size_))
  6282. break;
  6283. // Fall through.
  6284. case elfcpp::R_AARCH64_TSTBR14:
  6285. case elfcpp::R_AARCH64_CONDBR19:
  6286. reloc_status = Reloc::template pcrela_general<32>(
  6287. view, object, psymval, addend, address, reloc_property);
  6288. break;
  6289. case elfcpp::R_AARCH64_ADR_GOT_PAGE:
  6290. gold_assert(have_got_offset);
  6291. value = target->got_->address() + got_base + got_offset;
  6292. reloc_status = Reloc::adrp(view, value + addend, address);
  6293. break;
  6294. case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
  6295. gold_assert(have_got_offset);
  6296. value = target->got_->address() + got_base + got_offset;
  6297. reloc_status = Reloc::template rela_general<32>(
  6298. view, value, addend, reloc_property);
  6299. break;
  6300. case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
  6301. {
  6302. gold_assert(have_got_offset);
  6303. value = target->got_->address() + got_base + got_offset + addend -
  6304. Reloc::Page(target->got_->address() + got_base);
  6305. if ((value & 7) != 0)
  6306. reloc_status = Reloc::STATUS_OVERFLOW;
  6307. else
  6308. reloc_status = Reloc::template reloc_common<32>(
  6309. view, value, reloc_property);
  6310. break;
  6311. }
  6312. case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
  6313. case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
  6314. case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
  6315. case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
  6316. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
  6317. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
  6318. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
  6319. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
  6320. case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
  6321. case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
  6322. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
  6323. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
  6324. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
  6325. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
  6326. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
  6327. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
  6328. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
  6329. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
  6330. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12:
  6331. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
  6332. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12:
  6333. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
  6334. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12:
  6335. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
  6336. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12:
  6337. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
  6338. case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
  6339. case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
  6340. case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
  6341. case elfcpp::R_AARCH64_TLSDESC_CALL:
  6342. reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
  6343. gsym, psymval, view, address);
  6344. break;
  6345. // These are dynamic relocations, which are unexpected when linking.
  6346. case elfcpp::R_AARCH64_COPY:
  6347. case elfcpp::R_AARCH64_GLOB_DAT:
  6348. case elfcpp::R_AARCH64_JUMP_SLOT:
  6349. case elfcpp::R_AARCH64_RELATIVE:
  6350. case elfcpp::R_AARCH64_IRELATIVE:
  6351. case elfcpp::R_AARCH64_TLS_DTPREL64:
  6352. case elfcpp::R_AARCH64_TLS_DTPMOD64:
  6353. case elfcpp::R_AARCH64_TLS_TPREL64:
  6354. case elfcpp::R_AARCH64_TLSDESC:
  6355. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6356. _("unexpected reloc %u in object file"),
  6357. r_type);
  6358. break;
  6359. default:
  6360. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6361. _("unsupported reloc %s"),
  6362. reloc_property->name().c_str());
  6363. break;
  6364. }
  6365. // Report any errors.
  6366. switch (reloc_status)
  6367. {
  6368. case Reloc::STATUS_OKAY:
  6369. break;
  6370. case Reloc::STATUS_OVERFLOW:
  6371. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6372. _("relocation overflow in %s"),
  6373. reloc_property->name().c_str());
  6374. break;
  6375. case Reloc::STATUS_BAD_RELOC:
  6376. gold_error_at_location(
  6377. relinfo,
  6378. relnum,
  6379. rela.get_r_offset(),
  6380. _("unexpected opcode while processing relocation %s"),
  6381. reloc_property->name().c_str());
  6382. break;
  6383. default:
  6384. gold_unreachable();
  6385. }
  6386. return true;
  6387. }
  6388. template<int size, bool big_endian>
  6389. inline
  6390. typename AArch64_relocate_functions<size, big_endian>::Status
  6391. Target_aarch64<size, big_endian>::Relocate::relocate_tls(
  6392. const Relocate_info<size, big_endian>* relinfo,
  6393. Target_aarch64<size, big_endian>* target,
  6394. size_t relnum,
  6395. const elfcpp::Rela<size, big_endian>& rela,
  6396. unsigned int r_type, const Sized_symbol<size>* gsym,
  6397. const Symbol_value<size>* psymval,
  6398. unsigned char* view,
  6399. typename elfcpp::Elf_types<size>::Elf_Addr address)
  6400. {
  6401. typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
  6402. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  6403. Output_segment* tls_segment = relinfo->layout->tls_segment();
  6404. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  6405. const AArch64_reloc_property* reloc_property =
  6406. aarch64_reloc_property_table->get_reloc_property(r_type);
  6407. gold_assert(reloc_property != NULL);
  6408. const bool is_final = (gsym == NULL
  6409. ? !parameters->options().shared()
  6410. : gsym->final_value_is_known());
  6411. tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
  6412. optimize_tls_reloc(is_final, r_type);
  6413. Sized_relobj_file<size, big_endian>* object = relinfo->object;
  6414. int tls_got_offset_type;
  6415. switch (r_type)
  6416. {
  6417. case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
  6418. case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
  6419. {
  6420. if (tlsopt == tls::TLSOPT_TO_LE)
  6421. {
  6422. if (tls_segment == NULL)
  6423. {
  6424. gold_assert(parameters->errors()->error_count() > 0
  6425. || issue_undefined_symbol_error(gsym));
  6426. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6427. }
  6428. return tls_gd_to_le(relinfo, target, rela, r_type, view,
  6429. psymval);
  6430. }
  6431. else if (tlsopt == tls::TLSOPT_NONE)
  6432. {
  6433. tls_got_offset_type = GOT_TYPE_TLS_PAIR;
  6434. // Firstly get the address for the got entry.
  6435. typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
  6436. if (gsym != NULL)
  6437. {
  6438. gold_assert(gsym->has_got_offset(tls_got_offset_type));
  6439. got_entry_address = target->got_->address() +
  6440. gsym->got_offset(tls_got_offset_type);
  6441. }
  6442. else
  6443. {
  6444. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6445. gold_assert(
  6446. object->local_has_got_offset(r_sym, tls_got_offset_type));
  6447. got_entry_address = target->got_->address() +
  6448. object->local_got_offset(r_sym, tls_got_offset_type);
  6449. }
  6450. // Relocate the address into adrp/ld, adrp/add pair.
  6451. switch (r_type)
  6452. {
  6453. case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
  6454. return aarch64_reloc_funcs::adrp(
  6455. view, got_entry_address + addend, address);
  6456. break;
  6457. case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
  6458. return aarch64_reloc_funcs::template rela_general<32>(
  6459. view, got_entry_address, addend, reloc_property);
  6460. break;
  6461. default:
  6462. gold_unreachable();
  6463. }
  6464. }
  6465. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  6466. _("unsupported gd_to_ie relaxation on %u"),
  6467. r_type);
  6468. }
  6469. break;
  6470. case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
  6471. case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
  6472. {
  6473. if (tlsopt == tls::TLSOPT_TO_LE)
  6474. {
  6475. if (tls_segment == NULL)
  6476. {
  6477. gold_assert(parameters->errors()->error_count() > 0
  6478. || issue_undefined_symbol_error(gsym));
  6479. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6480. }
  6481. return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
  6482. psymval);
  6483. }
  6484. gold_assert(tlsopt == tls::TLSOPT_NONE);
  6485. // Relocate the field with the offset of the GOT entry for
  6486. // the module index.
  6487. typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
  6488. got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
  6489. target->got_->address());
  6490. switch (r_type)
  6491. {
  6492. case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
  6493. return aarch64_reloc_funcs::adrp(
  6494. view, got_entry_address + addend, address);
  6495. break;
  6496. case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
  6497. return aarch64_reloc_funcs::template rela_general<32>(
  6498. view, got_entry_address, addend, reloc_property);
  6499. break;
  6500. default:
  6501. gold_unreachable();
  6502. }
  6503. }
  6504. break;
  6505. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
  6506. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
  6507. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
  6508. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
  6509. {
  6510. AArch64_address value = psymval->value(object, 0);
  6511. if (tlsopt == tls::TLSOPT_TO_LE)
  6512. {
  6513. if (tls_segment == NULL)
  6514. {
  6515. gold_assert(parameters->errors()->error_count() > 0
  6516. || issue_undefined_symbol_error(gsym));
  6517. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6518. }
  6519. }
  6520. switch (r_type)
  6521. {
  6522. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
  6523. return aarch64_reloc_funcs::movnz(view, value + addend,
  6524. reloc_property);
  6525. break;
  6526. case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
  6527. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
  6528. case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
  6529. return aarch64_reloc_funcs::template rela_general<32>(
  6530. view, value, addend, reloc_property);
  6531. break;
  6532. default:
  6533. gold_unreachable();
  6534. }
  6535. // We should never reach here.
  6536. }
  6537. break;
  6538. case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
  6539. case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
  6540. {
  6541. if (tlsopt == tls::TLSOPT_TO_LE)
  6542. {
  6543. if (tls_segment == NULL)
  6544. {
  6545. gold_assert(parameters->errors()->error_count() > 0
  6546. || issue_undefined_symbol_error(gsym));
  6547. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6548. }
  6549. return tls_ie_to_le(relinfo, target, rela, r_type, view,
  6550. psymval);
  6551. }
  6552. tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
  6553. // Firstly get the address for the got entry.
  6554. typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
  6555. if (gsym != NULL)
  6556. {
  6557. gold_assert(gsym->has_got_offset(tls_got_offset_type));
  6558. got_entry_address = target->got_->address() +
  6559. gsym->got_offset(tls_got_offset_type);
  6560. }
  6561. else
  6562. {
  6563. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6564. gold_assert(
  6565. object->local_has_got_offset(r_sym, tls_got_offset_type));
  6566. got_entry_address = target->got_->address() +
  6567. object->local_got_offset(r_sym, tls_got_offset_type);
  6568. }
  6569. // Relocate the address into adrp/ld, adrp/add pair.
  6570. switch (r_type)
  6571. {
  6572. case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
  6573. return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
  6574. address);
  6575. break;
  6576. case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
  6577. return aarch64_reloc_funcs::template rela_general<32>(
  6578. view, got_entry_address, addend, reloc_property);
  6579. default:
  6580. gold_unreachable();
  6581. }
  6582. }
  6583. // We shall never reach here.
  6584. break;
  6585. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
  6586. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
  6587. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
  6588. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
  6589. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
  6590. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
  6591. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
  6592. case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
  6593. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12:
  6594. case elfcpp::R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
  6595. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12:
  6596. case elfcpp::R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
  6597. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12:
  6598. case elfcpp::R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
  6599. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12:
  6600. case elfcpp::R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
  6601. {
  6602. gold_assert(tls_segment != NULL);
  6603. AArch64_address value = psymval->value(object, 0);
  6604. if (!parameters->options().shared())
  6605. {
  6606. AArch64_address aligned_tcb_size =
  6607. align_address(target->tcb_size(),
  6608. tls_segment->maximum_alignment());
  6609. value += aligned_tcb_size;
  6610. switch (r_type)
  6611. {
  6612. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
  6613. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
  6614. case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
  6615. return aarch64_reloc_funcs::movnz(view, value + addend,
  6616. reloc_property);
  6617. default:
  6618. return aarch64_reloc_funcs::template
  6619. rela_general<32>(view,
  6620. value,
  6621. addend,
  6622. reloc_property);
  6623. }
  6624. }
  6625. else
  6626. gold_error(_("%s: unsupported reloc %u "
  6627. "in non-static TLSLE mode."),
  6628. object->name().c_str(), r_type);
  6629. }
  6630. break;
  6631. case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
  6632. case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
  6633. case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
  6634. case elfcpp::R_AARCH64_TLSDESC_CALL:
  6635. {
  6636. if (tlsopt == tls::TLSOPT_TO_LE)
  6637. {
  6638. if (tls_segment == NULL)
  6639. {
  6640. gold_assert(parameters->errors()->error_count() > 0
  6641. || issue_undefined_symbol_error(gsym));
  6642. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6643. }
  6644. return tls_desc_gd_to_le(relinfo, target, rela, r_type,
  6645. view, psymval);
  6646. }
  6647. else
  6648. {
  6649. tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
  6650. ? GOT_TYPE_TLS_OFFSET
  6651. : GOT_TYPE_TLS_DESC);
  6652. int got_tlsdesc_offset = 0;
  6653. if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
  6654. && tlsopt == tls::TLSOPT_NONE)
  6655. {
  6656. // We created GOT entries in the .got.tlsdesc portion of the
  6657. // .got.plt section, but the offset stored in the symbol is the
  6658. // offset within .got.tlsdesc.
  6659. got_tlsdesc_offset = (target->got_tlsdesc_->address()
  6660. - target->got_->address());
  6661. }
  6662. typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
  6663. if (gsym != NULL)
  6664. {
  6665. gold_assert(gsym->has_got_offset(tls_got_offset_type));
  6666. got_entry_address = target->got_->address()
  6667. + got_tlsdesc_offset
  6668. + gsym->got_offset(tls_got_offset_type);
  6669. }
  6670. else
  6671. {
  6672. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  6673. gold_assert(
  6674. object->local_has_got_offset(r_sym, tls_got_offset_type));
  6675. got_entry_address = target->got_->address() +
  6676. got_tlsdesc_offset +
  6677. object->local_got_offset(r_sym, tls_got_offset_type);
  6678. }
  6679. if (tlsopt == tls::TLSOPT_TO_IE)
  6680. {
  6681. return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
  6682. view, psymval, got_entry_address,
  6683. address);
  6684. }
  6685. // Now do tlsdesc relocation.
  6686. switch (r_type)
  6687. {
  6688. case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
  6689. return aarch64_reloc_funcs::adrp(view,
  6690. got_entry_address + addend,
  6691. address);
  6692. break;
  6693. case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
  6694. case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
  6695. return aarch64_reloc_funcs::template rela_general<32>(
  6696. view, got_entry_address, addend, reloc_property);
  6697. break;
  6698. case elfcpp::R_AARCH64_TLSDESC_CALL:
  6699. return aarch64_reloc_funcs::STATUS_OKAY;
  6700. break;
  6701. default:
  6702. gold_unreachable();
  6703. }
  6704. }
  6705. }
  6706. break;
  6707. default:
  6708. gold_error(_("%s: unsupported TLS reloc %u."),
  6709. object->name().c_str(), r_type);
  6710. }
  6711. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6712. } // End of relocate_tls.
  6713. template<int size, bool big_endian>
  6714. inline
  6715. typename AArch64_relocate_functions<size, big_endian>::Status
  6716. Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
  6717. const Relocate_info<size, big_endian>* relinfo,
  6718. Target_aarch64<size, big_endian>* target,
  6719. const elfcpp::Rela<size, big_endian>& rela,
  6720. unsigned int r_type,
  6721. unsigned char* view,
  6722. const Symbol_value<size>* psymval)
  6723. {
  6724. typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
  6725. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  6726. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  6727. Insntype* ip = reinterpret_cast<Insntype*>(view);
  6728. Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
  6729. Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
  6730. Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
  6731. if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
  6732. {
  6733. // This is the 2nd relocs, optimization should already have been
  6734. // done.
  6735. gold_assert((insn1 & 0xfff00000) == 0x91400000);
  6736. return aarch64_reloc_funcs::STATUS_OKAY;
  6737. }
  6738. // The original sequence is -
  6739. // 90000000 adrp x0, 0 <main>
  6740. // 91000000 add x0, x0, #0x0
  6741. // 94000000 bl 0 <__tls_get_addr>
  6742. // optimized to sequence -
  6743. // d53bd040 mrs x0, tpidr_el0
  6744. // 91400000 add x0, x0, #0x0, lsl #12
  6745. // 91000000 add x0, x0, #0x0
  6746. // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
  6747. // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
  6748. // have to change "bl tls_get_addr", which does not have a corresponding tls
  6749. // relocation type. So before proceeding, we need to make sure compiler
  6750. // does not change the sequence.
  6751. if(!(insn1 == 0x90000000 // adrp x0,0
  6752. && insn2 == 0x91000000 // add x0, x0, #0x0
  6753. && insn3 == 0x94000000)) // bl 0
  6754. {
  6755. // Ideally we should give up gd_to_le relaxation and do gd access.
  6756. // However the gd_to_le relaxation decision has been made early
  6757. // in the scan stage, where we did not allocate any GOT entry for
  6758. // this symbol. Therefore we have to exit and report error now.
  6759. gold_error(_("unexpected reloc insn sequence while relaxing "
  6760. "tls gd to le for reloc %u."), r_type);
  6761. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6762. }
  6763. // Write new insns.
  6764. insn1 = 0xd53bd040; // mrs x0, tpidr_el0
  6765. insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
  6766. insn3 = 0x91000000; // add x0, x0, #0x0
  6767. elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
  6768. elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
  6769. elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
  6770. // Calculate tprel value.
  6771. Output_segment* tls_segment = relinfo->layout->tls_segment();
  6772. gold_assert(tls_segment != NULL);
  6773. AArch64_address value = psymval->value(relinfo->object, 0);
  6774. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  6775. AArch64_address aligned_tcb_size =
  6776. align_address(target->tcb_size(), tls_segment->maximum_alignment());
  6777. AArch64_address x = value + aligned_tcb_size;
  6778. // After new insns are written, apply TLSLE relocs.
  6779. const AArch64_reloc_property* rp1 =
  6780. aarch64_reloc_property_table->get_reloc_property(
  6781. elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
  6782. const AArch64_reloc_property* rp2 =
  6783. aarch64_reloc_property_table->get_reloc_property(
  6784. elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
  6785. gold_assert(rp1 != NULL && rp2 != NULL);
  6786. typename aarch64_reloc_funcs::Status s1 =
  6787. aarch64_reloc_funcs::template rela_general<32>(view + 4,
  6788. x,
  6789. addend,
  6790. rp1);
  6791. if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
  6792. return s1;
  6793. typename aarch64_reloc_funcs::Status s2 =
  6794. aarch64_reloc_funcs::template rela_general<32>(view + 8,
  6795. x,
  6796. addend,
  6797. rp2);
  6798. this->skip_call_tls_get_addr_ = true;
  6799. return s2;
  6800. } // End of tls_gd_to_le
  6801. template<int size, bool big_endian>
  6802. inline
  6803. typename AArch64_relocate_functions<size, big_endian>::Status
  6804. Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
  6805. const Relocate_info<size, big_endian>* relinfo,
  6806. Target_aarch64<size, big_endian>* target,
  6807. const elfcpp::Rela<size, big_endian>& rela,
  6808. unsigned int r_type,
  6809. unsigned char* view,
  6810. const Symbol_value<size>* psymval)
  6811. {
  6812. typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
  6813. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  6814. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  6815. Insntype* ip = reinterpret_cast<Insntype*>(view);
  6816. Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
  6817. Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
  6818. Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
  6819. if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
  6820. {
  6821. // This is the 2nd relocs, optimization should already have been
  6822. // done.
  6823. gold_assert((insn1 & 0xfff00000) == 0x91400000);
  6824. return aarch64_reloc_funcs::STATUS_OKAY;
  6825. }
  6826. // The original sequence is -
  6827. // 90000000 adrp x0, 0 <main>
  6828. // 91000000 add x0, x0, #0x0
  6829. // 94000000 bl 0 <__tls_get_addr>
  6830. // optimized to sequence -
  6831. // d53bd040 mrs x0, tpidr_el0
  6832. // 91400000 add x0, x0, #0x0, lsl #12
  6833. // 91000000 add x0, x0, #0x0
  6834. // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
  6835. // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
  6836. // have to change "bl tls_get_addr", which does not have a corresponding tls
  6837. // relocation type. So before proceeding, we need to make sure compiler
  6838. // does not change the sequence.
  6839. if(!(insn1 == 0x90000000 // adrp x0,0
  6840. && insn2 == 0x91000000 // add x0, x0, #0x0
  6841. && insn3 == 0x94000000)) // bl 0
  6842. {
  6843. // Ideally we should give up gd_to_le relaxation and do gd access.
  6844. // However the gd_to_le relaxation decision has been made early
  6845. // in the scan stage, where we did not allocate a GOT entry for
  6846. // this symbol. Therefore we have to exit and report an error now.
  6847. gold_error(_("unexpected reloc insn sequence while relaxing "
  6848. "tls gd to le for reloc %u."), r_type);
  6849. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6850. }
  6851. // Write new insns.
  6852. insn1 = 0xd53bd040; // mrs x0, tpidr_el0
  6853. insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
  6854. insn3 = 0x91000000; // add x0, x0, #0x0
  6855. elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
  6856. elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
  6857. elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
  6858. // Calculate tprel value.
  6859. Output_segment* tls_segment = relinfo->layout->tls_segment();
  6860. gold_assert(tls_segment != NULL);
  6861. AArch64_address value = psymval->value(relinfo->object, 0);
  6862. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  6863. AArch64_address aligned_tcb_size =
  6864. align_address(target->tcb_size(), tls_segment->maximum_alignment());
  6865. AArch64_address x = value + aligned_tcb_size;
  6866. // After new insns are written, apply TLSLE relocs.
  6867. const AArch64_reloc_property* rp1 =
  6868. aarch64_reloc_property_table->get_reloc_property(
  6869. elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
  6870. const AArch64_reloc_property* rp2 =
  6871. aarch64_reloc_property_table->get_reloc_property(
  6872. elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
  6873. gold_assert(rp1 != NULL && rp2 != NULL);
  6874. typename aarch64_reloc_funcs::Status s1 =
  6875. aarch64_reloc_funcs::template rela_general<32>(view + 4,
  6876. x,
  6877. addend,
  6878. rp1);
  6879. if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
  6880. return s1;
  6881. typename aarch64_reloc_funcs::Status s2 =
  6882. aarch64_reloc_funcs::template rela_general<32>(view + 8,
  6883. x,
  6884. addend,
  6885. rp2);
  6886. this->skip_call_tls_get_addr_ = true;
  6887. return s2;
  6888. } // End of tls_ld_to_le
  6889. template<int size, bool big_endian>
  6890. inline
  6891. typename AArch64_relocate_functions<size, big_endian>::Status
  6892. Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
  6893. const Relocate_info<size, big_endian>* relinfo,
  6894. Target_aarch64<size, big_endian>* target,
  6895. const elfcpp::Rela<size, big_endian>& rela,
  6896. unsigned int r_type,
  6897. unsigned char* view,
  6898. const Symbol_value<size>* psymval)
  6899. {
  6900. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  6901. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  6902. typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
  6903. AArch64_address value = psymval->value(relinfo->object, 0);
  6904. Output_segment* tls_segment = relinfo->layout->tls_segment();
  6905. AArch64_address aligned_tcb_address =
  6906. align_address(target->tcb_size(), tls_segment->maximum_alignment());
  6907. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  6908. AArch64_address x = value + addend + aligned_tcb_address;
  6909. // "x" is the offset to tp, we can only do this if x is within
  6910. // range [0, 2^32-1]
  6911. if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
  6912. {
  6913. gold_error(_("TLS variable referred by reloc %u is too far from TP."),
  6914. r_type);
  6915. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6916. }
  6917. Insntype* ip = reinterpret_cast<Insntype*>(view);
  6918. Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
  6919. unsigned int regno;
  6920. Insntype newinsn;
  6921. if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
  6922. {
  6923. // Generate movz.
  6924. regno = (insn & 0x1f);
  6925. newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
  6926. }
  6927. else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
  6928. {
  6929. // Generate movk.
  6930. regno = (insn & 0x1f);
  6931. gold_assert(regno == ((insn >> 5) & 0x1f));
  6932. newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
  6933. }
  6934. else
  6935. gold_unreachable();
  6936. elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
  6937. return aarch64_reloc_funcs::STATUS_OKAY;
  6938. } // End of tls_ie_to_le
  6939. template<int size, bool big_endian>
  6940. inline
  6941. typename AArch64_relocate_functions<size, big_endian>::Status
  6942. Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
  6943. const Relocate_info<size, big_endian>* relinfo,
  6944. Target_aarch64<size, big_endian>* target,
  6945. const elfcpp::Rela<size, big_endian>& rela,
  6946. unsigned int r_type,
  6947. unsigned char* view,
  6948. const Symbol_value<size>* psymval)
  6949. {
  6950. typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
  6951. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  6952. typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
  6953. // TLSDESC-GD sequence is like:
  6954. // adrp x0, :tlsdesc:v1
  6955. // ldr x1, [x0, #:tlsdesc_lo12:v1]
  6956. // add x0, x0, :tlsdesc_lo12:v1
  6957. // .tlsdesccall v1
  6958. // blr x1
  6959. // After desc_gd_to_le optimization, the sequence will be like:
  6960. // movz x0, #0x0, lsl #16
  6961. // movk x0, #0x10
  6962. // nop
  6963. // nop
  6964. // Calculate tprel value.
  6965. Output_segment* tls_segment = relinfo->layout->tls_segment();
  6966. gold_assert(tls_segment != NULL);
  6967. Insntype* ip = reinterpret_cast<Insntype*>(view);
  6968. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  6969. AArch64_address value = psymval->value(relinfo->object, addend);
  6970. AArch64_address aligned_tcb_size =
  6971. align_address(target->tcb_size(), tls_segment->maximum_alignment());
  6972. AArch64_address x = value + aligned_tcb_size;
  6973. // x is the offset to tp, we can only do this if x is within range
  6974. // [0, 2^32-1]. If x is out of range, fail and exit.
  6975. if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
  6976. {
  6977. gold_error(_("TLS variable referred by reloc %u is too far from TP. "
  6978. "We Can't do gd_to_le relaxation.\n"), r_type);
  6979. return aarch64_reloc_funcs::STATUS_BAD_RELOC;
  6980. }
  6981. Insntype newinsn;
  6982. switch (r_type)
  6983. {
  6984. case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
  6985. case elfcpp::R_AARCH64_TLSDESC_CALL:
  6986. // Change to nop
  6987. newinsn = 0xd503201f;
  6988. break;
  6989. case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
  6990. // Change to movz.
  6991. newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
  6992. break;
  6993. case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
  6994. // Change to movk.
  6995. newinsn = 0xf2800000 | ((x & 0xffff) << 5);
  6996. break;
  6997. default:
  6998. gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
  6999. r_type);
  7000. gold_unreachable();
  7001. }
  7002. elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
  7003. return aarch64_reloc_funcs::STATUS_OKAY;
  7004. } // End of tls_desc_gd_to_le
  7005. template<int size, bool big_endian>
  7006. inline
  7007. typename AArch64_relocate_functions<size, big_endian>::Status
  7008. Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
  7009. const Relocate_info<size, big_endian>* /* relinfo */,
  7010. Target_aarch64<size, big_endian>* /* target */,
  7011. const elfcpp::Rela<size, big_endian>& rela,
  7012. unsigned int r_type,
  7013. unsigned char* view,
  7014. const Symbol_value<size>* /* psymval */,
  7015. typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
  7016. typename elfcpp::Elf_types<size>::Elf_Addr address)
  7017. {
  7018. typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
  7019. typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
  7020. // TLSDESC-GD sequence is like:
  7021. // adrp x0, :tlsdesc:v1
  7022. // ldr x1, [x0, #:tlsdesc_lo12:v1]
  7023. // add x0, x0, :tlsdesc_lo12:v1
  7024. // .tlsdesccall v1
  7025. // blr x1
  7026. // After desc_gd_to_ie optimization, the sequence will be like:
  7027. // adrp x0, :tlsie:v1
  7028. // ldr x0, [x0, :tlsie_lo12:v1]
  7029. // nop
  7030. // nop
  7031. Insntype* ip = reinterpret_cast<Insntype*>(view);
  7032. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  7033. Insntype newinsn;
  7034. switch (r_type)
  7035. {
  7036. case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
  7037. case elfcpp::R_AARCH64_TLSDESC_CALL:
  7038. // Change to nop
  7039. newinsn = 0xd503201f;
  7040. elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
  7041. break;
  7042. case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
  7043. {
  7044. return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
  7045. address);
  7046. }
  7047. break;
  7048. case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
  7049. {
  7050. // Set ldr target register to be x0.
  7051. Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
  7052. insn &= 0xffffffe0;
  7053. elfcpp::Swap<32, big_endian>::writeval(ip, insn);
  7054. // Do relocation.
  7055. const AArch64_reloc_property* reloc_property =
  7056. aarch64_reloc_property_table->get_reloc_property(
  7057. elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
  7058. return aarch64_reloc_funcs::template rela_general<32>(
  7059. view, got_entry_address, addend, reloc_property);
  7060. }
  7061. break;
  7062. default:
  7063. gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
  7064. r_type);
  7065. gold_unreachable();
  7066. }
  7067. return aarch64_reloc_funcs::STATUS_OKAY;
  7068. } // End of tls_desc_gd_to_ie
  7069. // Relocate section data.
  7070. template<int size, bool big_endian>
  7071. void
  7072. Target_aarch64<size, big_endian>::relocate_section(
  7073. const Relocate_info<size, big_endian>* relinfo,
  7074. unsigned int sh_type,
  7075. const unsigned char* prelocs,
  7076. size_t reloc_count,
  7077. Output_section* output_section,
  7078. bool needs_special_offset_handling,
  7079. unsigned char* view,
  7080. typename elfcpp::Elf_types<size>::Elf_Addr address,
  7081. section_size_type view_size,
  7082. const Reloc_symbol_changes* reloc_symbol_changes)
  7083. {
  7084. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  7085. typedef Target_aarch64<size, big_endian> Aarch64;
  7086. typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
  7087. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  7088. Classify_reloc;
  7089. gold_assert(sh_type == elfcpp::SHT_RELA);
  7090. // See if we are relocating a relaxed input section. If so, the view
  7091. // covers the whole output section and we need to adjust accordingly.
  7092. if (needs_special_offset_handling)
  7093. {
  7094. const Output_relaxed_input_section* poris =
  7095. output_section->find_relaxed_input_section(relinfo->object,
  7096. relinfo->data_shndx);
  7097. if (poris != NULL)
  7098. {
  7099. Address section_address = poris->address();
  7100. section_size_type section_size = poris->data_size();
  7101. gold_assert((section_address >= address)
  7102. && ((section_address + section_size)
  7103. <= (address + view_size)));
  7104. off_t offset = section_address - address;
  7105. view += offset;
  7106. address += offset;
  7107. view_size = section_size;
  7108. }
  7109. }
  7110. gold::relocate_section<size, big_endian, Aarch64, AArch64_relocate,
  7111. gold::Default_comdat_behavior, Classify_reloc>(
  7112. relinfo,
  7113. this,
  7114. prelocs,
  7115. reloc_count,
  7116. output_section,
  7117. needs_special_offset_handling,
  7118. view,
  7119. address,
  7120. view_size,
  7121. reloc_symbol_changes);
  7122. }
  7123. // Scan the relocs during a relocatable link.
  7124. template<int size, bool big_endian>
  7125. void
  7126. Target_aarch64<size, big_endian>::scan_relocatable_relocs(
  7127. Symbol_table* symtab,
  7128. Layout* layout,
  7129. Sized_relobj_file<size, big_endian>* object,
  7130. unsigned int data_shndx,
  7131. unsigned int sh_type,
  7132. const unsigned char* prelocs,
  7133. size_t reloc_count,
  7134. Output_section* output_section,
  7135. bool needs_special_offset_handling,
  7136. size_t local_symbol_count,
  7137. const unsigned char* plocal_symbols,
  7138. Relocatable_relocs* rr)
  7139. {
  7140. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  7141. Classify_reloc;
  7142. typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
  7143. Scan_relocatable_relocs;
  7144. gold_assert(sh_type == elfcpp::SHT_RELA);
  7145. gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
  7146. symtab,
  7147. layout,
  7148. object,
  7149. data_shndx,
  7150. prelocs,
  7151. reloc_count,
  7152. output_section,
  7153. needs_special_offset_handling,
  7154. local_symbol_count,
  7155. plocal_symbols,
  7156. rr);
  7157. }
  7158. // Scan the relocs for --emit-relocs.
  7159. template<int size, bool big_endian>
  7160. void
  7161. Target_aarch64<size, big_endian>::emit_relocs_scan(
  7162. Symbol_table* symtab,
  7163. Layout* layout,
  7164. Sized_relobj_file<size, big_endian>* object,
  7165. unsigned int data_shndx,
  7166. unsigned int sh_type,
  7167. const unsigned char* prelocs,
  7168. size_t reloc_count,
  7169. Output_section* output_section,
  7170. bool needs_special_offset_handling,
  7171. size_t local_symbol_count,
  7172. const unsigned char* plocal_syms,
  7173. Relocatable_relocs* rr)
  7174. {
  7175. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  7176. Classify_reloc;
  7177. typedef gold::Default_emit_relocs_strategy<Classify_reloc>
  7178. Emit_relocs_strategy;
  7179. gold_assert(sh_type == elfcpp::SHT_RELA);
  7180. gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
  7181. symtab,
  7182. layout,
  7183. object,
  7184. data_shndx,
  7185. prelocs,
  7186. reloc_count,
  7187. output_section,
  7188. needs_special_offset_handling,
  7189. local_symbol_count,
  7190. plocal_syms,
  7191. rr);
  7192. }
  7193. // Relocate a section during a relocatable link.
  7194. template<int size, bool big_endian>
  7195. void
  7196. Target_aarch64<size, big_endian>::relocate_relocs(
  7197. const Relocate_info<size, big_endian>* relinfo,
  7198. unsigned int sh_type,
  7199. const unsigned char* prelocs,
  7200. size_t reloc_count,
  7201. Output_section* output_section,
  7202. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  7203. unsigned char* view,
  7204. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  7205. section_size_type view_size,
  7206. unsigned char* reloc_view,
  7207. section_size_type reloc_view_size)
  7208. {
  7209. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  7210. Classify_reloc;
  7211. gold_assert(sh_type == elfcpp::SHT_RELA);
  7212. gold::relocate_relocs<size, big_endian, Classify_reloc>(
  7213. relinfo,
  7214. prelocs,
  7215. reloc_count,
  7216. output_section,
  7217. offset_in_output_section,
  7218. view,
  7219. view_address,
  7220. view_size,
  7221. reloc_view,
  7222. reloc_view_size);
  7223. }
  7224. // Return whether this is a 3-insn erratum sequence.
  7225. template<int size, bool big_endian>
  7226. bool
  7227. Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
  7228. typename elfcpp::Swap<32,big_endian>::Valtype insn1,
  7229. typename elfcpp::Swap<32,big_endian>::Valtype insn2,
  7230. typename elfcpp::Swap<32,big_endian>::Valtype insn3)
  7231. {
  7232. unsigned rt1, rt2;
  7233. bool load, pair;
  7234. // The 2nd insn is a single register load or store; or register pair
  7235. // store.
  7236. if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
  7237. && (!pair || (pair && !load)))
  7238. {
  7239. // The 3rd insn is a load or store instruction from the "Load/store
  7240. // register (unsigned immediate)" encoding class, using Rn as the
  7241. // base address register.
  7242. if (Insn_utilities::aarch64_ldst_uimm(insn3)
  7243. && (Insn_utilities::aarch64_rn(insn3)
  7244. == Insn_utilities::aarch64_rd(insn1)))
  7245. return true;
  7246. }
  7247. return false;
  7248. }
  7249. // Return whether this is a 835769 sequence.
  7250. // (Similarly implemented as in elfnn-aarch64.c.)
  7251. template<int size, bool big_endian>
  7252. bool
  7253. Target_aarch64<size, big_endian>::is_erratum_835769_sequence(
  7254. typename elfcpp::Swap<32,big_endian>::Valtype insn1,
  7255. typename elfcpp::Swap<32,big_endian>::Valtype insn2)
  7256. {
  7257. uint32_t rt;
  7258. uint32_t rt2 = 0;
  7259. uint32_t rn;
  7260. uint32_t rm;
  7261. uint32_t ra;
  7262. bool pair;
  7263. bool load;
  7264. if (Insn_utilities::aarch64_mlxl(insn2)
  7265. && Insn_utilities::aarch64_mem_op_p (insn1, &rt, &rt2, &pair, &load))
  7266. {
  7267. /* Any SIMD memory op is independent of the subsequent MLA
  7268. by definition of the erratum. */
  7269. if (Insn_utilities::aarch64_bit(insn1, 26))
  7270. return true;
  7271. /* If not SIMD, check for integer memory ops and MLA relationship. */
  7272. rn = Insn_utilities::aarch64_rn(insn2);
  7273. ra = Insn_utilities::aarch64_ra(insn2);
  7274. rm = Insn_utilities::aarch64_rm(insn2);
  7275. /* If this is a load and there's a true(RAW) dependency, we are safe
  7276. and this is not an erratum sequence. */
  7277. if (load &&
  7278. (rt == rn || rt == rm || rt == ra
  7279. || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
  7280. return false;
  7281. /* We conservatively put out stubs for all other cases (including
  7282. writebacks). */
  7283. return true;
  7284. }
  7285. return false;
  7286. }
  7287. // Helper method to create erratum stub for ST_E_843419 and ST_E_835769.
  7288. template<int size, bool big_endian>
  7289. void
  7290. Target_aarch64<size, big_endian>::create_erratum_stub(
  7291. AArch64_relobj<size, big_endian>* relobj,
  7292. unsigned int shndx,
  7293. section_size_type erratum_insn_offset,
  7294. Address erratum_address,
  7295. typename Insn_utilities::Insntype erratum_insn,
  7296. int erratum_type,
  7297. unsigned int e843419_adrp_offset)
  7298. {
  7299. gold_assert(erratum_type == ST_E_843419 || erratum_type == ST_E_835769);
  7300. The_stub_table* stub_table = relobj->stub_table(shndx);
  7301. gold_assert(stub_table != NULL);
  7302. if (stub_table->find_erratum_stub(relobj,
  7303. shndx,
  7304. erratum_insn_offset) == NULL)
  7305. {
  7306. const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
  7307. The_erratum_stub* stub;
  7308. if (erratum_type == ST_E_835769)
  7309. stub = new The_erratum_stub(relobj, erratum_type, shndx,
  7310. erratum_insn_offset);
  7311. else if (erratum_type == ST_E_843419)
  7312. stub = new E843419_stub<size, big_endian>(
  7313. relobj, shndx, erratum_insn_offset, e843419_adrp_offset);
  7314. else
  7315. gold_unreachable();
  7316. stub->set_erratum_insn(erratum_insn);
  7317. stub->set_erratum_address(erratum_address);
  7318. // For erratum ST_E_843419 and ST_E_835769, the destination address is
  7319. // always the next insn after erratum insn.
  7320. stub->set_destination_address(erratum_address + BPI);
  7321. stub_table->add_erratum_stub(stub);
  7322. }
  7323. }
  7324. // Scan erratum for section SHNDX range [output_address + span_start,
  7325. // output_address + span_end). Note here we do not share the code with
  7326. // scan_erratum_843419_span function, because for 843419 we optimize by only
  7327. // scanning the last few insns of a page, whereas for 835769, we need to scan
  7328. // every insn.
  7329. template<int size, bool big_endian>
  7330. void
  7331. Target_aarch64<size, big_endian>::scan_erratum_835769_span(
  7332. AArch64_relobj<size, big_endian>* relobj,
  7333. unsigned int shndx,
  7334. const section_size_type span_start,
  7335. const section_size_type span_end,
  7336. unsigned char* input_view,
  7337. Address output_address)
  7338. {
  7339. typedef typename Insn_utilities::Insntype Insntype;
  7340. const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
  7341. // Adjust output_address and view to the start of span.
  7342. output_address += span_start;
  7343. input_view += span_start;
  7344. section_size_type span_length = span_end - span_start;
  7345. section_size_type offset = 0;
  7346. for (offset = 0; offset + BPI < span_length; offset += BPI)
  7347. {
  7348. Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
  7349. Insntype insn1 = ip[0];
  7350. Insntype insn2 = ip[1];
  7351. if (is_erratum_835769_sequence(insn1, insn2))
  7352. {
  7353. Insntype erratum_insn = insn2;
  7354. // "span_start + offset" is the offset for insn1. So for insn2, it is
  7355. // "span_start + offset + BPI".
  7356. section_size_type erratum_insn_offset = span_start + offset + BPI;
  7357. Address erratum_address = output_address + offset + BPI;
  7358. gold_info(_("Erratum 835769 found and fixed at \"%s\", "
  7359. "section %d, offset 0x%08x."),
  7360. relobj->name().c_str(), shndx,
  7361. (unsigned int)(span_start + offset));
  7362. this->create_erratum_stub(relobj, shndx,
  7363. erratum_insn_offset, erratum_address,
  7364. erratum_insn, ST_E_835769);
  7365. offset += BPI; // Skip mac insn.
  7366. }
  7367. }
  7368. } // End of "Target_aarch64::scan_erratum_835769_span".
  7369. // Scan erratum for section SHNDX range
  7370. // [output_address + span_start, output_address + span_end).
  7371. template<int size, bool big_endian>
  7372. void
  7373. Target_aarch64<size, big_endian>::scan_erratum_843419_span(
  7374. AArch64_relobj<size, big_endian>* relobj,
  7375. unsigned int shndx,
  7376. const section_size_type span_start,
  7377. const section_size_type span_end,
  7378. unsigned char* input_view,
  7379. Address output_address)
  7380. {
  7381. typedef typename Insn_utilities::Insntype Insntype;
  7382. // Adjust output_address and view to the start of span.
  7383. output_address += span_start;
  7384. input_view += span_start;
  7385. if ((output_address & 0x03) != 0)
  7386. return;
  7387. section_size_type offset = 0;
  7388. section_size_type span_length = span_end - span_start;
  7389. // The first instruction must be ending at 0xFF8 or 0xFFC.
  7390. unsigned int page_offset = output_address & 0xFFF;
  7391. // Make sure starting position, that is "output_address+offset",
  7392. // starts at page position 0xff8 or 0xffc.
  7393. if (page_offset < 0xff8)
  7394. offset = 0xff8 - page_offset;
  7395. while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
  7396. {
  7397. Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
  7398. Insntype insn1 = ip[0];
  7399. if (Insn_utilities::is_adrp(insn1))
  7400. {
  7401. Insntype insn2 = ip[1];
  7402. Insntype insn3 = ip[2];
  7403. Insntype erratum_insn;
  7404. unsigned insn_offset;
  7405. bool do_report = false;
  7406. if (is_erratum_843419_sequence(insn1, insn2, insn3))
  7407. {
  7408. do_report = true;
  7409. erratum_insn = insn3;
  7410. insn_offset = 2 * Insn_utilities::BYTES_PER_INSN;
  7411. }
  7412. else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
  7413. {
  7414. // Optionally we can have an insn between ins2 and ins3
  7415. Insntype insn_opt = ip[2];
  7416. // And insn_opt must not be a branch.
  7417. if (!Insn_utilities::aarch64_b(insn_opt)
  7418. && !Insn_utilities::aarch64_bl(insn_opt)
  7419. && !Insn_utilities::aarch64_blr(insn_opt)
  7420. && !Insn_utilities::aarch64_br(insn_opt))
  7421. {
  7422. // And insn_opt must not write to dest reg in insn1. However
  7423. // we do a conservative scan, which means we may fix/report
  7424. // more than necessary, but it doesn't hurt.
  7425. Insntype insn4 = ip[3];
  7426. if (is_erratum_843419_sequence(insn1, insn2, insn4))
  7427. {
  7428. do_report = true;
  7429. erratum_insn = insn4;
  7430. insn_offset = 3 * Insn_utilities::BYTES_PER_INSN;
  7431. }
  7432. }
  7433. }
  7434. if (do_report)
  7435. {
  7436. unsigned int erratum_insn_offset =
  7437. span_start + offset + insn_offset;
  7438. Address erratum_address =
  7439. output_address + offset + insn_offset;
  7440. create_erratum_stub(relobj, shndx,
  7441. erratum_insn_offset, erratum_address,
  7442. erratum_insn, ST_E_843419,
  7443. span_start + offset);
  7444. }
  7445. }
  7446. // Advance to next candidate instruction. We only consider instruction
  7447. // sequences starting at a page offset of 0xff8 or 0xffc.
  7448. page_offset = (output_address + offset) & 0xfff;
  7449. if (page_offset == 0xff8)
  7450. offset += 4;
  7451. else // (page_offset == 0xffc), we move to next page's 0xff8.
  7452. offset += 0xffc;
  7453. }
  7454. } // End of "Target_aarch64::scan_erratum_843419_span".
  7455. // The selector for aarch64 object files.
  7456. template<int size, bool big_endian>
  7457. class Target_selector_aarch64 : public Target_selector
  7458. {
  7459. public:
  7460. Target_selector_aarch64();
  7461. virtual Target*
  7462. do_instantiate_target()
  7463. { return new Target_aarch64<size, big_endian>(); }
  7464. };
  7465. template<>
  7466. Target_selector_aarch64<32, true>::Target_selector_aarch64()
  7467. : Target_selector(elfcpp::EM_AARCH64, 32, true,
  7468. "elf32-bigaarch64", "aarch64_elf32_be_vec")
  7469. { }
  7470. template<>
  7471. Target_selector_aarch64<32, false>::Target_selector_aarch64()
  7472. : Target_selector(elfcpp::EM_AARCH64, 32, false,
  7473. "elf32-littleaarch64", "aarch64_elf32_le_vec")
  7474. { }
  7475. template<>
  7476. Target_selector_aarch64<64, true>::Target_selector_aarch64()
  7477. : Target_selector(elfcpp::EM_AARCH64, 64, true,
  7478. "elf64-bigaarch64", "aarch64_elf64_be_vec")
  7479. { }
  7480. template<>
  7481. Target_selector_aarch64<64, false>::Target_selector_aarch64()
  7482. : Target_selector(elfcpp::EM_AARCH64, 64, false,
  7483. "elf64-littleaarch64", "aarch64_elf64_le_vec")
  7484. { }
  7485. Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
  7486. Target_selector_aarch64<32, false> target_selector_aarch64elf32;
  7487. Target_selector_aarch64<64, true> target_selector_aarch64elfb;
  7488. Target_selector_aarch64<64, false> target_selector_aarch64elf;
  7489. } // End anonymous namespace.