elf32-d10v.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /* D10V-specific support for 32-bit ELF
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. Contributed by Martin Hunt (hunt@cygnus.com).
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "elf-bfd.h"
  21. #include "elf/d10v.h"
  22. /* Use REL instead of RELA to save space. */
  23. #define USE_REL 1
  24. static reloc_howto_type elf_d10v_howto_table[] =
  25. {
  26. /* This reloc does nothing. */
  27. HOWTO (R_D10V_NONE, /* Type. */
  28. 0, /* Rightshift. */
  29. 3, /* Size (0 = byte, 1 = short, 2 = long). */
  30. 0, /* Bitsize. */
  31. FALSE, /* PC_relative. */
  32. 0, /* Bitpos. */
  33. complain_overflow_dont,/* Complain_on_overflow. */
  34. bfd_elf_generic_reloc, /* Special_function. */
  35. "R_D10V_NONE", /* Name. */
  36. FALSE, /* Partial_inplace. */
  37. 0, /* Src_mask. */
  38. 0, /* Dst_mask. */
  39. FALSE), /* PCrel_offset. */
  40. /* An PC Relative 10-bit relocation, shifted by 2, right container. */
  41. HOWTO (R_D10V_10_PCREL_R, /* Type. */
  42. 2, /* Rightshift. */
  43. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  44. 8, /* Bitsize. */
  45. TRUE, /* PC_relative. */
  46. 0, /* Bitpos. */
  47. complain_overflow_signed, /* Complain_on_overflow. */
  48. bfd_elf_generic_reloc, /* Special_function. */
  49. "R_D10V_10_PCREL_R", /* Name. */
  50. FALSE, /* Partial_inplace. */
  51. 0xff, /* Src_mask. */
  52. 0xff, /* Dst_mask. */
  53. TRUE), /* PCrel_offset. */
  54. /* An PC Relative 10-bit relocation, shifted by 2, left container. */
  55. HOWTO (R_D10V_10_PCREL_L, /* Type. */
  56. 2, /* Rightshift. */
  57. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  58. 8, /* Bitsize. */
  59. TRUE, /* PC_relative. */
  60. 15, /* Bitpos. */
  61. complain_overflow_signed, /* Complain_on_overflow. */
  62. bfd_elf_generic_reloc, /* Special_function. */
  63. "R_D10V_10_PCREL_L", /* Name. */
  64. FALSE, /* Partial_inplace. */
  65. 0x07f8000, /* Src_mask. */
  66. 0x07f8000, /* Dst_mask. */
  67. TRUE), /* PCrel_offset. */
  68. /* A 16 bit absolute relocation. */
  69. HOWTO (R_D10V_16, /* Type. */
  70. 0, /* Rightshift. */
  71. 1, /* Size (0 = byte, 1 = short, 2 = long). */
  72. 16, /* Bitsize. */
  73. FALSE, /* PC_relative. */
  74. 0, /* Bitpos. */
  75. complain_overflow_dont,/* Complain_on_overflow. */
  76. bfd_elf_generic_reloc, /* Special_function. */
  77. "R_D10V_16", /* Name. */
  78. FALSE, /* Partial_inplace. */
  79. 0xffff, /* Src_mask. */
  80. 0xffff, /* Dst_mask. */
  81. FALSE), /* PCrel_offset. */
  82. /* An 18 bit absolute relocation, right shifted 2. */
  83. HOWTO (R_D10V_18, /* Type. */
  84. 2, /* Rightshift. */
  85. 1, /* Size (0 = byte, 1 = short, 2 = long). */
  86. 16, /* Bitsize. */
  87. FALSE, /* PC_relative. */
  88. 0, /* Bitpos. */
  89. complain_overflow_dont, /* Complain_on_overflow. */
  90. bfd_elf_generic_reloc, /* Special_function. */
  91. "R_D10V_18", /* Name. */
  92. FALSE, /* Partial_inplace. */
  93. 0xffff, /* Src_mask. */
  94. 0xffff, /* Dst_mask. */
  95. FALSE), /* PCrel_offset. */
  96. /* A relative 18 bit relocation, right shifted by 2. */
  97. HOWTO (R_D10V_18_PCREL, /* Type. */
  98. 2, /* Rightshift. */
  99. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  100. 16, /* Bitsize. */
  101. TRUE, /* PC_relative. */
  102. 0, /* Bitpos. */
  103. complain_overflow_signed, /* Complain_on_overflow. */
  104. bfd_elf_generic_reloc, /* Special_function. */
  105. "R_D10V_18_PCREL", /* Name. */
  106. FALSE, /* Partial_inplace. */
  107. 0xffff, /* Src_mask. */
  108. 0xffff, /* Dst_mask. */
  109. TRUE), /* PCrel_offset. */
  110. /* A 32 bit absolute relocation. */
  111. HOWTO (R_D10V_32, /* Type. */
  112. 0, /* Rightshift. */
  113. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  114. 32, /* Bitsize. */
  115. FALSE, /* PC_relative. */
  116. 0, /* Bitpos. */
  117. complain_overflow_dont,/* Complain_on_overflow. */
  118. bfd_elf_generic_reloc, /* Special_function. */
  119. "R_D10V_32", /* Name. */
  120. FALSE, /* Partial_inplace. */
  121. 0xffffffff, /* Src_mask. */
  122. 0xffffffff, /* Dst_mask. */
  123. FALSE), /* PCrel_offset. */
  124. /* GNU extension to record C++ vtable hierarchy. */
  125. HOWTO (R_D10V_GNU_VTINHERIT, /* Type. */
  126. 0, /* Rightshift. */
  127. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  128. 0, /* Bitsize. */
  129. FALSE, /* PC_relative. */
  130. 0, /* Bitpos. */
  131. complain_overflow_dont,/* Complain_on_overflow. */
  132. NULL, /* Special_function. */
  133. "R_D10V_GNU_VTINHERIT",/* Name. */
  134. FALSE, /* Partial_inplace. */
  135. 0, /* Src_mask. */
  136. 0, /* Dst_mask. */
  137. FALSE), /* PCrel_offset. */
  138. /* GNU extension to record C++ vtable member usage. */
  139. HOWTO (R_D10V_GNU_VTENTRY, /* Type. */
  140. 0, /* Rightshift. */
  141. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  142. 0, /* Bitsize. */
  143. FALSE, /* PC_relative. */
  144. 0, /* Bitpos. */
  145. complain_overflow_dont,/* Complain_on_overflow. */
  146. _bfd_elf_rel_vtable_reloc_fn, /* Special_function. */
  147. "R_D10V_GNU_VTENTRY", /* Name. */
  148. FALSE, /* Partial_inplace. */
  149. 0, /* Src_mask. */
  150. 0, /* Dst_mask. */
  151. FALSE), /* PCrel_offset. */
  152. };
  153. /* Map BFD reloc types to D10V ELF reloc types. */
  154. struct d10v_reloc_map
  155. {
  156. bfd_reloc_code_real_type bfd_reloc_val;
  157. unsigned char elf_reloc_val;
  158. };
  159. static const struct d10v_reloc_map d10v_reloc_map[] =
  160. {
  161. { BFD_RELOC_NONE, R_D10V_NONE, },
  162. { BFD_RELOC_D10V_10_PCREL_R, R_D10V_10_PCREL_R },
  163. { BFD_RELOC_D10V_10_PCREL_L, R_D10V_10_PCREL_L },
  164. { BFD_RELOC_16, R_D10V_16 },
  165. { BFD_RELOC_D10V_18, R_D10V_18 },
  166. { BFD_RELOC_D10V_18_PCREL, R_D10V_18_PCREL },
  167. { BFD_RELOC_32, R_D10V_32 },
  168. { BFD_RELOC_VTABLE_INHERIT, R_D10V_GNU_VTINHERIT },
  169. { BFD_RELOC_VTABLE_ENTRY, R_D10V_GNU_VTENTRY },
  170. };
  171. static reloc_howto_type *
  172. bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  173. bfd_reloc_code_real_type code)
  174. {
  175. unsigned int i;
  176. for (i = 0;
  177. i < sizeof (d10v_reloc_map) / sizeof (struct d10v_reloc_map);
  178. i++)
  179. if (d10v_reloc_map[i].bfd_reloc_val == code)
  180. return &elf_d10v_howto_table[d10v_reloc_map[i].elf_reloc_val];
  181. return NULL;
  182. }
  183. static reloc_howto_type *
  184. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  185. const char *r_name)
  186. {
  187. unsigned int i;
  188. for (i = 0;
  189. i < sizeof (elf_d10v_howto_table) / sizeof (elf_d10v_howto_table[0]);
  190. i++)
  191. if (elf_d10v_howto_table[i].name != NULL
  192. && strcasecmp (elf_d10v_howto_table[i].name, r_name) == 0)
  193. return &elf_d10v_howto_table[i];
  194. return NULL;
  195. }
  196. /* Set the howto pointer for an D10V ELF reloc. */
  197. static void
  198. d10v_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
  199. arelent *cache_ptr,
  200. Elf_Internal_Rela *dst)
  201. {
  202. unsigned int r_type;
  203. r_type = ELF32_R_TYPE (dst->r_info);
  204. if (r_type >= (unsigned int) R_D10V_max)
  205. {
  206. _bfd_error_handler (_("%B: invalid D10V reloc number: %d"), abfd, r_type);
  207. r_type = 0;
  208. }
  209. cache_ptr->howto = &elf_d10v_howto_table[r_type];
  210. }
  211. static asection *
  212. elf32_d10v_gc_mark_hook (asection *sec,
  213. struct bfd_link_info *info,
  214. Elf_Internal_Rela *rel,
  215. struct elf_link_hash_entry *h,
  216. Elf_Internal_Sym *sym)
  217. {
  218. if (h != NULL)
  219. switch (ELF32_R_TYPE (rel->r_info))
  220. {
  221. case R_D10V_GNU_VTINHERIT:
  222. case R_D10V_GNU_VTENTRY:
  223. return NULL;
  224. }
  225. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  226. }
  227. /* Look through the relocs for a section during the first phase.
  228. Since we don't do .gots or .plts, we just need to consider the
  229. virtual table relocs for gc. */
  230. static bfd_boolean
  231. elf32_d10v_check_relocs (bfd *abfd,
  232. struct bfd_link_info *info,
  233. asection *sec,
  234. const Elf_Internal_Rela *relocs)
  235. {
  236. Elf_Internal_Shdr *symtab_hdr;
  237. struct elf_link_hash_entry **sym_hashes;
  238. const Elf_Internal_Rela *rel;
  239. const Elf_Internal_Rela *rel_end;
  240. if (bfd_link_relocatable (info))
  241. return TRUE;
  242. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  243. sym_hashes = elf_sym_hashes (abfd);
  244. rel_end = relocs + sec->reloc_count;
  245. for (rel = relocs; rel < rel_end; rel++)
  246. {
  247. struct elf_link_hash_entry *h;
  248. unsigned long r_symndx;
  249. r_symndx = ELF32_R_SYM (rel->r_info);
  250. if (r_symndx < symtab_hdr->sh_info)
  251. h = NULL;
  252. else
  253. {
  254. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  255. while (h->root.type == bfd_link_hash_indirect
  256. || h->root.type == bfd_link_hash_warning)
  257. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  258. /* PR15323, ref flags aren't set for references in the same
  259. object. */
  260. h->root.non_ir_ref = 1;
  261. }
  262. switch (ELF32_R_TYPE (rel->r_info))
  263. {
  264. /* This relocation describes the C++ object vtable hierarchy.
  265. Reconstruct it for later use during GC. */
  266. case R_D10V_GNU_VTINHERIT:
  267. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  268. return FALSE;
  269. break;
  270. /* This relocation describes which C++ vtable entries are actually
  271. used. Record for later use during GC. */
  272. case R_D10V_GNU_VTENTRY:
  273. BFD_ASSERT (h != NULL);
  274. if (h != NULL
  275. && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
  276. return FALSE;
  277. break;
  278. }
  279. }
  280. return TRUE;
  281. }
  282. static bfd_vma
  283. extract_rel_addend (bfd *abfd,
  284. bfd_byte *where,
  285. reloc_howto_type *howto)
  286. {
  287. bfd_vma insn, val;
  288. switch (howto->size)
  289. {
  290. case 0:
  291. insn = bfd_get_8 (abfd, where);
  292. break;
  293. case 1:
  294. insn = bfd_get_16 (abfd, where);
  295. break;
  296. case 2:
  297. insn = bfd_get_32 (abfd, where);
  298. break;
  299. default:
  300. abort ();
  301. }
  302. val = (insn & howto->dst_mask) >> howto->bitpos << howto->rightshift;
  303. /* We should really be testing for signed addends here, but we don't
  304. have that info directly in the howto. */
  305. if (howto->pc_relative)
  306. {
  307. bfd_vma sign;
  308. sign = howto->dst_mask & (~howto->dst_mask >> 1 | ~(-(bfd_vma) 1 >> 1));
  309. sign = sign >> howto->bitpos << howto->rightshift;
  310. val = (val ^ sign) - sign;
  311. }
  312. return val;
  313. }
  314. static void
  315. insert_rel_addend (bfd *abfd,
  316. bfd_byte *where,
  317. reloc_howto_type *howto,
  318. bfd_vma addend)
  319. {
  320. bfd_vma insn;
  321. addend = (addend >> howto->rightshift << howto->bitpos) & howto->dst_mask;
  322. insn = ~howto->dst_mask;
  323. switch (howto->size)
  324. {
  325. case 0:
  326. insn &= bfd_get_8 (abfd, where);
  327. insn |= addend;
  328. bfd_put_8 (abfd, insn, where);
  329. break;
  330. case 1:
  331. insn &= bfd_get_16 (abfd, where);
  332. insn |= addend;
  333. bfd_put_16 (abfd, insn, where);
  334. break;
  335. case 2:
  336. insn &= bfd_get_32 (abfd, where);
  337. insn |= addend;
  338. bfd_put_32 (abfd, insn, where);
  339. break;
  340. default:
  341. abort ();
  342. }
  343. }
  344. /* Relocate a D10V ELF section. */
  345. static bfd_boolean
  346. elf32_d10v_relocate_section (bfd *output_bfd,
  347. struct bfd_link_info *info,
  348. bfd *input_bfd,
  349. asection *input_section,
  350. bfd_byte *contents,
  351. Elf_Internal_Rela *relocs,
  352. Elf_Internal_Sym *local_syms,
  353. asection **local_sections)
  354. {
  355. Elf_Internal_Shdr *symtab_hdr;
  356. struct elf_link_hash_entry **sym_hashes;
  357. Elf_Internal_Rela *rel, *relend;
  358. const char *name;
  359. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  360. sym_hashes = elf_sym_hashes (input_bfd);
  361. rel = relocs;
  362. relend = relocs + input_section->reloc_count;
  363. for (; rel < relend; rel++)
  364. {
  365. int r_type;
  366. reloc_howto_type *howto;
  367. unsigned long r_symndx;
  368. Elf_Internal_Sym *sym;
  369. asection *sec;
  370. struct elf_link_hash_entry *h;
  371. bfd_vma relocation;
  372. bfd_reloc_status_type r;
  373. r_symndx = ELF32_R_SYM (rel->r_info);
  374. r_type = ELF32_R_TYPE (rel->r_info);
  375. if (r_type == R_D10V_GNU_VTENTRY
  376. || r_type == R_D10V_GNU_VTINHERIT)
  377. continue;
  378. howto = elf_d10v_howto_table + r_type;
  379. h = NULL;
  380. sym = NULL;
  381. sec = NULL;
  382. if (r_symndx < symtab_hdr->sh_info)
  383. {
  384. sym = local_syms + r_symndx;
  385. sec = local_sections[r_symndx];
  386. relocation = (sec->output_section->vma
  387. + sec->output_offset
  388. + sym->st_value);
  389. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
  390. && ((sec->flags & SEC_MERGE) != 0
  391. || (bfd_link_relocatable (info)
  392. && sec->output_offset != 0)))
  393. {
  394. bfd_vma addend;
  395. bfd_byte *where = contents + rel->r_offset;
  396. addend = extract_rel_addend (input_bfd, where, howto);
  397. if (bfd_link_relocatable (info))
  398. addend += sec->output_offset;
  399. else
  400. {
  401. asection *msec = sec;
  402. addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
  403. addend);
  404. addend -= relocation;
  405. addend += msec->output_section->vma + msec->output_offset;
  406. }
  407. insert_rel_addend (input_bfd, where, howto, addend);
  408. }
  409. }
  410. else
  411. {
  412. bfd_boolean unresolved_reloc, warned, ignored;
  413. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  414. r_symndx, symtab_hdr, sym_hashes,
  415. h, sec, relocation,
  416. unresolved_reloc, warned, ignored);
  417. }
  418. if (sec != NULL && discarded_section (sec))
  419. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  420. rel, 1, relend, howto, 0, contents);
  421. if (bfd_link_relocatable (info))
  422. continue;
  423. if (h != NULL)
  424. name = h->root.root.string;
  425. else
  426. {
  427. name = (bfd_elf_string_from_elf_section
  428. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  429. if (name == NULL || *name == '\0')
  430. name = bfd_section_name (input_bfd, sec);
  431. }
  432. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  433. contents, rel->r_offset,
  434. relocation, (bfd_vma) 0);
  435. if (r != bfd_reloc_ok)
  436. {
  437. const char * msg = (const char *) 0;
  438. switch (r)
  439. {
  440. case bfd_reloc_overflow:
  441. if (!((*info->callbacks->reloc_overflow)
  442. (info, (h ? &h->root : NULL), name, howto->name,
  443. (bfd_vma) 0, input_bfd, input_section,
  444. rel->r_offset)))
  445. return FALSE;
  446. break;
  447. case bfd_reloc_undefined:
  448. if (!((*info->callbacks->undefined_symbol)
  449. (info, name, input_bfd, input_section,
  450. rel->r_offset, TRUE)))
  451. return FALSE;
  452. break;
  453. case bfd_reloc_outofrange:
  454. msg = _("internal error: out of range error");
  455. goto common_error;
  456. case bfd_reloc_notsupported:
  457. msg = _("internal error: unsupported relocation error");
  458. goto common_error;
  459. case bfd_reloc_dangerous:
  460. msg = _("internal error: dangerous error");
  461. goto common_error;
  462. default:
  463. msg = _("internal error: unknown error");
  464. /* fall through */
  465. common_error:
  466. if (!((*info->callbacks->warning)
  467. (info, msg, name, input_bfd, input_section,
  468. rel->r_offset)))
  469. return FALSE;
  470. break;
  471. }
  472. }
  473. }
  474. return TRUE;
  475. }
  476. #define ELF_ARCH bfd_arch_d10v
  477. #define ELF_MACHINE_CODE EM_D10V
  478. #define ELF_MACHINE_ALT1 EM_CYGNUS_D10V
  479. #define ELF_MAXPAGESIZE 0x1000
  480. #define TARGET_BIG_SYM d10v_elf32_vec
  481. #define TARGET_BIG_NAME "elf32-d10v"
  482. #define elf_info_to_howto 0
  483. #define elf_info_to_howto_rel d10v_info_to_howto_rel
  484. #define elf_backend_object_p 0
  485. #define elf_backend_final_write_processing 0
  486. #define elf_backend_gc_mark_hook elf32_d10v_gc_mark_hook
  487. #define elf_backend_check_relocs elf32_d10v_check_relocs
  488. #define elf_backend_relocate_section elf32_d10v_relocate_section
  489. #define elf_backend_can_gc_sections 1
  490. #include "elf32-target.h"