sparc.cc 130 KB

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