summaryrefslogtreecommitdiff
path: root/bindings/python/llvm/disassembler.py
blob: d1fd789dbaf527e579b0678dc7958e1f4e0d981b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#===- disassembler.py - Python LLVM Bindings -----------------*- python -*--===#
#
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
#===------------------------------------------------------------------------===#

from abc import ABCMeta
from abc import abstractmethod

from ctypes import CFUNCTYPE
from ctypes import POINTER
from ctypes import byref
from ctypes import c_char_p
from ctypes import c_int
from ctypes import c_ubyte
from ctypes import c_uint64
from ctypes import c_uint
from ctypes import c_void_p
from ctypes import memmove

from .common import CachedProperty
from .common import LLVMObject
from .common import c_object_p
from .common import get_library

__all__ = [
    'DisassemblerByteArraySource',
    'DisassemblerFileSource',
    'DisassemblerSource',
    'Disassembler',
    'Instruction',
    'Operand',
    'Token',
]

callbacks = {}

class DisassemblerSource:
    """Abstract base class for disassembler input.

    This defines the interface to which inputs to the disassembler must
    conform.

    Basically, the disassembler input is a read-only sequence of a finite
    length.
    """
    __metaclass__ = ABCMeta

    @abstractmethod
    def __len__(self):
        """Returns the number of bytes that are available for input."""
        pass

    @abstractmethod
    def get_byte(self, address):
        """Returns the byte at the specified address."""
        pass

    @abstractmethod
    def start_address(self):
        """Returns the address at which to start fetch bytes, as a long."""
        pass

class DisassemblerByteArraySource(DisassemblerSource):
    """A disassembler source for byte arrays."""

    def __init__(self, b):
        self._array = b

    def __len__(self):
        return len(self._array)

    def get_byte(self, address):
        return self._array[address]

    def start_address(self):
        return 0

class DisassemblerFileSource(DisassemblerSource):
    """A disassembler source for file segments.

    This allows you to feed in segments of a file into a Disassembler.
    """

    def __init__(self, filename, start_offset, length=None, end_offset=None,
                 start_address=None):
        """Create a new source from a file.

        A source begins at a specified byte offset and can be defined in terms
        of byte length of the end byte offset.
        """
        if length is None and end_offset is None:
            raise Exception('One of length or end_offset must be defined.')

        self._start_address = start_address
        if self._start_address is None:
            self._start_address = 0

        count = length
        if length is None:
            count = end_offset - start_offset

        with open(filename, 'rb') as fh:
            fh.seek(start_offset)

            # FIXME handle case where read bytes != requested
            self._buf = fh.read(count)

    def __len__(self):
        return len(self._buf)

    def get_byte(self, address):
        return self._buf[address - self._start_address]

    def start_address(self):
        return self._start_address

class Disassembler(LLVMObject):
    """Interface to LLVM's enhanced disassembler.

    The API is slightly different from the C API in that we tightly couple a
    disassembler instance to an input source. This saves an extra level of
    abstraction and makes the Python implementation easier.
    """

    SYNTAX_X86_INTEL = 0
    SYNTAX_X86_ATT = 1
    SYNTAX_ARM_UAL = 2

    def __init__(self, triple, source, syntax=0):
        """Create a new disassembler instance.

        Arguments:

        triple -- str target type (e.g. x86_64-apple-darwin10)
        source -- DisassemblerSource instance to be fed into this disassembler.
        syntax -- The assembly syntax to use. One of the SYNTAX_* class
            constants. e.g. EnhancedDisassembler.SYNTAX_X86_INTEL
        """
        assert isinstance(source, DisassemblerSource)

        ptr = c_object_p()
        result = lib.EDGetDisassembler(byref(ptr), c_char_p(triple),
                                       c_int(syntax))
        if result != 0:
            raise Exception('Non-0 return code.')

        LLVMObject.__init__(self, ptr)

        self._source = source

    def get_instructions(self):
        """Obtain the instructions from the input.

        This is a generator for Instruction instances.

        By default, this will return instructions for the entire source which
        has been defined. It does this by querying the source's start_address()
        method and continues to request instructions until len(source) is
        exhausted.
        """

        # We currently obtain 1 instruction at a time because it is easiest.

        # This serves as our EDByteReaderCallback. It is a proxy between C and
        # the Python DisassemblerSource.
        def byte_reader(dest, address, arg):
            try:
                byte = self._source.get_byte(address)
                memmove(dest, byte, 1)

                return 0
            except:
                return -1

        address = self._source.start_address()
        end_address = address + len(self._source)
        cb = callbacks['byte_reader'](byte_reader)
        while address < end_address:
            ptr = c_object_p()

            result = lib.EDCreateInsts(byref(ptr), c_uint(1), self, cb,
                                       address, c_void_p(None))

            if result != 1:
                raise Exception('Error obtaining instruction at address %d' %
                        address)

            instruction = Instruction(ptr, self)
            yield instruction

            address += instruction.byte_size


