elf32-s12z.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* Freescale S12Z-specific support for 32-bit ELF
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. (Heavily copied from the D10V port 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 "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "elf/s12z.h"
  23. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  24. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  25. /* Relocation functions. */
  26. static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
  27. (bfd *, bfd_reloc_code_real_type);
  28. static bool s12z_info_to_howto_rel
  29. (bfd *, arelent *, Elf_Internal_Rela *);
  30. static bfd_reloc_status_type
  31. opru18_reloc (bfd *abfd, arelent *reloc_entry, struct bfd_symbol *symbol,
  32. void *data, asection *input_section ATTRIBUTE_UNUSED,
  33. bfd *output ATTRIBUTE_UNUSED, char **msg ATTRIBUTE_UNUSED)
  34. {
  35. /* This reloc is used for 18 bit General Operand Addressing Postbyte in the
  36. INST opru18 form. This is an 18 bit reloc, but the most significant bit
  37. is shifted one place to the left of where it would normally be. See
  38. Appendix A.4 of the S12Z reference manual. */
  39. bfd_size_type octets = (reloc_entry->address
  40. * OCTETS_PER_BYTE (abfd, input_section));
  41. bfd_vma result = bfd_get_24 (abfd, (unsigned char *) data + octets);
  42. bfd_vma val = bfd_asymbol_value (symbol);
  43. /* Keep the wanted bits and discard the rest. */
  44. result &= 0xFA0000;
  45. val += symbol->section->output_section->vma;
  46. val += symbol->section->output_offset;
  47. /* The lowest 17 bits are copied verbatim. */
  48. result |= val & 0x1FFFF;
  49. /* The 18th bit is put into the 19th position. */
  50. result |= (val & 0x020000) << 1;
  51. bfd_put_24 (abfd, result, (unsigned char *) data + octets);
  52. return bfd_reloc_ok;
  53. }
  54. static bfd_reloc_status_type
  55. shift_addend_reloc (bfd *abfd, arelent *reloc_entry, struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
  56. void *data ATTRIBUTE_UNUSED, asection *input_section ATTRIBUTE_UNUSED,
  57. bfd *output ATTRIBUTE_UNUSED, char **msg ATTRIBUTE_UNUSED)
  58. {
  59. /* This is a really peculiar reloc, which is done for compatibility
  60. with the Freescale toolchain.
  61. That toolchain appears to (ab)use the lowest 15 bits of the addend for
  62. the purpose of holding flags. The purpose of these flags are unknown.
  63. So in this function, when writing the bfd we left shift the addend by
  64. 15, and when reading we right shift it by 15 (discarding the lower bits).
  65. This allows the linker to work with object files generated by Freescale,
  66. as well as by Gas. */
  67. if (abfd->is_linker_input)
  68. reloc_entry->addend >>= 15;
  69. else
  70. reloc_entry->addend <<= 15;
  71. return bfd_reloc_continue;
  72. }
  73. #define USE_REL 0
  74. static reloc_howto_type elf_s12z_howto_table[] =
  75. {
  76. /* This reloc does nothing. */
  77. HOWTO (R_S12Z_NONE, /* type */
  78. 0, /* rightshift */
  79. 3, /* size (0 = byte, 1 = short, 2 = long) */
  80. 0, /* bitsize */
  81. false, /* pc_relative */
  82. 0, /* bitpos */
  83. complain_overflow_dont,/* complain_on_overflow */
  84. bfd_elf_generic_reloc, /* special_function */
  85. "R_S12Z_NONE", /* name */
  86. false, /* partial_inplace */
  87. 0, /* src_mask */
  88. 0, /* dst_mask */
  89. false), /* pcrel_offset */
  90. /* A 24 bit absolute relocation emitted by the OPR mode operands */
  91. HOWTO (R_S12Z_OPR, /* type */
  92. 0, /* rightshift */
  93. 5, /* size (0 = byte, 1 = short, 2 = long) */
  94. 24, /* bitsize */
  95. false, /* pc_relative */
  96. 0, /* bitpos */
  97. complain_overflow_bitfield, /* complain_on_overflow */
  98. shift_addend_reloc,
  99. "R_S12Z_OPR", /* name */
  100. false, /* partial_inplace */
  101. 0x00ffffff, /* src_mask */
  102. 0x00ffffff, /* dst_mask */
  103. false), /* pcrel_offset */
  104. /* The purpose of this reloc is not known */
  105. HOWTO (R_S12Z_UKNWN_2, /* type */
  106. 0, /* rightshift */
  107. 3, /* size (0 = byte, 1 = short, 2 = long) */
  108. 0, /* bitsize */
  109. false, /* pc_relative */
  110. 0, /* bitpos */
  111. complain_overflow_dont,/* complain_on_overflow */
  112. bfd_elf_generic_reloc, /* special_function */
  113. "R_S12Z_UKNWN_2", /* name */
  114. false, /* partial_inplace */
  115. 0, /* src_mask */
  116. 0, /* dst_mask */
  117. false), /* pcrel_offset */
  118. /* A 15 bit PC-rel relocation */
  119. HOWTO (R_S12Z_PCREL_7_15, /* type */
  120. 0, /* rightshift */
  121. 1, /* size (0 = byte, 1 = short, 2 = long) */
  122. 15, /* bitsize */
  123. true, /* pc_relative */
  124. 0, /* bitpos */
  125. complain_overflow_bitfield, /* complain_on_overflow */
  126. shift_addend_reloc,
  127. "R_S12Z_PCREL_7_15", /* name */
  128. false, /* partial_inplace */
  129. 0x00, /* src_mask */
  130. 0x007fff, /* dst_mask */
  131. true), /* pcrel_offset */
  132. /* A 24 bit absolute relocation emitted by EXT24 mode operands */
  133. HOWTO (R_S12Z_EXT24, /* type */
  134. 0, /* rightshift */
  135. 5, /* size (0 = byte, 1 = short, 2 = long) */
  136. 24, /* bitsize */
  137. false, /* pc_relative */
  138. 0, /* bitpos */
  139. complain_overflow_bitfield, /* complain_on_overflow */
  140. bfd_elf_generic_reloc, /* special_function */
  141. "R_S12Z_EXT24", /* name */
  142. false, /* partial_inplace */
  143. 0x00000000, /* src_mask */
  144. 0x00ffffff, /* dst_mask */
  145. false), /* pcrel_offset */
  146. /* An 18 bit absolute relocation */
  147. HOWTO (R_S12Z_EXT18, /* type */
  148. 0, /* rightshift */
  149. 5, /* size (0 = byte, 1 = short, 2 = long) */
  150. 18, /* bitsize */
  151. false, /* pc_relative */
  152. 0, /* bitpos */
  153. complain_overflow_bitfield, /* complain_on_overflow */
  154. opru18_reloc, /* special_function */
  155. "R_S12Z_EXT18", /* name */
  156. false, /* partial_inplace */
  157. 0x00000000, /* src_mask */
  158. 0x0005ffff, /* dst_mask */
  159. false), /* pcrel_offset */
  160. /* A 32 bit absolute relocation. This kind of relocation is
  161. schizophrenic - Although they appear in sections named .rela.debug.*
  162. in some sections they behave as RELA relocs, but in others they have
  163. an added of zero and behave as REL.
  164. It is not recommended that new code emits this reloc. It is here
  165. only to support existing elf files generated by third party
  166. applications. */
  167. HOWTO (R_S12Z_CW32, /* type */
  168. 0, /* rightshift */
  169. 2, /* size (0 = byte, 1 = short, 2 = long) */
  170. 32, /* bitsize */
  171. false, /* pc_relative */
  172. 0, /* bitpos */
  173. complain_overflow_bitfield, /* complain_on_overflow */
  174. bfd_elf_generic_reloc, /* special_function */
  175. "R_S12Z_CW32", /* name */
  176. false, /* partial_inplace */
  177. 0xffffffff, /* src_mask */
  178. 0xffffffff, /* dst_mask */
  179. false), /* pcrel_offset */
  180. /* A 32 bit absolute relocation */
  181. HOWTO (R_S12Z_EXT32, /* type */
  182. 0, /* rightshift */
  183. 2, /* size (0 = byte, 1 = short, 2 = long) */
  184. 32, /* bitsize */
  185. false, /* pc_relative */
  186. 0, /* bitpos */
  187. complain_overflow_bitfield, /* complain_on_overflow */
  188. bfd_elf_generic_reloc, /* special_function */
  189. "R_S12Z_EXT32", /* name */
  190. false, /* partial_inplace */
  191. 0x00000000, /* src_mask */
  192. 0xffffffff, /* dst_mask */
  193. false), /* pcrel_offset */
  194. };
  195. /* Map BFD reloc types to S12Z ELF reloc types. */
  196. struct s12z_reloc_map
  197. {
  198. bfd_reloc_code_real_type bfd_reloc_val;
  199. unsigned char elf_reloc_val;
  200. };
  201. static const struct s12z_reloc_map s12z_reloc_map[] =
  202. {
  203. /* bfd reloc val */ /* elf reloc val */
  204. {BFD_RELOC_NONE, R_S12Z_NONE},
  205. {BFD_RELOC_32, R_S12Z_EXT32},
  206. {BFD_RELOC_24, R_S12Z_EXT24},
  207. {BFD_RELOC_16_PCREL, R_S12Z_PCREL_7_15},
  208. {BFD_RELOC_S12Z_OPR, R_S12Z_OPR}
  209. };
  210. static reloc_howto_type *
  211. bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  212. bfd_reloc_code_real_type code)
  213. {
  214. unsigned int i;
  215. for (i = 0;
  216. i < sizeof (s12z_reloc_map) / sizeof (struct s12z_reloc_map);
  217. i++)
  218. {
  219. if (s12z_reloc_map[i].bfd_reloc_val == code)
  220. {
  221. return &elf_s12z_howto_table[s12z_reloc_map[i].elf_reloc_val];
  222. }
  223. }
  224. printf ("%s:%d Not found type %d\n", __FILE__, __LINE__, code);
  225. return NULL;
  226. }
  227. static reloc_howto_type *
  228. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  229. const char *r_name)
  230. {
  231. unsigned int i;
  232. for (i = 0;
  233. i < (sizeof (elf_s12z_howto_table)
  234. / sizeof (elf_s12z_howto_table[0]));
  235. i++)
  236. if (elf_s12z_howto_table[i].name != NULL
  237. && strcasecmp (elf_s12z_howto_table[i].name, r_name) == 0)
  238. return &elf_s12z_howto_table[i];
  239. return NULL;
  240. }
  241. /* Set the howto pointer for an S12Z ELF reloc. */
  242. static bool
  243. s12z_info_to_howto_rel (bfd *abfd,
  244. arelent *cache_ptr, Elf_Internal_Rela *dst)
  245. {
  246. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  247. if (r_type >= (unsigned int) R_S12Z_max)
  248. {
  249. /* xgettext:c-format */
  250. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  251. abfd, r_type);
  252. bfd_set_error (bfd_error_bad_value);
  253. return false;
  254. }
  255. cache_ptr->howto = &elf_s12z_howto_table[r_type];
  256. return true;
  257. }
  258. static bool
  259. s12z_elf_set_mach_from_flags (bfd *abfd)
  260. {
  261. bfd_default_set_arch_mach (abfd, bfd_arch_s12z, 0);
  262. return true;
  263. }
  264. #define ELF_ARCH bfd_arch_s12z
  265. #define ELF_MACHINE_CODE EM_S12Z
  266. #define ELF_MAXPAGESIZE 0x1000
  267. #define TARGET_BIG_SYM s12z_elf32_vec
  268. #define TARGET_BIG_NAME "elf32-s12z"
  269. #define elf_info_to_howto NULL
  270. #define elf_info_to_howto_rel s12z_info_to_howto_rel
  271. #define elf_backend_object_p s12z_elf_set_mach_from_flags
  272. #include "elf32-target.h"