coff-sparc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* BFD back-end for Sparc COFF files.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  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 "coff/sparc.h"
  21. #include "coff/internal.h"
  22. #include "libcoff.h"
  23. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
  24. #define BADMAG(x) ((x).f_magic != SPARCMAGIC && (x).f_magic != LYNXCOFFMAGIC)
  25. /* The page size is a guess based on ELF. */
  26. #define COFF_PAGE_SIZE 0x10000
  27. enum reloc_type
  28. {
  29. R_SPARC_NONE = 0,
  30. R_SPARC_8, R_SPARC_16, R_SPARC_32,
  31. R_SPARC_DISP8, R_SPARC_DISP16, R_SPARC_DISP32,
  32. R_SPARC_WDISP30, R_SPARC_WDISP22,
  33. R_SPARC_HI22, R_SPARC_22,
  34. R_SPARC_13, R_SPARC_LO10,
  35. R_SPARC_GOT10, R_SPARC_GOT13, R_SPARC_GOT22,
  36. R_SPARC_PC10, R_SPARC_PC22,
  37. R_SPARC_WPLT30,
  38. R_SPARC_COPY,
  39. R_SPARC_GLOB_DAT, R_SPARC_JMP_SLOT,
  40. R_SPARC_RELATIVE,
  41. R_SPARC_UA32,
  42. R_SPARC_max
  43. };
  44. /* This is stolen pretty directly from elf.c. */
  45. static bfd_reloc_status_type
  46. bfd_coff_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  47. arelent *reloc_entry,
  48. asymbol *symbol,
  49. void * data ATTRIBUTE_UNUSED,
  50. asection *input_section,
  51. bfd *output_bfd,
  52. char **error_message ATTRIBUTE_UNUSED)
  53. {
  54. if (output_bfd != (bfd *) NULL
  55. && (symbol->flags & BSF_SECTION_SYM) == 0)
  56. {
  57. reloc_entry->address += input_section->output_offset;
  58. return bfd_reloc_ok;
  59. }
  60. return bfd_reloc_continue;
  61. }
  62. static reloc_howto_type coff_sparc_howto_table[] =
  63. {
  64. HOWTO(R_SPARC_NONE, 0,3, 0,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
  65. HOWTO(R_SPARC_8, 0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_8", FALSE,0,0x000000ff,TRUE),
  66. HOWTO(R_SPARC_16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_16", FALSE,0,0x0000ffff,TRUE),
  67. HOWTO(R_SPARC_32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_32", FALSE,0,0xffffffff,TRUE),
  68. HOWTO(R_SPARC_DISP8, 0,0, 8,TRUE, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_DISP8", FALSE,0,0x000000ff,TRUE),
  69. HOWTO(R_SPARC_DISP16, 0,1,16,TRUE, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_DISP16", FALSE,0,0x0000ffff,TRUE),
  70. HOWTO(R_SPARC_DISP32, 0,2,32,TRUE, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_DISP32", FALSE,0,0x00ffffff,TRUE),
  71. HOWTO(R_SPARC_WDISP30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
  72. HOWTO(R_SPARC_WDISP22, 2,2,22,TRUE, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
  73. HOWTO(R_SPARC_HI22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_HI22", FALSE,0,0x003fffff,TRUE),
  74. HOWTO(R_SPARC_22, 0,2,22,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_22", FALSE,0,0x003fffff,TRUE),
  75. HOWTO(R_SPARC_13, 0,2,13,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_13", FALSE,0,0x00001fff,TRUE),
  76. HOWTO(R_SPARC_LO10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_LO10", FALSE,0,0x000003ff,TRUE),
  77. HOWTO(R_SPARC_GOT10, 0,2,10,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT10", FALSE,0,0x000003ff,TRUE),
  78. HOWTO(R_SPARC_GOT13, 0,2,13,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT13", FALSE,0,0x00001fff,TRUE),
  79. HOWTO(R_SPARC_GOT22, 10,2,22,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT22", FALSE,0,0x003fffff,TRUE),
  80. HOWTO(R_SPARC_PC10, 0,2,10,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_PC10", FALSE,0,0x000003ff,TRUE),
  81. HOWTO(R_SPARC_PC22, 0,2,22,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_PC22", FALSE,0,0x003fffff,TRUE),
  82. HOWTO(R_SPARC_WPLT30, 0,0,00,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_WPLT30", FALSE,0,0x00000000,TRUE),
  83. HOWTO(R_SPARC_COPY, 0,0,00,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_COPY", FALSE,0,0x00000000,TRUE),
  84. HOWTO(R_SPARC_GLOB_DAT,0,0,00,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_GLOB_DAT",FALSE,0,0x00000000,TRUE),
  85. HOWTO(R_SPARC_JMP_SLOT,0,0,00,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_JMP_SLOT",FALSE,0,0x00000000,TRUE),
  86. HOWTO(R_SPARC_RELATIVE,0,0,00,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
  87. HOWTO(R_SPARC_UA32, 0,0,00,FALSE,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_UA32", FALSE,0,0x00000000,TRUE),
  88. };
  89. struct coff_reloc_map
  90. {
  91. bfd_reloc_code_real_type bfd_reloc_val;
  92. unsigned char coff_reloc_val;
  93. };
  94. static const struct coff_reloc_map sparc_reloc_map[] =
  95. {
  96. { BFD_RELOC_NONE, R_SPARC_NONE, },
  97. { BFD_RELOC_16, R_SPARC_16, },
  98. { BFD_RELOC_8, R_SPARC_8 },
  99. { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
  100. { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits. */
  101. { BFD_RELOC_32, R_SPARC_32 },
  102. { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
  103. { BFD_RELOC_HI22, R_SPARC_HI22 },
  104. { BFD_RELOC_LO10, R_SPARC_LO10, },
  105. { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
  106. { BFD_RELOC_SPARC22, R_SPARC_22 },
  107. { BFD_RELOC_SPARC13, R_SPARC_13 },
  108. { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
  109. { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
  110. { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
  111. { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
  112. { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
  113. { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
  114. { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
  115. { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
  116. { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
  117. { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
  118. { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
  119. /* { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
  120. };
  121. static reloc_howto_type *
  122. coff_sparc_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  123. bfd_reloc_code_real_type code)
  124. {
  125. unsigned int i;
  126. for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct coff_reloc_map); i++)
  127. {
  128. if (sparc_reloc_map[i].bfd_reloc_val == code)
  129. return &coff_sparc_howto_table[(int) sparc_reloc_map[i].coff_reloc_val];
  130. }
  131. return 0;
  132. }
  133. #define coff_bfd_reloc_type_lookup coff_sparc_reloc_type_lookup
  134. static reloc_howto_type *
  135. coff_sparc_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  136. const char *r_name)
  137. {
  138. unsigned int i;
  139. for (i = 0;
  140. i < (sizeof (coff_sparc_howto_table)
  141. / sizeof (coff_sparc_howto_table[0]));
  142. i++)
  143. if (coff_sparc_howto_table[i].name != NULL
  144. && strcasecmp (coff_sparc_howto_table[i].name, r_name) == 0)
  145. return &coff_sparc_howto_table[i];
  146. return NULL;
  147. }
  148. #define coff_bfd_reloc_name_lookup coff_sparc_reloc_name_lookup
  149. static void
  150. rtype2howto (arelent *cache_ptr, struct internal_reloc *dst)
  151. {
  152. BFD_ASSERT (dst->r_type < (unsigned int) R_SPARC_max);
  153. cache_ptr->howto = &coff_sparc_howto_table[dst->r_type];
  154. }
  155. #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
  156. #define SWAP_IN_RELOC_OFFSET H_GET_32
  157. #define SWAP_OUT_RELOC_OFFSET H_PUT_32
  158. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
  159. cache_ptr->addend = reloc.r_offset;
  160. /* Clear the r_spare field in relocs. */
  161. #define SWAP_OUT_RELOC_EXTRA(abfd,src,dst) \
  162. do { \
  163. dst->r_spare[0] = 0; \
  164. dst->r_spare[1] = 0; \
  165. } while (0)
  166. #define __A_MAGIC_SET__
  167. /* Enable Sparc-specific hacks in coffcode.h. */
  168. #define COFF_SPARC
  169. #define bfd_pe_print_pdata NULL
  170. #include "coffcode.h"
  171. #ifndef TARGET_SYM
  172. #define TARGET_SYM sparc_coff_vec
  173. #endif
  174. #ifndef TARGET_NAME
  175. #define TARGET_NAME "coff-sparc"
  176. #endif
  177. CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, '_', NULL, COFF_SWAP_TABLE)