class Instruction(LLVMObject):
    """Represents an individual instruction.

    Instruction instances are obtained from Disassembler.get_instructions().
    """
    def __init__(self, ptr, disassembler):
        """Create a new instruction.

        Instructions are created from within this module. You should have no
        need to call this from outside this module.
        """
        assert isinstance(ptr, c_object_p)
        assert isinstance(disassembler, Disassembler)

        LLVMObject.__init__(self, ptr, disposer=lib.EDReleaseInst)
        self._disassembler = disassembler

    def __str__(self):
        s = c_char_p(None)
        result = lib.EDGetInstString(byref(s), self)
        if result != 0:
            raise Exception('Non-0 return code.')

        return s.value

    @CachedProperty
    def byte_size(self):
        result = lib.EDInstByteSize(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result

    @CachedProperty
    def id(self):
        i = c_uint()
        result = lib.EDInstID(byref(i), self)
        if result != 0:
            raise Exception('Non-0 return code.')

        return i.value

    @CachedProperty
    def is_branch(self):
        result = lib.EDInstIsBranch(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def is_move(self):
        result = lib.EDInstIsMove(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def branch_target_id(self):
        result = lib.EDBranchTargetID(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result

    @CachedProperty
    def move_source_id(self):
        result = lib.EDMoveSourceID(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result

    def get_tokens(self):
        """Obtain the tokens in this instruction.

        This is a generator for Token instances.
        """
        count = lib.EDNumTokens(self)
        if count == -1:
            raise Exception('Error code returned.')

        for i in range(0, count):
            ptr = c_object_p()
            result = lib.EDGetToken(byref(ptr), self, c_int(i))
            if result != 0:
                raise Exception('Non-0 return code.')

            yield Token(ptr, self)

    def get_operands(self):
        """Obtain the operands in this instruction.

        This is a generator for Operand instances.
        """
        count = lib.EDNumOperands(self)
        if count == -1:
            raise Exception('Error code returned.')

        for i in range(0, count):
            ptr = c_object_p()
            result = lib.EDGetOperand(byref(ptr), self, c_int(i))
            if result != 0:
                raise Exception('Non-0 return code.')

            yield Operand(ptr, self)

class Token(LLVMObject):
    def __init__(self, ptr, instruction):
        assert isinstance(ptr, c_object_p)
        assert isinstance(instruction, Instruction)

        LLVMObject.__init__(self, ptr)

        self._instruction = instruction

    def __str__(self):
        s = c_char_p(None)
        result = lib.EDGetTokenString(byref(s), self)
        if result != 0:
            raise Exception('Non-0 return code.')

        return s.value

    @CachedProperty
    def operand_index(self):
        result = lib.EDOperandIndexForToken(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result

    @CachedProperty
    def is_whitespace(self):
        result = lib.EDTokenIsWhitespace(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def is_punctuation(self):
        result = lib.EDTokenIsPunctuation(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def is_opcode(self):
        result = lib.EDTokenIsOpcode(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def is_literal(self):
        result = lib.EDTokenIsLiteral(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def is_register(self):
        result = lib.EDTokenIsRegister(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def is_negative_literal(self):
        result = lib.EDTokenIsNegativeLiteral(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def absolute_value(self):
        value = c_uint64()
        result = lib.EDLiteralTokenAbsoluteValue(byref(value), self)
        if result != 0:
            raise Exception('Non-0 return code.')

        return value

    @CachedProperty
    def register_value(self):
        value = c_uint()
        result = lib.EDRegisterTokenValue(byref(value), self)
        if result != 0:
            raise Exception('Non-0 return code.')

        return value

class Operand(LLVMObject):
    """Represents an operand in an instruction.

    FIXME support register evaluation.
    """
    def __init__(self, ptr, instruction):
        assert isinstance(ptr, c_object_p)
        assert isinstance(instruction, Instruction)

        LLVMObject.__init__(self, ptr)

        self._instruction = instruction

    @CachedProperty
    def is_register(self):
        result = lib.EDOperandIsRegister(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def is_immediate(self):
        result = lib.EDOperandIsImmediate(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def is_memory(self):
        result = lib.EDOperandIsMemory(self)
        if result == -1:
            raise Exception('Error code returned.')

        return result > 0

    @CachedProperty
    def register_value(self):
        value = c_uint()
        result = lib.EDRegisterOperandValue(byref(value), self)
        if result != 0:
            raise Exception('Non-0 return code.')

        return value

    @CachedProperty
    def immediate_value(self):
        value = c_uint64()
        result = lib.EDImmediateOperandValue(byref(value), self)
        if result != 0:
            raise Exception('Non-0 return code.')

        return value

def register_library(library):
    library.EDGetDisassembler.argtypes = [POINTER(c_object_p), c_char_p, c_int]
    library.EDGetDisassembler.restype = c_int

    library.EDGetRegisterName.argtypes = [POINTER(c_char_p), Disassembler,
            c_uint]
    library.EDGetRegisterName.restype = c_int

    library.EDRegisterIsStackPointer.argtypes = [Disassembler, c_uint]
    library.EDRegisterIsStackPointer.restype = c_int

    library.EDRegisterIsProgramCounter.argtypes = [Disassembler, c_uint]
    library.EDRegisterIsProgramCounter.restype = c_int

    library.EDCreateInsts.argtypes = [POINTER(c_object_p), c_uint,
            Disassembler, callbacks['byte_reader'], c_uint64, c_void_p]
    library.EDCreateInsts.restype = c_uint

    library.EDReleaseInst.argtypes = [Instruction]

    library.EDInstByteSize.argtypes = [Instruction]
    library.EDInstByteSize.restype = c_int

    library.EDGetInstString.argtypes = [POINTER(c_char_p), Instruction]
    library.EDGetInstString.restype = c_int

    library.EDInstID.argtypes = [POINTER(c_uint), Instruction]
    library.EDInstID.restype = c_int

    library.EDInstIsBranch.argtypes = [Instruction]
    library.EDInstIsBranch.restype = c_int

    library.EDInstIsMove.argtypes = [Instruction]
    library.EDInstIsMove.restype = c_int

    library.EDBranchTargetID.argtypes = [Instruction]
    library.EDBranchTargetID.restype = c_int

    library.EDMoveSourceID.argtypes = [Instruction]
    library.EDMoveSourceID.restype = c_int

    library.EDMoveTargetID.argtypes = [Instruction]
    library.EDMoveTargetID.restype = c_int

    library.EDNumTokens.argtypes = [Instruction]
    library.EDNumTokens.restype = c_int

    library.EDGetToken.argtypes = [POINTER(c_object_p), Instruction, c_int]
    library.EDGetToken.restype = c_int

    library.EDGetTokenString.argtypes = [POINTER(c_char_p), Token]
    library.EDGetTokenString.restype = c_int

    library.EDOperandIndexForToken.argtypes = [Token]
    library.EDOperandIndexForToken.restype = c_int

    library.EDTokenIsWhitespace.argtypes = [Token]
    library.EDTokenIsWhitespace.restype = c_int

    library.EDTokenIsPunctuation.argtypes = [Token]
    library.EDTokenIsPunctuation.restype = c_int

    library.EDTokenIsOpcode.argtypes = [Token]
    library.EDTokenIsOpcode.restype = c_int

    library.EDTokenIsLiteral.argtypes = [Token]
    library.EDTokenIsLiteral.restype = c_int

    library.EDTokenIsRegister.argtypes = [Token]
    library.EDTokenIsRegister.restype = c_int

    library.EDTokenIsNegativeLiteral.argtypes = [Token]
    library.EDTokenIsNegativeLiteral.restype = c_int

    library.EDLiteralTokenAbsoluteValue.argtypes = [POINTER(c_uint64), Token]
    library.EDLiteralTokenAbsoluteValue.restype = c_int

    library.EDRegisterTokenValue.argtypes = [POINTER(c_uint), Token]
    library.EDRegisterTokenValue.restype = c_int

    library.EDNumOperands.argtypes = [Instruction]
    library.EDNumOperands.restype = c_int

    library.EDGetOperand.argtypes = [POINTER(c_object_p), Instruction, c_int]
    library.EDGetOperand.restype = c_int

    library.EDOperandIsRegister.argtypes = [Operand]
    library.EDOperandIsRegister.restype = c_int

    library.EDOperandIsImmediate.argtypes = [Operand]
    library.EDOperandIsImmediate.restype = c_int

    library.EDOperandIsMemory.argtypes = [Operand]
    library.EDOperandIsMemory.restype = c_int

    library.EDRegisterOperandValue.argtypes = [POINTER(c_uint), Operand]
    library.EDRegisterOperandValue.restype = c_int

    library.EDImmediateOperandValue.argtypes = [POINTER(c_uint64), Operand]
    library.EDImmediateOperandValue.restype = c_int

    library.EDEvaluateOperand.argtypes = [c_uint64, Operand,
        callbacks['register_reader'], c_void_p]
    library.EDEvaluateOperand.restype = c_int

# Enhanced disassembler.
callbacks['byte_reader'] = CFUNCTYPE(c_int, POINTER(c_ubyte), c_uint64,
                                     c_void_p)
callbacks['register_reader'] = CFUNCTYPE(c_int, POINTER(c_uint64), c_uint,
                                         c_void_p)

lib = get_library()
register_library(lib)