elfxx-sparc.c 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954
  1. /* SPARC-specific support for ELF
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /* This file handles functionality common to the different SPARC ABI's. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "libiberty.h"
  22. #include "elf-bfd.h"
  23. #include "elf/sparc.h"
  24. #include "opcode/sparc.h"
  25. #include "elfxx-sparc.h"
  26. #include "elf-vxworks.h"
  27. #include "objalloc.h"
  28. #include "hashtab.h"
  29. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
  30. #define MINUS_ONE (~ (bfd_vma) 0)
  31. #define ABI_64_P(abfd) \
  32. (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
  33. /* The relocation "howto" table. */
  34. /* Utility for performing the standard initial work of an instruction
  35. relocation.
  36. *PRELOCATION will contain the relocated item.
  37. *PINSN will contain the instruction from the input stream.
  38. If the result is `bfd_reloc_other' the caller can continue with
  39. performing the relocation. Otherwise it must stop and return the
  40. value to its caller. */
  41. static bfd_reloc_status_type
  42. init_insn_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  43. void * data, asection *input_section, bfd *output_bfd,
  44. bfd_vma *prelocation, bfd_vma *pinsn)
  45. {
  46. bfd_vma relocation;
  47. reloc_howto_type *howto = reloc_entry->howto;
  48. if (output_bfd != (bfd *) NULL
  49. && (symbol->flags & BSF_SECTION_SYM) == 0
  50. && (! howto->partial_inplace
  51. || reloc_entry->addend == 0))
  52. {
  53. reloc_entry->address += input_section->output_offset;
  54. return bfd_reloc_ok;
  55. }
  56. /* This works because partial_inplace is FALSE. */
  57. if (output_bfd != NULL)
  58. return bfd_reloc_continue;
  59. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  60. return bfd_reloc_outofrange;
  61. relocation = (symbol->value
  62. + symbol->section->output_section->vma
  63. + symbol->section->output_offset);
  64. relocation += reloc_entry->addend;
  65. if (howto->pc_relative)
  66. {
  67. relocation -= (input_section->output_section->vma
  68. + input_section->output_offset);
  69. relocation -= reloc_entry->address;
  70. }
  71. *prelocation = relocation;
  72. *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  73. return bfd_reloc_other;
  74. }
  75. /* For unsupported relocs. */
  76. static bfd_reloc_status_type
  77. sparc_elf_notsup_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  78. arelent *reloc_entry ATTRIBUTE_UNUSED,
  79. asymbol *symbol ATTRIBUTE_UNUSED,
  80. void * data ATTRIBUTE_UNUSED,
  81. asection *input_section ATTRIBUTE_UNUSED,
  82. bfd *output_bfd ATTRIBUTE_UNUSED,
  83. char **error_message ATTRIBUTE_UNUSED)
  84. {
  85. return bfd_reloc_notsupported;
  86. }
  87. /* Handle the WDISP16 reloc. */
  88. static bfd_reloc_status_type
  89. sparc_elf_wdisp16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  90. void * data, asection *input_section, bfd *output_bfd,
  91. char **error_message ATTRIBUTE_UNUSED)
  92. {
  93. bfd_vma relocation;
  94. bfd_vma insn;
  95. bfd_reloc_status_type status;
  96. status = init_insn_reloc (abfd, reloc_entry, symbol, data,
  97. input_section, output_bfd, &relocation, &insn);
  98. if (status != bfd_reloc_other)
  99. return status;
  100. insn &= ~ (bfd_vma) 0x303fff;
  101. insn |= (((relocation >> 2) & 0xc000) << 6) | ((relocation >> 2) & 0x3fff);
  102. bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  103. if ((bfd_signed_vma) relocation < - 0x40000
  104. || (bfd_signed_vma) relocation > 0x3ffff)
  105. return bfd_reloc_overflow;
  106. else
  107. return bfd_reloc_ok;
  108. }
  109. /* Handle the WDISP10 reloc. */
  110. static bfd_reloc_status_type
  111. sparc_elf_wdisp10_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  112. void * data, asection *input_section, bfd *output_bfd,
  113. char **error_message ATTRIBUTE_UNUSED)
  114. {
  115. bfd_vma relocation;
  116. bfd_vma insn;
  117. bfd_reloc_status_type status;
  118. status = init_insn_reloc (abfd, reloc_entry, symbol, data,
  119. input_section, output_bfd, &relocation, &insn);
  120. if (status != bfd_reloc_other)
  121. return status;
  122. insn &= ~ (bfd_vma) 0x181fe0;
  123. insn |= (((relocation >> 2) & 0x300) << 11)
  124. | (((relocation >> 2) & 0xff) << 5);
  125. bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  126. if ((bfd_signed_vma) relocation < - 0x1000
  127. || (bfd_signed_vma) relocation > 0xfff)
  128. return bfd_reloc_overflow;
  129. else
  130. return bfd_reloc_ok;
  131. }
  132. /* Handle the HIX22 reloc. */
  133. static bfd_reloc_status_type
  134. sparc_elf_hix22_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  135. void * data, asection *input_section, bfd *output_bfd,
  136. char **error_message ATTRIBUTE_UNUSED)
  137. {
  138. bfd_vma relocation;
  139. bfd_vma insn;
  140. bfd_reloc_status_type status;
  141. status = init_insn_reloc (abfd, reloc_entry, symbol, data,
  142. input_section, output_bfd, &relocation, &insn);
  143. if (status != bfd_reloc_other)
  144. return status;
  145. relocation ^= MINUS_ONE;
  146. insn = (insn &~ (bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
  147. bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  148. if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
  149. return bfd_reloc_overflow;
  150. else
  151. return bfd_reloc_ok;
  152. }
  153. /* Handle the LOX10 reloc. */
  154. static bfd_reloc_status_type
  155. sparc_elf_lox10_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
  156. void * data, asection *input_section, bfd *output_bfd,
  157. char **error_message ATTRIBUTE_UNUSED)
  158. {
  159. bfd_vma relocation;
  160. bfd_vma insn;
  161. bfd_reloc_status_type status;
  162. status = init_insn_reloc (abfd, reloc_entry, symbol, data,
  163. input_section, output_bfd, &relocation, &insn);
  164. if (status != bfd_reloc_other)
  165. return status;
  166. insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
  167. bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  168. return bfd_reloc_ok;
  169. }
  170. static reloc_howto_type _bfd_sparc_elf_howto_table[] =
  171. {
  172. HOWTO(R_SPARC_NONE, 0,3, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
  173. HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", false,0,0x000000ff,true),
  174. HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", false,0,0x0000ffff,true),
  175. HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", false,0,0xffffffff,true),
  176. HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", false,0,0x000000ff,true),
  177. HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", false,0,0x0000ffff,true),
  178. HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", false,0,0xffffffff,true),
  179. HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", false,0,0x3fffffff,true),
  180. HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", false,0,0x003fffff,true),
  181. HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", false,0,0x003fffff,true),
  182. HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", false,0,0x003fffff,true),
  183. HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", false,0,0x00001fff,true),
  184. HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", false,0,0x000003ff,true),
  185. HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", false,0,0x000003ff,true),
  186. HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", false,0,0x00001fff,true),
  187. HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", false,0,0x003fffff,true),
  188. HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", false,0,0x000003ff,true),
  189. HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", false,0,0x003fffff,true),
  190. HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", false,0,0x3fffffff,true),
  191. HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", false,0,0x00000000,true),
  192. HOWTO(R_SPARC_GLOB_DAT, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GLOB_DAT",false,0,0x00000000,true),
  193. HOWTO(R_SPARC_JMP_SLOT, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_JMP_SLOT",false,0,0x00000000,true),
  194. HOWTO(R_SPARC_RELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",false,0,0x00000000,true),
  195. HOWTO(R_SPARC_UA32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0xffffffff,true),
  196. HOWTO(R_SPARC_PLT32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT32", false,0,0xffffffff,true),
  197. HOWTO(R_SPARC_HIPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_HIPLT22", false,0,0x00000000,true),
  198. HOWTO(R_SPARC_LOPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_LOPLT10", false,0,0x00000000,true),
  199. HOWTO(R_SPARC_PCPLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT32", false,0,0x00000000,true),
  200. HOWTO(R_SPARC_PCPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT22", false,0,0x00000000,true),
  201. HOWTO(R_SPARC_PCPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT10", false,0,0x00000000,true),
  202. HOWTO(R_SPARC_10, 0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", false,0,0x000003ff,true),
  203. HOWTO(R_SPARC_11, 0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", false,0,0x000007ff,true),
  204. HOWTO(R_SPARC_64, 0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_64", false,0,MINUS_ONE, true),
  205. HOWTO(R_SPARC_OLO10, 0,2,13,false,0,complain_overflow_signed, sparc_elf_notsup_reloc, "R_SPARC_OLO10", false,0,0x00001fff,true),
  206. HOWTO(R_SPARC_HH22, 42,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_HH22", false,0,0x003fffff,true),
  207. HOWTO(R_SPARC_HM10, 32,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HM10", false,0,0x000003ff,true),
  208. HOWTO(R_SPARC_LM22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LM22", false,0,0x003fffff,true),
  209. HOWTO(R_SPARC_PC_HH22, 42,2,22,true, 0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_PC_HH22", false,0,0x003fffff,true),
  210. HOWTO(R_SPARC_PC_HM10, 32,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC_HM10", false,0,0x000003ff,true),
  211. HOWTO(R_SPARC_PC_LM22, 10,2,22,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC_LM22", false,0,0x003fffff,true),
  212. HOWTO(R_SPARC_WDISP16, 2,2,16,true, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
  213. HOWTO(R_SPARC_WDISP19, 2,2,19,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", false,0,0x0007ffff,true),
  214. HOWTO(R_SPARC_UNUSED_42, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UNUSED_42",false,0,0x00000000,true),
  215. HOWTO(R_SPARC_7, 0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", false,0,0x0000007f,true),
  216. HOWTO(R_SPARC_5, 0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", false,0,0x0000001f,true),
  217. HOWTO(R_SPARC_6, 0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", false,0,0x0000003f,true),
  218. HOWTO(R_SPARC_DISP64, 0,4,64,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP64", false,0,MINUS_ONE, true),
  219. HOWTO(R_SPARC_PLT64, 0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT64", false,0,MINUS_ONE, true),
  220. HOWTO(R_SPARC_HIX22, 0,4, 0,false,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_HIX22", false,0,MINUS_ONE, false),
  221. HOWTO(R_SPARC_LOX10, 0,4, 0,false,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_LOX10", false,0,MINUS_ONE, false),
  222. HOWTO(R_SPARC_H44, 22,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_H44", false,0,0x003fffff,false),
  223. HOWTO(R_SPARC_M44, 12,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_M44", false,0,0x000003ff,false),
  224. HOWTO(R_SPARC_L44, 0,2,13,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_L44", false,0,0x00000fff,false),
  225. HOWTO(R_SPARC_REGISTER, 0,4, 0,false,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",false,0,MINUS_ONE, false),
  226. HOWTO(R_SPARC_UA64, 0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA64", false,0,MINUS_ONE, true),
  227. HOWTO(R_SPARC_UA16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", false,0,0x0000ffff,true),
  228. HOWTO(R_SPARC_TLS_GD_HI22,10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_HI22",false,0,0x003fffff,true),
  229. HOWTO(R_SPARC_TLS_GD_LO10,0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_LO10",false,0,0x000003ff,true),
  230. HOWTO(R_SPARC_TLS_GD_ADD,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_ADD",false,0,0x00000000,true),
  231. HOWTO(R_SPARC_TLS_GD_CALL,2,2,30,true,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_CALL",false,0,0x3fffffff,true),
  232. HOWTO(R_SPARC_TLS_LDM_HI22,10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_HI22",false,0,0x003fffff,true),
  233. HOWTO(R_SPARC_TLS_LDM_LO10,0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_LO10",false,0,0x000003ff,true),
  234. HOWTO(R_SPARC_TLS_LDM_ADD,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_ADD",false,0,0x00000000,true),
  235. HOWTO(R_SPARC_TLS_LDM_CALL,2,2,30,true,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_CALL",false,0,0x3fffffff,true),
  236. HOWTO(R_SPARC_TLS_LDO_HIX22,0,2,0,false,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_TLS_LDO_HIX22",false,0,0x003fffff, false),
  237. HOWTO(R_SPARC_TLS_LDO_LOX10,0,2,0,false,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_TLS_LDO_LOX10",false,0,0x000003ff, false),
  238. HOWTO(R_SPARC_TLS_LDO_ADD,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDO_ADD",false,0,0x00000000,true),
  239. HOWTO(R_SPARC_TLS_IE_HI22,10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_HI22",false,0,0x003fffff,true),
  240. HOWTO(R_SPARC_TLS_IE_LO10,0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LO10",false,0,0x000003ff,true),
  241. HOWTO(R_SPARC_TLS_IE_LD,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LD",false,0,0x00000000,true),
  242. HOWTO(R_SPARC_TLS_IE_LDX,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LDX",false,0,0x00000000,true),
  243. HOWTO(R_SPARC_TLS_IE_ADD,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_ADD",false,0,0x00000000,true),
  244. HOWTO(R_SPARC_TLS_LE_HIX22,0,2,0,false,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_TLS_LE_HIX22",false,0,0x003fffff, false),
  245. HOWTO(R_SPARC_TLS_LE_LOX10,0,2,0,false,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_TLS_LE_LOX10",false,0,0x000003ff, false),
  246. HOWTO(R_SPARC_TLS_DTPMOD32,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_DTPMOD32",false,0,0x00000000,true),
  247. HOWTO(R_SPARC_TLS_DTPMOD64,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_DTPMOD64",false,0,0x00000000,true),
  248. HOWTO(R_SPARC_TLS_DTPOFF32,0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF32",false,0,0xffffffff,true),
  249. HOWTO(R_SPARC_TLS_DTPOFF64,0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF64",false,0,MINUS_ONE,true),
  250. HOWTO(R_SPARC_TLS_TPOFF32,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_TPOFF32",false,0,0x00000000,true),
  251. HOWTO(R_SPARC_TLS_TPOFF64,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_TPOFF64",false,0,0x00000000,true),
  252. HOWTO(R_SPARC_GOTDATA_HIX22,0,2,0,false,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_GOTDATA_HIX22",false,0,0x003fffff, false),
  253. HOWTO(R_SPARC_GOTDATA_LOX10,0,2,0,false,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_GOTDATA_LOX10",false,0,0x000003ff, false),
  254. HOWTO(R_SPARC_GOTDATA_OP_HIX22,0,2,0,false,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_GOTDATA_OP_HIX22",false,0,0x003fffff, false),
  255. HOWTO(R_SPARC_GOTDATA_OP_LOX10,0,2,0,false,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_GOTDATA_OP_LOX10",false,0,0x000003ff, false),
  256. HOWTO(R_SPARC_GOTDATA_OP,0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOTDATA_OP",false,0,0x00000000,true),
  257. HOWTO(R_SPARC_H34,12,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc,"R_SPARC_H34",false,0,0x003fffff,false),
  258. HOWTO(R_SPARC_SIZE32,0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_SIZE32",false,0,0xffffffff,true),
  259. HOWTO(R_SPARC_SIZE64,0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_SIZE64",false,0,MINUS_ONE, true),
  260. HOWTO(R_SPARC_WDISP10,2,2,10,true, 0,complain_overflow_signed,sparc_elf_wdisp10_reloc,"R_SPARC_WDISP10",false,0,0x00000000,true),
  261. };
  262. static reloc_howto_type sparc_jmp_irel_howto =
  263. HOWTO(R_SPARC_JMP_IREL, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_JMP_IREL",false,0,0x00000000,true);
  264. static reloc_howto_type sparc_irelative_howto =
  265. HOWTO(R_SPARC_IRELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_IRELATIVE",false,0,0x00000000,true);
  266. static reloc_howto_type sparc_vtinherit_howto =
  267. HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", false,0, 0, false);
  268. static reloc_howto_type sparc_vtentry_howto =
  269. HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,false,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", false,0,0, false);
  270. static reloc_howto_type sparc_rev32_howto =
  271. HOWTO(R_SPARC_REV32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", false,0,0xffffffff,true);
  272. reloc_howto_type *
  273. _bfd_sparc_elf_reloc_type_lookup (bfd *abfd,
  274. bfd_reloc_code_real_type code)
  275. {
  276. /* We explicitly handle each relocation type in the switch
  277. instead of using a lookup table for efficiency. */
  278. switch (code)
  279. {
  280. case BFD_RELOC_NONE:
  281. return &_bfd_sparc_elf_howto_table[R_SPARC_NONE];
  282. case BFD_RELOC_8:
  283. return &_bfd_sparc_elf_howto_table[R_SPARC_8];
  284. case BFD_RELOC_16:
  285. return &_bfd_sparc_elf_howto_table[R_SPARC_16];
  286. case BFD_RELOC_32:
  287. return &_bfd_sparc_elf_howto_table[R_SPARC_32];
  288. case BFD_RELOC_8_PCREL:
  289. return &_bfd_sparc_elf_howto_table[R_SPARC_DISP8];
  290. case BFD_RELOC_16_PCREL:
  291. return &_bfd_sparc_elf_howto_table[R_SPARC_DISP16];
  292. case BFD_RELOC_32_PCREL:
  293. return &_bfd_sparc_elf_howto_table[R_SPARC_DISP32];
  294. case BFD_RELOC_32_PCREL_S2:
  295. return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP30];
  296. case BFD_RELOC_SPARC_WDISP22:
  297. return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP22];
  298. case BFD_RELOC_HI22:
  299. return &_bfd_sparc_elf_howto_table[R_SPARC_HI22];
  300. case BFD_RELOC_SPARC22:
  301. return &_bfd_sparc_elf_howto_table[R_SPARC_22];
  302. case BFD_RELOC_SPARC13:
  303. return &_bfd_sparc_elf_howto_table[R_SPARC_13];
  304. case BFD_RELOC_LO10:
  305. return &_bfd_sparc_elf_howto_table[R_SPARC_LO10];
  306. case BFD_RELOC_SPARC_GOT10:
  307. return &_bfd_sparc_elf_howto_table[R_SPARC_GOT10];
  308. case BFD_RELOC_SPARC_GOT13:
  309. return &_bfd_sparc_elf_howto_table[R_SPARC_GOT13];
  310. case BFD_RELOC_SPARC_GOT22:
  311. return &_bfd_sparc_elf_howto_table[R_SPARC_GOT22];
  312. case BFD_RELOC_SPARC_PC10:
  313. return &_bfd_sparc_elf_howto_table[R_SPARC_PC10];
  314. case BFD_RELOC_SPARC_PC22:
  315. return &_bfd_sparc_elf_howto_table[R_SPARC_PC22];
  316. case BFD_RELOC_SPARC_WPLT30:
  317. return &_bfd_sparc_elf_howto_table[R_SPARC_WPLT30];
  318. case BFD_RELOC_SPARC_COPY:
  319. return &_bfd_sparc_elf_howto_table[R_SPARC_COPY];
  320. case BFD_RELOC_SPARC_GLOB_DAT:
  321. return &_bfd_sparc_elf_howto_table[R_SPARC_GLOB_DAT];
  322. case BFD_RELOC_SPARC_JMP_SLOT:
  323. return &_bfd_sparc_elf_howto_table[R_SPARC_JMP_SLOT];
  324. case BFD_RELOC_SPARC_RELATIVE:
  325. return &_bfd_sparc_elf_howto_table[R_SPARC_RELATIVE];
  326. case BFD_RELOC_SPARC_UA32:
  327. return &_bfd_sparc_elf_howto_table[R_SPARC_UA32];
  328. case BFD_RELOC_SPARC_PLT32:
  329. return &_bfd_sparc_elf_howto_table[R_SPARC_PLT32];
  330. case BFD_RELOC_SPARC_10:
  331. return &_bfd_sparc_elf_howto_table[R_SPARC_10];
  332. case BFD_RELOC_SPARC_11:
  333. return &_bfd_sparc_elf_howto_table[R_SPARC_11];
  334. case BFD_RELOC_SPARC_64:
  335. return &_bfd_sparc_elf_howto_table[R_SPARC_64];
  336. case BFD_RELOC_SPARC_OLO10:
  337. return &_bfd_sparc_elf_howto_table[R_SPARC_OLO10];
  338. case BFD_RELOC_SPARC_HH22:
  339. return &_bfd_sparc_elf_howto_table[R_SPARC_HH22];
  340. case BFD_RELOC_SPARC_HM10:
  341. return &_bfd_sparc_elf_howto_table[R_SPARC_HM10];
  342. case BFD_RELOC_SPARC_LM22:
  343. return &_bfd_sparc_elf_howto_table[R_SPARC_LM22];
  344. case BFD_RELOC_SPARC_PC_HH22:
  345. return &_bfd_sparc_elf_howto_table[R_SPARC_PC_HH22];
  346. case BFD_RELOC_SPARC_PC_HM10:
  347. return &_bfd_sparc_elf_howto_table[R_SPARC_PC_HM10];
  348. case BFD_RELOC_SPARC_PC_LM22:
  349. return &_bfd_sparc_elf_howto_table[R_SPARC_PC_LM22];
  350. case BFD_RELOC_SPARC_WDISP16:
  351. return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP16];
  352. case BFD_RELOC_SPARC_WDISP19:
  353. return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP19];
  354. case BFD_RELOC_SPARC_7:
  355. return &_bfd_sparc_elf_howto_table[R_SPARC_7];
  356. case BFD_RELOC_SPARC_5:
  357. return &_bfd_sparc_elf_howto_table[R_SPARC_5];
  358. case BFD_RELOC_SPARC_6:
  359. return &_bfd_sparc_elf_howto_table[R_SPARC_6];
  360. case BFD_RELOC_SPARC_DISP64:
  361. return &_bfd_sparc_elf_howto_table[R_SPARC_DISP64];
  362. case BFD_RELOC_SPARC_PLT64:
  363. return &_bfd_sparc_elf_howto_table[R_SPARC_PLT64];
  364. case BFD_RELOC_SPARC_HIX22:
  365. return &_bfd_sparc_elf_howto_table[R_SPARC_HIX22];
  366. case BFD_RELOC_SPARC_LOX10:
  367. return &_bfd_sparc_elf_howto_table[R_SPARC_LOX10];
  368. case BFD_RELOC_SPARC_H44:
  369. return &_bfd_sparc_elf_howto_table[R_SPARC_H44];
  370. case BFD_RELOC_SPARC_M44:
  371. return &_bfd_sparc_elf_howto_table[R_SPARC_M44];
  372. case BFD_RELOC_SPARC_L44:
  373. return &_bfd_sparc_elf_howto_table[R_SPARC_L44];
  374. case BFD_RELOC_SPARC_REGISTER:
  375. return &_bfd_sparc_elf_howto_table[R_SPARC_REGISTER];
  376. case BFD_RELOC_SPARC_UA64:
  377. return &_bfd_sparc_elf_howto_table[R_SPARC_UA64];
  378. case BFD_RELOC_SPARC_UA16:
  379. return &_bfd_sparc_elf_howto_table[R_SPARC_UA16];
  380. case BFD_RELOC_SPARC_TLS_GD_HI22:
  381. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_GD_HI22];
  382. case BFD_RELOC_SPARC_TLS_GD_LO10:
  383. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_GD_LO10];
  384. case BFD_RELOC_SPARC_TLS_GD_ADD:
  385. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_GD_ADD];
  386. case BFD_RELOC_SPARC_TLS_GD_CALL:
  387. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_GD_CALL];
  388. case BFD_RELOC_SPARC_TLS_LDM_HI22:
  389. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDM_HI22];
  390. case BFD_RELOC_SPARC_TLS_LDM_LO10:
  391. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDM_LO10];
  392. case BFD_RELOC_SPARC_TLS_LDM_ADD:
  393. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDM_ADD];
  394. case BFD_RELOC_SPARC_TLS_LDM_CALL:
  395. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDM_CALL];
  396. case BFD_RELOC_SPARC_TLS_LDO_HIX22:
  397. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDO_HIX22];
  398. case BFD_RELOC_SPARC_TLS_LDO_LOX10:
  399. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDO_LOX10];
  400. case BFD_RELOC_SPARC_TLS_LDO_ADD:
  401. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDO_ADD];
  402. case BFD_RELOC_SPARC_TLS_IE_HI22:
  403. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_HI22];
  404. case BFD_RELOC_SPARC_TLS_IE_LO10:
  405. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_LO10];
  406. case BFD_RELOC_SPARC_TLS_IE_LD:
  407. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_LD];
  408. case BFD_RELOC_SPARC_TLS_IE_LDX:
  409. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_LDX];
  410. case BFD_RELOC_SPARC_TLS_IE_ADD:
  411. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_ADD];
  412. case BFD_RELOC_SPARC_TLS_LE_HIX22:
  413. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LE_HIX22];
  414. case BFD_RELOC_SPARC_TLS_LE_LOX10:
  415. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LE_LOX10];
  416. case BFD_RELOC_SPARC_TLS_DTPMOD32:
  417. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_DTPMOD32];
  418. case BFD_RELOC_SPARC_TLS_DTPMOD64:
  419. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_DTPMOD64];
  420. case BFD_RELOC_SPARC_TLS_DTPOFF32:
  421. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_DTPOFF32];
  422. case BFD_RELOC_SPARC_TLS_DTPOFF64:
  423. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_DTPOFF64];
  424. case BFD_RELOC_SPARC_TLS_TPOFF32:
  425. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_TPOFF32];
  426. case BFD_RELOC_SPARC_TLS_TPOFF64:
  427. return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_TPOFF64];
  428. case BFD_RELOC_SPARC_GOTDATA_HIX22:
  429. return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_HIX22];
  430. case BFD_RELOC_SPARC_GOTDATA_LOX10:
  431. return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_LOX10];
  432. case BFD_RELOC_SPARC_GOTDATA_OP_HIX22:
  433. return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_OP_HIX22];
  434. case BFD_RELOC_SPARC_GOTDATA_OP_LOX10:
  435. return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_OP_LOX10];
  436. case BFD_RELOC_SPARC_GOTDATA_OP:
  437. return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_OP];
  438. case BFD_RELOC_SPARC_H34:
  439. return &_bfd_sparc_elf_howto_table[R_SPARC_H34];
  440. case BFD_RELOC_SPARC_SIZE32:
  441. return &_bfd_sparc_elf_howto_table[R_SPARC_SIZE32];
  442. case BFD_RELOC_SPARC_SIZE64:
  443. return &_bfd_sparc_elf_howto_table[R_SPARC_SIZE64];
  444. case BFD_RELOC_SPARC_WDISP10:
  445. return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP10];
  446. case BFD_RELOC_SPARC_JMP_IREL:
  447. return &sparc_jmp_irel_howto;
  448. case BFD_RELOC_SPARC_IRELATIVE:
  449. return &sparc_irelative_howto;
  450. case BFD_RELOC_VTABLE_INHERIT:
  451. return &sparc_vtinherit_howto;
  452. case BFD_RELOC_VTABLE_ENTRY:
  453. return &sparc_vtentry_howto;
  454. case BFD_RELOC_SPARC_REV32:
  455. return &sparc_rev32_howto;
  456. default:
  457. break;
  458. }
  459. /* xgettext:c-format */
  460. _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
  461. bfd_set_error (bfd_error_bad_value);
  462. return NULL;
  463. }
  464. reloc_howto_type *
  465. _bfd_sparc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  466. const char *r_name)
  467. {
  468. unsigned int i;
  469. for (i = 0; i < ARRAY_SIZE (_bfd_sparc_elf_howto_table); i++)
  470. if (_bfd_sparc_elf_howto_table[i].name != NULL
  471. && strcasecmp (_bfd_sparc_elf_howto_table[i].name, r_name) == 0)
  472. return &_bfd_sparc_elf_howto_table[i];
  473. if (strcasecmp (sparc_vtinherit_howto.name, r_name) == 0)
  474. return &sparc_vtinherit_howto;
  475. if (strcasecmp (sparc_vtentry_howto.name, r_name) == 0)
  476. return &sparc_vtentry_howto;
  477. if (strcasecmp (sparc_rev32_howto.name, r_name) == 0)
  478. return &sparc_rev32_howto;
  479. return NULL;
  480. }
  481. reloc_howto_type *
  482. _bfd_sparc_elf_info_to_howto_ptr (bfd *abfd ATTRIBUTE_UNUSED,
  483. unsigned int r_type)
  484. {
  485. switch (r_type)
  486. {
  487. case R_SPARC_JMP_IREL:
  488. return &sparc_jmp_irel_howto;
  489. case R_SPARC_IRELATIVE:
  490. return &sparc_irelative_howto;
  491. case R_SPARC_GNU_VTINHERIT:
  492. return &sparc_vtinherit_howto;
  493. case R_SPARC_GNU_VTENTRY:
  494. return &sparc_vtentry_howto;
  495. case R_SPARC_REV32:
  496. return &sparc_rev32_howto;
  497. default:
  498. if (r_type >= (unsigned int) R_SPARC_max_std)
  499. {
  500. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  501. abfd, r_type);
  502. bfd_set_error (bfd_error_bad_value);
  503. return NULL;
  504. }
  505. return &_bfd_sparc_elf_howto_table[r_type];
  506. }
  507. }
  508. /* Both 32-bit and 64-bit sparc encode this in an identical manner,
  509. so just take advantage of that. */
  510. #define SPARC_ELF_R_TYPE(r_info) \
  511. ((r_info) & 0xff)
  512. bool
  513. _bfd_sparc_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
  514. Elf_Internal_Rela *dst)
  515. {
  516. unsigned int r_type = SPARC_ELF_R_TYPE (dst->r_info);
  517. if ((cache_ptr->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, r_type)) == NULL)
  518. {
  519. bfd_set_error (bfd_error_bad_value);
  520. return false;
  521. }
  522. return true;
  523. }
  524. /* The nop opcode we use. */
  525. #define SPARC_NOP 0x01000000
  526. #define SPARC_INSN_BYTES 4
  527. /* Is an undefined weak symbol resolved to 0 ?
  528. Reference to an undefined weak symbol is resolved to 0 when
  529. building an executable if it isn't dynamic and
  530. 1. Has non-GOT/non-PLT relocations in text section.
  531. Or
  532. 2. Has no GOT/PLT relocation. */
  533. #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, EH) \
  534. ((EH)->elf.root.type == bfd_link_hash_undefweak \
  535. && bfd_link_executable (INFO) \
  536. && (_bfd_sparc_elf_hash_table (INFO)->interp == NULL \
  537. || !(INFO)->dynamic_undefined_weak \
  538. || (EH)->has_non_got_reloc \
  539. || !(EH)->has_got_reloc))
  540. /* SPARC ELF linker hash entry. */
  541. struct _bfd_sparc_elf_link_hash_entry
  542. {
  543. struct elf_link_hash_entry elf;
  544. #define GOT_UNKNOWN 0
  545. #define GOT_NORMAL 1
  546. #define GOT_TLS_GD 2
  547. #define GOT_TLS_IE 3
  548. unsigned char tls_type;
  549. /* Symbol has GOT or PLT relocations. */
  550. unsigned int has_got_reloc : 1;
  551. /* Symbol has old-style, non-relaxable GOT relocations. */
  552. unsigned int has_old_style_got_reloc : 1;
  553. /* Symbol has non-GOT/non-PLT relocations in text sections. */
  554. unsigned int has_non_got_reloc : 1;
  555. };
  556. #define _bfd_sparc_elf_hash_entry(ent) ((struct _bfd_sparc_elf_link_hash_entry *)(ent))
  557. struct _bfd_sparc_elf_obj_tdata
  558. {
  559. struct elf_obj_tdata root;
  560. /* tls_type for each local got entry. */
  561. char *local_got_tls_type;
  562. /* TRUE if TLS GD relocs has been seen for this object. */
  563. bool has_tlsgd;
  564. };
  565. #define _bfd_sparc_elf_tdata(abfd) \
  566. ((struct _bfd_sparc_elf_obj_tdata *) (abfd)->tdata.any)
  567. #define _bfd_sparc_elf_local_got_tls_type(abfd) \
  568. (_bfd_sparc_elf_tdata (abfd)->local_got_tls_type)
  569. #define is_sparc_elf(bfd) \
  570. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  571. && elf_tdata (bfd) != NULL \
  572. && elf_object_id (bfd) == SPARC_ELF_DATA)
  573. bool
  574. _bfd_sparc_elf_mkobject (bfd *abfd)
  575. {
  576. return bfd_elf_allocate_object (abfd, sizeof (struct _bfd_sparc_elf_obj_tdata),
  577. SPARC_ELF_DATA);
  578. }
  579. static void
  580. sparc_put_word_32 (bfd *abfd, bfd_vma val, void *ptr)
  581. {
  582. bfd_put_32 (abfd, val, ptr);
  583. }
  584. static void
  585. sparc_put_word_64 (bfd *abfd, bfd_vma val, void *ptr)
  586. {
  587. bfd_put_64 (abfd, val, ptr);
  588. }
  589. static void
  590. sparc_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  591. {
  592. const struct elf_backend_data *bed;
  593. bfd_byte *loc;
  594. bed = get_elf_backend_data (abfd);
  595. BFD_ASSERT (s->reloc_count * bed->s->sizeof_rela < s->size);
  596. loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
  597. bed->s->swap_reloca_out (abfd, rel, loc);
  598. }
  599. static bfd_vma
  600. sparc_elf_r_info_64 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
  601. bfd_vma rel_index ATTRIBUTE_UNUSED,
  602. bfd_vma type ATTRIBUTE_UNUSED)
  603. {
  604. return ELF64_R_INFO (rel_index,
  605. (in_rel ?
  606. ELF64_R_TYPE_INFO (ELF64_R_TYPE_DATA (in_rel->r_info),
  607. type) : type));
  608. }
  609. static bfd_vma
  610. sparc_elf_r_info_32 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
  611. bfd_vma rel_index, bfd_vma type)
  612. {
  613. return ELF32_R_INFO (rel_index, type);
  614. }
  615. static bfd_vma
  616. sparc_elf_r_symndx_64 (bfd_vma r_info)
  617. {
  618. bfd_vma r_symndx = ELF32_R_SYM (r_info);
  619. return (r_symndx >> 24);
  620. }
  621. static bfd_vma
  622. sparc_elf_r_symndx_32 (bfd_vma r_info)
  623. {
  624. return ELF32_R_SYM (r_info);
  625. }
  626. /* PLT/GOT stuff */
  627. #define PLT32_ENTRY_SIZE 12
  628. #define PLT32_HEADER_SIZE (4 * PLT32_ENTRY_SIZE)
  629. /* The first four entries in a 32-bit procedure linkage table are reserved,
  630. and the initial contents are unimportant (we zero them out).
  631. Subsequent entries look like this. See the SVR4 ABI SPARC
  632. supplement to see how this works. */
  633. /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
  634. #define PLT32_ENTRY_WORD0 0x03000000
  635. /* b,a .plt0. We fill in the offset later. */
  636. #define PLT32_ENTRY_WORD1 0x30800000
  637. /* nop. */
  638. #define PLT32_ENTRY_WORD2 SPARC_NOP
  639. static int
  640. sparc32_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
  641. bfd_vma max ATTRIBUTE_UNUSED,
  642. bfd_vma *r_offset)
  643. {
  644. bfd_put_32 (output_bfd,
  645. PLT32_ENTRY_WORD0 + offset,
  646. splt->contents + offset);
  647. bfd_put_32 (output_bfd,
  648. (PLT32_ENTRY_WORD1
  649. + (((- (offset + 4)) >> 2) & 0x3fffff)),
  650. splt->contents + offset + 4);
  651. bfd_put_32 (output_bfd, (bfd_vma) PLT32_ENTRY_WORD2,
  652. splt->contents + offset + 8);
  653. *r_offset = offset;
  654. return offset / PLT32_ENTRY_SIZE - 4;
  655. }
  656. /* Both the headers and the entries are icache aligned. */
  657. #define PLT64_ENTRY_SIZE 32
  658. #define PLT64_HEADER_SIZE (4 * PLT64_ENTRY_SIZE)
  659. #define PLT64_LARGE_THRESHOLD 32768
  660. static int
  661. sparc64_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
  662. bfd_vma max, bfd_vma *r_offset)
  663. {
  664. unsigned char *entry = splt->contents + offset;
  665. const unsigned int nop = SPARC_NOP;
  666. int plt_index;
  667. if (offset < (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
  668. {
  669. unsigned int sethi, ba;
  670. *r_offset = offset;
  671. plt_index = (offset / PLT64_ENTRY_SIZE);
  672. sethi = 0x03000000 | (plt_index * PLT64_ENTRY_SIZE);
  673. ba = 0x30680000
  674. | (((splt->contents + PLT64_ENTRY_SIZE) - (entry + 4)) / 4 & 0x7ffff);
  675. bfd_put_32 (output_bfd, (bfd_vma) sethi, entry);
  676. bfd_put_32 (output_bfd, (bfd_vma) ba, entry + 4);
  677. bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 8);
  678. bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 12);
  679. bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 16);
  680. bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 20);
  681. bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 24);
  682. bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 28);
  683. }
  684. else
  685. {
  686. unsigned char *ptr;
  687. unsigned int ldx;
  688. int block, last_block, ofs, last_ofs, chunks_this_block;
  689. const int insn_chunk_size = (6 * 4);
  690. const int ptr_chunk_size = (1 * 8);
  691. const int entries_per_block = 160;
  692. const int block_size = entries_per_block * (insn_chunk_size
  693. + ptr_chunk_size);
  694. /* Entries 32768 and higher are grouped into blocks of 160.
  695. The blocks are further subdivided into 160 sequences of
  696. 6 instructions and 160 pointers. If a block does not require
  697. the full 160 entries, let's say it requires N, then there
  698. will be N sequences of 6 instructions and N pointers. */
  699. offset -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
  700. max -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
  701. block = offset / block_size;
  702. last_block = max / block_size;
  703. if (block != last_block)
  704. {
  705. chunks_this_block = 160;
  706. }
  707. else
  708. {
  709. last_ofs = max % block_size;
  710. chunks_this_block = last_ofs / (insn_chunk_size + ptr_chunk_size);
  711. }
  712. ofs = offset % block_size;
  713. plt_index = (PLT64_LARGE_THRESHOLD +
  714. (block * 160) +
  715. (ofs / insn_chunk_size));
  716. ptr = splt->contents
  717. + (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
  718. + (block * block_size)
  719. + (chunks_this_block * insn_chunk_size)
  720. + (ofs / insn_chunk_size) * ptr_chunk_size;
  721. *r_offset = (bfd_vma) (ptr - splt->contents);
  722. ldx = 0xc25be000 | ((ptr - (entry+4)) & 0x1fff);
  723. /* mov %o7,%g5
  724. call .+8
  725. nop
  726. ldx [%o7+P],%g1
  727. jmpl %o7+%g1,%g1
  728. mov %g5,%o7 */
  729. bfd_put_32 (output_bfd, (bfd_vma) 0x8a10000f, entry);
  730. bfd_put_32 (output_bfd, (bfd_vma) 0x40000002, entry + 4);
  731. bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP, entry + 8);
  732. bfd_put_32 (output_bfd, (bfd_vma) ldx, entry + 12);
  733. bfd_put_32 (output_bfd, (bfd_vma) 0x83c3c001, entry + 16);
  734. bfd_put_32 (output_bfd, (bfd_vma) 0x9e100005, entry + 20);
  735. bfd_put_64 (output_bfd, (bfd_vma) (splt->contents - (entry + 4)), ptr);
  736. }
  737. return plt_index - 4;
  738. }
  739. /* The format of the first PLT entry in a VxWorks executable. */
  740. static const bfd_vma sparc_vxworks_exec_plt0_entry[] =
  741. {
  742. 0x05000000, /* sethi %hi(_GLOBAL_OFFSET_TABLE_+8), %g2 */
  743. 0x8410a000, /* or %g2, %lo(_GLOBAL_OFFSET_TABLE_+8), %g2 */
  744. 0xc4008000, /* ld [ %g2 ], %g2 */
  745. 0x81c08000, /* jmp %g2 */
  746. 0x01000000 /* nop */
  747. };
  748. /* The format of subsequent PLT entries. */
  749. static const bfd_vma sparc_vxworks_exec_plt_entry[] =
  750. {
  751. 0x03000000, /* sethi %hi(_GLOBAL_OFFSET_TABLE_+f@got), %g1 */
  752. 0x82106000, /* or %g1, %lo(_GLOBAL_OFFSET_TABLE_+f@got), %g1 */
  753. 0xc2004000, /* ld [ %g1 ], %g1 */
  754. 0x81c04000, /* jmp %g1 */
  755. 0x01000000, /* nop */
  756. 0x03000000, /* sethi %hi(f@pltindex), %g1 */
  757. 0x10800000, /* b _PLT_resolve */
  758. 0x82106000 /* or %g1, %lo(f@pltindex), %g1 */
  759. };
  760. /* The format of the first PLT entry in a VxWorks shared object. */
  761. static const bfd_vma sparc_vxworks_shared_plt0_entry[] =
  762. {
  763. 0xc405e008, /* ld [ %l7 + 8 ], %g2 */
  764. 0x81c08000, /* jmp %g2 */
  765. 0x01000000 /* nop */
  766. };
  767. /* The format of subsequent PLT entries. */
  768. static const bfd_vma sparc_vxworks_shared_plt_entry[] =
  769. {
  770. 0x03000000, /* sethi %hi(f@got), %g1 */
  771. 0x82106000, /* or %g1, %lo(f@got), %g1 */
  772. 0xc205c001, /* ld [ %l7 + %g1 ], %g1 */
  773. 0x81c04000, /* jmp %g1 */
  774. 0x01000000, /* nop */
  775. 0x03000000, /* sethi %hi(f@pltindex), %g1 */
  776. 0x10800000, /* b _PLT_resolve */
  777. 0x82106000 /* or %g1, %lo(f@pltindex), %g1 */
  778. };
  779. #define SPARC_ELF_PUT_WORD(htab, bfd, val, ptr) \
  780. htab->put_word(bfd, val, ptr)
  781. #define SPARC_ELF_R_INFO(htab, in_rel, index, type) \
  782. htab->r_info(in_rel, index, type)
  783. #define SPARC_ELF_R_SYMNDX(htab, r_info) \
  784. htab->r_symndx(r_info)
  785. #define SPARC_ELF_WORD_BYTES(htab) \
  786. htab->bytes_per_word
  787. #define SPARC_ELF_RELA_BYTES(htab) \
  788. htab->bytes_per_rela
  789. #define SPARC_ELF_DTPOFF_RELOC(htab) \
  790. htab->dtpoff_reloc
  791. #define SPARC_ELF_DTPMOD_RELOC(htab) \
  792. htab->dtpmod_reloc
  793. #define SPARC_ELF_TPOFF_RELOC(htab) \
  794. htab->tpoff_reloc
  795. #define SPARC_ELF_BUILD_PLT_ENTRY(htab, obfd, splt, off, max, r_off) \
  796. htab->build_plt_entry (obfd, splt, off, max, r_off)
  797. /* Create an entry in an SPARC ELF linker hash table. */
  798. static struct bfd_hash_entry *
  799. link_hash_newfunc (struct bfd_hash_entry *entry,
  800. struct bfd_hash_table *table, const char *string)
  801. {
  802. /* Allocate the structure if it has not already been allocated by a
  803. subclass. */
  804. if (entry == NULL)
  805. {
  806. entry = bfd_hash_allocate (table,
  807. sizeof (struct _bfd_sparc_elf_link_hash_entry));
  808. if (entry == NULL)
  809. return entry;
  810. }
  811. /* Call the allocation method of the superclass. */
  812. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  813. if (entry != NULL)
  814. {
  815. struct _bfd_sparc_elf_link_hash_entry *eh;
  816. eh = (struct _bfd_sparc_elf_link_hash_entry *) entry;
  817. eh->tls_type = GOT_UNKNOWN;
  818. eh->has_got_reloc = 0;
  819. eh->has_non_got_reloc = 0;
  820. }
  821. return entry;
  822. }
  823. /* The name of the dynamic interpreter. This is put in the .interp
  824. section. */
  825. #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  826. #define ELF64_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
  827. /* Compute a hash of a local hash entry. We use elf_link_hash_entry
  828. for local symbol so that we can handle local STT_GNU_IFUNC symbols
  829. as global symbol. We reuse indx and dynstr_index for local symbol
  830. hash since they aren't used by global symbols in this backend. */
  831. static hashval_t
  832. elf_sparc_local_htab_hash (const void *ptr)
  833. {
  834. struct elf_link_hash_entry *h
  835. = (struct elf_link_hash_entry *) ptr;
  836. return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
  837. }
  838. /* Compare local hash entries. */
  839. static int
  840. elf_sparc_local_htab_eq (const void *ptr1, const void *ptr2)
  841. {
  842. struct elf_link_hash_entry *h1
  843. = (struct elf_link_hash_entry *) ptr1;
  844. struct elf_link_hash_entry *h2
  845. = (struct elf_link_hash_entry *) ptr2;
  846. return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
  847. }
  848. /* Find and/or create a hash entry for local symbol. */
  849. static struct elf_link_hash_entry *
  850. elf_sparc_get_local_sym_hash (struct _bfd_sparc_elf_link_hash_table *htab,
  851. bfd *abfd, const Elf_Internal_Rela *rel,
  852. bool create)
  853. {
  854. struct _bfd_sparc_elf_link_hash_entry e, *ret;
  855. asection *sec = abfd->sections;
  856. unsigned long r_symndx;
  857. hashval_t h;
  858. void **slot;
  859. r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
  860. h = ELF_LOCAL_SYMBOL_HASH (sec->id, r_symndx);
  861. e.elf.indx = sec->id;
  862. e.elf.dynstr_index = r_symndx;
  863. slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
  864. create ? INSERT : NO_INSERT);
  865. if (!slot)
  866. return NULL;
  867. if (*slot)
  868. {
  869. ret = (struct _bfd_sparc_elf_link_hash_entry *) *slot;
  870. return &ret->elf;
  871. }
  872. ret = (struct _bfd_sparc_elf_link_hash_entry *)
  873. objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
  874. sizeof (struct _bfd_sparc_elf_link_hash_entry));
  875. if (ret)
  876. {
  877. memset (ret, 0, sizeof (*ret));
  878. ret->elf.indx = sec->id;
  879. ret->elf.dynstr_index = r_symndx;
  880. ret->elf.dynindx = -1;
  881. ret->elf.plt.offset = (bfd_vma) -1;
  882. ret->elf.got.offset = (bfd_vma) -1;
  883. *slot = ret;
  884. }
  885. return &ret->elf;
  886. }
  887. /* Destroy a SPARC ELF linker hash table. */
  888. static void
  889. _bfd_sparc_elf_link_hash_table_free (bfd *obfd)
  890. {
  891. struct _bfd_sparc_elf_link_hash_table *htab
  892. = (struct _bfd_sparc_elf_link_hash_table *) obfd->link.hash;
  893. if (htab->loc_hash_table)
  894. htab_delete (htab->loc_hash_table);
  895. if (htab->loc_hash_memory)
  896. objalloc_free ((struct objalloc *) htab->loc_hash_memory);
  897. _bfd_elf_link_hash_table_free (obfd);
  898. }
  899. /* Create a SPARC ELF linker hash table. */
  900. struct bfd_link_hash_table *
  901. _bfd_sparc_elf_link_hash_table_create (bfd *abfd)
  902. {
  903. struct _bfd_sparc_elf_link_hash_table *ret;
  904. size_t amt = sizeof (struct _bfd_sparc_elf_link_hash_table);
  905. ret = (struct _bfd_sparc_elf_link_hash_table *) bfd_zmalloc (amt);
  906. if (ret == NULL)
  907. return NULL;
  908. if (ABI_64_P (abfd))
  909. {
  910. ret->put_word = sparc_put_word_64;
  911. ret->r_info = sparc_elf_r_info_64;
  912. ret->r_symndx = sparc_elf_r_symndx_64;
  913. ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF64;
  914. ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD64;
  915. ret->tpoff_reloc = R_SPARC_TLS_TPOFF64;
  916. ret->word_align_power = 3;
  917. ret->align_power_max = 4;
  918. ret->bytes_per_word = 8;
  919. ret->bytes_per_rela = sizeof (Elf64_External_Rela);
  920. ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
  921. ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
  922. ret->build_plt_entry = sparc64_plt_entry_build;
  923. ret->plt_header_size = PLT64_HEADER_SIZE;
  924. ret->plt_entry_size = PLT64_ENTRY_SIZE;
  925. }
  926. else
  927. {
  928. ret->put_word = sparc_put_word_32;
  929. ret->r_info = sparc_elf_r_info_32;
  930. ret->r_symndx = sparc_elf_r_symndx_32;
  931. ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF32;
  932. ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD32;
  933. ret->tpoff_reloc = R_SPARC_TLS_TPOFF32;
  934. ret->word_align_power = 2;
  935. ret->align_power_max = 3;
  936. ret->bytes_per_word = 4;
  937. ret->bytes_per_rela = sizeof (Elf32_External_Rela);
  938. ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
  939. ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
  940. ret->build_plt_entry = sparc32_plt_entry_build;
  941. ret->plt_header_size = PLT32_HEADER_SIZE;
  942. ret->plt_entry_size = PLT32_ENTRY_SIZE;
  943. }
  944. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
  945. sizeof (struct _bfd_sparc_elf_link_hash_entry),
  946. SPARC_ELF_DATA))
  947. {
  948. free (ret);
  949. return NULL;
  950. }
  951. ret->loc_hash_table = htab_try_create (1024,
  952. elf_sparc_local_htab_hash,
  953. elf_sparc_local_htab_eq,
  954. NULL);
  955. ret->loc_hash_memory = objalloc_create ();
  956. if (!ret->loc_hash_table || !ret->loc_hash_memory)
  957. {
  958. _bfd_sparc_elf_link_hash_table_free (abfd);
  959. return NULL;
  960. }
  961. ret->elf.root.hash_table_free = _bfd_sparc_elf_link_hash_table_free;
  962. return &ret->elf.root;
  963. }
  964. /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
  965. .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
  966. hash table. */
  967. bool
  968. _bfd_sparc_elf_create_dynamic_sections (bfd *dynobj,
  969. struct bfd_link_info *info)
  970. {
  971. struct _bfd_sparc_elf_link_hash_table *htab;
  972. htab = _bfd_sparc_elf_hash_table (info);
  973. BFD_ASSERT (htab != NULL);
  974. if (!_bfd_elf_create_dynamic_sections (dynobj, info))
  975. return false;
  976. if (htab->elf.target_os == is_vxworks)
  977. {
  978. if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
  979. return false;
  980. if (bfd_link_pic (info))
  981. {
  982. htab->plt_header_size
  983. = 4 * ARRAY_SIZE (sparc_vxworks_shared_plt0_entry);
  984. htab->plt_entry_size
  985. = 4 * ARRAY_SIZE (sparc_vxworks_shared_plt_entry);
  986. }
  987. else
  988. {
  989. htab->plt_header_size
  990. = 4 * ARRAY_SIZE (sparc_vxworks_exec_plt0_entry);
  991. htab->plt_entry_size
  992. = 4 * ARRAY_SIZE (sparc_vxworks_exec_plt_entry);
  993. }
  994. }
  995. if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
  996. || (!bfd_link_pic (info) && !htab->elf.srelbss))
  997. abort ();
  998. return true;
  999. }
  1000. static bool
  1001. create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
  1002. {
  1003. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1004. struct elf_link_hash_table *htab = elf_hash_table (info);
  1005. flagword flags, pltflags;
  1006. asection *s;
  1007. if (htab->irelifunc != NULL || htab->iplt != NULL)
  1008. return true;
  1009. flags = bed->dynamic_sec_flags;
  1010. pltflags = flags | SEC_ALLOC | SEC_CODE | SEC_LOAD;
  1011. s = bfd_make_section_with_flags (abfd, ".iplt", pltflags);
  1012. if (s == NULL
  1013. || !bfd_set_section_alignment (s, bed->plt_alignment))
  1014. return false;
  1015. htab->iplt = s;
  1016. s = bfd_make_section_with_flags (abfd, ".rela.iplt",
  1017. flags | SEC_READONLY);
  1018. if (s == NULL
  1019. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  1020. return false;
  1021. htab->irelplt = s;
  1022. return true;
  1023. }
  1024. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  1025. void
  1026. _bfd_sparc_elf_copy_indirect_symbol (struct bfd_link_info *info,
  1027. struct elf_link_hash_entry *dir,
  1028. struct elf_link_hash_entry *ind)
  1029. {
  1030. struct _bfd_sparc_elf_link_hash_entry *edir, *eind;
  1031. edir = (struct _bfd_sparc_elf_link_hash_entry *) dir;
  1032. eind = (struct _bfd_sparc_elf_link_hash_entry *) ind;
  1033. if (ind->root.type == bfd_link_hash_indirect && dir->got.refcount <= 0)
  1034. {
  1035. edir->tls_type = eind->tls_type;
  1036. eind->tls_type = GOT_UNKNOWN;
  1037. }
  1038. /* Copy has_got_reloc and has_non_got_reloc. */
  1039. edir->has_got_reloc |= eind->has_got_reloc;
  1040. edir->has_non_got_reloc |= eind->has_non_got_reloc;
  1041. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  1042. }
  1043. static int
  1044. sparc_elf_tls_transition (struct bfd_link_info *info, bfd *abfd,
  1045. int r_type, int is_local)
  1046. {
  1047. if (! ABI_64_P (abfd)
  1048. && r_type == R_SPARC_TLS_GD_HI22
  1049. && ! _bfd_sparc_elf_tdata (abfd)->has_tlsgd)
  1050. return R_SPARC_REV32;
  1051. if (!bfd_link_executable (info))
  1052. return r_type;
  1053. switch (r_type)
  1054. {
  1055. case R_SPARC_TLS_GD_HI22:
  1056. return is_local ? R_SPARC_TLS_LE_HIX22 : R_SPARC_TLS_IE_HI22;
  1057. case R_SPARC_TLS_GD_LO10:
  1058. return is_local ? R_SPARC_TLS_LE_LOX10 : R_SPARC_TLS_IE_LO10;
  1059. case R_SPARC_TLS_LDM_HI22:
  1060. return R_SPARC_TLS_LE_HIX22;
  1061. case R_SPARC_TLS_LDM_LO10:
  1062. return R_SPARC_TLS_LE_LOX10;
  1063. case R_SPARC_TLS_IE_HI22:
  1064. return is_local ? R_SPARC_TLS_LE_HIX22 : r_type;
  1065. case R_SPARC_TLS_IE_LO10:
  1066. return is_local ? R_SPARC_TLS_LE_LOX10 : r_type;
  1067. }
  1068. return r_type;
  1069. }
  1070. /* Look through the relocs for a section during the first phase, and
  1071. allocate space in the global offset table or procedure linkage
  1072. table. */
  1073. bool
  1074. _bfd_sparc_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
  1075. asection *sec, const Elf_Internal_Rela *relocs)
  1076. {
  1077. struct _bfd_sparc_elf_link_hash_table *htab;
  1078. Elf_Internal_Shdr *symtab_hdr;
  1079. struct elf_link_hash_entry **sym_hashes;
  1080. const Elf_Internal_Rela *rel;
  1081. const Elf_Internal_Rela *rel_end;
  1082. asection *sreloc;
  1083. int num_relocs;
  1084. bool checked_tlsgd = false;
  1085. if (bfd_link_relocatable (info))
  1086. return true;
  1087. htab = _bfd_sparc_elf_hash_table (info);
  1088. BFD_ASSERT (htab != NULL);
  1089. symtab_hdr = &elf_symtab_hdr (abfd);
  1090. sym_hashes = elf_sym_hashes (abfd);
  1091. sreloc = NULL;
  1092. if (ABI_64_P (abfd))
  1093. num_relocs = NUM_SHDR_ENTRIES (_bfd_elf_single_rel_hdr (sec));
  1094. else
  1095. num_relocs = sec->reloc_count;
  1096. BFD_ASSERT (is_sparc_elf (abfd) || num_relocs == 0);
  1097. if (htab->elf.dynobj == NULL)
  1098. htab->elf.dynobj = abfd;
  1099. if (!create_ifunc_sections (htab->elf.dynobj, info))
  1100. return false;
  1101. rel_end = relocs + num_relocs;
  1102. for (rel = relocs; rel < rel_end; rel++)
  1103. {
  1104. unsigned int r_type;
  1105. unsigned int r_symndx;
  1106. struct elf_link_hash_entry *h;
  1107. struct _bfd_sparc_elf_link_hash_entry *eh;
  1108. Elf_Internal_Sym *isym;
  1109. r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
  1110. r_type = SPARC_ELF_R_TYPE (rel->r_info);
  1111. if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
  1112. {
  1113. /* xgettext:c-format */
  1114. _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd, r_symndx);
  1115. return false;
  1116. }
  1117. isym = NULL;
  1118. if (r_symndx < symtab_hdr->sh_info)
  1119. {
  1120. /* A local symbol. */
  1121. isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
  1122. r_symndx);
  1123. if (isym == NULL)
  1124. return false;
  1125. /* Check relocation against local STT_GNU_IFUNC symbol. */
  1126. if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  1127. {
  1128. h = elf_sparc_get_local_sym_hash (htab, abfd, rel, true);
  1129. if (h == NULL)
  1130. return false;
  1131. /* Fake a STT_GNU_IFUNC symbol. */
  1132. h->type = STT_GNU_IFUNC;
  1133. h->def_regular = 1;
  1134. h->ref_regular = 1;
  1135. h->forced_local = 1;
  1136. h->root.type = bfd_link_hash_defined;
  1137. }
  1138. else
  1139. h = NULL;
  1140. }
  1141. else
  1142. {
  1143. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1144. while (h->root.type == bfd_link_hash_indirect
  1145. || h->root.type == bfd_link_hash_warning)
  1146. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1147. }
  1148. if (h && h->type == STT_GNU_IFUNC && h->def_regular)
  1149. {
  1150. h->ref_regular = 1;
  1151. h->plt.refcount += 1;
  1152. }
  1153. /* Compatibility with old R_SPARC_REV32 reloc conflicting
  1154. with R_SPARC_TLS_GD_HI22. */
  1155. if (! ABI_64_P (abfd) && ! checked_tlsgd)
  1156. switch (r_type)
  1157. {
  1158. case R_SPARC_TLS_GD_HI22:
  1159. {
  1160. const Elf_Internal_Rela *relt;
  1161. for (relt = rel + 1; relt < rel_end; relt++)
  1162. if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10
  1163. || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD
  1164. || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL)
  1165. break;
  1166. checked_tlsgd = true;
  1167. _bfd_sparc_elf_tdata (abfd)->has_tlsgd = relt < rel_end;
  1168. }
  1169. break;
  1170. case R_SPARC_TLS_GD_LO10:
  1171. case R_SPARC_TLS_GD_ADD:
  1172. case R_SPARC_TLS_GD_CALL:
  1173. checked_tlsgd = true;
  1174. _bfd_sparc_elf_tdata (abfd)->has_tlsgd = true;
  1175. break;
  1176. }
  1177. r_type = sparc_elf_tls_transition (info, abfd, r_type, h == NULL);
  1178. eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
  1179. switch (r_type)
  1180. {
  1181. case R_SPARC_TLS_LDM_HI22:
  1182. case R_SPARC_TLS_LDM_LO10:
  1183. htab->tls_ldm_got.refcount += 1;
  1184. if (eh != NULL)
  1185. eh->has_got_reloc = 1;
  1186. break;
  1187. case R_SPARC_TLS_LE_HIX22:
  1188. case R_SPARC_TLS_LE_LOX10:
  1189. if (!bfd_link_executable (info))
  1190. goto r_sparc_plt32;
  1191. break;
  1192. case R_SPARC_TLS_IE_HI22:
  1193. case R_SPARC_TLS_IE_LO10:
  1194. if (!bfd_link_executable (info))
  1195. info->flags |= DF_STATIC_TLS;
  1196. /* Fall through */
  1197. case R_SPARC_GOT10:
  1198. case R_SPARC_GOT13:
  1199. case R_SPARC_GOT22:
  1200. case R_SPARC_GOTDATA_HIX22:
  1201. case R_SPARC_GOTDATA_LOX10:
  1202. case R_SPARC_GOTDATA_OP_HIX22:
  1203. case R_SPARC_GOTDATA_OP_LOX10:
  1204. case R_SPARC_TLS_GD_HI22:
  1205. case R_SPARC_TLS_GD_LO10:
  1206. /* This symbol requires a global offset table entry. */
  1207. {
  1208. int tls_type, old_tls_type;
  1209. switch (r_type)
  1210. {
  1211. case R_SPARC_TLS_GD_HI22:
  1212. case R_SPARC_TLS_GD_LO10:
  1213. tls_type = GOT_TLS_GD;
  1214. break;
  1215. case R_SPARC_TLS_IE_HI22:
  1216. case R_SPARC_TLS_IE_LO10:
  1217. tls_type = GOT_TLS_IE;
  1218. break;
  1219. default:
  1220. tls_type = GOT_NORMAL;
  1221. break;
  1222. }
  1223. if (h != NULL)
  1224. {
  1225. h->got.refcount += 1;
  1226. old_tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
  1227. }
  1228. else
  1229. {
  1230. bfd_signed_vma *local_got_refcounts;
  1231. /* This is a global offset table entry for a local symbol. */
  1232. local_got_refcounts = elf_local_got_refcounts (abfd);
  1233. if (local_got_refcounts == NULL)
  1234. {
  1235. bfd_size_type size;
  1236. size = symtab_hdr->sh_info;
  1237. size *= (sizeof (bfd_signed_vma) + sizeof(char));
  1238. local_got_refcounts = ((bfd_signed_vma *)
  1239. bfd_zalloc (abfd, size));
  1240. if (local_got_refcounts == NULL)
  1241. return false;
  1242. elf_local_got_refcounts (abfd) = local_got_refcounts;
  1243. _bfd_sparc_elf_local_got_tls_type (abfd)
  1244. = (char *) (local_got_refcounts + symtab_hdr->sh_info);
  1245. }
  1246. if (r_type != R_SPARC_GOTDATA_OP_HIX22
  1247. && r_type != R_SPARC_GOTDATA_OP_LOX10)
  1248. local_got_refcounts[r_symndx] += 1;
  1249. old_tls_type
  1250. = _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx];
  1251. }
  1252. /* If a TLS symbol is accessed using IE at least once, there is no
  1253. point in using the dynamic model for it. */
  1254. if (old_tls_type != tls_type)
  1255. {
  1256. if (old_tls_type == GOT_UNKNOWN)
  1257. ;
  1258. else if (old_tls_type == GOT_TLS_GD && tls_type == GOT_TLS_IE)
  1259. ;
  1260. else if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
  1261. tls_type = old_tls_type;
  1262. else
  1263. {
  1264. _bfd_error_handler
  1265. /* xgettext:c-format */
  1266. (_("%pB: `%s' accessed both as normal and thread local symbol"),
  1267. abfd, h ? h->root.root.string : "<local>");
  1268. return false;
  1269. }
  1270. if (h != NULL)
  1271. _bfd_sparc_elf_hash_entry (h)->tls_type = tls_type;
  1272. else
  1273. _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
  1274. }
  1275. }
  1276. if (!htab->elf.sgot
  1277. && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
  1278. return false;
  1279. if (eh != NULL)
  1280. {
  1281. eh->has_got_reloc = 1;
  1282. if (r_type == R_SPARC_GOT10
  1283. || r_type == R_SPARC_GOT13
  1284. || r_type == R_SPARC_GOT22)
  1285. eh->has_old_style_got_reloc = 1;
  1286. }
  1287. break;
  1288. case R_SPARC_TLS_GD_CALL:
  1289. case R_SPARC_TLS_LDM_CALL:
  1290. if (bfd_link_executable (info))
  1291. break;
  1292. /* Essentially R_SPARC_WPLT30 relocs against __tls_get_addr. */
  1293. h = (struct elf_link_hash_entry *)
  1294. bfd_link_hash_lookup (info->hash, "__tls_get_addr", false,
  1295. false, true);
  1296. BFD_ASSERT (h != NULL);
  1297. /* Fall through */
  1298. case R_SPARC_WPLT30:
  1299. case R_SPARC_PLT32:
  1300. case R_SPARC_PLT64:
  1301. case R_SPARC_HIPLT22:
  1302. case R_SPARC_LOPLT10:
  1303. case R_SPARC_PCPLT32:
  1304. case R_SPARC_PCPLT22:
  1305. case R_SPARC_PCPLT10:
  1306. /* This symbol requires a procedure linkage table entry.
  1307. We actually build the entry in adjust_dynamic_symbol,
  1308. because this might be a case of linking PIC code without
  1309. linking in any dynamic objects, in which case we don't
  1310. need to generate a procedure linkage table after all. */
  1311. if (h == NULL)
  1312. {
  1313. if (! ABI_64_P (abfd))
  1314. {
  1315. /* The Solaris native assembler will generate a WPLT30
  1316. reloc for a local symbol if you assemble a call from
  1317. one section to another when using -K pic. We treat
  1318. it as WDISP30. */
  1319. if (r_type == R_SPARC_PLT32)
  1320. goto r_sparc_plt32;
  1321. break;
  1322. }
  1323. /* PR 7027: We need similar behaviour for 64-bit binaries. */
  1324. else if (r_type == R_SPARC_WPLT30)
  1325. break;
  1326. /* It does not make sense to have a procedure linkage
  1327. table entry for a local symbol. */
  1328. bfd_set_error (bfd_error_bad_value);
  1329. return false;
  1330. }
  1331. h->needs_plt = 1;
  1332. if (r_type == R_SPARC_PLT32 || r_type == R_SPARC_PLT64)
  1333. goto r_sparc_plt32;
  1334. h->plt.refcount += 1;
  1335. eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
  1336. eh->has_got_reloc = 1;
  1337. break;
  1338. case R_SPARC_PC10:
  1339. case R_SPARC_PC22:
  1340. case R_SPARC_PC_HH22:
  1341. case R_SPARC_PC_HM10:
  1342. case R_SPARC_PC_LM22:
  1343. if (h != NULL)
  1344. h->non_got_ref = 1;
  1345. if (h != NULL
  1346. && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  1347. break;
  1348. /* Fall through. */
  1349. case R_SPARC_DISP8:
  1350. case R_SPARC_DISP16:
  1351. case R_SPARC_DISP32:
  1352. case R_SPARC_DISP64:
  1353. case R_SPARC_WDISP30:
  1354. case R_SPARC_WDISP22:
  1355. case R_SPARC_WDISP19:
  1356. case R_SPARC_WDISP16:
  1357. case R_SPARC_WDISP10:
  1358. case R_SPARC_8:
  1359. case R_SPARC_16:
  1360. case R_SPARC_32:
  1361. case R_SPARC_HI22:
  1362. case R_SPARC_22:
  1363. case R_SPARC_13:
  1364. case R_SPARC_LO10:
  1365. case R_SPARC_UA16:
  1366. case R_SPARC_UA32:
  1367. case R_SPARC_10:
  1368. case R_SPARC_11:
  1369. case R_SPARC_64:
  1370. case R_SPARC_OLO10:
  1371. case R_SPARC_HH22:
  1372. case R_SPARC_HM10:
  1373. case R_SPARC_LM22:
  1374. case R_SPARC_7:
  1375. case R_SPARC_5:
  1376. case R_SPARC_6:
  1377. case R_SPARC_HIX22:
  1378. case R_SPARC_LOX10:
  1379. case R_SPARC_H44:
  1380. case R_SPARC_M44:
  1381. case R_SPARC_L44:
  1382. case R_SPARC_H34:
  1383. case R_SPARC_UA64:
  1384. if (h != NULL)
  1385. h->non_got_ref = 1;
  1386. if (eh != NULL && (sec->flags & SEC_CODE) != 0)
  1387. eh->has_non_got_reloc = 1;
  1388. r_sparc_plt32:
  1389. if (h != NULL && !bfd_link_pic (info))
  1390. {
  1391. /* We may need a .plt entry if the function this reloc
  1392. refers to is in a shared lib. */
  1393. h->plt.refcount += 1;
  1394. }
  1395. /* If we are creating a shared library, and this is a reloc
  1396. against a global symbol, or a non PC relative reloc
  1397. against a local symbol, then we need to copy the reloc
  1398. into the shared library. However, if we are linking with
  1399. -Bsymbolic, we do not need to copy a reloc against a
  1400. global symbol which is defined in an object we are
  1401. including in the link (i.e., DEF_REGULAR is set). At
  1402. this point we have not seen all the input files, so it is
  1403. possible that DEF_REGULAR is not set now but will be set
  1404. later (it is never cleared). In case of a weak definition,
  1405. DEF_REGULAR may be cleared later by a strong definition in
  1406. a shared library. We account for that possibility below by
  1407. storing information in the relocs_copied field of the hash
  1408. table entry. A similar situation occurs when creating
  1409. shared libraries and symbol visibility changes render the
  1410. symbol local.
  1411. If on the other hand, we are creating an executable, we
  1412. may need to keep relocations for symbols satisfied by a
  1413. dynamic library if we manage to avoid copy relocs for the
  1414. symbol. */
  1415. if ((bfd_link_pic (info)
  1416. && (sec->flags & SEC_ALLOC) != 0
  1417. && (! _bfd_sparc_elf_howto_table[r_type].pc_relative
  1418. || (h != NULL
  1419. && (! SYMBOLIC_BIND (info, h)
  1420. || h->root.type == bfd_link_hash_defweak
  1421. || !h->def_regular))))
  1422. || (!bfd_link_pic (info)
  1423. && (sec->flags & SEC_ALLOC) != 0
  1424. && h != NULL
  1425. && (h->root.type == bfd_link_hash_defweak
  1426. || !h->def_regular))
  1427. || (!bfd_link_pic (info)
  1428. && h != NULL
  1429. && h->type == STT_GNU_IFUNC))
  1430. {
  1431. struct elf_dyn_relocs *p;
  1432. struct elf_dyn_relocs **head;
  1433. /* When creating a shared object, we must copy these
  1434. relocs into the output file. We create a reloc
  1435. section in dynobj and make room for the reloc. */
  1436. if (sreloc == NULL)
  1437. {
  1438. sreloc = _bfd_elf_make_dynamic_reloc_section
  1439. (sec, htab->elf.dynobj, htab->word_align_power,
  1440. abfd, /*rela?*/ true);
  1441. if (sreloc == NULL)
  1442. return false;
  1443. }
  1444. /* If this is a global symbol, we count the number of
  1445. relocations we need for this symbol. */
  1446. if (h != NULL)
  1447. head = &h->dyn_relocs;
  1448. else
  1449. {
  1450. /* Track dynamic relocs needed for local syms too.
  1451. We really need local syms available to do this
  1452. easily. Oh well. */
  1453. asection *s;
  1454. void *vpp;
  1455. BFD_ASSERT (isym != NULL);
  1456. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1457. if (s == NULL)
  1458. s = sec;
  1459. vpp = &elf_section_data (s)->local_dynrel;
  1460. head = (struct elf_dyn_relocs **) vpp;
  1461. }
  1462. p = *head;
  1463. if (p == NULL || p->sec != sec)
  1464. {
  1465. size_t amt = sizeof *p;
  1466. p = ((struct elf_dyn_relocs *)
  1467. bfd_alloc (htab->elf.dynobj, amt));
  1468. if (p == NULL)
  1469. return false;
  1470. p->next = *head;
  1471. *head = p;
  1472. p->sec = sec;
  1473. p->count = 0;
  1474. p->pc_count = 0;
  1475. }
  1476. p->count += 1;
  1477. if (_bfd_sparc_elf_howto_table[r_type].pc_relative)
  1478. p->pc_count += 1;
  1479. }
  1480. break;
  1481. case R_SPARC_GNU_VTINHERIT:
  1482. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  1483. return false;
  1484. break;
  1485. case R_SPARC_GNU_VTENTRY:
  1486. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  1487. return false;
  1488. break;
  1489. case R_SPARC_REGISTER:
  1490. /* Nothing to do. */
  1491. break;
  1492. default:
  1493. break;
  1494. }
  1495. }
  1496. return true;
  1497. }
  1498. asection *
  1499. _bfd_sparc_elf_gc_mark_hook (asection *sec,
  1500. struct bfd_link_info *info,
  1501. Elf_Internal_Rela *rel,
  1502. struct elf_link_hash_entry *h,
  1503. Elf_Internal_Sym *sym)
  1504. {
  1505. if (h != NULL)
  1506. switch (SPARC_ELF_R_TYPE (rel->r_info))
  1507. {
  1508. case R_SPARC_GNU_VTINHERIT:
  1509. case R_SPARC_GNU_VTENTRY:
  1510. return NULL;
  1511. }
  1512. if (!bfd_link_executable (info))
  1513. {
  1514. switch (SPARC_ELF_R_TYPE (rel->r_info))
  1515. {
  1516. case R_SPARC_TLS_GD_CALL:
  1517. case R_SPARC_TLS_LDM_CALL:
  1518. /* This reloc implicitly references __tls_get_addr. We know
  1519. another reloc will reference the same symbol as the one
  1520. on this reloc, so the real symbol and section will be
  1521. gc marked when processing the other reloc. That lets
  1522. us handle __tls_get_addr here. */
  1523. h = elf_link_hash_lookup (elf_hash_table (info), "__tls_get_addr",
  1524. false, false, true);
  1525. BFD_ASSERT (h != NULL);
  1526. h->mark = 1;
  1527. if (h->is_weakalias)
  1528. weakdef (h)->mark = 1;
  1529. sym = NULL;
  1530. }
  1531. }
  1532. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1533. }
  1534. static Elf_Internal_Rela *
  1535. sparc_elf_find_reloc_at_ofs (Elf_Internal_Rela *rel,
  1536. Elf_Internal_Rela *relend,
  1537. bfd_vma offset)
  1538. {
  1539. while (rel < relend)
  1540. {
  1541. if (rel->r_offset == offset)
  1542. return rel;
  1543. rel++;
  1544. }
  1545. return NULL;
  1546. }
  1547. /* Remove undefined weak symbol from the dynamic symbol table if it
  1548. is resolved to 0. */
  1549. bool
  1550. _bfd_sparc_elf_fixup_symbol (struct bfd_link_info *info,
  1551. struct elf_link_hash_entry *h)
  1552. {
  1553. if (h->dynindx != -1
  1554. && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
  1555. _bfd_sparc_elf_hash_entry (h)))
  1556. {
  1557. h->dynindx = -1;
  1558. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  1559. h->dynstr_index);
  1560. }
  1561. return true;
  1562. }
  1563. /* Adjust a symbol defined by a dynamic object and referenced by a
  1564. regular object. The current definition is in some section of the
  1565. dynamic object, but we're not including those sections. We have to
  1566. change the definition to something the rest of the link can
  1567. understand. */
  1568. bool
  1569. _bfd_sparc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  1570. struct elf_link_hash_entry *h)
  1571. {
  1572. struct _bfd_sparc_elf_link_hash_table *htab;
  1573. asection *s, *srel;
  1574. htab = _bfd_sparc_elf_hash_table (info);
  1575. BFD_ASSERT (htab != NULL);
  1576. /* Make sure we know what is going on here. */
  1577. BFD_ASSERT (htab->elf.dynobj != NULL
  1578. && (h->needs_plt
  1579. || h->type == STT_GNU_IFUNC
  1580. || h->is_weakalias
  1581. || (h->def_dynamic
  1582. && h->ref_regular
  1583. && !h->def_regular)));
  1584. /* If this is a function, put it in the procedure linkage table. We
  1585. will fill in the contents of the procedure linkage table later
  1586. (although we could actually do it here). The STT_NOTYPE
  1587. condition is a hack specifically for the Oracle libraries
  1588. delivered for Solaris; for some inexplicable reason, they define
  1589. some of their functions as STT_NOTYPE when they really should be
  1590. STT_FUNC. */
  1591. if (h->type == STT_FUNC
  1592. || h->type == STT_GNU_IFUNC
  1593. || h->needs_plt
  1594. || (h->type == STT_NOTYPE
  1595. && (h->root.type == bfd_link_hash_defined
  1596. || h->root.type == bfd_link_hash_defweak)
  1597. && (h->root.u.def.section->flags & SEC_CODE) != 0))
  1598. {
  1599. if (h->plt.refcount <= 0
  1600. || (h->type != STT_GNU_IFUNC
  1601. && (SYMBOL_CALLS_LOCAL (info, h)
  1602. || (h->root.type == bfd_link_hash_undefweak
  1603. && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))))
  1604. {
  1605. /* This case can occur if we saw a WPLT30 reloc in an input
  1606. file, but the symbol was never referred to by a dynamic
  1607. object, or if all references were garbage collected. In
  1608. such a case, we don't actually need to build a procedure
  1609. linkage table, and we can just do a WDISP30 reloc instead. */
  1610. h->plt.offset = (bfd_vma) -1;
  1611. h->needs_plt = 0;
  1612. }
  1613. return true;
  1614. }
  1615. else
  1616. h->plt.offset = (bfd_vma) -1;
  1617. /* If this is a weak symbol, and there is a real definition, the
  1618. processor independent code will have arranged for us to see the
  1619. real definition first, and we can just use the same value. */
  1620. if (h->is_weakalias)
  1621. {
  1622. struct elf_link_hash_entry *def = weakdef (h);
  1623. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  1624. h->root.u.def.section = def->root.u.def.section;
  1625. h->root.u.def.value = def->root.u.def.value;
  1626. return true;
  1627. }
  1628. /* This is a reference to a symbol defined by a dynamic object which
  1629. is not a function. */
  1630. /* If we are creating a shared library, we must presume that the
  1631. only references to the symbol are via the global offset table.
  1632. For such cases we need not do anything here; the relocations will
  1633. be handled correctly by relocate_section. */
  1634. if (bfd_link_pic (info))
  1635. return true;
  1636. /* If there are no references to this symbol that do not use the
  1637. GOT, we don't need to generate a copy reloc. */
  1638. if (!h->non_got_ref)
  1639. return true;
  1640. /* If -z nocopyreloc was given, we won't generate them either. */
  1641. if (info->nocopyreloc)
  1642. {
  1643. h->non_got_ref = 0;
  1644. return true;
  1645. }
  1646. /* If we don't find any dynamic relocs in read-only sections, then
  1647. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  1648. if (!_bfd_elf_readonly_dynrelocs (h))
  1649. {
  1650. h->non_got_ref = 0;
  1651. return true;
  1652. }
  1653. /* We must allocate the symbol in our .dynbss section, which will
  1654. become part of the .bss section of the executable. There will be
  1655. an entry for this symbol in the .dynsym section. The dynamic
  1656. object will contain position independent code, so all references
  1657. from the dynamic object to this symbol will go through the global
  1658. offset table. The dynamic linker will use the .dynsym entry to
  1659. determine the address it must put in the global offset table, so
  1660. both the dynamic object and the regular object will refer to the
  1661. same memory location for the variable. */
  1662. /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
  1663. to copy the initial value out of the dynamic object and into the
  1664. runtime process image. We need to remember the offset into the
  1665. .rel.bss section we are going to use. */
  1666. if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  1667. {
  1668. s = htab->elf.sdynrelro;
  1669. srel = htab->elf.sreldynrelro;
  1670. }
  1671. else
  1672. {
  1673. s = htab->elf.sdynbss;
  1674. srel = htab->elf.srelbss;
  1675. }
  1676. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  1677. {
  1678. srel->size += SPARC_ELF_RELA_BYTES (htab);
  1679. h->needs_copy = 1;
  1680. }
  1681. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  1682. }
  1683. /* Allocate space in .plt, .got and associated reloc sections for
  1684. dynamic relocs. */
  1685. static bool
  1686. allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
  1687. {
  1688. struct bfd_link_info *info;
  1689. struct _bfd_sparc_elf_link_hash_table *htab;
  1690. struct _bfd_sparc_elf_link_hash_entry *eh;
  1691. struct elf_dyn_relocs *p;
  1692. bool resolved_to_zero;
  1693. if (h->root.type == bfd_link_hash_indirect)
  1694. return true;
  1695. info = (struct bfd_link_info *) inf;
  1696. htab = _bfd_sparc_elf_hash_table (info);
  1697. BFD_ASSERT (htab != NULL);
  1698. eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
  1699. resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
  1700. if ((htab->elf.dynamic_sections_created
  1701. && h->plt.refcount > 0)
  1702. || (h->type == STT_GNU_IFUNC
  1703. && h->def_regular
  1704. && h->ref_regular))
  1705. {
  1706. /* Undefined weak syms won't yet be marked as dynamic. */
  1707. if (h->root.type == bfd_link_hash_undefweak
  1708. && !resolved_to_zero
  1709. && h->dynindx == -1
  1710. && !h->forced_local)
  1711. {
  1712. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1713. return false;
  1714. }
  1715. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h)
  1716. || (h->type == STT_GNU_IFUNC
  1717. && h->def_regular))
  1718. {
  1719. asection *s = htab->elf.splt;
  1720. if (s == NULL)
  1721. s = htab->elf.iplt;
  1722. /* Allocate room for the header. */
  1723. if (s->size == 0)
  1724. {
  1725. s->size = htab->plt_header_size;
  1726. /* Allocate space for the .rela.plt.unloaded relocations. */
  1727. if (htab->elf.target_os == is_vxworks
  1728. && !bfd_link_pic (info))
  1729. htab->srelplt2->size = sizeof (Elf32_External_Rela) * 2;
  1730. }
  1731. /* The procedure linkage table size is bounded by the magnitude
  1732. of the offset we can describe in the entry. */
  1733. if (s->size >= (SPARC_ELF_WORD_BYTES(htab) == 8 ?
  1734. (((bfd_vma)1 << 31) << 1) : 0x400000))
  1735. {
  1736. bfd_set_error (bfd_error_bad_value);
  1737. return false;
  1738. }
  1739. if (SPARC_ELF_WORD_BYTES(htab) == 8
  1740. && s->size >= PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
  1741. {
  1742. bfd_vma off = s->size - PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE;
  1743. off = (off % (160 * PLT64_ENTRY_SIZE)) / PLT64_ENTRY_SIZE;
  1744. h->plt.offset = (s->size - (off * 8));
  1745. }
  1746. else
  1747. h->plt.offset = s->size;
  1748. /* If this symbol is not defined in a regular file, and we are
  1749. not generating a shared library, then set the symbol to this
  1750. location in the .plt. This is required to make function
  1751. pointers compare as equal between the normal executable and
  1752. the shared library. */
  1753. if (! bfd_link_pic (info)
  1754. && !h->def_regular)
  1755. {
  1756. h->root.u.def.section = s;
  1757. h->root.u.def.value = h->plt.offset;
  1758. }
  1759. /* Make room for this entry. */
  1760. s->size += htab->plt_entry_size;
  1761. /* There should be no PLT relocations against resolved undefined
  1762. weak symbols in the executable. */
  1763. if (!resolved_to_zero)
  1764. {
  1765. /* We also need to make an entry in the .rela.plt section. */
  1766. if (s == htab->elf.splt)
  1767. htab->elf.srelplt->size += SPARC_ELF_RELA_BYTES (htab);
  1768. else
  1769. htab->elf.irelplt->size += SPARC_ELF_RELA_BYTES (htab);
  1770. }
  1771. if (htab->elf.target_os == is_vxworks)
  1772. {
  1773. /* Allocate space for the .got.plt entry. */
  1774. htab->elf.sgotplt->size += 4;
  1775. /* ...and for the .rela.plt.unloaded relocations. */
  1776. if (!bfd_link_pic (info))
  1777. htab->srelplt2->size += sizeof (Elf32_External_Rela) * 3;
  1778. }
  1779. }
  1780. else
  1781. {
  1782. h->plt.offset = (bfd_vma) -1;
  1783. h->needs_plt = 0;
  1784. }
  1785. }
  1786. else
  1787. {
  1788. h->plt.offset = (bfd_vma) -1;
  1789. h->needs_plt = 0;
  1790. }
  1791. /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
  1792. make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry. */
  1793. if (h->got.refcount > 0
  1794. && bfd_link_executable (info)
  1795. && h->dynindx == -1
  1796. && _bfd_sparc_elf_hash_entry(h)->tls_type == GOT_TLS_IE)
  1797. h->got.offset = (bfd_vma) -1;
  1798. else if (h->got.refcount > 0)
  1799. {
  1800. asection *s;
  1801. bool dyn;
  1802. int tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
  1803. /* Undefined weak syms won't yet be marked as dynamic. */
  1804. if (h->root.type == bfd_link_hash_undefweak
  1805. && !resolved_to_zero
  1806. && h->dynindx == -1
  1807. && !h->forced_local)
  1808. {
  1809. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1810. return false;
  1811. }
  1812. s = htab->elf.sgot;
  1813. h->got.offset = s->size;
  1814. s->size += SPARC_ELF_WORD_BYTES (htab);
  1815. /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots. */
  1816. if (tls_type == GOT_TLS_GD)
  1817. s->size += SPARC_ELF_WORD_BYTES (htab);
  1818. dyn = htab->elf.dynamic_sections_created;
  1819. /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
  1820. R_SPARC_TLS_GD_{HI22,LO10} needs one if local and two if global. */
  1821. if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
  1822. || tls_type == GOT_TLS_IE
  1823. || h->type == STT_GNU_IFUNC)
  1824. htab->elf.srelgot->size += SPARC_ELF_RELA_BYTES (htab);
  1825. else if (tls_type == GOT_TLS_GD)
  1826. htab->elf.srelgot->size += 2 * SPARC_ELF_RELA_BYTES (htab);
  1827. else if ((WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
  1828. /* Even if the symbol isn't dynamic, we may generate a
  1829. reloc for the dynamic linker in PIC mode. */
  1830. || (h->dynindx == -1
  1831. && !h->forced_local
  1832. && h->root.type != bfd_link_hash_undefweak
  1833. && bfd_link_pic (info)))
  1834. /* No dynamic relocations are needed against resolved
  1835. undefined weak symbols in an executable. */
  1836. && !(h->root.type == bfd_link_hash_undefweak
  1837. && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1838. || resolved_to_zero)))
  1839. htab->elf.srelgot->size += SPARC_ELF_RELA_BYTES (htab);
  1840. }
  1841. else
  1842. h->got.offset = (bfd_vma) -1;
  1843. if (h->dyn_relocs == NULL)
  1844. return true;
  1845. /* In the shared -Bsymbolic case, discard space allocated for
  1846. dynamic pc-relative relocs against symbols which turn out to be
  1847. defined in regular objects. For the normal shared case, discard
  1848. space for pc-relative relocs that have become local due to symbol
  1849. visibility changes. */
  1850. if (bfd_link_pic (info))
  1851. {
  1852. if (SYMBOL_CALLS_LOCAL (info, h))
  1853. {
  1854. struct elf_dyn_relocs **pp;
  1855. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  1856. {
  1857. p->count -= p->pc_count;
  1858. p->pc_count = 0;
  1859. if (p->count == 0)
  1860. *pp = p->next;
  1861. else
  1862. pp = &p->next;
  1863. }
  1864. }
  1865. if (htab->elf.target_os == is_vxworks)
  1866. {
  1867. struct elf_dyn_relocs **pp;
  1868. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  1869. {
  1870. if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
  1871. *pp = p->next;
  1872. else
  1873. pp = &p->next;
  1874. }
  1875. }
  1876. /* Also discard relocs on undefined weak syms with non-default
  1877. visibility or in PIE. */
  1878. if (h->dyn_relocs != NULL
  1879. && h->root.type == bfd_link_hash_undefweak)
  1880. {
  1881. /* An undefined weak symbol is never
  1882. bound locally in a shared library. */
  1883. if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1884. || resolved_to_zero)
  1885. {
  1886. if (h->non_got_ref)
  1887. {
  1888. /* Keep dynamic non-GOT/non-PLT relocation so that we
  1889. can branch to 0 without PLT. */
  1890. struct elf_dyn_relocs **pp;
  1891. for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
  1892. if (p->pc_count == 0)
  1893. *pp = p->next;
  1894. else
  1895. {
  1896. /* Remove other relocations. */
  1897. p->count = p->pc_count;
  1898. pp = &p->next;
  1899. }
  1900. if (h->dyn_relocs != NULL)
  1901. {
  1902. /* Make sure undefined weak symbols are output
  1903. as dynamic symbols in PIEs for dynamic non-GOT
  1904. non-PLT reloations. */
  1905. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1906. return false;
  1907. }
  1908. }
  1909. else
  1910. h->dyn_relocs = NULL;
  1911. }
  1912. /* Make sure undefined weak symbols are output as a dynamic
  1913. symbol in PIEs. */
  1914. else if (h->dynindx == -1
  1915. && !h->forced_local)
  1916. {
  1917. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1918. return false;
  1919. }
  1920. }
  1921. }
  1922. else
  1923. {
  1924. /* For the non-shared case, discard space for relocs against
  1925. symbols which turn out to need copy relocs or are not
  1926. dynamic. */
  1927. if ((!h->non_got_ref
  1928. || (h->root.type == bfd_link_hash_undefweak
  1929. && !resolved_to_zero))
  1930. && ((h->def_dynamic
  1931. && !h->def_regular)
  1932. || (htab->elf.dynamic_sections_created
  1933. && (h->root.type == bfd_link_hash_undefweak
  1934. || h->root.type == bfd_link_hash_undefined))))
  1935. {
  1936. /* Undefined weak syms won't yet be marked as dynamic. */
  1937. if (h->root.type == bfd_link_hash_undefweak
  1938. && !resolved_to_zero
  1939. && h->dynindx == -1
  1940. && !h->forced_local)
  1941. {
  1942. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1943. return false;
  1944. }
  1945. /* If that succeeded, we know we'll be keeping all the
  1946. relocs. */
  1947. if (h->dynindx != -1)
  1948. goto keep;
  1949. }
  1950. h->dyn_relocs = NULL;
  1951. keep: ;
  1952. }
  1953. /* Finally, allocate space. */
  1954. for (p = h->dyn_relocs; p != NULL; p = p->next)
  1955. {
  1956. asection *sreloc = elf_section_data (p->sec)->sreloc;
  1957. sreloc->size += p->count * SPARC_ELF_RELA_BYTES (htab);
  1958. }
  1959. return true;
  1960. }
  1961. /* Allocate space in .plt, .got and associated reloc sections for
  1962. local dynamic relocs. */
  1963. static int
  1964. allocate_local_dynrelocs (void **slot, void *inf)
  1965. {
  1966. struct elf_link_hash_entry *h
  1967. = (struct elf_link_hash_entry *) *slot;
  1968. if (h->type != STT_GNU_IFUNC
  1969. || !h->def_regular
  1970. || !h->ref_regular
  1971. || !h->forced_local
  1972. || h->root.type != bfd_link_hash_defined)
  1973. abort ();
  1974. return allocate_dynrelocs (h, inf);
  1975. }
  1976. /* Return true if the dynamic symbol for a given section should be
  1977. omitted when creating a shared library. */
  1978. bool
  1979. _bfd_sparc_elf_omit_section_dynsym (bfd *output_bfd,
  1980. struct bfd_link_info *info,
  1981. asection *p)
  1982. {
  1983. /* We keep the .got section symbol so that explicit relocations
  1984. against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
  1985. can be turned into relocations against the .got symbol. */
  1986. if (strcmp (p->name, ".got") == 0)
  1987. return false;
  1988. return _bfd_elf_omit_section_dynsym_default (output_bfd, info, p);
  1989. }
  1990. /* Set the sizes of the dynamic sections. */
  1991. bool
  1992. _bfd_sparc_elf_size_dynamic_sections (bfd *output_bfd,
  1993. struct bfd_link_info *info)
  1994. {
  1995. struct _bfd_sparc_elf_link_hash_table *htab;
  1996. bfd *dynobj;
  1997. asection *s;
  1998. bfd *ibfd;
  1999. htab = _bfd_sparc_elf_hash_table (info);
  2000. BFD_ASSERT (htab != NULL);
  2001. dynobj = htab->elf.dynobj;
  2002. BFD_ASSERT (dynobj != NULL);
  2003. if (elf_hash_table (info)->dynamic_sections_created)
  2004. {
  2005. /* Set the contents of the .interp section to the interpreter. */
  2006. if (bfd_link_executable (info) && !info->nointerp)
  2007. {
  2008. s = bfd_get_linker_section (dynobj, ".interp");
  2009. BFD_ASSERT (s != NULL);
  2010. s->size = htab->dynamic_interpreter_size;
  2011. s->contents = (unsigned char *) htab->dynamic_interpreter;
  2012. htab->interp = s;
  2013. }
  2014. }
  2015. /* Set up .got offsets for local syms, and space for local dynamic
  2016. relocs. */
  2017. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  2018. {
  2019. bfd_signed_vma *local_got;
  2020. bfd_signed_vma *end_local_got;
  2021. char *local_tls_type;
  2022. bfd_size_type locsymcount;
  2023. Elf_Internal_Shdr *symtab_hdr;
  2024. asection *srel;
  2025. if (! is_sparc_elf (ibfd))
  2026. continue;
  2027. for (s = ibfd->sections; s != NULL; s = s->next)
  2028. {
  2029. struct elf_dyn_relocs *p;
  2030. for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
  2031. {
  2032. if (!bfd_is_abs_section (p->sec)
  2033. && bfd_is_abs_section (p->sec->output_section))
  2034. {
  2035. /* Input section has been discarded, either because
  2036. it is a copy of a linkonce section or due to
  2037. linker script /DISCARD/, so we'll be discarding
  2038. the relocs too. */
  2039. }
  2040. else if (htab->elf.target_os == is_vxworks
  2041. && strcmp (p->sec->output_section->name,
  2042. ".tls_vars") == 0)
  2043. {
  2044. /* Relocations in vxworks .tls_vars sections are
  2045. handled specially by the loader. */
  2046. }
  2047. else if (p->count != 0)
  2048. {
  2049. srel = elf_section_data (p->sec)->sreloc;
  2050. if (!htab->elf.dynamic_sections_created)
  2051. srel = htab->elf.irelplt;
  2052. srel->size += p->count * SPARC_ELF_RELA_BYTES (htab);
  2053. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  2054. {
  2055. info->flags |= DF_TEXTREL;
  2056. info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
  2057. p->sec->owner, p->sec);
  2058. }
  2059. }
  2060. }
  2061. }
  2062. local_got = elf_local_got_refcounts (ibfd);
  2063. if (!local_got)
  2064. continue;
  2065. symtab_hdr = &elf_symtab_hdr (ibfd);
  2066. locsymcount = symtab_hdr->sh_info;
  2067. end_local_got = local_got + locsymcount;
  2068. local_tls_type = _bfd_sparc_elf_local_got_tls_type (ibfd);
  2069. s = htab->elf.sgot;
  2070. srel = htab->elf.srelgot;
  2071. for (; local_got < end_local_got; ++local_got, ++local_tls_type)
  2072. {
  2073. if (*local_got > 0)
  2074. {
  2075. *local_got = s->size;
  2076. s->size += SPARC_ELF_WORD_BYTES (htab);
  2077. if (*local_tls_type == GOT_TLS_GD)
  2078. s->size += SPARC_ELF_WORD_BYTES (htab);
  2079. if (bfd_link_pic (info)
  2080. || *local_tls_type == GOT_TLS_GD
  2081. || *local_tls_type == GOT_TLS_IE)
  2082. srel->size += SPARC_ELF_RELA_BYTES (htab);
  2083. }
  2084. else
  2085. *local_got = (bfd_vma) -1;
  2086. }
  2087. }
  2088. if (htab->tls_ldm_got.refcount > 0)
  2089. {
  2090. /* Allocate 2 got entries and 1 dynamic reloc for
  2091. R_SPARC_TLS_LDM_{HI22,LO10} relocs. */
  2092. htab->tls_ldm_got.offset = htab->elf.sgot->size;
  2093. htab->elf.sgot->size += (2 * SPARC_ELF_WORD_BYTES (htab));
  2094. htab->elf.srelgot->size += SPARC_ELF_RELA_BYTES (htab);
  2095. }
  2096. else
  2097. htab->tls_ldm_got.offset = -1;
  2098. /* Allocate global sym .plt and .got entries, and space for global
  2099. sym dynamic relocs. */
  2100. elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
  2101. /* Allocate .plt and .got entries, and space for local symbols. */
  2102. htab_traverse (htab->loc_hash_table, allocate_local_dynrelocs, info);
  2103. if (! ABI_64_P (output_bfd)
  2104. && htab->elf.target_os != is_vxworks
  2105. && elf_hash_table (info)->dynamic_sections_created)
  2106. {
  2107. /* Make space for the trailing nop in .plt. */
  2108. if (htab->elf.splt->size > 0)
  2109. htab->elf.splt->size += 1 * SPARC_INSN_BYTES;
  2110. /* If the .got section is more than 0x1000 bytes, we add
  2111. 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
  2112. bit relocations have a greater chance of working.
  2113. FIXME: Make this optimization work for 64-bit too. */
  2114. if (htab->elf.sgot->size >= 0x1000
  2115. && elf_hash_table (info)->hgot->root.u.def.value == 0)
  2116. elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
  2117. }
  2118. /* The check_relocs and adjust_dynamic_symbol entry points have
  2119. determined the sizes of the various dynamic sections. Allocate
  2120. memory for them. */
  2121. for (s = dynobj->sections; s != NULL; s = s->next)
  2122. {
  2123. if ((s->flags & SEC_LINKER_CREATED) == 0)
  2124. continue;
  2125. if (s == htab->elf.splt
  2126. || s == htab->elf.sgot
  2127. || s == htab->elf.sdynbss
  2128. || s == htab->elf.sdynrelro
  2129. || s == htab->elf.iplt
  2130. || s == htab->elf.sgotplt)
  2131. {
  2132. /* Strip this section if we don't need it; see the
  2133. comment below. */
  2134. }
  2135. else if (startswith (s->name, ".rela"))
  2136. {
  2137. if (s->size != 0)
  2138. {
  2139. /* We use the reloc_count field as a counter if we need
  2140. to copy relocs into the output file. */
  2141. s->reloc_count = 0;
  2142. }
  2143. }
  2144. else
  2145. {
  2146. /* It's not one of our sections. */
  2147. continue;
  2148. }
  2149. if (s->size == 0)
  2150. {
  2151. /* If we don't need this section, strip it from the
  2152. output file. This is mostly to handle .rela.bss and
  2153. .rela.plt. We must create both sections in
  2154. create_dynamic_sections, because they must be created
  2155. before the linker maps input sections to output
  2156. sections. The linker does that before
  2157. adjust_dynamic_symbol is called, and it is that
  2158. function which decides whether anything needs to go
  2159. into these sections. */
  2160. s->flags |= SEC_EXCLUDE;
  2161. continue;
  2162. }
  2163. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  2164. continue;
  2165. /* Allocate memory for the section contents. Zero the memory
  2166. for the benefit of .rela.plt, which has 4 unused entries
  2167. at the beginning, and we don't want garbage. */
  2168. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  2169. if (s->contents == NULL)
  2170. return false;
  2171. }
  2172. if (elf_hash_table (info)->dynamic_sections_created)
  2173. {
  2174. /* Add some entries to the .dynamic section. We fill in the
  2175. values later, in _bfd_sparc_elf_finish_dynamic_sections, but we
  2176. must add the entries now so that we get the correct size for
  2177. the .dynamic section. The DT_DEBUG entry is filled in by the
  2178. dynamic linker and used by the debugger. */
  2179. #define add_dynamic_entry(TAG, VAL) \
  2180. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  2181. if (!_bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
  2182. true))
  2183. return false;
  2184. if (ABI_64_P (output_bfd))
  2185. {
  2186. int reg;
  2187. struct _bfd_sparc_elf_app_reg * app_regs;
  2188. struct elf_strtab_hash *dynstr;
  2189. struct elf_link_hash_table *eht = elf_hash_table (info);
  2190. /* Add dynamic STT_REGISTER symbols and corresponding DT_SPARC_REGISTER
  2191. entries if needed. */
  2192. app_regs = _bfd_sparc_elf_hash_table (info)->app_regs;
  2193. dynstr = eht->dynstr;
  2194. for (reg = 0; reg < 4; reg++)
  2195. if (app_regs [reg].name != NULL)
  2196. {
  2197. struct elf_link_local_dynamic_entry *entry, *e;
  2198. if (!add_dynamic_entry (DT_SPARC_REGISTER, 0))
  2199. return false;
  2200. entry = (struct elf_link_local_dynamic_entry *)
  2201. bfd_hash_allocate (&info->hash->table, sizeof (*entry));
  2202. if (entry == NULL)
  2203. return false;
  2204. /* We cheat here a little bit: the symbol will not be local, so we
  2205. put it at the end of the dynlocal linked list. We will fix it
  2206. later on, as we have to fix other fields anyway. */
  2207. entry->isym.st_value = reg < 2 ? reg + 2 : reg + 4;
  2208. entry->isym.st_size = 0;
  2209. if (*app_regs [reg].name != '\0')
  2210. entry->isym.st_name
  2211. = _bfd_elf_strtab_add (dynstr, app_regs[reg].name, false);
  2212. else
  2213. entry->isym.st_name = 0;
  2214. entry->isym.st_other = 0;
  2215. entry->isym.st_info = ELF_ST_INFO (app_regs [reg].bind,
  2216. STT_REGISTER);
  2217. entry->isym.st_shndx = app_regs [reg].shndx;
  2218. entry->isym.st_target_internal = 0;
  2219. entry->next = NULL;
  2220. entry->input_bfd = output_bfd;
  2221. entry->input_indx = -1;
  2222. if (eht->dynlocal == NULL)
  2223. eht->dynlocal = entry;
  2224. else
  2225. {
  2226. for (e = eht->dynlocal; e->next; e = e->next)
  2227. ;
  2228. e->next = entry;
  2229. }
  2230. eht->dynsymcount++;
  2231. }
  2232. }
  2233. }
  2234. #undef add_dynamic_entry
  2235. return true;
  2236. }
  2237. bool
  2238. _bfd_sparc_elf_new_section_hook (bfd *abfd, asection *sec)
  2239. {
  2240. if (!sec->used_by_bfd)
  2241. {
  2242. struct _bfd_sparc_elf_section_data *sdata;
  2243. size_t amt = sizeof (*sdata);
  2244. sdata = bfd_zalloc (abfd, amt);
  2245. if (sdata == NULL)
  2246. return false;
  2247. sec->used_by_bfd = sdata;
  2248. }
  2249. return _bfd_elf_new_section_hook (abfd, sec);
  2250. }
  2251. bool
  2252. _bfd_sparc_elf_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
  2253. struct bfd_section *section,
  2254. struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
  2255. bool *again)
  2256. {
  2257. if (bfd_link_relocatable (link_info))
  2258. (*link_info->callbacks->einfo)
  2259. (_("%P%F: --relax and -r may not be used together\n"));
  2260. *again = false;
  2261. sec_do_relax (section) = 1;
  2262. return true;
  2263. }
  2264. /* Return the base VMA address which should be subtracted from real addresses
  2265. when resolving @dtpoff relocation.
  2266. This is PT_TLS segment p_vaddr. */
  2267. static bfd_vma
  2268. dtpoff_base (struct bfd_link_info *info)
  2269. {
  2270. /* If tls_sec is NULL, we should have signalled an error already. */
  2271. if (elf_hash_table (info)->tls_sec == NULL)
  2272. return 0;
  2273. return elf_hash_table (info)->tls_sec->vma;
  2274. }
  2275. /* Return the relocation value for @tpoff relocation
  2276. if STT_TLS virtual address is ADDRESS. */
  2277. static bfd_vma
  2278. tpoff (struct bfd_link_info *info, bfd_vma address)
  2279. {
  2280. struct elf_link_hash_table *htab = elf_hash_table (info);
  2281. const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
  2282. bfd_vma static_tls_size;
  2283. /* If tls_sec is NULL, we should have signalled an error already. */
  2284. if (htab->tls_sec == NULL)
  2285. return 0;
  2286. /* Consider special static TLS alignment requirements. */
  2287. static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
  2288. return address - static_tls_size - htab->tls_sec->vma;
  2289. }
  2290. /* Return the relocation value for a %gdop relocation. */
  2291. static bfd_vma
  2292. gdopoff (struct bfd_link_info *info, bfd_vma address)
  2293. {
  2294. struct elf_link_hash_table *htab = elf_hash_table (info);
  2295. bfd_vma got_base;
  2296. got_base = (htab->hgot->root.u.def.value
  2297. + htab->hgot->root.u.def.section->output_offset
  2298. + htab->hgot->root.u.def.section->output_section->vma);
  2299. return address - got_base;
  2300. }
  2301. /* Return whether H is local and its ADDRESS is within 4G of
  2302. _GLOBAL_OFFSET_TABLE_ and thus the offset may be calculated by a
  2303. sethi, xor sequence. */
  2304. static bool
  2305. gdop_relative_offset_ok (struct bfd_link_info *info,
  2306. struct elf_link_hash_entry *h,
  2307. bfd_vma address ATTRIBUTE_UNUSED)
  2308. {
  2309. if (!SYMBOL_REFERENCES_LOCAL (info, h))
  2310. return false;
  2311. /* If H is undefined, ADDRESS will be zero. We can't allow a
  2312. relative offset to "zero" when producing PIEs or shared libs.
  2313. Note that to get here with an undefined symbol it must also be
  2314. hidden or internal visibility. */
  2315. if (bfd_link_pic (info)
  2316. && h != NULL
  2317. && (h->root.type == bfd_link_hash_undefweak
  2318. || h->root.type == bfd_link_hash_undefined))
  2319. return false;
  2320. #ifdef BFD64
  2321. return gdopoff (info, address) + ((bfd_vma) 1 << 32) < (bfd_vma) 2 << 32;
  2322. #else
  2323. return true;
  2324. #endif
  2325. }
  2326. /* Relocate a SPARC ELF section. */
  2327. int
  2328. _bfd_sparc_elf_relocate_section (bfd *output_bfd,
  2329. struct bfd_link_info *info,
  2330. bfd *input_bfd,
  2331. asection *input_section,
  2332. bfd_byte *contents,
  2333. Elf_Internal_Rela *relocs,
  2334. Elf_Internal_Sym *local_syms,
  2335. asection **local_sections)
  2336. {
  2337. struct _bfd_sparc_elf_link_hash_table *htab;
  2338. Elf_Internal_Shdr *symtab_hdr;
  2339. struct elf_link_hash_entry **sym_hashes;
  2340. bfd_vma *local_got_offsets;
  2341. bfd_vma got_base;
  2342. asection *sreloc;
  2343. Elf_Internal_Rela *rel;
  2344. Elf_Internal_Rela *relend;
  2345. int num_relocs;
  2346. bool is_vxworks_tls;
  2347. htab = _bfd_sparc_elf_hash_table (info);
  2348. BFD_ASSERT (htab != NULL);
  2349. symtab_hdr = &elf_symtab_hdr (input_bfd);
  2350. sym_hashes = elf_sym_hashes (input_bfd);
  2351. local_got_offsets = elf_local_got_offsets (input_bfd);
  2352. if (elf_hash_table (info)->hgot == NULL)
  2353. got_base = 0;
  2354. else
  2355. got_base = elf_hash_table (info)->hgot->root.u.def.value;
  2356. sreloc = elf_section_data (input_section)->sreloc;
  2357. /* We have to handle relocations in vxworks .tls_vars sections
  2358. specially, because the dynamic loader is 'weird'. */
  2359. is_vxworks_tls = (htab->elf.target_os == is_vxworks
  2360. && bfd_link_pic (info)
  2361. && !strcmp (input_section->output_section->name,
  2362. ".tls_vars"));
  2363. rel = relocs;
  2364. if (ABI_64_P (output_bfd))
  2365. num_relocs = NUM_SHDR_ENTRIES (_bfd_elf_single_rel_hdr (input_section));
  2366. else
  2367. num_relocs = input_section->reloc_count;
  2368. relend = relocs + num_relocs;
  2369. for (; rel < relend; rel++)
  2370. {
  2371. int r_type, tls_type;
  2372. reloc_howto_type *howto;
  2373. unsigned long r_symndx;
  2374. struct elf_link_hash_entry *h;
  2375. struct _bfd_sparc_elf_link_hash_entry *eh;
  2376. Elf_Internal_Sym *sym;
  2377. asection *sec;
  2378. bfd_vma relocation, off;
  2379. bfd_reloc_status_type r;
  2380. bool is_plt = false;
  2381. bool unresolved_reloc;
  2382. bool resolved_to_zero;
  2383. bool relative_reloc;
  2384. r_type = SPARC_ELF_R_TYPE (rel->r_info);
  2385. if (r_type == R_SPARC_GNU_VTINHERIT
  2386. || r_type == R_SPARC_GNU_VTENTRY)
  2387. continue;
  2388. if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
  2389. {
  2390. bfd_set_error (bfd_error_bad_value);
  2391. return false;
  2392. }
  2393. howto = _bfd_sparc_elf_howto_table + r_type;
  2394. r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
  2395. h = NULL;
  2396. sym = NULL;
  2397. sec = NULL;
  2398. unresolved_reloc = false;
  2399. if (r_symndx < symtab_hdr->sh_info)
  2400. {
  2401. sym = local_syms + r_symndx;
  2402. sec = local_sections[r_symndx];
  2403. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  2404. if (!bfd_link_relocatable (info)
  2405. && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  2406. {
  2407. /* Relocate against local STT_GNU_IFUNC symbol. */
  2408. h = elf_sparc_get_local_sym_hash (htab, input_bfd,
  2409. rel, false);
  2410. if (h == NULL)
  2411. abort ();
  2412. /* Set STT_GNU_IFUNC symbol value. */
  2413. h->root.u.def.value = sym->st_value;
  2414. h->root.u.def.section = sec;
  2415. }
  2416. }
  2417. else
  2418. {
  2419. bool warned, ignored;
  2420. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  2421. r_symndx, symtab_hdr, sym_hashes,
  2422. h, sec, relocation,
  2423. unresolved_reloc, warned, ignored);
  2424. if (warned)
  2425. {
  2426. /* To avoid generating warning messages about truncated
  2427. relocations, set the relocation's address to be the same as
  2428. the start of this section. */
  2429. if (input_section->output_section != NULL)
  2430. relocation = input_section->output_section->vma;
  2431. else
  2432. relocation = 0;
  2433. }
  2434. }
  2435. if (sec != NULL && discarded_section (sec))
  2436. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  2437. rel, 1, relend, howto, 0, contents);
  2438. if (bfd_link_relocatable (info))
  2439. continue;
  2440. if (h != NULL
  2441. && h->type == STT_GNU_IFUNC
  2442. && h->def_regular)
  2443. {
  2444. asection *plt_sec;
  2445. const char *name;
  2446. if ((input_section->flags & SEC_ALLOC) == 0
  2447. || h->plt.offset == (bfd_vma) -1)
  2448. {
  2449. /* If this is a SHT_NOTE section without SHF_ALLOC, treat
  2450. STT_GNU_IFUNC symbol as STT_FUNC. */
  2451. if (elf_section_type (input_section) == SHT_NOTE)
  2452. goto skip_ifunc;
  2453. /* Dynamic relocs are not propagated for SEC_DEBUGGING
  2454. sections because such sections are not SEC_ALLOC and
  2455. thus ld.so will not process them. */
  2456. if ((input_section->flags & SEC_ALLOC) == 0
  2457. && (input_section->flags & SEC_DEBUGGING) != 0)
  2458. continue;
  2459. _bfd_error_handler
  2460. /* xgettext:c-format */
  2461. (_("%pB(%pA+%#" PRIx64 "): "
  2462. "unresolvable %s relocation against symbol `%s'"),
  2463. input_bfd,
  2464. input_section,
  2465. (uint64_t) rel->r_offset,
  2466. howto->name,
  2467. h->root.root.string);
  2468. bfd_set_error (bfd_error_bad_value);
  2469. return false;
  2470. }
  2471. plt_sec = htab->elf.splt;
  2472. if (! plt_sec)
  2473. plt_sec =htab->elf.iplt;
  2474. switch (r_type)
  2475. {
  2476. case R_SPARC_GOTDATA_OP:
  2477. continue;
  2478. case R_SPARC_GOTDATA_OP_HIX22:
  2479. case R_SPARC_GOTDATA_OP_LOX10:
  2480. r_type = (r_type == R_SPARC_GOTDATA_OP_HIX22
  2481. ? R_SPARC_GOT22
  2482. : R_SPARC_GOT10);
  2483. howto = _bfd_sparc_elf_howto_table + r_type;
  2484. /* Fall through. */
  2485. case R_SPARC_GOT10:
  2486. case R_SPARC_GOT13:
  2487. case R_SPARC_GOT22:
  2488. if (htab->elf.sgot == NULL)
  2489. abort ();
  2490. off = h->got.offset;
  2491. if (off == (bfd_vma) -1)
  2492. abort();
  2493. relocation = htab->elf.sgot->output_offset + off - got_base;
  2494. goto do_relocation;
  2495. case R_SPARC_WPLT30:
  2496. case R_SPARC_WDISP30:
  2497. relocation = (plt_sec->output_section->vma
  2498. + plt_sec->output_offset + h->plt.offset);
  2499. goto do_relocation;
  2500. case R_SPARC_32:
  2501. case R_SPARC_64:
  2502. if (bfd_link_pic (info) && h->non_got_ref)
  2503. {
  2504. Elf_Internal_Rela outrel;
  2505. bfd_vma offset;
  2506. offset = _bfd_elf_section_offset (output_bfd, info,
  2507. input_section,
  2508. rel->r_offset);
  2509. if (offset == (bfd_vma) -1
  2510. || offset == (bfd_vma) -2)
  2511. abort();
  2512. outrel.r_offset = (input_section->output_section->vma
  2513. + input_section->output_offset
  2514. + offset);
  2515. if (h->dynindx == -1
  2516. || h->forced_local
  2517. || bfd_link_executable (info))
  2518. {
  2519. outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
  2520. 0, R_SPARC_IRELATIVE);
  2521. outrel.r_addend = relocation + rel->r_addend;
  2522. }
  2523. else
  2524. {
  2525. if (h->dynindx == -1)
  2526. abort();
  2527. outrel.r_info = SPARC_ELF_R_INFO (htab, rel, h->dynindx, r_type);
  2528. outrel.r_addend = rel->r_addend;
  2529. }
  2530. sparc_elf_append_rela (output_bfd, sreloc, &outrel);
  2531. continue;
  2532. }
  2533. relocation = (plt_sec->output_section->vma
  2534. + plt_sec->output_offset + h->plt.offset);
  2535. goto do_relocation;
  2536. case R_SPARC_HI22:
  2537. case R_SPARC_LO10:
  2538. /* We should only see such relocs in static links. */
  2539. if (bfd_link_pic (info))
  2540. abort();
  2541. relocation = (plt_sec->output_section->vma
  2542. + plt_sec->output_offset + h->plt.offset);
  2543. goto do_relocation;
  2544. default:
  2545. if (h->root.root.string)
  2546. name = h->root.root.string;
  2547. else
  2548. name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
  2549. NULL);
  2550. _bfd_error_handler
  2551. /* xgettext:c-format */
  2552. (_("%pB: relocation %s against STT_GNU_IFUNC "
  2553. "symbol `%s' isn't handled by %s"), input_bfd,
  2554. _bfd_sparc_elf_howto_table[r_type].name,
  2555. name, __FUNCTION__);
  2556. bfd_set_error (bfd_error_bad_value);
  2557. return false;
  2558. }
  2559. }
  2560. skip_ifunc:
  2561. eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
  2562. resolved_to_zero = eh && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
  2563. switch (r_type)
  2564. {
  2565. case R_SPARC_GOTDATA_OP_HIX22:
  2566. case R_SPARC_GOTDATA_OP_LOX10:
  2567. if (gdop_relative_offset_ok (info, h, relocation))
  2568. {
  2569. r_type = (r_type == R_SPARC_GOTDATA_OP_HIX22
  2570. ? R_SPARC_GOTDATA_HIX22
  2571. : R_SPARC_GOTDATA_LOX10);
  2572. howto = _bfd_sparc_elf_howto_table + r_type;
  2573. }
  2574. break;
  2575. case R_SPARC_GOTDATA_OP:
  2576. if (gdop_relative_offset_ok (info, h, relocation))
  2577. {
  2578. bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  2579. /* {ld,ldx} [%rs1 + %rs2], %rd --> add %rs1, %rs2, %rd */
  2580. relocation = 0x80000000 | (insn & 0x3e07c01f);
  2581. bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
  2582. /* If the symbol is global but not dynamic, an .rela.* slot has
  2583. been allocated for it in the GOT so output R_SPARC_NONE here,
  2584. if it isn't also subject to another, old-style GOT relocation.
  2585. See also the handling of these GOT relocations just below. */
  2586. if (h != NULL
  2587. && h->dynindx == -1
  2588. && !h->forced_local
  2589. && h->root.type != bfd_link_hash_undefweak
  2590. && !eh->has_old_style_got_reloc
  2591. && (h->got.offset & 1) == 0
  2592. && bfd_link_pic (info))
  2593. {
  2594. asection *s = htab->elf.srelgot;
  2595. Elf_Internal_Rela outrel;
  2596. BFD_ASSERT (s != NULL);
  2597. memset (&outrel, 0, sizeof outrel);
  2598. sparc_elf_append_rela (output_bfd, s, &outrel);
  2599. h->got.offset |= 1;
  2600. }
  2601. }
  2602. continue;
  2603. }
  2604. switch (r_type)
  2605. {
  2606. case R_SPARC_GOTDATA_HIX22:
  2607. case R_SPARC_GOTDATA_LOX10:
  2608. relocation = gdopoff (info, relocation);
  2609. break;
  2610. case R_SPARC_GOTDATA_OP_HIX22:
  2611. case R_SPARC_GOTDATA_OP_LOX10:
  2612. case R_SPARC_GOT10:
  2613. case R_SPARC_GOT13:
  2614. case R_SPARC_GOT22:
  2615. /* Relocation is to the entry for this symbol in the global
  2616. offset table. */
  2617. if (htab->elf.sgot == NULL)
  2618. abort ();
  2619. relative_reloc = false;
  2620. if (h != NULL)
  2621. {
  2622. bool dyn;
  2623. off = h->got.offset;
  2624. BFD_ASSERT (off != (bfd_vma) -1);
  2625. dyn = elf_hash_table (info)->dynamic_sections_created;
  2626. if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  2627. bfd_link_pic (info),
  2628. h)
  2629. || (bfd_link_pic (info)
  2630. && SYMBOL_REFERENCES_LOCAL (info, h)))
  2631. {
  2632. /* This is actually a static link, or it is a
  2633. -Bsymbolic link and the symbol is defined
  2634. locally, or the symbol was forced to be local
  2635. because of a version file. We must initialize
  2636. this entry in the global offset table. Since the
  2637. offset must always be a multiple of 8 for 64-bit
  2638. and 4 for 32-bit, we use the least significant bit
  2639. to record whether we have initialized it already.
  2640. When doing a dynamic link, we create a .rela.got
  2641. relocation entry to initialize the value. This
  2642. is done in the finish_dynamic_symbol routine. */
  2643. if ((off & 1) != 0)
  2644. off &= ~1;
  2645. else
  2646. {
  2647. /* If this symbol isn't dynamic in PIC mode, treat it
  2648. like a local symbol in PIC mode below. */
  2649. if (h->dynindx == -1
  2650. && !h->forced_local
  2651. && h->root.type != bfd_link_hash_undefweak
  2652. && bfd_link_pic (info))
  2653. relative_reloc = true;
  2654. else
  2655. SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
  2656. htab->elf.sgot->contents + off);
  2657. h->got.offset |= 1;
  2658. }
  2659. }
  2660. else
  2661. unresolved_reloc = false;
  2662. }
  2663. else
  2664. {
  2665. BFD_ASSERT (local_got_offsets != NULL
  2666. && local_got_offsets[r_symndx] != (bfd_vma) -1);
  2667. off = local_got_offsets[r_symndx];
  2668. /* The offset must always be a multiple of 8 on 64-bit and
  2669. 4 on 32-bit. We use the least significant bit to record
  2670. whether we have already processed this entry. */
  2671. if ((off & 1) != 0)
  2672. off &= ~1;
  2673. else
  2674. {
  2675. /* For a local symbol in PIC mode, we need to generate a
  2676. R_SPARC_RELATIVE reloc for the dynamic linker. */
  2677. if (bfd_link_pic (info))
  2678. relative_reloc = true;
  2679. else
  2680. SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
  2681. htab->elf.sgot->contents + off);
  2682. local_got_offsets[r_symndx] |= 1;
  2683. }
  2684. }
  2685. if (relative_reloc)
  2686. {
  2687. asection *s = htab->elf.srelgot;
  2688. Elf_Internal_Rela outrel;
  2689. BFD_ASSERT (s != NULL);
  2690. outrel.r_offset = (htab->elf.sgot->output_section->vma
  2691. + htab->elf.sgot->output_offset
  2692. + off);
  2693. outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
  2694. 0, R_SPARC_RELATIVE);
  2695. outrel.r_addend = relocation;
  2696. sparc_elf_append_rela (output_bfd, s, &outrel);
  2697. /* Versions of glibc ld.so at least up to 2.26 wrongly
  2698. add the section contents to the value calculated for
  2699. a RELATIVE reloc. Zero the contents to work around
  2700. this bug. */
  2701. relocation = 0;
  2702. SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
  2703. htab->elf.sgot->contents + off);
  2704. }
  2705. relocation = htab->elf.sgot->output_offset + off - got_base;
  2706. break;
  2707. case R_SPARC_PLT32:
  2708. case R_SPARC_PLT64:
  2709. if (h == NULL || h->plt.offset == (bfd_vma) -1)
  2710. {
  2711. r_type = (r_type == R_SPARC_PLT32) ? R_SPARC_32 : R_SPARC_64;
  2712. goto r_sparc_plt32;
  2713. }
  2714. /* Fall through. */
  2715. case R_SPARC_WPLT30:
  2716. case R_SPARC_HIPLT22:
  2717. case R_SPARC_LOPLT10:
  2718. case R_SPARC_PCPLT32:
  2719. case R_SPARC_PCPLT22:
  2720. case R_SPARC_PCPLT10:
  2721. r_sparc_wplt30:
  2722. /* Relocation is to the entry for this symbol in the
  2723. procedure linkage table. */
  2724. if (! ABI_64_P (output_bfd))
  2725. {
  2726. /* The Solaris native assembler will generate a WPLT30 reloc
  2727. for a local symbol if you assemble a call from one
  2728. section to another when using -K pic. We treat it as
  2729. WDISP30. */
  2730. if (h == NULL)
  2731. break;
  2732. }
  2733. /* PR 7027: We need similar behaviour for 64-bit binaries. */
  2734. else if (r_type == R_SPARC_WPLT30 && h == NULL)
  2735. break;
  2736. else
  2737. {
  2738. BFD_ASSERT (h != NULL);
  2739. }
  2740. if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
  2741. {
  2742. /* We didn't make a PLT entry for this symbol. This
  2743. happens when statically linking PIC code, or when
  2744. using -Bsymbolic. */
  2745. break;
  2746. }
  2747. relocation = (htab->elf.splt->output_section->vma
  2748. + htab->elf.splt->output_offset
  2749. + h->plt.offset);
  2750. unresolved_reloc = false;
  2751. if (r_type == R_SPARC_PLT32 || r_type == R_SPARC_PLT64)
  2752. {
  2753. r_type = r_type == R_SPARC_PLT32 ? R_SPARC_32 : R_SPARC_64;
  2754. is_plt = true;
  2755. goto r_sparc_plt32;
  2756. }
  2757. break;
  2758. case R_SPARC_PC10:
  2759. case R_SPARC_PC22:
  2760. case R_SPARC_PC_HH22:
  2761. case R_SPARC_PC_HM10:
  2762. case R_SPARC_PC_LM22:
  2763. if (h != NULL
  2764. && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  2765. break;
  2766. /* Fall through. */
  2767. case R_SPARC_DISP8:
  2768. case R_SPARC_DISP16:
  2769. case R_SPARC_DISP32:
  2770. case R_SPARC_DISP64:
  2771. case R_SPARC_WDISP30:
  2772. case R_SPARC_WDISP22:
  2773. case R_SPARC_WDISP19:
  2774. case R_SPARC_WDISP16:
  2775. case R_SPARC_WDISP10:
  2776. case R_SPARC_8:
  2777. case R_SPARC_16:
  2778. case R_SPARC_32:
  2779. case R_SPARC_HI22:
  2780. case R_SPARC_22:
  2781. case R_SPARC_13:
  2782. case R_SPARC_LO10:
  2783. case R_SPARC_UA16:
  2784. case R_SPARC_UA32:
  2785. case R_SPARC_10:
  2786. case R_SPARC_11:
  2787. case R_SPARC_64:
  2788. case R_SPARC_OLO10:
  2789. case R_SPARC_HH22:
  2790. case R_SPARC_HM10:
  2791. case R_SPARC_LM22:
  2792. case R_SPARC_7:
  2793. case R_SPARC_5:
  2794. case R_SPARC_6:
  2795. case R_SPARC_HIX22:
  2796. case R_SPARC_LOX10:
  2797. case R_SPARC_H44:
  2798. case R_SPARC_M44:
  2799. case R_SPARC_L44:
  2800. case R_SPARC_H34:
  2801. case R_SPARC_UA64:
  2802. r_sparc_plt32:
  2803. if ((input_section->flags & SEC_ALLOC) == 0 || is_vxworks_tls)
  2804. break;
  2805. /* Copy dynamic function pointer relocations. Don't generate
  2806. dynamic relocations against resolved undefined weak symbols
  2807. in PIE. */
  2808. if ((bfd_link_pic (info)
  2809. && (h == NULL
  2810. || !(h->root.type == bfd_link_hash_undefweak
  2811. && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2812. || resolved_to_zero)))
  2813. && (! howto->pc_relative
  2814. || !SYMBOL_CALLS_LOCAL (info, h)))
  2815. || (!bfd_link_pic (info)
  2816. && h != NULL
  2817. && h->dynindx != -1
  2818. && !h->non_got_ref
  2819. && ((h->def_dynamic
  2820. && !h->def_regular)
  2821. || (h->root.type == bfd_link_hash_undefweak
  2822. && !resolved_to_zero)
  2823. || h->root.type == bfd_link_hash_undefined)))
  2824. {
  2825. Elf_Internal_Rela outrel;
  2826. bool skip, relocate = false;
  2827. /* When generating a shared object, these relocations
  2828. are copied into the output file to be resolved at run
  2829. time. */
  2830. BFD_ASSERT (sreloc != NULL);
  2831. skip = false;
  2832. outrel.r_offset =
  2833. _bfd_elf_section_offset (output_bfd, info, input_section,
  2834. rel->r_offset);
  2835. if (outrel.r_offset == (bfd_vma) -1)
  2836. skip = true;
  2837. else if (outrel.r_offset == (bfd_vma) -2)
  2838. skip = true, relocate = true;
  2839. outrel.r_offset += (input_section->output_section->vma
  2840. + input_section->output_offset);
  2841. /* Optimize unaligned reloc usage now that we know where
  2842. it finally resides. */
  2843. switch (r_type)
  2844. {
  2845. case R_SPARC_16:
  2846. if (outrel.r_offset & 1)
  2847. r_type = R_SPARC_UA16;
  2848. break;
  2849. case R_SPARC_UA16:
  2850. if (!(outrel.r_offset & 1))
  2851. r_type = R_SPARC_16;
  2852. break;
  2853. case R_SPARC_32:
  2854. if (outrel.r_offset & 3)
  2855. r_type = R_SPARC_UA32;
  2856. break;
  2857. case R_SPARC_UA32:
  2858. if (!(outrel.r_offset & 3))
  2859. r_type = R_SPARC_32;
  2860. break;
  2861. case R_SPARC_64:
  2862. if (outrel.r_offset & 7)
  2863. r_type = R_SPARC_UA64;
  2864. break;
  2865. case R_SPARC_UA64:
  2866. if (!(outrel.r_offset & 7))
  2867. r_type = R_SPARC_64;
  2868. break;
  2869. case R_SPARC_DISP8:
  2870. case R_SPARC_DISP16:
  2871. case R_SPARC_DISP32:
  2872. case R_SPARC_DISP64:
  2873. /* If the symbol is not dynamic, we should not keep
  2874. a dynamic relocation. But an .rela.* slot has been
  2875. allocated for it, output R_SPARC_NONE.
  2876. FIXME: Add code tracking needed dynamic relocs as
  2877. e.g. i386 has. */
  2878. if (h->dynindx == -1)
  2879. skip = true, relocate = true;
  2880. break;
  2881. }
  2882. if (skip)
  2883. memset (&outrel, 0, sizeof outrel);
  2884. /* h->dynindx may be -1 if the symbol was marked to
  2885. become local. */
  2886. else if (h != NULL
  2887. && h->dynindx != -1
  2888. && (_bfd_sparc_elf_howto_table[r_type].pc_relative
  2889. || !bfd_link_pic (info)
  2890. || !SYMBOLIC_BIND (info, h)
  2891. || !h->def_regular))
  2892. {
  2893. outrel.r_info = SPARC_ELF_R_INFO (htab, rel, h->dynindx, r_type);
  2894. outrel.r_addend = rel->r_addend;
  2895. }
  2896. else
  2897. {
  2898. if ( (!ABI_64_P (output_bfd) && r_type == R_SPARC_32)
  2899. || (ABI_64_P (output_bfd) && r_type == R_SPARC_64))
  2900. {
  2901. outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
  2902. 0, R_SPARC_RELATIVE);
  2903. outrel.r_addend = relocation + rel->r_addend;
  2904. }
  2905. else
  2906. {
  2907. long indx;
  2908. outrel.r_addend = relocation + rel->r_addend;
  2909. if (is_plt)
  2910. sec = htab->elf.splt;
  2911. if (bfd_is_abs_section (sec))
  2912. indx = 0;
  2913. else if (sec == NULL || sec->owner == NULL)
  2914. {
  2915. bfd_set_error (bfd_error_bad_value);
  2916. return false;
  2917. }
  2918. else
  2919. {
  2920. asection *osec;
  2921. /* We are turning this relocation into one
  2922. against a section symbol. It would be
  2923. proper to subtract the symbol's value,
  2924. osec->vma, from the emitted reloc addend,
  2925. but ld.so expects buggy relocs. */
  2926. osec = sec->output_section;
  2927. indx = elf_section_data (osec)->dynindx;
  2928. if (indx == 0)
  2929. {
  2930. osec = htab->elf.text_index_section;
  2931. indx = elf_section_data (osec)->dynindx;
  2932. }
  2933. /* FIXME: we really should be able to link non-pic
  2934. shared libraries. */
  2935. if (indx == 0)
  2936. {
  2937. BFD_FAIL ();
  2938. _bfd_error_handler
  2939. (_("%pB: probably compiled without -fPIC?"),
  2940. input_bfd);
  2941. bfd_set_error (bfd_error_bad_value);
  2942. return false;
  2943. }
  2944. }
  2945. outrel.r_info = SPARC_ELF_R_INFO (htab, rel, indx,
  2946. r_type);
  2947. }
  2948. }
  2949. sparc_elf_append_rela (output_bfd, sreloc, &outrel);
  2950. /* This reloc will be computed at runtime, so there's no
  2951. need to do anything now. */
  2952. if (! relocate)
  2953. continue;
  2954. }
  2955. break;
  2956. case R_SPARC_TLS_GD_HI22:
  2957. case R_SPARC_TLS_GD_LO10:
  2958. case R_SPARC_TLS_IE_HI22:
  2959. case R_SPARC_TLS_IE_LO10:
  2960. r_type = sparc_elf_tls_transition (info, input_bfd, r_type,
  2961. h == NULL || h->dynindx == -1);
  2962. if (r_type == R_SPARC_REV32)
  2963. break;
  2964. if (h != NULL)
  2965. tls_type = _bfd_sparc_elf_hash_entry (h)->tls_type;
  2966. else if (local_got_offsets)
  2967. tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
  2968. else
  2969. tls_type = GOT_UNKNOWN;
  2970. if (tls_type == GOT_TLS_IE)
  2971. switch (r_type)
  2972. {
  2973. case R_SPARC_TLS_GD_HI22:
  2974. r_type = R_SPARC_TLS_IE_HI22;
  2975. break;
  2976. case R_SPARC_TLS_GD_LO10:
  2977. r_type = R_SPARC_TLS_IE_LO10;
  2978. break;
  2979. }
  2980. if (r_type == R_SPARC_TLS_LE_HIX22)
  2981. {
  2982. relocation = tpoff (info, relocation);
  2983. break;
  2984. }
  2985. if (r_type == R_SPARC_TLS_LE_LOX10)
  2986. {
  2987. /* Change add into xor. */
  2988. relocation = tpoff (info, relocation);
  2989. bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
  2990. contents + rel->r_offset)
  2991. | 0x80182000), contents + rel->r_offset);
  2992. break;
  2993. }
  2994. if (h != NULL)
  2995. {
  2996. off = h->got.offset;
  2997. h->got.offset |= 1;
  2998. }
  2999. else
  3000. {
  3001. BFD_ASSERT (local_got_offsets != NULL);
  3002. off = local_got_offsets[r_symndx];
  3003. local_got_offsets[r_symndx] |= 1;
  3004. }
  3005. r_sparc_tlsldm:
  3006. if (htab->elf.sgot == NULL)
  3007. abort ();
  3008. if ((off & 1) != 0)
  3009. off &= ~1;
  3010. else
  3011. {
  3012. Elf_Internal_Rela outrel;
  3013. int dr_type, indx;
  3014. if (htab->elf.srelgot == NULL)
  3015. abort ();
  3016. SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
  3017. htab->elf.sgot->contents + off);
  3018. outrel.r_offset = (htab->elf.sgot->output_section->vma
  3019. + htab->elf.sgot->output_offset + off);
  3020. indx = h && h->dynindx != -1 ? h->dynindx : 0;
  3021. if (r_type == R_SPARC_TLS_IE_HI22
  3022. || r_type == R_SPARC_TLS_IE_LO10)
  3023. dr_type = SPARC_ELF_TPOFF_RELOC (htab);
  3024. else
  3025. dr_type = SPARC_ELF_DTPMOD_RELOC (htab);
  3026. if (dr_type == SPARC_ELF_TPOFF_RELOC (htab) && indx == 0)
  3027. outrel.r_addend = relocation - dtpoff_base (info);
  3028. else
  3029. outrel.r_addend = 0;
  3030. outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx, dr_type);
  3031. sparc_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
  3032. if (r_type == R_SPARC_TLS_GD_HI22
  3033. || r_type == R_SPARC_TLS_GD_LO10)
  3034. {
  3035. if (indx == 0)
  3036. {
  3037. BFD_ASSERT (! unresolved_reloc);
  3038. SPARC_ELF_PUT_WORD (htab, output_bfd,
  3039. relocation - dtpoff_base (info),
  3040. (htab->elf.sgot->contents + off
  3041. + SPARC_ELF_WORD_BYTES (htab)));
  3042. }
  3043. else
  3044. {
  3045. SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
  3046. (htab->elf.sgot->contents + off
  3047. + SPARC_ELF_WORD_BYTES (htab)));
  3048. outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx,
  3049. SPARC_ELF_DTPOFF_RELOC (htab));
  3050. outrel.r_offset += SPARC_ELF_WORD_BYTES (htab);
  3051. sparc_elf_append_rela (output_bfd, htab->elf.srelgot,
  3052. &outrel);
  3053. }
  3054. }
  3055. else if (dr_type == SPARC_ELF_DTPMOD_RELOC (htab))
  3056. {
  3057. SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
  3058. (htab->elf.sgot->contents + off
  3059. + SPARC_ELF_WORD_BYTES (htab)));
  3060. }
  3061. }
  3062. if (off >= (bfd_vma) -2)
  3063. abort ();
  3064. relocation = htab->elf.sgot->output_offset + off - got_base;
  3065. unresolved_reloc = false;
  3066. howto = _bfd_sparc_elf_howto_table + r_type;
  3067. break;
  3068. case R_SPARC_TLS_LDM_HI22:
  3069. case R_SPARC_TLS_LDM_LO10:
  3070. /* LD -> LE */
  3071. if (bfd_link_executable (info))
  3072. {
  3073. bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
  3074. continue;
  3075. }
  3076. off = htab->tls_ldm_got.offset;
  3077. htab->tls_ldm_got.offset |= 1;
  3078. goto r_sparc_tlsldm;
  3079. case R_SPARC_TLS_LDO_HIX22:
  3080. case R_SPARC_TLS_LDO_LOX10:
  3081. /* LD -> LE */
  3082. if (bfd_link_executable (info))
  3083. {
  3084. if (r_type == R_SPARC_TLS_LDO_HIX22)
  3085. r_type = R_SPARC_TLS_LE_HIX22;
  3086. else
  3087. r_type = R_SPARC_TLS_LE_LOX10;
  3088. }
  3089. else
  3090. {
  3091. relocation -= dtpoff_base (info);
  3092. break;
  3093. }
  3094. /* Fall through. */
  3095. case R_SPARC_TLS_LE_HIX22:
  3096. case R_SPARC_TLS_LE_LOX10:
  3097. if (!bfd_link_executable (info))
  3098. {
  3099. Elf_Internal_Rela outrel;
  3100. bfd_vma offset
  3101. = _bfd_elf_section_offset (output_bfd, info, input_section,
  3102. rel->r_offset);
  3103. if (offset == (bfd_vma) -1 || offset == (bfd_vma) -2)
  3104. memset (&outrel, 0, sizeof outrel);
  3105. else
  3106. {
  3107. outrel.r_offset = offset
  3108. + input_section->output_section->vma
  3109. + input_section->output_offset;
  3110. outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, r_type);
  3111. outrel.r_addend
  3112. = relocation - dtpoff_base (info) + rel->r_addend;
  3113. }
  3114. BFD_ASSERT (sreloc != NULL);
  3115. sparc_elf_append_rela (output_bfd, sreloc, &outrel);
  3116. continue;
  3117. }
  3118. relocation = tpoff (info, relocation);
  3119. break;
  3120. case R_SPARC_TLS_LDM_CALL:
  3121. /* LD -> LE */
  3122. if (bfd_link_executable (info))
  3123. {
  3124. /* mov %g0, %o0 */
  3125. bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
  3126. continue;
  3127. }
  3128. /* Fall through */
  3129. case R_SPARC_TLS_GD_CALL:
  3130. if (h != NULL)
  3131. tls_type = _bfd_sparc_elf_hash_entry (h)->tls_type;
  3132. else if (local_got_offsets)
  3133. tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
  3134. else
  3135. tls_type = GOT_UNKNOWN;
  3136. /* GD -> IE or LE */
  3137. if (bfd_link_executable (info)
  3138. || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
  3139. {
  3140. Elf_Internal_Rela *rel2;
  3141. bfd_vma insn;
  3142. /* GD -> LE */
  3143. if (bfd_link_executable (info) && (h == NULL || h->dynindx == -1))
  3144. {
  3145. bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
  3146. continue;
  3147. }
  3148. /* GD -> IE */
  3149. if (rel + 1 < relend
  3150. && SPARC_ELF_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD
  3151. && rel[1].r_offset == rel->r_offset + 4
  3152. && SPARC_ELF_R_SYMNDX (htab, rel[1].r_info) == r_symndx
  3153. && (((insn = bfd_get_32 (input_bfd,
  3154. contents + rel[1].r_offset))
  3155. >> 25) & 0x1f) == 8)
  3156. {
  3157. /* We have
  3158. call __tls_get_addr, %tgd_call(foo)
  3159. add %reg1, %reg2, %o0, %tgd_add(foo)
  3160. and change it into IE:
  3161. {ld,ldx} [%reg1 + %reg2], %o0, %tie_ldx(foo)
  3162. add %g7, %o0, %o0, %tie_add(foo).
  3163. add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
  3164. ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2,
  3165. ldx is 0xc0580000 | (rd << 25) | (rs1 << 14) | rs2. */
  3166. bfd_put_32 (output_bfd, insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000),
  3167. contents + rel->r_offset);
  3168. bfd_put_32 (output_bfd, 0x9001c008,
  3169. contents + rel->r_offset + 4);
  3170. rel++;
  3171. continue;
  3172. }
  3173. /* We cannot just overwrite the delay slot instruction,
  3174. as it might be what puts the %o0 argument to
  3175. __tls_get_addr into place. So we have to transpose
  3176. the delay slot with the add we patch in. */
  3177. insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
  3178. bfd_put_32 (output_bfd, insn,
  3179. contents + rel->r_offset);
  3180. bfd_put_32 (output_bfd, 0x9001c008,
  3181. contents + rel->r_offset + 4);
  3182. rel2 = rel;
  3183. while ((rel2 = sparc_elf_find_reloc_at_ofs (rel2 + 1, relend,
  3184. rel->r_offset + 4))
  3185. != NULL)
  3186. {
  3187. /* If the instruction we moved has a relocation attached to
  3188. it, adjust the offset so that it will apply to the correct
  3189. instruction. */
  3190. rel2->r_offset -= 4;
  3191. }
  3192. continue;
  3193. }
  3194. h = (struct elf_link_hash_entry *)
  3195. bfd_link_hash_lookup (info->hash, "__tls_get_addr", false,
  3196. false, true);
  3197. BFD_ASSERT (h != NULL);
  3198. r_type = R_SPARC_WPLT30;
  3199. howto = _bfd_sparc_elf_howto_table + r_type;
  3200. goto r_sparc_wplt30;
  3201. case R_SPARC_TLS_GD_ADD:
  3202. if (h != NULL)
  3203. tls_type = _bfd_sparc_elf_hash_entry (h)->tls_type;
  3204. else if (local_got_offsets)
  3205. tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
  3206. else
  3207. tls_type = GOT_UNKNOWN;
  3208. /* GD -> IE or LE */
  3209. if (bfd_link_executable (info) || tls_type == GOT_TLS_IE)
  3210. {
  3211. /* add %reg1, %reg2, %reg3, %tgd_add(foo)
  3212. changed into IE:
  3213. {ld,ldx} [%reg1 + %reg2], %reg3, %tie_ldx(foo)
  3214. or LE:
  3215. add %g7, %reg2, %reg3. */
  3216. bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3217. if (bfd_link_executable (info) && (h == NULL || h->dynindx == -1))
  3218. relocation = (insn & ~0x7c000) | 0x1c000;
  3219. else
  3220. relocation = insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000);
  3221. bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
  3222. }
  3223. continue;
  3224. case R_SPARC_TLS_LDM_ADD:
  3225. /* LD -> LE */
  3226. if (bfd_link_executable (info))
  3227. bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
  3228. continue;
  3229. case R_SPARC_TLS_LDO_ADD:
  3230. /* LD -> LE */
  3231. if (bfd_link_executable (info))
  3232. {
  3233. /* Change rs1 into %g7. */
  3234. bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3235. insn = (insn & ~0x7c000) | 0x1c000;
  3236. bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
  3237. }
  3238. continue;
  3239. case R_SPARC_TLS_IE_LD:
  3240. case R_SPARC_TLS_IE_LDX:
  3241. /* IE -> LE */
  3242. if (bfd_link_executable (info) && (h == NULL || h->dynindx == -1))
  3243. {
  3244. bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3245. int rs2 = insn & 0x1f;
  3246. int rd = (insn >> 25) & 0x1f;
  3247. if (rs2 == rd)
  3248. relocation = SPARC_NOP;
  3249. else
  3250. relocation = 0x80100000 | (insn & 0x3e00001f);
  3251. bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
  3252. }
  3253. continue;
  3254. case R_SPARC_TLS_IE_ADD:
  3255. /* Totally useless relocation. */
  3256. continue;
  3257. case R_SPARC_TLS_DTPOFF32:
  3258. case R_SPARC_TLS_DTPOFF64:
  3259. relocation -= dtpoff_base (info);
  3260. break;
  3261. default:
  3262. break;
  3263. }
  3264. /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
  3265. because such sections are not SEC_ALLOC and thus ld.so will
  3266. not process them. */
  3267. if (unresolved_reloc
  3268. && !((input_section->flags & SEC_DEBUGGING) != 0
  3269. && h->def_dynamic)
  3270. && _bfd_elf_section_offset (output_bfd, info, input_section,
  3271. rel->r_offset) != (bfd_vma) -1)
  3272. _bfd_error_handler
  3273. /* xgettext:c-format */
  3274. (_("%pB(%pA+%#" PRIx64 "): "
  3275. "unresolvable %s relocation against symbol `%s'"),
  3276. input_bfd,
  3277. input_section,
  3278. (uint64_t) rel->r_offset,
  3279. howto->name,
  3280. h->root.root.string);
  3281. r = bfd_reloc_continue;
  3282. if (r_type == R_SPARC_OLO10)
  3283. {
  3284. bfd_vma x;
  3285. if (! ABI_64_P (output_bfd))
  3286. abort ();
  3287. relocation += rel->r_addend;
  3288. relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
  3289. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3290. x = (x & ~(bfd_vma) 0x1fff) | (relocation & 0x1fff);
  3291. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  3292. r = bfd_check_overflow (howto->complain_on_overflow,
  3293. howto->bitsize, howto->rightshift,
  3294. bfd_arch_bits_per_address (input_bfd),
  3295. relocation);
  3296. }
  3297. else if (r_type == R_SPARC_WDISP16)
  3298. {
  3299. bfd_vma x;
  3300. relocation += rel->r_addend;
  3301. relocation -= (input_section->output_section->vma
  3302. + input_section->output_offset);
  3303. relocation -= rel->r_offset;
  3304. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3305. x |= ((((relocation >> 2) & 0xc000) << 6)
  3306. | ((relocation >> 2) & 0x3fff));
  3307. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  3308. r = bfd_check_overflow (howto->complain_on_overflow,
  3309. howto->bitsize, howto->rightshift,
  3310. bfd_arch_bits_per_address (input_bfd),
  3311. relocation);
  3312. }
  3313. else if (r_type == R_SPARC_WDISP10)
  3314. {
  3315. bfd_vma x;
  3316. relocation += rel->r_addend;
  3317. relocation -= (input_section->output_section->vma
  3318. + input_section->output_offset);
  3319. relocation -= rel->r_offset;
  3320. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3321. x |= ((((relocation >> 2) & 0x300) << 11)
  3322. | (((relocation >> 2) & 0xff) << 5));
  3323. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  3324. r = bfd_check_overflow (howto->complain_on_overflow,
  3325. howto->bitsize, howto->rightshift,
  3326. bfd_arch_bits_per_address (input_bfd),
  3327. relocation);
  3328. }
  3329. else if (r_type == R_SPARC_REV32)
  3330. {
  3331. bfd_vma x;
  3332. relocation = relocation + rel->r_addend;
  3333. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3334. x = x + relocation;
  3335. bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
  3336. r = bfd_reloc_ok;
  3337. }
  3338. else if (r_type == R_SPARC_TLS_LDO_HIX22
  3339. || r_type == R_SPARC_TLS_LE_HIX22)
  3340. {
  3341. bfd_vma x;
  3342. relocation += rel->r_addend;
  3343. if (r_type == R_SPARC_TLS_LE_HIX22)
  3344. relocation ^= MINUS_ONE;
  3345. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3346. x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
  3347. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  3348. r = bfd_reloc_ok;
  3349. }
  3350. else if (r_type == R_SPARC_TLS_LDO_LOX10
  3351. || r_type == R_SPARC_TLS_LE_LOX10)
  3352. {
  3353. bfd_vma x;
  3354. relocation += rel->r_addend;
  3355. relocation &= 0x3ff;
  3356. if (r_type == R_SPARC_TLS_LE_LOX10)
  3357. relocation |= 0x1c00;
  3358. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3359. x = (x & ~(bfd_vma) 0x1fff) | relocation;
  3360. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  3361. r = bfd_reloc_ok;
  3362. }
  3363. else if (r_type == R_SPARC_HIX22
  3364. || r_type == R_SPARC_GOTDATA_HIX22
  3365. || r_type == R_SPARC_GOTDATA_OP_HIX22)
  3366. {
  3367. bfd_vma x;
  3368. relocation += rel->r_addend;
  3369. if (r_type == R_SPARC_HIX22
  3370. || (bfd_signed_vma) relocation < 0)
  3371. relocation = relocation ^ MINUS_ONE;
  3372. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3373. x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
  3374. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  3375. r = bfd_check_overflow (howto->complain_on_overflow,
  3376. howto->bitsize, howto->rightshift,
  3377. bfd_arch_bits_per_address (input_bfd),
  3378. relocation);
  3379. }
  3380. else if (r_type == R_SPARC_LOX10
  3381. || r_type == R_SPARC_GOTDATA_LOX10
  3382. || r_type == R_SPARC_GOTDATA_OP_LOX10)
  3383. {
  3384. bfd_vma x;
  3385. relocation += rel->r_addend;
  3386. if (r_type == R_SPARC_LOX10
  3387. || (bfd_signed_vma) relocation < 0)
  3388. relocation = (relocation & 0x3ff) | 0x1c00;
  3389. else
  3390. relocation = (relocation & 0x3ff);
  3391. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3392. x = (x & ~(bfd_vma) 0x1fff) | relocation;
  3393. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  3394. r = bfd_reloc_ok;
  3395. }
  3396. else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
  3397. && sec_do_relax (input_section)
  3398. && rel->r_offset + 4 < input_section->size)
  3399. {
  3400. #define G0 0
  3401. #define O7 15
  3402. #define XCC (2 << 20)
  3403. #define COND(x) (((x)&0xf)<<25)
  3404. #define CONDA COND(0x8)
  3405. #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
  3406. #define INSN_BA (F2(0,2) | CONDA)
  3407. #define INSN_OR F3(2, 0x2, 0)
  3408. #define INSN_NOP F2(0,4)
  3409. bfd_vma x, y;
  3410. /* If the instruction is a call with either:
  3411. restore
  3412. arithmetic instruction with rd == %o7
  3413. where rs1 != %o7 and rs2 if it is register != %o7
  3414. then we can optimize if the call destination is near
  3415. by changing the call into a branch always. */
  3416. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  3417. y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
  3418. if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
  3419. {
  3420. if (((y & OP3(~0)) == OP3(0x3d) /* restore */
  3421. || ((y & OP3(0x28)) == 0 /* arithmetic */
  3422. && (y & RD(~0)) == RD(O7)))
  3423. && (y & RS1(~0)) != RS1(O7)
  3424. && ((y & F3I(~0))
  3425. || (y & RS2(~0)) != RS2(O7)))
  3426. {
  3427. bfd_vma reloc;
  3428. reloc = relocation + rel->r_addend - rel->r_offset;
  3429. reloc -= (input_section->output_section->vma
  3430. + input_section->output_offset);
  3431. /* Ensure the branch fits into simm22. */
  3432. if ((reloc & 3) == 0
  3433. && ((reloc & ~(bfd_vma)0x7fffff) == 0
  3434. || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
  3435. {
  3436. reloc >>= 2;
  3437. /* Check whether it fits into simm19. */
  3438. if (((reloc & 0x3c0000) == 0
  3439. || (reloc & 0x3c0000) == 0x3c0000)
  3440. && (ABI_64_P (output_bfd)
  3441. || elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
  3442. x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
  3443. else
  3444. x = INSN_BA | (reloc & 0x3fffff); /* ba */
  3445. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  3446. r = bfd_reloc_ok;
  3447. if (rel->r_offset >= 4
  3448. && (y & (0xffffffff ^ RS1(~0)))
  3449. == (INSN_OR | RD(O7) | RS2(G0)))
  3450. {
  3451. bfd_vma z;
  3452. unsigned int reg;
  3453. z = bfd_get_32 (input_bfd,
  3454. contents + rel->r_offset - 4);
  3455. if ((z & (0xffffffff ^ RD(~0)))
  3456. != (INSN_OR | RS1(O7) | RS2(G0)))
  3457. continue;
  3458. /* The sequence was
  3459. or %o7, %g0, %rN
  3460. call foo
  3461. or %rN, %g0, %o7
  3462. If call foo was replaced with ba, replace
  3463. or %rN, %g0, %o7 with nop. */
  3464. reg = (y & RS1(~0)) >> 14;
  3465. if (reg != ((z & RD(~0)) >> 25)
  3466. || reg == G0 || reg == O7)
  3467. continue;
  3468. bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
  3469. contents + rel->r_offset + 4);
  3470. }
  3471. }
  3472. }
  3473. }
  3474. }
  3475. if (r == bfd_reloc_continue)
  3476. {
  3477. do_relocation:
  3478. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  3479. contents, rel->r_offset,
  3480. relocation, rel->r_addend);
  3481. }
  3482. if (r != bfd_reloc_ok)
  3483. {
  3484. switch (r)
  3485. {
  3486. default:
  3487. case bfd_reloc_outofrange:
  3488. abort ();
  3489. case bfd_reloc_overflow:
  3490. {
  3491. const char *name;
  3492. /* The Solaris native linker silently disregards overflows.
  3493. We don't, but this breaks stabs debugging info, whose
  3494. relocations are only 32-bits wide. Ignore overflows in
  3495. this case and also for discarded entries. */
  3496. if ((r_type == R_SPARC_32
  3497. || r_type == R_SPARC_UA32
  3498. || r_type == R_SPARC_DISP32)
  3499. && (((input_section->flags & SEC_DEBUGGING) != 0
  3500. && strcmp (bfd_section_name (input_section),
  3501. ".stab") == 0)
  3502. || _bfd_elf_section_offset (output_bfd, info,
  3503. input_section,
  3504. rel->r_offset)
  3505. == (bfd_vma)-1))
  3506. break;
  3507. if (h != NULL)
  3508. {
  3509. /* Assume this is a call protected by other code that
  3510. detect the symbol is undefined. If this is the case,
  3511. we can safely ignore the overflow. If not, the
  3512. program is hosed anyway, and a little warning isn't
  3513. going to help. */
  3514. if (h->root.type == bfd_link_hash_undefweak
  3515. && howto->pc_relative)
  3516. break;
  3517. name = NULL;
  3518. }
  3519. else
  3520. {
  3521. name = bfd_elf_string_from_elf_section (input_bfd,
  3522. symtab_hdr->sh_link,
  3523. sym->st_name);
  3524. if (name == NULL)
  3525. return false;
  3526. if (*name == '\0')
  3527. name = bfd_section_name (sec);
  3528. }
  3529. (*info->callbacks->reloc_overflow)
  3530. (info, (h ? &h->root : NULL), name, howto->name,
  3531. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  3532. }
  3533. break;
  3534. }
  3535. }
  3536. }
  3537. return true;
  3538. }
  3539. /* Build a VxWorks PLT entry. PLT_INDEX is the index of the PLT entry
  3540. and PLT_OFFSET is the byte offset from the start of .plt. GOT_OFFSET
  3541. is the offset of the associated .got.plt entry from
  3542. _GLOBAL_OFFSET_TABLE_. */
  3543. static void
  3544. sparc_vxworks_build_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
  3545. bfd_vma plt_offset, bfd_vma plt_index,
  3546. bfd_vma got_offset)
  3547. {
  3548. bfd_vma got_base;
  3549. const bfd_vma *plt_entry;
  3550. struct _bfd_sparc_elf_link_hash_table *htab;
  3551. bfd_byte *loc;
  3552. Elf_Internal_Rela rela;
  3553. htab = _bfd_sparc_elf_hash_table (info);
  3554. BFD_ASSERT (htab != NULL);
  3555. if (bfd_link_pic (info))
  3556. {
  3557. plt_entry = sparc_vxworks_shared_plt_entry;
  3558. got_base = 0;
  3559. }
  3560. else
  3561. {
  3562. plt_entry = sparc_vxworks_exec_plt_entry;
  3563. got_base = (htab->elf.hgot->root.u.def.value
  3564. + htab->elf.hgot->root.u.def.section->output_offset
  3565. + htab->elf.hgot->root.u.def.section->output_section->vma);
  3566. }
  3567. /* Fill in the entry in the procedure linkage table. */
  3568. bfd_put_32 (output_bfd, plt_entry[0] + ((got_base + got_offset) >> 10),
  3569. htab->elf.splt->contents + plt_offset);
  3570. bfd_put_32 (output_bfd, plt_entry[1] + ((got_base + got_offset) & 0x3ff),
  3571. htab->elf.splt->contents + plt_offset + 4);
  3572. bfd_put_32 (output_bfd, plt_entry[2],
  3573. htab->elf.splt->contents + plt_offset + 8);
  3574. bfd_put_32 (output_bfd, plt_entry[3],
  3575. htab->elf.splt->contents + plt_offset + 12);
  3576. bfd_put_32 (output_bfd, plt_entry[4],
  3577. htab->elf.splt->contents + plt_offset + 16);
  3578. bfd_put_32 (output_bfd, plt_entry[5] + (plt_index >> 10),
  3579. htab->elf.splt->contents + plt_offset + 20);
  3580. /* PC-relative displacement for a branch to the start of
  3581. the PLT section. */
  3582. bfd_put_32 (output_bfd, plt_entry[6] + (((-plt_offset - 24) >> 2)
  3583. & 0x003fffff),
  3584. htab->elf.splt->contents + plt_offset + 24);
  3585. bfd_put_32 (output_bfd, plt_entry[7] + (plt_index & 0x3ff),
  3586. htab->elf.splt->contents + plt_offset + 28);
  3587. /* Fill in the .got.plt entry, pointing initially at the
  3588. second half of the PLT entry. */
  3589. BFD_ASSERT (htab->elf.sgotplt != NULL);
  3590. bfd_put_32 (output_bfd,
  3591. htab->elf.splt->output_section->vma
  3592. + htab->elf.splt->output_offset
  3593. + plt_offset + 20,
  3594. htab->elf.sgotplt->contents + got_offset);
  3595. /* Add relocations to .rela.plt.unloaded. */
  3596. if (!bfd_link_pic (info))
  3597. {
  3598. loc = (htab->srelplt2->contents
  3599. + (2 + 3 * plt_index) * sizeof (Elf32_External_Rela));
  3600. /* Relocate the initial sethi. */
  3601. rela.r_offset = (htab->elf.splt->output_section->vma
  3602. + htab->elf.splt->output_offset
  3603. + plt_offset);
  3604. rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
  3605. rela.r_addend = got_offset;
  3606. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  3607. loc += sizeof (Elf32_External_Rela);
  3608. /* Likewise the following or. */
  3609. rela.r_offset += 4;
  3610. rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
  3611. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  3612. loc += sizeof (Elf32_External_Rela);
  3613. /* Relocate the .got.plt entry. */
  3614. rela.r_offset = (htab->elf.sgotplt->output_section->vma
  3615. + htab->elf.sgotplt->output_offset
  3616. + got_offset);
  3617. rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_SPARC_32);
  3618. rela.r_addend = plt_offset + 20;
  3619. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  3620. }
  3621. }
  3622. /* Finish up dynamic symbol handling. We set the contents of various
  3623. dynamic sections here. */
  3624. bool
  3625. _bfd_sparc_elf_finish_dynamic_symbol (bfd *output_bfd,
  3626. struct bfd_link_info *info,
  3627. struct elf_link_hash_entry *h,
  3628. Elf_Internal_Sym *sym)
  3629. {
  3630. struct _bfd_sparc_elf_link_hash_table *htab;
  3631. const struct elf_backend_data *bed;
  3632. struct _bfd_sparc_elf_link_hash_entry *eh;
  3633. bool resolved_to_zero;
  3634. htab = _bfd_sparc_elf_hash_table (info);
  3635. BFD_ASSERT (htab != NULL);
  3636. bed = get_elf_backend_data (output_bfd);
  3637. eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
  3638. /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
  3639. resolved undefined weak symbols in executable so that their
  3640. references have value 0 at run-time. */
  3641. resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
  3642. if (h->plt.offset != (bfd_vma) -1)
  3643. {
  3644. asection *splt;
  3645. asection *srela;
  3646. Elf_Internal_Rela rela;
  3647. bfd_byte *loc;
  3648. bfd_vma r_offset, got_offset;
  3649. int rela_index;
  3650. /* When building a static executable, use .iplt and
  3651. .rela.iplt sections for STT_GNU_IFUNC symbols. */
  3652. if (htab->elf.splt != NULL)
  3653. {
  3654. splt = htab->elf.splt;
  3655. srela = htab->elf.srelplt;
  3656. }
  3657. else
  3658. {
  3659. splt = htab->elf.iplt;
  3660. srela = htab->elf.irelplt;
  3661. }
  3662. if (splt == NULL || srela == NULL)
  3663. abort ();
  3664. /* Fill in the entry in the .rela.plt section. */
  3665. if (htab->elf.target_os == is_vxworks)
  3666. {
  3667. /* Work out the index of this PLT entry. */
  3668. rela_index = ((h->plt.offset - htab->plt_header_size)
  3669. / htab->plt_entry_size);
  3670. /* Calculate the offset of the associated .got.plt entry.
  3671. The first three entries are reserved. */
  3672. got_offset = (rela_index + 3) * 4;
  3673. sparc_vxworks_build_plt_entry (output_bfd, info, h->plt.offset,
  3674. rela_index, got_offset);
  3675. /* On VxWorks, the relocation points to the .got.plt entry,
  3676. not the .plt entry. */
  3677. rela.r_offset = (htab->elf.sgotplt->output_section->vma
  3678. + htab->elf.sgotplt->output_offset
  3679. + got_offset);
  3680. rela.r_addend = 0;
  3681. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx,
  3682. R_SPARC_JMP_SLOT);
  3683. }
  3684. else
  3685. {
  3686. bool ifunc = false;
  3687. /* Fill in the entry in the procedure linkage table. */
  3688. rela_index = SPARC_ELF_BUILD_PLT_ENTRY (htab, output_bfd, splt,
  3689. h->plt.offset, splt->size,
  3690. &r_offset);
  3691. if (h == NULL
  3692. || h->dynindx == -1
  3693. || ((bfd_link_executable (info)
  3694. || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  3695. && h->def_regular
  3696. && h->type == STT_GNU_IFUNC))
  3697. {
  3698. ifunc = true;
  3699. BFD_ASSERT (h == NULL
  3700. || (h->type == STT_GNU_IFUNC
  3701. && h->def_regular
  3702. && (h->root.type == bfd_link_hash_defined
  3703. || h->root.type == bfd_link_hash_defweak)));
  3704. }
  3705. rela.r_offset = r_offset
  3706. + (splt->output_section->vma + splt->output_offset);
  3707. if (ABI_64_P (output_bfd)
  3708. && h->plt.offset >= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
  3709. {
  3710. if (ifunc)
  3711. {
  3712. rela.r_addend = (h->root.u.def.section->output_section->vma
  3713. + h->root.u.def.section->output_offset
  3714. + h->root.u.def.value);
  3715. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0,
  3716. R_SPARC_IRELATIVE);
  3717. }
  3718. else
  3719. {
  3720. rela.r_addend = (-(h->plt.offset + 4)
  3721. - splt->output_section->vma
  3722. - splt->output_offset);
  3723. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx,
  3724. R_SPARC_JMP_SLOT);
  3725. }
  3726. }
  3727. else
  3728. {
  3729. if (ifunc)
  3730. {
  3731. rela.r_addend = (h->root.u.def.section->output_section->vma
  3732. + h->root.u.def.section->output_offset
  3733. + h->root.u.def.value);
  3734. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0,
  3735. R_SPARC_JMP_IREL);
  3736. }
  3737. else
  3738. {
  3739. rela.r_addend = 0;
  3740. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx,
  3741. R_SPARC_JMP_SLOT);
  3742. }
  3743. }
  3744. }
  3745. /* Adjust for the first 4 reserved elements in the .plt section
  3746. when setting the offset in the .rela.plt section.
  3747. Sun forgot to read their own ABI and copied elf32-sparc behaviour,
  3748. thus .plt[4] has corresponding .rela.plt[0] and so on. */
  3749. loc = srela->contents;
  3750. loc += rela_index * bed->s->sizeof_rela;
  3751. bed->s->swap_reloca_out (output_bfd, &rela, loc);
  3752. if (!resolved_to_zero && !h->def_regular)
  3753. {
  3754. /* Mark the symbol as undefined, rather than as defined in
  3755. the .plt section. Leave the value alone. */
  3756. sym->st_shndx = SHN_UNDEF;
  3757. /* If the symbol is weak, we do need to clear the value.
  3758. Otherwise, the PLT entry would provide a definition for
  3759. the symbol even if the symbol wasn't defined anywhere,
  3760. and so the symbol would never be NULL. */
  3761. if (!h->ref_regular_nonweak)
  3762. sym->st_value = 0;
  3763. }
  3764. }
  3765. /* Don't generate dynamic GOT relocation against resolved undefined weak
  3766. symbols in an executable. */
  3767. if (h->got.offset != (bfd_vma) -1
  3768. && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_GD
  3769. && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_IE
  3770. && !(h->root.type == bfd_link_hash_undefweak
  3771. && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  3772. || resolved_to_zero)))
  3773. {
  3774. asection *sgot;
  3775. asection *srela;
  3776. Elf_Internal_Rela rela;
  3777. /* This symbol has an entry in the GOT. Set it up. */
  3778. sgot = htab->elf.sgot;
  3779. srela = htab->elf.srelgot;
  3780. BFD_ASSERT (sgot != NULL && srela != NULL);
  3781. rela.r_offset = (sgot->output_section->vma
  3782. + sgot->output_offset
  3783. + (h->got.offset &~ (bfd_vma) 1));
  3784. /* If this is a -Bsymbolic link, and the symbol is defined
  3785. locally, we just want to emit a RELATIVE reloc. Likewise if
  3786. the symbol was forced to be local because of a version file.
  3787. The entry in the global offset table will already have been
  3788. initialized in the relocate_section function. */
  3789. if (! bfd_link_pic (info)
  3790. && h->type == STT_GNU_IFUNC
  3791. && h->def_regular)
  3792. {
  3793. asection *plt;
  3794. /* We load the GOT entry with the PLT entry. */
  3795. plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
  3796. SPARC_ELF_PUT_WORD (htab, output_bfd,
  3797. (plt->output_section->vma
  3798. + plt->output_offset + h->plt.offset),
  3799. htab->elf.sgot->contents
  3800. + (h->got.offset & ~(bfd_vma) 1));
  3801. return true;
  3802. }
  3803. if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
  3804. {
  3805. asection *sec = h->root.u.def.section;
  3806. if (h->type == STT_GNU_IFUNC)
  3807. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, R_SPARC_IRELATIVE);
  3808. else
  3809. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, R_SPARC_RELATIVE);
  3810. rela.r_addend = (h->root.u.def.value
  3811. + sec->output_section->vma
  3812. + sec->output_offset);
  3813. }
  3814. else
  3815. {
  3816. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_GLOB_DAT);
  3817. rela.r_addend = 0;
  3818. }
  3819. SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
  3820. sgot->contents + (h->got.offset & ~(bfd_vma) 1));
  3821. sparc_elf_append_rela (output_bfd, srela, &rela);
  3822. }
  3823. if (h->needs_copy)
  3824. {
  3825. asection *s;
  3826. Elf_Internal_Rela rela;
  3827. /* This symbols needs a copy reloc. Set it up. */
  3828. BFD_ASSERT (h->dynindx != -1);
  3829. rela.r_offset = (h->root.u.def.value
  3830. + h->root.u.def.section->output_section->vma
  3831. + h->root.u.def.section->output_offset);
  3832. rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_COPY);
  3833. rela.r_addend = 0;
  3834. if (h->root.u.def.section == htab->elf.sdynrelro)
  3835. s = htab->elf.sreldynrelro;
  3836. else
  3837. s = htab->elf.srelbss;
  3838. sparc_elf_append_rela (output_bfd, s, &rela);
  3839. }
  3840. /* Mark some specially defined symbols as absolute. On VxWorks,
  3841. _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
  3842. ".got" section. Likewise _PROCEDURE_LINKAGE_TABLE_ and ".plt". */
  3843. if (sym != NULL
  3844. && (h == htab->elf.hdynamic
  3845. || (htab->elf.target_os != is_vxworks
  3846. && (h == htab->elf.hgot || h == htab->elf.hplt))))
  3847. sym->st_shndx = SHN_ABS;
  3848. return true;
  3849. }
  3850. /* Finish up the dynamic sections. */
  3851. static bool
  3852. sparc_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
  3853. bfd *dynobj, asection *sdyn,
  3854. asection *splt ATTRIBUTE_UNUSED)
  3855. {
  3856. struct _bfd_sparc_elf_link_hash_table *htab;
  3857. const struct elf_backend_data *bed;
  3858. bfd_byte *dyncon, *dynconend;
  3859. size_t dynsize;
  3860. int stt_regidx = -1;
  3861. bool abi_64_p;
  3862. htab = _bfd_sparc_elf_hash_table (info);
  3863. BFD_ASSERT (htab != NULL);
  3864. bed = get_elf_backend_data (output_bfd);
  3865. dynsize = bed->s->sizeof_dyn;
  3866. dynconend = sdyn->contents + sdyn->size;
  3867. abi_64_p = ABI_64_P (output_bfd);
  3868. for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
  3869. {
  3870. Elf_Internal_Dyn dyn;
  3871. bool size;
  3872. bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  3873. if (htab->elf.target_os == is_vxworks && dyn.d_tag == DT_PLTGOT)
  3874. {
  3875. /* On VxWorks, DT_PLTGOT should point to the start of the GOT,
  3876. not to the start of the PLT. */
  3877. if (htab->elf.sgotplt)
  3878. {
  3879. dyn.d_un.d_val = (htab->elf.sgotplt->output_section->vma
  3880. + htab->elf.sgotplt->output_offset);
  3881. bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
  3882. }
  3883. }
  3884. else if (htab->elf.target_os == is_vxworks
  3885. && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
  3886. bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
  3887. else if (abi_64_p && dyn.d_tag == DT_SPARC_REGISTER)
  3888. {
  3889. if (stt_regidx == -1)
  3890. {
  3891. stt_regidx =
  3892. _bfd_elf_link_lookup_local_dynindx (info, output_bfd, -1);
  3893. if (stt_regidx == -1)
  3894. return false;
  3895. }
  3896. dyn.d_un.d_val = stt_regidx++;
  3897. bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
  3898. }
  3899. else
  3900. {
  3901. asection *s;
  3902. switch (dyn.d_tag)
  3903. {
  3904. case DT_PLTGOT:
  3905. s = htab->elf.splt;
  3906. size = false;
  3907. break;
  3908. case DT_PLTRELSZ:
  3909. s = htab->elf.srelplt;
  3910. size = true;
  3911. break;
  3912. case DT_JMPREL:
  3913. s = htab->elf.srelplt;
  3914. size = false;
  3915. break;
  3916. default:
  3917. continue;
  3918. }
  3919. if (s == NULL)
  3920. dyn.d_un.d_val = 0;
  3921. else
  3922. {
  3923. if (!size)
  3924. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  3925. else
  3926. dyn.d_un.d_val = s->size;
  3927. }
  3928. bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
  3929. }
  3930. }
  3931. return true;
  3932. }
  3933. /* Install the first PLT entry in a VxWorks executable and make sure that
  3934. .rela.plt.unloaded relocations have the correct symbol indexes. */
  3935. static void
  3936. sparc_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
  3937. {
  3938. struct _bfd_sparc_elf_link_hash_table *htab;
  3939. Elf_Internal_Rela rela;
  3940. bfd_vma got_base;
  3941. bfd_byte *loc;
  3942. htab = _bfd_sparc_elf_hash_table (info);
  3943. BFD_ASSERT (htab != NULL);
  3944. /* Calculate the absolute value of _GLOBAL_OFFSET_TABLE_. */
  3945. got_base = (htab->elf.hgot->root.u.def.section->output_section->vma
  3946. + htab->elf.hgot->root.u.def.section->output_offset
  3947. + htab->elf.hgot->root.u.def.value);
  3948. /* Install the initial PLT entry. */
  3949. bfd_put_32 (output_bfd,
  3950. sparc_vxworks_exec_plt0_entry[0] + ((got_base + 8) >> 10),
  3951. htab->elf.splt->contents);
  3952. bfd_put_32 (output_bfd,
  3953. sparc_vxworks_exec_plt0_entry[1] + ((got_base + 8) & 0x3ff),
  3954. htab->elf.splt->contents + 4);
  3955. bfd_put_32 (output_bfd,
  3956. sparc_vxworks_exec_plt0_entry[2],
  3957. htab->elf.splt->contents + 8);
  3958. bfd_put_32 (output_bfd,
  3959. sparc_vxworks_exec_plt0_entry[3],
  3960. htab->elf.splt->contents + 12);
  3961. bfd_put_32 (output_bfd,
  3962. sparc_vxworks_exec_plt0_entry[4],
  3963. htab->elf.splt->contents + 16);
  3964. loc = htab->srelplt2->contents;
  3965. /* Add an unloaded relocation for the initial entry's "sethi". */
  3966. rela.r_offset = (htab->elf.splt->output_section->vma
  3967. + htab->elf.splt->output_offset);
  3968. rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
  3969. rela.r_addend = 8;
  3970. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  3971. loc += sizeof (Elf32_External_Rela);
  3972. /* Likewise the following "or". */
  3973. rela.r_offset += 4;
  3974. rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
  3975. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  3976. loc += sizeof (Elf32_External_Rela);
  3977. /* Fix up the remaining .rela.plt.unloaded relocations. They may have
  3978. the wrong symbol index for _G_O_T_ or _P_L_T_ depending on the order
  3979. in which symbols were output. */
  3980. while (loc < htab->srelplt2->contents + htab->srelplt2->size)
  3981. {
  3982. Elf_Internal_Rela rel;
  3983. /* The entry's initial "sethi" (against _G_O_T_). */
  3984. bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
  3985. rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
  3986. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  3987. loc += sizeof (Elf32_External_Rela);
  3988. /* The following "or" (also against _G_O_T_). */
  3989. bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
  3990. rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
  3991. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  3992. loc += sizeof (Elf32_External_Rela);
  3993. /* The .got.plt entry (against _P_L_T_). */
  3994. bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
  3995. rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_SPARC_32);
  3996. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  3997. loc += sizeof (Elf32_External_Rela);
  3998. }
  3999. }
  4000. /* Install the first PLT entry in a VxWorks shared object. */
  4001. static void
  4002. sparc_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
  4003. {
  4004. struct _bfd_sparc_elf_link_hash_table *htab;
  4005. unsigned int i;
  4006. htab = _bfd_sparc_elf_hash_table (info);
  4007. BFD_ASSERT (htab != NULL);
  4008. for (i = 0; i < ARRAY_SIZE (sparc_vxworks_shared_plt0_entry); i++)
  4009. bfd_put_32 (output_bfd, sparc_vxworks_shared_plt0_entry[i],
  4010. htab->elf.splt->contents + i * 4);
  4011. }
  4012. /* Finish up local dynamic symbol handling. We set the contents of
  4013. various dynamic sections here. */
  4014. static int
  4015. finish_local_dynamic_symbol (void **slot, void *inf)
  4016. {
  4017. struct elf_link_hash_entry *h
  4018. = (struct elf_link_hash_entry *) *slot;
  4019. struct bfd_link_info *info
  4020. = (struct bfd_link_info *) inf;
  4021. return _bfd_sparc_elf_finish_dynamic_symbol (info->output_bfd, info,
  4022. h, NULL);
  4023. }
  4024. /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
  4025. here since undefined weak symbol may not be dynamic and may not be
  4026. called for _bfd_sparc_elf_finish_dynamic_symbol. */
  4027. static bool
  4028. pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
  4029. void *inf)
  4030. {
  4031. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
  4032. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  4033. if (h->root.type != bfd_link_hash_undefweak
  4034. || h->dynindx != -1)
  4035. return true;
  4036. return _bfd_sparc_elf_finish_dynamic_symbol (info->output_bfd, info,
  4037. h, NULL);
  4038. }
  4039. bool
  4040. _bfd_sparc_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
  4041. {
  4042. bfd *dynobj;
  4043. asection *sdyn;
  4044. struct _bfd_sparc_elf_link_hash_table *htab;
  4045. htab = _bfd_sparc_elf_hash_table (info);
  4046. BFD_ASSERT (htab != NULL);
  4047. dynobj = htab->elf.dynobj;
  4048. /* We arranged in size_dynamic_sections to put the STT_REGISTER
  4049. entries at the end of the dynlocal list, so they came at the end
  4050. of the local symbols in the symtab. Except that they aren't
  4051. STB_LOCAL, so we need to back up symtab->sh_info. */
  4052. if (ABI_64_P (output_bfd)
  4053. && elf_hash_table (info)->dynlocal)
  4054. {
  4055. asection *dynsymsec = bfd_get_linker_section (dynobj, ".dynsym");
  4056. struct elf_link_local_dynamic_entry *e;
  4057. for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
  4058. if (e->input_indx == -1)
  4059. break;
  4060. if (e)
  4061. elf_section_data (dynsymsec->output_section)->this_hdr.sh_info
  4062. = e->dynindx;
  4063. }
  4064. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  4065. if (elf_hash_table (info)->dynamic_sections_created)
  4066. {
  4067. asection *splt;
  4068. splt = htab->elf.splt;
  4069. BFD_ASSERT (splt != NULL && sdyn != NULL);
  4070. if (!sparc_finish_dyn (output_bfd, info, dynobj, sdyn, splt))
  4071. return false;
  4072. /* Initialize the contents of the .plt section. */
  4073. if (splt->size > 0)
  4074. {
  4075. if (htab->elf.target_os == is_vxworks)
  4076. {
  4077. if (bfd_link_pic (info))
  4078. sparc_vxworks_finish_shared_plt (output_bfd, info);
  4079. else
  4080. sparc_vxworks_finish_exec_plt (output_bfd, info);
  4081. }
  4082. else
  4083. {
  4084. memset (splt->contents, 0, htab->plt_header_size);
  4085. if (!ABI_64_P (output_bfd))
  4086. bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
  4087. splt->contents + splt->size - 4);
  4088. }
  4089. }
  4090. if (elf_section_data (splt->output_section) != NULL)
  4091. elf_section_data (splt->output_section)->this_hdr.sh_entsize
  4092. = ((htab->elf.target_os == is_vxworks
  4093. || !ABI_64_P (output_bfd))
  4094. ? 0 : htab->plt_entry_size);
  4095. }
  4096. /* Set the first entry in the global offset table to the address of
  4097. the dynamic section. */
  4098. if (htab->elf.sgot && htab->elf.sgot->size > 0)
  4099. {
  4100. bfd_vma val = (sdyn ?
  4101. sdyn->output_section->vma + sdyn->output_offset :
  4102. 0);
  4103. SPARC_ELF_PUT_WORD (htab, output_bfd, val, htab->elf.sgot->contents);
  4104. }
  4105. if (htab->elf.sgot)
  4106. elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize =
  4107. SPARC_ELF_WORD_BYTES (htab);
  4108. /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
  4109. htab_traverse (htab->loc_hash_table, finish_local_dynamic_symbol, info);
  4110. /* Fill PLT entries for undefined weak symbols in PIE. */
  4111. if (bfd_link_pie (info))
  4112. bfd_hash_traverse (&info->hash->table,
  4113. pie_finish_undefweak_symbol,
  4114. info);
  4115. return true;
  4116. }
  4117. /* Set the right machine number for a SPARC ELF file. */
  4118. bool
  4119. _bfd_sparc_elf_object_p (bfd *abfd)
  4120. {
  4121. obj_attribute *attrs = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
  4122. obj_attribute *hwcaps = &attrs[Tag_GNU_Sparc_HWCAPS];
  4123. obj_attribute *hwcaps2 = &attrs[Tag_GNU_Sparc_HWCAPS2];
  4124. unsigned int v9c_hwcaps_mask = ELF_SPARC_HWCAP_ASI_BLK_INIT;
  4125. unsigned int v9d_hwcaps_mask = (ELF_SPARC_HWCAP_FMAF
  4126. | ELF_SPARC_HWCAP_VIS3
  4127. | ELF_SPARC_HWCAP_HPC);
  4128. unsigned int v9e_hwcaps_mask = (ELF_SPARC_HWCAP_AES
  4129. | ELF_SPARC_HWCAP_DES
  4130. | ELF_SPARC_HWCAP_KASUMI
  4131. | ELF_SPARC_HWCAP_CAMELLIA
  4132. | ELF_SPARC_HWCAP_MD5
  4133. | ELF_SPARC_HWCAP_SHA1
  4134. | ELF_SPARC_HWCAP_SHA256
  4135. | ELF_SPARC_HWCAP_SHA512
  4136. | ELF_SPARC_HWCAP_MPMUL
  4137. | ELF_SPARC_HWCAP_MONT
  4138. | ELF_SPARC_HWCAP_CRC32C
  4139. | ELF_SPARC_HWCAP_CBCOND
  4140. | ELF_SPARC_HWCAP_PAUSE);
  4141. unsigned int v9v_hwcaps_mask = (ELF_SPARC_HWCAP_FJFMAU
  4142. | ELF_SPARC_HWCAP_IMA);
  4143. unsigned int v9m_hwcaps2_mask = (ELF_SPARC_HWCAP2_SPARC5
  4144. | ELF_SPARC_HWCAP2_MWAIT
  4145. | ELF_SPARC_HWCAP2_XMPMUL
  4146. | ELF_SPARC_HWCAP2_XMONT);
  4147. unsigned int m8_hwcaps2_mask = (ELF_SPARC_HWCAP2_SPARC6
  4148. | ELF_SPARC_HWCAP2_ONADDSUB
  4149. | ELF_SPARC_HWCAP2_ONMUL
  4150. | ELF_SPARC_HWCAP2_ONDIV
  4151. | ELF_SPARC_HWCAP2_DICTUNP
  4152. | ELF_SPARC_HWCAP2_FPCMPSHL
  4153. | ELF_SPARC_HWCAP2_RLE
  4154. | ELF_SPARC_HWCAP2_SHA3);
  4155. if (ABI_64_P (abfd))
  4156. {
  4157. unsigned long mach = bfd_mach_sparc_v9;
  4158. if (hwcaps2->i & m8_hwcaps2_mask)
  4159. mach = bfd_mach_sparc_v9m8;
  4160. else if (hwcaps2->i & v9m_hwcaps2_mask)
  4161. mach = bfd_mach_sparc_v9m;
  4162. else if (hwcaps->i & v9v_hwcaps_mask)
  4163. mach = bfd_mach_sparc_v9v;
  4164. else if (hwcaps->i & v9e_hwcaps_mask)
  4165. mach = bfd_mach_sparc_v9e;
  4166. else if (hwcaps->i & v9d_hwcaps_mask)
  4167. mach = bfd_mach_sparc_v9d;
  4168. else if (hwcaps->i & v9c_hwcaps_mask)
  4169. mach = bfd_mach_sparc_v9c;
  4170. else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
  4171. mach = bfd_mach_sparc_v9b;
  4172. else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
  4173. mach = bfd_mach_sparc_v9a;
  4174. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
  4175. }
  4176. else
  4177. {
  4178. if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
  4179. {
  4180. if (hwcaps2->i & m8_hwcaps2_mask)
  4181. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4182. bfd_mach_sparc_v8plusm8);
  4183. else if (hwcaps2->i & v9m_hwcaps2_mask)
  4184. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4185. bfd_mach_sparc_v8plusm);
  4186. else if (hwcaps->i & v9v_hwcaps_mask)
  4187. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4188. bfd_mach_sparc_v8plusv);
  4189. else if (hwcaps->i & v9e_hwcaps_mask)
  4190. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4191. bfd_mach_sparc_v8pluse);
  4192. else if (hwcaps->i & v9d_hwcaps_mask)
  4193. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4194. bfd_mach_sparc_v8plusd);
  4195. else if (hwcaps->i & v9c_hwcaps_mask)
  4196. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4197. bfd_mach_sparc_v8plusc);
  4198. else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
  4199. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4200. bfd_mach_sparc_v8plusb);
  4201. else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
  4202. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4203. bfd_mach_sparc_v8plusa);
  4204. else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
  4205. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4206. bfd_mach_sparc_v8plus);
  4207. else
  4208. return false;
  4209. }
  4210. else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
  4211. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
  4212. bfd_mach_sparc_sparclite_le);
  4213. else
  4214. return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
  4215. }
  4216. }
  4217. /* Return address for Ith PLT stub in section PLT, for relocation REL
  4218. or (bfd_vma) -1 if it should not be included. */
  4219. bfd_vma
  4220. _bfd_sparc_elf_plt_sym_val (bfd_vma i, const asection *plt, const arelent *rel)
  4221. {
  4222. if (ABI_64_P (plt->owner))
  4223. {
  4224. bfd_vma j;
  4225. i += PLT64_HEADER_SIZE / PLT64_ENTRY_SIZE;
  4226. if (i < PLT64_LARGE_THRESHOLD)
  4227. return plt->vma + i * PLT64_ENTRY_SIZE;
  4228. j = (i - PLT64_LARGE_THRESHOLD) % 160;
  4229. i -= j;
  4230. return plt->vma + i * PLT64_ENTRY_SIZE + j * 4 * 6;
  4231. }
  4232. else
  4233. return rel->address;
  4234. }
  4235. /* Merge backend specific data from an object file to the output
  4236. object file when linking. */
  4237. bool
  4238. _bfd_sparc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  4239. {
  4240. bfd *obfd = info->output_bfd;
  4241. obj_attribute *in_attr, *in_attrs;
  4242. obj_attribute *out_attr, *out_attrs;
  4243. if (!elf_known_obj_attributes_proc (obfd)[0].i)
  4244. {
  4245. /* This is the first object. Copy the attributes. */
  4246. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  4247. /* Use the Tag_null value to indicate the attributes have been
  4248. initialized. */
  4249. elf_known_obj_attributes_proc (obfd)[0].i = 1;
  4250. return true;
  4251. }
  4252. in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
  4253. out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
  4254. in_attr = &in_attrs[Tag_GNU_Sparc_HWCAPS];
  4255. out_attr = &out_attrs[Tag_GNU_Sparc_HWCAPS];
  4256. out_attr->i |= in_attr->i;
  4257. out_attr->type = 1;
  4258. in_attr = &in_attrs[Tag_GNU_Sparc_HWCAPS2];
  4259. out_attr = &out_attrs[Tag_GNU_Sparc_HWCAPS2];
  4260. out_attr->i |= in_attr->i;
  4261. out_attr->type = 1;
  4262. /* Merge Tag_compatibility attributes and any common GNU ones. */
  4263. _bfd_elf_merge_object_attributes (ibfd, info);
  4264. return true;
  4265. }