s390.cc 147 KB

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