Skip to content

Commit c6d3da9

Browse files
committed
disas/capstone: Add skipdata hook for s390x
It is always possible to tell the length of an insn, even if the actual insn is unknown. Skip the correct number of bytes, so that we stay in sync with the instruction stream. Acked-by: Thomas Huth <[email protected]> Reviewed-by: Alex Bennée <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Signed-off-by: Richard Henderson <[email protected]>
1 parent 3d56284 commit c6d3da9

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

disas/capstone.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@
1616
*/
1717
static __thread cs_insn *cap_insn;
1818

19+
/*
20+
* The capstone library always skips 2 bytes for S390X.
21+
* This is less than ideal, since we can tell from the first two bits
22+
* the size of the insn and thus stay in sync with the insn stream.
23+
*/
24+
static size_t CAPSTONE_API
25+
cap_skipdata_s390x_cb(const uint8_t *code, size_t code_size,
26+
size_t offset, void *user_data)
27+
{
28+
size_t ilen;
29+
30+
/* See get_ilen() in target/s390x/internal.h. */
31+
switch (code[offset] >> 6) {
32+
case 0:
33+
ilen = 2;
34+
break;
35+
case 1:
36+
case 2:
37+
ilen = 4;
38+
break;
39+
default:
40+
ilen = 6;
41+
break;
42+
}
43+
44+
return ilen;
45+
}
46+
47+
static const cs_opt_skipdata cap_skipdata_s390x = {
48+
.mnemonic = ".byte",
49+
.callback = cap_skipdata_s390x_cb
50+
};
51+
1952
/*
2053
* Initialize the Capstone library.
2154
*
@@ -42,13 +75,20 @@ static cs_err cap_disas_start(disassemble_info *info, csh *handle)
4275
/* "Disassemble" unknown insns as ".byte W,X,Y,Z". */
4376
cs_option(*handle, CS_OPT_SKIPDATA, CS_OPT_ON);
4477

45-
if (info->cap_arch == CS_ARCH_X86) {
78+
switch (info->cap_arch) {
79+
case CS_ARCH_SYSZ:
80+
cs_option(*handle, CS_OPT_SKIPDATA_SETUP,
81+
(uintptr_t)&cap_skipdata_s390x);
82+
break;
83+
84+
case CS_ARCH_X86:
4685
/*
4786
* We don't care about errors (if for some reason the library
4887
* is compiled without AT&T syntax); the user will just have
4988
* to deal with the Intel syntax.
5089
*/
5190
cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
91+
break;
5292
}
5393

5494
/* Allocate temp space for cs_disasm_iter. */

0 commit comments

Comments
 (0)