keystone
keystone copied to clipboard
"Cannot find a symbol" is very unhelpful
I'm using Keystone as part of a compiler and I'm having trouble finding where the missing symbol exists in my 7000 lines of assembly. I'd definitely appreciate a way to print the symbol name in this case.
Can you give one example?
from keystone import *
ks = Ks(KS_ARCH_X86, KS_MODE_32)
ks.asm('mov eax, _sym')
At the output you have "count" value, which indicates the number of statements successfully compiled. That can tell you where the problem is.
This is not always useful, however. I am thinking about adding a new API to pass in symbol value at run time.
Ah, found the underlying problem (.align 3 eats my symbol). Will post a new issue on that.
At the output you have "count" value
The Python API doesn't expose this because an exception is thrown.
status = _ks.ks_asm(self._ksh, string, addr, byref(encode), byref(encode_size), byref(stat_count))
if (status != 0):
errno = _ks.ks_errno(self._ksh)
raise KsError(errno)
An easy solution would be to put the count on the error (maybe using a different error type).
errno = _ks.ks_errno(self._ksh)
raise KsError(errno, stat_count.value)
Then later reference e.count.
Can you send a pull req?
I just tested this, the count doesn't actually show the failed symbol.
mov eax, 1; mov eax, 2; jmp L2 will output a count of 3
mov eax, 1; mov eax, 2; jmp L2; mov eax, 3 will output a count of 4
That is how it works internally: Keystone reports the number of statements successfully parsed. In the second case it returns 4, as expected.
The docs on this param should be improved.
Was responding to this:
At the output you have "count" value, which indicates the number of statements successfully compiled. That can tell you where the problem is.
as above, count tells you how many statements was successfully compiled. it may be useful when the broken statement is the last one, but as in the second case you pointed out above, that is not always helpful.
fixed this issue by extending KsError() to return count via a new method. see commits https://github.com/keystone-engine/keystone/commit/961c2c869d4c2a78bb1c9f292fcf699a5f350602 & sample code for this at https://github.com/keystone-engine/keystone/commit/3aadf88ff3f2174e14cbaead80ec48f340837100
Worth noting that the count doesn't always tell you where the problem is.
I had mis-typed "0x200" as "x200" on line 2 of my 40 line assembly code, and it said it failed on line 40, (presumably because it was looking for a symbol x200....)