elf32-metag.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043
  1. /* Meta support for 32-bit ELF
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. Contributed by Imagination Technologies Ltd.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "elf-bfd.h"
  21. #include "elf32-metag.h"
  22. #include "elf/metag.h"
  23. #define GOT_ENTRY_SIZE 4
  24. #define ELF_DYNAMIC_INTERPRETER "/lib/ld-uClibc.so.0"
  25. /* ABI version:
  26. 0 - original
  27. 1 - with GOT offset */
  28. #define METAG_ELF_ABI_VERSION 1
  29. static const unsigned int plt0_entry[] =
  30. {
  31. 0x02000005, /* MOVT D0Re0, #HI(GOT+4) */
  32. 0x02000000, /* ADD D0Re0, D0Re0, #LO(GOT+4) */
  33. 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
  34. 0xc600012a, /* GETD PC, [D0Re0+#4] */
  35. 0xa0fffffe /* NOP */
  36. };
  37. static const unsigned int plt0_pic_entry[] =
  38. {
  39. 0x82900001, /* ADDT A0.2, CPC0, #0 */
  40. 0x82100000, /* ADD A0.2, A0.2, #0 */
  41. 0xa3100c20, /* MOV D0Re0, A0.2 */
  42. 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
  43. 0xc600012a, /* GETD PC, [D0Re0+#4] */
  44. };
  45. static const unsigned int plt_entry[] =
  46. {
  47. 0x82100005, /* MOVT A0.2, #HI(GOT+off) */
  48. 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */
  49. 0xc600806a, /* GETD PC, [A0.2] */
  50. 0x03000004, /* MOV D1Re0, #LO(offset) */
  51. 0xa0000000 /* B PLT0 */
  52. };
  53. static const unsigned int plt_pic_entry[] =
  54. {
  55. 0x82900001, /* ADDT A0.2, CPC0, #HI(GOT+off) */
  56. 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */
  57. 0xc600806a, /* GETD PC, [A0.2] */
  58. 0x03000004, /* MOV D1Re0, #LO(offset) */
  59. 0xa0000000 /* B PLT0 */
  60. };
  61. /* Variable names follow a coding style.
  62. Please follow this (Apps Hungarian) style:
  63. Structure/Variable Prefix
  64. elf_link_hash_table "etab"
  65. elf_link_hash_entry "eh"
  66. elf_metag_link_hash_table "htab"
  67. elf_metag_link_hash_entry "hh"
  68. bfd_link_hash_table "btab"
  69. bfd_link_hash_entry "bh"
  70. bfd_hash_table containing stubs "bstab"
  71. elf_metag_stub_hash_entry "hsh"
  72. Always remember to use GNU Coding Style. */
  73. #define PLT_ENTRY_SIZE sizeof(plt_entry)
  74. static reloc_howto_type elf_metag_howto_table[] =
  75. {
  76. /* High order 16 bit absolute. */
  77. HOWTO (R_METAG_HIADDR16, /* type */
  78. 16, /* rightshift */
  79. 2, /* size (0 = byte, 1 = short, 2 = long) */
  80. 16, /* bitsize */
  81. false, /* pc_relative */
  82. 3, /* bitpos */
  83. complain_overflow_dont, /* complain_on_overflow */
  84. bfd_elf_generic_reloc, /* special_function */
  85. "R_METAG_HIADDR16", /* name */
  86. false, /* partial_inplace */
  87. 0, /* src_mask */
  88. 0x0007fff8, /* dst_mask */
  89. false), /* pcrel_offset */
  90. /* Low order 16 bit absolute. */
  91. HOWTO (R_METAG_LOADDR16, /* type */
  92. 0, /* rightshift */
  93. 2, /* size (0 = byte, 1 = short, 2 = long) */
  94. 16, /* bitsize */
  95. false, /* pc_relative */
  96. 3, /* bitpos */
  97. complain_overflow_dont,/* complain_on_overflow */
  98. bfd_elf_generic_reloc, /* special_function */
  99. "R_METAG_LOADDR16", /* name */
  100. false, /* partial_inplace */
  101. 0, /* src_mask */
  102. 0x0007fff8, /* dst_mask */
  103. false), /* pcrel_offset */
  104. /* 32 bit absolute. */
  105. HOWTO (R_METAG_ADDR32, /* type */
  106. 0, /* rightshift */
  107. 2, /* size (0 = byte, 1 = short, 2 = long) */
  108. 32, /* bitsize */
  109. false, /* pc_relative */
  110. 0, /* bitpos */
  111. complain_overflow_bitfield, /* complain_on_overflow */
  112. bfd_elf_generic_reloc, /* special_function */
  113. "R_METAG_ADDR32", /* name */
  114. false, /* partial_inplace */
  115. 0x00000000, /* src_mask */
  116. 0xffffffff, /* dst_mask */
  117. false), /* pcrel_offset */
  118. /* No relocation. */
  119. HOWTO (R_METAG_NONE, /* type */
  120. 0, /* rightshift */
  121. 3, /* size (0 = byte, 1 = short, 2 = long) */
  122. 0, /* bitsize */
  123. false, /* pc_relative */
  124. 0, /* bitpos */
  125. complain_overflow_dont, /* complain_on_overflow */
  126. bfd_elf_generic_reloc, /* special_function */
  127. "R_METAG_NONE", /* name */
  128. false, /* partial_inplace */
  129. 0, /* src_mask */
  130. 0, /* dst_mask */
  131. false), /* pcrel_offset */
  132. /* 19 bit pc relative */
  133. HOWTO (R_METAG_RELBRANCH, /* type */
  134. 2, /* rightshift */
  135. 2, /* size (0 = byte, 1 = short, 2 = long) */
  136. 19, /* bitsize */
  137. true, /* pc_relative */
  138. 5, /* bitpos */
  139. complain_overflow_signed, /* complain_on_overflow */
  140. bfd_elf_generic_reloc, /* special_function */
  141. "R_METAG_RELBRANCH", /* name */
  142. false, /* partial_inplace */
  143. 0, /* src_mask */
  144. 0x00ffffe0, /* dst_mask */
  145. false), /* pcrel_offset */
  146. /* GET/SET offset */
  147. HOWTO (R_METAG_GETSETOFF, /* type */
  148. 0, /* rightshift */
  149. 1, /* size (0 = byte, 1 = short, 2 = long) */
  150. 12, /* bitsize */
  151. false, /* pc_relative */
  152. 7, /* bitpos */
  153. complain_overflow_dont, /* complain_on_overflow */
  154. bfd_elf_generic_reloc, /* special_function */
  155. "R_METAG_GETSETOFF", /* name */
  156. false, /* partial_inplace */
  157. 0, /* src_mask */
  158. 0, /* dst_mask */
  159. false), /* pcrel_offset */
  160. EMPTY_HOWTO (6),
  161. EMPTY_HOWTO (7),
  162. EMPTY_HOWTO (8),
  163. EMPTY_HOWTO (9),
  164. EMPTY_HOWTO (10),
  165. EMPTY_HOWTO (11),
  166. EMPTY_HOWTO (12),
  167. EMPTY_HOWTO (13),
  168. EMPTY_HOWTO (14),
  169. EMPTY_HOWTO (15),
  170. EMPTY_HOWTO (16),
  171. EMPTY_HOWTO (17),
  172. EMPTY_HOWTO (18),
  173. EMPTY_HOWTO (19),
  174. EMPTY_HOWTO (20),
  175. EMPTY_HOWTO (21),
  176. EMPTY_HOWTO (22),
  177. EMPTY_HOWTO (23),
  178. EMPTY_HOWTO (24),
  179. EMPTY_HOWTO (25),
  180. EMPTY_HOWTO (26),
  181. EMPTY_HOWTO (27),
  182. EMPTY_HOWTO (28),
  183. EMPTY_HOWTO (29),
  184. HOWTO (R_METAG_GNU_VTINHERIT, /* type */
  185. 0, /* rightshift */
  186. 2, /* size (0 = byte, 1 = short, 2 = long) */
  187. 0, /* bitsize */
  188. false, /* pc_relative */
  189. 0, /* bitpos */
  190. complain_overflow_dont, /* complain_on_overflow */
  191. NULL, /* special_function */
  192. "R_METAG_GNU_VTINHERIT", /* name */
  193. false, /* partial_inplace */
  194. 0, /* src_mask */
  195. 0, /* dst_mask */
  196. false), /* pcrel_offset */
  197. HOWTO (R_METAG_GNU_VTENTRY, /* type */
  198. 0, /* rightshift */
  199. 2, /* size (0 = byte, 1 = short, 2 = long) */
  200. 0, /* bitsize */
  201. false, /* pc_relative */
  202. 0, /* bitpos */
  203. complain_overflow_dont, /* complain_on_overflow */
  204. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  205. "R_METAG_GNU_VTENTRY", /* name */
  206. false, /* partial_inplace */
  207. 0, /* src_mask */
  208. 0, /* dst_mask */
  209. false), /* pcrel_offset */
  210. /* High order 16 bit GOT offset */
  211. HOWTO (R_METAG_HI16_GOTOFF, /* type */
  212. 16, /* rightshift */
  213. 2, /* size (0 = byte, 1 = short, 2 = long) */
  214. 16, /* bitsize */
  215. false, /* pc_relative */
  216. 3, /* bitpos */
  217. complain_overflow_dont, /* complain_on_overflow */
  218. bfd_elf_generic_reloc, /* special_function */
  219. "R_METAG_HI16_GOTOFF", /* name */
  220. false, /* partial_inplace */
  221. 0, /* src_mask */
  222. 0x0007fff8, /* dst_mask */
  223. false), /* pcrel_offset */
  224. /* Low order 16 bit GOT offset */
  225. HOWTO (R_METAG_LO16_GOTOFF, /* type */
  226. 0, /* rightshift */
  227. 2, /* size (0 = byte, 1 = short, 2 = long) */
  228. 16, /* bitsize */
  229. false, /* pc_relative */
  230. 3, /* bitpos */
  231. complain_overflow_dont, /* complain_on_overflow */
  232. bfd_elf_generic_reloc, /* special_function */
  233. "R_METAG_LO16_GOTOFF", /* name */
  234. false, /* partial_inplace */
  235. 0, /* src_mask */
  236. 0x0007fff8, /* dst_mask */
  237. false), /* pcrel_offset */
  238. /* GET/SET GOT offset */
  239. HOWTO (R_METAG_GETSET_GOTOFF, /* type */
  240. 0, /* rightshift */
  241. 1, /* size (0 = byte, 1 = short, 2 = long) */
  242. 12, /* bitsize */
  243. false, /* pc_relative */
  244. 7, /* bitpos */
  245. complain_overflow_dont, /* complain_on_overflow */
  246. bfd_elf_generic_reloc, /* special_function */
  247. "R_METAG_GETSET_GOTOFF", /* name */
  248. false, /* partial_inplace */
  249. 0, /* src_mask */
  250. 0, /* dst_mask */
  251. false), /* pcrel_offset */
  252. /* GET/SET GOT relative */
  253. HOWTO (R_METAG_GETSET_GOT, /* type */
  254. 0, /* rightshift */
  255. 1, /* size (0 = byte, 1 = short, 2 = long) */
  256. 12, /* bitsize */
  257. false, /* pc_relative */
  258. 7, /* bitpos */
  259. complain_overflow_dont, /* complain_on_overflow */
  260. bfd_elf_generic_reloc, /* special_function */
  261. "R_METAG_GETSET_GOT", /* name */
  262. false, /* partial_inplace */
  263. 0, /* src_mask */
  264. 0, /* dst_mask */
  265. false), /* pcrel_offset */
  266. /* High order 16 bit GOT reference */
  267. HOWTO (R_METAG_HI16_GOTPC, /* type */
  268. 16, /* rightshift */
  269. 2, /* size (0 = byte, 1 = short, 2 = long) */
  270. 16, /* bitsize */
  271. false, /* pc_relative */
  272. 3, /* bitpos */
  273. complain_overflow_dont, /* complain_on_overflow */
  274. bfd_elf_generic_reloc, /* special_function */
  275. "R_METAG_HI16_GOTPC", /* name */
  276. false, /* partial_inplace */
  277. 0, /* src_mask */
  278. 0x0007fff8, /* dst_mask */
  279. false), /* pcrel_offset */
  280. /* Low order 16 bit GOT reference */
  281. HOWTO (R_METAG_LO16_GOTPC, /* type */
  282. 0, /* rightshift */
  283. 2, /* size (0 = byte, 1 = short, 2 = long) */
  284. 16, /* bitsize */
  285. false, /* pc_relative */
  286. 3, /* bitpos */
  287. complain_overflow_dont, /* complain_on_overflow */
  288. bfd_elf_generic_reloc, /* special_function */
  289. "R_METAG_LO16_GOTPC", /* name */
  290. false, /* partial_inplace */
  291. 0, /* src_mask */
  292. 0x0007fff8, /* dst_mask */
  293. false), /* pcrel_offset */
  294. /* High order 16 bit PLT */
  295. HOWTO (R_METAG_HI16_PLT, /* type */
  296. 16, /* rightshift */
  297. 2, /* size (0 = byte, 1 = short, 2 = long) */
  298. 16, /* bitsize */
  299. false, /* pc_relative */
  300. 3, /* bitpos */
  301. complain_overflow_dont, /* complain_on_overflow */
  302. bfd_elf_generic_reloc, /* special_function */
  303. "R_METAG_HI16_PLT", /* name */
  304. false, /* partial_inplace */
  305. 0, /* src_mask */
  306. 0x0007fff8, /* dst_mask */
  307. false), /* pcrel_offset */
  308. /* Low order 16 bit PLT */
  309. HOWTO (R_METAG_LO16_PLT, /* type */
  310. 0, /* rightshift */
  311. 2, /* size (0 = byte, 1 = short, 2 = long) */
  312. 16, /* bitsize */
  313. false, /* pc_relative */
  314. 3, /* bitpos */
  315. complain_overflow_dont, /* complain_on_overflow */
  316. bfd_elf_generic_reloc, /* special_function */
  317. "R_METAG_LO16_PLT", /* name */
  318. false, /* partial_inplace */
  319. 0, /* src_mask */
  320. 0xffffffff, /* dst_mask */
  321. false), /* pcrel_offset */
  322. HOWTO (R_METAG_RELBRANCH_PLT, /* type */
  323. 2, /* rightshift */
  324. 2, /* size (0 = byte, 1 = short, 2 = long) */
  325. 19, /* bitsize */
  326. true, /* pc_relative */
  327. 5, /* bitpos */
  328. complain_overflow_signed, /* complain_on_overflow */
  329. bfd_elf_generic_reloc, /* special_function */
  330. "R_METAG_RELBRANCH_PLT", /* name */
  331. false, /* partial_inplace */
  332. 0, /* src_mask */
  333. 0x00ffffe0, /* dst_mask */
  334. false), /* pcrel_offset */
  335. /* Dummy relocs used by the linker internally. */
  336. HOWTO (R_METAG_GOTOFF, /* type */
  337. 0, /* rightshift */
  338. 2, /* size (0 = byte, 1 = short, 2 = long) */
  339. 32, /* bitsize */
  340. false, /* pc_relative */
  341. 0, /* bitpos */
  342. complain_overflow_bitfield, /* complain_on_overflow */
  343. bfd_elf_generic_reloc, /* special_function */
  344. "R_METAG_GOTOFF", /* name */
  345. false, /* partial_inplace */
  346. 0xffffffff, /* src_mask */
  347. 0xffffffff, /* dst_mask */
  348. false), /* pcrel_offset */
  349. HOWTO (R_METAG_PLT, /* type */
  350. 0, /* rightshift */
  351. 2, /* size (0 = byte, 1 = short, 2 = long) */
  352. 32, /* bitsize */
  353. false, /* pc_relative */
  354. 0, /* bitpos */
  355. complain_overflow_bitfield, /* complain_on_overflow */
  356. bfd_elf_generic_reloc, /* special_function */
  357. "R_METAG_GOTOFF", /* name */
  358. false, /* partial_inplace */
  359. 0xffffffff, /* src_mask */
  360. 0xffffffff, /* dst_mask */
  361. false), /* pcrel_offset */
  362. /* This is used only by the dynamic linker. The symbol should exist
  363. both in the object being run and in some shared library. The
  364. dynamic linker copies the data addressed by the symbol from the
  365. shared library into the object, because the object being
  366. run has to have the data at some particular address. */
  367. HOWTO (R_METAG_COPY, /* type */
  368. 0, /* rightshift */
  369. 2, /* size (0 = byte, 1 = short, 2 = long) */
  370. 32, /* bitsize */
  371. false, /* pc_relative */
  372. 0, /* bitpos */
  373. complain_overflow_bitfield, /* complain_on_overflow */
  374. bfd_elf_generic_reloc, /* special_function */
  375. "R_METAG_COPY", /* name */
  376. false, /* partial_inplace */
  377. 0xffffffff, /* src_mask */
  378. 0xffffffff, /* dst_mask */
  379. false), /* pcrel_offset */
  380. /* Marks a procedure linkage table entry for a symbol. */
  381. HOWTO (R_METAG_JMP_SLOT, /* type */
  382. 0, /* rightshift */
  383. 2, /* size (0 = byte, 1 = short, 2 = long) */
  384. 32, /* bitsize */
  385. false, /* pc_relative */
  386. 0, /* bitpos */
  387. complain_overflow_bitfield, /* complain_on_overflow */
  388. bfd_elf_generic_reloc, /* special_function */
  389. "R_METAG_JMP_SLOT", /* name */
  390. false, /* partial_inplace */
  391. 0xffffffff, /* src_mask */
  392. 0xffffffff, /* dst_mask */
  393. false), /* pcrel_offset */
  394. /* Used only by the dynamic linker. When the object is run, this
  395. longword is set to the load address of the object, plus the
  396. addend. */
  397. HOWTO (R_METAG_RELATIVE, /* type */
  398. 0, /* rightshift */
  399. 2, /* size (0 = byte, 1 = short, 2 = long) */
  400. 32, /* bitsize */
  401. false, /* pc_relative */
  402. 0, /* bitpos */
  403. complain_overflow_bitfield, /* complain_on_overflow */
  404. bfd_elf_generic_reloc, /* special_function */
  405. "R_METAG_RELATIVE", /* name */
  406. false, /* partial_inplace */
  407. 0xffffffff, /* src_mask */
  408. 0xffffffff, /* dst_mask */
  409. false), /* pcrel_offset */
  410. HOWTO (R_METAG_GLOB_DAT, /* type */
  411. 0, /* rightshift */
  412. 2, /* size (0 = byte, 1 = short, 2 = long) */
  413. 32, /* bitsize */
  414. false, /* pc_relative */
  415. 0, /* bitpos */
  416. complain_overflow_bitfield, /* complain_on_overflow */
  417. bfd_elf_generic_reloc, /* special_function */
  418. "R_METAG_GLOB_DAT", /* name */
  419. false, /* partial_inplace */
  420. 0xffffffff, /* src_mask */
  421. 0xffffffff, /* dst_mask */
  422. false), /* pcrel_offset */
  423. HOWTO (R_METAG_TLS_GD, /* type */
  424. 0, /* rightshift */
  425. 2, /* size (0 = byte, 1 = short, 2 = long) */
  426. 16, /* bitsize */
  427. false, /* pc_relative */
  428. 3, /* bitpos */
  429. complain_overflow_dont, /* complain_on_overflow */
  430. bfd_elf_generic_reloc, /* special_function */
  431. "R_METAG_TLS_GD", /* name */
  432. false, /* partial_inplace */
  433. 0, /* src_mask */
  434. 0x0007fff8, /* dst_mask */
  435. false), /* pcrel_offset */
  436. HOWTO (R_METAG_TLS_LDM, /* type */
  437. 0, /* rightshift */
  438. 2, /* size (0 = byte, 1 = short, 2 = long) */
  439. 16, /* bitsize */
  440. false, /* pc_relative */
  441. 3, /* bitpos */
  442. complain_overflow_bitfield, /* complain_on_overflow */
  443. bfd_elf_generic_reloc, /* special_function */
  444. "R_METAG_TLS_LDM", /* name */
  445. false, /* partial_inplace */
  446. 0, /* src_mask */
  447. 0x0007fff8, /* dst_mask */
  448. false), /* pcrel_offset */
  449. HOWTO (R_METAG_TLS_LDO_HI16, /* type */
  450. 16, /* rightshift */
  451. 2, /* size (0 = byte, 1 = short, 2 = long) */
  452. 16, /* bitsize */
  453. false, /* pc_relative */
  454. 3, /* bitpos */
  455. complain_overflow_bitfield, /* complain_on_overflow */
  456. bfd_elf_generic_reloc, /* special_function */
  457. "R_METAG_TLS_LDO_HI16", /* name */
  458. false, /* partial_inplace */
  459. 0, /* src_mask */
  460. 0x0007fff8, /* dst_mask */
  461. false), /* pcrel_offset */
  462. HOWTO (R_METAG_TLS_LDO_LO16, /* type */
  463. 0, /* rightshift */
  464. 2, /* size (0 = byte, 1 = short, 2 = long) */
  465. 16, /* bitsize */
  466. false, /* pc_relative */
  467. 3, /* bitpos */
  468. complain_overflow_bitfield, /* complain_on_overflow */
  469. bfd_elf_generic_reloc, /* special_function */
  470. "R_METAG_TLS_LDO_LO16", /* name */
  471. false, /* partial_inplace */
  472. 0, /* src_mask */
  473. 0x0007fff8, /* dst_mask */
  474. false), /* pcrel_offset */
  475. /* Dummy reloc used by the linker internally. */
  476. HOWTO (R_METAG_TLS_LDO, /* type */
  477. 0, /* rightshift */
  478. 2, /* size (0 = byte, 1 = short, 2 = long) */
  479. 16, /* bitsize */
  480. false, /* pc_relative */
  481. 3, /* bitpos */
  482. complain_overflow_bitfield, /* complain_on_overflow */
  483. bfd_elf_generic_reloc, /* special_function */
  484. "R_METAG_TLS_LDO", /* name */
  485. false, /* partial_inplace */
  486. 0, /* src_mask */
  487. 0x0007fff8, /* dst_mask */
  488. false), /* pcrel_offset */
  489. HOWTO (R_METAG_TLS_IE, /* type */
  490. 2, /* rightshift */
  491. 2, /* size (0 = byte, 1 = short, 2 = long) */
  492. 12, /* bitsize */
  493. false, /* pc_relative */
  494. 7, /* bitpos */
  495. complain_overflow_dont, /* complain_on_overflow */
  496. bfd_elf_generic_reloc, /* special_function */
  497. "R_METAG_TLS_IE", /* name */
  498. false, /* partial_inplace */
  499. 0, /* src_mask */
  500. 0x0007ff80, /* dst_mask */
  501. false), /* pcrel_offset */
  502. /* Dummy reloc used by the linker internally. */
  503. HOWTO (R_METAG_TLS_IENONPIC, /* type */
  504. 0, /* rightshift */
  505. 2, /* size (0 = byte, 1 = short, 2 = long) */
  506. 16, /* bitsize */
  507. false, /* pc_relative */
  508. 3, /* bitpos */
  509. complain_overflow_dont, /* complain_on_overflow */
  510. bfd_elf_generic_reloc, /* special_function */
  511. "R_METAG_TLS_IENONPIC", /* name */
  512. false, /* partial_inplace */
  513. 0, /* src_mask */
  514. 0x0007fff8, /* dst_mask */
  515. false), /* pcrel_offset */
  516. HOWTO (R_METAG_TLS_IENONPIC_HI16,/* type */
  517. 16, /* rightshift */
  518. 2, /* size (0 = byte, 1 = short, 2 = long) */
  519. 16, /* bitsize */
  520. false, /* pc_relative */
  521. 3, /* bitpos */
  522. complain_overflow_dont, /* complain_on_overflow */
  523. bfd_elf_generic_reloc, /* special_function */
  524. "R_METAG_TLS_IENONPIC_HI16", /* name */
  525. false, /* partial_inplace */
  526. 0, /* src_mask */
  527. 0x0007fff8, /* dst_mask */
  528. false), /* pcrel_offset */
  529. HOWTO (R_METAG_TLS_IENONPIC_LO16,/* type */
  530. 0, /* rightshift */
  531. 2, /* size (0 = byte, 1 = short, 2 = long) */
  532. 16, /* bitsize */
  533. false, /* pc_relative */
  534. 3, /* bitpos */
  535. complain_overflow_dont, /* complain_on_overflow */
  536. bfd_elf_generic_reloc, /* special_function */
  537. "R_METAG_TLS_IENONPIC_LO16", /* name */
  538. false, /* partial_inplace */
  539. 0, /* src_mask */
  540. 0x0007fff8, /* dst_mask */
  541. false), /* pcrel_offset */
  542. HOWTO (R_METAG_TLS_TPOFF, /* type */
  543. 0, /* rightshift */
  544. 2, /* size (0 = byte, 1 = short, 2 = long) */
  545. 32, /* bitsize */
  546. false, /* pc_relative */
  547. 0, /* bitpos */
  548. complain_overflow_bitfield, /* complain_on_overflow */
  549. bfd_elf_generic_reloc, /* special_function */
  550. "R_METAG_TLS_TPOFF", /* name */
  551. false, /* partial_inplace */
  552. 0, /* src_mask */
  553. 0xffffffff, /* dst_mask */
  554. false), /* pcrel_offset */
  555. HOWTO (R_METAG_TLS_DTPMOD, /* type */
  556. 0, /* rightshift */
  557. 2, /* size (0 = byte, 1 = short, 2 = long) */
  558. 32, /* bitsize */
  559. false, /* pc_relative */
  560. 0, /* bitpos */
  561. complain_overflow_bitfield, /* complain_on_overflow */
  562. bfd_elf_generic_reloc, /* special_function */
  563. "R_METAG_TLS_DTPMOD", /* name */
  564. false, /* partial_inplace */
  565. 0, /* src_mask */
  566. 0xffffffff, /* dst_mask */
  567. false), /* pcrel_offset */
  568. HOWTO (R_METAG_TLS_DTPOFF, /* type */
  569. 0, /* rightshift */
  570. 2, /* size (0 = byte, 1 = short, 2 = long) */
  571. 32, /* bitsize */
  572. false, /* pc_relative */
  573. 0, /* bitpos */
  574. complain_overflow_bitfield, /* complain_on_overflow */
  575. bfd_elf_generic_reloc, /* special_function */
  576. "R_METAG_TLS_DTPOFF", /* name */
  577. false, /* partial_inplace */
  578. 0, /* src_mask */
  579. 0xffffffff, /* dst_mask */
  580. false), /* pcrel_offset */
  581. /* Dummy reloc used by the linker internally. */
  582. HOWTO (R_METAG_TLS_LE, /* type */
  583. 0, /* rightshift */
  584. 2, /* size (0 = byte, 1 = short, 2 = long) */
  585. 32, /* bitsize */
  586. false, /* pc_relative */
  587. 0, /* bitpos */
  588. complain_overflow_bitfield, /* complain_on_overflow */
  589. bfd_elf_generic_reloc, /* special_function */
  590. "R_METAG_TLS_LE", /* name */
  591. false, /* partial_inplace */
  592. 0, /* src_mask */
  593. 0xffffffff, /* dst_mask */
  594. false), /* pcrel_offset */
  595. HOWTO (R_METAG_TLS_LE_HI16, /* type */
  596. 16, /* rightshift */
  597. 2, /* size (0 = byte, 1 = short, 2 = long) */
  598. 16, /* bitsize */
  599. false, /* pc_relative */
  600. 3, /* bitpos */
  601. complain_overflow_dont, /* complain_on_overflow */
  602. bfd_elf_generic_reloc, /* special_function */
  603. "R_METAG_TLS_LE_HI16", /* name */
  604. false, /* partial_inplace */
  605. 0, /* src_mask */
  606. 0x0007fff8, /* dst_mask */
  607. false), /* pcrel_offset */
  608. HOWTO (R_METAG_TLS_LE_LO16, /* type */
  609. 0, /* rightshift */
  610. 2, /* size (0 = byte, 1 = short, 2 = long) */
  611. 16, /* bitsize */
  612. false, /* pc_relative */
  613. 3, /* bitpos */
  614. complain_overflow_dont, /* complain_on_overflow */
  615. bfd_elf_generic_reloc, /* special_function */
  616. "R_METAG_TLS_LE_LO16", /* name */
  617. false, /* partial_inplace */
  618. 0, /* src_mask */
  619. 0x0007fff8, /* dst_mask */
  620. false), /* pcrel_offset */
  621. };
  622. #define BRANCH_BITS 19
  623. /* The GOT is typically accessed using a [GS]ETD instruction. The size of the
  624. immediate offset which can be used in such instructions therefore limits
  625. the usable size of the GOT. If the base register for the [GS]ETD (A1LbP)
  626. is pointing to the base of the GOT then the size is limited to the maximum
  627. 11 bits unsigned dword offset, or 2^13 = 0x2000 bytes. However the offset
  628. in a [GS]ETD instruction is signed, so by setting the base address register
  629. to an offset of that 0x2000 byte maximum unsigned offset from the base of
  630. the GOT we can use negative offsets in addition to positive. This
  631. effectively doubles the usable GOT size to 0x4000 bytes. */
  632. #define GOT_REG_OFFSET 0x2000
  633. struct metag_reloc_map
  634. {
  635. bfd_reloc_code_real_type bfd_reloc_val;
  636. unsigned int metag_reloc_val;
  637. };
  638. static const struct metag_reloc_map metag_reloc_map [] =
  639. {
  640. { BFD_RELOC_NONE, R_METAG_NONE },
  641. { BFD_RELOC_32, R_METAG_ADDR32 },
  642. { BFD_RELOC_METAG_HIADDR16, R_METAG_HIADDR16 },
  643. { BFD_RELOC_METAG_LOADDR16, R_METAG_LOADDR16 },
  644. { BFD_RELOC_METAG_RELBRANCH, R_METAG_RELBRANCH },
  645. { BFD_RELOC_METAG_GETSETOFF, R_METAG_GETSETOFF },
  646. { BFD_RELOC_VTABLE_INHERIT, R_METAG_GNU_VTINHERIT },
  647. { BFD_RELOC_VTABLE_ENTRY, R_METAG_GNU_VTENTRY },
  648. { BFD_RELOC_METAG_REL8, R_METAG_REL8 },
  649. { BFD_RELOC_METAG_REL16, R_METAG_REL16 },
  650. { BFD_RELOC_METAG_HI16_GOTOFF, R_METAG_HI16_GOTOFF },
  651. { BFD_RELOC_METAG_LO16_GOTOFF, R_METAG_LO16_GOTOFF },
  652. { BFD_RELOC_METAG_GETSET_GOTOFF, R_METAG_GETSET_GOTOFF },
  653. { BFD_RELOC_METAG_GETSET_GOT, R_METAG_GETSET_GOT },
  654. { BFD_RELOC_METAG_HI16_GOTPC, R_METAG_HI16_GOTPC },
  655. { BFD_RELOC_METAG_LO16_GOTPC, R_METAG_LO16_GOTPC },
  656. { BFD_RELOC_METAG_HI16_PLT, R_METAG_HI16_PLT },
  657. { BFD_RELOC_METAG_LO16_PLT, R_METAG_LO16_PLT },
  658. { BFD_RELOC_METAG_RELBRANCH_PLT, R_METAG_RELBRANCH_PLT },
  659. { BFD_RELOC_METAG_GOTOFF, R_METAG_GOTOFF },
  660. { BFD_RELOC_METAG_PLT, R_METAG_PLT },
  661. { BFD_RELOC_METAG_COPY, R_METAG_COPY },
  662. { BFD_RELOC_METAG_JMP_SLOT, R_METAG_JMP_SLOT },
  663. { BFD_RELOC_METAG_RELATIVE, R_METAG_RELATIVE },
  664. { BFD_RELOC_METAG_GLOB_DAT, R_METAG_GLOB_DAT },
  665. { BFD_RELOC_METAG_TLS_GD, R_METAG_TLS_GD },
  666. { BFD_RELOC_METAG_TLS_LDM, R_METAG_TLS_LDM },
  667. { BFD_RELOC_METAG_TLS_LDO_HI16, R_METAG_TLS_LDO_HI16 },
  668. { BFD_RELOC_METAG_TLS_LDO_LO16, R_METAG_TLS_LDO_LO16 },
  669. { BFD_RELOC_METAG_TLS_LDO, R_METAG_TLS_LDO },
  670. { BFD_RELOC_METAG_TLS_IE, R_METAG_TLS_IE },
  671. { BFD_RELOC_METAG_TLS_IENONPIC, R_METAG_TLS_IENONPIC },
  672. { BFD_RELOC_METAG_TLS_IENONPIC_HI16, R_METAG_TLS_IENONPIC_HI16 },
  673. { BFD_RELOC_METAG_TLS_IENONPIC_LO16, R_METAG_TLS_IENONPIC_LO16 },
  674. { BFD_RELOC_METAG_TLS_TPOFF, R_METAG_TLS_TPOFF },
  675. { BFD_RELOC_METAG_TLS_DTPMOD, R_METAG_TLS_DTPMOD },
  676. { BFD_RELOC_METAG_TLS_DTPOFF, R_METAG_TLS_DTPOFF },
  677. { BFD_RELOC_METAG_TLS_LE, R_METAG_TLS_LE },
  678. { BFD_RELOC_METAG_TLS_LE_HI16, R_METAG_TLS_LE_HI16 },
  679. { BFD_RELOC_METAG_TLS_LE_LO16, R_METAG_TLS_LE_LO16 },
  680. };
  681. enum elf_metag_stub_type
  682. {
  683. metag_stub_long_branch,
  684. metag_stub_long_branch_shared,
  685. metag_stub_none
  686. };
  687. struct elf_metag_stub_hash_entry
  688. {
  689. /* Base hash table entry structure. */
  690. struct bfd_hash_entry bh_root;
  691. /* The stub section. */
  692. asection *stub_sec;
  693. /* Offset within stub_sec of the beginning of this stub. */
  694. bfd_vma stub_offset;
  695. /* Given the symbol's value and its section we can determine its final
  696. value when building the stubs (so the stub knows where to jump. */
  697. bfd_vma target_value;
  698. asection *target_section;
  699. enum elf_metag_stub_type stub_type;
  700. /* The symbol table entry, if any, that this was derived from. */
  701. struct elf_metag_link_hash_entry *hh;
  702. /* And the reloc addend that this was derived from. */
  703. bfd_vma addend;
  704. /* Where this stub is being called from, or, in the case of combined
  705. stub sections, the first input section in the group. */
  706. asection *id_sec;
  707. };
  708. struct elf_metag_link_hash_entry
  709. {
  710. struct elf_link_hash_entry eh;
  711. /* A pointer to the most recently used stub hash entry against this
  712. symbol. */
  713. struct elf_metag_stub_hash_entry *hsh_cache;
  714. enum
  715. {
  716. GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_IE = 2, GOT_TLS_LDM = 4, GOT_TLS_GD = 8
  717. } tls_type;
  718. };
  719. struct elf_metag_link_hash_table
  720. {
  721. /* The main hash table. */
  722. struct elf_link_hash_table etab;
  723. /* The stub hash table. */
  724. struct bfd_hash_table bstab;
  725. /* Linker stub bfd. */
  726. bfd *stub_bfd;
  727. /* Linker call-backs. */
  728. asection * (*add_stub_section) (const char *, asection *);
  729. void (*layout_sections_again) (void);
  730. /* Array to keep track of which stub sections have been created, and
  731. information on stub grouping. */
  732. struct map_stub
  733. {
  734. /* This is the section to which stubs in the group will be
  735. attached. */
  736. asection *link_sec;
  737. /* The stub section. */
  738. asection *stub_sec;
  739. } *stub_group;
  740. /* Assorted information used by elf_metag_size_stubs. */
  741. unsigned int bfd_count;
  742. unsigned int top_index;
  743. asection **input_list;
  744. Elf_Internal_Sym **all_local_syms;
  745. /* Data for LDM relocations. */
  746. union
  747. {
  748. bfd_signed_vma refcount;
  749. bfd_vma offset;
  750. } tls_ldm_got;
  751. };
  752. /* Return the base vma address which should be subtracted from the
  753. real address when resolving a dtpoff relocation. This is PT_TLS
  754. segment p_vaddr. */
  755. static bfd_vma
  756. dtpoff_base (struct bfd_link_info *info)
  757. {
  758. /* If tls_sec is NULL, we should have signalled an error already. */
  759. if (elf_hash_table (info)->tls_sec == NULL)
  760. return 0;
  761. return elf_hash_table (info)->tls_sec->vma;
  762. }
  763. /* Return the relocation value for R_METAG_TLS_IE */
  764. static bfd_vma
  765. tpoff (struct bfd_link_info *info, bfd_vma address)
  766. {
  767. /* If tls_sec is NULL, we should have signalled an error already. */
  768. if (elf_hash_table (info)->tls_sec == NULL)
  769. return 0;
  770. /* METAG TLS ABI is variant I and static TLS blocks start just after
  771. tcbhead structure which has 2 pointer fields. */
  772. return (address - elf_hash_table (info)->tls_sec->vma
  773. + align_power ((bfd_vma) 8,
  774. elf_hash_table (info)->tls_sec->alignment_power));
  775. }
  776. static bool
  777. metag_info_to_howto_rela (bfd *abfd,
  778. arelent *cache_ptr,
  779. Elf_Internal_Rela *dst)
  780. {
  781. unsigned int r_type;
  782. r_type = ELF32_R_TYPE (dst->r_info);
  783. if (r_type >= (unsigned int) R_METAG_MAX)
  784. {
  785. /* xgettext:c-format */
  786. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  787. abfd, r_type);
  788. bfd_set_error (bfd_error_bad_value);
  789. return false;
  790. }
  791. cache_ptr->howto = & elf_metag_howto_table [r_type];
  792. return true;
  793. }
  794. static reloc_howto_type *
  795. metag_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  796. bfd_reloc_code_real_type code)
  797. {
  798. unsigned int i;
  799. for (i = 0; i < sizeof (metag_reloc_map) / sizeof (metag_reloc_map[0]); i++)
  800. if (metag_reloc_map [i].bfd_reloc_val == code)
  801. return & elf_metag_howto_table [metag_reloc_map[i].metag_reloc_val];
  802. return NULL;
  803. }
  804. static reloc_howto_type *
  805. metag_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  806. const char *r_name)
  807. {
  808. unsigned int i;
  809. for (i = 0; i < sizeof (elf_metag_howto_table) / sizeof (elf_metag_howto_table[0]); i++)
  810. if (elf_metag_howto_table[i].name != NULL
  811. && strcasecmp (elf_metag_howto_table[i].name, r_name) == 0)
  812. return &elf_metag_howto_table[i];
  813. return NULL;
  814. }
  815. /* Various hash macros and functions. */
  816. #define metag_link_hash_table(p) \
  817. ((is_elf_hash_table ((p)->hash) \
  818. && elf_hash_table_id (elf_hash_table (p)) == METAG_ELF_DATA) \
  819. ? (struct elf_metag_link_hash_table *) (p)->hash : NULL)
  820. #define metag_elf_hash_entry(ent) \
  821. ((struct elf_metag_link_hash_entry *)(ent))
  822. #define metag_stub_hash_entry(ent) \
  823. ((struct elf_metag_stub_hash_entry *)(ent))
  824. #define metag_stub_hash_lookup(table, string, create, copy) \
  825. ((struct elf_metag_stub_hash_entry *) \
  826. bfd_hash_lookup ((table), (string), (create), (copy)))
  827. #define metag_elf_local_got_tls_type(abfd) \
  828. ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info)))
  829. /* Assorted hash table functions. */
  830. /* Initialize an entry in the stub hash table. */
  831. static struct bfd_hash_entry *
  832. stub_hash_newfunc (struct bfd_hash_entry *entry,
  833. struct bfd_hash_table *table,
  834. const char *string)
  835. {
  836. /* Allocate the structure if it has not already been allocated by a
  837. subclass. */
  838. if (entry == NULL)
  839. {
  840. entry = bfd_hash_allocate (table,
  841. sizeof (struct elf_metag_stub_hash_entry));
  842. if (entry == NULL)
  843. return entry;
  844. }
  845. /* Call the allocation method of the superclass. */
  846. entry = bfd_hash_newfunc (entry, table, string);
  847. if (entry != NULL)
  848. {
  849. struct elf_metag_stub_hash_entry *hsh;
  850. /* Initialize the local fields. */
  851. hsh = (struct elf_metag_stub_hash_entry *) entry;
  852. hsh->stub_sec = NULL;
  853. hsh->stub_offset = 0;
  854. hsh->target_value = 0;
  855. hsh->target_section = NULL;
  856. hsh->stub_type = metag_stub_long_branch;
  857. hsh->hh = NULL;
  858. hsh->id_sec = NULL;
  859. }
  860. return entry;
  861. }
  862. /* Initialize an entry in the link hash table. */
  863. static struct bfd_hash_entry *
  864. metag_link_hash_newfunc (struct bfd_hash_entry *entry,
  865. struct bfd_hash_table *table,
  866. const char *string)
  867. {
  868. /* Allocate the structure if it has not already been allocated by a
  869. subclass. */
  870. if (entry == NULL)
  871. {
  872. entry = bfd_hash_allocate (table,
  873. sizeof (struct elf_metag_link_hash_entry));
  874. if (entry == NULL)
  875. return entry;
  876. }
  877. /* Call the allocation method of the superclass. */
  878. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  879. if (entry != NULL)
  880. {
  881. struct elf_metag_link_hash_entry *hh;
  882. /* Initialize the local fields. */
  883. hh = (struct elf_metag_link_hash_entry *) entry;
  884. hh->hsh_cache = NULL;
  885. hh->tls_type = GOT_UNKNOWN;
  886. }
  887. return entry;
  888. }
  889. /* Free the derived linker hash table. */
  890. static void
  891. elf_metag_link_hash_table_free (bfd *obfd)
  892. {
  893. struct elf_metag_link_hash_table *htab
  894. = (struct elf_metag_link_hash_table *) obfd->link.hash;
  895. bfd_hash_table_free (&htab->bstab);
  896. _bfd_elf_link_hash_table_free (obfd);
  897. }
  898. /* Create the derived linker hash table. The Meta ELF port uses the derived
  899. hash table to keep information specific to the Meta ELF linker (without
  900. using static variables). */
  901. static struct bfd_link_hash_table *
  902. elf_metag_link_hash_table_create (bfd *abfd)
  903. {
  904. struct elf_metag_link_hash_table *htab;
  905. size_t amt = sizeof (*htab);
  906. htab = bfd_zmalloc (amt);
  907. if (htab == NULL)
  908. return NULL;
  909. if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
  910. metag_link_hash_newfunc,
  911. sizeof (struct elf_metag_link_hash_entry),
  912. METAG_ELF_DATA))
  913. {
  914. free (htab);
  915. return NULL;
  916. }
  917. /* Init the stub hash table too. */
  918. if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
  919. sizeof (struct elf_metag_stub_hash_entry)))
  920. {
  921. _bfd_elf_link_hash_table_free (abfd);
  922. return NULL;
  923. }
  924. htab->etab.root.hash_table_free = elf_metag_link_hash_table_free;
  925. htab->etab.dt_pltgot_required = true;
  926. return &htab->etab.root;
  927. }
  928. /* Section name for stubs is the associated section name plus this
  929. string. */
  930. #define STUB_SUFFIX ".stub"
  931. /* Build a name for an entry in the stub hash table. */
  932. static char *
  933. metag_stub_name (const asection *input_section,
  934. const asection *sym_sec,
  935. const struct elf_metag_link_hash_entry *hh,
  936. const Elf_Internal_Rela *rel)
  937. {
  938. char *stub_name;
  939. bfd_size_type len;
  940. if (hh)
  941. {
  942. len = 8 + 1 + strlen (hh->eh.root.root.string) + 1 + 8 + 1;
  943. stub_name = bfd_malloc (len);
  944. if (stub_name != NULL)
  945. {
  946. sprintf (stub_name, "%08x_%s+%x",
  947. input_section->id & 0xffffffff,
  948. hh->eh.root.root.string,
  949. (int) rel->r_addend & 0xffffffff);
  950. }
  951. }
  952. else
  953. {
  954. len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
  955. stub_name = bfd_malloc (len);
  956. if (stub_name != NULL)
  957. {
  958. sprintf (stub_name, "%08x_%x:%x+%x",
  959. input_section->id & 0xffffffff,
  960. sym_sec->id & 0xffffffff,
  961. (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
  962. (int) rel->r_addend & 0xffffffff);
  963. }
  964. }
  965. return stub_name;
  966. }
  967. /* Look up an entry in the stub hash. Stub entries are cached because
  968. creating the stub name takes a bit of time. */
  969. static struct elf_metag_stub_hash_entry *
  970. metag_get_stub_entry (const asection *input_section,
  971. const asection *sym_sec,
  972. struct elf_metag_link_hash_entry *hh,
  973. const Elf_Internal_Rela *rel,
  974. struct elf_metag_link_hash_table *htab)
  975. {
  976. struct elf_metag_stub_hash_entry *hsh;
  977. const asection *id_sec;
  978. /* If this input section is part of a group of sections sharing one
  979. stub section, then use the id of the first section in the group.
  980. Stub names need to include a section id, as there may well be
  981. more than one stub used to reach say, printf, and we need to
  982. distinguish between them. */
  983. id_sec = htab->stub_group[input_section->id].link_sec;
  984. if (hh != NULL && hh->hsh_cache != NULL
  985. && hh->hsh_cache->hh == hh
  986. && hh->hsh_cache->id_sec == id_sec)
  987. {
  988. hsh = hh->hsh_cache;
  989. }
  990. else
  991. {
  992. char *stub_name;
  993. stub_name = metag_stub_name (id_sec, sym_sec, hh, rel);
  994. if (stub_name == NULL)
  995. return NULL;
  996. hsh = metag_stub_hash_lookup (&htab->bstab,
  997. stub_name, false, false);
  998. if (hh != NULL)
  999. hh->hsh_cache = hsh;
  1000. free (stub_name);
  1001. }
  1002. return hsh;
  1003. }
  1004. /* Add a new stub entry to the stub hash. Not all fields of the new
  1005. stub entry are initialised. */
  1006. static struct elf_metag_stub_hash_entry *
  1007. metag_add_stub (const char *stub_name,
  1008. asection *section,
  1009. struct elf_metag_link_hash_table *htab)
  1010. {
  1011. asection *link_sec;
  1012. asection *stub_sec;
  1013. struct elf_metag_stub_hash_entry *hsh;
  1014. link_sec = htab->stub_group[section->id].link_sec;
  1015. stub_sec = htab->stub_group[section->id].stub_sec;
  1016. if (stub_sec == NULL)
  1017. {
  1018. stub_sec = htab->stub_group[link_sec->id].stub_sec;
  1019. if (stub_sec == NULL)
  1020. {
  1021. size_t namelen;
  1022. bfd_size_type len;
  1023. char *s_name;
  1024. namelen = strlen (link_sec->name);
  1025. len = namelen + sizeof (STUB_SUFFIX);
  1026. s_name = bfd_alloc (htab->stub_bfd, len);
  1027. if (s_name == NULL)
  1028. return NULL;
  1029. memcpy (s_name, link_sec->name, namelen);
  1030. memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
  1031. stub_sec = (*htab->add_stub_section) (s_name, link_sec);
  1032. if (stub_sec == NULL)
  1033. return NULL;
  1034. htab->stub_group[link_sec->id].stub_sec = stub_sec;
  1035. }
  1036. htab->stub_group[section->id].stub_sec = stub_sec;
  1037. }
  1038. /* Enter this entry into the linker stub hash table. */
  1039. hsh = metag_stub_hash_lookup (&htab->bstab, stub_name,
  1040. true, false);
  1041. if (hsh == NULL)
  1042. {
  1043. /* xgettext:c-format */
  1044. _bfd_error_handler (_("%pB: cannot create stub entry %s"),
  1045. section->owner, stub_name);
  1046. return NULL;
  1047. }
  1048. hsh->stub_sec = stub_sec;
  1049. hsh->stub_offset = 0;
  1050. hsh->id_sec = link_sec;
  1051. return hsh;
  1052. }
  1053. /* Check a signed integer value can be represented in the given number
  1054. of bits. */
  1055. static bool
  1056. within_signed_range (int value, unsigned int bits)
  1057. {
  1058. int min_val = -(1 << (bits - 1));
  1059. int max_val = (1 << (bits - 1)) - 1;
  1060. return (value <= max_val) && (value >= min_val);
  1061. }
  1062. /* Perform a relocation as part of a final link. */
  1063. static bfd_reloc_status_type
  1064. metag_final_link_relocate (reloc_howto_type *howto,
  1065. bfd *input_bfd,
  1066. asection *input_section,
  1067. bfd_byte *contents,
  1068. Elf_Internal_Rela *rel,
  1069. bfd_vma relocation,
  1070. struct elf_metag_link_hash_entry *hh,
  1071. struct elf_metag_link_hash_table *htab,
  1072. asection *sym_sec)
  1073. {
  1074. bfd_reloc_status_type r = bfd_reloc_ok;
  1075. bfd_byte *hit_data = contents + rel->r_offset;
  1076. int opcode, op_shift, op_extended, l1, l2;
  1077. bfd_signed_vma srel, addend = rel->r_addend;
  1078. struct elf_metag_stub_hash_entry *hsh = NULL;
  1079. bfd_vma location;
  1080. /* Find out where we are and where we're going. */
  1081. location = (rel->r_offset +
  1082. input_section->output_offset +
  1083. input_section->output_section->vma);
  1084. switch (howto->type)
  1085. {
  1086. case R_METAG_RELBRANCH:
  1087. case R_METAG_RELBRANCH_PLT:
  1088. /* Make it a pc relative offset. */
  1089. relocation -= location;
  1090. break;
  1091. case R_METAG_TLS_GD:
  1092. case R_METAG_TLS_IE:
  1093. relocation -= elf_gp (input_section->output_section->owner);
  1094. break;
  1095. default:
  1096. break;
  1097. }
  1098. switch (howto->type)
  1099. {
  1100. case R_METAG_RELBRANCH_PLT:
  1101. case R_METAG_RELBRANCH:
  1102. opcode = bfd_get_32 (input_bfd, hit_data);
  1103. srel = (bfd_signed_vma) relocation;
  1104. srel += addend;
  1105. /* If the branch is out of reach, then redirect the
  1106. call to the local stub for this function. */
  1107. if (srel > ((1 << (BRANCH_BITS + 1)) - 1) ||
  1108. (srel < - (1 << (BRANCH_BITS + 1))))
  1109. {
  1110. if (sym_sec == NULL)
  1111. break;
  1112. hsh = metag_get_stub_entry (input_section, sym_sec,
  1113. hh, rel, htab);
  1114. if (hsh == NULL)
  1115. return bfd_reloc_undefined;
  1116. /* Munge up the value and addend so that we call the stub
  1117. rather than the procedure directly. */
  1118. srel = (hsh->stub_offset
  1119. + hsh->stub_sec->output_offset
  1120. + hsh->stub_sec->output_section->vma);
  1121. srel -= location;
  1122. }
  1123. srel = srel >> 2;
  1124. if (!within_signed_range (srel, BRANCH_BITS))
  1125. {
  1126. if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
  1127. srel = 0;
  1128. else
  1129. return bfd_reloc_overflow;
  1130. }
  1131. opcode &= ~(0x7ffff << 5);
  1132. opcode |= ((srel & 0x7ffff) << 5);
  1133. bfd_put_32 (input_bfd, opcode, hit_data);
  1134. break;
  1135. case R_METAG_GETSETOFF:
  1136. case R_METAG_GETSET_GOT:
  1137. case R_METAG_GETSET_GOTOFF:
  1138. opcode = bfd_get_32 (input_bfd, hit_data);
  1139. srel = (bfd_signed_vma) relocation;
  1140. srel += addend;
  1141. /* Is this a standard or extended GET/SET? */
  1142. if ((opcode & 0xf0000000) == 0xa0000000)
  1143. {
  1144. /* Extended GET/SET. */
  1145. l1 = opcode & 0x2;
  1146. l2 = opcode & 0x4;
  1147. op_extended = 1;
  1148. }
  1149. else
  1150. {
  1151. /* Standard GET/SET. */
  1152. l1 = opcode & 0x01000000;
  1153. l2 = opcode & 0x04000000;
  1154. op_extended = 0;
  1155. }
  1156. /* Calculate the width of the GET/SET and how much we need to
  1157. shift the result by. */
  1158. if (l2)
  1159. if (l1)
  1160. op_shift = 3;
  1161. else
  1162. op_shift = 2;
  1163. else
  1164. if (l1)
  1165. op_shift = 1;
  1166. else
  1167. op_shift = 0;
  1168. /* GET/SET offsets are scaled by the width of the transfer. */
  1169. srel = srel >> op_shift;
  1170. /* Extended GET/SET has signed 12 bits of offset, standard has
  1171. signed 6 bits. */
  1172. if (op_extended)
  1173. {
  1174. if (!within_signed_range (srel, 12))
  1175. {
  1176. if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
  1177. srel = 0;
  1178. else
  1179. return bfd_reloc_overflow;
  1180. }
  1181. opcode &= ~(0xfff << 7);
  1182. opcode |= ((srel & 0xfff) << 7);
  1183. }
  1184. else
  1185. {
  1186. if (!within_signed_range (srel, 5))
  1187. {
  1188. if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
  1189. srel = 0;
  1190. else
  1191. return bfd_reloc_overflow;
  1192. }
  1193. opcode &= ~(0x3f << 8);
  1194. opcode |= ((srel & 0x3f) << 8);
  1195. }
  1196. bfd_put_32 (input_bfd, opcode, hit_data);
  1197. break;
  1198. case R_METAG_TLS_GD:
  1199. case R_METAG_TLS_LDM:
  1200. opcode = bfd_get_32 (input_bfd, hit_data);
  1201. if ((bfd_signed_vma)relocation < 0)
  1202. {
  1203. /* sign extend immediate */
  1204. if ((opcode & 0xf2000001) == 0x02000000)
  1205. {
  1206. /* ADD De.e,Dx.r,#I16 */
  1207. /* set SE bit */
  1208. opcode |= (1 << 1);
  1209. } else
  1210. return bfd_reloc_overflow;
  1211. }
  1212. bfd_put_32 (input_bfd, opcode, hit_data);
  1213. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1214. contents, rel->r_offset,
  1215. relocation, rel->r_addend);
  1216. break;
  1217. default:
  1218. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1219. contents, rel->r_offset,
  1220. relocation, rel->r_addend);
  1221. }
  1222. return r;
  1223. }
  1224. /* This is defined because R_METAG_NONE != 0...
  1225. See RELOC_AGAINST_DISCARDED_SECTION for details. */
  1226. #define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \
  1227. rel, relend, howto, contents) \
  1228. { \
  1229. _bfd_clear_contents (howto, input_bfd, input_section, \
  1230. contents, rel->r_offset); \
  1231. \
  1232. if (bfd_link_relocatable (info) \
  1233. && (input_section->flags & SEC_DEBUGGING)) \
  1234. { \
  1235. /* Only remove relocations in debug sections since other \
  1236. sections may require relocations. */ \
  1237. Elf_Internal_Shdr *rel_hdr; \
  1238. \
  1239. rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \
  1240. \
  1241. /* Avoid empty output section. */ \
  1242. if (rel_hdr->sh_size > rel_hdr->sh_entsize) \
  1243. { \
  1244. rel_hdr->sh_size -= rel_hdr->sh_entsize; \
  1245. rel_hdr = _bfd_elf_single_rel_hdr (input_section); \
  1246. rel_hdr->sh_size -= rel_hdr->sh_entsize; \
  1247. \
  1248. memmove (rel, rel + 1, (relend - rel) * sizeof (*rel)); \
  1249. \
  1250. input_section->reloc_count--; \
  1251. relend--; \
  1252. rel--; \
  1253. continue; \
  1254. } \
  1255. } \
  1256. \
  1257. rel->r_info = R_METAG_NONE; \
  1258. rel->r_addend = 0; \
  1259. continue; \
  1260. }
  1261. /* Relocate a META ELF section.
  1262. The RELOCATE_SECTION function is called by the new ELF backend linker
  1263. to handle the relocations for a section.
  1264. The relocs are always passed as Rela structures; if the section
  1265. actually uses Rel structures, the r_addend field will always be
  1266. zero.
  1267. This function is responsible for adjusting the section contents as
  1268. necessary, and (if using Rela relocs and generating a relocatable
  1269. output file) adjusting the reloc addend as necessary.
  1270. This function does not have to worry about setting the reloc
  1271. address or the reloc symbol index.
  1272. LOCAL_SYMS is a pointer to the swapped in local symbols.
  1273. LOCAL_SECTIONS is an array giving the section in the input file
  1274. corresponding to the st_shndx field of each local symbol.
  1275. The global hash table entry for the global symbols can be found
  1276. via elf_sym_hashes (input_bfd).
  1277. When generating relocatable output, this function must handle
  1278. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  1279. going to be the section symbol corresponding to the output
  1280. section, which means that the addend must be adjusted
  1281. accordingly. */
  1282. static int
  1283. elf_metag_relocate_section (bfd *output_bfd,
  1284. struct bfd_link_info *info,
  1285. bfd *input_bfd,
  1286. asection *input_section,
  1287. bfd_byte *contents,
  1288. Elf_Internal_Rela *relocs,
  1289. Elf_Internal_Sym *local_syms,
  1290. asection **local_sections)
  1291. {
  1292. bfd_vma *local_got_offsets;
  1293. Elf_Internal_Shdr *symtab_hdr;
  1294. struct elf_link_hash_entry **eh_syms;
  1295. struct elf_metag_link_hash_table *htab;
  1296. Elf_Internal_Rela *rel;
  1297. Elf_Internal_Rela *relend;
  1298. asection *sreloc;
  1299. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  1300. eh_syms = elf_sym_hashes (input_bfd);
  1301. relend = relocs + input_section->reloc_count;
  1302. htab = metag_link_hash_table (info);
  1303. local_got_offsets = elf_local_got_offsets (input_bfd);
  1304. sreloc = NULL;
  1305. for (rel = relocs; rel < relend; rel ++)
  1306. {
  1307. reloc_howto_type *howto;
  1308. unsigned long r_symndx;
  1309. Elf_Internal_Sym *sym;
  1310. asection *sec;
  1311. struct elf_metag_link_hash_entry *hh;
  1312. bfd_vma relocation;
  1313. bfd_reloc_status_type r;
  1314. const char *name;
  1315. int r_type;
  1316. r_type = ELF32_R_TYPE (rel->r_info);
  1317. if (r_type == R_METAG_GNU_VTINHERIT
  1318. || r_type == R_METAG_GNU_VTENTRY
  1319. || r_type == R_METAG_NONE)
  1320. continue;
  1321. r_symndx = ELF32_R_SYM (rel->r_info);
  1322. howto = elf_metag_howto_table + ELF32_R_TYPE (rel->r_info);
  1323. hh = NULL;
  1324. sym = NULL;
  1325. sec = NULL;
  1326. if (r_symndx < symtab_hdr->sh_info)
  1327. {
  1328. sym = local_syms + r_symndx;
  1329. sec = local_sections [r_symndx];
  1330. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1331. name = bfd_elf_string_from_elf_section
  1332. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  1333. name = name == NULL ? bfd_section_name (sec) : name;
  1334. }
  1335. else
  1336. {
  1337. struct elf_link_hash_entry *eh;
  1338. bool unresolved_reloc, warned, ignored;
  1339. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1340. r_symndx, symtab_hdr, eh_syms,
  1341. eh, sec, relocation,
  1342. unresolved_reloc, warned, ignored);
  1343. name = eh->root.root.string;
  1344. hh = (struct elf_metag_link_hash_entry *) eh;
  1345. }
  1346. if (sec != NULL && discarded_section (sec))
  1347. METAG_RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1348. rel, relend, howto, contents);
  1349. if (bfd_link_relocatable (info))
  1350. continue;
  1351. switch (r_type)
  1352. {
  1353. case R_METAG_ADDR32:
  1354. case R_METAG_RELBRANCH:
  1355. if ((input_section->flags & SEC_ALLOC) == 0)
  1356. break;
  1357. if ((bfd_link_pic (info)
  1358. && r_symndx != STN_UNDEF
  1359. && (input_section->flags & SEC_ALLOC) != 0
  1360. && !(r_type == R_METAG_RELBRANCH
  1361. && (hh == NULL || SYMBOL_CALLS_LOCAL (info, &hh->eh))))
  1362. || (!bfd_link_pic (info)
  1363. && hh != NULL
  1364. && hh->eh.dynindx != -1
  1365. && !hh->eh.non_got_ref
  1366. && ((hh->eh.def_dynamic
  1367. && !hh->eh.def_regular)
  1368. || hh->eh.root.type == bfd_link_hash_undefweak
  1369. || hh->eh.root.type == bfd_link_hash_undefined)))
  1370. {
  1371. Elf_Internal_Rela outrel;
  1372. bool skip, relocate;
  1373. bfd_byte *loc;
  1374. /* When generating a shared object, these relocations
  1375. are copied into the output file to be resolved at run
  1376. time. */
  1377. sreloc = elf_section_data (input_section)->sreloc;
  1378. BFD_ASSERT (sreloc != NULL);
  1379. skip = false;
  1380. relocate = false;
  1381. outrel.r_offset = _bfd_elf_section_offset (output_bfd,
  1382. info,
  1383. input_section,
  1384. rel->r_offset);
  1385. if (outrel.r_offset == (bfd_vma) -1)
  1386. skip = true;
  1387. else if (outrel.r_offset == (bfd_vma) -2)
  1388. skip = true, relocate = true;
  1389. outrel.r_offset += (input_section->output_section->vma
  1390. + input_section->output_offset);
  1391. if (skip)
  1392. {
  1393. memset (&outrel, 0, sizeof outrel);
  1394. outrel.r_info = ELF32_R_INFO (0, R_METAG_NONE);
  1395. }
  1396. else if (r_type == R_METAG_RELBRANCH)
  1397. {
  1398. BFD_ASSERT (hh != NULL && hh->eh.dynindx != -1);
  1399. outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
  1400. outrel.r_addend = rel->r_addend;
  1401. }
  1402. else
  1403. {
  1404. /* h->dynindx may be -1 if this symbol was marked to
  1405. become local. */
  1406. if (hh == NULL
  1407. || ((info->symbolic || hh->eh.dynindx == -1)
  1408. && hh->eh.def_regular))
  1409. {
  1410. relocate = true;
  1411. outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
  1412. outrel.r_addend = relocation + rel->r_addend;
  1413. }
  1414. else
  1415. {
  1416. BFD_ASSERT (hh->eh.dynindx != -1);
  1417. outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
  1418. outrel.r_addend = rel->r_addend;
  1419. }
  1420. }
  1421. loc = sreloc->contents;
  1422. loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
  1423. bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
  1424. ++sreloc->reloc_count;
  1425. /* If this reloc is against an external symbol, we do
  1426. not want to fiddle with the addend. Otherwise, we
  1427. need to include the symbol value so that it becomes
  1428. an addend for the dynamic reloc. */
  1429. if (! relocate)
  1430. continue;
  1431. }
  1432. break;
  1433. case R_METAG_RELBRANCH_PLT:
  1434. /* Relocation is to the entry for this symbol in the
  1435. procedure linkage table. */
  1436. if (hh == NULL)
  1437. break;
  1438. if (hh->eh.forced_local)
  1439. break;
  1440. if (hh->eh.plt.offset == (bfd_vma) -1 || htab->etab.splt == NULL)
  1441. {
  1442. /* We didn't make a PLT entry for this symbol. This
  1443. happens when statically linking PIC code, or when
  1444. using -Bsymbolic. */
  1445. break;
  1446. }
  1447. relocation = (htab->etab.splt->output_section->vma
  1448. + htab->etab.splt->output_offset
  1449. + hh->eh.plt.offset);
  1450. break;
  1451. case R_METAG_HI16_GOTPC:
  1452. case R_METAG_LO16_GOTPC:
  1453. BFD_ASSERT (htab->etab.sgot != NULL);
  1454. relocation = (htab->etab.sgot->output_section->vma +
  1455. htab->etab.sgot->output_offset);
  1456. relocation += GOT_REG_OFFSET;
  1457. relocation -= (input_section->output_section->vma
  1458. + input_section->output_offset
  1459. + rel->r_offset);
  1460. break;
  1461. case R_METAG_HI16_GOTOFF:
  1462. case R_METAG_LO16_GOTOFF:
  1463. case R_METAG_GETSET_GOTOFF:
  1464. BFD_ASSERT (htab->etab.sgot != NULL);
  1465. relocation -= (htab->etab.sgot->output_section->vma +
  1466. htab->etab.sgot->output_offset);
  1467. relocation -= GOT_REG_OFFSET;
  1468. break;
  1469. case R_METAG_GETSET_GOT:
  1470. {
  1471. bfd_vma off;
  1472. bool do_got = 0;
  1473. /* Relocation is to the entry for this symbol in the
  1474. global offset table. */
  1475. if (hh != NULL)
  1476. {
  1477. bool dyn;
  1478. off = hh->eh.got.offset;
  1479. dyn = htab->etab.dynamic_sections_created;
  1480. if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  1481. bfd_link_pic (info),
  1482. &hh->eh))
  1483. {
  1484. /* If we aren't going to call finish_dynamic_symbol,
  1485. then we need to handle initialisation of the .got
  1486. entry and create needed relocs here. Since the
  1487. offset must always be a multiple of 4, we use the
  1488. least significant bit to record whether we have
  1489. initialised it already. */
  1490. if ((off & 1) != 0)
  1491. off &= ~1;
  1492. else
  1493. {
  1494. hh->eh.got.offset |= 1;
  1495. do_got = 1;
  1496. }
  1497. }
  1498. }
  1499. else
  1500. {
  1501. /* Local symbol case. */
  1502. if (local_got_offsets == NULL)
  1503. abort ();
  1504. off = local_got_offsets[r_symndx];
  1505. /* The offset must always be a multiple of 4. We use
  1506. the least significant bit to record whether we have
  1507. already generated the necessary reloc. */
  1508. if ((off & 1) != 0)
  1509. off &= ~1;
  1510. else
  1511. {
  1512. local_got_offsets[r_symndx] |= 1;
  1513. do_got = 1;
  1514. }
  1515. }
  1516. if (do_got)
  1517. {
  1518. if (bfd_link_pic (info))
  1519. {
  1520. /* Output a dynamic relocation for this GOT entry.
  1521. In this case it is relative to the base of the
  1522. object because the symbol index is zero. */
  1523. Elf_Internal_Rela outrel;
  1524. bfd_byte *loc;
  1525. asection *s = htab->etab.srelgot;
  1526. outrel.r_offset = (off
  1527. + htab->etab.sgot->output_offset
  1528. + htab->etab.sgot->output_section->vma);
  1529. outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
  1530. outrel.r_addend = relocation;
  1531. loc = s->contents;
  1532. loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
  1533. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1534. }
  1535. else
  1536. bfd_put_32 (output_bfd, relocation,
  1537. htab->etab.sgot->contents + off);
  1538. }
  1539. if (off >= (bfd_vma) -2)
  1540. abort ();
  1541. relocation = off - GOT_REG_OFFSET;
  1542. }
  1543. break;
  1544. case R_METAG_TLS_GD:
  1545. case R_METAG_TLS_IE:
  1546. {
  1547. /* XXXMJF There is room here for optimisations. For example
  1548. converting from GD->IE, etc. */
  1549. bfd_vma off;
  1550. int indx;
  1551. char tls_type;
  1552. if (htab->etab.sgot == NULL)
  1553. abort();
  1554. indx = 0;
  1555. if (hh != NULL)
  1556. {
  1557. bool dyn;
  1558. dyn = htab->etab.dynamic_sections_created;
  1559. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  1560. bfd_link_pic (info),
  1561. &hh->eh)
  1562. && (!bfd_link_pic (info)
  1563. || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
  1564. {
  1565. indx = hh->eh.dynindx;
  1566. }
  1567. off = hh->eh.got.offset;
  1568. tls_type = hh->tls_type;
  1569. }
  1570. else
  1571. {
  1572. /* Local symbol case. */
  1573. if (local_got_offsets == NULL)
  1574. abort ();
  1575. off = local_got_offsets[r_symndx];
  1576. tls_type = metag_elf_local_got_tls_type (input_bfd) [r_symndx];
  1577. }
  1578. if (tls_type == GOT_UNKNOWN)
  1579. abort ();
  1580. if ((off & 1) != 0)
  1581. off &= ~1;
  1582. else
  1583. {
  1584. bool need_relocs = false;
  1585. Elf_Internal_Rela outrel;
  1586. bfd_byte *loc = NULL;
  1587. int cur_off = off;
  1588. /* The GOT entries have not been initialized yet. Do it
  1589. now, and emit any relocations. If both an IE GOT and a
  1590. GD GOT are necessary, we emit the GD first. */
  1591. if ((bfd_link_pic (info) || indx != 0)
  1592. && (hh == NULL
  1593. || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
  1594. || hh->eh.root.type != bfd_link_hash_undefweak))
  1595. {
  1596. need_relocs = true;
  1597. loc = htab->etab.srelgot->contents;
  1598. /* FIXME (CAO): Should this be reloc_count++ ? */
  1599. loc += htab->etab.srelgot->reloc_count * sizeof (Elf32_External_Rela);
  1600. }
  1601. if (tls_type & GOT_TLS_GD)
  1602. {
  1603. if (need_relocs)
  1604. {
  1605. outrel.r_offset = (cur_off
  1606. + htab->etab.sgot->output_section->vma
  1607. + htab->etab.sgot->output_offset);
  1608. outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_DTPMOD);
  1609. outrel.r_addend = 0;
  1610. bfd_put_32 (output_bfd, 0, htab->etab.sgot->contents + cur_off);
  1611. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1612. htab->etab.srelgot->reloc_count++;
  1613. loc += sizeof (Elf32_External_Rela);
  1614. if (indx == 0)
  1615. bfd_put_32 (output_bfd, 0,
  1616. htab->etab.sgot->contents + cur_off + 4);
  1617. else
  1618. {
  1619. bfd_put_32 (output_bfd, 0,
  1620. htab->etab.sgot->contents + cur_off + 4);
  1621. outrel.r_info = ELF32_R_INFO (indx,
  1622. R_METAG_TLS_DTPOFF);
  1623. outrel.r_offset += 4;
  1624. bfd_elf32_swap_reloca_out (output_bfd,
  1625. &outrel, loc);
  1626. htab->etab.srelgot->reloc_count++;
  1627. loc += sizeof (Elf32_External_Rela);
  1628. }
  1629. }
  1630. else
  1631. {
  1632. /* We don't support changing the TLS model. */
  1633. /* PR 20675 */
  1634. if (bfd_link_pic (info))
  1635. _bfd_error_handler (_("%pB(%pA): multiple TLS models are not supported"),
  1636. input_bfd, input_section);
  1637. else
  1638. _bfd_error_handler (_("%pB(%pA): shared library symbol %s encountered whilst performing a static link"),
  1639. input_bfd, input_section, name);
  1640. return false;
  1641. }
  1642. cur_off += 8;
  1643. }
  1644. if (tls_type & GOT_TLS_IE)
  1645. {
  1646. if (need_relocs)
  1647. {
  1648. outrel.r_offset = (cur_off
  1649. + htab->etab.sgot->output_section->vma
  1650. + htab->etab.sgot->output_offset);
  1651. outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_TPOFF);
  1652. if (indx == 0)
  1653. outrel.r_addend = relocation - dtpoff_base (info);
  1654. else
  1655. outrel.r_addend = 0;
  1656. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1657. htab->etab.srelgot->reloc_count++;
  1658. loc += sizeof (Elf32_External_Rela);
  1659. }
  1660. else
  1661. bfd_put_32 (output_bfd, tpoff (info, relocation),
  1662. htab->etab.sgot->contents + cur_off);
  1663. cur_off += 4;
  1664. }
  1665. if (hh != NULL)
  1666. hh->eh.got.offset |= 1;
  1667. else
  1668. local_got_offsets[r_symndx] |= 1;
  1669. }
  1670. /* Add the base of the GOT to the relocation value. */
  1671. relocation = off - GOT_REG_OFFSET;
  1672. break;
  1673. }
  1674. case R_METAG_TLS_IENONPIC_HI16:
  1675. case R_METAG_TLS_IENONPIC_LO16:
  1676. case R_METAG_TLS_LE_HI16:
  1677. case R_METAG_TLS_LE_LO16:
  1678. if (bfd_link_pic (info))
  1679. {
  1680. _bfd_error_handler
  1681. /* xgettext:c-format */
  1682. (_("%pB(%pA+%#" PRIx64 "): "
  1683. "%s relocation not permitted in shared object"),
  1684. input_bfd, input_section, (uint64_t) rel->r_offset,
  1685. howto->name);
  1686. return false;
  1687. }
  1688. else
  1689. relocation = tpoff (info, relocation);
  1690. break;
  1691. case R_METAG_TLS_LDO_HI16:
  1692. case R_METAG_TLS_LDO_LO16:
  1693. if (! bfd_link_pic (info))
  1694. relocation = tpoff (info, relocation);
  1695. else
  1696. relocation -= dtpoff_base (info);
  1697. break;
  1698. case R_METAG_TLS_LDM:
  1699. {
  1700. bfd_vma off;
  1701. if (htab->etab.sgot == NULL)
  1702. abort();
  1703. off = htab->tls_ldm_got.offset;
  1704. if (off & 1)
  1705. off &= ~1;
  1706. else
  1707. {
  1708. Elf_Internal_Rela outrel;
  1709. bfd_byte *loc;
  1710. outrel.r_offset = (off
  1711. + htab->etab.sgot->output_section->vma
  1712. + htab->etab.sgot->output_offset);
  1713. outrel.r_addend = 0;
  1714. outrel.r_info = ELF32_R_INFO (0, R_METAG_TLS_DTPMOD);
  1715. loc = htab->etab.srelgot->contents;
  1716. loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  1717. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1718. htab->tls_ldm_got.offset |= 1;
  1719. }
  1720. relocation = off - GOT_REG_OFFSET;
  1721. break;
  1722. }
  1723. default:
  1724. break;
  1725. }
  1726. r = metag_final_link_relocate (howto, input_bfd, input_section,
  1727. contents, rel, relocation, hh, htab,
  1728. sec);
  1729. if (r != bfd_reloc_ok)
  1730. {
  1731. const char * msg = (const char *) NULL;
  1732. switch (r)
  1733. {
  1734. case bfd_reloc_overflow:
  1735. (*info->callbacks->reloc_overflow)
  1736. (info, (hh ? &hh->eh.root : NULL), name, howto->name,
  1737. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  1738. break;
  1739. case bfd_reloc_undefined:
  1740. (*info->callbacks->undefined_symbol)
  1741. (info, name, input_bfd, input_section, rel->r_offset, true);
  1742. break;
  1743. case bfd_reloc_outofrange:
  1744. msg = _("internal error: out of range error");
  1745. break;
  1746. case bfd_reloc_notsupported:
  1747. msg = _("internal error: unsupported relocation error");
  1748. break;
  1749. case bfd_reloc_dangerous:
  1750. msg = _("internal error: dangerous relocation");
  1751. break;
  1752. default:
  1753. msg = _("internal error: unknown error");
  1754. break;
  1755. }
  1756. if (msg)
  1757. (*info->callbacks->warning) (info, msg, name, input_bfd,
  1758. input_section, rel->r_offset);
  1759. }
  1760. }
  1761. return true;
  1762. }
  1763. /* Create the .plt and .got sections, and set up our hash table
  1764. short-cuts to various dynamic sections. */
  1765. static bool
  1766. elf_metag_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  1767. {
  1768. struct elf_metag_link_hash_table *htab;
  1769. struct elf_link_hash_entry *eh;
  1770. struct bfd_link_hash_entry *bh;
  1771. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1772. /* Don't try to create the .plt and .got twice. */
  1773. htab = metag_link_hash_table (info);
  1774. if (htab->etab.splt != NULL)
  1775. return true;
  1776. /* Call the generic code to do most of the work. */
  1777. if (! _bfd_elf_create_dynamic_sections (abfd, info))
  1778. return false;
  1779. /* The header goes at the start of the dynamic .got section, which
  1780. is placed after the dynamic .got.plt section. ie. The header is
  1781. not necessarily at the start of the output .got section. */
  1782. htab->etab.sgot->size += 12;
  1783. /* Define the symbol __GLOBAL_OFFSET_TABLE__ on the header. */
  1784. bh = NULL;
  1785. if (!(_bfd_generic_link_add_one_symbol
  1786. (info, abfd, "__GLOBAL_OFFSET_TABLE__", BSF_GLOBAL, htab->etab.sgot,
  1787. (bfd_vma) 0, NULL, false, bed->collect, &bh)))
  1788. return false;
  1789. eh = (struct elf_link_hash_entry *) bh;
  1790. eh->def_regular = 1;
  1791. eh->type = STT_OBJECT;
  1792. eh->other = STV_HIDDEN;
  1793. if (! bfd_link_executable (info)
  1794. && ! bfd_elf_link_record_dynamic_symbol (info, eh))
  1795. return false;
  1796. htab->etab.hgot = eh;
  1797. return true;
  1798. }
  1799. /* Look through the relocs for a section during the first phase, and
  1800. calculate needed space in the global offset table, procedure linkage
  1801. table, and dynamic reloc sections. At this point we haven't
  1802. necessarily read all the input files. */
  1803. static bool
  1804. elf_metag_check_relocs (bfd *abfd,
  1805. struct bfd_link_info *info,
  1806. asection *sec,
  1807. const Elf_Internal_Rela *relocs)
  1808. {
  1809. Elf_Internal_Shdr *symtab_hdr;
  1810. struct elf_link_hash_entry **eh_syms;
  1811. const Elf_Internal_Rela *rel;
  1812. const Elf_Internal_Rela *rel_end;
  1813. struct elf_metag_link_hash_table *htab;
  1814. asection *sreloc;
  1815. bfd *dynobj;
  1816. int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN;
  1817. if (bfd_link_relocatable (info))
  1818. return true;
  1819. htab = metag_link_hash_table (info);
  1820. dynobj = htab->etab.dynobj;
  1821. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1822. eh_syms = elf_sym_hashes (abfd);
  1823. sreloc = NULL;
  1824. if (htab == NULL)
  1825. return false;
  1826. rel_end = relocs + sec->reloc_count;
  1827. for (rel = relocs; rel < rel_end; rel++)
  1828. {
  1829. int r_type;
  1830. struct elf_metag_link_hash_entry *hh;
  1831. Elf_Internal_Sym *isym;
  1832. unsigned long r_symndx;
  1833. r_symndx = ELF32_R_SYM (rel->r_info);
  1834. r_type = ELF32_R_TYPE (rel->r_info);
  1835. if (r_symndx < symtab_hdr->sh_info)
  1836. {
  1837. /* A local symbol. */
  1838. isym = bfd_sym_from_r_symndx (&htab->etab.sym_cache,
  1839. abfd, r_symndx);
  1840. if (isym == NULL)
  1841. return false;
  1842. hh = NULL;
  1843. }
  1844. else
  1845. {
  1846. isym = NULL;
  1847. hh = (struct elf_metag_link_hash_entry *)
  1848. eh_syms[r_symndx - symtab_hdr->sh_info];
  1849. while (hh->eh.root.type == bfd_link_hash_indirect
  1850. || hh->eh.root.type == bfd_link_hash_warning)
  1851. hh = (struct elf_metag_link_hash_entry *) hh->eh.root.u.i.link;
  1852. }
  1853. /* Some relocs require a global offset table. */
  1854. if (htab->etab.sgot == NULL)
  1855. {
  1856. switch (r_type)
  1857. {
  1858. case R_METAG_TLS_GD:
  1859. case R_METAG_TLS_LDM:
  1860. case R_METAG_TLS_IE:
  1861. if (bfd_link_pic (info))
  1862. info->flags |= DF_STATIC_TLS;
  1863. /* Fall through. */
  1864. case R_METAG_HI16_GOTOFF:
  1865. case R_METAG_LO16_GOTOFF:
  1866. case R_METAG_GETSET_GOTOFF:
  1867. case R_METAG_GETSET_GOT:
  1868. case R_METAG_HI16_GOTPC:
  1869. case R_METAG_LO16_GOTPC:
  1870. if (dynobj == NULL)
  1871. htab->etab.dynobj = dynobj = abfd;
  1872. if (!elf_metag_create_dynamic_sections (dynobj, info))
  1873. return false;
  1874. break;
  1875. default:
  1876. break;
  1877. }
  1878. }
  1879. switch (r_type)
  1880. {
  1881. case R_METAG_TLS_IE:
  1882. case R_METAG_TLS_GD:
  1883. case R_METAG_GETSET_GOT:
  1884. switch (r_type)
  1885. {
  1886. default:
  1887. tls_type = GOT_NORMAL;
  1888. break;
  1889. case R_METAG_TLS_IE:
  1890. tls_type = GOT_TLS_IE;
  1891. break;
  1892. case R_METAG_TLS_GD:
  1893. tls_type = GOT_TLS_GD;
  1894. break;
  1895. }
  1896. if (hh != NULL)
  1897. {
  1898. hh->eh.got.refcount += 1;
  1899. old_tls_type = hh->tls_type;
  1900. }
  1901. else
  1902. {
  1903. bfd_signed_vma *local_got_refcounts;
  1904. /* This is a global offset table entry for a local
  1905. symbol. */
  1906. local_got_refcounts = elf_local_got_refcounts (abfd);
  1907. if (local_got_refcounts == NULL)
  1908. {
  1909. bfd_size_type size;
  1910. size = symtab_hdr->sh_info;
  1911. size *= sizeof (bfd_signed_vma);
  1912. /* Add in space to store the local GOT TLS types. */
  1913. size += symtab_hdr->sh_info;
  1914. local_got_refcounts = ((bfd_signed_vma *)
  1915. bfd_zalloc (abfd, size));
  1916. if (local_got_refcounts == NULL)
  1917. return false;
  1918. elf_local_got_refcounts (abfd) = local_got_refcounts;
  1919. memset (metag_elf_local_got_tls_type (abfd),
  1920. GOT_UNKNOWN, symtab_hdr->sh_info);
  1921. }
  1922. local_got_refcounts[r_symndx] += 1;
  1923. old_tls_type = metag_elf_local_got_tls_type (abfd) [r_symndx];
  1924. }
  1925. if (old_tls_type != tls_type)
  1926. {
  1927. if (hh != NULL)
  1928. {
  1929. hh->tls_type = tls_type;
  1930. }
  1931. else
  1932. {
  1933. metag_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
  1934. }
  1935. }
  1936. break;
  1937. case R_METAG_TLS_LDM:
  1938. metag_link_hash_table (info)->tls_ldm_got.refcount += 1;
  1939. break;
  1940. case R_METAG_RELBRANCH_PLT:
  1941. /* This symbol requires a procedure linkage table entry. We
  1942. actually build the entry in adjust_dynamic_symbol,
  1943. because this might be a case of linking PIC code without
  1944. linking in any dynamic objects, in which case we don't
  1945. need to generate a procedure linkage table after all. */
  1946. /* If this is a local symbol, we resolve it directly without
  1947. creating a procedure linkage table entry. */
  1948. if (hh == NULL)
  1949. continue;
  1950. if (hh->eh.forced_local)
  1951. break;
  1952. hh->eh.needs_plt = 1;
  1953. hh->eh.plt.refcount += 1;
  1954. break;
  1955. case R_METAG_HIADDR16:
  1956. case R_METAG_LOADDR16:
  1957. /* Let's help debug shared library creation. These relocs
  1958. cannot be used in shared libs. Don't error out for
  1959. sections we don't care about, such as debug sections or
  1960. non-constant sections. */
  1961. if (bfd_link_pic (info)
  1962. && (sec->flags & SEC_ALLOC) != 0
  1963. && (sec->flags & SEC_READONLY) != 0)
  1964. {
  1965. const char *name;
  1966. if (hh)
  1967. name = hh->eh.root.root.string;
  1968. else
  1969. name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
  1970. _bfd_error_handler
  1971. /* xgettext:c-format */
  1972. (_("%pB: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
  1973. abfd, elf_metag_howto_table[r_type].name, name);
  1974. bfd_set_error (bfd_error_bad_value);
  1975. return false;
  1976. }
  1977. /* Fall through. */
  1978. case R_METAG_ADDR32:
  1979. case R_METAG_RELBRANCH:
  1980. case R_METAG_GETSETOFF:
  1981. if (hh != NULL && !bfd_link_pic (info))
  1982. {
  1983. hh->eh.non_got_ref = 1;
  1984. hh->eh.plt.refcount += 1;
  1985. }
  1986. /* If we are creating a shared library, and this is a reloc
  1987. against a global symbol, or a non PC relative reloc
  1988. against a local symbol, then we need to copy the reloc
  1989. into the shared library. However, if we are linking with
  1990. -Bsymbolic, we do not need to copy a reloc against a
  1991. global symbol which is defined in an object we are
  1992. including in the link (i.e., DEF_REGULAR is set). At
  1993. this point we have not seen all the input files, so it is
  1994. possible that DEF_REGULAR is not set now but will be set
  1995. later (it is never cleared). We account for that
  1996. possibility below by storing information in the
  1997. dyn_relocs field of the hash table entry. A similar
  1998. situation occurs when creating shared libraries and symbol
  1999. visibility changes render the symbol local.
  2000. If on the other hand, we are creating an executable, we
  2001. may need to keep relocations for symbols satisfied by a
  2002. dynamic library if we manage to avoid copy relocs for the
  2003. symbol. */
  2004. if ((bfd_link_pic (info)
  2005. && (sec->flags & SEC_ALLOC) != 0
  2006. && (r_type != R_METAG_RELBRANCH
  2007. || (hh != NULL
  2008. && (! info->symbolic
  2009. || hh->eh.root.type == bfd_link_hash_defweak
  2010. || !hh->eh.def_regular))))
  2011. || (!bfd_link_pic (info)
  2012. && (sec->flags & SEC_ALLOC) != 0
  2013. && hh != NULL
  2014. && (hh->eh.root.type == bfd_link_hash_defweak
  2015. || !hh->eh.def_regular)))
  2016. {
  2017. struct elf_dyn_relocs *hdh_p;
  2018. struct elf_dyn_relocs **hdh_head;
  2019. if (dynobj == NULL)
  2020. htab->etab.dynobj = dynobj = abfd;
  2021. /* When creating a shared object, we must copy these
  2022. relocs into the output file. We create a reloc
  2023. section in dynobj and make room for the reloc. */
  2024. if (sreloc == NULL)
  2025. {
  2026. sreloc = _bfd_elf_make_dynamic_reloc_section
  2027. (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ true);
  2028. if (sreloc == NULL)
  2029. {
  2030. bfd_set_error (bfd_error_bad_value);
  2031. return false;
  2032. }
  2033. elf_section_data (sec)->sreloc = sreloc;
  2034. }
  2035. /* If this is a global symbol, we count the number of
  2036. relocations we need for this symbol. */
  2037. if (hh != NULL)
  2038. hdh_head = &hh->eh.dyn_relocs;
  2039. else
  2040. {
  2041. /* Track dynamic relocs needed for local syms too. */
  2042. asection *sr;
  2043. void *vpp;
  2044. sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
  2045. if (sr == NULL)
  2046. sr = sec;
  2047. vpp = &elf_section_data (sr)->local_dynrel;
  2048. hdh_head = (struct elf_dyn_relocs **) vpp;
  2049. }
  2050. hdh_p = *hdh_head;
  2051. if (hdh_p == NULL || hdh_p->sec != sec)
  2052. {
  2053. hdh_p = ((struct elf_dyn_relocs *)
  2054. bfd_alloc (dynobj, sizeof *hdh_p));
  2055. if (hdh_p == NULL)
  2056. return false;
  2057. hdh_p->next = *hdh_head;
  2058. *hdh_head = hdh_p;
  2059. hdh_p->sec = sec;
  2060. hdh_p->count = 0;
  2061. hdh_p->pc_count = 0;
  2062. }
  2063. hdh_p->count += 1;
  2064. if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH)
  2065. hdh_p->pc_count += 1;
  2066. }
  2067. break;
  2068. /* This relocation describes the C++ object vtable hierarchy.
  2069. Reconstruct it for later use during GC. */
  2070. case R_METAG_GNU_VTINHERIT:
  2071. if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh,
  2072. rel->r_offset))
  2073. return false;
  2074. break;
  2075. /* This relocation describes which C++ vtable entries are actually
  2076. used. Record for later use during GC. */
  2077. case R_METAG_GNU_VTENTRY:
  2078. if (!bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rel->r_addend))
  2079. return false;
  2080. break;
  2081. }
  2082. }
  2083. return true;
  2084. }
  2085. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  2086. static void
  2087. elf_metag_copy_indirect_symbol (struct bfd_link_info *info,
  2088. struct elf_link_hash_entry *eh_dir,
  2089. struct elf_link_hash_entry *eh_ind)
  2090. {
  2091. struct elf_metag_link_hash_entry *hh_dir, *hh_ind;
  2092. hh_dir = metag_elf_hash_entry (eh_dir);
  2093. hh_ind = metag_elf_hash_entry (eh_ind);
  2094. if (eh_ind->root.type == bfd_link_hash_indirect
  2095. && eh_dir->got.refcount <= 0)
  2096. {
  2097. hh_dir->tls_type = hh_ind->tls_type;
  2098. hh_ind->tls_type = GOT_UNKNOWN;
  2099. }
  2100. _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
  2101. }
  2102. /* Adjust a symbol defined by a dynamic object and referenced by a
  2103. regular object. The current definition is in some section of the
  2104. dynamic object, but we're not including those sections. We have to
  2105. change the definition to something the rest of the link can
  2106. understand. */
  2107. static bool
  2108. elf_metag_adjust_dynamic_symbol (struct bfd_link_info *info,
  2109. struct elf_link_hash_entry *eh)
  2110. {
  2111. struct elf_metag_link_hash_table *htab;
  2112. asection *s, *srel;
  2113. /* If this is a function, put it in the procedure linkage table. We
  2114. will fill in the contents of the procedure linkage table later,
  2115. when we know the address of the .got section. */
  2116. if (eh->type == STT_FUNC
  2117. || eh->needs_plt)
  2118. {
  2119. if (eh->plt.refcount <= 0
  2120. || SYMBOL_CALLS_LOCAL (info, eh)
  2121. || (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT
  2122. && eh->root.type == bfd_link_hash_undefweak))
  2123. {
  2124. /* This case can occur if we saw a PLT reloc in an input
  2125. file, but the symbol was never referred to by a dynamic
  2126. object. In such a case, we don't actually need to build
  2127. a procedure linkage table, and we can just do a PCREL
  2128. reloc instead. */
  2129. eh->plt.offset = (bfd_vma) -1;
  2130. eh->needs_plt = 0;
  2131. }
  2132. return true;
  2133. }
  2134. else
  2135. eh->plt.offset = (bfd_vma) -1;
  2136. /* If this is a weak symbol, and there is a real definition, the
  2137. processor independent code will have arranged for us to see the
  2138. real definition first, and we can just use the same value. */
  2139. if (eh->is_weakalias)
  2140. {
  2141. struct elf_link_hash_entry *def = weakdef (eh);
  2142. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  2143. eh->root.u.def.section = def->root.u.def.section;
  2144. eh->root.u.def.value = def->root.u.def.value;
  2145. eh->non_got_ref = def->non_got_ref;
  2146. return true;
  2147. }
  2148. /* This is a reference to a symbol defined by a dynamic object which
  2149. is not a function. */
  2150. /* If we are creating a shared library, we must presume that the
  2151. only references to the symbol are via the global offset table.
  2152. For such cases we need not do anything here; the relocations will
  2153. be handled correctly by relocate_section. */
  2154. if (bfd_link_pic (info))
  2155. return true;
  2156. /* If there are no references to this symbol that do not use the
  2157. GOT, we don't need to generate a copy reloc. */
  2158. if (!eh->non_got_ref)
  2159. return true;
  2160. /* If -z nocopyreloc was given, we won't generate them either. */
  2161. if (info->nocopyreloc)
  2162. {
  2163. eh->non_got_ref = 0;
  2164. return true;
  2165. }
  2166. /* If we don't find any dynamic relocs in read-only sections, then
  2167. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  2168. if (!_bfd_elf_readonly_dynrelocs (eh))
  2169. {
  2170. eh->non_got_ref = 0;
  2171. return true;
  2172. }
  2173. /* We must allocate the symbol in our .dynbss section, which will
  2174. become part of the .bss section of the executable. There will be
  2175. an entry for this symbol in the .dynsym section. The dynamic
  2176. object will contain position independent code, so all references
  2177. from the dynamic object to this symbol will go through the global
  2178. offset table. The dynamic linker will use the .dynsym entry to
  2179. determine the address it must put in the global offset table, so
  2180. both the dynamic object and the regular object will refer to the
  2181. same memory location for the variable. */
  2182. htab = metag_link_hash_table (info);
  2183. /* We must generate a COPY reloc to tell the dynamic linker to
  2184. copy the initial value out of the dynamic object and into the
  2185. runtime process image. */
  2186. if ((eh->root.u.def.section->flags & SEC_READONLY) != 0)
  2187. {
  2188. s = htab->etab.sdynrelro;
  2189. srel = htab->etab.sreldynrelro;
  2190. }
  2191. else
  2192. {
  2193. s = htab->etab.sdynbss;
  2194. srel = htab->etab.srelbss;
  2195. }
  2196. if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
  2197. {
  2198. srel->size += sizeof (Elf32_External_Rela);
  2199. eh->needs_copy = 1;
  2200. }
  2201. return _bfd_elf_adjust_dynamic_copy (info, eh, s);
  2202. }
  2203. /* Allocate space in .plt, .got and associated reloc sections for
  2204. global syms. */
  2205. static bool
  2206. allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
  2207. {
  2208. struct bfd_link_info *info;
  2209. struct elf_metag_link_hash_table *htab;
  2210. struct elf_dyn_relocs *hdh_p;
  2211. if (eh->root.type == bfd_link_hash_indirect)
  2212. return true;
  2213. if (eh->root.type == bfd_link_hash_warning)
  2214. eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
  2215. info = inf;
  2216. htab = metag_link_hash_table (info);
  2217. if (htab->etab.dynamic_sections_created
  2218. && eh->plt.refcount > 0)
  2219. {
  2220. /* Make sure this symbol is output as a dynamic symbol.
  2221. Undefined weak syms won't yet be marked as dynamic. */
  2222. if (eh->dynindx == -1
  2223. && !eh->forced_local)
  2224. {
  2225. if (! bfd_elf_link_record_dynamic_symbol (info, eh))
  2226. return false;
  2227. }
  2228. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), eh))
  2229. {
  2230. asection *s = htab->etab.splt;
  2231. /* If this is the first .plt entry, make room for the special
  2232. first entry. */
  2233. if (s->size == 0)
  2234. s->size += PLT_ENTRY_SIZE;
  2235. eh->plt.offset = s->size;
  2236. /* If this symbol is not defined in a regular file, and we are
  2237. not generating a shared library, then set the symbol to this
  2238. location in the .plt. This is required to make function
  2239. pointers compare as equal between the normal executable and
  2240. the shared library. */
  2241. if (! bfd_link_pic (info)
  2242. && !eh->def_regular)
  2243. {
  2244. eh->root.u.def.section = s;
  2245. eh->root.u.def.value = eh->plt.offset;
  2246. }
  2247. /* Make room for this entry. */
  2248. s->size += PLT_ENTRY_SIZE;
  2249. /* We also need to make an entry in the .got.plt section, which
  2250. will be placed in the .got section by the linker script. */
  2251. htab->etab.sgotplt->size += 4;
  2252. /* We also need to make an entry in the .rel.plt section. */
  2253. htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
  2254. }
  2255. else
  2256. {
  2257. eh->plt.offset = (bfd_vma) -1;
  2258. eh->needs_plt = 0;
  2259. }
  2260. }
  2261. else
  2262. {
  2263. eh->plt.offset = (bfd_vma) -1;
  2264. eh->needs_plt = 0;
  2265. }
  2266. if (eh->got.refcount > 0)
  2267. {
  2268. asection *s;
  2269. bool dyn;
  2270. int tls_type = metag_elf_hash_entry (eh)->tls_type;
  2271. /* Make sure this symbol is output as a dynamic symbol.
  2272. Undefined weak syms won't yet be marked as dynamic. */
  2273. if (eh->dynindx == -1
  2274. && !eh->forced_local)
  2275. {
  2276. if (! bfd_elf_link_record_dynamic_symbol (info, eh))
  2277. return false;
  2278. }
  2279. s = htab->etab.sgot;
  2280. eh->got.offset = s->size;
  2281. s->size += 4;
  2282. /* R_METAG_TLS_GD needs 2 consecutive GOT slots. */
  2283. if (tls_type == GOT_TLS_GD)
  2284. s->size += 4;
  2285. dyn = htab->etab.dynamic_sections_created;
  2286. /* R_METAG_TLS_IE needs one dynamic relocation if dynamic,
  2287. R_METAG_TLS_GD needs one if local symbol and two if global. */
  2288. if ((tls_type == GOT_TLS_GD && eh->dynindx == -1)
  2289. || (tls_type == GOT_TLS_IE && dyn))
  2290. htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
  2291. else if (tls_type == GOT_TLS_GD)
  2292. htab->etab.srelgot->size += 2 * sizeof (Elf32_External_Rela);
  2293. else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  2294. bfd_link_pic (info),
  2295. eh))
  2296. htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
  2297. }
  2298. else
  2299. eh->got.offset = (bfd_vma) -1;
  2300. if (eh->dyn_relocs == NULL)
  2301. return true;
  2302. /* If this is a -Bsymbolic shared link, then we need to discard all
  2303. space allocated for dynamic pc-relative relocs against symbols
  2304. defined in a regular object. For the normal shared case, discard
  2305. space for relocs that have become local due to symbol visibility
  2306. changes. */
  2307. if (bfd_link_pic (info))
  2308. {
  2309. if (SYMBOL_CALLS_LOCAL (info, eh))
  2310. {
  2311. struct elf_dyn_relocs **hdh_pp;
  2312. for (hdh_pp = &eh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
  2313. {
  2314. hdh_p->count -= hdh_p->pc_count;
  2315. hdh_p->pc_count = 0;
  2316. if (hdh_p->count == 0)
  2317. *hdh_pp = hdh_p->next;
  2318. else
  2319. hdh_pp = &hdh_p->next;
  2320. }
  2321. }
  2322. /* Also discard relocs on undefined weak syms with non-default
  2323. visibility. */
  2324. if (eh->dyn_relocs != NULL
  2325. && eh->root.type == bfd_link_hash_undefweak)
  2326. {
  2327. if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
  2328. eh->dyn_relocs = NULL;
  2329. /* Make sure undefined weak symbols are output as a dynamic
  2330. symbol in PIEs. */
  2331. else if (eh->dynindx == -1
  2332. && !eh->forced_local)
  2333. {
  2334. if (! bfd_elf_link_record_dynamic_symbol (info, eh))
  2335. return false;
  2336. }
  2337. }
  2338. }
  2339. else
  2340. {
  2341. /* For the non-shared case, discard space for relocs against
  2342. symbols which turn out to need copy relocs or are not
  2343. dynamic. */
  2344. if (!eh->non_got_ref
  2345. && ((eh->def_dynamic
  2346. && !eh->def_regular)
  2347. || (htab->etab.dynamic_sections_created
  2348. && (eh->root.type == bfd_link_hash_undefweak
  2349. || eh->root.type == bfd_link_hash_undefined))))
  2350. {
  2351. /* Make sure this symbol is output as a dynamic symbol.
  2352. Undefined weak syms won't yet be marked as dynamic. */
  2353. if (eh->dynindx == -1
  2354. && !eh->forced_local)
  2355. {
  2356. if (! bfd_elf_link_record_dynamic_symbol (info, eh))
  2357. return false;
  2358. }
  2359. /* If that succeeded, we know we'll be keeping all the
  2360. relocs. */
  2361. if (eh->dynindx != -1)
  2362. goto keep;
  2363. }
  2364. eh->dyn_relocs = NULL;
  2365. return true;
  2366. keep: ;
  2367. }
  2368. /* Finally, allocate space. */
  2369. for (hdh_p = eh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->next)
  2370. {
  2371. asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
  2372. sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
  2373. }
  2374. return true;
  2375. }
  2376. /* Set the sizes of the dynamic sections. */
  2377. static bool
  2378. elf_metag_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  2379. struct bfd_link_info *info)
  2380. {
  2381. struct elf_metag_link_hash_table *htab;
  2382. bfd *dynobj;
  2383. bfd *ibfd;
  2384. asection *s;
  2385. bool relocs;
  2386. htab = metag_link_hash_table (info);
  2387. dynobj = htab->etab.dynobj;
  2388. if (dynobj == NULL)
  2389. abort ();
  2390. if (htab->etab.dynamic_sections_created)
  2391. {
  2392. /* Set the contents of the .interp section to the interpreter. */
  2393. if (bfd_link_executable (info) && !info->nointerp)
  2394. {
  2395. s = bfd_get_linker_section (dynobj, ".interp");
  2396. if (s == NULL)
  2397. abort ();
  2398. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  2399. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  2400. }
  2401. }
  2402. /* Set up .got offsets for local syms, and space for local dynamic
  2403. relocs. */
  2404. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  2405. {
  2406. bfd_signed_vma *local_got;
  2407. bfd_signed_vma *end_local_got;
  2408. bfd_size_type locsymcount;
  2409. Elf_Internal_Shdr *symtab_hdr;
  2410. asection *srel;
  2411. char *local_tls_type;
  2412. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  2413. continue;
  2414. for (s = ibfd->sections; s != NULL; s = s->next)
  2415. {
  2416. struct elf_dyn_relocs *hdh_p;
  2417. for (hdh_p = ((struct elf_dyn_relocs *)
  2418. elf_section_data (s)->local_dynrel);
  2419. hdh_p != NULL;
  2420. hdh_p = hdh_p->next)
  2421. {
  2422. if (!bfd_is_abs_section (hdh_p->sec)
  2423. && bfd_is_abs_section (hdh_p->sec->output_section))
  2424. {
  2425. /* Input section has been discarded, either because
  2426. it is a copy of a linkonce section or due to
  2427. linker script /DISCARD/, so we'll be discarding
  2428. the relocs too. */
  2429. }
  2430. else if (hdh_p->count != 0)
  2431. {
  2432. srel = elf_section_data (hdh_p->sec)->sreloc;
  2433. srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
  2434. if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
  2435. info->flags |= DF_TEXTREL;
  2436. }
  2437. }
  2438. }
  2439. local_got = elf_local_got_refcounts (ibfd);
  2440. if (!local_got)
  2441. continue;
  2442. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  2443. locsymcount = symtab_hdr->sh_info;
  2444. end_local_got = local_got + locsymcount;
  2445. local_tls_type = metag_elf_local_got_tls_type (ibfd);
  2446. s = htab->etab.sgot;
  2447. srel = htab->etab.srelgot;
  2448. for (; local_got < end_local_got; ++local_got)
  2449. {
  2450. if (*local_got > 0)
  2451. {
  2452. *local_got = s->size;
  2453. s->size += GOT_ENTRY_SIZE;
  2454. /* R_METAG_TLS_GD relocs need 2 consecutive GOT entries. */
  2455. if (*local_tls_type == GOT_TLS_GD)
  2456. s->size += 4;
  2457. if (bfd_link_pic (info))
  2458. srel->size += sizeof (Elf32_External_Rela);
  2459. }
  2460. else
  2461. *local_got = (bfd_vma) -1;
  2462. ++local_tls_type;
  2463. }
  2464. }
  2465. if (htab->tls_ldm_got.refcount > 0)
  2466. {
  2467. /* Allocate 2 got entries and 1 dynamic reloc for R_METAG_TLS_LDM
  2468. reloc. */
  2469. htab->tls_ldm_got.offset = htab->etab.sgot->size;
  2470. htab->etab.sgot->size += 8;
  2471. htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
  2472. }
  2473. else
  2474. htab->tls_ldm_got.offset = -1;
  2475. /* Allocate global sym .plt and .got entries, and space for global
  2476. sym dynamic relocs. */
  2477. elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
  2478. /* We now have determined the sizes of the various dynamic sections.
  2479. Allocate memory for them. */
  2480. relocs = false;
  2481. for (s = dynobj->sections; s != NULL; s = s->next)
  2482. {
  2483. bool reloc_section = false;
  2484. if ((s->flags & SEC_LINKER_CREATED) == 0)
  2485. continue;
  2486. if (s == htab->etab.splt
  2487. || s == htab->etab.sgot
  2488. || s == htab->etab.sgotplt
  2489. || s == htab->etab.sdynbss
  2490. || s == htab->etab.sdynrelro)
  2491. {
  2492. /* Strip this section if we don't need it; see the
  2493. comment below. */
  2494. }
  2495. else if (startswith (bfd_section_name (s), ".rela"))
  2496. {
  2497. if (s->size != 0 && s != htab->etab.srelplt)
  2498. relocs = true;
  2499. /* We use the reloc_count field as a counter if we need
  2500. to copy relocs into the output file. */
  2501. s->reloc_count = 0;
  2502. reloc_section = true;
  2503. }
  2504. else
  2505. {
  2506. /* It's not one of our sections, so don't allocate space. */
  2507. continue;
  2508. }
  2509. if (s->size == 0)
  2510. {
  2511. /* If we don't need this section, strip it from the
  2512. output file. This is mostly to handle .rela.bss and
  2513. .rela.plt. We must create both sections in
  2514. create_dynamic_sections, because they must be created
  2515. before the linker maps input sections to output
  2516. sections. The linker does that before
  2517. adjust_dynamic_symbol is called, and it is that
  2518. function which decides whether anything needs to go
  2519. into these sections. */
  2520. s->flags |= SEC_EXCLUDE;
  2521. continue;
  2522. }
  2523. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  2524. continue;
  2525. /* Allocate memory for the section contents. */
  2526. s->contents = bfd_zalloc (dynobj, s->size);
  2527. if (s->contents == NULL)
  2528. return false;
  2529. else if (reloc_section)
  2530. {
  2531. unsigned char *contents = s->contents;
  2532. Elf32_External_Rela reloc;
  2533. /* Fill the reloc section with a R_METAG_NONE type reloc. */
  2534. memset(&reloc, 0, sizeof(Elf32_External_Rela));
  2535. reloc.r_info[0] = R_METAG_NONE;
  2536. for (; contents < (s->contents + s->size);
  2537. contents += sizeof(Elf32_External_Rela))
  2538. {
  2539. memcpy(contents, &reloc, sizeof(Elf32_External_Rela));
  2540. }
  2541. }
  2542. }
  2543. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
  2544. }
  2545. /* Finish up dynamic symbol handling. We set the contents of various
  2546. dynamic sections here. */
  2547. static bool
  2548. elf_metag_finish_dynamic_symbol (bfd *output_bfd,
  2549. struct bfd_link_info *info,
  2550. struct elf_link_hash_entry *eh,
  2551. Elf_Internal_Sym *sym)
  2552. {
  2553. struct elf_metag_link_hash_table *htab;
  2554. Elf_Internal_Rela rel;
  2555. bfd_byte *loc;
  2556. htab = metag_link_hash_table (info);
  2557. if (eh->plt.offset != (bfd_vma) -1)
  2558. {
  2559. asection *splt;
  2560. asection *sgot;
  2561. asection *srela;
  2562. bfd_vma plt_index;
  2563. bfd_vma got_offset;
  2564. bfd_vma got_entry;
  2565. if (eh->plt.offset & 1)
  2566. abort ();
  2567. BFD_ASSERT (eh->dynindx != -1);
  2568. splt = htab->etab.splt;
  2569. sgot = htab->etab.sgotplt;
  2570. srela = htab->etab.srelplt;
  2571. BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
  2572. /* Get the index in the procedure linkage table which
  2573. corresponds to this symbol. This is the index of this symbol
  2574. in all the symbols for which we are making plt entries. The
  2575. first entry in the procedure linkage table is reserved. */
  2576. plt_index = eh->plt.offset / PLT_ENTRY_SIZE - 1;
  2577. /* Get the offset into the .got.plt table of the entry that
  2578. corresponds to this function. */
  2579. got_offset = plt_index * GOT_ENTRY_SIZE;
  2580. BFD_ASSERT (got_offset < (1 << 16));
  2581. got_entry = sgot->output_section->vma
  2582. + sgot->output_offset
  2583. + got_offset;
  2584. BFD_ASSERT (plt_index < (1 << 16));
  2585. /* Fill in the entry in the procedure linkage table. */
  2586. if (! bfd_link_pic (info))
  2587. {
  2588. bfd_put_32 (output_bfd,
  2589. (plt_entry[0]
  2590. | (((got_entry >> 16) & 0xffff) << 3)),
  2591. splt->contents + eh->plt.offset);
  2592. bfd_put_32 (output_bfd,
  2593. (plt_entry[1]
  2594. | ((got_entry & 0xffff) << 3)),
  2595. splt->contents + eh->plt.offset + 4);
  2596. bfd_put_32 (output_bfd, plt_entry[2],
  2597. splt->contents + eh->plt.offset + 8);
  2598. bfd_put_32 (output_bfd,
  2599. (plt_entry[3] | (plt_index << 3)),
  2600. splt->contents + eh->plt.offset + 12);
  2601. bfd_put_32 (output_bfd,
  2602. (plt_entry[4]
  2603. | ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
  2604. splt->contents + eh->plt.offset + 16);
  2605. }
  2606. else
  2607. {
  2608. bfd_vma addr = got_entry - (splt->output_section->vma +
  2609. splt->output_offset + eh->plt.offset);
  2610. bfd_put_32 (output_bfd,
  2611. plt_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
  2612. splt->contents + eh->plt.offset);
  2613. bfd_put_32 (output_bfd,
  2614. plt_pic_entry[1] | ((addr & 0xffff) << 3),
  2615. splt->contents + eh->plt.offset + 4);
  2616. bfd_put_32 (output_bfd, plt_pic_entry[2],
  2617. splt->contents + eh->plt.offset + 8);
  2618. bfd_put_32 (output_bfd,
  2619. (plt_pic_entry[3] | (plt_index << 3)),
  2620. splt->contents + eh->plt.offset + 12);
  2621. bfd_put_32 (output_bfd,
  2622. (plt_pic_entry[4]
  2623. + ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
  2624. splt->contents + eh->plt.offset + 16);
  2625. }
  2626. /* Fill in the entry in the global offset table. */
  2627. bfd_put_32 (output_bfd,
  2628. (splt->output_section->vma
  2629. + splt->output_offset
  2630. + eh->plt.offset
  2631. + 12), /* offset within PLT entry */
  2632. sgot->contents + got_offset);
  2633. /* Fill in the entry in the .rela.plt section. */
  2634. rel.r_offset = (sgot->output_section->vma
  2635. + sgot->output_offset
  2636. + got_offset);
  2637. rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_JMP_SLOT);
  2638. rel.r_addend = 0;
  2639. loc = htab->etab.srelplt->contents;
  2640. loc += plt_index * sizeof(Elf32_External_Rela);
  2641. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  2642. if (!eh->def_regular)
  2643. {
  2644. /* Mark the symbol as undefined, rather than as defined in
  2645. the .plt section. Leave the value alone. */
  2646. sym->st_shndx = SHN_UNDEF;
  2647. }
  2648. }
  2649. if (eh->got.offset != (bfd_vma) -1
  2650. && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0
  2651. && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0)
  2652. {
  2653. /* This symbol has an entry in the global offset table. Set it
  2654. up. */
  2655. rel.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
  2656. + htab->etab.sgot->output_offset
  2657. + htab->etab.sgot->output_section->vma);
  2658. /* If this is a -Bsymbolic link and the symbol is defined
  2659. locally or was forced to be local because of a version file,
  2660. we just want to emit a RELATIVE reloc. The entry in the
  2661. global offset table will already have been initialized in the
  2662. relocate_section function. */
  2663. if (bfd_link_pic (info)
  2664. && (info->symbolic || eh->dynindx == -1)
  2665. && eh->def_regular)
  2666. {
  2667. rel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
  2668. rel.r_addend = (eh->root.u.def.value
  2669. + eh->root.u.def.section->output_offset
  2670. + eh->root.u.def.section->output_section->vma);
  2671. }
  2672. else
  2673. {
  2674. if ((eh->got.offset & 1) != 0)
  2675. abort ();
  2676. bfd_put_32 (output_bfd, 0, htab->etab.sgot->contents + eh->got.offset);
  2677. rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_GLOB_DAT);
  2678. rel.r_addend = 0;
  2679. }
  2680. loc = htab->etab.srelgot->contents;
  2681. loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  2682. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  2683. }
  2684. if (eh->needs_copy)
  2685. {
  2686. asection *s;
  2687. /* This symbol needs a copy reloc. Set it up. */
  2688. if (! (eh->dynindx != -1
  2689. && (eh->root.type == bfd_link_hash_defined
  2690. || eh->root.type == bfd_link_hash_defweak)))
  2691. abort ();
  2692. rel.r_offset = (eh->root.u.def.value
  2693. + eh->root.u.def.section->output_offset
  2694. + eh->root.u.def.section->output_section->vma);
  2695. rel.r_addend = 0;
  2696. rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_COPY);
  2697. if (eh->root.u.def.section == htab->etab.sdynrelro)
  2698. s = htab->etab.sreldynrelro;
  2699. else
  2700. s = htab->etab.srelbss;
  2701. loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
  2702. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  2703. }
  2704. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  2705. if (eh->root.root.string[0] == '_'
  2706. && (strcmp (eh->root.root.string, "_DYNAMIC") == 0
  2707. || eh == htab->etab.hgot))
  2708. {
  2709. sym->st_shndx = SHN_ABS;
  2710. }
  2711. return true;
  2712. }
  2713. /* Set the Meta ELF ABI version. */
  2714. static bool
  2715. elf_metag_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
  2716. {
  2717. Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
  2718. if (!_bfd_elf_init_file_header (abfd, link_info))
  2719. return false;
  2720. i_ehdrp = elf_elfheader (abfd);
  2721. i_ehdrp->e_ident[EI_ABIVERSION] = METAG_ELF_ABI_VERSION;
  2722. return true;
  2723. }
  2724. /* Used to decide how to sort relocs in an optimal manner for the
  2725. dynamic linker, before writing them out. */
  2726. static enum elf_reloc_type_class
  2727. elf_metag_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2728. const asection *rel_sec ATTRIBUTE_UNUSED,
  2729. const Elf_Internal_Rela *rela)
  2730. {
  2731. switch ((int) ELF32_R_TYPE (rela->r_info))
  2732. {
  2733. case R_METAG_RELATIVE:
  2734. return reloc_class_relative;
  2735. case R_METAG_JMP_SLOT:
  2736. return reloc_class_plt;
  2737. case R_METAG_COPY:
  2738. return reloc_class_copy;
  2739. default:
  2740. return reloc_class_normal;
  2741. }
  2742. }
  2743. /* Finish up the dynamic sections. */
  2744. static bool
  2745. elf_metag_finish_dynamic_sections (bfd *output_bfd,
  2746. struct bfd_link_info *info)
  2747. {
  2748. bfd *dynobj;
  2749. struct elf_metag_link_hash_table *htab;
  2750. asection *sdyn;
  2751. htab = metag_link_hash_table (info);
  2752. dynobj = htab->etab.dynobj;
  2753. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2754. if (htab->etab.dynamic_sections_created)
  2755. {
  2756. asection *splt;
  2757. Elf32_External_Dyn *dyncon, *dynconend;
  2758. if (sdyn == NULL)
  2759. abort ();
  2760. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  2761. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  2762. for (; dyncon < dynconend; dyncon++)
  2763. {
  2764. Elf_Internal_Dyn dyn;
  2765. asection *s;
  2766. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  2767. switch (dyn.d_tag)
  2768. {
  2769. default:
  2770. continue;
  2771. case DT_PLTGOT:
  2772. s = htab->etab.sgot;
  2773. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2774. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2775. break;
  2776. case DT_JMPREL:
  2777. s = htab->etab.srelplt;
  2778. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2779. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2780. break;
  2781. case DT_PLTRELSZ:
  2782. s = htab->etab.srelplt;
  2783. dyn.d_un.d_val = s->size;
  2784. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2785. break;
  2786. }
  2787. }
  2788. /* Fill in the first entry in the procedure linkage table. */
  2789. splt = htab->etab.splt;
  2790. if (splt && splt->size > 0)
  2791. {
  2792. unsigned long addr;
  2793. /* addr = .got + 4 */
  2794. addr = (htab->etab.sgot->output_section->vma
  2795. + htab->etab.sgot->output_offset + 4);
  2796. if (bfd_link_pic (info))
  2797. {
  2798. addr -= splt->output_section->vma + splt->output_offset;
  2799. bfd_put_32 (output_bfd,
  2800. plt0_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
  2801. splt->contents);
  2802. bfd_put_32 (output_bfd,
  2803. plt0_pic_entry[1] | ((addr & 0xffff) << 3),
  2804. splt->contents + 4);
  2805. bfd_put_32 (output_bfd, plt0_pic_entry[2], splt->contents + 8);
  2806. bfd_put_32 (output_bfd, plt0_pic_entry[3], splt->contents + 12);
  2807. bfd_put_32 (output_bfd, plt0_pic_entry[4], splt->contents + 16);
  2808. }
  2809. else
  2810. {
  2811. bfd_put_32 (output_bfd,
  2812. plt0_entry[0] | (((addr >> 16) & 0xffff) << 3),
  2813. splt->contents);
  2814. bfd_put_32 (output_bfd,
  2815. plt0_entry[1] | ((addr & 0xffff) << 3),
  2816. splt->contents + 4);
  2817. bfd_put_32 (output_bfd, plt0_entry[2], splt->contents + 8);
  2818. bfd_put_32 (output_bfd, plt0_entry[3], splt->contents + 12);
  2819. bfd_put_32 (output_bfd, plt0_entry[4], splt->contents + 16);
  2820. }
  2821. elf_section_data (splt->output_section)->this_hdr.sh_entsize =
  2822. PLT_ENTRY_SIZE;
  2823. }
  2824. }
  2825. if (htab->etab.sgot != NULL && htab->etab.sgot->size != 0)
  2826. {
  2827. /* Fill in the first entry in the global offset table.
  2828. We use it to point to our dynamic section, if we have one. */
  2829. bfd_put_32 (output_bfd,
  2830. sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
  2831. htab->etab.sgot->contents);
  2832. /* The second entry is reserved for use by the dynamic linker. */
  2833. memset (htab->etab.sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
  2834. /* Set .got entry size. */
  2835. elf_section_data (htab->etab.sgot->output_section)
  2836. ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
  2837. }
  2838. return true;
  2839. }
  2840. /* Return the section that should be marked against GC for a given
  2841. relocation. */
  2842. static asection *
  2843. elf_metag_gc_mark_hook (asection *sec,
  2844. struct bfd_link_info *info,
  2845. Elf_Internal_Rela *rela,
  2846. struct elf_link_hash_entry *hh,
  2847. Elf_Internal_Sym *sym)
  2848. {
  2849. if (hh != NULL)
  2850. switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
  2851. {
  2852. case R_METAG_GNU_VTINHERIT:
  2853. case R_METAG_GNU_VTENTRY:
  2854. return NULL;
  2855. }
  2856. return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
  2857. }
  2858. /* Determine the type of stub needed, if any, for a call. */
  2859. static enum elf_metag_stub_type
  2860. metag_type_of_stub (asection *input_sec,
  2861. const Elf_Internal_Rela *rel,
  2862. struct elf_metag_link_hash_entry *hh,
  2863. bfd_vma destination,
  2864. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2865. {
  2866. bfd_vma location;
  2867. bfd_vma branch_offset;
  2868. bfd_vma max_branch_offset;
  2869. if (hh != NULL &&
  2870. !(hh->eh.root.type == bfd_link_hash_defined
  2871. || hh->eh.root.type == bfd_link_hash_defweak))
  2872. return metag_stub_none;
  2873. /* Determine where the call point is. */
  2874. location = (input_sec->output_offset
  2875. + input_sec->output_section->vma
  2876. + rel->r_offset);
  2877. branch_offset = destination - location;
  2878. /* Determine if a long branch stub is needed. Meta branch offsets
  2879. are signed 19 bits 4 byte aligned. */
  2880. max_branch_offset = (1 << (BRANCH_BITS-1)) << 2;
  2881. if (branch_offset + max_branch_offset >= 2*max_branch_offset)
  2882. {
  2883. if (bfd_link_pic (info))
  2884. return metag_stub_long_branch_shared;
  2885. else
  2886. return metag_stub_long_branch;
  2887. }
  2888. return metag_stub_none;
  2889. }
  2890. #define MOVT_A0_3 0x82180005
  2891. #define JUMP_A0_3 0xac180003
  2892. #define MOVT_A1LBP 0x83080005
  2893. #define ADD_A1LBP 0x83080000
  2894. #define ADDT_A0_3_CPC 0x82980001
  2895. #define ADD_A0_3_A0_3 0x82180000
  2896. #define MOV_PC_A0_3 0xa3180ca0
  2897. static bool
  2898. metag_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
  2899. {
  2900. struct elf_metag_stub_hash_entry *hsh;
  2901. asection *stub_sec;
  2902. bfd *stub_bfd;
  2903. bfd_byte *loc;
  2904. bfd_vma sym_value;
  2905. int size;
  2906. struct bfd_link_info *info;
  2907. /* Massage our args to the form they really have. */
  2908. hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
  2909. info = (struct bfd_link_info *) in_arg;
  2910. /* Fail if the target section could not be assigned to an output
  2911. section. The user should fix his linker script. */
  2912. if (hsh->target_section->output_section == NULL
  2913. && info->non_contiguous_regions)
  2914. info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
  2915. "Retry without --enable-non-contiguous-regions.\n"),
  2916. hsh->target_section);
  2917. stub_sec = hsh->stub_sec;
  2918. /* Make a note of the offset within the stubs for this entry. */
  2919. hsh->stub_offset = stub_sec->size;
  2920. loc = stub_sec->contents + hsh->stub_offset;
  2921. stub_bfd = stub_sec->owner;
  2922. switch (hsh->stub_type)
  2923. {
  2924. case metag_stub_long_branch_shared:
  2925. /* A PIC long branch stub is an ADDT and an ADD instruction used to
  2926. calculate the jump target using A0.3 as a temporary. Then a MOV
  2927. to PC carries out the jump. */
  2928. sym_value = (hsh->target_value
  2929. + hsh->target_section->output_offset
  2930. + hsh->target_section->output_section->vma
  2931. + hsh->addend);
  2932. sym_value -= (hsh->stub_offset
  2933. + stub_sec->output_offset
  2934. + stub_sec->output_section->vma);
  2935. bfd_put_32 (stub_bfd, ADDT_A0_3_CPC | (((sym_value >> 16) & 0xffff) << 3),
  2936. loc);
  2937. bfd_put_32 (stub_bfd, ADD_A0_3_A0_3 | ((sym_value & 0xffff) << 3),
  2938. loc + 4);
  2939. bfd_put_32 (stub_bfd, MOV_PC_A0_3, loc + 8);
  2940. size = 12;
  2941. break;
  2942. case metag_stub_long_branch:
  2943. /* A standard long branch stub is a MOVT instruction followed by a
  2944. JUMP instruction using the A0.3 register as a temporary. This is
  2945. the same method used by the LDLK linker (patch.c). */
  2946. sym_value = (hsh->target_value
  2947. + hsh->target_section->output_offset
  2948. + hsh->target_section->output_section->vma
  2949. + hsh->addend);
  2950. bfd_put_32 (stub_bfd, MOVT_A0_3 | (((sym_value >> 16) & 0xffff) << 3),
  2951. loc);
  2952. bfd_put_32 (stub_bfd, JUMP_A0_3 | ((sym_value & 0xffff) << 3), loc + 4);
  2953. size = 8;
  2954. break;
  2955. default:
  2956. BFD_FAIL ();
  2957. return false;
  2958. }
  2959. stub_sec->size += size;
  2960. return true;
  2961. }
  2962. /* As above, but don't actually build the stub. Just bump offset so
  2963. we know stub section sizes. */
  2964. static bool
  2965. metag_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
  2966. {
  2967. struct elf_metag_stub_hash_entry *hsh;
  2968. int size = 0;
  2969. /* Massage our args to the form they really have. */
  2970. hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
  2971. if (hsh->stub_type == metag_stub_long_branch)
  2972. size = 8;
  2973. else if (hsh->stub_type == metag_stub_long_branch_shared)
  2974. size = 12;
  2975. hsh->stub_sec->size += size;
  2976. return true;
  2977. }
  2978. /* Set up various things so that we can make a list of input sections
  2979. for each output section included in the link. Returns -1 on error,
  2980. 0 when no stubs will be needed, and 1 on success. */
  2981. int
  2982. elf_metag_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
  2983. {
  2984. bfd *input_bfd;
  2985. unsigned int bfd_count;
  2986. unsigned int top_id, top_index;
  2987. asection *section;
  2988. asection **input_list, **list;
  2989. size_t amt;
  2990. struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
  2991. /* Count the number of input BFDs and find the top input section id. */
  2992. for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
  2993. input_bfd != NULL;
  2994. input_bfd = input_bfd->link.next)
  2995. {
  2996. bfd_count += 1;
  2997. for (section = input_bfd->sections;
  2998. section != NULL;
  2999. section = section->next)
  3000. {
  3001. if (top_id < section->id)
  3002. top_id = section->id;
  3003. }
  3004. }
  3005. htab->bfd_count = bfd_count;
  3006. amt = sizeof (struct map_stub) * (top_id + 1);
  3007. htab->stub_group = bfd_zmalloc (amt);
  3008. if (htab->stub_group == NULL)
  3009. return -1;
  3010. /* We can't use output_bfd->section_count here to find the top output
  3011. section index as some sections may have been removed, and
  3012. strip_excluded_output_sections doesn't renumber the indices. */
  3013. for (section = output_bfd->sections, top_index = 0;
  3014. section != NULL;
  3015. section = section->next)
  3016. {
  3017. if (top_index < section->index)
  3018. top_index = section->index;
  3019. }
  3020. htab->top_index = top_index;
  3021. amt = sizeof (asection *) * (top_index + 1);
  3022. input_list = bfd_malloc (amt);
  3023. htab->input_list = input_list;
  3024. if (input_list == NULL)
  3025. return -1;
  3026. /* For sections we aren't interested in, mark their entries with a
  3027. value we can check later. */
  3028. list = input_list + top_index;
  3029. do
  3030. *list = bfd_abs_section_ptr;
  3031. while (list-- != input_list);
  3032. for (section = output_bfd->sections;
  3033. section != NULL;
  3034. section = section->next)
  3035. {
  3036. /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
  3037. * have PC relative relocs in them but no code flag set. */
  3038. if (((section->flags & SEC_CODE) != 0) ||
  3039. strcmp(".ctors", section->name) ||
  3040. strcmp(".dtors", section->name))
  3041. input_list[section->index] = NULL;
  3042. }
  3043. return 1;
  3044. }
  3045. /* The linker repeatedly calls this function for each input section,
  3046. in the order that input sections are linked into output sections.
  3047. Build lists of input sections to determine groupings between which
  3048. we may insert linker stubs. */
  3049. void
  3050. elf_metag_next_input_section (struct bfd_link_info *info, asection *isec)
  3051. {
  3052. struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
  3053. if (isec->output_section->index <= htab->top_index)
  3054. {
  3055. asection **list = htab->input_list + isec->output_section->index;
  3056. if (*list != bfd_abs_section_ptr)
  3057. {
  3058. /* Steal the link_sec pointer for our list. */
  3059. #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
  3060. /* This happens to make the list in reverse order,
  3061. which is what we want. */
  3062. PREV_SEC (isec) = *list;
  3063. *list = isec;
  3064. }
  3065. }
  3066. }
  3067. /* See whether we can group stub sections together. Grouping stub
  3068. sections may result in fewer stubs. More importantly, we need to
  3069. put all .init* and .fini* stubs at the beginning of the .init or
  3070. .fini output sections respectively, because glibc splits the
  3071. _init and _fini functions into multiple parts. Putting a stub in
  3072. the middle of a function is not a good idea. */
  3073. static void
  3074. group_sections (struct elf_metag_link_hash_table *htab,
  3075. bfd_size_type stub_group_size,
  3076. bool stubs_always_before_branch)
  3077. {
  3078. asection **list = htab->input_list + htab->top_index;
  3079. do
  3080. {
  3081. asection *tail = *list;
  3082. if (tail == bfd_abs_section_ptr)
  3083. continue;
  3084. while (tail != NULL)
  3085. {
  3086. asection *curr;
  3087. asection *prev;
  3088. bfd_size_type total;
  3089. bool big_sec;
  3090. curr = tail;
  3091. total = tail->size;
  3092. big_sec = total >= stub_group_size;
  3093. while ((prev = PREV_SEC (curr)) != NULL
  3094. && ((total += curr->output_offset - prev->output_offset)
  3095. < stub_group_size))
  3096. curr = prev;
  3097. /* OK, the size from the start of CURR to the end is less
  3098. than stub_group_size bytes and thus can be handled by one stub
  3099. section. (or the tail section is itself larger than
  3100. stub_group_size bytes, in which case we may be toast.)
  3101. We should really be keeping track of the total size of
  3102. stubs added here, as stubs contribute to the final output
  3103. section size. */
  3104. do
  3105. {
  3106. prev = PREV_SEC (tail);
  3107. /* Set up this stub group. */
  3108. htab->stub_group[tail->id].link_sec = curr;
  3109. }
  3110. while (tail != curr && (tail = prev) != NULL);
  3111. /* But wait, there's more! Input sections up to stub_group_size
  3112. bytes before the stub section can be handled by it too.
  3113. Don't do this if we have a really large section after the
  3114. stubs, as adding more stubs increases the chance that
  3115. branches may not reach into the stub section. */
  3116. if (!stubs_always_before_branch && !big_sec)
  3117. {
  3118. total = 0;
  3119. while (prev != NULL
  3120. && ((total += tail->output_offset - prev->output_offset)
  3121. < stub_group_size))
  3122. {
  3123. tail = prev;
  3124. prev = PREV_SEC (tail);
  3125. htab->stub_group[tail->id].link_sec = curr;
  3126. }
  3127. }
  3128. tail = prev;
  3129. }
  3130. }
  3131. while (list-- != htab->input_list);
  3132. free (htab->input_list);
  3133. #undef PREV_SEC
  3134. }
  3135. /* Read in all local syms for all input bfds.
  3136. Returns -1 on error, 0 otherwise. */
  3137. static int
  3138. get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
  3139. struct bfd_link_info *info)
  3140. {
  3141. unsigned int bfd_indx;
  3142. Elf_Internal_Sym *local_syms, **all_local_syms;
  3143. int stub_changed = 0;
  3144. struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
  3145. /* We want to read in symbol extension records only once. To do this
  3146. we need to read in the local symbols in parallel and save them for
  3147. later use; so hold pointers to the local symbols in an array. */
  3148. size_t amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
  3149. all_local_syms = bfd_zmalloc (amt);
  3150. htab->all_local_syms = all_local_syms;
  3151. if (all_local_syms == NULL)
  3152. return -1;
  3153. /* Walk over all the input BFDs, swapping in local symbols. */
  3154. for (bfd_indx = 0;
  3155. input_bfd != NULL;
  3156. input_bfd = input_bfd->link.next, bfd_indx++)
  3157. {
  3158. Elf_Internal_Shdr *symtab_hdr;
  3159. /* We'll need the symbol table in a second. */
  3160. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  3161. if (symtab_hdr->sh_info == 0)
  3162. continue;
  3163. /* We need an array of the local symbols attached to the input bfd. */
  3164. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  3165. if (local_syms == NULL)
  3166. {
  3167. local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  3168. symtab_hdr->sh_info, 0,
  3169. NULL, NULL, NULL);
  3170. /* Cache them for elf_link_input_bfd. */
  3171. symtab_hdr->contents = (unsigned char *) local_syms;
  3172. }
  3173. if (local_syms == NULL)
  3174. return -1;
  3175. all_local_syms[bfd_indx] = local_syms;
  3176. }
  3177. return stub_changed;
  3178. }
  3179. /* Determine and set the size of the stub section for a final link.
  3180. The basic idea here is to examine all the relocations looking for
  3181. PC-relative calls to a target that is unreachable with a "CALLR"
  3182. instruction. */
  3183. /* See elf32-hppa.c and elf64-ppc.c. */
  3184. bool
  3185. elf_metag_size_stubs(bfd *output_bfd, bfd *stub_bfd,
  3186. struct bfd_link_info *info,
  3187. bfd_signed_vma group_size,
  3188. asection * (*add_stub_section) (const char *, asection *),
  3189. void (*layout_sections_again) (void))
  3190. {
  3191. bfd_size_type stub_group_size;
  3192. bool stubs_always_before_branch;
  3193. bool stub_changed;
  3194. struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
  3195. /* Stash our params away. */
  3196. htab->stub_bfd = stub_bfd;
  3197. htab->add_stub_section = add_stub_section;
  3198. htab->layout_sections_again = layout_sections_again;
  3199. stubs_always_before_branch = group_size < 0;
  3200. if (group_size < 0)
  3201. stub_group_size = -group_size;
  3202. else
  3203. stub_group_size = group_size;
  3204. if (stub_group_size == 1)
  3205. {
  3206. /* Default values. */
  3207. /* FIXME: not sure what these values should be */
  3208. if (stubs_always_before_branch)
  3209. {
  3210. stub_group_size = (1 << BRANCH_BITS);
  3211. }
  3212. else
  3213. {
  3214. stub_group_size = (1 << BRANCH_BITS);
  3215. }
  3216. }
  3217. group_sections (htab, stub_group_size, stubs_always_before_branch);
  3218. switch (get_local_syms (output_bfd, info->input_bfds, info))
  3219. {
  3220. default:
  3221. if (htab->all_local_syms)
  3222. goto error_ret_free_local;
  3223. return false;
  3224. case 0:
  3225. stub_changed = false;
  3226. break;
  3227. case 1:
  3228. stub_changed = true;
  3229. break;
  3230. }
  3231. while (1)
  3232. {
  3233. bfd *input_bfd;
  3234. unsigned int bfd_indx;
  3235. asection *stub_sec;
  3236. for (input_bfd = info->input_bfds, bfd_indx = 0;
  3237. input_bfd != NULL;
  3238. input_bfd = input_bfd->link.next, bfd_indx++)
  3239. {
  3240. Elf_Internal_Shdr *symtab_hdr;
  3241. asection *section;
  3242. Elf_Internal_Sym *local_syms;
  3243. /* We'll need the symbol table in a second. */
  3244. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  3245. if (symtab_hdr->sh_info == 0)
  3246. continue;
  3247. local_syms = htab->all_local_syms[bfd_indx];
  3248. /* Walk over each section attached to the input bfd. */
  3249. for (section = input_bfd->sections;
  3250. section != NULL;
  3251. section = section->next)
  3252. {
  3253. Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  3254. /* If there aren't any relocs, then there's nothing more
  3255. to do. */
  3256. if ((section->flags & SEC_RELOC) == 0
  3257. || section->reloc_count == 0)
  3258. continue;
  3259. /* If this section is a link-once section that will be
  3260. discarded, then don't create any stubs. */
  3261. if (section->output_section == NULL
  3262. || section->output_section->owner != output_bfd)
  3263. continue;
  3264. /* Get the relocs. */
  3265. internal_relocs
  3266. = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
  3267. info->keep_memory);
  3268. if (internal_relocs == NULL)
  3269. goto error_ret_free_local;
  3270. /* Now examine each relocation. */
  3271. irela = internal_relocs;
  3272. irelaend = irela + section->reloc_count;
  3273. for (; irela < irelaend; irela++)
  3274. {
  3275. unsigned int r_type, r_indx;
  3276. enum elf_metag_stub_type stub_type;
  3277. struct elf_metag_stub_hash_entry *hsh;
  3278. asection *sym_sec;
  3279. bfd_vma sym_value;
  3280. bfd_vma destination;
  3281. struct elf_metag_link_hash_entry *hh;
  3282. char *stub_name;
  3283. const asection *id_sec;
  3284. r_type = ELF32_R_TYPE (irela->r_info);
  3285. r_indx = ELF32_R_SYM (irela->r_info);
  3286. if (r_type >= (unsigned int) R_METAG_MAX)
  3287. {
  3288. bfd_set_error (bfd_error_bad_value);
  3289. error_ret_free_internal:
  3290. if (elf_section_data (section)->relocs == NULL)
  3291. free (internal_relocs);
  3292. goto error_ret_free_local;
  3293. }
  3294. /* Only look for stubs on CALLR and B instructions. */
  3295. if (!(r_type == (unsigned int) R_METAG_RELBRANCH ||
  3296. r_type == (unsigned int) R_METAG_RELBRANCH_PLT))
  3297. continue;
  3298. /* Now determine the call target, its name, value,
  3299. section. */
  3300. sym_sec = NULL;
  3301. sym_value = 0;
  3302. destination = 0;
  3303. hh = NULL;
  3304. if (r_indx < symtab_hdr->sh_info)
  3305. {
  3306. /* It's a local symbol. */
  3307. Elf_Internal_Sym *sym;
  3308. Elf_Internal_Shdr *hdr;
  3309. unsigned int shndx;
  3310. sym = local_syms + r_indx;
  3311. if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  3312. sym_value = sym->st_value;
  3313. shndx = sym->st_shndx;
  3314. if (shndx < elf_numsections (input_bfd))
  3315. {
  3316. hdr = elf_elfsections (input_bfd)[shndx];
  3317. sym_sec = hdr->bfd_section;
  3318. destination = (sym_value + irela->r_addend
  3319. + sym_sec->output_offset
  3320. + sym_sec->output_section->vma);
  3321. }
  3322. }
  3323. else
  3324. {
  3325. /* It's an external symbol. */
  3326. int e_indx;
  3327. e_indx = r_indx - symtab_hdr->sh_info;
  3328. hh = ((struct elf_metag_link_hash_entry *)
  3329. elf_sym_hashes (input_bfd)[e_indx]);
  3330. while (hh->eh.root.type == bfd_link_hash_indirect
  3331. || hh->eh.root.type == bfd_link_hash_warning)
  3332. hh = ((struct elf_metag_link_hash_entry *)
  3333. hh->eh.root.u.i.link);
  3334. if (hh->eh.root.type == bfd_link_hash_defined
  3335. || hh->eh.root.type == bfd_link_hash_defweak)
  3336. {
  3337. sym_sec = hh->eh.root.u.def.section;
  3338. sym_value = hh->eh.root.u.def.value;
  3339. if (hh->eh.plt.offset != (bfd_vma) -1
  3340. && hh->eh.dynindx != -1
  3341. && r_type == (unsigned int) R_METAG_RELBRANCH_PLT)
  3342. {
  3343. sym_sec = htab->etab.splt;
  3344. sym_value = hh->eh.plt.offset;
  3345. }
  3346. if (sym_sec->output_section != NULL)
  3347. destination = (sym_value + irela->r_addend
  3348. + sym_sec->output_offset
  3349. + sym_sec->output_section->vma);
  3350. else
  3351. continue;
  3352. }
  3353. else if (hh->eh.root.type == bfd_link_hash_undefweak)
  3354. {
  3355. if (! bfd_link_pic (info))
  3356. continue;
  3357. }
  3358. else if (hh->eh.root.type == bfd_link_hash_undefined)
  3359. {
  3360. if (! (info->unresolved_syms_in_objects == RM_IGNORE
  3361. && (ELF_ST_VISIBILITY (hh->eh.other)
  3362. == STV_DEFAULT)))
  3363. continue;
  3364. }
  3365. else
  3366. {
  3367. bfd_set_error (bfd_error_bad_value);
  3368. goto error_ret_free_internal;
  3369. }
  3370. }
  3371. /* Determine what (if any) linker stub is needed. */
  3372. stub_type = metag_type_of_stub (section, irela, hh,
  3373. destination, info);
  3374. if (stub_type == metag_stub_none)
  3375. continue;
  3376. /* Support for grouping stub sections. */
  3377. id_sec = htab->stub_group[section->id].link_sec;
  3378. /* Get the name of this stub. */
  3379. stub_name = metag_stub_name (id_sec, sym_sec, hh, irela);
  3380. if (!stub_name)
  3381. goto error_ret_free_internal;
  3382. hsh = metag_stub_hash_lookup (&htab->bstab,
  3383. stub_name,
  3384. false, false);
  3385. if (hsh != NULL)
  3386. {
  3387. /* The proper stub has already been created. */
  3388. free (stub_name);
  3389. continue;
  3390. }
  3391. hsh = metag_add_stub (stub_name, section, htab);
  3392. if (hsh == NULL)
  3393. {
  3394. free (stub_name);
  3395. goto error_ret_free_internal;
  3396. }
  3397. hsh->target_value = sym_value;
  3398. hsh->target_section = sym_sec;
  3399. hsh->stub_type = stub_type;
  3400. hsh->hh = hh;
  3401. hsh->addend = irela->r_addend;
  3402. stub_changed = true;
  3403. }
  3404. /* We're done with the internal relocs, free them. */
  3405. if (elf_section_data (section)->relocs == NULL)
  3406. free (internal_relocs);
  3407. }
  3408. }
  3409. if (!stub_changed)
  3410. break;
  3411. /* OK, we've added some stubs. Find out the new size of the
  3412. stub sections. */
  3413. for (stub_sec = htab->stub_bfd->sections;
  3414. stub_sec != NULL;
  3415. stub_sec = stub_sec->next)
  3416. stub_sec->size = 0;
  3417. bfd_hash_traverse (&htab->bstab, metag_size_one_stub, htab);
  3418. /* Ask the linker to do its stuff. */
  3419. (*htab->layout_sections_again) ();
  3420. stub_changed = false;
  3421. }
  3422. free (htab->all_local_syms);
  3423. return true;
  3424. error_ret_free_local:
  3425. free (htab->all_local_syms);
  3426. return false;
  3427. }
  3428. /* Build all the stubs associated with the current output file. The
  3429. stubs are kept in a hash table attached to the main linker hash
  3430. table. This function is called via metagelf_finish in the linker. */
  3431. bool
  3432. elf_metag_build_stubs (struct bfd_link_info *info)
  3433. {
  3434. asection *stub_sec;
  3435. struct bfd_hash_table *table;
  3436. struct elf_metag_link_hash_table *htab;
  3437. htab = metag_link_hash_table (info);
  3438. for (stub_sec = htab->stub_bfd->sections;
  3439. stub_sec != NULL;
  3440. stub_sec = stub_sec->next)
  3441. {
  3442. bfd_size_type size;
  3443. /* Allocate memory to hold the linker stubs. */
  3444. size = stub_sec->size;
  3445. stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
  3446. if (stub_sec->contents == NULL && size != 0)
  3447. return false;
  3448. stub_sec->size = 0;
  3449. }
  3450. /* Build the stubs as directed by the stub hash table. */
  3451. table = &htab->bstab;
  3452. bfd_hash_traverse (table, metag_build_one_stub, info);
  3453. return true;
  3454. }
  3455. /* Return TRUE if SYM represents a local label symbol. */
  3456. static bool
  3457. elf_metag_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
  3458. {
  3459. if (name[0] == '$' && name[1] == 'L')
  3460. return 1;
  3461. return _bfd_elf_is_local_label_name (abfd, name);
  3462. }
  3463. /* Return address for Ith PLT stub in section PLT, for relocation REL
  3464. or (bfd_vma) -1 if it should not be included. */
  3465. static bfd_vma
  3466. elf_metag_plt_sym_val (bfd_vma i, const asection *plt,
  3467. const arelent *rel ATTRIBUTE_UNUSED)
  3468. {
  3469. return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
  3470. }
  3471. #define ELF_ARCH bfd_arch_metag
  3472. #define ELF_TARGET_ID METAG_ELF_DATA
  3473. #define ELF_MACHINE_CODE EM_METAG
  3474. #define ELF_MAXPAGESIZE 0x4000
  3475. #define ELF_COMMONPAGESIZE 0x1000
  3476. #define TARGET_LITTLE_SYM metag_elf32_vec
  3477. #define TARGET_LITTLE_NAME "elf32-metag"
  3478. #define elf_symbol_leading_char '_'
  3479. #define elf_info_to_howto_rel NULL
  3480. #define elf_info_to_howto metag_info_to_howto_rela
  3481. #define bfd_elf32_bfd_is_local_label_name elf_metag_is_local_label_name
  3482. #define bfd_elf32_bfd_link_hash_table_create \
  3483. elf_metag_link_hash_table_create
  3484. #define elf_backend_relocate_section elf_metag_relocate_section
  3485. #define elf_backend_gc_mark_hook elf_metag_gc_mark_hook
  3486. #define elf_backend_check_relocs elf_metag_check_relocs
  3487. #define elf_backend_create_dynamic_sections elf_metag_create_dynamic_sections
  3488. #define elf_backend_adjust_dynamic_symbol elf_metag_adjust_dynamic_symbol
  3489. #define elf_backend_finish_dynamic_symbol elf_metag_finish_dynamic_symbol
  3490. #define elf_backend_finish_dynamic_sections elf_metag_finish_dynamic_sections
  3491. #define elf_backend_size_dynamic_sections elf_metag_size_dynamic_sections
  3492. #define elf_backend_omit_section_dynsym \
  3493. _bfd_elf_omit_section_dynsym_all
  3494. #define elf_backend_init_file_header elf_metag_init_file_header
  3495. #define elf_backend_reloc_type_class elf_metag_reloc_type_class
  3496. #define elf_backend_copy_indirect_symbol elf_metag_copy_indirect_symbol
  3497. #define elf_backend_plt_sym_val elf_metag_plt_sym_val
  3498. #define elf_backend_can_gc_sections 1
  3499. #define elf_backend_can_refcount 1
  3500. #define elf_backend_rela_normal 1
  3501. #define elf_backend_want_got_plt 1
  3502. #define elf_backend_want_got_sym 0
  3503. #define elf_backend_want_plt_sym 0
  3504. #define elf_backend_plt_readonly 1
  3505. #define elf_backend_dtrel_excludes_plt 1
  3506. #define elf_backend_want_dynrelro 1
  3507. #define bfd_elf32_bfd_reloc_type_lookup metag_reloc_type_lookup
  3508. #define bfd_elf32_bfd_reloc_name_lookup metag_reloc_name_lookup
  3509. #include "elf32-target.h"