elf32-cr16.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957
  1. /* BFD back-end for National Semiconductor's CR16 ELF
  2. Copyright (C) 2007-2017 Free Software Foundation, Inc.
  3. Written by M R Swami Reddy.
  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 Foundation,
  15. Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "bfdlink.h"
  19. #include "libbfd.h"
  20. #include "libiberty.h"
  21. #include "elf-bfd.h"
  22. #include "elf/cr16.h"
  23. /* The cr16 linker needs to keep track of the number of relocs that
  24. it decides to copy in check_relocs for each symbol. This is so
  25. that it can discard PC relative relocs if it doesn't need them when
  26. linking with -Bsymbolic. We store the information in a field
  27. extending the regular ELF linker hash table. */
  28. struct elf32_cr16_link_hash_entry
  29. {
  30. /* The basic elf link hash table entry. */
  31. struct elf_link_hash_entry root;
  32. /* For function symbols, the number of times this function is
  33. called directly (ie by name). */
  34. unsigned int direct_calls;
  35. /* For function symbols, the size of this function's stack
  36. (if <= 255 bytes). We stuff this into "call" instructions
  37. to this target when it's valid and profitable to do so.
  38. This does not include stack allocated by movm! */
  39. unsigned char stack_size;
  40. /* For function symbols, arguments (if any) for movm instruction
  41. in the prologue. We stuff this value into "call" instructions
  42. to the target when it's valid and profitable to do so. */
  43. unsigned char movm_args;
  44. /* For function symbols, the amount of stack space that would be allocated
  45. by the movm instruction. This is redundant with movm_args, but we
  46. add it to the hash table to avoid computing it over and over. */
  47. unsigned char movm_stack_size;
  48. /* Used to mark functions which have had redundant parts of their
  49. prologue deleted. */
  50. #define CR16_DELETED_PROLOGUE_BYTES 0x1
  51. unsigned char flags;
  52. /* Calculated value. */
  53. bfd_vma value;
  54. };
  55. /* cr16_reloc_map array maps BFD relocation enum into a CRGAS relocation type. */
  56. struct cr16_reloc_map
  57. {
  58. bfd_reloc_code_real_type bfd_reloc_enum; /* BFD relocation enum. */
  59. unsigned short cr16_reloc_type; /* CR16 relocation type. */
  60. };
  61. static const struct cr16_reloc_map cr16_reloc_map[R_CR16_MAX] =
  62. {
  63. {BFD_RELOC_NONE, R_CR16_NONE},
  64. {BFD_RELOC_CR16_NUM8, R_CR16_NUM8},
  65. {BFD_RELOC_CR16_NUM16, R_CR16_NUM16},
  66. {BFD_RELOC_CR16_NUM32, R_CR16_NUM32},
  67. {BFD_RELOC_CR16_NUM32a, R_CR16_NUM32a},
  68. {BFD_RELOC_CR16_REGREL4, R_CR16_REGREL4},
  69. {BFD_RELOC_CR16_REGREL4a, R_CR16_REGREL4a},
  70. {BFD_RELOC_CR16_REGREL14, R_CR16_REGREL14},
  71. {BFD_RELOC_CR16_REGREL14a, R_CR16_REGREL14a},
  72. {BFD_RELOC_CR16_REGREL16, R_CR16_REGREL16},
  73. {BFD_RELOC_CR16_REGREL20, R_CR16_REGREL20},
  74. {BFD_RELOC_CR16_REGREL20a, R_CR16_REGREL20a},
  75. {BFD_RELOC_CR16_ABS20, R_CR16_ABS20},
  76. {BFD_RELOC_CR16_ABS24, R_CR16_ABS24},
  77. {BFD_RELOC_CR16_IMM4, R_CR16_IMM4},
  78. {BFD_RELOC_CR16_IMM8, R_CR16_IMM8},
  79. {BFD_RELOC_CR16_IMM16, R_CR16_IMM16},
  80. {BFD_RELOC_CR16_IMM20, R_CR16_IMM20},
  81. {BFD_RELOC_CR16_IMM24, R_CR16_IMM24},
  82. {BFD_RELOC_CR16_IMM32, R_CR16_IMM32},
  83. {BFD_RELOC_CR16_IMM32a, R_CR16_IMM32a},
  84. {BFD_RELOC_CR16_DISP4, R_CR16_DISP4},
  85. {BFD_RELOC_CR16_DISP8, R_CR16_DISP8},
  86. {BFD_RELOC_CR16_DISP16, R_CR16_DISP16},
  87. {BFD_RELOC_CR16_DISP24, R_CR16_DISP24},
  88. {BFD_RELOC_CR16_DISP24a, R_CR16_DISP24a},
  89. {BFD_RELOC_CR16_SWITCH8, R_CR16_SWITCH8},
  90. {BFD_RELOC_CR16_SWITCH16, R_CR16_SWITCH16},
  91. {BFD_RELOC_CR16_SWITCH32, R_CR16_SWITCH32},
  92. {BFD_RELOC_CR16_GOT_REGREL20, R_CR16_GOT_REGREL20},
  93. {BFD_RELOC_CR16_GOTC_REGREL20, R_CR16_GOTC_REGREL20},
  94. {BFD_RELOC_CR16_GLOB_DAT, R_CR16_GLOB_DAT}
  95. };
  96. static reloc_howto_type cr16_elf_howto_table[] =
  97. {
  98. HOWTO (R_CR16_NONE, /* type */
  99. 0, /* rightshift */
  100. 3, /* size */
  101. 0, /* bitsize */
  102. FALSE, /* pc_relative */
  103. 0, /* bitpos */
  104. complain_overflow_dont, /* complain_on_overflow */
  105. bfd_elf_generic_reloc, /* special_function */
  106. "R_CR16_NONE", /* name */
  107. FALSE, /* partial_inplace */
  108. 0, /* src_mask */
  109. 0, /* dst_mask */
  110. FALSE), /* pcrel_offset */
  111. HOWTO (R_CR16_NUM8, /* type */
  112. 0, /* rightshift */
  113. 0, /* size */
  114. 8, /* bitsize */
  115. FALSE, /* pc_relative */
  116. 0, /* bitpos */
  117. complain_overflow_bitfield,/* complain_on_overflow */
  118. bfd_elf_generic_reloc, /* special_function */
  119. "R_CR16_NUM8", /* name */
  120. FALSE, /* partial_inplace */
  121. 0x0, /* src_mask */
  122. 0xff, /* dst_mask */
  123. FALSE), /* pcrel_offset */
  124. HOWTO (R_CR16_NUM16, /* type */
  125. 0, /* rightshift */
  126. 1, /* size */
  127. 16, /* bitsize */
  128. FALSE, /* pc_relative */
  129. 0, /* bitpos */
  130. complain_overflow_bitfield,/* complain_on_overflow */
  131. bfd_elf_generic_reloc, /* special_function */
  132. "R_CR16_NUM16", /* name */
  133. FALSE, /* partial_inplace */
  134. 0x0, /* src_mask */
  135. 0xffff, /* dst_mask */
  136. FALSE), /* pcrel_offset */
  137. HOWTO (R_CR16_NUM32, /* type */
  138. 0, /* rightshift */
  139. 2, /* size */
  140. 32, /* bitsize */
  141. FALSE, /* pc_relative */
  142. 0, /* bitpos */
  143. complain_overflow_bitfield,/* complain_on_overflow */
  144. bfd_elf_generic_reloc, /* special_function */
  145. "R_CR16_NUM32", /* name */
  146. FALSE, /* partial_inplace */
  147. 0x0, /* src_mask */
  148. 0xffffffff, /* dst_mask */
  149. FALSE), /* pcrel_offset */
  150. HOWTO (R_CR16_NUM32a, /* type */
  151. 1, /* rightshift */
  152. 2, /* size */
  153. 32, /* bitsize */
  154. FALSE, /* pc_relative */
  155. 0, /* bitpos */
  156. complain_overflow_bitfield,/* complain_on_overflow */
  157. bfd_elf_generic_reloc, /* special_function */
  158. "R_CR16_NUM32a", /* name */
  159. FALSE, /* partial_inplace */
  160. 0x0, /* src_mask */
  161. 0xffffffff, /* dst_mask */
  162. FALSE), /* pcrel_offset */
  163. HOWTO (R_CR16_REGREL4, /* type */
  164. 0, /* rightshift */
  165. 0, /* size */
  166. 4, /* bitsize */
  167. FALSE, /* pc_relative */
  168. 0, /* bitpos */
  169. complain_overflow_bitfield,/* complain_on_overflow */
  170. bfd_elf_generic_reloc, /* special_function */
  171. "R_CR16_REGREL4", /* name */
  172. FALSE, /* partial_inplace */
  173. 0x0, /* src_mask */
  174. 0xf, /* dst_mask */
  175. FALSE), /* pcrel_offset */
  176. HOWTO (R_CR16_REGREL4a, /* type */
  177. 0, /* rightshift */
  178. 0, /* size */
  179. 4, /* bitsize */
  180. FALSE, /* pc_relative */
  181. 0, /* bitpos */
  182. complain_overflow_bitfield,/* complain_on_overflow */
  183. bfd_elf_generic_reloc, /* special_function */
  184. "R_CR16_REGREL4a", /* name */
  185. FALSE, /* partial_inplace */
  186. 0x0, /* src_mask */
  187. 0xf, /* dst_mask */
  188. FALSE), /* pcrel_offset */
  189. HOWTO (R_CR16_REGREL14, /* type */
  190. 0, /* rightshift */
  191. 1, /* size */
  192. 14, /* bitsize */
  193. FALSE, /* pc_relative */
  194. 0, /* bitpos */
  195. complain_overflow_bitfield,/* complain_on_overflow */
  196. bfd_elf_generic_reloc, /* special_function */
  197. "R_CR16_REGREL14", /* name */
  198. FALSE, /* partial_inplace */
  199. 0x0, /* src_mask */
  200. 0x3fff, /* dst_mask */
  201. FALSE), /* pcrel_offset */
  202. HOWTO (R_CR16_REGREL14a, /* type */
  203. 0, /* rightshift */
  204. 1, /* size */
  205. 14, /* bitsize */
  206. FALSE, /* pc_relative */
  207. 0, /* bitpos */
  208. complain_overflow_bitfield,/* complain_on_overflow */
  209. bfd_elf_generic_reloc, /* special_function */
  210. "R_CR16_REGREL14a", /* name */
  211. FALSE, /* partial_inplace */
  212. 0x0, /* src_mask */
  213. 0x3fff, /* dst_mask */
  214. FALSE), /* pcrel_offset */
  215. HOWTO (R_CR16_REGREL16, /* type */
  216. 0, /* rightshift */
  217. 1, /* size */
  218. 16, /* bitsize */
  219. FALSE, /* pc_relative */
  220. 0, /* bitpos */
  221. complain_overflow_bitfield,/* complain_on_overflow */
  222. bfd_elf_generic_reloc, /* special_function */
  223. "R_CR16_REGREL16", /* name */
  224. FALSE, /* partial_inplace */
  225. 0x0, /* src_mask */
  226. 0xffff, /* dst_mask */
  227. FALSE), /* pcrel_offset */
  228. HOWTO (R_CR16_REGREL20, /* type */
  229. 0, /* rightshift */
  230. 2, /* size */
  231. 20, /* bitsize */
  232. FALSE, /* pc_relative */
  233. 0, /* bitpos */
  234. complain_overflow_bitfield,/* complain_on_overflow */
  235. bfd_elf_generic_reloc, /* special_function */
  236. "R_CR16_REGREL20", /* name */
  237. FALSE, /* partial_inplace */
  238. 0x0, /* src_mask */
  239. 0xfffff, /* dst_mask */
  240. FALSE), /* pcrel_offset */
  241. HOWTO (R_CR16_REGREL20a, /* type */
  242. 0, /* rightshift */
  243. 2, /* size */
  244. 20, /* bitsize */
  245. FALSE, /* pc_relative */
  246. 0, /* bitpos */
  247. complain_overflow_bitfield,/* complain_on_overflow */
  248. bfd_elf_generic_reloc, /* special_function */
  249. "R_CR16_REGREL20a", /* name */
  250. FALSE, /* partial_inplace */
  251. 0x0, /* src_mask */
  252. 0xfffff, /* dst_mask */
  253. FALSE), /* pcrel_offset */
  254. HOWTO (R_CR16_ABS20, /* type */
  255. 0, /* rightshift */
  256. 2, /* size */
  257. 20, /* bitsize */
  258. FALSE, /* pc_relative */
  259. 0, /* bitpos */
  260. complain_overflow_bitfield,/* complain_on_overflow */
  261. bfd_elf_generic_reloc, /* special_function */
  262. "R_CR16_ABS20", /* name */
  263. FALSE, /* partial_inplace */
  264. 0x0, /* src_mask */
  265. 0xfffff, /* dst_mask */
  266. FALSE), /* pcrel_offset */
  267. HOWTO (R_CR16_ABS24, /* type */
  268. 0, /* rightshift */
  269. 2, /* size */
  270. 24, /* bitsize */
  271. FALSE, /* pc_relative */
  272. 0, /* bitpos */
  273. complain_overflow_bitfield,/* complain_on_overflow */
  274. bfd_elf_generic_reloc, /* special_function */
  275. "R_CR16_ABS24", /* name */
  276. FALSE, /* partial_inplace */
  277. 0x0, /* src_mask */
  278. 0xffffff, /* dst_mask */
  279. FALSE), /* pcrel_offset */
  280. HOWTO (R_CR16_IMM4, /* type */
  281. 0, /* rightshift */
  282. 0, /* size */
  283. 4, /* bitsize */
  284. FALSE, /* pc_relative */
  285. 0, /* bitpos */
  286. complain_overflow_bitfield,/* complain_on_overflow */
  287. bfd_elf_generic_reloc, /* special_function */
  288. "R_CR16_IMM4", /* name */
  289. FALSE, /* partial_inplace */
  290. 0x0, /* src_mask */
  291. 0xf, /* dst_mask */
  292. FALSE), /* pcrel_offset */
  293. HOWTO (R_CR16_IMM8, /* type */
  294. 0, /* rightshift */
  295. 0, /* size */
  296. 8, /* bitsize */
  297. FALSE, /* pc_relative */
  298. 0, /* bitpos */
  299. complain_overflow_bitfield,/* complain_on_overflow */
  300. bfd_elf_generic_reloc, /* special_function */
  301. "R_CR16_IMM8", /* name */
  302. FALSE, /* partial_inplace */
  303. 0x0, /* src_mask */
  304. 0xff, /* dst_mask */
  305. FALSE), /* pcrel_offset */
  306. HOWTO (R_CR16_IMM16, /* type */
  307. 0, /* rightshift */
  308. 1, /* size */
  309. 16, /* bitsize */
  310. FALSE, /* pc_relative */
  311. 0, /* bitpos */
  312. complain_overflow_bitfield,/* complain_on_overflow */
  313. bfd_elf_generic_reloc, /* special_function */
  314. "R_CR16_IMM16", /* name */
  315. FALSE, /* partial_inplace */
  316. 0x0, /* src_mask */
  317. 0xffff, /* dst_mask */
  318. FALSE), /* pcrel_offset */
  319. HOWTO (R_CR16_IMM20, /* type */
  320. 0, /* rightshift */
  321. 2, /* size */
  322. 20, /* bitsize */
  323. FALSE, /* pc_relative */
  324. 0, /* bitpos */
  325. complain_overflow_bitfield,/* complain_on_overflow */
  326. bfd_elf_generic_reloc, /* special_function */
  327. "R_CR16_IMM20", /* name */
  328. FALSE, /* partial_inplace */
  329. 0x0, /* src_mask */
  330. 0xfffff, /* dst_mask */
  331. FALSE), /* pcrel_offset */
  332. HOWTO (R_CR16_IMM24, /* type */
  333. 0, /* rightshift */
  334. 2, /* size */
  335. 24, /* bitsize */
  336. FALSE, /* pc_relative */
  337. 0, /* bitpos */
  338. complain_overflow_bitfield,/* complain_on_overflow */
  339. bfd_elf_generic_reloc, /* special_function */
  340. "R_CR16_IMM24", /* name */
  341. FALSE, /* partial_inplace */
  342. 0x0, /* src_mask */
  343. 0xffffff, /* dst_mask */
  344. FALSE), /* pcrel_offset */
  345. HOWTO (R_CR16_IMM32, /* type */
  346. 0, /* rightshift */
  347. 2, /* size */
  348. 32, /* bitsize */
  349. FALSE, /* pc_relative */
  350. 0, /* bitpos */
  351. complain_overflow_bitfield,/* complain_on_overflow */
  352. bfd_elf_generic_reloc, /* special_function */
  353. "R_CR16_IMM32", /* name */
  354. FALSE, /* partial_inplace */
  355. 0x0, /* src_mask */
  356. 0xffffffff, /* dst_mask */
  357. FALSE), /* pcrel_offset */
  358. HOWTO (R_CR16_IMM32a, /* type */
  359. 1, /* rightshift */
  360. 2, /* size */
  361. 32, /* bitsize */
  362. FALSE, /* pc_relative */
  363. 0, /* bitpos */
  364. complain_overflow_bitfield,/* complain_on_overflow */
  365. bfd_elf_generic_reloc, /* special_function */
  366. "R_CR16_IMM32a", /* name */
  367. FALSE, /* partial_inplace */
  368. 0x0, /* src_mask */
  369. 0xffffffff, /* dst_mask */
  370. FALSE), /* pcrel_offset */
  371. HOWTO (R_CR16_DISP4, /* type */
  372. 1, /* rightshift */
  373. 0, /* size (0 = byte, 1 = short, 2 = long) */
  374. 4, /* bitsize */
  375. TRUE, /* pc_relative */
  376. 0, /* bitpos */
  377. complain_overflow_unsigned, /* complain_on_overflow */
  378. bfd_elf_generic_reloc, /* special_function */
  379. "R_CR16_DISP4", /* name */
  380. FALSE, /* partial_inplace */
  381. 0x0, /* src_mask */
  382. 0xf, /* dst_mask */
  383. FALSE), /* pcrel_offset */
  384. HOWTO (R_CR16_DISP8, /* type */
  385. 1, /* rightshift */
  386. 0, /* size (0 = byte, 1 = short, 2 = long) */
  387. 8, /* bitsize */
  388. TRUE, /* pc_relative */
  389. 0, /* bitpos */
  390. complain_overflow_unsigned, /* complain_on_overflow */
  391. bfd_elf_generic_reloc, /* special_function */
  392. "R_CR16_DISP8", /* name */
  393. FALSE, /* partial_inplace */
  394. 0x0, /* src_mask */
  395. 0x1ff, /* dst_mask */
  396. FALSE), /* pcrel_offset */
  397. HOWTO (R_CR16_DISP16, /* type */
  398. 0, /* rightshift REVIITS: To sync with WinIDEA*/
  399. 1, /* size (0 = byte, 1 = short, 2 = long) */
  400. 16, /* bitsize */
  401. TRUE, /* pc_relative */
  402. 0, /* bitpos */
  403. complain_overflow_unsigned, /* complain_on_overflow */
  404. bfd_elf_generic_reloc, /* special_function */
  405. "R_CR16_DISP16", /* name */
  406. FALSE, /* partial_inplace */
  407. 0x0, /* src_mask */
  408. 0x1ffff, /* dst_mask */
  409. FALSE), /* pcrel_offset */
  410. /* REVISIT: DISP24 should be left-shift by 2 as per ISA doc
  411. but its not done, to sync with WinIDEA and CR16 4.1 tools */
  412. HOWTO (R_CR16_DISP24, /* type */
  413. 0, /* rightshift */
  414. 2, /* size (0 = byte, 1 = short, 2 = long) */
  415. 24, /* bitsize */
  416. TRUE, /* pc_relative */
  417. 0, /* bitpos */
  418. complain_overflow_unsigned, /* complain_on_overflow */
  419. bfd_elf_generic_reloc, /* special_function */
  420. "R_CR16_DISP24", /* name */
  421. FALSE, /* partial_inplace */
  422. 0x0, /* src_mask */
  423. 0x1ffffff, /* dst_mask */
  424. FALSE), /* pcrel_offset */
  425. HOWTO (R_CR16_DISP24a, /* type */
  426. 0, /* rightshift */
  427. 2, /* size (0 = byte, 1 = short, 2 = long) */
  428. 24, /* bitsize */
  429. TRUE, /* pc_relative */
  430. 0, /* bitpos */
  431. complain_overflow_unsigned, /* complain_on_overflow */
  432. bfd_elf_generic_reloc, /* special_function */
  433. "R_CR16_DISP24a", /* name */
  434. FALSE, /* partial_inplace */
  435. 0x0, /* src_mask */
  436. 0xffffff, /* dst_mask */
  437. FALSE), /* pcrel_offset */
  438. /* An 8 bit switch table entry. This is generated for an expression
  439. such as ``.byte L1 - L2''. The offset holds the difference
  440. between the reloc address and L2. */
  441. HOWTO (R_CR16_SWITCH8, /* type */
  442. 0, /* rightshift */
  443. 0, /* size (0 = byte, 1 = short, 2 = long) */
  444. 8, /* bitsize */
  445. FALSE, /* pc_relative */
  446. 0, /* bitpos */
  447. complain_overflow_unsigned, /* complain_on_overflow */
  448. bfd_elf_generic_reloc, /* special_function */
  449. "R_CR16_SWITCH8", /* name */
  450. FALSE, /* partial_inplace */
  451. 0x0, /* src_mask */
  452. 0xff, /* dst_mask */
  453. TRUE), /* pcrel_offset */
  454. /* A 16 bit switch table entry. This is generated for an expression
  455. such as ``.word L1 - L2''. The offset holds the difference
  456. between the reloc address and L2. */
  457. HOWTO (R_CR16_SWITCH16, /* type */
  458. 0, /* rightshift */
  459. 1, /* size (0 = byte, 1 = short, 2 = long) */
  460. 16, /* bitsize */
  461. FALSE, /* pc_relative */
  462. 0, /* bitpos */
  463. complain_overflow_unsigned, /* complain_on_overflow */
  464. bfd_elf_generic_reloc, /* special_function */
  465. "R_CR16_SWITCH16", /* name */
  466. FALSE, /* partial_inplace */
  467. 0x0, /* src_mask */
  468. 0xffff, /* dst_mask */
  469. TRUE), /* pcrel_offset */
  470. /* A 32 bit switch table entry. This is generated for an expression
  471. such as ``.long L1 - L2''. The offset holds the difference
  472. between the reloc address and L2. */
  473. HOWTO (R_CR16_SWITCH32, /* type */
  474. 0, /* rightshift */
  475. 2, /* size (0 = byte, 1 = short, 2 = long) */
  476. 32, /* bitsize */
  477. FALSE, /* pc_relative */
  478. 0, /* bitpos */
  479. complain_overflow_unsigned, /* complain_on_overflow */
  480. bfd_elf_generic_reloc, /* special_function */
  481. "R_CR16_SWITCH32", /* name */
  482. FALSE, /* partial_inplace */
  483. 0x0, /* src_mask */
  484. 0xffffffff, /* dst_mask */
  485. TRUE), /* pcrel_offset */
  486. HOWTO (R_CR16_GOT_REGREL20, /* type */
  487. 0, /* rightshift */
  488. 2, /* size */
  489. 20, /* bitsize */
  490. FALSE, /* pc_relative */
  491. 0, /* bitpos */
  492. complain_overflow_bitfield,/* complain_on_overflow */
  493. bfd_elf_generic_reloc, /* special_function */
  494. "R_CR16_GOT_REGREL20", /* name */
  495. TRUE, /* partial_inplace */
  496. 0x0, /* src_mask */
  497. 0xfffff, /* dst_mask */
  498. FALSE), /* pcrel_offset */
  499. HOWTO (R_CR16_GOTC_REGREL20, /* type */
  500. 0, /* rightshift */
  501. 2, /* size */
  502. 20, /* bitsize */
  503. FALSE, /* pc_relative */
  504. 0, /* bitpos */
  505. complain_overflow_bitfield,/* complain_on_overflow */
  506. bfd_elf_generic_reloc, /* special_function */
  507. "R_CR16_GOTC_REGREL20", /* name */
  508. TRUE, /* partial_inplace */
  509. 0x0, /* src_mask */
  510. 0xfffff, /* dst_mask */
  511. FALSE), /* pcrel_offset */
  512. HOWTO (R_CR16_GLOB_DAT, /* type */
  513. 0, /* rightshift */
  514. 2, /* size (0 = byte, 1 = short, 2 = long) */
  515. 32, /* bitsize */
  516. FALSE, /* pc_relative */
  517. 0, /* bitpos */
  518. complain_overflow_unsigned, /* complain_on_overflow */
  519. bfd_elf_generic_reloc, /* special_function */
  520. "R_CR16_GLOB_DAT", /* name */
  521. FALSE, /* partial_inplace */
  522. 0x0, /* src_mask */
  523. 0xffffffff, /* dst_mask */
  524. TRUE) /* pcrel_offset */
  525. };
  526. /* Create the GOT section. */
  527. static bfd_boolean
  528. _bfd_cr16_elf_create_got_section (bfd * abfd, struct bfd_link_info * info)
  529. {
  530. flagword flags;
  531. asection * s;
  532. struct elf_link_hash_entry * h;
  533. const struct elf_backend_data * bed = get_elf_backend_data (abfd);
  534. struct elf_link_hash_table *htab = elf_hash_table (info);
  535. int ptralign;
  536. /* This function may be called more than once. */
  537. if (htab->sgot != NULL)
  538. return TRUE;
  539. switch (bed->s->arch_size)
  540. {
  541. case 16:
  542. ptralign = 1;
  543. break;
  544. case 32:
  545. ptralign = 2;
  546. break;
  547. default:
  548. bfd_set_error (bfd_error_bad_value);
  549. return FALSE;
  550. }
  551. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  552. | SEC_LINKER_CREATED);
  553. s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  554. htab->sgot= s;
  555. if (s == NULL
  556. || ! bfd_set_section_alignment (abfd, s, ptralign))
  557. return FALSE;
  558. if (bed->want_got_plt)
  559. {
  560. s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  561. htab->sgotplt = s;
  562. if (s == NULL
  563. || ! bfd_set_section_alignment (abfd, s, ptralign))
  564. return FALSE;
  565. }
  566. /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  567. (or .got.plt) section. We don't do this in the linker script
  568. because we don't want to define the symbol if we are not creating
  569. a global offset table. */
  570. h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
  571. htab->hgot = h;
  572. if (h == NULL)
  573. return FALSE;
  574. /* The first bit of the global offset table is the header. */
  575. s->size += bed->got_header_size;
  576. return TRUE;
  577. }
  578. /* Retrieve a howto ptr using a BFD reloc_code. */
  579. static reloc_howto_type *
  580. elf_cr16_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  581. bfd_reloc_code_real_type code)
  582. {
  583. unsigned int i;
  584. for (i = 0; i < R_CR16_MAX; i++)
  585. if (code == cr16_reloc_map[i].bfd_reloc_enum)
  586. return &cr16_elf_howto_table[cr16_reloc_map[i].cr16_reloc_type];
  587. _bfd_error_handler (_("Unsupported CR16 relocation type: 0x%x\n"), code);
  588. return NULL;
  589. }
  590. static reloc_howto_type *
  591. elf_cr16_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  592. const char *r_name)
  593. {
  594. unsigned int i;
  595. for (i = 0; ARRAY_SIZE (cr16_elf_howto_table); i++)
  596. if (cr16_elf_howto_table[i].name != NULL
  597. && strcasecmp (cr16_elf_howto_table[i].name, r_name) == 0)
  598. return cr16_elf_howto_table + i;
  599. return NULL;
  600. }
  601. /* Retrieve a howto ptr using an internal relocation entry. */
  602. static void
  603. elf_cr16_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
  604. Elf_Internal_Rela *dst)
  605. {
  606. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  607. if (r_type >= R_CR16_MAX)
  608. {
  609. /* xgettext:c-format */
  610. _bfd_error_handler (_("%B: unrecognised CR16 reloc number: %d"),
  611. abfd, r_type);
  612. bfd_set_error (bfd_error_bad_value);
  613. r_type = R_CR16_NONE;
  614. }
  615. cache_ptr->howto = cr16_elf_howto_table + r_type;
  616. }
  617. /* Look through the relocs for a section during the first phase.
  618. Since we don't do .gots or .plts, we just need to consider the
  619. virtual table relocs for gc. */
  620. static bfd_boolean
  621. cr16_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
  622. const Elf_Internal_Rela *relocs)
  623. {
  624. Elf_Internal_Shdr *symtab_hdr;
  625. Elf_Internal_Sym * isymbuf = NULL;
  626. struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
  627. const Elf_Internal_Rela *rel;
  628. const Elf_Internal_Rela *rel_end;
  629. bfd * dynobj;
  630. bfd_vma * local_got_offsets;
  631. asection * sgot;
  632. asection * srelgot;
  633. sgot = NULL;
  634. srelgot = NULL;
  635. bfd_boolean result = FALSE;
  636. if (bfd_link_relocatable (info))
  637. return TRUE;
  638. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  639. sym_hashes = elf_sym_hashes (abfd);
  640. sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
  641. if (!elf_bad_symtab (abfd))
  642. sym_hashes_end -= symtab_hdr->sh_info;
  643. dynobj = elf_hash_table (info)->dynobj;
  644. local_got_offsets = elf_local_got_offsets (abfd);
  645. rel_end = relocs + sec->reloc_count;
  646. for (rel = relocs; rel < rel_end; rel++)
  647. {
  648. struct elf_link_hash_entry *h;
  649. unsigned long r_symndx;
  650. r_symndx = ELF32_R_SYM (rel->r_info);
  651. if (r_symndx < symtab_hdr->sh_info)
  652. h = NULL;
  653. else
  654. {
  655. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  656. while (h->root.type == bfd_link_hash_indirect
  657. || h->root.type == bfd_link_hash_warning)
  658. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  659. /* PR15323, ref flags aren't set for references in the same
  660. object. */
  661. h->root.non_ir_ref_regular = 1;
  662. }
  663. /* Some relocs require a global offset table. */
  664. if (dynobj == NULL)
  665. {
  666. switch (ELF32_R_TYPE (rel->r_info))
  667. {
  668. case R_CR16_GOT_REGREL20:
  669. case R_CR16_GOTC_REGREL20:
  670. elf_hash_table (info)->dynobj = dynobj = abfd;
  671. if (! _bfd_cr16_elf_create_got_section (dynobj, info))
  672. goto fail;
  673. break;
  674. default:
  675. break;
  676. }
  677. }
  678. switch (ELF32_R_TYPE (rel->r_info))
  679. {
  680. case R_CR16_GOT_REGREL20:
  681. case R_CR16_GOTC_REGREL20:
  682. /* This symbol requires a global offset table entry. */
  683. sgot = elf_hash_table (info)->sgot;
  684. srelgot = elf_hash_table (info)->srelgot;
  685. BFD_ASSERT (sgot != NULL && srelgot != NULL);
  686. if (h != NULL)
  687. {
  688. if (h->got.offset != (bfd_vma) -1)
  689. /* We have already allocated space in the .got. */
  690. break;
  691. h->got.offset = sgot->size;
  692. /* Make sure this symbol is output as a dynamic symbol. */
  693. if (h->dynindx == -1)
  694. {
  695. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  696. goto fail;
  697. }
  698. srelgot->size += sizeof (Elf32_External_Rela);
  699. }
  700. else
  701. {
  702. /* This is a global offset table entry for a local
  703. symbol. */
  704. if (local_got_offsets == NULL)
  705. {
  706. size_t size;
  707. unsigned int i;
  708. size = symtab_hdr->sh_info * sizeof (bfd_vma);
  709. local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
  710. if (local_got_offsets == NULL)
  711. goto fail;
  712. elf_local_got_offsets (abfd) = local_got_offsets;
  713. for (i = 0; i < symtab_hdr->sh_info; i++)
  714. local_got_offsets[i] = (bfd_vma) -1;
  715. }
  716. if (local_got_offsets[r_symndx] != (bfd_vma) -1)
  717. /* We have already allocated space in the .got. */
  718. break;
  719. local_got_offsets[r_symndx] = sgot->size;
  720. if (bfd_link_executable (info))
  721. /* If we are generating a shared object, we need to
  722. output a R_CR16_RELATIVE reloc so that the dynamic
  723. linker can adjust this GOT entry. */
  724. srelgot->size += sizeof (Elf32_External_Rela);
  725. }
  726. sgot->size += 4;
  727. break;
  728. }
  729. }
  730. result = TRUE;
  731. fail:
  732. if (isymbuf != NULL)
  733. free (isymbuf);
  734. return result;
  735. }
  736. /* Perform a relocation as part of a final link. */
  737. static bfd_reloc_status_type
  738. cr16_elf_final_link_relocate (reloc_howto_type *howto,
  739. bfd *input_bfd,
  740. bfd *output_bfd ATTRIBUTE_UNUSED,
  741. asection *input_section,
  742. bfd_byte *contents,
  743. bfd_vma offset,
  744. bfd_vma Rvalue,
  745. bfd_vma addend,
  746. struct elf_link_hash_entry * h,
  747. unsigned long symndx ATTRIBUTE_UNUSED,
  748. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  749. asection *sec ATTRIBUTE_UNUSED,
  750. int is_local ATTRIBUTE_UNUSED)
  751. {
  752. unsigned short r_type = howto->type;
  753. bfd_byte *hit_data = contents + offset;
  754. bfd_vma reloc_bits, check, Rvalue1;
  755. switch (r_type)
  756. {
  757. case R_CR16_IMM4:
  758. case R_CR16_IMM20:
  759. case R_CR16_ABS20:
  760. break;
  761. case R_CR16_IMM8:
  762. case R_CR16_IMM16:
  763. case R_CR16_IMM32:
  764. case R_CR16_IMM32a:
  765. case R_CR16_REGREL4:
  766. case R_CR16_REGREL4a:
  767. case R_CR16_REGREL14:
  768. case R_CR16_REGREL14a:
  769. case R_CR16_REGREL16:
  770. case R_CR16_REGREL20:
  771. case R_CR16_REGREL20a:
  772. case R_CR16_GOT_REGREL20:
  773. case R_CR16_GOTC_REGREL20:
  774. case R_CR16_ABS24:
  775. case R_CR16_DISP16:
  776. case R_CR16_DISP24:
  777. /* 'hit_data' is relative to the start of the instruction, not the
  778. relocation offset. Advance it to account for the exact offset. */
  779. hit_data += 2;
  780. break;
  781. case R_CR16_NONE:
  782. return bfd_reloc_ok;
  783. break;
  784. case R_CR16_DISP4:
  785. if (is_local)
  786. Rvalue += -1;
  787. break;
  788. case R_CR16_DISP8:
  789. case R_CR16_DISP24a:
  790. if (is_local)
  791. Rvalue -= -1;
  792. break;
  793. case R_CR16_SWITCH8:
  794. case R_CR16_SWITCH16:
  795. case R_CR16_SWITCH32:
  796. /* We only care about the addend, where the difference between
  797. expressions is kept. */
  798. Rvalue = 0;
  799. default:
  800. break;
  801. }
  802. if (howto->pc_relative)
  803. {
  804. /* Subtract the address of the section containing the location. */
  805. Rvalue -= (input_section->output_section->vma
  806. + input_section->output_offset);
  807. /* Subtract the position of the location within the section. */
  808. Rvalue -= offset;
  809. }
  810. /* Add in supplied addend. */
  811. Rvalue += addend;
  812. /* Complain if the bitfield overflows, whether it is considered
  813. as signed or unsigned. */
  814. check = Rvalue >> howto->rightshift;
  815. /* Assumes two's complement. This expression avoids
  816. overflow if howto->bitsize is the number of bits in
  817. bfd_vma. */
  818. reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  819. /* For GOT and GOTC relocs no boundary checks applied. */
  820. if (!((r_type == R_CR16_GOT_REGREL20)
  821. || (r_type == R_CR16_GOTC_REGREL20)))
  822. {
  823. if (((bfd_vma) check & ~reloc_bits) != 0
  824. && (((bfd_vma) check & ~reloc_bits)
  825. != (-(bfd_vma) 1 & ~reloc_bits)))
  826. {
  827. /* The above right shift is incorrect for a signed
  828. value. See if turning on the upper bits fixes the
  829. overflow. */
  830. if (howto->rightshift && (bfd_signed_vma) Rvalue < 0)
  831. {
  832. check |= ((bfd_vma) - 1
  833. & ~((bfd_vma) - 1
  834. >> howto->rightshift));
  835. if (((bfd_vma) check & ~reloc_bits)
  836. != (-(bfd_vma) 1 & ~reloc_bits))
  837. return bfd_reloc_overflow;
  838. }
  839. else
  840. return bfd_reloc_overflow;
  841. }
  842. /* Drop unwanted bits from the value we are relocating to. */
  843. Rvalue >>= (bfd_vma) howto->rightshift;
  844. /* Apply dst_mask to select only relocatable part of the insn. */
  845. Rvalue &= howto->dst_mask;
  846. }
  847. switch (howto->size)
  848. {
  849. case 0:
  850. if (r_type == R_CR16_DISP8)
  851. {
  852. Rvalue1 = bfd_get_16 (input_bfd, hit_data);
  853. Rvalue = ((Rvalue1 & 0xf000) | ((Rvalue << 4) & 0xf00)
  854. | (Rvalue1 & 0x00f0) | (Rvalue & 0xf));
  855. bfd_put_16 (input_bfd, Rvalue, hit_data);
  856. }
  857. else if (r_type == R_CR16_IMM4)
  858. {
  859. Rvalue1 = bfd_get_16 (input_bfd, hit_data);
  860. Rvalue = (((Rvalue1 & 0xff) << 8) | ((Rvalue << 4) & 0xf0)
  861. | ((Rvalue1 & 0x0f00) >> 8));
  862. bfd_put_16 (input_bfd, Rvalue, hit_data);
  863. }
  864. else if (r_type == R_CR16_DISP4)
  865. {
  866. Rvalue1 = bfd_get_16 (input_bfd, hit_data);
  867. Rvalue = (Rvalue1 | ((Rvalue & 0xf) << 4));
  868. bfd_put_16 (input_bfd, Rvalue, hit_data);
  869. }
  870. else
  871. {
  872. bfd_put_8 (input_bfd, (unsigned char) Rvalue, hit_data);
  873. }
  874. break;
  875. case 1:
  876. if (r_type == R_CR16_DISP16)
  877. {
  878. Rvalue |= (bfd_get_16 (input_bfd, hit_data));
  879. Rvalue = ((Rvalue & 0xfffe) | ((Rvalue >> 16) & 0x1));
  880. }
  881. if (r_type == R_CR16_IMM16)
  882. {
  883. Rvalue1 = bfd_get_16 (input_bfd, hit_data);
  884. /* Add or subtract the offset value. */
  885. if (Rvalue1 & 0x8000)
  886. Rvalue -= (~Rvalue1 + 1) & 0xffff;
  887. else
  888. Rvalue += Rvalue1;
  889. /* Check for range. */
  890. if ((long) Rvalue > 0xffff || (long) Rvalue < 0x0)
  891. return bfd_reloc_overflow;
  892. }
  893. bfd_put_16 (input_bfd, Rvalue, hit_data);
  894. break;
  895. case 2:
  896. if ((r_type == R_CR16_ABS20) || (r_type == R_CR16_IMM20))
  897. {
  898. Rvalue1 = (bfd_get_16 (input_bfd, hit_data + 2)
  899. | (((bfd_get_16 (input_bfd, hit_data) & 0xf) <<16)));
  900. /* Add or subtract the offset value. */
  901. if (Rvalue1 & 0x80000)
  902. Rvalue -= (~Rvalue1 + 1) & 0xfffff;
  903. else
  904. Rvalue += Rvalue1;
  905. /* Check for range. */
  906. if ((long) Rvalue > 0xfffff || (long) Rvalue < 0x0)
  907. return bfd_reloc_overflow;
  908. bfd_put_16 (input_bfd, ((bfd_get_16 (input_bfd, hit_data) & 0xfff0)
  909. | ((Rvalue >> 16) & 0xf)), hit_data);
  910. bfd_put_16 (input_bfd, (Rvalue) & 0xffff, hit_data + 2);
  911. }
  912. else if (r_type == R_CR16_GOT_REGREL20)
  913. {
  914. asection *sgot = elf_hash_table (info)->sgot;
  915. if (h != NULL)
  916. {
  917. bfd_vma off;
  918. off = h->got.offset;
  919. BFD_ASSERT (off != (bfd_vma) -1);
  920. if (! elf_hash_table (info)->dynamic_sections_created
  921. || SYMBOL_REFERENCES_LOCAL (info, h))
  922. /* This is actually a static link, or it is a
  923. -Bsymbolic link and the symbol is defined
  924. locally, or the symbol was forced to be local
  925. because of a version file. We must initialize
  926. this entry in the global offset table.
  927. When doing a dynamic link, we create a .rela.got
  928. relocation entry to initialize the value. This
  929. is done in the finish_dynamic_symbol routine. */
  930. bfd_put_32 (output_bfd, Rvalue, sgot->contents + off);
  931. Rvalue = sgot->output_offset + off;
  932. }
  933. else
  934. {
  935. bfd_vma off;
  936. off = elf_local_got_offsets (input_bfd)[symndx];
  937. bfd_put_32 (output_bfd,Rvalue, sgot->contents + off);
  938. Rvalue = sgot->output_offset + off;
  939. }
  940. Rvalue += addend;
  941. /* REVISIT: if ((long) Rvalue > 0xffffff ||
  942. (long) Rvalue < -0x800000). */
  943. if ((long) Rvalue > 0xffffff || (long) Rvalue < 0)
  944. return bfd_reloc_overflow;
  945. bfd_put_16 (input_bfd, (bfd_get_16 (input_bfd, hit_data))
  946. | (((Rvalue >> 16) & 0xf) << 8), hit_data);
  947. bfd_put_16 (input_bfd, (Rvalue) & 0xffff, hit_data + 2);
  948. }
  949. else if (r_type == R_CR16_GOTC_REGREL20)
  950. {
  951. asection *sgot = elf_hash_table (info)->sgot;
  952. if (h != NULL)
  953. {
  954. bfd_vma off;
  955. off = h->got.offset;
  956. BFD_ASSERT (off != (bfd_vma) -1);
  957. Rvalue >>=1; /* For code symbols. */
  958. if (! elf_hash_table (info)->dynamic_sections_created
  959. || SYMBOL_REFERENCES_LOCAL (info, h))
  960. /* This is actually a static link, or it is a
  961. -Bsymbolic link and the symbol is defined
  962. locally, or the symbol was forced to be local
  963. because of a version file. We must initialize
  964. this entry in the global offset table.
  965. When doing a dynamic link, we create a .rela.got
  966. relocation entry to initialize the value. This
  967. is done in the finish_dynamic_symbol routine. */
  968. bfd_put_32 (output_bfd, Rvalue, sgot->contents + off);
  969. Rvalue = sgot->output_offset + off;
  970. }
  971. else
  972. {
  973. bfd_vma off;
  974. off = elf_local_got_offsets (input_bfd)[symndx];
  975. Rvalue >>= 1;
  976. bfd_put_32 (output_bfd,Rvalue, sgot->contents + off);
  977. Rvalue = sgot->output_offset + off;
  978. }
  979. Rvalue += addend;
  980. /* Check if any value in DISP. */
  981. Rvalue1 =((bfd_get_32 (input_bfd, hit_data) >>16)
  982. | (((bfd_get_32 (input_bfd, hit_data) & 0xfff) >> 8) <<16));
  983. /* Add or subtract the offset value. */
  984. if (Rvalue1 & 0x80000)
  985. Rvalue -= (~Rvalue1 + 1) & 0xfffff;
  986. else
  987. Rvalue += Rvalue1;
  988. /* Check for range. */
  989. /* REVISIT: if ((long) Rvalue > 0xffffff
  990. || (long) Rvalue < -0x800000). */
  991. if ((long) Rvalue > 0xffffff || (long) Rvalue < 0)
  992. return bfd_reloc_overflow;
  993. bfd_put_16 (input_bfd, (bfd_get_16 (input_bfd, hit_data))
  994. | (((Rvalue >> 16) & 0xf) << 8), hit_data);
  995. bfd_put_16 (input_bfd, (Rvalue) & 0xffff, hit_data + 2);
  996. }
  997. else
  998. {
  999. if (r_type == R_CR16_ABS24)
  1000. {
  1001. Rvalue1 = ((bfd_get_32 (input_bfd, hit_data) >> 16)
  1002. | (((bfd_get_32 (input_bfd, hit_data) & 0xfff) >> 8) <<16)
  1003. | (((bfd_get_32 (input_bfd, hit_data) & 0xf) <<20)));
  1004. /* Add or subtract the offset value. */
  1005. if (Rvalue1 & 0x800000)
  1006. Rvalue -= (~Rvalue1 + 1) & 0xffffff;
  1007. else
  1008. Rvalue += Rvalue1;
  1009. /* Check for Range. */
  1010. if ((long) Rvalue > 0xffffff || (long) Rvalue < 0x0)
  1011. return bfd_reloc_overflow;
  1012. Rvalue = ((((Rvalue >> 20) & 0xf) | (((Rvalue >> 16) & 0xf)<<8)
  1013. | (bfd_get_32 (input_bfd, hit_data) & 0xf0f0))
  1014. | ((Rvalue & 0xffff) << 16));
  1015. }
  1016. else if (r_type == R_CR16_DISP24)
  1017. {
  1018. Rvalue = ((((Rvalue >> 20)& 0xf) | (((Rvalue >>16) & 0xf)<<8)
  1019. | (bfd_get_16 (input_bfd, hit_data)))
  1020. | (((Rvalue & 0xfffe) | ((Rvalue >> 24) & 0x1)) << 16));
  1021. }
  1022. else if ((r_type == R_CR16_IMM32) || (r_type == R_CR16_IMM32a))
  1023. {
  1024. Rvalue1 =((((bfd_get_32 (input_bfd, hit_data)) >> 16) &0xffff)
  1025. | (((bfd_get_32 (input_bfd, hit_data)) &0xffff)) << 16);
  1026. /* Add or subtract the offset value. */
  1027. if (Rvalue1 & 0x80000000)
  1028. Rvalue -= (~Rvalue1 + 1) & 0xffffffff;
  1029. else
  1030. Rvalue += Rvalue1;
  1031. /* Check for range. */
  1032. if (Rvalue > 0xffffffff || (long) Rvalue < 0x0)
  1033. return bfd_reloc_overflow;
  1034. Rvalue = (((Rvalue >> 16)& 0xffff) | (Rvalue & 0xffff) << 16);
  1035. }
  1036. else if (r_type == R_CR16_DISP24a)
  1037. {
  1038. Rvalue = (((Rvalue & 0xfffffe) | (Rvalue >> 23)));
  1039. Rvalue = ((Rvalue >> 16) & 0xff) | ((Rvalue & 0xffff) << 16)
  1040. | (bfd_get_32 (input_bfd, hit_data));
  1041. }
  1042. else if ((r_type == R_CR16_REGREL20)
  1043. || (r_type == R_CR16_REGREL20a))
  1044. {
  1045. Rvalue1 = ((bfd_get_32 (input_bfd, hit_data) >> 16)
  1046. | (((bfd_get_32 (input_bfd, hit_data) & 0xfff) >> 8) <<16));
  1047. /* Add or subtract the offset value. */
  1048. if (Rvalue1 & 0x80000)
  1049. Rvalue -= (~Rvalue1 + 1) & 0xfffff;
  1050. else
  1051. Rvalue += Rvalue1;
  1052. /* Check for range. */
  1053. if ((long) Rvalue > 0xfffff || (long) Rvalue < 0x0)
  1054. return bfd_reloc_overflow;
  1055. Rvalue = (((((Rvalue >> 20)& 0xf) | (((Rvalue >>16) & 0xf)<<8)
  1056. | ((Rvalue & 0xffff) << 16)))
  1057. | (bfd_get_32 (input_bfd, hit_data) & 0xf0ff));
  1058. }
  1059. else if (r_type == R_CR16_NUM32)
  1060. {
  1061. Rvalue1 = (bfd_get_32 (input_bfd, hit_data));
  1062. /* Add or subtract the offset value */
  1063. if (Rvalue1 & 0x80000000)
  1064. Rvalue -= (~Rvalue1 + 1) & 0xffffffff;
  1065. else
  1066. Rvalue += Rvalue1;
  1067. /* Check for Ranga */
  1068. if (Rvalue > 0xffffffff)
  1069. return bfd_reloc_overflow;
  1070. }
  1071. bfd_put_32 (input_bfd, Rvalue, hit_data);
  1072. }
  1073. break;
  1074. default:
  1075. return bfd_reloc_notsupported;
  1076. }
  1077. return bfd_reloc_ok;
  1078. }
  1079. /* Delete some bytes from a section while relaxing. */
  1080. static bfd_boolean
  1081. elf32_cr16_relax_delete_bytes (struct bfd_link_info *link_info, bfd *abfd,
  1082. asection *sec, bfd_vma addr, int count)
  1083. {
  1084. Elf_Internal_Shdr *symtab_hdr;
  1085. unsigned int sec_shndx;
  1086. bfd_byte *contents;
  1087. Elf_Internal_Rela *irel, *irelend;
  1088. bfd_vma toaddr;
  1089. Elf_Internal_Sym *isym;
  1090. Elf_Internal_Sym *isymend;
  1091. struct elf_link_hash_entry **sym_hashes;
  1092. struct elf_link_hash_entry **end_hashes;
  1093. struct elf_link_hash_entry **start_hashes;
  1094. unsigned int symcount;
  1095. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1096. contents = elf_section_data (sec)->this_hdr.contents;
  1097. toaddr = sec->size;
  1098. irel = elf_section_data (sec)->relocs;
  1099. irelend = irel + sec->reloc_count;
  1100. /* Actually delete the bytes. */
  1101. memmove (contents + addr, contents + addr + count,
  1102. (size_t) (toaddr - addr - count));
  1103. sec->size -= count;
  1104. /* Adjust all the relocs. */
  1105. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  1106. /* Get the new reloc address. */
  1107. if ((irel->r_offset > addr && irel->r_offset < toaddr))
  1108. irel->r_offset -= count;
  1109. /* Adjust the local symbols defined in this section. */
  1110. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1111. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1112. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  1113. {
  1114. if (isym->st_shndx == sec_shndx
  1115. && isym->st_value > addr
  1116. && isym->st_value < toaddr)
  1117. {
  1118. /* Adjust the addend of SWITCH relocations in this section,
  1119. which reference this local symbol. */
  1120. #if 0
  1121. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  1122. {
  1123. unsigned long r_symndx;
  1124. Elf_Internal_Sym *rsym;
  1125. bfd_vma addsym, subsym;
  1126. /* Skip if not a SWITCH relocation. */
  1127. if (ELF32_R_TYPE (irel->r_info) != (int) R_CR16_SWITCH8
  1128. && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_SWITCH16
  1129. && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_SWITCH32)
  1130. continue;
  1131. r_symndx = ELF32_R_SYM (irel->r_info);
  1132. rsym = (Elf_Internal_Sym *) symtab_hdr->contents + r_symndx;
  1133. /* Skip if not the local adjusted symbol. */
  1134. if (rsym != isym)
  1135. continue;
  1136. addsym = isym->st_value;
  1137. subsym = addsym - irel->r_addend;
  1138. /* Fix the addend only when -->> (addsym > addr >= subsym). */
  1139. if (subsym <= addr)
  1140. irel->r_addend -= count;
  1141. else
  1142. continue;
  1143. }
  1144. #endif
  1145. isym->st_value -= count;
  1146. }
  1147. }
  1148. /* Now adjust the global symbols defined in this section. */
  1149. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1150. - symtab_hdr->sh_info);
  1151. sym_hashes = start_hashes = elf_sym_hashes (abfd);
  1152. end_hashes = sym_hashes + symcount;
  1153. for (; sym_hashes < end_hashes; sym_hashes++)
  1154. {
  1155. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1156. /* The '--wrap SYMBOL' option is causing a pain when the object file,
  1157. containing the definition of __wrap_SYMBOL, includes a direct
  1158. call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
  1159. the same symbol (which is __wrap_SYMBOL), but still exist as two
  1160. different symbols in 'sym_hashes', we don't want to adjust
  1161. the global symbol __wrap_SYMBOL twice.
  1162. This check is only relevant when symbols are being wrapped. */
  1163. if (link_info->wrap_hash != NULL)
  1164. {
  1165. struct elf_link_hash_entry **cur_sym_hashes;
  1166. /* Loop only over the symbols whom been already checked. */
  1167. for (cur_sym_hashes = start_hashes; cur_sym_hashes < sym_hashes;
  1168. cur_sym_hashes++)
  1169. /* If the current symbol is identical to 'sym_hash', that means
  1170. the symbol was already adjusted (or at least checked). */
  1171. if (*cur_sym_hashes == sym_hash)
  1172. break;
  1173. /* Don't adjust the symbol again. */
  1174. if (cur_sym_hashes < sym_hashes)
  1175. continue;
  1176. }
  1177. if ((sym_hash->root.type == bfd_link_hash_defined
  1178. || sym_hash->root.type == bfd_link_hash_defweak)
  1179. && sym_hash->root.u.def.section == sec
  1180. && sym_hash->root.u.def.value > addr
  1181. && sym_hash->root.u.def.value < toaddr)
  1182. sym_hash->root.u.def.value -= count;
  1183. }
  1184. return TRUE;
  1185. }
  1186. /* Relocate a CR16 ELF section. */
  1187. static bfd_boolean
  1188. elf32_cr16_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
  1189. bfd *input_bfd, asection *input_section,
  1190. bfd_byte *contents, Elf_Internal_Rela *relocs,
  1191. Elf_Internal_Sym *local_syms,
  1192. asection **local_sections)
  1193. {
  1194. Elf_Internal_Shdr *symtab_hdr;
  1195. struct elf_link_hash_entry **sym_hashes;
  1196. Elf_Internal_Rela *rel, *relend;
  1197. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1198. sym_hashes = elf_sym_hashes (input_bfd);
  1199. rel = relocs;
  1200. relend = relocs + input_section->reloc_count;
  1201. for (; rel < relend; rel++)
  1202. {
  1203. int r_type;
  1204. reloc_howto_type *howto;
  1205. unsigned long r_symndx;
  1206. Elf_Internal_Sym *sym;
  1207. asection *sec;
  1208. struct elf_link_hash_entry *h;
  1209. bfd_vma relocation;
  1210. bfd_reloc_status_type r;
  1211. r_symndx = ELF32_R_SYM (rel->r_info);
  1212. r_type = ELF32_R_TYPE (rel->r_info);
  1213. howto = cr16_elf_howto_table + (r_type);
  1214. h = NULL;
  1215. sym = NULL;
  1216. sec = NULL;
  1217. if (r_symndx < symtab_hdr->sh_info)
  1218. {
  1219. sym = local_syms + r_symndx;
  1220. sec = local_sections[r_symndx];
  1221. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1222. }
  1223. else
  1224. {
  1225. bfd_boolean unresolved_reloc, warned, ignored;
  1226. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1227. r_symndx, symtab_hdr, sym_hashes,
  1228. h, sec, relocation,
  1229. unresolved_reloc, warned, ignored);
  1230. }
  1231. if (sec != NULL && discarded_section (sec))
  1232. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1233. rel, 1, relend, howto, 0, contents);
  1234. if (bfd_link_relocatable (info))
  1235. continue;
  1236. r = cr16_elf_final_link_relocate (howto, input_bfd, output_bfd,
  1237. input_section,
  1238. contents, rel->r_offset,
  1239. relocation, rel->r_addend,
  1240. (struct elf_link_hash_entry *) h,
  1241. r_symndx,
  1242. info, sec, h == NULL);
  1243. if (r != bfd_reloc_ok)
  1244. {
  1245. const char *name;
  1246. const char *msg = NULL;
  1247. if (h != NULL)
  1248. name = h->root.root.string;
  1249. else
  1250. {
  1251. name = (bfd_elf_string_from_elf_section
  1252. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  1253. if (name == NULL || *name == '\0')
  1254. name = bfd_section_name (input_bfd, sec);
  1255. }
  1256. switch (r)
  1257. {
  1258. case bfd_reloc_overflow:
  1259. (*info->callbacks->reloc_overflow)
  1260. (info, (h ? &h->root : NULL), name, howto->name,
  1261. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  1262. break;
  1263. case bfd_reloc_undefined:
  1264. (*info->callbacks->undefined_symbol)
  1265. (info, name, input_bfd, input_section, rel->r_offset, TRUE);
  1266. break;
  1267. case bfd_reloc_outofrange:
  1268. msg = _("internal error: out of range error");
  1269. goto common_error;
  1270. case bfd_reloc_notsupported:
  1271. msg = _("internal error: unsupported relocation error");
  1272. goto common_error;
  1273. case bfd_reloc_dangerous:
  1274. msg = _("internal error: dangerous error");
  1275. goto common_error;
  1276. default:
  1277. msg = _("internal error: unknown error");
  1278. /* Fall through. */
  1279. common_error:
  1280. (*info->callbacks->warning) (info, msg, name, input_bfd,
  1281. input_section, rel->r_offset);
  1282. break;
  1283. }
  1284. }
  1285. }
  1286. return TRUE;
  1287. }
  1288. /* This is a version of bfd_generic_get_relocated_section_contents
  1289. which uses elf32_cr16_relocate_section. */
  1290. static bfd_byte *
  1291. elf32_cr16_get_relocated_section_contents (bfd *output_bfd,
  1292. struct bfd_link_info *link_info,
  1293. struct bfd_link_order *link_order,
  1294. bfd_byte *data,
  1295. bfd_boolean relocatable,
  1296. asymbol **symbols)
  1297. {
  1298. Elf_Internal_Shdr *symtab_hdr;
  1299. asection *input_section = link_order->u.indirect.section;
  1300. bfd *input_bfd = input_section->owner;
  1301. asection **sections = NULL;
  1302. Elf_Internal_Rela *internal_relocs = NULL;
  1303. Elf_Internal_Sym *isymbuf = NULL;
  1304. /* We only need to handle the case of relaxing, or of having a
  1305. particular set of section contents, specially. */
  1306. if (relocatable
  1307. || elf_section_data (input_section)->this_hdr.contents == NULL)
  1308. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  1309. link_order, data,
  1310. relocatable,
  1311. symbols);
  1312. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1313. memcpy (data, elf_section_data (input_section)->this_hdr.contents,
  1314. (size_t) input_section->size);
  1315. if ((input_section->flags & SEC_RELOC) != 0
  1316. && input_section->reloc_count > 0)
  1317. {
  1318. Elf_Internal_Sym *isym;
  1319. Elf_Internal_Sym *isymend;
  1320. asection **secpp;
  1321. bfd_size_type amt;
  1322. internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
  1323. NULL, NULL, FALSE);
  1324. if (internal_relocs == NULL)
  1325. goto error_return;
  1326. if (symtab_hdr->sh_info != 0)
  1327. {
  1328. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1329. if (isymbuf == NULL)
  1330. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  1331. symtab_hdr->sh_info, 0,
  1332. NULL, NULL, NULL);
  1333. if (isymbuf == NULL)
  1334. goto error_return;
  1335. }
  1336. amt = symtab_hdr->sh_info;
  1337. amt *= sizeof (asection *);
  1338. sections = bfd_malloc (amt);
  1339. if (sections == NULL && amt != 0)
  1340. goto error_return;
  1341. isymend = isymbuf + symtab_hdr->sh_info;
  1342. for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
  1343. {
  1344. asection *isec;
  1345. if (isym->st_shndx == SHN_UNDEF)
  1346. isec = bfd_und_section_ptr;
  1347. else if (isym->st_shndx == SHN_ABS)
  1348. isec = bfd_abs_section_ptr;
  1349. else if (isym->st_shndx == SHN_COMMON)
  1350. isec = bfd_com_section_ptr;
  1351. else
  1352. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  1353. *secpp = isec;
  1354. }
  1355. if (! elf32_cr16_relocate_section (output_bfd, link_info, input_bfd,
  1356. input_section, data, internal_relocs,
  1357. isymbuf, sections))
  1358. goto error_return;
  1359. if (sections != NULL)
  1360. free (sections);
  1361. if (isymbuf != NULL
  1362. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1363. free (isymbuf);
  1364. if (elf_section_data (input_section)->relocs != internal_relocs)
  1365. free (internal_relocs);
  1366. }
  1367. return data;
  1368. error_return:
  1369. if (sections != NULL)
  1370. free (sections);
  1371. if (isymbuf != NULL
  1372. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1373. free (isymbuf);
  1374. if (internal_relocs != NULL
  1375. && elf_section_data (input_section)->relocs != internal_relocs)
  1376. free (internal_relocs);
  1377. return NULL;
  1378. }
  1379. /* Assorted hash table functions. */
  1380. /* Initialize an entry in the link hash table. */
  1381. /* Create an entry in an CR16 ELF linker hash table. */
  1382. static struct bfd_hash_entry *
  1383. elf32_cr16_link_hash_newfunc (struct bfd_hash_entry *entry,
  1384. struct bfd_hash_table *table,
  1385. const char *string)
  1386. {
  1387. struct elf32_cr16_link_hash_entry *ret =
  1388. (struct elf32_cr16_link_hash_entry *) entry;
  1389. /* Allocate the structure if it has not already been allocated by a
  1390. subclass. */
  1391. if (ret == (struct elf32_cr16_link_hash_entry *) NULL)
  1392. ret = ((struct elf32_cr16_link_hash_entry *)
  1393. bfd_hash_allocate (table,
  1394. sizeof (struct elf32_cr16_link_hash_entry)));
  1395. if (ret == (struct elf32_cr16_link_hash_entry *) NULL)
  1396. return (struct bfd_hash_entry *) ret;
  1397. /* Call the allocation method of the superclass. */
  1398. ret = ((struct elf32_cr16_link_hash_entry *)
  1399. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  1400. table, string));
  1401. if (ret != (struct elf32_cr16_link_hash_entry *) NULL)
  1402. {
  1403. ret->direct_calls = 0;
  1404. ret->stack_size = 0;
  1405. ret->movm_args = 0;
  1406. ret->movm_stack_size = 0;
  1407. ret->flags = 0;
  1408. ret->value = 0;
  1409. }
  1410. return (struct bfd_hash_entry *) ret;
  1411. }
  1412. /* Create an cr16 ELF linker hash table. */
  1413. static struct bfd_link_hash_table *
  1414. elf32_cr16_link_hash_table_create (bfd *abfd)
  1415. {
  1416. struct elf_link_hash_table *ret;
  1417. bfd_size_type amt = sizeof (struct elf_link_hash_table);
  1418. ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
  1419. if (ret == (struct elf_link_hash_table *) NULL)
  1420. return NULL;
  1421. if (!_bfd_elf_link_hash_table_init (ret, abfd,
  1422. elf32_cr16_link_hash_newfunc,
  1423. sizeof (struct elf32_cr16_link_hash_entry),
  1424. GENERIC_ELF_DATA))
  1425. {
  1426. free (ret);
  1427. return NULL;
  1428. }
  1429. return &ret->root;
  1430. }
  1431. static unsigned long
  1432. elf_cr16_mach (flagword flags)
  1433. {
  1434. switch (flags)
  1435. {
  1436. case EM_CR16:
  1437. default:
  1438. return bfd_mach_cr16;
  1439. }
  1440. }
  1441. /* The final processing done just before writing out a CR16 ELF object
  1442. file. This gets the CR16 architecture right based on the machine
  1443. number. */
  1444. static void
  1445. _bfd_cr16_elf_final_write_processing (bfd *abfd,
  1446. bfd_boolean linker ATTRIBUTE_UNUSED)
  1447. {
  1448. unsigned long val;
  1449. switch (bfd_get_mach (abfd))
  1450. {
  1451. default:
  1452. case bfd_mach_cr16:
  1453. val = EM_CR16;
  1454. break;
  1455. }
  1456. elf_elfheader (abfd)->e_flags |= val;
  1457. }
  1458. static bfd_boolean
  1459. _bfd_cr16_elf_object_p (bfd *abfd)
  1460. {
  1461. bfd_default_set_arch_mach (abfd, bfd_arch_cr16,
  1462. elf_cr16_mach (elf_elfheader (abfd)->e_flags));
  1463. return TRUE;
  1464. }
  1465. /* Merge backend specific data from an object file to the output
  1466. object file when linking. */
  1467. static bfd_boolean
  1468. _bfd_cr16_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  1469. {
  1470. bfd *obfd = info->output_bfd;
  1471. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  1472. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1473. return TRUE;
  1474. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  1475. && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
  1476. {
  1477. if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  1478. bfd_get_mach (ibfd)))
  1479. return FALSE;
  1480. }
  1481. return TRUE;
  1482. }
  1483. /* This function handles relaxing for the CR16.
  1484. There's quite a few relaxing opportunites available on the CR16:
  1485. * bcond:24 -> bcond:16 1 byte
  1486. * bcond:16 -> bcond:8 1 byte
  1487. * arithmetic imm32 -> arithmetic imm20 12 bits
  1488. * arithmetic imm20/imm16 -> arithmetic imm4 12/16 bits
  1489. Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
  1490. static bfd_boolean
  1491. elf32_cr16_relax_section (bfd *abfd, asection *sec,
  1492. struct bfd_link_info *link_info, bfd_boolean *again)
  1493. {
  1494. Elf_Internal_Shdr *symtab_hdr;
  1495. Elf_Internal_Rela *internal_relocs;
  1496. Elf_Internal_Rela *irel, *irelend;
  1497. bfd_byte *contents = NULL;
  1498. Elf_Internal_Sym *isymbuf = NULL;
  1499. /* Assume nothing changes. */
  1500. *again = FALSE;
  1501. /* We don't have to do anything for a relocatable link, if
  1502. this section does not have relocs, or if this is not a
  1503. code section. */
  1504. if (bfd_link_relocatable (link_info)
  1505. || (sec->flags & SEC_RELOC) == 0
  1506. || sec->reloc_count == 0
  1507. || (sec->flags & SEC_CODE) == 0)
  1508. return TRUE;
  1509. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1510. /* Get a copy of the native relocations. */
  1511. internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  1512. link_info->keep_memory);
  1513. if (internal_relocs == NULL)
  1514. goto error_return;
  1515. /* Walk through them looking for relaxing opportunities. */
  1516. irelend = internal_relocs + sec->reloc_count;
  1517. for (irel = internal_relocs; irel < irelend; irel++)
  1518. {
  1519. bfd_vma symval;
  1520. /* If this isn't something that can be relaxed, then ignore
  1521. this reloc. */
  1522. if (ELF32_R_TYPE (irel->r_info) != (int) R_CR16_DISP16
  1523. && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_DISP24
  1524. && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_IMM32
  1525. && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_IMM20
  1526. && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_IMM16)
  1527. continue;
  1528. /* Get the section contents if we haven't done so already. */
  1529. if (contents == NULL)
  1530. {
  1531. /* Get cached copy if it exists. */
  1532. if (elf_section_data (sec)->this_hdr.contents != NULL)
  1533. contents = elf_section_data (sec)->this_hdr.contents;
  1534. /* Go get them off disk. */
  1535. else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  1536. goto error_return;
  1537. }
  1538. /* Read this BFD's local symbols if we haven't done so already. */
  1539. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  1540. {
  1541. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1542. if (isymbuf == NULL)
  1543. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  1544. symtab_hdr->sh_info, 0,
  1545. NULL, NULL, NULL);
  1546. if (isymbuf == NULL)
  1547. goto error_return;
  1548. }
  1549. /* Get the value of the symbol referred to by the reloc. */
  1550. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  1551. {
  1552. /* A local symbol. */
  1553. Elf_Internal_Sym *isym;
  1554. asection *sym_sec;
  1555. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  1556. if (isym->st_shndx == SHN_UNDEF)
  1557. sym_sec = bfd_und_section_ptr;
  1558. else if (isym->st_shndx == SHN_ABS)
  1559. sym_sec = bfd_abs_section_ptr;
  1560. else if (isym->st_shndx == SHN_COMMON)
  1561. sym_sec = bfd_com_section_ptr;
  1562. else
  1563. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1564. symval = (isym->st_value
  1565. + sym_sec->output_section->vma
  1566. + sym_sec->output_offset);
  1567. }
  1568. else
  1569. {
  1570. unsigned long indx;
  1571. struct elf_link_hash_entry *h;
  1572. /* An external symbol. */
  1573. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  1574. h = elf_sym_hashes (abfd)[indx];
  1575. BFD_ASSERT (h != NULL);
  1576. if (h->root.type != bfd_link_hash_defined
  1577. && h->root.type != bfd_link_hash_defweak)
  1578. /* This appears to be a reference to an undefined
  1579. symbol. Just ignore it--it will be caught by the
  1580. regular reloc processing. */
  1581. continue;
  1582. symval = (h->root.u.def.value
  1583. + h->root.u.def.section->output_section->vma
  1584. + h->root.u.def.section->output_offset);
  1585. }
  1586. /* For simplicity of coding, we are going to modify the section
  1587. contents, the section relocs, and the BFD symbol table. We
  1588. must tell the rest of the code not to free up this
  1589. information. It would be possible to instead create a table
  1590. of changes which have to be made, as is done in coff-mips.c;
  1591. that would be more work, but would require less memory when
  1592. the linker is run. */
  1593. /* Try to turn a 24 branch/call into a 16bit relative
  1594. branch/call. */
  1595. if (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_DISP24)
  1596. {
  1597. bfd_vma value = symval;
  1598. /* Deal with pc-relative gunk. */
  1599. value -= (sec->output_section->vma + sec->output_offset);
  1600. value -= irel->r_offset;
  1601. value += irel->r_addend;
  1602. /* See if the value will fit in 16 bits, note the high value is
  1603. 0xfffe + 2 as the target will be two bytes closer if we are
  1604. able to relax. */
  1605. if ((long) value < 0x10000 && (long) value > -0x10002)
  1606. {
  1607. unsigned int code;
  1608. /* Get the opcode. */
  1609. code = (unsigned int) bfd_get_32 (abfd, contents + irel->r_offset);
  1610. /* Verify it's a 'bcond' and fix the opcode. */
  1611. if ((code & 0xffff) == 0x0010)
  1612. bfd_put_16 (abfd, 0x1800 | ((0xf & (code >> 20)) << 4), contents + irel->r_offset);
  1613. else
  1614. continue;
  1615. /* Note that we've changed the relocs, section contents, etc. */
  1616. elf_section_data (sec)->relocs = internal_relocs;
  1617. elf_section_data (sec)->this_hdr.contents = contents;
  1618. symtab_hdr->contents = (unsigned char *) isymbuf;
  1619. /* Fix the relocation's type. */
  1620. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1621. R_CR16_DISP16);
  1622. /* Delete two bytes of data. */
  1623. if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
  1624. irel->r_offset + 2, 2))
  1625. goto error_return;
  1626. /* That will change things, so, we should relax again.
  1627. Note that this is not required, and it may be slow. */
  1628. *again = TRUE;
  1629. }
  1630. }
  1631. /* Try to turn a 16bit pc-relative branch into an
  1632. 8bit pc-relative branch. */
  1633. if (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_DISP16)
  1634. {
  1635. bfd_vma value = symval;
  1636. /* Deal with pc-relative gunk. */
  1637. value -= (sec->output_section->vma + sec->output_offset);
  1638. value -= irel->r_offset;
  1639. value += irel->r_addend;
  1640. /* See if the value will fit in 8 bits, note the high value is
  1641. 0xfc + 2 as the target will be two bytes closer if we are
  1642. able to relax. */
  1643. /*if ((long) value < 0x1fa && (long) value > -0x100) REVISIT:range */
  1644. if ((long) value < 0xfa && (long) value > -0x100)
  1645. {
  1646. unsigned short code;
  1647. /* Get the opcode. */
  1648. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1649. /* Verify it's a 'bcond' and fix the opcode. */
  1650. if ((code & 0xff0f) == 0x1800)
  1651. bfd_put_16 (abfd, (code & 0xf0f0), contents + irel->r_offset);
  1652. else
  1653. continue;
  1654. /* Note that we've changed the relocs, section contents, etc. */
  1655. elf_section_data (sec)->relocs = internal_relocs;
  1656. elf_section_data (sec)->this_hdr.contents = contents;
  1657. symtab_hdr->contents = (unsigned char *) isymbuf;
  1658. /* Fix the relocation's type. */
  1659. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1660. R_CR16_DISP8);
  1661. /* Delete two bytes of data. */
  1662. if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
  1663. irel->r_offset + 2, 2))
  1664. goto error_return;
  1665. /* That will change things, so, we should relax again.
  1666. Note that this is not required, and it may be slow. */
  1667. *again = TRUE;
  1668. }
  1669. }
  1670. /* Try to turn a 32-bit IMM address into a 20/16-bit IMM address */
  1671. if (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_IMM32)
  1672. {
  1673. bfd_vma value = symval;
  1674. unsigned short is_add_mov = 0;
  1675. bfd_vma value1 = 0;
  1676. /* Get the existing value from the mcode */
  1677. value1 = ((bfd_get_32 (abfd, contents + irel->r_offset + 2) >> 16)
  1678. |(((bfd_get_32 (abfd, contents + irel->r_offset + 2) & 0xffff) << 16)));
  1679. /* See if the value will fit in 20 bits. */
  1680. if ((long) (value + value1) < 0xfffff && (long) (value + value1) > 0)
  1681. {
  1682. unsigned short code;
  1683. /* Get the opcode. */
  1684. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1685. /* Verify it's a 'arithmetic ADDD or MOVD instruction'.
  1686. For ADDD and MOVD only, convert to IMM32 -> IMM20. */
  1687. if (((code & 0xfff0) == 0x0070) || ((code & 0xfff0) == 0x0020))
  1688. is_add_mov = 1;
  1689. if (is_add_mov)
  1690. {
  1691. /* Note that we've changed the relocs, section contents,
  1692. etc. */
  1693. elf_section_data (sec)->relocs = internal_relocs;
  1694. elf_section_data (sec)->this_hdr.contents = contents;
  1695. symtab_hdr->contents = (unsigned char *) isymbuf;
  1696. /* Fix the opcode. */
  1697. if ((code & 0xfff0) == 0x0070) /* For movd. */
  1698. bfd_put_8 (abfd, 0x05, contents + irel->r_offset + 1);
  1699. else /* code == 0x0020 for addd. */
  1700. bfd_put_8 (abfd, 0x04, contents + irel->r_offset + 1);
  1701. bfd_put_8 (abfd, (code & 0xf) << 4, contents + irel->r_offset);
  1702. /* If existing value is nagavive adjust approriately
  1703. place the 16-20bits (ie 4 bit) in new opcode,
  1704. as the 0xffffxxxx, the higher 2 byte values removed. */
  1705. if (value1 & 0x80000000)
  1706. bfd_put_8 (abfd, (0x0f | (bfd_get_8(abfd, contents + irel->r_offset))), contents + irel->r_offset);
  1707. else
  1708. bfd_put_8 (abfd, (((value1 >> 16)&0xf) | (bfd_get_8(abfd, contents + irel->r_offset))), contents + irel->r_offset);
  1709. /* Fix the relocation's type. */
  1710. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1711. R_CR16_IMM20);
  1712. /* Delete two bytes of data. */
  1713. if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
  1714. irel->r_offset + 2, 2))
  1715. goto error_return;
  1716. /* That will change things, so, we should relax again.
  1717. Note that this is not required, and it may be slow. */
  1718. *again = TRUE;
  1719. }
  1720. }
  1721. /* See if the value will fit in 16 bits. */
  1722. if ((!is_add_mov)
  1723. && ((long)(value + value1) < 0x7fff && (long)(value + value1) > 0))
  1724. {
  1725. unsigned short code;
  1726. /* Get the opcode. */
  1727. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1728. /* Note that we've changed the relocs, section contents, etc. */
  1729. elf_section_data (sec)->relocs = internal_relocs;
  1730. elf_section_data (sec)->this_hdr.contents = contents;
  1731. symtab_hdr->contents = (unsigned char *) isymbuf;
  1732. /* Fix the opcode. */
  1733. if ((code & 0xf0) == 0x70) /* For movd. */
  1734. bfd_put_8 (abfd, 0x54, contents + irel->r_offset + 1);
  1735. else if ((code & 0xf0) == 0x20) /* For addd. */
  1736. bfd_put_8 (abfd, 0x60, contents + irel->r_offset + 1);
  1737. else if ((code & 0xf0) == 0x90) /* For cmpd. */
  1738. bfd_put_8 (abfd, 0x56, contents + irel->r_offset + 1);
  1739. else
  1740. continue;
  1741. bfd_put_8 (abfd, 0xb0 | (code & 0xf), contents + irel->r_offset);
  1742. /* If existing value is nagavive adjust approriately
  1743. place the 12-16bits (ie 4 bit) in new opcode,
  1744. as the 0xfffffxxx, the higher 2 byte values removed. */
  1745. if (value1 & 0x80000000)
  1746. bfd_put_8 (abfd, (0x0f | (bfd_get_8(abfd, contents + irel->r_offset))), contents + irel->r_offset);
  1747. else
  1748. bfd_put_16 (abfd, value1, contents + irel->r_offset + 2);
  1749. /* Fix the relocation's type. */
  1750. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1751. R_CR16_IMM16);
  1752. /* Delete two bytes of data. */
  1753. if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
  1754. irel->r_offset + 2, 2))
  1755. goto error_return;
  1756. /* That will change things, so, we should relax again.
  1757. Note that this is not required, and it may be slow. */
  1758. *again = TRUE;
  1759. }
  1760. }
  1761. #if 0
  1762. /* Try to turn a 16bit immediate address into a 4bit
  1763. immediate address. */
  1764. if ((ELF32_R_TYPE (irel->r_info) == (int) R_CR16_IMM20)
  1765. || (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_IMM16))
  1766. {
  1767. bfd_vma value = symval;
  1768. bfd_vma value1 = 0;
  1769. /* Get the existing value from the mcode */
  1770. value1 = ((bfd_get_16 (abfd, contents + irel->r_offset + 2) & 0xffff));
  1771. if (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_IMM20)
  1772. {
  1773. value1 |= ((bfd_get_16 (abfd, contents + irel->r_offset + 1) & 0xf000) << 0x4);
  1774. }
  1775. /* See if the value will fit in 4 bits. */
  1776. if ((((long) (value + value1)) < 0xf)
  1777. && (((long) (value + value1)) > 0))
  1778. {
  1779. unsigned short code;
  1780. /* Get the opcode. */
  1781. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1782. /* Note that we've changed the relocs, section contents, etc. */
  1783. elf_section_data (sec)->relocs = internal_relocs;
  1784. elf_section_data (sec)->this_hdr.contents = contents;
  1785. symtab_hdr->contents = (unsigned char *) isymbuf;
  1786. /* Fix the opcode. */
  1787. if (((code & 0x0f00) == 0x0400) || ((code & 0x0f00) == 0x0500))
  1788. {
  1789. if ((code & 0x0f00) == 0x0400) /* For movd imm20. */
  1790. bfd_put_8 (abfd, 0x60, contents + irel->r_offset);
  1791. else /* For addd imm20. */
  1792. bfd_put_8 (abfd, 0x54, contents + irel->r_offset);
  1793. bfd_put_8 (abfd, (code & 0xf0) >> 4, contents + irel->r_offset + 1);
  1794. }
  1795. else
  1796. {
  1797. if ((code & 0xfff0) == 0x56b0) /* For cmpd imm16. */
  1798. bfd_put_8 (abfd, 0x56, contents + irel->r_offset);
  1799. else if ((code & 0xfff0) == 0x54b0) /* For movd imm16. */
  1800. bfd_put_8 (abfd, 0x54, contents + irel->r_offset);
  1801. else if ((code & 0xfff0) == 0x58b0) /* For movb imm16. */
  1802. bfd_put_8 (abfd, 0x58, contents + irel->r_offset);
  1803. else if ((code & 0xfff0) == 0x5Ab0) /* For movw imm16. */
  1804. bfd_put_8 (abfd, 0x5A, contents + irel->r_offset);
  1805. else if ((code & 0xfff0) == 0x60b0) /* For addd imm16. */
  1806. bfd_put_8 (abfd, 0x60, contents + irel->r_offset);
  1807. else if ((code & 0xfff0) == 0x30b0) /* For addb imm16. */
  1808. bfd_put_8 (abfd, 0x30, contents + irel->r_offset);
  1809. else if ((code & 0xfff0) == 0x2Cb0) /* For addub imm16. */
  1810. bfd_put_8 (abfd, 0x2C, contents + irel->r_offset);
  1811. else if ((code & 0xfff0) == 0x32b0) /* For adduw imm16. */
  1812. bfd_put_8 (abfd, 0x32, contents + irel->r_offset);
  1813. else if ((code & 0xfff0) == 0x38b0) /* For subb imm16. */
  1814. bfd_put_8 (abfd, 0x38, contents + irel->r_offset);
  1815. else if ((code & 0xfff0) == 0x3Cb0) /* For subcb imm16. */
  1816. bfd_put_8 (abfd, 0x3C, contents + irel->r_offset);
  1817. else if ((code & 0xfff0) == 0x3Fb0) /* For subcw imm16. */
  1818. bfd_put_8 (abfd, 0x3F, contents + irel->r_offset);
  1819. else if ((code & 0xfff0) == 0x3Ab0) /* For subw imm16. */
  1820. bfd_put_8 (abfd, 0x3A, contents + irel->r_offset);
  1821. else if ((code & 0xfff0) == 0x50b0) /* For cmpb imm16. */
  1822. bfd_put_8 (abfd, 0x50, contents + irel->r_offset);
  1823. else if ((code & 0xfff0) == 0x52b0) /* For cmpw imm16. */
  1824. bfd_put_8 (abfd, 0x52, contents + irel->r_offset);
  1825. else
  1826. continue;
  1827. bfd_put_8 (abfd, (code & 0xf), contents + irel->r_offset + 1);
  1828. }
  1829. /* Fix the relocation's type. */
  1830. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1831. R_CR16_IMM4);
  1832. /* Delete two bytes of data. */
  1833. if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
  1834. irel->r_offset + 2, 2))
  1835. goto error_return;
  1836. /* That will change things, so, we should relax again.
  1837. Note that this is not required, and it may be slow. */
  1838. *again = TRUE;
  1839. }
  1840. }
  1841. #endif
  1842. }
  1843. if (isymbuf != NULL
  1844. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1845. {
  1846. if (! link_info->keep_memory)
  1847. free (isymbuf);
  1848. else
  1849. /* Cache the symbols for elf_link_input_bfd. */
  1850. symtab_hdr->contents = (unsigned char *) isymbuf;
  1851. }
  1852. if (contents != NULL
  1853. && elf_section_data (sec)->this_hdr.contents != contents)
  1854. {
  1855. if (! link_info->keep_memory)
  1856. free (contents);
  1857. else
  1858. /* Cache the section contents for elf_link_input_bfd. */
  1859. elf_section_data (sec)->this_hdr.contents = contents;
  1860. }
  1861. if (internal_relocs != NULL
  1862. && elf_section_data (sec)->relocs != internal_relocs)
  1863. free (internal_relocs);
  1864. return TRUE;
  1865. error_return:
  1866. if (isymbuf != NULL
  1867. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1868. free (isymbuf);
  1869. if (contents != NULL
  1870. && elf_section_data (sec)->this_hdr.contents != contents)
  1871. free (contents);
  1872. if (internal_relocs != NULL
  1873. && elf_section_data (sec)->relocs != internal_relocs)
  1874. free (internal_relocs);
  1875. return FALSE;
  1876. }
  1877. static asection *
  1878. elf32_cr16_gc_mark_hook (asection *sec,
  1879. struct bfd_link_info *info,
  1880. Elf_Internal_Rela *rel,
  1881. struct elf_link_hash_entry *h,
  1882. Elf_Internal_Sym *sym)
  1883. {
  1884. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1885. }
  1886. /* Update the got entry reference counts for the section being removed. */
  1887. static bfd_boolean
  1888. elf32_cr16_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
  1889. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1890. asection *sec ATTRIBUTE_UNUSED,
  1891. const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
  1892. {
  1893. /* We don't support garbage collection of GOT and PLT relocs yet. */
  1894. return TRUE;
  1895. }
  1896. /* Create dynamic sections when linking against a dynamic object. */
  1897. static bfd_boolean
  1898. _bfd_cr16_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  1899. {
  1900. flagword flags;
  1901. asection * s;
  1902. const struct elf_backend_data * bed = get_elf_backend_data (abfd);
  1903. struct elf_link_hash_table *htab = elf_hash_table (info);
  1904. int ptralign = 0;
  1905. switch (bed->s->arch_size)
  1906. {
  1907. case 16:
  1908. ptralign = 1;
  1909. break;
  1910. case 32:
  1911. ptralign = 2;
  1912. break;
  1913. default:
  1914. bfd_set_error (bfd_error_bad_value);
  1915. return FALSE;
  1916. }
  1917. /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  1918. .rel[a].bss sections. */
  1919. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1920. | SEC_LINKER_CREATED);
  1921. s = bfd_make_section_anyway_with_flags (abfd,
  1922. (bed->default_use_rela_p
  1923. ? ".rela.plt" : ".rel.plt"),
  1924. flags | SEC_READONLY);
  1925. htab->srelplt = s;
  1926. if (s == NULL
  1927. || ! bfd_set_section_alignment (abfd, s, ptralign))
  1928. return FALSE;
  1929. if (! _bfd_cr16_elf_create_got_section (abfd, info))
  1930. return FALSE;
  1931. if (bed->want_dynbss)
  1932. {
  1933. /* The .dynbss section is a place to put symbols which are defined
  1934. by dynamic objects, are referenced by regular objects, and are
  1935. not functions. We must allocate space for them in the process
  1936. image and use a R_*_COPY reloc to tell the dynamic linker to
  1937. initialize them at run time. The linker script puts the .dynbss
  1938. section into the .bss section of the final image. */
  1939. s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
  1940. SEC_ALLOC | SEC_LINKER_CREATED);
  1941. if (s == NULL)
  1942. return FALSE;
  1943. /* The .rel[a].bss section holds copy relocs. This section is not
  1944. normally needed. We need to create it here, though, so that the
  1945. linker will map it to an output section. We can't just create it
  1946. only if we need it, because we will not know whether we need it
  1947. until we have seen all the input files, and the first time the
  1948. main linker code calls BFD after examining all the input files
  1949. (size_dynamic_sections) the input sections have already been
  1950. mapped to the output sections. If the section turns out not to
  1951. be needed, we can discard it later. We will never need this
  1952. section when generating a shared object, since they do not use
  1953. copy relocs. */
  1954. if (! bfd_link_executable (info))
  1955. {
  1956. s = bfd_make_section_anyway_with_flags (abfd,
  1957. (bed->default_use_rela_p
  1958. ? ".rela.bss" : ".rel.bss"),
  1959. flags | SEC_READONLY);
  1960. if (s == NULL
  1961. || ! bfd_set_section_alignment (abfd, s, ptralign))
  1962. return FALSE;
  1963. }
  1964. }
  1965. return TRUE;
  1966. }
  1967. /* Adjust a symbol defined by a dynamic object and referenced by a
  1968. regular object. The current definition is in some section of the
  1969. dynamic object, but we're not including those sections. We have to
  1970. change the definition to something the rest of the link can
  1971. understand. */
  1972. static bfd_boolean
  1973. _bfd_cr16_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
  1974. struct elf_link_hash_entry * h)
  1975. {
  1976. bfd * dynobj;
  1977. asection * s;
  1978. dynobj = elf_hash_table (info)->dynobj;
  1979. /* Make sure we know what is going on here. */
  1980. BFD_ASSERT (dynobj != NULL
  1981. && (h->needs_plt
  1982. || h->u.weakdef != NULL
  1983. || (h->def_dynamic
  1984. && h->ref_regular
  1985. && !h->def_regular)));
  1986. /* If this is a function, put it in the procedure linkage table. We
  1987. will fill in the contents of the procedure linkage table later,
  1988. when we know the address of the .got section. */
  1989. if (h->type == STT_FUNC
  1990. || h->needs_plt)
  1991. {
  1992. if (! bfd_link_executable (info)
  1993. && !h->def_dynamic
  1994. && !h->ref_dynamic)
  1995. {
  1996. /* This case can occur if we saw a PLT reloc in an input
  1997. file, but the symbol was never referred to by a dynamic
  1998. object. In such a case, we don't actually need to build
  1999. a procedure linkage table, and we can just do a REL32
  2000. reloc instead. */
  2001. BFD_ASSERT (h->needs_plt);
  2002. return TRUE;
  2003. }
  2004. /* Make sure this symbol is output as a dynamic symbol. */
  2005. if (h->dynindx == -1)
  2006. {
  2007. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2008. return FALSE;
  2009. }
  2010. /* We also need to make an entry in the .got.plt section, which
  2011. will be placed in the .got section by the linker script. */
  2012. s = elf_hash_table (info)->sgotplt;
  2013. BFD_ASSERT (s != NULL);
  2014. s->size += 4;
  2015. /* We also need to make an entry in the .rela.plt section. */
  2016. s = elf_hash_table (info)->srelplt;
  2017. BFD_ASSERT (s != NULL);
  2018. s->size += sizeof (Elf32_External_Rela);
  2019. return TRUE;
  2020. }
  2021. /* If this is a weak symbol, and there is a real definition, the
  2022. processor independent code will have arranged for us to see the
  2023. real definition first, and we can just use the same value. */
  2024. if (h->u.weakdef != NULL)
  2025. {
  2026. BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
  2027. || h->u.weakdef->root.type == bfd_link_hash_defweak);
  2028. h->root.u.def.section = h->u.weakdef->root.u.def.section;
  2029. h->root.u.def.value = h->u.weakdef->root.u.def.value;
  2030. return TRUE;
  2031. }
  2032. /* This is a reference to a symbol defined by a dynamic object which
  2033. is not a function. */
  2034. /* If we are creating a shared library, we must presume that the
  2035. only references to the symbol are via the global offset table.
  2036. For such cases we need not do anything here; the relocations will
  2037. be handled correctly by relocate_section. */
  2038. if (bfd_link_executable (info))
  2039. return TRUE;
  2040. /* If there are no references to this symbol that do not use the
  2041. GOT, we don't need to generate a copy reloc. */
  2042. if (!h->non_got_ref)
  2043. return TRUE;
  2044. /* We must allocate the symbol in our .dynbss section, which will
  2045. become part of the .bss section of the executable. There will be
  2046. an entry for this symbol in the .dynsym section. The dynamic
  2047. object will contain position independent code, so all references
  2048. from the dynamic object to this symbol will go through the global
  2049. offset table. The dynamic linker will use the .dynsym entry to
  2050. determine the address it must put in the global offset table, so
  2051. both the dynamic object and the regular object will refer to the
  2052. same memory location for the variable. */
  2053. s = bfd_get_linker_section (dynobj, ".dynbss");
  2054. BFD_ASSERT (s != NULL);
  2055. /* We must generate a R_CR16_COPY reloc to tell the dynamic linker to
  2056. copy the initial value out of the dynamic object and into the
  2057. runtime process image. We need to remember the offset into the
  2058. .rela.bss section we are going to use. */
  2059. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  2060. {
  2061. asection * srel;
  2062. srel = bfd_get_linker_section (dynobj, ".rela.bss");
  2063. BFD_ASSERT (srel != NULL);
  2064. srel->size += sizeof (Elf32_External_Rela);
  2065. h->needs_copy = 1;
  2066. }
  2067. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  2068. }
  2069. /* Set the sizes of the dynamic sections. */
  2070. static bfd_boolean
  2071. _bfd_cr16_elf_size_dynamic_sections (bfd * output_bfd,
  2072. struct bfd_link_info * info)
  2073. {
  2074. bfd * dynobj;
  2075. asection * s;
  2076. bfd_boolean plt;
  2077. bfd_boolean relocs;
  2078. bfd_boolean reltext;
  2079. dynobj = elf_hash_table (info)->dynobj;
  2080. BFD_ASSERT (dynobj != NULL);
  2081. if (elf_hash_table (info)->dynamic_sections_created)
  2082. {
  2083. /* Set the contents of the .interp section to the interpreter. */
  2084. if (bfd_link_executable (info) && !info->nointerp)
  2085. {
  2086. #if 0
  2087. s = bfd_get_linker_section (dynobj, ".interp");
  2088. BFD_ASSERT (s != NULL);
  2089. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  2090. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  2091. #endif
  2092. }
  2093. }
  2094. else
  2095. {
  2096. /* We may have created entries in the .rela.got section.
  2097. However, if we are not creating the dynamic sections, we will
  2098. not actually use these entries. Reset the size of .rela.got,
  2099. which will cause it to get stripped from the output file
  2100. below. */
  2101. s = elf_hash_table (info)->srelgot;
  2102. if (s != NULL)
  2103. s->size = 0;
  2104. }
  2105. /* The check_relocs and adjust_dynamic_symbol entry points have
  2106. determined the sizes of the various dynamic sections. Allocate
  2107. memory for them. */
  2108. plt = FALSE;
  2109. relocs = FALSE;
  2110. reltext = FALSE;
  2111. for (s = dynobj->sections; s != NULL; s = s->next)
  2112. {
  2113. const char * name;
  2114. if ((s->flags & SEC_LINKER_CREATED) == 0)
  2115. continue;
  2116. /* It's OK to base decisions on the section name, because none
  2117. of the dynobj section names depend upon the input files. */
  2118. name = bfd_get_section_name (dynobj, s);
  2119. if (strcmp (name, ".plt") == 0)
  2120. {
  2121. /* Remember whether there is a PLT. */
  2122. plt = s->size != 0;
  2123. }
  2124. else if (CONST_STRNEQ (name, ".rela"))
  2125. {
  2126. if (s->size != 0)
  2127. {
  2128. asection * target;
  2129. /* Remember whether there are any reloc sections other
  2130. than .rela.plt. */
  2131. if (strcmp (name, ".rela.plt") != 0)
  2132. {
  2133. const char * outname;
  2134. relocs = TRUE;
  2135. /* If this relocation section applies to a read only
  2136. section, then we probably need a DT_TEXTREL
  2137. entry. The entries in the .rela.plt section
  2138. really apply to the .got section, which we
  2139. created ourselves and so know is not readonly. */
  2140. outname = bfd_get_section_name (output_bfd,
  2141. s->output_section);
  2142. target = bfd_get_section_by_name (output_bfd, outname + 5);
  2143. if (target != NULL
  2144. && (target->flags & SEC_READONLY) != 0
  2145. && (target->flags & SEC_ALLOC) != 0)
  2146. reltext = TRUE;
  2147. }
  2148. /* We use the reloc_count field as a counter if we need
  2149. to copy relocs into the output file. */
  2150. s->reloc_count = 0;
  2151. }
  2152. }
  2153. else if (! CONST_STRNEQ (name, ".got")
  2154. && strcmp (name, ".dynbss") != 0)
  2155. /* It's not one of our sections, so don't allocate space. */
  2156. continue;
  2157. if (s->size == 0)
  2158. {
  2159. /* If we don't need this section, strip it from the
  2160. output file. This is mostly to handle .rela.bss and
  2161. .rela.plt. We must create both sections in
  2162. create_dynamic_sections, because they must be created
  2163. before the linker maps input sections to output
  2164. sections. The linker does that before
  2165. adjust_dynamic_symbol is called, and it is that
  2166. function which decides whether anything needs to go
  2167. into these sections. */
  2168. s->flags |= SEC_EXCLUDE;
  2169. continue;
  2170. }
  2171. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  2172. continue;
  2173. /* Allocate memory for the section contents. We use bfd_zalloc
  2174. here in case unused entries are not reclaimed before the
  2175. section's contents are written out. This should not happen,
  2176. but this way if it does, we get a R_CR16_NONE reloc
  2177. instead of garbage. */
  2178. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  2179. if (s->contents == NULL)
  2180. return FALSE;
  2181. }
  2182. if (elf_hash_table (info)->dynamic_sections_created)
  2183. {
  2184. /* Add some entries to the .dynamic section. We fill in the
  2185. values later, in _bfd_cr16_elf_finish_dynamic_sections,
  2186. but we must add the entries now so that we get the correct
  2187. size for the .dynamic section. The DT_DEBUG entry is filled
  2188. in by the dynamic linker and used by the debugger. */
  2189. if (! bfd_link_executable (info))
  2190. {
  2191. if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
  2192. return FALSE;
  2193. }
  2194. if (plt)
  2195. {
  2196. if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
  2197. || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  2198. || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
  2199. || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
  2200. return FALSE;
  2201. }
  2202. if (relocs)
  2203. {
  2204. if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
  2205. || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
  2206. || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
  2207. sizeof (Elf32_External_Rela)))
  2208. return FALSE;
  2209. }
  2210. if (reltext)
  2211. {
  2212. if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
  2213. return FALSE;
  2214. }
  2215. }
  2216. return TRUE;
  2217. }
  2218. /* Finish up dynamic symbol handling. We set the contents of various
  2219. dynamic sections here. */
  2220. static bfd_boolean
  2221. _bfd_cr16_elf_finish_dynamic_symbol (bfd * output_bfd,
  2222. struct bfd_link_info * info,
  2223. struct elf_link_hash_entry * h,
  2224. Elf_Internal_Sym * sym)
  2225. {
  2226. bfd * dynobj;
  2227. dynobj = elf_hash_table (info)->dynobj;
  2228. if (h->got.offset != (bfd_vma) -1)
  2229. {
  2230. asection * sgot;
  2231. asection * srel;
  2232. Elf_Internal_Rela rel;
  2233. /* This symbol has an entry in the global offset table. Set it up. */
  2234. sgot = elf_hash_table (info)->sgot;
  2235. srel = elf_hash_table (info)->srelgot;
  2236. BFD_ASSERT (sgot != NULL && srel != NULL);
  2237. rel.r_offset = (sgot->output_section->vma
  2238. + sgot->output_offset
  2239. + (h->got.offset & ~1));
  2240. /* If this is a -Bsymbolic link, and the symbol is defined
  2241. locally, we just want to emit a RELATIVE reloc. Likewise if
  2242. the symbol was forced to be local because of a version file.
  2243. The entry in the global offset table will already have been
  2244. initialized in the relocate_section function. */
  2245. if (bfd_link_executable (info)
  2246. && (info->symbolic || h->dynindx == -1)
  2247. && h->def_regular)
  2248. {
  2249. rel.r_info = ELF32_R_INFO (0, R_CR16_GOT_REGREL20);
  2250. rel.r_addend = (h->root.u.def.value
  2251. + h->root.u.def.section->output_section->vma
  2252. + h->root.u.def.section->output_offset);
  2253. }
  2254. else
  2255. {
  2256. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
  2257. rel.r_info = ELF32_R_INFO (h->dynindx, R_CR16_GOT_REGREL20);
  2258. rel.r_addend = 0;
  2259. }
  2260. bfd_elf32_swap_reloca_out (output_bfd, &rel,
  2261. (bfd_byte *) ((Elf32_External_Rela *) srel->contents
  2262. + srel->reloc_count));
  2263. ++ srel->reloc_count;
  2264. }
  2265. if (h->needs_copy)
  2266. {
  2267. asection * s;
  2268. Elf_Internal_Rela rel;
  2269. /* This symbol needs a copy reloc. Set it up. */
  2270. BFD_ASSERT (h->dynindx != -1
  2271. && (h->root.type == bfd_link_hash_defined
  2272. || h->root.type == bfd_link_hash_defweak));
  2273. s = bfd_get_linker_section (dynobj, ".rela.bss");
  2274. BFD_ASSERT (s != NULL);
  2275. rel.r_offset = (h->root.u.def.value
  2276. + h->root.u.def.section->output_section->vma
  2277. + h->root.u.def.section->output_offset);
  2278. rel.r_info = ELF32_R_INFO (h->dynindx, R_CR16_GOT_REGREL20);
  2279. rel.r_addend = 0;
  2280. bfd_elf32_swap_reloca_out (output_bfd, &rel,
  2281. (bfd_byte *) ((Elf32_External_Rela *) s->contents
  2282. + s->reloc_count));
  2283. ++ s->reloc_count;
  2284. }
  2285. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  2286. if (h == elf_hash_table (info)->hdynamic
  2287. || h == elf_hash_table (info)->hgot)
  2288. sym->st_shndx = SHN_ABS;
  2289. return TRUE;
  2290. }
  2291. /* Finish up the dynamic sections. */
  2292. static bfd_boolean
  2293. _bfd_cr16_elf_finish_dynamic_sections (bfd * output_bfd,
  2294. struct bfd_link_info * info)
  2295. {
  2296. bfd * dynobj;
  2297. asection * sgot;
  2298. asection * sdyn;
  2299. dynobj = elf_hash_table (info)->dynobj;
  2300. sgot = elf_hash_table (info)->sgotplt;
  2301. BFD_ASSERT (sgot != NULL);
  2302. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2303. if (elf_hash_table (info)->dynamic_sections_created)
  2304. {
  2305. Elf32_External_Dyn * dyncon;
  2306. Elf32_External_Dyn * dynconend;
  2307. BFD_ASSERT (sdyn != NULL);
  2308. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  2309. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  2310. for (; dyncon < dynconend; dyncon++)
  2311. {
  2312. Elf_Internal_Dyn dyn;
  2313. asection * s;
  2314. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  2315. switch (dyn.d_tag)
  2316. {
  2317. default:
  2318. break;
  2319. case DT_PLTGOT:
  2320. s = elf_hash_table (info)->sgotplt;
  2321. goto get_vma;
  2322. case DT_JMPREL:
  2323. s = elf_hash_table (info)->srelplt;
  2324. get_vma:
  2325. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2326. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2327. break;
  2328. case DT_PLTRELSZ:
  2329. s = elf_hash_table (info)->srelplt;
  2330. dyn.d_un.d_val = s->size;
  2331. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2332. break;
  2333. }
  2334. }
  2335. }
  2336. /* Fill in the first three entries in the global offset table. */
  2337. if (sgot->size > 0)
  2338. {
  2339. if (sdyn == NULL)
  2340. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
  2341. else
  2342. bfd_put_32 (output_bfd,
  2343. sdyn->output_section->vma + sdyn->output_offset,
  2344. sgot->contents);
  2345. }
  2346. elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  2347. return TRUE;
  2348. }
  2349. /* Given a .data.rel section and a .emreloc in-memory section, store
  2350. relocation information into the .emreloc section which can be
  2351. used at runtime to relocate the section. This is called by the
  2352. linker when the --embedded-relocs switch is used. This is called
  2353. after the add_symbols entry point has been called for all the
  2354. objects, and before the final_link entry point is called. */
  2355. bfd_boolean
  2356. bfd_cr16_elf32_create_embedded_relocs (bfd *abfd,
  2357. struct bfd_link_info *info,
  2358. asection *datasec,
  2359. asection *relsec,
  2360. char **errmsg)
  2361. {
  2362. Elf_Internal_Shdr *symtab_hdr;
  2363. Elf_Internal_Sym *isymbuf = NULL;
  2364. Elf_Internal_Rela *internal_relocs = NULL;
  2365. Elf_Internal_Rela *irel, *irelend;
  2366. bfd_byte *p;
  2367. bfd_size_type amt;
  2368. BFD_ASSERT (! bfd_link_relocatable (info));
  2369. *errmsg = NULL;
  2370. if (datasec->reloc_count == 0)
  2371. return TRUE;
  2372. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2373. /* Get a copy of the native relocations. */
  2374. internal_relocs = (_bfd_elf_link_read_relocs
  2375. (abfd, datasec, NULL, NULL, info->keep_memory));
  2376. if (internal_relocs == NULL)
  2377. goto error_return;
  2378. amt = (bfd_size_type) datasec->reloc_count * 8;
  2379. relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
  2380. if (relsec->contents == NULL)
  2381. goto error_return;
  2382. p = relsec->contents;
  2383. irelend = internal_relocs + datasec->reloc_count;
  2384. for (irel = internal_relocs; irel < irelend; irel++, p += 8)
  2385. {
  2386. asection *targetsec;
  2387. /* We are going to write a four byte longword into the runtime
  2388. reloc section. The longword will be the address in the data
  2389. section which must be relocated. It is followed by the name
  2390. of the target section NUL-padded or truncated to 8
  2391. characters. */
  2392. /* We can only relocate absolute longword relocs at run time. */
  2393. if (!((ELF32_R_TYPE (irel->r_info) == (int) R_CR16_NUM32a)
  2394. || (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_NUM32)))
  2395. {
  2396. *errmsg = _("unsupported reloc type");
  2397. bfd_set_error (bfd_error_bad_value);
  2398. goto error_return;
  2399. }
  2400. /* Get the target section referred to by the reloc. */
  2401. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  2402. {
  2403. /* A local symbol. */
  2404. Elf_Internal_Sym *isym;
  2405. /* Read this BFD's local symbols if we haven't done so already. */
  2406. if (isymbuf == NULL)
  2407. {
  2408. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2409. if (isymbuf == NULL)
  2410. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  2411. symtab_hdr->sh_info, 0,
  2412. NULL, NULL, NULL);
  2413. if (isymbuf == NULL)
  2414. goto error_return;
  2415. }
  2416. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  2417. targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  2418. }
  2419. else
  2420. {
  2421. unsigned long indx;
  2422. struct elf_link_hash_entry *h;
  2423. /* An external symbol. */
  2424. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  2425. h = elf_sym_hashes (abfd)[indx];
  2426. BFD_ASSERT (h != NULL);
  2427. if (h->root.type == bfd_link_hash_defined
  2428. || h->root.type == bfd_link_hash_defweak)
  2429. targetsec = h->root.u.def.section;
  2430. else
  2431. targetsec = NULL;
  2432. }
  2433. bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
  2434. memset (p + 4, 0, 4);
  2435. if ((ELF32_R_TYPE (irel->r_info) == (int) R_CR16_NUM32a)
  2436. && (targetsec != NULL) )
  2437. strncpy ((char *) p + 4, targetsec->output_section->name, 4);
  2438. }
  2439. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  2440. free (isymbuf);
  2441. if (internal_relocs != NULL
  2442. && elf_section_data (datasec)->relocs != internal_relocs)
  2443. free (internal_relocs);
  2444. return TRUE;
  2445. error_return:
  2446. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  2447. free (isymbuf);
  2448. if (internal_relocs != NULL
  2449. && elf_section_data (datasec)->relocs != internal_relocs)
  2450. free (internal_relocs);
  2451. return FALSE;
  2452. }
  2453. /* Classify relocation types, such that combreloc can sort them
  2454. properly. */
  2455. static enum elf_reloc_type_class
  2456. _bfd_cr16_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2457. const asection *rel_sec ATTRIBUTE_UNUSED,
  2458. const Elf_Internal_Rela *rela)
  2459. {
  2460. switch ((int) ELF32_R_TYPE (rela->r_info))
  2461. {
  2462. case R_CR16_GOT_REGREL20:
  2463. case R_CR16_GOTC_REGREL20:
  2464. return reloc_class_relative;
  2465. default:
  2466. return reloc_class_normal;
  2467. }
  2468. }
  2469. /* Definitions for setting CR16 target vector. */
  2470. #define TARGET_LITTLE_SYM cr16_elf32_vec
  2471. #define TARGET_LITTLE_NAME "elf32-cr16"
  2472. #define ELF_ARCH bfd_arch_cr16
  2473. #define ELF_MACHINE_CODE EM_CR16
  2474. #define ELF_MACHINE_ALT1 EM_CR16_OLD
  2475. #define ELF_MAXPAGESIZE 0x1
  2476. #define elf_symbol_leading_char '_'
  2477. #define bfd_elf32_bfd_reloc_type_lookup elf_cr16_reloc_type_lookup
  2478. #define bfd_elf32_bfd_reloc_name_lookup elf_cr16_reloc_name_lookup
  2479. #define elf_info_to_howto elf_cr16_info_to_howto
  2480. #define elf_info_to_howto_rel 0
  2481. #define elf_backend_relocate_section elf32_cr16_relocate_section
  2482. #define bfd_elf32_bfd_relax_section elf32_cr16_relax_section
  2483. #define bfd_elf32_bfd_get_relocated_section_contents \
  2484. elf32_cr16_get_relocated_section_contents
  2485. #define elf_backend_gc_mark_hook elf32_cr16_gc_mark_hook
  2486. #define elf_backend_gc_sweep_hook elf32_cr16_gc_sweep_hook
  2487. #define elf_backend_can_gc_sections 1
  2488. #define elf_backend_rela_normal 1
  2489. #define elf_backend_check_relocs cr16_elf_check_relocs
  2490. /* So we can set bits in e_flags. */
  2491. #define elf_backend_final_write_processing \
  2492. _bfd_cr16_elf_final_write_processing
  2493. #define elf_backend_object_p _bfd_cr16_elf_object_p
  2494. #define bfd_elf32_bfd_merge_private_bfd_data \
  2495. _bfd_cr16_elf_merge_private_bfd_data
  2496. #define bfd_elf32_bfd_link_hash_table_create \
  2497. elf32_cr16_link_hash_table_create
  2498. #define elf_backend_create_dynamic_sections \
  2499. _bfd_cr16_elf_create_dynamic_sections
  2500. #define elf_backend_adjust_dynamic_symbol \
  2501. _bfd_cr16_elf_adjust_dynamic_symbol
  2502. #define elf_backend_size_dynamic_sections \
  2503. _bfd_cr16_elf_size_dynamic_sections
  2504. #define elf_backend_omit_section_dynsym \
  2505. ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
  2506. #define elf_backend_finish_dynamic_symbol \
  2507. _bfd_cr16_elf_finish_dynamic_symbol
  2508. #define elf_backend_finish_dynamic_sections \
  2509. _bfd_cr16_elf_finish_dynamic_sections
  2510. #define elf_backend_reloc_type_class _bfd_cr16_elf_reloc_type_class
  2511. #define elf_backend_want_got_plt 1
  2512. #define elf_backend_plt_readonly 1
  2513. #define elf_backend_want_plt_sym 0
  2514. #define elf_backend_got_header_size 12
  2515. #define elf_backend_dtrel_excludes_plt 1
  2516. #include "elf32-target.h"