elf32-cris.c 135 KB

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