s390-dis.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* s390-dis.c -- Disassemble S390 instructions
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
  4. This file is part of the GNU opcodes library.
  5. This library 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, or (at your option)
  8. any later version.
  9. It is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this file; see the file COPYING. If not, write to the
  15. Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include <stdio.h>
  19. #include "ansidecl.h"
  20. #include "disassemble.h"
  21. #include "opintl.h"
  22. #include "opcode/s390.h"
  23. #include "libiberty.h"
  24. static int opc_index[256];
  25. static int current_arch_mask = 0;
  26. static int option_use_insn_len_bits_p = 0;
  27. typedef struct
  28. {
  29. const char *name;
  30. const char *description;
  31. } s390_options_t;
  32. static const s390_options_t options[] =
  33. {
  34. { "esa" , N_("Disassemble in ESA architecture mode") },
  35. { "zarch", N_("Disassemble in z/Architecture mode") },
  36. { "insnlength", N_("Print unknown instructions according to "
  37. "length from first two bits") }
  38. };
  39. /* Set up index table for first opcode byte. */
  40. void
  41. disassemble_init_s390 (struct disassemble_info *info)
  42. {
  43. int i;
  44. const char *p;
  45. memset (opc_index, 0, sizeof (opc_index));
  46. /* Reverse order, such that each opc_index ends up pointing to the
  47. first matching entry instead of the last. */
  48. for (i = s390_num_opcodes; i--; )
  49. opc_index[s390_opcodes[i].opcode[0]] = i;
  50. current_arch_mask = 1 << S390_OPCODE_ZARCH;
  51. option_use_insn_len_bits_p = 0;
  52. for (p = info->disassembler_options; p != NULL; )
  53. {
  54. if (startswith (p, "esa"))
  55. current_arch_mask = 1 << S390_OPCODE_ESA;
  56. else if (startswith (p, "zarch"))
  57. current_arch_mask = 1 << S390_OPCODE_ZARCH;
  58. else if (startswith (p, "insnlength"))
  59. option_use_insn_len_bits_p = 1;
  60. else
  61. /* xgettext:c-format */
  62. opcodes_error_handler (_("unknown S/390 disassembler option: %s"), p);
  63. p = strchr (p, ',');
  64. if (p != NULL)
  65. p++;
  66. }
  67. }
  68. /* Derive the length of an instruction from its first byte. */
  69. static inline int
  70. s390_insn_length (const bfd_byte *buffer)
  71. {
  72. /* 00xxxxxx -> 2, 01xxxxxx/10xxxxxx -> 4, 11xxxxxx -> 6. */
  73. return ((buffer[0] >> 6) + 3) & ~1U;
  74. }
  75. /* Match the instruction in BUFFER against the given OPCODE, excluding
  76. the first byte. */
  77. static inline int
  78. s390_insn_matches_opcode (const bfd_byte *buffer,
  79. const struct s390_opcode *opcode)
  80. {
  81. return (buffer[1] & opcode->mask[1]) == opcode->opcode[1]
  82. && (buffer[2] & opcode->mask[2]) == opcode->opcode[2]
  83. && (buffer[3] & opcode->mask[3]) == opcode->opcode[3]
  84. && (buffer[4] & opcode->mask[4]) == opcode->opcode[4]
  85. && (buffer[5] & opcode->mask[5]) == opcode->opcode[5];
  86. }
  87. union operand_value
  88. {
  89. int i;
  90. unsigned int u;
  91. };
  92. /* Extracts an operand value from an instruction. */
  93. /* We do not perform the shift operation for larl-type address
  94. operands here since that would lead to an overflow of the 32 bit
  95. integer value. Instead the shift operation is done when printing
  96. the operand. */
  97. static inline union operand_value
  98. s390_extract_operand (const bfd_byte *insn,
  99. const struct s390_operand *operand)
  100. {
  101. union operand_value ret;
  102. unsigned int val;
  103. int bits;
  104. const bfd_byte *orig_insn = insn;
  105. /* Extract fragments of the operand byte for byte. */
  106. insn += operand->shift / 8;
  107. bits = (operand->shift & 7) + operand->bits;
  108. val = 0;
  109. do
  110. {
  111. val <<= 8;
  112. val |= (unsigned int) *insn++;
  113. bits -= 8;
  114. }
  115. while (bits > 0);
  116. val >>= -bits;
  117. val &= ((1U << (operand->bits - 1)) << 1) - 1;
  118. /* Check for special long displacement case. */
  119. if (operand->bits == 20 && operand->shift == 20)
  120. val = (val & 0xff) << 12 | (val & 0xfff00) >> 8;
  121. /* Sign extend value if the operand is signed or pc relative. Avoid
  122. integer overflows. */
  123. if (operand->flags & (S390_OPERAND_SIGNED | S390_OPERAND_PCREL))
  124. {
  125. unsigned int m = 1U << (operand->bits - 1);
  126. if (val >= m)
  127. ret.i = (int) (val - m) - 1 - (int) (m - 1U);
  128. else
  129. ret.i = (int) val;
  130. }
  131. else if (operand->flags & S390_OPERAND_LENGTH)
  132. /* Length x in an instruction has real length x + 1. */
  133. ret.u = val + 1;
  134. else if (operand->flags & S390_OPERAND_VR)
  135. {
  136. /* Extract the extra bits for a vector register operand stored
  137. in the RXB field. */
  138. unsigned vr = operand->shift == 32 ? 3
  139. : (unsigned) operand->shift / 4 - 2;
  140. ret.u = val | ((orig_insn[4] & (1 << (3 - vr))) << (vr + 1));
  141. }
  142. else
  143. ret.u = val;
  144. return ret;
  145. }
  146. /* Print the S390 instruction in BUFFER, assuming that it matches the
  147. given OPCODE. */
  148. static void
  149. s390_print_insn_with_opcode (bfd_vma memaddr,
  150. struct disassemble_info *info,
  151. const bfd_byte *buffer,
  152. const struct s390_opcode *opcode)
  153. {
  154. const unsigned char *opindex;
  155. char separator;
  156. /* Mnemonic. */
  157. info->fprintf_func (info->stream, "%s", opcode->name);
  158. /* Operands. */
  159. separator = '\t';
  160. for (opindex = opcode->operands; *opindex != 0; opindex++)
  161. {
  162. const struct s390_operand *operand = s390_operands + *opindex;
  163. union operand_value val = s390_extract_operand (buffer, operand);
  164. unsigned long flags = operand->flags;
  165. if ((flags & S390_OPERAND_INDEX) && val.u == 0)
  166. continue;
  167. if ((flags & S390_OPERAND_BASE) &&
  168. val.u == 0 && separator == '(')
  169. {
  170. separator = ',';
  171. continue;
  172. }
  173. /* For instructions with a last optional operand don't print it
  174. if zero. */
  175. if ((opcode->flags & (S390_INSTR_FLAG_OPTPARM | S390_INSTR_FLAG_OPTPARM2))
  176. && val.u == 0
  177. && opindex[1] == 0)
  178. break;
  179. if ((opcode->flags & S390_INSTR_FLAG_OPTPARM2)
  180. && val.u == 0 && opindex[1] != 0 && opindex[2] == 0)
  181. {
  182. union operand_value next_op_val =
  183. s390_extract_operand (buffer, s390_operands + opindex[1]);
  184. if (next_op_val.u == 0)
  185. break;
  186. }
  187. if (flags & S390_OPERAND_GPR)
  188. info->fprintf_func (info->stream, "%c%%r%u", separator, val.u);
  189. else if (flags & S390_OPERAND_FPR)
  190. info->fprintf_func (info->stream, "%c%%f%u", separator, val.u);
  191. else if (flags & S390_OPERAND_VR)
  192. info->fprintf_func (info->stream, "%c%%v%i", separator, val.u);
  193. else if (flags & S390_OPERAND_AR)
  194. info->fprintf_func (info->stream, "%c%%a%u", separator, val.u);
  195. else if (flags & S390_OPERAND_CR)
  196. info->fprintf_func (info->stream, "%c%%c%u", separator, val.u);
  197. else if (flags & S390_OPERAND_PCREL)
  198. {
  199. info->fprintf_func (info->stream, "%c", separator);
  200. info->print_address_func (memaddr + val.i + val.i, info);
  201. }
  202. else if (flags & S390_OPERAND_SIGNED)
  203. info->fprintf_func (info->stream, "%c%i", separator, val.i);
  204. else
  205. {
  206. if (flags & S390_OPERAND_OR1)
  207. val.u &= ~1;
  208. if (flags & S390_OPERAND_OR2)
  209. val.u &= ~2;
  210. if (flags & S390_OPERAND_OR8)
  211. val.u &= ~8;
  212. if ((opcode->flags & S390_INSTR_FLAG_OPTPARM)
  213. && val.u == 0
  214. && opindex[1] == 0)
  215. break;
  216. info->fprintf_func (info->stream, "%c%u", separator, val.u);
  217. }
  218. if (flags & S390_OPERAND_DISP)
  219. separator = '(';
  220. else if (flags & S390_OPERAND_BASE)
  221. {
  222. info->fprintf_func (info->stream, ")");
  223. separator = ',';
  224. }
  225. else
  226. separator = ',';
  227. }
  228. }
  229. /* Check whether opcode A's mask is more specific than that of B. */
  230. static int
  231. opcode_mask_more_specific (const struct s390_opcode *a,
  232. const struct s390_opcode *b)
  233. {
  234. return (((int) a->mask[0] + a->mask[1] + a->mask[2]
  235. + a->mask[3] + a->mask[4] + a->mask[5])
  236. > ((int) b->mask[0] + b->mask[1] + b->mask[2]
  237. + b->mask[3] + b->mask[4] + b->mask[5]));
  238. }
  239. /* Print a S390 instruction. */
  240. int
  241. print_insn_s390 (bfd_vma memaddr, struct disassemble_info *info)
  242. {
  243. bfd_byte buffer[6];
  244. const struct s390_opcode *opcode = NULL;
  245. unsigned int value;
  246. int status, opsize, bufsize, bytes_to_dump, i;
  247. /* The output looks better if we put 6 bytes on a line. */
  248. info->bytes_per_line = 6;
  249. /* Every S390 instruction is max 6 bytes long. */
  250. memset (buffer, 0, 6);
  251. status = info->read_memory_func (memaddr, buffer, 6, info);
  252. if (status != 0)
  253. {
  254. for (bufsize = 0; bufsize < 6; bufsize++)
  255. if (info->read_memory_func (memaddr, buffer, bufsize + 1, info) != 0)
  256. break;
  257. if (bufsize <= 0)
  258. {
  259. info->memory_error_func (status, memaddr, info);
  260. return -1;
  261. }
  262. opsize = s390_insn_length (buffer);
  263. status = opsize > bufsize;
  264. }
  265. else
  266. {
  267. bufsize = 6;
  268. opsize = s390_insn_length (buffer);
  269. }
  270. if (status == 0)
  271. {
  272. const struct s390_opcode *op;
  273. /* Find the "best match" in the opcode table. */
  274. for (op = s390_opcodes + opc_index[buffer[0]];
  275. op != s390_opcodes + s390_num_opcodes
  276. && op->opcode[0] == buffer[0];
  277. op++)
  278. {
  279. if ((op->modes & current_arch_mask)
  280. && s390_insn_matches_opcode (buffer, op)
  281. && (opcode == NULL
  282. || opcode_mask_more_specific (op, opcode)))
  283. opcode = op;
  284. }
  285. if (opcode != NULL)
  286. {
  287. /* The instruction is valid. Print it and return its size. */
  288. s390_print_insn_with_opcode (memaddr, info, buffer, opcode);
  289. return opsize;
  290. }
  291. }
  292. /* For code sections it makes sense to skip unknown instructions
  293. according to their length bits. */
  294. if (status == 0
  295. && option_use_insn_len_bits_p
  296. && info->section != NULL
  297. && (info->section->flags & SEC_CODE))
  298. bytes_to_dump = opsize;
  299. else
  300. /* By default unknown instructions are printed as .long's/.short'
  301. depending on how many bytes are available. */
  302. bytes_to_dump = bufsize >= 4 ? 4 : bufsize;
  303. if (bytes_to_dump == 0)
  304. return 0;
  305. /* Fall back to hex print. */
  306. switch (bytes_to_dump)
  307. {
  308. case 4:
  309. value = (unsigned int) buffer[0];
  310. value = (value << 8) + (unsigned int) buffer[1];
  311. value = (value << 8) + (unsigned int) buffer[2];
  312. value = (value << 8) + (unsigned int) buffer[3];
  313. info->fprintf_func (info->stream, ".long\t0x%08x", value);
  314. return 4;
  315. case 2:
  316. value = (unsigned int) buffer[0];
  317. value = (value << 8) + (unsigned int) buffer[1];
  318. info->fprintf_func (info->stream, ".short\t0x%04x", value);
  319. return 2;
  320. default:
  321. info->fprintf_func (info->stream, ".byte\t0x%02x",
  322. (unsigned int) buffer[0]);
  323. for (i = 1; i < bytes_to_dump; i++)
  324. info->fprintf_func (info->stream, ",0x%02x",
  325. (unsigned int) buffer[i]);
  326. return bytes_to_dump;
  327. }
  328. return 0;
  329. }
  330. const disasm_options_and_args_t *
  331. disassembler_options_s390 (void)
  332. {
  333. static disasm_options_and_args_t *opts_and_args;
  334. if (opts_and_args == NULL)
  335. {
  336. size_t i, num_options = ARRAY_SIZE (options);
  337. disasm_options_t *opts;
  338. opts_and_args = XNEW (disasm_options_and_args_t);
  339. opts_and_args->args = NULL;
  340. opts = &opts_and_args->options;
  341. opts->name = XNEWVEC (const char *, num_options + 1);
  342. opts->description = XNEWVEC (const char *, num_options + 1);
  343. opts->arg = NULL;
  344. for (i = 0; i < num_options; i++)
  345. {
  346. opts->name[i] = options[i].name;
  347. opts->description[i] = _(options[i].description);
  348. }
  349. /* The array we return must be NULL terminated. */
  350. opts->name[i] = NULL;
  351. opts->description[i] = NULL;
  352. }
  353. return opts_and_args;
  354. }
  355. void
  356. print_s390_disassembler_options (FILE *stream)
  357. {
  358. unsigned int i, max_len = 0;
  359. fprintf (stream, _("\n\
  360. The following S/390 specific disassembler options are supported for use\n\
  361. with the -M switch (multiple options should be separated by commas):\n"));
  362. for (i = 0; i < ARRAY_SIZE (options); i++)
  363. {
  364. unsigned int len = strlen (options[i].name);
  365. if (max_len < len)
  366. max_len = len;
  367. }
  368. for (i = 0, max_len++; i < ARRAY_SIZE (options); i++)
  369. fprintf (stream, " %s%*c %s\n",
  370. options[i].name,
  371. (int)(max_len - strlen (options[i].name)), ' ',
  372. _(options[i].description));
  373. }