coff-tic30.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* BFD back-end for TMS320C30 coff binaries.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Steven Haworth (steve@pm.cse.rmit.edu.au)
  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, MA
  16. 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "bfdlink.h"
  21. #include "coff/tic30.h"
  22. #include "coff/internal.h"
  23. #include "libcoff.h"
  24. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
  25. reloc_howto_type tic30_coff_howto_table[] =
  26. {
  27. HOWTO (R_TIC30_ABS16, 2, 1, 16, false, 0, 0, NULL,
  28. "16", false, 0x0000FFFF, 0x0000FFFF, false),
  29. HOWTO (R_TIC30_ABS24, 2, 2, 24, false, 8, complain_overflow_bitfield, NULL,
  30. "24", false, 0xFFFFFF00, 0xFFFFFF00, false),
  31. HOWTO (R_TIC30_LDP, 18, 0, 24, false, 0, complain_overflow_bitfield, NULL,
  32. "LDP", false, 0x00FF0000, 0x000000FF, false),
  33. HOWTO (R_TIC30_ABS32, 2, 2, 32, false, 0, complain_overflow_bitfield, NULL,
  34. "32", false, 0xFFFFFFFF, 0xFFFFFFFF, false),
  35. HOWTO (R_TIC30_PC16, 2, 1, 16, true, 0, complain_overflow_signed, NULL,
  36. "PCREL", false, 0x0000FFFF, 0x0000FFFF, false),
  37. EMPTY_HOWTO (-1)
  38. };
  39. #ifndef coff_bfd_reloc_type_lookup
  40. #define coff_bfd_reloc_type_lookup tic30_coff_reloc_type_lookup
  41. #define coff_bfd_reloc_name_lookup tic30_coff_reloc_name_lookup
  42. /* For the case statement use the code values used in tc_gen_reloc to
  43. map to the howto table entries that match those in both the aout
  44. and coff implementations. */
  45. static reloc_howto_type *
  46. tic30_coff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  47. bfd_reloc_code_real_type code)
  48. {
  49. switch (code)
  50. {
  51. case BFD_RELOC_8:
  52. case BFD_RELOC_TIC30_LDP:
  53. return &tic30_coff_howto_table[2];
  54. case BFD_RELOC_16:
  55. return &tic30_coff_howto_table[0];
  56. case BFD_RELOC_24:
  57. return &tic30_coff_howto_table[1];
  58. case BFD_RELOC_16_PCREL:
  59. return &tic30_coff_howto_table[4];
  60. case BFD_RELOC_32:
  61. return &tic30_coff_howto_table[3];
  62. default:
  63. return (reloc_howto_type *) NULL;
  64. }
  65. }
  66. static reloc_howto_type *
  67. tic30_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  68. const char *r_name)
  69. {
  70. unsigned int i;
  71. for (i = 0;
  72. i < (sizeof (tic30_coff_howto_table)
  73. / sizeof (tic30_coff_howto_table[0]));
  74. i++)
  75. if (tic30_coff_howto_table[i].name != NULL
  76. && strcasecmp (tic30_coff_howto_table[i].name, r_name) == 0)
  77. return &tic30_coff_howto_table[i];
  78. return NULL;
  79. }
  80. #endif
  81. /* Turn a howto into a reloc number. */
  82. static int
  83. coff_tic30_select_reloc (reloc_howto_type *howto)
  84. {
  85. return howto->type;
  86. }
  87. #define SELECT_RELOC(x,howto) x.r_type = coff_tic30_select_reloc(howto)
  88. #define BADMAG(x) TIC30BADMAG(x)
  89. #define TIC30 1 /* Customize coffcode.h */
  90. #define __A_MAGIC_SET__
  91. /* Code to swap in the reloc */
  92. #define SWAP_IN_RELOC_OFFSET H_GET_32
  93. #define SWAP_OUT_RELOC_OFFSET H_PUT_32
  94. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) dst->r_stuff[0] = 'S'; \
  95. dst->r_stuff[1] = 'C';
  96. /* Code to turn a r_type into a howto ptr, uses the above howto table. */
  97. static void
  98. rtype2howto (arelent *internal, struct internal_reloc *dst)
  99. {
  100. switch (dst->r_type)
  101. {
  102. case R_TIC30_ABS16:
  103. internal->howto = &tic30_coff_howto_table[0];
  104. break;
  105. case R_TIC30_ABS24:
  106. internal->howto = &tic30_coff_howto_table[1];
  107. break;
  108. case R_TIC30_ABS32:
  109. internal->howto = &tic30_coff_howto_table[3];
  110. break;
  111. case R_TIC30_LDP:
  112. internal->howto = &tic30_coff_howto_table[2];
  113. break;
  114. case R_TIC30_PC16:
  115. internal->howto = &tic30_coff_howto_table[4];
  116. break;
  117. default:
  118. internal->howto = NULL;
  119. break;
  120. }
  121. }
  122. #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
  123. /* Perform any necessary magic to the addend in a reloc entry */
  124. #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
  125. cache_ptr->addend = ext_reloc.r_offset;
  126. #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
  127. reloc_processing(relent, reloc, symbols, abfd, section)
  128. static void
  129. reloc_processing (arelent *relent,
  130. struct internal_reloc *reloc,
  131. asymbol **symbols,
  132. bfd *abfd,
  133. asection *section)
  134. {
  135. relent->address = reloc->r_vaddr;
  136. rtype2howto (relent, reloc);
  137. if (reloc->r_symndx == -1)
  138. relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  139. else if (reloc->r_symndx >= 0 && reloc->r_symndx < obj_conv_table_size (abfd))
  140. relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
  141. else
  142. {
  143. _bfd_error_handler
  144. /* xgettext:c-format */
  145. (_("%pB: warning: illegal symbol index %ld in relocs"),
  146. abfd, reloc->r_symndx);
  147. relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  148. }
  149. relent->addend = reloc->r_offset;
  150. relent->address -= section->vma;
  151. }
  152. #ifndef bfd_pe_print_pdata
  153. #define bfd_pe_print_pdata NULL
  154. #endif
  155. #include "coffcode.h"
  156. const bfd_target tic30_coff_vec =
  157. {
  158. "coff-tic30", /* name */
  159. bfd_target_coff_flavour,
  160. BFD_ENDIAN_BIG, /* data byte order is big */
  161. BFD_ENDIAN_LITTLE, /* header byte order is little */
  162. (HAS_RELOC | EXEC_P /* object flags */
  163. | HAS_LINENO | HAS_DEBUG
  164. | HAS_SYMS | HAS_LOCALS | WP_TEXT),
  165. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  166. '_', /* leading symbol underscore */
  167. '/', /* ar_pad_char */
  168. 15, /* ar_max_namelen */
  169. 0, /* match priority. */
  170. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  171. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  172. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  173. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  174. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  175. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  176. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  177. { /* bfd_check_format */
  178. _bfd_dummy_target,
  179. coff_object_p,
  180. bfd_generic_archive_p,
  181. _bfd_dummy_target
  182. },
  183. { /* bfd_set_format */
  184. _bfd_bool_bfd_false_error,
  185. coff_mkobject,
  186. _bfd_generic_mkarchive,
  187. _bfd_bool_bfd_false_error
  188. },
  189. { /* bfd_write_contents */
  190. _bfd_bool_bfd_false_error,
  191. coff_write_object_contents,
  192. _bfd_write_archive_contents,
  193. _bfd_bool_bfd_false_error
  194. },
  195. BFD_JUMP_TABLE_GENERIC (coff),
  196. BFD_JUMP_TABLE_COPY (coff),
  197. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  198. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  199. BFD_JUMP_TABLE_SYMBOLS (coff),
  200. BFD_JUMP_TABLE_RELOCS (coff),
  201. BFD_JUMP_TABLE_WRITE (coff),
  202. BFD_JUMP_TABLE_LINK (coff),
  203. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  204. NULL,
  205. COFF_SWAP_TABLE
  206. };