aarch64.cc 254 KB

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