x86_64.cc 206 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523
  1. // x86_64.cc -- x86_64 target support for gold.
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@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 "elfcpp.h"
  20. #include "dwarf.h"
  21. #include "parameters.h"
  22. #include "reloc.h"
  23. #include "x86_64.h"
  24. #include "object.h"
  25. #include "symtab.h"
  26. #include "layout.h"
  27. #include "output.h"
  28. #include "copy-relocs.h"
  29. #include "target.h"
  30. #include "target-reloc.h"
  31. #include "target-select.h"
  32. #include "tls.h"
  33. #include "freebsd.h"
  34. #include "nacl.h"
  35. #include "gc.h"
  36. #include "icf.h"
  37. namespace
  38. {
  39. using namespace gold;
  40. // A class to handle the .got.plt section.
  41. class Output_data_got_plt_x86_64 : public Output_section_data_build
  42. {
  43. public:
  44. Output_data_got_plt_x86_64(Layout* layout)
  45. : Output_section_data_build(8),
  46. layout_(layout)
  47. { }
  48. Output_data_got_plt_x86_64(Layout* layout, off_t data_size)
  49. : Output_section_data_build(data_size, 8),
  50. layout_(layout)
  51. { }
  52. protected:
  53. // Write out the PLT data.
  54. void
  55. do_write(Output_file*);
  56. // Write to a map file.
  57. void
  58. do_print_to_mapfile(Mapfile* mapfile) const
  59. { mapfile->print_output_data(this, "** GOT PLT"); }
  60. private:
  61. // A pointer to the Layout class, so that we can find the .dynamic
  62. // section when we write out the GOT PLT section.
  63. Layout* layout_;
  64. };
  65. // A class to handle the PLT data.
  66. // This is an abstract base class that handles most of the linker details
  67. // but does not know the actual contents of PLT entries. The derived
  68. // classes below fill in those details.
  69. template<int size>
  70. class Output_data_plt_x86_64 : public Output_section_data
  71. {
  72. public:
  73. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
  74. Output_data_plt_x86_64(Layout* layout, uint64_t addralign,
  75. Output_data_got<64, false>* got,
  76. Output_data_got_plt_x86_64* got_plt,
  77. Output_data_space* got_irelative)
  78. : Output_section_data(addralign), tlsdesc_rel_(NULL),
  79. irelative_rel_(NULL), got_(got), got_plt_(got_plt),
  80. got_irelative_(got_irelative), count_(0), irelative_count_(0),
  81. tlsdesc_got_offset_(-1U), free_list_()
  82. { this->init(layout); }
  83. Output_data_plt_x86_64(Layout* layout, uint64_t plt_entry_size,
  84. Output_data_got<64, false>* got,
  85. Output_data_got_plt_x86_64* got_plt,
  86. Output_data_space* got_irelative,
  87. unsigned int plt_count)
  88. : Output_section_data((plt_count + 1) * plt_entry_size,
  89. plt_entry_size, false),
  90. tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got),
  91. got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
  92. irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
  93. {
  94. this->init(layout);
  95. // Initialize the free list and reserve the first entry.
  96. this->free_list_.init((plt_count + 1) * plt_entry_size, false);
  97. this->free_list_.remove(0, plt_entry_size);
  98. }
  99. // Initialize the PLT section.
  100. void
  101. init(Layout* layout);
  102. // Add an entry to the PLT.
  103. void
  104. add_entry(Symbol_table*, Layout*, Symbol* gsym);
  105. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
  106. unsigned int
  107. add_local_ifunc_entry(Symbol_table* symtab, Layout*,
  108. Sized_relobj_file<size, false>* relobj,
  109. unsigned int local_sym_index);
  110. // Add the relocation for a PLT entry.
  111. void
  112. add_relocation(Symbol_table*, Layout*, Symbol* gsym,
  113. unsigned int got_offset);
  114. // Add the reserved TLSDESC_PLT entry to the PLT.
  115. void
  116. reserve_tlsdesc_entry(unsigned int got_offset)
  117. { this->tlsdesc_got_offset_ = got_offset; }
  118. // Return true if a TLSDESC_PLT entry has been reserved.
  119. bool
  120. has_tlsdesc_entry() const
  121. { return this->tlsdesc_got_offset_ != -1U; }
  122. // Return the GOT offset for the reserved TLSDESC_PLT entry.
  123. unsigned int
  124. get_tlsdesc_got_offset() const
  125. { return this->tlsdesc_got_offset_; }
  126. // Return the offset of the reserved TLSDESC_PLT entry.
  127. unsigned int
  128. get_tlsdesc_plt_offset() const
  129. {
  130. return ((this->count_ + this->irelative_count_ + 1)
  131. * this->get_plt_entry_size());
  132. }
  133. // Return the .rela.plt section data.
  134. Reloc_section*
  135. rela_plt()
  136. { return this->rel_; }
  137. // Return where the TLSDESC relocations should go.
  138. Reloc_section*
  139. rela_tlsdesc(Layout*);
  140. // Return where the IRELATIVE relocations should go in the PLT
  141. // relocations.
  142. Reloc_section*
  143. rela_irelative(Symbol_table*, Layout*);
  144. // Return whether we created a section for IRELATIVE relocations.
  145. bool
  146. has_irelative_section() const
  147. { return this->irelative_rel_ != NULL; }
  148. // Get count of regular PLT entries.
  149. unsigned int
  150. regular_count() const
  151. { return this->count_; }
  152. // Return the total number of PLT entries.
  153. unsigned int
  154. entry_count() const
  155. { return this->count_ + this->irelative_count_; }
  156. // Return the offset of the first non-reserved PLT entry.
  157. unsigned int
  158. first_plt_entry_offset()
  159. { return this->get_plt_entry_size(); }
  160. // Return the size of a PLT entry.
  161. unsigned int
  162. get_plt_entry_size() const
  163. { return this->do_get_plt_entry_size(); }
  164. // Reserve a slot in the PLT for an existing symbol in an incremental update.
  165. void
  166. reserve_slot(unsigned int plt_index)
  167. {
  168. this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
  169. (plt_index + 2) * this->get_plt_entry_size());
  170. }
  171. // Return the PLT address to use for a global symbol.
  172. uint64_t
  173. address_for_global(const Symbol* sym)
  174. { return do_address_for_global(sym); }
  175. // Return the PLT address to use for a local symbol.
  176. uint64_t
  177. address_for_local(const Relobj* obj, unsigned int symndx)
  178. { return do_address_for_local(obj, symndx); }
  179. // Add .eh_frame information for the PLT.
  180. void
  181. add_eh_frame(Layout* layout)
  182. { this->do_add_eh_frame(layout); }
  183. protected:
  184. Output_data_got<64, false>*
  185. got() const
  186. { return this->got_; }
  187. Output_data_got_plt_x86_64*
  188. got_plt() const
  189. { return this->got_plt_; }
  190. Output_data_space*
  191. got_irelative() const
  192. { return this->got_irelative_; }
  193. // Fill in the first PLT entry.
  194. void
  195. fill_first_plt_entry(unsigned char* pov,
  196. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  197. typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
  198. { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
  199. // Fill in a normal PLT entry. Returns the offset into the entry that
  200. // should be the initial GOT slot value.
  201. unsigned int
  202. fill_plt_entry(unsigned char* pov,
  203. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  204. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  205. unsigned int got_offset,
  206. unsigned int plt_offset,
  207. unsigned int plt_index)
  208. {
  209. return this->do_fill_plt_entry(pov, got_address, plt_address,
  210. got_offset, plt_offset, plt_index);
  211. }
  212. // Fill in the reserved TLSDESC PLT entry.
  213. void
  214. fill_tlsdesc_entry(unsigned char* pov,
  215. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  216. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  217. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  218. unsigned int tlsdesc_got_offset,
  219. unsigned int plt_offset)
  220. {
  221. this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
  222. tlsdesc_got_offset, plt_offset);
  223. }
  224. virtual unsigned int
  225. do_get_plt_entry_size() const = 0;
  226. virtual void
  227. do_fill_first_plt_entry(unsigned char* pov,
  228. typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
  229. typename elfcpp::Elf_types<size>::Elf_Addr plt_addr)
  230. = 0;
  231. virtual unsigned int
  232. do_fill_plt_entry(unsigned char* pov,
  233. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  234. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  235. unsigned int got_offset,
  236. unsigned int plt_offset,
  237. unsigned int plt_index) = 0;
  238. virtual void
  239. do_fill_tlsdesc_entry(unsigned char* pov,
  240. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  241. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  242. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  243. unsigned int tlsdesc_got_offset,
  244. unsigned int plt_offset) = 0;
  245. // Return the PLT address to use for a global symbol.
  246. virtual uint64_t
  247. do_address_for_global(const Symbol* sym);
  248. // Return the PLT address to use for a local symbol.
  249. virtual uint64_t
  250. do_address_for_local(const Relobj* obj, unsigned int symndx);
  251. virtual void
  252. do_add_eh_frame(Layout* layout) = 0;
  253. void
  254. do_adjust_output_section(Output_section* os);
  255. // Write to a map file.
  256. void
  257. do_print_to_mapfile(Mapfile* mapfile) const
  258. { mapfile->print_output_data(this, _("** PLT")); }
  259. // The CIE of the .eh_frame unwind information for the PLT.
  260. static const int plt_eh_frame_cie_size = 16;
  261. static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
  262. private:
  263. // Set the final size.
  264. void
  265. set_final_data_size();
  266. // Write out the PLT data.
  267. void
  268. do_write(Output_file*);
  269. // The reloc section.
  270. Reloc_section* rel_;
  271. // The TLSDESC relocs, if necessary. These must follow the regular
  272. // PLT relocs.
  273. Reloc_section* tlsdesc_rel_;
  274. // The IRELATIVE relocs, if necessary. These must follow the
  275. // regular PLT relocations and the TLSDESC relocations.
  276. Reloc_section* irelative_rel_;
  277. // The .got section.
  278. Output_data_got<64, false>* got_;
  279. // The .got.plt section.
  280. Output_data_got_plt_x86_64* got_plt_;
  281. // The part of the .got.plt section used for IRELATIVE relocs.
  282. Output_data_space* got_irelative_;
  283. // The number of PLT entries.
  284. unsigned int count_;
  285. // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
  286. // follow the regular PLT entries.
  287. unsigned int irelative_count_;
  288. // Offset of the reserved TLSDESC_GOT entry when needed.
  289. unsigned int tlsdesc_got_offset_;
  290. // List of available regions within the section, for incremental
  291. // update links.
  292. Free_list free_list_;
  293. };
  294. template<int size>
  295. class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size>
  296. {
  297. public:
  298. Output_data_plt_x86_64_standard(Layout* layout,
  299. Output_data_got<64, false>* got,
  300. Output_data_got_plt_x86_64* got_plt,
  301. Output_data_space* got_irelative)
  302. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  303. got, got_plt, got_irelative)
  304. { }
  305. Output_data_plt_x86_64_standard(Layout* layout,
  306. Output_data_got<64, false>* got,
  307. Output_data_got_plt_x86_64* got_plt,
  308. Output_data_space* got_irelative,
  309. unsigned int plt_count)
  310. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  311. got, got_plt, got_irelative,
  312. plt_count)
  313. { }
  314. protected:
  315. virtual unsigned int
  316. do_get_plt_entry_size() const
  317. { return plt_entry_size; }
  318. virtual void
  319. do_add_eh_frame(Layout* layout)
  320. {
  321. layout->add_eh_frame_for_plt(this,
  322. this->plt_eh_frame_cie,
  323. this->plt_eh_frame_cie_size,
  324. plt_eh_frame_fde,
  325. plt_eh_frame_fde_size);
  326. }
  327. virtual void
  328. do_fill_first_plt_entry(unsigned char* pov,
  329. typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
  330. typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
  331. virtual unsigned int
  332. do_fill_plt_entry(unsigned char* pov,
  333. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  334. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  335. unsigned int got_offset,
  336. unsigned int plt_offset,
  337. unsigned int plt_index);
  338. virtual void
  339. do_fill_tlsdesc_entry(unsigned char* pov,
  340. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  341. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  342. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  343. unsigned int tlsdesc_got_offset,
  344. unsigned int plt_offset);
  345. private:
  346. // The size of an entry in the PLT.
  347. static const int plt_entry_size = 16;
  348. // The first entry in the PLT.
  349. // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
  350. // procedure linkage table for both programs and shared objects."
  351. static const unsigned char first_plt_entry[plt_entry_size];
  352. // Other entries in the PLT for an executable.
  353. static const unsigned char plt_entry[plt_entry_size];
  354. // The reserved TLSDESC entry in the PLT for an executable.
  355. static const unsigned char tlsdesc_plt_entry[plt_entry_size];
  356. // The .eh_frame unwind information for the PLT.
  357. static const int plt_eh_frame_fde_size = 32;
  358. static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
  359. };
  360. class Output_data_plt_x86_64_bnd : public Output_data_plt_x86_64<64>
  361. {
  362. public:
  363. Output_data_plt_x86_64_bnd(Layout* layout,
  364. Output_data_got<64, false>* got,
  365. Output_data_got_plt_x86_64* got_plt,
  366. Output_data_space* got_irelative)
  367. : Output_data_plt_x86_64<64>(layout, plt_entry_size,
  368. got, got_plt, got_irelative),
  369. aplt_offset_(0)
  370. { }
  371. Output_data_plt_x86_64_bnd(Layout* layout,
  372. Output_data_got<64, false>* got,
  373. Output_data_got_plt_x86_64* got_plt,
  374. Output_data_space* got_irelative,
  375. unsigned int plt_count)
  376. : Output_data_plt_x86_64<64>(layout, plt_entry_size,
  377. got, got_plt, got_irelative,
  378. plt_count),
  379. aplt_offset_(0)
  380. { }
  381. protected:
  382. virtual unsigned int
  383. do_get_plt_entry_size() const
  384. { return plt_entry_size; }
  385. // Return the PLT address to use for a global symbol.
  386. uint64_t
  387. do_address_for_global(const Symbol*);
  388. // Return the PLT address to use for a local symbol.
  389. uint64_t
  390. do_address_for_local(const Relobj*, unsigned int symndx);
  391. virtual void
  392. do_add_eh_frame(Layout* layout)
  393. {
  394. layout->add_eh_frame_for_plt(this,
  395. this->plt_eh_frame_cie,
  396. this->plt_eh_frame_cie_size,
  397. plt_eh_frame_fde,
  398. plt_eh_frame_fde_size);
  399. }
  400. virtual void
  401. do_fill_first_plt_entry(unsigned char* pov,
  402. elfcpp::Elf_types<64>::Elf_Addr got_addr,
  403. elfcpp::Elf_types<64>::Elf_Addr plt_addr);
  404. virtual unsigned int
  405. do_fill_plt_entry(unsigned char* pov,
  406. elfcpp::Elf_types<64>::Elf_Addr got_address,
  407. elfcpp::Elf_types<64>::Elf_Addr plt_address,
  408. unsigned int got_offset,
  409. unsigned int plt_offset,
  410. unsigned int plt_index);
  411. virtual void
  412. do_fill_tlsdesc_entry(unsigned char* pov,
  413. elfcpp::Elf_types<64>::Elf_Addr got_address,
  414. elfcpp::Elf_types<64>::Elf_Addr plt_address,
  415. elfcpp::Elf_types<64>::Elf_Addr got_base,
  416. unsigned int tlsdesc_got_offset,
  417. unsigned int plt_offset);
  418. void
  419. fill_aplt_entry(unsigned char* pov,
  420. elfcpp::Elf_types<64>::Elf_Addr got_address,
  421. elfcpp::Elf_types<64>::Elf_Addr plt_address,
  422. unsigned int got_offset,
  423. unsigned int plt_offset,
  424. unsigned int plt_index);
  425. private:
  426. // Set the final size.
  427. void
  428. set_final_data_size();
  429. // Write out the BND PLT data.
  430. void
  431. do_write(Output_file*);
  432. // Offset of the Additional PLT (if using -z bndplt).
  433. unsigned int aplt_offset_;
  434. // The size of an entry in the PLT.
  435. static const int plt_entry_size = 16;
  436. // The size of an entry in the additional PLT.
  437. static const int aplt_entry_size = 8;
  438. // The first entry in the PLT.
  439. // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
  440. // procedure linkage table for both programs and shared objects."
  441. static const unsigned char first_plt_entry[plt_entry_size];
  442. // Other entries in the PLT for an executable.
  443. static const unsigned char plt_entry[plt_entry_size];
  444. // Entries in the additional PLT.
  445. static const unsigned char aplt_entry[aplt_entry_size];
  446. // The reserved TLSDESC entry in the PLT for an executable.
  447. static const unsigned char tlsdesc_plt_entry[plt_entry_size];
  448. // The .eh_frame unwind information for the PLT.
  449. static const int plt_eh_frame_fde_size = 32;
  450. static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
  451. };
  452. // We use this PLT when Indirect Branch Tracking (IBT) is enabled.
  453. template <int size>
  454. class Output_data_plt_x86_64_ibt : public Output_data_plt_x86_64<size>
  455. {
  456. public:
  457. Output_data_plt_x86_64_ibt(Layout* layout,
  458. Output_data_got<64, false>* got,
  459. Output_data_got_plt_x86_64* got_plt,
  460. Output_data_space* got_irelative)
  461. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  462. got, got_plt, got_irelative),
  463. aplt_offset_(0)
  464. { }
  465. Output_data_plt_x86_64_ibt(Layout* layout,
  466. Output_data_got<64, false>* got,
  467. Output_data_got_plt_x86_64* got_plt,
  468. Output_data_space* got_irelative,
  469. unsigned int plt_count)
  470. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  471. got, got_plt, got_irelative,
  472. plt_count),
  473. aplt_offset_(0)
  474. { }
  475. protected:
  476. virtual unsigned int
  477. do_get_plt_entry_size() const
  478. { return plt_entry_size; }
  479. // Return the PLT address to use for a global symbol.
  480. uint64_t
  481. do_address_for_global(const Symbol*);
  482. // Return the PLT address to use for a local symbol.
  483. uint64_t
  484. do_address_for_local(const Relobj*, unsigned int symndx);
  485. virtual void
  486. do_add_eh_frame(Layout* layout)
  487. {
  488. layout->add_eh_frame_for_plt(this,
  489. this->plt_eh_frame_cie,
  490. this->plt_eh_frame_cie_size,
  491. plt_eh_frame_fde,
  492. plt_eh_frame_fde_size);
  493. }
  494. virtual void
  495. do_fill_first_plt_entry(unsigned char* pov,
  496. typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
  497. typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
  498. virtual unsigned int
  499. do_fill_plt_entry(unsigned char* pov,
  500. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  501. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  502. unsigned int got_offset,
  503. unsigned int plt_offset,
  504. unsigned int plt_index);
  505. virtual void
  506. do_fill_tlsdesc_entry(unsigned char* pov,
  507. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  508. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  509. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  510. unsigned int tlsdesc_got_offset,
  511. unsigned int plt_offset);
  512. void
  513. fill_aplt_entry(unsigned char* pov,
  514. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  515. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  516. unsigned int got_offset,
  517. unsigned int plt_offset,
  518. unsigned int plt_index);
  519. private:
  520. // Set the final size.
  521. void
  522. set_final_data_size();
  523. // Write out the BND PLT data.
  524. void
  525. do_write(Output_file*);
  526. // Offset of the Additional PLT (if using -z bndplt).
  527. unsigned int aplt_offset_;
  528. // The size of an entry in the PLT.
  529. static const int plt_entry_size = 16;
  530. // The size of an entry in the additional PLT.
  531. static const int aplt_entry_size = 16;
  532. // The first entry in the PLT.
  533. // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
  534. // procedure linkage table for both programs and shared objects."
  535. static const unsigned char first_plt_entry[plt_entry_size];
  536. // Other entries in the PLT for an executable.
  537. static const unsigned char plt_entry[plt_entry_size];
  538. // Entries in the additional PLT.
  539. static const unsigned char aplt_entry[aplt_entry_size];
  540. // The reserved TLSDESC entry in the PLT for an executable.
  541. static const unsigned char tlsdesc_plt_entry[plt_entry_size];
  542. // The .eh_frame unwind information for the PLT.
  543. static const int plt_eh_frame_fde_size = 32;
  544. static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
  545. };
  546. template<int size>
  547. class Lazy_view
  548. {
  549. public:
  550. Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx)
  551. : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0)
  552. { }
  553. inline unsigned char
  554. operator[](size_t offset)
  555. {
  556. if (this->view_ == NULL)
  557. this->view_ = this->object_->section_contents(this->data_shndx_,
  558. &this->view_size_,
  559. true);
  560. if (offset >= this->view_size_)
  561. return 0;
  562. return this->view_[offset];
  563. }
  564. private:
  565. Sized_relobj_file<size, false>* object_;
  566. unsigned int data_shndx_;
  567. const unsigned char* view_;
  568. section_size_type view_size_;
  569. };
  570. // The x86_64 target class.
  571. // See the ABI at
  572. // http://www.x86-64.org/documentation/abi.pdf
  573. // TLS info comes from
  574. // http://people.redhat.com/drepper/tls.pdf
  575. // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
  576. template<int size>
  577. class Target_x86_64 : public Sized_target<size, false>
  578. {
  579. public:
  580. // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
  581. // uses only Elf64_Rela relocation entries with explicit addends."
  582. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
  583. Target_x86_64(const Target::Target_info* info = &x86_64_info)
  584. : Sized_target<size, false>(info),
  585. got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
  586. got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
  587. rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
  588. got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
  589. tls_base_symbol_defined_(false), isa_1_used_(0), isa_1_needed_(0),
  590. feature_1_(0), feature_2_used_(0), feature_2_needed_(0),
  591. object_isa_1_used_(0), object_feature_1_(0),
  592. object_feature_2_used_(0), seen_first_object_(false)
  593. { }
  594. // Hook for a new output section.
  595. void
  596. do_new_output_section(Output_section*) const;
  597. // Scan the relocations to look for symbol adjustments.
  598. void
  599. gc_process_relocs(Symbol_table* symtab,
  600. Layout* layout,
  601. Sized_relobj_file<size, false>* object,
  602. unsigned int data_shndx,
  603. unsigned int sh_type,
  604. const unsigned char* prelocs,
  605. size_t reloc_count,
  606. Output_section* output_section,
  607. bool needs_special_offset_handling,
  608. size_t local_symbol_count,
  609. const unsigned char* plocal_symbols);
  610. // Scan the relocations to look for symbol adjustments.
  611. void
  612. scan_relocs(Symbol_table* symtab,
  613. Layout* layout,
  614. Sized_relobj_file<size, false>* object,
  615. unsigned int data_shndx,
  616. unsigned int sh_type,
  617. const unsigned char* prelocs,
  618. size_t reloc_count,
  619. Output_section* output_section,
  620. bool needs_special_offset_handling,
  621. size_t local_symbol_count,
  622. const unsigned char* plocal_symbols);
  623. // Finalize the sections.
  624. void
  625. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  626. // Return the value to use for a dynamic which requires special
  627. // treatment.
  628. uint64_t
  629. do_dynsym_value(const Symbol*) const;
  630. // Relocate a section.
  631. void
  632. relocate_section(const Relocate_info<size, false>*,
  633. unsigned int sh_type,
  634. const unsigned char* prelocs,
  635. size_t reloc_count,
  636. Output_section* output_section,
  637. bool needs_special_offset_handling,
  638. unsigned char* view,
  639. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  640. section_size_type view_size,
  641. const Reloc_symbol_changes*);
  642. // Scan the relocs during a relocatable link.
  643. void
  644. scan_relocatable_relocs(Symbol_table* symtab,
  645. Layout* layout,
  646. Sized_relobj_file<size, false>* object,
  647. unsigned int data_shndx,
  648. unsigned int sh_type,
  649. const unsigned char* prelocs,
  650. size_t reloc_count,
  651. Output_section* output_section,
  652. bool needs_special_offset_handling,
  653. size_t local_symbol_count,
  654. const unsigned char* plocal_symbols,
  655. Relocatable_relocs*);
  656. // Scan the relocs for --emit-relocs.
  657. void
  658. emit_relocs_scan(Symbol_table* symtab,
  659. Layout* layout,
  660. Sized_relobj_file<size, false>* object,
  661. unsigned int data_shndx,
  662. unsigned int sh_type,
  663. const unsigned char* prelocs,
  664. size_t reloc_count,
  665. Output_section* output_section,
  666. bool needs_special_offset_handling,
  667. size_t local_symbol_count,
  668. const unsigned char* plocal_syms,
  669. Relocatable_relocs* rr);
  670. // Emit relocations for a section.
  671. void
  672. relocate_relocs(
  673. const Relocate_info<size, false>*,
  674. unsigned int sh_type,
  675. const unsigned char* prelocs,
  676. size_t reloc_count,
  677. Output_section* output_section,
  678. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  679. unsigned char* view,
  680. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  681. section_size_type view_size,
  682. unsigned char* reloc_view,
  683. section_size_type reloc_view_size);
  684. // Return a string used to fill a code section with nops.
  685. std::string
  686. do_code_fill(section_size_type length) const;
  687. // Return whether SYM is defined by the ABI.
  688. bool
  689. do_is_defined_by_abi(const Symbol* sym) const
  690. { return strcmp(sym->name(), "__tls_get_addr") == 0; }
  691. // Return the symbol index to use for a target specific relocation.
  692. // The only target specific relocation is R_X86_64_TLSDESC for a
  693. // local symbol, which is an absolute reloc.
  694. unsigned int
  695. do_reloc_symbol_index(void*, unsigned int r_type) const
  696. {
  697. gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
  698. return 0;
  699. }
  700. // Return the addend to use for a target specific relocation.
  701. uint64_t
  702. do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
  703. // Return the PLT section.
  704. uint64_t
  705. do_plt_address_for_global(const Symbol* gsym) const
  706. { return this->plt_section()->address_for_global(gsym); }
  707. uint64_t
  708. do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
  709. { return this->plt_section()->address_for_local(relobj, symndx); }
  710. // This function should be defined in targets that can use relocation
  711. // types to determine (implemented in local_reloc_may_be_function_pointer
  712. // and global_reloc_may_be_function_pointer)
  713. // if a function's pointer is taken. ICF uses this in safe mode to only
  714. // fold those functions whose pointer is defintely not taken. For x86_64
  715. // pie binaries, safe ICF cannot be done by looking at only relocation
  716. // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
  717. // opcode is checked as well to distinguish a function call from taking
  718. // a function's pointer.
  719. bool
  720. do_can_check_for_function_pointers() const
  721. { return true; }
  722. // Return the base for a DW_EH_PE_datarel encoding.
  723. uint64_t
  724. do_ehframe_datarel_base() const;
  725. // Adjust -fsplit-stack code which calls non-split-stack code.
  726. void
  727. do_calls_non_split(Relobj* object, unsigned int shndx,
  728. section_offset_type fnoffset, section_size_type fnsize,
  729. const unsigned char* prelocs, size_t reloc_count,
  730. unsigned char* view, section_size_type view_size,
  731. std::string* from, std::string* to) const;
  732. // Return the size of the GOT section.
  733. section_size_type
  734. got_size() const
  735. {
  736. gold_assert(this->got_ != NULL);
  737. return this->got_->data_size();
  738. }
  739. // Return the number of entries in the GOT.
  740. unsigned int
  741. got_entry_count() const
  742. {
  743. if (this->got_ == NULL)
  744. return 0;
  745. return this->got_size() / 8;
  746. }
  747. // Return the number of entries in the PLT.
  748. unsigned int
  749. plt_entry_count() const;
  750. // Return the offset of the first non-reserved PLT entry.
  751. unsigned int
  752. first_plt_entry_offset() const;
  753. // Return the size of each PLT entry.
  754. unsigned int
  755. plt_entry_size() const;
  756. // Return the size of each GOT entry.
  757. unsigned int
  758. got_entry_size() const
  759. { return 8; };
  760. // Create the GOT section for an incremental update.
  761. Output_data_got_base*
  762. init_got_plt_for_update(Symbol_table* symtab,
  763. Layout* layout,
  764. unsigned int got_count,
  765. unsigned int plt_count);
  766. // Reserve a GOT entry for a local symbol, and regenerate any
  767. // necessary dynamic relocations.
  768. void
  769. reserve_local_got_entry(unsigned int got_index,
  770. Sized_relobj<size, false>* obj,
  771. unsigned int r_sym,
  772. unsigned int got_type);
  773. // Reserve a GOT entry for a global symbol, and regenerate any
  774. // necessary dynamic relocations.
  775. void
  776. reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
  777. unsigned int got_type);
  778. // Register an existing PLT entry for a global symbol.
  779. void
  780. register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
  781. Symbol* gsym);
  782. // Force a COPY relocation for a given symbol.
  783. void
  784. emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
  785. // Apply an incremental relocation.
  786. void
  787. apply_relocation(const Relocate_info<size, false>* relinfo,
  788. typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
  789. unsigned int r_type,
  790. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
  791. const Symbol* gsym,
  792. unsigned char* view,
  793. typename elfcpp::Elf_types<size>::Elf_Addr address,
  794. section_size_type view_size);
  795. // Add a new reloc argument, returning the index in the vector.
  796. size_t
  797. add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
  798. {
  799. this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
  800. return this->tlsdesc_reloc_info_.size() - 1;
  801. }
  802. Output_data_plt_x86_64<size>*
  803. make_data_plt(Layout* layout,
  804. Output_data_got<64, false>* got,
  805. Output_data_got_plt_x86_64* got_plt,
  806. Output_data_space* got_irelative)
  807. {
  808. return this->do_make_data_plt(layout, got, got_plt, got_irelative);
  809. }
  810. Output_data_plt_x86_64<size>*
  811. make_data_plt(Layout* layout,
  812. Output_data_got<64, false>* got,
  813. Output_data_got_plt_x86_64* got_plt,
  814. Output_data_space* got_irelative,
  815. unsigned int plt_count)
  816. {
  817. return this->do_make_data_plt(layout, got, got_plt, got_irelative,
  818. plt_count);
  819. }
  820. virtual Output_data_plt_x86_64<size>*
  821. do_make_data_plt(Layout* layout,
  822. Output_data_got<64, false>* got,
  823. Output_data_got_plt_x86_64* got_plt,
  824. Output_data_space* got_irelative);
  825. virtual Output_data_plt_x86_64<size>*
  826. do_make_data_plt(Layout* layout,
  827. Output_data_got<64, false>* got,
  828. Output_data_got_plt_x86_64* got_plt,
  829. Output_data_space* got_irelative,
  830. unsigned int plt_count);
  831. private:
  832. // The class which scans relocations.
  833. class Scan
  834. {
  835. public:
  836. Scan()
  837. : issued_non_pic_error_(false)
  838. { }
  839. static inline int
  840. get_reference_flags(unsigned int r_type);
  841. inline void
  842. local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
  843. Sized_relobj_file<size, false>* object,
  844. unsigned int data_shndx,
  845. Output_section* output_section,
  846. const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
  847. const elfcpp::Sym<size, false>& lsym,
  848. bool is_discarded);
  849. inline void
  850. global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
  851. Sized_relobj_file<size, false>* object,
  852. unsigned int data_shndx,
  853. Output_section* output_section,
  854. const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
  855. Symbol* gsym);
  856. inline bool
  857. local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
  858. Target_x86_64* target,
  859. Sized_relobj_file<size, false>* object,
  860. unsigned int data_shndx,
  861. Output_section* output_section,
  862. const elfcpp::Rela<size, false>& reloc,
  863. unsigned int r_type,
  864. const elfcpp::Sym<size, false>& lsym);
  865. inline bool
  866. global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
  867. Target_x86_64* target,
  868. Sized_relobj_file<size, false>* object,
  869. unsigned int data_shndx,
  870. Output_section* output_section,
  871. const elfcpp::Rela<size, false>& reloc,
  872. unsigned int r_type,
  873. Symbol* gsym);
  874. private:
  875. static void
  876. unsupported_reloc_local(Sized_relobj_file<size, false>*,
  877. unsigned int r_type);
  878. static void
  879. unsupported_reloc_global(Sized_relobj_file<size, false>*,
  880. unsigned int r_type, Symbol*);
  881. void
  882. check_non_pic(Relobj*, unsigned int r_type, Symbol*);
  883. inline bool
  884. possible_function_pointer_reloc(Sized_relobj_file<size, false>* src_obj,
  885. unsigned int src_indx,
  886. unsigned int r_offset,
  887. unsigned int r_type);
  888. bool
  889. reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
  890. unsigned int r_type);
  891. // Whether we have issued an error about a non-PIC compilation.
  892. bool issued_non_pic_error_;
  893. };
  894. // The class which implements relocation.
  895. class Relocate
  896. {
  897. public:
  898. Relocate()
  899. : skip_call_tls_get_addr_(false)
  900. { }
  901. ~Relocate()
  902. {
  903. if (this->skip_call_tls_get_addr_)
  904. {
  905. // FIXME: This needs to specify the location somehow.
  906. gold_error(_("missing expected TLS relocation"));
  907. }
  908. }
  909. // Do a relocation. Return false if the caller should not issue
  910. // any warnings about this relocation.
  911. inline bool
  912. relocate(const Relocate_info<size, false>*, unsigned int,
  913. Target_x86_64*, Output_section*, size_t, const unsigned char*,
  914. const Sized_symbol<size>*, const Symbol_value<size>*,
  915. unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
  916. section_size_type);
  917. private:
  918. // Do a TLS relocation.
  919. inline void
  920. relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
  921. size_t relnum, const elfcpp::Rela<size, false>&,
  922. unsigned int r_type, const Sized_symbol<size>*,
  923. const Symbol_value<size>*,
  924. unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
  925. section_size_type);
  926. // Do a TLS General-Dynamic to Initial-Exec transition.
  927. inline void
  928. tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
  929. const elfcpp::Rela<size, false>&, unsigned int r_type,
  930. typename elfcpp::Elf_types<size>::Elf_Addr value,
  931. unsigned char* view,
  932. typename elfcpp::Elf_types<size>::Elf_Addr,
  933. section_size_type view_size);
  934. // Do a TLS General-Dynamic to Local-Exec transition.
  935. inline void
  936. tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
  937. Output_segment* tls_segment,
  938. const elfcpp::Rela<size, false>&, unsigned int r_type,
  939. typename elfcpp::Elf_types<size>::Elf_Addr value,
  940. unsigned char* view,
  941. section_size_type view_size);
  942. // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
  943. inline void
  944. tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
  945. const elfcpp::Rela<size, false>&, unsigned int r_type,
  946. typename elfcpp::Elf_types<size>::Elf_Addr value,
  947. unsigned char* view,
  948. typename elfcpp::Elf_types<size>::Elf_Addr,
  949. section_size_type view_size);
  950. // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
  951. inline void
  952. tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
  953. Output_segment* tls_segment,
  954. const elfcpp::Rela<size, false>&, unsigned int r_type,
  955. typename elfcpp::Elf_types<size>::Elf_Addr value,
  956. unsigned char* view,
  957. section_size_type view_size);
  958. // Do a TLS Local-Dynamic to Local-Exec transition.
  959. inline void
  960. tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
  961. Output_segment* tls_segment,
  962. const elfcpp::Rela<size, false>&, unsigned int r_type,
  963. typename elfcpp::Elf_types<size>::Elf_Addr value,
  964. unsigned char* view,
  965. section_size_type view_size);
  966. // Do a TLS Initial-Exec to Local-Exec transition.
  967. static inline void
  968. tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
  969. Output_segment* tls_segment,
  970. const elfcpp::Rela<size, false>&, unsigned int r_type,
  971. typename elfcpp::Elf_types<size>::Elf_Addr value,
  972. unsigned char* view,
  973. section_size_type view_size);
  974. // This is set if we should skip the next reloc, which should be a
  975. // PLT32 reloc against ___tls_get_addr.
  976. bool skip_call_tls_get_addr_;
  977. };
  978. // Check if relocation against this symbol is a candidate for
  979. // conversion from
  980. // mov foo@GOTPCREL(%rip), %reg
  981. // to lea foo(%rip), %reg.
  982. template<class View_type>
  983. static inline bool
  984. can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type,
  985. size_t r_offset, View_type* view)
  986. {
  987. gold_assert(gsym != NULL);
  988. // We cannot do the conversion unless it's one of these relocations.
  989. if (r_type != elfcpp::R_X86_64_GOTPCREL
  990. && r_type != elfcpp::R_X86_64_GOTPCRELX
  991. && r_type != elfcpp::R_X86_64_REX_GOTPCRELX)
  992. return false;
  993. // We cannot convert references to IFUNC symbols, or to symbols that
  994. // are not local to the current module.
  995. // We can't do predefined symbols because they may become undefined
  996. // (e.g., __ehdr_start when the headers aren't mapped to a segment).
  997. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  998. || gsym->is_undefined()
  999. || gsym->is_predefined()
  1000. || gsym->is_from_dynobj()
  1001. || gsym->is_preemptible())
  1002. return false;
  1003. // If we are building a shared object and the symbol is protected, we may
  1004. // need to go through the GOT.
  1005. if (parameters->options().shared()
  1006. && gsym->visibility() == elfcpp::STV_PROTECTED)
  1007. return false;
  1008. // We cannot convert references to the _DYNAMIC symbol.
  1009. if (strcmp(gsym->name(), "_DYNAMIC") == 0)
  1010. return false;
  1011. // Check for a MOV opcode.
  1012. return (*view)[r_offset - 2] == 0x8b;
  1013. }
  1014. // Convert
  1015. // callq *foo@GOTPCRELX(%rip) to
  1016. // addr32 callq foo
  1017. // and jmpq *foo@GOTPCRELX(%rip) to
  1018. // jmpq foo
  1019. // nop
  1020. template<class View_type>
  1021. static inline bool
  1022. can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
  1023. size_t r_offset, View_type* view)
  1024. {
  1025. gold_assert(gsym != NULL);
  1026. // We cannot do the conversion unless it's a GOTPCRELX relocation.
  1027. if (r_type != elfcpp::R_X86_64_GOTPCRELX)
  1028. return false;
  1029. // We cannot convert references to IFUNC symbols, or to symbols that
  1030. // are not local to the current module.
  1031. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1032. || gsym->is_undefined ()
  1033. || gsym->is_from_dynobj()
  1034. || gsym->is_preemptible())
  1035. return false;
  1036. // Check for a CALLQ or JMPQ opcode.
  1037. return ((*view)[r_offset - 2] == 0xff
  1038. && ((*view)[r_offset - 1] == 0x15
  1039. || (*view)[r_offset - 1] == 0x25));
  1040. }
  1041. // Adjust TLS relocation type based on the options and whether this
  1042. // is a local symbol.
  1043. static tls::Tls_optimization
  1044. optimize_tls_reloc(bool is_final, int r_type);
  1045. // Get the GOT section, creating it if necessary.
  1046. Output_data_got<64, false>*
  1047. got_section(Symbol_table*, Layout*);
  1048. // Get the GOT PLT section.
  1049. Output_data_got_plt_x86_64*
  1050. got_plt_section() const
  1051. {
  1052. gold_assert(this->got_plt_ != NULL);
  1053. return this->got_plt_;
  1054. }
  1055. // Get the GOT section for TLSDESC entries.
  1056. Output_data_got<64, false>*
  1057. got_tlsdesc_section() const
  1058. {
  1059. gold_assert(this->got_tlsdesc_ != NULL);
  1060. return this->got_tlsdesc_;
  1061. }
  1062. // Create the PLT section.
  1063. void
  1064. make_plt_section(Symbol_table* symtab, Layout* layout);
  1065. // Create a PLT entry for a global symbol.
  1066. void
  1067. make_plt_entry(Symbol_table*, Layout*, Symbol*);
  1068. // Create a PLT entry for a local STT_GNU_IFUNC symbol.
  1069. void
  1070. make_local_ifunc_plt_entry(Symbol_table*, Layout*,
  1071. Sized_relobj_file<size, false>* relobj,
  1072. unsigned int local_sym_index);
  1073. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  1074. void
  1075. define_tls_base_symbol(Symbol_table*, Layout*);
  1076. // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
  1077. void
  1078. reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
  1079. // Create a GOT entry for the TLS module index.
  1080. unsigned int
  1081. got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  1082. Sized_relobj_file<size, false>* object);
  1083. // Get the PLT section.
  1084. Output_data_plt_x86_64<size>*
  1085. plt_section() const
  1086. {
  1087. gold_assert(this->plt_ != NULL);
  1088. return this->plt_;
  1089. }
  1090. // Get the dynamic reloc section, creating it if necessary.
  1091. Reloc_section*
  1092. rela_dyn_section(Layout*);
  1093. // Get the section to use for TLSDESC relocations.
  1094. Reloc_section*
  1095. rela_tlsdesc_section(Layout*) const;
  1096. // Get the section to use for IRELATIVE relocations.
  1097. Reloc_section*
  1098. rela_irelative_section(Layout*);
  1099. // Add a potential copy relocation.
  1100. void
  1101. copy_reloc(Symbol_table* symtab, Layout* layout,
  1102. Sized_relobj_file<size, false>* object,
  1103. unsigned int shndx, Output_section* output_section,
  1104. Symbol* sym, const elfcpp::Rela<size, false>& reloc)
  1105. {
  1106. unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
  1107. this->copy_relocs_.copy_reloc(symtab, layout,
  1108. symtab->get_sized_symbol<size>(sym),
  1109. object, shndx, output_section,
  1110. r_type, reloc.get_r_offset(),
  1111. reloc.get_r_addend(),
  1112. this->rela_dyn_section(layout));
  1113. }
  1114. // Record a target-specific program property in the .note.gnu.property
  1115. // section.
  1116. void
  1117. record_gnu_property(unsigned int, unsigned int, size_t,
  1118. const unsigned char*, const Object*);
  1119. // Merge the target-specific program properties from the current object.
  1120. void
  1121. merge_gnu_properties(const Object*);
  1122. // Finalize the target-specific program properties and add them back to
  1123. // the layout.
  1124. void
  1125. do_finalize_gnu_properties(Layout*) const;
  1126. // Information about this specific target which we pass to the
  1127. // general Target structure.
  1128. static const Target::Target_info x86_64_info;
  1129. // The types of GOT entries needed for this platform.
  1130. // These values are exposed to the ABI in an incremental link.
  1131. // Do not renumber existing values without changing the version
  1132. // number of the .gnu_incremental_inputs section.
  1133. enum Got_type
  1134. {
  1135. GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
  1136. GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
  1137. GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
  1138. GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
  1139. };
  1140. // This type is used as the argument to the target specific
  1141. // relocation routines. The only target specific reloc is
  1142. // R_X86_64_TLSDESC against a local symbol.
  1143. struct Tlsdesc_info
  1144. {
  1145. Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
  1146. : object(a_object), r_sym(a_r_sym)
  1147. { }
  1148. // The object in which the local symbol is defined.
  1149. Sized_relobj_file<size, false>* object;
  1150. // The local symbol index in the object.
  1151. unsigned int r_sym;
  1152. };
  1153. // The GOT section.
  1154. Output_data_got<64, false>* got_;
  1155. // The PLT section.
  1156. Output_data_plt_x86_64<size>* plt_;
  1157. // The GOT PLT section.
  1158. Output_data_got_plt_x86_64* got_plt_;
  1159. // The GOT section for IRELATIVE relocations.
  1160. Output_data_space* got_irelative_;
  1161. // The GOT section for TLSDESC relocations.
  1162. Output_data_got<64, false>* got_tlsdesc_;
  1163. // The _GLOBAL_OFFSET_TABLE_ symbol.
  1164. Symbol* global_offset_table_;
  1165. // The dynamic reloc section.
  1166. Reloc_section* rela_dyn_;
  1167. // The section to use for IRELATIVE relocs.
  1168. Reloc_section* rela_irelative_;
  1169. // Relocs saved to avoid a COPY reloc.
  1170. Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
  1171. // Offset of the GOT entry for the TLS module index.
  1172. unsigned int got_mod_index_offset_;
  1173. // We handle R_X86_64_TLSDESC against a local symbol as a target
  1174. // specific relocation. Here we store the object and local symbol
  1175. // index for the relocation.
  1176. std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
  1177. // True if the _TLS_MODULE_BASE_ symbol has been defined.
  1178. bool tls_base_symbol_defined_;
  1179. // Target-specific program properties, from .note.gnu.property section.
  1180. // Each bit represents a specific feature.
  1181. uint32_t isa_1_used_;
  1182. uint32_t isa_1_needed_;
  1183. uint32_t feature_1_;
  1184. uint32_t feature_2_used_;
  1185. uint32_t feature_2_needed_;
  1186. // Target-specific properties from the current object.
  1187. // These bits get ORed into ISA_1_USED_ after all properties for the object
  1188. // have been processed. But if either is all zeroes (as when the property
  1189. // is absent from an object), the result should be all zeroes.
  1190. // (See PR ld/23486.)
  1191. uint32_t object_isa_1_used_;
  1192. // These bits get ANDed into FEATURE_1_ after all properties for the object
  1193. // have been processed.
  1194. uint32_t object_feature_1_;
  1195. uint32_t object_feature_2_used_;
  1196. // Whether we have seen our first object, for use in initializing FEATURE_1_.
  1197. bool seen_first_object_;
  1198. };
  1199. template<>
  1200. const Target::Target_info Target_x86_64<64>::x86_64_info =
  1201. {
  1202. 64, // size
  1203. false, // is_big_endian
  1204. elfcpp::EM_X86_64, // machine_code
  1205. false, // has_make_symbol
  1206. false, // has_resolve
  1207. true, // has_code_fill
  1208. true, // is_default_stack_executable
  1209. true, // can_icf_inline_merge_sections
  1210. '\0', // wrap_char
  1211. "/lib/ld64.so.1", // program interpreter
  1212. 0x400000, // default_text_segment_address
  1213. 0x1000, // abi_pagesize (overridable by -z max-page-size)
  1214. 0x1000, // common_pagesize (overridable by -z common-page-size)
  1215. false, // isolate_execinstr
  1216. 0, // rosegment_gap
  1217. elfcpp::SHN_UNDEF, // small_common_shndx
  1218. elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
  1219. 0, // small_common_section_flags
  1220. elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
  1221. NULL, // attributes_section
  1222. NULL, // attributes_vendor
  1223. "_start", // entry_symbol_name
  1224. 32, // hash_entry_size
  1225. elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
  1226. };
  1227. template<>
  1228. const Target::Target_info Target_x86_64<32>::x86_64_info =
  1229. {
  1230. 32, // size
  1231. false, // is_big_endian
  1232. elfcpp::EM_X86_64, // machine_code
  1233. false, // has_make_symbol
  1234. false, // has_resolve
  1235. true, // has_code_fill
  1236. true, // is_default_stack_executable
  1237. true, // can_icf_inline_merge_sections
  1238. '\0', // wrap_char
  1239. "/libx32/ldx32.so.1", // program interpreter
  1240. 0x400000, // default_text_segment_address
  1241. 0x1000, // abi_pagesize (overridable by -z max-page-size)
  1242. 0x1000, // common_pagesize (overridable by -z common-page-size)
  1243. false, // isolate_execinstr
  1244. 0, // rosegment_gap
  1245. elfcpp::SHN_UNDEF, // small_common_shndx
  1246. elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
  1247. 0, // small_common_section_flags
  1248. elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
  1249. NULL, // attributes_section
  1250. NULL, // attributes_vendor
  1251. "_start", // entry_symbol_name
  1252. 32, // hash_entry_size
  1253. elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
  1254. };
  1255. // This is called when a new output section is created. This is where
  1256. // we handle the SHF_X86_64_LARGE.
  1257. template<int size>
  1258. void
  1259. Target_x86_64<size>::do_new_output_section(Output_section* os) const
  1260. {
  1261. if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
  1262. os->set_is_large_section();
  1263. }
  1264. // Get the GOT section, creating it if necessary.
  1265. template<int size>
  1266. Output_data_got<64, false>*
  1267. Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
  1268. {
  1269. if (this->got_ == NULL)
  1270. {
  1271. gold_assert(symtab != NULL && layout != NULL);
  1272. // When using -z now, we can treat .got.plt as a relro section.
  1273. // Without -z now, it is modified after program startup by lazy
  1274. // PLT relocations.
  1275. bool is_got_plt_relro = parameters->options().now();
  1276. Output_section_order got_order = (is_got_plt_relro
  1277. ? ORDER_RELRO
  1278. : ORDER_RELRO_LAST);
  1279. Output_section_order got_plt_order = (is_got_plt_relro
  1280. ? ORDER_RELRO
  1281. : ORDER_NON_RELRO_FIRST);
  1282. this->got_ = new Output_data_got<64, false>();
  1283. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  1284. (elfcpp::SHF_ALLOC
  1285. | elfcpp::SHF_WRITE),
  1286. this->got_, got_order, true);
  1287. this->got_plt_ = new Output_data_got_plt_x86_64(layout);
  1288. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  1289. (elfcpp::SHF_ALLOC
  1290. | elfcpp::SHF_WRITE),
  1291. this->got_plt_, got_plt_order,
  1292. is_got_plt_relro);
  1293. // The first three entries are reserved.
  1294. this->got_plt_->set_current_data_size(3 * 8);
  1295. if (!is_got_plt_relro)
  1296. {
  1297. // Those bytes can go into the relro segment.
  1298. layout->increase_relro(3 * 8);
  1299. }
  1300. // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
  1301. this->global_offset_table_ =
  1302. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  1303. Symbol_table::PREDEFINED,
  1304. this->got_plt_,
  1305. 0, 0, elfcpp::STT_OBJECT,
  1306. elfcpp::STB_LOCAL,
  1307. elfcpp::STV_HIDDEN, 0,
  1308. false, false);
  1309. // If there are any IRELATIVE relocations, they get GOT entries
  1310. // in .got.plt after the jump slot entries.
  1311. this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
  1312. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  1313. (elfcpp::SHF_ALLOC
  1314. | elfcpp::SHF_WRITE),
  1315. this->got_irelative_,
  1316. got_plt_order, is_got_plt_relro);
  1317. // If there are any TLSDESC relocations, they get GOT entries in
  1318. // .got.plt after the jump slot and IRELATIVE entries.
  1319. this->got_tlsdesc_ = new Output_data_got<64, false>();
  1320. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  1321. (elfcpp::SHF_ALLOC
  1322. | elfcpp::SHF_WRITE),
  1323. this->got_tlsdesc_,
  1324. got_plt_order, is_got_plt_relro);
  1325. }
  1326. return this->got_;
  1327. }
  1328. // Get the dynamic reloc section, creating it if necessary.
  1329. template<int size>
  1330. typename Target_x86_64<size>::Reloc_section*
  1331. Target_x86_64<size>::rela_dyn_section(Layout* layout)
  1332. {
  1333. if (this->rela_dyn_ == NULL)
  1334. {
  1335. gold_assert(layout != NULL);
  1336. this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
  1337. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  1338. elfcpp::SHF_ALLOC, this->rela_dyn_,
  1339. ORDER_DYNAMIC_RELOCS, false);
  1340. }
  1341. return this->rela_dyn_;
  1342. }
  1343. // Get the section to use for IRELATIVE relocs, creating it if
  1344. // necessary. These go in .rela.dyn, but only after all other dynamic
  1345. // relocations. They need to follow the other dynamic relocations so
  1346. // that they can refer to global variables initialized by those
  1347. // relocs.
  1348. template<int size>
  1349. typename Target_x86_64<size>::Reloc_section*
  1350. Target_x86_64<size>::rela_irelative_section(Layout* layout)
  1351. {
  1352. if (this->rela_irelative_ == NULL)
  1353. {
  1354. // Make sure we have already created the dynamic reloc section.
  1355. this->rela_dyn_section(layout);
  1356. this->rela_irelative_ = new Reloc_section(false);
  1357. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  1358. elfcpp::SHF_ALLOC, this->rela_irelative_,
  1359. ORDER_DYNAMIC_RELOCS, false);
  1360. gold_assert(this->rela_dyn_->output_section()
  1361. == this->rela_irelative_->output_section());
  1362. }
  1363. return this->rela_irelative_;
  1364. }
  1365. // Record a target-specific program property from the .note.gnu.property
  1366. // section.
  1367. template<int size>
  1368. void
  1369. Target_x86_64<size>::record_gnu_property(
  1370. unsigned int, unsigned int pr_type,
  1371. size_t pr_datasz, const unsigned char* pr_data,
  1372. const Object* object)
  1373. {
  1374. uint32_t val = 0;
  1375. switch (pr_type)
  1376. {
  1377. case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
  1378. case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
  1379. case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
  1380. case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
  1381. case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
  1382. case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
  1383. case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
  1384. case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
  1385. case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
  1386. if (pr_datasz != 4)
  1387. {
  1388. gold_warning(_("%s: corrupt .note.gnu.property section "
  1389. "(pr_datasz for property %d is not 4)"),
  1390. object->name().c_str(), pr_type);
  1391. return;
  1392. }
  1393. val = elfcpp::Swap<32, false>::readval(pr_data);
  1394. break;
  1395. default:
  1396. gold_warning(_("%s: unknown program property type 0x%x "
  1397. "in .note.gnu.property section"),
  1398. object->name().c_str(), pr_type);
  1399. break;
  1400. }
  1401. switch (pr_type)
  1402. {
  1403. case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
  1404. this->object_isa_1_used_ |= val;
  1405. break;
  1406. case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
  1407. this->isa_1_needed_ |= val;
  1408. break;
  1409. case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
  1410. // If we see multiple feature props in one object, OR them together.
  1411. this->object_feature_1_ |= val;
  1412. break;
  1413. case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
  1414. this->object_feature_2_used_ |= val;
  1415. break;
  1416. case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
  1417. this->feature_2_needed_ |= val;
  1418. break;
  1419. }
  1420. }
  1421. // Merge the target-specific program properties from the current object.
  1422. template<int size>
  1423. void
  1424. Target_x86_64<size>::merge_gnu_properties(const Object*)
  1425. {
  1426. if (this->seen_first_object_)
  1427. {
  1428. // If any object is missing the ISA_1_USED property, we must omit
  1429. // it from the output file.
  1430. if (this->object_isa_1_used_ == 0)
  1431. this->isa_1_used_ = 0;
  1432. else if (this->isa_1_used_ != 0)
  1433. this->isa_1_used_ |= this->object_isa_1_used_;
  1434. this->feature_1_ &= this->object_feature_1_;
  1435. // If any object is missing the FEATURE_2_USED property, we must
  1436. // omit it from the output file.
  1437. if (this->object_feature_2_used_ == 0)
  1438. this->feature_2_used_ = 0;
  1439. else if (this->feature_2_used_ != 0)
  1440. this->feature_2_used_ |= this->object_feature_2_used_;
  1441. }
  1442. else
  1443. {
  1444. this->isa_1_used_ = this->object_isa_1_used_;
  1445. this->feature_1_ = this->object_feature_1_;
  1446. this->feature_2_used_ = this->object_feature_2_used_;
  1447. this->seen_first_object_ = true;
  1448. }
  1449. this->object_isa_1_used_ = 0;
  1450. this->object_feature_1_ = 0;
  1451. this->object_feature_2_used_ = 0;
  1452. }
  1453. static inline void
  1454. add_property(Layout* layout, unsigned int pr_type, uint32_t val)
  1455. {
  1456. unsigned char buf[4];
  1457. elfcpp::Swap<32, false>::writeval(buf, val);
  1458. layout->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0, pr_type, 4, buf);
  1459. }
  1460. // Finalize the target-specific program properties and add them back to
  1461. // the layout.
  1462. template<int size>
  1463. void
  1464. Target_x86_64<size>::do_finalize_gnu_properties(Layout* layout) const
  1465. {
  1466. if (this->isa_1_used_ != 0)
  1467. add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_USED,
  1468. this->isa_1_used_);
  1469. if (this->isa_1_needed_ != 0)
  1470. add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED,
  1471. this->isa_1_needed_);
  1472. if (this->feature_1_ != 0)
  1473. add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND,
  1474. this->feature_1_);
  1475. if (this->feature_2_used_ != 0)
  1476. add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED,
  1477. this->feature_2_used_);
  1478. if (this->feature_2_needed_ != 0)
  1479. add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED,
  1480. this->feature_2_needed_);
  1481. }
  1482. // Write the first three reserved words of the .got.plt section.
  1483. // The remainder of the section is written while writing the PLT
  1484. // in Output_data_plt_i386::do_write.
  1485. void
  1486. Output_data_got_plt_x86_64::do_write(Output_file* of)
  1487. {
  1488. // The first entry in the GOT is the address of the .dynamic section
  1489. // aka the PT_DYNAMIC segment. The next two entries are reserved.
  1490. // We saved space for them when we created the section in
  1491. // Target_x86_64::got_section.
  1492. const off_t got_file_offset = this->offset();
  1493. gold_assert(this->data_size() >= 24);
  1494. unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
  1495. Output_section* dynamic = this->layout_->dynamic_section();
  1496. uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
  1497. elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
  1498. memset(got_view + 8, 0, 16);
  1499. of->write_output_view(got_file_offset, 24, got_view);
  1500. }
  1501. // Initialize the PLT section.
  1502. template<int size>
  1503. void
  1504. Output_data_plt_x86_64<size>::init(Layout* layout)
  1505. {
  1506. this->rel_ = new Reloc_section(false);
  1507. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  1508. elfcpp::SHF_ALLOC, this->rel_,
  1509. ORDER_DYNAMIC_PLT_RELOCS, false);
  1510. }
  1511. template<int size>
  1512. void
  1513. Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
  1514. {
  1515. os->set_entsize(this->get_plt_entry_size());
  1516. }
  1517. // Add an entry to the PLT.
  1518. template<int size>
  1519. void
  1520. Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
  1521. Symbol* gsym)
  1522. {
  1523. gold_assert(!gsym->has_plt_offset());
  1524. unsigned int plt_index;
  1525. off_t plt_offset;
  1526. section_offset_type got_offset;
  1527. unsigned int* pcount;
  1528. unsigned int offset;
  1529. unsigned int reserved;
  1530. Output_section_data_build* got;
  1531. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1532. && gsym->can_use_relative_reloc(false))
  1533. {
  1534. pcount = &this->irelative_count_;
  1535. offset = 0;
  1536. reserved = 0;
  1537. got = this->got_irelative_;
  1538. }
  1539. else
  1540. {
  1541. pcount = &this->count_;
  1542. offset = 1;
  1543. reserved = 3;
  1544. got = this->got_plt_;
  1545. }
  1546. if (!this->is_data_size_valid())
  1547. {
  1548. // Note that when setting the PLT offset for a non-IRELATIVE
  1549. // entry we skip the initial reserved PLT entry.
  1550. plt_index = *pcount + offset;
  1551. plt_offset = plt_index * this->get_plt_entry_size();
  1552. ++*pcount;
  1553. got_offset = (plt_index - offset + reserved) * 8;
  1554. gold_assert(got_offset == got->current_data_size());
  1555. // Every PLT entry needs a GOT entry which points back to the PLT
  1556. // entry (this will be changed by the dynamic linker, normally
  1557. // lazily when the function is called).
  1558. got->set_current_data_size(got_offset + 8);
  1559. }
  1560. else
  1561. {
  1562. // FIXME: This is probably not correct for IRELATIVE relocs.
  1563. // For incremental updates, find an available slot.
  1564. plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
  1565. this->get_plt_entry_size(), 0);
  1566. if (plt_offset == -1)
  1567. gold_fallback(_("out of patch space (PLT);"
  1568. " relink with --incremental-full"));
  1569. // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
  1570. // can be calculated from the PLT index, adjusting for the three
  1571. // reserved entries at the beginning of the GOT.
  1572. plt_index = plt_offset / this->get_plt_entry_size() - 1;
  1573. got_offset = (plt_index - offset + reserved) * 8;
  1574. }
  1575. gsym->set_plt_offset(plt_offset);
  1576. // Every PLT entry needs a reloc.
  1577. this->add_relocation(symtab, layout, gsym, got_offset);
  1578. // Note that we don't need to save the symbol. The contents of the
  1579. // PLT are independent of which symbols are used. The symbols only
  1580. // appear in the relocations.
  1581. }
  1582. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
  1583. // the PLT offset.
  1584. template<int size>
  1585. unsigned int
  1586. Output_data_plt_x86_64<size>::add_local_ifunc_entry(
  1587. Symbol_table* symtab,
  1588. Layout* layout,
  1589. Sized_relobj_file<size, false>* relobj,
  1590. unsigned int local_sym_index)
  1591. {
  1592. unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
  1593. ++this->irelative_count_;
  1594. section_offset_type got_offset = this->got_irelative_->current_data_size();
  1595. // Every PLT entry needs a GOT entry which points back to the PLT
  1596. // entry.
  1597. this->got_irelative_->set_current_data_size(got_offset + 8);
  1598. // Every PLT entry needs a reloc.
  1599. Reloc_section* rela = this->rela_irelative(symtab, layout);
  1600. rela->add_symbolless_local_addend(relobj, local_sym_index,
  1601. elfcpp::R_X86_64_IRELATIVE,
  1602. this->got_irelative_, got_offset, 0);
  1603. return plt_offset;
  1604. }
  1605. // Add the relocation for a PLT entry.
  1606. template<int size>
  1607. void
  1608. Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
  1609. Layout* layout,
  1610. Symbol* gsym,
  1611. unsigned int got_offset)
  1612. {
  1613. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1614. && gsym->can_use_relative_reloc(false))
  1615. {
  1616. Reloc_section* rela = this->rela_irelative(symtab, layout);
  1617. rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
  1618. this->got_irelative_, got_offset, 0);
  1619. }
  1620. else
  1621. {
  1622. gsym->set_needs_dynsym_entry();
  1623. this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
  1624. got_offset, 0);
  1625. }
  1626. }
  1627. // Return where the TLSDESC relocations should go, creating it if
  1628. // necessary. These follow the JUMP_SLOT relocations.
  1629. template<int size>
  1630. typename Output_data_plt_x86_64<size>::Reloc_section*
  1631. Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
  1632. {
  1633. if (this->tlsdesc_rel_ == NULL)
  1634. {
  1635. this->tlsdesc_rel_ = new Reloc_section(false);
  1636. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  1637. elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
  1638. ORDER_DYNAMIC_PLT_RELOCS, false);
  1639. gold_assert(this->tlsdesc_rel_->output_section()
  1640. == this->rel_->output_section());
  1641. }
  1642. return this->tlsdesc_rel_;
  1643. }
  1644. // Return where the IRELATIVE relocations should go in the PLT. These
  1645. // follow the JUMP_SLOT and the TLSDESC relocations.
  1646. template<int size>
  1647. typename Output_data_plt_x86_64<size>::Reloc_section*
  1648. Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
  1649. Layout* layout)
  1650. {
  1651. if (this->irelative_rel_ == NULL)
  1652. {
  1653. // Make sure we have a place for the TLSDESC relocations, in
  1654. // case we see any later on.
  1655. this->rela_tlsdesc(layout);
  1656. this->irelative_rel_ = new Reloc_section(false);
  1657. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  1658. elfcpp::SHF_ALLOC, this->irelative_rel_,
  1659. ORDER_DYNAMIC_PLT_RELOCS, false);
  1660. gold_assert(this->irelative_rel_->output_section()
  1661. == this->rel_->output_section());
  1662. if (parameters->doing_static_link())
  1663. {
  1664. // A statically linked executable will only have a .rela.plt
  1665. // section to hold R_X86_64_IRELATIVE relocs for
  1666. // STT_GNU_IFUNC symbols. The library will use these
  1667. // symbols to locate the IRELATIVE relocs at program startup
  1668. // time.
  1669. symtab->define_in_output_data("__rela_iplt_start", NULL,
  1670. Symbol_table::PREDEFINED,
  1671. this->irelative_rel_, 0, 0,
  1672. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  1673. elfcpp::STV_HIDDEN, 0, false, true);
  1674. symtab->define_in_output_data("__rela_iplt_end", NULL,
  1675. Symbol_table::PREDEFINED,
  1676. this->irelative_rel_, 0, 0,
  1677. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  1678. elfcpp::STV_HIDDEN, 0, true, true);
  1679. }
  1680. }
  1681. return this->irelative_rel_;
  1682. }
  1683. // Return the PLT address to use for a global symbol.
  1684. template<int size>
  1685. uint64_t
  1686. Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym)
  1687. {
  1688. uint64_t offset = 0;
  1689. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1690. && gsym->can_use_relative_reloc(false))
  1691. offset = (this->count_ + 1) * this->get_plt_entry_size();
  1692. return this->address() + offset + gsym->plt_offset();
  1693. }
  1694. // Return the PLT address to use for a local symbol. These are always
  1695. // IRELATIVE relocs.
  1696. template<int size>
  1697. uint64_t
  1698. Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object,
  1699. unsigned int r_sym)
  1700. {
  1701. return (this->address()
  1702. + (this->count_ + 1) * this->get_plt_entry_size()
  1703. + object->local_plt_offset(r_sym));
  1704. }
  1705. // Set the final size.
  1706. template<int size>
  1707. void
  1708. Output_data_plt_x86_64<size>::set_final_data_size()
  1709. {
  1710. // Number of regular and IFUNC PLT entries, plus the first entry.
  1711. unsigned int count = this->count_ + this->irelative_count_ + 1;
  1712. // Count the TLSDESC entry, if present.
  1713. if (this->has_tlsdesc_entry())
  1714. ++count;
  1715. this->set_data_size(count * this->get_plt_entry_size());
  1716. }
  1717. // The first entry in the PLT for an executable.
  1718. template<int size>
  1719. const unsigned char
  1720. Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
  1721. {
  1722. // From AMD64 ABI Draft 0.98, page 76
  1723. 0xff, 0x35, // pushq contents of memory address
  1724. 0, 0, 0, 0, // replaced with address of .got + 8
  1725. 0xff, 0x25, // jmp indirect
  1726. 0, 0, 0, 0, // replaced with address of .got + 16
  1727. 0x90, 0x90, 0x90, 0x90 // noop (x4)
  1728. };
  1729. template<int size>
  1730. void
  1731. Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
  1732. unsigned char* pov,
  1733. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  1734. typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
  1735. {
  1736. memcpy(pov, first_plt_entry, plt_entry_size);
  1737. // We do a jmp relative to the PC at the end of this instruction.
  1738. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1739. (got_address + 8
  1740. - (plt_address + 6)));
  1741. elfcpp::Swap<32, false>::writeval(pov + 8,
  1742. (got_address + 16
  1743. - (plt_address + 12)));
  1744. }
  1745. // Subsequent entries in the PLT for an executable.
  1746. template<int size>
  1747. const unsigned char
  1748. Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
  1749. {
  1750. // From AMD64 ABI Draft 0.98, page 76
  1751. 0xff, 0x25, // jmpq indirect
  1752. 0, 0, 0, 0, // replaced with address of symbol in .got
  1753. 0x68, // pushq immediate
  1754. 0, 0, 0, 0, // replaced with offset into relocation table
  1755. 0xe9, // jmpq relative
  1756. 0, 0, 0, 0 // replaced with offset to start of .plt
  1757. };
  1758. template<int size>
  1759. unsigned int
  1760. Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
  1761. unsigned char* pov,
  1762. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  1763. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  1764. unsigned int got_offset,
  1765. unsigned int plt_offset,
  1766. unsigned int plt_index)
  1767. {
  1768. // Check PC-relative offset overflow in PLT entry.
  1769. uint64_t plt_got_pcrel_offset = (got_address + got_offset
  1770. - (plt_address + plt_offset + 6));
  1771. if (Bits<32>::has_overflow(plt_got_pcrel_offset))
  1772. gold_error(_("PC-relative offset overflow in PLT entry %d"),
  1773. plt_index + 1);
  1774. memcpy(pov, plt_entry, plt_entry_size);
  1775. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1776. plt_got_pcrel_offset);
  1777. elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
  1778. elfcpp::Swap<32, false>::writeval(pov + 12,
  1779. - (plt_offset + plt_entry_size));
  1780. return 6;
  1781. }
  1782. // The reserved TLSDESC entry in the PLT for an executable.
  1783. template<int size>
  1784. const unsigned char
  1785. Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
  1786. {
  1787. // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
  1788. // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
  1789. 0xff, 0x35, // pushq x(%rip)
  1790. 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
  1791. 0xff, 0x25, // jmpq *y(%rip)
  1792. 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
  1793. 0x0f, 0x1f, // nop
  1794. 0x40, 0
  1795. };
  1796. template<int size>
  1797. void
  1798. Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
  1799. unsigned char* pov,
  1800. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  1801. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  1802. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  1803. unsigned int tlsdesc_got_offset,
  1804. unsigned int plt_offset)
  1805. {
  1806. memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
  1807. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1808. (got_address + 8
  1809. - (plt_address + plt_offset
  1810. + 6)));
  1811. elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
  1812. (got_base
  1813. + tlsdesc_got_offset
  1814. - (plt_address + plt_offset
  1815. + 12)));
  1816. }
  1817. // Return the APLT address to use for a global symbol (for -z bndplt).
  1818. uint64_t
  1819. Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol* gsym)
  1820. {
  1821. uint64_t offset = this->aplt_offset_;
  1822. // Convert the PLT offset into an APLT offset.
  1823. unsigned int plt_offset = gsym->plt_offset();
  1824. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1825. && gsym->can_use_relative_reloc(false))
  1826. offset += this->regular_count() * aplt_entry_size;
  1827. else
  1828. plt_offset -= plt_entry_size;
  1829. plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
  1830. return this->address() + offset + plt_offset;
  1831. }
  1832. // Return the PLT address to use for a local symbol. These are always
  1833. // IRELATIVE relocs.
  1834. uint64_t
  1835. Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj* object,
  1836. unsigned int r_sym)
  1837. {
  1838. // Convert the PLT offset into an APLT offset.
  1839. const Sized_relobj_file<64, false>* sized_relobj =
  1840. static_cast<const Sized_relobj_file<64, false>*>(object);
  1841. const Symbol_value<64>* psymval = sized_relobj->local_symbol(r_sym);
  1842. unsigned int plt_offset = ((object->local_plt_offset(r_sym)
  1843. - (psymval->is_ifunc_symbol()
  1844. ? 0 : plt_entry_size))
  1845. / (plt_entry_size / aplt_entry_size));
  1846. return (this->address()
  1847. + this->aplt_offset_
  1848. + this->regular_count() * aplt_entry_size
  1849. + plt_offset);
  1850. }
  1851. // Set the final size.
  1852. void
  1853. Output_data_plt_x86_64_bnd::set_final_data_size()
  1854. {
  1855. // Number of regular and IFUNC PLT entries.
  1856. unsigned int count = this->entry_count();
  1857. // Count the first entry and the TLSDESC entry, if present.
  1858. unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
  1859. unsigned int plt_size = (count + extra) * plt_entry_size;
  1860. // Offset of the APLT.
  1861. this->aplt_offset_ = plt_size;
  1862. // Size of the APLT.
  1863. plt_size += count * aplt_entry_size;
  1864. this->set_data_size(plt_size);
  1865. }
  1866. // The first entry in the BND PLT.
  1867. const unsigned char
  1868. Output_data_plt_x86_64_bnd::first_plt_entry[plt_entry_size] =
  1869. {
  1870. // From AMD64 ABI Draft 0.98, page 76
  1871. 0xff, 0x35, // pushq contents of memory address
  1872. 0, 0, 0, 0, // replaced with address of .got + 8
  1873. 0xf2, 0xff, 0x25, // bnd jmp indirect
  1874. 0, 0, 0, 0, // replaced with address of .got + 16
  1875. 0x0f, 0x1f, 0x00 // nop
  1876. };
  1877. void
  1878. Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
  1879. unsigned char* pov,
  1880. elfcpp::Elf_types<64>::Elf_Addr got_address,
  1881. elfcpp::Elf_types<64>::Elf_Addr plt_address)
  1882. {
  1883. memcpy(pov, first_plt_entry, plt_entry_size);
  1884. // We do a jmp relative to the PC at the end of this instruction.
  1885. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1886. (got_address + 8
  1887. - (plt_address + 6)));
  1888. elfcpp::Swap<32, false>::writeval(pov + 9,
  1889. (got_address + 16
  1890. - (plt_address + 13)));
  1891. }
  1892. // Subsequent entries in the BND PLT.
  1893. const unsigned char
  1894. Output_data_plt_x86_64_bnd::plt_entry[plt_entry_size] =
  1895. {
  1896. // From AMD64 ABI Draft 0.99.8, page 139
  1897. 0x68, // pushq immediate
  1898. 0, 0, 0, 0, // replaced with offset into relocation table
  1899. 0xf2, 0xe9, // bnd jmpq relative
  1900. 0, 0, 0, 0, // replaced with offset to start of .plt
  1901. 0x0f, 0x1f, 0x44, 0, 0 // nop
  1902. };
  1903. // Entries in the BND Additional PLT.
  1904. const unsigned char
  1905. Output_data_plt_x86_64_bnd::aplt_entry[aplt_entry_size] =
  1906. {
  1907. // From AMD64 ABI Draft 0.99.8, page 139
  1908. 0xf2, 0xff, 0x25, // bnd jmpq indirect
  1909. 0, 0, 0, 0, // replaced with address of symbol in .got
  1910. 0x90, // nop
  1911. };
  1912. unsigned int
  1913. Output_data_plt_x86_64_bnd::do_fill_plt_entry(
  1914. unsigned char* pov,
  1915. elfcpp::Elf_types<64>::Elf_Addr,
  1916. elfcpp::Elf_types<64>::Elf_Addr,
  1917. unsigned int,
  1918. unsigned int plt_offset,
  1919. unsigned int plt_index)
  1920. {
  1921. memcpy(pov, plt_entry, plt_entry_size);
  1922. elfcpp::Swap_unaligned<32, false>::writeval(pov + 1, plt_index);
  1923. elfcpp::Swap<32, false>::writeval(pov + 7, -(plt_offset + 11));
  1924. return 0;
  1925. }
  1926. void
  1927. Output_data_plt_x86_64_bnd::fill_aplt_entry(
  1928. unsigned char* pov,
  1929. elfcpp::Elf_types<64>::Elf_Addr got_address,
  1930. elfcpp::Elf_types<64>::Elf_Addr plt_address,
  1931. unsigned int got_offset,
  1932. unsigned int plt_offset,
  1933. unsigned int plt_index)
  1934. {
  1935. // Check PC-relative offset overflow in PLT entry.
  1936. uint64_t plt_got_pcrel_offset = (got_address + got_offset
  1937. - (plt_address + plt_offset + 7));
  1938. if (Bits<32>::has_overflow(plt_got_pcrel_offset))
  1939. gold_error(_("PC-relative offset overflow in APLT entry %d"),
  1940. plt_index + 1);
  1941. memcpy(pov, aplt_entry, aplt_entry_size);
  1942. elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, plt_got_pcrel_offset);
  1943. }
  1944. // The reserved TLSDESC entry in the PLT for an executable.
  1945. const unsigned char
  1946. Output_data_plt_x86_64_bnd::tlsdesc_plt_entry[plt_entry_size] =
  1947. {
  1948. // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
  1949. // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
  1950. 0xff, 0x35, // pushq x(%rip)
  1951. 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
  1952. 0xf2, 0xff, 0x25, // jmpq *y(%rip)
  1953. 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
  1954. 0x0f, 0x1f, 0 // nop
  1955. };
  1956. void
  1957. Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
  1958. unsigned char* pov,
  1959. elfcpp::Elf_types<64>::Elf_Addr got_address,
  1960. elfcpp::Elf_types<64>::Elf_Addr plt_address,
  1961. elfcpp::Elf_types<64>::Elf_Addr got_base,
  1962. unsigned int tlsdesc_got_offset,
  1963. unsigned int plt_offset)
  1964. {
  1965. memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
  1966. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1967. (got_address + 8
  1968. - (plt_address + plt_offset
  1969. + 6)));
  1970. elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
  1971. (got_base
  1972. + tlsdesc_got_offset
  1973. - (plt_address + plt_offset
  1974. + 13)));
  1975. }
  1976. // Return the APLT address to use for a global symbol (for IBT).
  1977. template<int size>
  1978. uint64_t
  1979. Output_data_plt_x86_64_ibt<size>::do_address_for_global(const Symbol* gsym)
  1980. {
  1981. uint64_t offset = this->aplt_offset_;
  1982. // Convert the PLT offset into an APLT offset.
  1983. unsigned int plt_offset = gsym->plt_offset();
  1984. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1985. && gsym->can_use_relative_reloc(false))
  1986. offset += this->regular_count() * aplt_entry_size;
  1987. else
  1988. plt_offset -= plt_entry_size;
  1989. plt_offset = plt_offset / (plt_entry_size / aplt_entry_size);
  1990. return this->address() + offset + plt_offset;
  1991. }
  1992. // Return the PLT address to use for a local symbol. These are always
  1993. // IRELATIVE relocs.
  1994. template<int size>
  1995. uint64_t
  1996. Output_data_plt_x86_64_ibt<size>::do_address_for_local(const Relobj* object,
  1997. unsigned int r_sym)
  1998. {
  1999. // Convert the PLT offset into an APLT offset.
  2000. const Sized_relobj_file<size, false>* sized_relobj =
  2001. static_cast<const Sized_relobj_file<size, false>*>(object);
  2002. const Symbol_value<size>* psymval = sized_relobj->local_symbol(r_sym);
  2003. unsigned int plt_offset = ((object->local_plt_offset(r_sym)
  2004. - (psymval->is_ifunc_symbol()
  2005. ? 0 : plt_entry_size))
  2006. / (plt_entry_size / aplt_entry_size));
  2007. return (this->address()
  2008. + this->aplt_offset_
  2009. + this->regular_count() * aplt_entry_size
  2010. + plt_offset);
  2011. }
  2012. // Set the final size.
  2013. template<int size>
  2014. void
  2015. Output_data_plt_x86_64_ibt<size>::set_final_data_size()
  2016. {
  2017. // Number of regular and IFUNC PLT entries.
  2018. unsigned int count = this->entry_count();
  2019. // Count the first entry and the TLSDESC entry, if present.
  2020. unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1;
  2021. unsigned int plt_size = (count + extra) * plt_entry_size;
  2022. // Offset of the APLT.
  2023. this->aplt_offset_ = plt_size;
  2024. // Size of the APLT.
  2025. plt_size += count * aplt_entry_size;
  2026. this->set_data_size(plt_size);
  2027. }
  2028. // The first entry in the IBT PLT.
  2029. template<>
  2030. const unsigned char
  2031. Output_data_plt_x86_64_ibt<32>::first_plt_entry[plt_entry_size] =
  2032. {
  2033. // MPX isn't supported for x32, so we don't need the BND prefix.
  2034. // From AMD64 ABI Draft 0.98, page 76
  2035. 0xff, 0x35, // pushq contents of memory address
  2036. 0, 0, 0, 0, // replaced with address of .got + 8
  2037. 0xff, 0x25, // jmp indirect
  2038. 0, 0, 0, 0, // replaced with address of .got + 16
  2039. 0x90, 0x90, 0x90, 0x90 // noop (x4)
  2040. };
  2041. template<>
  2042. const unsigned char
  2043. Output_data_plt_x86_64_ibt<64>::first_plt_entry[plt_entry_size] =
  2044. {
  2045. // Use the BND prefix so that IBT is compatible with MPX.
  2046. 0xff, 0x35, // pushq contents of memory address
  2047. 0, 0, 0, 0, // replaced with address of .got + 8
  2048. 0xf2, 0xff, 0x25, // bnd jmp indirect
  2049. 0, 0, 0, 0, // replaced with address of .got + 16
  2050. 0x0f, 0x1f, 0x00 // nop
  2051. };
  2052. template<int size>
  2053. void
  2054. Output_data_plt_x86_64_ibt<size>::do_fill_first_plt_entry(
  2055. unsigned char* pov,
  2056. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  2057. typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
  2058. {
  2059. // Offsets to the addresses needing relocation.
  2060. const unsigned int roff1 = 2;
  2061. const unsigned int roff2 = (size == 32) ? 8 : 9;
  2062. memcpy(pov, first_plt_entry, plt_entry_size);
  2063. // We do a jmp relative to the PC at the end of this instruction.
  2064. elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1,
  2065. (got_address + 8
  2066. - (plt_address + roff1 + 4)));
  2067. elfcpp::Swap<32, false>::writeval(pov + roff2,
  2068. (got_address + 16
  2069. - (plt_address + roff2 + 4)));
  2070. }
  2071. // Subsequent entries in the IBT PLT.
  2072. template<>
  2073. const unsigned char
  2074. Output_data_plt_x86_64_ibt<32>::plt_entry[plt_entry_size] =
  2075. {
  2076. // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
  2077. 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
  2078. 0x68, // pushq immediate
  2079. 0, 0, 0, 0, // replaced with offset into relocation table
  2080. 0xe9, // jmpq relative
  2081. 0, 0, 0, 0, // replaced with offset to start of .plt
  2082. 0x90, 0x90 // nop
  2083. };
  2084. template<>
  2085. const unsigned char
  2086. Output_data_plt_x86_64_ibt<64>::plt_entry[plt_entry_size] =
  2087. {
  2088. // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
  2089. 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
  2090. 0x68, // pushq immediate
  2091. 0, 0, 0, 0, // replaced with offset into relocation table
  2092. 0xf2, 0xe9, // bnd jmpq relative
  2093. 0, 0, 0, 0, // replaced with offset to start of .plt
  2094. 0x90 // nop
  2095. };
  2096. // Entries in the IBT Additional PLT.
  2097. template<>
  2098. const unsigned char
  2099. Output_data_plt_x86_64_ibt<32>::aplt_entry[aplt_entry_size] =
  2100. {
  2101. // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
  2102. 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
  2103. 0xff, 0x25, // jmpq indirect
  2104. 0, 0, 0, 0, // replaced with address of symbol in .got
  2105. 0x0f, 0x1f, 0x04, 0x00, // nop
  2106. 0x90, 0x90 // nop
  2107. };
  2108. template<>
  2109. const unsigned char
  2110. Output_data_plt_x86_64_ibt<64>::aplt_entry[aplt_entry_size] =
  2111. {
  2112. // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
  2113. 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
  2114. 0xf2, 0xff, 0x25, // bnd jmpq indirect
  2115. 0, 0, 0, 0, // replaced with address of symbol in .got
  2116. 0x0f, 0x1f, 0x04, 0x00, // nop
  2117. 0x90, // nop
  2118. };
  2119. template<int size>
  2120. unsigned int
  2121. Output_data_plt_x86_64_ibt<size>::do_fill_plt_entry(
  2122. unsigned char* pov,
  2123. typename elfcpp::Elf_types<size>::Elf_Addr,
  2124. typename elfcpp::Elf_types<size>::Elf_Addr,
  2125. unsigned int,
  2126. unsigned int plt_offset,
  2127. unsigned int plt_index)
  2128. {
  2129. // Offsets to the addresses needing relocation.
  2130. const unsigned int roff1 = 5;
  2131. const unsigned int roff2 = (size == 32) ? 10 : 11;
  2132. memcpy(pov, plt_entry, plt_entry_size);
  2133. elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1, plt_index);
  2134. elfcpp::Swap<32, false>::writeval(pov + roff2, -(plt_offset + roff2 + 4));
  2135. return 0;
  2136. }
  2137. template<int size>
  2138. void
  2139. Output_data_plt_x86_64_ibt<size>::fill_aplt_entry(
  2140. unsigned char* pov,
  2141. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  2142. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  2143. unsigned int got_offset,
  2144. unsigned int plt_offset,
  2145. unsigned int plt_index)
  2146. {
  2147. // Offset to the address needing relocation.
  2148. const unsigned int roff = (size == 32) ? 6 : 7;
  2149. // Check PC-relative offset overflow in PLT entry.
  2150. uint64_t plt_got_pcrel_offset = (got_address + got_offset
  2151. - (plt_address + plt_offset + roff + 4));
  2152. if (Bits<32>::has_overflow(plt_got_pcrel_offset))
  2153. gold_error(_("PC-relative offset overflow in APLT entry %d"),
  2154. plt_index + 1);
  2155. memcpy(pov, aplt_entry, aplt_entry_size);
  2156. elfcpp::Swap_unaligned<32, false>::writeval(pov + roff, plt_got_pcrel_offset);
  2157. }
  2158. // The reserved TLSDESC entry in the IBT PLT for an executable.
  2159. template<int size>
  2160. const unsigned char
  2161. Output_data_plt_x86_64_ibt<size>::tlsdesc_plt_entry[plt_entry_size] =
  2162. {
  2163. // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
  2164. // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
  2165. 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
  2166. 0xff, 0x35, // pushq x(%rip)
  2167. 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
  2168. 0xff, 0x25, // jmpq *y(%rip)
  2169. 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
  2170. };
  2171. template<int size>
  2172. void
  2173. Output_data_plt_x86_64_ibt<size>::do_fill_tlsdesc_entry(
  2174. unsigned char* pov,
  2175. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  2176. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  2177. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  2178. unsigned int tlsdesc_got_offset,
  2179. unsigned int plt_offset)
  2180. {
  2181. memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
  2182. elfcpp::Swap_unaligned<32, false>::writeval(pov + 6,
  2183. (got_address + 8
  2184. - (plt_address + plt_offset
  2185. + 10)));
  2186. elfcpp::Swap_unaligned<32, false>::writeval(pov + 12,
  2187. (got_base
  2188. + tlsdesc_got_offset
  2189. - (plt_address + plt_offset
  2190. + 16)));
  2191. }
  2192. // The .eh_frame unwind information for the PLT.
  2193. template<int size>
  2194. const unsigned char
  2195. Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
  2196. {
  2197. 1, // CIE version.
  2198. 'z', // Augmentation: augmentation size included.
  2199. 'R', // Augmentation: FDE encoding included.
  2200. '\0', // End of augmentation string.
  2201. 1, // Code alignment factor.
  2202. 0x78, // Data alignment factor.
  2203. 16, // Return address column.
  2204. 1, // Augmentation size.
  2205. (elfcpp::DW_EH_PE_pcrel // FDE encoding.
  2206. | elfcpp::DW_EH_PE_sdata4),
  2207. elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
  2208. elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
  2209. elfcpp::DW_CFA_nop, // Align to 16 bytes.
  2210. elfcpp::DW_CFA_nop
  2211. };
  2212. template<int size>
  2213. const unsigned char
  2214. Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
  2215. {
  2216. 0, 0, 0, 0, // Replaced with offset to .plt.
  2217. 0, 0, 0, 0, // Replaced with size of .plt.
  2218. 0, // Augmentation size.
  2219. elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
  2220. elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
  2221. elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
  2222. elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
  2223. elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
  2224. 11, // Block length.
  2225. elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
  2226. elfcpp::DW_OP_breg16, 0, // Push %rip.
  2227. elfcpp::DW_OP_lit15, // Push 0xf.
  2228. elfcpp::DW_OP_and, // & (%rip & 0xf).
  2229. elfcpp::DW_OP_lit11, // Push 0xb.
  2230. elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
  2231. elfcpp::DW_OP_lit3, // Push 3.
  2232. elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
  2233. elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
  2234. elfcpp::DW_CFA_nop, // Align to 32 bytes.
  2235. elfcpp::DW_CFA_nop,
  2236. elfcpp::DW_CFA_nop,
  2237. elfcpp::DW_CFA_nop
  2238. };
  2239. // The .eh_frame unwind information for the BND PLT.
  2240. const unsigned char
  2241. Output_data_plt_x86_64_bnd::plt_eh_frame_fde[plt_eh_frame_fde_size] =
  2242. {
  2243. 0, 0, 0, 0, // Replaced with offset to .plt.
  2244. 0, 0, 0, 0, // Replaced with size of .plt.
  2245. 0, // Augmentation size.
  2246. elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
  2247. elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
  2248. elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
  2249. elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
  2250. elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
  2251. 11, // Block length.
  2252. elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
  2253. elfcpp::DW_OP_breg16, 0, // Push %rip.
  2254. elfcpp::DW_OP_lit15, // Push 0xf.
  2255. elfcpp::DW_OP_and, // & (%rip & 0xf).
  2256. elfcpp::DW_OP_lit5, // Push 5.
  2257. elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 5)
  2258. elfcpp::DW_OP_lit3, // Push 3.
  2259. elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 5) << 3)
  2260. elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
  2261. elfcpp::DW_CFA_nop, // Align to 32 bytes.
  2262. elfcpp::DW_CFA_nop,
  2263. elfcpp::DW_CFA_nop,
  2264. elfcpp::DW_CFA_nop
  2265. };
  2266. // The .eh_frame unwind information for the BND PLT.
  2267. template<int size>
  2268. const unsigned char
  2269. Output_data_plt_x86_64_ibt<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
  2270. {
  2271. 0, 0, 0, 0, // Replaced with offset to .plt.
  2272. 0, 0, 0, 0, // Replaced with size of .plt.
  2273. 0, // Augmentation size.
  2274. elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
  2275. elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
  2276. elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
  2277. elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
  2278. elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
  2279. 11, // Block length.
  2280. elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
  2281. elfcpp::DW_OP_breg16, 0, // Push %rip.
  2282. elfcpp::DW_OP_lit15, // Push 0xf.
  2283. elfcpp::DW_OP_and, // & (%rip & 0xf).
  2284. elfcpp::DW_OP_lit9, // Push 9.
  2285. elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 9)
  2286. elfcpp::DW_OP_lit3, // Push 3.
  2287. elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 9) << 3)
  2288. elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=9)<<3)+%rsp+8
  2289. elfcpp::DW_CFA_nop, // Align to 32 bytes.
  2290. elfcpp::DW_CFA_nop,
  2291. elfcpp::DW_CFA_nop,
  2292. elfcpp::DW_CFA_nop
  2293. };
  2294. // Write out the PLT. This uses the hand-coded instructions above,
  2295. // and adjusts them as needed. This is specified by the AMD64 ABI.
  2296. template<int size>
  2297. void
  2298. Output_data_plt_x86_64<size>::do_write(Output_file* of)
  2299. {
  2300. const off_t offset = this->offset();
  2301. const section_size_type oview_size =
  2302. convert_to_section_size_type(this->data_size());
  2303. unsigned char* const oview = of->get_output_view(offset, oview_size);
  2304. const off_t got_file_offset = this->got_plt_->offset();
  2305. gold_assert(parameters->incremental_update()
  2306. || (got_file_offset + this->got_plt_->data_size()
  2307. == this->got_irelative_->offset()));
  2308. const section_size_type got_size =
  2309. convert_to_section_size_type(this->got_plt_->data_size()
  2310. + this->got_irelative_->data_size());
  2311. unsigned char* const got_view = of->get_output_view(got_file_offset,
  2312. got_size);
  2313. unsigned char* pov = oview;
  2314. // The base address of the .plt section.
  2315. typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
  2316. // The base address of the .got section.
  2317. typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
  2318. // The base address of the PLT portion of the .got section,
  2319. // which is where the GOT pointer will point, and where the
  2320. // three reserved GOT entries are located.
  2321. typename elfcpp::Elf_types<size>::Elf_Addr got_address
  2322. = this->got_plt_->address();
  2323. this->fill_first_plt_entry(pov, got_address, plt_address);
  2324. pov += this->get_plt_entry_size();
  2325. // The first three entries in the GOT are reserved, and are written
  2326. // by Output_data_got_plt_x86_64::do_write.
  2327. unsigned char* got_pov = got_view + 24;
  2328. unsigned int plt_offset = this->get_plt_entry_size();
  2329. unsigned int got_offset = 24;
  2330. const unsigned int count = this->count_ + this->irelative_count_;
  2331. for (unsigned int plt_index = 0;
  2332. plt_index < count;
  2333. ++plt_index,
  2334. pov += this->get_plt_entry_size(),
  2335. got_pov += 8,
  2336. plt_offset += this->get_plt_entry_size(),
  2337. got_offset += 8)
  2338. {
  2339. // Set and adjust the PLT entry itself.
  2340. unsigned int lazy_offset = this->fill_plt_entry(pov,
  2341. got_address, plt_address,
  2342. got_offset, plt_offset,
  2343. plt_index);
  2344. // Set the entry in the GOT.
  2345. elfcpp::Swap<64, false>::writeval(got_pov,
  2346. plt_address + plt_offset + lazy_offset);
  2347. }
  2348. if (this->has_tlsdesc_entry())
  2349. {
  2350. // Set and adjust the reserved TLSDESC PLT entry.
  2351. unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
  2352. this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
  2353. tlsdesc_got_offset, plt_offset);
  2354. pov += this->get_plt_entry_size();
  2355. }
  2356. gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
  2357. gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
  2358. of->write_output_view(offset, oview_size, oview);
  2359. of->write_output_view(got_file_offset, got_size, got_view);
  2360. }
  2361. // Write out the BND PLT.
  2362. void
  2363. Output_data_plt_x86_64_bnd::do_write(Output_file* of)
  2364. {
  2365. const off_t offset = this->offset();
  2366. const section_size_type oview_size =
  2367. convert_to_section_size_type(this->data_size());
  2368. unsigned char* const oview = of->get_output_view(offset, oview_size);
  2369. Output_data_got<64, false>* got = this->got();
  2370. Output_data_got_plt_x86_64* got_plt = this->got_plt();
  2371. Output_data_space* got_irelative = this->got_irelative();
  2372. const off_t got_file_offset = got_plt->offset();
  2373. gold_assert(parameters->incremental_update()
  2374. || (got_file_offset + got_plt->data_size()
  2375. == got_irelative->offset()));
  2376. const section_size_type got_size =
  2377. convert_to_section_size_type(got_plt->data_size()
  2378. + got_irelative->data_size());
  2379. unsigned char* const got_view = of->get_output_view(got_file_offset,
  2380. got_size);
  2381. unsigned char* pov = oview;
  2382. // The base address of the .plt section.
  2383. elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
  2384. // The base address of the .got section.
  2385. elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
  2386. // The base address of the PLT portion of the .got section,
  2387. // which is where the GOT pointer will point, and where the
  2388. // three reserved GOT entries are located.
  2389. elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
  2390. this->fill_first_plt_entry(pov, got_address, plt_address);
  2391. pov += plt_entry_size;
  2392. // The first three entries in the GOT are reserved, and are written
  2393. // by Output_data_got_plt_x86_64::do_write.
  2394. unsigned char* got_pov = got_view + 24;
  2395. unsigned int plt_offset = plt_entry_size;
  2396. unsigned int got_offset = 24;
  2397. const unsigned int count = this->entry_count();
  2398. for (unsigned int plt_index = 0;
  2399. plt_index < count;
  2400. ++plt_index,
  2401. pov += plt_entry_size,
  2402. got_pov += 8,
  2403. plt_offset += plt_entry_size,
  2404. got_offset += 8)
  2405. {
  2406. // Set and adjust the PLT entry itself.
  2407. unsigned int lazy_offset = this->fill_plt_entry(pov,
  2408. got_address, plt_address,
  2409. got_offset, plt_offset,
  2410. plt_index);
  2411. // Set the entry in the GOT.
  2412. elfcpp::Swap<64, false>::writeval(got_pov,
  2413. plt_address + plt_offset + lazy_offset);
  2414. }
  2415. if (this->has_tlsdesc_entry())
  2416. {
  2417. // Set and adjust the reserved TLSDESC PLT entry.
  2418. unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
  2419. this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
  2420. tlsdesc_got_offset, plt_offset);
  2421. pov += this->get_plt_entry_size();
  2422. plt_offset += plt_entry_size;
  2423. }
  2424. // Write the additional PLT.
  2425. got_offset = 24;
  2426. for (unsigned int plt_index = 0;
  2427. plt_index < count;
  2428. ++plt_index,
  2429. pov += aplt_entry_size,
  2430. plt_offset += aplt_entry_size,
  2431. got_offset += 8)
  2432. {
  2433. // Set and adjust the APLT entry.
  2434. this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
  2435. plt_offset, plt_index);
  2436. }
  2437. gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
  2438. gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
  2439. of->write_output_view(offset, oview_size, oview);
  2440. of->write_output_view(got_file_offset, got_size, got_view);
  2441. }
  2442. // Write out the IBT PLT.
  2443. template<int size>
  2444. void
  2445. Output_data_plt_x86_64_ibt<size>::do_write(Output_file* of)
  2446. {
  2447. const off_t offset = this->offset();
  2448. const section_size_type oview_size =
  2449. convert_to_section_size_type(this->data_size());
  2450. unsigned char* const oview = of->get_output_view(offset, oview_size);
  2451. Output_data_got<64, false>* got = this->got();
  2452. Output_data_got_plt_x86_64* got_plt = this->got_plt();
  2453. Output_data_space* got_irelative = this->got_irelative();
  2454. const off_t got_file_offset = got_plt->offset();
  2455. gold_assert(parameters->incremental_update()
  2456. || (got_file_offset + got_plt->data_size()
  2457. == got_irelative->offset()));
  2458. const section_size_type got_size =
  2459. convert_to_section_size_type(got_plt->data_size()
  2460. + got_irelative->data_size());
  2461. unsigned char* const got_view = of->get_output_view(got_file_offset,
  2462. got_size);
  2463. unsigned char* pov = oview;
  2464. // The base address of the .plt section.
  2465. elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
  2466. // The base address of the .got section.
  2467. elfcpp::Elf_types<64>::Elf_Addr got_base = got->address();
  2468. // The base address of the PLT portion of the .got section,
  2469. // which is where the GOT pointer will point, and where the
  2470. // three reserved GOT entries are located.
  2471. elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address();
  2472. this->fill_first_plt_entry(pov, got_address, plt_address);
  2473. pov += plt_entry_size;
  2474. // The first three entries in the GOT are reserved, and are written
  2475. // by Output_data_got_plt_x86_64::do_write.
  2476. unsigned char* got_pov = got_view + 24;
  2477. unsigned int plt_offset = plt_entry_size;
  2478. unsigned int got_offset = 24;
  2479. const unsigned int count = this->entry_count();
  2480. for (unsigned int plt_index = 0;
  2481. plt_index < count;
  2482. ++plt_index,
  2483. pov += plt_entry_size,
  2484. got_pov += 8,
  2485. plt_offset += plt_entry_size,
  2486. got_offset += 8)
  2487. {
  2488. // Set and adjust the PLT entry itself.
  2489. unsigned int lazy_offset = this->fill_plt_entry(pov,
  2490. got_address, plt_address,
  2491. got_offset, plt_offset,
  2492. plt_index);
  2493. // Set the entry in the GOT.
  2494. elfcpp::Swap<64, false>::writeval(got_pov,
  2495. plt_address + plt_offset + lazy_offset);
  2496. }
  2497. if (this->has_tlsdesc_entry())
  2498. {
  2499. // Set and adjust the reserved TLSDESC PLT entry.
  2500. unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
  2501. this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
  2502. tlsdesc_got_offset, plt_offset);
  2503. pov += this->get_plt_entry_size();
  2504. plt_offset += plt_entry_size;
  2505. }
  2506. // Write the additional PLT.
  2507. got_offset = 24;
  2508. for (unsigned int plt_index = 0;
  2509. plt_index < count;
  2510. ++plt_index,
  2511. pov += aplt_entry_size,
  2512. plt_offset += aplt_entry_size,
  2513. got_offset += 8)
  2514. {
  2515. // Set and adjust the APLT entry.
  2516. this->fill_aplt_entry(pov, got_address, plt_address, got_offset,
  2517. plt_offset, plt_index);
  2518. }
  2519. gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
  2520. gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
  2521. of->write_output_view(offset, oview_size, oview);
  2522. of->write_output_view(got_file_offset, got_size, got_view);
  2523. }
  2524. // Create the PLT section.
  2525. template<int size>
  2526. void
  2527. Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
  2528. {
  2529. if (this->plt_ == NULL)
  2530. {
  2531. // Create the GOT sections first.
  2532. this->got_section(symtab, layout);
  2533. this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
  2534. this->got_irelative_);
  2535. // Add unwind information if requested.
  2536. if (parameters->options().ld_generated_unwind_info())
  2537. this->plt_->add_eh_frame(layout);
  2538. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  2539. (elfcpp::SHF_ALLOC
  2540. | elfcpp::SHF_EXECINSTR),
  2541. this->plt_, ORDER_PLT, false);
  2542. // Make the sh_info field of .rela.plt point to .plt.
  2543. Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
  2544. rela_plt_os->set_info_section(this->plt_->output_section());
  2545. }
  2546. }
  2547. template<>
  2548. Output_data_plt_x86_64<32>*
  2549. Target_x86_64<32>::do_make_data_plt(Layout* layout,
  2550. Output_data_got<64, false>* got,
  2551. Output_data_got_plt_x86_64* got_plt,
  2552. Output_data_space* got_irelative)
  2553. {
  2554. if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
  2555. return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
  2556. got_irelative);
  2557. return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
  2558. got_irelative);
  2559. }
  2560. template<>
  2561. Output_data_plt_x86_64<64>*
  2562. Target_x86_64<64>::do_make_data_plt(Layout* layout,
  2563. Output_data_got<64, false>* got,
  2564. Output_data_got_plt_x86_64* got_plt,
  2565. Output_data_space* got_irelative)
  2566. {
  2567. if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
  2568. return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
  2569. got_irelative);
  2570. else if (parameters->options().bndplt())
  2571. return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
  2572. got_irelative);
  2573. else
  2574. return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
  2575. got_irelative);
  2576. }
  2577. template<>
  2578. Output_data_plt_x86_64<32>*
  2579. Target_x86_64<32>::do_make_data_plt(Layout* layout,
  2580. Output_data_got<64, false>* got,
  2581. Output_data_got_plt_x86_64* got_plt,
  2582. Output_data_space* got_irelative,
  2583. unsigned int plt_count)
  2584. {
  2585. if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
  2586. return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt,
  2587. got_irelative, plt_count);
  2588. return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt,
  2589. got_irelative, plt_count);
  2590. }
  2591. template<>
  2592. Output_data_plt_x86_64<64>*
  2593. Target_x86_64<64>::do_make_data_plt(Layout* layout,
  2594. Output_data_got<64, false>* got,
  2595. Output_data_got_plt_x86_64* got_plt,
  2596. Output_data_space* got_irelative,
  2597. unsigned int plt_count)
  2598. {
  2599. if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT)
  2600. return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt,
  2601. got_irelative, plt_count);
  2602. else if (parameters->options().bndplt())
  2603. return new Output_data_plt_x86_64_bnd(layout, got, got_plt,
  2604. got_irelative, plt_count);
  2605. else
  2606. return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt,
  2607. got_irelative,
  2608. plt_count);
  2609. }
  2610. // Return the section for TLSDESC relocations.
  2611. template<int size>
  2612. typename Target_x86_64<size>::Reloc_section*
  2613. Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
  2614. {
  2615. return this->plt_section()->rela_tlsdesc(layout);
  2616. }
  2617. // Create a PLT entry for a global symbol.
  2618. template<int size>
  2619. void
  2620. Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
  2621. Symbol* gsym)
  2622. {
  2623. if (gsym->has_plt_offset())
  2624. return;
  2625. if (this->plt_ == NULL)
  2626. this->make_plt_section(symtab, layout);
  2627. this->plt_->add_entry(symtab, layout, gsym);
  2628. }
  2629. // Make a PLT entry for a local STT_GNU_IFUNC symbol.
  2630. template<int size>
  2631. void
  2632. Target_x86_64<size>::make_local_ifunc_plt_entry(
  2633. Symbol_table* symtab, Layout* layout,
  2634. Sized_relobj_file<size, false>* relobj,
  2635. unsigned int local_sym_index)
  2636. {
  2637. if (relobj->local_has_plt_offset(local_sym_index))
  2638. return;
  2639. if (this->plt_ == NULL)
  2640. this->make_plt_section(symtab, layout);
  2641. unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
  2642. relobj,
  2643. local_sym_index);
  2644. relobj->set_local_plt_offset(local_sym_index, plt_offset);
  2645. }
  2646. // Return the number of entries in the PLT.
  2647. template<int size>
  2648. unsigned int
  2649. Target_x86_64<size>::plt_entry_count() const
  2650. {
  2651. if (this->plt_ == NULL)
  2652. return 0;
  2653. return this->plt_->entry_count();
  2654. }
  2655. // Return the offset of the first non-reserved PLT entry.
  2656. template<int size>
  2657. unsigned int
  2658. Target_x86_64<size>::first_plt_entry_offset() const
  2659. {
  2660. if (this->plt_ == NULL)
  2661. return 0;
  2662. return this->plt_->first_plt_entry_offset();
  2663. }
  2664. // Return the size of each PLT entry.
  2665. template<int size>
  2666. unsigned int
  2667. Target_x86_64<size>::plt_entry_size() const
  2668. {
  2669. if (this->plt_ == NULL)
  2670. return 0;
  2671. return this->plt_->get_plt_entry_size();
  2672. }
  2673. // Create the GOT and PLT sections for an incremental update.
  2674. template<int size>
  2675. Output_data_got_base*
  2676. Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
  2677. Layout* layout,
  2678. unsigned int got_count,
  2679. unsigned int plt_count)
  2680. {
  2681. gold_assert(this->got_ == NULL);
  2682. this->got_ = new Output_data_got<64, false>(got_count * 8);
  2683. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  2684. (elfcpp::SHF_ALLOC
  2685. | elfcpp::SHF_WRITE),
  2686. this->got_, ORDER_RELRO_LAST,
  2687. true);
  2688. // Add the three reserved entries.
  2689. this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
  2690. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  2691. (elfcpp::SHF_ALLOC
  2692. | elfcpp::SHF_WRITE),
  2693. this->got_plt_, ORDER_NON_RELRO_FIRST,
  2694. false);
  2695. // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
  2696. this->global_offset_table_ =
  2697. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  2698. Symbol_table::PREDEFINED,
  2699. this->got_plt_,
  2700. 0, 0, elfcpp::STT_OBJECT,
  2701. elfcpp::STB_LOCAL,
  2702. elfcpp::STV_HIDDEN, 0,
  2703. false, false);
  2704. // If there are any TLSDESC relocations, they get GOT entries in
  2705. // .got.plt after the jump slot entries.
  2706. // FIXME: Get the count for TLSDESC entries.
  2707. this->got_tlsdesc_ = new Output_data_got<64, false>(0);
  2708. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  2709. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  2710. this->got_tlsdesc_,
  2711. ORDER_NON_RELRO_FIRST, false);
  2712. // If there are any IRELATIVE relocations, they get GOT entries in
  2713. // .got.plt after the jump slot and TLSDESC entries.
  2714. this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
  2715. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  2716. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  2717. this->got_irelative_,
  2718. ORDER_NON_RELRO_FIRST, false);
  2719. // Create the PLT section.
  2720. this->plt_ = this->make_data_plt(layout, this->got_,
  2721. this->got_plt_,
  2722. this->got_irelative_,
  2723. plt_count);
  2724. // Add unwind information if requested.
  2725. if (parameters->options().ld_generated_unwind_info())
  2726. this->plt_->add_eh_frame(layout);
  2727. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  2728. elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
  2729. this->plt_, ORDER_PLT, false);
  2730. // Make the sh_info field of .rela.plt point to .plt.
  2731. Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
  2732. rela_plt_os->set_info_section(this->plt_->output_section());
  2733. // Create the rela_dyn section.
  2734. this->rela_dyn_section(layout);
  2735. return this->got_;
  2736. }
  2737. // Reserve a GOT entry for a local symbol, and regenerate any
  2738. // necessary dynamic relocations.
  2739. template<int size>
  2740. void
  2741. Target_x86_64<size>::reserve_local_got_entry(
  2742. unsigned int got_index,
  2743. Sized_relobj<size, false>* obj,
  2744. unsigned int r_sym,
  2745. unsigned int got_type)
  2746. {
  2747. unsigned int got_offset = got_index * 8;
  2748. Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
  2749. this->got_->reserve_local(got_index, obj, r_sym, got_type);
  2750. switch (got_type)
  2751. {
  2752. case GOT_TYPE_STANDARD:
  2753. if (parameters->options().output_is_position_independent())
  2754. rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
  2755. this->got_, got_offset, 0, false);
  2756. break;
  2757. case GOT_TYPE_TLS_OFFSET:
  2758. rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
  2759. this->got_, got_offset, 0);
  2760. break;
  2761. case GOT_TYPE_TLS_PAIR:
  2762. this->got_->reserve_slot(got_index + 1);
  2763. rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
  2764. this->got_, got_offset, 0);
  2765. break;
  2766. case GOT_TYPE_TLS_DESC:
  2767. gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
  2768. // this->got_->reserve_slot(got_index + 1);
  2769. // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
  2770. // this->got_, got_offset, 0);
  2771. break;
  2772. default:
  2773. gold_unreachable();
  2774. }
  2775. }
  2776. // Reserve a GOT entry for a global symbol, and regenerate any
  2777. // necessary dynamic relocations.
  2778. template<int size>
  2779. void
  2780. Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
  2781. Symbol* gsym,
  2782. unsigned int got_type)
  2783. {
  2784. unsigned int got_offset = got_index * 8;
  2785. Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
  2786. this->got_->reserve_global(got_index, gsym, got_type);
  2787. switch (got_type)
  2788. {
  2789. case GOT_TYPE_STANDARD:
  2790. if (!gsym->final_value_is_known())
  2791. {
  2792. if (gsym->is_from_dynobj()
  2793. || gsym->is_undefined()
  2794. || gsym->is_preemptible()
  2795. || gsym->type() == elfcpp::STT_GNU_IFUNC)
  2796. rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
  2797. this->got_, got_offset, 0);
  2798. else
  2799. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
  2800. this->got_, got_offset, 0, false);
  2801. }
  2802. break;
  2803. case GOT_TYPE_TLS_OFFSET:
  2804. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
  2805. this->got_, got_offset, 0, false);
  2806. break;
  2807. case GOT_TYPE_TLS_PAIR:
  2808. this->got_->reserve_slot(got_index + 1);
  2809. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
  2810. this->got_, got_offset, 0, false);
  2811. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
  2812. this->got_, got_offset + 8, 0, false);
  2813. break;
  2814. case GOT_TYPE_TLS_DESC:
  2815. this->got_->reserve_slot(got_index + 1);
  2816. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
  2817. this->got_, got_offset, 0, false);
  2818. break;
  2819. default:
  2820. gold_unreachable();
  2821. }
  2822. }
  2823. // Register an existing PLT entry for a global symbol.
  2824. template<int size>
  2825. void
  2826. Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
  2827. Layout* layout,
  2828. unsigned int plt_index,
  2829. Symbol* gsym)
  2830. {
  2831. gold_assert(this->plt_ != NULL);
  2832. gold_assert(!gsym->has_plt_offset());
  2833. this->plt_->reserve_slot(plt_index);
  2834. gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
  2835. unsigned int got_offset = (plt_index + 3) * 8;
  2836. this->plt_->add_relocation(symtab, layout, gsym, got_offset);
  2837. }
  2838. // Force a COPY relocation for a given symbol.
  2839. template<int size>
  2840. void
  2841. Target_x86_64<size>::emit_copy_reloc(
  2842. Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
  2843. {
  2844. this->copy_relocs_.emit_copy_reloc(symtab,
  2845. symtab->get_sized_symbol<size>(sym),
  2846. os,
  2847. offset,
  2848. this->rela_dyn_section(NULL));
  2849. }
  2850. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  2851. template<int size>
  2852. void
  2853. Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
  2854. Layout* layout)
  2855. {
  2856. if (this->tls_base_symbol_defined_)
  2857. return;
  2858. Output_segment* tls_segment = layout->tls_segment();
  2859. if (tls_segment != NULL)
  2860. {
  2861. bool is_exec = parameters->options().output_is_executable();
  2862. symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
  2863. Symbol_table::PREDEFINED,
  2864. tls_segment, 0, 0,
  2865. elfcpp::STT_TLS,
  2866. elfcpp::STB_LOCAL,
  2867. elfcpp::STV_HIDDEN, 0,
  2868. (is_exec
  2869. ? Symbol::SEGMENT_END
  2870. : Symbol::SEGMENT_START),
  2871. true);
  2872. }
  2873. this->tls_base_symbol_defined_ = true;
  2874. }
  2875. // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
  2876. template<int size>
  2877. void
  2878. Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
  2879. Layout* layout)
  2880. {
  2881. if (this->plt_ == NULL)
  2882. this->make_plt_section(symtab, layout);
  2883. if (!this->plt_->has_tlsdesc_entry())
  2884. {
  2885. // Allocate the TLSDESC_GOT entry.
  2886. Output_data_got<64, false>* got = this->got_section(symtab, layout);
  2887. unsigned int got_offset = got->add_constant(0);
  2888. // Allocate the TLSDESC_PLT entry.
  2889. this->plt_->reserve_tlsdesc_entry(got_offset);
  2890. }
  2891. }
  2892. // Create a GOT entry for the TLS module index.
  2893. template<int size>
  2894. unsigned int
  2895. Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  2896. Sized_relobj_file<size, false>* object)
  2897. {
  2898. if (this->got_mod_index_offset_ == -1U)
  2899. {
  2900. gold_assert(symtab != NULL && layout != NULL && object != NULL);
  2901. Reloc_section* rela_dyn = this->rela_dyn_section(layout);
  2902. Output_data_got<64, false>* got = this->got_section(symtab, layout);
  2903. unsigned int got_offset = got->add_constant(0);
  2904. rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
  2905. got_offset, 0);
  2906. got->add_constant(0);
  2907. this->got_mod_index_offset_ = got_offset;
  2908. }
  2909. return this->got_mod_index_offset_;
  2910. }
  2911. // Optimize the TLS relocation type based on what we know about the
  2912. // symbol. IS_FINAL is true if the final address of this symbol is
  2913. // known at link time.
  2914. template<int size>
  2915. tls::Tls_optimization
  2916. Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
  2917. {
  2918. // If we are generating a shared library, then we can't do anything
  2919. // in the linker.
  2920. if (parameters->options().shared())
  2921. return tls::TLSOPT_NONE;
  2922. switch (r_type)
  2923. {
  2924. case elfcpp::R_X86_64_TLSGD:
  2925. case elfcpp::R_X86_64_GOTPC32_TLSDESC:
  2926. case elfcpp::R_X86_64_TLSDESC_CALL:
  2927. // These are General-Dynamic which permits fully general TLS
  2928. // access. Since we know that we are generating an executable,
  2929. // we can convert this to Initial-Exec. If we also know that
  2930. // this is a local symbol, we can further switch to Local-Exec.
  2931. if (is_final)
  2932. return tls::TLSOPT_TO_LE;
  2933. return tls::TLSOPT_TO_IE;
  2934. case elfcpp::R_X86_64_TLSLD:
  2935. // This is Local-Dynamic, which refers to a local symbol in the
  2936. // dynamic TLS block. Since we know that we generating an
  2937. // executable, we can switch to Local-Exec.
  2938. return tls::TLSOPT_TO_LE;
  2939. case elfcpp::R_X86_64_DTPOFF32:
  2940. case elfcpp::R_X86_64_DTPOFF64:
  2941. // Another Local-Dynamic reloc.
  2942. return tls::TLSOPT_TO_LE;
  2943. case elfcpp::R_X86_64_GOTTPOFF:
  2944. // These are Initial-Exec relocs which get the thread offset
  2945. // from the GOT. If we know that we are linking against the
  2946. // local symbol, we can switch to Local-Exec, which links the
  2947. // thread offset into the instruction.
  2948. if (is_final)
  2949. return tls::TLSOPT_TO_LE;
  2950. return tls::TLSOPT_NONE;
  2951. case elfcpp::R_X86_64_TPOFF32:
  2952. // When we already have Local-Exec, there is nothing further we
  2953. // can do.
  2954. return tls::TLSOPT_NONE;
  2955. default:
  2956. gold_unreachable();
  2957. }
  2958. }
  2959. // Get the Reference_flags for a particular relocation.
  2960. template<int size>
  2961. int
  2962. Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
  2963. {
  2964. switch (r_type)
  2965. {
  2966. case elfcpp::R_X86_64_NONE:
  2967. case elfcpp::R_X86_64_GNU_VTINHERIT:
  2968. case elfcpp::R_X86_64_GNU_VTENTRY:
  2969. case elfcpp::R_X86_64_GOTPC32:
  2970. case elfcpp::R_X86_64_GOTPC64:
  2971. // No symbol reference.
  2972. return 0;
  2973. case elfcpp::R_X86_64_64:
  2974. case elfcpp::R_X86_64_32:
  2975. case elfcpp::R_X86_64_32S:
  2976. case elfcpp::R_X86_64_16:
  2977. case elfcpp::R_X86_64_8:
  2978. return Symbol::ABSOLUTE_REF;
  2979. case elfcpp::R_X86_64_PC64:
  2980. case elfcpp::R_X86_64_PC32:
  2981. case elfcpp::R_X86_64_PC32_BND:
  2982. case elfcpp::R_X86_64_PC16:
  2983. case elfcpp::R_X86_64_PC8:
  2984. case elfcpp::R_X86_64_GOTOFF64:
  2985. return Symbol::RELATIVE_REF;
  2986. case elfcpp::R_X86_64_PLT32:
  2987. case elfcpp::R_X86_64_PLT32_BND:
  2988. case elfcpp::R_X86_64_PLTOFF64:
  2989. return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  2990. case elfcpp::R_X86_64_GOT64:
  2991. case elfcpp::R_X86_64_GOT32:
  2992. case elfcpp::R_X86_64_GOTPCREL64:
  2993. case elfcpp::R_X86_64_GOTPCREL:
  2994. case elfcpp::R_X86_64_GOTPCRELX:
  2995. case elfcpp::R_X86_64_REX_GOTPCRELX:
  2996. case elfcpp::R_X86_64_GOTPLT64:
  2997. // Absolute in GOT.
  2998. return Symbol::ABSOLUTE_REF;
  2999. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  3000. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  3001. case elfcpp::R_X86_64_TLSDESC_CALL:
  3002. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  3003. case elfcpp::R_X86_64_DTPOFF32:
  3004. case elfcpp::R_X86_64_DTPOFF64:
  3005. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  3006. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  3007. return Symbol::TLS_REF;
  3008. case elfcpp::R_X86_64_COPY:
  3009. case elfcpp::R_X86_64_GLOB_DAT:
  3010. case elfcpp::R_X86_64_JUMP_SLOT:
  3011. case elfcpp::R_X86_64_RELATIVE:
  3012. case elfcpp::R_X86_64_IRELATIVE:
  3013. case elfcpp::R_X86_64_TPOFF64:
  3014. case elfcpp::R_X86_64_DTPMOD64:
  3015. case elfcpp::R_X86_64_TLSDESC:
  3016. case elfcpp::R_X86_64_SIZE32:
  3017. case elfcpp::R_X86_64_SIZE64:
  3018. default:
  3019. // Not expected. We will give an error later.
  3020. return 0;
  3021. }
  3022. }
  3023. // Report an unsupported relocation against a local symbol.
  3024. template<int size>
  3025. void
  3026. Target_x86_64<size>::Scan::unsupported_reloc_local(
  3027. Sized_relobj_file<size, false>* object,
  3028. unsigned int r_type)
  3029. {
  3030. gold_error(_("%s: unsupported reloc %u against local symbol"),
  3031. object->name().c_str(), r_type);
  3032. }
  3033. // We are about to emit a dynamic relocation of type R_TYPE. If the
  3034. // dynamic linker does not support it, issue an error. The GNU linker
  3035. // only issues a non-PIC error for an allocated read-only section.
  3036. // Here we know the section is allocated, but we don't know that it is
  3037. // read-only. But we check for all the relocation types which the
  3038. // glibc dynamic linker supports, so it seems appropriate to issue an
  3039. // error even if the section is not read-only. If GSYM is not NULL,
  3040. // it is the symbol the relocation is against; if it is NULL, the
  3041. // relocation is against a local symbol.
  3042. template<int size>
  3043. void
  3044. Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
  3045. Symbol* gsym)
  3046. {
  3047. switch (r_type)
  3048. {
  3049. // These are the relocation types supported by glibc for x86_64
  3050. // which should always work.
  3051. case elfcpp::R_X86_64_RELATIVE:
  3052. case elfcpp::R_X86_64_IRELATIVE:
  3053. case elfcpp::R_X86_64_GLOB_DAT:
  3054. case elfcpp::R_X86_64_JUMP_SLOT:
  3055. case elfcpp::R_X86_64_DTPMOD64:
  3056. case elfcpp::R_X86_64_DTPOFF64:
  3057. case elfcpp::R_X86_64_TPOFF64:
  3058. case elfcpp::R_X86_64_64:
  3059. case elfcpp::R_X86_64_COPY:
  3060. return;
  3061. // glibc supports these reloc types, but they can overflow.
  3062. case elfcpp::R_X86_64_PC32:
  3063. case elfcpp::R_X86_64_PC32_BND:
  3064. // A PC relative reference is OK against a local symbol or if
  3065. // the symbol is defined locally.
  3066. if (gsym == NULL
  3067. || (!gsym->is_from_dynobj()
  3068. && !gsym->is_undefined()
  3069. && !gsym->is_preemptible()))
  3070. return;
  3071. // Fall through.
  3072. case elfcpp::R_X86_64_32:
  3073. // R_X86_64_32 is OK for x32.
  3074. if (size == 32 && r_type == elfcpp::R_X86_64_32)
  3075. return;
  3076. if (this->issued_non_pic_error_)
  3077. return;
  3078. gold_assert(parameters->options().output_is_position_independent());
  3079. if (gsym == NULL)
  3080. object->error(_("requires dynamic R_X86_64_32 reloc which may "
  3081. "overflow at runtime; recompile with -fPIC"));
  3082. else
  3083. {
  3084. const char *r_name;
  3085. switch (r_type)
  3086. {
  3087. case elfcpp::R_X86_64_32:
  3088. r_name = "R_X86_64_32";
  3089. break;
  3090. case elfcpp::R_X86_64_PC32:
  3091. r_name = "R_X86_64_PC32";
  3092. break;
  3093. case elfcpp::R_X86_64_PC32_BND:
  3094. r_name = "R_X86_64_PC32_BND";
  3095. break;
  3096. default:
  3097. gold_unreachable();
  3098. break;
  3099. }
  3100. object->error(_("requires dynamic %s reloc against '%s' "
  3101. "which may overflow at runtime; recompile "
  3102. "with -fPIC"),
  3103. r_name, gsym->name());
  3104. }
  3105. this->issued_non_pic_error_ = true;
  3106. return;
  3107. default:
  3108. // This prevents us from issuing more than one error per reloc
  3109. // section. But we can still wind up issuing more than one
  3110. // error per object file.
  3111. if (this->issued_non_pic_error_)
  3112. return;
  3113. gold_assert(parameters->options().output_is_position_independent());
  3114. object->error(_("requires unsupported dynamic reloc %u; "
  3115. "recompile with -fPIC"),
  3116. r_type);
  3117. this->issued_non_pic_error_ = true;
  3118. return;
  3119. case elfcpp::R_X86_64_NONE:
  3120. gold_unreachable();
  3121. }
  3122. }
  3123. // Return whether we need to make a PLT entry for a relocation of the
  3124. // given type against a STT_GNU_IFUNC symbol.
  3125. template<int size>
  3126. bool
  3127. Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
  3128. Sized_relobj_file<size, false>* object,
  3129. unsigned int r_type)
  3130. {
  3131. int flags = Scan::get_reference_flags(r_type);
  3132. if (flags & Symbol::TLS_REF)
  3133. gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
  3134. object->name().c_str(), r_type);
  3135. return flags != 0;
  3136. }
  3137. // Scan a relocation for a local symbol.
  3138. template<int size>
  3139. inline void
  3140. Target_x86_64<size>::Scan::local(Symbol_table* symtab,
  3141. Layout* layout,
  3142. Target_x86_64<size>* target,
  3143. Sized_relobj_file<size, false>* object,
  3144. unsigned int data_shndx,
  3145. Output_section* output_section,
  3146. const elfcpp::Rela<size, false>& reloc,
  3147. unsigned int r_type,
  3148. const elfcpp::Sym<size, false>& lsym,
  3149. bool is_discarded)
  3150. {
  3151. if (is_discarded)
  3152. return;
  3153. // A local STT_GNU_IFUNC symbol may require a PLT entry.
  3154. bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
  3155. if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
  3156. {
  3157. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  3158. target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
  3159. }
  3160. switch (r_type)
  3161. {
  3162. case elfcpp::R_X86_64_NONE:
  3163. case elfcpp::R_X86_64_GNU_VTINHERIT:
  3164. case elfcpp::R_X86_64_GNU_VTENTRY:
  3165. break;
  3166. case elfcpp::R_X86_64_64:
  3167. // If building a shared library (or a position-independent
  3168. // executable), we need to create a dynamic relocation for this
  3169. // location. The relocation applied at link time will apply the
  3170. // link-time value, so we flag the location with an
  3171. // R_X86_64_RELATIVE relocation so the dynamic loader can
  3172. // relocate it easily.
  3173. if (parameters->options().output_is_position_independent())
  3174. {
  3175. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  3176. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  3177. rela_dyn->add_local_relative(object, r_sym,
  3178. (size == 32
  3179. ? elfcpp::R_X86_64_RELATIVE64
  3180. : elfcpp::R_X86_64_RELATIVE),
  3181. output_section, data_shndx,
  3182. reloc.get_r_offset(),
  3183. reloc.get_r_addend(), is_ifunc);
  3184. }
  3185. break;
  3186. case elfcpp::R_X86_64_32:
  3187. case elfcpp::R_X86_64_32S:
  3188. case elfcpp::R_X86_64_16:
  3189. case elfcpp::R_X86_64_8:
  3190. // If building a shared library (or a position-independent
  3191. // executable), we need to create a dynamic relocation for this
  3192. // location. We can't use an R_X86_64_RELATIVE relocation
  3193. // because that is always a 64-bit relocation.
  3194. if (parameters->options().output_is_position_independent())
  3195. {
  3196. // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
  3197. if (size == 32 && r_type == elfcpp::R_X86_64_32)
  3198. {
  3199. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  3200. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  3201. rela_dyn->add_local_relative(object, r_sym,
  3202. elfcpp::R_X86_64_RELATIVE,
  3203. output_section, data_shndx,
  3204. reloc.get_r_offset(),
  3205. reloc.get_r_addend(), is_ifunc);
  3206. break;
  3207. }
  3208. this->check_non_pic(object, r_type, NULL);
  3209. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  3210. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  3211. if (lsym.get_st_type() != elfcpp::STT_SECTION)
  3212. rela_dyn->add_local(object, r_sym, r_type, output_section,
  3213. data_shndx, reloc.get_r_offset(),
  3214. reloc.get_r_addend());
  3215. else
  3216. {
  3217. gold_assert(lsym.get_st_value() == 0);
  3218. unsigned int shndx = lsym.get_st_shndx();
  3219. bool is_ordinary;
  3220. shndx = object->adjust_sym_shndx(r_sym, shndx,
  3221. &is_ordinary);
  3222. if (!is_ordinary)
  3223. object->error(_("section symbol %u has bad shndx %u"),
  3224. r_sym, shndx);
  3225. else
  3226. rela_dyn->add_local_section(object, shndx,
  3227. r_type, output_section,
  3228. data_shndx, reloc.get_r_offset(),
  3229. reloc.get_r_addend());
  3230. }
  3231. }
  3232. break;
  3233. case elfcpp::R_X86_64_PC64:
  3234. case elfcpp::R_X86_64_PC32:
  3235. case elfcpp::R_X86_64_PC32_BND:
  3236. case elfcpp::R_X86_64_PC16:
  3237. case elfcpp::R_X86_64_PC8:
  3238. break;
  3239. case elfcpp::R_X86_64_PLT32:
  3240. case elfcpp::R_X86_64_PLT32_BND:
  3241. // Since we know this is a local symbol, we can handle this as a
  3242. // PC32 reloc.
  3243. break;
  3244. case elfcpp::R_X86_64_GOTPC32:
  3245. case elfcpp::R_X86_64_GOTOFF64:
  3246. case elfcpp::R_X86_64_GOTPC64:
  3247. case elfcpp::R_X86_64_PLTOFF64:
  3248. // We need a GOT section.
  3249. target->got_section(symtab, layout);
  3250. // For PLTOFF64, we'd normally want a PLT section, but since we
  3251. // know this is a local symbol, no PLT is needed.
  3252. break;
  3253. case elfcpp::R_X86_64_GOT64:
  3254. case elfcpp::R_X86_64_GOT32:
  3255. case elfcpp::R_X86_64_GOTPCREL64:
  3256. case elfcpp::R_X86_64_GOTPCREL:
  3257. case elfcpp::R_X86_64_GOTPCRELX:
  3258. case elfcpp::R_X86_64_REX_GOTPCRELX:
  3259. case elfcpp::R_X86_64_GOTPLT64:
  3260. {
  3261. // The symbol requires a GOT section.
  3262. Output_data_got<64, false>* got = target->got_section(symtab, layout);
  3263. // If the relocation symbol isn't IFUNC,
  3264. // and is local, then we will convert
  3265. // mov foo@GOTPCREL(%rip), %reg
  3266. // to lea foo(%rip), %reg.
  3267. // in Relocate::relocate.
  3268. if (!parameters->incremental()
  3269. && (r_type == elfcpp::R_X86_64_GOTPCREL
  3270. || r_type == elfcpp::R_X86_64_GOTPCRELX
  3271. || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
  3272. && reloc.get_r_addend() == -4
  3273. && reloc.get_r_offset() >= 2
  3274. && !is_ifunc)
  3275. {
  3276. section_size_type stype;
  3277. const unsigned char* view = object->section_contents(data_shndx,
  3278. &stype, true);
  3279. if (view[reloc.get_r_offset() - 2] == 0x8b)
  3280. break;
  3281. }
  3282. // The symbol requires a GOT entry.
  3283. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  3284. // For a STT_GNU_IFUNC symbol we want the PLT offset. That
  3285. // lets function pointers compare correctly with shared
  3286. // libraries. Otherwise we would need an IRELATIVE reloc.
  3287. bool is_new;
  3288. if (is_ifunc)
  3289. is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
  3290. else
  3291. is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
  3292. if (is_new)
  3293. {
  3294. // If we are generating a shared object, we need to add a
  3295. // dynamic relocation for this symbol's GOT entry.
  3296. if (parameters->options().output_is_position_independent())
  3297. {
  3298. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  3299. // R_X86_64_RELATIVE assumes a 64-bit relocation.
  3300. if (r_type != elfcpp::R_X86_64_GOT32)
  3301. {
  3302. unsigned int got_offset =
  3303. object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
  3304. rela_dyn->add_local_relative(object, r_sym,
  3305. elfcpp::R_X86_64_RELATIVE,
  3306. got, got_offset, 0, is_ifunc);
  3307. }
  3308. else
  3309. {
  3310. this->check_non_pic(object, r_type, NULL);
  3311. gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
  3312. rela_dyn->add_local(
  3313. object, r_sym, r_type, got,
  3314. object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
  3315. }
  3316. }
  3317. }
  3318. // For GOTPLT64, we'd normally want a PLT section, but since
  3319. // we know this is a local symbol, no PLT is needed.
  3320. }
  3321. break;
  3322. case elfcpp::R_X86_64_COPY:
  3323. case elfcpp::R_X86_64_GLOB_DAT:
  3324. case elfcpp::R_X86_64_JUMP_SLOT:
  3325. case elfcpp::R_X86_64_RELATIVE:
  3326. case elfcpp::R_X86_64_IRELATIVE:
  3327. // These are outstanding tls relocs, which are unexpected when linking
  3328. case elfcpp::R_X86_64_TPOFF64:
  3329. case elfcpp::R_X86_64_DTPMOD64:
  3330. case elfcpp::R_X86_64_TLSDESC:
  3331. gold_error(_("%s: unexpected reloc %u in object file"),
  3332. object->name().c_str(), r_type);
  3333. break;
  3334. // These are initial tls relocs, which are expected when linking
  3335. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  3336. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  3337. case elfcpp::R_X86_64_TLSDESC_CALL:
  3338. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  3339. case elfcpp::R_X86_64_DTPOFF32:
  3340. case elfcpp::R_X86_64_DTPOFF64:
  3341. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  3342. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  3343. {
  3344. bool output_is_shared = parameters->options().shared();
  3345. const tls::Tls_optimization optimized_type
  3346. = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
  3347. r_type);
  3348. switch (r_type)
  3349. {
  3350. case elfcpp::R_X86_64_TLSGD: // General-dynamic
  3351. if (optimized_type == tls::TLSOPT_NONE)
  3352. {
  3353. // Create a pair of GOT entries for the module index and
  3354. // dtv-relative offset.
  3355. Output_data_got<64, false>* got
  3356. = target->got_section(symtab, layout);
  3357. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  3358. unsigned int shndx = lsym.get_st_shndx();
  3359. bool is_ordinary;
  3360. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  3361. if (!is_ordinary)
  3362. object->error(_("local symbol %u has bad shndx %u"),
  3363. r_sym, shndx);
  3364. else
  3365. got->add_local_pair_with_rel(object, r_sym,
  3366. shndx,
  3367. GOT_TYPE_TLS_PAIR,
  3368. target->rela_dyn_section(layout),
  3369. elfcpp::R_X86_64_DTPMOD64);
  3370. }
  3371. else if (optimized_type != tls::TLSOPT_TO_LE)
  3372. unsupported_reloc_local(object, r_type);
  3373. break;
  3374. case elfcpp::R_X86_64_GOTPC32_TLSDESC:
  3375. target->define_tls_base_symbol(symtab, layout);
  3376. if (optimized_type == tls::TLSOPT_NONE)
  3377. {
  3378. // Create reserved PLT and GOT entries for the resolver.
  3379. target->reserve_tlsdesc_entries(symtab, layout);
  3380. // Generate a double GOT entry with an
  3381. // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
  3382. // is resolved lazily, so the GOT entry needs to be in
  3383. // an area in .got.plt, not .got. Call got_section to
  3384. // make sure the section has been created.
  3385. target->got_section(symtab, layout);
  3386. Output_data_got<64, false>* got = target->got_tlsdesc_section();
  3387. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  3388. if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
  3389. {
  3390. unsigned int got_offset = got->add_constant(0);
  3391. got->add_constant(0);
  3392. object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
  3393. got_offset);
  3394. Reloc_section* rt = target->rela_tlsdesc_section(layout);
  3395. // We store the arguments we need in a vector, and
  3396. // use the index into the vector as the parameter
  3397. // to pass to the target specific routines.
  3398. uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
  3399. void* arg = reinterpret_cast<void*>(intarg);
  3400. rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
  3401. got, got_offset, 0);
  3402. }
  3403. }
  3404. else if (optimized_type != tls::TLSOPT_TO_LE)
  3405. unsupported_reloc_local(object, r_type);
  3406. break;
  3407. case elfcpp::R_X86_64_TLSDESC_CALL:
  3408. break;
  3409. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  3410. if (optimized_type == tls::TLSOPT_NONE)
  3411. {
  3412. // Create a GOT entry for the module index.
  3413. target->got_mod_index_entry(symtab, layout, object);
  3414. }
  3415. else if (optimized_type != tls::TLSOPT_TO_LE)
  3416. unsupported_reloc_local(object, r_type);
  3417. break;
  3418. case elfcpp::R_X86_64_DTPOFF32:
  3419. case elfcpp::R_X86_64_DTPOFF64:
  3420. break;
  3421. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  3422. layout->set_has_static_tls();
  3423. if (optimized_type == tls::TLSOPT_NONE)
  3424. {
  3425. // Create a GOT entry for the tp-relative offset.
  3426. Output_data_got<64, false>* got
  3427. = target->got_section(symtab, layout);
  3428. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  3429. got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
  3430. target->rela_dyn_section(layout),
  3431. elfcpp::R_X86_64_TPOFF64);
  3432. }
  3433. else if (optimized_type != tls::TLSOPT_TO_LE)
  3434. unsupported_reloc_local(object, r_type);
  3435. break;
  3436. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  3437. layout->set_has_static_tls();
  3438. if (output_is_shared)
  3439. unsupported_reloc_local(object, r_type);
  3440. break;
  3441. default:
  3442. gold_unreachable();
  3443. }
  3444. }
  3445. break;
  3446. case elfcpp::R_X86_64_SIZE32:
  3447. case elfcpp::R_X86_64_SIZE64:
  3448. default:
  3449. gold_error(_("%s: unsupported reloc %u against local symbol"),
  3450. object->name().c_str(), r_type);
  3451. break;
  3452. }
  3453. }
  3454. // Report an unsupported relocation against a global symbol.
  3455. template<int size>
  3456. void
  3457. Target_x86_64<size>::Scan::unsupported_reloc_global(
  3458. Sized_relobj_file<size, false>* object,
  3459. unsigned int r_type,
  3460. Symbol* gsym)
  3461. {
  3462. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  3463. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  3464. }
  3465. // Returns true if this relocation type could be that of a function pointer.
  3466. template<int size>
  3467. inline bool
  3468. Target_x86_64<size>::Scan::possible_function_pointer_reloc(
  3469. Sized_relobj_file<size, false>* src_obj,
  3470. unsigned int src_indx,
  3471. unsigned int r_offset,
  3472. unsigned int r_type)
  3473. {
  3474. switch (r_type)
  3475. {
  3476. case elfcpp::R_X86_64_64:
  3477. case elfcpp::R_X86_64_32:
  3478. case elfcpp::R_X86_64_32S:
  3479. case elfcpp::R_X86_64_16:
  3480. case elfcpp::R_X86_64_8:
  3481. case elfcpp::R_X86_64_GOT64:
  3482. case elfcpp::R_X86_64_GOT32:
  3483. case elfcpp::R_X86_64_GOTPCREL64:
  3484. case elfcpp::R_X86_64_GOTPCREL:
  3485. case elfcpp::R_X86_64_GOTPCRELX:
  3486. case elfcpp::R_X86_64_REX_GOTPCRELX:
  3487. case elfcpp::R_X86_64_GOTPLT64:
  3488. {
  3489. return true;
  3490. }
  3491. case elfcpp::R_X86_64_PC32:
  3492. {
  3493. // This relocation may be used both for function calls and
  3494. // for taking address of a function. We distinguish between
  3495. // them by checking the opcodes.
  3496. uint64_t sh_flags = src_obj->section_flags(src_indx);
  3497. bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0;
  3498. if (is_executable)
  3499. {
  3500. section_size_type stype;
  3501. const unsigned char* view = src_obj->section_contents(src_indx,
  3502. &stype,
  3503. true);
  3504. // call
  3505. if (r_offset >= 1
  3506. && view[r_offset - 1] == 0xe8)
  3507. return false;
  3508. // jmp
  3509. if (r_offset >= 1
  3510. && view[r_offset - 1] == 0xe9)
  3511. return false;
  3512. // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
  3513. if (r_offset >= 2
  3514. && view[r_offset - 2] == 0x0f
  3515. && view[r_offset - 1] >= 0x80
  3516. && view[r_offset - 1] <= 0x8f)
  3517. return false;
  3518. }
  3519. // Be conservative and treat all others as function pointers.
  3520. return true;
  3521. }
  3522. }
  3523. return false;
  3524. }
  3525. // For safe ICF, scan a relocation for a local symbol to check if it
  3526. // corresponds to a function pointer being taken. In that case mark
  3527. // the function whose pointer was taken as not foldable.
  3528. template<int size>
  3529. inline bool
  3530. Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
  3531. Symbol_table* ,
  3532. Layout* ,
  3533. Target_x86_64<size>* ,
  3534. Sized_relobj_file<size, false>* src_obj,
  3535. unsigned int src_indx,
  3536. Output_section* ,
  3537. const elfcpp::Rela<size, false>& reloc,
  3538. unsigned int r_type,
  3539. const elfcpp::Sym<size, false>&)
  3540. {
  3541. return possible_function_pointer_reloc(src_obj, src_indx,
  3542. reloc.get_r_offset(), r_type);
  3543. }
  3544. // For safe ICF, scan a relocation for a global symbol to check if it
  3545. // corresponds to a function pointer being taken. In that case mark
  3546. // the function whose pointer was taken as not foldable.
  3547. template<int size>
  3548. inline bool
  3549. Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
  3550. Symbol_table*,
  3551. Layout* ,
  3552. Target_x86_64<size>* ,
  3553. Sized_relobj_file<size, false>* src_obj,
  3554. unsigned int src_indx,
  3555. Output_section* ,
  3556. const elfcpp::Rela<size, false>& reloc,
  3557. unsigned int r_type,
  3558. Symbol*)
  3559. {
  3560. return possible_function_pointer_reloc(src_obj, src_indx,
  3561. reloc.get_r_offset(), r_type);
  3562. }
  3563. // Scan a relocation for a global symbol.
  3564. template<int size>
  3565. inline void
  3566. Target_x86_64<size>::Scan::global(Symbol_table* symtab,
  3567. Layout* layout,
  3568. Target_x86_64<size>* target,
  3569. Sized_relobj_file<size, false>* object,
  3570. unsigned int data_shndx,
  3571. Output_section* output_section,
  3572. const elfcpp::Rela<size, false>& reloc,
  3573. unsigned int r_type,
  3574. Symbol* gsym)
  3575. {
  3576. // A STT_GNU_IFUNC symbol may require a PLT entry.
  3577. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  3578. && this->reloc_needs_plt_for_ifunc(object, r_type))
  3579. target->make_plt_entry(symtab, layout, gsym);
  3580. switch (r_type)
  3581. {
  3582. case elfcpp::R_X86_64_NONE:
  3583. case elfcpp::R_X86_64_GNU_VTINHERIT:
  3584. case elfcpp::R_X86_64_GNU_VTENTRY:
  3585. break;
  3586. case elfcpp::R_X86_64_64:
  3587. case elfcpp::R_X86_64_32:
  3588. case elfcpp::R_X86_64_32S:
  3589. case elfcpp::R_X86_64_16:
  3590. case elfcpp::R_X86_64_8:
  3591. {
  3592. // Make a PLT entry if necessary.
  3593. if (gsym->needs_plt_entry())
  3594. {
  3595. target->make_plt_entry(symtab, layout, gsym);
  3596. // Since this is not a PC-relative relocation, we may be
  3597. // taking the address of a function. In that case we need to
  3598. // set the entry in the dynamic symbol table to the address of
  3599. // the PLT entry.
  3600. if (gsym->is_from_dynobj() && !parameters->options().shared())
  3601. gsym->set_needs_dynsym_value();
  3602. }
  3603. // Make a dynamic relocation if necessary.
  3604. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  3605. {
  3606. if (!parameters->options().output_is_position_independent()
  3607. && gsym->may_need_copy_reloc())
  3608. {
  3609. target->copy_reloc(symtab, layout, object,
  3610. data_shndx, output_section, gsym, reloc);
  3611. }
  3612. else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
  3613. || (size == 32 && r_type == elfcpp::R_X86_64_32))
  3614. && gsym->type() == elfcpp::STT_GNU_IFUNC
  3615. && gsym->can_use_relative_reloc(false)
  3616. && !gsym->is_from_dynobj()
  3617. && !gsym->is_undefined()
  3618. && !gsym->is_preemptible())
  3619. {
  3620. // Use an IRELATIVE reloc for a locally defined
  3621. // STT_GNU_IFUNC symbol. This makes a function
  3622. // address in a PIE executable match the address in a
  3623. // shared library that it links against.
  3624. Reloc_section* rela_dyn =
  3625. target->rela_irelative_section(layout);
  3626. unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
  3627. rela_dyn->add_symbolless_global_addend(gsym, r_type,
  3628. output_section, object,
  3629. data_shndx,
  3630. reloc.get_r_offset(),
  3631. reloc.get_r_addend());
  3632. }
  3633. else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
  3634. || (size == 32 && r_type == elfcpp::R_X86_64_32))
  3635. && gsym->can_use_relative_reloc(false))
  3636. {
  3637. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  3638. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
  3639. output_section, object,
  3640. data_shndx,
  3641. reloc.get_r_offset(),
  3642. reloc.get_r_addend(), false);
  3643. }
  3644. else
  3645. {
  3646. this->check_non_pic(object, r_type, gsym);
  3647. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  3648. rela_dyn->add_global(gsym, r_type, output_section, object,
  3649. data_shndx, reloc.get_r_offset(),
  3650. reloc.get_r_addend());
  3651. }
  3652. }
  3653. }
  3654. break;
  3655. case elfcpp::R_X86_64_PC64:
  3656. case elfcpp::R_X86_64_PC32:
  3657. case elfcpp::R_X86_64_PC32_BND:
  3658. case elfcpp::R_X86_64_PC16:
  3659. case elfcpp::R_X86_64_PC8:
  3660. {
  3661. // Make a PLT entry if necessary.
  3662. if (gsym->needs_plt_entry())
  3663. target->make_plt_entry(symtab, layout, gsym);
  3664. // Make a dynamic relocation if necessary.
  3665. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  3666. {
  3667. if (parameters->options().output_is_executable()
  3668. && gsym->may_need_copy_reloc())
  3669. {
  3670. target->copy_reloc(symtab, layout, object,
  3671. data_shndx, output_section, gsym, reloc);
  3672. }
  3673. else
  3674. {
  3675. this->check_non_pic(object, r_type, gsym);
  3676. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  3677. rela_dyn->add_global(gsym, r_type, output_section, object,
  3678. data_shndx, reloc.get_r_offset(),
  3679. reloc.get_r_addend());
  3680. }
  3681. }
  3682. }
  3683. break;
  3684. case elfcpp::R_X86_64_GOT64:
  3685. case elfcpp::R_X86_64_GOT32:
  3686. case elfcpp::R_X86_64_GOTPCREL64:
  3687. case elfcpp::R_X86_64_GOTPCREL:
  3688. case elfcpp::R_X86_64_GOTPCRELX:
  3689. case elfcpp::R_X86_64_REX_GOTPCRELX:
  3690. case elfcpp::R_X86_64_GOTPLT64:
  3691. {
  3692. // The symbol requires a GOT entry.
  3693. Output_data_got<64, false>* got = target->got_section(symtab, layout);
  3694. // If we convert this from
  3695. // mov foo@GOTPCREL(%rip), %reg
  3696. // to lea foo(%rip), %reg.
  3697. // OR
  3698. // if we convert
  3699. // (callq|jmpq) *foo@GOTPCRELX(%rip) to
  3700. // (callq|jmpq) foo
  3701. // in Relocate::relocate, then there is nothing to do here.
  3702. // We cannot make these optimizations in incremental linking mode,
  3703. // because we look at the opcode to decide whether or not to make
  3704. // change, and during an incremental update, the change may have
  3705. // already been applied.
  3706. Lazy_view<size> view(object, data_shndx);
  3707. size_t r_offset = reloc.get_r_offset();
  3708. if (!parameters->incremental()
  3709. && reloc.get_r_addend() == -4
  3710. && r_offset >= 2
  3711. && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
  3712. r_offset, &view))
  3713. break;
  3714. if (!parameters->incremental()
  3715. && r_offset >= 2
  3716. && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
  3717. r_offset,
  3718. &view))
  3719. break;
  3720. if (gsym->final_value_is_known())
  3721. {
  3722. // For a STT_GNU_IFUNC symbol we want the PLT address.
  3723. if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  3724. got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  3725. else
  3726. got->add_global(gsym, GOT_TYPE_STANDARD);
  3727. }
  3728. else
  3729. {
  3730. // If this symbol is not fully resolved, we need to add a
  3731. // dynamic relocation for it.
  3732. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  3733. // Use a GLOB_DAT rather than a RELATIVE reloc if:
  3734. //
  3735. // 1) The symbol may be defined in some other module.
  3736. //
  3737. // 2) We are building a shared library and this is a
  3738. // protected symbol; using GLOB_DAT means that the dynamic
  3739. // linker can use the address of the PLT in the main
  3740. // executable when appropriate so that function address
  3741. // comparisons work.
  3742. //
  3743. // 3) This is a STT_GNU_IFUNC symbol in position dependent
  3744. // code, again so that function address comparisons work.
  3745. if (gsym->is_from_dynobj()
  3746. || gsym->is_undefined()
  3747. || gsym->is_preemptible()
  3748. || (gsym->visibility() == elfcpp::STV_PROTECTED
  3749. && parameters->options().shared())
  3750. || (gsym->type() == elfcpp::STT_GNU_IFUNC
  3751. && parameters->options().output_is_position_independent()))
  3752. got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
  3753. elfcpp::R_X86_64_GLOB_DAT);
  3754. else
  3755. {
  3756. // For a STT_GNU_IFUNC symbol we want to write the PLT
  3757. // offset into the GOT, so that function pointer
  3758. // comparisons work correctly.
  3759. bool is_new;
  3760. if (gsym->type() != elfcpp::STT_GNU_IFUNC)
  3761. is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
  3762. else
  3763. {
  3764. is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  3765. // Tell the dynamic linker to use the PLT address
  3766. // when resolving relocations.
  3767. if (gsym->is_from_dynobj()
  3768. && !parameters->options().shared())
  3769. gsym->set_needs_dynsym_value();
  3770. }
  3771. if (is_new)
  3772. {
  3773. unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
  3774. rela_dyn->add_global_relative(gsym,
  3775. elfcpp::R_X86_64_RELATIVE,
  3776. got, got_off, 0, false);
  3777. }
  3778. }
  3779. }
  3780. }
  3781. break;
  3782. case elfcpp::R_X86_64_PLT32:
  3783. case elfcpp::R_X86_64_PLT32_BND:
  3784. // If the symbol is fully resolved, this is just a PC32 reloc.
  3785. // Otherwise we need a PLT entry.
  3786. if (gsym->final_value_is_known())
  3787. break;
  3788. // If building a shared library, we can also skip the PLT entry
  3789. // if the symbol is defined in the output file and is protected
  3790. // or hidden.
  3791. if (gsym->is_defined()
  3792. && !gsym->is_from_dynobj()
  3793. && !gsym->is_preemptible())
  3794. break;
  3795. target->make_plt_entry(symtab, layout, gsym);
  3796. break;
  3797. case elfcpp::R_X86_64_GOTPC32:
  3798. case elfcpp::R_X86_64_GOTOFF64:
  3799. case elfcpp::R_X86_64_GOTPC64:
  3800. case elfcpp::R_X86_64_PLTOFF64:
  3801. // We need a GOT section.
  3802. target->got_section(symtab, layout);
  3803. // For PLTOFF64, we also need a PLT entry (but only if the
  3804. // symbol is not fully resolved).
  3805. if (r_type == elfcpp::R_X86_64_PLTOFF64
  3806. && !gsym->final_value_is_known())
  3807. target->make_plt_entry(symtab, layout, gsym);
  3808. break;
  3809. case elfcpp::R_X86_64_COPY:
  3810. case elfcpp::R_X86_64_GLOB_DAT:
  3811. case elfcpp::R_X86_64_JUMP_SLOT:
  3812. case elfcpp::R_X86_64_RELATIVE:
  3813. case elfcpp::R_X86_64_IRELATIVE:
  3814. // These are outstanding tls relocs, which are unexpected when linking
  3815. case elfcpp::R_X86_64_TPOFF64:
  3816. case elfcpp::R_X86_64_DTPMOD64:
  3817. case elfcpp::R_X86_64_TLSDESC:
  3818. gold_error(_("%s: unexpected reloc %u in object file"),
  3819. object->name().c_str(), r_type);
  3820. break;
  3821. // These are initial tls relocs, which are expected for global()
  3822. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  3823. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  3824. case elfcpp::R_X86_64_TLSDESC_CALL:
  3825. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  3826. case elfcpp::R_X86_64_DTPOFF32:
  3827. case elfcpp::R_X86_64_DTPOFF64:
  3828. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  3829. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  3830. {
  3831. // For the Initial-Exec model, we can treat undef symbols as final
  3832. // when building an executable.
  3833. const bool is_final = (gsym->final_value_is_known() ||
  3834. (r_type == elfcpp::R_X86_64_GOTTPOFF &&
  3835. gsym->is_undefined() &&
  3836. parameters->options().output_is_executable()));
  3837. const tls::Tls_optimization optimized_type
  3838. = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
  3839. switch (r_type)
  3840. {
  3841. case elfcpp::R_X86_64_TLSGD: // General-dynamic
  3842. if (optimized_type == tls::TLSOPT_NONE)
  3843. {
  3844. // Create a pair of GOT entries for the module index and
  3845. // dtv-relative offset.
  3846. Output_data_got<64, false>* got
  3847. = target->got_section(symtab, layout);
  3848. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
  3849. target->rela_dyn_section(layout),
  3850. elfcpp::R_X86_64_DTPMOD64,
  3851. elfcpp::R_X86_64_DTPOFF64);
  3852. }
  3853. else if (optimized_type == tls::TLSOPT_TO_IE)
  3854. {
  3855. // Create a GOT entry for the tp-relative offset.
  3856. Output_data_got<64, false>* got
  3857. = target->got_section(symtab, layout);
  3858. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  3859. target->rela_dyn_section(layout),
  3860. elfcpp::R_X86_64_TPOFF64);
  3861. }
  3862. else if (optimized_type != tls::TLSOPT_TO_LE)
  3863. unsupported_reloc_global(object, r_type, gsym);
  3864. break;
  3865. case elfcpp::R_X86_64_GOTPC32_TLSDESC:
  3866. target->define_tls_base_symbol(symtab, layout);
  3867. if (optimized_type == tls::TLSOPT_NONE)
  3868. {
  3869. // Create reserved PLT and GOT entries for the resolver.
  3870. target->reserve_tlsdesc_entries(symtab, layout);
  3871. // Create a double GOT entry with an R_X86_64_TLSDESC
  3872. // reloc. The R_X86_64_TLSDESC reloc is resolved
  3873. // lazily, so the GOT entry needs to be in an area in
  3874. // .got.plt, not .got. Call got_section to make sure
  3875. // the section has been created.
  3876. target->got_section(symtab, layout);
  3877. Output_data_got<64, false>* got = target->got_tlsdesc_section();
  3878. Reloc_section* rt = target->rela_tlsdesc_section(layout);
  3879. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
  3880. elfcpp::R_X86_64_TLSDESC, 0);
  3881. }
  3882. else if (optimized_type == tls::TLSOPT_TO_IE)
  3883. {
  3884. // Create a GOT entry for the tp-relative offset.
  3885. Output_data_got<64, false>* got
  3886. = target->got_section(symtab, layout);
  3887. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  3888. target->rela_dyn_section(layout),
  3889. elfcpp::R_X86_64_TPOFF64);
  3890. }
  3891. else if (optimized_type != tls::TLSOPT_TO_LE)
  3892. unsupported_reloc_global(object, r_type, gsym);
  3893. break;
  3894. case elfcpp::R_X86_64_TLSDESC_CALL:
  3895. break;
  3896. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  3897. if (optimized_type == tls::TLSOPT_NONE)
  3898. {
  3899. // Create a GOT entry for the module index.
  3900. target->got_mod_index_entry(symtab, layout, object);
  3901. }
  3902. else if (optimized_type != tls::TLSOPT_TO_LE)
  3903. unsupported_reloc_global(object, r_type, gsym);
  3904. break;
  3905. case elfcpp::R_X86_64_DTPOFF32:
  3906. case elfcpp::R_X86_64_DTPOFF64:
  3907. break;
  3908. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  3909. layout->set_has_static_tls();
  3910. if (optimized_type == tls::TLSOPT_NONE)
  3911. {
  3912. // Create a GOT entry for the tp-relative offset.
  3913. Output_data_got<64, false>* got
  3914. = target->got_section(symtab, layout);
  3915. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  3916. target->rela_dyn_section(layout),
  3917. elfcpp::R_X86_64_TPOFF64);
  3918. }
  3919. else if (optimized_type != tls::TLSOPT_TO_LE)
  3920. unsupported_reloc_global(object, r_type, gsym);
  3921. break;
  3922. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  3923. layout->set_has_static_tls();
  3924. if (parameters->options().shared())
  3925. unsupported_reloc_global(object, r_type, gsym);
  3926. break;
  3927. default:
  3928. gold_unreachable();
  3929. }
  3930. }
  3931. break;
  3932. case elfcpp::R_X86_64_SIZE32:
  3933. case elfcpp::R_X86_64_SIZE64:
  3934. default:
  3935. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  3936. object->name().c_str(), r_type,
  3937. gsym->demangled_name().c_str());
  3938. break;
  3939. }
  3940. }
  3941. template<int size>
  3942. void
  3943. Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
  3944. Layout* layout,
  3945. Sized_relobj_file<size, false>* object,
  3946. unsigned int data_shndx,
  3947. unsigned int sh_type,
  3948. const unsigned char* prelocs,
  3949. size_t reloc_count,
  3950. Output_section* output_section,
  3951. bool needs_special_offset_handling,
  3952. size_t local_symbol_count,
  3953. const unsigned char* plocal_symbols)
  3954. {
  3955. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
  3956. Classify_reloc;
  3957. if (sh_type == elfcpp::SHT_REL)
  3958. {
  3959. return;
  3960. }
  3961. gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
  3962. Classify_reloc>(
  3963. symtab,
  3964. layout,
  3965. this,
  3966. object,
  3967. data_shndx,
  3968. prelocs,
  3969. reloc_count,
  3970. output_section,
  3971. needs_special_offset_handling,
  3972. local_symbol_count,
  3973. plocal_symbols);
  3974. }
  3975. // Scan relocations for a section.
  3976. template<int size>
  3977. void
  3978. Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
  3979. Layout* layout,
  3980. Sized_relobj_file<size, false>* object,
  3981. unsigned int data_shndx,
  3982. unsigned int sh_type,
  3983. const unsigned char* prelocs,
  3984. size_t reloc_count,
  3985. Output_section* output_section,
  3986. bool needs_special_offset_handling,
  3987. size_t local_symbol_count,
  3988. const unsigned char* plocal_symbols)
  3989. {
  3990. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
  3991. Classify_reloc;
  3992. if (sh_type == elfcpp::SHT_REL)
  3993. {
  3994. gold_error(_("%s: unsupported REL reloc section"),
  3995. object->name().c_str());
  3996. return;
  3997. }
  3998. gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
  3999. symtab,
  4000. layout,
  4001. this,
  4002. object,
  4003. data_shndx,
  4004. prelocs,
  4005. reloc_count,
  4006. output_section,
  4007. needs_special_offset_handling,
  4008. local_symbol_count,
  4009. plocal_symbols);
  4010. }
  4011. // Finalize the sections.
  4012. template<int size>
  4013. void
  4014. Target_x86_64<size>::do_finalize_sections(
  4015. Layout* layout,
  4016. const Input_objects*,
  4017. Symbol_table* symtab)
  4018. {
  4019. const Reloc_section* rel_plt = (this->plt_ == NULL
  4020. ? NULL
  4021. : this->plt_->rela_plt());
  4022. layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
  4023. this->rela_dyn_, true, false);
  4024. // Fill in some more dynamic tags.
  4025. Output_data_dynamic* const odyn = layout->dynamic_data();
  4026. if (odyn != NULL)
  4027. {
  4028. if (this->plt_ != NULL
  4029. && this->plt_->output_section() != NULL
  4030. && this->plt_->has_tlsdesc_entry())
  4031. {
  4032. unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
  4033. unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
  4034. this->got_->finalize_data_size();
  4035. odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
  4036. this->plt_, plt_offset);
  4037. odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
  4038. this->got_, got_offset);
  4039. }
  4040. }
  4041. // Emit any relocs we saved in an attempt to avoid generating COPY
  4042. // relocs.
  4043. if (this->copy_relocs_.any_saved_relocs())
  4044. this->copy_relocs_.emit(this->rela_dyn_section(layout));
  4045. // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
  4046. // the .got.plt section.
  4047. Symbol* sym = this->global_offset_table_;
  4048. if (sym != NULL)
  4049. {
  4050. uint64_t data_size = this->got_plt_->current_data_size();
  4051. symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
  4052. }
  4053. if (parameters->doing_static_link()
  4054. && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
  4055. {
  4056. // If linking statically, make sure that the __rela_iplt symbols
  4057. // were defined if necessary, even if we didn't create a PLT.
  4058. static const Define_symbol_in_segment syms[] =
  4059. {
  4060. {
  4061. "__rela_iplt_start", // name
  4062. elfcpp::PT_LOAD, // segment_type
  4063. elfcpp::PF_W, // segment_flags_set
  4064. elfcpp::PF(0), // segment_flags_clear
  4065. 0, // value
  4066. 0, // size
  4067. elfcpp::STT_NOTYPE, // type
  4068. elfcpp::STB_GLOBAL, // binding
  4069. elfcpp::STV_HIDDEN, // visibility
  4070. 0, // nonvis
  4071. Symbol::SEGMENT_START, // offset_from_base
  4072. true // only_if_ref
  4073. },
  4074. {
  4075. "__rela_iplt_end", // name
  4076. elfcpp::PT_LOAD, // segment_type
  4077. elfcpp::PF_W, // segment_flags_set
  4078. elfcpp::PF(0), // segment_flags_clear
  4079. 0, // value
  4080. 0, // size
  4081. elfcpp::STT_NOTYPE, // type
  4082. elfcpp::STB_GLOBAL, // binding
  4083. elfcpp::STV_HIDDEN, // visibility
  4084. 0, // nonvis
  4085. Symbol::SEGMENT_START, // offset_from_base
  4086. true // only_if_ref
  4087. }
  4088. };
  4089. symtab->define_symbols(layout, 2, syms,
  4090. layout->script_options()->saw_sections_clause());
  4091. }
  4092. }
  4093. // For x32, we need to handle PC-relative relocations using full 64-bit
  4094. // arithmetic, so that we can detect relocation overflows properly.
  4095. // This class overrides the pcrela32_check methods from the defaults in
  4096. // Relocate_functions in reloc.h.
  4097. template<int size>
  4098. class X86_64_relocate_functions : public Relocate_functions<size, false>
  4099. {
  4100. public:
  4101. typedef Relocate_functions<size, false> Base;
  4102. // Do a simple PC relative relocation with the addend in the
  4103. // relocation.
  4104. static inline typename Base::Reloc_status
  4105. pcrela32_check(unsigned char* view,
  4106. typename elfcpp::Elf_types<64>::Elf_Addr value,
  4107. typename elfcpp::Elf_types<64>::Elf_Swxword addend,
  4108. typename elfcpp::Elf_types<64>::Elf_Addr address)
  4109. {
  4110. typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
  4111. Valtype* wv = reinterpret_cast<Valtype*>(view);
  4112. value = value + addend - address;
  4113. elfcpp::Swap<32, false>::writeval(wv, value);
  4114. return (Bits<32>::has_overflow(value)
  4115. ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
  4116. }
  4117. // Do a simple PC relative relocation with a Symbol_value with the
  4118. // addend in the relocation.
  4119. static inline typename Base::Reloc_status
  4120. pcrela32_check(unsigned char* view,
  4121. const Sized_relobj_file<size, false>* object,
  4122. const Symbol_value<size>* psymval,
  4123. typename elfcpp::Elf_types<64>::Elf_Swxword addend,
  4124. typename elfcpp::Elf_types<64>::Elf_Addr address)
  4125. {
  4126. typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
  4127. Valtype* wv = reinterpret_cast<Valtype*>(view);
  4128. typename elfcpp::Elf_types<64>::Elf_Addr value;
  4129. if (addend >= 0)
  4130. value = psymval->value(object, addend);
  4131. else
  4132. {
  4133. // For negative addends, get the symbol value without
  4134. // the addend, then add the addend using 64-bit arithmetic.
  4135. value = psymval->value(object, 0);
  4136. value += addend;
  4137. }
  4138. value -= address;
  4139. elfcpp::Swap<32, false>::writeval(wv, value);
  4140. return (Bits<32>::has_overflow(value)
  4141. ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
  4142. }
  4143. };
  4144. // Perform a relocation.
  4145. template<int size>
  4146. inline bool
  4147. Target_x86_64<size>::Relocate::relocate(
  4148. const Relocate_info<size, false>* relinfo,
  4149. unsigned int,
  4150. Target_x86_64<size>* target,
  4151. Output_section*,
  4152. size_t relnum,
  4153. const unsigned char* preloc,
  4154. const Sized_symbol<size>* gsym,
  4155. const Symbol_value<size>* psymval,
  4156. unsigned char* view,
  4157. typename elfcpp::Elf_types<size>::Elf_Addr address,
  4158. section_size_type view_size)
  4159. {
  4160. typedef X86_64_relocate_functions<size> Reloc_funcs;
  4161. const elfcpp::Rela<size, false> rela(preloc);
  4162. unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
  4163. if (this->skip_call_tls_get_addr_)
  4164. {
  4165. if ((r_type != elfcpp::R_X86_64_PLT32
  4166. && r_type != elfcpp::R_X86_64_GOTPCREL
  4167. && r_type != elfcpp::R_X86_64_GOTPCRELX
  4168. && r_type != elfcpp::R_X86_64_PLT32_BND
  4169. && r_type != elfcpp::R_X86_64_PC32_BND
  4170. && r_type != elfcpp::R_X86_64_PC32)
  4171. || gsym == NULL
  4172. || strcmp(gsym->name(), "__tls_get_addr") != 0)
  4173. {
  4174. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4175. _("missing expected TLS relocation"));
  4176. this->skip_call_tls_get_addr_ = false;
  4177. }
  4178. else
  4179. {
  4180. this->skip_call_tls_get_addr_ = false;
  4181. return false;
  4182. }
  4183. }
  4184. if (view == NULL)
  4185. return true;
  4186. const Sized_relobj_file<size, false>* object = relinfo->object;
  4187. // Pick the value to use for symbols defined in the PLT.
  4188. Symbol_value<size> symval;
  4189. if (gsym != NULL
  4190. && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
  4191. {
  4192. symval.set_output_value(target->plt_address_for_global(gsym));
  4193. psymval = &symval;
  4194. }
  4195. else if (gsym == NULL && psymval->is_ifunc_symbol())
  4196. {
  4197. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  4198. if (object->local_has_plt_offset(r_sym))
  4199. {
  4200. symval.set_output_value(target->plt_address_for_local(object, r_sym));
  4201. psymval = &symval;
  4202. }
  4203. }
  4204. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  4205. // Get the GOT offset if needed.
  4206. // The GOT pointer points to the end of the GOT section.
  4207. // We need to subtract the size of the GOT section to get
  4208. // the actual offset to use in the relocation.
  4209. bool have_got_offset = false;
  4210. // Since the actual offset is always negative, we use signed int to
  4211. // support 64-bit GOT relocations.
  4212. int got_offset = 0;
  4213. switch (r_type)
  4214. {
  4215. case elfcpp::R_X86_64_GOT32:
  4216. case elfcpp::R_X86_64_GOT64:
  4217. case elfcpp::R_X86_64_GOTPLT64:
  4218. case elfcpp::R_X86_64_GOTPCREL64:
  4219. if (gsym != NULL)
  4220. {
  4221. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  4222. got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
  4223. }
  4224. else
  4225. {
  4226. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  4227. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
  4228. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
  4229. - target->got_size());
  4230. }
  4231. have_got_offset = true;
  4232. break;
  4233. default:
  4234. break;
  4235. }
  4236. typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
  4237. switch (r_type)
  4238. {
  4239. case elfcpp::R_X86_64_NONE:
  4240. case elfcpp::R_X86_64_GNU_VTINHERIT:
  4241. case elfcpp::R_X86_64_GNU_VTENTRY:
  4242. break;
  4243. case elfcpp::R_X86_64_64:
  4244. Reloc_funcs::rela64(view, object, psymval, addend);
  4245. break;
  4246. case elfcpp::R_X86_64_PC64:
  4247. Reloc_funcs::pcrela64(view, object, psymval, addend,
  4248. address);
  4249. break;
  4250. case elfcpp::R_X86_64_32:
  4251. rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
  4252. Reloc_funcs::CHECK_UNSIGNED);
  4253. break;
  4254. case elfcpp::R_X86_64_32S:
  4255. rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
  4256. Reloc_funcs::CHECK_SIGNED);
  4257. break;
  4258. case elfcpp::R_X86_64_PC32:
  4259. case elfcpp::R_X86_64_PC32_BND:
  4260. rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
  4261. address);
  4262. break;
  4263. case elfcpp::R_X86_64_16:
  4264. Reloc_funcs::rela16(view, object, psymval, addend);
  4265. break;
  4266. case elfcpp::R_X86_64_PC16:
  4267. Reloc_funcs::pcrela16(view, object, psymval, addend, address);
  4268. break;
  4269. case elfcpp::R_X86_64_8:
  4270. Reloc_funcs::rela8(view, object, psymval, addend);
  4271. break;
  4272. case elfcpp::R_X86_64_PC8:
  4273. Reloc_funcs::pcrela8(view, object, psymval, addend, address);
  4274. break;
  4275. case elfcpp::R_X86_64_PLT32:
  4276. case elfcpp::R_X86_64_PLT32_BND:
  4277. gold_assert(gsym == NULL
  4278. || gsym->has_plt_offset()
  4279. || gsym->final_value_is_known()
  4280. || (gsym->is_defined()
  4281. && !gsym->is_from_dynobj()
  4282. && !gsym->is_preemptible()));
  4283. // Note: while this code looks the same as for R_X86_64_PC32, it
  4284. // behaves differently because psymval was set to point to
  4285. // the PLT entry, rather than the symbol, in Scan::global().
  4286. rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
  4287. address);
  4288. break;
  4289. case elfcpp::R_X86_64_PLTOFF64:
  4290. {
  4291. gold_assert(gsym);
  4292. gold_assert(gsym->has_plt_offset()
  4293. || gsym->final_value_is_known());
  4294. typename elfcpp::Elf_types<size>::Elf_Addr got_address;
  4295. // This is the address of GLOBAL_OFFSET_TABLE.
  4296. got_address = target->got_plt_section()->address();
  4297. Reloc_funcs::rela64(view, object, psymval, addend - got_address);
  4298. }
  4299. break;
  4300. case elfcpp::R_X86_64_GOT32:
  4301. gold_assert(have_got_offset);
  4302. Reloc_funcs::rela32(view, got_offset, addend);
  4303. break;
  4304. case elfcpp::R_X86_64_GOTPC32:
  4305. {
  4306. gold_assert(gsym);
  4307. typename elfcpp::Elf_types<size>::Elf_Addr value;
  4308. value = target->got_plt_section()->address();
  4309. Reloc_funcs::pcrela32_check(view, value, addend, address);
  4310. }
  4311. break;
  4312. case elfcpp::R_X86_64_GOT64:
  4313. case elfcpp::R_X86_64_GOTPLT64:
  4314. // R_X86_64_GOTPLT64 is obsolete and treated the same as
  4315. // GOT64.
  4316. gold_assert(have_got_offset);
  4317. Reloc_funcs::rela64(view, got_offset, addend);
  4318. break;
  4319. case elfcpp::R_X86_64_GOTPC64:
  4320. {
  4321. gold_assert(gsym);
  4322. typename elfcpp::Elf_types<size>::Elf_Addr value;
  4323. value = target->got_plt_section()->address();
  4324. Reloc_funcs::pcrela64(view, value, addend, address);
  4325. }
  4326. break;
  4327. case elfcpp::R_X86_64_GOTOFF64:
  4328. {
  4329. typename elfcpp::Elf_types<size>::Elf_Addr reladdr;
  4330. reladdr = target->got_plt_section()->address();
  4331. Reloc_funcs::pcrela64(view, object, psymval, addend, reladdr);
  4332. }
  4333. break;
  4334. case elfcpp::R_X86_64_GOTPCREL:
  4335. case elfcpp::R_X86_64_GOTPCRELX:
  4336. case elfcpp::R_X86_64_REX_GOTPCRELX:
  4337. {
  4338. bool converted_p = false;
  4339. if (rela.get_r_addend() == -4)
  4340. {
  4341. // Convert
  4342. // mov foo@GOTPCREL(%rip), %reg
  4343. // to lea foo(%rip), %reg.
  4344. // if possible.
  4345. if (!parameters->incremental()
  4346. && ((gsym == NULL
  4347. && rela.get_r_offset() >= 2
  4348. && view[-2] == 0x8b
  4349. && !psymval->is_ifunc_symbol())
  4350. || (gsym != NULL
  4351. && rela.get_r_offset() >= 2
  4352. && Target_x86_64<size>::can_convert_mov_to_lea(gsym,
  4353. r_type,
  4354. 0,
  4355. &view))))
  4356. {
  4357. view[-2] = 0x8d;
  4358. Reloc_funcs::pcrela32(view, object, psymval, addend, address);
  4359. converted_p = true;
  4360. }
  4361. // Convert
  4362. // callq *foo@GOTPCRELX(%rip) to
  4363. // addr32 callq foo
  4364. // and jmpq *foo@GOTPCRELX(%rip) to
  4365. // jmpq foo
  4366. // nop
  4367. else if (!parameters->incremental()
  4368. && gsym != NULL
  4369. && rela.get_r_offset() >= 2
  4370. && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
  4371. r_type,
  4372. 0,
  4373. &view))
  4374. {
  4375. if (view[-1] == 0x15)
  4376. {
  4377. // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
  4378. // Opcode of addr32 is 0x67 and opcode of direct callq
  4379. // is 0xe8.
  4380. view[-2] = 0x67;
  4381. view[-1] = 0xe8;
  4382. // Convert GOTPCRELX to 32-bit pc relative reloc.
  4383. Reloc_funcs::pcrela32(view, object, psymval, addend,
  4384. address);
  4385. converted_p = true;
  4386. }
  4387. else
  4388. {
  4389. // Convert jmpq *foo@GOTPCRELX(%rip) to
  4390. // jmpq foo
  4391. // nop
  4392. // The opcode of direct jmpq is 0xe9.
  4393. view[-2] = 0xe9;
  4394. // The opcode of nop is 0x90.
  4395. view[3] = 0x90;
  4396. // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq
  4397. // is rip relative and since the instruction following
  4398. // the jmpq is now the nop, offset the address by 1
  4399. // byte. The start of the relocation also moves ahead
  4400. // by 1 byte.
  4401. Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
  4402. address - 1);
  4403. converted_p = true;
  4404. }
  4405. }
  4406. }
  4407. if (!converted_p)
  4408. {
  4409. if (gsym != NULL)
  4410. {
  4411. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  4412. got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
  4413. - target->got_size());
  4414. }
  4415. else
  4416. {
  4417. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  4418. gold_assert(object->local_has_got_offset(r_sym,
  4419. GOT_TYPE_STANDARD));
  4420. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
  4421. - target->got_size());
  4422. }
  4423. typename elfcpp::Elf_types<size>::Elf_Addr value;
  4424. value = target->got_plt_section()->address() + got_offset;
  4425. Reloc_funcs::pcrela32_check(view, value, addend, address);
  4426. }
  4427. }
  4428. break;
  4429. case elfcpp::R_X86_64_GOTPCREL64:
  4430. {
  4431. gold_assert(have_got_offset);
  4432. typename elfcpp::Elf_types<size>::Elf_Addr value;
  4433. value = target->got_plt_section()->address() + got_offset;
  4434. Reloc_funcs::pcrela64(view, value, addend, address);
  4435. }
  4436. break;
  4437. case elfcpp::R_X86_64_COPY:
  4438. case elfcpp::R_X86_64_GLOB_DAT:
  4439. case elfcpp::R_X86_64_JUMP_SLOT:
  4440. case elfcpp::R_X86_64_RELATIVE:
  4441. case elfcpp::R_X86_64_IRELATIVE:
  4442. // These are outstanding tls relocs, which are unexpected when linking
  4443. case elfcpp::R_X86_64_TPOFF64:
  4444. case elfcpp::R_X86_64_DTPMOD64:
  4445. case elfcpp::R_X86_64_TLSDESC:
  4446. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4447. _("unexpected reloc %u in object file"),
  4448. r_type);
  4449. break;
  4450. // These are initial tls relocs, which are expected when linking
  4451. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  4452. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  4453. case elfcpp::R_X86_64_TLSDESC_CALL:
  4454. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  4455. case elfcpp::R_X86_64_DTPOFF32:
  4456. case elfcpp::R_X86_64_DTPOFF64:
  4457. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  4458. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  4459. this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
  4460. view, address, view_size);
  4461. break;
  4462. case elfcpp::R_X86_64_SIZE32:
  4463. case elfcpp::R_X86_64_SIZE64:
  4464. default:
  4465. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4466. _("unsupported reloc %u"),
  4467. r_type);
  4468. break;
  4469. }
  4470. if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
  4471. {
  4472. if (gsym == NULL)
  4473. {
  4474. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  4475. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4476. _("relocation overflow: "
  4477. "reference to local symbol %u in %s"),
  4478. r_sym, object->name().c_str());
  4479. }
  4480. else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
  4481. {
  4482. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4483. _("relocation overflow: "
  4484. "reference to '%s' defined in %s"),
  4485. gsym->name(),
  4486. gsym->object()->name().c_str());
  4487. }
  4488. else
  4489. {
  4490. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4491. _("relocation overflow: reference to '%s'"),
  4492. gsym->name());
  4493. }
  4494. }
  4495. return true;
  4496. }
  4497. // Perform a TLS relocation.
  4498. template<int size>
  4499. inline void
  4500. Target_x86_64<size>::Relocate::relocate_tls(
  4501. const Relocate_info<size, false>* relinfo,
  4502. Target_x86_64<size>* target,
  4503. size_t relnum,
  4504. const elfcpp::Rela<size, false>& rela,
  4505. unsigned int r_type,
  4506. const Sized_symbol<size>* gsym,
  4507. const Symbol_value<size>* psymval,
  4508. unsigned char* view,
  4509. typename elfcpp::Elf_types<size>::Elf_Addr address,
  4510. section_size_type view_size)
  4511. {
  4512. Output_segment* tls_segment = relinfo->layout->tls_segment();
  4513. const Sized_relobj_file<size, false>* object = relinfo->object;
  4514. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  4515. elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
  4516. bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
  4517. typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
  4518. const bool is_final = (gsym == NULL
  4519. ? !parameters->options().shared()
  4520. : gsym->final_value_is_known());
  4521. tls::Tls_optimization optimized_type
  4522. = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
  4523. switch (r_type)
  4524. {
  4525. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  4526. if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
  4527. {
  4528. // If this code sequence is used in a non-executable section,
  4529. // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
  4530. // on the assumption that it's being used by itself in a debug
  4531. // section. Therefore, in the unlikely event that the code
  4532. // sequence appears in a non-executable section, we simply
  4533. // leave it unoptimized.
  4534. optimized_type = tls::TLSOPT_NONE;
  4535. }
  4536. if (optimized_type == tls::TLSOPT_TO_LE)
  4537. {
  4538. if (tls_segment == NULL)
  4539. {
  4540. gold_assert(parameters->errors()->error_count() > 0
  4541. || issue_undefined_symbol_error(gsym));
  4542. return;
  4543. }
  4544. this->tls_gd_to_le(relinfo, relnum, tls_segment,
  4545. rela, r_type, value, view,
  4546. view_size);
  4547. break;
  4548. }
  4549. else
  4550. {
  4551. unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
  4552. ? GOT_TYPE_TLS_OFFSET
  4553. : GOT_TYPE_TLS_PAIR);
  4554. unsigned int got_offset;
  4555. if (gsym != NULL)
  4556. {
  4557. gold_assert(gsym->has_got_offset(got_type));
  4558. got_offset = gsym->got_offset(got_type) - target->got_size();
  4559. }
  4560. else
  4561. {
  4562. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  4563. gold_assert(object->local_has_got_offset(r_sym, got_type));
  4564. got_offset = (object->local_got_offset(r_sym, got_type)
  4565. - target->got_size());
  4566. }
  4567. if (optimized_type == tls::TLSOPT_TO_IE)
  4568. {
  4569. value = target->got_plt_section()->address() + got_offset;
  4570. this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
  4571. value, view, address, view_size);
  4572. break;
  4573. }
  4574. else if (optimized_type == tls::TLSOPT_NONE)
  4575. {
  4576. // Relocate the field with the offset of the pair of GOT
  4577. // entries.
  4578. value = target->got_plt_section()->address() + got_offset;
  4579. Relocate_functions<size, false>::pcrela32(view, value, addend,
  4580. address);
  4581. break;
  4582. }
  4583. }
  4584. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4585. _("unsupported reloc %u"), r_type);
  4586. break;
  4587. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  4588. case elfcpp::R_X86_64_TLSDESC_CALL:
  4589. if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
  4590. {
  4591. // See above comment for R_X86_64_TLSGD.
  4592. optimized_type = tls::TLSOPT_NONE;
  4593. }
  4594. if (optimized_type == tls::TLSOPT_TO_LE)
  4595. {
  4596. if (tls_segment == NULL)
  4597. {
  4598. gold_assert(parameters->errors()->error_count() > 0
  4599. || issue_undefined_symbol_error(gsym));
  4600. return;
  4601. }
  4602. this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
  4603. rela, r_type, value, view,
  4604. view_size);
  4605. break;
  4606. }
  4607. else
  4608. {
  4609. unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
  4610. ? GOT_TYPE_TLS_OFFSET
  4611. : GOT_TYPE_TLS_DESC);
  4612. unsigned int got_offset = 0;
  4613. if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
  4614. && optimized_type == tls::TLSOPT_NONE)
  4615. {
  4616. // We created GOT entries in the .got.tlsdesc portion of
  4617. // the .got.plt section, but the offset stored in the
  4618. // symbol is the offset within .got.tlsdesc.
  4619. got_offset = (target->got_size()
  4620. + target->got_plt_section()->data_size());
  4621. }
  4622. if (gsym != NULL)
  4623. {
  4624. gold_assert(gsym->has_got_offset(got_type));
  4625. got_offset += gsym->got_offset(got_type) - target->got_size();
  4626. }
  4627. else
  4628. {
  4629. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  4630. gold_assert(object->local_has_got_offset(r_sym, got_type));
  4631. got_offset += (object->local_got_offset(r_sym, got_type)
  4632. - target->got_size());
  4633. }
  4634. if (optimized_type == tls::TLSOPT_TO_IE)
  4635. {
  4636. value = target->got_plt_section()->address() + got_offset;
  4637. this->tls_desc_gd_to_ie(relinfo, relnum,
  4638. rela, r_type, value, view, address,
  4639. view_size);
  4640. break;
  4641. }
  4642. else if (optimized_type == tls::TLSOPT_NONE)
  4643. {
  4644. if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
  4645. {
  4646. // Relocate the field with the offset of the pair of GOT
  4647. // entries.
  4648. value = target->got_plt_section()->address() + got_offset;
  4649. Relocate_functions<size, false>::pcrela32(view, value, addend,
  4650. address);
  4651. }
  4652. break;
  4653. }
  4654. }
  4655. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4656. _("unsupported reloc %u"), r_type);
  4657. break;
  4658. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  4659. if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
  4660. {
  4661. // See above comment for R_X86_64_TLSGD.
  4662. optimized_type = tls::TLSOPT_NONE;
  4663. }
  4664. if (optimized_type == tls::TLSOPT_TO_LE)
  4665. {
  4666. if (tls_segment == NULL)
  4667. {
  4668. gold_assert(parameters->errors()->error_count() > 0
  4669. || issue_undefined_symbol_error(gsym));
  4670. return;
  4671. }
  4672. this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
  4673. value, view, view_size);
  4674. break;
  4675. }
  4676. else if (optimized_type == tls::TLSOPT_NONE)
  4677. {
  4678. // Relocate the field with the offset of the GOT entry for
  4679. // the module index.
  4680. unsigned int got_offset;
  4681. got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
  4682. - target->got_size());
  4683. value = target->got_plt_section()->address() + got_offset;
  4684. Relocate_functions<size, false>::pcrela32(view, value, addend,
  4685. address);
  4686. break;
  4687. }
  4688. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4689. _("unsupported reloc %u"), r_type);
  4690. break;
  4691. case elfcpp::R_X86_64_DTPOFF32:
  4692. // This relocation type is used in debugging information.
  4693. // In that case we need to not optimize the value. If the
  4694. // section is not executable, then we assume we should not
  4695. // optimize this reloc. See comments above for R_X86_64_TLSGD,
  4696. // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
  4697. // R_X86_64_TLSLD.
  4698. if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
  4699. {
  4700. if (tls_segment == NULL)
  4701. {
  4702. gold_assert(parameters->errors()->error_count() > 0
  4703. || issue_undefined_symbol_error(gsym));
  4704. return;
  4705. }
  4706. value -= tls_segment->memsz();
  4707. }
  4708. Relocate_functions<size, false>::rela32(view, value, addend);
  4709. break;
  4710. case elfcpp::R_X86_64_DTPOFF64:
  4711. // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
  4712. if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
  4713. {
  4714. if (tls_segment == NULL)
  4715. {
  4716. gold_assert(parameters->errors()->error_count() > 0
  4717. || issue_undefined_symbol_error(gsym));
  4718. return;
  4719. }
  4720. value -= tls_segment->memsz();
  4721. }
  4722. Relocate_functions<size, false>::rela64(view, value, addend);
  4723. break;
  4724. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  4725. if (gsym != NULL
  4726. && gsym->is_undefined()
  4727. && parameters->options().output_is_executable())
  4728. {
  4729. Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
  4730. NULL, rela,
  4731. r_type, value, view,
  4732. view_size);
  4733. break;
  4734. }
  4735. else if (optimized_type == tls::TLSOPT_TO_LE)
  4736. {
  4737. if (tls_segment == NULL)
  4738. {
  4739. gold_assert(parameters->errors()->error_count() > 0
  4740. || issue_undefined_symbol_error(gsym));
  4741. return;
  4742. }
  4743. Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
  4744. tls_segment, rela,
  4745. r_type, value, view,
  4746. view_size);
  4747. break;
  4748. }
  4749. else if (optimized_type == tls::TLSOPT_NONE)
  4750. {
  4751. // Relocate the field with the offset of the GOT entry for
  4752. // the tp-relative offset of the symbol.
  4753. unsigned int got_offset;
  4754. if (gsym != NULL)
  4755. {
  4756. gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
  4757. got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
  4758. - target->got_size());
  4759. }
  4760. else
  4761. {
  4762. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  4763. gold_assert(object->local_has_got_offset(r_sym,
  4764. GOT_TYPE_TLS_OFFSET));
  4765. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
  4766. - target->got_size());
  4767. }
  4768. value = target->got_plt_section()->address() + got_offset;
  4769. Relocate_functions<size, false>::pcrela32(view, value, addend,
  4770. address);
  4771. break;
  4772. }
  4773. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  4774. _("unsupported reloc type %u"),
  4775. r_type);
  4776. break;
  4777. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  4778. if (tls_segment == NULL)
  4779. {
  4780. gold_assert(parameters->errors()->error_count() > 0
  4781. || issue_undefined_symbol_error(gsym));
  4782. return;
  4783. }
  4784. value -= tls_segment->memsz();
  4785. Relocate_functions<size, false>::rela32(view, value, addend);
  4786. break;
  4787. }
  4788. }
  4789. // Do a relocation in which we convert a TLS General-Dynamic to an
  4790. // Initial-Exec.
  4791. template<int size>
  4792. inline void
  4793. Target_x86_64<size>::Relocate::tls_gd_to_ie(
  4794. const Relocate_info<size, false>* relinfo,
  4795. size_t relnum,
  4796. const elfcpp::Rela<size, false>& rela,
  4797. unsigned int,
  4798. typename elfcpp::Elf_types<size>::Elf_Addr value,
  4799. unsigned char* view,
  4800. typename elfcpp::Elf_types<size>::Elf_Addr address,
  4801. section_size_type view_size)
  4802. {
  4803. // For SIZE == 64:
  4804. // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
  4805. // .word 0x6666; rex64; call __tls_get_addr@PLT
  4806. // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
  4807. // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
  4808. // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
  4809. // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
  4810. // For SIZE == 32:
  4811. // leaq foo@tlsgd(%rip),%rdi;
  4812. // .word 0x6666; rex64; call __tls_get_addr@PLT
  4813. // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
  4814. // leaq foo@tlsgd(%rip),%rdi;
  4815. // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
  4816. // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
  4817. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
  4818. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4819. (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
  4820. || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
  4821. if (size == 64)
  4822. {
  4823. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
  4824. -4);
  4825. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4826. (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
  4827. memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
  4828. 16);
  4829. }
  4830. else
  4831. {
  4832. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
  4833. -3);
  4834. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4835. (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
  4836. memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
  4837. 15);
  4838. }
  4839. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  4840. Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
  4841. address);
  4842. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  4843. // We can skip it.
  4844. this->skip_call_tls_get_addr_ = true;
  4845. }
  4846. // Do a relocation in which we convert a TLS General-Dynamic to a
  4847. // Local-Exec.
  4848. template<int size>
  4849. inline void
  4850. Target_x86_64<size>::Relocate::tls_gd_to_le(
  4851. const Relocate_info<size, false>* relinfo,
  4852. size_t relnum,
  4853. Output_segment* tls_segment,
  4854. const elfcpp::Rela<size, false>& rela,
  4855. unsigned int,
  4856. typename elfcpp::Elf_types<size>::Elf_Addr value,
  4857. unsigned char* view,
  4858. section_size_type view_size)
  4859. {
  4860. // For SIZE == 64:
  4861. // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
  4862. // .word 0x6666; rex64; call __tls_get_addr@PLT
  4863. // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
  4864. // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
  4865. // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
  4866. // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
  4867. // For SIZE == 32:
  4868. // leaq foo@tlsgd(%rip),%rdi;
  4869. // .word 0x6666; rex64; call __tls_get_addr@PLT
  4870. // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
  4871. // leaq foo@tlsgd(%rip),%rdi;
  4872. // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
  4873. // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
  4874. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
  4875. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4876. (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
  4877. || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
  4878. if (size == 64)
  4879. {
  4880. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
  4881. -4);
  4882. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4883. (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
  4884. memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
  4885. 16);
  4886. }
  4887. else
  4888. {
  4889. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
  4890. -3);
  4891. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4892. (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
  4893. memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
  4894. 15);
  4895. }
  4896. value -= tls_segment->memsz();
  4897. Relocate_functions<size, false>::rela32(view + 8, value, 0);
  4898. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  4899. // We can skip it.
  4900. this->skip_call_tls_get_addr_ = true;
  4901. }
  4902. // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
  4903. template<int size>
  4904. inline void
  4905. Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
  4906. const Relocate_info<size, false>* relinfo,
  4907. size_t relnum,
  4908. const elfcpp::Rela<size, false>& rela,
  4909. unsigned int r_type,
  4910. typename elfcpp::Elf_types<size>::Elf_Addr value,
  4911. unsigned char* view,
  4912. typename elfcpp::Elf_types<size>::Elf_Addr address,
  4913. section_size_type view_size)
  4914. {
  4915. if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
  4916. {
  4917. // LP64: leaq foo@tlsdesc(%rip), %rax
  4918. // ==> movq foo@gottpoff(%rip), %rax
  4919. // X32: rex leal foo@tlsdesc(%rip), %eax
  4920. // ==> rex movl foo@gottpoff(%rip), %eax
  4921. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
  4922. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
  4923. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4924. (((view[-3] & 0xfb) == 0x48
  4925. || (size == 32 && (view[-3] & 0xfb) == 0x40))
  4926. && view[-2] == 0x8d
  4927. && (view[-1] & 0xc7) == 0x05));
  4928. view[-2] = 0x8b;
  4929. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  4930. Relocate_functions<size, false>::pcrela32(view, value, addend, address);
  4931. }
  4932. else
  4933. {
  4934. // LP64: call *foo@tlscall(%rax)
  4935. // ==> xchg %ax, %ax
  4936. // X32: call *foo@tlscall(%eax)
  4937. // ==> nopl (%rax)
  4938. gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
  4939. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
  4940. int prefix = 0;
  4941. if (size == 32 && view[0] == 0x67)
  4942. {
  4943. tls::check_range(relinfo, relnum, rela.get_r_offset(),
  4944. view_size, 3);
  4945. prefix = 1;
  4946. }
  4947. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4948. view[prefix] == 0xff && view[prefix + 1] == 0x10);
  4949. if (prefix)
  4950. {
  4951. view[0] = 0x0f;
  4952. view[1] = 0x1f;
  4953. view[2] = 0x00;
  4954. }
  4955. else
  4956. {
  4957. view[0] = 0x66;
  4958. view[1] = 0x90;
  4959. }
  4960. }
  4961. }
  4962. // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
  4963. template<int size>
  4964. inline void
  4965. Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
  4966. const Relocate_info<size, false>* relinfo,
  4967. size_t relnum,
  4968. Output_segment* tls_segment,
  4969. const elfcpp::Rela<size, false>& rela,
  4970. unsigned int r_type,
  4971. typename elfcpp::Elf_types<size>::Elf_Addr value,
  4972. unsigned char* view,
  4973. section_size_type view_size)
  4974. {
  4975. if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
  4976. {
  4977. // LP64: leaq foo@tlsdesc(%rip), %rax
  4978. // ==> movq foo@tpoff, %rax
  4979. // X32: rex leal foo@tlsdesc(%rip), %eax
  4980. // ==> rex movl foo@tpoff, %eax
  4981. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
  4982. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
  4983. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  4984. (((view[-3] & 0xfb) == 0x48
  4985. || (size == 32 && (view[-3] & 0xfb) == 0x40))
  4986. && view[-2] == 0x8d
  4987. && (view[-1] & 0xc7) == 0x05));
  4988. view[-3] = (view[-3] & 0x48) | ((view[-3] >> 2) & 1);
  4989. view[-2] = 0xc7;
  4990. view[-1] = 0xc0 | ((view[-1] >> 3) & 7);
  4991. value -= tls_segment->memsz();
  4992. Relocate_functions<size, false>::rela32(view, value, 0);
  4993. }
  4994. else
  4995. {
  4996. // LP64: call *foo@tlscall(%rax)
  4997. // ==> xchg %ax, %ax
  4998. // X32: call *foo@tlscall(%eax)
  4999. // ==> nopl (%rax)
  5000. gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
  5001. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
  5002. int prefix = 0;
  5003. if (size == 32 && view[0] == 0x67)
  5004. {
  5005. tls::check_range(relinfo, relnum, rela.get_r_offset(),
  5006. view_size, 3);
  5007. prefix = 1;
  5008. }
  5009. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  5010. view[prefix] == 0xff && view[prefix + 1] == 0x10);
  5011. if (prefix)
  5012. {
  5013. view[0] = 0x0f;
  5014. view[1] = 0x1f;
  5015. view[2] = 0x00;
  5016. }
  5017. else
  5018. {
  5019. view[0] = 0x66;
  5020. view[1] = 0x90;
  5021. }
  5022. }
  5023. }
  5024. template<int size>
  5025. inline void
  5026. Target_x86_64<size>::Relocate::tls_ld_to_le(
  5027. const Relocate_info<size, false>* relinfo,
  5028. size_t relnum,
  5029. Output_segment*,
  5030. const elfcpp::Rela<size, false>& rela,
  5031. unsigned int,
  5032. typename elfcpp::Elf_types<size>::Elf_Addr,
  5033. unsigned char* view,
  5034. section_size_type view_size)
  5035. {
  5036. // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
  5037. // For SIZE == 64:
  5038. // ... leq foo@dtpoff(%rax),%reg
  5039. // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
  5040. // For SIZE == 32:
  5041. // ... leq foo@dtpoff(%rax),%reg
  5042. // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
  5043. // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
  5044. // For SIZE == 64:
  5045. // ... leq foo@dtpoff(%rax),%reg
  5046. // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
  5047. // For SIZE == 32:
  5048. // ... leq foo@dtpoff(%rax),%reg
  5049. // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
  5050. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
  5051. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
  5052. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  5053. view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
  5054. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  5055. view[4] == 0xe8 || view[4] == 0xff);
  5056. if (view[4] == 0xe8)
  5057. {
  5058. if (size == 64)
  5059. memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
  5060. else
  5061. memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
  5062. }
  5063. else
  5064. {
  5065. if (size == 64)
  5066. memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
  5067. 13);
  5068. else
  5069. memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
  5070. 13);
  5071. }
  5072. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  5073. // We can skip it.
  5074. this->skip_call_tls_get_addr_ = true;
  5075. }
  5076. // Do a relocation in which we convert a TLS Initial-Exec to a
  5077. // Local-Exec.
  5078. template<int size>
  5079. inline void
  5080. Target_x86_64<size>::Relocate::tls_ie_to_le(
  5081. const Relocate_info<size, false>* relinfo,
  5082. size_t relnum,
  5083. Output_segment* tls_segment,
  5084. const elfcpp::Rela<size, false>& rela,
  5085. unsigned int,
  5086. typename elfcpp::Elf_types<size>::Elf_Addr value,
  5087. unsigned char* view,
  5088. section_size_type view_size)
  5089. {
  5090. // We need to examine the opcodes to figure out which instruction we
  5091. // are looking at.
  5092. // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
  5093. // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
  5094. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
  5095. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
  5096. unsigned char op1 = view[-3];
  5097. unsigned char op2 = view[-2];
  5098. unsigned char op3 = view[-1];
  5099. unsigned char reg = op3 >> 3;
  5100. if (op2 == 0x8b)
  5101. {
  5102. // movq
  5103. if (op1 == 0x4c)
  5104. view[-3] = 0x49;
  5105. else if (size == 32 && op1 == 0x44)
  5106. view[-3] = 0x41;
  5107. view[-2] = 0xc7;
  5108. view[-1] = 0xc0 | reg;
  5109. }
  5110. else if (reg == 4)
  5111. {
  5112. // Special handling for %rsp.
  5113. if (op1 == 0x4c)
  5114. view[-3] = 0x49;
  5115. else if (size == 32 && op1 == 0x44)
  5116. view[-3] = 0x41;
  5117. view[-2] = 0x81;
  5118. view[-1] = 0xc0 | reg;
  5119. }
  5120. else
  5121. {
  5122. // addq
  5123. if (op1 == 0x4c)
  5124. view[-3] = 0x4d;
  5125. else if (size == 32 && op1 == 0x44)
  5126. view[-3] = 0x45;
  5127. view[-2] = 0x8d;
  5128. view[-1] = 0x80 | reg | (reg << 3);
  5129. }
  5130. if (tls_segment != NULL)
  5131. value -= tls_segment->memsz();
  5132. Relocate_functions<size, false>::rela32(view, value, 0);
  5133. }
  5134. // Relocate section data.
  5135. template<int size>
  5136. void
  5137. Target_x86_64<size>::relocate_section(
  5138. const Relocate_info<size, false>* relinfo,
  5139. unsigned int sh_type,
  5140. const unsigned char* prelocs,
  5141. size_t reloc_count,
  5142. Output_section* output_section,
  5143. bool needs_special_offset_handling,
  5144. unsigned char* view,
  5145. typename elfcpp::Elf_types<size>::Elf_Addr address,
  5146. section_size_type view_size,
  5147. const Reloc_symbol_changes* reloc_symbol_changes)
  5148. {
  5149. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
  5150. Classify_reloc;
  5151. gold_assert(sh_type == elfcpp::SHT_RELA);
  5152. gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
  5153. gold::Default_comdat_behavior, Classify_reloc>(
  5154. relinfo,
  5155. this,
  5156. prelocs,
  5157. reloc_count,
  5158. output_section,
  5159. needs_special_offset_handling,
  5160. view,
  5161. address,
  5162. view_size,
  5163. reloc_symbol_changes);
  5164. }
  5165. // Apply an incremental relocation. Incremental relocations always refer
  5166. // to global symbols.
  5167. template<int size>
  5168. void
  5169. Target_x86_64<size>::apply_relocation(
  5170. const Relocate_info<size, false>* relinfo,
  5171. typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
  5172. unsigned int r_type,
  5173. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
  5174. const Symbol* gsym,
  5175. unsigned char* view,
  5176. typename elfcpp::Elf_types<size>::Elf_Addr address,
  5177. section_size_type view_size)
  5178. {
  5179. gold::apply_relocation<size, false, Target_x86_64<size>,
  5180. typename Target_x86_64<size>::Relocate>(
  5181. relinfo,
  5182. this,
  5183. r_offset,
  5184. r_type,
  5185. r_addend,
  5186. gsym,
  5187. view,
  5188. address,
  5189. view_size);
  5190. }
  5191. // Scan the relocs during a relocatable link.
  5192. template<int size>
  5193. void
  5194. Target_x86_64<size>::scan_relocatable_relocs(
  5195. Symbol_table* symtab,
  5196. Layout* layout,
  5197. Sized_relobj_file<size, false>* object,
  5198. unsigned int data_shndx,
  5199. unsigned int sh_type,
  5200. const unsigned char* prelocs,
  5201. size_t reloc_count,
  5202. Output_section* output_section,
  5203. bool needs_special_offset_handling,
  5204. size_t local_symbol_count,
  5205. const unsigned char* plocal_symbols,
  5206. Relocatable_relocs* rr)
  5207. {
  5208. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
  5209. Classify_reloc;
  5210. typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
  5211. Scan_relocatable_relocs;
  5212. gold_assert(sh_type == elfcpp::SHT_RELA);
  5213. gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
  5214. symtab,
  5215. layout,
  5216. object,
  5217. data_shndx,
  5218. prelocs,
  5219. reloc_count,
  5220. output_section,
  5221. needs_special_offset_handling,
  5222. local_symbol_count,
  5223. plocal_symbols,
  5224. rr);
  5225. }
  5226. // Scan the relocs for --emit-relocs.
  5227. template<int size>
  5228. void
  5229. Target_x86_64<size>::emit_relocs_scan(
  5230. Symbol_table* symtab,
  5231. Layout* layout,
  5232. Sized_relobj_file<size, false>* object,
  5233. unsigned int data_shndx,
  5234. unsigned int sh_type,
  5235. const unsigned char* prelocs,
  5236. size_t reloc_count,
  5237. Output_section* output_section,
  5238. bool needs_special_offset_handling,
  5239. size_t local_symbol_count,
  5240. const unsigned char* plocal_syms,
  5241. Relocatable_relocs* rr)
  5242. {
  5243. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
  5244. Classify_reloc;
  5245. typedef gold::Default_emit_relocs_strategy<Classify_reloc>
  5246. Emit_relocs_strategy;
  5247. gold_assert(sh_type == elfcpp::SHT_RELA);
  5248. gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
  5249. symtab,
  5250. layout,
  5251. object,
  5252. data_shndx,
  5253. prelocs,
  5254. reloc_count,
  5255. output_section,
  5256. needs_special_offset_handling,
  5257. local_symbol_count,
  5258. plocal_syms,
  5259. rr);
  5260. }
  5261. // Relocate a section during a relocatable link.
  5262. template<int size>
  5263. void
  5264. Target_x86_64<size>::relocate_relocs(
  5265. const Relocate_info<size, false>* relinfo,
  5266. unsigned int sh_type,
  5267. const unsigned char* prelocs,
  5268. size_t reloc_count,
  5269. Output_section* output_section,
  5270. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  5271. unsigned char* view,
  5272. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  5273. section_size_type view_size,
  5274. unsigned char* reloc_view,
  5275. section_size_type reloc_view_size)
  5276. {
  5277. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
  5278. Classify_reloc;
  5279. gold_assert(sh_type == elfcpp::SHT_RELA);
  5280. gold::relocate_relocs<size, false, Classify_reloc>(
  5281. relinfo,
  5282. prelocs,
  5283. reloc_count,
  5284. output_section,
  5285. offset_in_output_section,
  5286. view,
  5287. view_address,
  5288. view_size,
  5289. reloc_view,
  5290. reloc_view_size);
  5291. }
  5292. // Return the value to use for a dynamic which requires special
  5293. // treatment. This is how we support equality comparisons of function
  5294. // pointers across shared library boundaries, as described in the
  5295. // processor specific ABI supplement.
  5296. template<int size>
  5297. uint64_t
  5298. Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
  5299. {
  5300. gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
  5301. return this->plt_address_for_global(gsym);
  5302. }
  5303. // Return a string used to fill a code section with nops to take up
  5304. // the specified length.
  5305. template<int size>
  5306. std::string
  5307. Target_x86_64<size>::do_code_fill(section_size_type length) const
  5308. {
  5309. if (length >= 16)
  5310. {
  5311. // Build a jmpq instruction to skip over the bytes.
  5312. unsigned char jmp[5];
  5313. jmp[0] = 0xe9;
  5314. elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
  5315. return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
  5316. + std::string(length - 5, static_cast<char>(0x90)));
  5317. }
  5318. // Nop sequences of various lengths.
  5319. const char nop1[1] = { '\x90' }; // nop
  5320. const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
  5321. const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
  5322. const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
  5323. '\x00'};
  5324. const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
  5325. '\x00', '\x00' };
  5326. const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
  5327. '\x44', '\x00', '\x00' };
  5328. const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
  5329. '\x00', '\x00', '\x00',
  5330. '\x00' };
  5331. const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
  5332. '\x00', '\x00', '\x00',
  5333. '\x00', '\x00' };
  5334. const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
  5335. '\x84', '\x00', '\x00',
  5336. '\x00', '\x00', '\x00' };
  5337. const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
  5338. '\x1f', '\x84', '\x00',
  5339. '\x00', '\x00', '\x00',
  5340. '\x00' };
  5341. const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
  5342. '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
  5343. '\x00', '\x00', '\x00',
  5344. '\x00', '\x00' };
  5345. const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
  5346. '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
  5347. '\x84', '\x00', '\x00',
  5348. '\x00', '\x00', '\x00' };
  5349. const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
  5350. '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
  5351. '\x1f', '\x84', '\x00',
  5352. '\x00', '\x00', '\x00',
  5353. '\x00' };
  5354. const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
  5355. '\x66', '\x66', '\x2e', // data16
  5356. '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
  5357. '\x00', '\x00', '\x00',
  5358. '\x00', '\x00' };
  5359. const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
  5360. '\x66', '\x66', '\x66', // data16; data16
  5361. '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
  5362. '\x84', '\x00', '\x00',
  5363. '\x00', '\x00', '\x00' };
  5364. const char* nops[16] = {
  5365. NULL,
  5366. nop1, nop2, nop3, nop4, nop5, nop6, nop7,
  5367. nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
  5368. };
  5369. return std::string(nops[length], length);
  5370. }
  5371. // Return the addend to use for a target specific relocation. The
  5372. // only target specific relocation is R_X86_64_TLSDESC for a local
  5373. // symbol. We want to set the addend is the offset of the local
  5374. // symbol in the TLS segment.
  5375. template<int size>
  5376. uint64_t
  5377. Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
  5378. uint64_t) const
  5379. {
  5380. gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
  5381. uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
  5382. gold_assert(intarg < this->tlsdesc_reloc_info_.size());
  5383. const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
  5384. const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
  5385. gold_assert(psymval->is_tls_symbol());
  5386. // The value of a TLS symbol is the offset in the TLS segment.
  5387. return psymval->value(ti.object, 0);
  5388. }
  5389. // Return the value to use for the base of a DW_EH_PE_datarel offset
  5390. // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
  5391. // assembler can not write out the difference between two labels in
  5392. // different sections, so instead of using a pc-relative value they
  5393. // use an offset from the GOT.
  5394. template<int size>
  5395. uint64_t
  5396. Target_x86_64<size>::do_ehframe_datarel_base() const
  5397. {
  5398. gold_assert(this->global_offset_table_ != NULL);
  5399. Symbol* sym = this->global_offset_table_;
  5400. Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
  5401. return ssym->value();
  5402. }
  5403. // FNOFFSET in section SHNDX in OBJECT is the start of a function
  5404. // compiled with -fsplit-stack. The function calls non-split-stack
  5405. // code. We have to change the function so that it always ensures
  5406. // that it has enough stack space to run some random function.
  5407. static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
  5408. static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
  5409. static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
  5410. static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
  5411. static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
  5412. static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
  5413. template<int size>
  5414. void
  5415. Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
  5416. section_offset_type fnoffset,
  5417. section_size_type fnsize,
  5418. const unsigned char*,
  5419. size_t,
  5420. unsigned char* view,
  5421. section_size_type view_size,
  5422. std::string* from,
  5423. std::string* to) const
  5424. {
  5425. const char* const cmp_insn = reinterpret_cast<const char*>
  5426. (size == 32 ? cmp_insn_32 : cmp_insn_64);
  5427. const char* const lea_r10_insn = reinterpret_cast<const char*>
  5428. (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
  5429. const char* const lea_r11_insn = reinterpret_cast<const char*>
  5430. (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
  5431. const size_t cmp_insn_len =
  5432. (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
  5433. const size_t lea_r10_insn_len =
  5434. (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
  5435. const size_t lea_r11_insn_len =
  5436. (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
  5437. const size_t nop_len = (size == 32 ? 7 : 8);
  5438. // The function starts with a comparison of the stack pointer and a
  5439. // field in the TCB. This is followed by a jump.
  5440. // cmp %fs:NN,%rsp
  5441. if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
  5442. && fnsize > nop_len + 1)
  5443. {
  5444. // We will call __morestack if the carry flag is set after this
  5445. // comparison. We turn the comparison into an stc instruction
  5446. // and some nops.
  5447. view[fnoffset] = '\xf9';
  5448. this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
  5449. }
  5450. // lea NN(%rsp),%r10
  5451. // lea NN(%rsp),%r11
  5452. else if ((this->match_view(view, view_size, fnoffset,
  5453. lea_r10_insn, lea_r10_insn_len)
  5454. || this->match_view(view, view_size, fnoffset,
  5455. lea_r11_insn, lea_r11_insn_len))
  5456. && fnsize > 8)
  5457. {
  5458. // This is loading an offset from the stack pointer for a
  5459. // comparison. The offset is negative, so we decrease the
  5460. // offset by the amount of space we need for the stack. This
  5461. // means we will avoid calling __morestack if there happens to
  5462. // be plenty of space on the stack already.
  5463. unsigned char* pval = view + fnoffset + 4;
  5464. uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
  5465. val -= parameters->options().split_stack_adjust_size();
  5466. elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
  5467. }
  5468. else
  5469. {
  5470. if (!object->has_no_split_stack())
  5471. object->error(_("failed to match split-stack sequence at "
  5472. "section %u offset %0zx"),
  5473. shndx, static_cast<size_t>(fnoffset));
  5474. return;
  5475. }
  5476. // We have to change the function so that it calls
  5477. // __morestack_non_split instead of __morestack. The former will
  5478. // allocate additional stack space.
  5479. *from = "__morestack";
  5480. *to = "__morestack_non_split";
  5481. }
  5482. // The selector for x86_64 object files. Note this is never instantiated
  5483. // directly. It's only used in Target_selector_x86_64_nacl, below.
  5484. template<int size>
  5485. class Target_selector_x86_64 : public Target_selector_freebsd
  5486. {
  5487. public:
  5488. Target_selector_x86_64()
  5489. : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
  5490. (size == 64
  5491. ? "elf64-x86-64" : "elf32-x86-64"),
  5492. (size == 64
  5493. ? "elf64-x86-64-freebsd"
  5494. : "elf32-x86-64-freebsd"),
  5495. (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
  5496. { }
  5497. Target*
  5498. do_instantiate_target()
  5499. { return new Target_x86_64<size>(); }
  5500. };
  5501. // NaCl variant. It uses different PLT contents.
  5502. template<int size>
  5503. class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
  5504. {
  5505. public:
  5506. Output_data_plt_x86_64_nacl(Layout* layout,
  5507. Output_data_got<64, false>* got,
  5508. Output_data_got_plt_x86_64* got_plt,
  5509. Output_data_space* got_irelative)
  5510. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  5511. got, got_plt, got_irelative)
  5512. { }
  5513. Output_data_plt_x86_64_nacl(Layout* layout,
  5514. Output_data_got<64, false>* got,
  5515. Output_data_got_plt_x86_64* got_plt,
  5516. Output_data_space* got_irelative,
  5517. unsigned int plt_count)
  5518. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  5519. got, got_plt, got_irelative,
  5520. plt_count)
  5521. { }
  5522. protected:
  5523. virtual unsigned int
  5524. do_get_plt_entry_size() const
  5525. { return plt_entry_size; }
  5526. virtual void
  5527. do_add_eh_frame(Layout* layout)
  5528. {
  5529. layout->add_eh_frame_for_plt(this,
  5530. this->plt_eh_frame_cie,
  5531. this->plt_eh_frame_cie_size,
  5532. plt_eh_frame_fde,
  5533. plt_eh_frame_fde_size);
  5534. }
  5535. virtual void
  5536. do_fill_first_plt_entry(unsigned char* pov,
  5537. typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
  5538. typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
  5539. virtual unsigned int
  5540. do_fill_plt_entry(unsigned char* pov,
  5541. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  5542. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  5543. unsigned int got_offset,
  5544. unsigned int plt_offset,
  5545. unsigned int plt_index);
  5546. virtual void
  5547. do_fill_tlsdesc_entry(unsigned char* pov,
  5548. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  5549. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  5550. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  5551. unsigned int tlsdesc_got_offset,
  5552. unsigned int plt_offset);
  5553. private:
  5554. // The size of an entry in the PLT.
  5555. static const int plt_entry_size = 64;
  5556. // The first entry in the PLT.
  5557. static const unsigned char first_plt_entry[plt_entry_size];
  5558. // Other entries in the PLT for an executable.
  5559. static const unsigned char plt_entry[plt_entry_size];
  5560. // The reserved TLSDESC entry in the PLT for an executable.
  5561. static const unsigned char tlsdesc_plt_entry[plt_entry_size];
  5562. // The .eh_frame unwind information for the PLT.
  5563. static const int plt_eh_frame_fde_size = 32;
  5564. static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
  5565. };
  5566. template<int size>
  5567. class Target_x86_64_nacl : public Target_x86_64<size>
  5568. {
  5569. public:
  5570. Target_x86_64_nacl()
  5571. : Target_x86_64<size>(&x86_64_nacl_info)
  5572. { }
  5573. virtual Output_data_plt_x86_64<size>*
  5574. do_make_data_plt(Layout* layout,
  5575. Output_data_got<64, false>* got,
  5576. Output_data_got_plt_x86_64* got_plt,
  5577. Output_data_space* got_irelative)
  5578. {
  5579. return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
  5580. got_irelative);
  5581. }
  5582. virtual Output_data_plt_x86_64<size>*
  5583. do_make_data_plt(Layout* layout,
  5584. Output_data_got<64, false>* got,
  5585. Output_data_got_plt_x86_64* got_plt,
  5586. Output_data_space* got_irelative,
  5587. unsigned int plt_count)
  5588. {
  5589. return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
  5590. got_irelative,
  5591. plt_count);
  5592. }
  5593. virtual std::string
  5594. do_code_fill(section_size_type length) const;
  5595. private:
  5596. static const Target::Target_info x86_64_nacl_info;
  5597. };
  5598. template<>
  5599. const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
  5600. {
  5601. 64, // size
  5602. false, // is_big_endian
  5603. elfcpp::EM_X86_64, // machine_code
  5604. false, // has_make_symbol
  5605. false, // has_resolve
  5606. true, // has_code_fill
  5607. true, // is_default_stack_executable
  5608. true, // can_icf_inline_merge_sections
  5609. '\0', // wrap_char
  5610. "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
  5611. 0x20000, // default_text_segment_address
  5612. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  5613. 0x10000, // common_pagesize (overridable by -z common-page-size)
  5614. true, // isolate_execinstr
  5615. 0x10000000, // rosegment_gap
  5616. elfcpp::SHN_UNDEF, // small_common_shndx
  5617. elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
  5618. 0, // small_common_section_flags
  5619. elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
  5620. NULL, // attributes_section
  5621. NULL, // attributes_vendor
  5622. "_start", // entry_symbol_name
  5623. 32, // hash_entry_size
  5624. elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
  5625. };
  5626. template<>
  5627. const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
  5628. {
  5629. 32, // size
  5630. false, // is_big_endian
  5631. elfcpp::EM_X86_64, // machine_code
  5632. false, // has_make_symbol
  5633. false, // has_resolve
  5634. true, // has_code_fill
  5635. true, // is_default_stack_executable
  5636. true, // can_icf_inline_merge_sections
  5637. '\0', // wrap_char
  5638. "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
  5639. 0x20000, // default_text_segment_address
  5640. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  5641. 0x10000, // common_pagesize (overridable by -z common-page-size)
  5642. true, // isolate_execinstr
  5643. 0x10000000, // rosegment_gap
  5644. elfcpp::SHN_UNDEF, // small_common_shndx
  5645. elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
  5646. 0, // small_common_section_flags
  5647. elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
  5648. NULL, // attributes_section
  5649. NULL, // attributes_vendor
  5650. "_start", // entry_symbol_name
  5651. 32, // hash_entry_size
  5652. elfcpp::SHT_X86_64_UNWIND, // unwind_section_type
  5653. };
  5654. #define NACLMASK 0xe0 // 32-byte alignment mask.
  5655. // The first entry in the PLT.
  5656. template<int size>
  5657. const unsigned char
  5658. Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
  5659. {
  5660. 0xff, 0x35, // pushq contents of memory address
  5661. 0, 0, 0, 0, // replaced with address of .got + 8
  5662. 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
  5663. 0, 0, 0, 0, // replaced with address of .got + 16
  5664. 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
  5665. 0x4d, 0x01, 0xfb, // add %r15, %r11
  5666. 0x41, 0xff, 0xe3, // jmpq *%r11
  5667. // 9-byte nop sequence to pad out to the next 32-byte boundary.
  5668. 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
  5669. // 32 bytes of nop to pad out to the standard size
  5670. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  5671. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  5672. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  5673. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  5674. 0x66, // excess data32 prefix
  5675. 0x90 // nop
  5676. };
  5677. template<int size>
  5678. void
  5679. Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
  5680. unsigned char* pov,
  5681. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  5682. typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
  5683. {
  5684. memcpy(pov, first_plt_entry, plt_entry_size);
  5685. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  5686. (got_address + 8
  5687. - (plt_address + 2 + 4)));
  5688. elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
  5689. (got_address + 16
  5690. - (plt_address + 9 + 4)));
  5691. }
  5692. // Subsequent entries in the PLT.
  5693. template<int size>
  5694. const unsigned char
  5695. Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
  5696. {
  5697. 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
  5698. 0, 0, 0, 0, // replaced with address of symbol in .got
  5699. 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
  5700. 0x4d, 0x01, 0xfb, // add %r15, %r11
  5701. 0x41, 0xff, 0xe3, // jmpq *%r11
  5702. // 15-byte nop sequence to pad out to the next 32-byte boundary.
  5703. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  5704. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  5705. // Lazy GOT entries point here (32-byte aligned).
  5706. 0x68, // pushq immediate
  5707. 0, 0, 0, 0, // replaced with index into relocation table
  5708. 0xe9, // jmp relative
  5709. 0, 0, 0, 0, // replaced with offset to start of .plt0
  5710. // 22 bytes of nop to pad out to the standard size.
  5711. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  5712. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  5713. 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
  5714. };
  5715. template<int size>
  5716. unsigned int
  5717. Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
  5718. unsigned char* pov,
  5719. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  5720. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  5721. unsigned int got_offset,
  5722. unsigned int plt_offset,
  5723. unsigned int plt_index)
  5724. {
  5725. memcpy(pov, plt_entry, plt_entry_size);
  5726. elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
  5727. (got_address + got_offset
  5728. - (plt_address + plt_offset
  5729. + 3 + 4)));
  5730. elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
  5731. elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
  5732. - (plt_offset + 38 + 4));
  5733. return 32;
  5734. }
  5735. // The reserved TLSDESC entry in the PLT.
  5736. template<int size>
  5737. const unsigned char
  5738. Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
  5739. {
  5740. 0xff, 0x35, // pushq x(%rip)
  5741. 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
  5742. 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
  5743. 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
  5744. 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
  5745. 0x4d, 0x01, 0xfb, // add %r15, %r11
  5746. 0x41, 0xff, 0xe3, // jmpq *%r11
  5747. // 41 bytes of nop to pad out to the standard size.
  5748. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  5749. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  5750. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  5751. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  5752. 0x66, 0x66, // excess data32 prefixes
  5753. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  5754. };
  5755. template<int size>
  5756. void
  5757. Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
  5758. unsigned char* pov,
  5759. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  5760. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  5761. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  5762. unsigned int tlsdesc_got_offset,
  5763. unsigned int plt_offset)
  5764. {
  5765. memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
  5766. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  5767. (got_address + 8
  5768. - (plt_address + plt_offset
  5769. + 2 + 4)));
  5770. elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
  5771. (got_base
  5772. + tlsdesc_got_offset
  5773. - (plt_address + plt_offset
  5774. + 9 + 4)));
  5775. }
  5776. // The .eh_frame unwind information for the PLT.
  5777. template<int size>
  5778. const unsigned char
  5779. Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
  5780. {
  5781. 0, 0, 0, 0, // Replaced with offset to .plt.
  5782. 0, 0, 0, 0, // Replaced with size of .plt.
  5783. 0, // Augmentation size.
  5784. elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
  5785. elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
  5786. elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
  5787. elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
  5788. elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
  5789. 13, // Block length.
  5790. elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
  5791. elfcpp::DW_OP_breg16, 0, // Push %rip.
  5792. elfcpp::DW_OP_const1u, 63, // Push 0x3f.
  5793. elfcpp::DW_OP_and, // & (%rip & 0x3f).
  5794. elfcpp::DW_OP_const1u, 37, // Push 0x25.
  5795. elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
  5796. elfcpp::DW_OP_lit3, // Push 3.
  5797. elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
  5798. elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
  5799. elfcpp::DW_CFA_nop, // Align to 32 bytes.
  5800. elfcpp::DW_CFA_nop
  5801. };
  5802. // Return a string used to fill a code section with nops.
  5803. // For NaCl, long NOPs are only valid if they do not cross
  5804. // bundle alignment boundaries, so keep it simple with one-byte NOPs.
  5805. template<int size>
  5806. std::string
  5807. Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
  5808. {
  5809. return std::string(length, static_cast<char>(0x90));
  5810. }
  5811. // The selector for x86_64-nacl object files.
  5812. template<int size>
  5813. class Target_selector_x86_64_nacl
  5814. : public Target_selector_nacl<Target_selector_x86_64<size>,
  5815. Target_x86_64_nacl<size> >
  5816. {
  5817. public:
  5818. Target_selector_x86_64_nacl()
  5819. : Target_selector_nacl<Target_selector_x86_64<size>,
  5820. Target_x86_64_nacl<size> >("x86-64",
  5821. size == 64
  5822. ? "elf64-x86-64-nacl"
  5823. : "elf32-x86-64-nacl",
  5824. size == 64
  5825. ? "elf_x86_64_nacl"
  5826. : "elf32_x86_64_nacl")
  5827. { }
  5828. };
  5829. Target_selector_x86_64_nacl<64> target_selector_x86_64;
  5830. Target_selector_x86_64_nacl<32> target_selector_x32;
  5831. } // End anonymous namespace.