elf32-crx.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /* BFD back-end for National Semiconductor's CRX ELF
  2. Copyright (C) 2004-2015 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 void elf_crx_info_to_howto
  26. (bfd *, arelent *, Elf_Internal_Rela *);
  27. static bfd_boolean 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 bfd_boolean elf32_crx_relocate_section
  34. (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  35. Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
  36. static bfd_boolean elf32_crx_relax_section
  37. (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
  38. static bfd_byte * elf32_crx_get_relocated_section_contents
  39. (bfd *, struct bfd_link_info *, struct bfd_link_order *,
  40. bfd_byte *, bfd_boolean, 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 void
  383. elf_crx_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, 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. (*_bfd_error_handler) (_("%B: unrecognised CRX reloc number: %d"),
  390. abfd, r_type);
  391. bfd_set_error (bfd_error_bad_value);
  392. r_type = R_CRX_NONE;
  393. }
  394. cache_ptr->howto = &crx_elf_howto_table[r_type];
  395. }
  396. /* Perform a relocation as part of a final link. */
  397. static bfd_reloc_status_type
  398. crx_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
  399. bfd *output_bfd ATTRIBUTE_UNUSED,
  400. asection *input_section, bfd_byte *contents,
  401. bfd_vma offset, bfd_vma Rvalue, bfd_vma addend,
  402. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  403. asection *sec ATTRIBUTE_UNUSED,
  404. int is_local ATTRIBUTE_UNUSED)
  405. {
  406. unsigned short r_type = howto->type;
  407. bfd_byte *hit_data = contents + offset;
  408. bfd_vma reloc_bits, check;
  409. switch (r_type)
  410. {
  411. case R_CRX_IMM16:
  412. case R_CRX_IMM32:
  413. case R_CRX_ABS16:
  414. case R_CRX_ABS32:
  415. case R_CRX_REL8_CMP:
  416. case R_CRX_REL16:
  417. case R_CRX_REL24:
  418. case R_CRX_REL32:
  419. case R_CRX_REGREL12:
  420. case R_CRX_REGREL22:
  421. case R_CRX_REGREL28:
  422. case R_CRX_REGREL32:
  423. /* 'hit_data' is relative to the start of the instruction, not the
  424. relocation offset. Advance it to account for the exact offset. */
  425. hit_data += 2;
  426. break;
  427. case R_CRX_REL4:
  428. /* This relocation type is used only in 'Branch if Equal to 0'
  429. instructions and requires special handling. */
  430. Rvalue -= 1;
  431. break;
  432. case R_CRX_NONE:
  433. return bfd_reloc_ok;
  434. break;
  435. case R_CRX_SWITCH8:
  436. case R_CRX_SWITCH16:
  437. case R_CRX_SWITCH32:
  438. /* We only care about the addend, where the difference between
  439. expressions is kept. */
  440. Rvalue = 0;
  441. default:
  442. break;
  443. }
  444. if (howto->pc_relative)
  445. {
  446. /* Subtract the address of the section containing the location. */
  447. Rvalue -= (input_section->output_section->vma
  448. + input_section->output_offset);
  449. /* Subtract the position of the location within the section. */
  450. Rvalue -= offset;
  451. }
  452. /* Add in supplied addend. */
  453. Rvalue += addend;
  454. /* Complain if the bitfield overflows, whether it is considered
  455. as signed or unsigned. */
  456. check = Rvalue >> howto->rightshift;
  457. /* Assumes two's complement. This expression avoids
  458. overflow if howto->bitsize is the number of bits in
  459. bfd_vma. */
  460. reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  461. if (((bfd_vma) check & ~reloc_bits) != 0
  462. && (((bfd_vma) check & ~reloc_bits)
  463. != (-(bfd_vma) 1 & ~reloc_bits)))
  464. {
  465. /* The above right shift is incorrect for a signed
  466. value. See if turning on the upper bits fixes the
  467. overflow. */
  468. if (howto->rightshift && (bfd_signed_vma) Rvalue < 0)
  469. {
  470. check |= ((bfd_vma) - 1
  471. & ~((bfd_vma) - 1
  472. >> howto->rightshift));
  473. if (((bfd_vma) check & ~reloc_bits)
  474. != (-(bfd_vma) 1 & ~reloc_bits))
  475. return bfd_reloc_overflow;
  476. }
  477. else
  478. return bfd_reloc_overflow;
  479. }
  480. /* Drop unwanted bits from the value we are relocating to. */
  481. Rvalue >>= (bfd_vma) howto->rightshift;
  482. /* Apply dst_mask to select only relocatable part of the insn. */
  483. Rvalue &= howto->dst_mask;
  484. switch (howto->size)
  485. {
  486. case 0:
  487. if (r_type == R_CRX_REL4)
  488. {
  489. Rvalue <<= 4;
  490. Rvalue |= (bfd_get_8 (input_bfd, hit_data) & 0x0f);
  491. }
  492. bfd_put_8 (input_bfd, (unsigned char) Rvalue, hit_data);
  493. break;
  494. case 1:
  495. if (r_type == R_CRX_REGREL12)
  496. Rvalue |= (bfd_get_16 (input_bfd, hit_data) & 0xf000);
  497. bfd_put_16 (input_bfd, Rvalue, hit_data);
  498. break;
  499. case 2:
  500. if (r_type == R_CRX_REL24
  501. || r_type == R_CRX_REGREL22
  502. || r_type == R_CRX_REGREL28)
  503. Rvalue |= (((bfd_get_16 (input_bfd, hit_data) << 16) |
  504. bfd_get_16 (input_bfd, hit_data + 2)) & ~howto->dst_mask);
  505. if (r_type == R_CRX_NUM32 || r_type == R_CRX_SWITCH32)
  506. /* Relocation on DATA is purely little-endian, that is, for a
  507. multi-byte datum, the lowest address in memory contains the
  508. little end of the datum, that is, the least significant byte.
  509. Therefore we use BFD's byte Putting functions. */
  510. bfd_put_32 (input_bfd, Rvalue, hit_data);
  511. else
  512. /* Relocation on INSTRUCTIONS is different : Instructions are
  513. word-addressable, that is, each word itself is arranged according
  514. to little-endian convention, whereas the words are arranged with
  515. respect to one another in BIG ENDIAN fashion.
  516. When there is an immediate value that spans a word boundary, it is
  517. split in a big-endian way with respect to the words. */
  518. {
  519. bfd_put_16 (input_bfd, (Rvalue >> 16) & 0xffff, hit_data);
  520. bfd_put_16 (input_bfd, Rvalue & 0xffff, hit_data + 2);
  521. }
  522. break;
  523. default:
  524. return bfd_reloc_notsupported;
  525. }
  526. return bfd_reloc_ok;
  527. }
  528. /* Delete some bytes from a section while relaxing. */
  529. static bfd_boolean
  530. elf32_crx_relax_delete_bytes (struct bfd_link_info *link_info, bfd *abfd,
  531. asection *sec, bfd_vma addr, int count)
  532. {
  533. Elf_Internal_Shdr *symtab_hdr;
  534. unsigned int sec_shndx;
  535. bfd_byte *contents;
  536. Elf_Internal_Rela *irel, *irelend;
  537. bfd_vma toaddr;
  538. Elf_Internal_Sym *isym;
  539. Elf_Internal_Sym *isymend;
  540. struct elf_link_hash_entry **sym_hashes;
  541. struct elf_link_hash_entry **end_hashes;
  542. struct elf_link_hash_entry **start_hashes;
  543. unsigned int symcount;
  544. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  545. contents = elf_section_data (sec)->this_hdr.contents;
  546. toaddr = sec->size;
  547. irel = elf_section_data (sec)->relocs;
  548. irelend = irel + sec->reloc_count;
  549. /* Actually delete the bytes. */
  550. memmove (contents + addr, contents + addr + count,
  551. (size_t) (toaddr - addr - count));
  552. sec->size -= count;
  553. /* Adjust all the relocs. */
  554. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  555. {
  556. /* Get the new reloc address. */
  557. if ((irel->r_offset > addr
  558. && irel->r_offset < toaddr))
  559. irel->r_offset -= count;
  560. }
  561. /* Adjust the local symbols defined in this section. */
  562. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  563. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  564. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  565. {
  566. if (isym->st_shndx == sec_shndx
  567. && isym->st_value > addr
  568. && isym->st_value < toaddr)
  569. {
  570. /* Adjust the addend of SWITCH relocations in this section,
  571. which reference this local symbol. */
  572. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  573. {
  574. unsigned long r_symndx;
  575. Elf_Internal_Sym *rsym;
  576. bfd_vma addsym, subsym;
  577. /* Skip if not a SWITCH relocation. */
  578. if (ELF32_R_TYPE (irel->r_info) != (int) R_CRX_SWITCH8
  579. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_SWITCH16
  580. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_SWITCH32)
  581. continue;
  582. r_symndx = ELF32_R_SYM (irel->r_info);
  583. rsym = (Elf_Internal_Sym *) symtab_hdr->contents + r_symndx;
  584. /* Skip if not the local adjusted symbol. */
  585. if (rsym != isym)
  586. continue;
  587. addsym = isym->st_value;
  588. subsym = addsym - irel->r_addend;
  589. /* Fix the addend only when -->> (addsym > addr >= subsym). */
  590. if (subsym <= addr)
  591. irel->r_addend -= count;
  592. else
  593. continue;
  594. }
  595. isym->st_value -= count;
  596. }
  597. }
  598. /* Now adjust the global symbols defined in this section. */
  599. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  600. - symtab_hdr->sh_info);
  601. sym_hashes = start_hashes = elf_sym_hashes (abfd);
  602. end_hashes = sym_hashes + symcount;
  603. for (; sym_hashes < end_hashes; sym_hashes++)
  604. {
  605. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  606. /* The '--wrap SYMBOL' option is causing a pain when the object file,
  607. containing the definition of __wrap_SYMBOL, includes a direct
  608. call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
  609. the same symbol (which is __wrap_SYMBOL), but still exist as two
  610. different symbols in 'sym_hashes', we don't want to adjust
  611. the global symbol __wrap_SYMBOL twice.
  612. This check is only relevant when symbols are being wrapped. */
  613. if (link_info->wrap_hash != NULL)
  614. {
  615. struct elf_link_hash_entry **cur_sym_hashes;
  616. /* Loop only over the symbols whom been already checked. */
  617. for (cur_sym_hashes = start_hashes; cur_sym_hashes < sym_hashes;
  618. cur_sym_hashes++)
  619. {
  620. /* If the current symbol is identical to 'sym_hash', that means
  621. the symbol was already adjusted (or at least checked). */
  622. if (*cur_sym_hashes == sym_hash)
  623. break;
  624. }
  625. /* Don't adjust the symbol again. */
  626. if (cur_sym_hashes < sym_hashes)
  627. continue;
  628. }
  629. if ((sym_hash->root.type == bfd_link_hash_defined
  630. || sym_hash->root.type == bfd_link_hash_defweak)
  631. && sym_hash->root.u.def.section == sec
  632. && sym_hash->root.u.def.value > addr
  633. && sym_hash->root.u.def.value < toaddr)
  634. sym_hash->root.u.def.value -= count;
  635. }
  636. return TRUE;
  637. }
  638. /* This is a version of bfd_generic_get_relocated_section_contents
  639. which uses elf32_crx_relocate_section. */
  640. static bfd_byte *
  641. elf32_crx_get_relocated_section_contents (bfd *output_bfd,
  642. struct bfd_link_info *link_info,
  643. struct bfd_link_order *link_order,
  644. bfd_byte *data,
  645. bfd_boolean relocatable,
  646. asymbol **symbols)
  647. {
  648. Elf_Internal_Shdr *symtab_hdr;
  649. asection *input_section = link_order->u.indirect.section;
  650. bfd *input_bfd = input_section->owner;
  651. asection **sections = NULL;
  652. Elf_Internal_Rela *internal_relocs = NULL;
  653. Elf_Internal_Sym *isymbuf = NULL;
  654. /* We only need to handle the case of relaxing, or of having a
  655. particular set of section contents, specially. */
  656. if (relocatable
  657. || elf_section_data (input_section)->this_hdr.contents == NULL)
  658. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  659. link_order, data,
  660. relocatable,
  661. symbols);
  662. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  663. memcpy (data, elf_section_data (input_section)->this_hdr.contents,
  664. (size_t) input_section->size);
  665. if ((input_section->flags & SEC_RELOC) != 0
  666. && input_section->reloc_count > 0)
  667. {
  668. Elf_Internal_Sym *isym;
  669. Elf_Internal_Sym *isymend;
  670. asection **secpp;
  671. bfd_size_type amt;
  672. internal_relocs = (_bfd_elf_link_read_relocs
  673. (input_bfd, input_section, NULL,
  674. (Elf_Internal_Rela *) NULL, FALSE));
  675. if (internal_relocs == NULL)
  676. goto error_return;
  677. if (symtab_hdr->sh_info != 0)
  678. {
  679. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  680. if (isymbuf == NULL)
  681. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  682. symtab_hdr->sh_info, 0,
  683. NULL, NULL, NULL);
  684. if (isymbuf == NULL)
  685. goto error_return;
  686. }
  687. amt = symtab_hdr->sh_info;
  688. amt *= sizeof (asection *);
  689. sections = bfd_malloc (amt);
  690. if (sections == NULL && amt != 0)
  691. goto error_return;
  692. isymend = isymbuf + symtab_hdr->sh_info;
  693. for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
  694. {
  695. asection *isec;
  696. if (isym->st_shndx == SHN_UNDEF)
  697. isec = bfd_und_section_ptr;
  698. else if (isym->st_shndx == SHN_ABS)
  699. isec = bfd_abs_section_ptr;
  700. else if (isym->st_shndx == SHN_COMMON)
  701. isec = bfd_com_section_ptr;
  702. else
  703. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  704. *secpp = isec;
  705. }
  706. if (! elf32_crx_relocate_section (output_bfd, link_info, input_bfd,
  707. input_section, data, internal_relocs,
  708. isymbuf, sections))
  709. goto error_return;
  710. if (sections != NULL)
  711. free (sections);
  712. if (isymbuf != NULL
  713. && symtab_hdr->contents != (unsigned char *) isymbuf)
  714. free (isymbuf);
  715. if (elf_section_data (input_section)->relocs != internal_relocs)
  716. free (internal_relocs);
  717. }
  718. return data;
  719. error_return:
  720. if (sections != NULL)
  721. free (sections);
  722. if (isymbuf != NULL
  723. && symtab_hdr->contents != (unsigned char *) isymbuf)
  724. free (isymbuf);
  725. if (internal_relocs != NULL
  726. && elf_section_data (input_section)->relocs != internal_relocs)
  727. free (internal_relocs);
  728. return NULL;
  729. }
  730. /* Relocate a CRX ELF section. */
  731. static bfd_boolean
  732. elf32_crx_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
  733. bfd *input_bfd, asection *input_section,
  734. bfd_byte *contents, Elf_Internal_Rela *relocs,
  735. Elf_Internal_Sym *local_syms,
  736. asection **local_sections)
  737. {
  738. Elf_Internal_Shdr *symtab_hdr;
  739. struct elf_link_hash_entry **sym_hashes;
  740. Elf_Internal_Rela *rel, *relend;
  741. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  742. sym_hashes = elf_sym_hashes (input_bfd);
  743. rel = relocs;
  744. relend = relocs + input_section->reloc_count;
  745. for (; rel < relend; rel++)
  746. {
  747. int r_type;
  748. reloc_howto_type *howto;
  749. unsigned long r_symndx;
  750. Elf_Internal_Sym *sym;
  751. asection *sec;
  752. struct elf_link_hash_entry *h;
  753. bfd_vma relocation;
  754. bfd_reloc_status_type r;
  755. r_symndx = ELF32_R_SYM (rel->r_info);
  756. r_type = ELF32_R_TYPE (rel->r_info);
  757. howto = crx_elf_howto_table + (r_type);
  758. h = NULL;
  759. sym = NULL;
  760. sec = NULL;
  761. if (r_symndx < symtab_hdr->sh_info)
  762. {
  763. sym = local_syms + r_symndx;
  764. sec = local_sections[r_symndx];
  765. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  766. }
  767. else
  768. {
  769. bfd_boolean unresolved_reloc, warned, ignored;
  770. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  771. r_symndx, symtab_hdr, sym_hashes,
  772. h, sec, relocation,
  773. unresolved_reloc, warned, ignored);
  774. }
  775. if (sec != NULL && discarded_section (sec))
  776. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  777. rel, 1, relend, howto, 0, contents);
  778. if (bfd_link_relocatable (info))
  779. continue;
  780. r = crx_elf_final_link_relocate (howto, input_bfd, output_bfd,
  781. input_section,
  782. contents, rel->r_offset,
  783. relocation, rel->r_addend,
  784. info, sec, h == NULL);
  785. if (r != bfd_reloc_ok)
  786. {
  787. const char *name;
  788. const char *msg = (const char *) 0;
  789. if (h != NULL)
  790. name = h->root.root.string;
  791. else
  792. {
  793. name = (bfd_elf_string_from_elf_section
  794. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  795. if (name == NULL || *name == '\0')
  796. name = bfd_section_name (input_bfd, sec);
  797. }
  798. switch (r)
  799. {
  800. case bfd_reloc_overflow:
  801. if (!((*info->callbacks->reloc_overflow)
  802. (info, (h ? &h->root : NULL), name, howto->name,
  803. (bfd_vma) 0, input_bfd, input_section,
  804. rel->r_offset)))
  805. return FALSE;
  806. break;
  807. case bfd_reloc_undefined:
  808. if (!((*info->callbacks->undefined_symbol)
  809. (info, name, input_bfd, input_section,
  810. rel->r_offset, TRUE)))
  811. return FALSE;
  812. break;
  813. case bfd_reloc_outofrange:
  814. msg = _("internal error: out of range error");
  815. goto common_error;
  816. case bfd_reloc_notsupported:
  817. msg = _("internal error: unsupported relocation error");
  818. goto common_error;
  819. case bfd_reloc_dangerous:
  820. msg = _("internal error: dangerous error");
  821. goto common_error;
  822. default:
  823. msg = _("internal error: unknown error");
  824. /* Fall through. */
  825. common_error:
  826. if (!((*info->callbacks->warning)
  827. (info, msg, name, input_bfd, input_section,
  828. rel->r_offset)))
  829. return FALSE;
  830. break;
  831. }
  832. }
  833. }
  834. return TRUE;
  835. }
  836. /* This function handles relaxing for the CRX.
  837. There's quite a few relaxing opportunites available on the CRX:
  838. * bal/bcond:32 -> bal/bcond:16 2 bytes
  839. * bcond:16 -> bcond:8 2 bytes
  840. * cmpbcond:24 -> cmpbcond:8 2 bytes
  841. * arithmetic imm32 -> arithmetic imm16 2 bytes
  842. Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
  843. static bfd_boolean
  844. elf32_crx_relax_section (bfd *abfd, asection *sec,
  845. struct bfd_link_info *link_info, bfd_boolean *again)
  846. {
  847. Elf_Internal_Shdr *symtab_hdr;
  848. Elf_Internal_Rela *internal_relocs;
  849. Elf_Internal_Rela *irel, *irelend;
  850. bfd_byte *contents = NULL;
  851. Elf_Internal_Sym *isymbuf = NULL;
  852. /* Assume nothing changes. */
  853. *again = FALSE;
  854. /* We don't have to do anything for a relocatable link, if
  855. this section does not have relocs, or if this is not a
  856. code section. */
  857. if (bfd_link_relocatable (link_info)
  858. || (sec->flags & SEC_RELOC) == 0
  859. || sec->reloc_count == 0
  860. || (sec->flags & SEC_CODE) == 0)
  861. return TRUE;
  862. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  863. /* Get a copy of the native relocations. */
  864. internal_relocs = (_bfd_elf_link_read_relocs
  865. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  866. link_info->keep_memory));
  867. if (internal_relocs == NULL)
  868. goto error_return;
  869. /* Walk through them looking for relaxing opportunities. */
  870. irelend = internal_relocs + sec->reloc_count;
  871. for (irel = internal_relocs; irel < irelend; irel++)
  872. {
  873. bfd_vma symval;
  874. /* If this isn't something that can be relaxed, then ignore
  875. this reloc. */
  876. if (ELF32_R_TYPE (irel->r_info) != (int) R_CRX_REL32
  877. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_REL16
  878. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_REL24
  879. && ELF32_R_TYPE (irel->r_info) != (int) R_CRX_IMM32)
  880. continue;
  881. /* Get the section contents if we haven't done so already. */
  882. if (contents == NULL)
  883. {
  884. /* Get cached copy if it exists. */
  885. if (elf_section_data (sec)->this_hdr.contents != NULL)
  886. contents = elf_section_data (sec)->this_hdr.contents;
  887. /* Go get them off disk. */
  888. else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  889. goto error_return;
  890. }
  891. /* Read this BFD's local symbols if we haven't done so already. */
  892. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  893. {
  894. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  895. if (isymbuf == NULL)
  896. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  897. symtab_hdr->sh_info, 0,
  898. NULL, NULL, NULL);
  899. if (isymbuf == NULL)
  900. goto error_return;
  901. }
  902. /* Get the value of the symbol referred to by the reloc. */
  903. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  904. {
  905. /* A local symbol. */
  906. Elf_Internal_Sym *isym;
  907. asection *sym_sec;
  908. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  909. if (isym->st_shndx == SHN_UNDEF)
  910. sym_sec = bfd_und_section_ptr;
  911. else if (isym->st_shndx == SHN_ABS)
  912. sym_sec = bfd_abs_section_ptr;
  913. else if (isym->st_shndx == SHN_COMMON)
  914. sym_sec = bfd_com_section_ptr;
  915. else
  916. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  917. symval = (isym->st_value
  918. + sym_sec->output_section->vma
  919. + sym_sec->output_offset);
  920. }
  921. else
  922. {
  923. unsigned long indx;
  924. struct elf_link_hash_entry *h;
  925. /* An external symbol. */
  926. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  927. h = elf_sym_hashes (abfd)[indx];
  928. BFD_ASSERT (h != NULL);
  929. if (h->root.type != bfd_link_hash_defined
  930. && h->root.type != bfd_link_hash_defweak)
  931. /* This appears to be a reference to an undefined
  932. symbol. Just ignore it--it will be caught by the
  933. regular reloc processing. */
  934. continue;
  935. symval = (h->root.u.def.value
  936. + h->root.u.def.section->output_section->vma
  937. + h->root.u.def.section->output_offset);
  938. }
  939. /* For simplicity of coding, we are going to modify the section
  940. contents, the section relocs, and the BFD symbol table. We
  941. must tell the rest of the code not to free up this
  942. information. It would be possible to instead create a table
  943. of changes which have to be made, as is done in coff-mips.c;
  944. that would be more work, but would require less memory when
  945. the linker is run. */
  946. /* Try to turn a 32bit pc-relative branch/call into
  947. a 16bit pc-relative branch/call. */
  948. if (ELF32_R_TYPE (irel->r_info) == (int) R_CRX_REL32)
  949. {
  950. bfd_vma value = symval;
  951. /* Deal with pc-relative gunk. */
  952. value -= (sec->output_section->vma + sec->output_offset);
  953. value -= irel->r_offset;
  954. value += irel->r_addend;
  955. /* See if the value will fit in 16 bits, note the high value is
  956. 0xfffe + 2 as the target will be two bytes closer if we are
  957. able to relax. */
  958. if ((long) value < 0x10000 && (long) value > -0x10002)
  959. {
  960. unsigned short code;
  961. /* Get the opcode. */
  962. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  963. /* Verify it's a 'bal'/'bcond' and fix the opcode. */
  964. if ((code & 0xfff0) == 0x3170)
  965. bfd_put_8 (abfd, 0x30, contents + irel->r_offset + 1);
  966. else if ((code & 0xf0ff) == 0x707f)
  967. bfd_put_8 (abfd, 0x7e, contents + irel->r_offset);
  968. else
  969. continue;
  970. /* Note that we've changed the relocs, section contents, etc. */
  971. elf_section_data (sec)->relocs = internal_relocs;
  972. elf_section_data (sec)->this_hdr.contents = contents;
  973. symtab_hdr->contents = (unsigned char *) isymbuf;
  974. /* Fix the relocation's type. */
  975. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  976. R_CRX_REL16);
  977. /* Delete two bytes of data. */
  978. if (!elf32_crx_relax_delete_bytes (link_info, abfd, sec,
  979. irel->r_offset + 2, 2))
  980. goto error_return;
  981. /* That will change things, so, we should relax again.
  982. Note that this is not required, and it may be slow. */
  983. *again = TRUE;
  984. }
  985. }
  986. /* Try to turn a 16bit pc-relative branch into an
  987. 8bit pc-relative branch. */
  988. if (ELF32_R_TYPE (irel->r_info) == (int) R_CRX_REL16)
  989. {
  990. bfd_vma value = symval;
  991. /* Deal with pc-relative gunk. */
  992. value -= (sec->output_section->vma + sec->output_offset);
  993. value -= irel->r_offset;
  994. value += irel->r_addend;
  995. /* See if the value will fit in 8 bits, note the high value is
  996. 0xfc + 2 as the target will be two bytes closer if we are
  997. able to relax. */
  998. if ((long) value < 0xfe && (long) value > -0x100)
  999. {
  1000. unsigned short code;
  1001. /* Get the opcode. */
  1002. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1003. /* Verify it's a 'bcond' opcode. */
  1004. if ((code & 0xf0ff) != 0x707e)
  1005. continue;
  1006. /* Note that we've changed the relocs, section contents, etc. */
  1007. elf_section_data (sec)->relocs = internal_relocs;
  1008. elf_section_data (sec)->this_hdr.contents = contents;
  1009. symtab_hdr->contents = (unsigned char *) isymbuf;
  1010. /* Fix the relocation's type. */
  1011. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1012. R_CRX_REL8);
  1013. /* Delete two bytes of data. */
  1014. if (!elf32_crx_relax_delete_bytes (link_info, abfd, sec,
  1015. irel->r_offset + 2, 2))
  1016. goto error_return;
  1017. /* That will change things, so, we should relax again.
  1018. Note that this is not required, and it may be slow. */
  1019. *again = TRUE;
  1020. }
  1021. }
  1022. /* Try to turn a 24bit pc-relative cmp&branch into
  1023. an 8bit pc-relative cmp&branch. */
  1024. if (ELF32_R_TYPE (irel->r_info) == (int) R_CRX_REL24)
  1025. {
  1026. bfd_vma value = symval;
  1027. /* Deal with pc-relative gunk. */
  1028. value -= (sec->output_section->vma + sec->output_offset);
  1029. value -= irel->r_offset;
  1030. value += irel->r_addend;
  1031. /* See if the value will fit in 8 bits, note the high value is
  1032. 0x7e + 2 as the target will be two bytes closer if we are
  1033. able to relax. */
  1034. if ((long) value < 0x100 && (long) value > -0x100)
  1035. {
  1036. unsigned short code;
  1037. /* Get the opcode. */
  1038. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1039. /* Verify it's a 'cmp&branch' opcode. */
  1040. if ((code & 0xfff0) != 0x3180 && (code & 0xfff0) != 0x3190
  1041. && (code & 0xfff0) != 0x31a0 && (code & 0xfff0) != 0x31c0
  1042. && (code & 0xfff0) != 0x31d0 && (code & 0xfff0) != 0x31e0
  1043. /* Or a Co-processor branch ('bcop'). */
  1044. && (code & 0xfff0) != 0x3010 && (code & 0xfff0) != 0x3110)
  1045. continue;
  1046. /* Note that we've changed the relocs, section contents, etc. */
  1047. elf_section_data (sec)->relocs = internal_relocs;
  1048. elf_section_data (sec)->this_hdr.contents = contents;
  1049. symtab_hdr->contents = (unsigned char *) isymbuf;
  1050. /* Fix the opcode. */
  1051. bfd_put_8 (abfd, 0x30, contents + irel->r_offset + 1);
  1052. /* Fix the relocation's type. */
  1053. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1054. R_CRX_REL8_CMP);
  1055. /* Delete two bytes of data. */
  1056. if (!elf32_crx_relax_delete_bytes (link_info, abfd, sec,
  1057. irel->r_offset + 4, 2))
  1058. goto error_return;
  1059. /* That will change things, so, we should relax again.
  1060. Note that this is not required, and it may be slow. */
  1061. *again = TRUE;
  1062. }
  1063. }
  1064. /* Try to turn a 32bit immediate address into
  1065. a 16bit immediate address. */
  1066. if (ELF32_R_TYPE (irel->r_info) == (int) R_CRX_IMM32)
  1067. {
  1068. bfd_vma value = symval;
  1069. /* See if the value will fit in 16 bits. */
  1070. if ((long) value < 0x7fff && (long) value > -0x8000)
  1071. {
  1072. unsigned short code;
  1073. /* Get the opcode. */
  1074. code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
  1075. /* Verify it's a 'arithmetic double'. */
  1076. if ((code & 0xf0f0) != 0x20f0)
  1077. continue;
  1078. /* Note that we've changed the relocs, section contents, etc. */
  1079. elf_section_data (sec)->relocs = internal_relocs;
  1080. elf_section_data (sec)->this_hdr.contents = contents;
  1081. symtab_hdr->contents = (unsigned char *) isymbuf;
  1082. /* Fix the opcode. */
  1083. bfd_put_8 (abfd, (code & 0xff) - 0x10, contents + irel->r_offset);
  1084. /* Fix the relocation's type. */
  1085. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1086. R_CRX_IMM16);
  1087. /* Delete two bytes of data. */
  1088. if (!elf32_crx_relax_delete_bytes (link_info, abfd, sec,
  1089. irel->r_offset + 2, 2))
  1090. goto error_return;
  1091. /* That will change things, so, we should relax again.
  1092. Note that this is not required, and it may be slow. */
  1093. *again = TRUE;
  1094. }
  1095. }
  1096. }
  1097. if (isymbuf != NULL
  1098. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1099. {
  1100. if (! link_info->keep_memory)
  1101. free (isymbuf);
  1102. else
  1103. {
  1104. /* Cache the symbols for elf_link_input_bfd. */
  1105. symtab_hdr->contents = (unsigned char *) isymbuf;
  1106. }
  1107. }
  1108. if (contents != NULL
  1109. && elf_section_data (sec)->this_hdr.contents != contents)
  1110. {
  1111. if (! link_info->keep_memory)
  1112. free (contents);
  1113. else
  1114. {
  1115. /* Cache the section contents for elf_link_input_bfd. */
  1116. elf_section_data (sec)->this_hdr.contents = contents;
  1117. }
  1118. }
  1119. if (internal_relocs != NULL
  1120. && elf_section_data (sec)->relocs != internal_relocs)
  1121. free (internal_relocs);
  1122. return TRUE;
  1123. error_return:
  1124. if (isymbuf != NULL
  1125. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1126. free (isymbuf);
  1127. if (contents != NULL
  1128. && elf_section_data (sec)->this_hdr.contents != contents)
  1129. free (contents);
  1130. if (internal_relocs != NULL
  1131. && elf_section_data (sec)->relocs != internal_relocs)
  1132. free (internal_relocs);
  1133. return FALSE;
  1134. }
  1135. /* Definitions for setting CRX target vector. */
  1136. #define TARGET_LITTLE_SYM crx_elf32_vec
  1137. #define TARGET_LITTLE_NAME "elf32-crx"
  1138. #define ELF_ARCH bfd_arch_crx
  1139. #define ELF_MACHINE_CODE EM_CRX
  1140. #define ELF_MAXPAGESIZE 0x1
  1141. #define elf_symbol_leading_char '_'
  1142. #define bfd_elf32_bfd_reloc_type_lookup elf_crx_reloc_type_lookup
  1143. #define bfd_elf32_bfd_reloc_name_lookup \
  1144. elf_crx_reloc_name_lookup
  1145. #define elf_info_to_howto elf_crx_info_to_howto
  1146. #define elf_info_to_howto_rel 0
  1147. #define elf_backend_relocate_section elf32_crx_relocate_section
  1148. #define bfd_elf32_bfd_relax_section elf32_crx_relax_section
  1149. #define bfd_elf32_bfd_get_relocated_section_contents \
  1150. elf32_crx_get_relocated_section_contents
  1151. #define elf_backend_can_gc_sections 1
  1152. #define elf_backend_rela_normal 1
  1153. #include "elf32-target.h"