aout-ns32k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* BFD back-end for ns32k a.out-ish binaries.
  2. Copyright (C) 1990-2022 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. bool 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. unsigned int *r_index,
  122. int *r_extern,
  123. int *r_pcrel)
  124. {
  125. unsigned int r_length;
  126. unsigned 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. if (r_length > 2 || r_ns32k_type > 2)
  137. return NULL;
  138. return (MY (howto_table) + r_length + 3 * (*r_pcrel) + 6 * r_ns32k_type);
  139. }
  140. #define MY_reloc_howto(BFD, REL, IN, EX, PC) \
  141. MY (reloc_howto) (BFD, REL, &IN, &EX, &PC)
  142. static void
  143. MY (put_reloc) (bfd *abfd,
  144. int r_extern,
  145. int r_index,
  146. bfd_vma value,
  147. reloc_howto_type *howto,
  148. struct reloc_std_external *reloc)
  149. {
  150. unsigned int r_length;
  151. int r_pcrel;
  152. int r_ns32k_type;
  153. PUT_WORD (abfd, value, reloc->r_address);
  154. r_length = howto->size ; /* Size as a power of two. */
  155. r_pcrel = (int) howto->pc_relative; /* Relative to PC? */
  156. r_ns32k_type = (howto - MY (howto_table) )/6;
  157. reloc->r_index[2] = r_index >> 16;
  158. reloc->r_index[1] = r_index >> 8;
  159. reloc->r_index[0] = r_index;
  160. reloc->r_type[0] =
  161. (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0)
  162. | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0)
  163. | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE)
  164. | (r_ns32k_type << RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE);
  165. }
  166. #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \
  167. MY (put_reloc) (BFD, EXT, IDX, VAL, HOWTO, RELOC)
  168. #define STAT_FOR_EXEC
  169. #define MY_final_link_relocate _bfd_ns32k_final_link_relocate
  170. #define MY_relocate_contents _bfd_ns32k_relocate_contents
  171. static void MY_swap_std_reloc_in (bfd *, struct reloc_std_external *, arelent *, asymbol **, bfd_size_type);
  172. static void MY_swap_std_reloc_out (bfd *, arelent *, struct reloc_std_external *);
  173. #include "aoutx.h"
  174. reloc_howto_type *
  175. MY (bfd_reloc_type_lookup) (bfd *abfd, bfd_reloc_code_real_type code)
  176. {
  177. #define ENTRY(i,j) case i: return &MY (howto_table)[j]
  178. int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
  179. BFD_ASSERT (ext == 0);
  180. if (code == BFD_RELOC_CTOR)
  181. switch (bfd_arch_bits_per_address (abfd))
  182. {
  183. case 32:
  184. code = BFD_RELOC_32;
  185. break;
  186. default:
  187. break;
  188. }
  189. switch (code)
  190. {
  191. ENTRY (BFD_RELOC_NS32K_IMM_8, 0);
  192. ENTRY (BFD_RELOC_NS32K_IMM_16, 1);
  193. ENTRY (BFD_RELOC_NS32K_IMM_32, 2);
  194. ENTRY (BFD_RELOC_NS32K_IMM_8_PCREL, 3);
  195. ENTRY (BFD_RELOC_NS32K_IMM_16_PCREL, 4);
  196. ENTRY (BFD_RELOC_NS32K_IMM_32_PCREL, 5);
  197. ENTRY (BFD_RELOC_NS32K_DISP_8, 6);
  198. ENTRY (BFD_RELOC_NS32K_DISP_16, 7);
  199. ENTRY (BFD_RELOC_NS32K_DISP_32, 8);
  200. ENTRY (BFD_RELOC_NS32K_DISP_8_PCREL, 9);
  201. ENTRY (BFD_RELOC_NS32K_DISP_16_PCREL, 10);
  202. ENTRY (BFD_RELOC_NS32K_DISP_32_PCREL, 11);
  203. ENTRY (BFD_RELOC_8, 12);
  204. ENTRY (BFD_RELOC_16, 13);
  205. ENTRY (BFD_RELOC_32, 14);
  206. ENTRY (BFD_RELOC_8_PCREL, 15);
  207. ENTRY (BFD_RELOC_16_PCREL, 16);
  208. ENTRY (BFD_RELOC_32_PCREL, 17);
  209. default:
  210. return NULL;
  211. }
  212. #undef ENTRY
  213. }
  214. reloc_howto_type *
  215. MY (bfd_reloc_name_lookup) (bfd *abfd ATTRIBUTE_UNUSED,
  216. const char *r_name)
  217. {
  218. unsigned int i;
  219. for (i = 0;
  220. i < sizeof (MY (howto_table)) / sizeof (MY (howto_table)[0]);
  221. i++)
  222. if (MY (howto_table)[i].name != NULL
  223. && strcasecmp (MY (howto_table)[i].name, r_name) == 0)
  224. return &MY (howto_table)[i];
  225. return NULL;
  226. }
  227. static void
  228. MY_swap_std_reloc_in (bfd *abfd,
  229. struct reloc_std_external *bytes,
  230. arelent *cache_ptr,
  231. asymbol **symbols,
  232. bfd_size_type symcount ATTRIBUTE_UNUSED)
  233. {
  234. unsigned int r_index;
  235. int r_extern;
  236. int r_pcrel;
  237. struct aoutdata *su = &(abfd->tdata.aout_data->a);
  238. cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
  239. /* Now the fun stuff. */
  240. cache_ptr->howto = MY_reloc_howto (abfd, bytes, r_index, r_extern, r_pcrel);
  241. MOVE_ADDRESS (0);
  242. }
  243. static void
  244. MY_swap_std_reloc_out (bfd *abfd,
  245. arelent *g,
  246. struct reloc_std_external *natptr)
  247. {
  248. int r_index;
  249. asymbol *sym = *(g->sym_ptr_ptr);
  250. int r_extern;
  251. asection *output_section = sym->section->output_section;
  252. /* Name was clobbered by aout_write_syms to be symbol index. */
  253. /* If this relocation is relative to a symbol then set the
  254. r_index to the symbols index, and the r_extern bit.
  255. Absolute symbols can come in in two ways, either as an offset
  256. from the abs section, or as a symbol which has an abs value.
  257. Check for that here. */
  258. if (bfd_is_com_section (output_section)
  259. || bfd_is_abs_section (output_section)
  260. || bfd_is_und_section (output_section))
  261. {
  262. if (bfd_abs_section_ptr->symbol == sym)
  263. {
  264. /* Whoops, looked like an abs symbol, but is really an offset
  265. from the abs section. */
  266. r_index = 0;
  267. r_extern = 0;
  268. }
  269. else
  270. {
  271. /* Fill in symbol. */
  272. r_extern = 1;
  273. #undef KEEPIT
  274. #define KEEPIT udata.i
  275. r_index = (*(g->sym_ptr_ptr))->KEEPIT;
  276. #undef KEEPIT
  277. }
  278. }
  279. else
  280. {
  281. /* Just an ordinary section. */
  282. r_extern = 0;
  283. r_index = output_section->target_index;
  284. }
  285. MY_put_reloc (abfd, r_extern, r_index, g->address, g->howto, natptr);
  286. }
  287. bfd_reloc_status_type
  288. _bfd_ns32k_relocate_contents (reloc_howto_type *howto,
  289. bfd *input_bfd,
  290. bfd_vma relocation,
  291. bfd_byte *location)
  292. {
  293. int r_ns32k_type = (howto - MY (howto_table)) / 6;
  294. bfd_vma (*get_data) (bfd_byte *, int);
  295. void (*put_data) (bfd_vma, bfd_byte *, int);
  296. switch (r_ns32k_type)
  297. {
  298. case 0:
  299. get_data = _bfd_ns32k_get_immediate;
  300. put_data = _bfd_ns32k_put_immediate;
  301. break;
  302. case 1:
  303. get_data = _bfd_ns32k_get_displacement;
  304. put_data = _bfd_ns32k_put_displacement;
  305. break;
  306. case 2:
  307. return _bfd_relocate_contents (howto, input_bfd, relocation,
  308. location);
  309. default:
  310. return bfd_reloc_notsupported;
  311. }
  312. return _bfd_do_ns32k_reloc_contents (howto, input_bfd, relocation,
  313. location, get_data, put_data);
  314. }