sparc.cc 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536
  1. // sparc.cc -- sparc target support for gold.
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. // Written by David S. Miller <davem@davemloft.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 <cstdlib>
  19. #include <cstdio>
  20. #include <cstring>
  21. #include "elfcpp.h"
  22. #include "parameters.h"
  23. #include "reloc.h"
  24. #include "sparc.h"
  25. #include "object.h"
  26. #include "symtab.h"
  27. #include "layout.h"
  28. #include "output.h"
  29. #include "copy-relocs.h"
  30. #include "target.h"
  31. #include "target-reloc.h"
  32. #include "target-select.h"
  33. #include "tls.h"
  34. #include "errors.h"
  35. #include "gc.h"
  36. namespace
  37. {
  38. using namespace gold;
  39. template<int size, bool big_endian>
  40. class Output_data_plt_sparc;
  41. template<int size, bool big_endian>
  42. class Target_sparc : public Sized_target<size, big_endian>
  43. {
  44. public:
  45. typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
  46. Target_sparc()
  47. : Sized_target<size, big_endian>(&sparc_info),
  48. got_(NULL), plt_(NULL), rela_dyn_(NULL), rela_ifunc_(NULL),
  49. copy_relocs_(elfcpp::R_SPARC_COPY),
  50. got_mod_index_offset_(-1U), tls_get_addr_sym_(NULL),
  51. elf_machine_(sparc_info.machine_code), elf_flags_(0),
  52. elf_flags_set_(false), register_syms_()
  53. {
  54. }
  55. // Make a new symbol table entry.
  56. Sized_symbol<size>*
  57. make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t);
  58. // Process the relocations to determine unreferenced sections for
  59. // garbage collection.
  60. void
  61. gc_process_relocs(Symbol_table* symtab,
  62. Layout* layout,
  63. Sized_relobj_file<size, big_endian>* object,
  64. unsigned int data_shndx,
  65. unsigned int sh_type,
  66. const unsigned char* prelocs,
  67. size_t reloc_count,
  68. Output_section* output_section,
  69. bool needs_special_offset_handling,
  70. size_t local_symbol_count,
  71. const unsigned char* plocal_symbols);
  72. // Scan the relocations to look for symbol adjustments.
  73. void
  74. scan_relocs(Symbol_table* symtab,
  75. Layout* layout,
  76. Sized_relobj_file<size, big_endian>* object,
  77. unsigned int data_shndx,
  78. unsigned int sh_type,
  79. const unsigned char* prelocs,
  80. size_t reloc_count,
  81. Output_section* output_section,
  82. bool needs_special_offset_handling,
  83. size_t local_symbol_count,
  84. const unsigned char* plocal_symbols);
  85. // Finalize the sections.
  86. void
  87. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  88. // Return the value to use for a dynamic which requires special
  89. // treatment.
  90. uint64_t
  91. do_dynsym_value(const Symbol*) const;
  92. // Relocate a section.
  93. void
  94. relocate_section(const Relocate_info<size, big_endian>*,
  95. unsigned int sh_type,
  96. const unsigned char* prelocs,
  97. size_t reloc_count,
  98. Output_section* output_section,
  99. bool needs_special_offset_handling,
  100. unsigned char* view,
  101. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  102. section_size_type view_size,
  103. const Reloc_symbol_changes*);
  104. // Scan the relocs during a relocatable link.
  105. void
  106. scan_relocatable_relocs(Symbol_table* symtab,
  107. Layout* layout,
  108. Sized_relobj_file<size, big_endian>* object,
  109. unsigned int data_shndx,
  110. unsigned int sh_type,
  111. const unsigned char* prelocs,
  112. size_t reloc_count,
  113. Output_section* output_section,
  114. bool needs_special_offset_handling,
  115. size_t local_symbol_count,
  116. const unsigned char* plocal_symbols,
  117. Relocatable_relocs*);
  118. // Scan the relocs for --emit-relocs.
  119. void
  120. emit_relocs_scan(Symbol_table* symtab,
  121. Layout* layout,
  122. Sized_relobj_file<size, big_endian>* object,
  123. unsigned int data_shndx,
  124. unsigned int sh_type,
  125. const unsigned char* prelocs,
  126. size_t reloc_count,
  127. Output_section* output_section,
  128. bool needs_special_offset_handling,
  129. size_t local_symbol_count,
  130. const unsigned char* plocal_syms,
  131. Relocatable_relocs* rr);
  132. // Emit relocations for a section.
  133. void
  134. relocate_relocs(const Relocate_info<size, big_endian>*,
  135. unsigned int sh_type,
  136. const unsigned char* prelocs,
  137. size_t reloc_count,
  138. Output_section* output_section,
  139. typename elfcpp::Elf_types<size>::Elf_Off
  140. offset_in_output_section,
  141. unsigned char* view,
  142. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  143. section_size_type view_size,
  144. unsigned char* reloc_view,
  145. section_size_type reloc_view_size);
  146. // Return whether SYM is defined by the ABI.
  147. bool
  148. do_is_defined_by_abi(const Symbol* sym) const
  149. { return strcmp(sym->name(), "___tls_get_addr") == 0; }
  150. // Return the PLT address to use for a global symbol.
  151. uint64_t
  152. do_plt_address_for_global(const Symbol* gsym) const
  153. { return this->plt_section()->address_for_global(gsym); }
  154. uint64_t
  155. do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
  156. { return this->plt_section()->address_for_local(relobj, symndx); }
  157. // Return whether there is a GOT section.
  158. bool
  159. has_got_section() const
  160. { return this->got_ != NULL; }
  161. // Return the size of the GOT section.
  162. section_size_type
  163. got_size() const
  164. {
  165. gold_assert(this->got_ != NULL);
  166. return this->got_->data_size();
  167. }
  168. // Return the number of entries in the GOT.
  169. unsigned int
  170. got_entry_count() const
  171. {
  172. if (this->got_ == NULL)
  173. return 0;
  174. return this->got_size() / (size / 8);
  175. }
  176. // Return the address of the GOT.
  177. uint64_t
  178. got_address() const
  179. {
  180. if (this->got_ == NULL)
  181. return 0;
  182. return this->got_->address();
  183. }
  184. // Return the number of entries in the PLT.
  185. unsigned int
  186. plt_entry_count() const;
  187. // Return the offset of the first non-reserved PLT entry.
  188. unsigned int
  189. first_plt_entry_offset() const;
  190. // Return the size of each PLT entry.
  191. unsigned int
  192. plt_entry_size() const;
  193. protected:
  194. // Make an ELF object.
  195. Object*
  196. do_make_elf_object(const std::string&, Input_file*, off_t,
  197. const elfcpp::Ehdr<size, big_endian>& ehdr);
  198. void
  199. do_adjust_elf_header(unsigned char* view, int len);
  200. private:
  201. // The class which scans relocations.
  202. class Scan
  203. {
  204. public:
  205. Scan()
  206. : issued_non_pic_error_(false)
  207. { }
  208. static inline int
  209. get_reference_flags(unsigned int r_type);
  210. inline void
  211. local(Symbol_table* symtab, Layout* layout, Target_sparc* target,
  212. Sized_relobj_file<size, big_endian>* object,
  213. unsigned int data_shndx,
  214. Output_section* output_section,
  215. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  216. const elfcpp::Sym<size, big_endian>& lsym,
  217. bool is_discarded);
  218. inline void
  219. global(Symbol_table* symtab, Layout* layout, Target_sparc* target,
  220. Sized_relobj_file<size, big_endian>* object,
  221. unsigned int data_shndx,
  222. Output_section* output_section,
  223. const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
  224. Symbol* gsym);
  225. inline bool
  226. local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
  227. Target_sparc* ,
  228. Sized_relobj_file<size, big_endian>* ,
  229. unsigned int ,
  230. Output_section* ,
  231. const elfcpp::Rela<size, big_endian>& ,
  232. unsigned int ,
  233. const elfcpp::Sym<size, big_endian>&)
  234. { return false; }
  235. inline bool
  236. global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
  237. Target_sparc* ,
  238. Sized_relobj_file<size, big_endian>* ,
  239. unsigned int ,
  240. Output_section* ,
  241. const elfcpp::Rela<size,
  242. big_endian>& ,
  243. unsigned int , Symbol*)
  244. { return false; }
  245. private:
  246. static void
  247. unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
  248. unsigned int r_type);
  249. static void
  250. unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
  251. unsigned int r_type, Symbol*);
  252. static void
  253. generate_tls_call(Symbol_table* symtab, Layout* layout,
  254. Target_sparc* target);
  255. void
  256. check_non_pic(Relobj*, unsigned int r_type);
  257. bool
  258. reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
  259. unsigned int r_type);
  260. // Whether we have issued an error about a non-PIC compilation.
  261. bool issued_non_pic_error_;
  262. };
  263. // The class which implements relocation.
  264. class Relocate
  265. {
  266. public:
  267. Relocate()
  268. : ignore_gd_add_(false), reloc_adjust_addr_(NULL)
  269. { }
  270. ~Relocate()
  271. {
  272. if (this->ignore_gd_add_)
  273. {
  274. // FIXME: This needs to specify the location somehow.
  275. gold_error(_("missing expected TLS relocation"));
  276. }
  277. }
  278. // Do a relocation. Return false if the caller should not issue
  279. // any warnings about this relocation.
  280. inline bool
  281. relocate(const Relocate_info<size, big_endian>*, unsigned int,
  282. Target_sparc*, Output_section*, size_t, const unsigned char*,
  283. const Sized_symbol<size>*, const Symbol_value<size>*,
  284. unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
  285. section_size_type);
  286. private:
  287. // Do a TLS relocation.
  288. inline void
  289. relocate_tls(const Relocate_info<size, big_endian>*, Target_sparc* target,
  290. size_t relnum, const elfcpp::Rela<size, big_endian>&,
  291. unsigned int r_type, const Sized_symbol<size>*,
  292. const Symbol_value<size>*,
  293. unsigned char*,
  294. typename elfcpp::Elf_types<size>::Elf_Addr,
  295. section_size_type);
  296. inline void
  297. relax_call(Target_sparc<size, big_endian>* target,
  298. unsigned char* view,
  299. const elfcpp::Rela<size, big_endian>& rela,
  300. section_size_type view_size);
  301. // Ignore the next relocation which should be R_SPARC_TLS_GD_ADD
  302. bool ignore_gd_add_;
  303. // If we hit a reloc at this view address, adjust it back by 4 bytes.
  304. unsigned char *reloc_adjust_addr_;
  305. };
  306. // Get the GOT section, creating it if necessary.
  307. Output_data_got<size, big_endian>*
  308. got_section(Symbol_table*, Layout*);
  309. // Create the PLT section.
  310. void
  311. make_plt_section(Symbol_table* symtab, Layout* layout);
  312. // Create a PLT entry for a global symbol.
  313. void
  314. make_plt_entry(Symbol_table*, Layout*, Symbol*);
  315. // Create a PLT entry for a local STT_GNU_IFUNC symbol.
  316. void
  317. make_local_ifunc_plt_entry(Symbol_table*, Layout*,
  318. Sized_relobj_file<size, big_endian>* relobj,
  319. unsigned int local_sym_index);
  320. // Create a GOT entry for the TLS module index.
  321. unsigned int
  322. got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  323. Sized_relobj_file<size, big_endian>* object);
  324. // Return the gsym for "__tls_get_addr". Cache if not already
  325. // cached.
  326. Symbol*
  327. tls_get_addr_sym(Symbol_table* symtab)
  328. {
  329. if (!this->tls_get_addr_sym_)
  330. this->tls_get_addr_sym_ = symtab->lookup("__tls_get_addr", NULL);
  331. gold_assert(this->tls_get_addr_sym_);
  332. return this->tls_get_addr_sym_;
  333. }
  334. // Get the PLT section.
  335. Output_data_plt_sparc<size, big_endian>*
  336. plt_section() const
  337. {
  338. gold_assert(this->plt_ != NULL);
  339. return this->plt_;
  340. }
  341. // Get the dynamic reloc section, creating it if necessary.
  342. Reloc_section*
  343. rela_dyn_section(Layout*);
  344. // Get the section to use for IFUNC relocations.
  345. Reloc_section*
  346. rela_ifunc_section(Layout*);
  347. // Copy a relocation against a global symbol.
  348. void
  349. copy_reloc(Symbol_table* symtab, Layout* layout,
  350. Sized_relobj_file<size, big_endian>* object,
  351. unsigned int shndx, Output_section* output_section,
  352. Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
  353. {
  354. unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
  355. this->copy_relocs_.copy_reloc(symtab, layout,
  356. symtab->get_sized_symbol<size>(sym),
  357. object, shndx, output_section,
  358. r_type, reloc.get_r_offset(),
  359. reloc.get_r_addend(),
  360. this->rela_dyn_section(layout));
  361. }
  362. // Information about this specific target which we pass to the
  363. // general Target structure.
  364. static Target::Target_info sparc_info;
  365. // The types of GOT entries needed for this platform.
  366. // These values are exposed to the ABI in an incremental link.
  367. // Do not renumber existing values without changing the version
  368. // number of the .gnu_incremental_inputs section.
  369. enum Got_type
  370. {
  371. GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
  372. GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
  373. GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
  374. };
  375. struct Register_symbol
  376. {
  377. Register_symbol()
  378. : name(NULL), shndx(0), obj(NULL)
  379. { }
  380. const char* name;
  381. unsigned int shndx;
  382. Object* obj;
  383. };
  384. // The GOT section.
  385. Output_data_got<size, big_endian>* got_;
  386. // The PLT section.
  387. Output_data_plt_sparc<size, big_endian>* plt_;
  388. // The dynamic reloc section.
  389. Reloc_section* rela_dyn_;
  390. // The section to use for IFUNC relocs.
  391. Reloc_section* rela_ifunc_;
  392. // Relocs saved to avoid a COPY reloc.
  393. Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
  394. // Offset of the GOT entry for the TLS module index;
  395. unsigned int got_mod_index_offset_;
  396. // Cached pointer to __tls_get_addr symbol
  397. Symbol* tls_get_addr_sym_;
  398. // Accumulated elf machine type
  399. elfcpp::Elf_Half elf_machine_;
  400. // Accumulated elf header flags
  401. elfcpp::Elf_Word elf_flags_;
  402. // Whether elf_flags_ has been set for the first time yet
  403. bool elf_flags_set_;
  404. // STT_SPARC_REGISTER symbols (%g2, %g3, %g6, %g7).
  405. Register_symbol register_syms_[4];
  406. };
  407. template<>
  408. Target::Target_info Target_sparc<32, true>::sparc_info =
  409. {
  410. 32, // size
  411. true, // is_big_endian
  412. elfcpp::EM_SPARC, // machine_code
  413. false, // has_make_symbol
  414. false, // has_resolve
  415. false, // has_code_fill
  416. true, // is_default_stack_executable
  417. false, // can_icf_inline_merge_sections
  418. '\0', // wrap_char
  419. "/usr/lib/ld.so.1", // dynamic_linker
  420. 0x00010000, // default_text_segment_address
  421. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  422. 8 * 1024, // common_pagesize (overridable by -z common-page-size)
  423. false, // isolate_execinstr
  424. 0, // rosegment_gap
  425. elfcpp::SHN_UNDEF, // small_common_shndx
  426. elfcpp::SHN_UNDEF, // large_common_shndx
  427. 0, // small_common_section_flags
  428. 0, // large_common_section_flags
  429. NULL, // attributes_section
  430. NULL, // attributes_vendor
  431. "_start", // entry_symbol_name
  432. 32, // hash_entry_size
  433. elfcpp::SHT_PROGBITS, // unwind_section_type
  434. };
  435. template<>
  436. Target::Target_info Target_sparc<64, true>::sparc_info =
  437. {
  438. 64, // size
  439. true, // is_big_endian
  440. elfcpp::EM_SPARCV9, // machine_code
  441. true, // has_make_symbol
  442. false, // has_resolve
  443. false, // has_code_fill
  444. true, // is_default_stack_executable
  445. false, // can_icf_inline_merge_sections
  446. '\0', // wrap_char
  447. "/usr/lib/sparcv9/ld.so.1", // dynamic_linker
  448. 0x100000, // default_text_segment_address
  449. 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
  450. 8 * 1024, // common_pagesize (overridable by -z common-page-size)
  451. false, // isolate_execinstr
  452. 0, // rosegment_gap
  453. elfcpp::SHN_UNDEF, // small_common_shndx
  454. elfcpp::SHN_UNDEF, // large_common_shndx
  455. 0, // small_common_section_flags
  456. 0, // large_common_section_flags
  457. NULL, // attributes_section
  458. NULL, // attributes_vendor
  459. "_start", // entry_symbol_name
  460. 32, // hash_entry_size
  461. elfcpp::SHT_PROGBITS, // unwind_section_type
  462. };
  463. // We have to take care here, even when operating in little-endian
  464. // mode, sparc instructions are still big endian.
  465. template<int size, bool big_endian>
  466. class Sparc_relocate_functions
  467. {
  468. private:
  469. // Do a simple relocation with the addend in the relocation.
  470. template<int valsize>
  471. static inline void
  472. rela(unsigned char* view,
  473. unsigned int right_shift,
  474. typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
  475. typename elfcpp::Swap<size, big_endian>::Valtype value,
  476. typename elfcpp::Swap<size, big_endian>::Valtype addend)
  477. {
  478. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  479. Valtype* wv = reinterpret_cast<Valtype*>(view);
  480. Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
  481. Valtype reloc = ((value + addend) >> right_shift);
  482. val &= ~dst_mask;
  483. reloc &= dst_mask;
  484. elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
  485. }
  486. // Do a simple relocation using a symbol value with the addend in
  487. // the relocation.
  488. template<int valsize>
  489. static inline void
  490. rela(unsigned char* view,
  491. unsigned int right_shift,
  492. typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
  493. const Sized_relobj_file<size, big_endian>* object,
  494. const Symbol_value<size>* psymval,
  495. typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
  496. {
  497. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  498. Valtype* wv = reinterpret_cast<Valtype*>(view);
  499. Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
  500. Valtype reloc = (psymval->value(object, addend) >> right_shift);
  501. val &= ~dst_mask;
  502. reloc &= dst_mask;
  503. elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
  504. }
  505. // Do a simple relocation using a symbol value with the addend in
  506. // the relocation, unaligned.
  507. template<int valsize>
  508. static inline void
  509. rela_ua(unsigned char* view,
  510. unsigned int right_shift, elfcpp::Elf_Xword dst_mask,
  511. const Sized_relobj_file<size, big_endian>* object,
  512. const Symbol_value<size>* psymval,
  513. typename elfcpp::Swap<size, big_endian>::Valtype addend)
  514. {
  515. typedef typename elfcpp::Swap_unaligned<valsize,
  516. big_endian>::Valtype Valtype;
  517. unsigned char* wv = view;
  518. Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
  519. Valtype reloc = (psymval->value(object, addend) >> right_shift);
  520. val &= ~dst_mask;
  521. reloc &= dst_mask;
  522. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
  523. }
  524. // Do a simple PC relative relocation with a Symbol_value with the
  525. // addend in the relocation.
  526. template<int valsize>
  527. static inline void
  528. pcrela(unsigned char* view,
  529. unsigned int right_shift,
  530. typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
  531. const Sized_relobj_file<size, big_endian>* object,
  532. const Symbol_value<size>* psymval,
  533. typename elfcpp::Swap<size, big_endian>::Valtype addend,
  534. typename elfcpp::Elf_types<size>::Elf_Addr address)
  535. {
  536. typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
  537. Valtype* wv = reinterpret_cast<Valtype*>(view);
  538. Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
  539. Valtype reloc = ((psymval->value(object, addend) - address)
  540. >> right_shift);
  541. val &= ~dst_mask;
  542. reloc &= dst_mask;
  543. elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
  544. }
  545. template<int valsize>
  546. static inline void
  547. pcrela_unaligned(unsigned char* view,
  548. const Sized_relobj_file<size, big_endian>* object,
  549. const Symbol_value<size>* psymval,
  550. typename elfcpp::Swap<size, big_endian>::Valtype addend,
  551. typename elfcpp::Elf_types<size>::Elf_Addr address)
  552. {
  553. typedef typename elfcpp::Swap_unaligned<valsize,
  554. big_endian>::Valtype Valtype;
  555. unsigned char* wv = view;
  556. Valtype reloc = (psymval->value(object, addend) - address);
  557. elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
  558. }
  559. typedef Sparc_relocate_functions<size, big_endian> This;
  560. typedef Sparc_relocate_functions<size, true> This_insn;
  561. public:
  562. // R_SPARC_WDISP30: (Symbol + Addend - Address) >> 2
  563. static inline void
  564. wdisp30(unsigned char* view,
  565. const Sized_relobj_file<size, big_endian>* object,
  566. const Symbol_value<size>* psymval,
  567. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  568. typename elfcpp::Elf_types<size>::Elf_Addr address)
  569. {
  570. This_insn::template pcrela<32>(view, 2, 0x3fffffff, object,
  571. psymval, addend, address);
  572. }
  573. // R_SPARC_WDISP22: (Symbol + Addend - Address) >> 2
  574. static inline void
  575. wdisp22(unsigned char* view,
  576. const Sized_relobj_file<size, big_endian>* object,
  577. const Symbol_value<size>* psymval,
  578. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  579. typename elfcpp::Elf_types<size>::Elf_Addr address)
  580. {
  581. This_insn::template pcrela<32>(view, 2, 0x003fffff, object,
  582. psymval, addend, address);
  583. }
  584. // R_SPARC_WDISP19: (Symbol + Addend - Address) >> 2
  585. static inline void
  586. wdisp19(unsigned char* view,
  587. const Sized_relobj_file<size, big_endian>* object,
  588. const Symbol_value<size>* psymval,
  589. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  590. typename elfcpp::Elf_types<size>::Elf_Addr address)
  591. {
  592. This_insn::template pcrela<32>(view, 2, 0x0007ffff, object,
  593. psymval, addend, address);
  594. }
  595. // R_SPARC_WDISP16: (Symbol + Addend - Address) >> 2
  596. static inline void
  597. wdisp16(unsigned char* view,
  598. const Sized_relobj_file<size, big_endian>* object,
  599. const Symbol_value<size>* psymval,
  600. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  601. typename elfcpp::Elf_types<size>::Elf_Addr address)
  602. {
  603. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  604. Valtype* wv = reinterpret_cast<Valtype*>(view);
  605. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  606. Valtype reloc = ((psymval->value(object, addend) - address)
  607. >> 2);
  608. // The relocation value is split between the low 14 bits,
  609. // and bits 20-21.
  610. val &= ~((0x3 << 20) | 0x3fff);
  611. reloc = (((reloc & 0xc000) << (20 - 14))
  612. | (reloc & 0x3ffff));
  613. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  614. }
  615. // R_SPARC_WDISP10: (Symbol + Addend - Address) >> 2
  616. static inline void
  617. wdisp10(unsigned char* view,
  618. const Sized_relobj_file<size, big_endian>* object,
  619. const Symbol_value<size>* psymval,
  620. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  621. typename elfcpp::Elf_types<size>::Elf_Addr address)
  622. {
  623. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  624. Valtype* wv = reinterpret_cast<Valtype*>(view);
  625. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  626. Valtype reloc = ((psymval->value(object, addend) - address)
  627. >> 2);
  628. // The relocation value is split between the low bits 5-12,
  629. // and high bits 19-20.
  630. val &= ~((0x3 << 19) | (0xff << 5));
  631. reloc = (((reloc & 0x300) << (19 - 8))
  632. | ((reloc & 0xff) << (5 - 0)));
  633. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  634. }
  635. // R_SPARC_PC22: (Symbol + Addend - Address) >> 10
  636. static inline void
  637. pc22(unsigned char* view,
  638. const Sized_relobj_file<size, big_endian>* object,
  639. const Symbol_value<size>* psymval,
  640. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  641. typename elfcpp::Elf_types<size>::Elf_Addr address)
  642. {
  643. This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
  644. psymval, addend, address);
  645. }
  646. // R_SPARC_PC10: (Symbol + Addend - Address) & 0x3ff
  647. static inline void
  648. pc10(unsigned char* view,
  649. const Sized_relobj_file<size, big_endian>* object,
  650. const Symbol_value<size>* psymval,
  651. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  652. typename elfcpp::Elf_types<size>::Elf_Addr address)
  653. {
  654. This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
  655. psymval, addend, address);
  656. }
  657. // R_SPARC_HI22: (Symbol + Addend) >> 10
  658. static inline void
  659. hi22(unsigned char* view,
  660. typename elfcpp::Elf_types<size>::Elf_Addr value,
  661. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  662. {
  663. This_insn::template rela<32>(view, 10, 0x003fffff, value, addend);
  664. }
  665. // R_SPARC_HI22: (Symbol + Addend) >> 10
  666. static inline void
  667. hi22(unsigned char* view,
  668. const Sized_relobj_file<size, big_endian>* object,
  669. const Symbol_value<size>* psymval,
  670. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  671. {
  672. This_insn::template rela<32>(view, 10, 0x003fffff, object, psymval, addend);
  673. }
  674. // R_SPARC_PCPLT22: (Symbol + Addend - Address) >> 10
  675. static inline void
  676. pcplt22(unsigned char* view,
  677. const Sized_relobj_file<size, big_endian>* object,
  678. const Symbol_value<size>* psymval,
  679. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  680. typename elfcpp::Elf_types<size>::Elf_Addr address)
  681. {
  682. This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
  683. psymval, addend, address);
  684. }
  685. // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
  686. static inline void
  687. lo10(unsigned char* view,
  688. typename elfcpp::Elf_types<size>::Elf_Addr value,
  689. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  690. {
  691. This_insn::template rela<32>(view, 0, 0x000003ff, value, addend);
  692. }
  693. // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
  694. static inline void
  695. lo10(unsigned char* view,
  696. const Sized_relobj_file<size, big_endian>* object,
  697. const Symbol_value<size>* psymval,
  698. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  699. {
  700. This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
  701. }
  702. // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
  703. static inline void
  704. lo10(unsigned char* view,
  705. const Sized_relobj_file<size, big_endian>* object,
  706. const Symbol_value<size>* psymval,
  707. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  708. typename elfcpp::Elf_types<size>::Elf_Addr address)
  709. {
  710. This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
  711. psymval, addend, address);
  712. }
  713. // R_SPARC_OLO10: ((Symbol + Addend) & 0x3ff) + Addend2
  714. static inline void
  715. olo10(unsigned char* view,
  716. const Sized_relobj_file<size, big_endian>* object,
  717. const Symbol_value<size>* psymval,
  718. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  719. typename elfcpp::Elf_types<size>::Elf_Addr addend2)
  720. {
  721. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  722. Valtype* wv = reinterpret_cast<Valtype*>(view);
  723. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  724. Valtype reloc = psymval->value(object, addend);
  725. val &= ~0x1fff;
  726. reloc &= 0x3ff;
  727. reloc += addend2;
  728. reloc &= 0x1fff;
  729. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  730. }
  731. // R_SPARC_22: (Symbol + Addend)
  732. static inline void
  733. rela32_22(unsigned char* view,
  734. const Sized_relobj_file<size, big_endian>* object,
  735. const Symbol_value<size>* psymval,
  736. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  737. {
  738. This_insn::template rela<32>(view, 0, 0x003fffff, object, psymval, addend);
  739. }
  740. // R_SPARC_13: (Symbol + Addend)
  741. static inline void
  742. rela32_13(unsigned char* view,
  743. typename elfcpp::Elf_types<size>::Elf_Addr value,
  744. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  745. {
  746. This_insn::template rela<32>(view, 0, 0x00001fff, value, addend);
  747. }
  748. // R_SPARC_13: (Symbol + Addend)
  749. static inline void
  750. rela32_13(unsigned char* view,
  751. const Sized_relobj_file<size, big_endian>* object,
  752. const Symbol_value<size>* psymval,
  753. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  754. {
  755. This_insn::template rela<32>(view, 0, 0x00001fff, object, psymval, addend);
  756. }
  757. // R_SPARC_UA16: (Symbol + Addend)
  758. static inline void
  759. ua16(unsigned char* view,
  760. const Sized_relobj_file<size, big_endian>* object,
  761. const Symbol_value<size>* psymval,
  762. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  763. {
  764. This::template rela_ua<16>(view, 0, 0xffff, object, psymval, addend);
  765. }
  766. // R_SPARC_UA32: (Symbol + Addend)
  767. static inline void
  768. ua32(unsigned char* view,
  769. const Sized_relobj_file<size, big_endian>* object,
  770. const Symbol_value<size>* psymval,
  771. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  772. {
  773. This::template rela_ua<32>(view, 0, 0xffffffff, object, psymval, addend);
  774. }
  775. // R_SPARC_UA64: (Symbol + Addend)
  776. static inline void
  777. ua64(unsigned char* view,
  778. const Sized_relobj_file<size, big_endian>* object,
  779. const Symbol_value<size>* psymval,
  780. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  781. {
  782. This::template rela_ua<64>(view, 0, ~(elfcpp::Elf_Xword) 0,
  783. object, psymval, addend);
  784. }
  785. // R_SPARC_DISP8: (Symbol + Addend - Address)
  786. static inline void
  787. disp8(unsigned char* view,
  788. const Sized_relobj_file<size, big_endian>* object,
  789. const Symbol_value<size>* psymval,
  790. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  791. typename elfcpp::Elf_types<size>::Elf_Addr address)
  792. {
  793. This::template pcrela_unaligned<8>(view, object, psymval,
  794. addend, address);
  795. }
  796. // R_SPARC_DISP16: (Symbol + Addend - Address)
  797. static inline void
  798. disp16(unsigned char* view,
  799. const Sized_relobj_file<size, big_endian>* object,
  800. const Symbol_value<size>* psymval,
  801. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  802. typename elfcpp::Elf_types<size>::Elf_Addr address)
  803. {
  804. This::template pcrela_unaligned<16>(view, object, psymval,
  805. addend, address);
  806. }
  807. // R_SPARC_DISP32: (Symbol + Addend - Address)
  808. static inline void
  809. disp32(unsigned char* view,
  810. const Sized_relobj_file<size, big_endian>* object,
  811. const Symbol_value<size>* psymval,
  812. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  813. typename elfcpp::Elf_types<size>::Elf_Addr address)
  814. {
  815. This::template pcrela_unaligned<32>(view, object, psymval,
  816. addend, address);
  817. }
  818. // R_SPARC_DISP64: (Symbol + Addend - Address)
  819. static inline void
  820. disp64(unsigned char* view,
  821. const Sized_relobj_file<size, big_endian>* object,
  822. const Symbol_value<size>* psymval,
  823. elfcpp::Elf_Xword addend,
  824. typename elfcpp::Elf_types<size>::Elf_Addr address)
  825. {
  826. This::template pcrela_unaligned<64>(view, object, psymval,
  827. addend, address);
  828. }
  829. // R_SPARC_H34: (Symbol + Addend) >> 12
  830. static inline void
  831. h34(unsigned char* view,
  832. const Sized_relobj_file<size, big_endian>* object,
  833. const Symbol_value<size>* psymval,
  834. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  835. {
  836. This_insn::template rela<32>(view, 12, 0x003fffff, object, psymval, addend);
  837. }
  838. // R_SPARC_H44: (Symbol + Addend) >> 22
  839. static inline void
  840. h44(unsigned char* view,
  841. const Sized_relobj_file<size, big_endian>* object,
  842. const Symbol_value<size>* psymval,
  843. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  844. {
  845. This_insn::template rela<32>(view, 22, 0x003fffff, object, psymval, addend);
  846. }
  847. // R_SPARC_M44: ((Symbol + Addend) >> 12) & 0x3ff
  848. static inline void
  849. m44(unsigned char* view,
  850. const Sized_relobj_file<size, big_endian>* object,
  851. const Symbol_value<size>* psymval,
  852. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  853. {
  854. This_insn::template rela<32>(view, 12, 0x000003ff, object, psymval, addend);
  855. }
  856. // R_SPARC_L44: (Symbol + Addend) & 0xfff
  857. static inline void
  858. l44(unsigned char* view,
  859. const Sized_relobj_file<size, big_endian>* object,
  860. const Symbol_value<size>* psymval,
  861. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  862. {
  863. This_insn::template rela<32>(view, 0, 0x00000fff, object, psymval, addend);
  864. }
  865. // R_SPARC_HH22: (Symbol + Addend) >> 42
  866. static inline void
  867. hh22(unsigned char* view,
  868. const Sized_relobj_file<size, big_endian>* object,
  869. const Symbol_value<size>* psymval,
  870. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  871. {
  872. This_insn::template rela<32>(view, 42, 0x003fffff, object, psymval, addend);
  873. }
  874. // R_SPARC_PC_HH22: (Symbol + Addend - Address) >> 42
  875. static inline void
  876. pc_hh22(unsigned char* view,
  877. const Sized_relobj_file<size, big_endian>* object,
  878. const Symbol_value<size>* psymval,
  879. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  880. typename elfcpp::Elf_types<size>::Elf_Addr address)
  881. {
  882. This_insn::template pcrela<32>(view, 42, 0x003fffff, object,
  883. psymval, addend, address);
  884. }
  885. // R_SPARC_HM10: ((Symbol + Addend) >> 32) & 0x3ff
  886. static inline void
  887. hm10(unsigned char* view,
  888. const Sized_relobj_file<size, big_endian>* object,
  889. const Symbol_value<size>* psymval,
  890. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  891. {
  892. This_insn::template rela<32>(view, 32, 0x000003ff, object, psymval, addend);
  893. }
  894. // R_SPARC_PC_HM10: ((Symbol + Addend - Address) >> 32) & 0x3ff
  895. static inline void
  896. pc_hm10(unsigned char* view,
  897. const Sized_relobj_file<size, big_endian>* object,
  898. const Symbol_value<size>* psymval,
  899. typename elfcpp::Elf_types<size>::Elf_Addr addend,
  900. typename elfcpp::Elf_types<size>::Elf_Addr address)
  901. {
  902. This_insn::template pcrela<32>(view, 32, 0x000003ff, object,
  903. psymval, addend, address);
  904. }
  905. // R_SPARC_11: (Symbol + Addend)
  906. static inline void
  907. rela32_11(unsigned char* view,
  908. const Sized_relobj_file<size, big_endian>* object,
  909. const Symbol_value<size>* psymval,
  910. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  911. {
  912. This_insn::template rela<32>(view, 0, 0x000007ff, object, psymval, addend);
  913. }
  914. // R_SPARC_10: (Symbol + Addend)
  915. static inline void
  916. rela32_10(unsigned char* view,
  917. const Sized_relobj_file<size, big_endian>* object,
  918. const Symbol_value<size>* psymval,
  919. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  920. {
  921. This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
  922. }
  923. // R_SPARC_7: (Symbol + Addend)
  924. static inline void
  925. rela32_7(unsigned char* view,
  926. const Sized_relobj_file<size, big_endian>* object,
  927. const Symbol_value<size>* psymval,
  928. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  929. {
  930. This_insn::template rela<32>(view, 0, 0x0000007f, object, psymval, addend);
  931. }
  932. // R_SPARC_6: (Symbol + Addend)
  933. static inline void
  934. rela32_6(unsigned char* view,
  935. const Sized_relobj_file<size, big_endian>* object,
  936. const Symbol_value<size>* psymval,
  937. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  938. {
  939. This_insn::template rela<32>(view, 0, 0x0000003f, object, psymval, addend);
  940. }
  941. // R_SPARC_5: (Symbol + Addend)
  942. static inline void
  943. rela32_5(unsigned char* view,
  944. const Sized_relobj_file<size, big_endian>* object,
  945. const Symbol_value<size>* psymval,
  946. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  947. {
  948. This_insn::template rela<32>(view, 0, 0x0000001f, object, psymval, addend);
  949. }
  950. // R_SPARC_TLS_LDO_HIX22: @dtpoff(Symbol + Addend) >> 10
  951. static inline void
  952. ldo_hix22(unsigned char* view,
  953. typename elfcpp::Elf_types<size>::Elf_Addr value,
  954. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  955. {
  956. This_insn::hi22(view, value, addend);
  957. }
  958. // R_SPARC_TLS_LDO_LOX10: @dtpoff(Symbol + Addend) & 0x3ff
  959. static inline void
  960. ldo_lox10(unsigned char* view,
  961. typename elfcpp::Elf_types<size>::Elf_Addr value,
  962. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  963. {
  964. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  965. Valtype* wv = reinterpret_cast<Valtype*>(view);
  966. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  967. Valtype reloc = (value + addend);
  968. val &= ~0x1fff;
  969. reloc &= 0x3ff;
  970. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  971. }
  972. // R_SPARC_TLS_LE_HIX22: (@tpoff(Symbol + Addend) ^ 0xffffffffffffffff) >> 10
  973. static inline void
  974. hix22(unsigned char* view,
  975. typename elfcpp::Elf_types<size>::Elf_Addr value,
  976. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  977. {
  978. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  979. Valtype* wv = reinterpret_cast<Valtype*>(view);
  980. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  981. Valtype reloc = (value + addend);
  982. val &= ~0x3fffff;
  983. reloc ^= ~(Valtype)0;
  984. reloc >>= 10;
  985. reloc &= 0x3fffff;
  986. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  987. }
  988. // R_SPARC_GOTDATA_OP_HIX22: @gdopoff(Symbol + Addend) >> 10
  989. static inline void
  990. gdop_hix22(unsigned char* view,
  991. typename elfcpp::Elf_types<size>::Elf_Addr value)
  992. {
  993. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  994. Valtype* wv = reinterpret_cast<Valtype*>(view);
  995. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  996. int32_t reloc = static_cast<int32_t>(value);
  997. val &= ~0x3fffff;
  998. if (reloc < 0)
  999. reloc ^= ~static_cast<int32_t>(0);
  1000. reloc >>= 10;
  1001. reloc &= 0x3fffff;
  1002. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  1003. }
  1004. // R_SPARC_HIX22: ((Symbol + Addend) ^ 0xffffffffffffffff) >> 10
  1005. static inline void
  1006. hix22(unsigned char* view,
  1007. const Sized_relobj_file<size, big_endian>* object,
  1008. const Symbol_value<size>* psymval,
  1009. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  1010. {
  1011. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  1012. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1013. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  1014. Valtype reloc = psymval->value(object, addend);
  1015. val &= ~0x3fffff;
  1016. reloc ^= ~(Valtype)0;
  1017. reloc >>= 10;
  1018. reloc &= 0x3fffff;
  1019. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  1020. }
  1021. // R_SPARC_TLS_LE_LOX10: (@tpoff(Symbol + Addend) & 0x3ff) | 0x1c00
  1022. static inline void
  1023. lox10(unsigned char* view,
  1024. typename elfcpp::Elf_types<size>::Elf_Addr value,
  1025. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  1026. {
  1027. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  1028. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1029. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  1030. Valtype reloc = (value + addend);
  1031. val &= ~0x1fff;
  1032. reloc &= 0x3ff;
  1033. reloc |= 0x1c00;
  1034. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  1035. }
  1036. // R_SPARC_GOTDATA_OP_LOX10: (@gdopoff(Symbol + Addend) & 0x3ff) | 0x1c00
  1037. static inline void
  1038. gdop_lox10(unsigned char* view,
  1039. typename elfcpp::Elf_types<size>::Elf_Addr value)
  1040. {
  1041. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  1042. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1043. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  1044. int32_t reloc = static_cast<int32_t>(value);
  1045. if (reloc < 0)
  1046. reloc = (reloc & 0x3ff) | 0x1c00;
  1047. else
  1048. reloc = (reloc & 0x3ff);
  1049. val &= ~0x1fff;
  1050. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  1051. }
  1052. // R_SPARC_LOX10: ((Symbol + Addend) & 0x3ff) | 0x1c00
  1053. static inline void
  1054. lox10(unsigned char* view,
  1055. const Sized_relobj_file<size, big_endian>* object,
  1056. const Symbol_value<size>* psymval,
  1057. typename elfcpp::Elf_types<size>::Elf_Addr addend)
  1058. {
  1059. typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
  1060. Valtype* wv = reinterpret_cast<Valtype*>(view);
  1061. Valtype val = elfcpp::Swap<32, true>::readval(wv);
  1062. Valtype reloc = psymval->value(object, addend);
  1063. val &= ~0x1fff;
  1064. reloc &= 0x3ff;
  1065. reloc |= 0x1c00;
  1066. elfcpp::Swap<32, true>::writeval(wv, val | reloc);
  1067. }
  1068. };
  1069. // Get the GOT section, creating it if necessary.
  1070. template<int size, bool big_endian>
  1071. Output_data_got<size, big_endian>*
  1072. Target_sparc<size, big_endian>::got_section(Symbol_table* symtab,
  1073. Layout* layout)
  1074. {
  1075. if (this->got_ == NULL)
  1076. {
  1077. gold_assert(symtab != NULL && layout != NULL);
  1078. this->got_ = new Output_data_got<size, big_endian>();
  1079. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  1080. (elfcpp::SHF_ALLOC
  1081. | elfcpp::SHF_WRITE),
  1082. this->got_, ORDER_RELRO, true);
  1083. // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
  1084. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  1085. Symbol_table::PREDEFINED,
  1086. this->got_,
  1087. 0, 0, elfcpp::STT_OBJECT,
  1088. elfcpp::STB_LOCAL,
  1089. elfcpp::STV_HIDDEN, 0,
  1090. false, false);
  1091. }
  1092. return this->got_;
  1093. }
  1094. // Get the dynamic reloc section, creating it if necessary.
  1095. template<int size, bool big_endian>
  1096. typename Target_sparc<size, big_endian>::Reloc_section*
  1097. Target_sparc<size, big_endian>::rela_dyn_section(Layout* layout)
  1098. {
  1099. if (this->rela_dyn_ == NULL)
  1100. {
  1101. gold_assert(layout != NULL);
  1102. this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
  1103. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  1104. elfcpp::SHF_ALLOC, this->rela_dyn_,
  1105. ORDER_DYNAMIC_RELOCS, false);
  1106. }
  1107. return this->rela_dyn_;
  1108. }
  1109. // Get the section to use for IFUNC relocs, creating it if
  1110. // necessary. These go in .rela.dyn, but only after all other dynamic
  1111. // relocations. They need to follow the other dynamic relocations so
  1112. // that they can refer to global variables initialized by those
  1113. // relocs.
  1114. template<int size, bool big_endian>
  1115. typename Target_sparc<size, big_endian>::Reloc_section*
  1116. Target_sparc<size, big_endian>::rela_ifunc_section(Layout* layout)
  1117. {
  1118. if (this->rela_ifunc_ == NULL)
  1119. {
  1120. // Make sure we have already created the dynamic reloc section.
  1121. this->rela_dyn_section(layout);
  1122. this->rela_ifunc_ = new Reloc_section(false);
  1123. layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
  1124. elfcpp::SHF_ALLOC, this->rela_ifunc_,
  1125. ORDER_DYNAMIC_RELOCS, false);
  1126. gold_assert(this->rela_dyn_->output_section()
  1127. == this->rela_ifunc_->output_section());
  1128. }
  1129. return this->rela_ifunc_;
  1130. }
  1131. // A class to handle the PLT data.
  1132. template<int size, bool big_endian>
  1133. class Output_data_plt_sparc : public Output_section_data
  1134. {
  1135. public:
  1136. typedef Output_data_reloc<elfcpp::SHT_RELA, true,
  1137. size, big_endian> Reloc_section;
  1138. Output_data_plt_sparc(Layout*);
  1139. // Add an entry to the PLT.
  1140. void add_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym);
  1141. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
  1142. unsigned int
  1143. add_local_ifunc_entry(Symbol_table*, Layout*,
  1144. Sized_relobj_file<size, big_endian>* relobj,
  1145. unsigned int local_sym_index);
  1146. // Return the .rela.plt section data.
  1147. const Reloc_section* rel_plt() const
  1148. {
  1149. return this->rel_;
  1150. }
  1151. // Return where the IFUNC relocations should go.
  1152. Reloc_section*
  1153. rela_ifunc(Symbol_table*, Layout*);
  1154. void
  1155. emit_pending_ifunc_relocs();
  1156. // Return whether we created a section for IFUNC relocations.
  1157. bool
  1158. has_ifunc_section() const
  1159. { return this->ifunc_rel_ != NULL; }
  1160. // Return the number of PLT entries.
  1161. unsigned int
  1162. entry_count() const
  1163. { return this->count_ + this->ifunc_count_; }
  1164. // Return the offset of the first non-reserved PLT entry.
  1165. static unsigned int
  1166. first_plt_entry_offset()
  1167. { return 4 * base_plt_entry_size; }
  1168. // Return the size of a PLT entry.
  1169. static unsigned int
  1170. get_plt_entry_size()
  1171. { return base_plt_entry_size; }
  1172. // Return the PLT address to use for a global symbol.
  1173. uint64_t
  1174. address_for_global(const Symbol*);
  1175. // Return the PLT address to use for a local symbol.
  1176. uint64_t
  1177. address_for_local(const Relobj*, unsigned int symndx);
  1178. protected:
  1179. void do_adjust_output_section(Output_section* os);
  1180. // Write to a map file.
  1181. void
  1182. do_print_to_mapfile(Mapfile* mapfile) const
  1183. { mapfile->print_output_data(this, _("** PLT")); }
  1184. private:
  1185. // The size of an entry in the PLT.
  1186. static const int base_plt_entry_size = (size == 32 ? 12 : 32);
  1187. static const unsigned int plt_entries_per_block = 160;
  1188. static const unsigned int plt_insn_chunk_size = 24;
  1189. static const unsigned int plt_pointer_chunk_size = 8;
  1190. static const unsigned int plt_block_size =
  1191. (plt_entries_per_block
  1192. * (plt_insn_chunk_size + plt_pointer_chunk_size));
  1193. section_offset_type
  1194. plt_index_to_offset(unsigned int index)
  1195. {
  1196. section_offset_type offset;
  1197. if (size == 32 || index < 32768)
  1198. offset = index * base_plt_entry_size;
  1199. else
  1200. {
  1201. unsigned int ext_index = index - 32768;
  1202. offset = (32768 * base_plt_entry_size)
  1203. + ((ext_index / plt_entries_per_block)
  1204. * plt_block_size)
  1205. + ((ext_index % plt_entries_per_block)
  1206. * plt_insn_chunk_size);
  1207. }
  1208. return offset;
  1209. }
  1210. // Set the final size.
  1211. void
  1212. set_final_data_size()
  1213. {
  1214. unsigned int full_count = this->entry_count() + 4;
  1215. unsigned int extra = (size == 32 ? 4 : 0);
  1216. section_offset_type sz = plt_index_to_offset(full_count) + extra;
  1217. return this->set_data_size(sz);
  1218. }
  1219. // Write out the PLT data.
  1220. void
  1221. do_write(Output_file*);
  1222. struct Global_ifunc
  1223. {
  1224. Reloc_section* rel;
  1225. Symbol* gsym;
  1226. unsigned int plt_index;
  1227. };
  1228. struct Local_ifunc
  1229. {
  1230. Reloc_section* rel;
  1231. Sized_relobj_file<size, big_endian>* object;
  1232. unsigned int local_sym_index;
  1233. unsigned int plt_index;
  1234. };
  1235. // The reloc section.
  1236. Reloc_section* rel_;
  1237. // The IFUNC relocations, if necessary. These must follow the
  1238. // regular relocations.
  1239. Reloc_section* ifunc_rel_;
  1240. // The number of PLT entries.
  1241. unsigned int count_;
  1242. // The number of PLT entries for IFUNC symbols.
  1243. unsigned int ifunc_count_;
  1244. // Global STT_GNU_IFUNC symbols.
  1245. std::vector<Global_ifunc> global_ifuncs_;
  1246. // Local STT_GNU_IFUNC symbols.
  1247. std::vector<Local_ifunc> local_ifuncs_;
  1248. };
  1249. // Define the constants as required by C++ standard.
  1250. template<int size, bool big_endian>
  1251. const int Output_data_plt_sparc<size, big_endian>::base_plt_entry_size;
  1252. template<int size, bool big_endian>
  1253. const unsigned int
  1254. Output_data_plt_sparc<size, big_endian>::plt_entries_per_block;
  1255. template<int size, bool big_endian>
  1256. const unsigned int Output_data_plt_sparc<size, big_endian>::plt_insn_chunk_size;
  1257. template<int size, bool big_endian>
  1258. const unsigned int
  1259. Output_data_plt_sparc<size, big_endian>::plt_pointer_chunk_size;
  1260. template<int size, bool big_endian>
  1261. const unsigned int Output_data_plt_sparc<size, big_endian>::plt_block_size;
  1262. // Create the PLT section. The ordinary .got section is an argument,
  1263. // since we need to refer to the start.
  1264. template<int size, bool big_endian>
  1265. Output_data_plt_sparc<size, big_endian>::Output_data_plt_sparc(Layout* layout)
  1266. : Output_section_data(size == 32 ? 4 : 8), ifunc_rel_(NULL),
  1267. count_(0), ifunc_count_(0), global_ifuncs_(), local_ifuncs_()
  1268. {
  1269. this->rel_ = new Reloc_section(false);
  1270. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  1271. elfcpp::SHF_ALLOC, this->rel_,
  1272. ORDER_DYNAMIC_PLT_RELOCS, false);
  1273. }
  1274. template<int size, bool big_endian>
  1275. void
  1276. Output_data_plt_sparc<size, big_endian>::do_adjust_output_section(Output_section* os)
  1277. {
  1278. os->set_entsize(0);
  1279. }
  1280. // Add an entry to the PLT.
  1281. template<int size, bool big_endian>
  1282. void
  1283. Output_data_plt_sparc<size, big_endian>::add_entry(Symbol_table* symtab,
  1284. Layout* layout,
  1285. Symbol* gsym)
  1286. {
  1287. gold_assert(!gsym->has_plt_offset());
  1288. section_offset_type plt_offset;
  1289. unsigned int index;
  1290. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1291. && gsym->can_use_relative_reloc(false))
  1292. {
  1293. index = this->ifunc_count_;
  1294. plt_offset = plt_index_to_offset(index);
  1295. gsym->set_plt_offset(plt_offset);
  1296. ++this->ifunc_count_;
  1297. Reloc_section* rel = this->rela_ifunc(symtab, layout);
  1298. struct Global_ifunc gi;
  1299. gi.rel = rel;
  1300. gi.gsym = gsym;
  1301. gi.plt_index = index;
  1302. this->global_ifuncs_.push_back(gi);
  1303. }
  1304. else
  1305. {
  1306. plt_offset = plt_index_to_offset(this->count_ + 4);
  1307. gsym->set_plt_offset(plt_offset);
  1308. ++this->count_;
  1309. gsym->set_needs_dynsym_entry();
  1310. this->rel_->add_global(gsym, elfcpp::R_SPARC_JMP_SLOT, this,
  1311. plt_offset, 0);
  1312. }
  1313. // Note that we don't need to save the symbol. The contents of the
  1314. // PLT are independent of which symbols are used. The symbols only
  1315. // appear in the relocations.
  1316. }
  1317. template<int size, bool big_endian>
  1318. unsigned int
  1319. Output_data_plt_sparc<size, big_endian>::add_local_ifunc_entry(
  1320. Symbol_table* symtab,
  1321. Layout* layout,
  1322. Sized_relobj_file<size, big_endian>* relobj,
  1323. unsigned int local_sym_index)
  1324. {
  1325. unsigned int index = this->ifunc_count_;
  1326. section_offset_type plt_offset;
  1327. plt_offset = plt_index_to_offset(index);
  1328. ++this->ifunc_count_;
  1329. Reloc_section* rel = this->rela_ifunc(symtab, layout);
  1330. struct Local_ifunc li;
  1331. li.rel = rel;
  1332. li.object = relobj;
  1333. li.local_sym_index = local_sym_index;
  1334. li.plt_index = index;
  1335. this->local_ifuncs_.push_back(li);
  1336. return plt_offset;
  1337. }
  1338. // Emit any pending IFUNC plt relocations.
  1339. template<int size, bool big_endian>
  1340. void
  1341. Output_data_plt_sparc<size, big_endian>::emit_pending_ifunc_relocs()
  1342. {
  1343. // Emit any pending IFUNC relocs.
  1344. for (typename std::vector<Global_ifunc>::const_iterator p =
  1345. this->global_ifuncs_.begin();
  1346. p != this->global_ifuncs_.end();
  1347. ++p)
  1348. {
  1349. section_offset_type plt_offset;
  1350. unsigned int index;
  1351. index = this->count_ + p->plt_index + 4;
  1352. plt_offset = this->plt_index_to_offset(index);
  1353. p->rel->add_symbolless_global_addend(p->gsym, elfcpp::R_SPARC_JMP_IREL,
  1354. this, plt_offset, 0);
  1355. }
  1356. for (typename std::vector<Local_ifunc>::const_iterator p =
  1357. this->local_ifuncs_.begin();
  1358. p != this->local_ifuncs_.end();
  1359. ++p)
  1360. {
  1361. section_offset_type plt_offset;
  1362. unsigned int index;
  1363. index = this->count_ + p->plt_index + 4;
  1364. plt_offset = this->plt_index_to_offset(index);
  1365. p->rel->add_symbolless_local_addend(p->object, p->local_sym_index,
  1366. elfcpp::R_SPARC_JMP_IREL,
  1367. this, plt_offset, 0);
  1368. }
  1369. }
  1370. // Return where the IFUNC relocations should go in the PLT. These
  1371. // follow the non-IFUNC relocations.
  1372. template<int size, bool big_endian>
  1373. typename Output_data_plt_sparc<size, big_endian>::Reloc_section*
  1374. Output_data_plt_sparc<size, big_endian>::rela_ifunc(
  1375. Symbol_table* symtab,
  1376. Layout* layout)
  1377. {
  1378. if (this->ifunc_rel_ == NULL)
  1379. {
  1380. this->ifunc_rel_ = new Reloc_section(false);
  1381. layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
  1382. elfcpp::SHF_ALLOC, this->ifunc_rel_,
  1383. ORDER_DYNAMIC_PLT_RELOCS, false);
  1384. gold_assert(this->ifunc_rel_->output_section()
  1385. == this->rel_->output_section());
  1386. if (parameters->doing_static_link())
  1387. {
  1388. // A statically linked executable will only have a .rel.plt
  1389. // section to hold R_SPARC_IRELATIVE and R_SPARC_JMP_IREL
  1390. // relocs for STT_GNU_IFUNC symbols. The library will use
  1391. // these symbols to locate the IRELATIVE and JMP_IREL relocs
  1392. // at program startup time.
  1393. symtab->define_in_output_data("__rela_iplt_start", NULL,
  1394. Symbol_table::PREDEFINED,
  1395. this->ifunc_rel_, 0, 0,
  1396. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  1397. elfcpp::STV_HIDDEN, 0, false, true);
  1398. symtab->define_in_output_data("__rela_iplt_end", NULL,
  1399. Symbol_table::PREDEFINED,
  1400. this->ifunc_rel_, 0, 0,
  1401. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  1402. elfcpp::STV_HIDDEN, 0, true, true);
  1403. }
  1404. }
  1405. return this->ifunc_rel_;
  1406. }
  1407. // Return the PLT address to use for a global symbol.
  1408. template<int size, bool big_endian>
  1409. uint64_t
  1410. Output_data_plt_sparc<size, big_endian>::address_for_global(const Symbol* gsym)
  1411. {
  1412. uint64_t offset = 0;
  1413. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1414. && gsym->can_use_relative_reloc(false))
  1415. offset = plt_index_to_offset(this->count_ + 4);
  1416. return this->address() + offset + gsym->plt_offset();
  1417. }
  1418. // Return the PLT address to use for a local symbol. These are always
  1419. // IRELATIVE relocs.
  1420. template<int size, bool big_endian>
  1421. uint64_t
  1422. Output_data_plt_sparc<size, big_endian>::address_for_local(
  1423. const Relobj* object,
  1424. unsigned int r_sym)
  1425. {
  1426. return (this->address()
  1427. + plt_index_to_offset(this->count_ + 4)
  1428. + object->local_plt_offset(r_sym));
  1429. }
  1430. static const unsigned int sparc_nop = 0x01000000;
  1431. static const unsigned int sparc_sethi_g1 = 0x03000000;
  1432. static const unsigned int sparc_branch_always = 0x30800000;
  1433. static const unsigned int sparc_branch_always_pt = 0x30680000;
  1434. static const unsigned int sparc_mov = 0x80100000;
  1435. static const unsigned int sparc_mov_g0_o0 = 0x90100000;
  1436. static const unsigned int sparc_mov_o7_g5 = 0x8a10000f;
  1437. static const unsigned int sparc_call_plus_8 = 0x40000002;
  1438. static const unsigned int sparc_ldx_o7_imm_g1 = 0xc25be000;
  1439. static const unsigned int sparc_jmpl_o7_g1_g1 = 0x83c3c001;
  1440. static const unsigned int sparc_mov_g5_o7 = 0x9e100005;
  1441. // Write out the PLT.
  1442. template<int size, bool big_endian>
  1443. void
  1444. Output_data_plt_sparc<size, big_endian>::do_write(Output_file* of)
  1445. {
  1446. const off_t offset = this->offset();
  1447. const section_size_type oview_size =
  1448. convert_to_section_size_type(this->data_size());
  1449. unsigned char* const oview = of->get_output_view(offset, oview_size);
  1450. unsigned char* pov = oview;
  1451. memset(pov, 0, base_plt_entry_size * 4);
  1452. pov += this->first_plt_entry_offset();
  1453. unsigned int plt_offset = base_plt_entry_size * 4;
  1454. const unsigned int count = this->entry_count();
  1455. if (size == 64)
  1456. {
  1457. unsigned int limit;
  1458. limit = (count > 32768 ? 32768 : count);
  1459. for (unsigned int i = 0; i < limit; ++i)
  1460. {
  1461. elfcpp::Swap<32, true>::writeval(pov + 0x00,
  1462. sparc_sethi_g1 + plt_offset);
  1463. elfcpp::Swap<32, true>::writeval(pov + 0x04,
  1464. sparc_branch_always_pt +
  1465. (((base_plt_entry_size -
  1466. (plt_offset + 4)) >> 2) &
  1467. 0x7ffff));
  1468. elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
  1469. elfcpp::Swap<32, true>::writeval(pov + 0x0c, sparc_nop);
  1470. elfcpp::Swap<32, true>::writeval(pov + 0x10, sparc_nop);
  1471. elfcpp::Swap<32, true>::writeval(pov + 0x14, sparc_nop);
  1472. elfcpp::Swap<32, true>::writeval(pov + 0x18, sparc_nop);
  1473. elfcpp::Swap<32, true>::writeval(pov + 0x1c, sparc_nop);
  1474. pov += base_plt_entry_size;
  1475. plt_offset += base_plt_entry_size;
  1476. }
  1477. if (count > 32768)
  1478. {
  1479. unsigned int ext_cnt = count - 32768;
  1480. unsigned int blks = ext_cnt / plt_entries_per_block;
  1481. for (unsigned int i = 0; i < blks; ++i)
  1482. {
  1483. unsigned int data_off = (plt_entries_per_block
  1484. * plt_insn_chunk_size) - 4;
  1485. for (unsigned int j = 0; j < plt_entries_per_block; ++j)
  1486. {
  1487. elfcpp::Swap<32, true>::writeval(pov + 0x00,
  1488. sparc_mov_o7_g5);
  1489. elfcpp::Swap<32, true>::writeval(pov + 0x04,
  1490. sparc_call_plus_8);
  1491. elfcpp::Swap<32, true>::writeval(pov + 0x08,
  1492. sparc_nop);
  1493. elfcpp::Swap<32, true>::writeval(pov + 0x0c,
  1494. sparc_ldx_o7_imm_g1 +
  1495. (data_off & 0x1fff));
  1496. elfcpp::Swap<32, true>::writeval(pov + 0x10,
  1497. sparc_jmpl_o7_g1_g1);
  1498. elfcpp::Swap<32, true>::writeval(pov + 0x14,
  1499. sparc_mov_g5_o7);
  1500. elfcpp::Swap<64, big_endian>::writeval(
  1501. pov + 0x4 + data_off,
  1502. (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
  1503. pov += plt_insn_chunk_size;
  1504. data_off -= 16;
  1505. }
  1506. }
  1507. unsigned int sub_blk_cnt = ext_cnt % plt_entries_per_block;
  1508. for (unsigned int i = 0; i < sub_blk_cnt; ++i)
  1509. {
  1510. unsigned int data_off = (sub_blk_cnt
  1511. * plt_insn_chunk_size) - 4;
  1512. for (unsigned int j = 0; j < plt_entries_per_block; ++j)
  1513. {
  1514. elfcpp::Swap<32, true>::writeval(pov + 0x00,
  1515. sparc_mov_o7_g5);
  1516. elfcpp::Swap<32, true>::writeval(pov + 0x04,
  1517. sparc_call_plus_8);
  1518. elfcpp::Swap<32, true>::writeval(pov + 0x08,
  1519. sparc_nop);
  1520. elfcpp::Swap<32, true>::writeval(pov + 0x0c,
  1521. sparc_ldx_o7_imm_g1 +
  1522. (data_off & 0x1fff));
  1523. elfcpp::Swap<32, true>::writeval(pov + 0x10,
  1524. sparc_jmpl_o7_g1_g1);
  1525. elfcpp::Swap<32, true>::writeval(pov + 0x14,
  1526. sparc_mov_g5_o7);
  1527. elfcpp::Swap<64, big_endian>::writeval(
  1528. pov + 0x4 + data_off,
  1529. (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
  1530. pov += plt_insn_chunk_size;
  1531. data_off -= 16;
  1532. }
  1533. }
  1534. }
  1535. }
  1536. else
  1537. {
  1538. for (unsigned int i = 0; i < count; ++i)
  1539. {
  1540. elfcpp::Swap<32, true>::writeval(pov + 0x00,
  1541. sparc_sethi_g1 + plt_offset);
  1542. elfcpp::Swap<32, true>::writeval(pov + 0x04,
  1543. sparc_branch_always +
  1544. (((- (plt_offset + 4)) >> 2) &
  1545. 0x003fffff));
  1546. elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
  1547. pov += base_plt_entry_size;
  1548. plt_offset += base_plt_entry_size;
  1549. }
  1550. elfcpp::Swap<32, true>::writeval(pov, sparc_nop);
  1551. pov += 4;
  1552. }
  1553. gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
  1554. of->write_output_view(offset, oview_size, oview);
  1555. }
  1556. // Create the PLT section.
  1557. template<int size, bool big_endian>
  1558. void
  1559. Target_sparc<size, big_endian>::make_plt_section(Symbol_table* symtab,
  1560. Layout* layout)
  1561. {
  1562. // Create the GOT sections first.
  1563. this->got_section(symtab, layout);
  1564. // Ensure that .rela.dyn always appears before .rela.plt This is
  1565. // necessary due to how, on Sparc and some other targets, .rela.dyn
  1566. // needs to include .rela.plt in it's range.
  1567. this->rela_dyn_section(layout);
  1568. this->plt_ = new Output_data_plt_sparc<size, big_endian>(layout);
  1569. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  1570. (elfcpp::SHF_ALLOC
  1571. | elfcpp::SHF_EXECINSTR
  1572. | elfcpp::SHF_WRITE),
  1573. this->plt_, ORDER_NON_RELRO_FIRST, false);
  1574. // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
  1575. symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
  1576. Symbol_table::PREDEFINED,
  1577. this->plt_,
  1578. 0, 0, elfcpp::STT_OBJECT,
  1579. elfcpp::STB_LOCAL,
  1580. elfcpp::STV_HIDDEN, 0,
  1581. false, false);
  1582. }
  1583. // Create a PLT entry for a global symbol.
  1584. template<int size, bool big_endian>
  1585. void
  1586. Target_sparc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
  1587. Layout* layout,
  1588. Symbol* gsym)
  1589. {
  1590. if (gsym->has_plt_offset())
  1591. return;
  1592. if (this->plt_ == NULL)
  1593. this->make_plt_section(symtab, layout);
  1594. this->plt_->add_entry(symtab, layout, gsym);
  1595. }
  1596. // Make a PLT entry for a local STT_GNU_IFUNC symbol.
  1597. template<int size, bool big_endian>
  1598. void
  1599. Target_sparc<size, big_endian>::make_local_ifunc_plt_entry(
  1600. Symbol_table* symtab,
  1601. Layout* layout,
  1602. Sized_relobj_file<size, big_endian>* relobj,
  1603. unsigned int local_sym_index)
  1604. {
  1605. if (relobj->local_has_plt_offset(local_sym_index))
  1606. return;
  1607. if (this->plt_ == NULL)
  1608. this->make_plt_section(symtab, layout);
  1609. unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
  1610. relobj,
  1611. local_sym_index);
  1612. relobj->set_local_plt_offset(local_sym_index, plt_offset);
  1613. }
  1614. // Return the number of entries in the PLT.
  1615. template<int size, bool big_endian>
  1616. unsigned int
  1617. Target_sparc<size, big_endian>::plt_entry_count() const
  1618. {
  1619. if (this->plt_ == NULL)
  1620. return 0;
  1621. return this->plt_->entry_count();
  1622. }
  1623. // Return the offset of the first non-reserved PLT entry.
  1624. template<int size, bool big_endian>
  1625. unsigned int
  1626. Target_sparc<size, big_endian>::first_plt_entry_offset() const
  1627. {
  1628. return Output_data_plt_sparc<size, big_endian>::first_plt_entry_offset();
  1629. }
  1630. // Return the size of each PLT entry.
  1631. template<int size, bool big_endian>
  1632. unsigned int
  1633. Target_sparc<size, big_endian>::plt_entry_size() const
  1634. {
  1635. return Output_data_plt_sparc<size, big_endian>::get_plt_entry_size();
  1636. }
  1637. // Create a GOT entry for the TLS module index.
  1638. template<int size, bool big_endian>
  1639. unsigned int
  1640. Target_sparc<size, big_endian>::got_mod_index_entry(
  1641. Symbol_table* symtab,
  1642. Layout* layout,
  1643. Sized_relobj_file<size, big_endian>* object)
  1644. {
  1645. if (this->got_mod_index_offset_ == -1U)
  1646. {
  1647. gold_assert(symtab != NULL && layout != NULL && object != NULL);
  1648. Reloc_section* rela_dyn = this->rela_dyn_section(layout);
  1649. Output_data_got<size, big_endian>* got;
  1650. unsigned int got_offset;
  1651. got = this->got_section(symtab, layout);
  1652. got_offset = got->add_constant(0);
  1653. rela_dyn->add_local(object, 0,
  1654. (size == 64 ?
  1655. elfcpp::R_SPARC_TLS_DTPMOD64 :
  1656. elfcpp::R_SPARC_TLS_DTPMOD32), got,
  1657. got_offset, 0);
  1658. got->add_constant(0);
  1659. this->got_mod_index_offset_ = got_offset;
  1660. }
  1661. return this->got_mod_index_offset_;
  1662. }
  1663. // Optimize the TLS relocation type based on what we know about the
  1664. // symbol. IS_FINAL is true if the final address of this symbol is
  1665. // known at link time.
  1666. static tls::Tls_optimization
  1667. optimize_tls_reloc(bool is_final, int r_type)
  1668. {
  1669. // If we are generating a shared library, then we can't do anything
  1670. // in the linker.
  1671. if (parameters->options().shared())
  1672. return tls::TLSOPT_NONE;
  1673. switch (r_type)
  1674. {
  1675. case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
  1676. case elfcpp::R_SPARC_TLS_GD_LO10:
  1677. case elfcpp::R_SPARC_TLS_GD_ADD:
  1678. case elfcpp::R_SPARC_TLS_GD_CALL:
  1679. // These are General-Dynamic which permits fully general TLS
  1680. // access. Since we know that we are generating an executable,
  1681. // we can convert this to Initial-Exec. If we also know that
  1682. // this is a local symbol, we can further switch to Local-Exec.
  1683. if (is_final)
  1684. return tls::TLSOPT_TO_LE;
  1685. return tls::TLSOPT_TO_IE;
  1686. case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
  1687. case elfcpp::R_SPARC_TLS_LDM_LO10:
  1688. case elfcpp::R_SPARC_TLS_LDM_ADD:
  1689. case elfcpp::R_SPARC_TLS_LDM_CALL:
  1690. // This is Local-Dynamic, which refers to a local symbol in the
  1691. // dynamic TLS block. Since we know that we generating an
  1692. // executable, we can switch to Local-Exec.
  1693. return tls::TLSOPT_TO_LE;
  1694. case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
  1695. case elfcpp::R_SPARC_TLS_LDO_LOX10:
  1696. case elfcpp::R_SPARC_TLS_LDO_ADD:
  1697. // Another type of Local-Dynamic relocation.
  1698. return tls::TLSOPT_TO_LE;
  1699. case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
  1700. case elfcpp::R_SPARC_TLS_IE_LO10:
  1701. case elfcpp::R_SPARC_TLS_IE_LD:
  1702. case elfcpp::R_SPARC_TLS_IE_LDX:
  1703. case elfcpp::R_SPARC_TLS_IE_ADD:
  1704. // These are Initial-Exec relocs which get the thread offset
  1705. // from the GOT. If we know that we are linking against the
  1706. // local symbol, we can switch to Local-Exec, which links the
  1707. // thread offset into the instruction.
  1708. if (is_final)
  1709. return tls::TLSOPT_TO_LE;
  1710. return tls::TLSOPT_NONE;
  1711. case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
  1712. case elfcpp::R_SPARC_TLS_LE_LOX10:
  1713. // When we already have Local-Exec, there is nothing further we
  1714. // can do.
  1715. return tls::TLSOPT_NONE;
  1716. default:
  1717. gold_unreachable();
  1718. }
  1719. }
  1720. // Get the Reference_flags for a particular relocation.
  1721. template<int size, bool big_endian>
  1722. int
  1723. Target_sparc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
  1724. {
  1725. r_type &= 0xff;
  1726. switch (r_type)
  1727. {
  1728. case elfcpp::R_SPARC_NONE:
  1729. case elfcpp::R_SPARC_REGISTER:
  1730. case elfcpp::R_SPARC_GNU_VTINHERIT:
  1731. case elfcpp::R_SPARC_GNU_VTENTRY:
  1732. // No symbol reference.
  1733. return 0;
  1734. case elfcpp::R_SPARC_UA64:
  1735. case elfcpp::R_SPARC_64:
  1736. case elfcpp::R_SPARC_HIX22:
  1737. case elfcpp::R_SPARC_LOX10:
  1738. case elfcpp::R_SPARC_H34:
  1739. case elfcpp::R_SPARC_H44:
  1740. case elfcpp::R_SPARC_M44:
  1741. case elfcpp::R_SPARC_L44:
  1742. case elfcpp::R_SPARC_HH22:
  1743. case elfcpp::R_SPARC_HM10:
  1744. case elfcpp::R_SPARC_LM22:
  1745. case elfcpp::R_SPARC_HI22:
  1746. case elfcpp::R_SPARC_LO10:
  1747. case elfcpp::R_SPARC_OLO10:
  1748. case elfcpp::R_SPARC_UA32:
  1749. case elfcpp::R_SPARC_32:
  1750. case elfcpp::R_SPARC_UA16:
  1751. case elfcpp::R_SPARC_16:
  1752. case elfcpp::R_SPARC_11:
  1753. case elfcpp::R_SPARC_10:
  1754. case elfcpp::R_SPARC_8:
  1755. case elfcpp::R_SPARC_7:
  1756. case elfcpp::R_SPARC_6:
  1757. case elfcpp::R_SPARC_5:
  1758. return Symbol::ABSOLUTE_REF;
  1759. case elfcpp::R_SPARC_DISP8:
  1760. case elfcpp::R_SPARC_DISP16:
  1761. case elfcpp::R_SPARC_DISP32:
  1762. case elfcpp::R_SPARC_DISP64:
  1763. case elfcpp::R_SPARC_PC_HH22:
  1764. case elfcpp::R_SPARC_PC_HM10:
  1765. case elfcpp::R_SPARC_PC_LM22:
  1766. case elfcpp::R_SPARC_PC10:
  1767. case elfcpp::R_SPARC_PC22:
  1768. case elfcpp::R_SPARC_WDISP30:
  1769. case elfcpp::R_SPARC_WDISP22:
  1770. case elfcpp::R_SPARC_WDISP19:
  1771. case elfcpp::R_SPARC_WDISP16:
  1772. case elfcpp::R_SPARC_WDISP10:
  1773. return Symbol::RELATIVE_REF;
  1774. case elfcpp::R_SPARC_PLT64:
  1775. case elfcpp::R_SPARC_PLT32:
  1776. case elfcpp::R_SPARC_HIPLT22:
  1777. case elfcpp::R_SPARC_LOPLT10:
  1778. case elfcpp::R_SPARC_PCPLT10:
  1779. return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
  1780. case elfcpp::R_SPARC_PCPLT32:
  1781. case elfcpp::R_SPARC_PCPLT22:
  1782. case elfcpp::R_SPARC_WPLT30:
  1783. return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  1784. case elfcpp::R_SPARC_GOTDATA_OP:
  1785. case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
  1786. case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
  1787. case elfcpp::R_SPARC_GOT10:
  1788. case elfcpp::R_SPARC_GOT13:
  1789. case elfcpp::R_SPARC_GOT22:
  1790. // Absolute in GOT.
  1791. return Symbol::ABSOLUTE_REF;
  1792. case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
  1793. case elfcpp::R_SPARC_TLS_GD_LO10:
  1794. case elfcpp::R_SPARC_TLS_GD_ADD:
  1795. case elfcpp::R_SPARC_TLS_GD_CALL:
  1796. case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
  1797. case elfcpp::R_SPARC_TLS_LDM_LO10:
  1798. case elfcpp::R_SPARC_TLS_LDM_ADD:
  1799. case elfcpp::R_SPARC_TLS_LDM_CALL:
  1800. case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
  1801. case elfcpp::R_SPARC_TLS_LDO_LOX10:
  1802. case elfcpp::R_SPARC_TLS_LDO_ADD:
  1803. case elfcpp::R_SPARC_TLS_LE_HIX22:
  1804. case elfcpp::R_SPARC_TLS_LE_LOX10:
  1805. case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
  1806. case elfcpp::R_SPARC_TLS_IE_LO10:
  1807. case elfcpp::R_SPARC_TLS_IE_LD:
  1808. case elfcpp::R_SPARC_TLS_IE_LDX:
  1809. case elfcpp::R_SPARC_TLS_IE_ADD:
  1810. return Symbol::TLS_REF;
  1811. case elfcpp::R_SPARC_COPY:
  1812. case elfcpp::R_SPARC_GLOB_DAT:
  1813. case elfcpp::R_SPARC_JMP_SLOT:
  1814. case elfcpp::R_SPARC_JMP_IREL:
  1815. case elfcpp::R_SPARC_RELATIVE:
  1816. case elfcpp::R_SPARC_IRELATIVE:
  1817. case elfcpp::R_SPARC_TLS_DTPMOD64:
  1818. case elfcpp::R_SPARC_TLS_DTPMOD32:
  1819. case elfcpp::R_SPARC_TLS_DTPOFF64:
  1820. case elfcpp::R_SPARC_TLS_DTPOFF32:
  1821. case elfcpp::R_SPARC_TLS_TPOFF64:
  1822. case elfcpp::R_SPARC_TLS_TPOFF32:
  1823. default:
  1824. // Not expected. We will give an error later.
  1825. return 0;
  1826. }
  1827. }
  1828. // Generate a PLT entry slot for a call to __tls_get_addr
  1829. template<int size, bool big_endian>
  1830. void
  1831. Target_sparc<size, big_endian>::Scan::generate_tls_call(Symbol_table* symtab,
  1832. Layout* layout,
  1833. Target_sparc<size, big_endian>* target)
  1834. {
  1835. Symbol* gsym = target->tls_get_addr_sym(symtab);
  1836. target->make_plt_entry(symtab, layout, gsym);
  1837. }
  1838. // Report an unsupported relocation against a local symbol.
  1839. template<int size, bool big_endian>
  1840. void
  1841. Target_sparc<size, big_endian>::Scan::unsupported_reloc_local(
  1842. Sized_relobj_file<size, big_endian>* object,
  1843. unsigned int r_type)
  1844. {
  1845. gold_error(_("%s: unsupported reloc %u against local symbol"),
  1846. object->name().c_str(), r_type);
  1847. }
  1848. // We are about to emit a dynamic relocation of type R_TYPE. If the
  1849. // dynamic linker does not support it, issue an error.
  1850. template<int size, bool big_endian>
  1851. void
  1852. Target_sparc<size, big_endian>::Scan::check_non_pic(Relobj* object, unsigned int r_type)
  1853. {
  1854. gold_assert(r_type != elfcpp::R_SPARC_NONE);
  1855. if (size == 64)
  1856. {
  1857. switch (r_type)
  1858. {
  1859. // These are the relocation types supported by glibc for sparc 64-bit.
  1860. case elfcpp::R_SPARC_RELATIVE:
  1861. case elfcpp::R_SPARC_IRELATIVE:
  1862. case elfcpp::R_SPARC_COPY:
  1863. case elfcpp::R_SPARC_32:
  1864. case elfcpp::R_SPARC_64:
  1865. case elfcpp::R_SPARC_GLOB_DAT:
  1866. case elfcpp::R_SPARC_JMP_SLOT:
  1867. case elfcpp::R_SPARC_JMP_IREL:
  1868. case elfcpp::R_SPARC_TLS_DTPMOD64:
  1869. case elfcpp::R_SPARC_TLS_DTPOFF64:
  1870. case elfcpp::R_SPARC_TLS_TPOFF64:
  1871. case elfcpp::R_SPARC_TLS_LE_HIX22:
  1872. case elfcpp::R_SPARC_TLS_LE_LOX10:
  1873. case elfcpp::R_SPARC_8:
  1874. case elfcpp::R_SPARC_16:
  1875. case elfcpp::R_SPARC_DISP8:
  1876. case elfcpp::R_SPARC_DISP16:
  1877. case elfcpp::R_SPARC_DISP32:
  1878. case elfcpp::R_SPARC_WDISP30:
  1879. case elfcpp::R_SPARC_LO10:
  1880. case elfcpp::R_SPARC_HI22:
  1881. case elfcpp::R_SPARC_OLO10:
  1882. case elfcpp::R_SPARC_H34:
  1883. case elfcpp::R_SPARC_H44:
  1884. case elfcpp::R_SPARC_M44:
  1885. case elfcpp::R_SPARC_L44:
  1886. case elfcpp::R_SPARC_HH22:
  1887. case elfcpp::R_SPARC_HM10:
  1888. case elfcpp::R_SPARC_LM22:
  1889. case elfcpp::R_SPARC_UA16:
  1890. case elfcpp::R_SPARC_UA32:
  1891. case elfcpp::R_SPARC_UA64:
  1892. return;
  1893. default:
  1894. break;
  1895. }
  1896. }
  1897. else
  1898. {
  1899. switch (r_type)
  1900. {
  1901. // These are the relocation types supported by glibc for sparc 32-bit.
  1902. case elfcpp::R_SPARC_RELATIVE:
  1903. case elfcpp::R_SPARC_IRELATIVE:
  1904. case elfcpp::R_SPARC_COPY:
  1905. case elfcpp::R_SPARC_GLOB_DAT:
  1906. case elfcpp::R_SPARC_32:
  1907. case elfcpp::R_SPARC_JMP_SLOT:
  1908. case elfcpp::R_SPARC_JMP_IREL:
  1909. case elfcpp::R_SPARC_TLS_DTPMOD32:
  1910. case elfcpp::R_SPARC_TLS_DTPOFF32:
  1911. case elfcpp::R_SPARC_TLS_TPOFF32:
  1912. case elfcpp::R_SPARC_TLS_LE_HIX22:
  1913. case elfcpp::R_SPARC_TLS_LE_LOX10:
  1914. case elfcpp::R_SPARC_8:
  1915. case elfcpp::R_SPARC_16:
  1916. case elfcpp::R_SPARC_DISP8:
  1917. case elfcpp::R_SPARC_DISP16:
  1918. case elfcpp::R_SPARC_DISP32:
  1919. case elfcpp::R_SPARC_LO10:
  1920. case elfcpp::R_SPARC_WDISP30:
  1921. case elfcpp::R_SPARC_HI22:
  1922. case elfcpp::R_SPARC_UA16:
  1923. case elfcpp::R_SPARC_UA32:
  1924. return;
  1925. default:
  1926. break;
  1927. }
  1928. }
  1929. // This prevents us from issuing more than one error per reloc
  1930. // section. But we can still wind up issuing more than one
  1931. // error per object file.
  1932. if (this->issued_non_pic_error_)
  1933. return;
  1934. gold_assert(parameters->options().output_is_position_independent());
  1935. object->error(_("requires unsupported dynamic reloc; "
  1936. "recompile with -fPIC"));
  1937. this->issued_non_pic_error_ = true;
  1938. return;
  1939. }
  1940. // Return whether we need to make a PLT entry for a relocation of the
  1941. // given type against a STT_GNU_IFUNC symbol.
  1942. template<int size, bool big_endian>
  1943. bool
  1944. Target_sparc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
  1945. Sized_relobj_file<size, big_endian>* object,
  1946. unsigned int r_type)
  1947. {
  1948. int flags = Scan::get_reference_flags(r_type);
  1949. if (flags & Symbol::TLS_REF)
  1950. gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
  1951. object->name().c_str(), r_type);
  1952. return flags != 0;
  1953. }
  1954. // Scan a relocation for a local symbol.
  1955. template<int size, bool big_endian>
  1956. inline void
  1957. Target_sparc<size, big_endian>::Scan::local(
  1958. Symbol_table* symtab,
  1959. Layout* layout,
  1960. Target_sparc<size, big_endian>* target,
  1961. Sized_relobj_file<size, big_endian>* object,
  1962. unsigned int data_shndx,
  1963. Output_section* output_section,
  1964. const elfcpp::Rela<size, big_endian>& reloc,
  1965. unsigned int r_type,
  1966. const elfcpp::Sym<size, big_endian>& lsym,
  1967. bool is_discarded)
  1968. {
  1969. if (is_discarded)
  1970. return;
  1971. bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
  1972. unsigned int orig_r_type = r_type;
  1973. r_type &= 0xff;
  1974. if (is_ifunc
  1975. && this->reloc_needs_plt_for_ifunc(object, r_type))
  1976. {
  1977. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  1978. target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
  1979. }
  1980. switch (r_type)
  1981. {
  1982. case elfcpp::R_SPARC_NONE:
  1983. case elfcpp::R_SPARC_REGISTER:
  1984. case elfcpp::R_SPARC_GNU_VTINHERIT:
  1985. case elfcpp::R_SPARC_GNU_VTENTRY:
  1986. break;
  1987. case elfcpp::R_SPARC_64:
  1988. case elfcpp::R_SPARC_32:
  1989. // If building a shared library (or a position-independent
  1990. // executable), we need to create a dynamic relocation for
  1991. // this location. The relocation applied at link time will
  1992. // apply the link-time value, so we flag the location with
  1993. // an R_SPARC_RELATIVE relocation so the dynamic loader can
  1994. // relocate it easily.
  1995. if (parameters->options().output_is_position_independent()
  1996. && ((size == 64 && r_type == elfcpp::R_SPARC_64)
  1997. || (size == 32 && r_type == elfcpp::R_SPARC_32)))
  1998. {
  1999. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2000. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2001. rela_dyn->add_local_relative(object, r_sym, elfcpp::R_SPARC_RELATIVE,
  2002. output_section, data_shndx,
  2003. reloc.get_r_offset(),
  2004. reloc.get_r_addend(), is_ifunc);
  2005. break;
  2006. }
  2007. // Fall through.
  2008. case elfcpp::R_SPARC_HIX22:
  2009. case elfcpp::R_SPARC_LOX10:
  2010. case elfcpp::R_SPARC_H34:
  2011. case elfcpp::R_SPARC_H44:
  2012. case elfcpp::R_SPARC_M44:
  2013. case elfcpp::R_SPARC_L44:
  2014. case elfcpp::R_SPARC_HH22:
  2015. case elfcpp::R_SPARC_HM10:
  2016. case elfcpp::R_SPARC_LM22:
  2017. case elfcpp::R_SPARC_UA64:
  2018. case elfcpp::R_SPARC_UA32:
  2019. case elfcpp::R_SPARC_UA16:
  2020. case elfcpp::R_SPARC_HI22:
  2021. case elfcpp::R_SPARC_LO10:
  2022. case elfcpp::R_SPARC_OLO10:
  2023. case elfcpp::R_SPARC_16:
  2024. case elfcpp::R_SPARC_11:
  2025. case elfcpp::R_SPARC_10:
  2026. case elfcpp::R_SPARC_8:
  2027. case elfcpp::R_SPARC_7:
  2028. case elfcpp::R_SPARC_6:
  2029. case elfcpp::R_SPARC_5:
  2030. // If building a shared library (or a position-independent
  2031. // executable), we need to create a dynamic relocation for
  2032. // this location.
  2033. if (parameters->options().output_is_position_independent())
  2034. {
  2035. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2036. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2037. check_non_pic(object, r_type);
  2038. if (lsym.get_st_type() != elfcpp::STT_SECTION)
  2039. {
  2040. rela_dyn->add_local(object, r_sym, orig_r_type, output_section,
  2041. data_shndx, reloc.get_r_offset(),
  2042. reloc.get_r_addend());
  2043. }
  2044. else
  2045. {
  2046. gold_assert(lsym.get_st_value() == 0);
  2047. rela_dyn->add_symbolless_local_addend(object, r_sym, orig_r_type,
  2048. output_section, data_shndx,
  2049. reloc.get_r_offset(),
  2050. reloc.get_r_addend());
  2051. }
  2052. }
  2053. break;
  2054. case elfcpp::R_SPARC_WDISP30:
  2055. case elfcpp::R_SPARC_WPLT30:
  2056. case elfcpp::R_SPARC_WDISP22:
  2057. case elfcpp::R_SPARC_WDISP19:
  2058. case elfcpp::R_SPARC_WDISP16:
  2059. case elfcpp::R_SPARC_WDISP10:
  2060. case elfcpp::R_SPARC_DISP8:
  2061. case elfcpp::R_SPARC_DISP16:
  2062. case elfcpp::R_SPARC_DISP32:
  2063. case elfcpp::R_SPARC_DISP64:
  2064. case elfcpp::R_SPARC_PC10:
  2065. case elfcpp::R_SPARC_PC22:
  2066. break;
  2067. case elfcpp::R_SPARC_GOTDATA_OP:
  2068. case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
  2069. case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
  2070. // We will optimize this into a GOT relative relocation
  2071. // and code transform the GOT load into an addition.
  2072. break;
  2073. case elfcpp::R_SPARC_GOT10:
  2074. case elfcpp::R_SPARC_GOT13:
  2075. case elfcpp::R_SPARC_GOT22:
  2076. {
  2077. // The symbol requires a GOT entry.
  2078. Output_data_got<size, big_endian>* got;
  2079. unsigned int r_sym;
  2080. got = target->got_section(symtab, layout);
  2081. r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2082. // If we are generating a shared object, we need to add a
  2083. // dynamic relocation for this symbol's GOT entry.
  2084. if (parameters->options().output_is_position_independent())
  2085. {
  2086. if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
  2087. {
  2088. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2089. unsigned int off = got->add_constant(0);
  2090. object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
  2091. rela_dyn->add_local_relative(object, r_sym,
  2092. elfcpp::R_SPARC_RELATIVE,
  2093. got, off, 0, is_ifunc);
  2094. }
  2095. }
  2096. else
  2097. got->add_local(object, r_sym, GOT_TYPE_STANDARD);
  2098. }
  2099. break;
  2100. // These are initial TLS relocs, which are expected when
  2101. // linking.
  2102. case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
  2103. case elfcpp::R_SPARC_TLS_GD_LO10:
  2104. case elfcpp::R_SPARC_TLS_GD_ADD:
  2105. case elfcpp::R_SPARC_TLS_GD_CALL:
  2106. case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
  2107. case elfcpp::R_SPARC_TLS_LDM_LO10:
  2108. case elfcpp::R_SPARC_TLS_LDM_ADD:
  2109. case elfcpp::R_SPARC_TLS_LDM_CALL:
  2110. case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
  2111. case elfcpp::R_SPARC_TLS_LDO_LOX10:
  2112. case elfcpp::R_SPARC_TLS_LDO_ADD:
  2113. case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
  2114. case elfcpp::R_SPARC_TLS_IE_LO10:
  2115. case elfcpp::R_SPARC_TLS_IE_LD:
  2116. case elfcpp::R_SPARC_TLS_IE_LDX:
  2117. case elfcpp::R_SPARC_TLS_IE_ADD:
  2118. case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
  2119. case elfcpp::R_SPARC_TLS_LE_LOX10:
  2120. {
  2121. bool output_is_shared = parameters->options().shared();
  2122. const tls::Tls_optimization optimized_type
  2123. = optimize_tls_reloc(!output_is_shared, r_type);
  2124. switch (r_type)
  2125. {
  2126. case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
  2127. case elfcpp::R_SPARC_TLS_GD_LO10:
  2128. case elfcpp::R_SPARC_TLS_GD_ADD:
  2129. case elfcpp::R_SPARC_TLS_GD_CALL:
  2130. if (optimized_type == tls::TLSOPT_NONE)
  2131. {
  2132. // Create a pair of GOT entries for the module index and
  2133. // dtv-relative offset.
  2134. Output_data_got<size, big_endian>* got
  2135. = target->got_section(symtab, layout);
  2136. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2137. unsigned int shndx = lsym.get_st_shndx();
  2138. bool is_ordinary;
  2139. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  2140. if (!is_ordinary)
  2141. object->error(_("local symbol %u has bad shndx %u"),
  2142. r_sym, shndx);
  2143. else
  2144. got->add_local_pair_with_rel(object, r_sym,
  2145. lsym.get_st_shndx(),
  2146. GOT_TYPE_TLS_PAIR,
  2147. target->rela_dyn_section(layout),
  2148. (size == 64
  2149. ? elfcpp::R_SPARC_TLS_DTPMOD64
  2150. : elfcpp::R_SPARC_TLS_DTPMOD32));
  2151. if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
  2152. generate_tls_call(symtab, layout, target);
  2153. }
  2154. else if (optimized_type != tls::TLSOPT_TO_LE)
  2155. unsupported_reloc_local(object, r_type);
  2156. break;
  2157. case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
  2158. case elfcpp::R_SPARC_TLS_LDM_LO10:
  2159. case elfcpp::R_SPARC_TLS_LDM_ADD:
  2160. case elfcpp::R_SPARC_TLS_LDM_CALL:
  2161. if (optimized_type == tls::TLSOPT_NONE)
  2162. {
  2163. // Create a GOT entry for the module index.
  2164. target->got_mod_index_entry(symtab, layout, object);
  2165. if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
  2166. generate_tls_call(symtab, layout, target);
  2167. }
  2168. else if (optimized_type != tls::TLSOPT_TO_LE)
  2169. unsupported_reloc_local(object, r_type);
  2170. break;
  2171. case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
  2172. case elfcpp::R_SPARC_TLS_LDO_LOX10:
  2173. case elfcpp::R_SPARC_TLS_LDO_ADD:
  2174. break;
  2175. case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
  2176. case elfcpp::R_SPARC_TLS_IE_LO10:
  2177. case elfcpp::R_SPARC_TLS_IE_LD:
  2178. case elfcpp::R_SPARC_TLS_IE_LDX:
  2179. case elfcpp::R_SPARC_TLS_IE_ADD:
  2180. layout->set_has_static_tls();
  2181. if (optimized_type == tls::TLSOPT_NONE)
  2182. {
  2183. // Create a GOT entry for the tp-relative offset.
  2184. Output_data_got<size, big_endian>* got
  2185. = target->got_section(symtab, layout);
  2186. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2187. if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
  2188. {
  2189. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2190. unsigned int off = got->add_constant(0);
  2191. object->set_local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET, off);
  2192. rela_dyn->add_symbolless_local_addend(object, r_sym,
  2193. (size == 64 ?
  2194. elfcpp::R_SPARC_TLS_TPOFF64 :
  2195. elfcpp::R_SPARC_TLS_TPOFF32),
  2196. got, off, 0);
  2197. }
  2198. }
  2199. else if (optimized_type != tls::TLSOPT_TO_LE)
  2200. unsupported_reloc_local(object, r_type);
  2201. break;
  2202. case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
  2203. case elfcpp::R_SPARC_TLS_LE_LOX10:
  2204. layout->set_has_static_tls();
  2205. if (output_is_shared)
  2206. {
  2207. // We need to create a dynamic relocation.
  2208. gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
  2209. unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
  2210. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2211. rela_dyn->add_symbolless_local_addend(object, r_sym, r_type,
  2212. output_section, data_shndx,
  2213. reloc.get_r_offset(), 0);
  2214. }
  2215. break;
  2216. }
  2217. }
  2218. break;
  2219. // These are relocations which should only be seen by the
  2220. // dynamic linker, and should never be seen here.
  2221. case elfcpp::R_SPARC_COPY:
  2222. case elfcpp::R_SPARC_GLOB_DAT:
  2223. case elfcpp::R_SPARC_JMP_SLOT:
  2224. case elfcpp::R_SPARC_JMP_IREL:
  2225. case elfcpp::R_SPARC_RELATIVE:
  2226. case elfcpp::R_SPARC_IRELATIVE:
  2227. case elfcpp::R_SPARC_TLS_DTPMOD64:
  2228. case elfcpp::R_SPARC_TLS_DTPMOD32:
  2229. case elfcpp::R_SPARC_TLS_DTPOFF64:
  2230. case elfcpp::R_SPARC_TLS_DTPOFF32:
  2231. case elfcpp::R_SPARC_TLS_TPOFF64:
  2232. case elfcpp::R_SPARC_TLS_TPOFF32:
  2233. gold_error(_("%s: unexpected reloc %u in object file"),
  2234. object->name().c_str(), r_type);
  2235. break;
  2236. default:
  2237. unsupported_reloc_local(object, r_type);
  2238. break;
  2239. }
  2240. }
  2241. // Report an unsupported relocation against a global symbol.
  2242. template<int size, bool big_endian>
  2243. void
  2244. Target_sparc<size, big_endian>::Scan::unsupported_reloc_global(
  2245. Sized_relobj_file<size, big_endian>* object,
  2246. unsigned int r_type,
  2247. Symbol* gsym)
  2248. {
  2249. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  2250. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  2251. }
  2252. // Scan a relocation for a global symbol.
  2253. template<int size, bool big_endian>
  2254. inline void
  2255. Target_sparc<size, big_endian>::Scan::global(
  2256. Symbol_table* symtab,
  2257. Layout* layout,
  2258. Target_sparc<size, big_endian>* target,
  2259. Sized_relobj_file<size, big_endian>* object,
  2260. unsigned int data_shndx,
  2261. Output_section* output_section,
  2262. const elfcpp::Rela<size, big_endian>& reloc,
  2263. unsigned int r_type,
  2264. Symbol* gsym)
  2265. {
  2266. unsigned int orig_r_type = r_type;
  2267. bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
  2268. // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
  2269. // section. We check here to avoid creating a dynamic reloc against
  2270. // _GLOBAL_OFFSET_TABLE_.
  2271. if (!target->has_got_section()
  2272. && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
  2273. target->got_section(symtab, layout);
  2274. r_type &= 0xff;
  2275. // A STT_GNU_IFUNC symbol may require a PLT entry.
  2276. if (is_ifunc
  2277. && this->reloc_needs_plt_for_ifunc(object, r_type))
  2278. target->make_plt_entry(symtab, layout, gsym);
  2279. switch (r_type)
  2280. {
  2281. case elfcpp::R_SPARC_NONE:
  2282. case elfcpp::R_SPARC_REGISTER:
  2283. case elfcpp::R_SPARC_GNU_VTINHERIT:
  2284. case elfcpp::R_SPARC_GNU_VTENTRY:
  2285. break;
  2286. case elfcpp::R_SPARC_PLT64:
  2287. case elfcpp::R_SPARC_PLT32:
  2288. case elfcpp::R_SPARC_HIPLT22:
  2289. case elfcpp::R_SPARC_LOPLT10:
  2290. case elfcpp::R_SPARC_PCPLT32:
  2291. case elfcpp::R_SPARC_PCPLT22:
  2292. case elfcpp::R_SPARC_PCPLT10:
  2293. case elfcpp::R_SPARC_WPLT30:
  2294. // If the symbol is fully resolved, this is just a PC32 reloc.
  2295. // Otherwise we need a PLT entry.
  2296. if (gsym->final_value_is_known())
  2297. break;
  2298. // If building a shared library, we can also skip the PLT entry
  2299. // if the symbol is defined in the output file and is protected
  2300. // or hidden.
  2301. if (gsym->is_defined()
  2302. && !gsym->is_from_dynobj()
  2303. && !gsym->is_preemptible())
  2304. break;
  2305. target->make_plt_entry(symtab, layout, gsym);
  2306. break;
  2307. case elfcpp::R_SPARC_DISP8:
  2308. case elfcpp::R_SPARC_DISP16:
  2309. case elfcpp::R_SPARC_DISP32:
  2310. case elfcpp::R_SPARC_DISP64:
  2311. case elfcpp::R_SPARC_PC_HH22:
  2312. case elfcpp::R_SPARC_PC_HM10:
  2313. case elfcpp::R_SPARC_PC_LM22:
  2314. case elfcpp::R_SPARC_PC10:
  2315. case elfcpp::R_SPARC_PC22:
  2316. case elfcpp::R_SPARC_WDISP30:
  2317. case elfcpp::R_SPARC_WDISP22:
  2318. case elfcpp::R_SPARC_WDISP19:
  2319. case elfcpp::R_SPARC_WDISP16:
  2320. case elfcpp::R_SPARC_WDISP10:
  2321. {
  2322. if (gsym->needs_plt_entry())
  2323. target->make_plt_entry(symtab, layout, gsym);
  2324. // Make a dynamic relocation if necessary.
  2325. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  2326. {
  2327. if (parameters->options().output_is_executable()
  2328. && gsym->may_need_copy_reloc())
  2329. {
  2330. target->copy_reloc(symtab, layout, object,
  2331. data_shndx, output_section, gsym,
  2332. reloc);
  2333. }
  2334. else
  2335. {
  2336. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2337. check_non_pic(object, r_type);
  2338. rela_dyn->add_global(gsym, orig_r_type, output_section, object,
  2339. data_shndx, reloc.get_r_offset(),
  2340. reloc.get_r_addend());
  2341. }
  2342. }
  2343. }
  2344. break;
  2345. case elfcpp::R_SPARC_UA64:
  2346. case elfcpp::R_SPARC_64:
  2347. case elfcpp::R_SPARC_HIX22:
  2348. case elfcpp::R_SPARC_LOX10:
  2349. case elfcpp::R_SPARC_H34:
  2350. case elfcpp::R_SPARC_H44:
  2351. case elfcpp::R_SPARC_M44:
  2352. case elfcpp::R_SPARC_L44:
  2353. case elfcpp::R_SPARC_HH22:
  2354. case elfcpp::R_SPARC_HM10:
  2355. case elfcpp::R_SPARC_LM22:
  2356. case elfcpp::R_SPARC_HI22:
  2357. case elfcpp::R_SPARC_LO10:
  2358. case elfcpp::R_SPARC_OLO10:
  2359. case elfcpp::R_SPARC_UA32:
  2360. case elfcpp::R_SPARC_32:
  2361. case elfcpp::R_SPARC_UA16:
  2362. case elfcpp::R_SPARC_16:
  2363. case elfcpp::R_SPARC_11:
  2364. case elfcpp::R_SPARC_10:
  2365. case elfcpp::R_SPARC_8:
  2366. case elfcpp::R_SPARC_7:
  2367. case elfcpp::R_SPARC_6:
  2368. case elfcpp::R_SPARC_5:
  2369. {
  2370. // Make a PLT entry if necessary.
  2371. if (gsym->needs_plt_entry())
  2372. {
  2373. target->make_plt_entry(symtab, layout, gsym);
  2374. // Since this is not a PC-relative relocation, we may be
  2375. // taking the address of a function. In that case we need to
  2376. // set the entry in the dynamic symbol table to the address of
  2377. // the PLT entry.
  2378. if (gsym->is_from_dynobj() && !parameters->options().shared())
  2379. gsym->set_needs_dynsym_value();
  2380. }
  2381. // Make a dynamic relocation if necessary.
  2382. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  2383. {
  2384. unsigned int r_off = reloc.get_r_offset();
  2385. // The assembler can sometimes emit unaligned relocations
  2386. // for dwarf2 cfi directives.
  2387. switch (r_type)
  2388. {
  2389. case elfcpp::R_SPARC_16:
  2390. if (r_off & 0x1)
  2391. orig_r_type = r_type = elfcpp::R_SPARC_UA16;
  2392. break;
  2393. case elfcpp::R_SPARC_32:
  2394. if (r_off & 0x3)
  2395. orig_r_type = r_type = elfcpp::R_SPARC_UA32;
  2396. break;
  2397. case elfcpp::R_SPARC_64:
  2398. if (r_off & 0x7)
  2399. orig_r_type = r_type = elfcpp::R_SPARC_UA64;
  2400. break;
  2401. case elfcpp::R_SPARC_UA16:
  2402. if (!(r_off & 0x1))
  2403. orig_r_type = r_type = elfcpp::R_SPARC_16;
  2404. break;
  2405. case elfcpp::R_SPARC_UA32:
  2406. if (!(r_off & 0x3))
  2407. orig_r_type = r_type = elfcpp::R_SPARC_32;
  2408. break;
  2409. case elfcpp::R_SPARC_UA64:
  2410. if (!(r_off & 0x7))
  2411. orig_r_type = r_type = elfcpp::R_SPARC_64;
  2412. break;
  2413. }
  2414. if (!parameters->options().output_is_position_independent()
  2415. && gsym->may_need_copy_reloc())
  2416. {
  2417. target->copy_reloc(symtab, layout, object,
  2418. data_shndx, output_section, gsym, reloc);
  2419. }
  2420. else if (((size == 64 && r_type == elfcpp::R_SPARC_64)
  2421. || (size == 32 && r_type == elfcpp::R_SPARC_32))
  2422. && gsym->type() == elfcpp::STT_GNU_IFUNC
  2423. && gsym->can_use_relative_reloc(false)
  2424. && !gsym->is_from_dynobj()
  2425. && !gsym->is_undefined()
  2426. && !gsym->is_preemptible())
  2427. {
  2428. // Use an IRELATIVE reloc for a locally defined
  2429. // STT_GNU_IFUNC symbol. This makes a function
  2430. // address in a PIE executable match the address in a
  2431. // shared library that it links against.
  2432. Reloc_section* rela_dyn =
  2433. target->rela_ifunc_section(layout);
  2434. unsigned int r_type = elfcpp::R_SPARC_IRELATIVE;
  2435. rela_dyn->add_symbolless_global_addend(gsym, r_type,
  2436. output_section, object,
  2437. data_shndx,
  2438. reloc.get_r_offset(),
  2439. reloc.get_r_addend());
  2440. }
  2441. else if (((size == 64 && r_type == elfcpp::R_SPARC_64)
  2442. || (size == 32 && r_type == elfcpp::R_SPARC_32))
  2443. && gsym->can_use_relative_reloc(false))
  2444. {
  2445. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2446. rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
  2447. output_section, object,
  2448. data_shndx, reloc.get_r_offset(),
  2449. reloc.get_r_addend(), is_ifunc);
  2450. }
  2451. else
  2452. {
  2453. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2454. check_non_pic(object, r_type);
  2455. if (gsym->is_from_dynobj()
  2456. || gsym->is_undefined()
  2457. || gsym->is_preemptible())
  2458. rela_dyn->add_global(gsym, orig_r_type, output_section,
  2459. object, data_shndx,
  2460. reloc.get_r_offset(),
  2461. reloc.get_r_addend());
  2462. else
  2463. rela_dyn->add_symbolless_global_addend(gsym, orig_r_type,
  2464. output_section,
  2465. object, data_shndx,
  2466. reloc.get_r_offset(),
  2467. reloc.get_r_addend());
  2468. }
  2469. }
  2470. }
  2471. break;
  2472. case elfcpp::R_SPARC_GOTDATA_OP:
  2473. case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
  2474. case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
  2475. if (gsym->is_defined()
  2476. && !gsym->is_from_dynobj()
  2477. && !gsym->is_preemptible()
  2478. && !is_ifunc)
  2479. {
  2480. // We will optimize this into a GOT relative relocation
  2481. // and code transform the GOT load into an addition.
  2482. break;
  2483. }
  2484. // Fall through.
  2485. case elfcpp::R_SPARC_GOT10:
  2486. case elfcpp::R_SPARC_GOT13:
  2487. case elfcpp::R_SPARC_GOT22:
  2488. {
  2489. // The symbol requires a GOT entry.
  2490. Output_data_got<size, big_endian>* got;
  2491. got = target->got_section(symtab, layout);
  2492. if (gsym->final_value_is_known())
  2493. {
  2494. // For a STT_GNU_IFUNC symbol we want the PLT address.
  2495. if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  2496. got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  2497. else
  2498. got->add_global(gsym, GOT_TYPE_STANDARD);
  2499. }
  2500. else
  2501. {
  2502. // If this symbol is not fully resolved, we need to add a
  2503. // GOT entry with a dynamic relocation.
  2504. bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
  2505. // Use a GLOB_DAT rather than a RELATIVE reloc if:
  2506. //
  2507. // 1) The symbol may be defined in some other module.
  2508. //
  2509. // 2) We are building a shared library and this is a
  2510. // protected symbol; using GLOB_DAT means that the dynamic
  2511. // linker can use the address of the PLT in the main
  2512. // executable when appropriate so that function address
  2513. // comparisons work.
  2514. //
  2515. // 3) This is a STT_GNU_IFUNC symbol in position dependent
  2516. // code, again so that function address comparisons work.
  2517. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2518. if (gsym->is_from_dynobj()
  2519. || gsym->is_undefined()
  2520. || gsym->is_preemptible()
  2521. || (gsym->visibility() == elfcpp::STV_PROTECTED
  2522. && parameters->options().shared())
  2523. || (gsym->type() == elfcpp::STT_GNU_IFUNC
  2524. && parameters->options().output_is_position_independent()
  2525. && !gsym->is_forced_local()))
  2526. {
  2527. unsigned int r_type = elfcpp::R_SPARC_GLOB_DAT;
  2528. // If this symbol is forced local, this relocation will
  2529. // not work properly. That's because ld.so on sparc
  2530. // (and 32-bit powerpc) expects st_value in the r_addend
  2531. // of relocations for STB_LOCAL symbols. Curiously the
  2532. // BFD linker does not promote global hidden symbols to be
  2533. // STB_LOCAL in the dynamic symbol table like Gold does.
  2534. gold_assert(!gsym->is_forced_local());
  2535. got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
  2536. r_type);
  2537. }
  2538. else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
  2539. {
  2540. unsigned int off = got->add_constant(0);
  2541. gsym->set_got_offset(GOT_TYPE_STANDARD, off);
  2542. if (is_ifunc)
  2543. {
  2544. // Tell the dynamic linker to use the PLT address
  2545. // when resolving relocations.
  2546. if (gsym->is_from_dynobj()
  2547. && !parameters->options().shared())
  2548. gsym->set_needs_dynsym_value();
  2549. }
  2550. rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
  2551. got, off, 0, is_ifunc);
  2552. }
  2553. }
  2554. }
  2555. break;
  2556. // These are initial tls relocs, which are expected when
  2557. // linking.
  2558. case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
  2559. case elfcpp::R_SPARC_TLS_GD_LO10:
  2560. case elfcpp::R_SPARC_TLS_GD_ADD:
  2561. case elfcpp::R_SPARC_TLS_GD_CALL:
  2562. case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
  2563. case elfcpp::R_SPARC_TLS_LDM_LO10:
  2564. case elfcpp::R_SPARC_TLS_LDM_ADD:
  2565. case elfcpp::R_SPARC_TLS_LDM_CALL:
  2566. case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
  2567. case elfcpp::R_SPARC_TLS_LDO_LOX10:
  2568. case elfcpp::R_SPARC_TLS_LDO_ADD:
  2569. case elfcpp::R_SPARC_TLS_LE_HIX22:
  2570. case elfcpp::R_SPARC_TLS_LE_LOX10:
  2571. case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
  2572. case elfcpp::R_SPARC_TLS_IE_LO10:
  2573. case elfcpp::R_SPARC_TLS_IE_LD:
  2574. case elfcpp::R_SPARC_TLS_IE_LDX:
  2575. case elfcpp::R_SPARC_TLS_IE_ADD:
  2576. {
  2577. const bool is_final = gsym->final_value_is_known();
  2578. const tls::Tls_optimization optimized_type
  2579. = optimize_tls_reloc(is_final, r_type);
  2580. switch (r_type)
  2581. {
  2582. case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
  2583. case elfcpp::R_SPARC_TLS_GD_LO10:
  2584. case elfcpp::R_SPARC_TLS_GD_ADD:
  2585. case elfcpp::R_SPARC_TLS_GD_CALL:
  2586. if (optimized_type == tls::TLSOPT_NONE)
  2587. {
  2588. // Create a pair of GOT entries for the module index and
  2589. // dtv-relative offset.
  2590. Output_data_got<size, big_endian>* got
  2591. = target->got_section(symtab, layout);
  2592. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
  2593. target->rela_dyn_section(layout),
  2594. (size == 64
  2595. ? elfcpp::R_SPARC_TLS_DTPMOD64
  2596. : elfcpp::R_SPARC_TLS_DTPMOD32),
  2597. (size == 64
  2598. ? elfcpp::R_SPARC_TLS_DTPOFF64
  2599. : elfcpp::R_SPARC_TLS_DTPOFF32));
  2600. // Emit R_SPARC_WPLT30 against "__tls_get_addr"
  2601. if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
  2602. generate_tls_call(symtab, layout, target);
  2603. }
  2604. else if (optimized_type == tls::TLSOPT_TO_IE)
  2605. {
  2606. // Create a GOT entry for the tp-relative offset.
  2607. Output_data_got<size, big_endian>* got
  2608. = target->got_section(symtab, layout);
  2609. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  2610. target->rela_dyn_section(layout),
  2611. (size == 64 ?
  2612. elfcpp::R_SPARC_TLS_TPOFF64 :
  2613. elfcpp::R_SPARC_TLS_TPOFF32));
  2614. }
  2615. else if (optimized_type != tls::TLSOPT_TO_LE)
  2616. unsupported_reloc_global(object, r_type, gsym);
  2617. break;
  2618. case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
  2619. case elfcpp::R_SPARC_TLS_LDM_LO10:
  2620. case elfcpp::R_SPARC_TLS_LDM_ADD:
  2621. case elfcpp::R_SPARC_TLS_LDM_CALL:
  2622. if (optimized_type == tls::TLSOPT_NONE)
  2623. {
  2624. // Create a GOT entry for the module index.
  2625. target->got_mod_index_entry(symtab, layout, object);
  2626. if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
  2627. generate_tls_call(symtab, layout, target);
  2628. }
  2629. else if (optimized_type != tls::TLSOPT_TO_LE)
  2630. unsupported_reloc_global(object, r_type, gsym);
  2631. break;
  2632. case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
  2633. case elfcpp::R_SPARC_TLS_LDO_LOX10:
  2634. case elfcpp::R_SPARC_TLS_LDO_ADD:
  2635. break;
  2636. case elfcpp::R_SPARC_TLS_LE_HIX22:
  2637. case elfcpp::R_SPARC_TLS_LE_LOX10:
  2638. layout->set_has_static_tls();
  2639. if (parameters->options().shared())
  2640. {
  2641. Reloc_section* rela_dyn = target->rela_dyn_section(layout);
  2642. rela_dyn->add_symbolless_global_addend(gsym, orig_r_type,
  2643. output_section, object,
  2644. data_shndx, reloc.get_r_offset(),
  2645. 0);
  2646. }
  2647. break;
  2648. case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
  2649. case elfcpp::R_SPARC_TLS_IE_LO10:
  2650. case elfcpp::R_SPARC_TLS_IE_LD:
  2651. case elfcpp::R_SPARC_TLS_IE_LDX:
  2652. case elfcpp::R_SPARC_TLS_IE_ADD:
  2653. layout->set_has_static_tls();
  2654. if (optimized_type == tls::TLSOPT_NONE)
  2655. {
  2656. // Create a GOT entry for the tp-relative offset.
  2657. Output_data_got<size, big_endian>* got
  2658. = target->got_section(symtab, layout);
  2659. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  2660. target->rela_dyn_section(layout),
  2661. (size == 64
  2662. ? elfcpp::R_SPARC_TLS_TPOFF64
  2663. : elfcpp::R_SPARC_TLS_TPOFF32));
  2664. }
  2665. else if (optimized_type != tls::TLSOPT_TO_LE)
  2666. unsupported_reloc_global(object, r_type, gsym);
  2667. break;
  2668. }
  2669. }
  2670. break;
  2671. // These are relocations which should only be seen by the
  2672. // dynamic linker, and should never be seen here.
  2673. case elfcpp::R_SPARC_COPY:
  2674. case elfcpp::R_SPARC_GLOB_DAT:
  2675. case elfcpp::R_SPARC_JMP_SLOT:
  2676. case elfcpp::R_SPARC_JMP_IREL:
  2677. case elfcpp::R_SPARC_RELATIVE:
  2678. case elfcpp::R_SPARC_IRELATIVE:
  2679. case elfcpp::R_SPARC_TLS_DTPMOD64:
  2680. case elfcpp::R_SPARC_TLS_DTPMOD32:
  2681. case elfcpp::R_SPARC_TLS_DTPOFF64:
  2682. case elfcpp::R_SPARC_TLS_DTPOFF32:
  2683. case elfcpp::R_SPARC_TLS_TPOFF64:
  2684. case elfcpp::R_SPARC_TLS_TPOFF32:
  2685. gold_error(_("%s: unexpected reloc %u in object file"),
  2686. object->name().c_str(), r_type);
  2687. break;
  2688. default:
  2689. unsupported_reloc_global(object, r_type, gsym);
  2690. break;
  2691. }
  2692. }
  2693. // Make a new symbol table entry.
  2694. // STT_SPARC_REGISTER symbols require special handling,
  2695. // so we intercept these symbols and keep track of them separately.
  2696. // We will resolve register symbols here and output them at symbol
  2697. // finalization time.
  2698. template<int size, bool big_endian>
  2699. Sized_symbol<size>*
  2700. Target_sparc<size, big_endian>::make_symbol(const char* name,
  2701. elfcpp::STT type,
  2702. Object* object,
  2703. unsigned int shndx,
  2704. uint64_t value)
  2705. {
  2706. // REGISTER symbols are used only on SPARC-64.
  2707. if (size == 64 && type == elfcpp::STT_SPARC_REGISTER)
  2708. {
  2709. // Ignore REGISTER symbols in dynamic objects.
  2710. if (object->is_dynamic())
  2711. return NULL;
  2712. // Only registers 2, 3, 6, and 7 can be declared global.
  2713. int reg = value;
  2714. switch (reg)
  2715. {
  2716. case 2: case 3:
  2717. reg -= 2;
  2718. break;
  2719. case 6: case 7:
  2720. reg -= 4;
  2721. break;
  2722. default:
  2723. gold_error(_("%s: only registers %%g[2367] can be declared "
  2724. "using STT_REGISTER"),
  2725. object->name().c_str());
  2726. return NULL;
  2727. }
  2728. Register_symbol& rsym = this->register_syms_[reg];
  2729. if (rsym.name == NULL)
  2730. {
  2731. rsym.name = name;
  2732. rsym.shndx = shndx;
  2733. rsym.obj = object;
  2734. }
  2735. else
  2736. {
  2737. if (strcmp(rsym.name, name) != 0)
  2738. {
  2739. gold_error(_("%s: register %%g%d declared as '%s'; "
  2740. "previously declared as '%s' in %s"),
  2741. object->name().c_str(),
  2742. static_cast<int>(value),
  2743. *name ? name : "#scratch",
  2744. *rsym.name ? rsym.name : "#scratch",
  2745. rsym.obj->name().c_str());
  2746. return NULL;
  2747. }
  2748. }
  2749. return NULL;
  2750. }
  2751. return new Sized_symbol<size>();
  2752. }
  2753. // Process relocations for gc.
  2754. template<int size, bool big_endian>
  2755. void
  2756. Target_sparc<size, big_endian>::gc_process_relocs(
  2757. Symbol_table* symtab,
  2758. Layout* layout,
  2759. Sized_relobj_file<size, big_endian>* object,
  2760. unsigned int data_shndx,
  2761. unsigned int,
  2762. const unsigned char* prelocs,
  2763. size_t reloc_count,
  2764. Output_section* output_section,
  2765. bool needs_special_offset_handling,
  2766. size_t local_symbol_count,
  2767. const unsigned char* plocal_symbols)
  2768. {
  2769. typedef Target_sparc<size, big_endian> Sparc;
  2770. typedef typename Target_sparc<size, big_endian>::Scan Scan;
  2771. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  2772. Classify_reloc;
  2773. gold::gc_process_relocs<size, big_endian, Sparc, Scan, Classify_reloc>(
  2774. symtab,
  2775. layout,
  2776. this,
  2777. object,
  2778. data_shndx,
  2779. prelocs,
  2780. reloc_count,
  2781. output_section,
  2782. needs_special_offset_handling,
  2783. local_symbol_count,
  2784. plocal_symbols);
  2785. }
  2786. // Scan relocations for a section.
  2787. template<int size, bool big_endian>
  2788. void
  2789. Target_sparc<size, big_endian>::scan_relocs(
  2790. Symbol_table* symtab,
  2791. Layout* layout,
  2792. Sized_relobj_file<size, big_endian>* object,
  2793. unsigned int data_shndx,
  2794. unsigned int sh_type,
  2795. const unsigned char* prelocs,
  2796. size_t reloc_count,
  2797. Output_section* output_section,
  2798. bool needs_special_offset_handling,
  2799. size_t local_symbol_count,
  2800. const unsigned char* plocal_symbols)
  2801. {
  2802. typedef Target_sparc<size, big_endian> Sparc;
  2803. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  2804. Classify_reloc;
  2805. if (sh_type == elfcpp::SHT_REL)
  2806. {
  2807. gold_error(_("%s: unsupported REL reloc section"),
  2808. object->name().c_str());
  2809. return;
  2810. }
  2811. gold::scan_relocs<size, big_endian, Sparc, Scan, Classify_reloc>(
  2812. symtab,
  2813. layout,
  2814. this,
  2815. object,
  2816. data_shndx,
  2817. prelocs,
  2818. reloc_count,
  2819. output_section,
  2820. needs_special_offset_handling,
  2821. local_symbol_count,
  2822. plocal_symbols);
  2823. }
  2824. // Finalize the sections.
  2825. template<int size, bool big_endian>
  2826. void
  2827. Target_sparc<size, big_endian>::do_finalize_sections(
  2828. Layout* layout,
  2829. const Input_objects*,
  2830. Symbol_table* symtab)
  2831. {
  2832. if (this->plt_)
  2833. this->plt_->emit_pending_ifunc_relocs();
  2834. // Fill in some more dynamic tags.
  2835. const Reloc_section* rel_plt = (this->plt_ == NULL
  2836. ? NULL
  2837. : this->plt_->rel_plt());
  2838. layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
  2839. this->rela_dyn_, true, true);
  2840. // Emit any relocs we saved in an attempt to avoid generating COPY
  2841. // relocs.
  2842. if (this->copy_relocs_.any_saved_relocs())
  2843. this->copy_relocs_.emit(this->rela_dyn_section(layout));
  2844. if (parameters->doing_static_link()
  2845. && (this->plt_ == NULL || !this->plt_->has_ifunc_section()))
  2846. {
  2847. // If linking statically, make sure that the __rela_iplt symbols
  2848. // were defined if necessary, even if we didn't create a PLT.
  2849. static const Define_symbol_in_segment syms[] =
  2850. {
  2851. {
  2852. "__rela_iplt_start", // name
  2853. elfcpp::PT_LOAD, // segment_type
  2854. elfcpp::PF_W, // segment_flags_set
  2855. elfcpp::PF(0), // segment_flags_clear
  2856. 0, // value
  2857. 0, // size
  2858. elfcpp::STT_NOTYPE, // type
  2859. elfcpp::STB_GLOBAL, // binding
  2860. elfcpp::STV_HIDDEN, // visibility
  2861. 0, // nonvis
  2862. Symbol::SEGMENT_START, // offset_from_base
  2863. true // only_if_ref
  2864. },
  2865. {
  2866. "__rela_iplt_end", // name
  2867. elfcpp::PT_LOAD, // segment_type
  2868. elfcpp::PF_W, // segment_flags_set
  2869. elfcpp::PF(0), // segment_flags_clear
  2870. 0, // value
  2871. 0, // size
  2872. elfcpp::STT_NOTYPE, // type
  2873. elfcpp::STB_GLOBAL, // binding
  2874. elfcpp::STV_HIDDEN, // visibility
  2875. 0, // nonvis
  2876. Symbol::SEGMENT_START, // offset_from_base
  2877. true // only_if_ref
  2878. }
  2879. };
  2880. symtab->define_symbols(layout, 2, syms,
  2881. layout->script_options()->saw_sections_clause());
  2882. }
  2883. for (int reg = 0; reg < 4; ++reg)
  2884. {
  2885. Register_symbol& rsym = this->register_syms_[reg];
  2886. if (rsym.name != NULL)
  2887. {
  2888. int value = reg < 3 ? reg + 2 : reg + 4;
  2889. Sized_symbol<size>* sym = new Sized_symbol<size>();
  2890. if (rsym.shndx == elfcpp::SHN_UNDEF)
  2891. sym->init_undefined(rsym.name, NULL, value,
  2892. elfcpp::STT_SPARC_REGISTER, elfcpp::STB_GLOBAL,
  2893. elfcpp::STV_DEFAULT, 0);
  2894. else
  2895. sym->init_constant(rsym.name, NULL, value, 0,
  2896. elfcpp::STT_SPARC_REGISTER, elfcpp::STB_GLOBAL,
  2897. elfcpp::STV_DEFAULT, 0, false);
  2898. symtab->add_target_global_symbol(sym);
  2899. layout->add_target_specific_dynamic_tag(elfcpp::DT_SPARC_REGISTER,
  2900. value);
  2901. }
  2902. }
  2903. }
  2904. // Perform a relocation.
  2905. template<int size, bool big_endian>
  2906. inline bool
  2907. Target_sparc<size, big_endian>::Relocate::relocate(
  2908. const Relocate_info<size, big_endian>* relinfo,
  2909. unsigned int,
  2910. Target_sparc* target,
  2911. Output_section*,
  2912. size_t relnum,
  2913. const unsigned char* preloc,
  2914. const Sized_symbol<size>* gsym,
  2915. const Symbol_value<size>* psymval,
  2916. unsigned char* view,
  2917. typename elfcpp::Elf_types<size>::Elf_Addr address,
  2918. section_size_type view_size)
  2919. {
  2920. const elfcpp::Rela<size, big_endian> rela(preloc);
  2921. unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
  2922. bool orig_is_ifunc = psymval->is_ifunc_symbol();
  2923. r_type &= 0xff;
  2924. if (this->ignore_gd_add_)
  2925. {
  2926. if (r_type != elfcpp::R_SPARC_TLS_GD_ADD)
  2927. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  2928. _("missing expected TLS relocation"));
  2929. else
  2930. {
  2931. this->ignore_gd_add_ = false;
  2932. return false;
  2933. }
  2934. }
  2935. if (view == NULL)
  2936. return true;
  2937. if (this->reloc_adjust_addr_ == view)
  2938. view -= 4;
  2939. typedef Sparc_relocate_functions<size, big_endian> Reloc;
  2940. const Sized_relobj_file<size, big_endian>* object = relinfo->object;
  2941. // Pick the value to use for symbols defined in shared objects.
  2942. Symbol_value<size> symval;
  2943. if (gsym != NULL
  2944. && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
  2945. {
  2946. elfcpp::Elf_Xword value;
  2947. value = target->plt_address_for_global(gsym);
  2948. symval.set_output_value(value);
  2949. psymval = &symval;
  2950. }
  2951. else if (gsym == NULL && orig_is_ifunc)
  2952. {
  2953. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  2954. if (object->local_has_plt_offset(r_sym))
  2955. {
  2956. symval.set_output_value(target->plt_address_for_local(object, r_sym));
  2957. psymval = &symval;
  2958. }
  2959. }
  2960. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  2961. // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
  2962. // pointer points to the beginning, not the end, of the table.
  2963. // So we just use the plain offset.
  2964. unsigned int got_offset = 0;
  2965. bool gdop_valid = false;
  2966. switch (r_type)
  2967. {
  2968. case elfcpp::R_SPARC_GOTDATA_OP:
  2969. case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
  2970. case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
  2971. // If this is local, we did not create a GOT entry because we
  2972. // intend to transform this into a GOT relative relocation.
  2973. if (gsym == NULL
  2974. || (gsym->is_defined()
  2975. && !gsym->is_from_dynobj()
  2976. && !gsym->is_preemptible()
  2977. && !orig_is_ifunc))
  2978. {
  2979. got_offset = psymval->value(object, addend) - target->got_address();
  2980. gdop_valid = true;
  2981. break;
  2982. }
  2983. // Fall through.
  2984. case elfcpp::R_SPARC_GOT10:
  2985. case elfcpp::R_SPARC_GOT13:
  2986. case elfcpp::R_SPARC_GOT22:
  2987. if (gsym != NULL)
  2988. {
  2989. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  2990. got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
  2991. }
  2992. else
  2993. {
  2994. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  2995. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
  2996. got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
  2997. }
  2998. break;
  2999. default:
  3000. break;
  3001. }
  3002. switch (r_type)
  3003. {
  3004. case elfcpp::R_SPARC_NONE:
  3005. case elfcpp::R_SPARC_REGISTER:
  3006. case elfcpp::R_SPARC_GNU_VTINHERIT:
  3007. case elfcpp::R_SPARC_GNU_VTENTRY:
  3008. break;
  3009. case elfcpp::R_SPARC_8:
  3010. Relocate_functions<size, big_endian>::rela8(view, object,
  3011. psymval, addend);
  3012. break;
  3013. case elfcpp::R_SPARC_16:
  3014. if (rela.get_r_offset() & 0x1)
  3015. {
  3016. // The assembler can sometimes emit unaligned relocations
  3017. // for dwarf2 cfi directives.
  3018. Reloc::ua16(view, object, psymval, addend);
  3019. }
  3020. else
  3021. Relocate_functions<size, big_endian>::rela16(view, object,
  3022. psymval, addend);
  3023. break;
  3024. case elfcpp::R_SPARC_32:
  3025. if (!parameters->options().output_is_position_independent())
  3026. {
  3027. if (rela.get_r_offset() & 0x3)
  3028. {
  3029. // The assembler can sometimes emit unaligned relocations
  3030. // for dwarf2 cfi directives.
  3031. Reloc::ua32(view, object, psymval, addend);
  3032. }
  3033. else
  3034. Relocate_functions<size, big_endian>::rela32(view, object,
  3035. psymval, addend);
  3036. }
  3037. break;
  3038. case elfcpp::R_SPARC_DISP8:
  3039. Reloc::disp8(view, object, psymval, addend, address);
  3040. break;
  3041. case elfcpp::R_SPARC_DISP16:
  3042. Reloc::disp16(view, object, psymval, addend, address);
  3043. break;
  3044. case elfcpp::R_SPARC_DISP32:
  3045. Reloc::disp32(view, object, psymval, addend, address);
  3046. break;
  3047. case elfcpp::R_SPARC_DISP64:
  3048. Reloc::disp64(view, object, psymval, addend, address);
  3049. break;
  3050. case elfcpp::R_SPARC_WDISP30:
  3051. case elfcpp::R_SPARC_WPLT30:
  3052. Reloc::wdisp30(view, object, psymval, addend, address);
  3053. if (target->may_relax())
  3054. relax_call(target, view, rela, view_size);
  3055. break;
  3056. case elfcpp::R_SPARC_WDISP22:
  3057. Reloc::wdisp22(view, object, psymval, addend, address);
  3058. break;
  3059. case elfcpp::R_SPARC_WDISP19:
  3060. Reloc::wdisp19(view, object, psymval, addend, address);
  3061. break;
  3062. case elfcpp::R_SPARC_WDISP16:
  3063. Reloc::wdisp16(view, object, psymval, addend, address);
  3064. break;
  3065. case elfcpp::R_SPARC_WDISP10:
  3066. Reloc::wdisp10(view, object, psymval, addend, address);
  3067. break;
  3068. case elfcpp::R_SPARC_HI22:
  3069. Reloc::hi22(view, object, psymval, addend);
  3070. break;
  3071. case elfcpp::R_SPARC_22:
  3072. Reloc::rela32_22(view, object, psymval, addend);
  3073. break;
  3074. case elfcpp::R_SPARC_13:
  3075. Reloc::rela32_13(view, object, psymval, addend);
  3076. break;
  3077. case elfcpp::R_SPARC_LO10:
  3078. Reloc::lo10(view, object, psymval, addend);
  3079. break;
  3080. case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
  3081. if (gdop_valid)
  3082. {
  3083. Reloc::gdop_lox10(view, got_offset);
  3084. break;
  3085. }
  3086. // Fall through.
  3087. case elfcpp::R_SPARC_GOT10:
  3088. Reloc::lo10(view, got_offset, addend);
  3089. break;
  3090. case elfcpp::R_SPARC_GOTDATA_OP:
  3091. if (gdop_valid)
  3092. {
  3093. typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
  3094. Insntype* wv = reinterpret_cast<Insntype*>(view);
  3095. Insntype val;
  3096. // {ld,ldx} [%rs1 + %rs2], %rd --> add %rs1, %rs2, %rd
  3097. val = elfcpp::Swap<32, true>::readval(wv);
  3098. val = 0x80000000 | (val & 0x3e07c01f);
  3099. elfcpp::Swap<32, true>::writeval(wv, val);
  3100. }
  3101. break;
  3102. case elfcpp::R_SPARC_GOT13:
  3103. Reloc::rela32_13(view, got_offset, addend);
  3104. break;
  3105. case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
  3106. if (gdop_valid)
  3107. {
  3108. Reloc::gdop_hix22(view, got_offset);
  3109. break;
  3110. }
  3111. // Fall through.
  3112. case elfcpp::R_SPARC_GOT22:
  3113. Reloc::hi22(view, got_offset, addend);
  3114. break;
  3115. case elfcpp::R_SPARC_PC10:
  3116. Reloc::pc10(view, object, psymval, addend, address);
  3117. break;
  3118. case elfcpp::R_SPARC_PC22:
  3119. Reloc::pc22(view, object, psymval, addend, address);
  3120. break;
  3121. case elfcpp::R_SPARC_TLS_DTPOFF32:
  3122. case elfcpp::R_SPARC_UA32:
  3123. Reloc::ua32(view, object, psymval, addend);
  3124. break;
  3125. case elfcpp::R_SPARC_PLT64:
  3126. Relocate_functions<size, big_endian>::rela64(view, object,
  3127. psymval, addend);
  3128. break;
  3129. case elfcpp::R_SPARC_PLT32:
  3130. Relocate_functions<size, big_endian>::rela32(view, object,
  3131. psymval, addend);
  3132. break;
  3133. case elfcpp::R_SPARC_HIPLT22:
  3134. Reloc::hi22(view, object, psymval, addend);
  3135. break;
  3136. case elfcpp::R_SPARC_LOPLT10:
  3137. Reloc::lo10(view, object, psymval, addend);
  3138. break;
  3139. case elfcpp::R_SPARC_PCPLT32:
  3140. Reloc::disp32(view, object, psymval, addend, address);
  3141. break;
  3142. case elfcpp::R_SPARC_PCPLT22:
  3143. Reloc::pcplt22(view, object, psymval, addend, address);
  3144. break;
  3145. case elfcpp::R_SPARC_PCPLT10:
  3146. Reloc::lo10(view, object, psymval, addend, address);
  3147. break;
  3148. case elfcpp::R_SPARC_64:
  3149. if (!parameters->options().output_is_position_independent())
  3150. {
  3151. if (rela.get_r_offset() & 0x7)
  3152. {
  3153. // The assembler can sometimes emit unaligned relocations
  3154. // for dwarf2 cfi directives.
  3155. Reloc::ua64(view, object, psymval, addend);
  3156. }
  3157. else
  3158. Relocate_functions<size, big_endian>::rela64(view, object,
  3159. psymval, addend);
  3160. }
  3161. break;
  3162. case elfcpp::R_SPARC_OLO10:
  3163. {
  3164. unsigned int addend2 = rela.get_r_info() & 0xffffffff;
  3165. addend2 = ((addend2 >> 8) ^ 0x800000) - 0x800000;
  3166. Reloc::olo10(view, object, psymval, addend, addend2);
  3167. }
  3168. break;
  3169. case elfcpp::R_SPARC_HH22:
  3170. Reloc::hh22(view, object, psymval, addend);
  3171. break;
  3172. case elfcpp::R_SPARC_PC_HH22:
  3173. Reloc::pc_hh22(view, object, psymval, addend, address);
  3174. break;
  3175. case elfcpp::R_SPARC_HM10:
  3176. Reloc::hm10(view, object, psymval, addend);
  3177. break;
  3178. case elfcpp::R_SPARC_PC_HM10:
  3179. Reloc::pc_hm10(view, object, psymval, addend, address);
  3180. break;
  3181. case elfcpp::R_SPARC_LM22:
  3182. Reloc::hi22(view, object, psymval, addend);
  3183. break;
  3184. case elfcpp::R_SPARC_PC_LM22:
  3185. Reloc::pcplt22(view, object, psymval, addend, address);
  3186. break;
  3187. case elfcpp::R_SPARC_11:
  3188. Reloc::rela32_11(view, object, psymval, addend);
  3189. break;
  3190. case elfcpp::R_SPARC_10:
  3191. Reloc::rela32_10(view, object, psymval, addend);
  3192. break;
  3193. case elfcpp::R_SPARC_7:
  3194. Reloc::rela32_7(view, object, psymval, addend);
  3195. break;
  3196. case elfcpp::R_SPARC_6:
  3197. Reloc::rela32_6(view, object, psymval, addend);
  3198. break;
  3199. case elfcpp::R_SPARC_5:
  3200. Reloc::rela32_5(view, object, psymval, addend);
  3201. break;
  3202. case elfcpp::R_SPARC_HIX22:
  3203. Reloc::hix22(view, object, psymval, addend);
  3204. break;
  3205. case elfcpp::R_SPARC_LOX10:
  3206. Reloc::lox10(view, object, psymval, addend);
  3207. break;
  3208. case elfcpp::R_SPARC_H34:
  3209. Reloc::h34(view, object, psymval, addend);
  3210. break;
  3211. case elfcpp::R_SPARC_H44:
  3212. Reloc::h44(view, object, psymval, addend);
  3213. break;
  3214. case elfcpp::R_SPARC_M44:
  3215. Reloc::m44(view, object, psymval, addend);
  3216. break;
  3217. case elfcpp::R_SPARC_L44:
  3218. Reloc::l44(view, object, psymval, addend);
  3219. break;
  3220. case elfcpp::R_SPARC_TLS_DTPOFF64:
  3221. case elfcpp::R_SPARC_UA64:
  3222. Reloc::ua64(view, object, psymval, addend);
  3223. break;
  3224. case elfcpp::R_SPARC_UA16:
  3225. Reloc::ua16(view, object, psymval, addend);
  3226. break;
  3227. case elfcpp::R_SPARC_TLS_GD_HI22:
  3228. case elfcpp::R_SPARC_TLS_GD_LO10:
  3229. case elfcpp::R_SPARC_TLS_GD_ADD:
  3230. case elfcpp::R_SPARC_TLS_GD_CALL:
  3231. case elfcpp::R_SPARC_TLS_LDM_HI22:
  3232. case elfcpp::R_SPARC_TLS_LDM_LO10:
  3233. case elfcpp::R_SPARC_TLS_LDM_ADD:
  3234. case elfcpp::R_SPARC_TLS_LDM_CALL:
  3235. case elfcpp::R_SPARC_TLS_LDO_HIX22:
  3236. case elfcpp::R_SPARC_TLS_LDO_LOX10:
  3237. case elfcpp::R_SPARC_TLS_LDO_ADD:
  3238. case elfcpp::R_SPARC_TLS_IE_HI22:
  3239. case elfcpp::R_SPARC_TLS_IE_LO10:
  3240. case elfcpp::R_SPARC_TLS_IE_LD:
  3241. case elfcpp::R_SPARC_TLS_IE_LDX:
  3242. case elfcpp::R_SPARC_TLS_IE_ADD:
  3243. case elfcpp::R_SPARC_TLS_LE_HIX22:
  3244. case elfcpp::R_SPARC_TLS_LE_LOX10:
  3245. this->relocate_tls(relinfo, target, relnum, rela,
  3246. r_type, gsym, psymval, view,
  3247. address, view_size);
  3248. break;
  3249. case elfcpp::R_SPARC_COPY:
  3250. case elfcpp::R_SPARC_GLOB_DAT:
  3251. case elfcpp::R_SPARC_JMP_SLOT:
  3252. case elfcpp::R_SPARC_JMP_IREL:
  3253. case elfcpp::R_SPARC_RELATIVE:
  3254. case elfcpp::R_SPARC_IRELATIVE:
  3255. // These are outstanding tls relocs, which are unexpected when
  3256. // linking.
  3257. case elfcpp::R_SPARC_TLS_DTPMOD64:
  3258. case elfcpp::R_SPARC_TLS_DTPMOD32:
  3259. case elfcpp::R_SPARC_TLS_TPOFF64:
  3260. case elfcpp::R_SPARC_TLS_TPOFF32:
  3261. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3262. _("unexpected reloc %u in object file"),
  3263. r_type);
  3264. break;
  3265. default:
  3266. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3267. _("unsupported reloc %u"),
  3268. r_type);
  3269. break;
  3270. }
  3271. return true;
  3272. }
  3273. // Perform a TLS relocation.
  3274. template<int size, bool big_endian>
  3275. inline void
  3276. Target_sparc<size, big_endian>::Relocate::relocate_tls(
  3277. const Relocate_info<size, big_endian>* relinfo,
  3278. Target_sparc<size, big_endian>* target,
  3279. size_t relnum,
  3280. const elfcpp::Rela<size, big_endian>& rela,
  3281. unsigned int r_type,
  3282. const Sized_symbol<size>* gsym,
  3283. const Symbol_value<size>* psymval,
  3284. unsigned char* view,
  3285. typename elfcpp::Elf_types<size>::Elf_Addr address,
  3286. section_size_type)
  3287. {
  3288. Output_segment* tls_segment = relinfo->layout->tls_segment();
  3289. typedef Sparc_relocate_functions<size, big_endian> Reloc;
  3290. const Sized_relobj_file<size, big_endian>* object = relinfo->object;
  3291. typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
  3292. const elfcpp::Elf_Xword addend = rela.get_r_addend();
  3293. typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
  3294. const bool is_final =
  3295. (gsym == NULL
  3296. ? !parameters->options().shared()
  3297. : gsym->final_value_is_known());
  3298. const tls::Tls_optimization optimized_type
  3299. = optimize_tls_reloc(is_final, r_type);
  3300. switch (r_type)
  3301. {
  3302. case elfcpp::R_SPARC_TLS_GD_HI22:
  3303. case elfcpp::R_SPARC_TLS_GD_LO10:
  3304. case elfcpp::R_SPARC_TLS_GD_ADD:
  3305. case elfcpp::R_SPARC_TLS_GD_CALL:
  3306. if (optimized_type == tls::TLSOPT_TO_LE)
  3307. {
  3308. Insntype* wv = reinterpret_cast<Insntype*>(view);
  3309. Insntype val;
  3310. value -= tls_segment->memsz();
  3311. switch (r_type)
  3312. {
  3313. case elfcpp::R_SPARC_TLS_GD_HI22:
  3314. // TLS_GD_HI22 --> TLS_LE_HIX22
  3315. Reloc::hix22(view, value, addend);
  3316. break;
  3317. case elfcpp::R_SPARC_TLS_GD_LO10:
  3318. // TLS_GD_LO10 --> TLS_LE_LOX10
  3319. Reloc::lox10(view, value, addend);
  3320. break;
  3321. case elfcpp::R_SPARC_TLS_GD_ADD:
  3322. // add %reg1, %reg2, %reg3 --> mov %g7, %reg2, %reg3
  3323. val = elfcpp::Swap<32, true>::readval(wv);
  3324. val = (val & ~0x7c000) | 0x1c000;
  3325. elfcpp::Swap<32, true>::writeval(wv, val);
  3326. break;
  3327. case elfcpp::R_SPARC_TLS_GD_CALL:
  3328. // call __tls_get_addr --> nop
  3329. elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
  3330. break;
  3331. }
  3332. break;
  3333. }
  3334. else
  3335. {
  3336. unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
  3337. ? GOT_TYPE_TLS_OFFSET
  3338. : GOT_TYPE_TLS_PAIR);
  3339. if (gsym != NULL)
  3340. {
  3341. gold_assert(gsym->has_got_offset(got_type));
  3342. value = gsym->got_offset(got_type);
  3343. }
  3344. else
  3345. {
  3346. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  3347. gold_assert(object->local_has_got_offset(r_sym, got_type));
  3348. value = object->local_got_offset(r_sym, got_type);
  3349. }
  3350. if (optimized_type == tls::TLSOPT_TO_IE)
  3351. {
  3352. Insntype* wv = reinterpret_cast<Insntype*>(view);
  3353. Insntype val;
  3354. switch (r_type)
  3355. {
  3356. case elfcpp::R_SPARC_TLS_GD_HI22:
  3357. // TLS_GD_HI22 --> TLS_IE_HI22
  3358. Reloc::hi22(view, value, addend);
  3359. break;
  3360. case elfcpp::R_SPARC_TLS_GD_LO10:
  3361. // TLS_GD_LO10 --> TLS_IE_LO10
  3362. Reloc::lo10(view, value, addend);
  3363. break;
  3364. case elfcpp::R_SPARC_TLS_GD_ADD:
  3365. // add %reg1, %reg2, %reg3 --> ld [%reg1 + %reg2], %reg3
  3366. val = elfcpp::Swap<32, true>::readval(wv);
  3367. if (size == 64)
  3368. val |= 0xc0580000;
  3369. else
  3370. val |= 0xc0000000;
  3371. elfcpp::Swap<32, true>::writeval(wv, val);
  3372. break;
  3373. case elfcpp::R_SPARC_TLS_GD_CALL:
  3374. // The compiler can put the TLS_GD_ADD instruction
  3375. // into the delay slot of the call. If so, we need
  3376. // to transpose the two instructions so that the
  3377. // new sequence works properly.
  3378. //
  3379. // The test we use is if the instruction in the
  3380. // delay slot is an add with destination register
  3381. // equal to %o0
  3382. val = elfcpp::Swap<32, true>::readval(wv + 1);
  3383. if ((val & 0x81f80000) == 0x80000000
  3384. && ((val >> 25) & 0x1f) == 0x8)
  3385. {
  3386. if (size == 64)
  3387. val |= 0xc0580000;
  3388. else
  3389. val |= 0xc0000000;
  3390. elfcpp::Swap<32, true>::writeval(wv, val);
  3391. wv += 1;
  3392. this->ignore_gd_add_ = true;
  3393. }
  3394. else
  3395. {
  3396. // Even if the delay slot isn't the TLS_GD_ADD
  3397. // instruction, we still have to handle the case
  3398. // where it sets up %o0 in some other way.
  3399. elfcpp::Swap<32, true>::writeval(wv, val);
  3400. wv += 1;
  3401. this->reloc_adjust_addr_ = view + 4;
  3402. }
  3403. // call __tls_get_addr --> add %g7, %o0, %o0
  3404. elfcpp::Swap<32, true>::writeval(wv, 0x9001c008);
  3405. break;
  3406. }
  3407. break;
  3408. }
  3409. else if (optimized_type == tls::TLSOPT_NONE)
  3410. {
  3411. switch (r_type)
  3412. {
  3413. case elfcpp::R_SPARC_TLS_GD_HI22:
  3414. Reloc::hi22(view, value, addend);
  3415. break;
  3416. case elfcpp::R_SPARC_TLS_GD_LO10:
  3417. Reloc::lo10(view, value, addend);
  3418. break;
  3419. case elfcpp::R_SPARC_TLS_GD_ADD:
  3420. break;
  3421. case elfcpp::R_SPARC_TLS_GD_CALL:
  3422. {
  3423. Symbol_value<size> symval;
  3424. elfcpp::Elf_Xword value;
  3425. Symbol* tsym;
  3426. tsym = target->tls_get_addr_sym_;
  3427. gold_assert(tsym);
  3428. value = (target->plt_section()->address() +
  3429. tsym->plt_offset());
  3430. symval.set_output_value(value);
  3431. Reloc::wdisp30(view, object, &symval, addend, address);
  3432. }
  3433. break;
  3434. }
  3435. break;
  3436. }
  3437. }
  3438. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3439. _("unsupported reloc %u"),
  3440. r_type);
  3441. break;
  3442. case elfcpp::R_SPARC_TLS_LDM_HI22:
  3443. case elfcpp::R_SPARC_TLS_LDM_LO10:
  3444. case elfcpp::R_SPARC_TLS_LDM_ADD:
  3445. case elfcpp::R_SPARC_TLS_LDM_CALL:
  3446. if (optimized_type == tls::TLSOPT_TO_LE)
  3447. {
  3448. Insntype* wv = reinterpret_cast<Insntype*>(view);
  3449. switch (r_type)
  3450. {
  3451. case elfcpp::R_SPARC_TLS_LDM_HI22:
  3452. case elfcpp::R_SPARC_TLS_LDM_LO10:
  3453. case elfcpp::R_SPARC_TLS_LDM_ADD:
  3454. elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
  3455. break;
  3456. case elfcpp::R_SPARC_TLS_LDM_CALL:
  3457. elfcpp::Swap<32, true>::writeval(wv, sparc_mov_g0_o0);
  3458. break;
  3459. }
  3460. break;
  3461. }
  3462. else if (optimized_type == tls::TLSOPT_NONE)
  3463. {
  3464. // Relocate the field with the offset of the GOT entry for
  3465. // the module index.
  3466. unsigned int got_offset;
  3467. got_offset = target->got_mod_index_entry(NULL, NULL, NULL);
  3468. switch (r_type)
  3469. {
  3470. case elfcpp::R_SPARC_TLS_LDM_HI22:
  3471. Reloc::hi22(view, got_offset, addend);
  3472. break;
  3473. case elfcpp::R_SPARC_TLS_LDM_LO10:
  3474. Reloc::lo10(view, got_offset, addend);
  3475. break;
  3476. case elfcpp::R_SPARC_TLS_LDM_ADD:
  3477. break;
  3478. case elfcpp::R_SPARC_TLS_LDM_CALL:
  3479. {
  3480. Symbol_value<size> symval;
  3481. elfcpp::Elf_Xword value;
  3482. Symbol* tsym;
  3483. tsym = target->tls_get_addr_sym_;
  3484. gold_assert(tsym);
  3485. value = (target->plt_section()->address() +
  3486. tsym->plt_offset());
  3487. symval.set_output_value(value);
  3488. Reloc::wdisp30(view, object, &symval, addend, address);
  3489. }
  3490. break;
  3491. }
  3492. break;
  3493. }
  3494. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3495. _("unsupported reloc %u"),
  3496. r_type);
  3497. break;
  3498. // These relocs can appear in debugging sections, in which case
  3499. // we won't see the TLS_LDM relocs. The local_dynamic_type
  3500. // field tells us this.
  3501. case elfcpp::R_SPARC_TLS_LDO_HIX22:
  3502. if (optimized_type == tls::TLSOPT_TO_LE)
  3503. {
  3504. value -= tls_segment->memsz();
  3505. Reloc::hix22(view, value, addend);
  3506. }
  3507. else
  3508. Reloc::ldo_hix22(view, value, addend);
  3509. break;
  3510. case elfcpp::R_SPARC_TLS_LDO_LOX10:
  3511. if (optimized_type == tls::TLSOPT_TO_LE)
  3512. {
  3513. value -= tls_segment->memsz();
  3514. Reloc::lox10(view, value, addend);
  3515. }
  3516. else
  3517. Reloc::ldo_lox10(view, value, addend);
  3518. break;
  3519. case elfcpp::R_SPARC_TLS_LDO_ADD:
  3520. if (optimized_type == tls::TLSOPT_TO_LE)
  3521. {
  3522. Insntype* wv = reinterpret_cast<Insntype*>(view);
  3523. Insntype val;
  3524. // add %reg1, %reg2, %reg3 --> add %g7, %reg2, %reg3
  3525. val = elfcpp::Swap<32, true>::readval(wv);
  3526. val = (val & ~0x7c000) | 0x1c000;
  3527. elfcpp::Swap<32, true>::writeval(wv, val);
  3528. }
  3529. break;
  3530. // When optimizing IE --> LE, the only relocation that is handled
  3531. // differently is R_SPARC_TLS_IE_LD, it is rewritten from
  3532. // 'ld{,x} [rs1 + rs2], rd' into 'mov rs2, rd' or simply a NOP is
  3533. // rs2 and rd are the same.
  3534. case elfcpp::R_SPARC_TLS_IE_LD:
  3535. case elfcpp::R_SPARC_TLS_IE_LDX:
  3536. if (optimized_type == tls::TLSOPT_TO_LE)
  3537. {
  3538. Insntype* wv = reinterpret_cast<Insntype*>(view);
  3539. Insntype val = elfcpp::Swap<32, true>::readval(wv);
  3540. Insntype rs2 = val & 0x1f;
  3541. Insntype rd = (val >> 25) & 0x1f;
  3542. if (rs2 == rd)
  3543. val = sparc_nop;
  3544. else
  3545. val = sparc_mov | (val & 0x3e00001f);
  3546. elfcpp::Swap<32, true>::writeval(wv, val);
  3547. }
  3548. break;
  3549. case elfcpp::R_SPARC_TLS_IE_HI22:
  3550. case elfcpp::R_SPARC_TLS_IE_LO10:
  3551. if (optimized_type == tls::TLSOPT_TO_LE)
  3552. {
  3553. value -= tls_segment->memsz();
  3554. switch (r_type)
  3555. {
  3556. case elfcpp::R_SPARC_TLS_IE_HI22:
  3557. // IE_HI22 --> LE_HIX22
  3558. Reloc::hix22(view, value, addend);
  3559. break;
  3560. case elfcpp::R_SPARC_TLS_IE_LO10:
  3561. // IE_LO10 --> LE_LOX10
  3562. Reloc::lox10(view, value, addend);
  3563. break;
  3564. }
  3565. break;
  3566. }
  3567. else if (optimized_type == tls::TLSOPT_NONE)
  3568. {
  3569. // Relocate the field with the offset of the GOT entry for
  3570. // the tp-relative offset of the symbol.
  3571. if (gsym != NULL)
  3572. {
  3573. gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
  3574. value = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
  3575. }
  3576. else
  3577. {
  3578. unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
  3579. gold_assert(object->local_has_got_offset(r_sym,
  3580. GOT_TYPE_TLS_OFFSET));
  3581. value = object->local_got_offset(r_sym,
  3582. GOT_TYPE_TLS_OFFSET);
  3583. }
  3584. switch (r_type)
  3585. {
  3586. case elfcpp::R_SPARC_TLS_IE_HI22:
  3587. Reloc::hi22(view, value, addend);
  3588. break;
  3589. case elfcpp::R_SPARC_TLS_IE_LO10:
  3590. Reloc::lo10(view, value, addend);
  3591. break;
  3592. }
  3593. break;
  3594. }
  3595. gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
  3596. _("unsupported reloc %u"),
  3597. r_type);
  3598. break;
  3599. case elfcpp::R_SPARC_TLS_IE_ADD:
  3600. // This seems to be mainly so that we can find the addition
  3601. // instruction if there is one. There doesn't seem to be any
  3602. // actual relocation to apply.
  3603. break;
  3604. case elfcpp::R_SPARC_TLS_LE_HIX22:
  3605. // If we're creating a shared library, a dynamic relocation will
  3606. // have been created for this location, so do not apply it now.
  3607. if (!parameters->options().shared())
  3608. {
  3609. value -= tls_segment->memsz();
  3610. Reloc::hix22(view, value, addend);
  3611. }
  3612. break;
  3613. case elfcpp::R_SPARC_TLS_LE_LOX10:
  3614. // If we're creating a shared library, a dynamic relocation will
  3615. // have been created for this location, so do not apply it now.
  3616. if (!parameters->options().shared())
  3617. {
  3618. value -= tls_segment->memsz();
  3619. Reloc::lox10(view, value, addend);
  3620. }
  3621. break;
  3622. }
  3623. }
  3624. // Relax a call instruction.
  3625. template<int size, bool big_endian>
  3626. inline void
  3627. Target_sparc<size, big_endian>::Relocate::relax_call(
  3628. Target_sparc<size, big_endian>* target,
  3629. unsigned char* view,
  3630. const elfcpp::Rela<size, big_endian>& rela,
  3631. section_size_type view_size)
  3632. {
  3633. typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
  3634. Insntype *wv = reinterpret_cast<Insntype*>(view);
  3635. Insntype call_insn, delay_insn, set_insn;
  3636. uint32_t op3, reg, off;
  3637. // This code tries to relax call instructions that meet
  3638. // certain criteria.
  3639. //
  3640. // The first criteria is that the call must be such that the return
  3641. // address which the call writes into %o7 is unused. Two sequences
  3642. // meet this criteria, and are used to implement tail calls.
  3643. //
  3644. // Leaf function tail call:
  3645. //
  3646. // or %o7, %g0, %ANY_REG
  3647. // call FUNC
  3648. // or %ANY_REG, %g0, %o7
  3649. //
  3650. // Non-leaf function tail call:
  3651. //
  3652. // call FUNC
  3653. // restore
  3654. //
  3655. // The second criteria is that the call destination is close. If
  3656. // the displacement can fit in a signed 22-bit immediate field of a
  3657. // pre-V9 branch, we can do it. If we are generating a 64-bit
  3658. // object or a 32-bit object with ELF machine type EF_SPARC32PLUS,
  3659. // and the displacement fits in a signed 19-bit immediate field,
  3660. // then we can use a V9 branch.
  3661. // Make sure the delay instruction can be safely accessed.
  3662. if (rela.get_r_offset() + 8 > view_size)
  3663. return;
  3664. call_insn = elfcpp::Swap<32, true>::readval(wv);
  3665. delay_insn = elfcpp::Swap<32, true>::readval(wv + 1);
  3666. // Make sure it is really a call instruction.
  3667. if (((call_insn >> 30) & 0x3) != 1)
  3668. return;
  3669. if (((delay_insn >> 30) & 0x3) != 2)
  3670. return;
  3671. // Accept only a restore or an integer arithmetic operation whose
  3672. // sole side effect is to write the %o7 register (and perhaps set
  3673. // the condition codes, which are considered clobbered across
  3674. // function calls).
  3675. //
  3676. // For example, we don't want to match a tagged addition or
  3677. // subtraction. We also don't want to match something like a
  3678. // divide.
  3679. //
  3680. // Specifically we accept add{,cc}, and{,cc}, or{,cc},
  3681. // xor{,cc}, sub{,cc}, andn{,cc}, orn{,cc}, and xnor{,cc}.
  3682. op3 = (delay_insn >> 19) & 0x3f;
  3683. reg = (delay_insn >> 25) & 0x1f;
  3684. if (op3 != 0x3d
  3685. && ((op3 & 0x28) != 0 || reg != 15))
  3686. return;
  3687. // For non-restore instructions, make sure %o7 isn't
  3688. // an input.
  3689. if (op3 != 0x3d)
  3690. {
  3691. // First check RS1
  3692. reg = (delay_insn >> 14) & 0x1f;
  3693. if (reg == 15)
  3694. return;
  3695. // And if non-immediate, check RS2
  3696. if (((delay_insn >> 13) & 1) == 0)
  3697. {
  3698. reg = (delay_insn & 0x1f);
  3699. if (reg == 15)
  3700. return;
  3701. }
  3702. }
  3703. // Now check the branch distance. We are called after the
  3704. // call has been relocated, so we just have to peek at the
  3705. // offset contained in the instruction.
  3706. off = call_insn & 0x3fffffff;
  3707. if ((off & 0x3fe00000) != 0
  3708. && (off & 0x3fe00000) != 0x3fe00000)
  3709. return;
  3710. if ((size == 64 || target->elf_machine_ == elfcpp::EM_SPARC32PLUS)
  3711. && ((off & 0x3c0000) == 0
  3712. || (off & 0x3c0000) == 0x3c0000))
  3713. {
  3714. // ba,pt %xcc, FUNC
  3715. call_insn = 0x10680000 | (off & 0x07ffff);
  3716. }
  3717. else
  3718. {
  3719. // ba FUNC
  3720. call_insn = 0x10800000 | (off & 0x3fffff);
  3721. }
  3722. elfcpp::Swap<32, true>::writeval(wv, call_insn);
  3723. // See if we can NOP out the delay slot instruction. We peek
  3724. // at the instruction before the call to make sure we're dealing
  3725. // with exactly the:
  3726. //
  3727. // or %o7, %g0, %ANY_REG
  3728. // call
  3729. // or %ANY_REG, %g0, %o7
  3730. //
  3731. // case. Otherwise this might be a tricky piece of hand written
  3732. // assembler calculating %o7 in some non-trivial way, and therefore
  3733. // we can't be sure that NOP'ing out the delay slot is safe.
  3734. if (op3 == 0x02
  3735. && rela.get_r_offset() >= 4)
  3736. {
  3737. if ((delay_insn & ~(0x1f << 14)) != 0x9e100000)
  3738. return;
  3739. set_insn = elfcpp::Swap<32, true>::readval(wv - 1);
  3740. if ((set_insn & ~(0x1f << 25)) != 0x8013c000)
  3741. return;
  3742. reg = (set_insn >> 25) & 0x1f;
  3743. if (reg == 0 || reg == 15)
  3744. return;
  3745. if (reg != ((delay_insn >> 14) & 0x1f))
  3746. return;
  3747. // All tests pass, nop it out.
  3748. elfcpp::Swap<32, true>::writeval(wv + 1, sparc_nop);
  3749. }
  3750. }
  3751. // Relocate section data.
  3752. template<int size, bool big_endian>
  3753. void
  3754. Target_sparc<size, big_endian>::relocate_section(
  3755. const Relocate_info<size, big_endian>* relinfo,
  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. unsigned char* view,
  3762. typename elfcpp::Elf_types<size>::Elf_Addr address,
  3763. section_size_type view_size,
  3764. const Reloc_symbol_changes* reloc_symbol_changes)
  3765. {
  3766. typedef Target_sparc<size, big_endian> Sparc;
  3767. typedef typename Target_sparc<size, big_endian>::Relocate Sparc_relocate;
  3768. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  3769. Classify_reloc;
  3770. gold_assert(sh_type == elfcpp::SHT_RELA);
  3771. gold::relocate_section<size, big_endian, Sparc, Sparc_relocate,
  3772. gold::Default_comdat_behavior, Classify_reloc>(
  3773. relinfo,
  3774. this,
  3775. prelocs,
  3776. reloc_count,
  3777. output_section,
  3778. needs_special_offset_handling,
  3779. view,
  3780. address,
  3781. view_size,
  3782. reloc_symbol_changes);
  3783. }
  3784. // Scan the relocs during a relocatable link.
  3785. template<int size, bool big_endian>
  3786. void
  3787. Target_sparc<size, big_endian>::scan_relocatable_relocs(
  3788. Symbol_table* symtab,
  3789. Layout* layout,
  3790. Sized_relobj_file<size, big_endian>* object,
  3791. unsigned int data_shndx,
  3792. unsigned int sh_type,
  3793. const unsigned char* prelocs,
  3794. size_t reloc_count,
  3795. Output_section* output_section,
  3796. bool needs_special_offset_handling,
  3797. size_t local_symbol_count,
  3798. const unsigned char* plocal_symbols,
  3799. Relocatable_relocs* rr)
  3800. {
  3801. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  3802. Classify_reloc;
  3803. typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
  3804. Scan_relocatable_relocs;
  3805. gold_assert(sh_type == elfcpp::SHT_RELA);
  3806. gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
  3807. symtab,
  3808. layout,
  3809. object,
  3810. data_shndx,
  3811. prelocs,
  3812. reloc_count,
  3813. output_section,
  3814. needs_special_offset_handling,
  3815. local_symbol_count,
  3816. plocal_symbols,
  3817. rr);
  3818. }
  3819. // Scan the relocs for --emit-relocs.
  3820. template<int size, bool big_endian>
  3821. void
  3822. Target_sparc<size, big_endian>::emit_relocs_scan(
  3823. Symbol_table* symtab,
  3824. Layout* layout,
  3825. Sized_relobj_file<size, big_endian>* object,
  3826. unsigned int data_shndx,
  3827. unsigned int sh_type,
  3828. const unsigned char* prelocs,
  3829. size_t reloc_count,
  3830. Output_section* output_section,
  3831. bool needs_special_offset_handling,
  3832. size_t local_symbol_count,
  3833. const unsigned char* plocal_syms,
  3834. Relocatable_relocs* rr)
  3835. {
  3836. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  3837. Classify_reloc;
  3838. typedef gold::Default_emit_relocs_strategy<Classify_reloc>
  3839. Emit_relocs_strategy;
  3840. gold_assert(sh_type == elfcpp::SHT_RELA);
  3841. gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
  3842. symtab,
  3843. layout,
  3844. object,
  3845. data_shndx,
  3846. prelocs,
  3847. reloc_count,
  3848. output_section,
  3849. needs_special_offset_handling,
  3850. local_symbol_count,
  3851. plocal_syms,
  3852. rr);
  3853. }
  3854. // Emit relocations for a section.
  3855. template<int size, bool big_endian>
  3856. void
  3857. Target_sparc<size, big_endian>::relocate_relocs(
  3858. const Relocate_info<size, big_endian>* relinfo,
  3859. unsigned int sh_type,
  3860. const unsigned char* prelocs,
  3861. size_t reloc_count,
  3862. Output_section* output_section,
  3863. typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
  3864. unsigned char* view,
  3865. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  3866. section_size_type view_size,
  3867. unsigned char* reloc_view,
  3868. section_size_type reloc_view_size)
  3869. {
  3870. typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
  3871. Classify_reloc;
  3872. gold_assert(sh_type == elfcpp::SHT_RELA);
  3873. gold::relocate_relocs<size, big_endian, Classify_reloc>(
  3874. relinfo,
  3875. prelocs,
  3876. reloc_count,
  3877. output_section,
  3878. offset_in_output_section,
  3879. view,
  3880. view_address,
  3881. view_size,
  3882. reloc_view,
  3883. reloc_view_size);
  3884. }
  3885. // Return the value to use for a dynamic which requires special
  3886. // treatment. This is how we support equality comparisons of function
  3887. // pointers across shared library boundaries, as described in the
  3888. // processor specific ABI supplement.
  3889. template<int size, bool big_endian>
  3890. uint64_t
  3891. Target_sparc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
  3892. {
  3893. gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
  3894. return this->plt_section()->address() + gsym->plt_offset();
  3895. }
  3896. // do_make_elf_object to override the same function in the base class.
  3897. // We need to use a target-specific sub-class of
  3898. // Sized_relobj_file<size, big_endian> to process SPARC specific bits
  3899. // of the ELF headers. Hence we need to have our own ELF object creation.
  3900. template<int size, bool big_endian>
  3901. Object*
  3902. Target_sparc<size, big_endian>::do_make_elf_object(
  3903. const std::string& name,
  3904. Input_file* input_file,
  3905. off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
  3906. {
  3907. elfcpp::Elf_Half machine = ehdr.get_e_machine();
  3908. elfcpp::Elf_Word flags = ehdr.get_e_flags();
  3909. elfcpp::Elf_Word omm, mm;
  3910. switch (machine)
  3911. {
  3912. case elfcpp::EM_SPARC32PLUS:
  3913. this->elf_machine_ = elfcpp::EM_SPARC32PLUS;
  3914. break;
  3915. case elfcpp::EM_SPARC:
  3916. case elfcpp::EM_SPARCV9:
  3917. break;
  3918. default:
  3919. break;
  3920. }
  3921. if (!this->elf_flags_set_)
  3922. {
  3923. this->elf_flags_ = flags;
  3924. this->elf_flags_set_ = true;
  3925. }
  3926. else
  3927. {
  3928. // Accumulate cpu feature bits.
  3929. this->elf_flags_ |= (flags & (elfcpp::EF_SPARC_32PLUS
  3930. | elfcpp::EF_SPARC_SUN_US1
  3931. | elfcpp::EF_SPARC_HAL_R1
  3932. | elfcpp::EF_SPARC_SUN_US3));
  3933. // Bump the memory model setting to the most restrictive
  3934. // one we encounter.
  3935. omm = (this->elf_flags_ & elfcpp::EF_SPARCV9_MM);
  3936. mm = (flags & elfcpp::EF_SPARCV9_MM);
  3937. if (omm != mm)
  3938. {
  3939. if (mm == elfcpp::EF_SPARCV9_TSO)
  3940. {
  3941. this->elf_flags_ &= ~elfcpp::EF_SPARCV9_MM;
  3942. this->elf_flags_ |= elfcpp::EF_SPARCV9_TSO;
  3943. }
  3944. else if (mm == elfcpp::EF_SPARCV9_PSO
  3945. && omm == elfcpp::EF_SPARCV9_RMO)
  3946. {
  3947. this->elf_flags_ &= ~elfcpp::EF_SPARCV9_MM;
  3948. this->elf_flags_ |= elfcpp::EF_SPARCV9_PSO;
  3949. }
  3950. }
  3951. }
  3952. // Validate that the little-endian flag matches how we've
  3953. // been instantiated.
  3954. if (!(flags & elfcpp::EF_SPARC_LEDATA) != big_endian)
  3955. {
  3956. if (big_endian)
  3957. gold_error(_("%s: little endian elf flag set on BE object"),
  3958. name.c_str());
  3959. else
  3960. gold_error(_("%s: little endian elf flag clear on LE object"),
  3961. name.c_str());
  3962. }
  3963. return Target::do_make_elf_object(name, input_file, offset, ehdr);
  3964. }
  3965. // Adjust ELF file header.
  3966. template<int size, bool big_endian>
  3967. void
  3968. Target_sparc<size, big_endian>::do_adjust_elf_header(
  3969. unsigned char* view,
  3970. int len)
  3971. {
  3972. elfcpp::Ehdr_write<size, big_endian> oehdr(view);
  3973. oehdr.put_e_machine(this->elf_machine_);
  3974. oehdr.put_e_flags(this->elf_flags_);
  3975. Sized_target<size, big_endian>::do_adjust_elf_header(view, len);
  3976. }
  3977. // The selector for sparc object files.
  3978. template<int size, bool big_endian>
  3979. class Target_selector_sparc : public Target_selector
  3980. {
  3981. public:
  3982. Target_selector_sparc()
  3983. : Target_selector(elfcpp::EM_NONE, size, big_endian,
  3984. (size == 64 ? "elf64-sparc" : "elf32-sparc"),
  3985. (size == 64 ? "elf64_sparc" : "elf32_sparc"))
  3986. { }
  3987. virtual Target*
  3988. do_recognize(Input_file*, off_t, int machine, int, int)
  3989. {
  3990. switch (size)
  3991. {
  3992. case 64:
  3993. if (machine != elfcpp::EM_SPARCV9)
  3994. return NULL;
  3995. break;
  3996. case 32:
  3997. if (machine != elfcpp::EM_SPARC
  3998. && machine != elfcpp::EM_SPARC32PLUS)
  3999. return NULL;
  4000. break;
  4001. default:
  4002. return NULL;
  4003. }
  4004. return this->instantiate_target();
  4005. }
  4006. virtual Target*
  4007. do_instantiate_target()
  4008. { return new Target_sparc<size, big_endian>(); }
  4009. };
  4010. Target_selector_sparc<32, true> target_selector_sparc32;
  4011. Target_selector_sparc<64, true> target_selector_sparc64;
  4012. } // End anonymous namespace.