x86_64.cc 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037
  1. // x86_64.cc -- x86_64 target support for gold.
  2. // Copyright (C) 2006-2015 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. // Return the number of PLT entries.
  149. unsigned int
  150. entry_count() const
  151. { return this->count_ + this->irelative_count_; }
  152. // Return the offset of the first non-reserved PLT entry.
  153. unsigned int
  154. first_plt_entry_offset()
  155. { return this->get_plt_entry_size(); }
  156. // Return the size of a PLT entry.
  157. unsigned int
  158. get_plt_entry_size() const
  159. { return this->do_get_plt_entry_size(); }
  160. // Reserve a slot in the PLT for an existing symbol in an incremental update.
  161. void
  162. reserve_slot(unsigned int plt_index)
  163. {
  164. this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
  165. (plt_index + 2) * this->get_plt_entry_size());
  166. }
  167. // Return the PLT address to use for a global symbol.
  168. uint64_t
  169. address_for_global(const Symbol*);
  170. // Return the PLT address to use for a local symbol.
  171. uint64_t
  172. address_for_local(const Relobj*, unsigned int symndx);
  173. // Add .eh_frame information for the PLT.
  174. void
  175. add_eh_frame(Layout* layout)
  176. { this->do_add_eh_frame(layout); }
  177. protected:
  178. // Fill in the first PLT entry.
  179. void
  180. fill_first_plt_entry(unsigned char* pov,
  181. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  182. typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
  183. { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
  184. // Fill in a normal PLT entry. Returns the offset into the entry that
  185. // should be the initial GOT slot value.
  186. unsigned int
  187. fill_plt_entry(unsigned char* pov,
  188. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  189. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  190. unsigned int got_offset,
  191. unsigned int plt_offset,
  192. unsigned int plt_index)
  193. {
  194. return this->do_fill_plt_entry(pov, got_address, plt_address,
  195. got_offset, plt_offset, plt_index);
  196. }
  197. // Fill in the reserved TLSDESC PLT entry.
  198. void
  199. fill_tlsdesc_entry(unsigned char* pov,
  200. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  201. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  202. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  203. unsigned int tlsdesc_got_offset,
  204. unsigned int plt_offset)
  205. {
  206. this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
  207. tlsdesc_got_offset, plt_offset);
  208. }
  209. virtual unsigned int
  210. do_get_plt_entry_size() const = 0;
  211. virtual void
  212. do_fill_first_plt_entry(unsigned char* pov,
  213. typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
  214. typename elfcpp::Elf_types<size>::Elf_Addr plt_addr)
  215. = 0;
  216. virtual unsigned int
  217. do_fill_plt_entry(unsigned char* pov,
  218. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  219. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  220. unsigned int got_offset,
  221. unsigned int plt_offset,
  222. unsigned int plt_index) = 0;
  223. virtual void
  224. do_fill_tlsdesc_entry(unsigned char* pov,
  225. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  226. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  227. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  228. unsigned int tlsdesc_got_offset,
  229. unsigned int plt_offset) = 0;
  230. virtual void
  231. do_add_eh_frame(Layout* layout) = 0;
  232. void
  233. do_adjust_output_section(Output_section* os);
  234. // Write to a map file.
  235. void
  236. do_print_to_mapfile(Mapfile* mapfile) const
  237. { mapfile->print_output_data(this, _("** PLT")); }
  238. // The CIE of the .eh_frame unwind information for the PLT.
  239. static const int plt_eh_frame_cie_size = 16;
  240. static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
  241. private:
  242. // Set the final size.
  243. void
  244. set_final_data_size();
  245. // Write out the PLT data.
  246. void
  247. do_write(Output_file*);
  248. // The reloc section.
  249. Reloc_section* rel_;
  250. // The TLSDESC relocs, if necessary. These must follow the regular
  251. // PLT relocs.
  252. Reloc_section* tlsdesc_rel_;
  253. // The IRELATIVE relocs, if necessary. These must follow the
  254. // regular PLT relocations and the TLSDESC relocations.
  255. Reloc_section* irelative_rel_;
  256. // The .got section.
  257. Output_data_got<64, false>* got_;
  258. // The .got.plt section.
  259. Output_data_got_plt_x86_64* got_plt_;
  260. // The part of the .got.plt section used for IRELATIVE relocs.
  261. Output_data_space* got_irelative_;
  262. // The number of PLT entries.
  263. unsigned int count_;
  264. // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
  265. // follow the regular PLT entries.
  266. unsigned int irelative_count_;
  267. // Offset of the reserved TLSDESC_GOT entry when needed.
  268. unsigned int tlsdesc_got_offset_;
  269. // List of available regions within the section, for incremental
  270. // update links.
  271. Free_list free_list_;
  272. };
  273. template<int size>
  274. class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size>
  275. {
  276. public:
  277. Output_data_plt_x86_64_standard(Layout* layout,
  278. Output_data_got<64, false>* got,
  279. Output_data_got_plt_x86_64* got_plt,
  280. Output_data_space* got_irelative)
  281. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  282. got, got_plt, got_irelative)
  283. { }
  284. Output_data_plt_x86_64_standard(Layout* layout,
  285. Output_data_got<64, false>* got,
  286. Output_data_got_plt_x86_64* got_plt,
  287. Output_data_space* got_irelative,
  288. unsigned int plt_count)
  289. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  290. got, got_plt, got_irelative,
  291. plt_count)
  292. { }
  293. protected:
  294. virtual unsigned int
  295. do_get_plt_entry_size() const
  296. { return plt_entry_size; }
  297. virtual void
  298. do_add_eh_frame(Layout* layout)
  299. {
  300. layout->add_eh_frame_for_plt(this,
  301. this->plt_eh_frame_cie,
  302. this->plt_eh_frame_cie_size,
  303. plt_eh_frame_fde,
  304. plt_eh_frame_fde_size);
  305. }
  306. virtual void
  307. do_fill_first_plt_entry(unsigned char* pov,
  308. typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
  309. typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
  310. virtual unsigned int
  311. do_fill_plt_entry(unsigned char* pov,
  312. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  313. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  314. unsigned int got_offset,
  315. unsigned int plt_offset,
  316. unsigned int plt_index);
  317. virtual void
  318. do_fill_tlsdesc_entry(unsigned char* pov,
  319. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  320. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  321. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  322. unsigned int tlsdesc_got_offset,
  323. unsigned int plt_offset);
  324. private:
  325. // The size of an entry in the PLT.
  326. static const int plt_entry_size = 16;
  327. // The first entry in the PLT.
  328. // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
  329. // procedure linkage table for both programs and shared objects."
  330. static const unsigned char first_plt_entry[plt_entry_size];
  331. // Other entries in the PLT for an executable.
  332. static const unsigned char plt_entry[plt_entry_size];
  333. // The reserved TLSDESC entry in the PLT for an executable.
  334. static const unsigned char tlsdesc_plt_entry[plt_entry_size];
  335. // The .eh_frame unwind information for the PLT.
  336. static const int plt_eh_frame_fde_size = 32;
  337. static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
  338. };
  339. // The x86_64 target class.
  340. // See the ABI at
  341. // http://www.x86-64.org/documentation/abi.pdf
  342. // TLS info comes from
  343. // http://people.redhat.com/drepper/tls.pdf
  344. // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
  345. template<int size>
  346. class Target_x86_64 : public Sized_target<size, false>
  347. {
  348. public:
  349. // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
  350. // uses only Elf64_Rela relocation entries with explicit addends."
  351. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
  352. Target_x86_64(const Target::Target_info* info = &x86_64_info)
  353. : Sized_target<size, false>(info),
  354. got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
  355. got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
  356. rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY),
  357. got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
  358. tls_base_symbol_defined_(false)
  359. { }
  360. // Hook for a new output section.
  361. void
  362. do_new_output_section(Output_section*) const;
  363. // Scan the relocations to look for symbol adjustments.
  364. void
  365. gc_process_relocs(Symbol_table* symtab,
  366. Layout* layout,
  367. Sized_relobj_file<size, false>* object,
  368. unsigned int data_shndx,
  369. unsigned int sh_type,
  370. const unsigned char* prelocs,
  371. size_t reloc_count,
  372. Output_section* output_section,
  373. bool needs_special_offset_handling,
  374. size_t local_symbol_count,
  375. const unsigned char* plocal_symbols);
  376. // Scan the relocations to look for symbol adjustments.
  377. void
  378. scan_relocs(Symbol_table* symtab,
  379. Layout* layout,
  380. Sized_relobj_file<size, false>* object,
  381. unsigned int data_shndx,
  382. unsigned int sh_type,
  383. const unsigned char* prelocs,
  384. size_t reloc_count,
  385. Output_section* output_section,
  386. bool needs_special_offset_handling,
  387. size_t local_symbol_count,
  388. const unsigned char* plocal_symbols);
  389. // Finalize the sections.
  390. void
  391. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  392. // Return the value to use for a dynamic which requires special
  393. // treatment.
  394. uint64_t
  395. do_dynsym_value(const Symbol*) const;
  396. // Relocate a section.
  397. void
  398. relocate_section(const Relocate_info<size, false>*,
  399. unsigned int sh_type,
  400. const unsigned char* prelocs,
  401. size_t reloc_count,
  402. Output_section* output_section,
  403. bool needs_special_offset_handling,
  404. unsigned char* view,
  405. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  406. section_size_type view_size,
  407. const Reloc_symbol_changes*);
  408. // Scan the relocs during a relocatable link.
  409. void
  410. scan_relocatable_relocs(Symbol_table* symtab,
  411. Layout* layout,
  412. Sized_relobj_file<size, false>* object,
  413. unsigned int data_shndx,
  414. unsigned int sh_type,
  415. const unsigned char* prelocs,
  416. size_t reloc_count,
  417. Output_section* output_section,
  418. bool needs_special_offset_handling,
  419. size_t local_symbol_count,
  420. const unsigned char* plocal_symbols,
  421. Relocatable_relocs*);
  422. // Emit relocations for a section.
  423. void
  424. relocate_relocs(
  425. const Relocate_info<size, false>*,
  426. unsigned int sh_type,
  427. const unsigned char* prelocs,
  428. size_t reloc_count,
  429. Output_section* output_section,
  430. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  431. const Relocatable_relocs*,
  432. unsigned char* view,
  433. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  434. section_size_type view_size,
  435. unsigned char* reloc_view,
  436. section_size_type reloc_view_size);
  437. // Return a string used to fill a code section with nops.
  438. std::string
  439. do_code_fill(section_size_type length) const;
  440. // Return whether SYM is defined by the ABI.
  441. bool
  442. do_is_defined_by_abi(const Symbol* sym) const
  443. { return strcmp(sym->name(), "__tls_get_addr") == 0; }
  444. // Return the symbol index to use for a target specific relocation.
  445. // The only target specific relocation is R_X86_64_TLSDESC for a
  446. // local symbol, which is an absolute reloc.
  447. unsigned int
  448. do_reloc_symbol_index(void*, unsigned int r_type) const
  449. {
  450. gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
  451. return 0;
  452. }
  453. // Return the addend to use for a target specific relocation.
  454. uint64_t
  455. do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
  456. // Return the PLT section.
  457. uint64_t
  458. do_plt_address_for_global(const Symbol* gsym) const
  459. { return this->plt_section()->address_for_global(gsym); }
  460. uint64_t
  461. do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
  462. { return this->plt_section()->address_for_local(relobj, symndx); }
  463. // This function should be defined in targets that can use relocation
  464. // types to determine (implemented in local_reloc_may_be_function_pointer
  465. // and global_reloc_may_be_function_pointer)
  466. // if a function's pointer is taken. ICF uses this in safe mode to only
  467. // fold those functions whose pointer is defintely not taken. For x86_64
  468. // pie binaries, safe ICF cannot be done by looking at relocation types.
  469. bool
  470. do_can_check_for_function_pointers() const
  471. { return !parameters->options().pie(); }
  472. // Return the base for a DW_EH_PE_datarel encoding.
  473. uint64_t
  474. do_ehframe_datarel_base() const;
  475. // Adjust -fsplit-stack code which calls non-split-stack code.
  476. void
  477. do_calls_non_split(Relobj* object, unsigned int shndx,
  478. section_offset_type fnoffset, section_size_type fnsize,
  479. unsigned char* view, section_size_type view_size,
  480. std::string* from, std::string* to) const;
  481. // Return the size of the GOT section.
  482. section_size_type
  483. got_size() const
  484. {
  485. gold_assert(this->got_ != NULL);
  486. return this->got_->data_size();
  487. }
  488. // Return the number of entries in the GOT.
  489. unsigned int
  490. got_entry_count() const
  491. {
  492. if (this->got_ == NULL)
  493. return 0;
  494. return this->got_size() / 8;
  495. }
  496. // Return the number of entries in the PLT.
  497. unsigned int
  498. plt_entry_count() const;
  499. // Return the offset of the first non-reserved PLT entry.
  500. unsigned int
  501. first_plt_entry_offset() const;
  502. // Return the size of each PLT entry.
  503. unsigned int
  504. plt_entry_size() const;
  505. // Create the GOT section for an incremental update.
  506. Output_data_got_base*
  507. init_got_plt_for_update(Symbol_table* symtab,
  508. Layout* layout,
  509. unsigned int got_count,
  510. unsigned int plt_count);
  511. // Reserve a GOT entry for a local symbol, and regenerate any
  512. // necessary dynamic relocations.
  513. void
  514. reserve_local_got_entry(unsigned int got_index,
  515. Sized_relobj<size, false>* obj,
  516. unsigned int r_sym,
  517. unsigned int got_type);
  518. // Reserve a GOT entry for a global symbol, and regenerate any
  519. // necessary dynamic relocations.
  520. void
  521. reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
  522. unsigned int got_type);
  523. // Register an existing PLT entry for a global symbol.
  524. void
  525. register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
  526. Symbol* gsym);
  527. // Force a COPY relocation for a given symbol.
  528. void
  529. emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
  530. // Apply an incremental relocation.
  531. void
  532. apply_relocation(const Relocate_info<size, false>* relinfo,
  533. typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
  534. unsigned int r_type,
  535. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
  536. const Symbol* gsym,
  537. unsigned char* view,
  538. typename elfcpp::Elf_types<size>::Elf_Addr address,
  539. section_size_type view_size);
  540. // Add a new reloc argument, returning the index in the vector.
  541. size_t
  542. add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
  543. {
  544. this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
  545. return this->tlsdesc_reloc_info_.size() - 1;
  546. }
  547. Output_data_plt_x86_64<size>*
  548. make_data_plt(Layout* layout,
  549. Output_data_got<64, false>* got,
  550. Output_data_got_plt_x86_64* got_plt,
  551. Output_data_space* got_irelative)
  552. {
  553. return this->do_make_data_plt(layout, got, got_plt, got_irelative);
  554. }
  555. Output_data_plt_x86_64<size>*
  556. make_data_plt(Layout* layout,
  557. Output_data_got<64, false>* got,
  558. Output_data_got_plt_x86_64* got_plt,
  559. Output_data_space* got_irelative,
  560. unsigned int plt_count)
  561. {
  562. return this->do_make_data_plt(layout, got, got_plt, got_irelative,
  563. plt_count);
  564. }
  565. virtual Output_data_plt_x86_64<size>*
  566. do_make_data_plt(Layout* layout,
  567. Output_data_got<64, false>* got,
  568. Output_data_got_plt_x86_64* got_plt,
  569. Output_data_space* got_irelative)
  570. {
  571. return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
  572. got_irelative);
  573. }
  574. virtual Output_data_plt_x86_64<size>*
  575. do_make_data_plt(Layout* layout,
  576. Output_data_got<64, false>* got,
  577. Output_data_got_plt_x86_64* got_plt,
  578. Output_data_space* got_irelative,
  579. unsigned int plt_count)
  580. {
  581. return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
  582. got_irelative,
  583. plt_count);
  584. }
  585. private:
  586. // The class which scans relocations.
  587. class Scan
  588. {
  589. public:
  590. Scan()
  591. : issued_non_pic_error_(false)
  592. { }
  593. static inline int
  594. get_reference_flags(unsigned int r_type);
  595. inline void
  596. local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
  597. Sized_relobj_file<size, false>* object,
  598. unsigned int data_shndx,
  599. Output_section* output_section,
  600. const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
  601. const elfcpp::Sym<size, false>& lsym,
  602. bool is_discarded);
  603. inline void
  604. global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
  605. Sized_relobj_file<size, false>* object,
  606. unsigned int data_shndx,
  607. Output_section* output_section,
  608. const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
  609. Symbol* gsym);
  610. inline bool
  611. local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
  612. Target_x86_64* target,
  613. Sized_relobj_file<size, false>* object,
  614. unsigned int data_shndx,
  615. Output_section* output_section,
  616. const elfcpp::Rela<size, false>& reloc,
  617. unsigned int r_type,
  618. const elfcpp::Sym<size, false>& lsym);
  619. inline bool
  620. global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
  621. Target_x86_64* target,
  622. Sized_relobj_file<size, false>* object,
  623. unsigned int data_shndx,
  624. Output_section* output_section,
  625. const elfcpp::Rela<size, false>& reloc,
  626. unsigned int r_type,
  627. Symbol* gsym);
  628. private:
  629. static void
  630. unsupported_reloc_local(Sized_relobj_file<size, false>*,
  631. unsigned int r_type);
  632. static void
  633. unsupported_reloc_global(Sized_relobj_file<size, false>*,
  634. unsigned int r_type, Symbol*);
  635. void
  636. check_non_pic(Relobj*, unsigned int r_type, Symbol*);
  637. inline bool
  638. possible_function_pointer_reloc(unsigned int r_type);
  639. bool
  640. reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
  641. unsigned int r_type);
  642. // Whether we have issued an error about a non-PIC compilation.
  643. bool issued_non_pic_error_;
  644. };
  645. // The class which implements relocation.
  646. class Relocate
  647. {
  648. public:
  649. Relocate()
  650. : skip_call_tls_get_addr_(false)
  651. { }
  652. ~Relocate()
  653. {
  654. if (this->skip_call_tls_get_addr_)
  655. {
  656. // FIXME: This needs to specify the location somehow.
  657. gold_error(_("missing expected TLS relocation"));
  658. }
  659. }
  660. // Do a relocation. Return false if the caller should not issue
  661. // any warnings about this relocation.
  662. inline bool
  663. relocate(const Relocate_info<size, false>*, Target_x86_64*,
  664. Output_section*,
  665. size_t relnum, const elfcpp::Rela<size, false>&,
  666. unsigned int r_type, const Sized_symbol<size>*,
  667. const Symbol_value<size>*,
  668. unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
  669. section_size_type);
  670. private:
  671. // Do a TLS relocation.
  672. inline void
  673. relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
  674. size_t relnum, const elfcpp::Rela<size, false>&,
  675. unsigned int r_type, const Sized_symbol<size>*,
  676. const Symbol_value<size>*,
  677. unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
  678. section_size_type);
  679. // Do a TLS General-Dynamic to Initial-Exec transition.
  680. inline void
  681. tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
  682. Output_segment* tls_segment,
  683. const elfcpp::Rela<size, false>&, unsigned int r_type,
  684. typename elfcpp::Elf_types<size>::Elf_Addr value,
  685. unsigned char* view,
  686. typename elfcpp::Elf_types<size>::Elf_Addr,
  687. section_size_type view_size);
  688. // Do a TLS General-Dynamic to Local-Exec transition.
  689. inline void
  690. tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
  691. Output_segment* tls_segment,
  692. const elfcpp::Rela<size, false>&, unsigned int r_type,
  693. typename elfcpp::Elf_types<size>::Elf_Addr value,
  694. unsigned char* view,
  695. section_size_type view_size);
  696. // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
  697. inline void
  698. tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
  699. Output_segment* tls_segment,
  700. const elfcpp::Rela<size, false>&, unsigned int r_type,
  701. typename elfcpp::Elf_types<size>::Elf_Addr value,
  702. unsigned char* view,
  703. typename elfcpp::Elf_types<size>::Elf_Addr,
  704. section_size_type view_size);
  705. // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
  706. inline void
  707. tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
  708. Output_segment* tls_segment,
  709. const elfcpp::Rela<size, false>&, unsigned int r_type,
  710. typename elfcpp::Elf_types<size>::Elf_Addr value,
  711. unsigned char* view,
  712. section_size_type view_size);
  713. // Do a TLS Local-Dynamic to Local-Exec transition.
  714. inline void
  715. tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
  716. Output_segment* tls_segment,
  717. const elfcpp::Rela<size, false>&, unsigned int r_type,
  718. typename elfcpp::Elf_types<size>::Elf_Addr value,
  719. unsigned char* view,
  720. section_size_type view_size);
  721. // Do a TLS Initial-Exec to Local-Exec transition.
  722. static inline void
  723. tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
  724. Output_segment* tls_segment,
  725. const elfcpp::Rela<size, false>&, unsigned int r_type,
  726. typename elfcpp::Elf_types<size>::Elf_Addr value,
  727. unsigned char* view,
  728. section_size_type view_size);
  729. // This is set if we should skip the next reloc, which should be a
  730. // PLT32 reloc against ___tls_get_addr.
  731. bool skip_call_tls_get_addr_;
  732. };
  733. // A class which returns the size required for a relocation type,
  734. // used while scanning relocs during a relocatable link.
  735. class Relocatable_size_for_reloc
  736. {
  737. public:
  738. unsigned int
  739. get_size_for_reloc(unsigned int, Relobj*);
  740. };
  741. // Check if relocation against this symbol is a candidate for
  742. // conversion from
  743. // mov foo@GOTPCREL(%rip), %reg
  744. // to lea foo(%rip), %reg.
  745. static bool
  746. can_convert_mov_to_lea(const Symbol* gsym)
  747. {
  748. gold_assert(gsym != NULL);
  749. return (gsym->type() != elfcpp::STT_GNU_IFUNC
  750. && !gsym->is_undefined ()
  751. && !gsym->is_from_dynobj()
  752. && !gsym->is_preemptible()
  753. && (!parameters->options().shared()
  754. || (gsym->visibility() != elfcpp::STV_DEFAULT
  755. && gsym->visibility() != elfcpp::STV_PROTECTED)
  756. || parameters->options().Bsymbolic())
  757. && strcmp(gsym->name(), "_DYNAMIC") != 0);
  758. }
  759. // Adjust TLS relocation type based on the options and whether this
  760. // is a local symbol.
  761. static tls::Tls_optimization
  762. optimize_tls_reloc(bool is_final, int r_type);
  763. // Get the GOT section, creating it if necessary.
  764. Output_data_got<64, false>*
  765. got_section(Symbol_table*, Layout*);
  766. // Get the GOT PLT section.
  767. Output_data_got_plt_x86_64*
  768. got_plt_section() const
  769. {
  770. gold_assert(this->got_plt_ != NULL);
  771. return this->got_plt_;
  772. }
  773. // Get the GOT section for TLSDESC entries.
  774. Output_data_got<64, false>*
  775. got_tlsdesc_section() const
  776. {
  777. gold_assert(this->got_tlsdesc_ != NULL);
  778. return this->got_tlsdesc_;
  779. }
  780. // Create the PLT section.
  781. void
  782. make_plt_section(Symbol_table* symtab, Layout* layout);
  783. // Create a PLT entry for a global symbol.
  784. void
  785. make_plt_entry(Symbol_table*, Layout*, Symbol*);
  786. // Create a PLT entry for a local STT_GNU_IFUNC symbol.
  787. void
  788. make_local_ifunc_plt_entry(Symbol_table*, Layout*,
  789. Sized_relobj_file<size, false>* relobj,
  790. unsigned int local_sym_index);
  791. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  792. void
  793. define_tls_base_symbol(Symbol_table*, Layout*);
  794. // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
  795. void
  796. reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
  797. // Create a GOT entry for the TLS module index.
  798. unsigned int
  799. got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  800. Sized_relobj_file<size, false>* object);
  801. // Get the PLT section.
  802. Output_data_plt_x86_64<size>*
  803. plt_section() const
  804. {
  805. gold_assert(this->plt_ != NULL);
  806. return this->plt_;
  807. }
  808. // Get the dynamic reloc section, creating it if necessary.
  809. Reloc_section*
  810. rela_dyn_section(Layout*);
  811. // Get the section to use for TLSDESC relocations.
  812. Reloc_section*
  813. rela_tlsdesc_section(Layout*) const;
  814. // Get the section to use for IRELATIVE relocations.
  815. Reloc_section*
  816. rela_irelative_section(Layout*);
  817. // Add a potential copy relocation.
  818. void
  819. copy_reloc(Symbol_table* symtab, Layout* layout,
  820. Sized_relobj_file<size, false>* object,
  821. unsigned int shndx, Output_section* output_section,
  822. Symbol* sym, const elfcpp::Rela<size, false>& reloc)
  823. {
  824. this->copy_relocs_.copy_reloc(symtab, layout,
  825. symtab->get_sized_symbol<size>(sym),
  826. object, shndx, output_section,
  827. reloc, this->rela_dyn_section(layout));
  828. }
  829. // Information about this specific target which we pass to the
  830. // general Target structure.
  831. static const Target::Target_info x86_64_info;
  832. // The types of GOT entries needed for this platform.
  833. // These values are exposed to the ABI in an incremental link.
  834. // Do not renumber existing values without changing the version
  835. // number of the .gnu_incremental_inputs section.
  836. enum Got_type
  837. {
  838. GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
  839. GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
  840. GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
  841. GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
  842. };
  843. // This type is used as the argument to the target specific
  844. // relocation routines. The only target specific reloc is
  845. // R_X86_64_TLSDESC against a local symbol.
  846. struct Tlsdesc_info
  847. {
  848. Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
  849. : object(a_object), r_sym(a_r_sym)
  850. { }
  851. // The object in which the local symbol is defined.
  852. Sized_relobj_file<size, false>* object;
  853. // The local symbol index in the object.
  854. unsigned int r_sym;
  855. };
  856. // The GOT section.
  857. Output_data_got<64, false>* got_;
  858. // The PLT section.
  859. Output_data_plt_x86_64<size>* plt_;
  860. // The GOT PLT section.
  861. Output_data_got_plt_x86_64* got_plt_;
  862. // The GOT section for IRELATIVE relocations.
  863. Output_data_space* got_irelative_;
  864. // The GOT section for TLSDESC relocations.
  865. Output_data_got<64, false>* got_tlsdesc_;
  866. // The _GLOBAL_OFFSET_TABLE_ symbol.
  867. Symbol* global_offset_table_;
  868. // The dynamic reloc section.
  869. Reloc_section* rela_dyn_;
  870. // The section to use for IRELATIVE relocs.
  871. Reloc_section* rela_irelative_;
  872. // Relocs saved to avoid a COPY reloc.
  873. Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
  874. // Offset of the GOT entry for the TLS module index.
  875. unsigned int got_mod_index_offset_;
  876. // We handle R_X86_64_TLSDESC against a local symbol as a target
  877. // specific relocation. Here we store the object and local symbol
  878. // index for the relocation.
  879. std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
  880. // True if the _TLS_MODULE_BASE_ symbol has been defined.
  881. bool tls_base_symbol_defined_;
  882. };
  883. template<>
  884. const Target::Target_info Target_x86_64<64>::x86_64_info =
  885. {
  886. 64, // size
  887. false, // is_big_endian
  888. elfcpp::EM_X86_64, // machine_code
  889. false, // has_make_symbol
  890. false, // has_resolve
  891. true, // has_code_fill
  892. true, // is_default_stack_executable
  893. true, // can_icf_inline_merge_sections
  894. '\0', // wrap_char
  895. "/lib/ld64.so.1", // program interpreter
  896. 0x400000, // default_text_segment_address
  897. 0x1000, // abi_pagesize (overridable by -z max-page-size)
  898. 0x1000, // common_pagesize (overridable by -z common-page-size)
  899. false, // isolate_execinstr
  900. 0, // rosegment_gap
  901. elfcpp::SHN_UNDEF, // small_common_shndx
  902. elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
  903. 0, // small_common_section_flags
  904. elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
  905. NULL, // attributes_section
  906. NULL, // attributes_vendor
  907. "_start" // entry_symbol_name
  908. };
  909. template<>
  910. const Target::Target_info Target_x86_64<32>::x86_64_info =
  911. {
  912. 32, // size
  913. false, // is_big_endian
  914. elfcpp::EM_X86_64, // machine_code
  915. false, // has_make_symbol
  916. false, // has_resolve
  917. true, // has_code_fill
  918. true, // is_default_stack_executable
  919. true, // can_icf_inline_merge_sections
  920. '\0', // wrap_char
  921. "/libx32/ldx32.so.1", // program interpreter
  922. 0x400000, // default_text_segment_address
  923. 0x1000, // abi_pagesize (overridable by -z max-page-size)
  924. 0x1000, // common_pagesize (overridable by -z common-page-size)
  925. false, // isolate_execinstr
  926. 0, // rosegment_gap
  927. elfcpp::SHN_UNDEF, // small_common_shndx
  928. elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
  929. 0, // small_common_section_flags
  930. elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
  931. NULL, // attributes_section
  932. NULL, // attributes_vendor
  933. "_start" // entry_symbol_name
  934. };
  935. // This is called when a new output section is created. This is where
  936. // we handle the SHF_X86_64_LARGE.
  937. template<int size>
  938. void
  939. Target_x86_64<size>::do_new_output_section(Output_section* os) const
  940. {
  941. if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
  942. os->set_is_large_section();
  943. }
  944. // Get the GOT section, creating it if necessary.
  945. template<int size>
  946. Output_data_got<64, false>*
  947. Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
  948. {
  949. if (this->got_ == NULL)
  950. {
  951. gold_assert(symtab != NULL && layout != NULL);
  952. // When using -z now, we can treat .got.plt as a relro section.
  953. // Without -z now, it is modified after program startup by lazy
  954. // PLT relocations.
  955. bool is_got_plt_relro = parameters->options().now();
  956. Output_section_order got_order = (is_got_plt_relro
  957. ? ORDER_RELRO
  958. : ORDER_RELRO_LAST);
  959. Output_section_order got_plt_order = (is_got_plt_relro
  960. ? ORDER_RELRO
  961. : ORDER_NON_RELRO_FIRST);
  962. this->got_ = new Output_data_got<64, false>();
  963. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  964. (elfcpp::SHF_ALLOC
  965. | elfcpp::SHF_WRITE),
  966. this->got_, got_order, true);
  967. this->got_plt_ = new Output_data_got_plt_x86_64(layout);
  968. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  969. (elfcpp::SHF_ALLOC
  970. | elfcpp::SHF_WRITE),
  971. this->got_plt_, got_plt_order,
  972. is_got_plt_relro);
  973. // The first three entries are reserved.
  974. this->got_plt_->set_current_data_size(3 * 8);
  975. if (!is_got_plt_relro)
  976. {
  977. // Those bytes can go into the relro segment.
  978. layout->increase_relro(3 * 8);
  979. }
  980. // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
  981. this->global_offset_table_ =
  982. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  983. Symbol_table::PREDEFINED,
  984. this->got_plt_,
  985. 0, 0, elfcpp::STT_OBJECT,
  986. elfcpp::STB_LOCAL,
  987. elfcpp::STV_HIDDEN, 0,
  988. false, false);
  989. // If there are any IRELATIVE relocations, they get GOT entries
  990. // in .got.plt after the jump slot entries.
  991. this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
  992. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  993. (elfcpp::SHF_ALLOC
  994. | elfcpp::SHF_WRITE),
  995. this->got_irelative_,
  996. got_plt_order, is_got_plt_relro);
  997. // If there are any TLSDESC relocations, they get GOT entries in
  998. // .got.plt after the jump slot and IRELATIVE entries.
  999. this->got_tlsdesc_ = new Output_data_got<64, false>();
  1000. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  1001. (elfcpp::SHF_ALLOC
  1002. | elfcpp::SHF_WRITE),
  1003. this->got_tlsdesc_,
  1004. got_plt_order, is_got_plt_relro);
  1005. }
  1006. return this->got_;
  1007. }
  1008. // Get the dynamic reloc section, creating it if necessary.
  1009. template<int size>
  1010. typename Target_x86_64<size>::Reloc_section*
  1011. Target_x86_64<size>::rela_dyn_section(Layout* layout)
  1012. {
  1013. if (this->rela_dyn_ == NULL)
  1014. {
  1015. gold_assert(layout != NULL);
  1016. this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
  1017. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  1018. elfcpp::SHF_ALLOC, this->rela_dyn_,
  1019. ORDER_DYNAMIC_RELOCS, false);
  1020. }
  1021. return this->rela_dyn_;
  1022. }
  1023. // Get the section to use for IRELATIVE relocs, creating it if
  1024. // necessary. These go in .rela.dyn, but only after all other dynamic
  1025. // relocations. They need to follow the other dynamic relocations so
  1026. // that they can refer to global variables initialized by those
  1027. // relocs.
  1028. template<int size>
  1029. typename Target_x86_64<size>::Reloc_section*
  1030. Target_x86_64<size>::rela_irelative_section(Layout* layout)
  1031. {
  1032. if (this->rela_irelative_ == NULL)
  1033. {
  1034. // Make sure we have already created the dynamic reloc section.
  1035. this->rela_dyn_section(layout);
  1036. this->rela_irelative_ = new Reloc_section(false);
  1037. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  1038. elfcpp::SHF_ALLOC, this->rela_irelative_,
  1039. ORDER_DYNAMIC_RELOCS, false);
  1040. gold_assert(this->rela_dyn_->output_section()
  1041. == this->rela_irelative_->output_section());
  1042. }
  1043. return this->rela_irelative_;
  1044. }
  1045. // Write the first three reserved words of the .got.plt section.
  1046. // The remainder of the section is written while writing the PLT
  1047. // in Output_data_plt_i386::do_write.
  1048. void
  1049. Output_data_got_plt_x86_64::do_write(Output_file* of)
  1050. {
  1051. // The first entry in the GOT is the address of the .dynamic section
  1052. // aka the PT_DYNAMIC segment. The next two entries are reserved.
  1053. // We saved space for them when we created the section in
  1054. // Target_x86_64::got_section.
  1055. const off_t got_file_offset = this->offset();
  1056. gold_assert(this->data_size() >= 24);
  1057. unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
  1058. Output_section* dynamic = this->layout_->dynamic_section();
  1059. uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
  1060. elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
  1061. memset(got_view + 8, 0, 16);
  1062. of->write_output_view(got_file_offset, 24, got_view);
  1063. }
  1064. // Initialize the PLT section.
  1065. template<int size>
  1066. void
  1067. Output_data_plt_x86_64<size>::init(Layout* layout)
  1068. {
  1069. this->rel_ = new Reloc_section(false);
  1070. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  1071. elfcpp::SHF_ALLOC, this->rel_,
  1072. ORDER_DYNAMIC_PLT_RELOCS, false);
  1073. }
  1074. template<int size>
  1075. void
  1076. Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
  1077. {
  1078. os->set_entsize(this->get_plt_entry_size());
  1079. }
  1080. // Add an entry to the PLT.
  1081. template<int size>
  1082. void
  1083. Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
  1084. Symbol* gsym)
  1085. {
  1086. gold_assert(!gsym->has_plt_offset());
  1087. unsigned int plt_index;
  1088. off_t plt_offset;
  1089. section_offset_type got_offset;
  1090. unsigned int* pcount;
  1091. unsigned int offset;
  1092. unsigned int reserved;
  1093. Output_section_data_build* got;
  1094. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1095. && gsym->can_use_relative_reloc(false))
  1096. {
  1097. pcount = &this->irelative_count_;
  1098. offset = 0;
  1099. reserved = 0;
  1100. got = this->got_irelative_;
  1101. }
  1102. else
  1103. {
  1104. pcount = &this->count_;
  1105. offset = 1;
  1106. reserved = 3;
  1107. got = this->got_plt_;
  1108. }
  1109. if (!this->is_data_size_valid())
  1110. {
  1111. // Note that when setting the PLT offset for a non-IRELATIVE
  1112. // entry we skip the initial reserved PLT entry.
  1113. plt_index = *pcount + offset;
  1114. plt_offset = plt_index * this->get_plt_entry_size();
  1115. ++*pcount;
  1116. got_offset = (plt_index - offset + reserved) * 8;
  1117. gold_assert(got_offset == got->current_data_size());
  1118. // Every PLT entry needs a GOT entry which points back to the PLT
  1119. // entry (this will be changed by the dynamic linker, normally
  1120. // lazily when the function is called).
  1121. got->set_current_data_size(got_offset + 8);
  1122. }
  1123. else
  1124. {
  1125. // FIXME: This is probably not correct for IRELATIVE relocs.
  1126. // For incremental updates, find an available slot.
  1127. plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
  1128. this->get_plt_entry_size(), 0);
  1129. if (plt_offset == -1)
  1130. gold_fallback(_("out of patch space (PLT);"
  1131. " relink with --incremental-full"));
  1132. // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
  1133. // can be calculated from the PLT index, adjusting for the three
  1134. // reserved entries at the beginning of the GOT.
  1135. plt_index = plt_offset / this->get_plt_entry_size() - 1;
  1136. got_offset = (plt_index - offset + reserved) * 8;
  1137. }
  1138. gsym->set_plt_offset(plt_offset);
  1139. // Every PLT entry needs a reloc.
  1140. this->add_relocation(symtab, layout, gsym, got_offset);
  1141. // Note that we don't need to save the symbol. The contents of the
  1142. // PLT are independent of which symbols are used. The symbols only
  1143. // appear in the relocations.
  1144. }
  1145. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
  1146. // the PLT offset.
  1147. template<int size>
  1148. unsigned int
  1149. Output_data_plt_x86_64<size>::add_local_ifunc_entry(
  1150. Symbol_table* symtab,
  1151. Layout* layout,
  1152. Sized_relobj_file<size, false>* relobj,
  1153. unsigned int local_sym_index)
  1154. {
  1155. unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
  1156. ++this->irelative_count_;
  1157. section_offset_type got_offset = this->got_irelative_->current_data_size();
  1158. // Every PLT entry needs a GOT entry which points back to the PLT
  1159. // entry.
  1160. this->got_irelative_->set_current_data_size(got_offset + 8);
  1161. // Every PLT entry needs a reloc.
  1162. Reloc_section* rela = this->rela_irelative(symtab, layout);
  1163. rela->add_symbolless_local_addend(relobj, local_sym_index,
  1164. elfcpp::R_X86_64_IRELATIVE,
  1165. this->got_irelative_, got_offset, 0);
  1166. return plt_offset;
  1167. }
  1168. // Add the relocation for a PLT entry.
  1169. template<int size>
  1170. void
  1171. Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
  1172. Layout* layout,
  1173. Symbol* gsym,
  1174. unsigned int got_offset)
  1175. {
  1176. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1177. && gsym->can_use_relative_reloc(false))
  1178. {
  1179. Reloc_section* rela = this->rela_irelative(symtab, layout);
  1180. rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
  1181. this->got_irelative_, got_offset, 0);
  1182. }
  1183. else
  1184. {
  1185. gsym->set_needs_dynsym_entry();
  1186. this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
  1187. got_offset, 0);
  1188. }
  1189. }
  1190. // Return where the TLSDESC relocations should go, creating it if
  1191. // necessary. These follow the JUMP_SLOT relocations.
  1192. template<int size>
  1193. typename Output_data_plt_x86_64<size>::Reloc_section*
  1194. Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
  1195. {
  1196. if (this->tlsdesc_rel_ == NULL)
  1197. {
  1198. this->tlsdesc_rel_ = new Reloc_section(false);
  1199. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  1200. elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
  1201. ORDER_DYNAMIC_PLT_RELOCS, false);
  1202. gold_assert(this->tlsdesc_rel_->output_section()
  1203. == this->rel_->output_section());
  1204. }
  1205. return this->tlsdesc_rel_;
  1206. }
  1207. // Return where the IRELATIVE relocations should go in the PLT. These
  1208. // follow the JUMP_SLOT and the TLSDESC relocations.
  1209. template<int size>
  1210. typename Output_data_plt_x86_64<size>::Reloc_section*
  1211. Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
  1212. Layout* layout)
  1213. {
  1214. if (this->irelative_rel_ == NULL)
  1215. {
  1216. // Make sure we have a place for the TLSDESC relocations, in
  1217. // case we see any later on.
  1218. this->rela_tlsdesc(layout);
  1219. this->irelative_rel_ = new Reloc_section(false);
  1220. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  1221. elfcpp::SHF_ALLOC, this->irelative_rel_,
  1222. ORDER_DYNAMIC_PLT_RELOCS, false);
  1223. gold_assert(this->irelative_rel_->output_section()
  1224. == this->rel_->output_section());
  1225. if (parameters->doing_static_link())
  1226. {
  1227. // A statically linked executable will only have a .rela.plt
  1228. // section to hold R_X86_64_IRELATIVE relocs for
  1229. // STT_GNU_IFUNC symbols. The library will use these
  1230. // symbols to locate the IRELATIVE relocs at program startup
  1231. // time.
  1232. symtab->define_in_output_data("__rela_iplt_start", NULL,
  1233. Symbol_table::PREDEFINED,
  1234. this->irelative_rel_, 0, 0,
  1235. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  1236. elfcpp::STV_HIDDEN, 0, false, true);
  1237. symtab->define_in_output_data("__rela_iplt_end", NULL,
  1238. Symbol_table::PREDEFINED,
  1239. this->irelative_rel_, 0, 0,
  1240. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  1241. elfcpp::STV_HIDDEN, 0, true, true);
  1242. }
  1243. }
  1244. return this->irelative_rel_;
  1245. }
  1246. // Return the PLT address to use for a global symbol.
  1247. template<int size>
  1248. uint64_t
  1249. Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym)
  1250. {
  1251. uint64_t offset = 0;
  1252. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1253. && gsym->can_use_relative_reloc(false))
  1254. offset = (this->count_ + 1) * this->get_plt_entry_size();
  1255. return this->address() + offset + gsym->plt_offset();
  1256. }
  1257. // Return the PLT address to use for a local symbol. These are always
  1258. // IRELATIVE relocs.
  1259. template<int size>
  1260. uint64_t
  1261. Output_data_plt_x86_64<size>::address_for_local(const Relobj* object,
  1262. unsigned int r_sym)
  1263. {
  1264. return (this->address()
  1265. + (this->count_ + 1) * this->get_plt_entry_size()
  1266. + object->local_plt_offset(r_sym));
  1267. }
  1268. // Set the final size.
  1269. template<int size>
  1270. void
  1271. Output_data_plt_x86_64<size>::set_final_data_size()
  1272. {
  1273. unsigned int count = this->count_ + this->irelative_count_;
  1274. if (this->has_tlsdesc_entry())
  1275. ++count;
  1276. this->set_data_size((count + 1) * this->get_plt_entry_size());
  1277. }
  1278. // The first entry in the PLT for an executable.
  1279. template<int size>
  1280. const unsigned char
  1281. Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
  1282. {
  1283. // From AMD64 ABI Draft 0.98, page 76
  1284. 0xff, 0x35, // pushq contents of memory address
  1285. 0, 0, 0, 0, // replaced with address of .got + 8
  1286. 0xff, 0x25, // jmp indirect
  1287. 0, 0, 0, 0, // replaced with address of .got + 16
  1288. 0x90, 0x90, 0x90, 0x90 // noop (x4)
  1289. };
  1290. template<int size>
  1291. void
  1292. Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
  1293. unsigned char* pov,
  1294. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  1295. typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
  1296. {
  1297. memcpy(pov, first_plt_entry, plt_entry_size);
  1298. // We do a jmp relative to the PC at the end of this instruction.
  1299. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1300. (got_address + 8
  1301. - (plt_address + 6)));
  1302. elfcpp::Swap<32, false>::writeval(pov + 8,
  1303. (got_address + 16
  1304. - (plt_address + 12)));
  1305. }
  1306. // Subsequent entries in the PLT for an executable.
  1307. template<int size>
  1308. const unsigned char
  1309. Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
  1310. {
  1311. // From AMD64 ABI Draft 0.98, page 76
  1312. 0xff, 0x25, // jmpq indirect
  1313. 0, 0, 0, 0, // replaced with address of symbol in .got
  1314. 0x68, // pushq immediate
  1315. 0, 0, 0, 0, // replaced with offset into relocation table
  1316. 0xe9, // jmpq relative
  1317. 0, 0, 0, 0 // replaced with offset to start of .plt
  1318. };
  1319. template<int size>
  1320. unsigned int
  1321. Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
  1322. unsigned char* pov,
  1323. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  1324. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  1325. unsigned int got_offset,
  1326. unsigned int plt_offset,
  1327. unsigned int plt_index)
  1328. {
  1329. // Check PC-relative offset overflow in PLT entry.
  1330. uint64_t plt_got_pcrel_offset = (got_address + got_offset
  1331. - (plt_address + plt_offset + 6));
  1332. if (Bits<32>::has_overflow(plt_got_pcrel_offset))
  1333. gold_error(_("PC-relative offset overflow in PLT entry %d"),
  1334. plt_index + 1);
  1335. memcpy(pov, plt_entry, plt_entry_size);
  1336. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1337. plt_got_pcrel_offset);
  1338. elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
  1339. elfcpp::Swap<32, false>::writeval(pov + 12,
  1340. - (plt_offset + plt_entry_size));
  1341. return 6;
  1342. }
  1343. // The reserved TLSDESC entry in the PLT for an executable.
  1344. template<int size>
  1345. const unsigned char
  1346. Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
  1347. {
  1348. // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
  1349. // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
  1350. 0xff, 0x35, // pushq x(%rip)
  1351. 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
  1352. 0xff, 0x25, // jmpq *y(%rip)
  1353. 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
  1354. 0x0f, 0x1f, // nop
  1355. 0x40, 0
  1356. };
  1357. template<int size>
  1358. void
  1359. Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
  1360. unsigned char* pov,
  1361. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  1362. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  1363. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  1364. unsigned int tlsdesc_got_offset,
  1365. unsigned int plt_offset)
  1366. {
  1367. memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
  1368. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1369. (got_address + 8
  1370. - (plt_address + plt_offset
  1371. + 6)));
  1372. elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
  1373. (got_base
  1374. + tlsdesc_got_offset
  1375. - (plt_address + plt_offset
  1376. + 12)));
  1377. }
  1378. // The .eh_frame unwind information for the PLT.
  1379. template<int size>
  1380. const unsigned char
  1381. Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
  1382. {
  1383. 1, // CIE version.
  1384. 'z', // Augmentation: augmentation size included.
  1385. 'R', // Augmentation: FDE encoding included.
  1386. '\0', // End of augmentation string.
  1387. 1, // Code alignment factor.
  1388. 0x78, // Data alignment factor.
  1389. 16, // Return address column.
  1390. 1, // Augmentation size.
  1391. (elfcpp::DW_EH_PE_pcrel // FDE encoding.
  1392. | elfcpp::DW_EH_PE_sdata4),
  1393. elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
  1394. elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
  1395. elfcpp::DW_CFA_nop, // Align to 16 bytes.
  1396. elfcpp::DW_CFA_nop
  1397. };
  1398. template<int size>
  1399. const unsigned char
  1400. Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
  1401. {
  1402. 0, 0, 0, 0, // Replaced with offset to .plt.
  1403. 0, 0, 0, 0, // Replaced with size of .plt.
  1404. 0, // Augmentation size.
  1405. elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
  1406. elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
  1407. elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
  1408. elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
  1409. elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
  1410. 11, // Block length.
  1411. elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
  1412. elfcpp::DW_OP_breg16, 0, // Push %rip.
  1413. elfcpp::DW_OP_lit15, // Push 0xf.
  1414. elfcpp::DW_OP_and, // & (%rip & 0xf).
  1415. elfcpp::DW_OP_lit11, // Push 0xb.
  1416. elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb)
  1417. elfcpp::DW_OP_lit3, // Push 3.
  1418. elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3)
  1419. elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
  1420. elfcpp::DW_CFA_nop, // Align to 32 bytes.
  1421. elfcpp::DW_CFA_nop,
  1422. elfcpp::DW_CFA_nop,
  1423. elfcpp::DW_CFA_nop
  1424. };
  1425. // Write out the PLT. This uses the hand-coded instructions above,
  1426. // and adjusts them as needed. This is specified by the AMD64 ABI.
  1427. template<int size>
  1428. void
  1429. Output_data_plt_x86_64<size>::do_write(Output_file* of)
  1430. {
  1431. const off_t offset = this->offset();
  1432. const section_size_type oview_size =
  1433. convert_to_section_size_type(this->data_size());
  1434. unsigned char* const oview = of->get_output_view(offset, oview_size);
  1435. const off_t got_file_offset = this->got_plt_->offset();
  1436. gold_assert(parameters->incremental_update()
  1437. || (got_file_offset + this->got_plt_->data_size()
  1438. == this->got_irelative_->offset()));
  1439. const section_size_type got_size =
  1440. convert_to_section_size_type(this->got_plt_->data_size()
  1441. + this->got_irelative_->data_size());
  1442. unsigned char* const got_view = of->get_output_view(got_file_offset,
  1443. got_size);
  1444. unsigned char* pov = oview;
  1445. // The base address of the .plt section.
  1446. typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
  1447. // The base address of the .got section.
  1448. typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
  1449. // The base address of the PLT portion of the .got section,
  1450. // which is where the GOT pointer will point, and where the
  1451. // three reserved GOT entries are located.
  1452. typename elfcpp::Elf_types<size>::Elf_Addr got_address
  1453. = this->got_plt_->address();
  1454. this->fill_first_plt_entry(pov, got_address, plt_address);
  1455. pov += this->get_plt_entry_size();
  1456. // The first three entries in the GOT are reserved, and are written
  1457. // by Output_data_got_plt_x86_64::do_write.
  1458. unsigned char* got_pov = got_view + 24;
  1459. unsigned int plt_offset = this->get_plt_entry_size();
  1460. unsigned int got_offset = 24;
  1461. const unsigned int count = this->count_ + this->irelative_count_;
  1462. for (unsigned int plt_index = 0;
  1463. plt_index < count;
  1464. ++plt_index,
  1465. pov += this->get_plt_entry_size(),
  1466. got_pov += 8,
  1467. plt_offset += this->get_plt_entry_size(),
  1468. got_offset += 8)
  1469. {
  1470. // Set and adjust the PLT entry itself.
  1471. unsigned int lazy_offset = this->fill_plt_entry(pov,
  1472. got_address, plt_address,
  1473. got_offset, plt_offset,
  1474. plt_index);
  1475. // Set the entry in the GOT.
  1476. elfcpp::Swap<64, false>::writeval(got_pov,
  1477. plt_address + plt_offset + lazy_offset);
  1478. }
  1479. if (this->has_tlsdesc_entry())
  1480. {
  1481. // Set and adjust the reserved TLSDESC PLT entry.
  1482. unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
  1483. this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
  1484. tlsdesc_got_offset, plt_offset);
  1485. pov += this->get_plt_entry_size();
  1486. }
  1487. gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
  1488. gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
  1489. of->write_output_view(offset, oview_size, oview);
  1490. of->write_output_view(got_file_offset, got_size, got_view);
  1491. }
  1492. // Create the PLT section.
  1493. template<int size>
  1494. void
  1495. Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
  1496. {
  1497. if (this->plt_ == NULL)
  1498. {
  1499. // Create the GOT sections first.
  1500. this->got_section(symtab, layout);
  1501. this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
  1502. this->got_irelative_);
  1503. // Add unwind information if requested.
  1504. if (parameters->options().ld_generated_unwind_info())
  1505. this->plt_->add_eh_frame(layout);
  1506. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  1507. (elfcpp::SHF_ALLOC
  1508. | elfcpp::SHF_EXECINSTR),
  1509. this->plt_, ORDER_PLT, false);
  1510. // Make the sh_info field of .rela.plt point to .plt.
  1511. Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
  1512. rela_plt_os->set_info_section(this->plt_->output_section());
  1513. }
  1514. }
  1515. // Return the section for TLSDESC relocations.
  1516. template<int size>
  1517. typename Target_x86_64<size>::Reloc_section*
  1518. Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
  1519. {
  1520. return this->plt_section()->rela_tlsdesc(layout);
  1521. }
  1522. // Create a PLT entry for a global symbol.
  1523. template<int size>
  1524. void
  1525. Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
  1526. Symbol* gsym)
  1527. {
  1528. if (gsym->has_plt_offset())
  1529. return;
  1530. if (this->plt_ == NULL)
  1531. this->make_plt_section(symtab, layout);
  1532. this->plt_->add_entry(symtab, layout, gsym);
  1533. }
  1534. // Make a PLT entry for a local STT_GNU_IFUNC symbol.
  1535. template<int size>
  1536. void
  1537. Target_x86_64<size>::make_local_ifunc_plt_entry(
  1538. Symbol_table* symtab, Layout* layout,
  1539. Sized_relobj_file<size, false>* relobj,
  1540. unsigned int local_sym_index)
  1541. {
  1542. if (relobj->local_has_plt_offset(local_sym_index))
  1543. return;
  1544. if (this->plt_ == NULL)
  1545. this->make_plt_section(symtab, layout);
  1546. unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
  1547. relobj,
  1548. local_sym_index);
  1549. relobj->set_local_plt_offset(local_sym_index, plt_offset);
  1550. }
  1551. // Return the number of entries in the PLT.
  1552. template<int size>
  1553. unsigned int
  1554. Target_x86_64<size>::plt_entry_count() const
  1555. {
  1556. if (this->plt_ == NULL)
  1557. return 0;
  1558. return this->plt_->entry_count();
  1559. }
  1560. // Return the offset of the first non-reserved PLT entry.
  1561. template<int size>
  1562. unsigned int
  1563. Target_x86_64<size>::first_plt_entry_offset() const
  1564. {
  1565. return this->plt_->first_plt_entry_offset();
  1566. }
  1567. // Return the size of each PLT entry.
  1568. template<int size>
  1569. unsigned int
  1570. Target_x86_64<size>::plt_entry_size() const
  1571. {
  1572. return this->plt_->get_plt_entry_size();
  1573. }
  1574. // Create the GOT and PLT sections for an incremental update.
  1575. template<int size>
  1576. Output_data_got_base*
  1577. Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
  1578. Layout* layout,
  1579. unsigned int got_count,
  1580. unsigned int plt_count)
  1581. {
  1582. gold_assert(this->got_ == NULL);
  1583. this->got_ = new Output_data_got<64, false>(got_count * 8);
  1584. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  1585. (elfcpp::SHF_ALLOC
  1586. | elfcpp::SHF_WRITE),
  1587. this->got_, ORDER_RELRO_LAST,
  1588. true);
  1589. // Add the three reserved entries.
  1590. this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
  1591. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  1592. (elfcpp::SHF_ALLOC
  1593. | elfcpp::SHF_WRITE),
  1594. this->got_plt_, ORDER_NON_RELRO_FIRST,
  1595. false);
  1596. // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
  1597. this->global_offset_table_ =
  1598. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  1599. Symbol_table::PREDEFINED,
  1600. this->got_plt_,
  1601. 0, 0, elfcpp::STT_OBJECT,
  1602. elfcpp::STB_LOCAL,
  1603. elfcpp::STV_HIDDEN, 0,
  1604. false, false);
  1605. // If there are any TLSDESC relocations, they get GOT entries in
  1606. // .got.plt after the jump slot entries.
  1607. // FIXME: Get the count for TLSDESC entries.
  1608. this->got_tlsdesc_ = new Output_data_got<64, false>(0);
  1609. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  1610. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  1611. this->got_tlsdesc_,
  1612. ORDER_NON_RELRO_FIRST, false);
  1613. // If there are any IRELATIVE relocations, they get GOT entries in
  1614. // .got.plt after the jump slot and TLSDESC entries.
  1615. this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
  1616. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  1617. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  1618. this->got_irelative_,
  1619. ORDER_NON_RELRO_FIRST, false);
  1620. // Create the PLT section.
  1621. this->plt_ = this->make_data_plt(layout, this->got_,
  1622. this->got_plt_,
  1623. this->got_irelative_,
  1624. plt_count);
  1625. // Add unwind information if requested.
  1626. if (parameters->options().ld_generated_unwind_info())
  1627. this->plt_->add_eh_frame(layout);
  1628. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  1629. elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
  1630. this->plt_, ORDER_PLT, false);
  1631. // Make the sh_info field of .rela.plt point to .plt.
  1632. Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
  1633. rela_plt_os->set_info_section(this->plt_->output_section());
  1634. // Create the rela_dyn section.
  1635. this->rela_dyn_section(layout);
  1636. return this->got_;
  1637. }
  1638. // Reserve a GOT entry for a local symbol, and regenerate any
  1639. // necessary dynamic relocations.
  1640. template<int size>
  1641. void
  1642. Target_x86_64<size>::reserve_local_got_entry(
  1643. unsigned int got_index,
  1644. Sized_relobj<size, false>* obj,
  1645. unsigned int r_sym,
  1646. unsigned int got_type)
  1647. {
  1648. unsigned int got_offset = got_index * 8;
  1649. Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
  1650. this->got_->reserve_local(got_index, obj, r_sym, got_type);
  1651. switch (got_type)
  1652. {
  1653. case GOT_TYPE_STANDARD:
  1654. if (parameters->options().output_is_position_independent())
  1655. rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
  1656. this->got_, got_offset, 0, false);
  1657. break;
  1658. case GOT_TYPE_TLS_OFFSET:
  1659. rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
  1660. this->got_, got_offset, 0);
  1661. break;
  1662. case GOT_TYPE_TLS_PAIR:
  1663. this->got_->reserve_slot(got_index + 1);
  1664. rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
  1665. this->got_, got_offset, 0);
  1666. break;
  1667. case GOT_TYPE_TLS_DESC:
  1668. gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
  1669. // this->got_->reserve_slot(got_index + 1);
  1670. // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
  1671. // this->got_, got_offset, 0);
  1672. break;
  1673. default:
  1674. gold_unreachable();
  1675. }
  1676. }
  1677. // Reserve a GOT entry for a global symbol, and regenerate any
  1678. // necessary dynamic relocations.
  1679. template<int size>
  1680. void
  1681. Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
  1682. Symbol* gsym,
  1683. unsigned int got_type)
  1684. {
  1685. unsigned int got_offset = got_index * 8;
  1686. Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
  1687. this->got_->reserve_global(got_index, gsym, got_type);
  1688. switch (got_type)
  1689. {
  1690. case GOT_TYPE_STANDARD:
  1691. if (!gsym->final_value_is_known())
  1692. {
  1693. if (gsym->is_from_dynobj()
  1694. || gsym->is_undefined()
  1695. || gsym->is_preemptible()
  1696. || gsym->type() == elfcpp::STT_GNU_IFUNC)
  1697. rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
  1698. this->got_, got_offset, 0);
  1699. else
  1700. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
  1701. this->got_, got_offset, 0, false);
  1702. }
  1703. break;
  1704. case GOT_TYPE_TLS_OFFSET:
  1705. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
  1706. this->got_, got_offset, 0, false);
  1707. break;
  1708. case GOT_TYPE_TLS_PAIR:
  1709. this->got_->reserve_slot(got_index + 1);
  1710. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
  1711. this->got_, got_offset, 0, false);
  1712. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
  1713. this->got_, got_offset + 8, 0, false);
  1714. break;
  1715. case GOT_TYPE_TLS_DESC:
  1716. this->got_->reserve_slot(got_index + 1);
  1717. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
  1718. this->got_, got_offset, 0, false);
  1719. break;
  1720. default:
  1721. gold_unreachable();
  1722. }
  1723. }
  1724. // Register an existing PLT entry for a global symbol.
  1725. template<int size>
  1726. void
  1727. Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
  1728. Layout* layout,
  1729. unsigned int plt_index,
  1730. Symbol* gsym)
  1731. {
  1732. gold_assert(this->plt_ != NULL);
  1733. gold_assert(!gsym->has_plt_offset());
  1734. this->plt_->reserve_slot(plt_index);
  1735. gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
  1736. unsigned int got_offset = (plt_index + 3) * 8;
  1737. this->plt_->add_relocation(symtab, layout, gsym, got_offset);
  1738. }
  1739. // Force a COPY relocation for a given symbol.
  1740. template<int size>
  1741. void
  1742. Target_x86_64<size>::emit_copy_reloc(
  1743. Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
  1744. {
  1745. this->copy_relocs_.emit_copy_reloc(symtab,
  1746. symtab->get_sized_symbol<size>(sym),
  1747. os,
  1748. offset,
  1749. this->rela_dyn_section(NULL));
  1750. }
  1751. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  1752. template<int size>
  1753. void
  1754. Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
  1755. Layout* layout)
  1756. {
  1757. if (this->tls_base_symbol_defined_)
  1758. return;
  1759. Output_segment* tls_segment = layout->tls_segment();
  1760. if (tls_segment != NULL)
  1761. {
  1762. bool is_exec = parameters->options().output_is_executable();
  1763. symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
  1764. Symbol_table::PREDEFINED,
  1765. tls_segment, 0, 0,
  1766. elfcpp::STT_TLS,
  1767. elfcpp::STB_LOCAL,
  1768. elfcpp::STV_HIDDEN, 0,
  1769. (is_exec
  1770. ? Symbol::SEGMENT_END
  1771. : Symbol::SEGMENT_START),
  1772. true);
  1773. }
  1774. this->tls_base_symbol_defined_ = true;
  1775. }
  1776. // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
  1777. template<int size>
  1778. void
  1779. Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
  1780. Layout* layout)
  1781. {
  1782. if (this->plt_ == NULL)
  1783. this->make_plt_section(symtab, layout);
  1784. if (!this->plt_->has_tlsdesc_entry())
  1785. {
  1786. // Allocate the TLSDESC_GOT entry.
  1787. Output_data_got<64, false>* got = this->got_section(symtab, layout);
  1788. unsigned int got_offset = got->add_constant(0);
  1789. // Allocate the TLSDESC_PLT entry.
  1790. this->plt_->reserve_tlsdesc_entry(got_offset);
  1791. }
  1792. }
  1793. // Create a GOT entry for the TLS module index.
  1794. template<int size>
  1795. unsigned int
  1796. Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  1797. Sized_relobj_file<size, false>* object)
  1798. {
  1799. if (this->got_mod_index_offset_ == -1U)
  1800. {
  1801. gold_assert(symtab != NULL && layout != NULL && object != NULL);
  1802. Reloc_section* rela_dyn = this->rela_dyn_section(layout);
  1803. Output_data_got<64, false>* got = this->got_section(symtab, layout);
  1804. unsigned int got_offset = got->add_constant(0);
  1805. rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
  1806. got_offset, 0);
  1807. got->add_constant(0);
  1808. this->got_mod_index_offset_ = got_offset;
  1809. }
  1810. return this->got_mod_index_offset_;
  1811. }
  1812. // Optimize the TLS relocation type based on what we know about the
  1813. // symbol. IS_FINAL is true if the final address of this symbol is
  1814. // known at link time.
  1815. template<int size>
  1816. tls::Tls_optimization
  1817. Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
  1818. {
  1819. // If we are generating a shared library, then we can't do anything
  1820. // in the linker.
  1821. if (parameters->options().shared())
  1822. return tls::TLSOPT_NONE;
  1823. switch (r_type)
  1824. {
  1825. case elfcpp::R_X86_64_TLSGD:
  1826. case elfcpp::R_X86_64_GOTPC32_TLSDESC:
  1827. case elfcpp::R_X86_64_TLSDESC_CALL:
  1828. // These are General-Dynamic which permits fully general TLS
  1829. // access. Since we know that we are generating an executable,
  1830. // we can convert this to Initial-Exec. If we also know that
  1831. // this is a local symbol, we can further switch to Local-Exec.
  1832. if (is_final)
  1833. return tls::TLSOPT_TO_LE;
  1834. return tls::TLSOPT_TO_IE;
  1835. case elfcpp::R_X86_64_TLSLD:
  1836. // This is Local-Dynamic, which refers to a local symbol in the
  1837. // dynamic TLS block. Since we know that we generating an
  1838. // executable, we can switch to Local-Exec.
  1839. return tls::TLSOPT_TO_LE;
  1840. case elfcpp::R_X86_64_DTPOFF32:
  1841. case elfcpp::R_X86_64_DTPOFF64:
  1842. // Another Local-Dynamic reloc.
  1843. return tls::TLSOPT_TO_LE;
  1844. case elfcpp::R_X86_64_GOTTPOFF:
  1845. // These are Initial-Exec relocs which get the thread offset
  1846. // from the GOT. If we know that we are linking against the
  1847. // local symbol, we can switch to Local-Exec, which links the
  1848. // thread offset into the instruction.
  1849. if (is_final)
  1850. return tls::TLSOPT_TO_LE;
  1851. return tls::TLSOPT_NONE;
  1852. case elfcpp::R_X86_64_TPOFF32:
  1853. // When we already have Local-Exec, there is nothing further we
  1854. // can do.
  1855. return tls::TLSOPT_NONE;
  1856. default:
  1857. gold_unreachable();
  1858. }
  1859. }
  1860. // Get the Reference_flags for a particular relocation.
  1861. template<int size>
  1862. int
  1863. Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
  1864. {
  1865. switch (r_type)
  1866. {
  1867. case elfcpp::R_X86_64_NONE:
  1868. case elfcpp::R_X86_64_GNU_VTINHERIT:
  1869. case elfcpp::R_X86_64_GNU_VTENTRY:
  1870. case elfcpp::R_X86_64_GOTPC32:
  1871. case elfcpp::R_X86_64_GOTPC64:
  1872. // No symbol reference.
  1873. return 0;
  1874. case elfcpp::R_X86_64_64:
  1875. case elfcpp::R_X86_64_32:
  1876. case elfcpp::R_X86_64_32S:
  1877. case elfcpp::R_X86_64_16:
  1878. case elfcpp::R_X86_64_8:
  1879. return Symbol::ABSOLUTE_REF;
  1880. case elfcpp::R_X86_64_PC64:
  1881. case elfcpp::R_X86_64_PC32:
  1882. case elfcpp::R_X86_64_PC32_BND:
  1883. case elfcpp::R_X86_64_PC16:
  1884. case elfcpp::R_X86_64_PC8:
  1885. case elfcpp::R_X86_64_GOTOFF64:
  1886. return Symbol::RELATIVE_REF;
  1887. case elfcpp::R_X86_64_PLT32:
  1888. case elfcpp::R_X86_64_PLT32_BND:
  1889. case elfcpp::R_X86_64_PLTOFF64:
  1890. return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  1891. case elfcpp::R_X86_64_GOT64:
  1892. case elfcpp::R_X86_64_GOT32:
  1893. case elfcpp::R_X86_64_GOTPCREL64:
  1894. case elfcpp::R_X86_64_GOTPCREL:
  1895. case elfcpp::R_X86_64_GOTPLT64:
  1896. // Absolute in GOT.
  1897. return Symbol::ABSOLUTE_REF;
  1898. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  1899. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  1900. case elfcpp::R_X86_64_TLSDESC_CALL:
  1901. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  1902. case elfcpp::R_X86_64_DTPOFF32:
  1903. case elfcpp::R_X86_64_DTPOFF64:
  1904. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  1905. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  1906. return Symbol::TLS_REF;
  1907. case elfcpp::R_X86_64_COPY:
  1908. case elfcpp::R_X86_64_GLOB_DAT:
  1909. case elfcpp::R_X86_64_JUMP_SLOT:
  1910. case elfcpp::R_X86_64_RELATIVE:
  1911. case elfcpp::R_X86_64_IRELATIVE:
  1912. case elfcpp::R_X86_64_TPOFF64:
  1913. case elfcpp::R_X86_64_DTPMOD64:
  1914. case elfcpp::R_X86_64_TLSDESC:
  1915. case elfcpp::R_X86_64_SIZE32:
  1916. case elfcpp::R_X86_64_SIZE64:
  1917. default:
  1918. // Not expected. We will give an error later.
  1919. return 0;
  1920. }
  1921. }
  1922. // Report an unsupported relocation against a local symbol.
  1923. template<int size>
  1924. void
  1925. Target_x86_64<size>::Scan::unsupported_reloc_local(
  1926. Sized_relobj_file<size, false>* object,
  1927. unsigned int r_type)
  1928. {
  1929. gold_error(_("%s: unsupported reloc %u against local symbol"),
  1930. object->name().c_str(), r_type);
  1931. }
  1932. // We are about to emit a dynamic relocation of type R_TYPE. If the
  1933. // dynamic linker does not support it, issue an error. The GNU linker
  1934. // only issues a non-PIC error for an allocated read-only section.
  1935. // Here we know the section is allocated, but we don't know that it is
  1936. // read-only. But we check for all the relocation types which the
  1937. // glibc dynamic linker supports, so it seems appropriate to issue an
  1938. // error even if the section is not read-only. If GSYM is not NULL,
  1939. // it is the symbol the relocation is against; if it is NULL, the
  1940. // relocation is against a local symbol.
  1941. template<int size>
  1942. void
  1943. Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
  1944. Symbol* gsym)
  1945. {
  1946. switch (r_type)
  1947. {
  1948. // These are the relocation types supported by glibc for x86_64
  1949. // which should always work.
  1950. case elfcpp::R_X86_64_RELATIVE:
  1951. case elfcpp::R_X86_64_IRELATIVE:
  1952. case elfcpp::R_X86_64_GLOB_DAT:
  1953. case elfcpp::R_X86_64_JUMP_SLOT:
  1954. case elfcpp::R_X86_64_DTPMOD64:
  1955. case elfcpp::R_X86_64_DTPOFF64:
  1956. case elfcpp::R_X86_64_TPOFF64:
  1957. case elfcpp::R_X86_64_64:
  1958. case elfcpp::R_X86_64_COPY:
  1959. return;
  1960. // glibc supports these reloc types, but they can overflow.
  1961. case elfcpp::R_X86_64_PC32:
  1962. case elfcpp::R_X86_64_PC32_BND:
  1963. // A PC relative reference is OK against a local symbol or if
  1964. // the symbol is defined locally.
  1965. if (gsym == NULL
  1966. || (!gsym->is_from_dynobj()
  1967. && !gsym->is_undefined()
  1968. && !gsym->is_preemptible()))
  1969. return;
  1970. /* Fall through. */
  1971. case elfcpp::R_X86_64_32:
  1972. // R_X86_64_32 is OK for x32.
  1973. if (size == 32 && r_type == elfcpp::R_X86_64_32)
  1974. return;
  1975. if (this->issued_non_pic_error_)
  1976. return;
  1977. gold_assert(parameters->options().output_is_position_independent());
  1978. if (gsym == NULL)
  1979. object->error(_("requires dynamic R_X86_64_32 reloc which may "
  1980. "overflow at runtime; recompile with -fPIC"));
  1981. else
  1982. {
  1983. const char *r_name;
  1984. switch (r_type)
  1985. {
  1986. case elfcpp::R_X86_64_32:
  1987. r_name = "R_X86_64_32";
  1988. break;
  1989. case elfcpp::R_X86_64_PC32:
  1990. r_name = "R_X86_64_PC32";
  1991. break;
  1992. case elfcpp::R_X86_64_PC32_BND:
  1993. r_name = "R_X86_64_PC32_BND";
  1994. break;
  1995. default:
  1996. gold_unreachable();
  1997. break;
  1998. }
  1999. object->error(_("requires dynamic %s reloc against '%s' "
  2000. "which may overflow at runtime; recompile "
  2001. "with -fPIC"),
  2002. r_name, gsym->name());
  2003. }
  2004. this->issued_non_pic_error_ = true;
  2005. return;
  2006. default:
  2007. // This prevents us from issuing more than one error per reloc
  2008. // section. But we can still wind up issuing more than one
  2009. // error per object file.
  2010. if (this->issued_non_pic_error_)
  2011. return;
  2012. gold_assert(parameters->options().output_is_position_independent());
  2013. object->error(_("requires unsupported dynamic reloc %u; "
  2014. "recompile with -fPIC"),
  2015. r_type);
  2016. this->issued_non_pic_error_ = true;
  2017. return;
  2018. case elfcpp::R_X86_64_NONE:
  2019. gold_unreachable();
  2020. }
  2021. }
  2022. // Return whether we need to make a PLT entry for a relocation of the
  2023. // given type against a STT_GNU_IFUNC symbol.
  2024. template<int size>
  2025. bool
  2026. Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
  2027. Sized_relobj_file<size, false>* object,
  2028. unsigned int r_type)
  2029. {
  2030. int flags = Scan::get_reference_flags(r_type);
  2031. if (flags & Symbol::TLS_REF)
  2032. gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
  2033. object->name().c_str(), r_type);
  2034. return flags != 0;
  2035. }
  2036. // Scan a relocation for a local symbol.
  2037. template<int size>
  2038. inline void
  2039. Target_x86_64<size>::Scan::local(Symbol_table* symtab,
  2040. Layout* layout,
  2041. Target_x86_64<size>* target,
  2042. Sized_relobj_file<size, false>* object,
  2043. unsigned int data_shndx,
  2044. Output_section* output_section,
  2045. const elfcpp::Rela<size, false>& reloc,
  2046. unsigned int r_type,
  2047. const elfcpp::Sym<size, false>& lsym,
  2048. bool is_discarded)
  2049. {
  2050. if (is_discarded)
  2051. return;
  2052. // A local STT_GNU_IFUNC symbol may require a PLT entry.
  2053. bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
  2054. if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
  2055. {
  2056. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2057. target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
  2058. }
  2059. switch (r_type)
  2060. {
  2061. case elfcpp::R_X86_64_NONE:
  2062. case elfcpp::R_X86_64_GNU_VTINHERIT:
  2063. case elfcpp::R_X86_64_GNU_VTENTRY:
  2064. break;
  2065. case elfcpp::R_X86_64_64:
  2066. // If building a shared library (or a position-independent
  2067. // executable), we need to create a dynamic relocation for this
  2068. // location. The relocation applied at link time will apply the
  2069. // link-time value, so we flag the location with an
  2070. // R_X86_64_RELATIVE relocation so the dynamic loader can
  2071. // relocate it easily.
  2072. if (parameters->options().output_is_position_independent())
  2073. {
  2074. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2075. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2076. rela_dyn->add_local_relative(object, r_sym,
  2077. (size == 32
  2078. ? elfcpp::R_X86_64_RELATIVE64
  2079. : elfcpp::R_X86_64_RELATIVE),
  2080. output_section, data_shndx,
  2081. reloc.get_r_offset(),
  2082. reloc.get_r_addend(), is_ifunc);
  2083. }
  2084. break;
  2085. case elfcpp::R_X86_64_32:
  2086. case elfcpp::R_X86_64_32S:
  2087. case elfcpp::R_X86_64_16:
  2088. case elfcpp::R_X86_64_8:
  2089. // If building a shared library (or a position-independent
  2090. // executable), we need to create a dynamic relocation for this
  2091. // location. We can't use an R_X86_64_RELATIVE relocation
  2092. // because that is always a 64-bit relocation.
  2093. if (parameters->options().output_is_position_independent())
  2094. {
  2095. // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
  2096. if (size == 32 && r_type == elfcpp::R_X86_64_32)
  2097. {
  2098. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2099. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2100. rela_dyn->add_local_relative(object, r_sym,
  2101. elfcpp::R_X86_64_RELATIVE,
  2102. output_section, data_shndx,
  2103. reloc.get_r_offset(),
  2104. reloc.get_r_addend(), is_ifunc);
  2105. break;
  2106. }
  2107. this->check_non_pic(object, r_type, NULL);
  2108. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2109. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2110. if (lsym.get_st_type() != elfcpp::STT_SECTION)
  2111. rela_dyn->add_local(object, r_sym, r_type, output_section,
  2112. data_shndx, reloc.get_r_offset(),
  2113. reloc.get_r_addend());
  2114. else
  2115. {
  2116. gold_assert(lsym.get_st_value() == 0);
  2117. unsigned int shndx = lsym.get_st_shndx();
  2118. bool is_ordinary;
  2119. shndx = object->adjust_sym_shndx(r_sym, shndx,
  2120. &is_ordinary);
  2121. if (!is_ordinary)
  2122. object->error(_("section symbol %u has bad shndx %u"),
  2123. r_sym, shndx);
  2124. else
  2125. rela_dyn->add_local_section(object, shndx,
  2126. r_type, output_section,
  2127. data_shndx, reloc.get_r_offset(),
  2128. reloc.get_r_addend());
  2129. }
  2130. }
  2131. break;
  2132. case elfcpp::R_X86_64_PC64:
  2133. case elfcpp::R_X86_64_PC32:
  2134. case elfcpp::R_X86_64_PC32_BND:
  2135. case elfcpp::R_X86_64_PC16:
  2136. case elfcpp::R_X86_64_PC8:
  2137. break;
  2138. case elfcpp::R_X86_64_PLT32:
  2139. case elfcpp::R_X86_64_PLT32_BND:
  2140. // Since we know this is a local symbol, we can handle this as a
  2141. // PC32 reloc.
  2142. break;
  2143. case elfcpp::R_X86_64_GOTPC32:
  2144. case elfcpp::R_X86_64_GOTOFF64:
  2145. case elfcpp::R_X86_64_GOTPC64:
  2146. case elfcpp::R_X86_64_PLTOFF64:
  2147. // We need a GOT section.
  2148. target->got_section(symtab, layout);
  2149. // For PLTOFF64, we'd normally want a PLT section, but since we
  2150. // know this is a local symbol, no PLT is needed.
  2151. break;
  2152. case elfcpp::R_X86_64_GOT64:
  2153. case elfcpp::R_X86_64_GOT32:
  2154. case elfcpp::R_X86_64_GOTPCREL64:
  2155. case elfcpp::R_X86_64_GOTPCREL:
  2156. case elfcpp::R_X86_64_GOTPLT64:
  2157. {
  2158. // The symbol requires a GOT section.
  2159. Output_data_got<64, false>* got = target->got_section(symtab, layout);
  2160. // If the relocation symbol isn't IFUNC,
  2161. // and is local, then we will convert
  2162. // mov foo@GOTPCREL(%rip), %reg
  2163. // to lea foo(%rip), %reg.
  2164. // in Relocate::relocate.
  2165. if (r_type == elfcpp::R_X86_64_GOTPCREL
  2166. && reloc.get_r_offset() >= 2
  2167. && !is_ifunc)
  2168. {
  2169. section_size_type stype;
  2170. const unsigned char* view = object->section_contents(data_shndx,
  2171. &stype, true);
  2172. if (view[reloc.get_r_offset() - 2] == 0x8b)
  2173. break;
  2174. }
  2175. // The symbol requires a GOT entry.
  2176. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2177. // For a STT_GNU_IFUNC symbol we want the PLT offset. That
  2178. // lets function pointers compare correctly with shared
  2179. // libraries. Otherwise we would need an IRELATIVE reloc.
  2180. bool is_new;
  2181. if (is_ifunc)
  2182. is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
  2183. else
  2184. is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
  2185. if (is_new)
  2186. {
  2187. // If we are generating a shared object, we need to add a
  2188. // dynamic relocation for this symbol's GOT entry.
  2189. if (parameters->options().output_is_position_independent())
  2190. {
  2191. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2192. // R_X86_64_RELATIVE assumes a 64-bit relocation.
  2193. if (r_type != elfcpp::R_X86_64_GOT32)
  2194. {
  2195. unsigned int got_offset =
  2196. object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
  2197. rela_dyn->add_local_relative(object, r_sym,
  2198. elfcpp::R_X86_64_RELATIVE,
  2199. got, got_offset, 0, is_ifunc);
  2200. }
  2201. else
  2202. {
  2203. this->check_non_pic(object, r_type, NULL);
  2204. gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
  2205. rela_dyn->add_local(
  2206. object, r_sym, r_type, got,
  2207. object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
  2208. }
  2209. }
  2210. }
  2211. // For GOTPLT64, we'd normally want a PLT section, but since
  2212. // we know this is a local symbol, no PLT is needed.
  2213. }
  2214. break;
  2215. case elfcpp::R_X86_64_COPY:
  2216. case elfcpp::R_X86_64_GLOB_DAT:
  2217. case elfcpp::R_X86_64_JUMP_SLOT:
  2218. case elfcpp::R_X86_64_RELATIVE:
  2219. case elfcpp::R_X86_64_IRELATIVE:
  2220. // These are outstanding tls relocs, which are unexpected when linking
  2221. case elfcpp::R_X86_64_TPOFF64:
  2222. case elfcpp::R_X86_64_DTPMOD64:
  2223. case elfcpp::R_X86_64_TLSDESC:
  2224. gold_error(_("%s: unexpected reloc %u in object file"),
  2225. object->name().c_str(), r_type);
  2226. break;
  2227. // These are initial tls relocs, which are expected when linking
  2228. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  2229. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  2230. case elfcpp::R_X86_64_TLSDESC_CALL:
  2231. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  2232. case elfcpp::R_X86_64_DTPOFF32:
  2233. case elfcpp::R_X86_64_DTPOFF64:
  2234. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  2235. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  2236. {
  2237. bool output_is_shared = parameters->options().shared();
  2238. const tls::Tls_optimization optimized_type
  2239. = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
  2240. r_type);
  2241. switch (r_type)
  2242. {
  2243. case elfcpp::R_X86_64_TLSGD: // General-dynamic
  2244. if (optimized_type == tls::TLSOPT_NONE)
  2245. {
  2246. // Create a pair of GOT entries for the module index and
  2247. // dtv-relative offset.
  2248. Output_data_got<64, false>* got
  2249. = target->got_section(symtab, layout);
  2250. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2251. unsigned int shndx = lsym.get_st_shndx();
  2252. bool is_ordinary;
  2253. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  2254. if (!is_ordinary)
  2255. object->error(_("local symbol %u has bad shndx %u"),
  2256. r_sym, shndx);
  2257. else
  2258. got->add_local_pair_with_rel(object, r_sym,
  2259. shndx,
  2260. GOT_TYPE_TLS_PAIR,
  2261. target->rela_dyn_section(layout),
  2262. elfcpp::R_X86_64_DTPMOD64);
  2263. }
  2264. else if (optimized_type != tls::TLSOPT_TO_LE)
  2265. unsupported_reloc_local(object, r_type);
  2266. break;
  2267. case elfcpp::R_X86_64_GOTPC32_TLSDESC:
  2268. target->define_tls_base_symbol(symtab, layout);
  2269. if (optimized_type == tls::TLSOPT_NONE)
  2270. {
  2271. // Create reserved PLT and GOT entries for the resolver.
  2272. target->reserve_tlsdesc_entries(symtab, layout);
  2273. // Generate a double GOT entry with an
  2274. // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
  2275. // is resolved lazily, so the GOT entry needs to be in
  2276. // an area in .got.plt, not .got. Call got_section to
  2277. // make sure the section has been created.
  2278. target->got_section(symtab, layout);
  2279. Output_data_got<64, false>* got = target->got_tlsdesc_section();
  2280. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2281. if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
  2282. {
  2283. unsigned int got_offset = got->add_constant(0);
  2284. got->add_constant(0);
  2285. object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
  2286. got_offset);
  2287. Reloc_section* rt = target->rela_tlsdesc_section(layout);
  2288. // We store the arguments we need in a vector, and
  2289. // use the index into the vector as the parameter
  2290. // to pass to the target specific routines.
  2291. uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
  2292. void* arg = reinterpret_cast<void*>(intarg);
  2293. rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
  2294. got, got_offset, 0);
  2295. }
  2296. }
  2297. else if (optimized_type != tls::TLSOPT_TO_LE)
  2298. unsupported_reloc_local(object, r_type);
  2299. break;
  2300. case elfcpp::R_X86_64_TLSDESC_CALL:
  2301. break;
  2302. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  2303. if (optimized_type == tls::TLSOPT_NONE)
  2304. {
  2305. // Create a GOT entry for the module index.
  2306. target->got_mod_index_entry(symtab, layout, object);
  2307. }
  2308. else if (optimized_type != tls::TLSOPT_TO_LE)
  2309. unsupported_reloc_local(object, r_type);
  2310. break;
  2311. case elfcpp::R_X86_64_DTPOFF32:
  2312. case elfcpp::R_X86_64_DTPOFF64:
  2313. break;
  2314. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  2315. layout->set_has_static_tls();
  2316. if (optimized_type == tls::TLSOPT_NONE)
  2317. {
  2318. // Create a GOT entry for the tp-relative offset.
  2319. Output_data_got<64, false>* got
  2320. = target->got_section(symtab, layout);
  2321. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2322. got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
  2323. target->rela_dyn_section(layout),
  2324. elfcpp::R_X86_64_TPOFF64);
  2325. }
  2326. else if (optimized_type != tls::TLSOPT_TO_LE)
  2327. unsupported_reloc_local(object, r_type);
  2328. break;
  2329. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  2330. layout->set_has_static_tls();
  2331. if (output_is_shared)
  2332. unsupported_reloc_local(object, r_type);
  2333. break;
  2334. default:
  2335. gold_unreachable();
  2336. }
  2337. }
  2338. break;
  2339. case elfcpp::R_X86_64_SIZE32:
  2340. case elfcpp::R_X86_64_SIZE64:
  2341. default:
  2342. gold_error(_("%s: unsupported reloc %u against local symbol"),
  2343. object->name().c_str(), r_type);
  2344. break;
  2345. }
  2346. }
  2347. // Report an unsupported relocation against a global symbol.
  2348. template<int size>
  2349. void
  2350. Target_x86_64<size>::Scan::unsupported_reloc_global(
  2351. Sized_relobj_file<size, false>* object,
  2352. unsigned int r_type,
  2353. Symbol* gsym)
  2354. {
  2355. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  2356. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  2357. }
  2358. // Returns true if this relocation type could be that of a function pointer.
  2359. template<int size>
  2360. inline bool
  2361. Target_x86_64<size>::Scan::possible_function_pointer_reloc(unsigned int r_type)
  2362. {
  2363. switch (r_type)
  2364. {
  2365. case elfcpp::R_X86_64_64:
  2366. case elfcpp::R_X86_64_32:
  2367. case elfcpp::R_X86_64_32S:
  2368. case elfcpp::R_X86_64_16:
  2369. case elfcpp::R_X86_64_8:
  2370. case elfcpp::R_X86_64_GOT64:
  2371. case elfcpp::R_X86_64_GOT32:
  2372. case elfcpp::R_X86_64_GOTPCREL64:
  2373. case elfcpp::R_X86_64_GOTPCREL:
  2374. case elfcpp::R_X86_64_GOTPLT64:
  2375. {
  2376. return true;
  2377. }
  2378. }
  2379. return false;
  2380. }
  2381. // For safe ICF, scan a relocation for a local symbol to check if it
  2382. // corresponds to a function pointer being taken. In that case mark
  2383. // the function whose pointer was taken as not foldable.
  2384. template<int size>
  2385. inline bool
  2386. Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
  2387. Symbol_table* ,
  2388. Layout* ,
  2389. Target_x86_64<size>* ,
  2390. Sized_relobj_file<size, false>* ,
  2391. unsigned int ,
  2392. Output_section* ,
  2393. const elfcpp::Rela<size, false>& ,
  2394. unsigned int r_type,
  2395. const elfcpp::Sym<size, false>&)
  2396. {
  2397. // When building a shared library, do not fold any local symbols as it is
  2398. // not possible to distinguish pointer taken versus a call by looking at
  2399. // the relocation types.
  2400. return (parameters->options().shared()
  2401. || possible_function_pointer_reloc(r_type));
  2402. }
  2403. // For safe ICF, scan a relocation for a global symbol to check if it
  2404. // corresponds to a function pointer being taken. In that case mark
  2405. // the function whose pointer was taken as not foldable.
  2406. template<int size>
  2407. inline bool
  2408. Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
  2409. Symbol_table*,
  2410. Layout* ,
  2411. Target_x86_64<size>* ,
  2412. Sized_relobj_file<size, false>* ,
  2413. unsigned int ,
  2414. Output_section* ,
  2415. const elfcpp::Rela<size, false>& ,
  2416. unsigned int r_type,
  2417. Symbol* gsym)
  2418. {
  2419. // When building a shared library, do not fold symbols whose visibility
  2420. // is hidden, internal or protected.
  2421. return ((parameters->options().shared()
  2422. && (gsym->visibility() == elfcpp::STV_INTERNAL
  2423. || gsym->visibility() == elfcpp::STV_PROTECTED
  2424. || gsym->visibility() == elfcpp::STV_HIDDEN))
  2425. || possible_function_pointer_reloc(r_type));
  2426. }
  2427. // Scan a relocation for a global symbol.
  2428. template<int size>
  2429. inline void
  2430. Target_x86_64<size>::Scan::global(Symbol_table* symtab,
  2431. Layout* layout,
  2432. Target_x86_64<size>* target,
  2433. Sized_relobj_file<size, false>* object,
  2434. unsigned int data_shndx,
  2435. Output_section* output_section,
  2436. const elfcpp::Rela<size, false>& reloc,
  2437. unsigned int r_type,
  2438. Symbol* gsym)
  2439. {
  2440. // A STT_GNU_IFUNC symbol may require a PLT entry.
  2441. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  2442. && this->reloc_needs_plt_for_ifunc(object, r_type))
  2443. target->make_plt_entry(symtab, layout, gsym);
  2444. switch (r_type)
  2445. {
  2446. case elfcpp::R_X86_64_NONE:
  2447. case elfcpp::R_X86_64_GNU_VTINHERIT:
  2448. case elfcpp::R_X86_64_GNU_VTENTRY:
  2449. break;
  2450. case elfcpp::R_X86_64_64:
  2451. case elfcpp::R_X86_64_32:
  2452. case elfcpp::R_X86_64_32S:
  2453. case elfcpp::R_X86_64_16:
  2454. case elfcpp::R_X86_64_8:
  2455. {
  2456. // Make a PLT entry if necessary.
  2457. if (gsym->needs_plt_entry())
  2458. {
  2459. target->make_plt_entry(symtab, layout, gsym);
  2460. // Since this is not a PC-relative relocation, we may be
  2461. // taking the address of a function. In that case we need to
  2462. // set the entry in the dynamic symbol table to the address of
  2463. // the PLT entry.
  2464. if (gsym->is_from_dynobj() && !parameters->options().shared())
  2465. gsym->set_needs_dynsym_value();
  2466. }
  2467. // Make a dynamic relocation if necessary.
  2468. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  2469. {
  2470. if (!parameters->options().output_is_position_independent()
  2471. && gsym->may_need_copy_reloc())
  2472. {
  2473. target->copy_reloc(symtab, layout, object,
  2474. data_shndx, output_section, gsym, reloc);
  2475. }
  2476. else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
  2477. || (size == 32 && r_type == elfcpp::R_X86_64_32))
  2478. && gsym->type() == elfcpp::STT_GNU_IFUNC
  2479. && gsym->can_use_relative_reloc(false)
  2480. && !gsym->is_from_dynobj()
  2481. && !gsym->is_undefined()
  2482. && !gsym->is_preemptible())
  2483. {
  2484. // Use an IRELATIVE reloc for a locally defined
  2485. // STT_GNU_IFUNC symbol. This makes a function
  2486. // address in a PIE executable match the address in a
  2487. // shared library that it links against.
  2488. Reloc_section* rela_dyn =
  2489. target->rela_irelative_section(layout);
  2490. unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
  2491. rela_dyn->add_symbolless_global_addend(gsym, r_type,
  2492. output_section, object,
  2493. data_shndx,
  2494. reloc.get_r_offset(),
  2495. reloc.get_r_addend());
  2496. }
  2497. else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
  2498. || (size == 32 && r_type == elfcpp::R_X86_64_32))
  2499. && gsym->can_use_relative_reloc(false))
  2500. {
  2501. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2502. rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
  2503. output_section, object,
  2504. data_shndx,
  2505. reloc.get_r_offset(),
  2506. reloc.get_r_addend(), false);
  2507. }
  2508. else
  2509. {
  2510. this->check_non_pic(object, r_type, gsym);
  2511. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2512. rela_dyn->add_global(gsym, r_type, output_section, object,
  2513. data_shndx, reloc.get_r_offset(),
  2514. reloc.get_r_addend());
  2515. }
  2516. }
  2517. }
  2518. break;
  2519. case elfcpp::R_X86_64_PC64:
  2520. case elfcpp::R_X86_64_PC32:
  2521. case elfcpp::R_X86_64_PC32_BND:
  2522. case elfcpp::R_X86_64_PC16:
  2523. case elfcpp::R_X86_64_PC8:
  2524. {
  2525. // Make a PLT entry if necessary.
  2526. if (gsym->needs_plt_entry())
  2527. target->make_plt_entry(symtab, layout, gsym);
  2528. // Make a dynamic relocation if necessary.
  2529. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  2530. {
  2531. if (parameters->options().output_is_executable()
  2532. && gsym->may_need_copy_reloc())
  2533. {
  2534. target->copy_reloc(symtab, layout, object,
  2535. data_shndx, output_section, gsym, reloc);
  2536. }
  2537. else
  2538. {
  2539. this->check_non_pic(object, r_type, gsym);
  2540. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2541. rela_dyn->add_global(gsym, r_type, output_section, object,
  2542. data_shndx, reloc.get_r_offset(),
  2543. reloc.get_r_addend());
  2544. }
  2545. }
  2546. }
  2547. break;
  2548. case elfcpp::R_X86_64_GOT64:
  2549. case elfcpp::R_X86_64_GOT32:
  2550. case elfcpp::R_X86_64_GOTPCREL64:
  2551. case elfcpp::R_X86_64_GOTPCREL:
  2552. case elfcpp::R_X86_64_GOTPLT64:
  2553. {
  2554. // The symbol requires a GOT entry.
  2555. Output_data_got<64, false>* got = target->got_section(symtab, layout);
  2556. // If we convert this from
  2557. // mov foo@GOTPCREL(%rip), %reg
  2558. // to lea foo(%rip), %reg.
  2559. // in Relocate::relocate, then there is nothing to do here.
  2560. if (r_type == elfcpp::R_X86_64_GOTPCREL
  2561. && reloc.get_r_offset() >= 2
  2562. && Target_x86_64<size>::can_convert_mov_to_lea(gsym))
  2563. {
  2564. section_size_type stype;
  2565. const unsigned char* view = object->section_contents(data_shndx,
  2566. &stype, true);
  2567. if (view[reloc.get_r_offset() - 2] == 0x8b)
  2568. break;
  2569. }
  2570. if (gsym->final_value_is_known())
  2571. {
  2572. // For a STT_GNU_IFUNC symbol we want the PLT address.
  2573. if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  2574. got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  2575. else
  2576. got->add_global(gsym, GOT_TYPE_STANDARD);
  2577. }
  2578. else
  2579. {
  2580. // If this symbol is not fully resolved, we need to add a
  2581. // dynamic relocation for it.
  2582. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2583. // Use a GLOB_DAT rather than a RELATIVE reloc if:
  2584. //
  2585. // 1) The symbol may be defined in some other module.
  2586. //
  2587. // 2) We are building a shared library and this is a
  2588. // protected symbol; using GLOB_DAT means that the dynamic
  2589. // linker can use the address of the PLT in the main
  2590. // executable when appropriate so that function address
  2591. // comparisons work.
  2592. //
  2593. // 3) This is a STT_GNU_IFUNC symbol in position dependent
  2594. // code, again so that function address comparisons work.
  2595. if (gsym->is_from_dynobj()
  2596. || gsym->is_undefined()
  2597. || gsym->is_preemptible()
  2598. || (gsym->visibility() == elfcpp::STV_PROTECTED
  2599. && parameters->options().shared())
  2600. || (gsym->type() == elfcpp::STT_GNU_IFUNC
  2601. && parameters->options().output_is_position_independent()))
  2602. got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
  2603. elfcpp::R_X86_64_GLOB_DAT);
  2604. else
  2605. {
  2606. // For a STT_GNU_IFUNC symbol we want to write the PLT
  2607. // offset into the GOT, so that function pointer
  2608. // comparisons work correctly.
  2609. bool is_new;
  2610. if (gsym->type() != elfcpp::STT_GNU_IFUNC)
  2611. is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
  2612. else
  2613. {
  2614. is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  2615. // Tell the dynamic linker to use the PLT address
  2616. // when resolving relocations.
  2617. if (gsym->is_from_dynobj()
  2618. && !parameters->options().shared())
  2619. gsym->set_needs_dynsym_value();
  2620. }
  2621. if (is_new)
  2622. {
  2623. unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
  2624. rela_dyn->add_global_relative(gsym,
  2625. elfcpp::R_X86_64_RELATIVE,
  2626. got, got_off, 0, false);
  2627. }
  2628. }
  2629. }
  2630. }
  2631. break;
  2632. case elfcpp::R_X86_64_PLT32:
  2633. case elfcpp::R_X86_64_PLT32_BND:
  2634. // If the symbol is fully resolved, this is just a PC32 reloc.
  2635. // Otherwise we need a PLT entry.
  2636. if (gsym->final_value_is_known())
  2637. break;
  2638. // If building a shared library, we can also skip the PLT entry
  2639. // if the symbol is defined in the output file and is protected
  2640. // or hidden.
  2641. if (gsym->is_defined()
  2642. && !gsym->is_from_dynobj()
  2643. && !gsym->is_preemptible())
  2644. break;
  2645. target->make_plt_entry(symtab, layout, gsym);
  2646. break;
  2647. case elfcpp::R_X86_64_GOTPC32:
  2648. case elfcpp::R_X86_64_GOTOFF64:
  2649. case elfcpp::R_X86_64_GOTPC64:
  2650. case elfcpp::R_X86_64_PLTOFF64:
  2651. // We need a GOT section.
  2652. target->got_section(symtab, layout);
  2653. // For PLTOFF64, we also need a PLT entry (but only if the
  2654. // symbol is not fully resolved).
  2655. if (r_type == elfcpp::R_X86_64_PLTOFF64
  2656. && !gsym->final_value_is_known())
  2657. target->make_plt_entry(symtab, layout, gsym);
  2658. break;
  2659. case elfcpp::R_X86_64_COPY:
  2660. case elfcpp::R_X86_64_GLOB_DAT:
  2661. case elfcpp::R_X86_64_JUMP_SLOT:
  2662. case elfcpp::R_X86_64_RELATIVE:
  2663. case elfcpp::R_X86_64_IRELATIVE:
  2664. // These are outstanding tls relocs, which are unexpected when linking
  2665. case elfcpp::R_X86_64_TPOFF64:
  2666. case elfcpp::R_X86_64_DTPMOD64:
  2667. case elfcpp::R_X86_64_TLSDESC:
  2668. gold_error(_("%s: unexpected reloc %u in object file"),
  2669. object->name().c_str(), r_type);
  2670. break;
  2671. // These are initial tls relocs, which are expected for global()
  2672. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  2673. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  2674. case elfcpp::R_X86_64_TLSDESC_CALL:
  2675. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  2676. case elfcpp::R_X86_64_DTPOFF32:
  2677. case elfcpp::R_X86_64_DTPOFF64:
  2678. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  2679. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  2680. {
  2681. // For the Initial-Exec model, we can treat undef symbols as final
  2682. // when building an executable.
  2683. const bool is_final = (gsym->final_value_is_known() ||
  2684. (r_type == elfcpp::R_X86_64_GOTTPOFF &&
  2685. gsym->is_undefined() &&
  2686. parameters->options().output_is_executable()));
  2687. const tls::Tls_optimization optimized_type
  2688. = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
  2689. switch (r_type)
  2690. {
  2691. case elfcpp::R_X86_64_TLSGD: // General-dynamic
  2692. if (optimized_type == tls::TLSOPT_NONE)
  2693. {
  2694. // Create a pair of GOT entries for the module index and
  2695. // dtv-relative offset.
  2696. Output_data_got<64, false>* got
  2697. = target->got_section(symtab, layout);
  2698. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
  2699. target->rela_dyn_section(layout),
  2700. elfcpp::R_X86_64_DTPMOD64,
  2701. elfcpp::R_X86_64_DTPOFF64);
  2702. }
  2703. else if (optimized_type == tls::TLSOPT_TO_IE)
  2704. {
  2705. // Create a GOT entry for the tp-relative offset.
  2706. Output_data_got<64, false>* got
  2707. = target->got_section(symtab, layout);
  2708. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  2709. target->rela_dyn_section(layout),
  2710. elfcpp::R_X86_64_TPOFF64);
  2711. }
  2712. else if (optimized_type != tls::TLSOPT_TO_LE)
  2713. unsupported_reloc_global(object, r_type, gsym);
  2714. break;
  2715. case elfcpp::R_X86_64_GOTPC32_TLSDESC:
  2716. target->define_tls_base_symbol(symtab, layout);
  2717. if (optimized_type == tls::TLSOPT_NONE)
  2718. {
  2719. // Create reserved PLT and GOT entries for the resolver.
  2720. target->reserve_tlsdesc_entries(symtab, layout);
  2721. // Create a double GOT entry with an R_X86_64_TLSDESC
  2722. // reloc. The R_X86_64_TLSDESC reloc is resolved
  2723. // lazily, so the GOT entry needs to be in an area in
  2724. // .got.plt, not .got. Call got_section to make sure
  2725. // the section has been created.
  2726. target->got_section(symtab, layout);
  2727. Output_data_got<64, false>* got = target->got_tlsdesc_section();
  2728. Reloc_section* rt = target->rela_tlsdesc_section(layout);
  2729. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
  2730. elfcpp::R_X86_64_TLSDESC, 0);
  2731. }
  2732. else if (optimized_type == tls::TLSOPT_TO_IE)
  2733. {
  2734. // Create a GOT entry for the tp-relative offset.
  2735. Output_data_got<64, false>* got
  2736. = target->got_section(symtab, layout);
  2737. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  2738. target->rela_dyn_section(layout),
  2739. elfcpp::R_X86_64_TPOFF64);
  2740. }
  2741. else if (optimized_type != tls::TLSOPT_TO_LE)
  2742. unsupported_reloc_global(object, r_type, gsym);
  2743. break;
  2744. case elfcpp::R_X86_64_TLSDESC_CALL:
  2745. break;
  2746. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  2747. if (optimized_type == tls::TLSOPT_NONE)
  2748. {
  2749. // Create a GOT entry for the module index.
  2750. target->got_mod_index_entry(symtab, layout, object);
  2751. }
  2752. else if (optimized_type != tls::TLSOPT_TO_LE)
  2753. unsupported_reloc_global(object, r_type, gsym);
  2754. break;
  2755. case elfcpp::R_X86_64_DTPOFF32:
  2756. case elfcpp::R_X86_64_DTPOFF64:
  2757. break;
  2758. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  2759. layout->set_has_static_tls();
  2760. if (optimized_type == tls::TLSOPT_NONE)
  2761. {
  2762. // Create a GOT entry for the tp-relative offset.
  2763. Output_data_got<64, false>* got
  2764. = target->got_section(symtab, layout);
  2765. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  2766. target->rela_dyn_section(layout),
  2767. elfcpp::R_X86_64_TPOFF64);
  2768. }
  2769. else if (optimized_type != tls::TLSOPT_TO_LE)
  2770. unsupported_reloc_global(object, r_type, gsym);
  2771. break;
  2772. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  2773. layout->set_has_static_tls();
  2774. if (parameters->options().shared())
  2775. unsupported_reloc_global(object, r_type, gsym);
  2776. break;
  2777. default:
  2778. gold_unreachable();
  2779. }
  2780. }
  2781. break;
  2782. case elfcpp::R_X86_64_SIZE32:
  2783. case elfcpp::R_X86_64_SIZE64:
  2784. default:
  2785. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  2786. object->name().c_str(), r_type,
  2787. gsym->demangled_name().c_str());
  2788. break;
  2789. }
  2790. }
  2791. template<int size>
  2792. void
  2793. Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
  2794. Layout* layout,
  2795. Sized_relobj_file<size, false>* object,
  2796. unsigned int data_shndx,
  2797. unsigned int sh_type,
  2798. const unsigned char* prelocs,
  2799. size_t reloc_count,
  2800. Output_section* output_section,
  2801. bool needs_special_offset_handling,
  2802. size_t local_symbol_count,
  2803. const unsigned char* plocal_symbols)
  2804. {
  2805. if (sh_type == elfcpp::SHT_REL)
  2806. {
  2807. return;
  2808. }
  2809. gold::gc_process_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
  2810. typename Target_x86_64<size>::Scan,
  2811. typename Target_x86_64<size>::Relocatable_size_for_reloc>(
  2812. symtab,
  2813. layout,
  2814. this,
  2815. object,
  2816. data_shndx,
  2817. prelocs,
  2818. reloc_count,
  2819. output_section,
  2820. needs_special_offset_handling,
  2821. local_symbol_count,
  2822. plocal_symbols);
  2823. }
  2824. // Scan relocations for a section.
  2825. template<int size>
  2826. void
  2827. Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
  2828. Layout* layout,
  2829. Sized_relobj_file<size, false>* object,
  2830. unsigned int data_shndx,
  2831. unsigned int sh_type,
  2832. const unsigned char* prelocs,
  2833. size_t reloc_count,
  2834. Output_section* output_section,
  2835. bool needs_special_offset_handling,
  2836. size_t local_symbol_count,
  2837. const unsigned char* plocal_symbols)
  2838. {
  2839. if (sh_type == elfcpp::SHT_REL)
  2840. {
  2841. gold_error(_("%s: unsupported REL reloc section"),
  2842. object->name().c_str());
  2843. return;
  2844. }
  2845. gold::scan_relocs<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
  2846. typename Target_x86_64<size>::Scan>(
  2847. symtab,
  2848. layout,
  2849. this,
  2850. object,
  2851. data_shndx,
  2852. prelocs,
  2853. reloc_count,
  2854. output_section,
  2855. needs_special_offset_handling,
  2856. local_symbol_count,
  2857. plocal_symbols);
  2858. }
  2859. // Finalize the sections.
  2860. template<int size>
  2861. void
  2862. Target_x86_64<size>::do_finalize_sections(
  2863. Layout* layout,
  2864. const Input_objects*,
  2865. Symbol_table* symtab)
  2866. {
  2867. const Reloc_section* rel_plt = (this->plt_ == NULL
  2868. ? NULL
  2869. : this->plt_->rela_plt());
  2870. layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
  2871. this->rela_dyn_, true, false);
  2872. // Fill in some more dynamic tags.
  2873. Output_data_dynamic* const odyn = layout->dynamic_data();
  2874. if (odyn != NULL)
  2875. {
  2876. if (this->plt_ != NULL
  2877. && this->plt_->output_section() != NULL
  2878. && this->plt_->has_tlsdesc_entry())
  2879. {
  2880. unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
  2881. unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
  2882. this->got_->finalize_data_size();
  2883. odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
  2884. this->plt_, plt_offset);
  2885. odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
  2886. this->got_, got_offset);
  2887. }
  2888. }
  2889. // Emit any relocs we saved in an attempt to avoid generating COPY
  2890. // relocs.
  2891. if (this->copy_relocs_.any_saved_relocs())
  2892. this->copy_relocs_.emit(this->rela_dyn_section(layout));
  2893. // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
  2894. // the .got.plt section.
  2895. Symbol* sym = this->global_offset_table_;
  2896. if (sym != NULL)
  2897. {
  2898. uint64_t data_size = this->got_plt_->current_data_size();
  2899. symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
  2900. }
  2901. if (parameters->doing_static_link()
  2902. && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
  2903. {
  2904. // If linking statically, make sure that the __rela_iplt symbols
  2905. // were defined if necessary, even if we didn't create a PLT.
  2906. static const Define_symbol_in_segment syms[] =
  2907. {
  2908. {
  2909. "__rela_iplt_start", // name
  2910. elfcpp::PT_LOAD, // segment_type
  2911. elfcpp::PF_W, // segment_flags_set
  2912. elfcpp::PF(0), // segment_flags_clear
  2913. 0, // value
  2914. 0, // size
  2915. elfcpp::STT_NOTYPE, // type
  2916. elfcpp::STB_GLOBAL, // binding
  2917. elfcpp::STV_HIDDEN, // visibility
  2918. 0, // nonvis
  2919. Symbol::SEGMENT_START, // offset_from_base
  2920. true // only_if_ref
  2921. },
  2922. {
  2923. "__rela_iplt_end", // name
  2924. elfcpp::PT_LOAD, // segment_type
  2925. elfcpp::PF_W, // segment_flags_set
  2926. elfcpp::PF(0), // segment_flags_clear
  2927. 0, // value
  2928. 0, // size
  2929. elfcpp::STT_NOTYPE, // type
  2930. elfcpp::STB_GLOBAL, // binding
  2931. elfcpp::STV_HIDDEN, // visibility
  2932. 0, // nonvis
  2933. Symbol::SEGMENT_START, // offset_from_base
  2934. true // only_if_ref
  2935. }
  2936. };
  2937. symtab->define_symbols(layout, 2, syms,
  2938. layout->script_options()->saw_sections_clause());
  2939. }
  2940. }
  2941. // Perform a relocation.
  2942. template<int size>
  2943. inline bool
  2944. Target_x86_64<size>::Relocate::relocate(
  2945. const Relocate_info<size, false>* relinfo,
  2946. Target_x86_64<size>* target,
  2947. Output_section*,
  2948. size_t relnum,
  2949. const elfcpp::Rela<size, false>& rela,
  2950. unsigned int r_type,
  2951. const Sized_symbol<size>* gsym,
  2952. const Symbol_value<size>* psymval,
  2953. unsigned char* view,
  2954. typename elfcpp::Elf_types<size>::Elf_Addr address,
  2955. section_size_type view_size)
  2956. {
  2957. if (this->skip_call_tls_get_addr_)
  2958. {
  2959. if ((r_type != elfcpp::R_X86_64_PLT32
  2960. && r_type != elfcpp::R_X86_64_PLT32_BND
  2961. && r_type != elfcpp::R_X86_64_PC32_BND
  2962. && r_type != elfcpp::R_X86_64_PC32)
  2963. || gsym == NULL
  2964. || strcmp(gsym->name(), "__tls_get_addr") != 0)
  2965. {
  2966. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  2967. _("missing expected TLS relocation"));
  2968. }
  2969. else
  2970. {
  2971. this->skip_call_tls_get_addr_ = false;
  2972. return false;
  2973. }
  2974. }
  2975. if (view == NULL)
  2976. return true;
  2977. const Sized_relobj_file<size, false>* object = relinfo->object;
  2978. // Pick the value to use for symbols defined in the PLT.
  2979. Symbol_value<size> symval;
  2980. if (gsym != NULL
  2981. && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
  2982. {
  2983. symval.set_output_value(target->plt_address_for_global(gsym));
  2984. psymval = &symval;
  2985. }
  2986. else if (gsym == NULL && psymval->is_ifunc_symbol())
  2987. {
  2988. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  2989. if (object->local_has_plt_offset(r_sym))
  2990. {
  2991. symval.set_output_value(target->plt_address_for_local(object, r_sym));
  2992. psymval = &symval;
  2993. }
  2994. }
  2995. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  2996. // Get the GOT offset if needed.
  2997. // The GOT pointer points to the end of the GOT section.
  2998. // We need to subtract the size of the GOT section to get
  2999. // the actual offset to use in the relocation.
  3000. bool have_got_offset = false;
  3001. // Since the actual offset is always negative, we use signed int to
  3002. // support 64-bit GOT relocations.
  3003. int got_offset = 0;
  3004. switch (r_type)
  3005. {
  3006. case elfcpp::R_X86_64_GOT32:
  3007. case elfcpp::R_X86_64_GOT64:
  3008. case elfcpp::R_X86_64_GOTPLT64:
  3009. case elfcpp::R_X86_64_GOTPCREL64:
  3010. if (gsym != NULL)
  3011. {
  3012. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  3013. got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
  3014. }
  3015. else
  3016. {
  3017. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  3018. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
  3019. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
  3020. - target->got_size());
  3021. }
  3022. have_got_offset = true;
  3023. break;
  3024. default:
  3025. break;
  3026. }
  3027. switch (r_type)
  3028. {
  3029. case elfcpp::R_X86_64_NONE:
  3030. case elfcpp::R_X86_64_GNU_VTINHERIT:
  3031. case elfcpp::R_X86_64_GNU_VTENTRY:
  3032. break;
  3033. case elfcpp::R_X86_64_64:
  3034. Relocate_functions<size, false>::rela64(view, object, psymval, addend);
  3035. break;
  3036. case elfcpp::R_X86_64_PC64:
  3037. Relocate_functions<size, false>::pcrela64(view, object, psymval, addend,
  3038. address);
  3039. break;
  3040. case elfcpp::R_X86_64_32:
  3041. // FIXME: we need to verify that value + addend fits into 32 bits:
  3042. // uint64_t x = value + addend;
  3043. // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
  3044. // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
  3045. Relocate_functions<size, false>::rela32(view, object, psymval, addend);
  3046. break;
  3047. case elfcpp::R_X86_64_32S:
  3048. // FIXME: we need to verify that value + addend fits into 32 bits:
  3049. // int64_t x = value + addend; // note this quantity is signed!
  3050. // x == static_cast<int64_t>(static_cast<int32_t>(x))
  3051. Relocate_functions<size, false>::rela32(view, object, psymval, addend);
  3052. break;
  3053. case elfcpp::R_X86_64_PC32:
  3054. case elfcpp::R_X86_64_PC32_BND:
  3055. Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
  3056. address);
  3057. break;
  3058. case elfcpp::R_X86_64_16:
  3059. Relocate_functions<size, false>::rela16(view, object, psymval, addend);
  3060. break;
  3061. case elfcpp::R_X86_64_PC16:
  3062. Relocate_functions<size, false>::pcrela16(view, object, psymval, addend,
  3063. address);
  3064. break;
  3065. case elfcpp::R_X86_64_8:
  3066. Relocate_functions<size, false>::rela8(view, object, psymval, addend);
  3067. break;
  3068. case elfcpp::R_X86_64_PC8:
  3069. Relocate_functions<size, false>::pcrela8(view, object, psymval, addend,
  3070. address);
  3071. break;
  3072. case elfcpp::R_X86_64_PLT32:
  3073. case elfcpp::R_X86_64_PLT32_BND:
  3074. gold_assert(gsym == NULL
  3075. || gsym->has_plt_offset()
  3076. || gsym->final_value_is_known()
  3077. || (gsym->is_defined()
  3078. && !gsym->is_from_dynobj()
  3079. && !gsym->is_preemptible()));
  3080. // Note: while this code looks the same as for R_X86_64_PC32, it
  3081. // behaves differently because psymval was set to point to
  3082. // the PLT entry, rather than the symbol, in Scan::global().
  3083. Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
  3084. address);
  3085. break;
  3086. case elfcpp::R_X86_64_PLTOFF64:
  3087. {
  3088. gold_assert(gsym);
  3089. gold_assert(gsym->has_plt_offset()
  3090. || gsym->final_value_is_known());
  3091. typename elfcpp::Elf_types<size>::Elf_Addr got_address;
  3092. // This is the address of GLOBAL_OFFSET_TABLE.
  3093. got_address = target->got_plt_section()->address();
  3094. Relocate_functions<size, false>::rela64(view, object, psymval,
  3095. addend - got_address);
  3096. }
  3097. break;
  3098. case elfcpp::R_X86_64_GOT32:
  3099. gold_assert(have_got_offset);
  3100. Relocate_functions<size, false>::rela32(view, got_offset, addend);
  3101. break;
  3102. case elfcpp::R_X86_64_GOTPC32:
  3103. {
  3104. gold_assert(gsym);
  3105. typename elfcpp::Elf_types<size>::Elf_Addr value;
  3106. value = target->got_plt_section()->address();
  3107. Relocate_functions<size, false>::pcrela32(view, value, addend, address);
  3108. }
  3109. break;
  3110. case elfcpp::R_X86_64_GOT64:
  3111. case elfcpp::R_X86_64_GOTPLT64:
  3112. // R_X86_64_GOTPLT64 is obsolete and treated the the same as
  3113. // GOT64.
  3114. gold_assert(have_got_offset);
  3115. Relocate_functions<size, false>::rela64(view, got_offset, addend);
  3116. break;
  3117. case elfcpp::R_X86_64_GOTPC64:
  3118. {
  3119. gold_assert(gsym);
  3120. typename elfcpp::Elf_types<size>::Elf_Addr value;
  3121. value = target->got_plt_section()->address();
  3122. Relocate_functions<size, false>::pcrela64(view, value, addend, address);
  3123. }
  3124. break;
  3125. case elfcpp::R_X86_64_GOTOFF64:
  3126. {
  3127. typename elfcpp::Elf_types<size>::Elf_Addr value;
  3128. value = (psymval->value(object, 0)
  3129. - target->got_plt_section()->address());
  3130. Relocate_functions<size, false>::rela64(view, value, addend);
  3131. }
  3132. break;
  3133. case elfcpp::R_X86_64_GOTPCREL:
  3134. {
  3135. // Convert
  3136. // mov foo@GOTPCREL(%rip), %reg
  3137. // to lea foo(%rip), %reg.
  3138. // if possible.
  3139. if (rela.get_r_offset() >= 2
  3140. && view[-2] == 0x8b
  3141. && ((gsym == NULL && !psymval->is_ifunc_symbol())
  3142. || (gsym != NULL
  3143. && Target_x86_64<size>::can_convert_mov_to_lea(gsym))))
  3144. {
  3145. view[-2] = 0x8d;
  3146. Relocate_functions<size, false>::pcrela32(view, object, psymval, addend,
  3147. address);
  3148. }
  3149. else
  3150. {
  3151. if (gsym != NULL)
  3152. {
  3153. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  3154. got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
  3155. }
  3156. else
  3157. {
  3158. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  3159. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
  3160. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
  3161. - target->got_size());
  3162. }
  3163. typename elfcpp::Elf_types<size>::Elf_Addr value;
  3164. value = target->got_plt_section()->address() + got_offset;
  3165. Relocate_functions<size, false>::pcrela32(view, value, addend, address);
  3166. }
  3167. }
  3168. break;
  3169. case elfcpp::R_X86_64_GOTPCREL64:
  3170. {
  3171. gold_assert(have_got_offset);
  3172. typename elfcpp::Elf_types<size>::Elf_Addr value;
  3173. value = target->got_plt_section()->address() + got_offset;
  3174. Relocate_functions<size, false>::pcrela64(view, value, addend, address);
  3175. }
  3176. break;
  3177. case elfcpp::R_X86_64_COPY:
  3178. case elfcpp::R_X86_64_GLOB_DAT:
  3179. case elfcpp::R_X86_64_JUMP_SLOT:
  3180. case elfcpp::R_X86_64_RELATIVE:
  3181. case elfcpp::R_X86_64_IRELATIVE:
  3182. // These are outstanding tls relocs, which are unexpected when linking
  3183. case elfcpp::R_X86_64_TPOFF64:
  3184. case elfcpp::R_X86_64_DTPMOD64:
  3185. case elfcpp::R_X86_64_TLSDESC:
  3186. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3187. _("unexpected reloc %u in object file"),
  3188. r_type);
  3189. break;
  3190. // These are initial tls relocs, which are expected when linking
  3191. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  3192. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  3193. case elfcpp::R_X86_64_TLSDESC_CALL:
  3194. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  3195. case elfcpp::R_X86_64_DTPOFF32:
  3196. case elfcpp::R_X86_64_DTPOFF64:
  3197. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  3198. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  3199. this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
  3200. view, address, view_size);
  3201. break;
  3202. case elfcpp::R_X86_64_SIZE32:
  3203. case elfcpp::R_X86_64_SIZE64:
  3204. default:
  3205. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3206. _("unsupported reloc %u"),
  3207. r_type);
  3208. break;
  3209. }
  3210. return true;
  3211. }
  3212. // Perform a TLS relocation.
  3213. template<int size>
  3214. inline void
  3215. Target_x86_64<size>::Relocate::relocate_tls(
  3216. const Relocate_info<size, false>* relinfo,
  3217. Target_x86_64<size>* target,
  3218. size_t relnum,
  3219. const elfcpp::Rela<size, false>& rela,
  3220. unsigned int r_type,
  3221. const Sized_symbol<size>* gsym,
  3222. const Symbol_value<size>* psymval,
  3223. unsigned char* view,
  3224. typename elfcpp::Elf_types<size>::Elf_Addr address,
  3225. section_size_type view_size)
  3226. {
  3227. Output_segment* tls_segment = relinfo->layout->tls_segment();
  3228. const Sized_relobj_file<size, false>* object = relinfo->object;
  3229. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  3230. elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
  3231. bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
  3232. typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
  3233. const bool is_final = (gsym == NULL
  3234. ? !parameters->options().shared()
  3235. : gsym->final_value_is_known());
  3236. tls::Tls_optimization optimized_type
  3237. = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
  3238. switch (r_type)
  3239. {
  3240. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  3241. if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
  3242. {
  3243. // If this code sequence is used in a non-executable section,
  3244. // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
  3245. // on the assumption that it's being used by itself in a debug
  3246. // section. Therefore, in the unlikely event that the code
  3247. // sequence appears in a non-executable section, we simply
  3248. // leave it unoptimized.
  3249. optimized_type = tls::TLSOPT_NONE;
  3250. }
  3251. if (optimized_type == tls::TLSOPT_TO_LE)
  3252. {
  3253. if (tls_segment == NULL)
  3254. {
  3255. gold_assert(parameters->errors()->error_count() > 0
  3256. || issue_undefined_symbol_error(gsym));
  3257. return;
  3258. }
  3259. this->tls_gd_to_le(relinfo, relnum, tls_segment,
  3260. rela, r_type, value, view,
  3261. view_size);
  3262. break;
  3263. }
  3264. else
  3265. {
  3266. unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
  3267. ? GOT_TYPE_TLS_OFFSET
  3268. : GOT_TYPE_TLS_PAIR);
  3269. unsigned int got_offset;
  3270. if (gsym != NULL)
  3271. {
  3272. gold_assert(gsym->has_got_offset(got_type));
  3273. got_offset = gsym->got_offset(got_type) - target->got_size();
  3274. }
  3275. else
  3276. {
  3277. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  3278. gold_assert(object->local_has_got_offset(r_sym, got_type));
  3279. got_offset = (object->local_got_offset(r_sym, got_type)
  3280. - target->got_size());
  3281. }
  3282. if (optimized_type == tls::TLSOPT_TO_IE)
  3283. {
  3284. value = target->got_plt_section()->address() + got_offset;
  3285. this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
  3286. value, view, address, view_size);
  3287. break;
  3288. }
  3289. else if (optimized_type == tls::TLSOPT_NONE)
  3290. {
  3291. // Relocate the field with the offset of the pair of GOT
  3292. // entries.
  3293. value = target->got_plt_section()->address() + got_offset;
  3294. Relocate_functions<size, false>::pcrela32(view, value, addend,
  3295. address);
  3296. break;
  3297. }
  3298. }
  3299. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3300. _("unsupported reloc %u"), r_type);
  3301. break;
  3302. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  3303. case elfcpp::R_X86_64_TLSDESC_CALL:
  3304. if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
  3305. {
  3306. // See above comment for R_X86_64_TLSGD.
  3307. optimized_type = tls::TLSOPT_NONE;
  3308. }
  3309. if (optimized_type == tls::TLSOPT_TO_LE)
  3310. {
  3311. if (tls_segment == NULL)
  3312. {
  3313. gold_assert(parameters->errors()->error_count() > 0
  3314. || issue_undefined_symbol_error(gsym));
  3315. return;
  3316. }
  3317. this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
  3318. rela, r_type, value, view,
  3319. view_size);
  3320. break;
  3321. }
  3322. else
  3323. {
  3324. unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
  3325. ? GOT_TYPE_TLS_OFFSET
  3326. : GOT_TYPE_TLS_DESC);
  3327. unsigned int got_offset = 0;
  3328. if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
  3329. && optimized_type == tls::TLSOPT_NONE)
  3330. {
  3331. // We created GOT entries in the .got.tlsdesc portion of
  3332. // the .got.plt section, but the offset stored in the
  3333. // symbol is the offset within .got.tlsdesc.
  3334. got_offset = (target->got_size()
  3335. + target->got_plt_section()->data_size());
  3336. }
  3337. if (gsym != NULL)
  3338. {
  3339. gold_assert(gsym->has_got_offset(got_type));
  3340. got_offset += gsym->got_offset(got_type) - target->got_size();
  3341. }
  3342. else
  3343. {
  3344. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  3345. gold_assert(object->local_has_got_offset(r_sym, got_type));
  3346. got_offset += (object->local_got_offset(r_sym, got_type)
  3347. - target->got_size());
  3348. }
  3349. if (optimized_type == tls::TLSOPT_TO_IE)
  3350. {
  3351. if (tls_segment == NULL)
  3352. {
  3353. gold_assert(parameters->errors()->error_count() > 0
  3354. || issue_undefined_symbol_error(gsym));
  3355. return;
  3356. }
  3357. value = target->got_plt_section()->address() + got_offset;
  3358. this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
  3359. rela, r_type, value, view, address,
  3360. view_size);
  3361. break;
  3362. }
  3363. else if (optimized_type == tls::TLSOPT_NONE)
  3364. {
  3365. if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
  3366. {
  3367. // Relocate the field with the offset of the pair of GOT
  3368. // entries.
  3369. value = target->got_plt_section()->address() + got_offset;
  3370. Relocate_functions<size, false>::pcrela32(view, value, addend,
  3371. address);
  3372. }
  3373. break;
  3374. }
  3375. }
  3376. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3377. _("unsupported reloc %u"), r_type);
  3378. break;
  3379. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  3380. if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
  3381. {
  3382. // See above comment for R_X86_64_TLSGD.
  3383. optimized_type = tls::TLSOPT_NONE;
  3384. }
  3385. if (optimized_type == tls::TLSOPT_TO_LE)
  3386. {
  3387. if (tls_segment == NULL)
  3388. {
  3389. gold_assert(parameters->errors()->error_count() > 0
  3390. || issue_undefined_symbol_error(gsym));
  3391. return;
  3392. }
  3393. this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
  3394. value, view, view_size);
  3395. break;
  3396. }
  3397. else if (optimized_type == tls::TLSOPT_NONE)
  3398. {
  3399. // Relocate the field with the offset of the GOT entry for
  3400. // the module index.
  3401. unsigned int got_offset;
  3402. got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
  3403. - target->got_size());
  3404. value = target->got_plt_section()->address() + got_offset;
  3405. Relocate_functions<size, false>::pcrela32(view, value, addend,
  3406. address);
  3407. break;
  3408. }
  3409. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3410. _("unsupported reloc %u"), r_type);
  3411. break;
  3412. case elfcpp::R_X86_64_DTPOFF32:
  3413. // This relocation type is used in debugging information.
  3414. // In that case we need to not optimize the value. If the
  3415. // section is not executable, then we assume we should not
  3416. // optimize this reloc. See comments above for R_X86_64_TLSGD,
  3417. // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
  3418. // R_X86_64_TLSLD.
  3419. if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
  3420. {
  3421. if (tls_segment == NULL)
  3422. {
  3423. gold_assert(parameters->errors()->error_count() > 0
  3424. || issue_undefined_symbol_error(gsym));
  3425. return;
  3426. }
  3427. value -= tls_segment->memsz();
  3428. }
  3429. Relocate_functions<size, false>::rela32(view, value, addend);
  3430. break;
  3431. case elfcpp::R_X86_64_DTPOFF64:
  3432. // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
  3433. if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
  3434. {
  3435. if (tls_segment == NULL)
  3436. {
  3437. gold_assert(parameters->errors()->error_count() > 0
  3438. || issue_undefined_symbol_error(gsym));
  3439. return;
  3440. }
  3441. value -= tls_segment->memsz();
  3442. }
  3443. Relocate_functions<size, false>::rela64(view, value, addend);
  3444. break;
  3445. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  3446. if (gsym != NULL
  3447. && gsym->is_undefined()
  3448. && parameters->options().output_is_executable())
  3449. {
  3450. Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
  3451. NULL, rela,
  3452. r_type, value, view,
  3453. view_size);
  3454. break;
  3455. }
  3456. else if (optimized_type == tls::TLSOPT_TO_LE)
  3457. {
  3458. if (tls_segment == NULL)
  3459. {
  3460. gold_assert(parameters->errors()->error_count() > 0
  3461. || issue_undefined_symbol_error(gsym));
  3462. return;
  3463. }
  3464. Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
  3465. tls_segment, rela,
  3466. r_type, value, view,
  3467. view_size);
  3468. break;
  3469. }
  3470. else if (optimized_type == tls::TLSOPT_NONE)
  3471. {
  3472. // Relocate the field with the offset of the GOT entry for
  3473. // the tp-relative offset of the symbol.
  3474. unsigned int got_offset;
  3475. if (gsym != NULL)
  3476. {
  3477. gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
  3478. got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
  3479. - target->got_size());
  3480. }
  3481. else
  3482. {
  3483. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  3484. gold_assert(object->local_has_got_offset(r_sym,
  3485. GOT_TYPE_TLS_OFFSET));
  3486. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
  3487. - target->got_size());
  3488. }
  3489. value = target->got_plt_section()->address() + got_offset;
  3490. Relocate_functions<size, false>::pcrela32(view, value, addend,
  3491. address);
  3492. break;
  3493. }
  3494. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3495. _("unsupported reloc type %u"),
  3496. r_type);
  3497. break;
  3498. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  3499. if (tls_segment == NULL)
  3500. {
  3501. gold_assert(parameters->errors()->error_count() > 0
  3502. || issue_undefined_symbol_error(gsym));
  3503. return;
  3504. }
  3505. value -= tls_segment->memsz();
  3506. Relocate_functions<size, false>::rela32(view, value, addend);
  3507. break;
  3508. }
  3509. }
  3510. // Do a relocation in which we convert a TLS General-Dynamic to an
  3511. // Initial-Exec.
  3512. template<int size>
  3513. inline void
  3514. Target_x86_64<size>::Relocate::tls_gd_to_ie(
  3515. const Relocate_info<size, false>* relinfo,
  3516. size_t relnum,
  3517. Output_segment*,
  3518. const elfcpp::Rela<size, false>& rela,
  3519. unsigned int,
  3520. typename elfcpp::Elf_types<size>::Elf_Addr value,
  3521. unsigned char* view,
  3522. typename elfcpp::Elf_types<size>::Elf_Addr address,
  3523. section_size_type view_size)
  3524. {
  3525. // For SIZE == 64:
  3526. // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
  3527. // .word 0x6666; rex64; call __tls_get_addr
  3528. // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
  3529. // For SIZE == 32:
  3530. // leaq foo@tlsgd(%rip),%rdi;
  3531. // .word 0x6666; rex64; call __tls_get_addr
  3532. // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
  3533. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
  3534. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3535. (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
  3536. if (size == 64)
  3537. {
  3538. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
  3539. -4);
  3540. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3541. (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
  3542. memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
  3543. 16);
  3544. }
  3545. else
  3546. {
  3547. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
  3548. -3);
  3549. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3550. (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
  3551. memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
  3552. 15);
  3553. }
  3554. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  3555. Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
  3556. address);
  3557. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  3558. // We can skip it.
  3559. this->skip_call_tls_get_addr_ = true;
  3560. }
  3561. // Do a relocation in which we convert a TLS General-Dynamic to a
  3562. // Local-Exec.
  3563. template<int size>
  3564. inline void
  3565. Target_x86_64<size>::Relocate::tls_gd_to_le(
  3566. const Relocate_info<size, false>* relinfo,
  3567. size_t relnum,
  3568. Output_segment* tls_segment,
  3569. const elfcpp::Rela<size, false>& rela,
  3570. unsigned int,
  3571. typename elfcpp::Elf_types<size>::Elf_Addr value,
  3572. unsigned char* view,
  3573. section_size_type view_size)
  3574. {
  3575. // For SIZE == 64:
  3576. // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
  3577. // .word 0x6666; rex64; call __tls_get_addr
  3578. // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
  3579. // For SIZE == 32:
  3580. // leaq foo@tlsgd(%rip),%rdi;
  3581. // .word 0x6666; rex64; call __tls_get_addr
  3582. // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
  3583. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
  3584. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3585. (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
  3586. if (size == 64)
  3587. {
  3588. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
  3589. -4);
  3590. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3591. (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
  3592. memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
  3593. 16);
  3594. }
  3595. else
  3596. {
  3597. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
  3598. -3);
  3599. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3600. (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
  3601. memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
  3602. 15);
  3603. }
  3604. value -= tls_segment->memsz();
  3605. Relocate_functions<size, false>::rela32(view + 8, value, 0);
  3606. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  3607. // We can skip it.
  3608. this->skip_call_tls_get_addr_ = true;
  3609. }
  3610. // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
  3611. template<int size>
  3612. inline void
  3613. Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
  3614. const Relocate_info<size, false>* relinfo,
  3615. size_t relnum,
  3616. Output_segment*,
  3617. const elfcpp::Rela<size, false>& rela,
  3618. unsigned int r_type,
  3619. typename elfcpp::Elf_types<size>::Elf_Addr value,
  3620. unsigned char* view,
  3621. typename elfcpp::Elf_types<size>::Elf_Addr address,
  3622. section_size_type view_size)
  3623. {
  3624. if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
  3625. {
  3626. // leaq foo@tlsdesc(%rip), %rax
  3627. // ==> movq foo@gottpoff(%rip), %rax
  3628. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
  3629. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
  3630. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3631. view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
  3632. view[-2] = 0x8b;
  3633. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  3634. Relocate_functions<size, false>::pcrela32(view, value, addend, address);
  3635. }
  3636. else
  3637. {
  3638. // call *foo@tlscall(%rax)
  3639. // ==> nop; nop
  3640. gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
  3641. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
  3642. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3643. view[0] == 0xff && view[1] == 0x10);
  3644. view[0] = 0x66;
  3645. view[1] = 0x90;
  3646. }
  3647. }
  3648. // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
  3649. template<int size>
  3650. inline void
  3651. Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
  3652. const Relocate_info<size, false>* relinfo,
  3653. size_t relnum,
  3654. Output_segment* tls_segment,
  3655. const elfcpp::Rela<size, false>& rela,
  3656. unsigned int r_type,
  3657. typename elfcpp::Elf_types<size>::Elf_Addr value,
  3658. unsigned char* view,
  3659. section_size_type view_size)
  3660. {
  3661. if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
  3662. {
  3663. // leaq foo@tlsdesc(%rip), %rax
  3664. // ==> movq foo@tpoff, %rax
  3665. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
  3666. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
  3667. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3668. view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
  3669. view[-2] = 0xc7;
  3670. view[-1] = 0xc0;
  3671. value -= tls_segment->memsz();
  3672. Relocate_functions<size, false>::rela32(view, value, 0);
  3673. }
  3674. else
  3675. {
  3676. // call *foo@tlscall(%rax)
  3677. // ==> nop; nop
  3678. gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
  3679. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
  3680. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3681. view[0] == 0xff && view[1] == 0x10);
  3682. view[0] = 0x66;
  3683. view[1] = 0x90;
  3684. }
  3685. }
  3686. template<int size>
  3687. inline void
  3688. Target_x86_64<size>::Relocate::tls_ld_to_le(
  3689. const Relocate_info<size, false>* relinfo,
  3690. size_t relnum,
  3691. Output_segment*,
  3692. const elfcpp::Rela<size, false>& rela,
  3693. unsigned int,
  3694. typename elfcpp::Elf_types<size>::Elf_Addr,
  3695. unsigned char* view,
  3696. section_size_type view_size)
  3697. {
  3698. // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
  3699. // For SIZE == 64:
  3700. // ... leq foo@dtpoff(%rax),%reg
  3701. // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
  3702. // For SIZE == 32:
  3703. // ... leq foo@dtpoff(%rax),%reg
  3704. // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
  3705. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
  3706. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
  3707. tls::check_tls(relinfo, relnum, rela.get_r_offset(),
  3708. view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
  3709. tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
  3710. if (size == 64)
  3711. memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
  3712. else
  3713. memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
  3714. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  3715. // We can skip it.
  3716. this->skip_call_tls_get_addr_ = true;
  3717. }
  3718. // Do a relocation in which we convert a TLS Initial-Exec to a
  3719. // Local-Exec.
  3720. template<int size>
  3721. inline void
  3722. Target_x86_64<size>::Relocate::tls_ie_to_le(
  3723. const Relocate_info<size, false>* relinfo,
  3724. size_t relnum,
  3725. Output_segment* tls_segment,
  3726. const elfcpp::Rela<size, false>& rela,
  3727. unsigned int,
  3728. typename elfcpp::Elf_types<size>::Elf_Addr value,
  3729. unsigned char* view,
  3730. section_size_type view_size)
  3731. {
  3732. // We need to examine the opcodes to figure out which instruction we
  3733. // are looking at.
  3734. // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
  3735. // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
  3736. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
  3737. tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
  3738. unsigned char op1 = view[-3];
  3739. unsigned char op2 = view[-2];
  3740. unsigned char op3 = view[-1];
  3741. unsigned char reg = op3 >> 3;
  3742. if (op2 == 0x8b)
  3743. {
  3744. // movq
  3745. if (op1 == 0x4c)
  3746. view[-3] = 0x49;
  3747. else if (size == 32 && op1 == 0x44)
  3748. view[-3] = 0x41;
  3749. view[-2] = 0xc7;
  3750. view[-1] = 0xc0 | reg;
  3751. }
  3752. else if (reg == 4)
  3753. {
  3754. // Special handling for %rsp.
  3755. if (op1 == 0x4c)
  3756. view[-3] = 0x49;
  3757. else if (size == 32 && op1 == 0x44)
  3758. view[-3] = 0x41;
  3759. view[-2] = 0x81;
  3760. view[-1] = 0xc0 | reg;
  3761. }
  3762. else
  3763. {
  3764. // addq
  3765. if (op1 == 0x4c)
  3766. view[-3] = 0x4d;
  3767. else if (size == 32 && op1 == 0x44)
  3768. view[-3] = 0x45;
  3769. view[-2] = 0x8d;
  3770. view[-1] = 0x80 | reg | (reg << 3);
  3771. }
  3772. if (tls_segment != NULL)
  3773. value -= tls_segment->memsz();
  3774. Relocate_functions<size, false>::rela32(view, value, 0);
  3775. }
  3776. // Relocate section data.
  3777. template<int size>
  3778. void
  3779. Target_x86_64<size>::relocate_section(
  3780. const Relocate_info<size, false>* relinfo,
  3781. unsigned int sh_type,
  3782. const unsigned char* prelocs,
  3783. size_t reloc_count,
  3784. Output_section* output_section,
  3785. bool needs_special_offset_handling,
  3786. unsigned char* view,
  3787. typename elfcpp::Elf_types<size>::Elf_Addr address,
  3788. section_size_type view_size,
  3789. const Reloc_symbol_changes* reloc_symbol_changes)
  3790. {
  3791. gold_assert(sh_type == elfcpp::SHT_RELA);
  3792. gold::relocate_section<size, false, Target_x86_64<size>, elfcpp::SHT_RELA,
  3793. typename Target_x86_64<size>::Relocate,
  3794. gold::Default_comdat_behavior>(
  3795. relinfo,
  3796. this,
  3797. prelocs,
  3798. reloc_count,
  3799. output_section,
  3800. needs_special_offset_handling,
  3801. view,
  3802. address,
  3803. view_size,
  3804. reloc_symbol_changes);
  3805. }
  3806. // Apply an incremental relocation. Incremental relocations always refer
  3807. // to global symbols.
  3808. template<int size>
  3809. void
  3810. Target_x86_64<size>::apply_relocation(
  3811. const Relocate_info<size, false>* relinfo,
  3812. typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
  3813. unsigned int r_type,
  3814. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
  3815. const Symbol* gsym,
  3816. unsigned char* view,
  3817. typename elfcpp::Elf_types<size>::Elf_Addr address,
  3818. section_size_type view_size)
  3819. {
  3820. gold::apply_relocation<size, false, Target_x86_64<size>,
  3821. typename Target_x86_64<size>::Relocate>(
  3822. relinfo,
  3823. this,
  3824. r_offset,
  3825. r_type,
  3826. r_addend,
  3827. gsym,
  3828. view,
  3829. address,
  3830. view_size);
  3831. }
  3832. // Return the size of a relocation while scanning during a relocatable
  3833. // link.
  3834. template<int size>
  3835. unsigned int
  3836. Target_x86_64<size>::Relocatable_size_for_reloc::get_size_for_reloc(
  3837. unsigned int r_type,
  3838. Relobj* object)
  3839. {
  3840. switch (r_type)
  3841. {
  3842. case elfcpp::R_X86_64_NONE:
  3843. case elfcpp::R_X86_64_GNU_VTINHERIT:
  3844. case elfcpp::R_X86_64_GNU_VTENTRY:
  3845. case elfcpp::R_X86_64_TLSGD: // Global-dynamic
  3846. case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
  3847. case elfcpp::R_X86_64_TLSDESC_CALL:
  3848. case elfcpp::R_X86_64_TLSLD: // Local-dynamic
  3849. case elfcpp::R_X86_64_DTPOFF32:
  3850. case elfcpp::R_X86_64_DTPOFF64:
  3851. case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
  3852. case elfcpp::R_X86_64_TPOFF32: // Local-exec
  3853. return 0;
  3854. case elfcpp::R_X86_64_64:
  3855. case elfcpp::R_X86_64_PC64:
  3856. case elfcpp::R_X86_64_GOTOFF64:
  3857. case elfcpp::R_X86_64_GOTPC64:
  3858. case elfcpp::R_X86_64_PLTOFF64:
  3859. case elfcpp::R_X86_64_GOT64:
  3860. case elfcpp::R_X86_64_GOTPCREL64:
  3861. case elfcpp::R_X86_64_GOTPCREL:
  3862. case elfcpp::R_X86_64_GOTPLT64:
  3863. return 8;
  3864. case elfcpp::R_X86_64_32:
  3865. case elfcpp::R_X86_64_32S:
  3866. case elfcpp::R_X86_64_PC32:
  3867. case elfcpp::R_X86_64_PC32_BND:
  3868. case elfcpp::R_X86_64_PLT32:
  3869. case elfcpp::R_X86_64_PLT32_BND:
  3870. case elfcpp::R_X86_64_GOTPC32:
  3871. case elfcpp::R_X86_64_GOT32:
  3872. return 4;
  3873. case elfcpp::R_X86_64_16:
  3874. case elfcpp::R_X86_64_PC16:
  3875. return 2;
  3876. case elfcpp::R_X86_64_8:
  3877. case elfcpp::R_X86_64_PC8:
  3878. return 1;
  3879. case elfcpp::R_X86_64_COPY:
  3880. case elfcpp::R_X86_64_GLOB_DAT:
  3881. case elfcpp::R_X86_64_JUMP_SLOT:
  3882. case elfcpp::R_X86_64_RELATIVE:
  3883. case elfcpp::R_X86_64_IRELATIVE:
  3884. // These are outstanding tls relocs, which are unexpected when linking
  3885. case elfcpp::R_X86_64_TPOFF64:
  3886. case elfcpp::R_X86_64_DTPMOD64:
  3887. case elfcpp::R_X86_64_TLSDESC:
  3888. object->error(_("unexpected reloc %u in object file"), r_type);
  3889. return 0;
  3890. case elfcpp::R_X86_64_SIZE32:
  3891. case elfcpp::R_X86_64_SIZE64:
  3892. default:
  3893. object->error(_("unsupported reloc %u against local symbol"), r_type);
  3894. return 0;
  3895. }
  3896. }
  3897. // Scan the relocs during a relocatable link.
  3898. template<int size>
  3899. void
  3900. Target_x86_64<size>::scan_relocatable_relocs(
  3901. Symbol_table* symtab,
  3902. Layout* layout,
  3903. Sized_relobj_file<size, false>* object,
  3904. unsigned int data_shndx,
  3905. unsigned int sh_type,
  3906. const unsigned char* prelocs,
  3907. size_t reloc_count,
  3908. Output_section* output_section,
  3909. bool needs_special_offset_handling,
  3910. size_t local_symbol_count,
  3911. const unsigned char* plocal_symbols,
  3912. Relocatable_relocs* rr)
  3913. {
  3914. gold_assert(sh_type == elfcpp::SHT_RELA);
  3915. typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
  3916. Relocatable_size_for_reloc> Scan_relocatable_relocs;
  3917. gold::scan_relocatable_relocs<size, false, elfcpp::SHT_RELA,
  3918. Scan_relocatable_relocs>(
  3919. symtab,
  3920. layout,
  3921. object,
  3922. data_shndx,
  3923. prelocs,
  3924. reloc_count,
  3925. output_section,
  3926. needs_special_offset_handling,
  3927. local_symbol_count,
  3928. plocal_symbols,
  3929. rr);
  3930. }
  3931. // Relocate a section during a relocatable link.
  3932. template<int size>
  3933. void
  3934. Target_x86_64<size>::relocate_relocs(
  3935. const Relocate_info<size, false>* relinfo,
  3936. unsigned int sh_type,
  3937. const unsigned char* prelocs,
  3938. size_t reloc_count,
  3939. Output_section* output_section,
  3940. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  3941. const Relocatable_relocs* rr,
  3942. unsigned char* view,
  3943. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  3944. section_size_type view_size,
  3945. unsigned char* reloc_view,
  3946. section_size_type reloc_view_size)
  3947. {
  3948. gold_assert(sh_type == elfcpp::SHT_RELA);
  3949. gold::relocate_relocs<size, false, elfcpp::SHT_RELA>(
  3950. relinfo,
  3951. prelocs,
  3952. reloc_count,
  3953. output_section,
  3954. offset_in_output_section,
  3955. rr,
  3956. view,
  3957. view_address,
  3958. view_size,
  3959. reloc_view,
  3960. reloc_view_size);
  3961. }
  3962. // Return the value to use for a dynamic which requires special
  3963. // treatment. This is how we support equality comparisons of function
  3964. // pointers across shared library boundaries, as described in the
  3965. // processor specific ABI supplement.
  3966. template<int size>
  3967. uint64_t
  3968. Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
  3969. {
  3970. gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
  3971. return this->plt_address_for_global(gsym);
  3972. }
  3973. // Return a string used to fill a code section with nops to take up
  3974. // the specified length.
  3975. template<int size>
  3976. std::string
  3977. Target_x86_64<size>::do_code_fill(section_size_type length) const
  3978. {
  3979. if (length >= 16)
  3980. {
  3981. // Build a jmpq instruction to skip over the bytes.
  3982. unsigned char jmp[5];
  3983. jmp[0] = 0xe9;
  3984. elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
  3985. return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
  3986. + std::string(length - 5, static_cast<char>(0x90)));
  3987. }
  3988. // Nop sequences of various lengths.
  3989. const char nop1[1] = { '\x90' }; // nop
  3990. const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
  3991. const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
  3992. const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
  3993. '\x00'};
  3994. const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
  3995. '\x00', '\x00' };
  3996. const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
  3997. '\x44', '\x00', '\x00' };
  3998. const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
  3999. '\x00', '\x00', '\x00',
  4000. '\x00' };
  4001. const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
  4002. '\x00', '\x00', '\x00',
  4003. '\x00', '\x00' };
  4004. const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
  4005. '\x84', '\x00', '\x00',
  4006. '\x00', '\x00', '\x00' };
  4007. const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
  4008. '\x1f', '\x84', '\x00',
  4009. '\x00', '\x00', '\x00',
  4010. '\x00' };
  4011. const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
  4012. '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
  4013. '\x00', '\x00', '\x00',
  4014. '\x00', '\x00' };
  4015. const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
  4016. '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
  4017. '\x84', '\x00', '\x00',
  4018. '\x00', '\x00', '\x00' };
  4019. const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
  4020. '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
  4021. '\x1f', '\x84', '\x00',
  4022. '\x00', '\x00', '\x00',
  4023. '\x00' };
  4024. const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
  4025. '\x66', '\x66', '\x2e', // data16
  4026. '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
  4027. '\x00', '\x00', '\x00',
  4028. '\x00', '\x00' };
  4029. const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
  4030. '\x66', '\x66', '\x66', // data16; data16
  4031. '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
  4032. '\x84', '\x00', '\x00',
  4033. '\x00', '\x00', '\x00' };
  4034. const char* nops[16] = {
  4035. NULL,
  4036. nop1, nop2, nop3, nop4, nop5, nop6, nop7,
  4037. nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
  4038. };
  4039. return std::string(nops[length], length);
  4040. }
  4041. // Return the addend to use for a target specific relocation. The
  4042. // only target specific relocation is R_X86_64_TLSDESC for a local
  4043. // symbol. We want to set the addend is the offset of the local
  4044. // symbol in the TLS segment.
  4045. template<int size>
  4046. uint64_t
  4047. Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
  4048. uint64_t) const
  4049. {
  4050. gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
  4051. uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
  4052. gold_assert(intarg < this->tlsdesc_reloc_info_.size());
  4053. const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
  4054. const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
  4055. gold_assert(psymval->is_tls_symbol());
  4056. // The value of a TLS symbol is the offset in the TLS segment.
  4057. return psymval->value(ti.object, 0);
  4058. }
  4059. // Return the value to use for the base of a DW_EH_PE_datarel offset
  4060. // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
  4061. // assembler can not write out the difference between two labels in
  4062. // different sections, so instead of using a pc-relative value they
  4063. // use an offset from the GOT.
  4064. template<int size>
  4065. uint64_t
  4066. Target_x86_64<size>::do_ehframe_datarel_base() const
  4067. {
  4068. gold_assert(this->global_offset_table_ != NULL);
  4069. Symbol* sym = this->global_offset_table_;
  4070. Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
  4071. return ssym->value();
  4072. }
  4073. // FNOFFSET in section SHNDX in OBJECT is the start of a function
  4074. // compiled with -fsplit-stack. The function calls non-split-stack
  4075. // code. We have to change the function so that it always ensures
  4076. // that it has enough stack space to run some random function.
  4077. static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
  4078. static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
  4079. static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
  4080. static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
  4081. static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
  4082. static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
  4083. template<int size>
  4084. void
  4085. Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
  4086. section_offset_type fnoffset,
  4087. section_size_type fnsize,
  4088. unsigned char* view,
  4089. section_size_type view_size,
  4090. std::string* from,
  4091. std::string* to) const
  4092. {
  4093. const char* const cmp_insn = reinterpret_cast<const char*>
  4094. (size == 32 ? cmp_insn_32 : cmp_insn_64);
  4095. const char* const lea_r10_insn = reinterpret_cast<const char*>
  4096. (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
  4097. const char* const lea_r11_insn = reinterpret_cast<const char*>
  4098. (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
  4099. const size_t cmp_insn_len =
  4100. (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
  4101. const size_t lea_r10_insn_len =
  4102. (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
  4103. const size_t lea_r11_insn_len =
  4104. (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
  4105. const size_t nop_len = (size == 32 ? 7 : 8);
  4106. // The function starts with a comparison of the stack pointer and a
  4107. // field in the TCB. This is followed by a jump.
  4108. // cmp %fs:NN,%rsp
  4109. if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
  4110. && fnsize > nop_len + 1)
  4111. {
  4112. // We will call __morestack if the carry flag is set after this
  4113. // comparison. We turn the comparison into an stc instruction
  4114. // and some nops.
  4115. view[fnoffset] = '\xf9';
  4116. this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
  4117. }
  4118. // lea NN(%rsp),%r10
  4119. // lea NN(%rsp),%r11
  4120. else if ((this->match_view(view, view_size, fnoffset,
  4121. lea_r10_insn, lea_r10_insn_len)
  4122. || this->match_view(view, view_size, fnoffset,
  4123. lea_r11_insn, lea_r11_insn_len))
  4124. && fnsize > 8)
  4125. {
  4126. // This is loading an offset from the stack pointer for a
  4127. // comparison. The offset is negative, so we decrease the
  4128. // offset by the amount of space we need for the stack. This
  4129. // means we will avoid calling __morestack if there happens to
  4130. // be plenty of space on the stack already.
  4131. unsigned char* pval = view + fnoffset + 4;
  4132. uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
  4133. val -= parameters->options().split_stack_adjust_size();
  4134. elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
  4135. }
  4136. else
  4137. {
  4138. if (!object->has_no_split_stack())
  4139. object->error(_("failed to match split-stack sequence at "
  4140. "section %u offset %0zx"),
  4141. shndx, static_cast<size_t>(fnoffset));
  4142. return;
  4143. }
  4144. // We have to change the function so that it calls
  4145. // __morestack_non_split instead of __morestack. The former will
  4146. // allocate additional stack space.
  4147. *from = "__morestack";
  4148. *to = "__morestack_non_split";
  4149. }
  4150. // The selector for x86_64 object files. Note this is never instantiated
  4151. // directly. It's only used in Target_selector_x86_64_nacl, below.
  4152. template<int size>
  4153. class Target_selector_x86_64 : public Target_selector_freebsd
  4154. {
  4155. public:
  4156. Target_selector_x86_64()
  4157. : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
  4158. (size == 64
  4159. ? "elf64-x86-64" : "elf32-x86-64"),
  4160. (size == 64
  4161. ? "elf64-x86-64-freebsd"
  4162. : "elf32-x86-64-freebsd"),
  4163. (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
  4164. { }
  4165. Target*
  4166. do_instantiate_target()
  4167. { return new Target_x86_64<size>(); }
  4168. };
  4169. // NaCl variant. It uses different PLT contents.
  4170. template<int size>
  4171. class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
  4172. {
  4173. public:
  4174. Output_data_plt_x86_64_nacl(Layout* layout,
  4175. Output_data_got<64, false>* got,
  4176. Output_data_got_plt_x86_64* got_plt,
  4177. Output_data_space* got_irelative)
  4178. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  4179. got, got_plt, got_irelative)
  4180. { }
  4181. Output_data_plt_x86_64_nacl(Layout* layout,
  4182. Output_data_got<64, false>* got,
  4183. Output_data_got_plt_x86_64* got_plt,
  4184. Output_data_space* got_irelative,
  4185. unsigned int plt_count)
  4186. : Output_data_plt_x86_64<size>(layout, plt_entry_size,
  4187. got, got_plt, got_irelative,
  4188. plt_count)
  4189. { }
  4190. protected:
  4191. virtual unsigned int
  4192. do_get_plt_entry_size() const
  4193. { return plt_entry_size; }
  4194. virtual void
  4195. do_add_eh_frame(Layout* layout)
  4196. {
  4197. layout->add_eh_frame_for_plt(this,
  4198. this->plt_eh_frame_cie,
  4199. this->plt_eh_frame_cie_size,
  4200. plt_eh_frame_fde,
  4201. plt_eh_frame_fde_size);
  4202. }
  4203. virtual void
  4204. do_fill_first_plt_entry(unsigned char* pov,
  4205. typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
  4206. typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
  4207. virtual unsigned int
  4208. do_fill_plt_entry(unsigned char* pov,
  4209. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  4210. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  4211. unsigned int got_offset,
  4212. unsigned int plt_offset,
  4213. unsigned int plt_index);
  4214. virtual void
  4215. do_fill_tlsdesc_entry(unsigned char* pov,
  4216. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  4217. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  4218. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  4219. unsigned int tlsdesc_got_offset,
  4220. unsigned int plt_offset);
  4221. private:
  4222. // The size of an entry in the PLT.
  4223. static const int plt_entry_size = 64;
  4224. // The first entry in the PLT.
  4225. static const unsigned char first_plt_entry[plt_entry_size];
  4226. // Other entries in the PLT for an executable.
  4227. static const unsigned char plt_entry[plt_entry_size];
  4228. // The reserved TLSDESC entry in the PLT for an executable.
  4229. static const unsigned char tlsdesc_plt_entry[plt_entry_size];
  4230. // The .eh_frame unwind information for the PLT.
  4231. static const int plt_eh_frame_fde_size = 32;
  4232. static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
  4233. };
  4234. template<int size>
  4235. class Target_x86_64_nacl : public Target_x86_64<size>
  4236. {
  4237. public:
  4238. Target_x86_64_nacl()
  4239. : Target_x86_64<size>(&x86_64_nacl_info)
  4240. { }
  4241. virtual Output_data_plt_x86_64<size>*
  4242. do_make_data_plt(Layout* layout,
  4243. Output_data_got<64, false>* got,
  4244. Output_data_got_plt_x86_64* got_plt,
  4245. Output_data_space* got_irelative)
  4246. {
  4247. return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
  4248. got_irelative);
  4249. }
  4250. virtual Output_data_plt_x86_64<size>*
  4251. do_make_data_plt(Layout* layout,
  4252. Output_data_got<64, false>* got,
  4253. Output_data_got_plt_x86_64* got_plt,
  4254. Output_data_space* got_irelative,
  4255. unsigned int plt_count)
  4256. {
  4257. return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
  4258. got_irelative,
  4259. plt_count);
  4260. }
  4261. virtual std::string
  4262. do_code_fill(section_size_type length) const;
  4263. private:
  4264. static const Target::Target_info x86_64_nacl_info;
  4265. };
  4266. template<>
  4267. const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
  4268. {
  4269. 64, // size
  4270. false, // is_big_endian
  4271. elfcpp::EM_X86_64, // machine_code
  4272. false, // has_make_symbol
  4273. false, // has_resolve
  4274. true, // has_code_fill
  4275. true, // is_default_stack_executable
  4276. true, // can_icf_inline_merge_sections
  4277. '\0', // wrap_char
  4278. "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
  4279. 0x20000, // default_text_segment_address
  4280. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  4281. 0x10000, // common_pagesize (overridable by -z common-page-size)
  4282. true, // isolate_execinstr
  4283. 0x10000000, // rosegment_gap
  4284. elfcpp::SHN_UNDEF, // small_common_shndx
  4285. elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
  4286. 0, // small_common_section_flags
  4287. elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
  4288. NULL, // attributes_section
  4289. NULL, // attributes_vendor
  4290. "_start" // entry_symbol_name
  4291. };
  4292. template<>
  4293. const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
  4294. {
  4295. 32, // size
  4296. false, // is_big_endian
  4297. elfcpp::EM_X86_64, // machine_code
  4298. false, // has_make_symbol
  4299. false, // has_resolve
  4300. true, // has_code_fill
  4301. true, // is_default_stack_executable
  4302. true, // can_icf_inline_merge_sections
  4303. '\0', // wrap_char
  4304. "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
  4305. 0x20000, // default_text_segment_address
  4306. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  4307. 0x10000, // common_pagesize (overridable by -z common-page-size)
  4308. true, // isolate_execinstr
  4309. 0x10000000, // rosegment_gap
  4310. elfcpp::SHN_UNDEF, // small_common_shndx
  4311. elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
  4312. 0, // small_common_section_flags
  4313. elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
  4314. NULL, // attributes_section
  4315. NULL, // attributes_vendor
  4316. "_start" // entry_symbol_name
  4317. };
  4318. #define NACLMASK 0xe0 // 32-byte alignment mask.
  4319. // The first entry in the PLT.
  4320. template<int size>
  4321. const unsigned char
  4322. Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
  4323. {
  4324. 0xff, 0x35, // pushq contents of memory address
  4325. 0, 0, 0, 0, // replaced with address of .got + 8
  4326. 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
  4327. 0, 0, 0, 0, // replaced with address of .got + 16
  4328. 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
  4329. 0x4d, 0x01, 0xfb, // add %r15, %r11
  4330. 0x41, 0xff, 0xe3, // jmpq *%r11
  4331. // 9-byte nop sequence to pad out to the next 32-byte boundary.
  4332. 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
  4333. // 32 bytes of nop to pad out to the standard size
  4334. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  4335. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  4336. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  4337. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  4338. 0x66, // excess data32 prefix
  4339. 0x90 // nop
  4340. };
  4341. template<int size>
  4342. void
  4343. Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
  4344. unsigned char* pov,
  4345. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  4346. typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
  4347. {
  4348. memcpy(pov, first_plt_entry, plt_entry_size);
  4349. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  4350. (got_address + 8
  4351. - (plt_address + 2 + 4)));
  4352. elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
  4353. (got_address + 16
  4354. - (plt_address + 9 + 4)));
  4355. }
  4356. // Subsequent entries in the PLT.
  4357. template<int size>
  4358. const unsigned char
  4359. Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
  4360. {
  4361. 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
  4362. 0, 0, 0, 0, // replaced with address of symbol in .got
  4363. 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
  4364. 0x4d, 0x01, 0xfb, // add %r15, %r11
  4365. 0x41, 0xff, 0xe3, // jmpq *%r11
  4366. // 15-byte nop sequence to pad out to the next 32-byte boundary.
  4367. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  4368. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  4369. // Lazy GOT entries point here (32-byte aligned).
  4370. 0x68, // pushq immediate
  4371. 0, 0, 0, 0, // replaced with index into relocation table
  4372. 0xe9, // jmp relative
  4373. 0, 0, 0, 0, // replaced with offset to start of .plt0
  4374. // 22 bytes of nop to pad out to the standard size.
  4375. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  4376. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  4377. 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
  4378. };
  4379. template<int size>
  4380. unsigned int
  4381. Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
  4382. unsigned char* pov,
  4383. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  4384. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  4385. unsigned int got_offset,
  4386. unsigned int plt_offset,
  4387. unsigned int plt_index)
  4388. {
  4389. memcpy(pov, plt_entry, plt_entry_size);
  4390. elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
  4391. (got_address + got_offset
  4392. - (plt_address + plt_offset
  4393. + 3 + 4)));
  4394. elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
  4395. elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
  4396. - (plt_offset + 38 + 4));
  4397. return 32;
  4398. }
  4399. // The reserved TLSDESC entry in the PLT.
  4400. template<int size>
  4401. const unsigned char
  4402. Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
  4403. {
  4404. 0xff, 0x35, // pushq x(%rip)
  4405. 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
  4406. 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
  4407. 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
  4408. 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d
  4409. 0x4d, 0x01, 0xfb, // add %r15, %r11
  4410. 0x41, 0xff, 0xe3, // jmpq *%r11
  4411. // 41 bytes of nop to pad out to the standard size.
  4412. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  4413. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  4414. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
  4415. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  4416. 0x66, 0x66, // excess data32 prefixes
  4417. 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
  4418. };
  4419. template<int size>
  4420. void
  4421. Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
  4422. unsigned char* pov,
  4423. typename elfcpp::Elf_types<size>::Elf_Addr got_address,
  4424. typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
  4425. typename elfcpp::Elf_types<size>::Elf_Addr got_base,
  4426. unsigned int tlsdesc_got_offset,
  4427. unsigned int plt_offset)
  4428. {
  4429. memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
  4430. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  4431. (got_address + 8
  4432. - (plt_address + plt_offset
  4433. + 2 + 4)));
  4434. elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
  4435. (got_base
  4436. + tlsdesc_got_offset
  4437. - (plt_address + plt_offset
  4438. + 9 + 4)));
  4439. }
  4440. // The .eh_frame unwind information for the PLT.
  4441. template<int size>
  4442. const unsigned char
  4443. Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
  4444. {
  4445. 0, 0, 0, 0, // Replaced with offset to .plt.
  4446. 0, 0, 0, 0, // Replaced with size of .plt.
  4447. 0, // Augmentation size.
  4448. elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16.
  4449. elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
  4450. elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24.
  4451. elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
  4452. elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
  4453. 13, // Block length.
  4454. elfcpp::DW_OP_breg7, 8, // Push %rsp + 8.
  4455. elfcpp::DW_OP_breg16, 0, // Push %rip.
  4456. elfcpp::DW_OP_const1u, 63, // Push 0x3f.
  4457. elfcpp::DW_OP_and, // & (%rip & 0x3f).
  4458. elfcpp::DW_OP_const1u, 37, // Push 0x25.
  4459. elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25)
  4460. elfcpp::DW_OP_lit3, // Push 3.
  4461. elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3)
  4462. elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
  4463. elfcpp::DW_CFA_nop, // Align to 32 bytes.
  4464. elfcpp::DW_CFA_nop
  4465. };
  4466. // Return a string used to fill a code section with nops.
  4467. // For NaCl, long NOPs are only valid if they do not cross
  4468. // bundle alignment boundaries, so keep it simple with one-byte NOPs.
  4469. template<int size>
  4470. std::string
  4471. Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
  4472. {
  4473. return std::string(length, static_cast<char>(0x90));
  4474. }
  4475. // The selector for x86_64-nacl object files.
  4476. template<int size>
  4477. class Target_selector_x86_64_nacl
  4478. : public Target_selector_nacl<Target_selector_x86_64<size>,
  4479. Target_x86_64_nacl<size> >
  4480. {
  4481. public:
  4482. Target_selector_x86_64_nacl()
  4483. : Target_selector_nacl<Target_selector_x86_64<size>,
  4484. Target_x86_64_nacl<size> >("x86-64",
  4485. size == 64
  4486. ? "elf64-x86-64-nacl"
  4487. : "elf32-x86-64-nacl",
  4488. size == 64
  4489. ? "elf_x86_64_nacl"
  4490. : "elf32_x86_64_nacl")
  4491. { }
  4492. };
  4493. Target_selector_x86_64_nacl<64> target_selector_x86_64;
  4494. Target_selector_x86_64_nacl<32> target_selector_x32;
  4495. } // End anonymous namespace.