elf32-cr16.c 85 KB

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