aout-ns32k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* BFD back-end for ns32k a.out-ish binaries.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Contributed by Ian Dall (idall@eleceng.adelaide.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,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "aout/aout64.h"
  20. #include "ns32k.h"
  21. /* Do not "beautify" the CONCAT* macro args. Traditional C will not
  22. remove whitespace added here, and thus will fail to concatenate
  23. the tokens. */
  24. #define MYNS(OP) CONCAT2 (ns32k_aout_,OP)
  25. reloc_howto_type * MYNS (bfd_reloc_type_lookup) (bfd *, bfd_reloc_code_real_type);
  26. reloc_howto_type * MYNS (bfd_reloc_name_lookup) (bfd *, const char *);
  27. bfd_boolean MYNS (write_object_contents) (bfd *);
  28. /* Avoid multiple definitions from aoutx if supporting
  29. standard a.out format(s) as well as this one. */
  30. #define NAME(x,y) CONCAT3 (ns32kaout,_32_,y)
  31. void bfd_ns32k_arch (void);
  32. #include "libaout.h"
  33. #define MY(OP) MYNS (OP)
  34. #define MY_swap_std_reloc_in MY (swap_std_reloc_in)
  35. #define MY_swap_std_reloc_out MY (swap_std_reloc_out)
  36. /* The ns32k series is ah, unusual, when it comes to relocation.
  37. There are three storage methods for relocatable objects. There
  38. are displacements, immediate operands and ordinary twos complement
  39. data. Of these, only the last fits into the standard relocation
  40. scheme. Immediate operands are stored huffman encoded and
  41. immediate operands are stored big endian (where as the natural byte
  42. order is little endian for this architecture).
  43. Note that the ns32k displacement storage method is orthogonal to
  44. whether the relocation is pc relative or not. The "displacement"
  45. storage scheme is used for essentially all address constants. The
  46. displacement can be relative to zero (absolute displacement),
  47. relative to the pc (pc relative), the stack pointer, the frame
  48. pointer, the static base register and general purpose register etc.
  49. For example:
  50. sym1: .long . # pc relative 2's complement
  51. sym1: .long foo # 2's complement not pc relative
  52. self: movd @self, r0 # pc relative displacement
  53. movd foo, r0 # non pc relative displacement
  54. self: movd self, r0 # pc relative immediate
  55. movd foo, r0 # non pc relative immediate
  56. In addition, for historical reasons the encoding of the relocation types
  57. in the a.out format relocation entries is such that even the relocation
  58. methods which are standard are not encoded the standard way. */
  59. reloc_howto_type MY (howto_table)[] =
  60. {
  61. /* ns32k immediate operands. */
  62. HOWTO (BFD_RELOC_NS32K_IMM_8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
  63. _bfd_ns32k_reloc_imm, "NS32K_IMM_8",
  64. TRUE, 0x000000ff,0x000000ff, FALSE),
  65. HOWTO (BFD_RELOC_NS32K_IMM_16, 0, 1, 16, FALSE, 0, complain_overflow_signed,
  66. _bfd_ns32k_reloc_imm, "NS32K_IMM_16",
  67. TRUE, 0x0000ffff,0x0000ffff, FALSE),
  68. HOWTO (BFD_RELOC_NS32K_IMM_32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
  69. _bfd_ns32k_reloc_imm, "NS32K_IMM_32",
  70. TRUE, 0xffffffff,0xffffffff, FALSE),
  71. HOWTO (BFD_RELOC_NS32K_IMM_8_PCREL, 0, 0, 8, TRUE, 0, complain_overflow_signed,
  72. _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_8",
  73. TRUE, 0x000000ff, 0x000000ff, FALSE),
  74. HOWTO (BFD_RELOC_NS32K_IMM_16_PCREL, 0, 1, 16, TRUE, 0, complain_overflow_signed,
  75. _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_16",
  76. TRUE, 0x0000ffff,0x0000ffff, FALSE),
  77. HOWTO (BFD_RELOC_NS32K_IMM_32_PCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
  78. _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_32",
  79. TRUE, 0xffffffff,0xffffffff, FALSE),
  80. /* ns32k displacements. */
  81. HOWTO (BFD_RELOC_NS32K_DISP_8, 0, 0, 7, FALSE, 0, complain_overflow_signed,
  82. _bfd_ns32k_reloc_disp, "NS32K_DISP_8",
  83. TRUE, 0x000000ff,0x000000ff, FALSE),
  84. HOWTO (BFD_RELOC_NS32K_DISP_16, 0, 1, 14, FALSE, 0, complain_overflow_signed,
  85. _bfd_ns32k_reloc_disp, "NS32K_DISP_16",
  86. TRUE, 0x0000ffff, 0x0000ffff, FALSE),
  87. HOWTO (BFD_RELOC_NS32K_DISP_32, 0, 2, 30, FALSE, 0, complain_overflow_signed,
  88. _bfd_ns32k_reloc_disp, "NS32K_DISP_32",
  89. TRUE, 0xffffffff, 0xffffffff, FALSE),
  90. HOWTO (BFD_RELOC_NS32K_DISP_8_PCREL, 0, 0, 7, TRUE, 0, complain_overflow_signed,
  91. _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_8",
  92. TRUE, 0x000000ff,0x000000ff, FALSE),
  93. HOWTO (BFD_RELOC_NS32K_DISP_16_PCREL, 0, 1, 14, TRUE, 0, complain_overflow_signed,
  94. _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_16",
  95. TRUE, 0x0000ffff,0x0000ffff, FALSE),
  96. HOWTO (BFD_RELOC_NS32K_DISP_32_PCREL, 0, 2, 30, TRUE, 0, complain_overflow_signed,
  97. _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_32",
  98. TRUE, 0xffffffff,0xffffffff, FALSE),
  99. /* Normal 2's complement. */
  100. HOWTO (BFD_RELOC_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,0,
  101. "8", TRUE, 0x000000ff,0x000000ff, FALSE),
  102. HOWTO (BFD_RELOC_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,
  103. "16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
  104. HOWTO (BFD_RELOC_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,
  105. "32", TRUE, 0xffffffff,0xffffffff, FALSE),
  106. HOWTO (BFD_RELOC_8_PCREL, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0,
  107. "PCREL_8", TRUE, 0x000000ff,0x000000ff, FALSE),
  108. HOWTO (BFD_RELOC_16_PCREL, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0,
  109. "PCREL_16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
  110. HOWTO (BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0,
  111. "PCREL_32", TRUE, 0xffffffff,0xffffffff, FALSE),
  112. };
  113. #define CTOR_TABLE_RELOC_HOWTO(BFD) (MY (howto_table) + 14)
  114. #define RELOC_STD_BITS_NS32K_TYPE_BIG 0x06
  115. #define RELOC_STD_BITS_NS32K_TYPE_LITTLE 0x60
  116. #define RELOC_STD_BITS_NS32K_TYPE_SH_BIG 1
  117. #define RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE 5
  118. static reloc_howto_type *
  119. MY (reloc_howto) (bfd *abfd ATTRIBUTE_UNUSED,
  120. struct reloc_std_external *rel,
  121. int *r_index,
  122. int *r_extern,
  123. int *r_pcrel)
  124. {
  125. unsigned int r_length;
  126. int r_ns32k_type;
  127. *r_index = ((rel->r_index[2] << 16)
  128. | (rel->r_index[1] << 8)
  129. | rel->r_index[0] );
  130. *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
  131. *r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
  132. r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
  133. >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
  134. r_ns32k_type = ((rel->r_type[0] & RELOC_STD_BITS_NS32K_TYPE_LITTLE)
  135. >> RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE);
  136. return (MY (howto_table) + r_length + 3 * (*r_pcrel) + 6 * r_ns32k_type);
  137. }
  138. #define MY_reloc_howto(BFD, REL, IN, EX, PC) \
  139. MY (reloc_howto) (BFD, REL, &IN, &EX, &PC)
  140. static void
  141. MY (put_reloc) (bfd *abfd,
  142. int r_extern,
  143. int r_index,
  144. bfd_vma value,
  145. reloc_howto_type *howto,
  146. struct reloc_std_external *reloc)
  147. {
  148. unsigned int r_length;
  149. int r_pcrel;
  150. int r_ns32k_type;
  151. PUT_WORD (abfd, value, reloc->r_address);
  152. r_length = howto->size ; /* Size as a power of two. */
  153. r_pcrel = (int) howto->pc_relative; /* Relative to PC? */
  154. r_ns32k_type = (howto - MY (howto_table) )/6;
  155. reloc->r_index[2] = r_index >> 16;
  156. reloc->r_index[1] = r_index >> 8;
  157. reloc->r_index[0] = r_index;
  158. reloc->r_type[0] =
  159. (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0)
  160. | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0)
  161. | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE)
  162. | (r_ns32k_type << RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE);
  163. }
  164. #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \
  165. MY (put_reloc) (BFD, EXT, IDX, VAL, HOWTO, RELOC)
  166. #define STAT_FOR_EXEC
  167. #define MY_final_link_relocate _bfd_ns32k_final_link_relocate
  168. #define MY_relocate_contents _bfd_ns32k_relocate_contents
  169. static void MY_swap_std_reloc_in (bfd *, struct reloc_std_external *, arelent *, asymbol **, bfd_size_type);
  170. static void MY_swap_std_reloc_out (bfd *, arelent *, struct reloc_std_external *);
  171. #include "aoutx.h"
  172. reloc_howto_type *
  173. MY (bfd_reloc_type_lookup) (bfd *abfd, bfd_reloc_code_real_type code)
  174. {
  175. #define ENTRY(i,j) case i: return &MY (howto_table)[j]
  176. int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
  177. BFD_ASSERT (ext == 0);
  178. if (code == BFD_RELOC_CTOR)
  179. switch (bfd_arch_bits_per_address (abfd))
  180. {
  181. case 32:
  182. code = BFD_RELOC_32;
  183. break;
  184. default:
  185. break;
  186. }
  187. switch (code)
  188. {
  189. ENTRY (BFD_RELOC_NS32K_IMM_8, 0);
  190. ENTRY (BFD_RELOC_NS32K_IMM_16, 1);
  191. ENTRY (BFD_RELOC_NS32K_IMM_32, 2);
  192. ENTRY (BFD_RELOC_NS32K_IMM_8_PCREL, 3);
  193. ENTRY (BFD_RELOC_NS32K_IMM_16_PCREL, 4);
  194. ENTRY (BFD_RELOC_NS32K_IMM_32_PCREL, 5);
  195. ENTRY (BFD_RELOC_NS32K_DISP_8, 6);
  196. ENTRY (BFD_RELOC_NS32K_DISP_16, 7);
  197. ENTRY (BFD_RELOC_NS32K_DISP_32, 8);
  198. ENTRY (BFD_RELOC_NS32K_DISP_8_PCREL, 9);
  199. ENTRY (BFD_RELOC_NS32K_DISP_16_PCREL, 10);
  200. ENTRY (BFD_RELOC_NS32K_DISP_32_PCREL, 11);
  201. ENTRY (BFD_RELOC_8, 12);
  202. ENTRY (BFD_RELOC_16, 13);
  203. ENTRY (BFD_RELOC_32, 14);
  204. ENTRY (BFD_RELOC_8_PCREL, 15);
  205. ENTRY (BFD_RELOC_16_PCREL, 16);
  206. ENTRY (BFD_RELOC_32_PCREL, 17);
  207. default:
  208. return NULL;
  209. }
  210. #undef ENTRY
  211. }
  212. reloc_howto_type *
  213. MY (bfd_reloc_name_lookup) (bfd *abfd ATTRIBUTE_UNUSED,
  214. const char *r_name)
  215. {
  216. unsigned int i;
  217. for (i = 0;
  218. i < sizeof (MY (howto_table)) / sizeof (MY (howto_table)[0]);
  219. i++)
  220. if (MY (howto_table)[i].name != NULL
  221. && strcasecmp (MY (howto_table)[i].name, r_name) == 0)
  222. return &MY (howto_table)[i];
  223. return NULL;
  224. }
  225. static void
  226. MY_swap_std_reloc_in (bfd *abfd,
  227. struct reloc_std_external *bytes,
  228. arelent *cache_ptr,
  229. asymbol **symbols,
  230. bfd_size_type symcount ATTRIBUTE_UNUSED)
  231. {
  232. int r_index;
  233. int r_extern;
  234. int r_pcrel;
  235. struct aoutdata *su = &(abfd->tdata.aout_data->a);
  236. cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
  237. /* Now the fun stuff. */
  238. cache_ptr->howto = MY_reloc_howto (abfd, bytes, r_index, r_extern, r_pcrel);
  239. MOVE_ADDRESS (0);
  240. }
  241. static void
  242. MY_swap_std_reloc_out (bfd *abfd,
  243. arelent *g,
  244. struct reloc_std_external *natptr)
  245. {
  246. int r_index;
  247. asymbol *sym = *(g->sym_ptr_ptr);
  248. int r_extern;
  249. asection *output_section = sym->section->output_section;
  250. /* Name was clobbered by aout_write_syms to be symbol index. */
  251. /* If this relocation is relative to a symbol then set the
  252. r_index to the symbols index, and the r_extern bit.
  253. Absolute symbols can come in in two ways, either as an offset
  254. from the abs section, or as a symbol which has an abs value.
  255. Check for that here. */
  256. if (bfd_is_com_section (output_section)
  257. || bfd_is_abs_section (output_section)
  258. || bfd_is_und_section (output_section))
  259. {
  260. if (bfd_abs_section_ptr->symbol == sym)
  261. {
  262. /* Whoops, looked like an abs symbol, but is really an offset
  263. from the abs section. */
  264. r_index = 0;
  265. r_extern = 0;
  266. }
  267. else
  268. {
  269. /* Fill in symbol. */
  270. r_extern = 1;
  271. #undef KEEPIT
  272. #define KEEPIT udata.i
  273. r_index = (*(g->sym_ptr_ptr))->KEEPIT;
  274. #undef KEEPIT
  275. }
  276. }
  277. else
  278. {
  279. /* Just an ordinary section. */
  280. r_extern = 0;
  281. r_index = output_section->target_index;
  282. }
  283. MY_put_reloc (abfd, r_extern, r_index, g->address, g->howto, natptr);
  284. }
  285. bfd_reloc_status_type
  286. _bfd_ns32k_relocate_contents (reloc_howto_type *howto,
  287. bfd *input_bfd,
  288. bfd_vma relocation,
  289. bfd_byte *location)
  290. {
  291. int r_ns32k_type = (howto - MY (howto_table)) / 6;
  292. bfd_vma (*get_data) (bfd_byte *, int);
  293. void (*put_data) (bfd_vma, bfd_byte *, int);
  294. switch (r_ns32k_type)
  295. {
  296. case 0:
  297. get_data = _bfd_ns32k_get_immediate;
  298. put_data = _bfd_ns32k_put_immediate;
  299. break;
  300. case 1:
  301. get_data = _bfd_ns32k_get_displacement;
  302. put_data = _bfd_ns32k_put_displacement;
  303. break;
  304. case 2:
  305. return _bfd_relocate_contents (howto, input_bfd, relocation,
  306. location);
  307. default:
  308. return bfd_reloc_notsupported;
  309. }
  310. return _bfd_do_ns32k_reloc_contents (howto, input_bfd, relocation,
  311. location, get_data, put_data);
  312. }