elf32-pj.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* picoJava specific support for 32-bit ELF
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Contributed by Steve Chamberlan of Transmeta (sac@pobox.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 "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "elf/pj.h"
  23. /* This function is used for normal relocs. This is like the COFF
  24. function, and is almost certainly incorrect for other ELF targets. */
  25. static bfd_reloc_status_type
  26. pj_elf_reloc (bfd *abfd,
  27. arelent *reloc_entry,
  28. asymbol *symbol_in,
  29. void * data,
  30. asection *input_section,
  31. bfd *output_bfd,
  32. char **error_message ATTRIBUTE_UNUSED)
  33. {
  34. unsigned long insn;
  35. bfd_vma sym_value;
  36. enum elf_pj_reloc_type r_type;
  37. bfd_vma addr = reloc_entry->address;
  38. bfd_byte *hit_data = addr + (bfd_byte *) data;
  39. r_type = (enum elf_pj_reloc_type) reloc_entry->howto->type;
  40. if (output_bfd != NULL)
  41. {
  42. /* Partial linking--do nothing. */
  43. reloc_entry->address += input_section->output_offset;
  44. return bfd_reloc_ok;
  45. }
  46. if (symbol_in != NULL
  47. && (symbol_in->flags & BSF_WEAK) == 0
  48. && bfd_is_und_section (symbol_in->section))
  49. return bfd_reloc_undefined;
  50. if (bfd_is_com_section (symbol_in->section))
  51. sym_value = 0;
  52. else
  53. sym_value = (symbol_in->value +
  54. symbol_in->section->output_section->vma +
  55. symbol_in->section->output_offset);
  56. switch (r_type)
  57. {
  58. case R_PJ_DATA_DIR32:
  59. insn = bfd_get_32 (abfd, hit_data);
  60. insn += sym_value + reloc_entry->addend;
  61. bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
  62. break;
  63. /* Relocations in code are always bigendian, no matter what the
  64. data endianness is. */
  65. case R_PJ_CODE_DIR32:
  66. insn = bfd_getb32 (hit_data);
  67. insn += sym_value + reloc_entry->addend;
  68. bfd_putb32 ((bfd_vma) insn, hit_data);
  69. break;
  70. case R_PJ_CODE_REL16:
  71. insn = bfd_getb16 (hit_data);
  72. insn += sym_value + reloc_entry->addend
  73. - (input_section->output_section->vma
  74. + input_section->output_offset);
  75. bfd_putb16 ((bfd_vma) insn, hit_data);
  76. break;
  77. case R_PJ_CODE_LO16:
  78. insn = bfd_getb16 (hit_data);
  79. insn += sym_value + reloc_entry->addend;
  80. bfd_putb16 ((bfd_vma) insn, hit_data);
  81. break;
  82. case R_PJ_CODE_HI16:
  83. insn = bfd_getb16 (hit_data);
  84. insn += (sym_value + reloc_entry->addend) >> 16;
  85. bfd_putb16 ((bfd_vma) insn, hit_data);
  86. break;
  87. default:
  88. abort ();
  89. break;
  90. }
  91. return bfd_reloc_ok;
  92. }
  93. static reloc_howto_type pj_elf_howto_table[] =
  94. {
  95. /* No relocation. */
  96. HOWTO (R_PJ_NONE, /* type */
  97. 0, /* rightshift */
  98. 3, /* size (0 = byte, 1 = short, 2 = long) */
  99. 0, /* bitsize */
  100. false, /* pc_relative */
  101. 0, /* bitpos */
  102. complain_overflow_dont, /* complain_on_overflow */
  103. pj_elf_reloc, /* special_function */
  104. "R_PJ_NONE", /* name */
  105. false, /* partial_inplace */
  106. 0, /* src_mask */
  107. 0, /* dst_mask */
  108. false), /* pcrel_offset */
  109. /* 32 bit absolute relocation. Setting partial_inplace to TRUE and
  110. src_mask to a non-zero value is similar to the COFF toolchain. */
  111. HOWTO (R_PJ_DATA_DIR32, /* 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_bitfield, /* complain_on_overflow */
  118. pj_elf_reloc, /* special_function */
  119. "R_PJ_DIR32", /* name */
  120. true, /* partial_inplace */
  121. 0xffffffff, /* src_mask */
  122. 0xffffffff, /* dst_mask */
  123. false), /* pcrel_offset */
  124. /* 32 bit PC relative relocation. */
  125. HOWTO (R_PJ_CODE_REL32, /* type */
  126. 0, /* rightshift */
  127. 2, /* size (0 = byte, 1 = short, 2 = long) */
  128. 32, /* bitsize */
  129. true, /* pc_relative */
  130. 0, /* bitpos */
  131. complain_overflow_signed, /* complain_on_overflow */
  132. pj_elf_reloc, /* special_function */
  133. "R_PJ_REL32", /* name */
  134. false, /* partial_inplace */
  135. 0, /* src_mask */
  136. 0xffffffff, /* dst_mask */
  137. true), /* pcrel_offset */
  138. /* 16 bit PC relative relocation. */
  139. HOWTO (R_PJ_CODE_REL16, /* type */
  140. 0, /* rightshift */
  141. 1, /* size (0 = byte, 1 = short, 2 = long) */
  142. 16, /* bitsize */
  143. true, /* pc_relative */
  144. 0, /* bitpos */
  145. complain_overflow_signed, /* complain_on_overf6w */
  146. pj_elf_reloc, /* special_function */
  147. "R_PJ_REL16", /* name */
  148. false, /* partial_inplace */
  149. 0xffff, /* src_mask */
  150. 0xffff, /* dst_mask */
  151. true), /* pcrel_offset */
  152. EMPTY_HOWTO (4),
  153. EMPTY_HOWTO (5),
  154. HOWTO (R_PJ_CODE_DIR32, /* type */
  155. 0, /* rightshift */
  156. 2, /* size (0 = byte, 1 = short, 2 = long) */
  157. 32, /* bitsize */
  158. false, /* pc_relative */
  159. 0, /* bitpos */
  160. complain_overflow_bitfield, /* complain_on_overflow */
  161. pj_elf_reloc, /* special_function */
  162. "R_PJ_CODE_DIR32", /* name */
  163. true, /* partial_inplace */
  164. 0xffffffff, /* src_mask */
  165. 0xffffffff, /* dst_mask */
  166. false), /* pcrel_offset */
  167. EMPTY_HOWTO (7),
  168. EMPTY_HOWTO (8),
  169. EMPTY_HOWTO (9),
  170. EMPTY_HOWTO (10),
  171. EMPTY_HOWTO (11),
  172. EMPTY_HOWTO (12),
  173. HOWTO (R_PJ_CODE_LO16, /* type */
  174. 0, /* rightshift */
  175. 1, /* size (0 = byte, 1 = short, 2 = long) */
  176. 16, /* bitsize */
  177. false, /* pc_relative */
  178. 0, /* bitpos */
  179. complain_overflow_unsigned, /* complain_on_overflow */
  180. pj_elf_reloc, /* special_function */
  181. "R_PJ_LO16", /* name */
  182. false, /* partial_inplace */
  183. 0xffff, /* src_mask */
  184. 0xffff, /* dst_mask */
  185. true), /* pcrel_offset */
  186. HOWTO (R_PJ_CODE_HI16, /* type */
  187. 16, /* rightshift */
  188. 1, /* size (0 = byte, 1 = short, 2 = long) */
  189. 16, /* bitsize */
  190. false, /* pc_relative */
  191. 0, /* bitpos */
  192. complain_overflow_unsigned, /* complain_on_overflow */
  193. pj_elf_reloc, /* special_function */
  194. "R_PJ_HI16", /* name */
  195. false, /* partial_inplace */
  196. 0xffff, /* src_mask */
  197. 0xffff, /* dst_mask */
  198. true), /* pcrel_offset */
  199. /* GNU extension to record C++ vtable hierarchy. */
  200. HOWTO (R_PJ_GNU_VTINHERIT, /* type */
  201. 0, /* rightshift */
  202. 2, /* size (0 = byte, 1 = short, 2 = long) */
  203. 0, /* bitsize */
  204. false, /* pc_relative */
  205. 0, /* bitpos */
  206. complain_overflow_dont, /* complain_on_overflow */
  207. NULL, /* special_function */
  208. "R_PJ_GNU_VTINHERIT", /* name */
  209. false, /* partial_inplace */
  210. 0, /* src_mask */
  211. 0, /* dst_mask */
  212. false), /* pcrel_offset */
  213. /* GNU extension to record C++ vtable member usage. */
  214. HOWTO (R_PJ_GNU_VTENTRY, /* type */
  215. 0, /* rightshift */
  216. 2, /* size (0 = byte, 1 = short, 2 = long) */
  217. 0, /* bitsize */
  218. false, /* pc_relative */
  219. 0, /* bitpos */
  220. complain_overflow_dont, /* complain_on_overflow */
  221. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  222. "R_PJ_GNU_VTENTRY", /* name */
  223. false, /* partial_inplace */
  224. 0, /* src_mask */
  225. 0, /* dst_mask */
  226. false), /* pcrel_offset */
  227. };
  228. /* This structure is used to map BFD reloc codes to PJ ELF relocs. */
  229. struct elf_reloc_map
  230. {
  231. bfd_reloc_code_real_type bfd_reloc_val;
  232. unsigned char elf_reloc_val;
  233. };
  234. /* An array mapping BFD reloc codes to PJ ELF relocs. */
  235. static const struct elf_reloc_map pj_reloc_map[] =
  236. {
  237. { BFD_RELOC_NONE, R_PJ_NONE },
  238. { BFD_RELOC_32, R_PJ_DATA_DIR32 },
  239. { BFD_RELOC_PJ_CODE_DIR16, R_PJ_CODE_DIR16 },
  240. { BFD_RELOC_PJ_CODE_DIR32, R_PJ_CODE_DIR32 },
  241. { BFD_RELOC_PJ_CODE_LO16, R_PJ_CODE_LO16 },
  242. { BFD_RELOC_PJ_CODE_HI16, R_PJ_CODE_HI16 },
  243. { BFD_RELOC_PJ_CODE_REL32, R_PJ_CODE_REL32 },
  244. { BFD_RELOC_PJ_CODE_REL16, R_PJ_CODE_REL16 },
  245. { BFD_RELOC_VTABLE_INHERIT, R_PJ_GNU_VTINHERIT },
  246. { BFD_RELOC_VTABLE_ENTRY, R_PJ_GNU_VTENTRY },
  247. };
  248. /* Given a BFD reloc code, return the howto structure for the
  249. corresponding PJ ELf reloc. */
  250. static reloc_howto_type *
  251. pj_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  252. bfd_reloc_code_real_type code)
  253. {
  254. unsigned int i;
  255. for (i = 0; i < sizeof (pj_reloc_map) / sizeof (struct elf_reloc_map); i++)
  256. if (pj_reloc_map[i].bfd_reloc_val == code)
  257. return & pj_elf_howto_table[(int) pj_reloc_map[i].elf_reloc_val];
  258. return NULL;
  259. }
  260. static reloc_howto_type *
  261. pj_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  262. const char *r_name)
  263. {
  264. unsigned int i;
  265. for (i = 0;
  266. i < sizeof (pj_elf_howto_table) / sizeof (pj_elf_howto_table[0]);
  267. i++)
  268. if (pj_elf_howto_table[i].name != NULL
  269. && strcasecmp (pj_elf_howto_table[i].name, r_name) == 0)
  270. return &pj_elf_howto_table[i];
  271. return NULL;
  272. }
  273. /* Given an ELF reloc, fill in the howto field of a relent. */
  274. static bool
  275. pj_elf_info_to_howto (bfd *abfd,
  276. arelent *cache_ptr,
  277. Elf_Internal_Rela *dst)
  278. {
  279. unsigned int r;
  280. r = ELF32_R_TYPE (dst->r_info);
  281. if (r >= R_PJ_max)
  282. {
  283. /* xgettext:c-format */
  284. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  285. abfd, r);
  286. bfd_set_error (bfd_error_bad_value);
  287. return false;
  288. }
  289. cache_ptr->howto = &pj_elf_howto_table[r];
  290. return true;
  291. }
  292. /* Take this moment to fill in the special picoJava bits in the
  293. e_flags field. */
  294. static bool
  295. pj_elf_final_write_processing (bfd *abfd)
  296. {
  297. elf_elfheader (abfd)->e_flags |= EF_PICOJAVA_ARCH;
  298. elf_elfheader (abfd)->e_flags |= EF_PICOJAVA_GNUCALLS;
  299. return _bfd_elf_final_write_processing (abfd);
  300. }
  301. #define TARGET_BIG_SYM pj_elf32_vec
  302. #define TARGET_BIG_NAME "elf32-pj"
  303. #define TARGET_LITTLE_SYM pj_elf32_le_vec
  304. #define TARGET_LITTLE_NAME "elf32-pjl"
  305. #define ELF_ARCH bfd_arch_pj
  306. #define ELF_MACHINE_CODE EM_PJ
  307. #define ELF_MACHINE_ALT1 EM_PJ_OLD
  308. #define ELF_MAXPAGESIZE 0x1000
  309. #define bfd_elf32_bfd_get_relocated_section_contents \
  310. bfd_generic_get_relocated_section_contents
  311. #define bfd_elf32_bfd_reloc_type_lookup pj_elf_reloc_type_lookup
  312. #define bfd_elf32_bfd_reloc_name_lookup pj_elf_reloc_name_lookup
  313. #define elf_backend_final_write_processing pj_elf_final_write_processing
  314. #define elf_info_to_howto pj_elf_info_to_howto
  315. #include "elf32-target.h"