elf32-d10v.c 15 KB

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