elf32-d10v.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* D10V-specific support for 32-bit ELF
  2. Copyright (C) 1996-2017 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. /* xgettext:c-format */
  207. _bfd_error_handler (_("%B: invalid D10V reloc number: %d"), abfd, r_type);
  208. r_type = 0;
  209. }
  210. cache_ptr->howto = &elf_d10v_howto_table[r_type];
  211. }
  212. static asection *
  213. elf32_d10v_gc_mark_hook (asection *sec,
  214. struct bfd_link_info *info,
  215. Elf_Internal_Rela *rel,
  216. struct elf_link_hash_entry *h,
  217. Elf_Internal_Sym *sym)
  218. {
  219. if (h != NULL)
  220. switch (ELF32_R_TYPE (rel->r_info))
  221. {
  222. case R_D10V_GNU_VTINHERIT:
  223. case R_D10V_GNU_VTENTRY:
  224. return NULL;
  225. }
  226. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  227. }
  228. /* Look through the relocs for a section during the first phase.
  229. Since we don't do .gots or .plts, we just need to consider the
  230. virtual table relocs for gc. */
  231. static bfd_boolean
  232. elf32_d10v_check_relocs (bfd *abfd,
  233. struct bfd_link_info *info,
  234. asection *sec,
  235. const Elf_Internal_Rela *relocs)
  236. {
  237. Elf_Internal_Shdr *symtab_hdr;
  238. struct elf_link_hash_entry **sym_hashes;
  239. const Elf_Internal_Rela *rel;
  240. const Elf_Internal_Rela *rel_end;
  241. if (bfd_link_relocatable (info))
  242. return TRUE;
  243. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  244. sym_hashes = elf_sym_hashes (abfd);
  245. rel_end = relocs + sec->reloc_count;
  246. for (rel = relocs; rel < rel_end; rel++)
  247. {
  248. struct elf_link_hash_entry *h;
  249. unsigned long r_symndx;
  250. r_symndx = ELF32_R_SYM (rel->r_info);
  251. if (r_symndx < symtab_hdr->sh_info)
  252. h = NULL;
  253. else
  254. {
  255. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  256. while (h->root.type == bfd_link_hash_indirect
  257. || h->root.type == bfd_link_hash_warning)
  258. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  259. /* PR15323, ref flags aren't set for references in the same
  260. object. */
  261. h->root.non_ir_ref_regular = 1;
  262. }
  263. switch (ELF32_R_TYPE (rel->r_info))
  264. {
  265. /* This relocation describes the C++ object vtable hierarchy.
  266. Reconstruct it for later use during GC. */
  267. case R_D10V_GNU_VTINHERIT:
  268. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  269. return FALSE;
  270. break;
  271. /* This relocation describes which C++ vtable entries are actually
  272. used. Record for later use during GC. */
  273. case R_D10V_GNU_VTENTRY:
  274. BFD_ASSERT (h != NULL);
  275. if (h != NULL
  276. && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
  277. return FALSE;
  278. break;
  279. }
  280. }
  281. return TRUE;
  282. }
  283. static bfd_vma
  284. extract_rel_addend (bfd *abfd,
  285. bfd_byte *where,
  286. reloc_howto_type *howto)
  287. {
  288. bfd_vma insn, val;
  289. switch (howto->size)
  290. {
  291. case 0:
  292. insn = bfd_get_8 (abfd, where);
  293. break;
  294. case 1:
  295. insn = bfd_get_16 (abfd, where);
  296. break;
  297. case 2:
  298. insn = bfd_get_32 (abfd, where);
  299. break;
  300. default:
  301. abort ();
  302. }
  303. val = (insn & howto->dst_mask) >> howto->bitpos << howto->rightshift;
  304. /* We should really be testing for signed addends here, but we don't
  305. have that info directly in the howto. */
  306. if (howto->pc_relative)
  307. {
  308. bfd_vma sign;
  309. sign = howto->dst_mask & (~howto->dst_mask >> 1 | ~(-(bfd_vma) 1 >> 1));
  310. sign = sign >> howto->bitpos << howto->rightshift;
  311. val = (val ^ sign) - sign;
  312. }
  313. return val;
  314. }
  315. static void
  316. insert_rel_addend (bfd *abfd,
  317. bfd_byte *where,
  318. reloc_howto_type *howto,
  319. bfd_vma addend)
  320. {
  321. bfd_vma insn;
  322. addend = (addend >> howto->rightshift << howto->bitpos) & howto->dst_mask;
  323. insn = ~howto->dst_mask;
  324. switch (howto->size)
  325. {
  326. case 0:
  327. insn &= bfd_get_8 (abfd, where);
  328. insn |= addend;
  329. bfd_put_8 (abfd, insn, where);
  330. break;
  331. case 1:
  332. insn &= bfd_get_16 (abfd, where);
  333. insn |= addend;
  334. bfd_put_16 (abfd, insn, where);
  335. break;
  336. case 2:
  337. insn &= bfd_get_32 (abfd, where);
  338. insn |= addend;
  339. bfd_put_32 (abfd, insn, where);
  340. break;
  341. default:
  342. abort ();
  343. }
  344. }
  345. /* Relocate a D10V ELF section. */
  346. static bfd_boolean
  347. elf32_d10v_relocate_section (bfd *output_bfd,
  348. struct bfd_link_info *info,
  349. bfd *input_bfd,
  350. asection *input_section,
  351. bfd_byte *contents,
  352. Elf_Internal_Rela *relocs,
  353. Elf_Internal_Sym *local_syms,
  354. asection **local_sections)
  355. {
  356. Elf_Internal_Shdr *symtab_hdr;
  357. struct elf_link_hash_entry **sym_hashes;
  358. Elf_Internal_Rela *rel, *relend;
  359. const char *name;
  360. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  361. sym_hashes = elf_sym_hashes (input_bfd);
  362. rel = relocs;
  363. relend = relocs + input_section->reloc_count;
  364. for (; rel < relend; rel++)
  365. {
  366. int r_type;
  367. reloc_howto_type *howto;
  368. unsigned long r_symndx;
  369. Elf_Internal_Sym *sym;
  370. asection *sec;
  371. struct elf_link_hash_entry *h;
  372. bfd_vma relocation;
  373. bfd_reloc_status_type r;
  374. r_symndx = ELF32_R_SYM (rel->r_info);
  375. r_type = ELF32_R_TYPE (rel->r_info);
  376. if (r_type == R_D10V_GNU_VTENTRY
  377. || r_type == R_D10V_GNU_VTINHERIT)
  378. continue;
  379. howto = elf_d10v_howto_table + r_type;
  380. h = NULL;
  381. sym = NULL;
  382. sec = NULL;
  383. if (r_symndx < symtab_hdr->sh_info)
  384. {
  385. sym = local_syms + r_symndx;
  386. sec = local_sections[r_symndx];
  387. relocation = (sec->output_section->vma
  388. + sec->output_offset
  389. + sym->st_value);
  390. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
  391. && ((sec->flags & SEC_MERGE) != 0
  392. || (bfd_link_relocatable (info)
  393. && sec->output_offset != 0)))
  394. {
  395. bfd_vma addend;
  396. bfd_byte *where = contents + rel->r_offset;
  397. addend = extract_rel_addend (input_bfd, where, howto);
  398. if (bfd_link_relocatable (info))
  399. addend += sec->output_offset;
  400. else
  401. {
  402. asection *msec = sec;
  403. addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
  404. addend);
  405. addend -= relocation;
  406. addend += msec->output_section->vma + msec->output_offset;
  407. }
  408. insert_rel_addend (input_bfd, where, howto, addend);
  409. }
  410. }
  411. else
  412. {
  413. bfd_boolean unresolved_reloc, warned, ignored;
  414. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  415. r_symndx, symtab_hdr, sym_hashes,
  416. h, sec, relocation,
  417. unresolved_reloc, warned, ignored);
  418. }
  419. if (sec != NULL && discarded_section (sec))
  420. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  421. rel, 1, relend, howto, 0, contents);
  422. if (bfd_link_relocatable (info))
  423. continue;
  424. if (h != NULL)
  425. name = h->root.root.string;
  426. else
  427. {
  428. name = (bfd_elf_string_from_elf_section
  429. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  430. if (name == NULL || *name == '\0')
  431. name = bfd_section_name (input_bfd, sec);
  432. }
  433. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  434. contents, rel->r_offset,
  435. relocation, (bfd_vma) 0);
  436. if (r != bfd_reloc_ok)
  437. {
  438. const char * msg = (const char *) 0;
  439. switch (r)
  440. {
  441. case bfd_reloc_overflow:
  442. (*info->callbacks->reloc_overflow)
  443. (info, (h ? &h->root : NULL), name, howto->name,
  444. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  445. break;
  446. case bfd_reloc_undefined:
  447. (*info->callbacks->undefined_symbol)
  448. (info, name, input_bfd, input_section, rel->r_offset, TRUE);
  449. break;
  450. case bfd_reloc_outofrange:
  451. msg = _("internal error: out of range error");
  452. goto common_error;
  453. case bfd_reloc_notsupported:
  454. msg = _("internal error: unsupported relocation error");
  455. goto common_error;
  456. case bfd_reloc_dangerous:
  457. msg = _("internal error: dangerous error");
  458. goto common_error;
  459. default:
  460. msg = _("internal error: unknown error");
  461. /* fall through */
  462. common_error:
  463. (*info->callbacks->warning) (info, msg, name, input_bfd,
  464. input_section, rel->r_offset);
  465. break;
  466. }
  467. }
  468. }
  469. return TRUE;
  470. }
  471. #define ELF_ARCH bfd_arch_d10v
  472. #define ELF_MACHINE_CODE EM_D10V
  473. #define ELF_MACHINE_ALT1 EM_CYGNUS_D10V
  474. #define ELF_MAXPAGESIZE 0x1000
  475. #define TARGET_BIG_SYM d10v_elf32_vec
  476. #define TARGET_BIG_NAME "elf32-d10v"
  477. #define elf_info_to_howto 0
  478. #define elf_info_to_howto_rel d10v_info_to_howto_rel
  479. #define elf_backend_object_p 0
  480. #define elf_backend_final_write_processing 0
  481. #define elf_backend_gc_mark_hook elf32_d10v_gc_mark_hook
  482. #define elf_backend_check_relocs elf32_d10v_check_relocs
  483. #define elf_backend_relocate_section elf32_d10v_relocate_section
  484. #define elf_backend_can_gc_sections 1
  485. #include "elf32-target.h"