elf32-crx.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. /* BFD back-end for National Semiconductor's CRX ELF
  2. Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. Written by Tomer Levi, NSC, Israel.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "elf/crx.h"
  23. static reloc_howto_type *elf_crx_reloc_type_lookup
  24. (bfd *, bfd_reloc_code_real_type);
  25. static bool elf_crx_info_to_howto
  26. (bfd *, arelent *, Elf_Internal_Rela *);
  27. static bool elf32_crx_relax_delete_bytes
  28. (struct bfd_link_info *, bfd *, asection *, bfd_vma, int);
  29. static bfd_reloc_status_type crx_elf_final_link_relocate
  30. (reloc_howto_type *, bfd *, bfd *, asection *,
  31. bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
  32. struct bfd_link_info *, asection *, int);
  33. static int elf32_crx_relocate_section
  34. (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  35. Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
  36. static bool elf32_crx_relax_section
  37. (bfd *, asection *, struct bfd_link_info *, bool *);
  38. static bfd_byte * elf32_crx_get_relocated_section_contents
  39. (bfd *, struct bfd_link_info *, struct bfd_link_order *,
  40. bfd_byte *, bool, asymbol **);
  41. /* crx_reloc_map array maps BFD relocation enum into a CRGAS relocation type. */
  42. struct crx_reloc_map
  43. {
  44. bfd_reloc_code_real_type bfd_reloc_enum; /* BFD relocation enum. */
  45. unsigned short crx_reloc_type; /* CRX relocation type. */
  46. };
  47. static const struct crx_reloc_map crx_reloc_map[R_CRX_MAX] =
  48. {
  49. {BFD_RELOC_NONE, R_CRX_NONE},
  50. {BFD_RELOC_CRX_REL4, R_CRX_REL4},
  51. {BFD_RELOC_CRX_REL8, R_CRX_REL8},
  52. {BFD_RELOC_CRX_REL8_CMP, R_CRX_REL8_CMP},
  53. {BFD_RELOC_CRX_REL16, R_CRX_REL16},
  54. {BFD_RELOC_CRX_REL24, R_CRX_REL24},
  55. {BFD_RELOC_CRX_REL32, R_CRX_REL32},
  56. {BFD_RELOC_CRX_REGREL12, R_CRX_REGREL12},
  57. {BFD_RELOC_CRX_REGREL22, R_CRX_REGREL22},
  58. {BFD_RELOC_CRX_REGREL28, R_CRX_REGREL28},
  59. {BFD_RELOC_CRX_REGREL32, R_CRX_REGREL32},
  60. {BFD_RELOC_CRX_ABS16, R_CRX_ABS16},
  61. {BFD_RELOC_CRX_ABS32, R_CRX_ABS32},
  62. {BFD_RELOC_CRX_NUM8, R_CRX_NUM8},
  63. {BFD_RELOC_CRX_NUM16, R_CRX_NUM16},
  64. {BFD_RELOC_CRX_NUM32, R_CRX_NUM32},
  65. {BFD_RELOC_CRX_IMM16, R_CRX_IMM16},
  66. {BFD_RELOC_CRX_IMM32, R_CRX_IMM32},
  67. {BFD_RELOC_CRX_SWITCH8, R_CRX_SWITCH8},
  68. {BFD_RELOC_CRX_SWITCH16, R_CRX_SWITCH16},
  69. {BFD_RELOC_CRX_SWITCH32, R_CRX_SWITCH32}
  70. };
  71. static reloc_howto_type crx_elf_howto_table[] =
  72. {
  73. HOWTO (R_CRX_NONE, /* type */
  74. 0, /* rightshift */
  75. 3, /* size */
  76. 0, /* bitsize */
  77. false, /* pc_relative */
  78. 0, /* bitpos */
  79. complain_overflow_dont,/* complain_on_overflow */
  80. bfd_elf_generic_reloc, /* special_function */
  81. "R_CRX_NONE", /* name */
  82. false, /* partial_inplace */
  83. 0, /* src_mask */
  84. 0, /* dst_mask */
  85. false), /* pcrel_offset */
  86. HOWTO (R_CRX_REL4, /* type */
  87. 1, /* rightshift */
  88. 0, /* size */
  89. 4, /* bitsize */
  90. true, /* pc_relative */
  91. 0, /* bitpos */
  92. complain_overflow_bitfield,/* complain_on_overflow */
  93. bfd_elf_generic_reloc, /* special_function */
  94. "R_CRX_REL4", /* name */
  95. false, /* partial_inplace */
  96. 0x0, /* src_mask */
  97. 0xf, /* dst_mask */
  98. false), /* pcrel_offset */
  99. HOWTO (R_CRX_REL8, /* type */
  100. 1, /* rightshift */
  101. 0, /* size */
  102. 8, /* bitsize */
  103. true, /* pc_relative */
  104. 0, /* bitpos */
  105. complain_overflow_bitfield,/* complain_on_overflow */
  106. bfd_elf_generic_reloc, /* special_function */
  107. "R_CRX_REL8", /* name */
  108. false, /* partial_inplace */
  109. 0x0, /* src_mask */
  110. 0xff, /* dst_mask */
  111. false), /* pcrel_offset */
  112. HOWTO (R_CRX_REL8_CMP, /* type */
  113. 1, /* rightshift */
  114. 0, /* size */
  115. 8, /* bitsize */
  116. true, /* pc_relative */
  117. 0, /* bitpos */
  118. complain_overflow_bitfield,/* complain_on_overflow */
  119. bfd_elf_generic_reloc, /* special_function */
  120. "R_CRX_REL8_CMP", /* name */
  121. false, /* partial_inplace */
  122. 0x0, /* src_mask */
  123. 0xff, /* dst_mask */
  124. false), /* pcrel_offset */
  125. HOWTO (R_CRX_REL16, /* type */
  126. 1, /* rightshift */
  127. 1, /* size */
  128. 16, /* bitsize */
  129. true, /* pc_relative */
  130. 0, /* bitpos */
  131. complain_overflow_bitfield,/* complain_on_overflow */
  132. bfd_elf_generic_reloc, /* special_function */
  133. "R_CRX_REL16", /* name */
  134. false, /* partial_inplace */
  135. 0x0, /* src_mask */
  136. 0xffff, /* dst_mask */
  137. false), /* pcrel_offset */
  138. HOWTO (R_CRX_REL24, /* type */
  139. 1, /* rightshift */
  140. 2, /* size */
  141. 24, /* bitsize */
  142. true, /* pc_relative */
  143. 0, /* bitpos */
  144. complain_overflow_bitfield,/* complain_on_overflow */
  145. bfd_elf_generic_reloc, /* special_function */
  146. "R_CRX_REL24", /* name */
  147. false, /* partial_inplace */
  148. 0x0, /* src_mask */
  149. 0xffffff, /* dst_mask */
  150. false), /* pcrel_offset */
  151. HOWTO (R_CRX_REL32, /* type */
  152. 1, /* rightshift */
  153. 2, /* size */
  154. 32, /* bitsize */
  155. true, /* pc_relative */
  156. 0, /* bitpos */
  157. complain_overflow_bitfield,/* complain_on_overflow */
  158. bfd_elf_generic_reloc, /* special_function */
  159. "R_CRX_REL32", /* name */
  160. false, /* partial_inplace */
  161. 0x0, /* src_mask */
  162. 0xffffffff, /* dst_mask */
  163. false), /* pcrel_offset */
  164. HOWTO (R_CRX_REGREL12, /* type */
  165. 0, /* rightshift */
  166. 1, /* size */
  167. 12, /* bitsize */
  168. false, /* pc_relative */
  169. 0, /* bitpos */
  170. complain_overflow_bitfield,/* complain_on_overflow */
  171. bfd_elf_generic_reloc, /* special_function */
  172. "R_CRX_REGREL12", /* name */
  173. false, /* partial_inplace */
  174. 0x0, /* src_mask */
  175. 0xfff, /* dst_mask */
  176. false), /* pcrel_offset */
  177. HOWTO (R_CRX_REGREL22, /* type */
  178. 0, /* rightshift */
  179. 2, /* size */
  180. 22, /* bitsize */
  181. false, /* pc_relative */
  182. 0, /* bitpos */
  183. complain_overflow_bitfield,/* complain_on_overflow */
  184. bfd_elf_generic_reloc, /* special_function */
  185. "R_CRX_REGREL22", /* name */
  186. false, /* partial_inplace */
  187. 0x0, /* src_mask */
  188. 0x3fffff, /* dst_mask */
  189. false), /* pcrel_offset */
  190. HOWTO (R_CRX_REGREL28, /* type */
  191. 0, /* rightshift */
  192. 2, /* size */
  193. 28, /* bitsize */
  194. false, /* pc_relative */
  195. 0, /* bitpos */
  196. complain_overflow_bitfield,/* complain_on_overflow */
  197. bfd_elf_generic_reloc, /* special_function */
  198. "R_CRX_REGREL28", /* name */
  199. false, /* partial_inplace */
  200. 0x0, /* src_mask */
  201. 0xfffffff, /* dst_mask */
  202. false), /* pcrel_offset */
  203. HOWTO (R_CRX_REGREL32, /* type */
  204. 0, /* rightshift */
  205. 2, /* size */
  206. 32, /* bitsize */
  207. false, /* pc_relative */
  208. 0, /* bitpos */
  209. complain_overflow_bitfield,/* complain_on_overflow */
  210. bfd_elf_generic_reloc, /* special_function */
  211. "R_CRX_REGREL32", /* name */
  212. false, /* partial_inplace */
  213. 0x0, /* src_mask */
  214. 0xffffffff, /* dst_mask */
  215. false), /* pcrel_offset */
  216. HOWTO (R_CRX_ABS16, /* 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_CRX_ABS16", /* name */
  225. false, /* partial_inplace */
  226. 0x0, /* src_mask */
  227. 0xffff, /* dst_mask */
  228. false), /* pcrel_offset */
  229. HOWTO (R_CRX_ABS32, /* type */
  230. 0, /* rightshift */
  231. 2, /* size */
  232. 32, /* bitsize */
  233. false, /* pc_relative */
  234. 0, /* bitpos */
  235. complain_overflow_bitfield,/* complain_on_overflow */
  236. bfd_elf_generic_reloc, /* special_function */
  237. "R_CRX_ABS32", /* name */
  238. false, /* partial_inplace */
  239. 0x0, /* src_mask */
  240. 0xffffffff, /* dst_mask */
  241. false), /* pcrel_offset */
  242. HOWTO (R_CRX_NUM8, /* type */
  243. 0, /* rightshift */
  244. 0, /* size */
  245. 8, /* bitsize */
  246. false, /* pc_relative */
  247. 0, /* bitpos */
  248. complain_overflow_bitfield,/* complain_on_overflow */
  249. bfd_elf_generic_reloc, /* special_function */
  250. "R_CRX_NUM8", /* name */
  251. false, /* partial_inplace */
  252. 0x0, /* src_mask */
  253. 0xff, /* dst_mask */
  254. false), /* pcrel_offset */
  255. HOWTO (R_CRX_NUM16, /* type */
  256. 0, /* rightshift */
  257. 1, /* size */
  258. 16, /* bitsize */
  259. false, /* pc_relative */
  260. 0, /* bitpos */
  261. complain_overflow_bitfield,/* complain_on_overflow */
  262. bfd_elf_generic_reloc, /* special_function */
  263. "R_CRX_NUM16", /* name */
  264. false, /* partial_inplace */
  265. 0x0, /* src_mask */
  266. 0xffff, /* dst_mask */
  267. false), /* pcrel_offset */
  268. HOWTO (R_CRX_NUM32, /* type */
  269. 0, /* rightshift */
  270. 2, /* size */
  271. 32, /* bitsize */
  272. false, /* pc_relative */
  273. 0, /* bitpos */
  274. complain_overflow_bitfield,/* complain_on_overflow */
  275. bfd_elf_generic_reloc, /* special_function */
  276. "R_CRX_NUM32", /* name */
  277. false, /* partial_inplace */
  278. 0x0, /* src_mask */
  279. 0xffffffff, /* dst_mask */
  280. false), /* pcrel_offset */
  281. HOWTO (R_CRX_IMM16, /* type */
  282. 0, /* rightshift */
  283. 1, /* size */
  284. 16, /* bitsize */
  285. false, /* pc_relative */
  286. 0, /* bitpos */
  287. complain_overflow_bitfield,/* complain_on_overflow */
  288. bfd_elf_generic_reloc, /* special_function */
  289. "R_CRX_IMM16", /* name */
  290. false, /* partial_inplace */
  291. 0x0, /* src_mask */
  292. 0xffff, /* dst_mask */
  293. false), /* pcrel_offset */
  294. HOWTO (R_CRX_IMM32, /* type */
  295. 0, /* rightshift */
  296. 2, /* size */
  297. 32, /* bitsize */
  298. false, /* pc_relative */
  299. 0, /* bitpos */
  300. complain_overflow_bitfield,/* complain_on_overflow */
  301. bfd_elf_generic_reloc, /* special_function */
  302. "R_CRX_IMM32", /* name */
  303. false, /* partial_inplace */
  304. 0x0, /* src_mask */
  305. 0xffffffff, /* dst_mask */
  306. false), /* pcrel_offset */
  307. /* An 8 bit switch table entry. This is generated for an expression
  308. such as ``.byte L1 - L2''. The offset holds the difference
  309. between the reloc address and L2. */
  310. HOWTO (R_CRX_SWITCH8, /* type */
  311. 0, /* rightshift */
  312. 0, /* size (0 = byte, 1 = short, 2 = long) */
  313. 8, /* bitsize */
  314. false, /* pc_relative */
  315. 0, /* bitpos */
  316. complain_overflow_unsigned, /* complain_on_overflow */
  317. bfd_elf_generic_reloc, /* special_function */
  318. "R_CRX_SWITCH8", /* name */
  319. false, /* partial_inplace */
  320. 0x0, /* src_mask */
  321. 0xff, /* dst_mask */
  322. true), /* pcrel_offset */
  323. /* A 16 bit switch table entry. This is generated for an expression
  324. such as ``.word L1 - L2''. The offset holds the difference
  325. between the reloc address and L2. */
  326. HOWTO (R_CRX_SWITCH16, /* type */
  327. 0, /* rightshift */
  328. 1, /* size (0 = byte, 1 = short, 2 = long) */
  329. 16, /* bitsize */
  330. false, /* pc_relative */
  331. 0, /* bitpos */
  332. complain_overflow_unsigned, /* complain_on_overflow */
  333. bfd_elf_generic_reloc, /* special_function */
  334. "R_CRX_SWITCH16", /* name */
  335. false, /* partial_inplace */
  336. 0x0, /* src_mask */
  337. 0xffff, /* dst_mask */
  338. true), /* pcrel_offset */
  339. /* A 32 bit switch table entry. This is generated for an expression
  340. such as ``.long L1 - L2''. The offset holds the difference
  341. between the reloc address and L2. */
  342. HOWTO (R_CRX_SWITCH32, /* type */
  343. 0, /* rightshift */
  344. 2, /* size (0 = byte, 1 = short, 2 = long) */
  345. 32, /* bitsize */
  346. false, /* pc_relative */
  347. 0, /* bitpos */
  348. complain_overflow_unsigned, /* complain_on_overflow */
  349. bfd_elf_generic_reloc, /* special_function */
  350. "R_CRX_SWITCH32", /* name */
  351. false, /* partial_inplace */
  352. 0x0, /* src_mask */
  353. 0xffffffff, /* dst_mask */
  354. true) /* pcrel_offset */
  355. };
  356. /* Retrieve a howto ptr using a BFD reloc_code. */
  357. static reloc_howto_type *
  358. elf_crx_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  359. bfd_reloc_code_real_type code)
  360. {
  361. unsigned int i;
  362. for (i = 0; i < R_CRX_MAX; i++)
  363. if (code == crx_reloc_map[i].bfd_reloc_enum)
  364. return &crx_elf_howto_table[crx_reloc_map[i].crx_reloc_type];
  365. printf ("This relocation Type is not supported -0x%x\n", code);
  366. return 0;
  367. }
  368. static reloc_howto_type *
  369. elf_crx_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  370. const char *r_name)
  371. {
  372. unsigned int i;
  373. for (i = 0;
  374. i < sizeof (crx_elf_howto_table) / sizeof (crx_elf_howto_table[0]);
  375. i++)
  376. if (crx_elf_howto_table[i].name != NULL
  377. && strcasecmp (crx_elf_howto_table[i].name, r_name) == 0)
  378. return &crx_elf_howto_table[i];
  379. return NULL;
  380. }
  381. /* Retrieve a howto ptr using an internal relocation entry. */
  382. static bool
  383. elf_crx_info_to_howto (bfd *abfd, arelent *cache_ptr,
  384. Elf_Internal_Rela *dst)
  385. {
  386. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  387. if (r_type >= R_CRX_MAX)
  388. {
  389. /* xgettext:c-format */
  390. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  391. abfd, r_type);
  392. bfd_set_error (bfd_error_bad_value);
  393. return false;
  394. }
  395. cache_ptr->howto = &crx_elf_howto_table[r_type];
  396. return true;
  397. }
  398. /* Perform a relocation as part of a final link. */
  399. static bfd_reloc_status_type
  400. crx_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
  401. bfd *output_bfd ATTRIBUTE_UNUSED,
  402. asection *input_section, bfd_byte *contents,
  403. bfd_vma offset, bfd_vma Rvalue, bfd_vma addend,
  404. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  405. asection *sec ATTRIBUTE_UNUSED,
  406. int is_local ATTRIBUTE_UNUSED)
  407. {
  408. unsigned short r_type = howto->type;
  409. bfd_byte *hit_data = contents + offset;
  410. bfd_vma reloc_bits, check;
  411. switch (r_type)
  412. {
  413. case R_CRX_IMM16:
  414. case R_CRX_IMM32:
  415. case R_CRX_ABS16:
  416. case R_CRX_ABS32:
  417. case R_CRX_REL8_CMP:
  418. case R_CRX_REL16:
  419. case R_CRX_REL24:
  420. case R_CRX_REL32:
  421. case R_CRX_REGREL12:
  422. case R_CRX_REGREL22:
  423. case R_CRX_REGREL28:
  424. case R_CRX_REGREL32:
  425. /* 'hit_data' is relative to the start of the instruction, not the
  426. relocation offset. Advance it to account for the exact offset. */
  427. hit_data += 2;
  428. break;
  429. case R_CRX_REL4:
  430. /* This relocation type is used only in 'Branch if Equal to 0'
  431. instructions and requires special handling. */
  432. Rvalue -= 1;
  433. break;
  434. case R_CRX_NONE:
  435. return bfd_reloc_ok;
  436. break;
  437. case R_CRX_SWITCH8:
  438. case R_CRX_SWITCH16:
  439. case R_CRX_SWITCH32:
  440. /* We only care about the addend, where the difference between
  441. expressions is kept. */
  442. Rvalue = 0;
  443. default:
  444. break;
  445. }
  446. if (howto->pc_relative)
  447. {
  448. /* Subtract the address of the section containing the location. */
  449. Rvalue -= (input_section->output_section->vma
  450. + input_section->output_offset);
  451. /* Subtract the position of the location within the section. */
  452. Rvalue -= offset;
  453. }
  454. /* Add in supplied addend. */
  455. Rvalue += addend;
  456. /* Complain if the bitfield overflows, whether it is considered
  457. as signed or unsigned. */
  458. check = Rvalue >> howto->rightshift;
  459. reloc_bits = ((bfd_vma) 1 << (howto->bitsize - 1) << 1) - 1;
  460. if ((check & ~reloc_bits) != 0
  461. && (check & ~reloc_bits) != ((bfd_vma) -1 & ~reloc_bits))
  462. {
  463. /* The above right shift is incorrect for a signed
  464. value. See if turning on the upper bits fixes the
  465. overflow. */
  466. if (howto->rightshift && (bfd_signed_vma) Rvalue < 0)
  467. {
  468. check |= (bfd_vma) -1 & ~((bfd_vma) -1 >> howto->rightshift);
  469. if ((check & ~reloc_bits) != ((bfd_vma) -1 & ~reloc_bits))
  470. return bfd_reloc_overflow;
  471. }
  472. else
  473. return bfd_reloc_overflow;
  474. }
  475. /* Drop unwanted bits from the value we are relocating to. */
  476. Rvalue >>= howto->rightshift;
  477. /* Apply dst_mask to select only relocatable part of the insn. */
  478. Rvalue &= howto->dst_mask;
  479. switch (howto->size)
  480. {
  481. case 0:
  482. if (r_type == R_CRX_REL4)
  483. {
  484. Rvalue <<= 4;
  485. Rvalue |= (bfd_get_8 (input_bfd, hit_data) & 0x0f);
  486. }
  487. bfd_put_8 (input_bfd, (unsigned char) Rvalue, hit_data);
  488. break;
  489. case 1:
  490. if (r_type == R_CRX_REGREL12)
  491. Rvalue |= (bfd_get_16 (input_bfd, hit_data) & 0xf000);
  492. bfd_put_16 (input_bfd, Rvalue, hit_data);
  493. break;
  494. case 2:
  495. if (r_type == R_CRX_REL24
  496. || r_type == R_CRX_REGREL22
  497. || r_type == R_CRX_REGREL28)
  498. Rvalue |= (((bfd_get_16 (input_bfd, hit_data) << 16) |
  499. bfd_get_16 (input_bfd, hit_data + 2)) & ~howto->dst_mask);
  500. if (r_type == R_CRX_NUM32 || r_type == R_CRX_SWITCH32)
  501. /* Relocation on DATA is purely little-endian, that is, for a
  502. multi-byte datum, the lowest address in memory contains the
  503. little end of the datum, that is, the least significant byte.
  504. Therefore we use BFD's byte Putting functions. */
  505. bfd_put_32 (input_bfd, Rvalue, hit_data);
  506. else
  507. /* Relocation on INSTRUCTIONS is different : Instructions are
  508. word-addressable, that is, each word itself is arranged according
  509. to little-endian convention, whereas the words are arranged with
  510. respect to one another in BIG ENDIAN fashion.
  511. When there is an immediate value that spans a word boundary, it is
  512. split in a big-endian way with respect to the words. */
  513. {
  514. bfd_put_16 (input_bfd, (Rvalue >> 16) & 0xffff, hit_data);
  515. bfd_put_16 (input_bfd, Rvalue & 0xffff, hit_data + 2);
  516. }
  517. break;
  518. default:
  519. return bfd_reloc_notsupported;
  520. }
  521. return bfd_reloc_ok;
  522. }
  523. /* Delete some bytes from a section while relaxing. */
  524. static bool
  525. elf32_crx_relax_delete_bytes (struct bfd_link_info *link_info, bfd *abfd,
  526. asection *sec, bfd_vma addr, int count)
  527. {
  528. Elf_Internal_Shdr *symtab_hdr;
  529. unsigned int sec_shndx;
  530. bfd_byte *contents;
  531. Elf_Internal_Rela *irel, *irelend;
  532. bfd_vma toaddr;
  533. Elf_Internal_Sym *isym;
  534. Elf_Internal_Sym *isymend;
  535. struct elf_link_hash_entry **sym_hashes;
  536. struct elf_link_hash_entry **end_hashes;
  537. struct elf_link_hash_entry **start_hashes;
  538. unsigned int symcount;
  539. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  540. contents = elf_section_data (sec)->this_hdr.contents;
  541. toaddr = sec->size;
  542. irel = elf_section_data (sec)->relocs;
  543. irelend = irel + sec->reloc_count;
  544. /* Actually delete the bytes. */
  545. memmove (contents + addr, contents + addr + count,
  546. (size_t) (toaddr - addr - count));
  547. sec->size -= count;
  548. /* Adjust all the relocs. */
  549. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  550. {
  551. /* Get the new reloc address. */
  552. if ((irel->r_offset > addr
  553. && irel->r_offset < toaddr))
  554. irel->r_offset -= count;
  555. }
  556. /* Adjust the local symbols defined in this section. */
  557. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  558. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  559. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  560. {
  561. if (isym->st_shndx == sec_shndx
  562. && isym->st_value > addr
  563. && isym->st_value < toaddr)
  564. {
  565. /* Adjust the addend of SWITCH relocations in this section,
  566. which reference this local symbol. */
  567. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  568. {
  569. unsigned long r_symndx;
  570. Elf_Internal_Sym *rsym;
  571. bfd_vma addsym, subsym;
  572. /* Skip if not a SWITCH relocation. */
  573. if (ELF32_R_TYPE (irel->r_info) != (int) R_CRX_SWITCH8
  574. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_SWITCH16
  575. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_SWITCH32)
  576. continue;
  577. r_symndx = ELF32_R_SYM (irel->r_info);
  578. rsym = (Elf_Internal_Sym *) symtab_hdr->contents + r_symndx;
  579. /* Skip if not the local adjusted symbol. */
  580. if (rsym != isym)
  581. continue;
  582. addsym = isym->st_value;
  583. subsym = addsym - irel->r_addend;
  584. /* Fix the addend only when -->> (addsym > addr >= subsym). */
  585. if (subsym <= addr)
  586. irel->r_addend -= count;
  587. else
  588. continue;
  589. }
  590. isym->st_value -= count;
  591. }
  592. }
  593. /* Now adjust the global symbols defined in this section. */
  594. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  595. - symtab_hdr->sh_info);
  596. sym_hashes = start_hashes = elf_sym_hashes (abfd);
  597. end_hashes = sym_hashes + symcount;
  598. for (; sym_hashes < end_hashes; sym_hashes++)
  599. {
  600. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  601. /* The '--wrap SYMBOL' option is causing a pain when the object file,
  602. containing the definition of __wrap_SYMBOL, includes a direct
  603. call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
  604. the same symbol (which is __wrap_SYMBOL), but still exist as two
  605. different symbols in 'sym_hashes', we don't want to adjust
  606. the global symbol __wrap_SYMBOL twice.
  607. This check is only relevant when symbols are being wrapped. */
  608. if (link_info->wrap_hash != NULL)
  609. {
  610. struct elf_link_hash_entry **cur_sym_hashes;
  611. /* Loop only over the symbols whom been already checked. */
  612. for (cur_sym_hashes = start_hashes; cur_sym_hashes < sym_hashes;
  613. cur_sym_hashes++)
  614. {
  615. /* If the current symbol is identical to 'sym_hash', that means
  616. the symbol was already adjusted (or at least checked). */
  617. if (*cur_sym_hashes == sym_hash)
  618. break;
  619. }
  620. /* Don't adjust the symbol again. */
  621. if (cur_sym_hashes < sym_hashes)
  622. continue;
  623. }
  624. if ((sym_hash->root.type == bfd_link_hash_defined
  625. || sym_hash->root.type == bfd_link_hash_defweak)
  626. && sym_hash->root.u.def.section == sec
  627. && sym_hash->root.u.def.value > addr
  628. && sym_hash->root.u.def.value < toaddr)
  629. sym_hash->root.u.def.value -= count;
  630. }
  631. return true;
  632. }
  633. /* This is a version of bfd_generic_get_relocated_section_contents
  634. which uses elf32_crx_relocate_section. */
  635. static bfd_byte *
  636. elf32_crx_get_relocated_section_contents (bfd *output_bfd,
  637. struct bfd_link_info *link_info,
  638. struct bfd_link_order *link_order,
  639. bfd_byte *data,
  640. bool relocatable,
  641. asymbol **symbols)
  642. {
  643. Elf_Internal_Shdr *symtab_hdr;
  644. asection *input_section = link_order->u.indirect.section;
  645. bfd *input_bfd = input_section->owner;
  646. asection **sections = NULL;
  647. Elf_Internal_Rela *internal_relocs = NULL;
  648. Elf_Internal_Sym *isymbuf = NULL;
  649. /* We only need to handle the case of relaxing, or of having a
  650. particular set of section contents, specially. */
  651. if (relocatable
  652. || elf_section_data (input_section)->this_hdr.contents == NULL)
  653. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  654. link_order, data,
  655. relocatable,
  656. symbols);
  657. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  658. memcpy (data, elf_section_data (input_section)->this_hdr.contents,
  659. (size_t) input_section->size);
  660. if ((input_section->flags & SEC_RELOC) != 0
  661. && input_section->reloc_count > 0)
  662. {
  663. Elf_Internal_Sym *isym;
  664. Elf_Internal_Sym *isymend;
  665. asection **secpp;
  666. bfd_size_type amt;
  667. internal_relocs = (_bfd_elf_link_read_relocs
  668. (input_bfd, input_section, NULL,
  669. (Elf_Internal_Rela *) NULL, false));
  670. if (internal_relocs == NULL)
  671. goto error_return;
  672. if (symtab_hdr->sh_info != 0)
  673. {
  674. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  675. if (isymbuf == NULL)
  676. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  677. symtab_hdr->sh_info, 0,
  678. NULL, NULL, NULL);
  679. if (isymbuf == NULL)
  680. goto error_return;
  681. }
  682. amt = symtab_hdr->sh_info;
  683. amt *= sizeof (asection *);
  684. sections = bfd_malloc (amt);
  685. if (sections == NULL && amt != 0)
  686. goto error_return;
  687. isymend = isymbuf + symtab_hdr->sh_info;
  688. for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
  689. {
  690. asection *isec;
  691. if (isym->st_shndx == SHN_UNDEF)
  692. isec = bfd_und_section_ptr;
  693. else if (isym->st_shndx == SHN_ABS)
  694. isec = bfd_abs_section_ptr;
  695. else if (isym->st_shndx == SHN_COMMON)
  696. isec = bfd_com_section_ptr;
  697. else
  698. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  699. *secpp = isec;
  700. }
  701. if (! elf32_crx_relocate_section (output_bfd, link_info, input_bfd,
  702. input_section, data, internal_relocs,
  703. isymbuf, sections))
  704. goto error_return;
  705. free (sections);
  706. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  707. free (isymbuf);
  708. if (elf_section_data (input_section)->relocs != internal_relocs)
  709. free (internal_relocs);
  710. }
  711. return data;
  712. error_return:
  713. free (sections);
  714. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  715. free (isymbuf);
  716. if (elf_section_data (input_section)->relocs != internal_relocs)
  717. free (internal_relocs);
  718. return NULL;
  719. }
  720. /* Relocate a CRX ELF section. */
  721. static int
  722. elf32_crx_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
  723. bfd *input_bfd, asection *input_section,
  724. bfd_byte *contents, Elf_Internal_Rela *relocs,
  725. Elf_Internal_Sym *local_syms,
  726. asection **local_sections)
  727. {
  728. Elf_Internal_Shdr *symtab_hdr;
  729. struct elf_link_hash_entry **sym_hashes;
  730. Elf_Internal_Rela *rel, *relend;
  731. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  732. sym_hashes = elf_sym_hashes (input_bfd);
  733. rel = relocs;
  734. relend = relocs + input_section->reloc_count;
  735. for (; rel < relend; rel++)
  736. {
  737. int r_type;
  738. reloc_howto_type *howto;
  739. unsigned long r_symndx;
  740. Elf_Internal_Sym *sym;
  741. asection *sec;
  742. struct elf_link_hash_entry *h;
  743. bfd_vma relocation;
  744. bfd_reloc_status_type r;
  745. r_symndx = ELF32_R_SYM (rel->r_info);
  746. r_type = ELF32_R_TYPE (rel->r_info);
  747. howto = crx_elf_howto_table + (r_type);
  748. h = NULL;
  749. sym = NULL;
  750. sec = NULL;
  751. if (r_symndx < symtab_hdr->sh_info)
  752. {
  753. sym = local_syms + r_symndx;
  754. sec = local_sections[r_symndx];
  755. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  756. }
  757. else
  758. {
  759. bool unresolved_reloc, warned, ignored;
  760. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  761. r_symndx, symtab_hdr, sym_hashes,
  762. h, sec, relocation,
  763. unresolved_reloc, warned, ignored);
  764. }
  765. if (sec != NULL && discarded_section (sec))
  766. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  767. rel, 1, relend, howto, 0, contents);
  768. if (bfd_link_relocatable (info))
  769. continue;
  770. r = crx_elf_final_link_relocate (howto, input_bfd, output_bfd,
  771. input_section,
  772. contents, rel->r_offset,
  773. relocation, rel->r_addend,
  774. info, sec, h == NULL);
  775. if (r != bfd_reloc_ok)
  776. {
  777. const char *name;
  778. const char *msg = (const char *) 0;
  779. if (h != NULL)
  780. name = h->root.root.string;
  781. else
  782. {
  783. name = (bfd_elf_string_from_elf_section
  784. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  785. if (name == NULL || *name == '\0')
  786. name = bfd_section_name (sec);
  787. }
  788. switch (r)
  789. {
  790. case bfd_reloc_overflow:
  791. (*info->callbacks->reloc_overflow)
  792. (info, (h ? &h->root : NULL), name, howto->name,
  793. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  794. break;
  795. case bfd_reloc_undefined:
  796. (*info->callbacks->undefined_symbol)
  797. (info, name, input_bfd, input_section, rel->r_offset, true);
  798. break;
  799. case bfd_reloc_outofrange:
  800. msg = _("internal error: out of range error");
  801. goto common_error;
  802. case bfd_reloc_notsupported:
  803. msg = _("internal error: unsupported relocation error");
  804. goto common_error;
  805. case bfd_reloc_dangerous:
  806. msg = _("internal error: dangerous error");
  807. goto common_error;
  808. default:
  809. msg = _("internal error: unknown error");
  810. /* Fall through. */
  811. common_error:
  812. (*info->callbacks->warning) (info, msg, name, input_bfd,
  813. input_section, rel->r_offset);
  814. break;
  815. }
  816. }
  817. }
  818. return true;
  819. }
  820. /* This function handles relaxing for the CRX.
  821. There's quite a few relaxing opportunites available on the CRX:
  822. * bal/bcond:32 -> bal/bcond:16 2 bytes
  823. * bcond:16 -> bcond:8 2 bytes
  824. * cmpbcond:24 -> cmpbcond:8 2 bytes
  825. * arithmetic imm32 -> arithmetic imm16 2 bytes
  826. Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
  827. static bool
  828. elf32_crx_relax_section (bfd *abfd, asection *sec,
  829. struct bfd_link_info *link_info, bool *again)
  830. {
  831. Elf_Internal_Shdr *symtab_hdr;
  832. Elf_Internal_Rela *internal_relocs;
  833. Elf_Internal_Rela *irel, *irelend;
  834. bfd_byte *contents = NULL;
  835. Elf_Internal_Sym *isymbuf = NULL;
  836. /* Assume nothing changes. */
  837. *again = false;
  838. /* We don't have to do anything for a relocatable link, if
  839. this section does not have relocs, or if this is not a
  840. code section. */
  841. if (bfd_link_relocatable (link_info)
  842. || (sec->flags & SEC_RELOC) == 0
  843. || sec->reloc_count == 0
  844. || (sec->flags & SEC_CODE) == 0)
  845. return true;
  846. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  847. /* Get a copy of the native relocations. */
  848. internal_relocs = (_bfd_elf_link_read_relocs
  849. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  850. link_info->keep_memory));
  851. if (internal_relocs == NULL)
  852. goto error_return;
  853. /* Walk through them looking for relaxing opportunities. */
  854. irelend = internal_relocs + sec->reloc_count;
  855. for (irel = internal_relocs; irel < irelend; irel++)
  856. {
  857. bfd_vma symval;
  858. /* If this isn't something that can be relaxed, then ignore
  859. this reloc. */
  860. if (ELF32_R_TYPE (irel->r_info) != (int) R_CRX_REL32
  861. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_REL16
  862. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_REL24
  863. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_IMM32)
  864. continue;
  865. /* Get the section contents if we haven't done so already. */
  866. if (contents == NULL)
  867. {
  868. /* Get cached copy if it exists. */
  869. if (elf_section_data (sec)->this_hdr.contents != NULL)
  870. contents = elf_section_data (sec)->this_hdr.contents;
  871. /* Go get them off disk. */
  872. else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  873. goto error_return;
  874. }
  875. /* Read this BFD's local symbols if we haven't done so already. */
  876. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  877. {
  878. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  879. if (isymbuf == NULL)
  880. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  881. symtab_hdr->sh_info, 0,
  882. NULL, NULL, NULL);
  883. if (isymbuf == NULL)
  884. goto error_return;
  885. }
  886. /* Get the value of the symbol referred to by the reloc. */
  887. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  888. {
  889. /* A local symbol. */
  890. Elf_Internal_Sym *isym;
  891. asection *sym_sec;
  892. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  893. if (isym->st_shndx == SHN_UNDEF)
  894. sym_sec = bfd_und_section_ptr;
  895. else if (isym->st_shndx == SHN_ABS)
  896. sym_sec = bfd_abs_section_ptr;
  897. else if (isym->st_shndx == SHN_COMMON)
  898. sym_sec = bfd_com_section_ptr;
  899. else
  900. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  901. symval = (isym->st_value
  902. + sym_sec->output_section->vma
  903. + sym_sec->output_offset);
  904. }
  905. else
  906. {
  907. unsigned long indx;
  908. struct elf_link_hash_entry *h;
  909. /* An external symbol. */
  910. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  911. h = elf_sym_hashes (abfd)[indx];
  912. BFD_ASSERT (h != NULL);
  913. if (h->root.type != bfd_link_hash_defined
  914. && h->root.type != bfd_link_hash_defweak)
  915. /* This appears to be a reference to an undefined
  916. symbol. Just ignore it--it will be caught by the
  917. regular reloc processing. */
  918. continue;
  919. symval = (h->root.u.def.value
  920. + h->root.u.def.section->output_section->vma
  921. + h->root.u.def.section->output_offset);
  922. }
  923. /* For simplicity of coding, we are going to modify the section
  924. contents, the section relocs, and the BFD symbol table. We
  925. must tell the rest of the code not to free up this
  926. information. It would be possible to instead create a table
  927. of changes which have to be made, as is done in coff-mips.c;
  928. that would be more work, but would require less memory when
  929. the linker is run. */
  930. /* Try to turn a 32bit pc-relative branch/call into
  931. a 16bit pc-relative branch/call. */
  932. if (ELF32_R_TYPE (irel->r_info) == (int) R_CRX_REL32)
  933. {
  934. bfd_vma value = symval;
  935. /* Deal with pc-relative gunk. */
  936. value -= (sec->output_section->vma + sec->output_offset);
  937. value -= irel->r_offset;
  938. value += irel->r_addend;
  939. /* See if the value will fit in 16 bits, note the high value is
  940. 0xfffe + 2 as the target will be two bytes closer if we are
  941. able to relax. */
  942. if ((long) value < 0x10000 && (long) value > -0x10002)
  943. {
  944. unsigned short code;
  945. /* Get the opcode. */
  946. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  947. /* Verify it's a 'bal'/'bcond' and fix the opcode. */
  948. if ((code & 0xfff0) == 0x3170)
  949. bfd_put_8 (abfd, 0x30, contents + irel->r_offset + 1);
  950. else if ((code & 0xf0ff) == 0x707f)
  951. bfd_put_8 (abfd, 0x7e, contents + irel->r_offset);
  952. else
  953. continue;
  954. /* Note that we've changed the relocs, section contents, etc. */
  955. elf_section_data (sec)->relocs = internal_relocs;
  956. elf_section_data (sec)->this_hdr.contents = contents;
  957. symtab_hdr->contents = (unsigned char *) isymbuf;
  958. /* Fix the relocation's type. */
  959. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  960. R_CRX_REL16);
  961. /* Delete two bytes of data. */
  962. if (!elf32_crx_relax_delete_bytes (link_info, abfd, sec,
  963. irel->r_offset + 2, 2))
  964. goto error_return;
  965. /* That will change things, so, we should relax again.
  966. Note that this is not required, and it may be slow. */
  967. *again = true;
  968. }
  969. }
  970. /* Try to turn a 16bit pc-relative branch into an
  971. 8bit pc-relative branch. */
  972. if (ELF32_R_TYPE (irel->r_info) == (int) R_CRX_REL16)
  973. {
  974. bfd_vma value = symval;
  975. /* Deal with pc-relative gunk. */
  976. value -= (sec->output_section->vma + sec->output_offset);
  977. value -= irel->r_offset;
  978. value += irel->r_addend;
  979. /* See if the value will fit in 8 bits, note the high value is
  980. 0xfc + 2 as the target will be two bytes closer if we are
  981. able to relax. */
  982. if ((long) value < 0xfe && (long) value > -0x100)
  983. {
  984. unsigned short code;
  985. /* Get the opcode. */
  986. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  987. /* Verify it's a 'bcond' opcode. */
  988. if ((code & 0xf0ff) != 0x707e)
  989. continue;
  990. /* Note that we've changed the relocs, section contents, etc. */
  991. elf_section_data (sec)->relocs = internal_relocs;
  992. elf_section_data (sec)->this_hdr.contents = contents;
  993. symtab_hdr->contents = (unsigned char *) isymbuf;
  994. /* Fix the relocation's type. */
  995. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  996. R_CRX_REL8);
  997. /* Delete two bytes of data. */
  998. if (!elf32_crx_relax_delete_bytes (link_info, abfd, sec,
  999. irel->r_offset + 2, 2))
  1000. goto error_return;
  1001. /* That will change things, so, we should relax again.
  1002. Note that this is not required, and it may be slow. */
  1003. *again = true;
  1004. }
  1005. }
  1006. /* Try to turn a 24bit pc-relative cmp&branch into
  1007. an 8bit pc-relative cmp&branch. */
  1008. if (ELF32_R_TYPE (irel->r_info) == (int) R_CRX_REL24)
  1009. {
  1010. bfd_vma value = symval;
  1011. /* Deal with pc-relative gunk. */
  1012. value -= (sec->output_section->vma + sec->output_offset);
  1013. value -= irel->r_offset;
  1014. value += irel->r_addend;
  1015. /* See if the value will fit in 8 bits, note the high value is
  1016. 0x7e + 2 as the target will be two bytes closer if we are
  1017. able to relax. */
  1018. if ((long) value < 0x100 && (long) value > -0x100)
  1019. {
  1020. unsigned short code;
  1021. /* Get the opcode. */
  1022. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1023. /* Verify it's a 'cmp&branch' opcode. */
  1024. if ((code & 0xfff0) != 0x3180 && (code & 0xfff0) != 0x3190
  1025. && (code & 0xfff0) != 0x31a0 && (code & 0xfff0) != 0x31c0
  1026. && (code & 0xfff0) != 0x31d0 && (code & 0xfff0) != 0x31e0
  1027. /* Or a Co-processor branch ('bcop'). */
  1028. && (code & 0xfff0) != 0x3010 && (code & 0xfff0) != 0x3110)
  1029. continue;
  1030. /* Note that we've changed the relocs, section contents, etc. */
  1031. elf_section_data (sec)->relocs = internal_relocs;
  1032. elf_section_data (sec)->this_hdr.contents = contents;
  1033. symtab_hdr->contents = (unsigned char *) isymbuf;
  1034. /* Fix the opcode. */
  1035. bfd_put_8 (abfd, 0x30, contents + irel->r_offset + 1);
  1036. /* Fix the relocation's type. */
  1037. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1038. R_CRX_REL8_CMP);
  1039. /* Delete two bytes of data. */
  1040. if (!elf32_crx_relax_delete_bytes (link_info, abfd, sec,
  1041. irel->r_offset + 4, 2))
  1042. goto error_return;
  1043. /* That will change things, so, we should relax again.
  1044. Note that this is not required, and it may be slow. */
  1045. *again = true;
  1046. }
  1047. }
  1048. /* Try to turn a 32bit immediate address into
  1049. a 16bit immediate address. */
  1050. if (ELF32_R_TYPE (irel->r_info) == (int) R_CRX_IMM32)
  1051. {
  1052. bfd_vma value = symval;
  1053. /* See if the value will fit in 16 bits. */
  1054. if ((long) value < 0x7fff && (long) value > -0x8000)
  1055. {
  1056. unsigned short code;
  1057. /* Get the opcode. */
  1058. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1059. /* Verify it's a 'arithmetic double'. */
  1060. if ((code & 0xf0f0) != 0x20f0)
  1061. continue;
  1062. /* Note that we've changed the relocs, section contents, etc. */
  1063. elf_section_data (sec)->relocs = internal_relocs;
  1064. elf_section_data (sec)->this_hdr.contents = contents;
  1065. symtab_hdr->contents = (unsigned char *) isymbuf;
  1066. /* Fix the opcode. */
  1067. bfd_put_8 (abfd, (code & 0xff) - 0x10, contents + irel->r_offset);
  1068. /* Fix the relocation's type. */
  1069. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1070. R_CRX_IMM16);
  1071. /* Delete two bytes of data. */
  1072. if (!elf32_crx_relax_delete_bytes (link_info, abfd, sec,
  1073. irel->r_offset + 2, 2))
  1074. goto error_return;
  1075. /* That will change things, so, we should relax again.
  1076. Note that this is not required, and it may be slow. */
  1077. *again = true;
  1078. }
  1079. }
  1080. }
  1081. if (isymbuf != NULL
  1082. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1083. {
  1084. if (! link_info->keep_memory)
  1085. free (isymbuf);
  1086. else
  1087. {
  1088. /* Cache the symbols for elf_link_input_bfd. */
  1089. symtab_hdr->contents = (unsigned char *) isymbuf;
  1090. }
  1091. }
  1092. if (contents != NULL
  1093. && elf_section_data (sec)->this_hdr.contents != contents)
  1094. {
  1095. if (! link_info->keep_memory)
  1096. free (contents);
  1097. else
  1098. {
  1099. /* Cache the section contents for elf_link_input_bfd. */
  1100. elf_section_data (sec)->this_hdr.contents = contents;
  1101. }
  1102. }
  1103. if (elf_section_data (sec)->relocs != internal_relocs)
  1104. free (internal_relocs);
  1105. return true;
  1106. error_return:
  1107. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  1108. free (isymbuf);
  1109. if (elf_section_data (sec)->this_hdr.contents != contents)
  1110. free (contents);
  1111. if (elf_section_data (sec)->relocs != internal_relocs)
  1112. free (internal_relocs);
  1113. return false;
  1114. }
  1115. /* Definitions for setting CRX target vector. */
  1116. #define TARGET_LITTLE_SYM crx_elf32_vec
  1117. #define TARGET_LITTLE_NAME "elf32-crx"
  1118. #define ELF_ARCH bfd_arch_crx
  1119. #define ELF_MACHINE_CODE EM_CRX
  1120. #define ELF_MAXPAGESIZE 0x1
  1121. #define elf_symbol_leading_char '_'
  1122. #define bfd_elf32_bfd_reloc_type_lookup elf_crx_reloc_type_lookup
  1123. #define bfd_elf32_bfd_reloc_name_lookup \
  1124. elf_crx_reloc_name_lookup
  1125. #define elf_info_to_howto elf_crx_info_to_howto
  1126. #define elf_info_to_howto_rel NULL
  1127. #define elf_backend_relocate_section elf32_crx_relocate_section
  1128. #define bfd_elf32_bfd_relax_section elf32_crx_relax_section
  1129. #define bfd_elf32_bfd_get_relocated_section_contents \
  1130. elf32_crx_get_relocated_section_contents
  1131. #define elf_backend_can_gc_sections 1
  1132. #define elf_backend_rela_normal 1
  1133. #include "elf32-target.h"