elf32-cris.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136
  1. /* CRIS-specific support for 32-bit ELF.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Axis Communications AB.
  4. Written by Hans-Peter Nilsson, based on elf32-fr30.c
  5. PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
  6. This file is part of BFD, the Binary File Descriptor library.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. MA 02110-1301, USA. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "libbfd.h"
  22. #include "elf-bfd.h"
  23. #include "elf/cris.h"
  24. #include <limits.h>
  25. bfd_reloc_status_type
  26. cris_elf_pcrel_reloc (bfd *, arelent *, asymbol *, void *,
  27. asection *, bfd *, char **);
  28. static bool
  29. cris_elf_set_mach_from_flags (bfd *, unsigned long);
  30. /* Forward declarations. */
  31. static reloc_howto_type cris_elf_howto_table [] =
  32. {
  33. /* This reloc does nothing. */
  34. HOWTO (R_CRIS_NONE, /* type */
  35. 0, /* rightshift */
  36. 3, /* size (0 = byte, 1 = short, 2 = long) */
  37. 0, /* bitsize */
  38. false, /* pc_relative */
  39. 0, /* bitpos */
  40. complain_overflow_dont, /* complain_on_overflow */
  41. bfd_elf_generic_reloc, /* special_function */
  42. "R_CRIS_NONE", /* name */
  43. false, /* partial_inplace */
  44. 0, /* src_mask */
  45. 0, /* dst_mask */
  46. false), /* pcrel_offset */
  47. /* An 8 bit absolute relocation. */
  48. HOWTO (R_CRIS_8, /* type */
  49. 0, /* rightshift */
  50. 0, /* size (0 = byte, 1 = short, 2 = long) */
  51. 8, /* bitsize */
  52. false, /* pc_relative */
  53. 0, /* bitpos */
  54. complain_overflow_bitfield, /* complain_on_overflow */
  55. bfd_elf_generic_reloc, /* special_function */
  56. "R_CRIS_8", /* name */
  57. false, /* partial_inplace */
  58. 0x0000, /* src_mask */
  59. 0x00ff, /* dst_mask */
  60. false), /* pcrel_offset */
  61. /* A 16 bit absolute relocation. */
  62. HOWTO (R_CRIS_16, /* type */
  63. 0, /* rightshift */
  64. 1, /* size (0 = byte, 1 = short, 2 = long) */
  65. 16, /* bitsize */
  66. false, /* pc_relative */
  67. 0, /* bitpos */
  68. complain_overflow_bitfield, /* complain_on_overflow */
  69. bfd_elf_generic_reloc, /* special_function */
  70. "R_CRIS_16", /* name */
  71. false, /* partial_inplace */
  72. 0x00000000, /* src_mask */
  73. 0x0000ffff, /* dst_mask */
  74. false), /* pcrel_offset */
  75. /* A 32 bit absolute relocation. */
  76. HOWTO (R_CRIS_32, /* type */
  77. 0, /* rightshift */
  78. 2, /* size (0 = byte, 1 = short, 2 = long) */
  79. 32, /* bitsize */
  80. false, /* pc_relative */
  81. 0, /* bitpos */
  82. /* We don't want overflow complaints for 64-bit vma builds
  83. for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
  84. 32-bit ELF) where sym=0xc0001234.
  85. Don't do this for the PIC relocs, as we don't expect to
  86. see them with large offsets. */
  87. complain_overflow_dont, /* complain_on_overflow */
  88. bfd_elf_generic_reloc, /* special_function */
  89. "R_CRIS_32", /* name */
  90. false, /* partial_inplace */
  91. 0x00000000, /* src_mask */
  92. 0xffffffff, /* dst_mask */
  93. false), /* pcrel_offset */
  94. /* An 8 bit PC-relative relocation. */
  95. HOWTO (R_CRIS_8_PCREL, /* type */
  96. 0, /* rightshift */
  97. 0, /* size (0 = byte, 1 = short, 2 = long) */
  98. 8, /* bitsize */
  99. true, /* pc_relative */
  100. 0, /* bitpos */
  101. complain_overflow_bitfield, /* complain_on_overflow */
  102. cris_elf_pcrel_reloc, /* special_function */
  103. "R_CRIS_8_PCREL", /* name */
  104. false, /* partial_inplace */
  105. 0x0000, /* src_mask */
  106. 0x00ff, /* dst_mask */
  107. true), /* pcrel_offset */
  108. /* A 16 bit PC-relative relocation. */
  109. HOWTO (R_CRIS_16_PCREL, /* type */
  110. 0, /* rightshift */
  111. 1, /* size (0 = byte, 1 = short, 2 = long) */
  112. 16, /* bitsize */
  113. true, /* pc_relative */
  114. 0, /* bitpos */
  115. complain_overflow_bitfield, /* complain_on_overflow */
  116. cris_elf_pcrel_reloc, /* special_function */
  117. "R_CRIS_16_PCREL", /* name */
  118. false, /* partial_inplace */
  119. 0x00000000, /* src_mask */
  120. 0x0000ffff, /* dst_mask */
  121. true), /* pcrel_offset */
  122. /* A 32 bit PC-relative relocation. */
  123. HOWTO (R_CRIS_32_PCREL, /* type */
  124. 0, /* rightshift */
  125. 2, /* size (0 = byte, 1 = short, 2 = long) */
  126. 32, /* bitsize */
  127. true, /* pc_relative */
  128. 0, /* bitpos */
  129. complain_overflow_bitfield, /* complain_on_overflow */
  130. cris_elf_pcrel_reloc, /* special_function */
  131. "R_CRIS_32_PCREL", /* name */
  132. false, /* partial_inplace */
  133. 0x00000000, /* src_mask */
  134. 0xffffffff, /* dst_mask */
  135. true), /* pcrel_offset */
  136. /* GNU extension to record C++ vtable hierarchy. */
  137. HOWTO (R_CRIS_GNU_VTINHERIT, /* type */
  138. 0, /* rightshift */
  139. 2, /* size (0 = byte, 1 = short, 2 = long) */
  140. 0, /* bitsize */
  141. false, /* pc_relative */
  142. 0, /* bitpos */
  143. complain_overflow_dont, /* complain_on_overflow */
  144. NULL, /* special_function */
  145. "R_CRIS_GNU_VTINHERIT", /* name */
  146. false, /* partial_inplace */
  147. 0, /* src_mask */
  148. 0, /* dst_mask */
  149. false), /* pcrel_offset */
  150. /* GNU extension to record C++ vtable member usage. */
  151. HOWTO (R_CRIS_GNU_VTENTRY, /* type */
  152. 0, /* rightshift */
  153. 2, /* size (0 = byte, 1 = short, 2 = long) */
  154. 0, /* bitsize */
  155. false, /* pc_relative */
  156. 0, /* bitpos */
  157. complain_overflow_dont, /* complain_on_overflow */
  158. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  159. "R_CRIS_GNU_VTENTRY", /* name */
  160. false, /* partial_inplace */
  161. 0, /* src_mask */
  162. 0, /* dst_mask */
  163. false), /* pcrel_offset */
  164. /* This is used only by the dynamic linker. The symbol should exist
  165. both in the object being run and in some shared library. The
  166. dynamic linker copies the data addressed by the symbol from the
  167. shared library into the object, because the object being
  168. run has to have the data at some particular address. */
  169. HOWTO (R_CRIS_COPY, /* type */
  170. 0, /* rightshift */
  171. 2, /* size (0 = byte, 1 = short, 2 = long) */
  172. 32, /* bitsize */
  173. false, /* pc_relative */
  174. 0, /* bitpos */
  175. complain_overflow_bitfield, /* complain_on_overflow */
  176. bfd_elf_generic_reloc, /* special_function */
  177. "R_CRIS_COPY", /* name */
  178. false, /* partial_inplace */
  179. 0, /* src_mask */
  180. 0, /* dst_mask */
  181. false), /* pcrel_offset */
  182. /* Like R_CRIS_32, but used when setting global offset table entries. */
  183. HOWTO (R_CRIS_GLOB_DAT, /* type */
  184. 0, /* rightshift */
  185. 2, /* size (0 = byte, 1 = short, 2 = long) */
  186. 32, /* bitsize */
  187. false, /* pc_relative */
  188. 0, /* bitpos */
  189. complain_overflow_bitfield, /* complain_on_overflow */
  190. bfd_elf_generic_reloc, /* special_function */
  191. "R_CRIS_GLOB_DAT", /* name */
  192. false, /* partial_inplace */
  193. 0, /* src_mask */
  194. 0xffffffff, /* dst_mask */
  195. false), /* pcrel_offset */
  196. /* Marks a procedure linkage table entry for a symbol. */
  197. HOWTO (R_CRIS_JUMP_SLOT, /* type */
  198. 0, /* rightshift */
  199. 2, /* size (0 = byte, 1 = short, 2 = long) */
  200. 32, /* bitsize */
  201. false, /* pc_relative */
  202. 0, /* bitpos */
  203. complain_overflow_bitfield, /* complain_on_overflow */
  204. bfd_elf_generic_reloc, /* special_function */
  205. "R_CRIS_JUMP_SLOT", /* name */
  206. false, /* partial_inplace */
  207. 0, /* src_mask */
  208. 0, /* dst_mask */
  209. false), /* pcrel_offset */
  210. /* Used only by the dynamic linker. When the object is run, this
  211. longword is set to the load address of the object, plus the
  212. addend. */
  213. HOWTO (R_CRIS_RELATIVE, /* type */
  214. 0, /* rightshift */
  215. 2, /* size (0 = byte, 1 = short, 2 = long) */
  216. 32, /* bitsize */
  217. false, /* pc_relative */
  218. 0, /* bitpos */
  219. complain_overflow_bitfield, /* complain_on_overflow */
  220. bfd_elf_generic_reloc, /* special_function */
  221. "R_CRIS_RELATIVE", /* name */
  222. false, /* partial_inplace */
  223. 0, /* src_mask */
  224. 0xffffffff, /* dst_mask */
  225. false), /* pcrel_offset */
  226. /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */
  227. HOWTO (R_CRIS_16_GOT, /* type */
  228. 0, /* rightshift */
  229. 1, /* size (0 = byte, 1 = short, 2 = long) */
  230. 16, /* bitsize */
  231. false, /* pc_relative */
  232. 0, /* bitpos */
  233. complain_overflow_bitfield, /* complain_on_overflow */
  234. bfd_elf_generic_reloc, /* special_function */
  235. "R_CRIS_16_GOT", /* name */
  236. false, /* partial_inplace */
  237. 0, /* src_mask */
  238. 0xffff, /* dst_mask */
  239. false), /* pcrel_offset */
  240. HOWTO (R_CRIS_32_GOT, /* type */
  241. 0, /* rightshift */
  242. 2, /* size (0 = byte, 1 = short, 2 = long) */
  243. 32, /* bitsize */
  244. false, /* pc_relative */
  245. 0, /* bitpos */
  246. complain_overflow_bitfield, /* complain_on_overflow */
  247. bfd_elf_generic_reloc, /* special_function */
  248. "R_CRIS_32_GOT", /* name */
  249. false, /* partial_inplace */
  250. 0, /* src_mask */
  251. 0xffffffff, /* dst_mask */
  252. false), /* pcrel_offset */
  253. /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
  254. the GOT table for the symbol. */
  255. HOWTO (R_CRIS_16_GOTPLT, /* type */
  256. 0, /* rightshift */
  257. 1, /* size (0 = byte, 1 = short, 2 = long) */
  258. 16, /* bitsize */
  259. false, /* pc_relative */
  260. 0, /* bitpos */
  261. complain_overflow_bitfield, /* complain_on_overflow */
  262. bfd_elf_generic_reloc, /* special_function */
  263. "R_CRIS_16_GOTPLT", /* name */
  264. false, /* partial_inplace */
  265. 0, /* src_mask */
  266. 0xffff, /* dst_mask */
  267. false), /* pcrel_offset */
  268. HOWTO (R_CRIS_32_GOTPLT, /* type */
  269. 0, /* rightshift */
  270. 2, /* size (0 = byte, 1 = short, 2 = long) */
  271. 32, /* bitsize */
  272. false, /* pc_relative */
  273. 0, /* bitpos */
  274. complain_overflow_bitfield, /* complain_on_overflow */
  275. bfd_elf_generic_reloc, /* special_function */
  276. "R_CRIS_32_GOTPLT", /* name */
  277. false, /* partial_inplace */
  278. 0, /* src_mask */
  279. 0xffffffff, /* dst_mask */
  280. false), /* pcrel_offset */
  281. /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
  282. be necessary. */
  283. HOWTO (R_CRIS_32_GOTREL, /* type */
  284. 0, /* rightshift */
  285. 2, /* size (0 = byte, 1 = short, 2 = long) */
  286. 32, /* bitsize */
  287. false, /* pc_relative */
  288. 0, /* bitpos */
  289. complain_overflow_bitfield, /* complain_on_overflow */
  290. bfd_elf_generic_reloc, /* special_function */
  291. "R_CRIS_32_GOTREL", /* name */
  292. false, /* partial_inplace */
  293. 0, /* src_mask */
  294. 0xffffffff, /* dst_mask */
  295. false), /* pcrel_offset */
  296. /* A 32-bit offset from GOT to entry for this symbol in PLT and request
  297. to create PLT entry for symbol. */
  298. HOWTO (R_CRIS_32_PLT_GOTREL, /* type */
  299. 0, /* rightshift */
  300. 2, /* size (0 = byte, 1 = short, 2 = long) */
  301. 32, /* bitsize */
  302. false, /* pc_relative */
  303. 0, /* bitpos */
  304. complain_overflow_bitfield, /* complain_on_overflow */
  305. bfd_elf_generic_reloc, /* special_function */
  306. "R_CRIS_32_PLT_GOTREL", /* name */
  307. false, /* partial_inplace */
  308. 0, /* src_mask */
  309. 0xffffffff, /* dst_mask */
  310. false), /* pcrel_offset */
  311. /* A 32-bit offset from PC (location after the relocation) + addend to
  312. entry for this symbol in PLT and request to create PLT entry for
  313. symbol. */
  314. HOWTO (R_CRIS_32_PLT_PCREL, /* type */
  315. 0, /* rightshift */
  316. 2, /* size (0 = byte, 1 = short, 2 = long) */
  317. 32, /* bitsize */
  318. true, /* pc_relative */
  319. 0, /* bitpos */
  320. complain_overflow_bitfield, /* complain_on_overflow */
  321. cris_elf_pcrel_reloc, /* special_function */
  322. "R_CRIS_32_PLT_PCREL", /* name */
  323. false, /* partial_inplace */
  324. 0, /* src_mask */
  325. 0xffffffff, /* dst_mask */
  326. true), /* pcrel_offset */
  327. /* We don't handle these in any special manner and cross-format
  328. linking is not supported; just recognize them enough to pass them
  329. around. FIXME: do the same for most PIC relocs and add sanity
  330. tests to actually refuse gracefully to handle these and PIC
  331. relocs for cross-format linking. */
  332. #define TLSHOWTO32(name) \
  333. HOWTO (name, 0, 2, 32, false, 0, complain_overflow_bitfield, \
  334. bfd_elf_generic_reloc, #name, false, 0, 0xffffffff, false)
  335. #define TLSHOWTO16X(name, X) \
  336. HOWTO (name, 0, 1, 16, false, 0, complain_overflow_ ## X, \
  337. bfd_elf_generic_reloc, #name, false, 0, 0xffff, false)
  338. #define TLSHOWTO16(name) TLSHOWTO16X(name, unsigned)
  339. #define TLSHOWTO16S(name) TLSHOWTO16X(name, signed)
  340. TLSHOWTO32 (R_CRIS_32_GOT_GD),
  341. TLSHOWTO16 (R_CRIS_16_GOT_GD),
  342. TLSHOWTO32 (R_CRIS_32_GD),
  343. TLSHOWTO32 (R_CRIS_DTP),
  344. TLSHOWTO32 (R_CRIS_32_DTPREL),
  345. TLSHOWTO16S (R_CRIS_16_DTPREL),
  346. TLSHOWTO32 (R_CRIS_32_GOT_TPREL),
  347. TLSHOWTO16S (R_CRIS_16_GOT_TPREL),
  348. TLSHOWTO32 (R_CRIS_32_TPREL),
  349. TLSHOWTO16S (R_CRIS_16_TPREL),
  350. TLSHOWTO32 (R_CRIS_DTPMOD),
  351. TLSHOWTO32 (R_CRIS_32_IE)
  352. };
  353. /* Map BFD reloc types to CRIS ELF reloc types. */
  354. struct cris_reloc_map
  355. {
  356. bfd_reloc_code_real_type bfd_reloc_val;
  357. unsigned int cris_reloc_val;
  358. };
  359. static const struct cris_reloc_map cris_reloc_map [] =
  360. {
  361. { BFD_RELOC_NONE, R_CRIS_NONE },
  362. { BFD_RELOC_8, R_CRIS_8 },
  363. { BFD_RELOC_16, R_CRIS_16 },
  364. { BFD_RELOC_32, R_CRIS_32 },
  365. { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL },
  366. { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL },
  367. { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL },
  368. { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
  369. { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
  370. { BFD_RELOC_CRIS_COPY, R_CRIS_COPY },
  371. { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT },
  372. { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT },
  373. { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE },
  374. { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT },
  375. { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT },
  376. { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
  377. { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
  378. { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
  379. { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
  380. { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL },
  381. { BFD_RELOC_CRIS_32_GOT_GD, R_CRIS_32_GOT_GD },
  382. { BFD_RELOC_CRIS_16_GOT_GD, R_CRIS_16_GOT_GD },
  383. { BFD_RELOC_CRIS_32_GD, R_CRIS_32_GD },
  384. { BFD_RELOC_CRIS_DTP, R_CRIS_DTP },
  385. { BFD_RELOC_CRIS_32_DTPREL, R_CRIS_32_DTPREL },
  386. { BFD_RELOC_CRIS_16_DTPREL, R_CRIS_16_DTPREL },
  387. { BFD_RELOC_CRIS_32_GOT_TPREL, R_CRIS_32_GOT_TPREL },
  388. { BFD_RELOC_CRIS_16_GOT_TPREL, R_CRIS_16_GOT_TPREL },
  389. { BFD_RELOC_CRIS_32_TPREL, R_CRIS_32_TPREL },
  390. { BFD_RELOC_CRIS_16_TPREL, R_CRIS_16_TPREL },
  391. { BFD_RELOC_CRIS_DTPMOD, R_CRIS_DTPMOD },
  392. { BFD_RELOC_CRIS_32_IE, R_CRIS_32_IE }
  393. };
  394. static reloc_howto_type *
  395. cris_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  396. bfd_reloc_code_real_type code)
  397. {
  398. unsigned int i;
  399. for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
  400. if (cris_reloc_map [i].bfd_reloc_val == code)
  401. return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
  402. return NULL;
  403. }
  404. static reloc_howto_type *
  405. cris_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
  406. {
  407. unsigned int i;
  408. for (i = 0;
  409. i < sizeof (cris_elf_howto_table) / sizeof (cris_elf_howto_table[0]);
  410. i++)
  411. if (cris_elf_howto_table[i].name != NULL
  412. && strcasecmp (cris_elf_howto_table[i].name, r_name) == 0)
  413. return &cris_elf_howto_table[i];
  414. return NULL;
  415. }
  416. /* Set the howto pointer for an CRIS ELF reloc. */
  417. static bool
  418. cris_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
  419. arelent * cache_ptr,
  420. Elf_Internal_Rela * dst)
  421. {
  422. enum elf_cris_reloc_type r_type;
  423. r_type = ELF32_R_TYPE (dst->r_info);
  424. if (r_type >= R_CRIS_max)
  425. {
  426. /* xgettext:c-format */
  427. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  428. abfd, r_type);
  429. bfd_set_error (bfd_error_bad_value);
  430. return false;
  431. }
  432. cache_ptr->howto = & cris_elf_howto_table [r_type];
  433. return true;
  434. }
  435. bfd_reloc_status_type
  436. cris_elf_pcrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  437. arelent *reloc_entry,
  438. asymbol *symbol,
  439. void * data ATTRIBUTE_UNUSED,
  440. asection *input_section,
  441. bfd *output_bfd,
  442. char **error_message ATTRIBUTE_UNUSED)
  443. {
  444. /* By default (using only bfd_elf_generic_reloc when linking to
  445. non-ELF formats) PC-relative relocs are relative to the beginning
  446. of the reloc. CRIS PC-relative relocs are relative to the position
  447. *after* the reloc because that's what pre-CRISv32 PC points to
  448. after reading an insn field with that reloc. (For CRISv32, PC is
  449. actually relative to the start of the insn, but we keep the old
  450. definition.) Still, we use as much generic machinery as we can.
  451. Only adjust when doing a final link. */
  452. if (output_bfd == (bfd *) NULL)
  453. reloc_entry->addend -= 1 << reloc_entry->howto->size;
  454. return
  455. bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  456. input_section, output_bfd, error_message);
  457. }
  458. /* Support for core dump NOTE sections.
  459. The slightly unintuitive code layout is an attempt to keep at least
  460. some similarities with other ports, hoping to simplify general
  461. changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart. */
  462. static bool
  463. cris_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  464. {
  465. int offset;
  466. size_t size;
  467. if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
  468. switch (note->descsz)
  469. {
  470. default:
  471. return false;
  472. case 202: /* Linux/CRISv32 */
  473. /* pr_cursig */
  474. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  475. /* pr_pid */
  476. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
  477. /* pr_reg */
  478. offset = 70;
  479. size = 128;
  480. break;
  481. }
  482. else
  483. switch (note->descsz)
  484. {
  485. default:
  486. return false;
  487. case 214: /* Linux/CRIS */
  488. /* pr_cursig */
  489. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  490. /* pr_pid */
  491. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
  492. /* pr_reg */
  493. offset = 70;
  494. size = 140;
  495. break;
  496. }
  497. /* Make a ".reg/999" section. */
  498. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  499. size, note->descpos + offset);
  500. }
  501. static bool
  502. cris_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  503. {
  504. if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
  505. switch (note->descsz)
  506. {
  507. default:
  508. return false;
  509. case 124: /* Linux/CRISv32 elf_prpsinfo */
  510. elf_tdata (abfd)->core->program
  511. = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  512. elf_tdata (abfd)->core->command
  513. = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  514. }
  515. else
  516. switch (note->descsz)
  517. {
  518. default:
  519. return false;
  520. case 124: /* Linux/CRIS elf_prpsinfo */
  521. elf_tdata (abfd)->core->program
  522. = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  523. elf_tdata (abfd)->core->command
  524. = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  525. }
  526. /* Note that for some reason, a spurious space is tacked
  527. onto the end of the args in some (at least one anyway)
  528. implementations, so strip it off if it exists. */
  529. {
  530. char *command = elf_tdata (abfd)->core->command;
  531. int n = strlen (command);
  532. if (0 < n && command[n - 1] == ' ')
  533. command[n - 1] = '\0';
  534. }
  535. return true;
  536. }
  537. /* The name of the dynamic interpreter. This is put in the .interp
  538. section. */
  539. #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
  540. /* The size in bytes of an entry in the procedure linkage table. */
  541. #define PLT_ENTRY_SIZE 20
  542. #define PLT_ENTRY_SIZE_V32 26
  543. /* The first entry in an absolute procedure linkage table looks like this. */
  544. static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
  545. {
  546. 0xfc, 0xe1,
  547. 0x7e, 0x7e, /* push mof. */
  548. 0x7f, 0x0d, /* (dip [pc+]) */
  549. 0, 0, 0, 0, /* Replaced with address of .got + 4. */
  550. 0x30, 0x7a, /* move [...],mof */
  551. 0x7f, 0x0d, /* (dip [pc+]) */
  552. 0, 0, 0, 0, /* Replaced with address of .got + 8. */
  553. 0x30, 0x09 /* jump [...] */
  554. };
  555. static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
  556. {
  557. 0x84, 0xe2, /* subq 4,$sp */
  558. 0x6f, 0xfe, /* move.d 0,$acr */
  559. 0, 0, 0, 0, /* Replaced by address of .got + 4. */
  560. 0x7e, 0x7a, /* move $mof,[$sp] */
  561. 0x3f, 0x7a, /* move [$acr],$mof */
  562. 0x04, 0xf2, /* addq 4,acr */
  563. 0x6f, 0xfa, /* move.d [$acr],$acr */
  564. 0xbf, 0x09, /* jump $acr */
  565. 0xb0, 0x05, /* nop */
  566. 0, 0 /* Pad out to 26 bytes. */
  567. };
  568. /* Subsequent entries in an absolute procedure linkage table look like
  569. this. */
  570. static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
  571. {
  572. 0x7f, 0x0d, /* (dip [pc+]) */
  573. 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
  574. 0x30, 0x09, /* jump [...] */
  575. 0x3f, 0x7e, /* move [pc+],mof */
  576. 0, 0, 0, 0, /* Replaced with offset into relocation table. */
  577. 0x2f, 0xfe, /* add.d [pc+],pc */
  578. 0xec, 0xff,
  579. 0xff, 0xff /* Replaced with offset to start of .plt. */
  580. };
  581. static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
  582. {
  583. 0x6f, 0xfe, /* move.d 0,$acr */
  584. 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */
  585. 0x6f, 0xfa, /* move.d [$acr],$acr */
  586. 0xbf, 0x09, /* jump $acr */
  587. 0xb0, 0x05, /* nop */
  588. 0x3f, 0x7e, /* move 0,mof */
  589. 0, 0, 0, 0, /* Replaced with offset into relocation table. */
  590. 0xbf, 0x0e, /* ba start_of_plt0_entry */
  591. 0, 0, 0, 0, /* Replaced with offset to plt0 entry. */
  592. 0xb0, 0x05 /* nop */
  593. };
  594. /* The first entry in a PIC procedure linkage table looks like this. */
  595. static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
  596. {
  597. 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
  598. 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
  599. 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
  600. 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */
  601. };
  602. static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
  603. {
  604. 0x84, 0xe2, /* subq 4,$sp */
  605. 0x04, 0x01, /* addoq 4,$r0,$acr */
  606. 0x7e, 0x7a, /* move $mof,[$sp] */
  607. 0x3f, 0x7a, /* move [$acr],$mof */
  608. 0x04, 0xf2, /* addq 4,$acr */
  609. 0x6f, 0xfa, /* move.d [$acr],$acr */
  610. 0xbf, 0x09, /* jump $acr */
  611. 0xb0, 0x05, /* nop */
  612. 0, 0, /* Pad out to 26 bytes. */
  613. 0, 0, 0, 0,
  614. 0, 0, 0, 0
  615. };
  616. /* Subsequent entries in a PIC procedure linkage table look like this. */
  617. static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
  618. {
  619. 0x6f, 0x0d, /* (bdap [pc+].d,r0) */
  620. 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
  621. 0x30, 0x09, /* jump [...] */
  622. 0x3f, 0x7e, /* move [pc+],mof */
  623. 0, 0, 0, 0, /* Replaced with offset into relocation table. */
  624. 0x2f, 0xfe, /* add.d [pc+],pc */
  625. 0xec, 0xff, /* Replaced with offset to start of .plt. */
  626. 0xff, 0xff
  627. };
  628. static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
  629. {
  630. 0x6f, 0x0d, /* addo.d 0,$r0,$acr */
  631. 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */
  632. 0x6f, 0xfa, /* move.d [$acr],$acr */
  633. 0xbf, 0x09, /* jump $acr */
  634. 0xb0, 0x05, /* nop */
  635. 0x3f, 0x7e, /* move relocoffs,$mof */
  636. 0, 0, 0, 0, /* Replaced with offset into relocation table. */
  637. 0xbf, 0x0e, /* ba start_of_plt */
  638. 0, 0, 0, 0, /* Replaced with offset to start of .plt. */
  639. 0xb0, 0x05 /* nop */
  640. };
  641. /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
  642. (and most other PIC/shlib stuff). Check that we don't drift away
  643. without reason.
  644. The CRIS linker, like the m68k and i386 linkers (and probably the rest
  645. too) needs to keep track of the number of relocs that it decides to
  646. copy in check_relocs for each symbol. This is so that it can discard
  647. PC relative relocs if it doesn't need them when linking with
  648. -Bsymbolic. We store the information in a field extending the regular
  649. ELF linker hash table. */
  650. /* This structure keeps track of the number of PC relative relocs we have
  651. copied for a given symbol. */
  652. struct elf_cris_pcrel_relocs_copied
  653. {
  654. /* Next section. */
  655. struct elf_cris_pcrel_relocs_copied *next;
  656. /* A section in dynobj. */
  657. asection *section;
  658. /* Number of relocs copied in this section. */
  659. bfd_size_type count;
  660. /* Example of reloc being copied, for message. */
  661. enum elf_cris_reloc_type r_type;
  662. };
  663. /* CRIS ELF linker hash entry. */
  664. struct elf_cris_link_hash_entry
  665. {
  666. struct elf_link_hash_entry root;
  667. /* Number of PC relative relocs copied for this symbol. */
  668. struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
  669. /* The GOTPLT references are CRIS-specific; the goal is to avoid having
  670. both a general GOT and a PLT-specific GOT entry for the same symbol,
  671. when it is referenced both as a function and as a function pointer.
  672. Number of GOTPLT references for a function. */
  673. bfd_signed_vma gotplt_refcount;
  674. /* Actual GOTPLT index for this symbol, if applicable, or zero if not
  675. (zero is never used as an index). FIXME: We should be able to fold
  676. this with gotplt_refcount in a union, like the got and plt unions in
  677. elf_link_hash_entry. */
  678. bfd_size_type gotplt_offset;
  679. /* The root.got.refcount is the sum of the regular reference counts
  680. (this) and those members below. We have to keep a separate count
  681. to track when we've found the first (or last) reference to a
  682. regular got entry. The offset is in root.got.offset. */
  683. bfd_signed_vma reg_got_refcount;
  684. /* Similar to the above, the number of reloc references to this
  685. symbols that need a R_CRIS_32_TPREL slot. The offset is in
  686. root.got.offset, because this and .dtp_refcount can't validly
  687. happen when there's also a regular GOT entry; that's invalid
  688. input for which an error is emitted. */
  689. bfd_signed_vma tprel_refcount;
  690. /* Similar to the above, the number of reloc references to this
  691. symbols that need a R_CRIS_DTP slot. The offset is in
  692. root.got.offset; plus 4 if .tprel_refcount > 0. */
  693. bfd_signed_vma dtp_refcount;
  694. };
  695. static bool
  696. elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *,
  697. void * );
  698. static bool
  699. elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *,
  700. void *);
  701. /* The local_got_refcounts and local_got_offsets are a multiple of
  702. LSNUM in size, namely LGOT_ALLOC_NELTS_FOR(LSNUM) (plus one for the
  703. refcount for GOT itself, see code), with the summary / group offset
  704. for local symbols located at offset N, reference counts for
  705. ordinary (address) relocs at offset N + LSNUM, for R_CRIS_DTP
  706. relocs at offset N + 2*LSNUM, and for R_CRIS_32_TPREL relocs at N +
  707. 3*LSNUM. */
  708. #define LGOT_REG_NDX(x) ((x) + symtab_hdr->sh_info)
  709. #define LGOT_DTP_NDX(x) ((x) + 2 * symtab_hdr->sh_info)
  710. #define LGOT_TPREL_NDX(x) ((x) + 3 * symtab_hdr->sh_info)
  711. #define LGOT_ALLOC_NELTS_FOR(x) ((x) * 4)
  712. /* CRIS ELF linker hash table. */
  713. struct elf_cris_link_hash_table
  714. {
  715. struct elf_link_hash_table root;
  716. /* We can't use the PLT offset and calculate to get the GOTPLT offset,
  717. since we try and avoid creating GOTPLT:s when there's already a GOT.
  718. Instead, we keep and update the next available index here. */
  719. bfd_size_type next_gotplt_entry;
  720. /* The number of R_CRIS_32_DTPREL and R_CRIS_16_DTPREL that have
  721. been seen for any input; if != 0, then the constant-offset
  722. R_CRIS_DTPMOD is needed for this DSO/executable. This turns
  723. negative at relocation, so that we don't need an extra flag for
  724. when the reloc is output. */
  725. bfd_signed_vma dtpmod_refcount;
  726. };
  727. /* Traverse a CRIS ELF linker hash table. */
  728. #define elf_cris_link_hash_traverse(table, func, info) \
  729. (elf_link_hash_traverse \
  730. (&(table)->root, \
  731. (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
  732. (info)))
  733. /* Get the CRIS ELF linker hash table from a link_info structure. */
  734. #define elf_cris_hash_table(p) \
  735. ((is_elf_hash_table ((p)->hash) \
  736. && elf_hash_table_id (elf_hash_table (p)) == CRIS_ELF_DATA) \
  737. ? (struct elf_cris_link_hash_table *) (p)->hash : NULL)
  738. /* Get the CRIS ELF linker hash entry from a regular hash entry (the
  739. "parent class"). The .root reference is just a simple type
  740. check on the argument. */
  741. #define elf_cris_hash_entry(p) \
  742. ((struct elf_cris_link_hash_entry *) (&(p)->root))
  743. /* Create an entry in a CRIS ELF linker hash table. */
  744. static struct bfd_hash_entry *
  745. elf_cris_link_hash_newfunc (struct bfd_hash_entry *entry,
  746. struct bfd_hash_table *table,
  747. const char *string)
  748. {
  749. struct elf_cris_link_hash_entry *ret =
  750. (struct elf_cris_link_hash_entry *) entry;
  751. /* Allocate the structure if it has not already been allocated by a
  752. subclass. */
  753. if (ret == (struct elf_cris_link_hash_entry *) NULL)
  754. ret = ((struct elf_cris_link_hash_entry *)
  755. bfd_hash_allocate (table,
  756. sizeof (struct elf_cris_link_hash_entry)));
  757. if (ret == (struct elf_cris_link_hash_entry *) NULL)
  758. return (struct bfd_hash_entry *) ret;
  759. /* Call the allocation method of the superclass. */
  760. ret = ((struct elf_cris_link_hash_entry *)
  761. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  762. table, string));
  763. if (ret != (struct elf_cris_link_hash_entry *) NULL)
  764. {
  765. ret->pcrel_relocs_copied = NULL;
  766. ret->gotplt_refcount = 0;
  767. ret->gotplt_offset = 0;
  768. ret->dtp_refcount = 0;
  769. ret->tprel_refcount = 0;
  770. ret->reg_got_refcount = 0;
  771. }
  772. return (struct bfd_hash_entry *) ret;
  773. }
  774. /* Create a CRIS ELF linker hash table. */
  775. static struct bfd_link_hash_table *
  776. elf_cris_link_hash_table_create (bfd *abfd)
  777. {
  778. struct elf_cris_link_hash_table *ret;
  779. size_t amt = sizeof (struct elf_cris_link_hash_table);
  780. ret = ((struct elf_cris_link_hash_table *) bfd_zmalloc (amt));
  781. if (ret == (struct elf_cris_link_hash_table *) NULL)
  782. return NULL;
  783. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  784. elf_cris_link_hash_newfunc,
  785. sizeof (struct elf_cris_link_hash_entry),
  786. CRIS_ELF_DATA))
  787. {
  788. free (ret);
  789. return NULL;
  790. }
  791. /* Initialize to skip over the first three entries in the gotplt; they
  792. are used for run-time symbol evaluation. */
  793. ret->next_gotplt_entry = 12;
  794. return &ret->root.root;
  795. }
  796. /* Perform a single relocation. By default we use the standard BFD
  797. routines, with a few tweaks. */
  798. static bfd_reloc_status_type
  799. cris_final_link_relocate (reloc_howto_type * howto,
  800. bfd * input_bfd,
  801. asection * input_section,
  802. bfd_byte * contents,
  803. Elf_Internal_Rela * rel,
  804. bfd_vma relocation)
  805. {
  806. bfd_reloc_status_type r;
  807. enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
  808. /* PC-relative relocations are relative to the position *after*
  809. the reloc. Note that for R_CRIS_8_PCREL the adjustment is
  810. not a single byte, since PC must be 16-bit-aligned. */
  811. switch (r_type)
  812. {
  813. /* Check that the 16-bit GOT relocs are positive. */
  814. case R_CRIS_16_GOTPLT:
  815. case R_CRIS_16_GOT:
  816. if ((bfd_signed_vma) relocation < 0)
  817. return bfd_reloc_overflow;
  818. break;
  819. case R_CRIS_32_PLT_PCREL:
  820. case R_CRIS_32_PCREL:
  821. relocation -= 2;
  822. /* Fall through. */
  823. case R_CRIS_8_PCREL:
  824. case R_CRIS_16_PCREL:
  825. relocation -= 2;
  826. break;
  827. default:
  828. break;
  829. }
  830. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  831. contents, rel->r_offset,
  832. relocation, rel->r_addend);
  833. return r;
  834. }
  835. /* The number of errors left before we stop outputting reloc-specific
  836. explanatory messages. By coincidence, this works nicely together
  837. with the default number of messages you'll get from LD about
  838. "relocation truncated to fit" messages before you get an
  839. "additional relocation overflows omitted from the output". */
  840. static int additional_relocation_error_msg_count = 10;
  841. /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was
  842. copied, for further comments. */
  843. static int
  844. cris_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
  845. struct bfd_link_info *info,
  846. bfd *input_bfd,
  847. asection *input_section,
  848. bfd_byte *contents,
  849. Elf_Internal_Rela *relocs,
  850. Elf_Internal_Sym *local_syms,
  851. asection **local_sections)
  852. {
  853. struct elf_cris_link_hash_table * htab;
  854. bfd *dynobj;
  855. Elf_Internal_Shdr *symtab_hdr;
  856. struct elf_link_hash_entry **sym_hashes;
  857. bfd_vma *local_got_offsets;
  858. asection *sgot;
  859. asection *splt;
  860. asection *sreloc;
  861. Elf_Internal_Rela *rel;
  862. Elf_Internal_Rela *relend;
  863. asection *srelgot;
  864. htab = elf_cris_hash_table (info);
  865. if (htab == NULL)
  866. return false;
  867. dynobj = htab->root.dynobj;
  868. local_got_offsets = elf_local_got_offsets (input_bfd);
  869. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  870. sym_hashes = elf_sym_hashes (input_bfd);
  871. relend = relocs + input_section->reloc_count;
  872. sgot = NULL;
  873. splt = NULL;
  874. sreloc = NULL;
  875. srelgot = NULL;
  876. if (dynobj != NULL)
  877. {
  878. splt = htab->root.splt;
  879. sgot = htab->root.sgot;
  880. }
  881. for (rel = relocs; rel < relend; rel ++)
  882. {
  883. reloc_howto_type *howto;
  884. unsigned long r_symndx;
  885. Elf_Internal_Sym *sym;
  886. asection *sec;
  887. struct elf_link_hash_entry *h;
  888. bfd_vma relocation;
  889. bfd_reloc_status_type r;
  890. const char *symname = NULL;
  891. enum elf_cris_reloc_type r_type;
  892. bool resolved_to_zero;
  893. r_type = ELF32_R_TYPE (rel->r_info);
  894. if ( r_type == R_CRIS_GNU_VTINHERIT
  895. || r_type == R_CRIS_GNU_VTENTRY)
  896. continue;
  897. r_symndx = ELF32_R_SYM (rel->r_info);
  898. howto = cris_elf_howto_table + r_type;
  899. h = NULL;
  900. sym = NULL;
  901. sec = NULL;
  902. if (r_symndx < symtab_hdr->sh_info)
  903. {
  904. sym = local_syms + r_symndx;
  905. sec = local_sections [r_symndx];
  906. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  907. symname = (bfd_elf_string_from_elf_section
  908. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  909. if (symname == NULL)
  910. symname = bfd_section_name (sec);
  911. }
  912. else
  913. {
  914. bool warned, ignored;
  915. bool unresolved_reloc;
  916. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  917. r_symndx, symtab_hdr, sym_hashes,
  918. h, sec, relocation,
  919. unresolved_reloc, warned, ignored);
  920. symname = h->root.root.string;
  921. if (unresolved_reloc
  922. /* Perhaps we should detect the cases that
  923. sec->output_section is expected to be NULL like i386 and
  924. m68k, but apparently (and according to elfxx-ia64.c) all
  925. valid cases are where the symbol is defined in a shared
  926. object which we link dynamically against. This includes
  927. PLT relocs for which we've created a PLT entry and other
  928. relocs for which we're prepared to create dynamic
  929. relocations.
  930. For now, new situations cause us to just err when
  931. sec->output_offset is NULL but the object with the symbol
  932. is *not* dynamically linked against. Thus this will
  933. automatically remind us so we can see if there are other
  934. valid cases we need to revisit. */
  935. && (sec->owner->flags & DYNAMIC) != 0)
  936. relocation = 0;
  937. else if (h->root.type == bfd_link_hash_defined
  938. || h->root.type == bfd_link_hash_defweak)
  939. {
  940. /* Here follow the cases where the relocation value must
  941. be zero (or when further handling is simplified when
  942. zero). I can't claim to understand the various
  943. conditions and they weren't described in the files
  944. where I copied them from (elf32-m68k.c and
  945. elf32-i386.c), but let's mention examples of where
  946. they happen. FIXME: Perhaps define and use a
  947. dynamic_symbol_p function like ia64.
  948. - When creating a shared library, we can have an
  949. ordinary relocation for a symbol defined in a shared
  950. library (perhaps the one we create). We then make
  951. the relocation value zero, as the value seen now will
  952. be added into the relocation addend in this shared
  953. library, but must be handled only at dynamic-link
  954. time. FIXME: Not sure this example covers the
  955. h->elf_link_hash_flags test, though it's there in
  956. other targets. */
  957. if (bfd_link_pic (info)
  958. && ((!SYMBOLIC_BIND (info, h) && h->dynindx != -1)
  959. || !h->def_regular)
  960. && (input_section->flags & SEC_ALLOC) != 0
  961. && (r_type == R_CRIS_8
  962. || r_type == R_CRIS_16
  963. || r_type == R_CRIS_32
  964. || r_type == R_CRIS_8_PCREL
  965. || r_type == R_CRIS_16_PCREL
  966. || r_type == R_CRIS_32_PCREL))
  967. relocation = 0;
  968. else if (!bfd_link_relocatable (info) && unresolved_reloc
  969. && (_bfd_elf_section_offset (output_bfd, info,
  970. input_section,
  971. rel->r_offset)
  972. != (bfd_vma) -1))
  973. {
  974. _bfd_error_handler
  975. /* xgettext:c-format */
  976. (_("%pB, section %pA: unresolvable relocation %s against symbol `%s'"),
  977. input_bfd,
  978. input_section,
  979. cris_elf_howto_table[r_type].name,
  980. symname);
  981. bfd_set_error (bfd_error_bad_value);
  982. return false;
  983. }
  984. }
  985. }
  986. if (sec != NULL && discarded_section (sec))
  987. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  988. rel, 1, relend, howto, 0, contents);
  989. if (bfd_link_relocatable (info))
  990. continue;
  991. resolved_to_zero = (h != NULL
  992. && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
  993. switch (r_type)
  994. {
  995. case R_CRIS_16_GOTPLT:
  996. case R_CRIS_32_GOTPLT:
  997. /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
  998. but we require a PLT, and the PLT handling will take care of
  999. filling in the PLT-specific GOT entry. For the GOT offset,
  1000. calculate it as we do when filling it in for the .got.plt
  1001. section. If we don't have a PLT, punt to GOT handling. */
  1002. if (h != NULL
  1003. && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
  1004. {
  1005. asection *sgotplt = htab->root.sgotplt;
  1006. bfd_vma got_offset;
  1007. BFD_ASSERT (h->dynindx != -1);
  1008. BFD_ASSERT (sgotplt != NULL);
  1009. got_offset
  1010. = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
  1011. relocation = got_offset;
  1012. break;
  1013. }
  1014. /* We didn't make a PLT entry for this symbol. Maybe everything is
  1015. folded into the GOT. Other than folding, this happens when
  1016. statically linking PIC code, or when using -Bsymbolic. Check
  1017. that we instead have a GOT entry as done for us by
  1018. elf_cris_adjust_dynamic_symbol, and drop through into the
  1019. ordinary GOT cases. This must not happen for the
  1020. executable, because any reference it does to a function
  1021. that is satisfied by a DSO must generate a PLT. We assume
  1022. these call-specific relocs don't address non-functions. */
  1023. if (h != NULL
  1024. && (h->got.offset == (bfd_vma) -1
  1025. || (!bfd_link_pic (info)
  1026. && !(h->def_regular
  1027. || (!h->def_dynamic
  1028. && h->root.type == bfd_link_hash_undefweak)))))
  1029. {
  1030. _bfd_error_handler
  1031. ((h->got.offset == (bfd_vma) -1)
  1032. /* xgettext:c-format */
  1033. ? _("%pB, section %pA: no PLT nor GOT for relocation %s"
  1034. " against symbol `%s'")
  1035. /* xgettext:c-format */
  1036. : _("%pB, section %pA: no PLT for relocation %s"
  1037. " against symbol `%s'"),
  1038. input_bfd,
  1039. input_section,
  1040. cris_elf_howto_table[r_type].name,
  1041. (symname != NULL && symname[0] != '\0'
  1042. ? symname : _("[whose name is lost]")));
  1043. /* FIXME: Perhaps blaming input is not the right thing to
  1044. do; this is probably an internal error. But it is true
  1045. that we didn't like that particular input. */
  1046. bfd_set_error (bfd_error_bad_value);
  1047. return false;
  1048. }
  1049. /* Fall through. */
  1050. /* The size of the actual relocation is not used here; we only
  1051. fill in the GOT table here. */
  1052. case R_CRIS_16_GOT:
  1053. case R_CRIS_32_GOT:
  1054. {
  1055. bfd_vma off;
  1056. /* Note that despite using RELA relocations, the .got contents
  1057. is always filled in with the link-relative relocation
  1058. value; the addend. */
  1059. if (h != NULL)
  1060. {
  1061. off = h->got.offset;
  1062. BFD_ASSERT (off != (bfd_vma) -1);
  1063. if (!elf_hash_table (info)->dynamic_sections_created
  1064. || (! bfd_link_pic (info)
  1065. && (h->def_regular
  1066. || h->type == STT_FUNC
  1067. || h->needs_plt))
  1068. || (bfd_link_pic (info)
  1069. && (SYMBOLIC_BIND (info, h) || h->dynindx == -1)
  1070. && h->def_regular))
  1071. {
  1072. /* This wasn't checked above for ! bfd_link_pic (info), but
  1073. must hold there if we get here; the symbol must
  1074. be defined in the regular program or be undefweak
  1075. or be a function or otherwise need a PLT. */
  1076. BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
  1077. || bfd_link_pic (info)
  1078. || h->def_regular
  1079. || h->type == STT_FUNC
  1080. || h->needs_plt
  1081. || h->root.type == bfd_link_hash_undefweak);
  1082. /* This is actually a static link, or it is a
  1083. -Bsymbolic link and the symbol is defined locally,
  1084. or is undefweak, or the symbol was forced to be
  1085. local because of a version file, or we're not
  1086. creating a dynamic object. We must initialize this
  1087. entry in the global offset table. Since the offset
  1088. must always be a multiple of 4, we use the least
  1089. significant bit to record whether we have
  1090. initialized it already.
  1091. If this GOT entry should be runtime-initialized, we
  1092. will create a .rela.got relocation entry to
  1093. initialize the value. This is done in the
  1094. finish_dynamic_symbol routine. */
  1095. if ((off & 1) != 0)
  1096. off &= ~1;
  1097. else
  1098. {
  1099. bfd_put_32 (output_bfd, relocation,
  1100. sgot->contents + off);
  1101. h->got.offset |= 1;
  1102. }
  1103. }
  1104. }
  1105. else
  1106. {
  1107. BFD_ASSERT (local_got_offsets != NULL
  1108. && local_got_offsets[r_symndx] != (bfd_vma) -1);
  1109. off = local_got_offsets[r_symndx];
  1110. /* The offset must always be a multiple of 4. We use
  1111. the least significant bit to record whether we have
  1112. already generated the necessary reloc. */
  1113. if ((off & 1) != 0)
  1114. off &= ~1;
  1115. else
  1116. {
  1117. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  1118. if (bfd_link_pic (info))
  1119. {
  1120. Elf_Internal_Rela outrel;
  1121. bfd_byte *loc;
  1122. srelgot = htab->root.srelgot;
  1123. BFD_ASSERT (srelgot != NULL);
  1124. outrel.r_offset = (sgot->output_section->vma
  1125. + sgot->output_offset
  1126. + off);
  1127. outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
  1128. outrel.r_addend = relocation;
  1129. loc = srelgot->contents;
  1130. loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  1131. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1132. }
  1133. local_got_offsets[r_symndx] |= 1;
  1134. }
  1135. }
  1136. relocation = sgot->output_offset + off;
  1137. if (rel->r_addend != 0)
  1138. {
  1139. /* We can't do anything for a relocation which is against
  1140. a symbol *plus offset*. GOT holds relocations for
  1141. symbols. Make this an error; the compiler isn't
  1142. allowed to pass us these kinds of things. */
  1143. if (h == NULL)
  1144. _bfd_error_handler
  1145. /* xgettext:c-format */
  1146. (_("%pB, section %pA: relocation %s with non-zero addend"
  1147. " %" PRId64 " against local symbol"),
  1148. input_bfd,
  1149. input_section,
  1150. cris_elf_howto_table[r_type].name,
  1151. (int64_t) rel->r_addend);
  1152. else
  1153. _bfd_error_handler
  1154. /* xgettext:c-format */
  1155. (_("%pB, section %pA: relocation %s with non-zero addend"
  1156. " %" PRId64 " against symbol `%s'"),
  1157. input_bfd,
  1158. input_section,
  1159. cris_elf_howto_table[r_type].name,
  1160. (int64_t) rel->r_addend,
  1161. symname[0] != '\0' ? symname : _("[whose name is lost]"));
  1162. bfd_set_error (bfd_error_bad_value);
  1163. return false;
  1164. }
  1165. }
  1166. break;
  1167. case R_CRIS_32_GOTREL:
  1168. /* This relocation must only be performed against local symbols.
  1169. It's also ok when we link a program and the symbol is either
  1170. defined in an ordinary (non-DSO) object or is undefined weak. */
  1171. if (h != NULL
  1172. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  1173. && !(!bfd_link_pic (info)
  1174. && (h->def_regular
  1175. || (!h->def_dynamic
  1176. && h->root.type == bfd_link_hash_undefweak))))
  1177. {
  1178. _bfd_error_handler
  1179. /* xgettext:c-format */
  1180. (_("%pB, section %pA: relocation %s is"
  1181. " not allowed for global symbol: `%s'"),
  1182. input_bfd,
  1183. input_section,
  1184. cris_elf_howto_table[r_type].name,
  1185. symname);
  1186. bfd_set_error (bfd_error_bad_value);
  1187. return false;
  1188. }
  1189. /* This can happen if we get a link error with the input ELF
  1190. variant mismatching the output variant. Emit an error so
  1191. it's noticed if it happens elsewhere. */
  1192. if (sgot == NULL)
  1193. {
  1194. _bfd_error_handler
  1195. /* xgettext:c-format */
  1196. (_("%pB, section %pA: relocation %s with no GOT created"),
  1197. input_bfd,
  1198. input_section,
  1199. cris_elf_howto_table[r_type].name);
  1200. bfd_set_error (bfd_error_bad_value);
  1201. return false;
  1202. }
  1203. /* This relocation is like a PC-relative one, except the
  1204. reference point is the location of GOT. Note that
  1205. sgot->output_offset is not involved in this calculation. We
  1206. always want the start of entire .got section, not the
  1207. position after the reserved header. */
  1208. relocation -= sgot->output_section->vma;
  1209. break;
  1210. case R_CRIS_32_PLT_PCREL:
  1211. /* Relocation is to the entry for this symbol in the
  1212. procedure linkage table. */
  1213. /* Resolve a PLT_PCREL reloc against a local symbol directly,
  1214. without using the procedure linkage table. */
  1215. if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  1216. break;
  1217. if (h->plt.offset == (bfd_vma) -1
  1218. || splt == NULL)
  1219. {
  1220. /* We didn't make a PLT entry for this symbol. This
  1221. happens when statically linking PIC code, or when
  1222. using -Bsymbolic. */
  1223. break;
  1224. }
  1225. relocation = (splt->output_section->vma
  1226. + splt->output_offset
  1227. + h->plt.offset);
  1228. break;
  1229. case R_CRIS_32_PLT_GOTREL:
  1230. /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
  1231. start of the .got section. See also comment at
  1232. R_CRIS_32_GOT. */
  1233. relocation -= sgot->output_section->vma;
  1234. /* Resolve a PLT_GOTREL reloc against a local symbol directly,
  1235. without using the procedure linkage table. */
  1236. if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  1237. break;
  1238. if (h->plt.offset == (bfd_vma) -1
  1239. || splt == NULL)
  1240. {
  1241. /* We didn't make a PLT entry for this symbol. This
  1242. happens when statically linking PIC code, or when
  1243. using -Bsymbolic. */
  1244. break;
  1245. }
  1246. relocation = (splt->output_section->vma
  1247. + splt->output_offset
  1248. + h->plt.offset
  1249. - sgot->output_section->vma);
  1250. break;
  1251. case R_CRIS_8_PCREL:
  1252. case R_CRIS_16_PCREL:
  1253. case R_CRIS_32_PCREL:
  1254. /* If the symbol was local, we need no shlib-specific handling. */
  1255. if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1256. || h->dynindx == -1)
  1257. break;
  1258. /* Fall through. */
  1259. case R_CRIS_8:
  1260. case R_CRIS_16:
  1261. case R_CRIS_32:
  1262. if (bfd_link_pic (info)
  1263. && !resolved_to_zero
  1264. && r_symndx != STN_UNDEF
  1265. && (input_section->flags & SEC_ALLOC) != 0
  1266. && ((r_type != R_CRIS_8_PCREL
  1267. && r_type != R_CRIS_16_PCREL
  1268. && r_type != R_CRIS_32_PCREL)
  1269. || (!SYMBOLIC_BIND (info, h)
  1270. || (h != NULL && !h->def_regular))))
  1271. {
  1272. Elf_Internal_Rela outrel;
  1273. bfd_byte *loc;
  1274. bool skip, relocate;
  1275. /* When generating a shared object, these relocations
  1276. are copied into the output file to be resolved at run
  1277. time. */
  1278. if (sreloc == NULL)
  1279. {
  1280. sreloc = _bfd_elf_get_dynamic_reloc_section
  1281. (dynobj, input_section, /*rela?*/ true);
  1282. /* The section should have been created in cris_elf_check_relocs,
  1283. but that function will not be called for objects which fail in
  1284. cris_elf_merge_private_bfd_data. */
  1285. if (sreloc == NULL)
  1286. {
  1287. bfd_set_error (bfd_error_bad_value);
  1288. return false;
  1289. }
  1290. }
  1291. skip = false;
  1292. relocate = false;
  1293. outrel.r_offset =
  1294. _bfd_elf_section_offset (output_bfd, info, input_section,
  1295. rel->r_offset);
  1296. if (outrel.r_offset == (bfd_vma) -1)
  1297. skip = true;
  1298. else if (outrel.r_offset == (bfd_vma) -2
  1299. /* For now, undefined weak symbols with non-default
  1300. visibility (yielding 0), like exception info for
  1301. discarded sections, will get a R_CRIS_NONE
  1302. relocation rather than no relocation, because we
  1303. notice too late that the symbol doesn't need a
  1304. relocation. */
  1305. || (h != NULL
  1306. && h->root.type == bfd_link_hash_undefweak
  1307. && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))
  1308. skip = true, relocate = true;
  1309. outrel.r_offset += (input_section->output_section->vma
  1310. + input_section->output_offset);
  1311. if (skip)
  1312. memset (&outrel, 0, sizeof outrel);
  1313. /* h->dynindx may be -1 if the symbol was marked to
  1314. become local. */
  1315. else if (h != NULL
  1316. && ((!SYMBOLIC_BIND (info, h) && h->dynindx != -1)
  1317. || !h->def_regular))
  1318. {
  1319. BFD_ASSERT (h->dynindx != -1);
  1320. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  1321. outrel.r_addend = relocation + rel->r_addend;
  1322. }
  1323. else
  1324. {
  1325. outrel.r_addend = relocation + rel->r_addend;
  1326. if (r_type == R_CRIS_32)
  1327. {
  1328. relocate = true;
  1329. outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
  1330. }
  1331. else
  1332. {
  1333. long indx;
  1334. if (bfd_is_abs_section (sec))
  1335. indx = 0;
  1336. else if (sec == NULL || sec->owner == NULL)
  1337. {
  1338. bfd_set_error (bfd_error_bad_value);
  1339. return false;
  1340. }
  1341. else
  1342. {
  1343. asection *osec;
  1344. /* We are turning this relocation into one
  1345. against a section symbol. It would be
  1346. proper to subtract the symbol's value,
  1347. osec->vma, from the emitted reloc addend,
  1348. but ld.so expects buggy relocs. */
  1349. osec = sec->output_section;
  1350. indx = elf_section_data (osec)->dynindx;
  1351. if (indx == 0)
  1352. {
  1353. osec = htab->root.text_index_section;
  1354. indx = elf_section_data (osec)->dynindx;
  1355. }
  1356. BFD_ASSERT (indx != 0);
  1357. }
  1358. outrel.r_info = ELF32_R_INFO (indx, r_type);
  1359. }
  1360. }
  1361. loc = sreloc->contents;
  1362. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  1363. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1364. /* This reloc will be computed at runtime, so there's no
  1365. need to do anything now, except for R_CRIS_32 relocations
  1366. that have been turned into R_CRIS_RELATIVE. */
  1367. if (!relocate)
  1368. continue;
  1369. }
  1370. break;
  1371. case R_CRIS_16_DTPREL:
  1372. case R_CRIS_32_DTPREL:
  1373. /* This relocation must only be performed against local
  1374. symbols, or to sections that are not loadable. It's also
  1375. ok when we link a program and the symbol is defined in an
  1376. ordinary (non-DSO) object (if it's undefined there, we've
  1377. already seen an error). */
  1378. if (h != NULL
  1379. && (input_section->flags & SEC_ALLOC) != 0
  1380. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  1381. && (bfd_link_pic (info)
  1382. || (!h->def_regular
  1383. && h->root.type != bfd_link_hash_undefined)))
  1384. {
  1385. _bfd_error_handler
  1386. ((h->root.type == bfd_link_hash_undefined)
  1387. /* We shouldn't get here for GCC-emitted code. */
  1388. /* xgettext:c-format */
  1389. ? _("%pB, section %pA: relocation %s has an undefined"
  1390. " reference to `%s', perhaps a declaration mixup?")
  1391. /* xgettext:c-format */
  1392. : _("%pB, section %pA: relocation %s is"
  1393. " not allowed for `%s', a global symbol with default"
  1394. " visibility, perhaps a declaration mixup?"),
  1395. input_bfd,
  1396. input_section,
  1397. cris_elf_howto_table[r_type].name,
  1398. symname != NULL && symname[0] != '\0'
  1399. ? symname : _("[whose name is lost]"));
  1400. bfd_set_error (bfd_error_bad_value);
  1401. return false;
  1402. }
  1403. BFD_ASSERT ((input_section->flags & SEC_ALLOC) == 0
  1404. || htab->dtpmod_refcount != 0);
  1405. /* Fill in a R_CRIS_DTPMOD reloc at offset 3 if we haven't
  1406. already done so. Note that we do this in .got.plt, not
  1407. in .got, as .got.plt contains the first part, still the
  1408. reloc is against .got, because the linker script directs
  1409. (is required to direct) them both into .got. */
  1410. if (htab->dtpmod_refcount > 0
  1411. && (input_section->flags & SEC_ALLOC) != 0)
  1412. {
  1413. asection *sgotplt = htab->root.sgotplt;
  1414. BFD_ASSERT (sgotplt != NULL);
  1415. if (bfd_link_pic (info))
  1416. {
  1417. Elf_Internal_Rela outrel;
  1418. bfd_byte *loc;
  1419. srelgot = htab->root.srelgot;
  1420. BFD_ASSERT (srelgot != NULL);
  1421. loc = srelgot->contents;
  1422. loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  1423. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 12);
  1424. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
  1425. outrel.r_offset = (sgotplt->output_section->vma
  1426. + sgotplt->output_offset
  1427. + 12);
  1428. outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTPMOD);
  1429. outrel.r_addend = 0;
  1430. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1431. }
  1432. else
  1433. {
  1434. /* For an executable, the GOT entry contents is known. */
  1435. bfd_put_32 (output_bfd, (bfd_vma) 1, sgotplt->contents + 12);
  1436. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
  1437. }
  1438. /* Reverse the sign to mark that we've emitted the
  1439. required GOT entry. */
  1440. htab->dtpmod_refcount = - htab->dtpmod_refcount;
  1441. }
  1442. /* The relocation is the offset from the start of the module
  1443. TLS block to the (local) symbol. */
  1444. relocation -= elf_hash_table (info)->tls_sec == NULL
  1445. ? 0 : elf_hash_table (info)->tls_sec->vma;
  1446. break;
  1447. case R_CRIS_32_GD:
  1448. if (bfd_link_pic (info))
  1449. {
  1450. bfd_set_error (bfd_error_invalid_operation);
  1451. /* We've already informed in cris_elf_check_relocs that
  1452. this is an error. */
  1453. return false;
  1454. }
  1455. /* Fall through. */
  1456. case R_CRIS_16_GOT_GD:
  1457. case R_CRIS_32_GOT_GD:
  1458. if (rel->r_addend != 0)
  1459. {
  1460. /* We can't do anything for a relocation which is against a
  1461. symbol *plus offset*. The GOT holds relocations for
  1462. symbols. Make this an error; the compiler isn't allowed
  1463. to pass us these kinds of things. */
  1464. _bfd_error_handler
  1465. /* xgettext:c-format */
  1466. (_("%pB, section %pA: relocation %s with non-zero addend"
  1467. " %" PRId64 " against symbol `%s'"),
  1468. input_bfd,
  1469. input_section,
  1470. cris_elf_howto_table[r_type].name,
  1471. (int64_t) rel->r_addend,
  1472. symname[0] != '\0' ? symname : _("[whose name is lost]"));
  1473. bfd_set_error (bfd_error_bad_value);
  1474. return false;
  1475. }
  1476. if (!bfd_link_pic (info)
  1477. && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
  1478. {
  1479. /* Known contents of the GOT. */
  1480. bfd_vma off;
  1481. /* The symbol is defined in the program, so just write
  1482. (1, known_tpoffset) into the GOT. */
  1483. relocation -= elf_hash_table (info)->tls_sec->vma;
  1484. if (h != NULL)
  1485. {
  1486. off = elf_cris_hash_entry (h)->tprel_refcount > 0
  1487. ? h->got.offset + 4 : h->got.offset;
  1488. }
  1489. else
  1490. {
  1491. off = local_got_offsets[r_symndx];
  1492. if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
  1493. off += 4;
  1494. }
  1495. /* We use bit 1 of the offset as a flag for GOT entry with
  1496. the R_CRIS_DTP reloc, setting it when we've emitted the
  1497. GOT entry and reloc. Bit 0 is used for R_CRIS_32_TPREL
  1498. relocs. */
  1499. if ((off & 2) == 0)
  1500. {
  1501. off &= ~3;
  1502. if (h != NULL)
  1503. h->got.offset |= 2;
  1504. else
  1505. local_got_offsets[r_symndx] |= 2;
  1506. bfd_put_32 (output_bfd, 1, sgot->contents + off);
  1507. bfd_put_32 (output_bfd, relocation, sgot->contents + off + 4);
  1508. }
  1509. else
  1510. off &= ~3;
  1511. relocation = sgot->output_offset + off
  1512. + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
  1513. }
  1514. else
  1515. {
  1516. /* Not all parts of the GOT entry are known; emit a real
  1517. relocation. */
  1518. bfd_vma off;
  1519. if (h != NULL)
  1520. off = elf_cris_hash_entry (h)->tprel_refcount > 0
  1521. ? h->got.offset + 4 : h->got.offset;
  1522. else
  1523. {
  1524. off = local_got_offsets[r_symndx];
  1525. if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
  1526. off += 4;
  1527. }
  1528. /* See above re bit 1 and bit 0 usage. */
  1529. if ((off & 2) == 0)
  1530. {
  1531. Elf_Internal_Rela outrel;
  1532. bfd_byte *loc;
  1533. off &= ~3;
  1534. if (h != NULL)
  1535. h->got.offset |= 2;
  1536. else
  1537. local_got_offsets[r_symndx] |= 2;
  1538. /* Clear the target contents of the GOT (just as a
  1539. gesture; it's already cleared on allocation): this
  1540. relocation is not like the other dynrelocs. */
  1541. bfd_put_32 (output_bfd, 0, sgot->contents + off);
  1542. bfd_put_32 (output_bfd, 0, sgot->contents + off + 4);
  1543. srelgot = htab->root.srelgot;
  1544. BFD_ASSERT (srelgot != NULL);
  1545. if (h != NULL && h->dynindx != -1)
  1546. {
  1547. outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_DTP);
  1548. relocation = 0;
  1549. }
  1550. else
  1551. {
  1552. outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTP);
  1553. /* NULL if we had an error. */
  1554. relocation -= elf_hash_table (info)->tls_sec == NULL
  1555. ? 0 : elf_hash_table (info)->tls_sec->vma;
  1556. }
  1557. outrel.r_offset = (sgot->output_section->vma
  1558. + sgot->output_offset
  1559. + off);
  1560. outrel.r_addend = relocation;
  1561. loc = srelgot->contents;
  1562. loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  1563. /* NULL if we had an error. */
  1564. if (srelgot->contents != NULL)
  1565. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1566. }
  1567. else
  1568. off &= ~3;
  1569. relocation = sgot->output_offset + off
  1570. + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
  1571. }
  1572. /* The GOT-relative offset to the GOT entry is the
  1573. relocation, or for R_CRIS_32_GD, the actual address of
  1574. the GOT entry. */
  1575. break;
  1576. case R_CRIS_32_IE:
  1577. if (bfd_link_pic (info))
  1578. {
  1579. bfd_set_error (bfd_error_invalid_operation);
  1580. /* We've already informed in cris_elf_check_relocs that
  1581. this is an error. */
  1582. return false;
  1583. }
  1584. /* Fall through. */
  1585. case R_CRIS_32_GOT_TPREL:
  1586. case R_CRIS_16_GOT_TPREL:
  1587. if (rel->r_addend != 0)
  1588. {
  1589. /* We can't do anything for a relocation which is
  1590. against a symbol *plus offset*. GOT holds
  1591. relocations for symbols. Make this an error; the
  1592. compiler isn't allowed to pass us these kinds of
  1593. things. */
  1594. _bfd_error_handler
  1595. /* xgettext:c-format */
  1596. (_("%pB, section %pA: relocation %s with non-zero addend"
  1597. " %" PRId64 " against symbol `%s'"),
  1598. input_bfd,
  1599. input_section,
  1600. cris_elf_howto_table[r_type].name,
  1601. (int64_t) rel->r_addend,
  1602. symname[0] != '\0' ? symname : _("[whose name is lost]"));
  1603. bfd_set_error (bfd_error_bad_value);
  1604. return false;
  1605. }
  1606. if (!bfd_link_pic (info)
  1607. && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
  1608. {
  1609. /* Known contents of the GOT. */
  1610. bfd_vma off;
  1611. /* The symbol is defined in the program, so just write
  1612. the -prog_tls_size+known_tpoffset into the GOT. */
  1613. relocation -= elf_hash_table (info)->tls_sec->vma;
  1614. relocation -= elf_hash_table (info)->tls_size;
  1615. if (h != NULL)
  1616. off = h->got.offset;
  1617. else
  1618. off = local_got_offsets[r_symndx];
  1619. /* Bit 0 is used to mark whether we've emitted the required
  1620. entry (and if needed R_CRIS_32_TPREL reloc). Bit 1
  1621. is used similarly for R_CRIS_DTP, see above. */
  1622. if ((off & 1) == 0)
  1623. {
  1624. off &= ~3;
  1625. if (h != NULL)
  1626. h->got.offset |= 1;
  1627. else
  1628. local_got_offsets[r_symndx] |= 1;
  1629. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  1630. }
  1631. else
  1632. off &= ~3;
  1633. relocation = sgot->output_offset + off
  1634. + (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
  1635. }
  1636. else
  1637. {
  1638. /* Emit a real relocation. */
  1639. bfd_vma off;
  1640. if (h != NULL)
  1641. off = h->got.offset;
  1642. else
  1643. off = local_got_offsets[r_symndx];
  1644. /* See above re usage of bit 0 and 1. */
  1645. if ((off & 1) == 0)
  1646. {
  1647. Elf_Internal_Rela outrel;
  1648. bfd_byte *loc;
  1649. off &= ~3;
  1650. if (h != NULL)
  1651. h->got.offset |= 1;
  1652. else
  1653. local_got_offsets[r_symndx] |= 1;
  1654. srelgot = htab->root.srelgot;
  1655. BFD_ASSERT (srelgot != NULL);
  1656. if (h != NULL && h->dynindx != -1)
  1657. {
  1658. outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_32_TPREL);
  1659. relocation = 0;
  1660. }
  1661. else
  1662. {
  1663. outrel.r_info = ELF32_R_INFO (0, R_CRIS_32_TPREL);
  1664. /* NULL if we had an error. */
  1665. relocation -= elf_hash_table (info)->tls_sec == NULL
  1666. ? 0 : elf_hash_table (info)->tls_sec->vma;
  1667. }
  1668. /* Just "define" the initial contents in some
  1669. semi-logical way. */
  1670. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  1671. outrel.r_offset = (sgot->output_section->vma
  1672. + sgot->output_offset
  1673. + off);
  1674. outrel.r_addend = relocation;
  1675. loc = srelgot->contents;
  1676. loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  1677. /* NULL if we had an error. */
  1678. if (srelgot->contents != NULL)
  1679. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1680. }
  1681. else
  1682. off &= ~3;
  1683. relocation = sgot->output_offset + off
  1684. + (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
  1685. }
  1686. /* The GOT-relative offset to the GOT entry is the relocation,
  1687. or for R_CRIS_32_GD, the actual address of the GOT entry. */
  1688. break;
  1689. case R_CRIS_16_TPREL:
  1690. case R_CRIS_32_TPREL:
  1691. /* This relocation must only be performed against symbols
  1692. defined in an ordinary (non-DSO) object. */
  1693. if (bfd_link_pic (info))
  1694. {
  1695. bfd_set_error (bfd_error_invalid_operation);
  1696. /* We've already informed in cris_elf_check_relocs that
  1697. this is an error. */
  1698. return false;
  1699. }
  1700. if (h != NULL
  1701. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  1702. && !(h->def_regular || ELF_COMMON_DEF_P (h))
  1703. /* If it's undefined, then an error message has already
  1704. been emitted. */
  1705. && h->root.type != bfd_link_hash_undefined)
  1706. {
  1707. _bfd_error_handler
  1708. /* xgettext:c-format */
  1709. (_("%pB, section %pA: relocation %s is"
  1710. " not allowed for symbol: `%s'"
  1711. " which is defined outside the program,"
  1712. " perhaps a declaration mixup?"),
  1713. input_bfd,
  1714. input_section,
  1715. cris_elf_howto_table[r_type].name,
  1716. symname);
  1717. bfd_set_error (bfd_error_bad_value);
  1718. return false;
  1719. }
  1720. /* NULL if we had an error. */
  1721. relocation -= elf_hash_table (info)->tls_sec == NULL
  1722. ? 0
  1723. : (elf_hash_table (info)->tls_sec->vma
  1724. + elf_hash_table (info)->tls_size);
  1725. /* The TLS-relative offset is the relocation. */
  1726. break;
  1727. default:
  1728. BFD_FAIL ();
  1729. return false;
  1730. }
  1731. r = cris_final_link_relocate (howto, input_bfd, input_section,
  1732. contents, rel, relocation);
  1733. if (r != bfd_reloc_ok)
  1734. {
  1735. const char * msg = (const char *) NULL;
  1736. switch (r)
  1737. {
  1738. case bfd_reloc_overflow:
  1739. (*info->callbacks->reloc_overflow)
  1740. (info, (h ? &h->root : NULL), symname, howto->name,
  1741. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  1742. if (additional_relocation_error_msg_count > 0)
  1743. {
  1744. additional_relocation_error_msg_count--;
  1745. switch (r_type)
  1746. {
  1747. case R_CRIS_16_GOTPLT:
  1748. case R_CRIS_16_GOT:
  1749. /* Not just TLS is involved here, so we make
  1750. generation and message depend on -fPIC/-fpic
  1751. only. */
  1752. case R_CRIS_16_GOT_TPREL:
  1753. case R_CRIS_16_GOT_GD:
  1754. _bfd_error_handler
  1755. (_("(too many global variables for -fpic:"
  1756. " recompile with -fPIC)"));
  1757. break;
  1758. case R_CRIS_16_TPREL:
  1759. case R_CRIS_16_DTPREL:
  1760. _bfd_error_handler
  1761. (_("(thread-local data too big for -fpic or"
  1762. " -msmall-tls: recompile with -fPIC or"
  1763. " -mno-small-tls)"));
  1764. break;
  1765. /* No known cause for overflow for other relocs. */
  1766. default:
  1767. break;
  1768. }
  1769. }
  1770. break;
  1771. case bfd_reloc_undefined:
  1772. (*info->callbacks->undefined_symbol)
  1773. (info, symname, input_bfd, input_section, rel->r_offset, true);
  1774. break;
  1775. case bfd_reloc_outofrange:
  1776. msg = _("internal error: out of range error");
  1777. break;
  1778. case bfd_reloc_notsupported:
  1779. msg = _("internal error: unsupported relocation error");
  1780. break;
  1781. case bfd_reloc_dangerous:
  1782. msg = _("internal error: dangerous relocation");
  1783. break;
  1784. default:
  1785. msg = _("internal error: unknown error");
  1786. break;
  1787. }
  1788. if (msg)
  1789. (*info->callbacks->warning) (info, msg, symname, input_bfd,
  1790. input_section, rel->r_offset);
  1791. }
  1792. }
  1793. return true;
  1794. }
  1795. /* Finish up dynamic symbol handling. We set the contents of various
  1796. dynamic sections here. */
  1797. static bool
  1798. elf_cris_finish_dynamic_symbol (bfd *output_bfd,
  1799. struct bfd_link_info *info,
  1800. struct elf_link_hash_entry *h,
  1801. Elf_Internal_Sym *sym)
  1802. {
  1803. struct elf_cris_link_hash_table * htab;
  1804. /* Where in the plt entry to put values. */
  1805. int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
  1806. /* What offset to add to the distance to the first PLT entry for the
  1807. value at plt_off3. */
  1808. int plt_off3_value_bias = 4;
  1809. /* Where in the PLT entry the call-dynlink-stub is (happens to be same
  1810. for PIC and non-PIC for v32 and pre-v32). */
  1811. int plt_stub_offset = 8;
  1812. int plt_entry_size = PLT_ENTRY_SIZE;
  1813. const bfd_byte *plt_entry = elf_cris_plt_entry;
  1814. const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
  1815. htab = elf_cris_hash_table (info);
  1816. if (htab == NULL)
  1817. return false;
  1818. /* Adjust the various PLT entry offsets. */
  1819. if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
  1820. {
  1821. plt_off2 = 14;
  1822. plt_off3 = 20;
  1823. plt_off3_value_bias = -2;
  1824. plt_stub_offset = 12;
  1825. plt_entry_size = PLT_ENTRY_SIZE_V32;
  1826. plt_entry = elf_cris_plt_entry_v32;
  1827. plt_pic_entry = elf_cris_pic_plt_entry_v32;
  1828. }
  1829. if (h->plt.offset != (bfd_vma) -1)
  1830. {
  1831. asection *splt;
  1832. asection *sgotplt;
  1833. asection *srela;
  1834. bfd_vma got_base;
  1835. bfd_vma gotplt_offset
  1836. = elf_cris_hash_entry (h)->gotplt_offset;
  1837. Elf_Internal_Rela rela;
  1838. bfd_byte *loc;
  1839. bool has_gotplt = gotplt_offset != 0;
  1840. /* Get the index in the .rela.plt relocations for the .got.plt
  1841. entry that corresponds to this symbol.
  1842. We have to count backwards here, and the result is only valid
  1843. as an index into .rela.plt. We also have to undo the effect
  1844. of the R_CRIS_DTPMOD entry at .got index 3 (offset 12 into
  1845. .got.plt) for which gotplt_offset is adjusted, because while
  1846. that entry goes into .got.plt, its relocation goes into
  1847. .rela.got, not .rela.plt. (It's not PLT-specific; not to be
  1848. processed as part of the runtime lazy .rela.plt relocation).
  1849. FIXME: There be literal constants here... */
  1850. bfd_vma rela_plt_index
  1851. = (htab->dtpmod_refcount != 0
  1852. ? gotplt_offset/4 - 2 - 3 : gotplt_offset/4 - 3);
  1853. /* Get the offset into the .got table of the entry that corresponds
  1854. to this function. Note that we embed knowledge that "incoming"
  1855. .got goes after .got.plt in the output without padding (pointer
  1856. aligned). However, that knowledge is present in several other
  1857. places too. */
  1858. bfd_vma got_offset
  1859. = (has_gotplt
  1860. ? gotplt_offset
  1861. : h->got.offset + htab->next_gotplt_entry);
  1862. /* This symbol has an entry in the procedure linkage table. Set it
  1863. up. */
  1864. BFD_ASSERT (h->dynindx != -1);
  1865. splt = htab->root.splt;
  1866. sgotplt = htab->root.sgotplt;
  1867. srela = htab->root.srelplt;
  1868. BFD_ASSERT (splt != NULL && sgotplt != NULL
  1869. && (! has_gotplt || srela != NULL));
  1870. got_base = sgotplt->output_section->vma + sgotplt->output_offset;
  1871. /* Fill in the entry in the procedure linkage table. */
  1872. if (! bfd_link_pic (info))
  1873. {
  1874. memcpy (splt->contents + h->plt.offset, plt_entry,
  1875. plt_entry_size);
  1876. /* We need to enter the absolute address of the GOT entry here. */
  1877. bfd_put_32 (output_bfd, got_base + got_offset,
  1878. splt->contents + h->plt.offset + plt_off1);
  1879. }
  1880. else
  1881. {
  1882. memcpy (splt->contents + h->plt.offset, plt_pic_entry,
  1883. plt_entry_size);
  1884. bfd_put_32 (output_bfd, got_offset,
  1885. splt->contents + h->plt.offset + plt_off1);
  1886. }
  1887. /* Fill in the plt entry and make a relocation, if this is a "real"
  1888. PLT entry. */
  1889. if (has_gotplt)
  1890. {
  1891. /* Fill in the offset to the reloc table. */
  1892. bfd_put_32 (output_bfd,
  1893. rela_plt_index * sizeof (Elf32_External_Rela),
  1894. splt->contents + h->plt.offset + plt_off2);
  1895. /* Fill in the offset to the first PLT entry, where to "jump". */
  1896. bfd_put_32 (output_bfd,
  1897. - (h->plt.offset + plt_off3 + plt_off3_value_bias),
  1898. splt->contents + h->plt.offset + plt_off3);
  1899. /* Fill in the entry in the global offset table with the address of
  1900. the relocating stub. */
  1901. bfd_put_32 (output_bfd,
  1902. (splt->output_section->vma
  1903. + splt->output_offset
  1904. + h->plt.offset
  1905. + plt_stub_offset),
  1906. sgotplt->contents + got_offset);
  1907. /* Fill in the entry in the .rela.plt section. */
  1908. rela.r_offset = (sgotplt->output_section->vma
  1909. + sgotplt->output_offset
  1910. + got_offset);
  1911. rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
  1912. rela.r_addend = 0;
  1913. loc = srela->contents + rela_plt_index * sizeof (Elf32_External_Rela);
  1914. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  1915. }
  1916. if (!h->def_regular)
  1917. {
  1918. /* Mark the symbol as undefined, rather than as defined in
  1919. the .plt section. Leave the value alone. */
  1920. sym->st_shndx = SHN_UNDEF;
  1921. /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't
  1922. know whether resetting the value is significant; if it really
  1923. is, rather than a quirk or bug in the sparc port, then I
  1924. believe we'd see this elsewhere. */
  1925. /* If the symbol is weak, we do need to clear the value.
  1926. Otherwise, the PLT entry would provide a definition for
  1927. the symbol even if the symbol wasn't defined anywhere,
  1928. and so the symbol would never be NULL. */
  1929. if (!h->ref_regular_nonweak)
  1930. sym->st_value = 0;
  1931. }
  1932. }
  1933. /* For an ordinary program, we emit .got relocs only for symbols that
  1934. are in the dynamic-symbols table and are either defined by the
  1935. program or are undefined weak symbols, or are function symbols
  1936. where we do not output a PLT: the PLT reloc was output above and all
  1937. references to the function symbol are redirected to the PLT. */
  1938. if (h->got.offset != (bfd_vma) -1
  1939. && (elf_cris_hash_entry (h)->reg_got_refcount > 0)
  1940. && (bfd_link_pic (info)
  1941. || (h->dynindx != -1
  1942. && h->plt.offset == (bfd_vma) -1
  1943. && !h->def_regular
  1944. && h->root.type != bfd_link_hash_undefweak)))
  1945. {
  1946. asection *sgot;
  1947. asection *srela;
  1948. Elf_Internal_Rela rela;
  1949. bfd_byte *loc;
  1950. bfd_byte *where;
  1951. /* This symbol has an entry in the global offset table. Set it up. */
  1952. sgot = htab->root.sgot;
  1953. srela = htab->root.srelgot;
  1954. BFD_ASSERT (sgot != NULL && srela != NULL);
  1955. rela.r_offset = (sgot->output_section->vma
  1956. + sgot->output_offset
  1957. + (h->got.offset &~ (bfd_vma) 1));
  1958. /* If this is a static link, or it is a -Bsymbolic link and the
  1959. symbol is defined locally or was forced to be local because
  1960. of a version file, we just want to emit a RELATIVE reloc.
  1961. The entry in the global offset table will already have been
  1962. initialized in the relocate_section function. */
  1963. where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
  1964. if (! elf_hash_table (info)->dynamic_sections_created
  1965. || (bfd_link_pic (info)
  1966. && (SYMBOLIC_BIND (info, h) || h->dynindx == -1)
  1967. && h->def_regular))
  1968. {
  1969. rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
  1970. rela.r_addend = bfd_get_signed_32 (output_bfd, where);
  1971. }
  1972. else
  1973. {
  1974. bfd_put_32 (output_bfd, (bfd_vma) 0, where);
  1975. rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
  1976. rela.r_addend = 0;
  1977. }
  1978. loc = srela->contents;
  1979. loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
  1980. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  1981. }
  1982. if (h->needs_copy)
  1983. {
  1984. asection *s;
  1985. Elf_Internal_Rela rela;
  1986. bfd_byte *loc;
  1987. /* This symbol needs a copy reloc. Set it up. */
  1988. BFD_ASSERT (h->dynindx != -1
  1989. && (h->root.type == bfd_link_hash_defined
  1990. || h->root.type == bfd_link_hash_defweak));
  1991. if (h->root.u.def.section == htab->root.sdynrelro)
  1992. s = htab->root.sreldynrelro;
  1993. else
  1994. s = htab->root.srelbss;
  1995. rela.r_offset = (h->root.u.def.value
  1996. + h->root.u.def.section->output_section->vma
  1997. + h->root.u.def.section->output_offset);
  1998. rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
  1999. rela.r_addend = 0;
  2000. loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
  2001. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  2002. }
  2003. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  2004. if (h == elf_hash_table (info)->hdynamic
  2005. || h == elf_hash_table (info)->hgot)
  2006. sym->st_shndx = SHN_ABS;
  2007. return true;
  2008. }
  2009. /* Finish up the dynamic sections. Do *not* emit relocs here, as their
  2010. offsets were changed, as part of -z combreloc handling, from those we
  2011. computed. */
  2012. static bool
  2013. elf_cris_finish_dynamic_sections (bfd *output_bfd,
  2014. struct bfd_link_info *info)
  2015. {
  2016. bfd *dynobj;
  2017. asection *sgot;
  2018. asection *sdyn;
  2019. dynobj = elf_hash_table (info)->dynobj;
  2020. sgot = elf_hash_table (info)->sgotplt;
  2021. BFD_ASSERT (sgot != NULL);
  2022. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2023. if (elf_hash_table (info)->dynamic_sections_created)
  2024. {
  2025. asection *splt;
  2026. Elf32_External_Dyn *dyncon, *dynconend;
  2027. splt = elf_hash_table (info)->splt;
  2028. BFD_ASSERT (splt != NULL && sdyn != NULL);
  2029. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  2030. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  2031. for (; dyncon < dynconend; dyncon++)
  2032. {
  2033. Elf_Internal_Dyn dyn;
  2034. asection *s;
  2035. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  2036. switch (dyn.d_tag)
  2037. {
  2038. default:
  2039. break;
  2040. case DT_PLTGOT:
  2041. dyn.d_un.d_ptr = sgot->output_section->vma + sgot->output_offset;
  2042. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2043. break;
  2044. case DT_JMPREL:
  2045. /* Yes, we *can* have a .plt and no .plt.rela, for instance
  2046. if all symbols are found in the .got (not .got.plt). */
  2047. s = elf_hash_table (info)->srelplt;
  2048. dyn.d_un.d_ptr = s != NULL ? (s->output_section->vma
  2049. + s->output_offset) : 0;
  2050. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2051. break;
  2052. case DT_PLTRELSZ:
  2053. s = elf_hash_table (info)->srelplt;
  2054. if (s == NULL)
  2055. dyn.d_un.d_val = 0;
  2056. else
  2057. dyn.d_un.d_val = s->size;
  2058. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2059. break;
  2060. }
  2061. }
  2062. /* Fill in the first entry in the procedure linkage table. */
  2063. if (splt->size > 0)
  2064. {
  2065. if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
  2066. {
  2067. if (bfd_link_pic (info))
  2068. memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
  2069. PLT_ENTRY_SIZE_V32);
  2070. else
  2071. {
  2072. memcpy (splt->contents, elf_cris_plt0_entry_v32,
  2073. PLT_ENTRY_SIZE_V32);
  2074. bfd_put_32 (output_bfd,
  2075. sgot->output_section->vma
  2076. + sgot->output_offset + 4,
  2077. splt->contents + 4);
  2078. elf_section_data (splt->output_section)->this_hdr.sh_entsize
  2079. = PLT_ENTRY_SIZE_V32;
  2080. }
  2081. }
  2082. else
  2083. {
  2084. if (bfd_link_pic (info))
  2085. memcpy (splt->contents, elf_cris_pic_plt0_entry,
  2086. PLT_ENTRY_SIZE);
  2087. else
  2088. {
  2089. memcpy (splt->contents, elf_cris_plt0_entry,
  2090. PLT_ENTRY_SIZE);
  2091. bfd_put_32 (output_bfd,
  2092. sgot->output_section->vma
  2093. + sgot->output_offset + 4,
  2094. splt->contents + 6);
  2095. bfd_put_32 (output_bfd,
  2096. sgot->output_section->vma
  2097. + sgot->output_offset + 8,
  2098. splt->contents + 14);
  2099. elf_section_data (splt->output_section)->this_hdr.sh_entsize
  2100. = PLT_ENTRY_SIZE;
  2101. }
  2102. }
  2103. }
  2104. }
  2105. /* Fill in the first three entries in the global offset table. */
  2106. if (sgot->size > 0)
  2107. {
  2108. if (sdyn == NULL)
  2109. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
  2110. else
  2111. bfd_put_32 (output_bfd,
  2112. sdyn->output_section->vma + sdyn->output_offset,
  2113. sgot->contents);
  2114. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
  2115. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
  2116. }
  2117. elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  2118. return true;
  2119. }
  2120. /* Return the section that should be marked against GC for a given
  2121. relocation. */
  2122. static asection *
  2123. cris_elf_gc_mark_hook (asection *sec,
  2124. struct bfd_link_info *info,
  2125. Elf_Internal_Rela *rel,
  2126. struct elf_link_hash_entry *h,
  2127. Elf_Internal_Sym *sym)
  2128. {
  2129. enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
  2130. if (h != NULL)
  2131. switch (r_type)
  2132. {
  2133. case R_CRIS_GNU_VTINHERIT:
  2134. case R_CRIS_GNU_VTENTRY:
  2135. return NULL;
  2136. default:
  2137. break;
  2138. }
  2139. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  2140. }
  2141. /* The elf_backend_plt_sym_val hook function. */
  2142. static bfd_vma
  2143. cris_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED, const asection *plt,
  2144. const arelent *rel)
  2145. {
  2146. bfd_size_type plt_entry_size;
  2147. bfd_size_type pltoffs;
  2148. bfd *abfd = plt->owner;
  2149. /* Same for CRIS and CRIS v32; see elf_cris_(|pic_)plt_entry(|_v32)[]. */
  2150. bfd_size_type plt_entry_got_offset = 2;
  2151. bfd_size_type plt_sec_size;
  2152. bfd_size_type got_vma_for_dyn;
  2153. asection *got;
  2154. /* FIXME: the .got section should be readily available also when
  2155. we're not linking. */
  2156. if ((got = bfd_get_section_by_name (abfd, ".got")) == NULL)
  2157. return (bfd_vma) -1;
  2158. plt_sec_size = bfd_section_size (plt);
  2159. plt_entry_size
  2160. = (bfd_get_mach (abfd) == bfd_mach_cris_v32
  2161. ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
  2162. /* Data in PLT is GOT-relative for DYN, but absolute for EXE. */
  2163. got_vma_for_dyn = (abfd->flags & EXEC_P) ? 0 : got->vma;
  2164. /* Because we can have merged GOT entries; a single .got entry for
  2165. both GOT and the PLT part of the GOT (.got.plt), the index of the
  2166. reloc in .rela.plt is not the same as the index in the PLT.
  2167. Instead, we have to hunt down the GOT offset in the PLT that
  2168. corresponds to that of this reloc. Unfortunately, we will only
  2169. be called for the .rela.plt relocs, so we'll miss synthetic
  2170. symbols for .plt entries with merged GOT entries. (FIXME:
  2171. fixable by providing our own bfd_elf32_get_synthetic_symtab.
  2172. Doesn't seem worthwile at time of this writing.) FIXME: we've
  2173. gone from O(1) to O(N) (N number of PLT entries) for finding each
  2174. PLT address. Shouldn't matter in practice though. */
  2175. for (pltoffs = plt_entry_size;
  2176. pltoffs < plt_sec_size;
  2177. pltoffs += plt_entry_size)
  2178. {
  2179. bfd_size_type got_offset;
  2180. bfd_byte gotoffs_raw[4];
  2181. if (!bfd_get_section_contents (abfd, (asection *) plt, gotoffs_raw,
  2182. pltoffs + plt_entry_got_offset,
  2183. sizeof (gotoffs_raw)))
  2184. return (bfd_vma) -1;
  2185. got_offset = bfd_get_32 (abfd, gotoffs_raw);
  2186. if (got_offset + got_vma_for_dyn == rel->address)
  2187. return plt->vma + pltoffs;
  2188. }
  2189. /* While it's tempting to BFD_ASSERT that we shouldn't get here,
  2190. that'd not be graceful behavior for invalid input. */
  2191. return (bfd_vma) -1;
  2192. }
  2193. /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
  2194. entry but we found we will not create any. Called when we find we will
  2195. not have any PLT for this symbol, by for example
  2196. elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
  2197. or elf_cris_size_dynamic_sections if no dynamic sections will be
  2198. created (we're only linking static objects). */
  2199. static bool
  2200. elf_cris_adjust_gotplt_to_got (struct elf_cris_link_hash_entry *h, void * p)
  2201. {
  2202. struct bfd_link_info *info = (struct bfd_link_info *) p;
  2203. /* A GOTPLT reloc, when activated, is supposed to be included into
  2204. the PLT refcount, when the symbol isn't set-or-forced local. */
  2205. BFD_ASSERT (h->gotplt_refcount == 0
  2206. || h->root.plt.refcount == -1
  2207. || h->gotplt_refcount <= h->root.plt.refcount);
  2208. /* If nobody wanted a GOTPLT with this symbol, we're done. */
  2209. if (h->gotplt_refcount <= 0)
  2210. return true;
  2211. if (h->reg_got_refcount > 0)
  2212. {
  2213. /* There's a GOT entry for this symbol. Just adjust the refcounts.
  2214. Probably not necessary at this stage, but keeping them accurate
  2215. helps avoiding surprises later. */
  2216. h->root.got.refcount += h->gotplt_refcount;
  2217. h->reg_got_refcount += h->gotplt_refcount;
  2218. h->gotplt_refcount = 0;
  2219. }
  2220. else
  2221. {
  2222. /* No GOT entry for this symbol. We need to create one. */
  2223. asection *sgot;
  2224. asection *srelgot;
  2225. sgot = elf_hash_table (info)->sgot;
  2226. srelgot = elf_hash_table (info)->srelgot;
  2227. /* Put accurate refcounts there. */
  2228. BFD_ASSERT (h->root.got.refcount >= 0);
  2229. h->root.got.refcount += h->gotplt_refcount;
  2230. h->reg_got_refcount = h->gotplt_refcount;
  2231. h->gotplt_refcount = 0;
  2232. /* We always have a .got and a .rela.got section if there were
  2233. GOTPLT relocs in input. */
  2234. BFD_ASSERT (sgot != NULL && srelgot != NULL);
  2235. /* Allocate space in the .got section. */
  2236. sgot->size += 4;
  2237. /* Allocate relocation space. */
  2238. srelgot->size += sizeof (Elf32_External_Rela);
  2239. }
  2240. return true;
  2241. }
  2242. /* Try to fold PLT entries with GOT entries. There are two cases when we
  2243. want to do this:
  2244. - When all PLT references are GOTPLT references, and there are GOT
  2245. references, and this is not the executable. We don't have to
  2246. generate a PLT at all.
  2247. - When there are both (ordinary) PLT references and GOT references,
  2248. and this isn't the executable.
  2249. We want to make the PLT reference use the ordinary GOT entry rather
  2250. than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
  2251. since the GOT entry will have to be resolved at startup anyway.
  2252. Though the latter case is handled when room for the PLT is allocated,
  2253. not here.
  2254. By folding into the GOT, we may need a round-trip to a PLT in the
  2255. executable for calls, a loss in performance. Still, losing a
  2256. reloc is a win in size and at least in start-up time.
  2257. Note that this function is called before symbols are forced local by
  2258. version scripts. The differing cases are handled by
  2259. elf_cris_hide_symbol. */
  2260. static bool
  2261. elf_cris_try_fold_plt_to_got (struct elf_cris_link_hash_entry *h, void * p)
  2262. {
  2263. struct bfd_link_info *info = (struct bfd_link_info *) p;
  2264. /* If there are no GOT references for this symbol, we can't fold any
  2265. other reference so there's nothing to do. Likewise if there are no
  2266. PLT references; GOTPLT references included. */
  2267. if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
  2268. return true;
  2269. /* GOTPLT relocs are supposed to be included into the PLT refcount. */
  2270. BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
  2271. if (h->gotplt_refcount == h->root.plt.refcount)
  2272. {
  2273. /* The only PLT references are GOTPLT references, and there are GOT
  2274. references. Convert PLT to GOT references. */
  2275. if (! elf_cris_adjust_gotplt_to_got (h, info))
  2276. return false;
  2277. /* Clear the PLT references, so no PLT will be created. */
  2278. h->root.plt.offset = (bfd_vma) -1;
  2279. }
  2280. return true;
  2281. }
  2282. /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
  2283. to use a GOT entry (and create one) rather than requiring a GOTPLT
  2284. entry. */
  2285. static void
  2286. elf_cris_hide_symbol (struct bfd_link_info *info,
  2287. struct elf_link_hash_entry *h,
  2288. bool force_local)
  2289. {
  2290. elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
  2291. _bfd_elf_link_hash_hide_symbol (info, h, force_local);
  2292. }
  2293. /* Adjust a symbol defined by a dynamic object and referenced by a
  2294. regular object. The current definition is in some section of the
  2295. dynamic object, but we're not including those sections. We have to
  2296. change the definition to something the rest of the link can
  2297. understand. */
  2298. static bool
  2299. elf_cris_adjust_dynamic_symbol (struct bfd_link_info *info,
  2300. struct elf_link_hash_entry *h)
  2301. {
  2302. struct elf_cris_link_hash_table * htab;
  2303. bfd *dynobj;
  2304. asection *s;
  2305. asection *srel;
  2306. bfd_size_type plt_entry_size;
  2307. htab = elf_cris_hash_table (info);
  2308. if (htab == NULL)
  2309. return false;
  2310. dynobj = htab->root.dynobj;
  2311. /* Make sure we know what is going on here. */
  2312. BFD_ASSERT (dynobj != NULL
  2313. && (h->needs_plt
  2314. || h->is_weakalias
  2315. || (h->def_dynamic
  2316. && h->ref_regular
  2317. && !h->def_regular)));
  2318. plt_entry_size
  2319. = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
  2320. ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
  2321. /* If this is a function, put it in the procedure linkage table. We
  2322. will fill in the contents of the procedure linkage table later,
  2323. when we know the address of the .got section. */
  2324. if (h->type == STT_FUNC
  2325. || h->needs_plt)
  2326. {
  2327. /* If we link a program (not a DSO), we'll get rid of unnecessary
  2328. PLT entries; we point to the actual symbols -- even for pic
  2329. relocs, because a program built with -fpic should have the same
  2330. result as one built without -fpic, specifically considering weak
  2331. symbols.
  2332. FIXME: m68k and i386 differ here, for unclear reasons. */
  2333. if (! bfd_link_pic (info)
  2334. && !h->def_dynamic)
  2335. {
  2336. /* This case can occur if we saw a PLT reloc in an input file,
  2337. but the symbol was not defined by a dynamic object. In such
  2338. a case, we don't actually need to build a procedure linkage
  2339. table, and we can just do an absolute or PC reloc instead, or
  2340. change a .got.plt index to a .got index for GOTPLT relocs. */
  2341. BFD_ASSERT (h->needs_plt);
  2342. h->needs_plt = 0;
  2343. h->plt.offset = (bfd_vma) -1;
  2344. return
  2345. elf_cris_adjust_gotplt_to_got ((struct
  2346. elf_cris_link_hash_entry *) h,
  2347. info);
  2348. }
  2349. /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
  2350. where a pointer-equivalent symbol was unimportant (i.e. more
  2351. like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
  2352. of the PLT. We can't for the executable, because the GOT
  2353. entries will point to the PLT there (and be constant). */
  2354. if (bfd_link_pic (info)
  2355. && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
  2356. h, info))
  2357. return false;
  2358. /* GC or folding may have rendered this entry unused. */
  2359. if (h->plt.refcount <= 0)
  2360. {
  2361. h->needs_plt = 0;
  2362. h->plt.offset = (bfd_vma) -1;
  2363. return true;
  2364. }
  2365. /* Make sure this symbol is output as a dynamic symbol. */
  2366. if (h->dynindx == -1)
  2367. {
  2368. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2369. return false;
  2370. }
  2371. s = htab->root.splt;
  2372. BFD_ASSERT (s != NULL);
  2373. /* If this is the first .plt entry, make room for the special
  2374. first entry. */
  2375. if (s->size == 0)
  2376. s->size += plt_entry_size;
  2377. /* If this symbol is not defined in a regular file, and we are
  2378. not generating a shared library, then set the symbol to this
  2379. location in the .plt. */
  2380. if (!bfd_link_pic (info)
  2381. && !h->def_regular)
  2382. {
  2383. h->root.u.def.section = s;
  2384. h->root.u.def.value = s->size;
  2385. }
  2386. /* If there's already a GOT entry, use that, not a .got.plt. A
  2387. GOT field still has a reference count when we get here; it's
  2388. not yet changed to an offset. We can't do this for an
  2389. executable, because then the reloc associated with the PLT
  2390. would get a non-PLT reloc pointing to the PLT. FIXME: Move
  2391. this to elf_cris_try_fold_plt_to_got. */
  2392. if (bfd_link_pic (info) && h->got.refcount > 0)
  2393. {
  2394. h->got.refcount += h->plt.refcount;
  2395. /* Mark the PLT offset to use the GOT entry by setting the low
  2396. bit in the plt offset; it is always a multiple of
  2397. plt_entry_size (which is at least a multiple of 2). */
  2398. BFD_ASSERT ((s->size % plt_entry_size) == 0);
  2399. /* Change the PLT refcount to an offset. */
  2400. h->plt.offset = s->size;
  2401. /* By not setting gotplt_offset (i.e. it remains at 0), we signal
  2402. that the got entry should be used instead. */
  2403. BFD_ASSERT (((struct elf_cris_link_hash_entry *)
  2404. h)->gotplt_offset == 0);
  2405. /* Make room for this entry. */
  2406. s->size += plt_entry_size;
  2407. return true;
  2408. }
  2409. /* No GOT reference for this symbol; prepare for an ordinary PLT. */
  2410. h->plt.offset = s->size;
  2411. /* Make room for this entry. */
  2412. s->size += plt_entry_size;
  2413. /* We also need to make an entry in the .got.plt section, which
  2414. will be placed in the .got section by the linker script. */
  2415. ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
  2416. = htab->next_gotplt_entry;
  2417. htab->next_gotplt_entry += 4;
  2418. s = htab->root.sgotplt;
  2419. BFD_ASSERT (s != NULL);
  2420. s->size += 4;
  2421. /* We also need to make an entry in the .rela.plt section. */
  2422. s = htab->root.srelplt;
  2423. BFD_ASSERT (s != NULL);
  2424. s->size += sizeof (Elf32_External_Rela);
  2425. return true;
  2426. }
  2427. /* Reinitialize the plt offset now that it is not used as a reference
  2428. count any more. */
  2429. h->plt.offset = (bfd_vma) -1;
  2430. /* If this is a weak symbol, and there is a real definition, the
  2431. processor independent code will have arranged for us to see the
  2432. real definition first, and we can just use the same value. */
  2433. if (h->is_weakalias)
  2434. {
  2435. struct elf_link_hash_entry *def = weakdef (h);
  2436. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  2437. h->root.u.def.section = def->root.u.def.section;
  2438. h->root.u.def.value = def->root.u.def.value;
  2439. return true;
  2440. }
  2441. /* This is a reference to a symbol defined by a dynamic object which
  2442. is not a function. */
  2443. /* If we are creating a shared library, we must presume that the
  2444. only references to the symbol are via the global offset table.
  2445. For such cases we need not do anything here; the relocations will
  2446. be handled correctly by relocate_section. */
  2447. if (bfd_link_pic (info))
  2448. return true;
  2449. /* If there are no references to this symbol that do not use the
  2450. GOT, we don't need to generate a copy reloc. */
  2451. if (!h->non_got_ref)
  2452. return true;
  2453. /* We must allocate the symbol in our .dynbss section, which will
  2454. become part of the .bss section of the executable. There will be
  2455. an entry for this symbol in the .dynsym section. The dynamic
  2456. object will contain position independent code, so all references
  2457. from the dynamic object to this symbol will go through the global
  2458. offset table. The dynamic linker will use the .dynsym entry to
  2459. determine the address it must put in the global offset table, so
  2460. both the dynamic object and the regular object will refer to the
  2461. same memory location for the variable. */
  2462. /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
  2463. copy the initial value out of the dynamic object and into the
  2464. runtime process image. We need to remember the offset into the
  2465. .rela.bss section we are going to use. */
  2466. if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  2467. {
  2468. s = htab->root.sdynrelro;
  2469. srel = htab->root.sreldynrelro;
  2470. }
  2471. else
  2472. {
  2473. s = htab->root.sdynbss;
  2474. srel = htab->root.srelbss;
  2475. }
  2476. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  2477. {
  2478. BFD_ASSERT (srel != NULL);
  2479. srel->size += sizeof (Elf32_External_Rela);
  2480. h->needs_copy = 1;
  2481. }
  2482. BFD_ASSERT (s != NULL);
  2483. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  2484. }
  2485. /* Adjust our "subclass" elements for an indirect symbol. */
  2486. static void
  2487. elf_cris_copy_indirect_symbol (struct bfd_link_info *info,
  2488. struct elf_link_hash_entry *dir,
  2489. struct elf_link_hash_entry *ind)
  2490. {
  2491. struct elf_cris_link_hash_entry *edir, *eind;
  2492. edir = (struct elf_cris_link_hash_entry *) dir;
  2493. eind = (struct elf_cris_link_hash_entry *) ind;
  2494. /* Only indirect symbols are replaced; we're not interested in
  2495. updating any of EIND's fields for other symbols. */
  2496. if (eind->root.root.type != bfd_link_hash_indirect)
  2497. {
  2498. /* Still, we need to copy flags for e.g. weak definitions. */
  2499. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  2500. return;
  2501. }
  2502. BFD_ASSERT (edir->gotplt_offset == 0 || eind->gotplt_offset == 0);
  2503. #define XMOVOPZ(F, OP, Z) edir->F OP eind->F; eind->F = Z
  2504. #define XMOVE(F) XMOVOPZ (F, +=, 0)
  2505. if (eind->pcrel_relocs_copied != NULL)
  2506. {
  2507. if (edir->pcrel_relocs_copied != NULL)
  2508. {
  2509. struct elf_cris_pcrel_relocs_copied **pp;
  2510. struct elf_cris_pcrel_relocs_copied *p;
  2511. /* Add reloc counts against the indirect sym to the direct sym
  2512. list. Merge any entries against the same section. */
  2513. for (pp = &eind->pcrel_relocs_copied; *pp != NULL;)
  2514. {
  2515. struct elf_cris_pcrel_relocs_copied *q;
  2516. p = *pp;
  2517. for (q = edir->pcrel_relocs_copied; q != NULL; q = q->next)
  2518. if (q->section == p->section)
  2519. {
  2520. q->count += p->count;
  2521. *pp = p->next;
  2522. break;
  2523. }
  2524. if (q == NULL)
  2525. pp = &p->next;
  2526. }
  2527. *pp = edir->pcrel_relocs_copied;
  2528. }
  2529. XMOVOPZ (pcrel_relocs_copied, =, NULL);
  2530. }
  2531. XMOVE (gotplt_refcount);
  2532. XMOVE (gotplt_offset);
  2533. XMOVE (reg_got_refcount);
  2534. XMOVE (tprel_refcount);
  2535. XMOVE (dtp_refcount);
  2536. #undef XMOVE
  2537. #undef XMOVOPZ
  2538. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  2539. }
  2540. /* Look through the relocs for a section during the first phase. */
  2541. static bool
  2542. cris_elf_check_relocs (bfd *abfd,
  2543. struct bfd_link_info *info,
  2544. asection *sec,
  2545. const Elf_Internal_Rela *relocs)
  2546. {
  2547. struct elf_cris_link_hash_table * htab;
  2548. bfd *dynobj;
  2549. Elf_Internal_Shdr *symtab_hdr;
  2550. struct elf_link_hash_entry **sym_hashes;
  2551. bfd_signed_vma *local_got_refcounts;
  2552. const Elf_Internal_Rela *rel;
  2553. const Elf_Internal_Rela *rel_end;
  2554. asection *sgot;
  2555. asection *srelgot;
  2556. asection *sreloc;
  2557. if (bfd_link_relocatable (info))
  2558. return true;
  2559. htab = elf_cris_hash_table (info);
  2560. if (htab == NULL)
  2561. return false;
  2562. dynobj = elf_hash_table (info)->dynobj;
  2563. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2564. sym_hashes = elf_sym_hashes (abfd);
  2565. local_got_refcounts = elf_local_got_refcounts (abfd);
  2566. sgot = NULL;
  2567. srelgot = NULL;
  2568. sreloc = NULL;
  2569. rel_end = relocs + sec->reloc_count;
  2570. for (rel = relocs; rel < rel_end; rel++)
  2571. {
  2572. struct elf_link_hash_entry *h;
  2573. unsigned long r_symndx;
  2574. enum elf_cris_reloc_type r_type;
  2575. bfd_signed_vma got_element_size = 4;
  2576. unsigned long r_symndx_lgot = INT_MAX;
  2577. r_symndx = ELF32_R_SYM (rel->r_info);
  2578. if (r_symndx < symtab_hdr->sh_info)
  2579. {
  2580. h = NULL;
  2581. r_symndx_lgot = LGOT_REG_NDX (r_symndx);
  2582. }
  2583. else
  2584. {
  2585. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  2586. while (h->root.type == bfd_link_hash_indirect
  2587. || h->root.type == bfd_link_hash_warning)
  2588. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2589. }
  2590. r_type = ELF32_R_TYPE (rel->r_info);
  2591. /* Some relocs require linker-created sections; we need to hang them
  2592. on the first input bfd we found that contained dynamic relocs. */
  2593. switch (r_type)
  2594. {
  2595. case R_CRIS_32_DTPREL:
  2596. if ((sec->flags & SEC_ALLOC) == 0)
  2597. /* This'd be a .dtpreld entry in e.g. debug info. We have
  2598. several different switch statements below, but none of
  2599. that is needed; we need no preparations for resolving
  2600. R_CRIS_32_DTPREL into a non-allocated section (debug
  2601. info), so let's just move on to the next
  2602. relocation. */
  2603. continue;
  2604. /* Fall through. */
  2605. case R_CRIS_16_DTPREL:
  2606. /* The first .got.plt entry is right after the R_CRIS_DTPMOD
  2607. entry at index 3. */
  2608. if (htab->dtpmod_refcount == 0)
  2609. htab->next_gotplt_entry += 8;
  2610. htab->dtpmod_refcount++;
  2611. /* Fall through. */
  2612. case R_CRIS_32_IE:
  2613. case R_CRIS_32_GD:
  2614. case R_CRIS_16_GOT_GD:
  2615. case R_CRIS_32_GOT_GD:
  2616. case R_CRIS_32_GOT_TPREL:
  2617. case R_CRIS_16_GOT_TPREL:
  2618. case R_CRIS_16_GOT:
  2619. case R_CRIS_32_GOT:
  2620. case R_CRIS_32_GOTREL:
  2621. case R_CRIS_32_PLT_GOTREL:
  2622. case R_CRIS_32_PLT_PCREL:
  2623. case R_CRIS_16_GOTPLT:
  2624. case R_CRIS_32_GOTPLT:
  2625. if (dynobj == NULL)
  2626. {
  2627. elf_hash_table (info)->dynobj = dynobj = abfd;
  2628. /* We could handle this if we can get a handle on the
  2629. output bfd in elf_cris_adjust_dynamic_symbol. Failing
  2630. that, we must insist on dynobj being a specific mach. */
  2631. if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
  2632. {
  2633. _bfd_error_handler
  2634. /* xgettext:c-format */
  2635. (_("%pB, section %pA: v10/v32 compatible object"
  2636. " must not contain a PIC relocation"),
  2637. abfd, sec);
  2638. return false;
  2639. }
  2640. }
  2641. if (sgot == NULL)
  2642. {
  2643. /* We may have a dynobj but no .got section, if machine-
  2644. independent parts of the linker found a reason to create
  2645. a dynobj. We want to create the .got section now, so we
  2646. can assume it's always present whenever there's a dynobj.
  2647. It's ok to call this function more than once. */
  2648. if (!_bfd_elf_create_got_section (dynobj, info))
  2649. return false;
  2650. sgot = elf_hash_table (info)->sgot;
  2651. srelgot = elf_hash_table (info)->srelgot;
  2652. }
  2653. if (local_got_refcounts == NULL)
  2654. {
  2655. bfd_size_type amt;
  2656. /* We use index local_got_refcounts[-1] to count all
  2657. GOT-relative relocations that do not have explicit
  2658. GOT entries. */
  2659. amt = LGOT_ALLOC_NELTS_FOR (symtab_hdr->sh_info) + 1;
  2660. amt *= sizeof (bfd_signed_vma);
  2661. local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
  2662. if (local_got_refcounts == NULL)
  2663. return false;
  2664. local_got_refcounts++;
  2665. elf_local_got_refcounts (abfd) = local_got_refcounts;
  2666. }
  2667. break;
  2668. default:
  2669. break;
  2670. }
  2671. /* Warn and error for invalid input. */
  2672. switch (r_type)
  2673. {
  2674. case R_CRIS_32_IE:
  2675. case R_CRIS_32_TPREL:
  2676. case R_CRIS_16_TPREL:
  2677. case R_CRIS_32_GD:
  2678. if (bfd_link_pic (info))
  2679. {
  2680. _bfd_error_handler
  2681. /* xgettext:c-format */
  2682. (_("%pB, section %pA:\n relocation %s not valid"
  2683. " in a shared object;"
  2684. " typically an option mixup, recompile with -fPIC"),
  2685. abfd,
  2686. sec,
  2687. cris_elf_howto_table[r_type].name);
  2688. /* Don't return FALSE here; we want messages for all of
  2689. these and the error behavior is ungraceful
  2690. anyway. */
  2691. }
  2692. default:
  2693. break;
  2694. }
  2695. switch (r_type)
  2696. {
  2697. case R_CRIS_32_GD:
  2698. case R_CRIS_16_GOT_GD:
  2699. case R_CRIS_32_GOT_GD:
  2700. /* These are requests for tls_index entries, run-time R_CRIS_DTP. */
  2701. got_element_size = 8;
  2702. r_symndx_lgot = LGOT_DTP_NDX (r_symndx);
  2703. break;
  2704. case R_CRIS_16_DTPREL:
  2705. case R_CRIS_32_DTPREL:
  2706. /* These two just request for the constant-index
  2707. module-local tls_index-sized GOT entry, which we add
  2708. elsewhere. */
  2709. break;
  2710. case R_CRIS_32_IE:
  2711. case R_CRIS_32_GOT_TPREL:
  2712. case R_CRIS_16_GOT_TPREL:
  2713. r_symndx_lgot = LGOT_TPREL_NDX (r_symndx);
  2714. /* Those relocs also require that a DSO is of type
  2715. Initial Exec. Like other targets, we don't reset this
  2716. flag even if the relocs are GC:ed away. */
  2717. if (bfd_link_pic (info))
  2718. info->flags |= DF_STATIC_TLS;
  2719. break;
  2720. /* Let's list the other assembler-generated TLS-relocs too,
  2721. just to show that they're not forgotten. */
  2722. case R_CRIS_16_TPREL:
  2723. case R_CRIS_32_TPREL:
  2724. default:
  2725. break;
  2726. }
  2727. switch (r_type)
  2728. {
  2729. case R_CRIS_16_GOTPLT:
  2730. case R_CRIS_32_GOTPLT:
  2731. /* Mark that we need a GOT entry if the PLT entry (and its GOT
  2732. entry) is eliminated. We can only do this for a non-local
  2733. symbol. */
  2734. if (h != NULL)
  2735. {
  2736. elf_cris_hash_entry (h)->gotplt_refcount++;
  2737. goto handle_gotplt_reloc;
  2738. }
  2739. /* If h is NULL then this is a local symbol, and we must make a
  2740. GOT entry for it, so handle it like a GOT reloc. */
  2741. /* Fall through. */
  2742. case R_CRIS_32_IE:
  2743. case R_CRIS_32_GD:
  2744. case R_CRIS_16_GOT_GD:
  2745. case R_CRIS_32_GOT_GD:
  2746. case R_CRIS_32_GOT_TPREL:
  2747. case R_CRIS_16_GOT_TPREL:
  2748. case R_CRIS_16_GOT:
  2749. case R_CRIS_32_GOT:
  2750. /* This symbol requires a global offset table entry. */
  2751. if (h != NULL)
  2752. {
  2753. if (h->got.refcount == 0)
  2754. {
  2755. /* Make sure this symbol is output as a dynamic symbol. */
  2756. if (h->dynindx == -1)
  2757. {
  2758. if (!bfd_elf_link_record_dynamic_symbol (info, h))
  2759. return false;
  2760. }
  2761. }
  2762. /* Update the sum of reloc counts for this symbol. */
  2763. h->got.refcount++;
  2764. switch (r_type)
  2765. {
  2766. case R_CRIS_16_GOT:
  2767. case R_CRIS_32_GOT:
  2768. if (elf_cris_hash_entry (h)->reg_got_refcount == 0)
  2769. {
  2770. /* Allocate space in the .got section. */
  2771. sgot->size += got_element_size;
  2772. /* Allocate relocation space. */
  2773. srelgot->size += sizeof (Elf32_External_Rela);
  2774. }
  2775. elf_cris_hash_entry (h)->reg_got_refcount++;
  2776. break;
  2777. case R_CRIS_32_GD:
  2778. case R_CRIS_16_GOT_GD:
  2779. case R_CRIS_32_GOT_GD:
  2780. if (elf_cris_hash_entry (h)->dtp_refcount == 0)
  2781. {
  2782. /* Allocate space in the .got section. */
  2783. sgot->size += got_element_size;
  2784. /* Allocate relocation space. */
  2785. srelgot->size += sizeof (Elf32_External_Rela);
  2786. }
  2787. elf_cris_hash_entry (h)->dtp_refcount++;
  2788. break;
  2789. case R_CRIS_32_IE:
  2790. case R_CRIS_32_GOT_TPREL:
  2791. case R_CRIS_16_GOT_TPREL:
  2792. if (elf_cris_hash_entry (h)->tprel_refcount == 0)
  2793. {
  2794. /* Allocate space in the .got section. */
  2795. sgot->size += got_element_size;
  2796. /* Allocate relocation space. */
  2797. srelgot->size += sizeof (Elf32_External_Rela);
  2798. }
  2799. elf_cris_hash_entry (h)->tprel_refcount++;
  2800. break;
  2801. default:
  2802. BFD_FAIL ();
  2803. break;
  2804. }
  2805. }
  2806. else
  2807. {
  2808. /* This is a global offset table entry for a local symbol. */
  2809. if (local_got_refcounts[r_symndx_lgot] == 0)
  2810. {
  2811. sgot->size += got_element_size;
  2812. if (bfd_link_pic (info))
  2813. {
  2814. /* If we are generating a shared object, we need
  2815. to output a R_CRIS_RELATIVE reloc so that the
  2816. dynamic linker can adjust this GOT entry.
  2817. Similarly for non-regular got entries. */
  2818. srelgot->size += sizeof (Elf32_External_Rela);
  2819. }
  2820. }
  2821. /* Update the reloc-specific count. */
  2822. local_got_refcounts[r_symndx_lgot]++;
  2823. /* This one is the sum of all the others. */
  2824. local_got_refcounts[r_symndx]++;
  2825. }
  2826. break;
  2827. case R_CRIS_16_DTPREL:
  2828. case R_CRIS_32_DTPREL:
  2829. case R_CRIS_32_GOTREL:
  2830. /* This reference requires a global offset table.
  2831. FIXME: The actual refcount isn't used currently; the .got
  2832. section can't be removed if there were any references in the
  2833. input. */
  2834. local_got_refcounts[-1]++;
  2835. break;
  2836. handle_gotplt_reloc:
  2837. case R_CRIS_32_PLT_GOTREL:
  2838. /* This reference requires a global offset table. */
  2839. local_got_refcounts[-1]++;
  2840. /* Fall through. */
  2841. case R_CRIS_32_PLT_PCREL:
  2842. /* This symbol requires a procedure linkage table entry. We
  2843. actually build the entry in adjust_dynamic_symbol,
  2844. because this might be a case of linking PIC code which is
  2845. never referenced by a dynamic object, in which case we
  2846. don't need to generate a procedure linkage table entry
  2847. after all. */
  2848. /* Beware: if we'd check for visibility of the symbol here
  2849. (and not marking the need for a PLT when non-visible), we'd
  2850. get into trouble with keeping handling consistent with
  2851. regards to relocs found before definition and GOTPLT
  2852. handling. Eliminable PLT entries will be dealt with later
  2853. anyway. */
  2854. if (h == NULL)
  2855. continue;
  2856. h->needs_plt = 1;
  2857. /* If the symbol is forced local, the refcount is unavailable. */
  2858. if (h->plt.refcount != -1)
  2859. h->plt.refcount++;
  2860. break;
  2861. case R_CRIS_8:
  2862. case R_CRIS_16:
  2863. case R_CRIS_32:
  2864. /* Let's help debug shared library creation. Any of these
  2865. relocs *can* be used in shared libs, but pages containing
  2866. them cannot be shared, so they're not appropriate for
  2867. common use. Don't warn for sections we don't care about,
  2868. such as debug sections or non-constant sections. We
  2869. can't help tables of (global) function pointers, for
  2870. example, though they must be emitted in a (writable) data
  2871. section to avoid having impure text sections. */
  2872. if (bfd_link_pic (info)
  2873. && (sec->flags & SEC_ALLOC) != 0
  2874. && (sec->flags & SEC_READONLY) != 0)
  2875. {
  2876. /* FIXME: How do we make this optionally a warning only? */
  2877. _bfd_error_handler
  2878. /* xgettext:c-format */
  2879. (_("%pB, section %pA: relocation %s should not"
  2880. " be used in a shared object; recompile with -fPIC"),
  2881. abfd,
  2882. sec,
  2883. cris_elf_howto_table[r_type].name);
  2884. }
  2885. /* We don't need to handle relocs into sections not going into
  2886. the "real" output. */
  2887. if ((sec->flags & SEC_ALLOC) == 0)
  2888. break;
  2889. if (h != NULL)
  2890. {
  2891. h->non_got_ref = 1;
  2892. /* Make sure a plt entry is created for this symbol if it
  2893. turns out to be a function defined by a dynamic object. */
  2894. if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  2895. h->plt.refcount++;
  2896. }
  2897. /* If we are creating a shared library and this is not a local
  2898. symbol, we need to copy the reloc into the shared library.
  2899. However when linking with -Bsymbolic and this is a global
  2900. symbol which is defined in an object we are including in the
  2901. link (i.e., DEF_REGULAR is set), then we can resolve the
  2902. reloc directly. At this point we have not seen all the input
  2903. files, so it is possible that DEF_REGULAR is not set now but
  2904. will be set later (it is never cleared). In case of a weak
  2905. definition, DEF_REGULAR may be cleared later by a strong
  2906. definition in a shared library. We account for that
  2907. possibility below by storing information in the relocs_copied
  2908. field of the hash table entry. A similar situation occurs
  2909. when creating shared libraries and symbol visibility changes
  2910. render the symbol local. */
  2911. /* No need to do anything if we're not creating a shared object. */
  2912. if (! bfd_link_pic (info)
  2913. || (h != NULL && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
  2914. break;
  2915. /* We may need to create a reloc section in the dynobj and made room
  2916. for this reloc. */
  2917. if (sreloc == NULL)
  2918. {
  2919. sreloc = _bfd_elf_make_dynamic_reloc_section
  2920. (sec, dynobj, 2, abfd, /*rela?*/ true);
  2921. if (sreloc == NULL)
  2922. return false;
  2923. }
  2924. if (sec->flags & SEC_READONLY)
  2925. info->flags |= DF_TEXTREL;
  2926. sreloc->size += sizeof (Elf32_External_Rela);
  2927. break;
  2928. case R_CRIS_8_PCREL:
  2929. case R_CRIS_16_PCREL:
  2930. case R_CRIS_32_PCREL:
  2931. if (h != NULL)
  2932. {
  2933. h->non_got_ref = 1;
  2934. /* Make sure a plt entry is created for this symbol if it
  2935. turns out to be a function defined by a dynamic object. */
  2936. if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  2937. h->plt.refcount++;
  2938. }
  2939. /* If we are creating a shared library and this is not a local
  2940. symbol, we need to copy the reloc into the shared library.
  2941. However when linking with -Bsymbolic and this is a global
  2942. symbol which is defined in an object we are including in the
  2943. link (i.e., DEF_REGULAR is set), then we can resolve the
  2944. reloc directly. At this point we have not seen all the input
  2945. files, so it is possible that DEF_REGULAR is not set now but
  2946. will be set later (it is never cleared). In case of a weak
  2947. definition, DEF_REGULAR may be cleared later by a strong
  2948. definition in a shared library. We account for that
  2949. possibility below by storing information in the relocs_copied
  2950. field of the hash table entry. A similar situation occurs
  2951. when creating shared libraries and symbol visibility changes
  2952. render the symbol local. */
  2953. /* No need to do anything if we're not creating a shared object. */
  2954. if (! bfd_link_pic (info))
  2955. break;
  2956. /* We don't need to handle relocs into sections not going into
  2957. the "real" output. */
  2958. if ((sec->flags & SEC_ALLOC) == 0)
  2959. break;
  2960. /* If the symbol is local, then we know already we can
  2961. eliminate the reloc. */
  2962. if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  2963. break;
  2964. /* If this is with -Bsymbolic and the symbol isn't weak, and
  2965. is defined by an ordinary object (the ones we include in
  2966. this shared library) then we can also eliminate the
  2967. reloc. See comment above for more eliminable cases which
  2968. we can't identify at this time. */
  2969. if (SYMBOLIC_BIND (info, h)
  2970. && h->root.type != bfd_link_hash_defweak
  2971. && h->def_regular)
  2972. break;
  2973. /* We may need to create a reloc section in the dynobj and made room
  2974. for this reloc. */
  2975. if (sreloc == NULL)
  2976. {
  2977. sreloc = _bfd_elf_make_dynamic_reloc_section
  2978. (sec, dynobj, 2, abfd, /*rela?*/ true);
  2979. if (sreloc == NULL)
  2980. return false;
  2981. }
  2982. sreloc->size += sizeof (Elf32_External_Rela);
  2983. /* We count the number of PC relative relocations we have
  2984. entered for this symbol, so that we can discard them
  2985. again if the symbol is later defined by a regular object.
  2986. We know that h is really a pointer to an
  2987. elf_cris_link_hash_entry. */
  2988. {
  2989. struct elf_cris_link_hash_entry *eh;
  2990. struct elf_cris_pcrel_relocs_copied *p;
  2991. eh = elf_cris_hash_entry (h);
  2992. for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
  2993. if (p->section == sec)
  2994. break;
  2995. if (p == NULL)
  2996. {
  2997. p = ((struct elf_cris_pcrel_relocs_copied *)
  2998. bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
  2999. if (p == NULL)
  3000. return false;
  3001. p->next = eh->pcrel_relocs_copied;
  3002. eh->pcrel_relocs_copied = p;
  3003. p->section = sec;
  3004. p->count = 0;
  3005. p->r_type = r_type;
  3006. }
  3007. ++p->count;
  3008. }
  3009. break;
  3010. /* This relocation describes the C++ object vtable hierarchy.
  3011. Reconstruct it for later use during GC. */
  3012. case R_CRIS_GNU_VTINHERIT:
  3013. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  3014. return false;
  3015. break;
  3016. /* This relocation describes which C++ vtable entries are actually
  3017. used. Record for later use during GC. */
  3018. case R_CRIS_GNU_VTENTRY:
  3019. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  3020. return false;
  3021. break;
  3022. case R_CRIS_16_TPREL:
  3023. case R_CRIS_32_TPREL:
  3024. /* Already warned above, when necessary. */
  3025. break;
  3026. default:
  3027. /* Other relocs do not appear here. */
  3028. bfd_set_error (bfd_error_bad_value);
  3029. return false;
  3030. }
  3031. }
  3032. return true;
  3033. }
  3034. /* Set the sizes of the dynamic sections. */
  3035. static bool
  3036. elf_cris_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  3037. struct bfd_link_info *info)
  3038. {
  3039. struct elf_cris_link_hash_table * htab;
  3040. bfd *dynobj;
  3041. asection *s;
  3042. bool relocs;
  3043. htab = elf_cris_hash_table (info);
  3044. if (htab == NULL)
  3045. return false;
  3046. dynobj = htab->root.dynobj;
  3047. BFD_ASSERT (dynobj != NULL);
  3048. if (htab->root.dynamic_sections_created)
  3049. {
  3050. /* Set the contents of the .interp section to the interpreter. */
  3051. if (bfd_link_executable (info) && !info->nointerp)
  3052. {
  3053. s = bfd_get_linker_section (dynobj, ".interp");
  3054. BFD_ASSERT (s != NULL);
  3055. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  3056. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  3057. }
  3058. }
  3059. else
  3060. {
  3061. /* Adjust all expected GOTPLT uses to use a GOT entry instead. */
  3062. elf_cris_link_hash_traverse (htab, elf_cris_adjust_gotplt_to_got,
  3063. info);
  3064. /* We may have created entries in the .rela.got section.
  3065. However, if we are not creating the dynamic sections, we will
  3066. not actually use these entries. Reset the size of .rela.got,
  3067. which will cause it to get stripped from the output file
  3068. below. */
  3069. s = htab->root.srelgot;
  3070. if (s != NULL)
  3071. s->size = 0;
  3072. }
  3073. /* If this is a -Bsymbolic shared link, then we need to discard all PC
  3074. relative relocs against symbols defined in a regular object. We
  3075. allocated space for them in the check_relocs routine, but we will not
  3076. fill them in in the relocate_section routine. We also discard space
  3077. for relocs that have become for local symbols due to symbol
  3078. visibility changes. For programs, we discard space for relocs for
  3079. symbols not referenced by any dynamic object. */
  3080. if (bfd_link_pic (info))
  3081. elf_cris_link_hash_traverse (htab,
  3082. elf_cris_discard_excess_dso_dynamics,
  3083. info);
  3084. else
  3085. elf_cris_link_hash_traverse (htab,
  3086. elf_cris_discard_excess_program_dynamics,
  3087. info);
  3088. /* The check_relocs and adjust_dynamic_symbol entry points have
  3089. determined the sizes of the various dynamic sections. Allocate
  3090. memory for them. */
  3091. relocs = false;
  3092. for (s = dynobj->sections; s != NULL; s = s->next)
  3093. {
  3094. const char *name;
  3095. if ((s->flags & SEC_LINKER_CREATED) == 0)
  3096. continue;
  3097. /* It's OK to base decisions on the section name, because none
  3098. of the dynobj section names depend upon the input files. */
  3099. name = bfd_section_name (s);
  3100. if (strcmp (name, ".plt") == 0)
  3101. {
  3102. ;
  3103. }
  3104. else if (strcmp (name, ".got.plt") == 0)
  3105. {
  3106. /* The .got.plt contains the .got header as well as the
  3107. actual .got.plt contents. The .got header may contain a
  3108. R_CRIS_DTPMOD entry at index 3. */
  3109. s->size += htab->dtpmod_refcount != 0
  3110. ? 8 : 0;
  3111. }
  3112. else if (startswith (name, ".rela"))
  3113. {
  3114. if (strcmp (name, ".rela.got") == 0
  3115. && htab->dtpmod_refcount != 0
  3116. && bfd_link_pic (info))
  3117. s->size += sizeof (Elf32_External_Rela);
  3118. if (s->size != 0)
  3119. {
  3120. /* Remember whether there are any reloc sections other
  3121. than .rela.plt. */
  3122. if (strcmp (name, ".rela.plt") != 0)
  3123. relocs = true;
  3124. /* We use the reloc_count field as a counter if we need
  3125. to copy relocs into the output file. */
  3126. s->reloc_count = 0;
  3127. }
  3128. }
  3129. else if (! startswith (name, ".got")
  3130. && strcmp (name, ".dynbss") != 0
  3131. && s != htab->root.sdynrelro)
  3132. {
  3133. /* It's not one of our sections, so don't allocate space. */
  3134. continue;
  3135. }
  3136. if (s->size == 0)
  3137. {
  3138. /* If we don't need this section, strip it from the
  3139. output file. This is mostly to handle .rela.bss and
  3140. .rela.plt. We must create both sections in
  3141. create_dynamic_sections, because they must be created
  3142. before the linker maps input sections to output
  3143. sections. The linker does that before
  3144. adjust_dynamic_symbol is called, and it is that
  3145. function which decides whether anything needs to go
  3146. into these sections. */
  3147. s->flags |= SEC_EXCLUDE;
  3148. continue;
  3149. }
  3150. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  3151. continue;
  3152. /* Allocate memory for the section contents. We use bfd_zalloc here
  3153. in case unused entries are not reclaimed before the section's
  3154. contents are written out. This should not happen, but this way
  3155. if it does, we will not write out garbage. For reloc sections,
  3156. this will make entries have the type R_CRIS_NONE. */
  3157. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  3158. if (s->contents == NULL)
  3159. return false;
  3160. }
  3161. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
  3162. }
  3163. /* This function is called via elf_cris_link_hash_traverse if we are
  3164. creating a shared object. In the -Bsymbolic case, it discards the
  3165. space allocated to copy PC relative relocs against symbols which
  3166. are defined in regular objects. For the normal non-symbolic case,
  3167. we also discard space for relocs that have become local due to
  3168. symbol visibility changes. We allocated space for them in the
  3169. check_relocs routine, but we won't fill them in in the
  3170. relocate_section routine. */
  3171. static bool
  3172. elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *h,
  3173. void * inf)
  3174. {
  3175. struct elf_cris_pcrel_relocs_copied *s;
  3176. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  3177. /* If a symbol has been forced local or we have found a regular
  3178. definition for the symbolic link case, then we won't be needing
  3179. any relocs. */
  3180. if (h->root.def_regular
  3181. && (h->root.forced_local
  3182. || SYMBOLIC_BIND (info, &h->root)))
  3183. {
  3184. for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
  3185. {
  3186. asection *sreloc
  3187. = _bfd_elf_get_dynamic_reloc_section (elf_hash_table (info)
  3188. ->dynobj,
  3189. s->section,
  3190. /*rela?*/ true);
  3191. sreloc->size -= s->count * sizeof (Elf32_External_Rela);
  3192. }
  3193. return true;
  3194. }
  3195. /* If we have accounted for PC-relative relocs for read-only
  3196. sections, now is the time to warn for them. We can't do it in
  3197. cris_elf_check_relocs, because we don't know the status of all
  3198. symbols at that time (and it's common to force symbols local
  3199. late). */
  3200. for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
  3201. if ((s->section->flags & SEC_READONLY) != 0)
  3202. {
  3203. /* FIXME: How do we make this optionally a warning only? */
  3204. _bfd_error_handler
  3205. /* xgettext:c-format */
  3206. (_("%pB, section `%pA', to symbol `%s':"
  3207. " relocation %s should not be used"
  3208. " in a shared object; recompile with -fPIC"),
  3209. s->section->owner,
  3210. s->section,
  3211. h->root.root.root.string,
  3212. cris_elf_howto_table[s->r_type].name);
  3213. info->flags |= DF_TEXTREL;
  3214. }
  3215. return true;
  3216. }
  3217. /* This function is called via elf_cris_link_hash_traverse if we are *not*
  3218. creating a shared object. We discard space for relocs for symbols put
  3219. in the .got, but which we found we do not have to resolve at run-time. */
  3220. static bool
  3221. elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *h,
  3222. void * inf)
  3223. {
  3224. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  3225. /* If we're not creating a shared library and have a symbol which is
  3226. referred to by .got references, but the symbol is defined locally,
  3227. (or rather, not defined by a DSO) then lose the reloc for the .got
  3228. (don't allocate room for it). Likewise for relocs for something
  3229. for which we create a PLT. */
  3230. if (!h->root.def_dynamic
  3231. || h->root.plt.refcount > 0)
  3232. {
  3233. if (h->reg_got_refcount > 0
  3234. /* The size of this section is only valid and in sync with the
  3235. various reference counts if we do dynamic; don't decrement it
  3236. otherwise. */
  3237. && elf_hash_table (info)->dynamic_sections_created)
  3238. {
  3239. bfd *dynobj = elf_hash_table (info)->dynobj;
  3240. asection *srelgot = elf_hash_table (info)->srelgot;
  3241. BFD_ASSERT (dynobj != NULL);
  3242. BFD_ASSERT (srelgot != NULL);
  3243. srelgot->size -= sizeof (Elf32_External_Rela);
  3244. }
  3245. /* If the locally-defined symbol isn't used by a DSO, then we don't
  3246. have to export it as a dynamic symbol. This was already done for
  3247. functions; doing this for all symbols would presumably not
  3248. introduce new problems. Of course we don't do this if we're
  3249. exporting all dynamic symbols, or all data symbols, regardless of
  3250. them being referenced or not. */
  3251. if (! (info->export_dynamic
  3252. || (h->root.type != STT_FUNC && info->dynamic_data))
  3253. && h->root.dynindx != -1
  3254. && !h->root.dynamic
  3255. && !h->root.def_dynamic
  3256. && !h->root.ref_dynamic)
  3257. {
  3258. h->root.dynindx = -1;
  3259. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  3260. h->root.dynstr_index);
  3261. }
  3262. }
  3263. return true;
  3264. }
  3265. /* Reject a file depending on presence and expectation of prefixed
  3266. underscores on symbols. */
  3267. static bool
  3268. cris_elf_object_p (bfd *abfd)
  3269. {
  3270. if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
  3271. return false;
  3272. if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
  3273. return (bfd_get_symbol_leading_char (abfd) == '_');
  3274. else
  3275. return (bfd_get_symbol_leading_char (abfd) == 0);
  3276. }
  3277. /* Mark presence or absence of leading underscore. Set machine type
  3278. flags from mach type. */
  3279. static bool
  3280. cris_elf_final_write_processing (bfd *abfd)
  3281. {
  3282. unsigned long e_flags = elf_elfheader (abfd)->e_flags;
  3283. e_flags &= ~EF_CRIS_UNDERSCORE;
  3284. if (bfd_get_symbol_leading_char (abfd) == '_')
  3285. e_flags |= EF_CRIS_UNDERSCORE;
  3286. switch (bfd_get_mach (abfd))
  3287. {
  3288. case bfd_mach_cris_v0_v10:
  3289. e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
  3290. break;
  3291. case bfd_mach_cris_v10_v32:
  3292. e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
  3293. break;
  3294. case bfd_mach_cris_v32:
  3295. e_flags |= EF_CRIS_VARIANT_V32;
  3296. break;
  3297. default:
  3298. _bfd_abort (__FILE__, __LINE__,
  3299. _("unexpected machine number"));
  3300. }
  3301. elf_elfheader (abfd)->e_flags = e_flags;
  3302. return _bfd_elf_final_write_processing (abfd);
  3303. }
  3304. /* Set the mach type from e_flags value. */
  3305. static bool
  3306. cris_elf_set_mach_from_flags (bfd *abfd,
  3307. unsigned long flags)
  3308. {
  3309. switch (flags & EF_CRIS_VARIANT_MASK)
  3310. {
  3311. case EF_CRIS_VARIANT_ANY_V0_V10:
  3312. bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
  3313. break;
  3314. case EF_CRIS_VARIANT_V32:
  3315. bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
  3316. break;
  3317. case EF_CRIS_VARIANT_COMMON_V10_V32:
  3318. bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
  3319. break;
  3320. default:
  3321. /* Since we don't recognize them, we obviously can't support them
  3322. with this code; we'd have to require that all future handling
  3323. would be optional. */
  3324. bfd_set_error (bfd_error_wrong_format);
  3325. return false;
  3326. }
  3327. return true;
  3328. }
  3329. /* Display the flags field. */
  3330. static bool
  3331. cris_elf_print_private_bfd_data (bfd *abfd, void * ptr)
  3332. {
  3333. FILE *file = (FILE *) ptr;
  3334. BFD_ASSERT (abfd != NULL && ptr != NULL);
  3335. _bfd_elf_print_private_bfd_data (abfd, ptr);
  3336. fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
  3337. if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
  3338. fprintf (file, _(" [symbols have a _ prefix]"));
  3339. if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
  3340. == EF_CRIS_VARIANT_COMMON_V10_V32)
  3341. fprintf (file, _(" [v10 and v32]"));
  3342. if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
  3343. == EF_CRIS_VARIANT_V32)
  3344. fprintf (file, _(" [v32]"));
  3345. fputc ('\n', file);
  3346. return true;
  3347. }
  3348. /* Don't mix files with and without a leading underscore. */
  3349. static bool
  3350. cris_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  3351. {
  3352. bfd *obfd = info->output_bfd;
  3353. int imach, omach;
  3354. if (! _bfd_generic_verify_endian_match (ibfd, info))
  3355. return false;
  3356. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  3357. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  3358. return true;
  3359. imach = bfd_get_mach (ibfd);
  3360. if (! elf_flags_init (obfd))
  3361. {
  3362. /* This happens when ld starts out with a 'blank' output file. */
  3363. elf_flags_init (obfd) = true;
  3364. /* We ignore the linker-set mach, and instead set it according to
  3365. the first input file. This would also happen if we could
  3366. somehow filter out the OUTPUT_ARCH () setting from elf.sc.
  3367. This allows us to keep the same linker config across
  3368. cris(v0..v10) and crisv32. The drawback is that we can't force
  3369. the output type, which might be a sane thing to do for a
  3370. v10+v32 compatibility object. */
  3371. if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
  3372. return false;
  3373. }
  3374. if (bfd_get_symbol_leading_char (ibfd)
  3375. != bfd_get_symbol_leading_char (obfd))
  3376. {
  3377. _bfd_error_handler
  3378. (bfd_get_symbol_leading_char (ibfd) == '_'
  3379. ? _("%pB: uses _-prefixed symbols, but writing file with non-prefixed symbols")
  3380. : _("%pB: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
  3381. ibfd);
  3382. bfd_set_error (bfd_error_bad_value);
  3383. return false;
  3384. }
  3385. omach = bfd_get_mach (obfd);
  3386. if (imach != omach)
  3387. {
  3388. /* We can get an incompatible combination only if either is
  3389. bfd_mach_cris_v32, and the other one isn't compatible. */
  3390. if ((imach == bfd_mach_cris_v32
  3391. && omach != bfd_mach_cris_v10_v32)
  3392. || (omach == bfd_mach_cris_v32
  3393. && imach != bfd_mach_cris_v10_v32))
  3394. {
  3395. _bfd_error_handler
  3396. ((imach == bfd_mach_cris_v32)
  3397. ? _("%pB contains CRIS v32 code, incompatible"
  3398. " with previous objects")
  3399. : _("%pB contains non-CRIS-v32 code, incompatible"
  3400. " with previous objects"),
  3401. ibfd);
  3402. bfd_set_error (bfd_error_bad_value);
  3403. return false;
  3404. }
  3405. /* We don't have to check the case where the input is compatible
  3406. with v10 and v32, because the output is already known to be set
  3407. to the other (compatible) mach. */
  3408. if (omach == bfd_mach_cris_v10_v32
  3409. && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
  3410. return false;
  3411. }
  3412. return true;
  3413. }
  3414. /* Do side-effects of e_flags copying to obfd. */
  3415. static bool
  3416. cris_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  3417. {
  3418. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  3419. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  3420. return true;
  3421. /* Call the base function. */
  3422. if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
  3423. return false;
  3424. /* Do what we really came here for. */
  3425. return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
  3426. }
  3427. static enum elf_reloc_type_class
  3428. elf_cris_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3429. const asection *rel_sec ATTRIBUTE_UNUSED,
  3430. const Elf_Internal_Rela *rela)
  3431. {
  3432. enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rela->r_info);
  3433. switch (r_type)
  3434. {
  3435. case R_CRIS_RELATIVE:
  3436. return reloc_class_relative;
  3437. case R_CRIS_JUMP_SLOT:
  3438. return reloc_class_plt;
  3439. case R_CRIS_COPY:
  3440. return reloc_class_copy;
  3441. default:
  3442. return reloc_class_normal;
  3443. }
  3444. }
  3445. /* The elf_backend_got_elt_size worker. For one symbol, we can have up to
  3446. two GOT entries from three types with two different sizes. We handle
  3447. it as a single entry, so we can use the regular offset-calculation
  3448. machinery. */
  3449. static bfd_vma
  3450. elf_cris_got_elt_size (bfd *abfd ATTRIBUTE_UNUSED,
  3451. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3452. struct elf_link_hash_entry *hr,
  3453. bfd *ibfd,
  3454. unsigned long symndx)
  3455. {
  3456. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) hr;
  3457. bfd_vma eltsiz = 0;
  3458. /* We may have one regular GOT entry or up to two TLS GOT
  3459. entries. */
  3460. if (h == NULL)
  3461. {
  3462. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  3463. bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (ibfd);
  3464. BFD_ASSERT (local_got_refcounts != NULL);
  3465. if (local_got_refcounts[LGOT_REG_NDX (symndx)] > 0)
  3466. {
  3467. /* We can't have a variable referred to both as a regular
  3468. variable and through TLS relocs. */
  3469. BFD_ASSERT (local_got_refcounts[LGOT_DTP_NDX (symndx)] == 0
  3470. && local_got_refcounts[LGOT_TPREL_NDX (symndx)] == 0);
  3471. return 4;
  3472. }
  3473. if (local_got_refcounts[LGOT_DTP_NDX (symndx)] > 0)
  3474. eltsiz += 8;
  3475. if (local_got_refcounts[LGOT_TPREL_NDX (symndx)] > 0)
  3476. eltsiz += 4;
  3477. }
  3478. else
  3479. {
  3480. struct elf_cris_link_hash_entry *hh = elf_cris_hash_entry (h);
  3481. if (hh->reg_got_refcount > 0)
  3482. {
  3483. /* The actual error-on-input is emitted elsewhere. */
  3484. BFD_ASSERT (hh->dtp_refcount == 0 && hh->tprel_refcount == 0);
  3485. return 4;
  3486. }
  3487. if (hh->dtp_refcount > 0)
  3488. eltsiz += 8;
  3489. if (hh->tprel_refcount > 0)
  3490. eltsiz += 4;
  3491. }
  3492. /* We're only called when h->got.refcount is non-zero, so we must
  3493. have a non-zero size. */
  3494. BFD_ASSERT (eltsiz != 0);
  3495. return eltsiz;
  3496. }
  3497. #define ELF_ARCH bfd_arch_cris
  3498. #define ELF_TARGET_ID CRIS_ELF_DATA
  3499. #define ELF_MACHINE_CODE EM_CRIS
  3500. #define ELF_MAXPAGESIZE 0x2000
  3501. #define TARGET_LITTLE_SYM cris_elf32_vec
  3502. #define TARGET_LITTLE_NAME "elf32-cris"
  3503. #define elf_symbol_leading_char 0
  3504. #define elf_info_to_howto_rel NULL
  3505. #define elf_info_to_howto cris_info_to_howto_rela
  3506. #define elf_backend_relocate_section cris_elf_relocate_section
  3507. #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook
  3508. #define elf_backend_plt_sym_val cris_elf_plt_sym_val
  3509. #define elf_backend_check_relocs cris_elf_check_relocs
  3510. #define elf_backend_grok_prstatus cris_elf_grok_prstatus
  3511. #define elf_backend_grok_psinfo cris_elf_grok_psinfo
  3512. #define elf_backend_can_gc_sections 1
  3513. #define elf_backend_can_refcount 1
  3514. #define elf_backend_object_p cris_elf_object_p
  3515. #define elf_backend_final_write_processing \
  3516. cris_elf_final_write_processing
  3517. #define bfd_elf32_bfd_print_private_bfd_data \
  3518. cris_elf_print_private_bfd_data
  3519. #define bfd_elf32_bfd_merge_private_bfd_data \
  3520. cris_elf_merge_private_bfd_data
  3521. #define bfd_elf32_bfd_copy_private_bfd_data \
  3522. cris_elf_copy_private_bfd_data
  3523. #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup
  3524. #define bfd_elf32_bfd_reloc_name_lookup cris_reloc_name_lookup
  3525. #define bfd_elf32_bfd_link_hash_table_create \
  3526. elf_cris_link_hash_table_create
  3527. #define elf_backend_adjust_dynamic_symbol \
  3528. elf_cris_adjust_dynamic_symbol
  3529. #define elf_backend_copy_indirect_symbol \
  3530. elf_cris_copy_indirect_symbol
  3531. #define elf_backend_size_dynamic_sections \
  3532. elf_cris_size_dynamic_sections
  3533. #define elf_backend_init_index_section _bfd_elf_init_1_index_section
  3534. #define elf_backend_finish_dynamic_symbol \
  3535. elf_cris_finish_dynamic_symbol
  3536. #define elf_backend_finish_dynamic_sections \
  3537. elf_cris_finish_dynamic_sections
  3538. #define elf_backend_create_dynamic_sections \
  3539. _bfd_elf_create_dynamic_sections
  3540. #define bfd_elf32_bfd_final_link \
  3541. bfd_elf_gc_common_final_link
  3542. #define elf_backend_hide_symbol elf_cris_hide_symbol
  3543. #define elf_backend_reloc_type_class elf_cris_reloc_type_class
  3544. #define elf_backend_want_got_plt 1
  3545. #define elf_backend_plt_readonly 1
  3546. #define elf_backend_want_plt_sym 0
  3547. #define elf_backend_got_header_size 12
  3548. #define elf_backend_got_elt_size elf_cris_got_elt_size
  3549. #define elf_backend_dtrel_excludes_plt 1
  3550. #define elf_backend_want_dynrelro 1
  3551. /* Later, we my want to optimize RELA entries into REL entries for dynamic
  3552. linking and libraries (if it's a win of any significance). Until then,
  3553. take the easy route. */
  3554. #define elf_backend_may_use_rel_p 0
  3555. #define elf_backend_may_use_rela_p 1
  3556. #define elf_backend_rela_normal 1
  3557. #define elf_backend_linux_prpsinfo32_ugid16 true
  3558. #include "elf32-target.h"
  3559. #undef TARGET_LITTLE_SYM
  3560. #undef TARGET_LITTLE_NAME
  3561. #undef elf_symbol_leading_char
  3562. #define TARGET_LITTLE_SYM cris_elf32_us_vec
  3563. #define TARGET_LITTLE_NAME "elf32-us-cris"
  3564. #define elf_symbol_leading_char '_'
  3565. #undef elf32_bed
  3566. #define elf32_bed elf32_us_cris_bed
  3567. #include "elf32-target.h"